x86.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/module.h>
  33. #include <linux/mman.h>
  34. #include <linux/highmem.h>
  35. #include <linux/iommu.h>
  36. #include <linux/intel-iommu.h>
  37. #include <linux/cpufreq.h>
  38. #include <linux/user-return-notifier.h>
  39. #include <linux/srcu.h>
  40. #include <linux/slab.h>
  41. #include <trace/events/kvm.h>
  42. #undef TRACE_INCLUDE_FILE
  43. #define CREATE_TRACE_POINTS
  44. #include "trace.h"
  45. #include <asm/debugreg.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/msr.h>
  48. #include <asm/desc.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/mce.h>
  51. #define MAX_IO_MSRS 256
  52. #define CR0_RESERVED_BITS \
  53. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  54. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  55. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  56. #define CR4_RESERVED_BITS \
  57. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  58. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  59. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  60. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  61. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  62. #define KVM_MAX_MCE_BANKS 32
  63. #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
  64. /* EFER defaults:
  65. * - enable syscall per default because its emulated by KVM
  66. * - enable LME and LMA per default on 64 bit KVM
  67. */
  68. #ifdef CONFIG_X86_64
  69. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  70. #else
  71. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  72. #endif
  73. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  74. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  75. static void update_cr8_intercept(struct kvm_vcpu *vcpu);
  76. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  77. struct kvm_cpuid_entry2 __user *entries);
  78. struct kvm_x86_ops *kvm_x86_ops;
  79. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  80. int ignore_msrs = 0;
  81. module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
  82. #define KVM_NR_SHARED_MSRS 16
  83. struct kvm_shared_msrs_global {
  84. int nr;
  85. u32 msrs[KVM_NR_SHARED_MSRS];
  86. };
  87. struct kvm_shared_msrs {
  88. struct user_return_notifier urn;
  89. bool registered;
  90. struct kvm_shared_msr_values {
  91. u64 host;
  92. u64 curr;
  93. } values[KVM_NR_SHARED_MSRS];
  94. };
  95. static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
  96. static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
  97. struct kvm_stats_debugfs_item debugfs_entries[] = {
  98. { "pf_fixed", VCPU_STAT(pf_fixed) },
  99. { "pf_guest", VCPU_STAT(pf_guest) },
  100. { "tlb_flush", VCPU_STAT(tlb_flush) },
  101. { "invlpg", VCPU_STAT(invlpg) },
  102. { "exits", VCPU_STAT(exits) },
  103. { "io_exits", VCPU_STAT(io_exits) },
  104. { "mmio_exits", VCPU_STAT(mmio_exits) },
  105. { "signal_exits", VCPU_STAT(signal_exits) },
  106. { "irq_window", VCPU_STAT(irq_window_exits) },
  107. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  108. { "halt_exits", VCPU_STAT(halt_exits) },
  109. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  110. { "hypercalls", VCPU_STAT(hypercalls) },
  111. { "request_irq", VCPU_STAT(request_irq_exits) },
  112. { "irq_exits", VCPU_STAT(irq_exits) },
  113. { "host_state_reload", VCPU_STAT(host_state_reload) },
  114. { "efer_reload", VCPU_STAT(efer_reload) },
  115. { "fpu_reload", VCPU_STAT(fpu_reload) },
  116. { "insn_emulation", VCPU_STAT(insn_emulation) },
  117. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  118. { "irq_injections", VCPU_STAT(irq_injections) },
  119. { "nmi_injections", VCPU_STAT(nmi_injections) },
  120. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  121. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  122. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  123. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  124. { "mmu_flooded", VM_STAT(mmu_flooded) },
  125. { "mmu_recycled", VM_STAT(mmu_recycled) },
  126. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  127. { "mmu_unsync", VM_STAT(mmu_unsync) },
  128. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  129. { "largepages", VM_STAT(lpages) },
  130. { NULL }
  131. };
  132. static void kvm_on_user_return(struct user_return_notifier *urn)
  133. {
  134. unsigned slot;
  135. struct kvm_shared_msrs *locals
  136. = container_of(urn, struct kvm_shared_msrs, urn);
  137. struct kvm_shared_msr_values *values;
  138. for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
  139. values = &locals->values[slot];
  140. if (values->host != values->curr) {
  141. wrmsrl(shared_msrs_global.msrs[slot], values->host);
  142. values->curr = values->host;
  143. }
  144. }
  145. locals->registered = false;
  146. user_return_notifier_unregister(urn);
  147. }
  148. static void shared_msr_update(unsigned slot, u32 msr)
  149. {
  150. struct kvm_shared_msrs *smsr;
  151. u64 value;
  152. smsr = &__get_cpu_var(shared_msrs);
  153. /* only read, and nobody should modify it at this time,
  154. * so don't need lock */
  155. if (slot >= shared_msrs_global.nr) {
  156. printk(KERN_ERR "kvm: invalid MSR slot!");
  157. return;
  158. }
  159. rdmsrl_safe(msr, &value);
  160. smsr->values[slot].host = value;
  161. smsr->values[slot].curr = value;
  162. }
  163. void kvm_define_shared_msr(unsigned slot, u32 msr)
  164. {
  165. if (slot >= shared_msrs_global.nr)
  166. shared_msrs_global.nr = slot + 1;
  167. shared_msrs_global.msrs[slot] = msr;
  168. /* we need ensured the shared_msr_global have been updated */
  169. smp_wmb();
  170. }
  171. EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
  172. static void kvm_shared_msr_cpu_online(void)
  173. {
  174. unsigned i;
  175. for (i = 0; i < shared_msrs_global.nr; ++i)
  176. shared_msr_update(i, shared_msrs_global.msrs[i]);
  177. }
  178. void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
  179. {
  180. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  181. if (((value ^ smsr->values[slot].curr) & mask) == 0)
  182. return;
  183. smsr->values[slot].curr = value;
  184. wrmsrl(shared_msrs_global.msrs[slot], value);
  185. if (!smsr->registered) {
  186. smsr->urn.on_user_return = kvm_on_user_return;
  187. user_return_notifier_register(&smsr->urn);
  188. smsr->registered = true;
  189. }
  190. }
  191. EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
  192. static void drop_user_return_notifiers(void *ignore)
  193. {
  194. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  195. if (smsr->registered)
  196. kvm_on_user_return(&smsr->urn);
  197. }
  198. unsigned long segment_base(u16 selector)
  199. {
  200. struct descriptor_table gdt;
  201. struct desc_struct *d;
  202. unsigned long table_base;
  203. unsigned long v;
  204. if (selector == 0)
  205. return 0;
  206. kvm_get_gdt(&gdt);
  207. table_base = gdt.base;
  208. if (selector & 4) { /* from ldt */
  209. u16 ldt_selector = kvm_read_ldt();
  210. table_base = segment_base(ldt_selector);
  211. }
  212. d = (struct desc_struct *)(table_base + (selector & ~7));
  213. v = get_desc_base(d);
  214. #ifdef CONFIG_X86_64
  215. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  216. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  217. #endif
  218. return v;
  219. }
  220. EXPORT_SYMBOL_GPL(segment_base);
  221. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  222. {
  223. if (irqchip_in_kernel(vcpu->kvm))
  224. return vcpu->arch.apic_base;
  225. else
  226. return vcpu->arch.apic_base;
  227. }
  228. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  229. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  230. {
  231. /* TODO: reserve bits check */
  232. if (irqchip_in_kernel(vcpu->kvm))
  233. kvm_lapic_set_base(vcpu, data);
  234. else
  235. vcpu->arch.apic_base = data;
  236. }
  237. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  238. #define EXCPT_BENIGN 0
  239. #define EXCPT_CONTRIBUTORY 1
  240. #define EXCPT_PF 2
  241. static int exception_class(int vector)
  242. {
  243. switch (vector) {
  244. case PF_VECTOR:
  245. return EXCPT_PF;
  246. case DE_VECTOR:
  247. case TS_VECTOR:
  248. case NP_VECTOR:
  249. case SS_VECTOR:
  250. case GP_VECTOR:
  251. return EXCPT_CONTRIBUTORY;
  252. default:
  253. break;
  254. }
  255. return EXCPT_BENIGN;
  256. }
  257. static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
  258. unsigned nr, bool has_error, u32 error_code)
  259. {
  260. u32 prev_nr;
  261. int class1, class2;
  262. if (!vcpu->arch.exception.pending) {
  263. queue:
  264. vcpu->arch.exception.pending = true;
  265. vcpu->arch.exception.has_error_code = has_error;
  266. vcpu->arch.exception.nr = nr;
  267. vcpu->arch.exception.error_code = error_code;
  268. return;
  269. }
  270. /* to check exception */
  271. prev_nr = vcpu->arch.exception.nr;
  272. if (prev_nr == DF_VECTOR) {
  273. /* triple fault -> shutdown */
  274. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  275. return;
  276. }
  277. class1 = exception_class(prev_nr);
  278. class2 = exception_class(nr);
  279. if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
  280. || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
  281. /* generate double fault per SDM Table 5-5 */
  282. vcpu->arch.exception.pending = true;
  283. vcpu->arch.exception.has_error_code = true;
  284. vcpu->arch.exception.nr = DF_VECTOR;
  285. vcpu->arch.exception.error_code = 0;
  286. } else
  287. /* replace previous exception with a new one in a hope
  288. that instruction re-execution will regenerate lost
  289. exception */
  290. goto queue;
  291. }
  292. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  293. {
  294. kvm_multiple_exception(vcpu, nr, false, 0);
  295. }
  296. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  297. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  298. u32 error_code)
  299. {
  300. ++vcpu->stat.pf_guest;
  301. vcpu->arch.cr2 = addr;
  302. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  303. }
  304. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  305. {
  306. vcpu->arch.nmi_pending = 1;
  307. }
  308. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  309. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  310. {
  311. kvm_multiple_exception(vcpu, nr, true, error_code);
  312. }
  313. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  314. /*
  315. * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
  316. * a #GP and return false.
  317. */
  318. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
  319. {
  320. if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
  321. return true;
  322. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  323. return false;
  324. }
  325. EXPORT_SYMBOL_GPL(kvm_require_cpl);
  326. /*
  327. * Load the pae pdptrs. Return true is they are all valid.
  328. */
  329. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  330. {
  331. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  332. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  333. int i;
  334. int ret;
  335. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  336. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  337. offset * sizeof(u64), sizeof(pdpte));
  338. if (ret < 0) {
  339. ret = 0;
  340. goto out;
  341. }
  342. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  343. if (is_present_gpte(pdpte[i]) &&
  344. (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
  345. ret = 0;
  346. goto out;
  347. }
  348. }
  349. ret = 1;
  350. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  351. __set_bit(VCPU_EXREG_PDPTR,
  352. (unsigned long *)&vcpu->arch.regs_avail);
  353. __set_bit(VCPU_EXREG_PDPTR,
  354. (unsigned long *)&vcpu->arch.regs_dirty);
  355. out:
  356. return ret;
  357. }
  358. EXPORT_SYMBOL_GPL(load_pdptrs);
  359. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  360. {
  361. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  362. bool changed = true;
  363. int r;
  364. if (is_long_mode(vcpu) || !is_pae(vcpu))
  365. return false;
  366. if (!test_bit(VCPU_EXREG_PDPTR,
  367. (unsigned long *)&vcpu->arch.regs_avail))
  368. return true;
  369. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  370. if (r < 0)
  371. goto out;
  372. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  373. out:
  374. return changed;
  375. }
  376. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  377. {
  378. cr0 |= X86_CR0_ET;
  379. #ifdef CONFIG_X86_64
  380. if (cr0 & 0xffffffff00000000UL) {
  381. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  382. cr0, kvm_read_cr0(vcpu));
  383. kvm_inject_gp(vcpu, 0);
  384. return;
  385. }
  386. #endif
  387. cr0 &= ~CR0_RESERVED_BITS;
  388. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  389. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  390. kvm_inject_gp(vcpu, 0);
  391. return;
  392. }
  393. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  394. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  395. "and a clear PE flag\n");
  396. kvm_inject_gp(vcpu, 0);
  397. return;
  398. }
  399. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  400. #ifdef CONFIG_X86_64
  401. if ((vcpu->arch.efer & EFER_LME)) {
  402. int cs_db, cs_l;
  403. if (!is_pae(vcpu)) {
  404. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  405. "in long mode while PAE is disabled\n");
  406. kvm_inject_gp(vcpu, 0);
  407. return;
  408. }
  409. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  410. if (cs_l) {
  411. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  412. "in long mode while CS.L == 1\n");
  413. kvm_inject_gp(vcpu, 0);
  414. return;
  415. }
  416. } else
  417. #endif
  418. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  419. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  420. "reserved bits\n");
  421. kvm_inject_gp(vcpu, 0);
  422. return;
  423. }
  424. }
  425. kvm_x86_ops->set_cr0(vcpu, cr0);
  426. vcpu->arch.cr0 = cr0;
  427. kvm_mmu_reset_context(vcpu);
  428. return;
  429. }
  430. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  431. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  432. {
  433. kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
  434. }
  435. EXPORT_SYMBOL_GPL(kvm_lmsw);
  436. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  437. {
  438. unsigned long old_cr4 = kvm_read_cr4(vcpu);
  439. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
  440. if (cr4 & CR4_RESERVED_BITS) {
  441. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  442. kvm_inject_gp(vcpu, 0);
  443. return;
  444. }
  445. if (is_long_mode(vcpu)) {
  446. if (!(cr4 & X86_CR4_PAE)) {
  447. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  448. "in long mode\n");
  449. kvm_inject_gp(vcpu, 0);
  450. return;
  451. }
  452. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  453. && ((cr4 ^ old_cr4) & pdptr_bits)
  454. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  455. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  456. kvm_inject_gp(vcpu, 0);
  457. return;
  458. }
  459. if (cr4 & X86_CR4_VMXE) {
  460. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  461. kvm_inject_gp(vcpu, 0);
  462. return;
  463. }
  464. kvm_x86_ops->set_cr4(vcpu, cr4);
  465. vcpu->arch.cr4 = cr4;
  466. vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
  467. kvm_mmu_reset_context(vcpu);
  468. }
  469. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  470. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  471. {
  472. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  473. kvm_mmu_sync_roots(vcpu);
  474. kvm_mmu_flush_tlb(vcpu);
  475. return;
  476. }
  477. if (is_long_mode(vcpu)) {
  478. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  479. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  480. kvm_inject_gp(vcpu, 0);
  481. return;
  482. }
  483. } else {
  484. if (is_pae(vcpu)) {
  485. if (cr3 & CR3_PAE_RESERVED_BITS) {
  486. printk(KERN_DEBUG
  487. "set_cr3: #GP, reserved bits\n");
  488. kvm_inject_gp(vcpu, 0);
  489. return;
  490. }
  491. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  492. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  493. "reserved bits\n");
  494. kvm_inject_gp(vcpu, 0);
  495. return;
  496. }
  497. }
  498. /*
  499. * We don't check reserved bits in nonpae mode, because
  500. * this isn't enforced, and VMware depends on this.
  501. */
  502. }
  503. /*
  504. * Does the new cr3 value map to physical memory? (Note, we
  505. * catch an invalid cr3 even in real-mode, because it would
  506. * cause trouble later on when we turn on paging anyway.)
  507. *
  508. * A real CPU would silently accept an invalid cr3 and would
  509. * attempt to use it - with largely undefined (and often hard
  510. * to debug) behavior on the guest side.
  511. */
  512. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  513. kvm_inject_gp(vcpu, 0);
  514. else {
  515. vcpu->arch.cr3 = cr3;
  516. vcpu->arch.mmu.new_cr3(vcpu);
  517. }
  518. }
  519. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  520. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  521. {
  522. if (cr8 & CR8_RESERVED_BITS) {
  523. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  524. kvm_inject_gp(vcpu, 0);
  525. return;
  526. }
  527. if (irqchip_in_kernel(vcpu->kvm))
  528. kvm_lapic_set_tpr(vcpu, cr8);
  529. else
  530. vcpu->arch.cr8 = cr8;
  531. }
  532. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  533. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  534. {
  535. if (irqchip_in_kernel(vcpu->kvm))
  536. return kvm_lapic_get_cr8(vcpu);
  537. else
  538. return vcpu->arch.cr8;
  539. }
  540. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  541. static inline u32 bit(int bitno)
  542. {
  543. return 1 << (bitno & 31);
  544. }
  545. /*
  546. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  547. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  548. *
  549. * This list is modified at module load time to reflect the
  550. * capabilities of the host cpu. This capabilities test skips MSRs that are
  551. * kvm-specific. Those are put in the beginning of the list.
  552. */
  553. #define KVM_SAVE_MSRS_BEGIN 5
  554. static u32 msrs_to_save[] = {
  555. MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  556. HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
  557. HV_X64_MSR_APIC_ASSIST_PAGE,
  558. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  559. MSR_K6_STAR,
  560. #ifdef CONFIG_X86_64
  561. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  562. #endif
  563. MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
  564. };
  565. static unsigned num_msrs_to_save;
  566. static u32 emulated_msrs[] = {
  567. MSR_IA32_MISC_ENABLE,
  568. };
  569. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  570. {
  571. if (efer & efer_reserved_bits) {
  572. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  573. efer);
  574. kvm_inject_gp(vcpu, 0);
  575. return;
  576. }
  577. if (is_paging(vcpu)
  578. && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) {
  579. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  580. kvm_inject_gp(vcpu, 0);
  581. return;
  582. }
  583. if (efer & EFER_FFXSR) {
  584. struct kvm_cpuid_entry2 *feat;
  585. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  586. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
  587. printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
  588. kvm_inject_gp(vcpu, 0);
  589. return;
  590. }
  591. }
  592. if (efer & EFER_SVME) {
  593. struct kvm_cpuid_entry2 *feat;
  594. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  595. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
  596. printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
  597. kvm_inject_gp(vcpu, 0);
  598. return;
  599. }
  600. }
  601. kvm_x86_ops->set_efer(vcpu, efer);
  602. efer &= ~EFER_LMA;
  603. efer |= vcpu->arch.efer & EFER_LMA;
  604. vcpu->arch.efer = efer;
  605. vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
  606. kvm_mmu_reset_context(vcpu);
  607. }
  608. void kvm_enable_efer_bits(u64 mask)
  609. {
  610. efer_reserved_bits &= ~mask;
  611. }
  612. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  613. /*
  614. * Writes msr value into into the appropriate "register".
  615. * Returns 0 on success, non-0 otherwise.
  616. * Assumes vcpu_load() was already called.
  617. */
  618. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  619. {
  620. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  621. }
  622. /*
  623. * Adapt set_msr() to msr_io()'s calling convention
  624. */
  625. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  626. {
  627. return kvm_set_msr(vcpu, index, *data);
  628. }
  629. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  630. {
  631. static int version;
  632. struct pvclock_wall_clock wc;
  633. struct timespec boot;
  634. if (!wall_clock)
  635. return;
  636. version++;
  637. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  638. /*
  639. * The guest calculates current wall clock time by adding
  640. * system time (updated by kvm_write_guest_time below) to the
  641. * wall clock specified here. guest system time equals host
  642. * system time for us, thus we must fill in host boot time here.
  643. */
  644. getboottime(&boot);
  645. wc.sec = boot.tv_sec;
  646. wc.nsec = boot.tv_nsec;
  647. wc.version = version;
  648. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  649. version++;
  650. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  651. }
  652. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  653. {
  654. uint32_t quotient, remainder;
  655. /* Don't try to replace with do_div(), this one calculates
  656. * "(dividend << 32) / divisor" */
  657. __asm__ ( "divl %4"
  658. : "=a" (quotient), "=d" (remainder)
  659. : "0" (0), "1" (dividend), "r" (divisor) );
  660. return quotient;
  661. }
  662. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  663. {
  664. uint64_t nsecs = 1000000000LL;
  665. int32_t shift = 0;
  666. uint64_t tps64;
  667. uint32_t tps32;
  668. tps64 = tsc_khz * 1000LL;
  669. while (tps64 > nsecs*2) {
  670. tps64 >>= 1;
  671. shift--;
  672. }
  673. tps32 = (uint32_t)tps64;
  674. while (tps32 <= (uint32_t)nsecs) {
  675. tps32 <<= 1;
  676. shift++;
  677. }
  678. hv_clock->tsc_shift = shift;
  679. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  680. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  681. __func__, tsc_khz, hv_clock->tsc_shift,
  682. hv_clock->tsc_to_system_mul);
  683. }
  684. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  685. static void kvm_write_guest_time(struct kvm_vcpu *v)
  686. {
  687. struct timespec ts;
  688. unsigned long flags;
  689. struct kvm_vcpu_arch *vcpu = &v->arch;
  690. void *shared_kaddr;
  691. unsigned long this_tsc_khz;
  692. if ((!vcpu->time_page))
  693. return;
  694. this_tsc_khz = get_cpu_var(cpu_tsc_khz);
  695. if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
  696. kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
  697. vcpu->hv_clock_tsc_khz = this_tsc_khz;
  698. }
  699. put_cpu_var(cpu_tsc_khz);
  700. /* Keep irq disabled to prevent changes to the clock */
  701. local_irq_save(flags);
  702. kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
  703. ktime_get_ts(&ts);
  704. monotonic_to_bootbased(&ts);
  705. local_irq_restore(flags);
  706. /* With all the info we got, fill in the values */
  707. vcpu->hv_clock.system_time = ts.tv_nsec +
  708. (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
  709. /*
  710. * The interface expects us to write an even number signaling that the
  711. * update is finished. Since the guest won't see the intermediate
  712. * state, we just increase by 2 at the end.
  713. */
  714. vcpu->hv_clock.version += 2;
  715. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  716. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  717. sizeof(vcpu->hv_clock));
  718. kunmap_atomic(shared_kaddr, KM_USER0);
  719. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  720. }
  721. static int kvm_request_guest_time_update(struct kvm_vcpu *v)
  722. {
  723. struct kvm_vcpu_arch *vcpu = &v->arch;
  724. if (!vcpu->time_page)
  725. return 0;
  726. set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
  727. return 1;
  728. }
  729. static bool msr_mtrr_valid(unsigned msr)
  730. {
  731. switch (msr) {
  732. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  733. case MSR_MTRRfix64K_00000:
  734. case MSR_MTRRfix16K_80000:
  735. case MSR_MTRRfix16K_A0000:
  736. case MSR_MTRRfix4K_C0000:
  737. case MSR_MTRRfix4K_C8000:
  738. case MSR_MTRRfix4K_D0000:
  739. case MSR_MTRRfix4K_D8000:
  740. case MSR_MTRRfix4K_E0000:
  741. case MSR_MTRRfix4K_E8000:
  742. case MSR_MTRRfix4K_F0000:
  743. case MSR_MTRRfix4K_F8000:
  744. case MSR_MTRRdefType:
  745. case MSR_IA32_CR_PAT:
  746. return true;
  747. case 0x2f8:
  748. return true;
  749. }
  750. return false;
  751. }
  752. static bool valid_pat_type(unsigned t)
  753. {
  754. return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
  755. }
  756. static bool valid_mtrr_type(unsigned t)
  757. {
  758. return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
  759. }
  760. static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  761. {
  762. int i;
  763. if (!msr_mtrr_valid(msr))
  764. return false;
  765. if (msr == MSR_IA32_CR_PAT) {
  766. for (i = 0; i < 8; i++)
  767. if (!valid_pat_type((data >> (i * 8)) & 0xff))
  768. return false;
  769. return true;
  770. } else if (msr == MSR_MTRRdefType) {
  771. if (data & ~0xcff)
  772. return false;
  773. return valid_mtrr_type(data & 0xff);
  774. } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
  775. for (i = 0; i < 8 ; i++)
  776. if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
  777. return false;
  778. return true;
  779. }
  780. /* variable MTRRs */
  781. return valid_mtrr_type(data & 0xff);
  782. }
  783. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  784. {
  785. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  786. if (!mtrr_valid(vcpu, msr, data))
  787. return 1;
  788. if (msr == MSR_MTRRdefType) {
  789. vcpu->arch.mtrr_state.def_type = data;
  790. vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
  791. } else if (msr == MSR_MTRRfix64K_00000)
  792. p[0] = data;
  793. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  794. p[1 + msr - MSR_MTRRfix16K_80000] = data;
  795. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  796. p[3 + msr - MSR_MTRRfix4K_C0000] = data;
  797. else if (msr == MSR_IA32_CR_PAT)
  798. vcpu->arch.pat = data;
  799. else { /* Variable MTRRs */
  800. int idx, is_mtrr_mask;
  801. u64 *pt;
  802. idx = (msr - 0x200) / 2;
  803. is_mtrr_mask = msr - 0x200 - 2 * idx;
  804. if (!is_mtrr_mask)
  805. pt =
  806. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  807. else
  808. pt =
  809. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  810. *pt = data;
  811. }
  812. kvm_mmu_reset_context(vcpu);
  813. return 0;
  814. }
  815. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  816. {
  817. u64 mcg_cap = vcpu->arch.mcg_cap;
  818. unsigned bank_num = mcg_cap & 0xff;
  819. switch (msr) {
  820. case MSR_IA32_MCG_STATUS:
  821. vcpu->arch.mcg_status = data;
  822. break;
  823. case MSR_IA32_MCG_CTL:
  824. if (!(mcg_cap & MCG_CTL_P))
  825. return 1;
  826. if (data != 0 && data != ~(u64)0)
  827. return -1;
  828. vcpu->arch.mcg_ctl = data;
  829. break;
  830. default:
  831. if (msr >= MSR_IA32_MC0_CTL &&
  832. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  833. u32 offset = msr - MSR_IA32_MC0_CTL;
  834. /* only 0 or all 1s can be written to IA32_MCi_CTL */
  835. if ((offset & 0x3) == 0 &&
  836. data != 0 && data != ~(u64)0)
  837. return -1;
  838. vcpu->arch.mce_banks[offset] = data;
  839. break;
  840. }
  841. return 1;
  842. }
  843. return 0;
  844. }
  845. static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
  846. {
  847. struct kvm *kvm = vcpu->kvm;
  848. int lm = is_long_mode(vcpu);
  849. u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
  850. : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
  851. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  852. : kvm->arch.xen_hvm_config.blob_size_32;
  853. u32 page_num = data & ~PAGE_MASK;
  854. u64 page_addr = data & PAGE_MASK;
  855. u8 *page;
  856. int r;
  857. r = -E2BIG;
  858. if (page_num >= blob_size)
  859. goto out;
  860. r = -ENOMEM;
  861. page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  862. if (!page)
  863. goto out;
  864. r = -EFAULT;
  865. if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
  866. goto out_free;
  867. if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
  868. goto out_free;
  869. r = 0;
  870. out_free:
  871. kfree(page);
  872. out:
  873. return r;
  874. }
  875. static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  876. {
  877. return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  878. }
  879. static bool kvm_hv_msr_partition_wide(u32 msr)
  880. {
  881. bool r = false;
  882. switch (msr) {
  883. case HV_X64_MSR_GUEST_OS_ID:
  884. case HV_X64_MSR_HYPERCALL:
  885. r = true;
  886. break;
  887. }
  888. return r;
  889. }
  890. static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  891. {
  892. struct kvm *kvm = vcpu->kvm;
  893. switch (msr) {
  894. case HV_X64_MSR_GUEST_OS_ID:
  895. kvm->arch.hv_guest_os_id = data;
  896. /* setting guest os id to zero disables hypercall page */
  897. if (!kvm->arch.hv_guest_os_id)
  898. kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  899. break;
  900. case HV_X64_MSR_HYPERCALL: {
  901. u64 gfn;
  902. unsigned long addr;
  903. u8 instructions[4];
  904. /* if guest os id is not set hypercall should remain disabled */
  905. if (!kvm->arch.hv_guest_os_id)
  906. break;
  907. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  908. kvm->arch.hv_hypercall = data;
  909. break;
  910. }
  911. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  912. addr = gfn_to_hva(kvm, gfn);
  913. if (kvm_is_error_hva(addr))
  914. return 1;
  915. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  916. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  917. if (copy_to_user((void __user *)addr, instructions, 4))
  918. return 1;
  919. kvm->arch.hv_hypercall = data;
  920. break;
  921. }
  922. default:
  923. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  924. "data 0x%llx\n", msr, data);
  925. return 1;
  926. }
  927. return 0;
  928. }
  929. static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  930. {
  931. switch (msr) {
  932. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  933. unsigned long addr;
  934. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  935. vcpu->arch.hv_vapic = data;
  936. break;
  937. }
  938. addr = gfn_to_hva(vcpu->kvm, data >>
  939. HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
  940. if (kvm_is_error_hva(addr))
  941. return 1;
  942. if (clear_user((void __user *)addr, PAGE_SIZE))
  943. return 1;
  944. vcpu->arch.hv_vapic = data;
  945. break;
  946. }
  947. case HV_X64_MSR_EOI:
  948. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  949. case HV_X64_MSR_ICR:
  950. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  951. case HV_X64_MSR_TPR:
  952. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  953. default:
  954. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  955. "data 0x%llx\n", msr, data);
  956. return 1;
  957. }
  958. return 0;
  959. }
  960. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  961. {
  962. switch (msr) {
  963. case MSR_EFER:
  964. set_efer(vcpu, data);
  965. break;
  966. case MSR_K7_HWCR:
  967. data &= ~(u64)0x40; /* ignore flush filter disable */
  968. if (data != 0) {
  969. pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  970. data);
  971. return 1;
  972. }
  973. break;
  974. case MSR_FAM10H_MMIO_CONF_BASE:
  975. if (data != 0) {
  976. pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  977. "0x%llx\n", data);
  978. return 1;
  979. }
  980. break;
  981. case MSR_AMD64_NB_CFG:
  982. break;
  983. case MSR_IA32_DEBUGCTLMSR:
  984. if (!data) {
  985. /* We support the non-activated case already */
  986. break;
  987. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  988. /* Values other than LBR and BTF are vendor-specific,
  989. thus reserved and should throw a #GP */
  990. return 1;
  991. }
  992. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  993. __func__, data);
  994. break;
  995. case MSR_IA32_UCODE_REV:
  996. case MSR_IA32_UCODE_WRITE:
  997. case MSR_VM_HSAVE_PA:
  998. case MSR_AMD64_PATCH_LOADER:
  999. break;
  1000. case 0x200 ... 0x2ff:
  1001. return set_msr_mtrr(vcpu, msr, data);
  1002. case MSR_IA32_APICBASE:
  1003. kvm_set_apic_base(vcpu, data);
  1004. break;
  1005. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1006. return kvm_x2apic_msr_write(vcpu, msr, data);
  1007. case MSR_IA32_MISC_ENABLE:
  1008. vcpu->arch.ia32_misc_enable_msr = data;
  1009. break;
  1010. case MSR_KVM_WALL_CLOCK:
  1011. vcpu->kvm->arch.wall_clock = data;
  1012. kvm_write_wall_clock(vcpu->kvm, data);
  1013. break;
  1014. case MSR_KVM_SYSTEM_TIME: {
  1015. if (vcpu->arch.time_page) {
  1016. kvm_release_page_dirty(vcpu->arch.time_page);
  1017. vcpu->arch.time_page = NULL;
  1018. }
  1019. vcpu->arch.time = data;
  1020. /* we verify if the enable bit is set... */
  1021. if (!(data & 1))
  1022. break;
  1023. /* ...but clean it before doing the actual write */
  1024. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  1025. vcpu->arch.time_page =
  1026. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  1027. if (is_error_page(vcpu->arch.time_page)) {
  1028. kvm_release_page_clean(vcpu->arch.time_page);
  1029. vcpu->arch.time_page = NULL;
  1030. }
  1031. kvm_request_guest_time_update(vcpu);
  1032. break;
  1033. }
  1034. case MSR_IA32_MCG_CTL:
  1035. case MSR_IA32_MCG_STATUS:
  1036. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1037. return set_msr_mce(vcpu, msr, data);
  1038. /* Performance counters are not protected by a CPUID bit,
  1039. * so we should check all of them in the generic path for the sake of
  1040. * cross vendor migration.
  1041. * Writing a zero into the event select MSRs disables them,
  1042. * which we perfectly emulate ;-). Any other value should be at least
  1043. * reported, some guests depend on them.
  1044. */
  1045. case MSR_P6_EVNTSEL0:
  1046. case MSR_P6_EVNTSEL1:
  1047. case MSR_K7_EVNTSEL0:
  1048. case MSR_K7_EVNTSEL1:
  1049. case MSR_K7_EVNTSEL2:
  1050. case MSR_K7_EVNTSEL3:
  1051. if (data != 0)
  1052. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1053. "0x%x data 0x%llx\n", msr, data);
  1054. break;
  1055. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  1056. * so we ignore writes to make it happy.
  1057. */
  1058. case MSR_P6_PERFCTR0:
  1059. case MSR_P6_PERFCTR1:
  1060. case MSR_K7_PERFCTR0:
  1061. case MSR_K7_PERFCTR1:
  1062. case MSR_K7_PERFCTR2:
  1063. case MSR_K7_PERFCTR3:
  1064. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1065. "0x%x data 0x%llx\n", msr, data);
  1066. break;
  1067. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1068. if (kvm_hv_msr_partition_wide(msr)) {
  1069. int r;
  1070. mutex_lock(&vcpu->kvm->lock);
  1071. r = set_msr_hyperv_pw(vcpu, msr, data);
  1072. mutex_unlock(&vcpu->kvm->lock);
  1073. return r;
  1074. } else
  1075. return set_msr_hyperv(vcpu, msr, data);
  1076. break;
  1077. default:
  1078. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1079. return xen_hvm_config(vcpu, data);
  1080. if (!ignore_msrs) {
  1081. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  1082. msr, data);
  1083. return 1;
  1084. } else {
  1085. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  1086. msr, data);
  1087. break;
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1093. /*
  1094. * Reads an msr value (of 'msr_index') into 'pdata'.
  1095. * Returns 0 on success, non-0 otherwise.
  1096. * Assumes vcpu_load() was already called.
  1097. */
  1098. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1099. {
  1100. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1101. }
  1102. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1103. {
  1104. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1105. if (!msr_mtrr_valid(msr))
  1106. return 1;
  1107. if (msr == MSR_MTRRdefType)
  1108. *pdata = vcpu->arch.mtrr_state.def_type +
  1109. (vcpu->arch.mtrr_state.enabled << 10);
  1110. else if (msr == MSR_MTRRfix64K_00000)
  1111. *pdata = p[0];
  1112. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1113. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  1114. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1115. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  1116. else if (msr == MSR_IA32_CR_PAT)
  1117. *pdata = vcpu->arch.pat;
  1118. else { /* Variable MTRRs */
  1119. int idx, is_mtrr_mask;
  1120. u64 *pt;
  1121. idx = (msr - 0x200) / 2;
  1122. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1123. if (!is_mtrr_mask)
  1124. pt =
  1125. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1126. else
  1127. pt =
  1128. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1129. *pdata = *pt;
  1130. }
  1131. return 0;
  1132. }
  1133. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1134. {
  1135. u64 data;
  1136. u64 mcg_cap = vcpu->arch.mcg_cap;
  1137. unsigned bank_num = mcg_cap & 0xff;
  1138. switch (msr) {
  1139. case MSR_IA32_P5_MC_ADDR:
  1140. case MSR_IA32_P5_MC_TYPE:
  1141. data = 0;
  1142. break;
  1143. case MSR_IA32_MCG_CAP:
  1144. data = vcpu->arch.mcg_cap;
  1145. break;
  1146. case MSR_IA32_MCG_CTL:
  1147. if (!(mcg_cap & MCG_CTL_P))
  1148. return 1;
  1149. data = vcpu->arch.mcg_ctl;
  1150. break;
  1151. case MSR_IA32_MCG_STATUS:
  1152. data = vcpu->arch.mcg_status;
  1153. break;
  1154. default:
  1155. if (msr >= MSR_IA32_MC0_CTL &&
  1156. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1157. u32 offset = msr - MSR_IA32_MC0_CTL;
  1158. data = vcpu->arch.mce_banks[offset];
  1159. break;
  1160. }
  1161. return 1;
  1162. }
  1163. *pdata = data;
  1164. return 0;
  1165. }
  1166. static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1167. {
  1168. u64 data = 0;
  1169. struct kvm *kvm = vcpu->kvm;
  1170. switch (msr) {
  1171. case HV_X64_MSR_GUEST_OS_ID:
  1172. data = kvm->arch.hv_guest_os_id;
  1173. break;
  1174. case HV_X64_MSR_HYPERCALL:
  1175. data = kvm->arch.hv_hypercall;
  1176. break;
  1177. default:
  1178. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1179. return 1;
  1180. }
  1181. *pdata = data;
  1182. return 0;
  1183. }
  1184. static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1185. {
  1186. u64 data = 0;
  1187. switch (msr) {
  1188. case HV_X64_MSR_VP_INDEX: {
  1189. int r;
  1190. struct kvm_vcpu *v;
  1191. kvm_for_each_vcpu(r, v, vcpu->kvm)
  1192. if (v == vcpu)
  1193. data = r;
  1194. break;
  1195. }
  1196. case HV_X64_MSR_EOI:
  1197. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  1198. case HV_X64_MSR_ICR:
  1199. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  1200. case HV_X64_MSR_TPR:
  1201. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  1202. default:
  1203. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1204. return 1;
  1205. }
  1206. *pdata = data;
  1207. return 0;
  1208. }
  1209. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1210. {
  1211. u64 data;
  1212. switch (msr) {
  1213. case MSR_IA32_PLATFORM_ID:
  1214. case MSR_IA32_UCODE_REV:
  1215. case MSR_IA32_EBL_CR_POWERON:
  1216. case MSR_IA32_DEBUGCTLMSR:
  1217. case MSR_IA32_LASTBRANCHFROMIP:
  1218. case MSR_IA32_LASTBRANCHTOIP:
  1219. case MSR_IA32_LASTINTFROMIP:
  1220. case MSR_IA32_LASTINTTOIP:
  1221. case MSR_K8_SYSCFG:
  1222. case MSR_K7_HWCR:
  1223. case MSR_VM_HSAVE_PA:
  1224. case MSR_P6_PERFCTR0:
  1225. case MSR_P6_PERFCTR1:
  1226. case MSR_P6_EVNTSEL0:
  1227. case MSR_P6_EVNTSEL1:
  1228. case MSR_K7_EVNTSEL0:
  1229. case MSR_K7_PERFCTR0:
  1230. case MSR_K8_INT_PENDING_MSG:
  1231. case MSR_AMD64_NB_CFG:
  1232. case MSR_FAM10H_MMIO_CONF_BASE:
  1233. data = 0;
  1234. break;
  1235. case MSR_MTRRcap:
  1236. data = 0x500 | KVM_NR_VAR_MTRR;
  1237. break;
  1238. case 0x200 ... 0x2ff:
  1239. return get_msr_mtrr(vcpu, msr, pdata);
  1240. case 0xcd: /* fsb frequency */
  1241. data = 3;
  1242. break;
  1243. case MSR_IA32_APICBASE:
  1244. data = kvm_get_apic_base(vcpu);
  1245. break;
  1246. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1247. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  1248. break;
  1249. case MSR_IA32_MISC_ENABLE:
  1250. data = vcpu->arch.ia32_misc_enable_msr;
  1251. break;
  1252. case MSR_IA32_PERF_STATUS:
  1253. /* TSC increment by tick */
  1254. data = 1000ULL;
  1255. /* CPU multiplier */
  1256. data |= (((uint64_t)4ULL) << 40);
  1257. break;
  1258. case MSR_EFER:
  1259. data = vcpu->arch.efer;
  1260. break;
  1261. case MSR_KVM_WALL_CLOCK:
  1262. data = vcpu->kvm->arch.wall_clock;
  1263. break;
  1264. case MSR_KVM_SYSTEM_TIME:
  1265. data = vcpu->arch.time;
  1266. break;
  1267. case MSR_IA32_P5_MC_ADDR:
  1268. case MSR_IA32_P5_MC_TYPE:
  1269. case MSR_IA32_MCG_CAP:
  1270. case MSR_IA32_MCG_CTL:
  1271. case MSR_IA32_MCG_STATUS:
  1272. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1273. return get_msr_mce(vcpu, msr, pdata);
  1274. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1275. if (kvm_hv_msr_partition_wide(msr)) {
  1276. int r;
  1277. mutex_lock(&vcpu->kvm->lock);
  1278. r = get_msr_hyperv_pw(vcpu, msr, pdata);
  1279. mutex_unlock(&vcpu->kvm->lock);
  1280. return r;
  1281. } else
  1282. return get_msr_hyperv(vcpu, msr, pdata);
  1283. break;
  1284. default:
  1285. if (!ignore_msrs) {
  1286. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  1287. return 1;
  1288. } else {
  1289. pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  1290. data = 0;
  1291. }
  1292. break;
  1293. }
  1294. *pdata = data;
  1295. return 0;
  1296. }
  1297. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1298. /*
  1299. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1300. *
  1301. * @return number of msrs set successfully.
  1302. */
  1303. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1304. struct kvm_msr_entry *entries,
  1305. int (*do_msr)(struct kvm_vcpu *vcpu,
  1306. unsigned index, u64 *data))
  1307. {
  1308. int i, idx;
  1309. vcpu_load(vcpu);
  1310. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1311. for (i = 0; i < msrs->nmsrs; ++i)
  1312. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1313. break;
  1314. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1315. vcpu_put(vcpu);
  1316. return i;
  1317. }
  1318. /*
  1319. * Read or write a bunch of msrs. Parameters are user addresses.
  1320. *
  1321. * @return number of msrs set successfully.
  1322. */
  1323. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1324. int (*do_msr)(struct kvm_vcpu *vcpu,
  1325. unsigned index, u64 *data),
  1326. int writeback)
  1327. {
  1328. struct kvm_msrs msrs;
  1329. struct kvm_msr_entry *entries;
  1330. int r, n;
  1331. unsigned size;
  1332. r = -EFAULT;
  1333. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1334. goto out;
  1335. r = -E2BIG;
  1336. if (msrs.nmsrs >= MAX_IO_MSRS)
  1337. goto out;
  1338. r = -ENOMEM;
  1339. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1340. entries = vmalloc(size);
  1341. if (!entries)
  1342. goto out;
  1343. r = -EFAULT;
  1344. if (copy_from_user(entries, user_msrs->entries, size))
  1345. goto out_free;
  1346. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1347. if (r < 0)
  1348. goto out_free;
  1349. r = -EFAULT;
  1350. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1351. goto out_free;
  1352. r = n;
  1353. out_free:
  1354. vfree(entries);
  1355. out:
  1356. return r;
  1357. }
  1358. int kvm_dev_ioctl_check_extension(long ext)
  1359. {
  1360. int r;
  1361. switch (ext) {
  1362. case KVM_CAP_IRQCHIP:
  1363. case KVM_CAP_HLT:
  1364. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  1365. case KVM_CAP_SET_TSS_ADDR:
  1366. case KVM_CAP_EXT_CPUID:
  1367. case KVM_CAP_CLOCKSOURCE:
  1368. case KVM_CAP_PIT:
  1369. case KVM_CAP_NOP_IO_DELAY:
  1370. case KVM_CAP_MP_STATE:
  1371. case KVM_CAP_SYNC_MMU:
  1372. case KVM_CAP_REINJECT_CONTROL:
  1373. case KVM_CAP_IRQ_INJECT_STATUS:
  1374. case KVM_CAP_ASSIGN_DEV_IRQ:
  1375. case KVM_CAP_IRQFD:
  1376. case KVM_CAP_IOEVENTFD:
  1377. case KVM_CAP_PIT2:
  1378. case KVM_CAP_PIT_STATE2:
  1379. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  1380. case KVM_CAP_XEN_HVM:
  1381. case KVM_CAP_ADJUST_CLOCK:
  1382. case KVM_CAP_VCPU_EVENTS:
  1383. case KVM_CAP_HYPERV:
  1384. case KVM_CAP_HYPERV_VAPIC:
  1385. case KVM_CAP_HYPERV_SPIN:
  1386. case KVM_CAP_PCI_SEGMENT:
  1387. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  1388. r = 1;
  1389. break;
  1390. case KVM_CAP_COALESCED_MMIO:
  1391. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1392. break;
  1393. case KVM_CAP_VAPIC:
  1394. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1395. break;
  1396. case KVM_CAP_NR_VCPUS:
  1397. r = KVM_MAX_VCPUS;
  1398. break;
  1399. case KVM_CAP_NR_MEMSLOTS:
  1400. r = KVM_MEMORY_SLOTS;
  1401. break;
  1402. case KVM_CAP_PV_MMU: /* obsolete */
  1403. r = 0;
  1404. break;
  1405. case KVM_CAP_IOMMU:
  1406. r = iommu_found();
  1407. break;
  1408. case KVM_CAP_MCE:
  1409. r = KVM_MAX_MCE_BANKS;
  1410. break;
  1411. default:
  1412. r = 0;
  1413. break;
  1414. }
  1415. return r;
  1416. }
  1417. long kvm_arch_dev_ioctl(struct file *filp,
  1418. unsigned int ioctl, unsigned long arg)
  1419. {
  1420. void __user *argp = (void __user *)arg;
  1421. long r;
  1422. switch (ioctl) {
  1423. case KVM_GET_MSR_INDEX_LIST: {
  1424. struct kvm_msr_list __user *user_msr_list = argp;
  1425. struct kvm_msr_list msr_list;
  1426. unsigned n;
  1427. r = -EFAULT;
  1428. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1429. goto out;
  1430. n = msr_list.nmsrs;
  1431. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1432. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1433. goto out;
  1434. r = -E2BIG;
  1435. if (n < msr_list.nmsrs)
  1436. goto out;
  1437. r = -EFAULT;
  1438. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1439. num_msrs_to_save * sizeof(u32)))
  1440. goto out;
  1441. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  1442. &emulated_msrs,
  1443. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1444. goto out;
  1445. r = 0;
  1446. break;
  1447. }
  1448. case KVM_GET_SUPPORTED_CPUID: {
  1449. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1450. struct kvm_cpuid2 cpuid;
  1451. r = -EFAULT;
  1452. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1453. goto out;
  1454. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1455. cpuid_arg->entries);
  1456. if (r)
  1457. goto out;
  1458. r = -EFAULT;
  1459. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1460. goto out;
  1461. r = 0;
  1462. break;
  1463. }
  1464. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  1465. u64 mce_cap;
  1466. mce_cap = KVM_MCE_CAP_SUPPORTED;
  1467. r = -EFAULT;
  1468. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  1469. goto out;
  1470. r = 0;
  1471. break;
  1472. }
  1473. default:
  1474. r = -EINVAL;
  1475. }
  1476. out:
  1477. return r;
  1478. }
  1479. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1480. {
  1481. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1482. if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
  1483. unsigned long khz = cpufreq_quick_get(cpu);
  1484. if (!khz)
  1485. khz = tsc_khz;
  1486. per_cpu(cpu_tsc_khz, cpu) = khz;
  1487. }
  1488. kvm_request_guest_time_update(vcpu);
  1489. }
  1490. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1491. {
  1492. kvm_put_guest_fpu(vcpu);
  1493. kvm_x86_ops->vcpu_put(vcpu);
  1494. }
  1495. static int is_efer_nx(void)
  1496. {
  1497. unsigned long long efer = 0;
  1498. rdmsrl_safe(MSR_EFER, &efer);
  1499. return efer & EFER_NX;
  1500. }
  1501. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1502. {
  1503. int i;
  1504. struct kvm_cpuid_entry2 *e, *entry;
  1505. entry = NULL;
  1506. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1507. e = &vcpu->arch.cpuid_entries[i];
  1508. if (e->function == 0x80000001) {
  1509. entry = e;
  1510. break;
  1511. }
  1512. }
  1513. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1514. entry->edx &= ~(1 << 20);
  1515. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1516. }
  1517. }
  1518. /* when an old userspace process fills a new kernel module */
  1519. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1520. struct kvm_cpuid *cpuid,
  1521. struct kvm_cpuid_entry __user *entries)
  1522. {
  1523. int r, i;
  1524. struct kvm_cpuid_entry *cpuid_entries;
  1525. r = -E2BIG;
  1526. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1527. goto out;
  1528. r = -ENOMEM;
  1529. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1530. if (!cpuid_entries)
  1531. goto out;
  1532. r = -EFAULT;
  1533. if (copy_from_user(cpuid_entries, entries,
  1534. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1535. goto out_free;
  1536. for (i = 0; i < cpuid->nent; i++) {
  1537. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1538. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1539. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1540. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1541. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1542. vcpu->arch.cpuid_entries[i].index = 0;
  1543. vcpu->arch.cpuid_entries[i].flags = 0;
  1544. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1545. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1546. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1547. }
  1548. vcpu->arch.cpuid_nent = cpuid->nent;
  1549. cpuid_fix_nx_cap(vcpu);
  1550. r = 0;
  1551. kvm_apic_set_version(vcpu);
  1552. kvm_x86_ops->cpuid_update(vcpu);
  1553. out_free:
  1554. vfree(cpuid_entries);
  1555. out:
  1556. return r;
  1557. }
  1558. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1559. struct kvm_cpuid2 *cpuid,
  1560. struct kvm_cpuid_entry2 __user *entries)
  1561. {
  1562. int r;
  1563. r = -E2BIG;
  1564. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1565. goto out;
  1566. r = -EFAULT;
  1567. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1568. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1569. goto out;
  1570. vcpu->arch.cpuid_nent = cpuid->nent;
  1571. kvm_apic_set_version(vcpu);
  1572. kvm_x86_ops->cpuid_update(vcpu);
  1573. return 0;
  1574. out:
  1575. return r;
  1576. }
  1577. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1578. struct kvm_cpuid2 *cpuid,
  1579. struct kvm_cpuid_entry2 __user *entries)
  1580. {
  1581. int r;
  1582. r = -E2BIG;
  1583. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1584. goto out;
  1585. r = -EFAULT;
  1586. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1587. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1588. goto out;
  1589. return 0;
  1590. out:
  1591. cpuid->nent = vcpu->arch.cpuid_nent;
  1592. return r;
  1593. }
  1594. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1595. u32 index)
  1596. {
  1597. entry->function = function;
  1598. entry->index = index;
  1599. cpuid_count(entry->function, entry->index,
  1600. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1601. entry->flags = 0;
  1602. }
  1603. #define F(x) bit(X86_FEATURE_##x)
  1604. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1605. u32 index, int *nent, int maxnent)
  1606. {
  1607. unsigned f_nx = is_efer_nx() ? F(NX) : 0;
  1608. #ifdef CONFIG_X86_64
  1609. unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
  1610. ? F(GBPAGES) : 0;
  1611. unsigned f_lm = F(LM);
  1612. #else
  1613. unsigned f_gbpages = 0;
  1614. unsigned f_lm = 0;
  1615. #endif
  1616. unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
  1617. /* cpuid 1.edx */
  1618. const u32 kvm_supported_word0_x86_features =
  1619. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1620. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1621. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
  1622. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1623. F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
  1624. 0 /* Reserved, DS, ACPI */ | F(MMX) |
  1625. F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
  1626. 0 /* HTT, TM, Reserved, PBE */;
  1627. /* cpuid 0x80000001.edx */
  1628. const u32 kvm_supported_word1_x86_features =
  1629. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1630. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1631. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
  1632. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1633. F(PAT) | F(PSE36) | 0 /* Reserved */ |
  1634. f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
  1635. F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
  1636. 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
  1637. /* cpuid 1.ecx */
  1638. const u32 kvm_supported_word4_x86_features =
  1639. F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
  1640. 0 /* DS-CPL, VMX, SMX, EST */ |
  1641. 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
  1642. 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
  1643. 0 /* Reserved, DCA */ | F(XMM4_1) |
  1644. F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
  1645. 0 /* Reserved, XSAVE, OSXSAVE */;
  1646. /* cpuid 0x80000001.ecx */
  1647. const u32 kvm_supported_word6_x86_features =
  1648. F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
  1649. F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
  1650. F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
  1651. 0 /* SKINIT */ | 0 /* WDT */;
  1652. /* all calls to cpuid_count() should be made on the same cpu */
  1653. get_cpu();
  1654. do_cpuid_1_ent(entry, function, index);
  1655. ++*nent;
  1656. switch (function) {
  1657. case 0:
  1658. entry->eax = min(entry->eax, (u32)0xb);
  1659. break;
  1660. case 1:
  1661. entry->edx &= kvm_supported_word0_x86_features;
  1662. entry->ecx &= kvm_supported_word4_x86_features;
  1663. /* we support x2apic emulation even if host does not support
  1664. * it since we emulate x2apic in software */
  1665. entry->ecx |= F(X2APIC);
  1666. break;
  1667. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1668. * may return different values. This forces us to get_cpu() before
  1669. * issuing the first command, and also to emulate this annoying behavior
  1670. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1671. case 2: {
  1672. int t, times = entry->eax & 0xff;
  1673. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1674. entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  1675. for (t = 1; t < times && *nent < maxnent; ++t) {
  1676. do_cpuid_1_ent(&entry[t], function, 0);
  1677. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1678. ++*nent;
  1679. }
  1680. break;
  1681. }
  1682. /* function 4 and 0xb have additional index. */
  1683. case 4: {
  1684. int i, cache_type;
  1685. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1686. /* read more entries until cache_type is zero */
  1687. for (i = 1; *nent < maxnent; ++i) {
  1688. cache_type = entry[i - 1].eax & 0x1f;
  1689. if (!cache_type)
  1690. break;
  1691. do_cpuid_1_ent(&entry[i], function, i);
  1692. entry[i].flags |=
  1693. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1694. ++*nent;
  1695. }
  1696. break;
  1697. }
  1698. case 0xb: {
  1699. int i, level_type;
  1700. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1701. /* read more entries until level_type is zero */
  1702. for (i = 1; *nent < maxnent; ++i) {
  1703. level_type = entry[i - 1].ecx & 0xff00;
  1704. if (!level_type)
  1705. break;
  1706. do_cpuid_1_ent(&entry[i], function, i);
  1707. entry[i].flags |=
  1708. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1709. ++*nent;
  1710. }
  1711. break;
  1712. }
  1713. case 0x80000000:
  1714. entry->eax = min(entry->eax, 0x8000001a);
  1715. break;
  1716. case 0x80000001:
  1717. entry->edx &= kvm_supported_word1_x86_features;
  1718. entry->ecx &= kvm_supported_word6_x86_features;
  1719. break;
  1720. }
  1721. put_cpu();
  1722. }
  1723. #undef F
  1724. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1725. struct kvm_cpuid_entry2 __user *entries)
  1726. {
  1727. struct kvm_cpuid_entry2 *cpuid_entries;
  1728. int limit, nent = 0, r = -E2BIG;
  1729. u32 func;
  1730. if (cpuid->nent < 1)
  1731. goto out;
  1732. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1733. cpuid->nent = KVM_MAX_CPUID_ENTRIES;
  1734. r = -ENOMEM;
  1735. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1736. if (!cpuid_entries)
  1737. goto out;
  1738. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1739. limit = cpuid_entries[0].eax;
  1740. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1741. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1742. &nent, cpuid->nent);
  1743. r = -E2BIG;
  1744. if (nent >= cpuid->nent)
  1745. goto out_free;
  1746. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1747. limit = cpuid_entries[nent - 1].eax;
  1748. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1749. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1750. &nent, cpuid->nent);
  1751. r = -E2BIG;
  1752. if (nent >= cpuid->nent)
  1753. goto out_free;
  1754. r = -EFAULT;
  1755. if (copy_to_user(entries, cpuid_entries,
  1756. nent * sizeof(struct kvm_cpuid_entry2)))
  1757. goto out_free;
  1758. cpuid->nent = nent;
  1759. r = 0;
  1760. out_free:
  1761. vfree(cpuid_entries);
  1762. out:
  1763. return r;
  1764. }
  1765. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1766. struct kvm_lapic_state *s)
  1767. {
  1768. vcpu_load(vcpu);
  1769. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1770. vcpu_put(vcpu);
  1771. return 0;
  1772. }
  1773. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1774. struct kvm_lapic_state *s)
  1775. {
  1776. vcpu_load(vcpu);
  1777. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1778. kvm_apic_post_state_restore(vcpu);
  1779. update_cr8_intercept(vcpu);
  1780. vcpu_put(vcpu);
  1781. return 0;
  1782. }
  1783. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1784. struct kvm_interrupt *irq)
  1785. {
  1786. if (irq->irq < 0 || irq->irq >= 256)
  1787. return -EINVAL;
  1788. if (irqchip_in_kernel(vcpu->kvm))
  1789. return -ENXIO;
  1790. vcpu_load(vcpu);
  1791. kvm_queue_interrupt(vcpu, irq->irq, false);
  1792. vcpu_put(vcpu);
  1793. return 0;
  1794. }
  1795. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  1796. {
  1797. vcpu_load(vcpu);
  1798. kvm_inject_nmi(vcpu);
  1799. vcpu_put(vcpu);
  1800. return 0;
  1801. }
  1802. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1803. struct kvm_tpr_access_ctl *tac)
  1804. {
  1805. if (tac->flags)
  1806. return -EINVAL;
  1807. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1808. return 0;
  1809. }
  1810. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  1811. u64 mcg_cap)
  1812. {
  1813. int r;
  1814. unsigned bank_num = mcg_cap & 0xff, bank;
  1815. r = -EINVAL;
  1816. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  1817. goto out;
  1818. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  1819. goto out;
  1820. r = 0;
  1821. vcpu->arch.mcg_cap = mcg_cap;
  1822. /* Init IA32_MCG_CTL to all 1s */
  1823. if (mcg_cap & MCG_CTL_P)
  1824. vcpu->arch.mcg_ctl = ~(u64)0;
  1825. /* Init IA32_MCi_CTL to all 1s */
  1826. for (bank = 0; bank < bank_num; bank++)
  1827. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  1828. out:
  1829. return r;
  1830. }
  1831. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  1832. struct kvm_x86_mce *mce)
  1833. {
  1834. u64 mcg_cap = vcpu->arch.mcg_cap;
  1835. unsigned bank_num = mcg_cap & 0xff;
  1836. u64 *banks = vcpu->arch.mce_banks;
  1837. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  1838. return -EINVAL;
  1839. /*
  1840. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  1841. * reporting is disabled
  1842. */
  1843. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  1844. vcpu->arch.mcg_ctl != ~(u64)0)
  1845. return 0;
  1846. banks += 4 * mce->bank;
  1847. /*
  1848. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  1849. * reporting is disabled for the bank
  1850. */
  1851. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  1852. return 0;
  1853. if (mce->status & MCI_STATUS_UC) {
  1854. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  1855. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  1856. printk(KERN_DEBUG "kvm: set_mce: "
  1857. "injects mce exception while "
  1858. "previous one is in progress!\n");
  1859. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  1860. return 0;
  1861. }
  1862. if (banks[1] & MCI_STATUS_VAL)
  1863. mce->status |= MCI_STATUS_OVER;
  1864. banks[2] = mce->addr;
  1865. banks[3] = mce->misc;
  1866. vcpu->arch.mcg_status = mce->mcg_status;
  1867. banks[1] = mce->status;
  1868. kvm_queue_exception(vcpu, MC_VECTOR);
  1869. } else if (!(banks[1] & MCI_STATUS_VAL)
  1870. || !(banks[1] & MCI_STATUS_UC)) {
  1871. if (banks[1] & MCI_STATUS_VAL)
  1872. mce->status |= MCI_STATUS_OVER;
  1873. banks[2] = mce->addr;
  1874. banks[3] = mce->misc;
  1875. banks[1] = mce->status;
  1876. } else
  1877. banks[1] |= MCI_STATUS_OVER;
  1878. return 0;
  1879. }
  1880. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  1881. struct kvm_vcpu_events *events)
  1882. {
  1883. vcpu_load(vcpu);
  1884. events->exception.injected = vcpu->arch.exception.pending;
  1885. events->exception.nr = vcpu->arch.exception.nr;
  1886. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  1887. events->exception.error_code = vcpu->arch.exception.error_code;
  1888. events->interrupt.injected = vcpu->arch.interrupt.pending;
  1889. events->interrupt.nr = vcpu->arch.interrupt.nr;
  1890. events->interrupt.soft = vcpu->arch.interrupt.soft;
  1891. events->nmi.injected = vcpu->arch.nmi_injected;
  1892. events->nmi.pending = vcpu->arch.nmi_pending;
  1893. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  1894. events->sipi_vector = vcpu->arch.sipi_vector;
  1895. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  1896. | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
  1897. vcpu_put(vcpu);
  1898. }
  1899. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  1900. struct kvm_vcpu_events *events)
  1901. {
  1902. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  1903. | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
  1904. return -EINVAL;
  1905. vcpu_load(vcpu);
  1906. vcpu->arch.exception.pending = events->exception.injected;
  1907. vcpu->arch.exception.nr = events->exception.nr;
  1908. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  1909. vcpu->arch.exception.error_code = events->exception.error_code;
  1910. vcpu->arch.interrupt.pending = events->interrupt.injected;
  1911. vcpu->arch.interrupt.nr = events->interrupt.nr;
  1912. vcpu->arch.interrupt.soft = events->interrupt.soft;
  1913. if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
  1914. kvm_pic_clear_isr_ack(vcpu->kvm);
  1915. vcpu->arch.nmi_injected = events->nmi.injected;
  1916. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  1917. vcpu->arch.nmi_pending = events->nmi.pending;
  1918. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  1919. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
  1920. vcpu->arch.sipi_vector = events->sipi_vector;
  1921. vcpu_put(vcpu);
  1922. return 0;
  1923. }
  1924. long kvm_arch_vcpu_ioctl(struct file *filp,
  1925. unsigned int ioctl, unsigned long arg)
  1926. {
  1927. struct kvm_vcpu *vcpu = filp->private_data;
  1928. void __user *argp = (void __user *)arg;
  1929. int r;
  1930. struct kvm_lapic_state *lapic = NULL;
  1931. switch (ioctl) {
  1932. case KVM_GET_LAPIC: {
  1933. r = -EINVAL;
  1934. if (!vcpu->arch.apic)
  1935. goto out;
  1936. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1937. r = -ENOMEM;
  1938. if (!lapic)
  1939. goto out;
  1940. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  1941. if (r)
  1942. goto out;
  1943. r = -EFAULT;
  1944. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  1945. goto out;
  1946. r = 0;
  1947. break;
  1948. }
  1949. case KVM_SET_LAPIC: {
  1950. r = -EINVAL;
  1951. if (!vcpu->arch.apic)
  1952. goto out;
  1953. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1954. r = -ENOMEM;
  1955. if (!lapic)
  1956. goto out;
  1957. r = -EFAULT;
  1958. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  1959. goto out;
  1960. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  1961. if (r)
  1962. goto out;
  1963. r = 0;
  1964. break;
  1965. }
  1966. case KVM_INTERRUPT: {
  1967. struct kvm_interrupt irq;
  1968. r = -EFAULT;
  1969. if (copy_from_user(&irq, argp, sizeof irq))
  1970. goto out;
  1971. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1972. if (r)
  1973. goto out;
  1974. r = 0;
  1975. break;
  1976. }
  1977. case KVM_NMI: {
  1978. r = kvm_vcpu_ioctl_nmi(vcpu);
  1979. if (r)
  1980. goto out;
  1981. r = 0;
  1982. break;
  1983. }
  1984. case KVM_SET_CPUID: {
  1985. struct kvm_cpuid __user *cpuid_arg = argp;
  1986. struct kvm_cpuid cpuid;
  1987. r = -EFAULT;
  1988. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1989. goto out;
  1990. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1991. if (r)
  1992. goto out;
  1993. break;
  1994. }
  1995. case KVM_SET_CPUID2: {
  1996. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1997. struct kvm_cpuid2 cpuid;
  1998. r = -EFAULT;
  1999. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2000. goto out;
  2001. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2002. cpuid_arg->entries);
  2003. if (r)
  2004. goto out;
  2005. break;
  2006. }
  2007. case KVM_GET_CPUID2: {
  2008. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2009. struct kvm_cpuid2 cpuid;
  2010. r = -EFAULT;
  2011. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2012. goto out;
  2013. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2014. cpuid_arg->entries);
  2015. if (r)
  2016. goto out;
  2017. r = -EFAULT;
  2018. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2019. goto out;
  2020. r = 0;
  2021. break;
  2022. }
  2023. case KVM_GET_MSRS:
  2024. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2025. break;
  2026. case KVM_SET_MSRS:
  2027. r = msr_io(vcpu, argp, do_set_msr, 0);
  2028. break;
  2029. case KVM_TPR_ACCESS_REPORTING: {
  2030. struct kvm_tpr_access_ctl tac;
  2031. r = -EFAULT;
  2032. if (copy_from_user(&tac, argp, sizeof tac))
  2033. goto out;
  2034. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2035. if (r)
  2036. goto out;
  2037. r = -EFAULT;
  2038. if (copy_to_user(argp, &tac, sizeof tac))
  2039. goto out;
  2040. r = 0;
  2041. break;
  2042. };
  2043. case KVM_SET_VAPIC_ADDR: {
  2044. struct kvm_vapic_addr va;
  2045. r = -EINVAL;
  2046. if (!irqchip_in_kernel(vcpu->kvm))
  2047. goto out;
  2048. r = -EFAULT;
  2049. if (copy_from_user(&va, argp, sizeof va))
  2050. goto out;
  2051. r = 0;
  2052. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  2053. break;
  2054. }
  2055. case KVM_X86_SETUP_MCE: {
  2056. u64 mcg_cap;
  2057. r = -EFAULT;
  2058. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  2059. goto out;
  2060. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  2061. break;
  2062. }
  2063. case KVM_X86_SET_MCE: {
  2064. struct kvm_x86_mce mce;
  2065. r = -EFAULT;
  2066. if (copy_from_user(&mce, argp, sizeof mce))
  2067. goto out;
  2068. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  2069. break;
  2070. }
  2071. case KVM_GET_VCPU_EVENTS: {
  2072. struct kvm_vcpu_events events;
  2073. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  2074. r = -EFAULT;
  2075. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  2076. break;
  2077. r = 0;
  2078. break;
  2079. }
  2080. case KVM_SET_VCPU_EVENTS: {
  2081. struct kvm_vcpu_events events;
  2082. r = -EFAULT;
  2083. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  2084. break;
  2085. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  2086. break;
  2087. }
  2088. default:
  2089. r = -EINVAL;
  2090. }
  2091. out:
  2092. kfree(lapic);
  2093. return r;
  2094. }
  2095. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  2096. {
  2097. int ret;
  2098. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  2099. return -1;
  2100. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  2101. return ret;
  2102. }
  2103. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  2104. u64 ident_addr)
  2105. {
  2106. kvm->arch.ept_identity_map_addr = ident_addr;
  2107. return 0;
  2108. }
  2109. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  2110. u32 kvm_nr_mmu_pages)
  2111. {
  2112. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  2113. return -EINVAL;
  2114. mutex_lock(&kvm->slots_lock);
  2115. spin_lock(&kvm->mmu_lock);
  2116. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  2117. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  2118. spin_unlock(&kvm->mmu_lock);
  2119. mutex_unlock(&kvm->slots_lock);
  2120. return 0;
  2121. }
  2122. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  2123. {
  2124. return kvm->arch.n_alloc_mmu_pages;
  2125. }
  2126. gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
  2127. {
  2128. int i;
  2129. struct kvm_mem_alias *alias;
  2130. struct kvm_mem_aliases *aliases;
  2131. aliases = rcu_dereference(kvm->arch.aliases);
  2132. for (i = 0; i < aliases->naliases; ++i) {
  2133. alias = &aliases->aliases[i];
  2134. if (alias->flags & KVM_ALIAS_INVALID)
  2135. continue;
  2136. if (gfn >= alias->base_gfn
  2137. && gfn < alias->base_gfn + alias->npages)
  2138. return alias->target_gfn + gfn - alias->base_gfn;
  2139. }
  2140. return gfn;
  2141. }
  2142. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  2143. {
  2144. int i;
  2145. struct kvm_mem_alias *alias;
  2146. struct kvm_mem_aliases *aliases;
  2147. aliases = rcu_dereference(kvm->arch.aliases);
  2148. for (i = 0; i < aliases->naliases; ++i) {
  2149. alias = &aliases->aliases[i];
  2150. if (gfn >= alias->base_gfn
  2151. && gfn < alias->base_gfn + alias->npages)
  2152. return alias->target_gfn + gfn - alias->base_gfn;
  2153. }
  2154. return gfn;
  2155. }
  2156. /*
  2157. * Set a new alias region. Aliases map a portion of physical memory into
  2158. * another portion. This is useful for memory windows, for example the PC
  2159. * VGA region.
  2160. */
  2161. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  2162. struct kvm_memory_alias *alias)
  2163. {
  2164. int r, n;
  2165. struct kvm_mem_alias *p;
  2166. struct kvm_mem_aliases *aliases, *old_aliases;
  2167. r = -EINVAL;
  2168. /* General sanity checks */
  2169. if (alias->memory_size & (PAGE_SIZE - 1))
  2170. goto out;
  2171. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  2172. goto out;
  2173. if (alias->slot >= KVM_ALIAS_SLOTS)
  2174. goto out;
  2175. if (alias->guest_phys_addr + alias->memory_size
  2176. < alias->guest_phys_addr)
  2177. goto out;
  2178. if (alias->target_phys_addr + alias->memory_size
  2179. < alias->target_phys_addr)
  2180. goto out;
  2181. r = -ENOMEM;
  2182. aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  2183. if (!aliases)
  2184. goto out;
  2185. mutex_lock(&kvm->slots_lock);
  2186. /* invalidate any gfn reference in case of deletion/shrinking */
  2187. memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
  2188. aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
  2189. old_aliases = kvm->arch.aliases;
  2190. rcu_assign_pointer(kvm->arch.aliases, aliases);
  2191. synchronize_srcu_expedited(&kvm->srcu);
  2192. kvm_mmu_zap_all(kvm);
  2193. kfree(old_aliases);
  2194. r = -ENOMEM;
  2195. aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  2196. if (!aliases)
  2197. goto out_unlock;
  2198. memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
  2199. p = &aliases->aliases[alias->slot];
  2200. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  2201. p->npages = alias->memory_size >> PAGE_SHIFT;
  2202. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  2203. p->flags &= ~(KVM_ALIAS_INVALID);
  2204. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  2205. if (aliases->aliases[n - 1].npages)
  2206. break;
  2207. aliases->naliases = n;
  2208. old_aliases = kvm->arch.aliases;
  2209. rcu_assign_pointer(kvm->arch.aliases, aliases);
  2210. synchronize_srcu_expedited(&kvm->srcu);
  2211. kfree(old_aliases);
  2212. r = 0;
  2213. out_unlock:
  2214. mutex_unlock(&kvm->slots_lock);
  2215. out:
  2216. return r;
  2217. }
  2218. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2219. {
  2220. int r;
  2221. r = 0;
  2222. switch (chip->chip_id) {
  2223. case KVM_IRQCHIP_PIC_MASTER:
  2224. memcpy(&chip->chip.pic,
  2225. &pic_irqchip(kvm)->pics[0],
  2226. sizeof(struct kvm_pic_state));
  2227. break;
  2228. case KVM_IRQCHIP_PIC_SLAVE:
  2229. memcpy(&chip->chip.pic,
  2230. &pic_irqchip(kvm)->pics[1],
  2231. sizeof(struct kvm_pic_state));
  2232. break;
  2233. case KVM_IRQCHIP_IOAPIC:
  2234. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  2235. break;
  2236. default:
  2237. r = -EINVAL;
  2238. break;
  2239. }
  2240. return r;
  2241. }
  2242. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2243. {
  2244. int r;
  2245. r = 0;
  2246. switch (chip->chip_id) {
  2247. case KVM_IRQCHIP_PIC_MASTER:
  2248. raw_spin_lock(&pic_irqchip(kvm)->lock);
  2249. memcpy(&pic_irqchip(kvm)->pics[0],
  2250. &chip->chip.pic,
  2251. sizeof(struct kvm_pic_state));
  2252. raw_spin_unlock(&pic_irqchip(kvm)->lock);
  2253. break;
  2254. case KVM_IRQCHIP_PIC_SLAVE:
  2255. raw_spin_lock(&pic_irqchip(kvm)->lock);
  2256. memcpy(&pic_irqchip(kvm)->pics[1],
  2257. &chip->chip.pic,
  2258. sizeof(struct kvm_pic_state));
  2259. raw_spin_unlock(&pic_irqchip(kvm)->lock);
  2260. break;
  2261. case KVM_IRQCHIP_IOAPIC:
  2262. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  2263. break;
  2264. default:
  2265. r = -EINVAL;
  2266. break;
  2267. }
  2268. kvm_pic_update_irq(pic_irqchip(kvm));
  2269. return r;
  2270. }
  2271. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2272. {
  2273. int r = 0;
  2274. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2275. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  2276. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2277. return r;
  2278. }
  2279. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2280. {
  2281. int r = 0;
  2282. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2283. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  2284. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  2285. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2286. return r;
  2287. }
  2288. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2289. {
  2290. int r = 0;
  2291. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2292. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  2293. sizeof(ps->channels));
  2294. ps->flags = kvm->arch.vpit->pit_state.flags;
  2295. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2296. return r;
  2297. }
  2298. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2299. {
  2300. int r = 0, start = 0;
  2301. u32 prev_legacy, cur_legacy;
  2302. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2303. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2304. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2305. if (!prev_legacy && cur_legacy)
  2306. start = 1;
  2307. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  2308. sizeof(kvm->arch.vpit->pit_state.channels));
  2309. kvm->arch.vpit->pit_state.flags = ps->flags;
  2310. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  2311. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2312. return r;
  2313. }
  2314. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  2315. struct kvm_reinject_control *control)
  2316. {
  2317. if (!kvm->arch.vpit)
  2318. return -ENXIO;
  2319. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2320. kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
  2321. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2322. return 0;
  2323. }
  2324. /*
  2325. * Get (and clear) the dirty memory log for a memory slot.
  2326. */
  2327. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  2328. struct kvm_dirty_log *log)
  2329. {
  2330. int r, n, i;
  2331. struct kvm_memory_slot *memslot;
  2332. unsigned long is_dirty = 0;
  2333. unsigned long *dirty_bitmap = NULL;
  2334. mutex_lock(&kvm->slots_lock);
  2335. r = -EINVAL;
  2336. if (log->slot >= KVM_MEMORY_SLOTS)
  2337. goto out;
  2338. memslot = &kvm->memslots->memslots[log->slot];
  2339. r = -ENOENT;
  2340. if (!memslot->dirty_bitmap)
  2341. goto out;
  2342. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  2343. r = -ENOMEM;
  2344. dirty_bitmap = vmalloc(n);
  2345. if (!dirty_bitmap)
  2346. goto out;
  2347. memset(dirty_bitmap, 0, n);
  2348. for (i = 0; !is_dirty && i < n/sizeof(long); i++)
  2349. is_dirty = memslot->dirty_bitmap[i];
  2350. /* If nothing is dirty, don't bother messing with page tables. */
  2351. if (is_dirty) {
  2352. struct kvm_memslots *slots, *old_slots;
  2353. spin_lock(&kvm->mmu_lock);
  2354. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  2355. spin_unlock(&kvm->mmu_lock);
  2356. slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
  2357. if (!slots)
  2358. goto out_free;
  2359. memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
  2360. slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
  2361. old_slots = kvm->memslots;
  2362. rcu_assign_pointer(kvm->memslots, slots);
  2363. synchronize_srcu_expedited(&kvm->srcu);
  2364. dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
  2365. kfree(old_slots);
  2366. }
  2367. r = 0;
  2368. if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
  2369. r = -EFAULT;
  2370. out_free:
  2371. vfree(dirty_bitmap);
  2372. out:
  2373. mutex_unlock(&kvm->slots_lock);
  2374. return r;
  2375. }
  2376. long kvm_arch_vm_ioctl(struct file *filp,
  2377. unsigned int ioctl, unsigned long arg)
  2378. {
  2379. struct kvm *kvm = filp->private_data;
  2380. void __user *argp = (void __user *)arg;
  2381. int r = -ENOTTY;
  2382. /*
  2383. * This union makes it completely explicit to gcc-3.x
  2384. * that these two variables' stack usage should be
  2385. * combined, not added together.
  2386. */
  2387. union {
  2388. struct kvm_pit_state ps;
  2389. struct kvm_pit_state2 ps2;
  2390. struct kvm_memory_alias alias;
  2391. struct kvm_pit_config pit_config;
  2392. } u;
  2393. switch (ioctl) {
  2394. case KVM_SET_TSS_ADDR:
  2395. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  2396. if (r < 0)
  2397. goto out;
  2398. break;
  2399. case KVM_SET_IDENTITY_MAP_ADDR: {
  2400. u64 ident_addr;
  2401. r = -EFAULT;
  2402. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  2403. goto out;
  2404. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  2405. if (r < 0)
  2406. goto out;
  2407. break;
  2408. }
  2409. case KVM_SET_MEMORY_REGION: {
  2410. struct kvm_memory_region kvm_mem;
  2411. struct kvm_userspace_memory_region kvm_userspace_mem;
  2412. r = -EFAULT;
  2413. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2414. goto out;
  2415. kvm_userspace_mem.slot = kvm_mem.slot;
  2416. kvm_userspace_mem.flags = kvm_mem.flags;
  2417. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  2418. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  2419. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2420. if (r)
  2421. goto out;
  2422. break;
  2423. }
  2424. case KVM_SET_NR_MMU_PAGES:
  2425. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  2426. if (r)
  2427. goto out;
  2428. break;
  2429. case KVM_GET_NR_MMU_PAGES:
  2430. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  2431. break;
  2432. case KVM_SET_MEMORY_ALIAS:
  2433. r = -EFAULT;
  2434. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  2435. goto out;
  2436. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  2437. if (r)
  2438. goto out;
  2439. break;
  2440. case KVM_CREATE_IRQCHIP: {
  2441. struct kvm_pic *vpic;
  2442. mutex_lock(&kvm->lock);
  2443. r = -EEXIST;
  2444. if (kvm->arch.vpic)
  2445. goto create_irqchip_unlock;
  2446. r = -ENOMEM;
  2447. vpic = kvm_create_pic(kvm);
  2448. if (vpic) {
  2449. r = kvm_ioapic_init(kvm);
  2450. if (r) {
  2451. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2452. &vpic->dev);
  2453. kfree(vpic);
  2454. goto create_irqchip_unlock;
  2455. }
  2456. } else
  2457. goto create_irqchip_unlock;
  2458. smp_wmb();
  2459. kvm->arch.vpic = vpic;
  2460. smp_wmb();
  2461. r = kvm_setup_default_irq_routing(kvm);
  2462. if (r) {
  2463. mutex_lock(&kvm->irq_lock);
  2464. kvm_ioapic_destroy(kvm);
  2465. kvm_destroy_pic(kvm);
  2466. mutex_unlock(&kvm->irq_lock);
  2467. }
  2468. create_irqchip_unlock:
  2469. mutex_unlock(&kvm->lock);
  2470. break;
  2471. }
  2472. case KVM_CREATE_PIT:
  2473. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  2474. goto create_pit;
  2475. case KVM_CREATE_PIT2:
  2476. r = -EFAULT;
  2477. if (copy_from_user(&u.pit_config, argp,
  2478. sizeof(struct kvm_pit_config)))
  2479. goto out;
  2480. create_pit:
  2481. mutex_lock(&kvm->slots_lock);
  2482. r = -EEXIST;
  2483. if (kvm->arch.vpit)
  2484. goto create_pit_unlock;
  2485. r = -ENOMEM;
  2486. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  2487. if (kvm->arch.vpit)
  2488. r = 0;
  2489. create_pit_unlock:
  2490. mutex_unlock(&kvm->slots_lock);
  2491. break;
  2492. case KVM_IRQ_LINE_STATUS:
  2493. case KVM_IRQ_LINE: {
  2494. struct kvm_irq_level irq_event;
  2495. r = -EFAULT;
  2496. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2497. goto out;
  2498. if (irqchip_in_kernel(kvm)) {
  2499. __s32 status;
  2500. status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  2501. irq_event.irq, irq_event.level);
  2502. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2503. irq_event.status = status;
  2504. if (copy_to_user(argp, &irq_event,
  2505. sizeof irq_event))
  2506. goto out;
  2507. }
  2508. r = 0;
  2509. }
  2510. break;
  2511. }
  2512. case KVM_GET_IRQCHIP: {
  2513. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2514. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2515. r = -ENOMEM;
  2516. if (!chip)
  2517. goto out;
  2518. r = -EFAULT;
  2519. if (copy_from_user(chip, argp, sizeof *chip))
  2520. goto get_irqchip_out;
  2521. r = -ENXIO;
  2522. if (!irqchip_in_kernel(kvm))
  2523. goto get_irqchip_out;
  2524. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  2525. if (r)
  2526. goto get_irqchip_out;
  2527. r = -EFAULT;
  2528. if (copy_to_user(argp, chip, sizeof *chip))
  2529. goto get_irqchip_out;
  2530. r = 0;
  2531. get_irqchip_out:
  2532. kfree(chip);
  2533. if (r)
  2534. goto out;
  2535. break;
  2536. }
  2537. case KVM_SET_IRQCHIP: {
  2538. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2539. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2540. r = -ENOMEM;
  2541. if (!chip)
  2542. goto out;
  2543. r = -EFAULT;
  2544. if (copy_from_user(chip, argp, sizeof *chip))
  2545. goto set_irqchip_out;
  2546. r = -ENXIO;
  2547. if (!irqchip_in_kernel(kvm))
  2548. goto set_irqchip_out;
  2549. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  2550. if (r)
  2551. goto set_irqchip_out;
  2552. r = 0;
  2553. set_irqchip_out:
  2554. kfree(chip);
  2555. if (r)
  2556. goto out;
  2557. break;
  2558. }
  2559. case KVM_GET_PIT: {
  2560. r = -EFAULT;
  2561. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  2562. goto out;
  2563. r = -ENXIO;
  2564. if (!kvm->arch.vpit)
  2565. goto out;
  2566. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  2567. if (r)
  2568. goto out;
  2569. r = -EFAULT;
  2570. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  2571. goto out;
  2572. r = 0;
  2573. break;
  2574. }
  2575. case KVM_SET_PIT: {
  2576. r = -EFAULT;
  2577. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  2578. goto out;
  2579. r = -ENXIO;
  2580. if (!kvm->arch.vpit)
  2581. goto out;
  2582. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  2583. if (r)
  2584. goto out;
  2585. r = 0;
  2586. break;
  2587. }
  2588. case KVM_GET_PIT2: {
  2589. r = -ENXIO;
  2590. if (!kvm->arch.vpit)
  2591. goto out;
  2592. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  2593. if (r)
  2594. goto out;
  2595. r = -EFAULT;
  2596. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  2597. goto out;
  2598. r = 0;
  2599. break;
  2600. }
  2601. case KVM_SET_PIT2: {
  2602. r = -EFAULT;
  2603. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  2604. goto out;
  2605. r = -ENXIO;
  2606. if (!kvm->arch.vpit)
  2607. goto out;
  2608. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  2609. if (r)
  2610. goto out;
  2611. r = 0;
  2612. break;
  2613. }
  2614. case KVM_REINJECT_CONTROL: {
  2615. struct kvm_reinject_control control;
  2616. r = -EFAULT;
  2617. if (copy_from_user(&control, argp, sizeof(control)))
  2618. goto out;
  2619. r = kvm_vm_ioctl_reinject(kvm, &control);
  2620. if (r)
  2621. goto out;
  2622. r = 0;
  2623. break;
  2624. }
  2625. case KVM_XEN_HVM_CONFIG: {
  2626. r = -EFAULT;
  2627. if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
  2628. sizeof(struct kvm_xen_hvm_config)))
  2629. goto out;
  2630. r = -EINVAL;
  2631. if (kvm->arch.xen_hvm_config.flags)
  2632. goto out;
  2633. r = 0;
  2634. break;
  2635. }
  2636. case KVM_SET_CLOCK: {
  2637. struct timespec now;
  2638. struct kvm_clock_data user_ns;
  2639. u64 now_ns;
  2640. s64 delta;
  2641. r = -EFAULT;
  2642. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  2643. goto out;
  2644. r = -EINVAL;
  2645. if (user_ns.flags)
  2646. goto out;
  2647. r = 0;
  2648. ktime_get_ts(&now);
  2649. now_ns = timespec_to_ns(&now);
  2650. delta = user_ns.clock - now_ns;
  2651. kvm->arch.kvmclock_offset = delta;
  2652. break;
  2653. }
  2654. case KVM_GET_CLOCK: {
  2655. struct timespec now;
  2656. struct kvm_clock_data user_ns;
  2657. u64 now_ns;
  2658. ktime_get_ts(&now);
  2659. now_ns = timespec_to_ns(&now);
  2660. user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
  2661. user_ns.flags = 0;
  2662. r = -EFAULT;
  2663. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  2664. goto out;
  2665. r = 0;
  2666. break;
  2667. }
  2668. default:
  2669. ;
  2670. }
  2671. out:
  2672. return r;
  2673. }
  2674. static void kvm_init_msr_list(void)
  2675. {
  2676. u32 dummy[2];
  2677. unsigned i, j;
  2678. /* skip the first msrs in the list. KVM-specific */
  2679. for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
  2680. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2681. continue;
  2682. if (j < i)
  2683. msrs_to_save[j] = msrs_to_save[i];
  2684. j++;
  2685. }
  2686. num_msrs_to_save = j;
  2687. }
  2688. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  2689. const void *v)
  2690. {
  2691. if (vcpu->arch.apic &&
  2692. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
  2693. return 0;
  2694. return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
  2695. }
  2696. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  2697. {
  2698. if (vcpu->arch.apic &&
  2699. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
  2700. return 0;
  2701. return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
  2702. }
  2703. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2704. {
  2705. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2706. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2707. }
  2708. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2709. {
  2710. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2711. access |= PFERR_FETCH_MASK;
  2712. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2713. }
  2714. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2715. {
  2716. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2717. access |= PFERR_WRITE_MASK;
  2718. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2719. }
  2720. /* uses this to access any guest's mapped memory without checking CPL */
  2721. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2722. {
  2723. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
  2724. }
  2725. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  2726. struct kvm_vcpu *vcpu, u32 access,
  2727. u32 *error)
  2728. {
  2729. void *data = val;
  2730. int r = X86EMUL_CONTINUE;
  2731. while (bytes) {
  2732. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
  2733. unsigned offset = addr & (PAGE_SIZE-1);
  2734. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  2735. int ret;
  2736. if (gpa == UNMAPPED_GVA) {
  2737. r = X86EMUL_PROPAGATE_FAULT;
  2738. goto out;
  2739. }
  2740. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  2741. if (ret < 0) {
  2742. r = X86EMUL_UNHANDLEABLE;
  2743. goto out;
  2744. }
  2745. bytes -= toread;
  2746. data += toread;
  2747. addr += toread;
  2748. }
  2749. out:
  2750. return r;
  2751. }
  2752. /* used for instruction fetching */
  2753. static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2754. struct kvm_vcpu *vcpu, u32 *error)
  2755. {
  2756. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2757. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
  2758. access | PFERR_FETCH_MASK, error);
  2759. }
  2760. static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2761. struct kvm_vcpu *vcpu, u32 *error)
  2762. {
  2763. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2764. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  2765. error);
  2766. }
  2767. static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
  2768. struct kvm_vcpu *vcpu, u32 *error)
  2769. {
  2770. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
  2771. }
  2772. static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2773. struct kvm_vcpu *vcpu, u32 *error)
  2774. {
  2775. void *data = val;
  2776. int r = X86EMUL_CONTINUE;
  2777. while (bytes) {
  2778. gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error);
  2779. unsigned offset = addr & (PAGE_SIZE-1);
  2780. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  2781. int ret;
  2782. if (gpa == UNMAPPED_GVA) {
  2783. r = X86EMUL_PROPAGATE_FAULT;
  2784. goto out;
  2785. }
  2786. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  2787. if (ret < 0) {
  2788. r = X86EMUL_UNHANDLEABLE;
  2789. goto out;
  2790. }
  2791. bytes -= towrite;
  2792. data += towrite;
  2793. addr += towrite;
  2794. }
  2795. out:
  2796. return r;
  2797. }
  2798. static int emulator_read_emulated(unsigned long addr,
  2799. void *val,
  2800. unsigned int bytes,
  2801. struct kvm_vcpu *vcpu)
  2802. {
  2803. gpa_t gpa;
  2804. u32 error_code;
  2805. if (vcpu->mmio_read_completed) {
  2806. memcpy(val, vcpu->mmio_data, bytes);
  2807. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  2808. vcpu->mmio_phys_addr, *(u64 *)val);
  2809. vcpu->mmio_read_completed = 0;
  2810. return X86EMUL_CONTINUE;
  2811. }
  2812. gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
  2813. if (gpa == UNMAPPED_GVA) {
  2814. kvm_inject_page_fault(vcpu, addr, error_code);
  2815. return X86EMUL_PROPAGATE_FAULT;
  2816. }
  2817. /* For APIC access vmexit */
  2818. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2819. goto mmio;
  2820. if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
  2821. == X86EMUL_CONTINUE)
  2822. return X86EMUL_CONTINUE;
  2823. mmio:
  2824. /*
  2825. * Is this MMIO handled locally?
  2826. */
  2827. if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
  2828. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
  2829. return X86EMUL_CONTINUE;
  2830. }
  2831. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  2832. vcpu->mmio_needed = 1;
  2833. vcpu->mmio_phys_addr = gpa;
  2834. vcpu->mmio_size = bytes;
  2835. vcpu->mmio_is_write = 0;
  2836. return X86EMUL_UNHANDLEABLE;
  2837. }
  2838. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  2839. const void *val, int bytes)
  2840. {
  2841. int ret;
  2842. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  2843. if (ret < 0)
  2844. return 0;
  2845. kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
  2846. return 1;
  2847. }
  2848. static int emulator_write_emulated_onepage(unsigned long addr,
  2849. const void *val,
  2850. unsigned int bytes,
  2851. struct kvm_vcpu *vcpu)
  2852. {
  2853. gpa_t gpa;
  2854. u32 error_code;
  2855. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
  2856. if (gpa == UNMAPPED_GVA) {
  2857. kvm_inject_page_fault(vcpu, addr, error_code);
  2858. return X86EMUL_PROPAGATE_FAULT;
  2859. }
  2860. /* For APIC access vmexit */
  2861. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2862. goto mmio;
  2863. if (emulator_write_phys(vcpu, gpa, val, bytes))
  2864. return X86EMUL_CONTINUE;
  2865. mmio:
  2866. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  2867. /*
  2868. * Is this MMIO handled locally?
  2869. */
  2870. if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
  2871. return X86EMUL_CONTINUE;
  2872. vcpu->mmio_needed = 1;
  2873. vcpu->mmio_phys_addr = gpa;
  2874. vcpu->mmio_size = bytes;
  2875. vcpu->mmio_is_write = 1;
  2876. memcpy(vcpu->mmio_data, val, bytes);
  2877. return X86EMUL_CONTINUE;
  2878. }
  2879. int emulator_write_emulated(unsigned long addr,
  2880. const void *val,
  2881. unsigned int bytes,
  2882. struct kvm_vcpu *vcpu)
  2883. {
  2884. /* Crossing a page boundary? */
  2885. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  2886. int rc, now;
  2887. now = -addr & ~PAGE_MASK;
  2888. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  2889. if (rc != X86EMUL_CONTINUE)
  2890. return rc;
  2891. addr += now;
  2892. val += now;
  2893. bytes -= now;
  2894. }
  2895. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  2896. }
  2897. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  2898. static int emulator_cmpxchg_emulated(unsigned long addr,
  2899. const void *old,
  2900. const void *new,
  2901. unsigned int bytes,
  2902. struct kvm_vcpu *vcpu)
  2903. {
  2904. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  2905. #ifndef CONFIG_X86_64
  2906. /* guests cmpxchg8b have to be emulated atomically */
  2907. if (bytes == 8) {
  2908. gpa_t gpa;
  2909. struct page *page;
  2910. char *kaddr;
  2911. u64 val;
  2912. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  2913. if (gpa == UNMAPPED_GVA ||
  2914. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2915. goto emul_write;
  2916. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  2917. goto emul_write;
  2918. val = *(u64 *)new;
  2919. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2920. kaddr = kmap_atomic(page, KM_USER0);
  2921. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  2922. kunmap_atomic(kaddr, KM_USER0);
  2923. kvm_release_page_dirty(page);
  2924. }
  2925. emul_write:
  2926. #endif
  2927. return emulator_write_emulated(addr, new, bytes, vcpu);
  2928. }
  2929. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2930. {
  2931. return kvm_x86_ops->get_segment_base(vcpu, seg);
  2932. }
  2933. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  2934. {
  2935. kvm_mmu_invlpg(vcpu, address);
  2936. return X86EMUL_CONTINUE;
  2937. }
  2938. int emulate_clts(struct kvm_vcpu *vcpu)
  2939. {
  2940. kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  2941. kvm_x86_ops->fpu_activate(vcpu);
  2942. return X86EMUL_CONTINUE;
  2943. }
  2944. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  2945. {
  2946. return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest);
  2947. }
  2948. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  2949. {
  2950. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  2951. return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask);
  2952. }
  2953. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  2954. {
  2955. u8 opcodes[4];
  2956. unsigned long rip = kvm_rip_read(vcpu);
  2957. unsigned long rip_linear;
  2958. if (!printk_ratelimit())
  2959. return;
  2960. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  2961. kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
  2962. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  2963. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  2964. }
  2965. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  2966. static struct x86_emulate_ops emulate_ops = {
  2967. .read_std = kvm_read_guest_virt_system,
  2968. .fetch = kvm_fetch_guest_virt,
  2969. .read_emulated = emulator_read_emulated,
  2970. .write_emulated = emulator_write_emulated,
  2971. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  2972. };
  2973. static void cache_all_regs(struct kvm_vcpu *vcpu)
  2974. {
  2975. kvm_register_read(vcpu, VCPU_REGS_RAX);
  2976. kvm_register_read(vcpu, VCPU_REGS_RSP);
  2977. kvm_register_read(vcpu, VCPU_REGS_RIP);
  2978. vcpu->arch.regs_dirty = ~0;
  2979. }
  2980. int emulate_instruction(struct kvm_vcpu *vcpu,
  2981. unsigned long cr2,
  2982. u16 error_code,
  2983. int emulation_type)
  2984. {
  2985. int r, shadow_mask;
  2986. struct decode_cache *c;
  2987. struct kvm_run *run = vcpu->run;
  2988. kvm_clear_exception_queue(vcpu);
  2989. vcpu->arch.mmio_fault_cr2 = cr2;
  2990. /*
  2991. * TODO: fix emulate.c to use guest_read/write_register
  2992. * instead of direct ->regs accesses, can save hundred cycles
  2993. * on Intel for instructions that don't read/change RSP, for
  2994. * for example.
  2995. */
  2996. cache_all_regs(vcpu);
  2997. vcpu->mmio_is_write = 0;
  2998. vcpu->arch.pio.string = 0;
  2999. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  3000. int cs_db, cs_l;
  3001. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3002. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  3003. vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
  3004. vcpu->arch.emulate_ctxt.mode =
  3005. (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  3006. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  3007. ? X86EMUL_MODE_VM86 : cs_l
  3008. ? X86EMUL_MODE_PROT64 : cs_db
  3009. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  3010. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  3011. /* Only allow emulation of specific instructions on #UD
  3012. * (namely VMMCALL, sysenter, sysexit, syscall)*/
  3013. c = &vcpu->arch.emulate_ctxt.decode;
  3014. if (emulation_type & EMULTYPE_TRAP_UD) {
  3015. if (!c->twobyte)
  3016. return EMULATE_FAIL;
  3017. switch (c->b) {
  3018. case 0x01: /* VMMCALL */
  3019. if (c->modrm_mod != 3 || c->modrm_rm != 1)
  3020. return EMULATE_FAIL;
  3021. break;
  3022. case 0x34: /* sysenter */
  3023. case 0x35: /* sysexit */
  3024. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  3025. return EMULATE_FAIL;
  3026. break;
  3027. case 0x05: /* syscall */
  3028. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  3029. return EMULATE_FAIL;
  3030. break;
  3031. default:
  3032. return EMULATE_FAIL;
  3033. }
  3034. if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
  3035. return EMULATE_FAIL;
  3036. }
  3037. ++vcpu->stat.insn_emulation;
  3038. if (r) {
  3039. ++vcpu->stat.insn_emulation_fail;
  3040. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  3041. return EMULATE_DONE;
  3042. return EMULATE_FAIL;
  3043. }
  3044. }
  3045. if (emulation_type & EMULTYPE_SKIP) {
  3046. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
  3047. return EMULATE_DONE;
  3048. }
  3049. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  3050. shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
  3051. if (r == 0)
  3052. kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
  3053. if (vcpu->arch.pio.string)
  3054. return EMULATE_DO_MMIO;
  3055. if ((r || vcpu->mmio_is_write) && run) {
  3056. run->exit_reason = KVM_EXIT_MMIO;
  3057. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  3058. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  3059. run->mmio.len = vcpu->mmio_size;
  3060. run->mmio.is_write = vcpu->mmio_is_write;
  3061. }
  3062. if (r) {
  3063. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  3064. return EMULATE_DONE;
  3065. if (!vcpu->mmio_needed) {
  3066. kvm_report_emulation_failure(vcpu, "mmio");
  3067. return EMULATE_FAIL;
  3068. }
  3069. return EMULATE_DO_MMIO;
  3070. }
  3071. kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  3072. if (vcpu->mmio_is_write) {
  3073. vcpu->mmio_needed = 0;
  3074. return EMULATE_DO_MMIO;
  3075. }
  3076. return EMULATE_DONE;
  3077. }
  3078. EXPORT_SYMBOL_GPL(emulate_instruction);
  3079. static int pio_copy_data(struct kvm_vcpu *vcpu)
  3080. {
  3081. void *p = vcpu->arch.pio_data;
  3082. gva_t q = vcpu->arch.pio.guest_gva;
  3083. unsigned bytes;
  3084. int ret;
  3085. u32 error_code;
  3086. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  3087. if (vcpu->arch.pio.in)
  3088. ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code);
  3089. else
  3090. ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code);
  3091. if (ret == X86EMUL_PROPAGATE_FAULT)
  3092. kvm_inject_page_fault(vcpu, q, error_code);
  3093. return ret;
  3094. }
  3095. int complete_pio(struct kvm_vcpu *vcpu)
  3096. {
  3097. struct kvm_pio_request *io = &vcpu->arch.pio;
  3098. long delta;
  3099. int r;
  3100. unsigned long val;
  3101. if (!io->string) {
  3102. if (io->in) {
  3103. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3104. memcpy(&val, vcpu->arch.pio_data, io->size);
  3105. kvm_register_write(vcpu, VCPU_REGS_RAX, val);
  3106. }
  3107. } else {
  3108. if (io->in) {
  3109. r = pio_copy_data(vcpu);
  3110. if (r)
  3111. goto out;
  3112. }
  3113. delta = 1;
  3114. if (io->rep) {
  3115. delta *= io->cur_count;
  3116. /*
  3117. * The size of the register should really depend on
  3118. * current address size.
  3119. */
  3120. val = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3121. val -= delta;
  3122. kvm_register_write(vcpu, VCPU_REGS_RCX, val);
  3123. }
  3124. if (io->down)
  3125. delta = -delta;
  3126. delta *= io->size;
  3127. if (io->in) {
  3128. val = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3129. val += delta;
  3130. kvm_register_write(vcpu, VCPU_REGS_RDI, val);
  3131. } else {
  3132. val = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3133. val += delta;
  3134. kvm_register_write(vcpu, VCPU_REGS_RSI, val);
  3135. }
  3136. }
  3137. out:
  3138. io->count -= io->cur_count;
  3139. io->cur_count = 0;
  3140. return 0;
  3141. }
  3142. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  3143. {
  3144. /* TODO: String I/O for in kernel device */
  3145. int r;
  3146. if (vcpu->arch.pio.in)
  3147. r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
  3148. vcpu->arch.pio.size, pd);
  3149. else
  3150. r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3151. vcpu->arch.pio.port, vcpu->arch.pio.size,
  3152. pd);
  3153. return r;
  3154. }
  3155. static int pio_string_write(struct kvm_vcpu *vcpu)
  3156. {
  3157. struct kvm_pio_request *io = &vcpu->arch.pio;
  3158. void *pd = vcpu->arch.pio_data;
  3159. int i, r = 0;
  3160. for (i = 0; i < io->cur_count; i++) {
  3161. if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3162. io->port, io->size, pd)) {
  3163. r = -EOPNOTSUPP;
  3164. break;
  3165. }
  3166. pd += io->size;
  3167. }
  3168. return r;
  3169. }
  3170. int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
  3171. {
  3172. unsigned long val;
  3173. trace_kvm_pio(!in, port, size, 1);
  3174. vcpu->run->exit_reason = KVM_EXIT_IO;
  3175. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  3176. vcpu->run->io.size = vcpu->arch.pio.size = size;
  3177. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3178. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  3179. vcpu->run->io.port = vcpu->arch.pio.port = port;
  3180. vcpu->arch.pio.in = in;
  3181. vcpu->arch.pio.string = 0;
  3182. vcpu->arch.pio.down = 0;
  3183. vcpu->arch.pio.rep = 0;
  3184. if (!vcpu->arch.pio.in) {
  3185. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3186. memcpy(vcpu->arch.pio_data, &val, 4);
  3187. }
  3188. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3189. complete_pio(vcpu);
  3190. return 1;
  3191. }
  3192. return 0;
  3193. }
  3194. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  3195. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
  3196. int size, unsigned long count, int down,
  3197. gva_t address, int rep, unsigned port)
  3198. {
  3199. unsigned now, in_page;
  3200. int ret = 0;
  3201. trace_kvm_pio(!in, port, size, count);
  3202. vcpu->run->exit_reason = KVM_EXIT_IO;
  3203. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  3204. vcpu->run->io.size = vcpu->arch.pio.size = size;
  3205. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3206. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  3207. vcpu->run->io.port = vcpu->arch.pio.port = port;
  3208. vcpu->arch.pio.in = in;
  3209. vcpu->arch.pio.string = 1;
  3210. vcpu->arch.pio.down = down;
  3211. vcpu->arch.pio.rep = rep;
  3212. if (!count) {
  3213. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3214. return 1;
  3215. }
  3216. if (!down)
  3217. in_page = PAGE_SIZE - offset_in_page(address);
  3218. else
  3219. in_page = offset_in_page(address) + size;
  3220. now = min(count, (unsigned long)in_page / size);
  3221. if (!now)
  3222. now = 1;
  3223. if (down) {
  3224. /*
  3225. * String I/O in reverse. Yuck. Kill the guest, fix later.
  3226. */
  3227. pr_unimpl(vcpu, "guest string pio down\n");
  3228. kvm_inject_gp(vcpu, 0);
  3229. return 1;
  3230. }
  3231. vcpu->run->io.count = now;
  3232. vcpu->arch.pio.cur_count = now;
  3233. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  3234. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3235. vcpu->arch.pio.guest_gva = address;
  3236. if (!vcpu->arch.pio.in) {
  3237. /* string PIO write */
  3238. ret = pio_copy_data(vcpu);
  3239. if (ret == X86EMUL_PROPAGATE_FAULT)
  3240. return 1;
  3241. if (ret == 0 && !pio_string_write(vcpu)) {
  3242. complete_pio(vcpu);
  3243. if (vcpu->arch.pio.count == 0)
  3244. ret = 1;
  3245. }
  3246. }
  3247. /* no string PIO read support yet */
  3248. return ret;
  3249. }
  3250. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  3251. static void bounce_off(void *info)
  3252. {
  3253. /* nothing */
  3254. }
  3255. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  3256. void *data)
  3257. {
  3258. struct cpufreq_freqs *freq = data;
  3259. struct kvm *kvm;
  3260. struct kvm_vcpu *vcpu;
  3261. int i, send_ipi = 0;
  3262. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  3263. return 0;
  3264. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  3265. return 0;
  3266. per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
  3267. spin_lock(&kvm_lock);
  3268. list_for_each_entry(kvm, &vm_list, vm_list) {
  3269. kvm_for_each_vcpu(i, vcpu, kvm) {
  3270. if (vcpu->cpu != freq->cpu)
  3271. continue;
  3272. if (!kvm_request_guest_time_update(vcpu))
  3273. continue;
  3274. if (vcpu->cpu != smp_processor_id())
  3275. send_ipi++;
  3276. }
  3277. }
  3278. spin_unlock(&kvm_lock);
  3279. if (freq->old < freq->new && send_ipi) {
  3280. /*
  3281. * We upscale the frequency. Must make the guest
  3282. * doesn't see old kvmclock values while running with
  3283. * the new frequency, otherwise we risk the guest sees
  3284. * time go backwards.
  3285. *
  3286. * In case we update the frequency for another cpu
  3287. * (which might be in guest context) send an interrupt
  3288. * to kick the cpu out of guest context. Next time
  3289. * guest context is entered kvmclock will be updated,
  3290. * so the guest will not see stale values.
  3291. */
  3292. smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
  3293. }
  3294. return 0;
  3295. }
  3296. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  3297. .notifier_call = kvmclock_cpufreq_notifier
  3298. };
  3299. static void kvm_timer_init(void)
  3300. {
  3301. int cpu;
  3302. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3303. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  3304. CPUFREQ_TRANSITION_NOTIFIER);
  3305. for_each_online_cpu(cpu) {
  3306. unsigned long khz = cpufreq_get(cpu);
  3307. if (!khz)
  3308. khz = tsc_khz;
  3309. per_cpu(cpu_tsc_khz, cpu) = khz;
  3310. }
  3311. } else {
  3312. for_each_possible_cpu(cpu)
  3313. per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
  3314. }
  3315. }
  3316. int kvm_arch_init(void *opaque)
  3317. {
  3318. int r;
  3319. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  3320. if (kvm_x86_ops) {
  3321. printk(KERN_ERR "kvm: already loaded the other module\n");
  3322. r = -EEXIST;
  3323. goto out;
  3324. }
  3325. if (!ops->cpu_has_kvm_support()) {
  3326. printk(KERN_ERR "kvm: no hardware support\n");
  3327. r = -EOPNOTSUPP;
  3328. goto out;
  3329. }
  3330. if (ops->disabled_by_bios()) {
  3331. printk(KERN_ERR "kvm: disabled by bios\n");
  3332. r = -EOPNOTSUPP;
  3333. goto out;
  3334. }
  3335. r = kvm_mmu_module_init();
  3336. if (r)
  3337. goto out;
  3338. kvm_init_msr_list();
  3339. kvm_x86_ops = ops;
  3340. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  3341. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  3342. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  3343. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  3344. kvm_timer_init();
  3345. return 0;
  3346. out:
  3347. return r;
  3348. }
  3349. void kvm_arch_exit(void)
  3350. {
  3351. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  3352. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  3353. CPUFREQ_TRANSITION_NOTIFIER);
  3354. kvm_x86_ops = NULL;
  3355. kvm_mmu_module_exit();
  3356. }
  3357. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  3358. {
  3359. ++vcpu->stat.halt_exits;
  3360. if (irqchip_in_kernel(vcpu->kvm)) {
  3361. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  3362. return 1;
  3363. } else {
  3364. vcpu->run->exit_reason = KVM_EXIT_HLT;
  3365. return 0;
  3366. }
  3367. }
  3368. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  3369. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  3370. unsigned long a1)
  3371. {
  3372. if (is_long_mode(vcpu))
  3373. return a0;
  3374. else
  3375. return a0 | ((gpa_t)a1 << 32);
  3376. }
  3377. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  3378. {
  3379. u64 param, ingpa, outgpa, ret;
  3380. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  3381. bool fast, longmode;
  3382. int cs_db, cs_l;
  3383. /*
  3384. * hypercall generates UD from non zero cpl and real mode
  3385. * per HYPER-V spec
  3386. */
  3387. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  3388. kvm_queue_exception(vcpu, UD_VECTOR);
  3389. return 0;
  3390. }
  3391. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3392. longmode = is_long_mode(vcpu) && cs_l == 1;
  3393. if (!longmode) {
  3394. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  3395. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  3396. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  3397. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  3398. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  3399. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  3400. }
  3401. #ifdef CONFIG_X86_64
  3402. else {
  3403. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3404. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3405. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  3406. }
  3407. #endif
  3408. code = param & 0xffff;
  3409. fast = (param >> 16) & 0x1;
  3410. rep_cnt = (param >> 32) & 0xfff;
  3411. rep_idx = (param >> 48) & 0xfff;
  3412. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  3413. switch (code) {
  3414. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  3415. kvm_vcpu_on_spin(vcpu);
  3416. break;
  3417. default:
  3418. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  3419. break;
  3420. }
  3421. ret = res | (((u64)rep_done & 0xfff) << 32);
  3422. if (longmode) {
  3423. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  3424. } else {
  3425. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  3426. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  3427. }
  3428. return 1;
  3429. }
  3430. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  3431. {
  3432. unsigned long nr, a0, a1, a2, a3, ret;
  3433. int r = 1;
  3434. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  3435. return kvm_hv_hypercall(vcpu);
  3436. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3437. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3438. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3439. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3440. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3441. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  3442. if (!is_long_mode(vcpu)) {
  3443. nr &= 0xFFFFFFFF;
  3444. a0 &= 0xFFFFFFFF;
  3445. a1 &= 0xFFFFFFFF;
  3446. a2 &= 0xFFFFFFFF;
  3447. a3 &= 0xFFFFFFFF;
  3448. }
  3449. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  3450. ret = -KVM_EPERM;
  3451. goto out;
  3452. }
  3453. switch (nr) {
  3454. case KVM_HC_VAPIC_POLL_IRQ:
  3455. ret = 0;
  3456. break;
  3457. case KVM_HC_MMU_OP:
  3458. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  3459. break;
  3460. default:
  3461. ret = -KVM_ENOSYS;
  3462. break;
  3463. }
  3464. out:
  3465. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  3466. ++vcpu->stat.hypercalls;
  3467. return r;
  3468. }
  3469. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  3470. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  3471. {
  3472. char instruction[3];
  3473. unsigned long rip = kvm_rip_read(vcpu);
  3474. /*
  3475. * Blow out the MMU to ensure that no other VCPU has an active mapping
  3476. * to ensure that the updated hypercall appears atomically across all
  3477. * VCPUs.
  3478. */
  3479. kvm_mmu_zap_all(vcpu->kvm);
  3480. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  3481. return emulator_write_emulated(rip, instruction, 3, vcpu);
  3482. }
  3483. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  3484. {
  3485. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  3486. }
  3487. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  3488. {
  3489. struct descriptor_table dt = { limit, base };
  3490. kvm_x86_ops->set_gdt(vcpu, &dt);
  3491. }
  3492. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  3493. {
  3494. struct descriptor_table dt = { limit, base };
  3495. kvm_x86_ops->set_idt(vcpu, &dt);
  3496. }
  3497. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  3498. unsigned long *rflags)
  3499. {
  3500. kvm_lmsw(vcpu, msw);
  3501. *rflags = kvm_get_rflags(vcpu);
  3502. }
  3503. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  3504. {
  3505. unsigned long value;
  3506. switch (cr) {
  3507. case 0:
  3508. value = kvm_read_cr0(vcpu);
  3509. break;
  3510. case 2:
  3511. value = vcpu->arch.cr2;
  3512. break;
  3513. case 3:
  3514. value = vcpu->arch.cr3;
  3515. break;
  3516. case 4:
  3517. value = kvm_read_cr4(vcpu);
  3518. break;
  3519. case 8:
  3520. value = kvm_get_cr8(vcpu);
  3521. break;
  3522. default:
  3523. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3524. return 0;
  3525. }
  3526. return value;
  3527. }
  3528. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  3529. unsigned long *rflags)
  3530. {
  3531. switch (cr) {
  3532. case 0:
  3533. kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  3534. *rflags = kvm_get_rflags(vcpu);
  3535. break;
  3536. case 2:
  3537. vcpu->arch.cr2 = val;
  3538. break;
  3539. case 3:
  3540. kvm_set_cr3(vcpu, val);
  3541. break;
  3542. case 4:
  3543. kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  3544. break;
  3545. case 8:
  3546. kvm_set_cr8(vcpu, val & 0xfUL);
  3547. break;
  3548. default:
  3549. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3550. }
  3551. }
  3552. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  3553. {
  3554. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  3555. int j, nent = vcpu->arch.cpuid_nent;
  3556. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  3557. /* when no next entry is found, the current entry[i] is reselected */
  3558. for (j = i + 1; ; j = (j + 1) % nent) {
  3559. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  3560. if (ej->function == e->function) {
  3561. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  3562. return j;
  3563. }
  3564. }
  3565. return 0; /* silence gcc, even though control never reaches here */
  3566. }
  3567. /* find an entry with matching function, matching index (if needed), and that
  3568. * should be read next (if it's stateful) */
  3569. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  3570. u32 function, u32 index)
  3571. {
  3572. if (e->function != function)
  3573. return 0;
  3574. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  3575. return 0;
  3576. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  3577. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  3578. return 0;
  3579. return 1;
  3580. }
  3581. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  3582. u32 function, u32 index)
  3583. {
  3584. int i;
  3585. struct kvm_cpuid_entry2 *best = NULL;
  3586. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  3587. struct kvm_cpuid_entry2 *e;
  3588. e = &vcpu->arch.cpuid_entries[i];
  3589. if (is_matching_cpuid_entry(e, function, index)) {
  3590. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  3591. move_to_next_stateful_cpuid_entry(vcpu, i);
  3592. best = e;
  3593. break;
  3594. }
  3595. /*
  3596. * Both basic or both extended?
  3597. */
  3598. if (((e->function ^ function) & 0x80000000) == 0)
  3599. if (!best || e->function > best->function)
  3600. best = e;
  3601. }
  3602. return best;
  3603. }
  3604. EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
  3605. int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
  3606. {
  3607. struct kvm_cpuid_entry2 *best;
  3608. best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
  3609. if (best)
  3610. return best->eax & 0xff;
  3611. return 36;
  3612. }
  3613. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  3614. {
  3615. u32 function, index;
  3616. struct kvm_cpuid_entry2 *best;
  3617. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3618. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3619. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  3620. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  3621. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  3622. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  3623. best = kvm_find_cpuid_entry(vcpu, function, index);
  3624. if (best) {
  3625. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  3626. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  3627. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  3628. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  3629. }
  3630. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3631. trace_kvm_cpuid(function,
  3632. kvm_register_read(vcpu, VCPU_REGS_RAX),
  3633. kvm_register_read(vcpu, VCPU_REGS_RBX),
  3634. kvm_register_read(vcpu, VCPU_REGS_RCX),
  3635. kvm_register_read(vcpu, VCPU_REGS_RDX));
  3636. }
  3637. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  3638. /*
  3639. * Check if userspace requested an interrupt window, and that the
  3640. * interrupt window is open.
  3641. *
  3642. * No need to exit to userspace if we already have an interrupt queued.
  3643. */
  3644. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  3645. {
  3646. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  3647. vcpu->run->request_interrupt_window &&
  3648. kvm_arch_interrupt_allowed(vcpu));
  3649. }
  3650. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  3651. {
  3652. struct kvm_run *kvm_run = vcpu->run;
  3653. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  3654. kvm_run->cr8 = kvm_get_cr8(vcpu);
  3655. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  3656. if (irqchip_in_kernel(vcpu->kvm))
  3657. kvm_run->ready_for_interrupt_injection = 1;
  3658. else
  3659. kvm_run->ready_for_interrupt_injection =
  3660. kvm_arch_interrupt_allowed(vcpu) &&
  3661. !kvm_cpu_has_interrupt(vcpu) &&
  3662. !kvm_event_needs_reinjection(vcpu);
  3663. }
  3664. static void vapic_enter(struct kvm_vcpu *vcpu)
  3665. {
  3666. struct kvm_lapic *apic = vcpu->arch.apic;
  3667. struct page *page;
  3668. if (!apic || !apic->vapic_addr)
  3669. return;
  3670. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3671. vcpu->arch.apic->vapic_page = page;
  3672. }
  3673. static void vapic_exit(struct kvm_vcpu *vcpu)
  3674. {
  3675. struct kvm_lapic *apic = vcpu->arch.apic;
  3676. int idx;
  3677. if (!apic || !apic->vapic_addr)
  3678. return;
  3679. idx = srcu_read_lock(&vcpu->kvm->srcu);
  3680. kvm_release_page_dirty(apic->vapic_page);
  3681. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3682. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  3683. }
  3684. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  3685. {
  3686. int max_irr, tpr;
  3687. if (!kvm_x86_ops->update_cr8_intercept)
  3688. return;
  3689. if (!vcpu->arch.apic)
  3690. return;
  3691. if (!vcpu->arch.apic->vapic_addr)
  3692. max_irr = kvm_lapic_find_highest_irr(vcpu);
  3693. else
  3694. max_irr = -1;
  3695. if (max_irr != -1)
  3696. max_irr >>= 4;
  3697. tpr = kvm_lapic_get_cr8(vcpu);
  3698. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  3699. }
  3700. static void inject_pending_event(struct kvm_vcpu *vcpu)
  3701. {
  3702. /* try to reinject previous events if any */
  3703. if (vcpu->arch.exception.pending) {
  3704. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  3705. vcpu->arch.exception.has_error_code,
  3706. vcpu->arch.exception.error_code);
  3707. return;
  3708. }
  3709. if (vcpu->arch.nmi_injected) {
  3710. kvm_x86_ops->set_nmi(vcpu);
  3711. return;
  3712. }
  3713. if (vcpu->arch.interrupt.pending) {
  3714. kvm_x86_ops->set_irq(vcpu);
  3715. return;
  3716. }
  3717. /* try to inject new event if pending */
  3718. if (vcpu->arch.nmi_pending) {
  3719. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  3720. vcpu->arch.nmi_pending = false;
  3721. vcpu->arch.nmi_injected = true;
  3722. kvm_x86_ops->set_nmi(vcpu);
  3723. }
  3724. } else if (kvm_cpu_has_interrupt(vcpu)) {
  3725. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  3726. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  3727. false);
  3728. kvm_x86_ops->set_irq(vcpu);
  3729. }
  3730. }
  3731. }
  3732. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  3733. {
  3734. int r;
  3735. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  3736. vcpu->run->request_interrupt_window;
  3737. if (vcpu->requests)
  3738. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  3739. kvm_mmu_unload(vcpu);
  3740. r = kvm_mmu_reload(vcpu);
  3741. if (unlikely(r))
  3742. goto out;
  3743. if (vcpu->requests) {
  3744. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  3745. __kvm_migrate_timers(vcpu);
  3746. if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
  3747. kvm_write_guest_time(vcpu);
  3748. if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
  3749. kvm_mmu_sync_roots(vcpu);
  3750. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  3751. kvm_x86_ops->tlb_flush(vcpu);
  3752. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  3753. &vcpu->requests)) {
  3754. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  3755. r = 0;
  3756. goto out;
  3757. }
  3758. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  3759. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  3760. r = 0;
  3761. goto out;
  3762. }
  3763. if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
  3764. vcpu->fpu_active = 0;
  3765. kvm_x86_ops->fpu_deactivate(vcpu);
  3766. }
  3767. }
  3768. preempt_disable();
  3769. kvm_x86_ops->prepare_guest_switch(vcpu);
  3770. if (vcpu->fpu_active)
  3771. kvm_load_guest_fpu(vcpu);
  3772. local_irq_disable();
  3773. clear_bit(KVM_REQ_KICK, &vcpu->requests);
  3774. smp_mb__after_clear_bit();
  3775. if (vcpu->requests || need_resched() || signal_pending(current)) {
  3776. set_bit(KVM_REQ_KICK, &vcpu->requests);
  3777. local_irq_enable();
  3778. preempt_enable();
  3779. r = 1;
  3780. goto out;
  3781. }
  3782. inject_pending_event(vcpu);
  3783. /* enable NMI/IRQ window open exits if needed */
  3784. if (vcpu->arch.nmi_pending)
  3785. kvm_x86_ops->enable_nmi_window(vcpu);
  3786. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  3787. kvm_x86_ops->enable_irq_window(vcpu);
  3788. if (kvm_lapic_enabled(vcpu)) {
  3789. update_cr8_intercept(vcpu);
  3790. kvm_lapic_sync_to_vapic(vcpu);
  3791. }
  3792. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  3793. kvm_guest_enter();
  3794. if (unlikely(vcpu->arch.switch_db_regs)) {
  3795. set_debugreg(0, 7);
  3796. set_debugreg(vcpu->arch.eff_db[0], 0);
  3797. set_debugreg(vcpu->arch.eff_db[1], 1);
  3798. set_debugreg(vcpu->arch.eff_db[2], 2);
  3799. set_debugreg(vcpu->arch.eff_db[3], 3);
  3800. }
  3801. trace_kvm_entry(vcpu->vcpu_id);
  3802. kvm_x86_ops->run(vcpu);
  3803. /*
  3804. * If the guest has used debug registers, at least dr7
  3805. * will be disabled while returning to the host.
  3806. * If we don't have active breakpoints in the host, we don't
  3807. * care about the messed up debug address registers. But if
  3808. * we have some of them active, restore the old state.
  3809. */
  3810. if (hw_breakpoint_active())
  3811. hw_breakpoint_restore();
  3812. set_bit(KVM_REQ_KICK, &vcpu->requests);
  3813. local_irq_enable();
  3814. ++vcpu->stat.exits;
  3815. /*
  3816. * We must have an instruction between local_irq_enable() and
  3817. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  3818. * the interrupt shadow. The stat.exits increment will do nicely.
  3819. * But we need to prevent reordering, hence this barrier():
  3820. */
  3821. barrier();
  3822. kvm_guest_exit();
  3823. preempt_enable();
  3824. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  3825. /*
  3826. * Profile KVM exit RIPs:
  3827. */
  3828. if (unlikely(prof_on == KVM_PROFILING)) {
  3829. unsigned long rip = kvm_rip_read(vcpu);
  3830. profile_hit(KVM_PROFILING, (void *)rip);
  3831. }
  3832. kvm_lapic_sync_from_vapic(vcpu);
  3833. r = kvm_x86_ops->handle_exit(vcpu);
  3834. out:
  3835. return r;
  3836. }
  3837. static int __vcpu_run(struct kvm_vcpu *vcpu)
  3838. {
  3839. int r;
  3840. struct kvm *kvm = vcpu->kvm;
  3841. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  3842. pr_debug("vcpu %d received sipi with vector # %x\n",
  3843. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  3844. kvm_lapic_reset(vcpu);
  3845. r = kvm_arch_vcpu_reset(vcpu);
  3846. if (r)
  3847. return r;
  3848. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3849. }
  3850. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  3851. vapic_enter(vcpu);
  3852. r = 1;
  3853. while (r > 0) {
  3854. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  3855. r = vcpu_enter_guest(vcpu);
  3856. else {
  3857. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  3858. kvm_vcpu_block(vcpu);
  3859. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  3860. if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
  3861. {
  3862. switch(vcpu->arch.mp_state) {
  3863. case KVM_MP_STATE_HALTED:
  3864. vcpu->arch.mp_state =
  3865. KVM_MP_STATE_RUNNABLE;
  3866. case KVM_MP_STATE_RUNNABLE:
  3867. break;
  3868. case KVM_MP_STATE_SIPI_RECEIVED:
  3869. default:
  3870. r = -EINTR;
  3871. break;
  3872. }
  3873. }
  3874. }
  3875. if (r <= 0)
  3876. break;
  3877. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  3878. if (kvm_cpu_has_pending_timer(vcpu))
  3879. kvm_inject_pending_timer_irqs(vcpu);
  3880. if (dm_request_for_irq_injection(vcpu)) {
  3881. r = -EINTR;
  3882. vcpu->run->exit_reason = KVM_EXIT_INTR;
  3883. ++vcpu->stat.request_irq_exits;
  3884. }
  3885. if (signal_pending(current)) {
  3886. r = -EINTR;
  3887. vcpu->run->exit_reason = KVM_EXIT_INTR;
  3888. ++vcpu->stat.signal_exits;
  3889. }
  3890. if (need_resched()) {
  3891. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  3892. kvm_resched(vcpu);
  3893. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  3894. }
  3895. }
  3896. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  3897. post_kvm_run_save(vcpu);
  3898. vapic_exit(vcpu);
  3899. return r;
  3900. }
  3901. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  3902. {
  3903. int r;
  3904. sigset_t sigsaved;
  3905. vcpu_load(vcpu);
  3906. if (vcpu->sigset_active)
  3907. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  3908. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  3909. kvm_vcpu_block(vcpu);
  3910. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  3911. r = -EAGAIN;
  3912. goto out;
  3913. }
  3914. /* re-sync apic's tpr */
  3915. if (!irqchip_in_kernel(vcpu->kvm))
  3916. kvm_set_cr8(vcpu, kvm_run->cr8);
  3917. if (vcpu->arch.pio.cur_count) {
  3918. r = complete_pio(vcpu);
  3919. if (r)
  3920. goto out;
  3921. }
  3922. if (vcpu->mmio_needed) {
  3923. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  3924. vcpu->mmio_read_completed = 1;
  3925. vcpu->mmio_needed = 0;
  3926. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  3927. r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
  3928. EMULTYPE_NO_DECODE);
  3929. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  3930. if (r == EMULATE_DO_MMIO) {
  3931. /*
  3932. * Read-modify-write. Back to userspace.
  3933. */
  3934. r = 0;
  3935. goto out;
  3936. }
  3937. }
  3938. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  3939. kvm_register_write(vcpu, VCPU_REGS_RAX,
  3940. kvm_run->hypercall.ret);
  3941. r = __vcpu_run(vcpu);
  3942. out:
  3943. if (vcpu->sigset_active)
  3944. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  3945. vcpu_put(vcpu);
  3946. return r;
  3947. }
  3948. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3949. {
  3950. vcpu_load(vcpu);
  3951. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3952. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3953. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3954. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3955. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3956. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3957. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3958. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3959. #ifdef CONFIG_X86_64
  3960. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  3961. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  3962. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  3963. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  3964. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  3965. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  3966. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  3967. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  3968. #endif
  3969. regs->rip = kvm_rip_read(vcpu);
  3970. regs->rflags = kvm_get_rflags(vcpu);
  3971. vcpu_put(vcpu);
  3972. return 0;
  3973. }
  3974. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3975. {
  3976. vcpu_load(vcpu);
  3977. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  3978. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  3979. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  3980. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  3981. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  3982. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  3983. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  3984. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  3985. #ifdef CONFIG_X86_64
  3986. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  3987. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  3988. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  3989. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  3990. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  3991. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  3992. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  3993. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  3994. #endif
  3995. kvm_rip_write(vcpu, regs->rip);
  3996. kvm_set_rflags(vcpu, regs->rflags);
  3997. vcpu->arch.exception.pending = false;
  3998. vcpu_put(vcpu);
  3999. return 0;
  4000. }
  4001. void kvm_get_segment(struct kvm_vcpu *vcpu,
  4002. struct kvm_segment *var, int seg)
  4003. {
  4004. kvm_x86_ops->get_segment(vcpu, var, seg);
  4005. }
  4006. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4007. {
  4008. struct kvm_segment cs;
  4009. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4010. *db = cs.db;
  4011. *l = cs.l;
  4012. }
  4013. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  4014. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  4015. struct kvm_sregs *sregs)
  4016. {
  4017. struct descriptor_table dt;
  4018. vcpu_load(vcpu);
  4019. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4020. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4021. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4022. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4023. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4024. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4025. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4026. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4027. kvm_x86_ops->get_idt(vcpu, &dt);
  4028. sregs->idt.limit = dt.limit;
  4029. sregs->idt.base = dt.base;
  4030. kvm_x86_ops->get_gdt(vcpu, &dt);
  4031. sregs->gdt.limit = dt.limit;
  4032. sregs->gdt.base = dt.base;
  4033. sregs->cr0 = kvm_read_cr0(vcpu);
  4034. sregs->cr2 = vcpu->arch.cr2;
  4035. sregs->cr3 = vcpu->arch.cr3;
  4036. sregs->cr4 = kvm_read_cr4(vcpu);
  4037. sregs->cr8 = kvm_get_cr8(vcpu);
  4038. sregs->efer = vcpu->arch.efer;
  4039. sregs->apic_base = kvm_get_apic_base(vcpu);
  4040. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  4041. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  4042. set_bit(vcpu->arch.interrupt.nr,
  4043. (unsigned long *)sregs->interrupt_bitmap);
  4044. vcpu_put(vcpu);
  4045. return 0;
  4046. }
  4047. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  4048. struct kvm_mp_state *mp_state)
  4049. {
  4050. vcpu_load(vcpu);
  4051. mp_state->mp_state = vcpu->arch.mp_state;
  4052. vcpu_put(vcpu);
  4053. return 0;
  4054. }
  4055. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  4056. struct kvm_mp_state *mp_state)
  4057. {
  4058. vcpu_load(vcpu);
  4059. vcpu->arch.mp_state = mp_state->mp_state;
  4060. vcpu_put(vcpu);
  4061. return 0;
  4062. }
  4063. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  4064. struct kvm_segment *var, int seg)
  4065. {
  4066. kvm_x86_ops->set_segment(vcpu, var, seg);
  4067. }
  4068. static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
  4069. struct kvm_segment *kvm_desct)
  4070. {
  4071. kvm_desct->base = get_desc_base(seg_desc);
  4072. kvm_desct->limit = get_desc_limit(seg_desc);
  4073. if (seg_desc->g) {
  4074. kvm_desct->limit <<= 12;
  4075. kvm_desct->limit |= 0xfff;
  4076. }
  4077. kvm_desct->selector = selector;
  4078. kvm_desct->type = seg_desc->type;
  4079. kvm_desct->present = seg_desc->p;
  4080. kvm_desct->dpl = seg_desc->dpl;
  4081. kvm_desct->db = seg_desc->d;
  4082. kvm_desct->s = seg_desc->s;
  4083. kvm_desct->l = seg_desc->l;
  4084. kvm_desct->g = seg_desc->g;
  4085. kvm_desct->avl = seg_desc->avl;
  4086. if (!selector)
  4087. kvm_desct->unusable = 1;
  4088. else
  4089. kvm_desct->unusable = 0;
  4090. kvm_desct->padding = 0;
  4091. }
  4092. static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
  4093. u16 selector,
  4094. struct descriptor_table *dtable)
  4095. {
  4096. if (selector & 1 << 2) {
  4097. struct kvm_segment kvm_seg;
  4098. kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
  4099. if (kvm_seg.unusable)
  4100. dtable->limit = 0;
  4101. else
  4102. dtable->limit = kvm_seg.limit;
  4103. dtable->base = kvm_seg.base;
  4104. }
  4105. else
  4106. kvm_x86_ops->get_gdt(vcpu, dtable);
  4107. }
  4108. /* allowed just for 8 bytes segments */
  4109. static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  4110. struct desc_struct *seg_desc)
  4111. {
  4112. struct descriptor_table dtable;
  4113. u16 index = selector >> 3;
  4114. int ret;
  4115. u32 err;
  4116. gva_t addr;
  4117. get_segment_descriptor_dtable(vcpu, selector, &dtable);
  4118. if (dtable.limit < index * 8 + 7) {
  4119. kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
  4120. return X86EMUL_PROPAGATE_FAULT;
  4121. }
  4122. addr = dtable.base + index * 8;
  4123. ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc),
  4124. vcpu, &err);
  4125. if (ret == X86EMUL_PROPAGATE_FAULT)
  4126. kvm_inject_page_fault(vcpu, addr, err);
  4127. return ret;
  4128. }
  4129. /* allowed just for 8 bytes segments */
  4130. static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  4131. struct desc_struct *seg_desc)
  4132. {
  4133. struct descriptor_table dtable;
  4134. u16 index = selector >> 3;
  4135. get_segment_descriptor_dtable(vcpu, selector, &dtable);
  4136. if (dtable.limit < index * 8 + 7)
  4137. return 1;
  4138. return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL);
  4139. }
  4140. static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu,
  4141. struct desc_struct *seg_desc)
  4142. {
  4143. u32 base_addr = get_desc_base(seg_desc);
  4144. return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL);
  4145. }
  4146. static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu,
  4147. struct desc_struct *seg_desc)
  4148. {
  4149. u32 base_addr = get_desc_base(seg_desc);
  4150. return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL);
  4151. }
  4152. static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
  4153. {
  4154. struct kvm_segment kvm_seg;
  4155. kvm_get_segment(vcpu, &kvm_seg, seg);
  4156. return kvm_seg.selector;
  4157. }
  4158. static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
  4159. {
  4160. struct kvm_segment segvar = {
  4161. .base = selector << 4,
  4162. .limit = 0xffff,
  4163. .selector = selector,
  4164. .type = 3,
  4165. .present = 1,
  4166. .dpl = 3,
  4167. .db = 0,
  4168. .s = 1,
  4169. .l = 0,
  4170. .g = 0,
  4171. .avl = 0,
  4172. .unusable = 0,
  4173. };
  4174. kvm_x86_ops->set_segment(vcpu, &segvar, seg);
  4175. return X86EMUL_CONTINUE;
  4176. }
  4177. static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
  4178. {
  4179. return (seg != VCPU_SREG_LDTR) &&
  4180. (seg != VCPU_SREG_TR) &&
  4181. (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
  4182. }
  4183. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg)
  4184. {
  4185. struct kvm_segment kvm_seg;
  4186. struct desc_struct seg_desc;
  4187. u8 dpl, rpl, cpl;
  4188. unsigned err_vec = GP_VECTOR;
  4189. u32 err_code = 0;
  4190. bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
  4191. int ret;
  4192. if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu))
  4193. return kvm_load_realmode_segment(vcpu, selector, seg);
  4194. /* NULL selector is not valid for TR, CS and SS */
  4195. if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
  4196. && null_selector)
  4197. goto exception;
  4198. /* TR should be in GDT only */
  4199. if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
  4200. goto exception;
  4201. ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc);
  4202. if (ret)
  4203. return ret;
  4204. seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg);
  4205. if (null_selector) { /* for NULL selector skip all following checks */
  4206. kvm_seg.unusable = 1;
  4207. goto load;
  4208. }
  4209. err_code = selector & 0xfffc;
  4210. err_vec = GP_VECTOR;
  4211. /* can't load system descriptor into segment selecor */
  4212. if (seg <= VCPU_SREG_GS && !kvm_seg.s)
  4213. goto exception;
  4214. if (!kvm_seg.present) {
  4215. err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
  4216. goto exception;
  4217. }
  4218. rpl = selector & 3;
  4219. dpl = kvm_seg.dpl;
  4220. cpl = kvm_x86_ops->get_cpl(vcpu);
  4221. switch (seg) {
  4222. case VCPU_SREG_SS:
  4223. /*
  4224. * segment is not a writable data segment or segment
  4225. * selector's RPL != CPL or segment selector's RPL != CPL
  4226. */
  4227. if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl)
  4228. goto exception;
  4229. break;
  4230. case VCPU_SREG_CS:
  4231. if (!(kvm_seg.type & 8))
  4232. goto exception;
  4233. if (kvm_seg.type & 4) {
  4234. /* conforming */
  4235. if (dpl > cpl)
  4236. goto exception;
  4237. } else {
  4238. /* nonconforming */
  4239. if (rpl > cpl || dpl != cpl)
  4240. goto exception;
  4241. }
  4242. /* CS(RPL) <- CPL */
  4243. selector = (selector & 0xfffc) | cpl;
  4244. break;
  4245. case VCPU_SREG_TR:
  4246. if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9))
  4247. goto exception;
  4248. break;
  4249. case VCPU_SREG_LDTR:
  4250. if (kvm_seg.s || kvm_seg.type != 2)
  4251. goto exception;
  4252. break;
  4253. default: /* DS, ES, FS, or GS */
  4254. /*
  4255. * segment is not a data or readable code segment or
  4256. * ((segment is a data or nonconforming code segment)
  4257. * and (both RPL and CPL > DPL))
  4258. */
  4259. if ((kvm_seg.type & 0xa) == 0x8 ||
  4260. (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl)))
  4261. goto exception;
  4262. break;
  4263. }
  4264. if (!kvm_seg.unusable && kvm_seg.s) {
  4265. /* mark segment as accessed */
  4266. kvm_seg.type |= 1;
  4267. seg_desc.type |= 1;
  4268. save_guest_segment_descriptor(vcpu, selector, &seg_desc);
  4269. }
  4270. load:
  4271. kvm_set_segment(vcpu, &kvm_seg, seg);
  4272. return X86EMUL_CONTINUE;
  4273. exception:
  4274. kvm_queue_exception_e(vcpu, err_vec, err_code);
  4275. return X86EMUL_PROPAGATE_FAULT;
  4276. }
  4277. static void save_state_to_tss32(struct kvm_vcpu *vcpu,
  4278. struct tss_segment_32 *tss)
  4279. {
  4280. tss->cr3 = vcpu->arch.cr3;
  4281. tss->eip = kvm_rip_read(vcpu);
  4282. tss->eflags = kvm_get_rflags(vcpu);
  4283. tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4284. tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4285. tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4286. tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4287. tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  4288. tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  4289. tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4290. tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  4291. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  4292. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  4293. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  4294. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  4295. tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
  4296. tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
  4297. tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  4298. }
  4299. static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg)
  4300. {
  4301. struct kvm_segment kvm_seg;
  4302. kvm_get_segment(vcpu, &kvm_seg, seg);
  4303. kvm_seg.selector = sel;
  4304. kvm_set_segment(vcpu, &kvm_seg, seg);
  4305. }
  4306. static int load_state_from_tss32(struct kvm_vcpu *vcpu,
  4307. struct tss_segment_32 *tss)
  4308. {
  4309. kvm_set_cr3(vcpu, tss->cr3);
  4310. kvm_rip_write(vcpu, tss->eip);
  4311. kvm_set_rflags(vcpu, tss->eflags | 2);
  4312. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
  4313. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
  4314. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
  4315. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
  4316. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
  4317. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
  4318. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
  4319. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
  4320. /*
  4321. * SDM says that segment selectors are loaded before segment
  4322. * descriptors
  4323. */
  4324. kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR);
  4325. kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
  4326. kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
  4327. kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
  4328. kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
  4329. kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS);
  4330. kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS);
  4331. /*
  4332. * Now load segment descriptors. If fault happenes at this stage
  4333. * it is handled in a context of new task
  4334. */
  4335. if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR))
  4336. return 1;
  4337. if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
  4338. return 1;
  4339. if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
  4340. return 1;
  4341. if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
  4342. return 1;
  4343. if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
  4344. return 1;
  4345. if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS))
  4346. return 1;
  4347. if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS))
  4348. return 1;
  4349. return 0;
  4350. }
  4351. static void save_state_to_tss16(struct kvm_vcpu *vcpu,
  4352. struct tss_segment_16 *tss)
  4353. {
  4354. tss->ip = kvm_rip_read(vcpu);
  4355. tss->flag = kvm_get_rflags(vcpu);
  4356. tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4357. tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4358. tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4359. tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4360. tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  4361. tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  4362. tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4363. tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
  4364. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  4365. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  4366. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  4367. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  4368. tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  4369. }
  4370. static int load_state_from_tss16(struct kvm_vcpu *vcpu,
  4371. struct tss_segment_16 *tss)
  4372. {
  4373. kvm_rip_write(vcpu, tss->ip);
  4374. kvm_set_rflags(vcpu, tss->flag | 2);
  4375. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
  4376. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
  4377. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
  4378. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
  4379. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
  4380. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
  4381. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
  4382. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
  4383. /*
  4384. * SDM says that segment selectors are loaded before segment
  4385. * descriptors
  4386. */
  4387. kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR);
  4388. kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
  4389. kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
  4390. kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
  4391. kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
  4392. /*
  4393. * Now load segment descriptors. If fault happenes at this stage
  4394. * it is handled in a context of new task
  4395. */
  4396. if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR))
  4397. return 1;
  4398. if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
  4399. return 1;
  4400. if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
  4401. return 1;
  4402. if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
  4403. return 1;
  4404. if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
  4405. return 1;
  4406. return 0;
  4407. }
  4408. static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
  4409. u16 old_tss_sel, u32 old_tss_base,
  4410. struct desc_struct *nseg_desc)
  4411. {
  4412. struct tss_segment_16 tss_segment_16;
  4413. int ret = 0;
  4414. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  4415. sizeof tss_segment_16))
  4416. goto out;
  4417. save_state_to_tss16(vcpu, &tss_segment_16);
  4418. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  4419. sizeof tss_segment_16))
  4420. goto out;
  4421. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
  4422. &tss_segment_16, sizeof tss_segment_16))
  4423. goto out;
  4424. if (old_tss_sel != 0xffff) {
  4425. tss_segment_16.prev_task_link = old_tss_sel;
  4426. if (kvm_write_guest(vcpu->kvm,
  4427. get_tss_base_addr_write(vcpu, nseg_desc),
  4428. &tss_segment_16.prev_task_link,
  4429. sizeof tss_segment_16.prev_task_link))
  4430. goto out;
  4431. }
  4432. if (load_state_from_tss16(vcpu, &tss_segment_16))
  4433. goto out;
  4434. ret = 1;
  4435. out:
  4436. return ret;
  4437. }
  4438. static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
  4439. u16 old_tss_sel, u32 old_tss_base,
  4440. struct desc_struct *nseg_desc)
  4441. {
  4442. struct tss_segment_32 tss_segment_32;
  4443. int ret = 0;
  4444. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  4445. sizeof tss_segment_32))
  4446. goto out;
  4447. save_state_to_tss32(vcpu, &tss_segment_32);
  4448. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  4449. sizeof tss_segment_32))
  4450. goto out;
  4451. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
  4452. &tss_segment_32, sizeof tss_segment_32))
  4453. goto out;
  4454. if (old_tss_sel != 0xffff) {
  4455. tss_segment_32.prev_task_link = old_tss_sel;
  4456. if (kvm_write_guest(vcpu->kvm,
  4457. get_tss_base_addr_write(vcpu, nseg_desc),
  4458. &tss_segment_32.prev_task_link,
  4459. sizeof tss_segment_32.prev_task_link))
  4460. goto out;
  4461. }
  4462. if (load_state_from_tss32(vcpu, &tss_segment_32))
  4463. goto out;
  4464. ret = 1;
  4465. out:
  4466. return ret;
  4467. }
  4468. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
  4469. {
  4470. struct kvm_segment tr_seg;
  4471. struct desc_struct cseg_desc;
  4472. struct desc_struct nseg_desc;
  4473. int ret = 0;
  4474. u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
  4475. u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
  4476. old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL);
  4477. /* FIXME: Handle errors. Failure to read either TSS or their
  4478. * descriptors should generate a pagefault.
  4479. */
  4480. if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
  4481. goto out;
  4482. if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
  4483. goto out;
  4484. if (reason != TASK_SWITCH_IRET) {
  4485. int cpl;
  4486. cpl = kvm_x86_ops->get_cpl(vcpu);
  4487. if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
  4488. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  4489. return 1;
  4490. }
  4491. }
  4492. if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
  4493. kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
  4494. return 1;
  4495. }
  4496. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  4497. cseg_desc.type &= ~(1 << 1); //clear the B flag
  4498. save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
  4499. }
  4500. if (reason == TASK_SWITCH_IRET) {
  4501. u32 eflags = kvm_get_rflags(vcpu);
  4502. kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
  4503. }
  4504. /* set back link to prev task only if NT bit is set in eflags
  4505. note that old_tss_sel is not used afetr this point */
  4506. if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
  4507. old_tss_sel = 0xffff;
  4508. if (nseg_desc.type & 8)
  4509. ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
  4510. old_tss_base, &nseg_desc);
  4511. else
  4512. ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
  4513. old_tss_base, &nseg_desc);
  4514. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
  4515. u32 eflags = kvm_get_rflags(vcpu);
  4516. kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
  4517. }
  4518. if (reason != TASK_SWITCH_IRET) {
  4519. nseg_desc.type |= (1 << 1);
  4520. save_guest_segment_descriptor(vcpu, tss_selector,
  4521. &nseg_desc);
  4522. }
  4523. kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS);
  4524. seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
  4525. tr_seg.type = 11;
  4526. kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
  4527. out:
  4528. return ret;
  4529. }
  4530. EXPORT_SYMBOL_GPL(kvm_task_switch);
  4531. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  4532. struct kvm_sregs *sregs)
  4533. {
  4534. int mmu_reset_needed = 0;
  4535. int pending_vec, max_bits;
  4536. struct descriptor_table dt;
  4537. vcpu_load(vcpu);
  4538. dt.limit = sregs->idt.limit;
  4539. dt.base = sregs->idt.base;
  4540. kvm_x86_ops->set_idt(vcpu, &dt);
  4541. dt.limit = sregs->gdt.limit;
  4542. dt.base = sregs->gdt.base;
  4543. kvm_x86_ops->set_gdt(vcpu, &dt);
  4544. vcpu->arch.cr2 = sregs->cr2;
  4545. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  4546. vcpu->arch.cr3 = sregs->cr3;
  4547. kvm_set_cr8(vcpu, sregs->cr8);
  4548. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  4549. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  4550. kvm_set_apic_base(vcpu, sregs->apic_base);
  4551. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  4552. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  4553. vcpu->arch.cr0 = sregs->cr0;
  4554. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  4555. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  4556. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  4557. load_pdptrs(vcpu, vcpu->arch.cr3);
  4558. mmu_reset_needed = 1;
  4559. }
  4560. if (mmu_reset_needed)
  4561. kvm_mmu_reset_context(vcpu);
  4562. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  4563. pending_vec = find_first_bit(
  4564. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  4565. if (pending_vec < max_bits) {
  4566. kvm_queue_interrupt(vcpu, pending_vec, false);
  4567. pr_debug("Set back pending irq %d\n", pending_vec);
  4568. if (irqchip_in_kernel(vcpu->kvm))
  4569. kvm_pic_clear_isr_ack(vcpu->kvm);
  4570. }
  4571. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4572. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4573. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4574. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4575. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4576. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4577. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4578. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4579. update_cr8_intercept(vcpu);
  4580. /* Older userspace won't unhalt the vcpu on reset. */
  4581. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  4582. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  4583. !is_protmode(vcpu))
  4584. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4585. vcpu_put(vcpu);
  4586. return 0;
  4587. }
  4588. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  4589. struct kvm_guest_debug *dbg)
  4590. {
  4591. unsigned long rflags;
  4592. int i, r;
  4593. vcpu_load(vcpu);
  4594. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  4595. r = -EBUSY;
  4596. if (vcpu->arch.exception.pending)
  4597. goto unlock_out;
  4598. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  4599. kvm_queue_exception(vcpu, DB_VECTOR);
  4600. else
  4601. kvm_queue_exception(vcpu, BP_VECTOR);
  4602. }
  4603. /*
  4604. * Read rflags as long as potentially injected trace flags are still
  4605. * filtered out.
  4606. */
  4607. rflags = kvm_get_rflags(vcpu);
  4608. vcpu->guest_debug = dbg->control;
  4609. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  4610. vcpu->guest_debug = 0;
  4611. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4612. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  4613. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  4614. vcpu->arch.switch_db_regs =
  4615. (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
  4616. } else {
  4617. for (i = 0; i < KVM_NR_DB_REGS; i++)
  4618. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  4619. vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
  4620. }
  4621. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  4622. vcpu->arch.singlestep_cs =
  4623. get_segment_selector(vcpu, VCPU_SREG_CS);
  4624. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
  4625. }
  4626. /*
  4627. * Trigger an rflags update that will inject or remove the trace
  4628. * flags.
  4629. */
  4630. kvm_set_rflags(vcpu, rflags);
  4631. kvm_x86_ops->set_guest_debug(vcpu, dbg);
  4632. r = 0;
  4633. unlock_out:
  4634. vcpu_put(vcpu);
  4635. return r;
  4636. }
  4637. /*
  4638. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  4639. * we have asm/x86/processor.h
  4640. */
  4641. struct fxsave {
  4642. u16 cwd;
  4643. u16 swd;
  4644. u16 twd;
  4645. u16 fop;
  4646. u64 rip;
  4647. u64 rdp;
  4648. u32 mxcsr;
  4649. u32 mxcsr_mask;
  4650. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  4651. #ifdef CONFIG_X86_64
  4652. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  4653. #else
  4654. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  4655. #endif
  4656. };
  4657. /*
  4658. * Translate a guest virtual address to a guest physical address.
  4659. */
  4660. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  4661. struct kvm_translation *tr)
  4662. {
  4663. unsigned long vaddr = tr->linear_address;
  4664. gpa_t gpa;
  4665. int idx;
  4666. vcpu_load(vcpu);
  4667. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4668. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  4669. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4670. tr->physical_address = gpa;
  4671. tr->valid = gpa != UNMAPPED_GVA;
  4672. tr->writeable = 1;
  4673. tr->usermode = 0;
  4674. vcpu_put(vcpu);
  4675. return 0;
  4676. }
  4677. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4678. {
  4679. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  4680. vcpu_load(vcpu);
  4681. memcpy(fpu->fpr, fxsave->st_space, 128);
  4682. fpu->fcw = fxsave->cwd;
  4683. fpu->fsw = fxsave->swd;
  4684. fpu->ftwx = fxsave->twd;
  4685. fpu->last_opcode = fxsave->fop;
  4686. fpu->last_ip = fxsave->rip;
  4687. fpu->last_dp = fxsave->rdp;
  4688. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  4689. vcpu_put(vcpu);
  4690. return 0;
  4691. }
  4692. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4693. {
  4694. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  4695. vcpu_load(vcpu);
  4696. memcpy(fxsave->st_space, fpu->fpr, 128);
  4697. fxsave->cwd = fpu->fcw;
  4698. fxsave->swd = fpu->fsw;
  4699. fxsave->twd = fpu->ftwx;
  4700. fxsave->fop = fpu->last_opcode;
  4701. fxsave->rip = fpu->last_ip;
  4702. fxsave->rdp = fpu->last_dp;
  4703. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  4704. vcpu_put(vcpu);
  4705. return 0;
  4706. }
  4707. void fx_init(struct kvm_vcpu *vcpu)
  4708. {
  4709. unsigned after_mxcsr_mask;
  4710. /*
  4711. * Touch the fpu the first time in non atomic context as if
  4712. * this is the first fpu instruction the exception handler
  4713. * will fire before the instruction returns and it'll have to
  4714. * allocate ram with GFP_KERNEL.
  4715. */
  4716. if (!used_math())
  4717. kvm_fx_save(&vcpu->arch.host_fx_image);
  4718. /* Initialize guest FPU by resetting ours and saving into guest's */
  4719. preempt_disable();
  4720. kvm_fx_save(&vcpu->arch.host_fx_image);
  4721. kvm_fx_finit();
  4722. kvm_fx_save(&vcpu->arch.guest_fx_image);
  4723. kvm_fx_restore(&vcpu->arch.host_fx_image);
  4724. preempt_enable();
  4725. vcpu->arch.cr0 |= X86_CR0_ET;
  4726. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  4727. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  4728. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  4729. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  4730. }
  4731. EXPORT_SYMBOL_GPL(fx_init);
  4732. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  4733. {
  4734. if (vcpu->guest_fpu_loaded)
  4735. return;
  4736. vcpu->guest_fpu_loaded = 1;
  4737. kvm_fx_save(&vcpu->arch.host_fx_image);
  4738. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  4739. trace_kvm_fpu(1);
  4740. }
  4741. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  4742. {
  4743. if (!vcpu->guest_fpu_loaded)
  4744. return;
  4745. vcpu->guest_fpu_loaded = 0;
  4746. kvm_fx_save(&vcpu->arch.guest_fx_image);
  4747. kvm_fx_restore(&vcpu->arch.host_fx_image);
  4748. ++vcpu->stat.fpu_reload;
  4749. set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
  4750. trace_kvm_fpu(0);
  4751. }
  4752. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  4753. {
  4754. if (vcpu->arch.time_page) {
  4755. kvm_release_page_dirty(vcpu->arch.time_page);
  4756. vcpu->arch.time_page = NULL;
  4757. }
  4758. kvm_x86_ops->vcpu_free(vcpu);
  4759. }
  4760. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  4761. unsigned int id)
  4762. {
  4763. return kvm_x86_ops->vcpu_create(kvm, id);
  4764. }
  4765. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  4766. {
  4767. int r;
  4768. /* We do fxsave: this must be aligned. */
  4769. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  4770. vcpu->arch.mtrr_state.have_fixed = 1;
  4771. vcpu_load(vcpu);
  4772. r = kvm_arch_vcpu_reset(vcpu);
  4773. if (r == 0)
  4774. r = kvm_mmu_setup(vcpu);
  4775. vcpu_put(vcpu);
  4776. if (r < 0)
  4777. goto free_vcpu;
  4778. return 0;
  4779. free_vcpu:
  4780. kvm_x86_ops->vcpu_free(vcpu);
  4781. return r;
  4782. }
  4783. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  4784. {
  4785. vcpu_load(vcpu);
  4786. kvm_mmu_unload(vcpu);
  4787. vcpu_put(vcpu);
  4788. kvm_x86_ops->vcpu_free(vcpu);
  4789. }
  4790. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  4791. {
  4792. vcpu->arch.nmi_pending = false;
  4793. vcpu->arch.nmi_injected = false;
  4794. vcpu->arch.switch_db_regs = 0;
  4795. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  4796. vcpu->arch.dr6 = DR6_FIXED_1;
  4797. vcpu->arch.dr7 = DR7_FIXED_1;
  4798. return kvm_x86_ops->vcpu_reset(vcpu);
  4799. }
  4800. int kvm_arch_hardware_enable(void *garbage)
  4801. {
  4802. /*
  4803. * Since this may be called from a hotplug notifcation,
  4804. * we can't get the CPU frequency directly.
  4805. */
  4806. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4807. int cpu = raw_smp_processor_id();
  4808. per_cpu(cpu_tsc_khz, cpu) = 0;
  4809. }
  4810. kvm_shared_msr_cpu_online();
  4811. return kvm_x86_ops->hardware_enable(garbage);
  4812. }
  4813. void kvm_arch_hardware_disable(void *garbage)
  4814. {
  4815. kvm_x86_ops->hardware_disable(garbage);
  4816. drop_user_return_notifiers(garbage);
  4817. }
  4818. int kvm_arch_hardware_setup(void)
  4819. {
  4820. return kvm_x86_ops->hardware_setup();
  4821. }
  4822. void kvm_arch_hardware_unsetup(void)
  4823. {
  4824. kvm_x86_ops->hardware_unsetup();
  4825. }
  4826. void kvm_arch_check_processor_compat(void *rtn)
  4827. {
  4828. kvm_x86_ops->check_processor_compatibility(rtn);
  4829. }
  4830. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  4831. {
  4832. struct page *page;
  4833. struct kvm *kvm;
  4834. int r;
  4835. BUG_ON(vcpu->kvm == NULL);
  4836. kvm = vcpu->kvm;
  4837. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  4838. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  4839. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4840. else
  4841. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  4842. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  4843. if (!page) {
  4844. r = -ENOMEM;
  4845. goto fail;
  4846. }
  4847. vcpu->arch.pio_data = page_address(page);
  4848. r = kvm_mmu_create(vcpu);
  4849. if (r < 0)
  4850. goto fail_free_pio_data;
  4851. if (irqchip_in_kernel(kvm)) {
  4852. r = kvm_create_lapic(vcpu);
  4853. if (r < 0)
  4854. goto fail_mmu_destroy;
  4855. }
  4856. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  4857. GFP_KERNEL);
  4858. if (!vcpu->arch.mce_banks) {
  4859. r = -ENOMEM;
  4860. goto fail_free_lapic;
  4861. }
  4862. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  4863. return 0;
  4864. fail_free_lapic:
  4865. kvm_free_lapic(vcpu);
  4866. fail_mmu_destroy:
  4867. kvm_mmu_destroy(vcpu);
  4868. fail_free_pio_data:
  4869. free_page((unsigned long)vcpu->arch.pio_data);
  4870. fail:
  4871. return r;
  4872. }
  4873. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  4874. {
  4875. int idx;
  4876. kfree(vcpu->arch.mce_banks);
  4877. kvm_free_lapic(vcpu);
  4878. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4879. kvm_mmu_destroy(vcpu);
  4880. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4881. free_page((unsigned long)vcpu->arch.pio_data);
  4882. }
  4883. struct kvm *kvm_arch_create_vm(void)
  4884. {
  4885. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  4886. if (!kvm)
  4887. return ERR_PTR(-ENOMEM);
  4888. kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  4889. if (!kvm->arch.aliases) {
  4890. kfree(kvm);
  4891. return ERR_PTR(-ENOMEM);
  4892. }
  4893. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  4894. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  4895. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  4896. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  4897. rdtscll(kvm->arch.vm_init_tsc);
  4898. return kvm;
  4899. }
  4900. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  4901. {
  4902. vcpu_load(vcpu);
  4903. kvm_mmu_unload(vcpu);
  4904. vcpu_put(vcpu);
  4905. }
  4906. static void kvm_free_vcpus(struct kvm *kvm)
  4907. {
  4908. unsigned int i;
  4909. struct kvm_vcpu *vcpu;
  4910. /*
  4911. * Unpin any mmu pages first.
  4912. */
  4913. kvm_for_each_vcpu(i, vcpu, kvm)
  4914. kvm_unload_vcpu_mmu(vcpu);
  4915. kvm_for_each_vcpu(i, vcpu, kvm)
  4916. kvm_arch_vcpu_free(vcpu);
  4917. mutex_lock(&kvm->lock);
  4918. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  4919. kvm->vcpus[i] = NULL;
  4920. atomic_set(&kvm->online_vcpus, 0);
  4921. mutex_unlock(&kvm->lock);
  4922. }
  4923. void kvm_arch_sync_events(struct kvm *kvm)
  4924. {
  4925. kvm_free_all_assigned_devices(kvm);
  4926. }
  4927. void kvm_arch_destroy_vm(struct kvm *kvm)
  4928. {
  4929. kvm_iommu_unmap_guest(kvm);
  4930. kvm_free_pit(kvm);
  4931. kfree(kvm->arch.vpic);
  4932. kfree(kvm->arch.vioapic);
  4933. kvm_free_vcpus(kvm);
  4934. kvm_free_physmem(kvm);
  4935. if (kvm->arch.apic_access_page)
  4936. put_page(kvm->arch.apic_access_page);
  4937. if (kvm->arch.ept_identity_pagetable)
  4938. put_page(kvm->arch.ept_identity_pagetable);
  4939. cleanup_srcu_struct(&kvm->srcu);
  4940. kfree(kvm->arch.aliases);
  4941. kfree(kvm);
  4942. }
  4943. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  4944. struct kvm_memory_slot *memslot,
  4945. struct kvm_memory_slot old,
  4946. struct kvm_userspace_memory_region *mem,
  4947. int user_alloc)
  4948. {
  4949. int npages = memslot->npages;
  4950. /*To keep backward compatibility with older userspace,
  4951. *x86 needs to hanlde !user_alloc case.
  4952. */
  4953. if (!user_alloc) {
  4954. if (npages && !old.rmap) {
  4955. unsigned long userspace_addr;
  4956. down_write(&current->mm->mmap_sem);
  4957. userspace_addr = do_mmap(NULL, 0,
  4958. npages * PAGE_SIZE,
  4959. PROT_READ | PROT_WRITE,
  4960. MAP_PRIVATE | MAP_ANONYMOUS,
  4961. 0);
  4962. up_write(&current->mm->mmap_sem);
  4963. if (IS_ERR((void *)userspace_addr))
  4964. return PTR_ERR((void *)userspace_addr);
  4965. memslot->userspace_addr = userspace_addr;
  4966. }
  4967. }
  4968. return 0;
  4969. }
  4970. void kvm_arch_commit_memory_region(struct kvm *kvm,
  4971. struct kvm_userspace_memory_region *mem,
  4972. struct kvm_memory_slot old,
  4973. int user_alloc)
  4974. {
  4975. int npages = mem->memory_size >> PAGE_SHIFT;
  4976. if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
  4977. int ret;
  4978. down_write(&current->mm->mmap_sem);
  4979. ret = do_munmap(current->mm, old.userspace_addr,
  4980. old.npages * PAGE_SIZE);
  4981. up_write(&current->mm->mmap_sem);
  4982. if (ret < 0)
  4983. printk(KERN_WARNING
  4984. "kvm_vm_ioctl_set_memory_region: "
  4985. "failed to munmap memory\n");
  4986. }
  4987. spin_lock(&kvm->mmu_lock);
  4988. if (!kvm->arch.n_requested_mmu_pages) {
  4989. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  4990. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  4991. }
  4992. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  4993. spin_unlock(&kvm->mmu_lock);
  4994. }
  4995. void kvm_arch_flush_shadow(struct kvm *kvm)
  4996. {
  4997. kvm_mmu_zap_all(kvm);
  4998. kvm_reload_remote_mmus(kvm);
  4999. }
  5000. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  5001. {
  5002. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  5003. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  5004. || vcpu->arch.nmi_pending ||
  5005. (kvm_arch_interrupt_allowed(vcpu) &&
  5006. kvm_cpu_has_interrupt(vcpu));
  5007. }
  5008. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  5009. {
  5010. int me;
  5011. int cpu = vcpu->cpu;
  5012. if (waitqueue_active(&vcpu->wq)) {
  5013. wake_up_interruptible(&vcpu->wq);
  5014. ++vcpu->stat.halt_wakeup;
  5015. }
  5016. me = get_cpu();
  5017. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  5018. if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
  5019. smp_send_reschedule(cpu);
  5020. put_cpu();
  5021. }
  5022. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  5023. {
  5024. return kvm_x86_ops->interrupt_allowed(vcpu);
  5025. }
  5026. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  5027. {
  5028. unsigned long rflags;
  5029. rflags = kvm_x86_ops->get_rflags(vcpu);
  5030. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5031. rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
  5032. return rflags;
  5033. }
  5034. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  5035. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  5036. {
  5037. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  5038. vcpu->arch.singlestep_cs ==
  5039. get_segment_selector(vcpu, VCPU_SREG_CS) &&
  5040. vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
  5041. rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
  5042. kvm_x86_ops->set_rflags(vcpu, rflags);
  5043. }
  5044. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  5045. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  5046. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  5047. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  5048. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  5049. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  5050. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  5051. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  5052. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  5053. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  5054. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  5055. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);