x86.c 161 KB

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