vmx.c 374 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "cpuid.h"
  21. #include "lapic.h"
  22. #include <linux/kvm_host.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/sched.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/trace_events.h>
  31. #include <linux/slab.h>
  32. #include <linux/tboot.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/frame.h>
  35. #include <linux/nospec.h>
  36. #include "kvm_cache_regs.h"
  37. #include "x86.h"
  38. #include <asm/cpu.h>
  39. #include <asm/io.h>
  40. #include <asm/desc.h>
  41. #include <asm/vmx.h>
  42. #include <asm/virtext.h>
  43. #include <asm/mce.h>
  44. #include <asm/fpu/internal.h>
  45. #include <asm/perf_event.h>
  46. #include <asm/debugreg.h>
  47. #include <asm/kexec.h>
  48. #include <asm/apic.h>
  49. #include <asm/irq_remapping.h>
  50. #include <asm/mmu_context.h>
  51. #include <asm/spec-ctrl.h>
  52. #include <asm/mshyperv.h>
  53. #include "trace.h"
  54. #include "pmu.h"
  55. #include "vmx_evmcs.h"
  56. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  57. #define __ex_clear(x, reg) \
  58. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  59. MODULE_AUTHOR("Qumranet");
  60. MODULE_LICENSE("GPL");
  61. static const struct x86_cpu_id vmx_cpu_id[] = {
  62. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  63. {}
  64. };
  65. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  66. static bool __read_mostly enable_vpid = 1;
  67. module_param_named(vpid, enable_vpid, bool, 0444);
  68. static bool __read_mostly enable_vnmi = 1;
  69. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  70. static bool __read_mostly flexpriority_enabled = 1;
  71. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  72. static bool __read_mostly enable_ept = 1;
  73. module_param_named(ept, enable_ept, bool, S_IRUGO);
  74. static bool __read_mostly enable_unrestricted_guest = 1;
  75. module_param_named(unrestricted_guest,
  76. enable_unrestricted_guest, bool, S_IRUGO);
  77. static bool __read_mostly enable_ept_ad_bits = 1;
  78. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  79. static bool __read_mostly emulate_invalid_guest_state = true;
  80. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  81. static bool __read_mostly fasteoi = 1;
  82. module_param(fasteoi, bool, S_IRUGO);
  83. static bool __read_mostly enable_apicv = 1;
  84. module_param(enable_apicv, bool, S_IRUGO);
  85. static bool __read_mostly enable_shadow_vmcs = 1;
  86. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  87. /*
  88. * If nested=1, nested virtualization is supported, i.e., guests may use
  89. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  90. * use VMX instructions.
  91. */
  92. static bool __read_mostly nested = 0;
  93. module_param(nested, bool, S_IRUGO);
  94. static u64 __read_mostly host_xss;
  95. static bool __read_mostly enable_pml = 1;
  96. module_param_named(pml, enable_pml, bool, S_IRUGO);
  97. #define MSR_TYPE_R 1
  98. #define MSR_TYPE_W 2
  99. #define MSR_TYPE_RW 3
  100. #define MSR_BITMAP_MODE_X2APIC 1
  101. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  102. #define MSR_BITMAP_MODE_LM 4
  103. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  104. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  105. static int __read_mostly cpu_preemption_timer_multi;
  106. static bool __read_mostly enable_preemption_timer = 1;
  107. #ifdef CONFIG_X86_64
  108. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  109. #endif
  110. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  111. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
  112. #define KVM_VM_CR0_ALWAYS_ON \
  113. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
  114. X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
  115. #define KVM_CR4_GUEST_OWNED_BITS \
  116. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  117. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  118. #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
  119. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  120. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  121. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  122. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  123. /*
  124. * Hyper-V requires all of these, so mark them as supported even though
  125. * they are just treated the same as all-context.
  126. */
  127. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  128. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  129. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  130. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  131. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  132. /*
  133. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  134. * ple_gap: upper bound on the amount of time between two successive
  135. * executions of PAUSE in a loop. Also indicate if ple enabled.
  136. * According to test, this time is usually smaller than 128 cycles.
  137. * ple_window: upper bound on the amount of time a guest is allowed to execute
  138. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  139. * less than 2^12 cycles
  140. * Time is measured based on a counter that runs at the same rate as the TSC,
  141. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  142. */
  143. static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
  144. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  145. module_param(ple_window, uint, 0444);
  146. /* Default doubles per-vcpu window every exit. */
  147. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  148. module_param(ple_window_grow, uint, 0444);
  149. /* Default resets per-vcpu window every exit to ple_window. */
  150. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  151. module_param(ple_window_shrink, uint, 0444);
  152. /* Default is to compute the maximum so we can never overflow. */
  153. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  154. module_param(ple_window_max, uint, 0444);
  155. extern const ulong vmx_return;
  156. struct kvm_vmx {
  157. struct kvm kvm;
  158. unsigned int tss_addr;
  159. bool ept_identity_pagetable_done;
  160. gpa_t ept_identity_map_addr;
  161. };
  162. #define NR_AUTOLOAD_MSRS 8
  163. struct vmcs {
  164. u32 revision_id;
  165. u32 abort;
  166. char data[0];
  167. };
  168. /*
  169. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  170. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  171. * loaded on this CPU (so we can clear them if the CPU goes down).
  172. */
  173. struct loaded_vmcs {
  174. struct vmcs *vmcs;
  175. struct vmcs *shadow_vmcs;
  176. int cpu;
  177. bool launched;
  178. bool nmi_known_unmasked;
  179. unsigned long vmcs_host_cr3; /* May not match real cr3 */
  180. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  181. /* Support for vnmi-less CPUs */
  182. int soft_vnmi_blocked;
  183. ktime_t entry_time;
  184. s64 vnmi_blocked_time;
  185. unsigned long *msr_bitmap;
  186. struct list_head loaded_vmcss_on_cpu_link;
  187. };
  188. struct shared_msr_entry {
  189. unsigned index;
  190. u64 data;
  191. u64 mask;
  192. };
  193. /*
  194. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  195. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  196. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  197. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  198. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  199. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  200. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  201. * underlying hardware which will be used to run L2.
  202. * This structure is packed to ensure that its layout is identical across
  203. * machines (necessary for live migration).
  204. *
  205. * IMPORTANT: Changing the layout of existing fields in this structure
  206. * will break save/restore compatibility with older kvm releases. When
  207. * adding new fields, either use space in the reserved padding* arrays
  208. * or add the new fields to the end of the structure.
  209. */
  210. typedef u64 natural_width;
  211. struct __packed vmcs12 {
  212. /* According to the Intel spec, a VMCS region must start with the
  213. * following two fields. Then follow implementation-specific data.
  214. */
  215. u32 revision_id;
  216. u32 abort;
  217. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  218. u32 padding[7]; /* room for future expansion */
  219. u64 io_bitmap_a;
  220. u64 io_bitmap_b;
  221. u64 msr_bitmap;
  222. u64 vm_exit_msr_store_addr;
  223. u64 vm_exit_msr_load_addr;
  224. u64 vm_entry_msr_load_addr;
  225. u64 tsc_offset;
  226. u64 virtual_apic_page_addr;
  227. u64 apic_access_addr;
  228. u64 posted_intr_desc_addr;
  229. u64 ept_pointer;
  230. u64 eoi_exit_bitmap0;
  231. u64 eoi_exit_bitmap1;
  232. u64 eoi_exit_bitmap2;
  233. u64 eoi_exit_bitmap3;
  234. u64 xss_exit_bitmap;
  235. u64 guest_physical_address;
  236. u64 vmcs_link_pointer;
  237. u64 guest_ia32_debugctl;
  238. u64 guest_ia32_pat;
  239. u64 guest_ia32_efer;
  240. u64 guest_ia32_perf_global_ctrl;
  241. u64 guest_pdptr0;
  242. u64 guest_pdptr1;
  243. u64 guest_pdptr2;
  244. u64 guest_pdptr3;
  245. u64 guest_bndcfgs;
  246. u64 host_ia32_pat;
  247. u64 host_ia32_efer;
  248. u64 host_ia32_perf_global_ctrl;
  249. u64 vmread_bitmap;
  250. u64 vmwrite_bitmap;
  251. u64 vm_function_control;
  252. u64 eptp_list_address;
  253. u64 pml_address;
  254. u64 padding64[3]; /* room for future expansion */
  255. /*
  256. * To allow migration of L1 (complete with its L2 guests) between
  257. * machines of different natural widths (32 or 64 bit), we cannot have
  258. * unsigned long fields with no explict size. We use u64 (aliased
  259. * natural_width) instead. Luckily, x86 is little-endian.
  260. */
  261. natural_width cr0_guest_host_mask;
  262. natural_width cr4_guest_host_mask;
  263. natural_width cr0_read_shadow;
  264. natural_width cr4_read_shadow;
  265. natural_width cr3_target_value0;
  266. natural_width cr3_target_value1;
  267. natural_width cr3_target_value2;
  268. natural_width cr3_target_value3;
  269. natural_width exit_qualification;
  270. natural_width guest_linear_address;
  271. natural_width guest_cr0;
  272. natural_width guest_cr3;
  273. natural_width guest_cr4;
  274. natural_width guest_es_base;
  275. natural_width guest_cs_base;
  276. natural_width guest_ss_base;
  277. natural_width guest_ds_base;
  278. natural_width guest_fs_base;
  279. natural_width guest_gs_base;
  280. natural_width guest_ldtr_base;
  281. natural_width guest_tr_base;
  282. natural_width guest_gdtr_base;
  283. natural_width guest_idtr_base;
  284. natural_width guest_dr7;
  285. natural_width guest_rsp;
  286. natural_width guest_rip;
  287. natural_width guest_rflags;
  288. natural_width guest_pending_dbg_exceptions;
  289. natural_width guest_sysenter_esp;
  290. natural_width guest_sysenter_eip;
  291. natural_width host_cr0;
  292. natural_width host_cr3;
  293. natural_width host_cr4;
  294. natural_width host_fs_base;
  295. natural_width host_gs_base;
  296. natural_width host_tr_base;
  297. natural_width host_gdtr_base;
  298. natural_width host_idtr_base;
  299. natural_width host_ia32_sysenter_esp;
  300. natural_width host_ia32_sysenter_eip;
  301. natural_width host_rsp;
  302. natural_width host_rip;
  303. natural_width paddingl[8]; /* room for future expansion */
  304. u32 pin_based_vm_exec_control;
  305. u32 cpu_based_vm_exec_control;
  306. u32 exception_bitmap;
  307. u32 page_fault_error_code_mask;
  308. u32 page_fault_error_code_match;
  309. u32 cr3_target_count;
  310. u32 vm_exit_controls;
  311. u32 vm_exit_msr_store_count;
  312. u32 vm_exit_msr_load_count;
  313. u32 vm_entry_controls;
  314. u32 vm_entry_msr_load_count;
  315. u32 vm_entry_intr_info_field;
  316. u32 vm_entry_exception_error_code;
  317. u32 vm_entry_instruction_len;
  318. u32 tpr_threshold;
  319. u32 secondary_vm_exec_control;
  320. u32 vm_instruction_error;
  321. u32 vm_exit_reason;
  322. u32 vm_exit_intr_info;
  323. u32 vm_exit_intr_error_code;
  324. u32 idt_vectoring_info_field;
  325. u32 idt_vectoring_error_code;
  326. u32 vm_exit_instruction_len;
  327. u32 vmx_instruction_info;
  328. u32 guest_es_limit;
  329. u32 guest_cs_limit;
  330. u32 guest_ss_limit;
  331. u32 guest_ds_limit;
  332. u32 guest_fs_limit;
  333. u32 guest_gs_limit;
  334. u32 guest_ldtr_limit;
  335. u32 guest_tr_limit;
  336. u32 guest_gdtr_limit;
  337. u32 guest_idtr_limit;
  338. u32 guest_es_ar_bytes;
  339. u32 guest_cs_ar_bytes;
  340. u32 guest_ss_ar_bytes;
  341. u32 guest_ds_ar_bytes;
  342. u32 guest_fs_ar_bytes;
  343. u32 guest_gs_ar_bytes;
  344. u32 guest_ldtr_ar_bytes;
  345. u32 guest_tr_ar_bytes;
  346. u32 guest_interruptibility_info;
  347. u32 guest_activity_state;
  348. u32 guest_sysenter_cs;
  349. u32 host_ia32_sysenter_cs;
  350. u32 vmx_preemption_timer_value;
  351. u32 padding32[7]; /* room for future expansion */
  352. u16 virtual_processor_id;
  353. u16 posted_intr_nv;
  354. u16 guest_es_selector;
  355. u16 guest_cs_selector;
  356. u16 guest_ss_selector;
  357. u16 guest_ds_selector;
  358. u16 guest_fs_selector;
  359. u16 guest_gs_selector;
  360. u16 guest_ldtr_selector;
  361. u16 guest_tr_selector;
  362. u16 guest_intr_status;
  363. u16 host_es_selector;
  364. u16 host_cs_selector;
  365. u16 host_ss_selector;
  366. u16 host_ds_selector;
  367. u16 host_fs_selector;
  368. u16 host_gs_selector;
  369. u16 host_tr_selector;
  370. u16 guest_pml_index;
  371. };
  372. /*
  373. * For save/restore compatibility, the vmcs12 field offsets must not change.
  374. */
  375. #define CHECK_OFFSET(field, loc) \
  376. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  377. "Offset of " #field " in struct vmcs12 has changed.")
  378. static inline void vmx_check_vmcs12_offsets(void) {
  379. CHECK_OFFSET(revision_id, 0);
  380. CHECK_OFFSET(abort, 4);
  381. CHECK_OFFSET(launch_state, 8);
  382. CHECK_OFFSET(io_bitmap_a, 40);
  383. CHECK_OFFSET(io_bitmap_b, 48);
  384. CHECK_OFFSET(msr_bitmap, 56);
  385. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  386. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  387. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  388. CHECK_OFFSET(tsc_offset, 88);
  389. CHECK_OFFSET(virtual_apic_page_addr, 96);
  390. CHECK_OFFSET(apic_access_addr, 104);
  391. CHECK_OFFSET(posted_intr_desc_addr, 112);
  392. CHECK_OFFSET(ept_pointer, 120);
  393. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  394. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  395. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  396. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  397. CHECK_OFFSET(xss_exit_bitmap, 160);
  398. CHECK_OFFSET(guest_physical_address, 168);
  399. CHECK_OFFSET(vmcs_link_pointer, 176);
  400. CHECK_OFFSET(guest_ia32_debugctl, 184);
  401. CHECK_OFFSET(guest_ia32_pat, 192);
  402. CHECK_OFFSET(guest_ia32_efer, 200);
  403. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  404. CHECK_OFFSET(guest_pdptr0, 216);
  405. CHECK_OFFSET(guest_pdptr1, 224);
  406. CHECK_OFFSET(guest_pdptr2, 232);
  407. CHECK_OFFSET(guest_pdptr3, 240);
  408. CHECK_OFFSET(guest_bndcfgs, 248);
  409. CHECK_OFFSET(host_ia32_pat, 256);
  410. CHECK_OFFSET(host_ia32_efer, 264);
  411. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  412. CHECK_OFFSET(vmread_bitmap, 280);
  413. CHECK_OFFSET(vmwrite_bitmap, 288);
  414. CHECK_OFFSET(vm_function_control, 296);
  415. CHECK_OFFSET(eptp_list_address, 304);
  416. CHECK_OFFSET(pml_address, 312);
  417. CHECK_OFFSET(cr0_guest_host_mask, 344);
  418. CHECK_OFFSET(cr4_guest_host_mask, 352);
  419. CHECK_OFFSET(cr0_read_shadow, 360);
  420. CHECK_OFFSET(cr4_read_shadow, 368);
  421. CHECK_OFFSET(cr3_target_value0, 376);
  422. CHECK_OFFSET(cr3_target_value1, 384);
  423. CHECK_OFFSET(cr3_target_value2, 392);
  424. CHECK_OFFSET(cr3_target_value3, 400);
  425. CHECK_OFFSET(exit_qualification, 408);
  426. CHECK_OFFSET(guest_linear_address, 416);
  427. CHECK_OFFSET(guest_cr0, 424);
  428. CHECK_OFFSET(guest_cr3, 432);
  429. CHECK_OFFSET(guest_cr4, 440);
  430. CHECK_OFFSET(guest_es_base, 448);
  431. CHECK_OFFSET(guest_cs_base, 456);
  432. CHECK_OFFSET(guest_ss_base, 464);
  433. CHECK_OFFSET(guest_ds_base, 472);
  434. CHECK_OFFSET(guest_fs_base, 480);
  435. CHECK_OFFSET(guest_gs_base, 488);
  436. CHECK_OFFSET(guest_ldtr_base, 496);
  437. CHECK_OFFSET(guest_tr_base, 504);
  438. CHECK_OFFSET(guest_gdtr_base, 512);
  439. CHECK_OFFSET(guest_idtr_base, 520);
  440. CHECK_OFFSET(guest_dr7, 528);
  441. CHECK_OFFSET(guest_rsp, 536);
  442. CHECK_OFFSET(guest_rip, 544);
  443. CHECK_OFFSET(guest_rflags, 552);
  444. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  445. CHECK_OFFSET(guest_sysenter_esp, 568);
  446. CHECK_OFFSET(guest_sysenter_eip, 576);
  447. CHECK_OFFSET(host_cr0, 584);
  448. CHECK_OFFSET(host_cr3, 592);
  449. CHECK_OFFSET(host_cr4, 600);
  450. CHECK_OFFSET(host_fs_base, 608);
  451. CHECK_OFFSET(host_gs_base, 616);
  452. CHECK_OFFSET(host_tr_base, 624);
  453. CHECK_OFFSET(host_gdtr_base, 632);
  454. CHECK_OFFSET(host_idtr_base, 640);
  455. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  456. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  457. CHECK_OFFSET(host_rsp, 664);
  458. CHECK_OFFSET(host_rip, 672);
  459. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  460. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  461. CHECK_OFFSET(exception_bitmap, 752);
  462. CHECK_OFFSET(page_fault_error_code_mask, 756);
  463. CHECK_OFFSET(page_fault_error_code_match, 760);
  464. CHECK_OFFSET(cr3_target_count, 764);
  465. CHECK_OFFSET(vm_exit_controls, 768);
  466. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  467. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  468. CHECK_OFFSET(vm_entry_controls, 780);
  469. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  470. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  471. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  472. CHECK_OFFSET(vm_entry_instruction_len, 796);
  473. CHECK_OFFSET(tpr_threshold, 800);
  474. CHECK_OFFSET(secondary_vm_exec_control, 804);
  475. CHECK_OFFSET(vm_instruction_error, 808);
  476. CHECK_OFFSET(vm_exit_reason, 812);
  477. CHECK_OFFSET(vm_exit_intr_info, 816);
  478. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  479. CHECK_OFFSET(idt_vectoring_info_field, 824);
  480. CHECK_OFFSET(idt_vectoring_error_code, 828);
  481. CHECK_OFFSET(vm_exit_instruction_len, 832);
  482. CHECK_OFFSET(vmx_instruction_info, 836);
  483. CHECK_OFFSET(guest_es_limit, 840);
  484. CHECK_OFFSET(guest_cs_limit, 844);
  485. CHECK_OFFSET(guest_ss_limit, 848);
  486. CHECK_OFFSET(guest_ds_limit, 852);
  487. CHECK_OFFSET(guest_fs_limit, 856);
  488. CHECK_OFFSET(guest_gs_limit, 860);
  489. CHECK_OFFSET(guest_ldtr_limit, 864);
  490. CHECK_OFFSET(guest_tr_limit, 868);
  491. CHECK_OFFSET(guest_gdtr_limit, 872);
  492. CHECK_OFFSET(guest_idtr_limit, 876);
  493. CHECK_OFFSET(guest_es_ar_bytes, 880);
  494. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  495. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  496. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  497. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  498. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  499. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  500. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  501. CHECK_OFFSET(guest_interruptibility_info, 912);
  502. CHECK_OFFSET(guest_activity_state, 916);
  503. CHECK_OFFSET(guest_sysenter_cs, 920);
  504. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  505. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  506. CHECK_OFFSET(virtual_processor_id, 960);
  507. CHECK_OFFSET(posted_intr_nv, 962);
  508. CHECK_OFFSET(guest_es_selector, 964);
  509. CHECK_OFFSET(guest_cs_selector, 966);
  510. CHECK_OFFSET(guest_ss_selector, 968);
  511. CHECK_OFFSET(guest_ds_selector, 970);
  512. CHECK_OFFSET(guest_fs_selector, 972);
  513. CHECK_OFFSET(guest_gs_selector, 974);
  514. CHECK_OFFSET(guest_ldtr_selector, 976);
  515. CHECK_OFFSET(guest_tr_selector, 978);
  516. CHECK_OFFSET(guest_intr_status, 980);
  517. CHECK_OFFSET(host_es_selector, 982);
  518. CHECK_OFFSET(host_cs_selector, 984);
  519. CHECK_OFFSET(host_ss_selector, 986);
  520. CHECK_OFFSET(host_ds_selector, 988);
  521. CHECK_OFFSET(host_fs_selector, 990);
  522. CHECK_OFFSET(host_gs_selector, 992);
  523. CHECK_OFFSET(host_tr_selector, 994);
  524. CHECK_OFFSET(guest_pml_index, 996);
  525. }
  526. /*
  527. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  528. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  529. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  530. *
  531. * IMPORTANT: Changing this value will break save/restore compatibility with
  532. * older kvm releases.
  533. */
  534. #define VMCS12_REVISION 0x11e57ed0
  535. /*
  536. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  537. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  538. * current implementation, 4K are reserved to avoid future complications.
  539. */
  540. #define VMCS12_SIZE 0x1000
  541. /*
  542. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  543. * supported VMCS12 field encoding.
  544. */
  545. #define VMCS12_MAX_FIELD_INDEX 0x17
  546. struct nested_vmx_msrs {
  547. /*
  548. * We only store the "true" versions of the VMX capability MSRs. We
  549. * generate the "non-true" versions by setting the must-be-1 bits
  550. * according to the SDM.
  551. */
  552. u32 procbased_ctls_low;
  553. u32 procbased_ctls_high;
  554. u32 secondary_ctls_low;
  555. u32 secondary_ctls_high;
  556. u32 pinbased_ctls_low;
  557. u32 pinbased_ctls_high;
  558. u32 exit_ctls_low;
  559. u32 exit_ctls_high;
  560. u32 entry_ctls_low;
  561. u32 entry_ctls_high;
  562. u32 misc_low;
  563. u32 misc_high;
  564. u32 ept_caps;
  565. u32 vpid_caps;
  566. u64 basic;
  567. u64 cr0_fixed0;
  568. u64 cr0_fixed1;
  569. u64 cr4_fixed0;
  570. u64 cr4_fixed1;
  571. u64 vmcs_enum;
  572. u64 vmfunc_controls;
  573. };
  574. /*
  575. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  576. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  577. */
  578. struct nested_vmx {
  579. /* Has the level1 guest done vmxon? */
  580. bool vmxon;
  581. gpa_t vmxon_ptr;
  582. bool pml_full;
  583. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  584. gpa_t current_vmptr;
  585. /*
  586. * Cache of the guest's VMCS, existing outside of guest memory.
  587. * Loaded from guest memory during VMPTRLD. Flushed to guest
  588. * memory during VMCLEAR and VMPTRLD.
  589. */
  590. struct vmcs12 *cached_vmcs12;
  591. /*
  592. * Indicates if the shadow vmcs must be updated with the
  593. * data hold by vmcs12
  594. */
  595. bool sync_shadow_vmcs;
  596. bool dirty_vmcs12;
  597. bool change_vmcs01_virtual_apic_mode;
  598. /* L2 must run next, and mustn't decide to exit to L1. */
  599. bool nested_run_pending;
  600. struct loaded_vmcs vmcs02;
  601. /*
  602. * Guest pages referred to in the vmcs02 with host-physical
  603. * pointers, so we must keep them pinned while L2 runs.
  604. */
  605. struct page *apic_access_page;
  606. struct page *virtual_apic_page;
  607. struct page *pi_desc_page;
  608. struct pi_desc *pi_desc;
  609. bool pi_pending;
  610. u16 posted_intr_nv;
  611. struct hrtimer preemption_timer;
  612. bool preemption_timer_expired;
  613. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  614. u64 vmcs01_debugctl;
  615. u16 vpid02;
  616. u16 last_vpid;
  617. struct nested_vmx_msrs msrs;
  618. /* SMM related state */
  619. struct {
  620. /* in VMX operation on SMM entry? */
  621. bool vmxon;
  622. /* in guest mode on SMM entry? */
  623. bool guest_mode;
  624. } smm;
  625. };
  626. #define POSTED_INTR_ON 0
  627. #define POSTED_INTR_SN 1
  628. /* Posted-Interrupt Descriptor */
  629. struct pi_desc {
  630. u32 pir[8]; /* Posted interrupt requested */
  631. union {
  632. struct {
  633. /* bit 256 - Outstanding Notification */
  634. u16 on : 1,
  635. /* bit 257 - Suppress Notification */
  636. sn : 1,
  637. /* bit 271:258 - Reserved */
  638. rsvd_1 : 14;
  639. /* bit 279:272 - Notification Vector */
  640. u8 nv;
  641. /* bit 287:280 - Reserved */
  642. u8 rsvd_2;
  643. /* bit 319:288 - Notification Destination */
  644. u32 ndst;
  645. };
  646. u64 control;
  647. };
  648. u32 rsvd[6];
  649. } __aligned(64);
  650. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  651. {
  652. return test_and_set_bit(POSTED_INTR_ON,
  653. (unsigned long *)&pi_desc->control);
  654. }
  655. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  656. {
  657. return test_and_clear_bit(POSTED_INTR_ON,
  658. (unsigned long *)&pi_desc->control);
  659. }
  660. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  661. {
  662. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  663. }
  664. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  665. {
  666. return clear_bit(POSTED_INTR_SN,
  667. (unsigned long *)&pi_desc->control);
  668. }
  669. static inline void pi_set_sn(struct pi_desc *pi_desc)
  670. {
  671. return set_bit(POSTED_INTR_SN,
  672. (unsigned long *)&pi_desc->control);
  673. }
  674. static inline void pi_clear_on(struct pi_desc *pi_desc)
  675. {
  676. clear_bit(POSTED_INTR_ON,
  677. (unsigned long *)&pi_desc->control);
  678. }
  679. static inline int pi_test_on(struct pi_desc *pi_desc)
  680. {
  681. return test_bit(POSTED_INTR_ON,
  682. (unsigned long *)&pi_desc->control);
  683. }
  684. static inline int pi_test_sn(struct pi_desc *pi_desc)
  685. {
  686. return test_bit(POSTED_INTR_SN,
  687. (unsigned long *)&pi_desc->control);
  688. }
  689. struct vcpu_vmx {
  690. struct kvm_vcpu vcpu;
  691. unsigned long host_rsp;
  692. u8 fail;
  693. u8 msr_bitmap_mode;
  694. u32 exit_intr_info;
  695. u32 idt_vectoring_info;
  696. ulong rflags;
  697. struct shared_msr_entry *guest_msrs;
  698. int nmsrs;
  699. int save_nmsrs;
  700. unsigned long host_idt_base;
  701. #ifdef CONFIG_X86_64
  702. u64 msr_host_kernel_gs_base;
  703. u64 msr_guest_kernel_gs_base;
  704. #endif
  705. u64 arch_capabilities;
  706. u64 spec_ctrl;
  707. u32 vm_entry_controls_shadow;
  708. u32 vm_exit_controls_shadow;
  709. u32 secondary_exec_control;
  710. /*
  711. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  712. * non-nested (L1) guest, it always points to vmcs01. For a nested
  713. * guest (L2), it points to a different VMCS.
  714. */
  715. struct loaded_vmcs vmcs01;
  716. struct loaded_vmcs *loaded_vmcs;
  717. bool __launched; /* temporary, used in vmx_vcpu_run */
  718. struct msr_autoload {
  719. unsigned nr;
  720. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  721. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  722. } msr_autoload;
  723. struct {
  724. int loaded;
  725. u16 fs_sel, gs_sel, ldt_sel;
  726. #ifdef CONFIG_X86_64
  727. u16 ds_sel, es_sel;
  728. #endif
  729. int gs_ldt_reload_needed;
  730. int fs_reload_needed;
  731. u64 msr_host_bndcfgs;
  732. } host_state;
  733. struct {
  734. int vm86_active;
  735. ulong save_rflags;
  736. struct kvm_segment segs[8];
  737. } rmode;
  738. struct {
  739. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  740. struct kvm_save_segment {
  741. u16 selector;
  742. unsigned long base;
  743. u32 limit;
  744. u32 ar;
  745. } seg[8];
  746. } segment_cache;
  747. int vpid;
  748. bool emulation_required;
  749. u32 exit_reason;
  750. /* Posted interrupt descriptor */
  751. struct pi_desc pi_desc;
  752. /* Support for a guest hypervisor (nested VMX) */
  753. struct nested_vmx nested;
  754. /* Dynamic PLE window. */
  755. int ple_window;
  756. bool ple_window_dirty;
  757. /* Support for PML */
  758. #define PML_ENTITY_NUM 512
  759. struct page *pml_pg;
  760. /* apic deadline value in host tsc */
  761. u64 hv_deadline_tsc;
  762. u64 current_tsc_ratio;
  763. u32 host_pkru;
  764. unsigned long host_debugctlmsr;
  765. /*
  766. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  767. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  768. * in msr_ia32_feature_control_valid_bits.
  769. */
  770. u64 msr_ia32_feature_control;
  771. u64 msr_ia32_feature_control_valid_bits;
  772. };
  773. enum segment_cache_field {
  774. SEG_FIELD_SEL = 0,
  775. SEG_FIELD_BASE = 1,
  776. SEG_FIELD_LIMIT = 2,
  777. SEG_FIELD_AR = 3,
  778. SEG_FIELD_NR = 4
  779. };
  780. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  781. {
  782. return container_of(kvm, struct kvm_vmx, kvm);
  783. }
  784. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  785. {
  786. return container_of(vcpu, struct vcpu_vmx, vcpu);
  787. }
  788. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  789. {
  790. return &(to_vmx(vcpu)->pi_desc);
  791. }
  792. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  793. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  794. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  795. #define FIELD64(number, name) \
  796. FIELD(number, name), \
  797. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  798. static u16 shadow_read_only_fields[] = {
  799. #define SHADOW_FIELD_RO(x) x,
  800. #include "vmx_shadow_fields.h"
  801. };
  802. static int max_shadow_read_only_fields =
  803. ARRAY_SIZE(shadow_read_only_fields);
  804. static u16 shadow_read_write_fields[] = {
  805. #define SHADOW_FIELD_RW(x) x,
  806. #include "vmx_shadow_fields.h"
  807. };
  808. static int max_shadow_read_write_fields =
  809. ARRAY_SIZE(shadow_read_write_fields);
  810. static const unsigned short vmcs_field_to_offset_table[] = {
  811. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  812. FIELD(POSTED_INTR_NV, posted_intr_nv),
  813. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  814. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  815. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  816. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  817. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  818. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  819. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  820. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  821. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  822. FIELD(GUEST_PML_INDEX, guest_pml_index),
  823. FIELD(HOST_ES_SELECTOR, host_es_selector),
  824. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  825. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  826. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  827. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  828. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  829. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  830. FIELD64(IO_BITMAP_A, io_bitmap_a),
  831. FIELD64(IO_BITMAP_B, io_bitmap_b),
  832. FIELD64(MSR_BITMAP, msr_bitmap),
  833. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  834. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  835. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  836. FIELD64(PML_ADDRESS, pml_address),
  837. FIELD64(TSC_OFFSET, tsc_offset),
  838. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  839. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  840. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  841. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  842. FIELD64(EPT_POINTER, ept_pointer),
  843. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  844. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  845. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  846. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  847. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  848. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  849. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  850. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  851. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  852. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  853. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  854. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  855. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  856. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  857. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  858. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  859. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  860. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  861. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  862. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  863. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  864. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  865. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  866. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  867. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  868. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  869. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  870. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  871. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  872. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  873. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  874. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  875. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  876. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  877. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  878. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  879. FIELD(TPR_THRESHOLD, tpr_threshold),
  880. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  881. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  882. FIELD(VM_EXIT_REASON, vm_exit_reason),
  883. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  884. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  885. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  886. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  887. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  888. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  889. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  890. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  891. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  892. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  893. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  894. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  895. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  896. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  897. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  898. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  899. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  900. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  901. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  902. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  903. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  904. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  905. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  906. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  907. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  908. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  909. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  910. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  911. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  912. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  913. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  914. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  915. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  916. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  917. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  918. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  919. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  920. FIELD(EXIT_QUALIFICATION, exit_qualification),
  921. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  922. FIELD(GUEST_CR0, guest_cr0),
  923. FIELD(GUEST_CR3, guest_cr3),
  924. FIELD(GUEST_CR4, guest_cr4),
  925. FIELD(GUEST_ES_BASE, guest_es_base),
  926. FIELD(GUEST_CS_BASE, guest_cs_base),
  927. FIELD(GUEST_SS_BASE, guest_ss_base),
  928. FIELD(GUEST_DS_BASE, guest_ds_base),
  929. FIELD(GUEST_FS_BASE, guest_fs_base),
  930. FIELD(GUEST_GS_BASE, guest_gs_base),
  931. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  932. FIELD(GUEST_TR_BASE, guest_tr_base),
  933. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  934. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  935. FIELD(GUEST_DR7, guest_dr7),
  936. FIELD(GUEST_RSP, guest_rsp),
  937. FIELD(GUEST_RIP, guest_rip),
  938. FIELD(GUEST_RFLAGS, guest_rflags),
  939. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  940. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  941. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  942. FIELD(HOST_CR0, host_cr0),
  943. FIELD(HOST_CR3, host_cr3),
  944. FIELD(HOST_CR4, host_cr4),
  945. FIELD(HOST_FS_BASE, host_fs_base),
  946. FIELD(HOST_GS_BASE, host_gs_base),
  947. FIELD(HOST_TR_BASE, host_tr_base),
  948. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  949. FIELD(HOST_IDTR_BASE, host_idtr_base),
  950. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  951. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  952. FIELD(HOST_RSP, host_rsp),
  953. FIELD(HOST_RIP, host_rip),
  954. };
  955. static inline short vmcs_field_to_offset(unsigned long field)
  956. {
  957. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  958. unsigned short offset;
  959. unsigned index;
  960. if (field >> 15)
  961. return -ENOENT;
  962. index = ROL16(field, 6);
  963. if (index >= size)
  964. return -ENOENT;
  965. index = array_index_nospec(index, size);
  966. offset = vmcs_field_to_offset_table[index];
  967. if (offset == 0)
  968. return -ENOENT;
  969. return offset;
  970. }
  971. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  972. {
  973. return to_vmx(vcpu)->nested.cached_vmcs12;
  974. }
  975. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  976. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  977. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  978. static bool vmx_xsaves_supported(void);
  979. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  980. struct kvm_segment *var, int seg);
  981. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  982. struct kvm_segment *var, int seg);
  983. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  984. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  985. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  986. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  987. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  988. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  989. u16 error_code);
  990. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  991. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  992. u32 msr, int type);
  993. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  994. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  995. /*
  996. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  997. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  998. */
  999. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1000. /*
  1001. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1002. * can find which vCPU should be waken up.
  1003. */
  1004. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1005. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1006. enum {
  1007. VMX_VMREAD_BITMAP,
  1008. VMX_VMWRITE_BITMAP,
  1009. VMX_BITMAP_NR
  1010. };
  1011. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1012. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1013. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1014. static bool cpu_has_load_ia32_efer;
  1015. static bool cpu_has_load_perf_global_ctrl;
  1016. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1017. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1018. static struct vmcs_config {
  1019. int size;
  1020. int order;
  1021. u32 basic_cap;
  1022. u32 revision_id;
  1023. u32 pin_based_exec_ctrl;
  1024. u32 cpu_based_exec_ctrl;
  1025. u32 cpu_based_2nd_exec_ctrl;
  1026. u32 vmexit_ctrl;
  1027. u32 vmentry_ctrl;
  1028. struct nested_vmx_msrs nested;
  1029. } vmcs_config;
  1030. static struct vmx_capability {
  1031. u32 ept;
  1032. u32 vpid;
  1033. } vmx_capability;
  1034. #define VMX_SEGMENT_FIELD(seg) \
  1035. [VCPU_SREG_##seg] = { \
  1036. .selector = GUEST_##seg##_SELECTOR, \
  1037. .base = GUEST_##seg##_BASE, \
  1038. .limit = GUEST_##seg##_LIMIT, \
  1039. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1040. }
  1041. static const struct kvm_vmx_segment_field {
  1042. unsigned selector;
  1043. unsigned base;
  1044. unsigned limit;
  1045. unsigned ar_bytes;
  1046. } kvm_vmx_segment_fields[] = {
  1047. VMX_SEGMENT_FIELD(CS),
  1048. VMX_SEGMENT_FIELD(DS),
  1049. VMX_SEGMENT_FIELD(ES),
  1050. VMX_SEGMENT_FIELD(FS),
  1051. VMX_SEGMENT_FIELD(GS),
  1052. VMX_SEGMENT_FIELD(SS),
  1053. VMX_SEGMENT_FIELD(TR),
  1054. VMX_SEGMENT_FIELD(LDTR),
  1055. };
  1056. static u64 host_efer;
  1057. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1058. /*
  1059. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1060. * away by decrementing the array size.
  1061. */
  1062. static const u32 vmx_msr_index[] = {
  1063. #ifdef CONFIG_X86_64
  1064. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1065. #endif
  1066. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1067. };
  1068. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1069. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1070. #define KVM_EVMCS_VERSION 1
  1071. #if IS_ENABLED(CONFIG_HYPERV)
  1072. static bool __read_mostly enlightened_vmcs = true;
  1073. module_param(enlightened_vmcs, bool, 0444);
  1074. static inline void evmcs_write64(unsigned long field, u64 value)
  1075. {
  1076. u16 clean_field;
  1077. int offset = get_evmcs_offset(field, &clean_field);
  1078. if (offset < 0)
  1079. return;
  1080. *(u64 *)((char *)current_evmcs + offset) = value;
  1081. current_evmcs->hv_clean_fields &= ~clean_field;
  1082. }
  1083. static inline void evmcs_write32(unsigned long field, u32 value)
  1084. {
  1085. u16 clean_field;
  1086. int offset = get_evmcs_offset(field, &clean_field);
  1087. if (offset < 0)
  1088. return;
  1089. *(u32 *)((char *)current_evmcs + offset) = value;
  1090. current_evmcs->hv_clean_fields &= ~clean_field;
  1091. }
  1092. static inline void evmcs_write16(unsigned long field, u16 value)
  1093. {
  1094. u16 clean_field;
  1095. int offset = get_evmcs_offset(field, &clean_field);
  1096. if (offset < 0)
  1097. return;
  1098. *(u16 *)((char *)current_evmcs + offset) = value;
  1099. current_evmcs->hv_clean_fields &= ~clean_field;
  1100. }
  1101. static inline u64 evmcs_read64(unsigned long field)
  1102. {
  1103. int offset = get_evmcs_offset(field, NULL);
  1104. if (offset < 0)
  1105. return 0;
  1106. return *(u64 *)((char *)current_evmcs + offset);
  1107. }
  1108. static inline u32 evmcs_read32(unsigned long field)
  1109. {
  1110. int offset = get_evmcs_offset(field, NULL);
  1111. if (offset < 0)
  1112. return 0;
  1113. return *(u32 *)((char *)current_evmcs + offset);
  1114. }
  1115. static inline u16 evmcs_read16(unsigned long field)
  1116. {
  1117. int offset = get_evmcs_offset(field, NULL);
  1118. if (offset < 0)
  1119. return 0;
  1120. return *(u16 *)((char *)current_evmcs + offset);
  1121. }
  1122. static inline void evmcs_touch_msr_bitmap(void)
  1123. {
  1124. if (unlikely(!current_evmcs))
  1125. return;
  1126. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1127. current_evmcs->hv_clean_fields &=
  1128. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1129. }
  1130. static void evmcs_load(u64 phys_addr)
  1131. {
  1132. struct hv_vp_assist_page *vp_ap =
  1133. hv_get_vp_assist_page(smp_processor_id());
  1134. vp_ap->current_nested_vmcs = phys_addr;
  1135. vp_ap->enlighten_vmentry = 1;
  1136. }
  1137. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1138. {
  1139. /*
  1140. * Enlightened VMCSv1 doesn't support these:
  1141. *
  1142. * POSTED_INTR_NV = 0x00000002,
  1143. * GUEST_INTR_STATUS = 0x00000810,
  1144. * APIC_ACCESS_ADDR = 0x00002014,
  1145. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1146. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1147. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1148. * EOI_EXIT_BITMAP2 = 0x00002020,
  1149. * EOI_EXIT_BITMAP3 = 0x00002022,
  1150. */
  1151. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1152. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1153. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1154. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1155. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1156. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1157. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1158. /*
  1159. * GUEST_PML_INDEX = 0x00000812,
  1160. * PML_ADDRESS = 0x0000200e,
  1161. */
  1162. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1163. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1164. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1165. /*
  1166. * EPTP_LIST_ADDRESS = 0x00002024,
  1167. * VMREAD_BITMAP = 0x00002026,
  1168. * VMWRITE_BITMAP = 0x00002028,
  1169. */
  1170. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1171. /*
  1172. * TSC_MULTIPLIER = 0x00002032,
  1173. */
  1174. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1175. /*
  1176. * PLE_GAP = 0x00004020,
  1177. * PLE_WINDOW = 0x00004022,
  1178. */
  1179. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1180. /*
  1181. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1182. */
  1183. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1184. /*
  1185. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1186. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1187. */
  1188. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1189. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1190. /*
  1191. * Currently unsupported in KVM:
  1192. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1193. */
  1194. }
  1195. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1196. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1197. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1198. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1199. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1200. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1201. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1202. static inline void evmcs_load(u64 phys_addr) {}
  1203. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1204. static inline void evmcs_touch_msr_bitmap(void) {}
  1205. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1206. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1207. {
  1208. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1209. INTR_INFO_VALID_MASK)) ==
  1210. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1211. }
  1212. static inline bool is_debug(u32 intr_info)
  1213. {
  1214. return is_exception_n(intr_info, DB_VECTOR);
  1215. }
  1216. static inline bool is_breakpoint(u32 intr_info)
  1217. {
  1218. return is_exception_n(intr_info, BP_VECTOR);
  1219. }
  1220. static inline bool is_page_fault(u32 intr_info)
  1221. {
  1222. return is_exception_n(intr_info, PF_VECTOR);
  1223. }
  1224. static inline bool is_no_device(u32 intr_info)
  1225. {
  1226. return is_exception_n(intr_info, NM_VECTOR);
  1227. }
  1228. static inline bool is_invalid_opcode(u32 intr_info)
  1229. {
  1230. return is_exception_n(intr_info, UD_VECTOR);
  1231. }
  1232. static inline bool is_gp_fault(u32 intr_info)
  1233. {
  1234. return is_exception_n(intr_info, GP_VECTOR);
  1235. }
  1236. static inline bool is_external_interrupt(u32 intr_info)
  1237. {
  1238. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1239. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1240. }
  1241. static inline bool is_machine_check(u32 intr_info)
  1242. {
  1243. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1244. INTR_INFO_VALID_MASK)) ==
  1245. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1246. }
  1247. /* Undocumented: icebp/int1 */
  1248. static inline bool is_icebp(u32 intr_info)
  1249. {
  1250. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1251. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1252. }
  1253. static inline bool cpu_has_vmx_msr_bitmap(void)
  1254. {
  1255. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1256. }
  1257. static inline bool cpu_has_vmx_tpr_shadow(void)
  1258. {
  1259. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1260. }
  1261. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1262. {
  1263. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1264. }
  1265. static inline bool cpu_has_secondary_exec_ctrls(void)
  1266. {
  1267. return vmcs_config.cpu_based_exec_ctrl &
  1268. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1269. }
  1270. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1271. {
  1272. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1273. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1274. }
  1275. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1276. {
  1277. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1278. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1279. }
  1280. static inline bool cpu_has_vmx_apic_register_virt(void)
  1281. {
  1282. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1283. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1284. }
  1285. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1286. {
  1287. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1288. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1289. }
  1290. /*
  1291. * Comment's format: document - errata name - stepping - processor name.
  1292. * Refer from
  1293. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1294. */
  1295. static u32 vmx_preemption_cpu_tfms[] = {
  1296. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1297. 0x000206E6,
  1298. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1299. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1300. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1301. 0x00020652,
  1302. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1303. 0x00020655,
  1304. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1305. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1306. /*
  1307. * 320767.pdf - AAP86 - B1 -
  1308. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1309. */
  1310. 0x000106E5,
  1311. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1312. 0x000106A0,
  1313. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1314. 0x000106A1,
  1315. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1316. 0x000106A4,
  1317. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1318. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1319. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1320. 0x000106A5,
  1321. };
  1322. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1323. {
  1324. u32 eax = cpuid_eax(0x00000001), i;
  1325. /* Clear the reserved bits */
  1326. eax &= ~(0x3U << 14 | 0xfU << 28);
  1327. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1328. if (eax == vmx_preemption_cpu_tfms[i])
  1329. return true;
  1330. return false;
  1331. }
  1332. static inline bool cpu_has_vmx_preemption_timer(void)
  1333. {
  1334. return vmcs_config.pin_based_exec_ctrl &
  1335. PIN_BASED_VMX_PREEMPTION_TIMER;
  1336. }
  1337. static inline bool cpu_has_vmx_posted_intr(void)
  1338. {
  1339. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1340. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1341. }
  1342. static inline bool cpu_has_vmx_apicv(void)
  1343. {
  1344. return cpu_has_vmx_apic_register_virt() &&
  1345. cpu_has_vmx_virtual_intr_delivery() &&
  1346. cpu_has_vmx_posted_intr();
  1347. }
  1348. static inline bool cpu_has_vmx_flexpriority(void)
  1349. {
  1350. return cpu_has_vmx_tpr_shadow() &&
  1351. cpu_has_vmx_virtualize_apic_accesses();
  1352. }
  1353. static inline bool cpu_has_vmx_ept_execute_only(void)
  1354. {
  1355. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1356. }
  1357. static inline bool cpu_has_vmx_ept_2m_page(void)
  1358. {
  1359. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1360. }
  1361. static inline bool cpu_has_vmx_ept_1g_page(void)
  1362. {
  1363. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1364. }
  1365. static inline bool cpu_has_vmx_ept_4levels(void)
  1366. {
  1367. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1368. }
  1369. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1370. {
  1371. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1372. }
  1373. static inline bool cpu_has_vmx_ept_5levels(void)
  1374. {
  1375. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1376. }
  1377. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1378. {
  1379. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1380. }
  1381. static inline bool cpu_has_vmx_invept_context(void)
  1382. {
  1383. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1384. }
  1385. static inline bool cpu_has_vmx_invept_global(void)
  1386. {
  1387. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1388. }
  1389. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1390. {
  1391. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1392. }
  1393. static inline bool cpu_has_vmx_invvpid_single(void)
  1394. {
  1395. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1396. }
  1397. static inline bool cpu_has_vmx_invvpid_global(void)
  1398. {
  1399. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1400. }
  1401. static inline bool cpu_has_vmx_invvpid(void)
  1402. {
  1403. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1404. }
  1405. static inline bool cpu_has_vmx_ept(void)
  1406. {
  1407. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1408. SECONDARY_EXEC_ENABLE_EPT;
  1409. }
  1410. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1411. {
  1412. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1413. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1414. }
  1415. static inline bool cpu_has_vmx_ple(void)
  1416. {
  1417. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1418. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1419. }
  1420. static inline bool cpu_has_vmx_basic_inout(void)
  1421. {
  1422. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1423. }
  1424. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1425. {
  1426. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1427. }
  1428. static inline bool cpu_has_vmx_vpid(void)
  1429. {
  1430. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1431. SECONDARY_EXEC_ENABLE_VPID;
  1432. }
  1433. static inline bool cpu_has_vmx_rdtscp(void)
  1434. {
  1435. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1436. SECONDARY_EXEC_RDTSCP;
  1437. }
  1438. static inline bool cpu_has_vmx_invpcid(void)
  1439. {
  1440. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1441. SECONDARY_EXEC_ENABLE_INVPCID;
  1442. }
  1443. static inline bool cpu_has_virtual_nmis(void)
  1444. {
  1445. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1446. }
  1447. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1448. {
  1449. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1450. SECONDARY_EXEC_WBINVD_EXITING;
  1451. }
  1452. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1453. {
  1454. u64 vmx_msr;
  1455. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1456. /* check if the cpu supports writing r/o exit information fields */
  1457. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1458. return false;
  1459. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1460. SECONDARY_EXEC_SHADOW_VMCS;
  1461. }
  1462. static inline bool cpu_has_vmx_pml(void)
  1463. {
  1464. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1465. }
  1466. static inline bool cpu_has_vmx_tsc_scaling(void)
  1467. {
  1468. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1469. SECONDARY_EXEC_TSC_SCALING;
  1470. }
  1471. static inline bool cpu_has_vmx_vmfunc(void)
  1472. {
  1473. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1474. SECONDARY_EXEC_ENABLE_VMFUNC;
  1475. }
  1476. static bool vmx_umip_emulated(void)
  1477. {
  1478. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1479. SECONDARY_EXEC_DESC;
  1480. }
  1481. static inline bool report_flexpriority(void)
  1482. {
  1483. return flexpriority_enabled;
  1484. }
  1485. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1486. {
  1487. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1488. }
  1489. /*
  1490. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1491. * to modify any valid field of the VMCS, or are the VM-exit
  1492. * information fields read-only?
  1493. */
  1494. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1495. {
  1496. return to_vmx(vcpu)->nested.msrs.misc_low &
  1497. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1498. }
  1499. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1500. {
  1501. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1502. }
  1503. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1504. {
  1505. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1506. CPU_BASED_MONITOR_TRAP_FLAG;
  1507. }
  1508. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1509. {
  1510. return vmcs12->cpu_based_vm_exec_control & bit;
  1511. }
  1512. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1513. {
  1514. return (vmcs12->cpu_based_vm_exec_control &
  1515. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1516. (vmcs12->secondary_vm_exec_control & bit);
  1517. }
  1518. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1519. {
  1520. return vmcs12->pin_based_vm_exec_control &
  1521. PIN_BASED_VMX_PREEMPTION_TIMER;
  1522. }
  1523. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1524. {
  1525. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1526. }
  1527. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1528. {
  1529. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1530. }
  1531. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1532. {
  1533. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1534. }
  1535. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1536. {
  1537. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1538. }
  1539. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1540. {
  1541. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1542. }
  1543. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1544. {
  1545. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1546. }
  1547. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1548. {
  1549. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1550. }
  1551. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1552. {
  1553. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1554. }
  1555. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1556. {
  1557. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1558. }
  1559. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1560. {
  1561. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1562. }
  1563. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1564. {
  1565. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1566. }
  1567. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1568. {
  1569. return nested_cpu_has_vmfunc(vmcs12) &&
  1570. (vmcs12->vm_function_control &
  1571. VMX_VMFUNC_EPTP_SWITCHING);
  1572. }
  1573. static inline bool is_nmi(u32 intr_info)
  1574. {
  1575. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1576. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1577. }
  1578. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1579. u32 exit_intr_info,
  1580. unsigned long exit_qualification);
  1581. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1582. struct vmcs12 *vmcs12,
  1583. u32 reason, unsigned long qualification);
  1584. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1585. {
  1586. int i;
  1587. for (i = 0; i < vmx->nmsrs; ++i)
  1588. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1589. return i;
  1590. return -1;
  1591. }
  1592. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1593. {
  1594. struct {
  1595. u64 vpid : 16;
  1596. u64 rsvd : 48;
  1597. u64 gva;
  1598. } operand = { vpid, 0, gva };
  1599. asm volatile (__ex(ASM_VMX_INVVPID)
  1600. /* CF==1 or ZF==1 --> rc = -1 */
  1601. "; ja 1f ; ud2 ; 1:"
  1602. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1603. }
  1604. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1605. {
  1606. struct {
  1607. u64 eptp, gpa;
  1608. } operand = {eptp, gpa};
  1609. asm volatile (__ex(ASM_VMX_INVEPT)
  1610. /* CF==1 or ZF==1 --> rc = -1 */
  1611. "; ja 1f ; ud2 ; 1:\n"
  1612. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1613. }
  1614. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1615. {
  1616. int i;
  1617. i = __find_msr_index(vmx, msr);
  1618. if (i >= 0)
  1619. return &vmx->guest_msrs[i];
  1620. return NULL;
  1621. }
  1622. static void vmcs_clear(struct vmcs *vmcs)
  1623. {
  1624. u64 phys_addr = __pa(vmcs);
  1625. u8 error;
  1626. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1627. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1628. : "cc", "memory");
  1629. if (error)
  1630. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1631. vmcs, phys_addr);
  1632. }
  1633. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1634. {
  1635. vmcs_clear(loaded_vmcs->vmcs);
  1636. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1637. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1638. loaded_vmcs->cpu = -1;
  1639. loaded_vmcs->launched = 0;
  1640. }
  1641. static void vmcs_load(struct vmcs *vmcs)
  1642. {
  1643. u64 phys_addr = __pa(vmcs);
  1644. u8 error;
  1645. if (static_branch_unlikely(&enable_evmcs))
  1646. return evmcs_load(phys_addr);
  1647. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1648. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1649. : "cc", "memory");
  1650. if (error)
  1651. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1652. vmcs, phys_addr);
  1653. }
  1654. #ifdef CONFIG_KEXEC_CORE
  1655. /*
  1656. * This bitmap is used to indicate whether the vmclear
  1657. * operation is enabled on all cpus. All disabled by
  1658. * default.
  1659. */
  1660. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1661. static inline void crash_enable_local_vmclear(int cpu)
  1662. {
  1663. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1664. }
  1665. static inline void crash_disable_local_vmclear(int cpu)
  1666. {
  1667. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1668. }
  1669. static inline int crash_local_vmclear_enabled(int cpu)
  1670. {
  1671. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1672. }
  1673. static void crash_vmclear_local_loaded_vmcss(void)
  1674. {
  1675. int cpu = raw_smp_processor_id();
  1676. struct loaded_vmcs *v;
  1677. if (!crash_local_vmclear_enabled(cpu))
  1678. return;
  1679. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1680. loaded_vmcss_on_cpu_link)
  1681. vmcs_clear(v->vmcs);
  1682. }
  1683. #else
  1684. static inline void crash_enable_local_vmclear(int cpu) { }
  1685. static inline void crash_disable_local_vmclear(int cpu) { }
  1686. #endif /* CONFIG_KEXEC_CORE */
  1687. static void __loaded_vmcs_clear(void *arg)
  1688. {
  1689. struct loaded_vmcs *loaded_vmcs = arg;
  1690. int cpu = raw_smp_processor_id();
  1691. if (loaded_vmcs->cpu != cpu)
  1692. return; /* vcpu migration can race with cpu offline */
  1693. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1694. per_cpu(current_vmcs, cpu) = NULL;
  1695. crash_disable_local_vmclear(cpu);
  1696. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1697. /*
  1698. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1699. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1700. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1701. * then adds the vmcs into percpu list before it is deleted.
  1702. */
  1703. smp_wmb();
  1704. loaded_vmcs_init(loaded_vmcs);
  1705. crash_enable_local_vmclear(cpu);
  1706. }
  1707. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1708. {
  1709. int cpu = loaded_vmcs->cpu;
  1710. if (cpu != -1)
  1711. smp_call_function_single(cpu,
  1712. __loaded_vmcs_clear, loaded_vmcs, 1);
  1713. }
  1714. static inline void vpid_sync_vcpu_single(int vpid)
  1715. {
  1716. if (vpid == 0)
  1717. return;
  1718. if (cpu_has_vmx_invvpid_single())
  1719. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1720. }
  1721. static inline void vpid_sync_vcpu_global(void)
  1722. {
  1723. if (cpu_has_vmx_invvpid_global())
  1724. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1725. }
  1726. static inline void vpid_sync_context(int vpid)
  1727. {
  1728. if (cpu_has_vmx_invvpid_single())
  1729. vpid_sync_vcpu_single(vpid);
  1730. else
  1731. vpid_sync_vcpu_global();
  1732. }
  1733. static inline void ept_sync_global(void)
  1734. {
  1735. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1736. }
  1737. static inline void ept_sync_context(u64 eptp)
  1738. {
  1739. if (cpu_has_vmx_invept_context())
  1740. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1741. else
  1742. ept_sync_global();
  1743. }
  1744. static __always_inline void vmcs_check16(unsigned long field)
  1745. {
  1746. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1747. "16-bit accessor invalid for 64-bit field");
  1748. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1749. "16-bit accessor invalid for 64-bit high field");
  1750. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1751. "16-bit accessor invalid for 32-bit high field");
  1752. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1753. "16-bit accessor invalid for natural width field");
  1754. }
  1755. static __always_inline void vmcs_check32(unsigned long field)
  1756. {
  1757. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1758. "32-bit accessor invalid for 16-bit field");
  1759. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1760. "32-bit accessor invalid for natural width field");
  1761. }
  1762. static __always_inline void vmcs_check64(unsigned long field)
  1763. {
  1764. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1765. "64-bit accessor invalid for 16-bit field");
  1766. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1767. "64-bit accessor invalid for 64-bit high field");
  1768. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1769. "64-bit accessor invalid for 32-bit field");
  1770. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1771. "64-bit accessor invalid for natural width field");
  1772. }
  1773. static __always_inline void vmcs_checkl(unsigned long field)
  1774. {
  1775. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1776. "Natural width accessor invalid for 16-bit field");
  1777. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1778. "Natural width accessor invalid for 64-bit field");
  1779. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1780. "Natural width accessor invalid for 64-bit high field");
  1781. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1782. "Natural width accessor invalid for 32-bit field");
  1783. }
  1784. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1785. {
  1786. unsigned long value;
  1787. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1788. : "=a"(value) : "d"(field) : "cc");
  1789. return value;
  1790. }
  1791. static __always_inline u16 vmcs_read16(unsigned long field)
  1792. {
  1793. vmcs_check16(field);
  1794. if (static_branch_unlikely(&enable_evmcs))
  1795. return evmcs_read16(field);
  1796. return __vmcs_readl(field);
  1797. }
  1798. static __always_inline u32 vmcs_read32(unsigned long field)
  1799. {
  1800. vmcs_check32(field);
  1801. if (static_branch_unlikely(&enable_evmcs))
  1802. return evmcs_read32(field);
  1803. return __vmcs_readl(field);
  1804. }
  1805. static __always_inline u64 vmcs_read64(unsigned long field)
  1806. {
  1807. vmcs_check64(field);
  1808. if (static_branch_unlikely(&enable_evmcs))
  1809. return evmcs_read64(field);
  1810. #ifdef CONFIG_X86_64
  1811. return __vmcs_readl(field);
  1812. #else
  1813. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  1814. #endif
  1815. }
  1816. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1817. {
  1818. vmcs_checkl(field);
  1819. if (static_branch_unlikely(&enable_evmcs))
  1820. return evmcs_read64(field);
  1821. return __vmcs_readl(field);
  1822. }
  1823. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1824. {
  1825. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1826. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1827. dump_stack();
  1828. }
  1829. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  1830. {
  1831. u8 error;
  1832. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1833. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1834. if (unlikely(error))
  1835. vmwrite_error(field, value);
  1836. }
  1837. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  1838. {
  1839. vmcs_check16(field);
  1840. if (static_branch_unlikely(&enable_evmcs))
  1841. return evmcs_write16(field, value);
  1842. __vmcs_writel(field, value);
  1843. }
  1844. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  1845. {
  1846. vmcs_check32(field);
  1847. if (static_branch_unlikely(&enable_evmcs))
  1848. return evmcs_write32(field, value);
  1849. __vmcs_writel(field, value);
  1850. }
  1851. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  1852. {
  1853. vmcs_check64(field);
  1854. if (static_branch_unlikely(&enable_evmcs))
  1855. return evmcs_write64(field, value);
  1856. __vmcs_writel(field, value);
  1857. #ifndef CONFIG_X86_64
  1858. asm volatile ("");
  1859. __vmcs_writel(field+1, value >> 32);
  1860. #endif
  1861. }
  1862. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  1863. {
  1864. vmcs_checkl(field);
  1865. if (static_branch_unlikely(&enable_evmcs))
  1866. return evmcs_write64(field, value);
  1867. __vmcs_writel(field, value);
  1868. }
  1869. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  1870. {
  1871. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1872. "vmcs_clear_bits does not support 64-bit fields");
  1873. if (static_branch_unlikely(&enable_evmcs))
  1874. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  1875. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  1876. }
  1877. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  1878. {
  1879. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1880. "vmcs_set_bits does not support 64-bit fields");
  1881. if (static_branch_unlikely(&enable_evmcs))
  1882. return evmcs_write32(field, evmcs_read32(field) | mask);
  1883. __vmcs_writel(field, __vmcs_readl(field) | mask);
  1884. }
  1885. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  1886. {
  1887. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  1888. }
  1889. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1890. {
  1891. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1892. vmx->vm_entry_controls_shadow = val;
  1893. }
  1894. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1895. {
  1896. if (vmx->vm_entry_controls_shadow != val)
  1897. vm_entry_controls_init(vmx, val);
  1898. }
  1899. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1900. {
  1901. return vmx->vm_entry_controls_shadow;
  1902. }
  1903. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1904. {
  1905. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1906. }
  1907. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1908. {
  1909. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1910. }
  1911. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  1912. {
  1913. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  1914. }
  1915. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1916. {
  1917. vmcs_write32(VM_EXIT_CONTROLS, val);
  1918. vmx->vm_exit_controls_shadow = val;
  1919. }
  1920. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1921. {
  1922. if (vmx->vm_exit_controls_shadow != val)
  1923. vm_exit_controls_init(vmx, val);
  1924. }
  1925. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1926. {
  1927. return vmx->vm_exit_controls_shadow;
  1928. }
  1929. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1930. {
  1931. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1932. }
  1933. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1934. {
  1935. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1936. }
  1937. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1938. {
  1939. vmx->segment_cache.bitmask = 0;
  1940. }
  1941. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1942. unsigned field)
  1943. {
  1944. bool ret;
  1945. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1946. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1947. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1948. vmx->segment_cache.bitmask = 0;
  1949. }
  1950. ret = vmx->segment_cache.bitmask & mask;
  1951. vmx->segment_cache.bitmask |= mask;
  1952. return ret;
  1953. }
  1954. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1955. {
  1956. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1957. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1958. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1959. return *p;
  1960. }
  1961. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1962. {
  1963. ulong *p = &vmx->segment_cache.seg[seg].base;
  1964. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1965. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1966. return *p;
  1967. }
  1968. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1969. {
  1970. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1971. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1972. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1973. return *p;
  1974. }
  1975. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1976. {
  1977. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1978. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1979. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1980. return *p;
  1981. }
  1982. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1983. {
  1984. u32 eb;
  1985. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1986. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  1987. /*
  1988. * Guest access to VMware backdoor ports could legitimately
  1989. * trigger #GP because of TSS I/O permission bitmap.
  1990. * We intercept those #GP and allow access to them anyway
  1991. * as VMware does.
  1992. */
  1993. if (enable_vmware_backdoor)
  1994. eb |= (1u << GP_VECTOR);
  1995. if ((vcpu->guest_debug &
  1996. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1997. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1998. eb |= 1u << BP_VECTOR;
  1999. if (to_vmx(vcpu)->rmode.vm86_active)
  2000. eb = ~0;
  2001. if (enable_ept)
  2002. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2003. /* When we are running a nested L2 guest and L1 specified for it a
  2004. * certain exception bitmap, we must trap the same exceptions and pass
  2005. * them to L1. When running L2, we will only handle the exceptions
  2006. * specified above if L1 did not want them.
  2007. */
  2008. if (is_guest_mode(vcpu))
  2009. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2010. vmcs_write32(EXCEPTION_BITMAP, eb);
  2011. }
  2012. /*
  2013. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2014. */
  2015. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2016. {
  2017. unsigned long *msr_bitmap;
  2018. int f = sizeof(unsigned long);
  2019. if (!cpu_has_vmx_msr_bitmap())
  2020. return true;
  2021. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2022. if (msr <= 0x1fff) {
  2023. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2024. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2025. msr &= 0x1fff;
  2026. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2027. }
  2028. return true;
  2029. }
  2030. /*
  2031. * Check if MSR is intercepted for L01 MSR bitmap.
  2032. */
  2033. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2034. {
  2035. unsigned long *msr_bitmap;
  2036. int f = sizeof(unsigned long);
  2037. if (!cpu_has_vmx_msr_bitmap())
  2038. return true;
  2039. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2040. if (msr <= 0x1fff) {
  2041. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2042. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2043. msr &= 0x1fff;
  2044. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2045. }
  2046. return true;
  2047. }
  2048. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2049. unsigned long entry, unsigned long exit)
  2050. {
  2051. vm_entry_controls_clearbit(vmx, entry);
  2052. vm_exit_controls_clearbit(vmx, exit);
  2053. }
  2054. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2055. {
  2056. unsigned i;
  2057. struct msr_autoload *m = &vmx->msr_autoload;
  2058. switch (msr) {
  2059. case MSR_EFER:
  2060. if (cpu_has_load_ia32_efer) {
  2061. clear_atomic_switch_msr_special(vmx,
  2062. VM_ENTRY_LOAD_IA32_EFER,
  2063. VM_EXIT_LOAD_IA32_EFER);
  2064. return;
  2065. }
  2066. break;
  2067. case MSR_CORE_PERF_GLOBAL_CTRL:
  2068. if (cpu_has_load_perf_global_ctrl) {
  2069. clear_atomic_switch_msr_special(vmx,
  2070. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2071. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2072. return;
  2073. }
  2074. break;
  2075. }
  2076. for (i = 0; i < m->nr; ++i)
  2077. if (m->guest[i].index == msr)
  2078. break;
  2079. if (i == m->nr)
  2080. return;
  2081. --m->nr;
  2082. m->guest[i] = m->guest[m->nr];
  2083. m->host[i] = m->host[m->nr];
  2084. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2085. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2086. }
  2087. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2088. unsigned long entry, unsigned long exit,
  2089. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2090. u64 guest_val, u64 host_val)
  2091. {
  2092. vmcs_write64(guest_val_vmcs, guest_val);
  2093. vmcs_write64(host_val_vmcs, host_val);
  2094. vm_entry_controls_setbit(vmx, entry);
  2095. vm_exit_controls_setbit(vmx, exit);
  2096. }
  2097. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2098. u64 guest_val, u64 host_val)
  2099. {
  2100. unsigned i;
  2101. struct msr_autoload *m = &vmx->msr_autoload;
  2102. switch (msr) {
  2103. case MSR_EFER:
  2104. if (cpu_has_load_ia32_efer) {
  2105. add_atomic_switch_msr_special(vmx,
  2106. VM_ENTRY_LOAD_IA32_EFER,
  2107. VM_EXIT_LOAD_IA32_EFER,
  2108. GUEST_IA32_EFER,
  2109. HOST_IA32_EFER,
  2110. guest_val, host_val);
  2111. return;
  2112. }
  2113. break;
  2114. case MSR_CORE_PERF_GLOBAL_CTRL:
  2115. if (cpu_has_load_perf_global_ctrl) {
  2116. add_atomic_switch_msr_special(vmx,
  2117. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2118. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2119. GUEST_IA32_PERF_GLOBAL_CTRL,
  2120. HOST_IA32_PERF_GLOBAL_CTRL,
  2121. guest_val, host_val);
  2122. return;
  2123. }
  2124. break;
  2125. case MSR_IA32_PEBS_ENABLE:
  2126. /* PEBS needs a quiescent period after being disabled (to write
  2127. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2128. * provide that period, so a CPU could write host's record into
  2129. * guest's memory.
  2130. */
  2131. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2132. }
  2133. for (i = 0; i < m->nr; ++i)
  2134. if (m->guest[i].index == msr)
  2135. break;
  2136. if (i == NR_AUTOLOAD_MSRS) {
  2137. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2138. "Can't add msr %x\n", msr);
  2139. return;
  2140. } else if (i == m->nr) {
  2141. ++m->nr;
  2142. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2143. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2144. }
  2145. m->guest[i].index = msr;
  2146. m->guest[i].value = guest_val;
  2147. m->host[i].index = msr;
  2148. m->host[i].value = host_val;
  2149. }
  2150. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2151. {
  2152. u64 guest_efer = vmx->vcpu.arch.efer;
  2153. u64 ignore_bits = 0;
  2154. if (!enable_ept) {
  2155. /*
  2156. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2157. * host CPUID is more efficient than testing guest CPUID
  2158. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2159. */
  2160. if (boot_cpu_has(X86_FEATURE_SMEP))
  2161. guest_efer |= EFER_NX;
  2162. else if (!(guest_efer & EFER_NX))
  2163. ignore_bits |= EFER_NX;
  2164. }
  2165. /*
  2166. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2167. */
  2168. ignore_bits |= EFER_SCE;
  2169. #ifdef CONFIG_X86_64
  2170. ignore_bits |= EFER_LMA | EFER_LME;
  2171. /* SCE is meaningful only in long mode on Intel */
  2172. if (guest_efer & EFER_LMA)
  2173. ignore_bits &= ~(u64)EFER_SCE;
  2174. #endif
  2175. clear_atomic_switch_msr(vmx, MSR_EFER);
  2176. /*
  2177. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2178. * On CPUs that support "load IA32_EFER", always switch EFER
  2179. * atomically, since it's faster than switching it manually.
  2180. */
  2181. if (cpu_has_load_ia32_efer ||
  2182. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2183. if (!(guest_efer & EFER_LMA))
  2184. guest_efer &= ~EFER_LME;
  2185. if (guest_efer != host_efer)
  2186. add_atomic_switch_msr(vmx, MSR_EFER,
  2187. guest_efer, host_efer);
  2188. return false;
  2189. } else {
  2190. guest_efer &= ~ignore_bits;
  2191. guest_efer |= host_efer & ignore_bits;
  2192. vmx->guest_msrs[efer_offset].data = guest_efer;
  2193. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2194. return true;
  2195. }
  2196. }
  2197. #ifdef CONFIG_X86_32
  2198. /*
  2199. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2200. * VMCS rather than the segment table. KVM uses this helper to figure
  2201. * out the current bases to poke them into the VMCS before entry.
  2202. */
  2203. static unsigned long segment_base(u16 selector)
  2204. {
  2205. struct desc_struct *table;
  2206. unsigned long v;
  2207. if (!(selector & ~SEGMENT_RPL_MASK))
  2208. return 0;
  2209. table = get_current_gdt_ro();
  2210. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2211. u16 ldt_selector = kvm_read_ldt();
  2212. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2213. return 0;
  2214. table = (struct desc_struct *)segment_base(ldt_selector);
  2215. }
  2216. v = get_desc_base(&table[selector >> 3]);
  2217. return v;
  2218. }
  2219. #endif
  2220. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  2221. {
  2222. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2223. #ifdef CONFIG_X86_64
  2224. int cpu = raw_smp_processor_id();
  2225. unsigned long fs_base, kernel_gs_base;
  2226. #endif
  2227. int i;
  2228. if (vmx->host_state.loaded)
  2229. return;
  2230. vmx->host_state.loaded = 1;
  2231. /*
  2232. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2233. * allow segment selectors with cpl > 0 or ti == 1.
  2234. */
  2235. vmx->host_state.ldt_sel = kvm_read_ldt();
  2236. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  2237. #ifdef CONFIG_X86_64
  2238. if (likely(is_64bit_mm(current->mm))) {
  2239. save_fsgs_for_kvm();
  2240. vmx->host_state.fs_sel = current->thread.fsindex;
  2241. vmx->host_state.gs_sel = current->thread.gsindex;
  2242. fs_base = current->thread.fsbase;
  2243. kernel_gs_base = current->thread.gsbase;
  2244. } else {
  2245. #endif
  2246. savesegment(fs, vmx->host_state.fs_sel);
  2247. savesegment(gs, vmx->host_state.gs_sel);
  2248. #ifdef CONFIG_X86_64
  2249. fs_base = read_msr(MSR_FS_BASE);
  2250. kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2251. }
  2252. #endif
  2253. if (!(vmx->host_state.fs_sel & 7)) {
  2254. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  2255. vmx->host_state.fs_reload_needed = 0;
  2256. } else {
  2257. vmcs_write16(HOST_FS_SELECTOR, 0);
  2258. vmx->host_state.fs_reload_needed = 1;
  2259. }
  2260. if (!(vmx->host_state.gs_sel & 7))
  2261. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  2262. else {
  2263. vmcs_write16(HOST_GS_SELECTOR, 0);
  2264. vmx->host_state.gs_ldt_reload_needed = 1;
  2265. }
  2266. #ifdef CONFIG_X86_64
  2267. savesegment(ds, vmx->host_state.ds_sel);
  2268. savesegment(es, vmx->host_state.es_sel);
  2269. vmcs_writel(HOST_FS_BASE, fs_base);
  2270. vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
  2271. vmx->msr_host_kernel_gs_base = kernel_gs_base;
  2272. if (is_long_mode(&vmx->vcpu))
  2273. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2274. #else
  2275. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  2276. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  2277. #endif
  2278. if (boot_cpu_has(X86_FEATURE_MPX))
  2279. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  2280. for (i = 0; i < vmx->save_nmsrs; ++i)
  2281. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2282. vmx->guest_msrs[i].data,
  2283. vmx->guest_msrs[i].mask);
  2284. }
  2285. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  2286. {
  2287. if (!vmx->host_state.loaded)
  2288. return;
  2289. ++vmx->vcpu.stat.host_state_reload;
  2290. vmx->host_state.loaded = 0;
  2291. #ifdef CONFIG_X86_64
  2292. if (is_long_mode(&vmx->vcpu))
  2293. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2294. #endif
  2295. if (vmx->host_state.gs_ldt_reload_needed) {
  2296. kvm_load_ldt(vmx->host_state.ldt_sel);
  2297. #ifdef CONFIG_X86_64
  2298. load_gs_index(vmx->host_state.gs_sel);
  2299. #else
  2300. loadsegment(gs, vmx->host_state.gs_sel);
  2301. #endif
  2302. }
  2303. if (vmx->host_state.fs_reload_needed)
  2304. loadsegment(fs, vmx->host_state.fs_sel);
  2305. #ifdef CONFIG_X86_64
  2306. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  2307. loadsegment(ds, vmx->host_state.ds_sel);
  2308. loadsegment(es, vmx->host_state.es_sel);
  2309. }
  2310. #endif
  2311. invalidate_tss_limit();
  2312. #ifdef CONFIG_X86_64
  2313. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2314. #endif
  2315. if (vmx->host_state.msr_host_bndcfgs)
  2316. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  2317. load_fixmap_gdt(raw_smp_processor_id());
  2318. }
  2319. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  2320. {
  2321. preempt_disable();
  2322. __vmx_load_host_state(vmx);
  2323. preempt_enable();
  2324. }
  2325. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2326. {
  2327. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2328. struct pi_desc old, new;
  2329. unsigned int dest;
  2330. /*
  2331. * In case of hot-plug or hot-unplug, we may have to undo
  2332. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2333. * always keep PI.NDST up to date for simplicity: it makes the
  2334. * code easier, and CPU migration is not a fast path.
  2335. */
  2336. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2337. return;
  2338. /*
  2339. * First handle the simple case where no cmpxchg is necessary; just
  2340. * allow posting non-urgent interrupts.
  2341. *
  2342. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2343. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2344. * expects the VCPU to be on the blocked_vcpu_list that matches
  2345. * PI.NDST.
  2346. */
  2347. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2348. vcpu->cpu == cpu) {
  2349. pi_clear_sn(pi_desc);
  2350. return;
  2351. }
  2352. /* The full case. */
  2353. do {
  2354. old.control = new.control = pi_desc->control;
  2355. dest = cpu_physical_id(cpu);
  2356. if (x2apic_enabled())
  2357. new.ndst = dest;
  2358. else
  2359. new.ndst = (dest << 8) & 0xFF00;
  2360. new.sn = 0;
  2361. } while (cmpxchg64(&pi_desc->control, old.control,
  2362. new.control) != old.control);
  2363. }
  2364. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2365. {
  2366. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2367. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2368. }
  2369. /*
  2370. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2371. * vcpu mutex is already taken.
  2372. */
  2373. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2374. {
  2375. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2376. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2377. if (!already_loaded) {
  2378. loaded_vmcs_clear(vmx->loaded_vmcs);
  2379. local_irq_disable();
  2380. crash_disable_local_vmclear(cpu);
  2381. /*
  2382. * Read loaded_vmcs->cpu should be before fetching
  2383. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2384. * See the comments in __loaded_vmcs_clear().
  2385. */
  2386. smp_rmb();
  2387. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2388. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2389. crash_enable_local_vmclear(cpu);
  2390. local_irq_enable();
  2391. }
  2392. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2393. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2394. vmcs_load(vmx->loaded_vmcs->vmcs);
  2395. indirect_branch_prediction_barrier();
  2396. }
  2397. if (!already_loaded) {
  2398. void *gdt = get_current_gdt_ro();
  2399. unsigned long sysenter_esp;
  2400. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2401. /*
  2402. * Linux uses per-cpu TSS and GDT, so set these when switching
  2403. * processors. See 22.2.4.
  2404. */
  2405. vmcs_writel(HOST_TR_BASE,
  2406. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2407. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2408. /*
  2409. * VM exits change the host TR limit to 0x67 after a VM
  2410. * exit. This is okay, since 0x67 covers everything except
  2411. * the IO bitmap and have have code to handle the IO bitmap
  2412. * being lost after a VM exit.
  2413. */
  2414. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2415. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2416. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2417. vmx->loaded_vmcs->cpu = cpu;
  2418. }
  2419. /* Setup TSC multiplier */
  2420. if (kvm_has_tsc_control &&
  2421. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2422. decache_tsc_multiplier(vmx);
  2423. vmx_vcpu_pi_load(vcpu, cpu);
  2424. vmx->host_pkru = read_pkru();
  2425. vmx->host_debugctlmsr = get_debugctlmsr();
  2426. }
  2427. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2428. {
  2429. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2430. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2431. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2432. !kvm_vcpu_apicv_active(vcpu))
  2433. return;
  2434. /* Set SN when the vCPU is preempted */
  2435. if (vcpu->preempted)
  2436. pi_set_sn(pi_desc);
  2437. }
  2438. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2439. {
  2440. vmx_vcpu_pi_put(vcpu);
  2441. __vmx_load_host_state(to_vmx(vcpu));
  2442. }
  2443. static bool emulation_required(struct kvm_vcpu *vcpu)
  2444. {
  2445. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2446. }
  2447. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2448. /*
  2449. * Return the cr0 value that a nested guest would read. This is a combination
  2450. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2451. * its hypervisor (cr0_read_shadow).
  2452. */
  2453. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2454. {
  2455. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2456. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2457. }
  2458. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2459. {
  2460. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2461. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2462. }
  2463. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2464. {
  2465. unsigned long rflags, save_rflags;
  2466. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2467. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2468. rflags = vmcs_readl(GUEST_RFLAGS);
  2469. if (to_vmx(vcpu)->rmode.vm86_active) {
  2470. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2471. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2472. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2473. }
  2474. to_vmx(vcpu)->rflags = rflags;
  2475. }
  2476. return to_vmx(vcpu)->rflags;
  2477. }
  2478. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2479. {
  2480. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2481. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2482. to_vmx(vcpu)->rflags = rflags;
  2483. if (to_vmx(vcpu)->rmode.vm86_active) {
  2484. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2485. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2486. }
  2487. vmcs_writel(GUEST_RFLAGS, rflags);
  2488. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2489. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2490. }
  2491. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2492. {
  2493. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2494. int ret = 0;
  2495. if (interruptibility & GUEST_INTR_STATE_STI)
  2496. ret |= KVM_X86_SHADOW_INT_STI;
  2497. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2498. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2499. return ret;
  2500. }
  2501. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2502. {
  2503. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2504. u32 interruptibility = interruptibility_old;
  2505. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2506. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2507. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2508. else if (mask & KVM_X86_SHADOW_INT_STI)
  2509. interruptibility |= GUEST_INTR_STATE_STI;
  2510. if ((interruptibility != interruptibility_old))
  2511. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2512. }
  2513. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2514. {
  2515. unsigned long rip;
  2516. rip = kvm_rip_read(vcpu);
  2517. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2518. kvm_rip_write(vcpu, rip);
  2519. /* skipping an emulated instruction also counts */
  2520. vmx_set_interrupt_shadow(vcpu, 0);
  2521. }
  2522. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2523. unsigned long exit_qual)
  2524. {
  2525. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2526. unsigned int nr = vcpu->arch.exception.nr;
  2527. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2528. if (vcpu->arch.exception.has_error_code) {
  2529. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2530. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2531. }
  2532. if (kvm_exception_is_soft(nr))
  2533. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2534. else
  2535. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2536. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2537. vmx_get_nmi_mask(vcpu))
  2538. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2539. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2540. }
  2541. /*
  2542. * KVM wants to inject page-faults which it got to the guest. This function
  2543. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2544. */
  2545. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2546. {
  2547. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2548. unsigned int nr = vcpu->arch.exception.nr;
  2549. if (nr == PF_VECTOR) {
  2550. if (vcpu->arch.exception.nested_apf) {
  2551. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2552. return 1;
  2553. }
  2554. /*
  2555. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2556. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2557. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2558. * can be written only when inject_pending_event runs. This should be
  2559. * conditional on a new capability---if the capability is disabled,
  2560. * kvm_multiple_exception would write the ancillary information to
  2561. * CR2 or DR6, for backwards ABI-compatibility.
  2562. */
  2563. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2564. vcpu->arch.exception.error_code)) {
  2565. *exit_qual = vcpu->arch.cr2;
  2566. return 1;
  2567. }
  2568. } else {
  2569. if (vmcs12->exception_bitmap & (1u << nr)) {
  2570. if (nr == DB_VECTOR)
  2571. *exit_qual = vcpu->arch.dr6;
  2572. else
  2573. *exit_qual = 0;
  2574. return 1;
  2575. }
  2576. }
  2577. return 0;
  2578. }
  2579. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2580. {
  2581. /*
  2582. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2583. * explicitly skip the instruction because if the HLT state is set,
  2584. * then the instruction is already executing and RIP has already been
  2585. * advanced.
  2586. */
  2587. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2588. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2589. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2590. }
  2591. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2592. {
  2593. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2594. unsigned nr = vcpu->arch.exception.nr;
  2595. bool has_error_code = vcpu->arch.exception.has_error_code;
  2596. u32 error_code = vcpu->arch.exception.error_code;
  2597. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2598. if (has_error_code) {
  2599. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2600. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2601. }
  2602. if (vmx->rmode.vm86_active) {
  2603. int inc_eip = 0;
  2604. if (kvm_exception_is_soft(nr))
  2605. inc_eip = vcpu->arch.event_exit_inst_len;
  2606. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2607. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2608. return;
  2609. }
  2610. WARN_ON_ONCE(vmx->emulation_required);
  2611. if (kvm_exception_is_soft(nr)) {
  2612. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2613. vmx->vcpu.arch.event_exit_inst_len);
  2614. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2615. } else
  2616. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2617. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2618. vmx_clear_hlt(vcpu);
  2619. }
  2620. static bool vmx_rdtscp_supported(void)
  2621. {
  2622. return cpu_has_vmx_rdtscp();
  2623. }
  2624. static bool vmx_invpcid_supported(void)
  2625. {
  2626. return cpu_has_vmx_invpcid() && enable_ept;
  2627. }
  2628. /*
  2629. * Swap MSR entry in host/guest MSR entry array.
  2630. */
  2631. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2632. {
  2633. struct shared_msr_entry tmp;
  2634. tmp = vmx->guest_msrs[to];
  2635. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2636. vmx->guest_msrs[from] = tmp;
  2637. }
  2638. /*
  2639. * Set up the vmcs to automatically save and restore system
  2640. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2641. * mode, as fiddling with msrs is very expensive.
  2642. */
  2643. static void setup_msrs(struct vcpu_vmx *vmx)
  2644. {
  2645. int save_nmsrs, index;
  2646. save_nmsrs = 0;
  2647. #ifdef CONFIG_X86_64
  2648. if (is_long_mode(&vmx->vcpu)) {
  2649. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2650. if (index >= 0)
  2651. move_msr_up(vmx, index, save_nmsrs++);
  2652. index = __find_msr_index(vmx, MSR_LSTAR);
  2653. if (index >= 0)
  2654. move_msr_up(vmx, index, save_nmsrs++);
  2655. index = __find_msr_index(vmx, MSR_CSTAR);
  2656. if (index >= 0)
  2657. move_msr_up(vmx, index, save_nmsrs++);
  2658. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2659. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2660. move_msr_up(vmx, index, save_nmsrs++);
  2661. /*
  2662. * MSR_STAR is only needed on long mode guests, and only
  2663. * if efer.sce is enabled.
  2664. */
  2665. index = __find_msr_index(vmx, MSR_STAR);
  2666. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2667. move_msr_up(vmx, index, save_nmsrs++);
  2668. }
  2669. #endif
  2670. index = __find_msr_index(vmx, MSR_EFER);
  2671. if (index >= 0 && update_transition_efer(vmx, index))
  2672. move_msr_up(vmx, index, save_nmsrs++);
  2673. vmx->save_nmsrs = save_nmsrs;
  2674. if (cpu_has_vmx_msr_bitmap())
  2675. vmx_update_msr_bitmap(&vmx->vcpu);
  2676. }
  2677. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2678. {
  2679. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2680. if (is_guest_mode(vcpu) &&
  2681. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2682. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2683. return vcpu->arch.tsc_offset;
  2684. }
  2685. /*
  2686. * writes 'offset' into guest's timestamp counter offset register
  2687. */
  2688. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2689. {
  2690. if (is_guest_mode(vcpu)) {
  2691. /*
  2692. * We're here if L1 chose not to trap WRMSR to TSC. According
  2693. * to the spec, this should set L1's TSC; The offset that L1
  2694. * set for L2 remains unchanged, and still needs to be added
  2695. * to the newly set TSC to get L2's TSC.
  2696. */
  2697. struct vmcs12 *vmcs12;
  2698. /* recalculate vmcs02.TSC_OFFSET: */
  2699. vmcs12 = get_vmcs12(vcpu);
  2700. vmcs_write64(TSC_OFFSET, offset +
  2701. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2702. vmcs12->tsc_offset : 0));
  2703. } else {
  2704. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2705. vmcs_read64(TSC_OFFSET), offset);
  2706. vmcs_write64(TSC_OFFSET, offset);
  2707. }
  2708. }
  2709. /*
  2710. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2711. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2712. * all guests if the "nested" module option is off, and can also be disabled
  2713. * for a single guest by disabling its VMX cpuid bit.
  2714. */
  2715. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2716. {
  2717. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2718. }
  2719. /*
  2720. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2721. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2722. * The same values should also be used to verify that vmcs12 control fields are
  2723. * valid during nested entry from L1 to L2.
  2724. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2725. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2726. * bit in the high half is on if the corresponding bit in the control field
  2727. * may be on. See also vmx_control_verify().
  2728. */
  2729. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2730. {
  2731. if (!nested) {
  2732. memset(msrs, 0, sizeof(*msrs));
  2733. return;
  2734. }
  2735. /*
  2736. * Note that as a general rule, the high half of the MSRs (bits in
  2737. * the control fields which may be 1) should be initialized by the
  2738. * intersection of the underlying hardware's MSR (i.e., features which
  2739. * can be supported) and the list of features we want to expose -
  2740. * because they are known to be properly supported in our code.
  2741. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2742. * be set to 0, meaning that L1 may turn off any of these bits. The
  2743. * reason is that if one of these bits is necessary, it will appear
  2744. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2745. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2746. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2747. * These rules have exceptions below.
  2748. */
  2749. /* pin-based controls */
  2750. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2751. msrs->pinbased_ctls_low,
  2752. msrs->pinbased_ctls_high);
  2753. msrs->pinbased_ctls_low |=
  2754. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2755. msrs->pinbased_ctls_high &=
  2756. PIN_BASED_EXT_INTR_MASK |
  2757. PIN_BASED_NMI_EXITING |
  2758. PIN_BASED_VIRTUAL_NMIS |
  2759. (apicv ? PIN_BASED_POSTED_INTR : 0);
  2760. msrs->pinbased_ctls_high |=
  2761. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2762. PIN_BASED_VMX_PREEMPTION_TIMER;
  2763. /* exit controls */
  2764. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2765. msrs->exit_ctls_low,
  2766. msrs->exit_ctls_high);
  2767. msrs->exit_ctls_low =
  2768. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2769. msrs->exit_ctls_high &=
  2770. #ifdef CONFIG_X86_64
  2771. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2772. #endif
  2773. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2774. msrs->exit_ctls_high |=
  2775. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2776. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2777. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2778. if (kvm_mpx_supported())
  2779. msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2780. /* We support free control of debug control saving. */
  2781. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2782. /* entry controls */
  2783. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2784. msrs->entry_ctls_low,
  2785. msrs->entry_ctls_high);
  2786. msrs->entry_ctls_low =
  2787. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2788. msrs->entry_ctls_high &=
  2789. #ifdef CONFIG_X86_64
  2790. VM_ENTRY_IA32E_MODE |
  2791. #endif
  2792. VM_ENTRY_LOAD_IA32_PAT;
  2793. msrs->entry_ctls_high |=
  2794. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2795. if (kvm_mpx_supported())
  2796. msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2797. /* We support free control of debug control loading. */
  2798. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2799. /* cpu-based controls */
  2800. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2801. msrs->procbased_ctls_low,
  2802. msrs->procbased_ctls_high);
  2803. msrs->procbased_ctls_low =
  2804. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2805. msrs->procbased_ctls_high &=
  2806. CPU_BASED_VIRTUAL_INTR_PENDING |
  2807. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2808. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2809. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2810. CPU_BASED_CR3_STORE_EXITING |
  2811. #ifdef CONFIG_X86_64
  2812. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2813. #endif
  2814. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2815. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2816. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2817. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2818. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2819. /*
  2820. * We can allow some features even when not supported by the
  2821. * hardware. For example, L1 can specify an MSR bitmap - and we
  2822. * can use it to avoid exits to L1 - even when L0 runs L2
  2823. * without MSR bitmaps.
  2824. */
  2825. msrs->procbased_ctls_high |=
  2826. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2827. CPU_BASED_USE_MSR_BITMAPS;
  2828. /* We support free control of CR3 access interception. */
  2829. msrs->procbased_ctls_low &=
  2830. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2831. /*
  2832. * secondary cpu-based controls. Do not include those that
  2833. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  2834. */
  2835. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2836. msrs->secondary_ctls_low,
  2837. msrs->secondary_ctls_high);
  2838. msrs->secondary_ctls_low = 0;
  2839. msrs->secondary_ctls_high &=
  2840. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2841. SECONDARY_EXEC_DESC |
  2842. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2843. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2844. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2845. SECONDARY_EXEC_WBINVD_EXITING;
  2846. if (enable_ept) {
  2847. /* nested EPT: emulate EPT also to L1 */
  2848. msrs->secondary_ctls_high |=
  2849. SECONDARY_EXEC_ENABLE_EPT;
  2850. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2851. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  2852. if (cpu_has_vmx_ept_execute_only())
  2853. msrs->ept_caps |=
  2854. VMX_EPT_EXECUTE_ONLY_BIT;
  2855. msrs->ept_caps &= vmx_capability.ept;
  2856. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  2857. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  2858. VMX_EPT_1GB_PAGE_BIT;
  2859. if (enable_ept_ad_bits) {
  2860. msrs->secondary_ctls_high |=
  2861. SECONDARY_EXEC_ENABLE_PML;
  2862. msrs->ept_caps |= VMX_EPT_AD_BIT;
  2863. }
  2864. }
  2865. if (cpu_has_vmx_vmfunc()) {
  2866. msrs->secondary_ctls_high |=
  2867. SECONDARY_EXEC_ENABLE_VMFUNC;
  2868. /*
  2869. * Advertise EPTP switching unconditionally
  2870. * since we emulate it
  2871. */
  2872. if (enable_ept)
  2873. msrs->vmfunc_controls =
  2874. VMX_VMFUNC_EPTP_SWITCHING;
  2875. }
  2876. /*
  2877. * Old versions of KVM use the single-context version without
  2878. * checking for support, so declare that it is supported even
  2879. * though it is treated as global context. The alternative is
  2880. * not failing the single-context invvpid, and it is worse.
  2881. */
  2882. if (enable_vpid) {
  2883. msrs->secondary_ctls_high |=
  2884. SECONDARY_EXEC_ENABLE_VPID;
  2885. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  2886. VMX_VPID_EXTENT_SUPPORTED_MASK;
  2887. }
  2888. if (enable_unrestricted_guest)
  2889. msrs->secondary_ctls_high |=
  2890. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2891. /* miscellaneous data */
  2892. rdmsr(MSR_IA32_VMX_MISC,
  2893. msrs->misc_low,
  2894. msrs->misc_high);
  2895. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2896. msrs->misc_low |=
  2897. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  2898. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2899. VMX_MISC_ACTIVITY_HLT;
  2900. msrs->misc_high = 0;
  2901. /*
  2902. * This MSR reports some information about VMX support. We
  2903. * should return information about the VMX we emulate for the
  2904. * guest, and the VMCS structure we give it - not about the
  2905. * VMX support of the underlying hardware.
  2906. */
  2907. msrs->basic =
  2908. VMCS12_REVISION |
  2909. VMX_BASIC_TRUE_CTLS |
  2910. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2911. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2912. if (cpu_has_vmx_basic_inout())
  2913. msrs->basic |= VMX_BASIC_INOUT;
  2914. /*
  2915. * These MSRs specify bits which the guest must keep fixed on
  2916. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2917. * We picked the standard core2 setting.
  2918. */
  2919. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2920. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2921. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  2922. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  2923. /* These MSRs specify bits which the guest must keep fixed off. */
  2924. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  2925. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  2926. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2927. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  2928. }
  2929. /*
  2930. * if fixed0[i] == 1: val[i] must be 1
  2931. * if fixed1[i] == 0: val[i] must be 0
  2932. */
  2933. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  2934. {
  2935. return ((val & fixed1) | fixed0) == val;
  2936. }
  2937. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2938. {
  2939. return fixed_bits_valid(control, low, high);
  2940. }
  2941. static inline u64 vmx_control_msr(u32 low, u32 high)
  2942. {
  2943. return low | ((u64)high << 32);
  2944. }
  2945. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  2946. {
  2947. superset &= mask;
  2948. subset &= mask;
  2949. return (superset | subset) == superset;
  2950. }
  2951. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  2952. {
  2953. const u64 feature_and_reserved =
  2954. /* feature (except bit 48; see below) */
  2955. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  2956. /* reserved */
  2957. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  2958. u64 vmx_basic = vmx->nested.msrs.basic;
  2959. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  2960. return -EINVAL;
  2961. /*
  2962. * KVM does not emulate a version of VMX that constrains physical
  2963. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  2964. */
  2965. if (data & BIT_ULL(48))
  2966. return -EINVAL;
  2967. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  2968. vmx_basic_vmcs_revision_id(data))
  2969. return -EINVAL;
  2970. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  2971. return -EINVAL;
  2972. vmx->nested.msrs.basic = data;
  2973. return 0;
  2974. }
  2975. static int
  2976. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2977. {
  2978. u64 supported;
  2979. u32 *lowp, *highp;
  2980. switch (msr_index) {
  2981. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2982. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  2983. highp = &vmx->nested.msrs.pinbased_ctls_high;
  2984. break;
  2985. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2986. lowp = &vmx->nested.msrs.procbased_ctls_low;
  2987. highp = &vmx->nested.msrs.procbased_ctls_high;
  2988. break;
  2989. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2990. lowp = &vmx->nested.msrs.exit_ctls_low;
  2991. highp = &vmx->nested.msrs.exit_ctls_high;
  2992. break;
  2993. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2994. lowp = &vmx->nested.msrs.entry_ctls_low;
  2995. highp = &vmx->nested.msrs.entry_ctls_high;
  2996. break;
  2997. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2998. lowp = &vmx->nested.msrs.secondary_ctls_low;
  2999. highp = &vmx->nested.msrs.secondary_ctls_high;
  3000. break;
  3001. default:
  3002. BUG();
  3003. }
  3004. supported = vmx_control_msr(*lowp, *highp);
  3005. /* Check must-be-1 bits are still 1. */
  3006. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3007. return -EINVAL;
  3008. /* Check must-be-0 bits are still 0. */
  3009. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3010. return -EINVAL;
  3011. *lowp = data;
  3012. *highp = data >> 32;
  3013. return 0;
  3014. }
  3015. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3016. {
  3017. const u64 feature_and_reserved_bits =
  3018. /* feature */
  3019. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3020. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3021. /* reserved */
  3022. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3023. u64 vmx_misc;
  3024. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3025. vmx->nested.msrs.misc_high);
  3026. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3027. return -EINVAL;
  3028. if ((vmx->nested.msrs.pinbased_ctls_high &
  3029. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3030. vmx_misc_preemption_timer_rate(data) !=
  3031. vmx_misc_preemption_timer_rate(vmx_misc))
  3032. return -EINVAL;
  3033. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3034. return -EINVAL;
  3035. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3036. return -EINVAL;
  3037. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3038. return -EINVAL;
  3039. vmx->nested.msrs.misc_low = data;
  3040. vmx->nested.msrs.misc_high = data >> 32;
  3041. /*
  3042. * If L1 has read-only VM-exit information fields, use the
  3043. * less permissive vmx_vmwrite_bitmap to specify write
  3044. * permissions for the shadow VMCS.
  3045. */
  3046. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3047. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3048. return 0;
  3049. }
  3050. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3051. {
  3052. u64 vmx_ept_vpid_cap;
  3053. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3054. vmx->nested.msrs.vpid_caps);
  3055. /* Every bit is either reserved or a feature bit. */
  3056. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3057. return -EINVAL;
  3058. vmx->nested.msrs.ept_caps = data;
  3059. vmx->nested.msrs.vpid_caps = data >> 32;
  3060. return 0;
  3061. }
  3062. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3063. {
  3064. u64 *msr;
  3065. switch (msr_index) {
  3066. case MSR_IA32_VMX_CR0_FIXED0:
  3067. msr = &vmx->nested.msrs.cr0_fixed0;
  3068. break;
  3069. case MSR_IA32_VMX_CR4_FIXED0:
  3070. msr = &vmx->nested.msrs.cr4_fixed0;
  3071. break;
  3072. default:
  3073. BUG();
  3074. }
  3075. /*
  3076. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3077. * must be 1 in the restored value.
  3078. */
  3079. if (!is_bitwise_subset(data, *msr, -1ULL))
  3080. return -EINVAL;
  3081. *msr = data;
  3082. return 0;
  3083. }
  3084. /*
  3085. * Called when userspace is restoring VMX MSRs.
  3086. *
  3087. * Returns 0 on success, non-0 otherwise.
  3088. */
  3089. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3090. {
  3091. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3092. /*
  3093. * Don't allow changes to the VMX capability MSRs while the vCPU
  3094. * is in VMX operation.
  3095. */
  3096. if (vmx->nested.vmxon)
  3097. return -EBUSY;
  3098. switch (msr_index) {
  3099. case MSR_IA32_VMX_BASIC:
  3100. return vmx_restore_vmx_basic(vmx, data);
  3101. case MSR_IA32_VMX_PINBASED_CTLS:
  3102. case MSR_IA32_VMX_PROCBASED_CTLS:
  3103. case MSR_IA32_VMX_EXIT_CTLS:
  3104. case MSR_IA32_VMX_ENTRY_CTLS:
  3105. /*
  3106. * The "non-true" VMX capability MSRs are generated from the
  3107. * "true" MSRs, so we do not support restoring them directly.
  3108. *
  3109. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3110. * should restore the "true" MSRs with the must-be-1 bits
  3111. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3112. * DEFAULT SETTINGS".
  3113. */
  3114. return -EINVAL;
  3115. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3116. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3117. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3118. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3119. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3120. return vmx_restore_control_msr(vmx, msr_index, data);
  3121. case MSR_IA32_VMX_MISC:
  3122. return vmx_restore_vmx_misc(vmx, data);
  3123. case MSR_IA32_VMX_CR0_FIXED0:
  3124. case MSR_IA32_VMX_CR4_FIXED0:
  3125. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3126. case MSR_IA32_VMX_CR0_FIXED1:
  3127. case MSR_IA32_VMX_CR4_FIXED1:
  3128. /*
  3129. * These MSRs are generated based on the vCPU's CPUID, so we
  3130. * do not support restoring them directly.
  3131. */
  3132. return -EINVAL;
  3133. case MSR_IA32_VMX_EPT_VPID_CAP:
  3134. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3135. case MSR_IA32_VMX_VMCS_ENUM:
  3136. vmx->nested.msrs.vmcs_enum = data;
  3137. return 0;
  3138. default:
  3139. /*
  3140. * The rest of the VMX capability MSRs do not support restore.
  3141. */
  3142. return -EINVAL;
  3143. }
  3144. }
  3145. /* Returns 0 on success, non-0 otherwise. */
  3146. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3147. {
  3148. switch (msr_index) {
  3149. case MSR_IA32_VMX_BASIC:
  3150. *pdata = msrs->basic;
  3151. break;
  3152. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3153. case MSR_IA32_VMX_PINBASED_CTLS:
  3154. *pdata = vmx_control_msr(
  3155. msrs->pinbased_ctls_low,
  3156. msrs->pinbased_ctls_high);
  3157. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3158. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3159. break;
  3160. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3161. case MSR_IA32_VMX_PROCBASED_CTLS:
  3162. *pdata = vmx_control_msr(
  3163. msrs->procbased_ctls_low,
  3164. msrs->procbased_ctls_high);
  3165. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3166. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3167. break;
  3168. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3169. case MSR_IA32_VMX_EXIT_CTLS:
  3170. *pdata = vmx_control_msr(
  3171. msrs->exit_ctls_low,
  3172. msrs->exit_ctls_high);
  3173. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3174. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3175. break;
  3176. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3177. case MSR_IA32_VMX_ENTRY_CTLS:
  3178. *pdata = vmx_control_msr(
  3179. msrs->entry_ctls_low,
  3180. msrs->entry_ctls_high);
  3181. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3182. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3183. break;
  3184. case MSR_IA32_VMX_MISC:
  3185. *pdata = vmx_control_msr(
  3186. msrs->misc_low,
  3187. msrs->misc_high);
  3188. break;
  3189. case MSR_IA32_VMX_CR0_FIXED0:
  3190. *pdata = msrs->cr0_fixed0;
  3191. break;
  3192. case MSR_IA32_VMX_CR0_FIXED1:
  3193. *pdata = msrs->cr0_fixed1;
  3194. break;
  3195. case MSR_IA32_VMX_CR4_FIXED0:
  3196. *pdata = msrs->cr4_fixed0;
  3197. break;
  3198. case MSR_IA32_VMX_CR4_FIXED1:
  3199. *pdata = msrs->cr4_fixed1;
  3200. break;
  3201. case MSR_IA32_VMX_VMCS_ENUM:
  3202. *pdata = msrs->vmcs_enum;
  3203. break;
  3204. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3205. *pdata = vmx_control_msr(
  3206. msrs->secondary_ctls_low,
  3207. msrs->secondary_ctls_high);
  3208. break;
  3209. case MSR_IA32_VMX_EPT_VPID_CAP:
  3210. *pdata = msrs->ept_caps |
  3211. ((u64)msrs->vpid_caps << 32);
  3212. break;
  3213. case MSR_IA32_VMX_VMFUNC:
  3214. *pdata = msrs->vmfunc_controls;
  3215. break;
  3216. default:
  3217. return 1;
  3218. }
  3219. return 0;
  3220. }
  3221. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3222. uint64_t val)
  3223. {
  3224. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3225. return !(val & ~valid_bits);
  3226. }
  3227. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3228. {
  3229. switch (msr->index) {
  3230. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3231. if (!nested)
  3232. return 1;
  3233. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3234. default:
  3235. return 1;
  3236. }
  3237. return 0;
  3238. }
  3239. /*
  3240. * Reads an msr value (of 'msr_index') into 'pdata'.
  3241. * Returns 0 on success, non-0 otherwise.
  3242. * Assumes vcpu_load() was already called.
  3243. */
  3244. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3245. {
  3246. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3247. struct shared_msr_entry *msr;
  3248. switch (msr_info->index) {
  3249. #ifdef CONFIG_X86_64
  3250. case MSR_FS_BASE:
  3251. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3252. break;
  3253. case MSR_GS_BASE:
  3254. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3255. break;
  3256. case MSR_KERNEL_GS_BASE:
  3257. vmx_load_host_state(vmx);
  3258. msr_info->data = vmx->msr_guest_kernel_gs_base;
  3259. break;
  3260. #endif
  3261. case MSR_EFER:
  3262. return kvm_get_msr_common(vcpu, msr_info);
  3263. case MSR_IA32_SPEC_CTRL:
  3264. if (!msr_info->host_initiated &&
  3265. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3266. return 1;
  3267. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3268. break;
  3269. case MSR_IA32_ARCH_CAPABILITIES:
  3270. if (!msr_info->host_initiated &&
  3271. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3272. return 1;
  3273. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3274. break;
  3275. case MSR_IA32_SYSENTER_CS:
  3276. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3277. break;
  3278. case MSR_IA32_SYSENTER_EIP:
  3279. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3280. break;
  3281. case MSR_IA32_SYSENTER_ESP:
  3282. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3283. break;
  3284. case MSR_IA32_BNDCFGS:
  3285. if (!kvm_mpx_supported() ||
  3286. (!msr_info->host_initiated &&
  3287. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3288. return 1;
  3289. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3290. break;
  3291. case MSR_IA32_MCG_EXT_CTL:
  3292. if (!msr_info->host_initiated &&
  3293. !(vmx->msr_ia32_feature_control &
  3294. FEATURE_CONTROL_LMCE))
  3295. return 1;
  3296. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3297. break;
  3298. case MSR_IA32_FEATURE_CONTROL:
  3299. msr_info->data = vmx->msr_ia32_feature_control;
  3300. break;
  3301. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3302. if (!nested_vmx_allowed(vcpu))
  3303. return 1;
  3304. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3305. &msr_info->data);
  3306. case MSR_IA32_XSS:
  3307. if (!vmx_xsaves_supported())
  3308. return 1;
  3309. msr_info->data = vcpu->arch.ia32_xss;
  3310. break;
  3311. case MSR_TSC_AUX:
  3312. if (!msr_info->host_initiated &&
  3313. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3314. return 1;
  3315. /* Otherwise falls through */
  3316. default:
  3317. msr = find_msr_entry(vmx, msr_info->index);
  3318. if (msr) {
  3319. msr_info->data = msr->data;
  3320. break;
  3321. }
  3322. return kvm_get_msr_common(vcpu, msr_info);
  3323. }
  3324. return 0;
  3325. }
  3326. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3327. /*
  3328. * Writes msr value into into the appropriate "register".
  3329. * Returns 0 on success, non-0 otherwise.
  3330. * Assumes vcpu_load() was already called.
  3331. */
  3332. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3333. {
  3334. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3335. struct shared_msr_entry *msr;
  3336. int ret = 0;
  3337. u32 msr_index = msr_info->index;
  3338. u64 data = msr_info->data;
  3339. switch (msr_index) {
  3340. case MSR_EFER:
  3341. ret = kvm_set_msr_common(vcpu, msr_info);
  3342. break;
  3343. #ifdef CONFIG_X86_64
  3344. case MSR_FS_BASE:
  3345. vmx_segment_cache_clear(vmx);
  3346. vmcs_writel(GUEST_FS_BASE, data);
  3347. break;
  3348. case MSR_GS_BASE:
  3349. vmx_segment_cache_clear(vmx);
  3350. vmcs_writel(GUEST_GS_BASE, data);
  3351. break;
  3352. case MSR_KERNEL_GS_BASE:
  3353. vmx_load_host_state(vmx);
  3354. vmx->msr_guest_kernel_gs_base = data;
  3355. break;
  3356. #endif
  3357. case MSR_IA32_SYSENTER_CS:
  3358. vmcs_write32(GUEST_SYSENTER_CS, data);
  3359. break;
  3360. case MSR_IA32_SYSENTER_EIP:
  3361. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3362. break;
  3363. case MSR_IA32_SYSENTER_ESP:
  3364. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3365. break;
  3366. case MSR_IA32_BNDCFGS:
  3367. if (!kvm_mpx_supported() ||
  3368. (!msr_info->host_initiated &&
  3369. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3370. return 1;
  3371. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3372. (data & MSR_IA32_BNDCFGS_RSVD))
  3373. return 1;
  3374. vmcs_write64(GUEST_BNDCFGS, data);
  3375. break;
  3376. case MSR_IA32_SPEC_CTRL:
  3377. if (!msr_info->host_initiated &&
  3378. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3379. return 1;
  3380. /* The STIBP bit doesn't fault even if it's not advertised */
  3381. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3382. return 1;
  3383. vmx->spec_ctrl = data;
  3384. if (!data)
  3385. break;
  3386. /*
  3387. * For non-nested:
  3388. * When it's written (to non-zero) for the first time, pass
  3389. * it through.
  3390. *
  3391. * For nested:
  3392. * The handling of the MSR bitmap for L2 guests is done in
  3393. * nested_vmx_merge_msr_bitmap. We should not touch the
  3394. * vmcs02.msr_bitmap here since it gets completely overwritten
  3395. * in the merging. We update the vmcs01 here for L1 as well
  3396. * since it will end up touching the MSR anyway now.
  3397. */
  3398. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3399. MSR_IA32_SPEC_CTRL,
  3400. MSR_TYPE_RW);
  3401. break;
  3402. case MSR_IA32_PRED_CMD:
  3403. if (!msr_info->host_initiated &&
  3404. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3405. return 1;
  3406. if (data & ~PRED_CMD_IBPB)
  3407. return 1;
  3408. if (!data)
  3409. break;
  3410. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3411. /*
  3412. * For non-nested:
  3413. * When it's written (to non-zero) for the first time, pass
  3414. * it through.
  3415. *
  3416. * For nested:
  3417. * The handling of the MSR bitmap for L2 guests is done in
  3418. * nested_vmx_merge_msr_bitmap. We should not touch the
  3419. * vmcs02.msr_bitmap here since it gets completely overwritten
  3420. * in the merging.
  3421. */
  3422. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3423. MSR_TYPE_W);
  3424. break;
  3425. case MSR_IA32_ARCH_CAPABILITIES:
  3426. if (!msr_info->host_initiated)
  3427. return 1;
  3428. vmx->arch_capabilities = data;
  3429. break;
  3430. case MSR_IA32_CR_PAT:
  3431. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3432. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3433. return 1;
  3434. vmcs_write64(GUEST_IA32_PAT, data);
  3435. vcpu->arch.pat = data;
  3436. break;
  3437. }
  3438. ret = kvm_set_msr_common(vcpu, msr_info);
  3439. break;
  3440. case MSR_IA32_TSC_ADJUST:
  3441. ret = kvm_set_msr_common(vcpu, msr_info);
  3442. break;
  3443. case MSR_IA32_MCG_EXT_CTL:
  3444. if ((!msr_info->host_initiated &&
  3445. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3446. FEATURE_CONTROL_LMCE)) ||
  3447. (data & ~MCG_EXT_CTL_LMCE_EN))
  3448. return 1;
  3449. vcpu->arch.mcg_ext_ctl = data;
  3450. break;
  3451. case MSR_IA32_FEATURE_CONTROL:
  3452. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3453. (to_vmx(vcpu)->msr_ia32_feature_control &
  3454. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3455. return 1;
  3456. vmx->msr_ia32_feature_control = data;
  3457. if (msr_info->host_initiated && data == 0)
  3458. vmx_leave_nested(vcpu);
  3459. break;
  3460. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3461. if (!msr_info->host_initiated)
  3462. return 1; /* they are read-only */
  3463. if (!nested_vmx_allowed(vcpu))
  3464. return 1;
  3465. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3466. case MSR_IA32_XSS:
  3467. if (!vmx_xsaves_supported())
  3468. return 1;
  3469. /*
  3470. * The only supported bit as of Skylake is bit 8, but
  3471. * it is not supported on KVM.
  3472. */
  3473. if (data != 0)
  3474. return 1;
  3475. vcpu->arch.ia32_xss = data;
  3476. if (vcpu->arch.ia32_xss != host_xss)
  3477. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3478. vcpu->arch.ia32_xss, host_xss);
  3479. else
  3480. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3481. break;
  3482. case MSR_TSC_AUX:
  3483. if (!msr_info->host_initiated &&
  3484. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3485. return 1;
  3486. /* Check reserved bit, higher 32 bits should be zero */
  3487. if ((data >> 32) != 0)
  3488. return 1;
  3489. /* Otherwise falls through */
  3490. default:
  3491. msr = find_msr_entry(vmx, msr_index);
  3492. if (msr) {
  3493. u64 old_msr_data = msr->data;
  3494. msr->data = data;
  3495. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3496. preempt_disable();
  3497. ret = kvm_set_shared_msr(msr->index, msr->data,
  3498. msr->mask);
  3499. preempt_enable();
  3500. if (ret)
  3501. msr->data = old_msr_data;
  3502. }
  3503. break;
  3504. }
  3505. ret = kvm_set_msr_common(vcpu, msr_info);
  3506. }
  3507. return ret;
  3508. }
  3509. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3510. {
  3511. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3512. switch (reg) {
  3513. case VCPU_REGS_RSP:
  3514. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3515. break;
  3516. case VCPU_REGS_RIP:
  3517. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3518. break;
  3519. case VCPU_EXREG_PDPTR:
  3520. if (enable_ept)
  3521. ept_save_pdptrs(vcpu);
  3522. break;
  3523. default:
  3524. break;
  3525. }
  3526. }
  3527. static __init int cpu_has_kvm_support(void)
  3528. {
  3529. return cpu_has_vmx();
  3530. }
  3531. static __init int vmx_disabled_by_bios(void)
  3532. {
  3533. u64 msr;
  3534. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3535. if (msr & FEATURE_CONTROL_LOCKED) {
  3536. /* launched w/ TXT and VMX disabled */
  3537. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3538. && tboot_enabled())
  3539. return 1;
  3540. /* launched w/o TXT and VMX only enabled w/ TXT */
  3541. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3542. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3543. && !tboot_enabled()) {
  3544. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3545. "activate TXT before enabling KVM\n");
  3546. return 1;
  3547. }
  3548. /* launched w/o TXT and VMX disabled */
  3549. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3550. && !tboot_enabled())
  3551. return 1;
  3552. }
  3553. return 0;
  3554. }
  3555. static void kvm_cpu_vmxon(u64 addr)
  3556. {
  3557. cr4_set_bits(X86_CR4_VMXE);
  3558. intel_pt_handle_vmx(1);
  3559. asm volatile (ASM_VMX_VMXON_RAX
  3560. : : "a"(&addr), "m"(addr)
  3561. : "memory", "cc");
  3562. }
  3563. static int hardware_enable(void)
  3564. {
  3565. int cpu = raw_smp_processor_id();
  3566. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3567. u64 old, test_bits;
  3568. if (cr4_read_shadow() & X86_CR4_VMXE)
  3569. return -EBUSY;
  3570. /*
  3571. * This can happen if we hot-added a CPU but failed to allocate
  3572. * VP assist page for it.
  3573. */
  3574. if (static_branch_unlikely(&enable_evmcs) &&
  3575. !hv_get_vp_assist_page(cpu))
  3576. return -EFAULT;
  3577. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3578. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3579. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3580. /*
  3581. * Now we can enable the vmclear operation in kdump
  3582. * since the loaded_vmcss_on_cpu list on this cpu
  3583. * has been initialized.
  3584. *
  3585. * Though the cpu is not in VMX operation now, there
  3586. * is no problem to enable the vmclear operation
  3587. * for the loaded_vmcss_on_cpu list is empty!
  3588. */
  3589. crash_enable_local_vmclear(cpu);
  3590. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3591. test_bits = FEATURE_CONTROL_LOCKED;
  3592. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3593. if (tboot_enabled())
  3594. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3595. if ((old & test_bits) != test_bits) {
  3596. /* enable and lock */
  3597. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3598. }
  3599. kvm_cpu_vmxon(phys_addr);
  3600. if (enable_ept)
  3601. ept_sync_global();
  3602. return 0;
  3603. }
  3604. static void vmclear_local_loaded_vmcss(void)
  3605. {
  3606. int cpu = raw_smp_processor_id();
  3607. struct loaded_vmcs *v, *n;
  3608. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3609. loaded_vmcss_on_cpu_link)
  3610. __loaded_vmcs_clear(v);
  3611. }
  3612. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3613. * tricks.
  3614. */
  3615. static void kvm_cpu_vmxoff(void)
  3616. {
  3617. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3618. intel_pt_handle_vmx(0);
  3619. cr4_clear_bits(X86_CR4_VMXE);
  3620. }
  3621. static void hardware_disable(void)
  3622. {
  3623. vmclear_local_loaded_vmcss();
  3624. kvm_cpu_vmxoff();
  3625. }
  3626. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3627. u32 msr, u32 *result)
  3628. {
  3629. u32 vmx_msr_low, vmx_msr_high;
  3630. u32 ctl = ctl_min | ctl_opt;
  3631. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3632. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3633. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3634. /* Ensure minimum (required) set of control bits are supported. */
  3635. if (ctl_min & ~ctl)
  3636. return -EIO;
  3637. *result = ctl;
  3638. return 0;
  3639. }
  3640. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3641. {
  3642. u32 vmx_msr_low, vmx_msr_high;
  3643. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3644. return vmx_msr_high & ctl;
  3645. }
  3646. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3647. {
  3648. u32 vmx_msr_low, vmx_msr_high;
  3649. u32 min, opt, min2, opt2;
  3650. u32 _pin_based_exec_control = 0;
  3651. u32 _cpu_based_exec_control = 0;
  3652. u32 _cpu_based_2nd_exec_control = 0;
  3653. u32 _vmexit_control = 0;
  3654. u32 _vmentry_control = 0;
  3655. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3656. min = CPU_BASED_HLT_EXITING |
  3657. #ifdef CONFIG_X86_64
  3658. CPU_BASED_CR8_LOAD_EXITING |
  3659. CPU_BASED_CR8_STORE_EXITING |
  3660. #endif
  3661. CPU_BASED_CR3_LOAD_EXITING |
  3662. CPU_BASED_CR3_STORE_EXITING |
  3663. CPU_BASED_UNCOND_IO_EXITING |
  3664. CPU_BASED_MOV_DR_EXITING |
  3665. CPU_BASED_USE_TSC_OFFSETING |
  3666. CPU_BASED_MWAIT_EXITING |
  3667. CPU_BASED_MONITOR_EXITING |
  3668. CPU_BASED_INVLPG_EXITING |
  3669. CPU_BASED_RDPMC_EXITING;
  3670. opt = CPU_BASED_TPR_SHADOW |
  3671. CPU_BASED_USE_MSR_BITMAPS |
  3672. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3673. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3674. &_cpu_based_exec_control) < 0)
  3675. return -EIO;
  3676. #ifdef CONFIG_X86_64
  3677. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3678. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3679. ~CPU_BASED_CR8_STORE_EXITING;
  3680. #endif
  3681. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3682. min2 = 0;
  3683. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3684. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3685. SECONDARY_EXEC_WBINVD_EXITING |
  3686. SECONDARY_EXEC_ENABLE_VPID |
  3687. SECONDARY_EXEC_ENABLE_EPT |
  3688. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3689. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3690. SECONDARY_EXEC_DESC |
  3691. SECONDARY_EXEC_RDTSCP |
  3692. SECONDARY_EXEC_ENABLE_INVPCID |
  3693. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3694. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3695. SECONDARY_EXEC_SHADOW_VMCS |
  3696. SECONDARY_EXEC_XSAVES |
  3697. SECONDARY_EXEC_RDSEED_EXITING |
  3698. SECONDARY_EXEC_RDRAND_EXITING |
  3699. SECONDARY_EXEC_ENABLE_PML |
  3700. SECONDARY_EXEC_TSC_SCALING |
  3701. SECONDARY_EXEC_ENABLE_VMFUNC;
  3702. if (adjust_vmx_controls(min2, opt2,
  3703. MSR_IA32_VMX_PROCBASED_CTLS2,
  3704. &_cpu_based_2nd_exec_control) < 0)
  3705. return -EIO;
  3706. }
  3707. #ifndef CONFIG_X86_64
  3708. if (!(_cpu_based_2nd_exec_control &
  3709. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3710. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3711. #endif
  3712. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3713. _cpu_based_2nd_exec_control &= ~(
  3714. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3715. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3716. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3717. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3718. &vmx_capability.ept, &vmx_capability.vpid);
  3719. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3720. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3721. enabled */
  3722. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3723. CPU_BASED_CR3_STORE_EXITING |
  3724. CPU_BASED_INVLPG_EXITING);
  3725. } else if (vmx_capability.ept) {
  3726. vmx_capability.ept = 0;
  3727. pr_warn_once("EPT CAP should not exist if not support "
  3728. "1-setting enable EPT VM-execution control\n");
  3729. }
  3730. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3731. vmx_capability.vpid) {
  3732. vmx_capability.vpid = 0;
  3733. pr_warn_once("VPID CAP should not exist if not support "
  3734. "1-setting enable VPID VM-execution control\n");
  3735. }
  3736. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3737. #ifdef CONFIG_X86_64
  3738. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3739. #endif
  3740. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3741. VM_EXIT_CLEAR_BNDCFGS;
  3742. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3743. &_vmexit_control) < 0)
  3744. return -EIO;
  3745. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3746. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3747. PIN_BASED_VMX_PREEMPTION_TIMER;
  3748. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3749. &_pin_based_exec_control) < 0)
  3750. return -EIO;
  3751. if (cpu_has_broken_vmx_preemption_timer())
  3752. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3753. if (!(_cpu_based_2nd_exec_control &
  3754. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3755. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3756. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3757. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3758. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3759. &_vmentry_control) < 0)
  3760. return -EIO;
  3761. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3762. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3763. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3764. return -EIO;
  3765. #ifdef CONFIG_X86_64
  3766. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3767. if (vmx_msr_high & (1u<<16))
  3768. return -EIO;
  3769. #endif
  3770. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3771. if (((vmx_msr_high >> 18) & 15) != 6)
  3772. return -EIO;
  3773. vmcs_conf->size = vmx_msr_high & 0x1fff;
  3774. vmcs_conf->order = get_order(vmcs_conf->size);
  3775. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  3776. vmcs_conf->revision_id = vmx_msr_low;
  3777. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  3778. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  3779. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  3780. vmcs_conf->vmexit_ctrl = _vmexit_control;
  3781. vmcs_conf->vmentry_ctrl = _vmentry_control;
  3782. if (static_branch_unlikely(&enable_evmcs))
  3783. evmcs_sanitize_exec_ctrls(vmcs_conf);
  3784. cpu_has_load_ia32_efer =
  3785. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3786. VM_ENTRY_LOAD_IA32_EFER)
  3787. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3788. VM_EXIT_LOAD_IA32_EFER);
  3789. cpu_has_load_perf_global_ctrl =
  3790. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3791. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  3792. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3793. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  3794. /*
  3795. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  3796. * but due to errata below it can't be used. Workaround is to use
  3797. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  3798. *
  3799. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  3800. *
  3801. * AAK155 (model 26)
  3802. * AAP115 (model 30)
  3803. * AAT100 (model 37)
  3804. * BC86,AAY89,BD102 (model 44)
  3805. * BA97 (model 46)
  3806. *
  3807. */
  3808. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  3809. switch (boot_cpu_data.x86_model) {
  3810. case 26:
  3811. case 30:
  3812. case 37:
  3813. case 44:
  3814. case 46:
  3815. cpu_has_load_perf_global_ctrl = false;
  3816. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  3817. "does not work properly. Using workaround\n");
  3818. break;
  3819. default:
  3820. break;
  3821. }
  3822. }
  3823. if (boot_cpu_has(X86_FEATURE_XSAVES))
  3824. rdmsrl(MSR_IA32_XSS, host_xss);
  3825. return 0;
  3826. }
  3827. static struct vmcs *alloc_vmcs_cpu(int cpu)
  3828. {
  3829. int node = cpu_to_node(cpu);
  3830. struct page *pages;
  3831. struct vmcs *vmcs;
  3832. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  3833. if (!pages)
  3834. return NULL;
  3835. vmcs = page_address(pages);
  3836. memset(vmcs, 0, vmcs_config.size);
  3837. /* KVM supports Enlightened VMCS v1 only */
  3838. if (static_branch_unlikely(&enable_evmcs))
  3839. vmcs->revision_id = KVM_EVMCS_VERSION;
  3840. else
  3841. vmcs->revision_id = vmcs_config.revision_id;
  3842. return vmcs;
  3843. }
  3844. static void free_vmcs(struct vmcs *vmcs)
  3845. {
  3846. free_pages((unsigned long)vmcs, vmcs_config.order);
  3847. }
  3848. /*
  3849. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  3850. */
  3851. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3852. {
  3853. if (!loaded_vmcs->vmcs)
  3854. return;
  3855. loaded_vmcs_clear(loaded_vmcs);
  3856. free_vmcs(loaded_vmcs->vmcs);
  3857. loaded_vmcs->vmcs = NULL;
  3858. if (loaded_vmcs->msr_bitmap)
  3859. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  3860. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  3861. }
  3862. static struct vmcs *alloc_vmcs(void)
  3863. {
  3864. return alloc_vmcs_cpu(raw_smp_processor_id());
  3865. }
  3866. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3867. {
  3868. loaded_vmcs->vmcs = alloc_vmcs();
  3869. if (!loaded_vmcs->vmcs)
  3870. return -ENOMEM;
  3871. loaded_vmcs->shadow_vmcs = NULL;
  3872. loaded_vmcs_init(loaded_vmcs);
  3873. if (cpu_has_vmx_msr_bitmap()) {
  3874. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  3875. if (!loaded_vmcs->msr_bitmap)
  3876. goto out_vmcs;
  3877. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  3878. if (IS_ENABLED(CONFIG_HYPERV) &&
  3879. static_branch_unlikely(&enable_evmcs) &&
  3880. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  3881. struct hv_enlightened_vmcs *evmcs =
  3882. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  3883. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  3884. }
  3885. }
  3886. return 0;
  3887. out_vmcs:
  3888. free_loaded_vmcs(loaded_vmcs);
  3889. return -ENOMEM;
  3890. }
  3891. static void free_kvm_area(void)
  3892. {
  3893. int cpu;
  3894. for_each_possible_cpu(cpu) {
  3895. free_vmcs(per_cpu(vmxarea, cpu));
  3896. per_cpu(vmxarea, cpu) = NULL;
  3897. }
  3898. }
  3899. enum vmcs_field_width {
  3900. VMCS_FIELD_WIDTH_U16 = 0,
  3901. VMCS_FIELD_WIDTH_U64 = 1,
  3902. VMCS_FIELD_WIDTH_U32 = 2,
  3903. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  3904. };
  3905. static inline int vmcs_field_width(unsigned long field)
  3906. {
  3907. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  3908. return VMCS_FIELD_WIDTH_U32;
  3909. return (field >> 13) & 0x3 ;
  3910. }
  3911. static inline int vmcs_field_readonly(unsigned long field)
  3912. {
  3913. return (((field >> 10) & 0x3) == 1);
  3914. }
  3915. static void init_vmcs_shadow_fields(void)
  3916. {
  3917. int i, j;
  3918. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  3919. u16 field = shadow_read_only_fields[i];
  3920. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3921. (i + 1 == max_shadow_read_only_fields ||
  3922. shadow_read_only_fields[i + 1] != field + 1))
  3923. pr_err("Missing field from shadow_read_only_field %x\n",
  3924. field + 1);
  3925. clear_bit(field, vmx_vmread_bitmap);
  3926. #ifdef CONFIG_X86_64
  3927. if (field & 1)
  3928. continue;
  3929. #endif
  3930. if (j < i)
  3931. shadow_read_only_fields[j] = field;
  3932. j++;
  3933. }
  3934. max_shadow_read_only_fields = j;
  3935. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  3936. u16 field = shadow_read_write_fields[i];
  3937. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3938. (i + 1 == max_shadow_read_write_fields ||
  3939. shadow_read_write_fields[i + 1] != field + 1))
  3940. pr_err("Missing field from shadow_read_write_field %x\n",
  3941. field + 1);
  3942. /*
  3943. * PML and the preemption timer can be emulated, but the
  3944. * processor cannot vmwrite to fields that don't exist
  3945. * on bare metal.
  3946. */
  3947. switch (field) {
  3948. case GUEST_PML_INDEX:
  3949. if (!cpu_has_vmx_pml())
  3950. continue;
  3951. break;
  3952. case VMX_PREEMPTION_TIMER_VALUE:
  3953. if (!cpu_has_vmx_preemption_timer())
  3954. continue;
  3955. break;
  3956. case GUEST_INTR_STATUS:
  3957. if (!cpu_has_vmx_apicv())
  3958. continue;
  3959. break;
  3960. default:
  3961. break;
  3962. }
  3963. clear_bit(field, vmx_vmwrite_bitmap);
  3964. clear_bit(field, vmx_vmread_bitmap);
  3965. #ifdef CONFIG_X86_64
  3966. if (field & 1)
  3967. continue;
  3968. #endif
  3969. if (j < i)
  3970. shadow_read_write_fields[j] = field;
  3971. j++;
  3972. }
  3973. max_shadow_read_write_fields = j;
  3974. }
  3975. static __init int alloc_kvm_area(void)
  3976. {
  3977. int cpu;
  3978. for_each_possible_cpu(cpu) {
  3979. struct vmcs *vmcs;
  3980. vmcs = alloc_vmcs_cpu(cpu);
  3981. if (!vmcs) {
  3982. free_kvm_area();
  3983. return -ENOMEM;
  3984. }
  3985. /*
  3986. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  3987. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  3988. * revision_id reported by MSR_IA32_VMX_BASIC.
  3989. *
  3990. * However, even though not explictly documented by
  3991. * TLFS, VMXArea passed as VMXON argument should
  3992. * still be marked with revision_id reported by
  3993. * physical CPU.
  3994. */
  3995. if (static_branch_unlikely(&enable_evmcs))
  3996. vmcs->revision_id = vmcs_config.revision_id;
  3997. per_cpu(vmxarea, cpu) = vmcs;
  3998. }
  3999. return 0;
  4000. }
  4001. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4002. struct kvm_segment *save)
  4003. {
  4004. if (!emulate_invalid_guest_state) {
  4005. /*
  4006. * CS and SS RPL should be equal during guest entry according
  4007. * to VMX spec, but in reality it is not always so. Since vcpu
  4008. * is in the middle of the transition from real mode to
  4009. * protected mode it is safe to assume that RPL 0 is a good
  4010. * default value.
  4011. */
  4012. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4013. save->selector &= ~SEGMENT_RPL_MASK;
  4014. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4015. save->s = 1;
  4016. }
  4017. vmx_set_segment(vcpu, save, seg);
  4018. }
  4019. static void enter_pmode(struct kvm_vcpu *vcpu)
  4020. {
  4021. unsigned long flags;
  4022. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4023. /*
  4024. * Update real mode segment cache. It may be not up-to-date if sement
  4025. * register was written while vcpu was in a guest mode.
  4026. */
  4027. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4028. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4029. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4030. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4031. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4032. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4033. vmx->rmode.vm86_active = 0;
  4034. vmx_segment_cache_clear(vmx);
  4035. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4036. flags = vmcs_readl(GUEST_RFLAGS);
  4037. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4038. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4039. vmcs_writel(GUEST_RFLAGS, flags);
  4040. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4041. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4042. update_exception_bitmap(vcpu);
  4043. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4044. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4045. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4046. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4047. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4048. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4049. }
  4050. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4051. {
  4052. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4053. struct kvm_segment var = *save;
  4054. var.dpl = 0x3;
  4055. if (seg == VCPU_SREG_CS)
  4056. var.type = 0x3;
  4057. if (!emulate_invalid_guest_state) {
  4058. var.selector = var.base >> 4;
  4059. var.base = var.base & 0xffff0;
  4060. var.limit = 0xffff;
  4061. var.g = 0;
  4062. var.db = 0;
  4063. var.present = 1;
  4064. var.s = 1;
  4065. var.l = 0;
  4066. var.unusable = 0;
  4067. var.type = 0x3;
  4068. var.avl = 0;
  4069. if (save->base & 0xf)
  4070. printk_once(KERN_WARNING "kvm: segment base is not "
  4071. "paragraph aligned when entering "
  4072. "protected mode (seg=%d)", seg);
  4073. }
  4074. vmcs_write16(sf->selector, var.selector);
  4075. vmcs_writel(sf->base, var.base);
  4076. vmcs_write32(sf->limit, var.limit);
  4077. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4078. }
  4079. static void enter_rmode(struct kvm_vcpu *vcpu)
  4080. {
  4081. unsigned long flags;
  4082. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4083. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4084. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4085. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4086. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4087. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4088. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4089. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4090. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4091. vmx->rmode.vm86_active = 1;
  4092. /*
  4093. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4094. * vcpu. Warn the user that an update is overdue.
  4095. */
  4096. if (!kvm_vmx->tss_addr)
  4097. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4098. "called before entering vcpu\n");
  4099. vmx_segment_cache_clear(vmx);
  4100. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4101. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4102. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4103. flags = vmcs_readl(GUEST_RFLAGS);
  4104. vmx->rmode.save_rflags = flags;
  4105. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4106. vmcs_writel(GUEST_RFLAGS, flags);
  4107. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4108. update_exception_bitmap(vcpu);
  4109. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4110. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4111. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4112. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4113. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4114. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4115. kvm_mmu_reset_context(vcpu);
  4116. }
  4117. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4118. {
  4119. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4120. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4121. if (!msr)
  4122. return;
  4123. /*
  4124. * Force kernel_gs_base reloading before EFER changes, as control
  4125. * of this msr depends on is_long_mode().
  4126. */
  4127. vmx_load_host_state(to_vmx(vcpu));
  4128. vcpu->arch.efer = efer;
  4129. if (efer & EFER_LMA) {
  4130. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4131. msr->data = efer;
  4132. } else {
  4133. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4134. msr->data = efer & ~EFER_LME;
  4135. }
  4136. setup_msrs(vmx);
  4137. }
  4138. #ifdef CONFIG_X86_64
  4139. static void enter_lmode(struct kvm_vcpu *vcpu)
  4140. {
  4141. u32 guest_tr_ar;
  4142. vmx_segment_cache_clear(to_vmx(vcpu));
  4143. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4144. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4145. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4146. __func__);
  4147. vmcs_write32(GUEST_TR_AR_BYTES,
  4148. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4149. | VMX_AR_TYPE_BUSY_64_TSS);
  4150. }
  4151. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4152. }
  4153. static void exit_lmode(struct kvm_vcpu *vcpu)
  4154. {
  4155. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4156. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4157. }
  4158. #endif
  4159. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4160. bool invalidate_gpa)
  4161. {
  4162. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4163. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4164. return;
  4165. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4166. } else {
  4167. vpid_sync_context(vpid);
  4168. }
  4169. }
  4170. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4171. {
  4172. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4173. }
  4174. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4175. {
  4176. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4177. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4178. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4179. }
  4180. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4181. {
  4182. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4183. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4184. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4185. }
  4186. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4187. {
  4188. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4189. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4190. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4191. }
  4192. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4193. {
  4194. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4195. if (!test_bit(VCPU_EXREG_PDPTR,
  4196. (unsigned long *)&vcpu->arch.regs_dirty))
  4197. return;
  4198. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4199. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4200. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4201. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4202. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4203. }
  4204. }
  4205. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4206. {
  4207. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4208. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4209. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4210. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4211. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4212. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4213. }
  4214. __set_bit(VCPU_EXREG_PDPTR,
  4215. (unsigned long *)&vcpu->arch.regs_avail);
  4216. __set_bit(VCPU_EXREG_PDPTR,
  4217. (unsigned long *)&vcpu->arch.regs_dirty);
  4218. }
  4219. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4220. {
  4221. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4222. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4223. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4224. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4225. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4226. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4227. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4228. return fixed_bits_valid(val, fixed0, fixed1);
  4229. }
  4230. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4231. {
  4232. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4233. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4234. return fixed_bits_valid(val, fixed0, fixed1);
  4235. }
  4236. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4237. {
  4238. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4239. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4240. return fixed_bits_valid(val, fixed0, fixed1);
  4241. }
  4242. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4243. #define nested_guest_cr4_valid nested_cr4_valid
  4244. #define nested_host_cr4_valid nested_cr4_valid
  4245. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4246. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4247. unsigned long cr0,
  4248. struct kvm_vcpu *vcpu)
  4249. {
  4250. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4251. vmx_decache_cr3(vcpu);
  4252. if (!(cr0 & X86_CR0_PG)) {
  4253. /* From paging/starting to nonpaging */
  4254. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4255. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4256. (CPU_BASED_CR3_LOAD_EXITING |
  4257. CPU_BASED_CR3_STORE_EXITING));
  4258. vcpu->arch.cr0 = cr0;
  4259. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4260. } else if (!is_paging(vcpu)) {
  4261. /* From nonpaging to paging */
  4262. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4263. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4264. ~(CPU_BASED_CR3_LOAD_EXITING |
  4265. CPU_BASED_CR3_STORE_EXITING));
  4266. vcpu->arch.cr0 = cr0;
  4267. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4268. }
  4269. if (!(cr0 & X86_CR0_WP))
  4270. *hw_cr0 &= ~X86_CR0_WP;
  4271. }
  4272. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4273. {
  4274. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4275. unsigned long hw_cr0;
  4276. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4277. if (enable_unrestricted_guest)
  4278. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4279. else {
  4280. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4281. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4282. enter_pmode(vcpu);
  4283. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4284. enter_rmode(vcpu);
  4285. }
  4286. #ifdef CONFIG_X86_64
  4287. if (vcpu->arch.efer & EFER_LME) {
  4288. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4289. enter_lmode(vcpu);
  4290. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4291. exit_lmode(vcpu);
  4292. }
  4293. #endif
  4294. if (enable_ept && !enable_unrestricted_guest)
  4295. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4296. vmcs_writel(CR0_READ_SHADOW, cr0);
  4297. vmcs_writel(GUEST_CR0, hw_cr0);
  4298. vcpu->arch.cr0 = cr0;
  4299. /* depends on vcpu->arch.cr0 to be set to a new value */
  4300. vmx->emulation_required = emulation_required(vcpu);
  4301. }
  4302. static int get_ept_level(struct kvm_vcpu *vcpu)
  4303. {
  4304. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4305. return 5;
  4306. return 4;
  4307. }
  4308. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4309. {
  4310. u64 eptp = VMX_EPTP_MT_WB;
  4311. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4312. if (enable_ept_ad_bits &&
  4313. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4314. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4315. eptp |= (root_hpa & PAGE_MASK);
  4316. return eptp;
  4317. }
  4318. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4319. {
  4320. unsigned long guest_cr3;
  4321. u64 eptp;
  4322. guest_cr3 = cr3;
  4323. if (enable_ept) {
  4324. eptp = construct_eptp(vcpu, cr3);
  4325. vmcs_write64(EPT_POINTER, eptp);
  4326. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4327. is_guest_mode(vcpu))
  4328. guest_cr3 = kvm_read_cr3(vcpu);
  4329. else
  4330. guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
  4331. ept_load_pdptrs(vcpu);
  4332. }
  4333. vmx_flush_tlb(vcpu, true);
  4334. vmcs_writel(GUEST_CR3, guest_cr3);
  4335. }
  4336. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4337. {
  4338. /*
  4339. * Pass through host's Machine Check Enable value to hw_cr4, which
  4340. * is in force while we are in guest mode. Do not let guests control
  4341. * this bit, even if host CR4.MCE == 0.
  4342. */
  4343. unsigned long hw_cr4;
  4344. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4345. if (enable_unrestricted_guest)
  4346. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4347. else if (to_vmx(vcpu)->rmode.vm86_active)
  4348. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4349. else
  4350. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4351. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4352. if (cr4 & X86_CR4_UMIP) {
  4353. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4354. SECONDARY_EXEC_DESC);
  4355. hw_cr4 &= ~X86_CR4_UMIP;
  4356. } else if (!is_guest_mode(vcpu) ||
  4357. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4358. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4359. SECONDARY_EXEC_DESC);
  4360. }
  4361. if (cr4 & X86_CR4_VMXE) {
  4362. /*
  4363. * To use VMXON (and later other VMX instructions), a guest
  4364. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4365. * So basically the check on whether to allow nested VMX
  4366. * is here.
  4367. */
  4368. if (!nested_vmx_allowed(vcpu))
  4369. return 1;
  4370. }
  4371. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4372. return 1;
  4373. vcpu->arch.cr4 = cr4;
  4374. if (!enable_unrestricted_guest) {
  4375. if (enable_ept) {
  4376. if (!is_paging(vcpu)) {
  4377. hw_cr4 &= ~X86_CR4_PAE;
  4378. hw_cr4 |= X86_CR4_PSE;
  4379. } else if (!(cr4 & X86_CR4_PAE)) {
  4380. hw_cr4 &= ~X86_CR4_PAE;
  4381. }
  4382. }
  4383. /*
  4384. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4385. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4386. * to be manually disabled when guest switches to non-paging
  4387. * mode.
  4388. *
  4389. * If !enable_unrestricted_guest, the CPU is always running
  4390. * with CR0.PG=1 and CR4 needs to be modified.
  4391. * If enable_unrestricted_guest, the CPU automatically
  4392. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4393. */
  4394. if (!is_paging(vcpu))
  4395. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4396. }
  4397. vmcs_writel(CR4_READ_SHADOW, cr4);
  4398. vmcs_writel(GUEST_CR4, hw_cr4);
  4399. return 0;
  4400. }
  4401. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4402. struct kvm_segment *var, int seg)
  4403. {
  4404. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4405. u32 ar;
  4406. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4407. *var = vmx->rmode.segs[seg];
  4408. if (seg == VCPU_SREG_TR
  4409. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4410. return;
  4411. var->base = vmx_read_guest_seg_base(vmx, seg);
  4412. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4413. return;
  4414. }
  4415. var->base = vmx_read_guest_seg_base(vmx, seg);
  4416. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4417. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4418. ar = vmx_read_guest_seg_ar(vmx, seg);
  4419. var->unusable = (ar >> 16) & 1;
  4420. var->type = ar & 15;
  4421. var->s = (ar >> 4) & 1;
  4422. var->dpl = (ar >> 5) & 3;
  4423. /*
  4424. * Some userspaces do not preserve unusable property. Since usable
  4425. * segment has to be present according to VMX spec we can use present
  4426. * property to amend userspace bug by making unusable segment always
  4427. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4428. * segment as unusable.
  4429. */
  4430. var->present = !var->unusable;
  4431. var->avl = (ar >> 12) & 1;
  4432. var->l = (ar >> 13) & 1;
  4433. var->db = (ar >> 14) & 1;
  4434. var->g = (ar >> 15) & 1;
  4435. }
  4436. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4437. {
  4438. struct kvm_segment s;
  4439. if (to_vmx(vcpu)->rmode.vm86_active) {
  4440. vmx_get_segment(vcpu, &s, seg);
  4441. return s.base;
  4442. }
  4443. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4444. }
  4445. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4446. {
  4447. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4448. if (unlikely(vmx->rmode.vm86_active))
  4449. return 0;
  4450. else {
  4451. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4452. return VMX_AR_DPL(ar);
  4453. }
  4454. }
  4455. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4456. {
  4457. u32 ar;
  4458. if (var->unusable || !var->present)
  4459. ar = 1 << 16;
  4460. else {
  4461. ar = var->type & 15;
  4462. ar |= (var->s & 1) << 4;
  4463. ar |= (var->dpl & 3) << 5;
  4464. ar |= (var->present & 1) << 7;
  4465. ar |= (var->avl & 1) << 12;
  4466. ar |= (var->l & 1) << 13;
  4467. ar |= (var->db & 1) << 14;
  4468. ar |= (var->g & 1) << 15;
  4469. }
  4470. return ar;
  4471. }
  4472. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4473. struct kvm_segment *var, int seg)
  4474. {
  4475. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4476. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4477. vmx_segment_cache_clear(vmx);
  4478. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4479. vmx->rmode.segs[seg] = *var;
  4480. if (seg == VCPU_SREG_TR)
  4481. vmcs_write16(sf->selector, var->selector);
  4482. else if (var->s)
  4483. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4484. goto out;
  4485. }
  4486. vmcs_writel(sf->base, var->base);
  4487. vmcs_write32(sf->limit, var->limit);
  4488. vmcs_write16(sf->selector, var->selector);
  4489. /*
  4490. * Fix the "Accessed" bit in AR field of segment registers for older
  4491. * qemu binaries.
  4492. * IA32 arch specifies that at the time of processor reset the
  4493. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4494. * is setting it to 0 in the userland code. This causes invalid guest
  4495. * state vmexit when "unrestricted guest" mode is turned on.
  4496. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4497. * tree. Newer qemu binaries with that qemu fix would not need this
  4498. * kvm hack.
  4499. */
  4500. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4501. var->type |= 0x1; /* Accessed */
  4502. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4503. out:
  4504. vmx->emulation_required = emulation_required(vcpu);
  4505. }
  4506. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4507. {
  4508. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4509. *db = (ar >> 14) & 1;
  4510. *l = (ar >> 13) & 1;
  4511. }
  4512. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4513. {
  4514. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4515. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4516. }
  4517. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4518. {
  4519. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4520. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4521. }
  4522. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4523. {
  4524. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4525. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4526. }
  4527. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4528. {
  4529. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4530. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4531. }
  4532. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4533. {
  4534. struct kvm_segment var;
  4535. u32 ar;
  4536. vmx_get_segment(vcpu, &var, seg);
  4537. var.dpl = 0x3;
  4538. if (seg == VCPU_SREG_CS)
  4539. var.type = 0x3;
  4540. ar = vmx_segment_access_rights(&var);
  4541. if (var.base != (var.selector << 4))
  4542. return false;
  4543. if (var.limit != 0xffff)
  4544. return false;
  4545. if (ar != 0xf3)
  4546. return false;
  4547. return true;
  4548. }
  4549. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4550. {
  4551. struct kvm_segment cs;
  4552. unsigned int cs_rpl;
  4553. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4554. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4555. if (cs.unusable)
  4556. return false;
  4557. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4558. return false;
  4559. if (!cs.s)
  4560. return false;
  4561. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4562. if (cs.dpl > cs_rpl)
  4563. return false;
  4564. } else {
  4565. if (cs.dpl != cs_rpl)
  4566. return false;
  4567. }
  4568. if (!cs.present)
  4569. return false;
  4570. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4571. return true;
  4572. }
  4573. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4574. {
  4575. struct kvm_segment ss;
  4576. unsigned int ss_rpl;
  4577. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4578. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4579. if (ss.unusable)
  4580. return true;
  4581. if (ss.type != 3 && ss.type != 7)
  4582. return false;
  4583. if (!ss.s)
  4584. return false;
  4585. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4586. return false;
  4587. if (!ss.present)
  4588. return false;
  4589. return true;
  4590. }
  4591. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4592. {
  4593. struct kvm_segment var;
  4594. unsigned int rpl;
  4595. vmx_get_segment(vcpu, &var, seg);
  4596. rpl = var.selector & SEGMENT_RPL_MASK;
  4597. if (var.unusable)
  4598. return true;
  4599. if (!var.s)
  4600. return false;
  4601. if (!var.present)
  4602. return false;
  4603. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4604. if (var.dpl < rpl) /* DPL < RPL */
  4605. return false;
  4606. }
  4607. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4608. * rights flags
  4609. */
  4610. return true;
  4611. }
  4612. static bool tr_valid(struct kvm_vcpu *vcpu)
  4613. {
  4614. struct kvm_segment tr;
  4615. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4616. if (tr.unusable)
  4617. return false;
  4618. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4619. return false;
  4620. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4621. return false;
  4622. if (!tr.present)
  4623. return false;
  4624. return true;
  4625. }
  4626. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4627. {
  4628. struct kvm_segment ldtr;
  4629. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4630. if (ldtr.unusable)
  4631. return true;
  4632. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4633. return false;
  4634. if (ldtr.type != 2)
  4635. return false;
  4636. if (!ldtr.present)
  4637. return false;
  4638. return true;
  4639. }
  4640. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4641. {
  4642. struct kvm_segment cs, ss;
  4643. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4644. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4645. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4646. (ss.selector & SEGMENT_RPL_MASK));
  4647. }
  4648. /*
  4649. * Check if guest state is valid. Returns true if valid, false if
  4650. * not.
  4651. * We assume that registers are always usable
  4652. */
  4653. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4654. {
  4655. if (enable_unrestricted_guest)
  4656. return true;
  4657. /* real mode guest state checks */
  4658. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4659. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4660. return false;
  4661. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4662. return false;
  4663. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4664. return false;
  4665. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4666. return false;
  4667. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4668. return false;
  4669. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4670. return false;
  4671. } else {
  4672. /* protected mode guest state checks */
  4673. if (!cs_ss_rpl_check(vcpu))
  4674. return false;
  4675. if (!code_segment_valid(vcpu))
  4676. return false;
  4677. if (!stack_segment_valid(vcpu))
  4678. return false;
  4679. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4680. return false;
  4681. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4682. return false;
  4683. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4684. return false;
  4685. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4686. return false;
  4687. if (!tr_valid(vcpu))
  4688. return false;
  4689. if (!ldtr_valid(vcpu))
  4690. return false;
  4691. }
  4692. /* TODO:
  4693. * - Add checks on RIP
  4694. * - Add checks on RFLAGS
  4695. */
  4696. return true;
  4697. }
  4698. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4699. {
  4700. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4701. }
  4702. static int init_rmode_tss(struct kvm *kvm)
  4703. {
  4704. gfn_t fn;
  4705. u16 data = 0;
  4706. int idx, r;
  4707. idx = srcu_read_lock(&kvm->srcu);
  4708. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  4709. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4710. if (r < 0)
  4711. goto out;
  4712. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4713. r = kvm_write_guest_page(kvm, fn++, &data,
  4714. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4715. if (r < 0)
  4716. goto out;
  4717. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4718. if (r < 0)
  4719. goto out;
  4720. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4721. if (r < 0)
  4722. goto out;
  4723. data = ~0;
  4724. r = kvm_write_guest_page(kvm, fn, &data,
  4725. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4726. sizeof(u8));
  4727. out:
  4728. srcu_read_unlock(&kvm->srcu, idx);
  4729. return r;
  4730. }
  4731. static int init_rmode_identity_map(struct kvm *kvm)
  4732. {
  4733. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  4734. int i, idx, r = 0;
  4735. kvm_pfn_t identity_map_pfn;
  4736. u32 tmp;
  4737. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  4738. mutex_lock(&kvm->slots_lock);
  4739. if (likely(kvm_vmx->ept_identity_pagetable_done))
  4740. goto out2;
  4741. if (!kvm_vmx->ept_identity_map_addr)
  4742. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4743. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  4744. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  4745. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  4746. if (r < 0)
  4747. goto out2;
  4748. idx = srcu_read_lock(&kvm->srcu);
  4749. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  4750. if (r < 0)
  4751. goto out;
  4752. /* Set up identity-mapping pagetable for EPT in real mode */
  4753. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  4754. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  4755. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  4756. r = kvm_write_guest_page(kvm, identity_map_pfn,
  4757. &tmp, i * sizeof(tmp), sizeof(tmp));
  4758. if (r < 0)
  4759. goto out;
  4760. }
  4761. kvm_vmx->ept_identity_pagetable_done = true;
  4762. out:
  4763. srcu_read_unlock(&kvm->srcu, idx);
  4764. out2:
  4765. mutex_unlock(&kvm->slots_lock);
  4766. return r;
  4767. }
  4768. static void seg_setup(int seg)
  4769. {
  4770. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4771. unsigned int ar;
  4772. vmcs_write16(sf->selector, 0);
  4773. vmcs_writel(sf->base, 0);
  4774. vmcs_write32(sf->limit, 0xffff);
  4775. ar = 0x93;
  4776. if (seg == VCPU_SREG_CS)
  4777. ar |= 0x08; /* code segment */
  4778. vmcs_write32(sf->ar_bytes, ar);
  4779. }
  4780. static int alloc_apic_access_page(struct kvm *kvm)
  4781. {
  4782. struct page *page;
  4783. int r = 0;
  4784. mutex_lock(&kvm->slots_lock);
  4785. if (kvm->arch.apic_access_page_done)
  4786. goto out;
  4787. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  4788. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  4789. if (r)
  4790. goto out;
  4791. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  4792. if (is_error_page(page)) {
  4793. r = -EFAULT;
  4794. goto out;
  4795. }
  4796. /*
  4797. * Do not pin the page in memory, so that memory hot-unplug
  4798. * is able to migrate it.
  4799. */
  4800. put_page(page);
  4801. kvm->arch.apic_access_page_done = true;
  4802. out:
  4803. mutex_unlock(&kvm->slots_lock);
  4804. return r;
  4805. }
  4806. static int allocate_vpid(void)
  4807. {
  4808. int vpid;
  4809. if (!enable_vpid)
  4810. return 0;
  4811. spin_lock(&vmx_vpid_lock);
  4812. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  4813. if (vpid < VMX_NR_VPIDS)
  4814. __set_bit(vpid, vmx_vpid_bitmap);
  4815. else
  4816. vpid = 0;
  4817. spin_unlock(&vmx_vpid_lock);
  4818. return vpid;
  4819. }
  4820. static void free_vpid(int vpid)
  4821. {
  4822. if (!enable_vpid || vpid == 0)
  4823. return;
  4824. spin_lock(&vmx_vpid_lock);
  4825. __clear_bit(vpid, vmx_vpid_bitmap);
  4826. spin_unlock(&vmx_vpid_lock);
  4827. }
  4828. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  4829. u32 msr, int type)
  4830. {
  4831. int f = sizeof(unsigned long);
  4832. if (!cpu_has_vmx_msr_bitmap())
  4833. return;
  4834. if (static_branch_unlikely(&enable_evmcs))
  4835. evmcs_touch_msr_bitmap();
  4836. /*
  4837. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4838. * have the write-low and read-high bitmap offsets the wrong way round.
  4839. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4840. */
  4841. if (msr <= 0x1fff) {
  4842. if (type & MSR_TYPE_R)
  4843. /* read-low */
  4844. __clear_bit(msr, msr_bitmap + 0x000 / f);
  4845. if (type & MSR_TYPE_W)
  4846. /* write-low */
  4847. __clear_bit(msr, msr_bitmap + 0x800 / f);
  4848. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4849. msr &= 0x1fff;
  4850. if (type & MSR_TYPE_R)
  4851. /* read-high */
  4852. __clear_bit(msr, msr_bitmap + 0x400 / f);
  4853. if (type & MSR_TYPE_W)
  4854. /* write-high */
  4855. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  4856. }
  4857. }
  4858. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  4859. u32 msr, int type)
  4860. {
  4861. int f = sizeof(unsigned long);
  4862. if (!cpu_has_vmx_msr_bitmap())
  4863. return;
  4864. if (static_branch_unlikely(&enable_evmcs))
  4865. evmcs_touch_msr_bitmap();
  4866. /*
  4867. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4868. * have the write-low and read-high bitmap offsets the wrong way round.
  4869. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4870. */
  4871. if (msr <= 0x1fff) {
  4872. if (type & MSR_TYPE_R)
  4873. /* read-low */
  4874. __set_bit(msr, msr_bitmap + 0x000 / f);
  4875. if (type & MSR_TYPE_W)
  4876. /* write-low */
  4877. __set_bit(msr, msr_bitmap + 0x800 / f);
  4878. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4879. msr &= 0x1fff;
  4880. if (type & MSR_TYPE_R)
  4881. /* read-high */
  4882. __set_bit(msr, msr_bitmap + 0x400 / f);
  4883. if (type & MSR_TYPE_W)
  4884. /* write-high */
  4885. __set_bit(msr, msr_bitmap + 0xc00 / f);
  4886. }
  4887. }
  4888. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  4889. u32 msr, int type, bool value)
  4890. {
  4891. if (value)
  4892. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  4893. else
  4894. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  4895. }
  4896. /*
  4897. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  4898. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  4899. */
  4900. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  4901. unsigned long *msr_bitmap_nested,
  4902. u32 msr, int type)
  4903. {
  4904. int f = sizeof(unsigned long);
  4905. /*
  4906. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4907. * have the write-low and read-high bitmap offsets the wrong way round.
  4908. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4909. */
  4910. if (msr <= 0x1fff) {
  4911. if (type & MSR_TYPE_R &&
  4912. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  4913. /* read-low */
  4914. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  4915. if (type & MSR_TYPE_W &&
  4916. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  4917. /* write-low */
  4918. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  4919. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4920. msr &= 0x1fff;
  4921. if (type & MSR_TYPE_R &&
  4922. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  4923. /* read-high */
  4924. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  4925. if (type & MSR_TYPE_W &&
  4926. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  4927. /* write-high */
  4928. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  4929. }
  4930. }
  4931. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  4932. {
  4933. u8 mode = 0;
  4934. if (cpu_has_secondary_exec_ctrls() &&
  4935. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  4936. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  4937. mode |= MSR_BITMAP_MODE_X2APIC;
  4938. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  4939. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  4940. }
  4941. if (is_long_mode(vcpu))
  4942. mode |= MSR_BITMAP_MODE_LM;
  4943. return mode;
  4944. }
  4945. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  4946. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  4947. u8 mode)
  4948. {
  4949. int msr;
  4950. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  4951. unsigned word = msr / BITS_PER_LONG;
  4952. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  4953. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  4954. }
  4955. if (mode & MSR_BITMAP_MODE_X2APIC) {
  4956. /*
  4957. * TPR reads and writes can be virtualized even if virtual interrupt
  4958. * delivery is not in use.
  4959. */
  4960. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  4961. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  4962. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  4963. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  4964. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  4965. }
  4966. }
  4967. }
  4968. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  4969. {
  4970. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4971. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  4972. u8 mode = vmx_msr_bitmap_mode(vcpu);
  4973. u8 changed = mode ^ vmx->msr_bitmap_mode;
  4974. if (!changed)
  4975. return;
  4976. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  4977. !(mode & MSR_BITMAP_MODE_LM));
  4978. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  4979. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  4980. vmx->msr_bitmap_mode = mode;
  4981. }
  4982. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  4983. {
  4984. return enable_apicv;
  4985. }
  4986. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  4987. {
  4988. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4989. gfn_t gfn;
  4990. /*
  4991. * Don't need to mark the APIC access page dirty; it is never
  4992. * written to by the CPU during APIC virtualization.
  4993. */
  4994. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  4995. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  4996. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4997. }
  4998. if (nested_cpu_has_posted_intr(vmcs12)) {
  4999. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5000. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5001. }
  5002. }
  5003. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5004. {
  5005. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5006. int max_irr;
  5007. void *vapic_page;
  5008. u16 status;
  5009. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5010. return;
  5011. vmx->nested.pi_pending = false;
  5012. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5013. return;
  5014. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5015. if (max_irr != 256) {
  5016. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5017. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5018. vapic_page, &max_irr);
  5019. kunmap(vmx->nested.virtual_apic_page);
  5020. status = vmcs_read16(GUEST_INTR_STATUS);
  5021. if ((u8)max_irr > ((u8)status & 0xff)) {
  5022. status &= ~0xff;
  5023. status |= (u8)max_irr;
  5024. vmcs_write16(GUEST_INTR_STATUS, status);
  5025. }
  5026. }
  5027. nested_mark_vmcs12_pages_dirty(vcpu);
  5028. }
  5029. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5030. bool nested)
  5031. {
  5032. #ifdef CONFIG_SMP
  5033. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5034. if (vcpu->mode == IN_GUEST_MODE) {
  5035. /*
  5036. * The vector of interrupt to be delivered to vcpu had
  5037. * been set in PIR before this function.
  5038. *
  5039. * Following cases will be reached in this block, and
  5040. * we always send a notification event in all cases as
  5041. * explained below.
  5042. *
  5043. * Case 1: vcpu keeps in non-root mode. Sending a
  5044. * notification event posts the interrupt to vcpu.
  5045. *
  5046. * Case 2: vcpu exits to root mode and is still
  5047. * runnable. PIR will be synced to vIRR before the
  5048. * next vcpu entry. Sending a notification event in
  5049. * this case has no effect, as vcpu is not in root
  5050. * mode.
  5051. *
  5052. * Case 3: vcpu exits to root mode and is blocked.
  5053. * vcpu_block() has already synced PIR to vIRR and
  5054. * never blocks vcpu if vIRR is not cleared. Therefore,
  5055. * a blocked vcpu here does not wait for any requested
  5056. * interrupts in PIR, and sending a notification event
  5057. * which has no effect is safe here.
  5058. */
  5059. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5060. return true;
  5061. }
  5062. #endif
  5063. return false;
  5064. }
  5065. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5066. int vector)
  5067. {
  5068. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5069. if (is_guest_mode(vcpu) &&
  5070. vector == vmx->nested.posted_intr_nv) {
  5071. /*
  5072. * If a posted intr is not recognized by hardware,
  5073. * we will accomplish it in the next vmentry.
  5074. */
  5075. vmx->nested.pi_pending = true;
  5076. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5077. /* the PIR and ON have been set by L1. */
  5078. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5079. kvm_vcpu_kick(vcpu);
  5080. return 0;
  5081. }
  5082. return -1;
  5083. }
  5084. /*
  5085. * Send interrupt to vcpu via posted interrupt way.
  5086. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5087. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5088. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5089. * interrupt from PIR in next vmentry.
  5090. */
  5091. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5092. {
  5093. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5094. int r;
  5095. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5096. if (!r)
  5097. return;
  5098. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5099. return;
  5100. /* If a previous notification has sent the IPI, nothing to do. */
  5101. if (pi_test_and_set_on(&vmx->pi_desc))
  5102. return;
  5103. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5104. kvm_vcpu_kick(vcpu);
  5105. }
  5106. /*
  5107. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5108. * will not change in the lifetime of the guest.
  5109. * Note that host-state that does change is set elsewhere. E.g., host-state
  5110. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5111. */
  5112. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5113. {
  5114. u32 low32, high32;
  5115. unsigned long tmpl;
  5116. struct desc_ptr dt;
  5117. unsigned long cr0, cr3, cr4;
  5118. cr0 = read_cr0();
  5119. WARN_ON(cr0 & X86_CR0_TS);
  5120. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5121. /*
  5122. * Save the most likely value for this task's CR3 in the VMCS.
  5123. * We can't use __get_current_cr3_fast() because we're not atomic.
  5124. */
  5125. cr3 = __read_cr3();
  5126. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5127. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  5128. /* Save the most likely value for this task's CR4 in the VMCS. */
  5129. cr4 = cr4_read_shadow();
  5130. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5131. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  5132. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5133. #ifdef CONFIG_X86_64
  5134. /*
  5135. * Load null selectors, so we can avoid reloading them in
  5136. * __vmx_load_host_state(), in case userspace uses the null selectors
  5137. * too (the expected case).
  5138. */
  5139. vmcs_write16(HOST_DS_SELECTOR, 0);
  5140. vmcs_write16(HOST_ES_SELECTOR, 0);
  5141. #else
  5142. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5143. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5144. #endif
  5145. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5146. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5147. store_idt(&dt);
  5148. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5149. vmx->host_idt_base = dt.address;
  5150. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5151. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5152. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5153. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5154. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5155. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5156. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5157. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5158. }
  5159. }
  5160. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5161. {
  5162. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5163. if (enable_ept)
  5164. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5165. if (is_guest_mode(&vmx->vcpu))
  5166. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5167. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5168. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5169. }
  5170. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5171. {
  5172. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5173. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5174. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5175. if (!enable_vnmi)
  5176. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5177. /* Enable the preemption timer dynamically */
  5178. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5179. return pin_based_exec_ctrl;
  5180. }
  5181. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5182. {
  5183. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5184. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5185. if (cpu_has_secondary_exec_ctrls()) {
  5186. if (kvm_vcpu_apicv_active(vcpu))
  5187. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5188. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5189. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5190. else
  5191. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5192. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5193. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5194. }
  5195. if (cpu_has_vmx_msr_bitmap())
  5196. vmx_update_msr_bitmap(vcpu);
  5197. }
  5198. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5199. {
  5200. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5201. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5202. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5203. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5204. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5205. #ifdef CONFIG_X86_64
  5206. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5207. CPU_BASED_CR8_LOAD_EXITING;
  5208. #endif
  5209. }
  5210. if (!enable_ept)
  5211. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5212. CPU_BASED_CR3_LOAD_EXITING |
  5213. CPU_BASED_INVLPG_EXITING;
  5214. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5215. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5216. CPU_BASED_MONITOR_EXITING);
  5217. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5218. exec_control &= ~CPU_BASED_HLT_EXITING;
  5219. return exec_control;
  5220. }
  5221. static bool vmx_rdrand_supported(void)
  5222. {
  5223. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5224. SECONDARY_EXEC_RDRAND_EXITING;
  5225. }
  5226. static bool vmx_rdseed_supported(void)
  5227. {
  5228. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5229. SECONDARY_EXEC_RDSEED_EXITING;
  5230. }
  5231. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5232. {
  5233. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5234. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5235. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5236. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5237. if (vmx->vpid == 0)
  5238. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5239. if (!enable_ept) {
  5240. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5241. enable_unrestricted_guest = 0;
  5242. /* Enable INVPCID for non-ept guests may cause performance regression. */
  5243. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5244. }
  5245. if (!enable_unrestricted_guest)
  5246. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5247. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5248. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5249. if (!kvm_vcpu_apicv_active(vcpu))
  5250. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5251. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5252. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5253. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5254. * in vmx_set_cr4. */
  5255. exec_control &= ~SECONDARY_EXEC_DESC;
  5256. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5257. (handle_vmptrld).
  5258. We can NOT enable shadow_vmcs here because we don't have yet
  5259. a current VMCS12
  5260. */
  5261. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5262. if (!enable_pml)
  5263. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5264. if (vmx_xsaves_supported()) {
  5265. /* Exposing XSAVES only when XSAVE is exposed */
  5266. bool xsaves_enabled =
  5267. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5268. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5269. if (!xsaves_enabled)
  5270. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5271. if (nested) {
  5272. if (xsaves_enabled)
  5273. vmx->nested.msrs.secondary_ctls_high |=
  5274. SECONDARY_EXEC_XSAVES;
  5275. else
  5276. vmx->nested.msrs.secondary_ctls_high &=
  5277. ~SECONDARY_EXEC_XSAVES;
  5278. }
  5279. }
  5280. if (vmx_rdtscp_supported()) {
  5281. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5282. if (!rdtscp_enabled)
  5283. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5284. if (nested) {
  5285. if (rdtscp_enabled)
  5286. vmx->nested.msrs.secondary_ctls_high |=
  5287. SECONDARY_EXEC_RDTSCP;
  5288. else
  5289. vmx->nested.msrs.secondary_ctls_high &=
  5290. ~SECONDARY_EXEC_RDTSCP;
  5291. }
  5292. }
  5293. if (vmx_invpcid_supported()) {
  5294. /* Exposing INVPCID only when PCID is exposed */
  5295. bool invpcid_enabled =
  5296. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5297. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5298. if (!invpcid_enabled) {
  5299. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5300. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5301. }
  5302. if (nested) {
  5303. if (invpcid_enabled)
  5304. vmx->nested.msrs.secondary_ctls_high |=
  5305. SECONDARY_EXEC_ENABLE_INVPCID;
  5306. else
  5307. vmx->nested.msrs.secondary_ctls_high &=
  5308. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5309. }
  5310. }
  5311. if (vmx_rdrand_supported()) {
  5312. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5313. if (rdrand_enabled)
  5314. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5315. if (nested) {
  5316. if (rdrand_enabled)
  5317. vmx->nested.msrs.secondary_ctls_high |=
  5318. SECONDARY_EXEC_RDRAND_EXITING;
  5319. else
  5320. vmx->nested.msrs.secondary_ctls_high &=
  5321. ~SECONDARY_EXEC_RDRAND_EXITING;
  5322. }
  5323. }
  5324. if (vmx_rdseed_supported()) {
  5325. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5326. if (rdseed_enabled)
  5327. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5328. if (nested) {
  5329. if (rdseed_enabled)
  5330. vmx->nested.msrs.secondary_ctls_high |=
  5331. SECONDARY_EXEC_RDSEED_EXITING;
  5332. else
  5333. vmx->nested.msrs.secondary_ctls_high &=
  5334. ~SECONDARY_EXEC_RDSEED_EXITING;
  5335. }
  5336. }
  5337. vmx->secondary_exec_control = exec_control;
  5338. }
  5339. static void ept_set_mmio_spte_mask(void)
  5340. {
  5341. /*
  5342. * EPT Misconfigurations can be generated if the value of bits 2:0
  5343. * of an EPT paging-structure entry is 110b (write/execute).
  5344. */
  5345. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5346. VMX_EPT_MISCONFIG_WX_VALUE);
  5347. }
  5348. #define VMX_XSS_EXIT_BITMAP 0
  5349. /*
  5350. * Sets up the vmcs for emulated real mode.
  5351. */
  5352. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5353. {
  5354. #ifdef CONFIG_X86_64
  5355. unsigned long a;
  5356. #endif
  5357. int i;
  5358. if (enable_shadow_vmcs) {
  5359. /*
  5360. * At vCPU creation, "VMWRITE to any supported field
  5361. * in the VMCS" is supported, so use the more
  5362. * permissive vmx_vmread_bitmap to specify both read
  5363. * and write permissions for the shadow VMCS.
  5364. */
  5365. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5366. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5367. }
  5368. if (cpu_has_vmx_msr_bitmap())
  5369. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5370. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5371. /* Control */
  5372. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5373. vmx->hv_deadline_tsc = -1;
  5374. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5375. if (cpu_has_secondary_exec_ctrls()) {
  5376. vmx_compute_secondary_exec_control(vmx);
  5377. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5378. vmx->secondary_exec_control);
  5379. }
  5380. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5381. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5382. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5383. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5384. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5385. vmcs_write16(GUEST_INTR_STATUS, 0);
  5386. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5387. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5388. }
  5389. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5390. vmcs_write32(PLE_GAP, ple_gap);
  5391. vmx->ple_window = ple_window;
  5392. vmx->ple_window_dirty = true;
  5393. }
  5394. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5395. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5396. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5397. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5398. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5399. vmx_set_constant_host_state(vmx);
  5400. #ifdef CONFIG_X86_64
  5401. rdmsrl(MSR_FS_BASE, a);
  5402. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  5403. rdmsrl(MSR_GS_BASE, a);
  5404. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  5405. #else
  5406. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5407. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5408. #endif
  5409. if (cpu_has_vmx_vmfunc())
  5410. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5411. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5412. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5413. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  5414. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5415. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  5416. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5417. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5418. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5419. u32 index = vmx_msr_index[i];
  5420. u32 data_low, data_high;
  5421. int j = vmx->nmsrs;
  5422. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5423. continue;
  5424. if (wrmsr_safe(index, data_low, data_high) < 0)
  5425. continue;
  5426. vmx->guest_msrs[j].index = i;
  5427. vmx->guest_msrs[j].data = 0;
  5428. vmx->guest_msrs[j].mask = -1ull;
  5429. ++vmx->nmsrs;
  5430. }
  5431. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  5432. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
  5433. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5434. /* 22.2.1, 20.8.1 */
  5435. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5436. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5437. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5438. set_cr4_guest_host_mask(vmx);
  5439. if (vmx_xsaves_supported())
  5440. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5441. if (enable_pml) {
  5442. ASSERT(vmx->pml_pg);
  5443. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5444. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5445. }
  5446. }
  5447. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5448. {
  5449. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5450. struct msr_data apic_base_msr;
  5451. u64 cr0;
  5452. vmx->rmode.vm86_active = 0;
  5453. vmx->spec_ctrl = 0;
  5454. vcpu->arch.microcode_version = 0x100000000ULL;
  5455. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5456. kvm_set_cr8(vcpu, 0);
  5457. if (!init_event) {
  5458. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5459. MSR_IA32_APICBASE_ENABLE;
  5460. if (kvm_vcpu_is_reset_bsp(vcpu))
  5461. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5462. apic_base_msr.host_initiated = true;
  5463. kvm_set_apic_base(vcpu, &apic_base_msr);
  5464. }
  5465. vmx_segment_cache_clear(vmx);
  5466. seg_setup(VCPU_SREG_CS);
  5467. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5468. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5469. seg_setup(VCPU_SREG_DS);
  5470. seg_setup(VCPU_SREG_ES);
  5471. seg_setup(VCPU_SREG_FS);
  5472. seg_setup(VCPU_SREG_GS);
  5473. seg_setup(VCPU_SREG_SS);
  5474. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5475. vmcs_writel(GUEST_TR_BASE, 0);
  5476. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5477. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5478. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5479. vmcs_writel(GUEST_LDTR_BASE, 0);
  5480. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5481. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5482. if (!init_event) {
  5483. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5484. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5485. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5486. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5487. }
  5488. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5489. kvm_rip_write(vcpu, 0xfff0);
  5490. vmcs_writel(GUEST_GDTR_BASE, 0);
  5491. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5492. vmcs_writel(GUEST_IDTR_BASE, 0);
  5493. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5494. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5495. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5496. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5497. if (kvm_mpx_supported())
  5498. vmcs_write64(GUEST_BNDCFGS, 0);
  5499. setup_msrs(vmx);
  5500. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5501. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5502. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5503. if (cpu_need_tpr_shadow(vcpu))
  5504. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5505. __pa(vcpu->arch.apic->regs));
  5506. vmcs_write32(TPR_THRESHOLD, 0);
  5507. }
  5508. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5509. if (vmx->vpid != 0)
  5510. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5511. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5512. vmx->vcpu.arch.cr0 = cr0;
  5513. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5514. vmx_set_cr4(vcpu, 0);
  5515. vmx_set_efer(vcpu, 0);
  5516. update_exception_bitmap(vcpu);
  5517. vpid_sync_context(vmx->vpid);
  5518. if (init_event)
  5519. vmx_clear_hlt(vcpu);
  5520. }
  5521. /*
  5522. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5523. * For most existing hypervisors, this will always return true.
  5524. */
  5525. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5526. {
  5527. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5528. PIN_BASED_EXT_INTR_MASK;
  5529. }
  5530. /*
  5531. * In nested virtualization, check if L1 has set
  5532. * VM_EXIT_ACK_INTR_ON_EXIT
  5533. */
  5534. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5535. {
  5536. return get_vmcs12(vcpu)->vm_exit_controls &
  5537. VM_EXIT_ACK_INTR_ON_EXIT;
  5538. }
  5539. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5540. {
  5541. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5542. }
  5543. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5544. {
  5545. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5546. CPU_BASED_VIRTUAL_INTR_PENDING);
  5547. }
  5548. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5549. {
  5550. if (!enable_vnmi ||
  5551. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5552. enable_irq_window(vcpu);
  5553. return;
  5554. }
  5555. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5556. CPU_BASED_VIRTUAL_NMI_PENDING);
  5557. }
  5558. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5559. {
  5560. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5561. uint32_t intr;
  5562. int irq = vcpu->arch.interrupt.nr;
  5563. trace_kvm_inj_virq(irq);
  5564. ++vcpu->stat.irq_injections;
  5565. if (vmx->rmode.vm86_active) {
  5566. int inc_eip = 0;
  5567. if (vcpu->arch.interrupt.soft)
  5568. inc_eip = vcpu->arch.event_exit_inst_len;
  5569. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5570. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5571. return;
  5572. }
  5573. intr = irq | INTR_INFO_VALID_MASK;
  5574. if (vcpu->arch.interrupt.soft) {
  5575. intr |= INTR_TYPE_SOFT_INTR;
  5576. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5577. vmx->vcpu.arch.event_exit_inst_len);
  5578. } else
  5579. intr |= INTR_TYPE_EXT_INTR;
  5580. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5581. vmx_clear_hlt(vcpu);
  5582. }
  5583. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5584. {
  5585. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5586. if (!enable_vnmi) {
  5587. /*
  5588. * Tracking the NMI-blocked state in software is built upon
  5589. * finding the next open IRQ window. This, in turn, depends on
  5590. * well-behaving guests: They have to keep IRQs disabled at
  5591. * least as long as the NMI handler runs. Otherwise we may
  5592. * cause NMI nesting, maybe breaking the guest. But as this is
  5593. * highly unlikely, we can live with the residual risk.
  5594. */
  5595. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5596. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5597. }
  5598. ++vcpu->stat.nmi_injections;
  5599. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5600. if (vmx->rmode.vm86_active) {
  5601. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5602. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5603. return;
  5604. }
  5605. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5606. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5607. vmx_clear_hlt(vcpu);
  5608. }
  5609. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5610. {
  5611. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5612. bool masked;
  5613. if (!enable_vnmi)
  5614. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5615. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5616. return false;
  5617. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5618. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5619. return masked;
  5620. }
  5621. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5622. {
  5623. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5624. if (!enable_vnmi) {
  5625. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5626. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5627. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5628. }
  5629. } else {
  5630. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5631. if (masked)
  5632. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5633. GUEST_INTR_STATE_NMI);
  5634. else
  5635. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5636. GUEST_INTR_STATE_NMI);
  5637. }
  5638. }
  5639. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5640. {
  5641. if (to_vmx(vcpu)->nested.nested_run_pending)
  5642. return 0;
  5643. if (!enable_vnmi &&
  5644. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5645. return 0;
  5646. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5647. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5648. | GUEST_INTR_STATE_NMI));
  5649. }
  5650. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5651. {
  5652. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5653. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5654. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5655. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5656. }
  5657. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5658. {
  5659. int ret;
  5660. if (enable_unrestricted_guest)
  5661. return 0;
  5662. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5663. PAGE_SIZE * 3);
  5664. if (ret)
  5665. return ret;
  5666. to_kvm_vmx(kvm)->tss_addr = addr;
  5667. return init_rmode_tss(kvm);
  5668. }
  5669. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5670. {
  5671. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5672. return 0;
  5673. }
  5674. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5675. {
  5676. switch (vec) {
  5677. case BP_VECTOR:
  5678. /*
  5679. * Update instruction length as we may reinject the exception
  5680. * from user space while in guest debugging mode.
  5681. */
  5682. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5683. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5684. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5685. return false;
  5686. /* fall through */
  5687. case DB_VECTOR:
  5688. if (vcpu->guest_debug &
  5689. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5690. return false;
  5691. /* fall through */
  5692. case DE_VECTOR:
  5693. case OF_VECTOR:
  5694. case BR_VECTOR:
  5695. case UD_VECTOR:
  5696. case DF_VECTOR:
  5697. case SS_VECTOR:
  5698. case GP_VECTOR:
  5699. case MF_VECTOR:
  5700. return true;
  5701. break;
  5702. }
  5703. return false;
  5704. }
  5705. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5706. int vec, u32 err_code)
  5707. {
  5708. /*
  5709. * Instruction with address size override prefix opcode 0x67
  5710. * Cause the #SS fault with 0 error code in VM86 mode.
  5711. */
  5712. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5713. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5714. if (vcpu->arch.halt_request) {
  5715. vcpu->arch.halt_request = 0;
  5716. return kvm_vcpu_halt(vcpu);
  5717. }
  5718. return 1;
  5719. }
  5720. return 0;
  5721. }
  5722. /*
  5723. * Forward all other exceptions that are valid in real mode.
  5724. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5725. * the required debugging infrastructure rework.
  5726. */
  5727. kvm_queue_exception(vcpu, vec);
  5728. return 1;
  5729. }
  5730. /*
  5731. * Trigger machine check on the host. We assume all the MSRs are already set up
  5732. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  5733. * We pass a fake environment to the machine check handler because we want
  5734. * the guest to be always treated like user space, no matter what context
  5735. * it used internally.
  5736. */
  5737. static void kvm_machine_check(void)
  5738. {
  5739. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  5740. struct pt_regs regs = {
  5741. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  5742. .flags = X86_EFLAGS_IF,
  5743. };
  5744. do_machine_check(&regs, 0);
  5745. #endif
  5746. }
  5747. static int handle_machine_check(struct kvm_vcpu *vcpu)
  5748. {
  5749. /* already handled by vcpu_run */
  5750. return 1;
  5751. }
  5752. static int handle_exception(struct kvm_vcpu *vcpu)
  5753. {
  5754. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5755. struct kvm_run *kvm_run = vcpu->run;
  5756. u32 intr_info, ex_no, error_code;
  5757. unsigned long cr2, rip, dr6;
  5758. u32 vect_info;
  5759. enum emulation_result er;
  5760. vect_info = vmx->idt_vectoring_info;
  5761. intr_info = vmx->exit_intr_info;
  5762. if (is_machine_check(intr_info))
  5763. return handle_machine_check(vcpu);
  5764. if (is_nmi(intr_info))
  5765. return 1; /* already handled by vmx_vcpu_run() */
  5766. if (is_invalid_opcode(intr_info))
  5767. return handle_ud(vcpu);
  5768. error_code = 0;
  5769. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  5770. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  5771. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  5772. WARN_ON_ONCE(!enable_vmware_backdoor);
  5773. er = emulate_instruction(vcpu,
  5774. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  5775. if (er == EMULATE_USER_EXIT)
  5776. return 0;
  5777. else if (er != EMULATE_DONE)
  5778. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  5779. return 1;
  5780. }
  5781. /*
  5782. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  5783. * MMIO, it is better to report an internal error.
  5784. * See the comments in vmx_handle_exit.
  5785. */
  5786. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  5787. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  5788. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5789. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  5790. vcpu->run->internal.ndata = 3;
  5791. vcpu->run->internal.data[0] = vect_info;
  5792. vcpu->run->internal.data[1] = intr_info;
  5793. vcpu->run->internal.data[2] = error_code;
  5794. return 0;
  5795. }
  5796. if (is_page_fault(intr_info)) {
  5797. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  5798. /* EPT won't cause page fault directly */
  5799. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  5800. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  5801. }
  5802. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  5803. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  5804. return handle_rmode_exception(vcpu, ex_no, error_code);
  5805. switch (ex_no) {
  5806. case AC_VECTOR:
  5807. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  5808. return 1;
  5809. case DB_VECTOR:
  5810. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  5811. if (!(vcpu->guest_debug &
  5812. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  5813. vcpu->arch.dr6 &= ~15;
  5814. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  5815. if (is_icebp(intr_info))
  5816. skip_emulated_instruction(vcpu);
  5817. kvm_queue_exception(vcpu, DB_VECTOR);
  5818. return 1;
  5819. }
  5820. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  5821. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  5822. /* fall through */
  5823. case BP_VECTOR:
  5824. /*
  5825. * Update instruction length as we may reinject #BP from
  5826. * user space while in guest debugging mode. Reading it for
  5827. * #DB as well causes no harm, it is not used in that case.
  5828. */
  5829. vmx->vcpu.arch.event_exit_inst_len =
  5830. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5831. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  5832. rip = kvm_rip_read(vcpu);
  5833. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  5834. kvm_run->debug.arch.exception = ex_no;
  5835. break;
  5836. default:
  5837. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  5838. kvm_run->ex.exception = ex_no;
  5839. kvm_run->ex.error_code = error_code;
  5840. break;
  5841. }
  5842. return 0;
  5843. }
  5844. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  5845. {
  5846. ++vcpu->stat.irq_exits;
  5847. return 1;
  5848. }
  5849. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  5850. {
  5851. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5852. vcpu->mmio_needed = 0;
  5853. return 0;
  5854. }
  5855. static int handle_io(struct kvm_vcpu *vcpu)
  5856. {
  5857. unsigned long exit_qualification;
  5858. int size, in, string;
  5859. unsigned port;
  5860. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5861. string = (exit_qualification & 16) != 0;
  5862. ++vcpu->stat.io_exits;
  5863. if (string)
  5864. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5865. port = exit_qualification >> 16;
  5866. size = (exit_qualification & 7) + 1;
  5867. in = (exit_qualification & 8) != 0;
  5868. return kvm_fast_pio(vcpu, size, port, in);
  5869. }
  5870. static void
  5871. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  5872. {
  5873. /*
  5874. * Patch in the VMCALL instruction:
  5875. */
  5876. hypercall[0] = 0x0f;
  5877. hypercall[1] = 0x01;
  5878. hypercall[2] = 0xc1;
  5879. }
  5880. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  5881. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  5882. {
  5883. if (is_guest_mode(vcpu)) {
  5884. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5885. unsigned long orig_val = val;
  5886. /*
  5887. * We get here when L2 changed cr0 in a way that did not change
  5888. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  5889. * but did change L0 shadowed bits. So we first calculate the
  5890. * effective cr0 value that L1 would like to write into the
  5891. * hardware. It consists of the L2-owned bits from the new
  5892. * value combined with the L1-owned bits from L1's guest_cr0.
  5893. */
  5894. val = (val & ~vmcs12->cr0_guest_host_mask) |
  5895. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  5896. if (!nested_guest_cr0_valid(vcpu, val))
  5897. return 1;
  5898. if (kvm_set_cr0(vcpu, val))
  5899. return 1;
  5900. vmcs_writel(CR0_READ_SHADOW, orig_val);
  5901. return 0;
  5902. } else {
  5903. if (to_vmx(vcpu)->nested.vmxon &&
  5904. !nested_host_cr0_valid(vcpu, val))
  5905. return 1;
  5906. return kvm_set_cr0(vcpu, val);
  5907. }
  5908. }
  5909. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  5910. {
  5911. if (is_guest_mode(vcpu)) {
  5912. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5913. unsigned long orig_val = val;
  5914. /* analogously to handle_set_cr0 */
  5915. val = (val & ~vmcs12->cr4_guest_host_mask) |
  5916. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  5917. if (kvm_set_cr4(vcpu, val))
  5918. return 1;
  5919. vmcs_writel(CR4_READ_SHADOW, orig_val);
  5920. return 0;
  5921. } else
  5922. return kvm_set_cr4(vcpu, val);
  5923. }
  5924. static int handle_desc(struct kvm_vcpu *vcpu)
  5925. {
  5926. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  5927. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5928. }
  5929. static int handle_cr(struct kvm_vcpu *vcpu)
  5930. {
  5931. unsigned long exit_qualification, val;
  5932. int cr;
  5933. int reg;
  5934. int err;
  5935. int ret;
  5936. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5937. cr = exit_qualification & 15;
  5938. reg = (exit_qualification >> 8) & 15;
  5939. switch ((exit_qualification >> 4) & 3) {
  5940. case 0: /* mov to cr */
  5941. val = kvm_register_readl(vcpu, reg);
  5942. trace_kvm_cr_write(cr, val);
  5943. switch (cr) {
  5944. case 0:
  5945. err = handle_set_cr0(vcpu, val);
  5946. return kvm_complete_insn_gp(vcpu, err);
  5947. case 3:
  5948. WARN_ON_ONCE(enable_unrestricted_guest);
  5949. err = kvm_set_cr3(vcpu, val);
  5950. return kvm_complete_insn_gp(vcpu, err);
  5951. case 4:
  5952. err = handle_set_cr4(vcpu, val);
  5953. return kvm_complete_insn_gp(vcpu, err);
  5954. case 8: {
  5955. u8 cr8_prev = kvm_get_cr8(vcpu);
  5956. u8 cr8 = (u8)val;
  5957. err = kvm_set_cr8(vcpu, cr8);
  5958. ret = kvm_complete_insn_gp(vcpu, err);
  5959. if (lapic_in_kernel(vcpu))
  5960. return ret;
  5961. if (cr8_prev <= cr8)
  5962. return ret;
  5963. /*
  5964. * TODO: we might be squashing a
  5965. * KVM_GUESTDBG_SINGLESTEP-triggered
  5966. * KVM_EXIT_DEBUG here.
  5967. */
  5968. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  5969. return 0;
  5970. }
  5971. }
  5972. break;
  5973. case 2: /* clts */
  5974. WARN_ONCE(1, "Guest should always own CR0.TS");
  5975. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  5976. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  5977. return kvm_skip_emulated_instruction(vcpu);
  5978. case 1: /*mov from cr*/
  5979. switch (cr) {
  5980. case 3:
  5981. WARN_ON_ONCE(enable_unrestricted_guest);
  5982. val = kvm_read_cr3(vcpu);
  5983. kvm_register_write(vcpu, reg, val);
  5984. trace_kvm_cr_read(cr, val);
  5985. return kvm_skip_emulated_instruction(vcpu);
  5986. case 8:
  5987. val = kvm_get_cr8(vcpu);
  5988. kvm_register_write(vcpu, reg, val);
  5989. trace_kvm_cr_read(cr, val);
  5990. return kvm_skip_emulated_instruction(vcpu);
  5991. }
  5992. break;
  5993. case 3: /* lmsw */
  5994. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  5995. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  5996. kvm_lmsw(vcpu, val);
  5997. return kvm_skip_emulated_instruction(vcpu);
  5998. default:
  5999. break;
  6000. }
  6001. vcpu->run->exit_reason = 0;
  6002. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6003. (int)(exit_qualification >> 4) & 3, cr);
  6004. return 0;
  6005. }
  6006. static int handle_dr(struct kvm_vcpu *vcpu)
  6007. {
  6008. unsigned long exit_qualification;
  6009. int dr, dr7, reg;
  6010. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6011. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6012. /* First, if DR does not exist, trigger UD */
  6013. if (!kvm_require_dr(vcpu, dr))
  6014. return 1;
  6015. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6016. if (!kvm_require_cpl(vcpu, 0))
  6017. return 1;
  6018. dr7 = vmcs_readl(GUEST_DR7);
  6019. if (dr7 & DR7_GD) {
  6020. /*
  6021. * As the vm-exit takes precedence over the debug trap, we
  6022. * need to emulate the latter, either for the host or the
  6023. * guest debugging itself.
  6024. */
  6025. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6026. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6027. vcpu->run->debug.arch.dr7 = dr7;
  6028. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6029. vcpu->run->debug.arch.exception = DB_VECTOR;
  6030. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6031. return 0;
  6032. } else {
  6033. vcpu->arch.dr6 &= ~15;
  6034. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6035. kvm_queue_exception(vcpu, DB_VECTOR);
  6036. return 1;
  6037. }
  6038. }
  6039. if (vcpu->guest_debug == 0) {
  6040. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6041. CPU_BASED_MOV_DR_EXITING);
  6042. /*
  6043. * No more DR vmexits; force a reload of the debug registers
  6044. * and reenter on this instruction. The next vmexit will
  6045. * retrieve the full state of the debug registers.
  6046. */
  6047. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6048. return 1;
  6049. }
  6050. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6051. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6052. unsigned long val;
  6053. if (kvm_get_dr(vcpu, dr, &val))
  6054. return 1;
  6055. kvm_register_write(vcpu, reg, val);
  6056. } else
  6057. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6058. return 1;
  6059. return kvm_skip_emulated_instruction(vcpu);
  6060. }
  6061. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6062. {
  6063. return vcpu->arch.dr6;
  6064. }
  6065. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6066. {
  6067. }
  6068. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6069. {
  6070. get_debugreg(vcpu->arch.db[0], 0);
  6071. get_debugreg(vcpu->arch.db[1], 1);
  6072. get_debugreg(vcpu->arch.db[2], 2);
  6073. get_debugreg(vcpu->arch.db[3], 3);
  6074. get_debugreg(vcpu->arch.dr6, 6);
  6075. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6076. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6077. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6078. }
  6079. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6080. {
  6081. vmcs_writel(GUEST_DR7, val);
  6082. }
  6083. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6084. {
  6085. return kvm_emulate_cpuid(vcpu);
  6086. }
  6087. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6088. {
  6089. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6090. struct msr_data msr_info;
  6091. msr_info.index = ecx;
  6092. msr_info.host_initiated = false;
  6093. if (vmx_get_msr(vcpu, &msr_info)) {
  6094. trace_kvm_msr_read_ex(ecx);
  6095. kvm_inject_gp(vcpu, 0);
  6096. return 1;
  6097. }
  6098. trace_kvm_msr_read(ecx, msr_info.data);
  6099. /* FIXME: handling of bits 32:63 of rax, rdx */
  6100. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6101. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6102. return kvm_skip_emulated_instruction(vcpu);
  6103. }
  6104. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6105. {
  6106. struct msr_data msr;
  6107. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6108. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6109. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6110. msr.data = data;
  6111. msr.index = ecx;
  6112. msr.host_initiated = false;
  6113. if (kvm_set_msr(vcpu, &msr) != 0) {
  6114. trace_kvm_msr_write_ex(ecx, data);
  6115. kvm_inject_gp(vcpu, 0);
  6116. return 1;
  6117. }
  6118. trace_kvm_msr_write(ecx, data);
  6119. return kvm_skip_emulated_instruction(vcpu);
  6120. }
  6121. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6122. {
  6123. kvm_apic_update_ppr(vcpu);
  6124. return 1;
  6125. }
  6126. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6127. {
  6128. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6129. CPU_BASED_VIRTUAL_INTR_PENDING);
  6130. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6131. ++vcpu->stat.irq_window_exits;
  6132. return 1;
  6133. }
  6134. static int handle_halt(struct kvm_vcpu *vcpu)
  6135. {
  6136. return kvm_emulate_halt(vcpu);
  6137. }
  6138. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6139. {
  6140. return kvm_emulate_hypercall(vcpu);
  6141. }
  6142. static int handle_invd(struct kvm_vcpu *vcpu)
  6143. {
  6144. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6145. }
  6146. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6147. {
  6148. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6149. kvm_mmu_invlpg(vcpu, exit_qualification);
  6150. return kvm_skip_emulated_instruction(vcpu);
  6151. }
  6152. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6153. {
  6154. int err;
  6155. err = kvm_rdpmc(vcpu);
  6156. return kvm_complete_insn_gp(vcpu, err);
  6157. }
  6158. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6159. {
  6160. return kvm_emulate_wbinvd(vcpu);
  6161. }
  6162. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6163. {
  6164. u64 new_bv = kvm_read_edx_eax(vcpu);
  6165. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6166. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6167. return kvm_skip_emulated_instruction(vcpu);
  6168. return 1;
  6169. }
  6170. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6171. {
  6172. kvm_skip_emulated_instruction(vcpu);
  6173. WARN(1, "this should never happen\n");
  6174. return 1;
  6175. }
  6176. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6177. {
  6178. kvm_skip_emulated_instruction(vcpu);
  6179. WARN(1, "this should never happen\n");
  6180. return 1;
  6181. }
  6182. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6183. {
  6184. if (likely(fasteoi)) {
  6185. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6186. int access_type, offset;
  6187. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6188. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6189. /*
  6190. * Sane guest uses MOV to write EOI, with written value
  6191. * not cared. So make a short-circuit here by avoiding
  6192. * heavy instruction emulation.
  6193. */
  6194. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6195. (offset == APIC_EOI)) {
  6196. kvm_lapic_set_eoi(vcpu);
  6197. return kvm_skip_emulated_instruction(vcpu);
  6198. }
  6199. }
  6200. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6201. }
  6202. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6203. {
  6204. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6205. int vector = exit_qualification & 0xff;
  6206. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6207. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6208. return 1;
  6209. }
  6210. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6211. {
  6212. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6213. u32 offset = exit_qualification & 0xfff;
  6214. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6215. kvm_apic_write_nodecode(vcpu, offset);
  6216. return 1;
  6217. }
  6218. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6219. {
  6220. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6221. unsigned long exit_qualification;
  6222. bool has_error_code = false;
  6223. u32 error_code = 0;
  6224. u16 tss_selector;
  6225. int reason, type, idt_v, idt_index;
  6226. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6227. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6228. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6229. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6230. reason = (u32)exit_qualification >> 30;
  6231. if (reason == TASK_SWITCH_GATE && idt_v) {
  6232. switch (type) {
  6233. case INTR_TYPE_NMI_INTR:
  6234. vcpu->arch.nmi_injected = false;
  6235. vmx_set_nmi_mask(vcpu, true);
  6236. break;
  6237. case INTR_TYPE_EXT_INTR:
  6238. case INTR_TYPE_SOFT_INTR:
  6239. kvm_clear_interrupt_queue(vcpu);
  6240. break;
  6241. case INTR_TYPE_HARD_EXCEPTION:
  6242. if (vmx->idt_vectoring_info &
  6243. VECTORING_INFO_DELIVER_CODE_MASK) {
  6244. has_error_code = true;
  6245. error_code =
  6246. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6247. }
  6248. /* fall through */
  6249. case INTR_TYPE_SOFT_EXCEPTION:
  6250. kvm_clear_exception_queue(vcpu);
  6251. break;
  6252. default:
  6253. break;
  6254. }
  6255. }
  6256. tss_selector = exit_qualification;
  6257. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6258. type != INTR_TYPE_EXT_INTR &&
  6259. type != INTR_TYPE_NMI_INTR))
  6260. skip_emulated_instruction(vcpu);
  6261. if (kvm_task_switch(vcpu, tss_selector,
  6262. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6263. has_error_code, error_code) == EMULATE_FAIL) {
  6264. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6265. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6266. vcpu->run->internal.ndata = 0;
  6267. return 0;
  6268. }
  6269. /*
  6270. * TODO: What about debug traps on tss switch?
  6271. * Are we supposed to inject them and update dr6?
  6272. */
  6273. return 1;
  6274. }
  6275. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6276. {
  6277. unsigned long exit_qualification;
  6278. gpa_t gpa;
  6279. u64 error_code;
  6280. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6281. /*
  6282. * EPT violation happened while executing iret from NMI,
  6283. * "blocked by NMI" bit has to be set before next VM entry.
  6284. * There are errata that may cause this bit to not be set:
  6285. * AAK134, BY25.
  6286. */
  6287. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6288. enable_vnmi &&
  6289. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6290. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6291. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6292. trace_kvm_page_fault(gpa, exit_qualification);
  6293. /* Is it a read fault? */
  6294. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6295. ? PFERR_USER_MASK : 0;
  6296. /* Is it a write fault? */
  6297. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6298. ? PFERR_WRITE_MASK : 0;
  6299. /* Is it a fetch fault? */
  6300. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6301. ? PFERR_FETCH_MASK : 0;
  6302. /* ept page table entry is present? */
  6303. error_code |= (exit_qualification &
  6304. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6305. EPT_VIOLATION_EXECUTABLE))
  6306. ? PFERR_PRESENT_MASK : 0;
  6307. error_code |= (exit_qualification & 0x100) != 0 ?
  6308. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6309. vcpu->arch.exit_qualification = exit_qualification;
  6310. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6311. }
  6312. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6313. {
  6314. gpa_t gpa;
  6315. /*
  6316. * A nested guest cannot optimize MMIO vmexits, because we have an
  6317. * nGPA here instead of the required GPA.
  6318. */
  6319. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6320. if (!is_guest_mode(vcpu) &&
  6321. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6322. trace_kvm_fast_mmio(gpa);
  6323. /*
  6324. * Doing kvm_skip_emulated_instruction() depends on undefined
  6325. * behavior: Intel's manual doesn't mandate
  6326. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6327. * occurs and while on real hardware it was observed to be set,
  6328. * other hypervisors (namely Hyper-V) don't set it, we end up
  6329. * advancing IP with some random value. Disable fast mmio when
  6330. * running nested and keep it for real hardware in hope that
  6331. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6332. */
  6333. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6334. return kvm_skip_emulated_instruction(vcpu);
  6335. else
  6336. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  6337. NULL, 0) == EMULATE_DONE;
  6338. }
  6339. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6340. }
  6341. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6342. {
  6343. WARN_ON_ONCE(!enable_vnmi);
  6344. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6345. CPU_BASED_VIRTUAL_NMI_PENDING);
  6346. ++vcpu->stat.nmi_window_exits;
  6347. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6348. return 1;
  6349. }
  6350. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6351. {
  6352. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6353. enum emulation_result err = EMULATE_DONE;
  6354. int ret = 1;
  6355. u32 cpu_exec_ctrl;
  6356. bool intr_window_requested;
  6357. unsigned count = 130;
  6358. /*
  6359. * We should never reach the point where we are emulating L2
  6360. * due to invalid guest state as that means we incorrectly
  6361. * allowed a nested VMEntry with an invalid vmcs12.
  6362. */
  6363. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6364. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6365. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6366. while (vmx->emulation_required && count-- != 0) {
  6367. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6368. return handle_interrupt_window(&vmx->vcpu);
  6369. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6370. return 1;
  6371. err = emulate_instruction(vcpu, 0);
  6372. if (err == EMULATE_USER_EXIT) {
  6373. ++vcpu->stat.mmio_exits;
  6374. ret = 0;
  6375. goto out;
  6376. }
  6377. if (err != EMULATE_DONE)
  6378. goto emulation_error;
  6379. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6380. vcpu->arch.exception.pending)
  6381. goto emulation_error;
  6382. if (vcpu->arch.halt_request) {
  6383. vcpu->arch.halt_request = 0;
  6384. ret = kvm_vcpu_halt(vcpu);
  6385. goto out;
  6386. }
  6387. if (signal_pending(current))
  6388. goto out;
  6389. if (need_resched())
  6390. schedule();
  6391. }
  6392. out:
  6393. return ret;
  6394. emulation_error:
  6395. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6396. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6397. vcpu->run->internal.ndata = 0;
  6398. return 0;
  6399. }
  6400. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6401. {
  6402. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6403. int old = vmx->ple_window;
  6404. vmx->ple_window = __grow_ple_window(old, ple_window,
  6405. ple_window_grow,
  6406. ple_window_max);
  6407. if (vmx->ple_window != old)
  6408. vmx->ple_window_dirty = true;
  6409. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6410. }
  6411. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6412. {
  6413. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6414. int old = vmx->ple_window;
  6415. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6416. ple_window_shrink,
  6417. ple_window);
  6418. if (vmx->ple_window != old)
  6419. vmx->ple_window_dirty = true;
  6420. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6421. }
  6422. /*
  6423. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6424. */
  6425. static void wakeup_handler(void)
  6426. {
  6427. struct kvm_vcpu *vcpu;
  6428. int cpu = smp_processor_id();
  6429. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6430. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6431. blocked_vcpu_list) {
  6432. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6433. if (pi_test_on(pi_desc) == 1)
  6434. kvm_vcpu_kick(vcpu);
  6435. }
  6436. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6437. }
  6438. static void vmx_enable_tdp(void)
  6439. {
  6440. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6441. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6442. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6443. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6444. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6445. VMX_EPT_RWX_MASK, 0ull);
  6446. ept_set_mmio_spte_mask();
  6447. kvm_enable_tdp();
  6448. }
  6449. static __init int hardware_setup(void)
  6450. {
  6451. int r = -ENOMEM, i;
  6452. rdmsrl_safe(MSR_EFER, &host_efer);
  6453. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6454. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6455. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6456. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6457. if (!vmx_bitmap[i])
  6458. goto out;
  6459. }
  6460. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6461. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6462. if (setup_vmcs_config(&vmcs_config) < 0) {
  6463. r = -EIO;
  6464. goto out;
  6465. }
  6466. if (boot_cpu_has(X86_FEATURE_NX))
  6467. kvm_enable_efer_bits(EFER_NX);
  6468. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6469. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6470. enable_vpid = 0;
  6471. if (!cpu_has_vmx_ept() ||
  6472. !cpu_has_vmx_ept_4levels() ||
  6473. !cpu_has_vmx_ept_mt_wb() ||
  6474. !cpu_has_vmx_invept_global())
  6475. enable_ept = 0;
  6476. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6477. enable_ept_ad_bits = 0;
  6478. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6479. enable_unrestricted_guest = 0;
  6480. if (!cpu_has_vmx_flexpriority())
  6481. flexpriority_enabled = 0;
  6482. if (!cpu_has_virtual_nmis())
  6483. enable_vnmi = 0;
  6484. /*
  6485. * set_apic_access_page_addr() is used to reload apic access
  6486. * page upon invalidation. No need to do anything if not
  6487. * using the APIC_ACCESS_ADDR VMCS field.
  6488. */
  6489. if (!flexpriority_enabled)
  6490. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6491. if (!cpu_has_vmx_tpr_shadow())
  6492. kvm_x86_ops->update_cr8_intercept = NULL;
  6493. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6494. kvm_disable_largepages();
  6495. if (!cpu_has_vmx_ple()) {
  6496. ple_gap = 0;
  6497. ple_window = 0;
  6498. ple_window_grow = 0;
  6499. ple_window_max = 0;
  6500. ple_window_shrink = 0;
  6501. }
  6502. if (!cpu_has_vmx_apicv()) {
  6503. enable_apicv = 0;
  6504. kvm_x86_ops->sync_pir_to_irr = NULL;
  6505. }
  6506. if (cpu_has_vmx_tsc_scaling()) {
  6507. kvm_has_tsc_control = true;
  6508. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6509. kvm_tsc_scaling_ratio_frac_bits = 48;
  6510. }
  6511. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6512. if (enable_ept)
  6513. vmx_enable_tdp();
  6514. else
  6515. kvm_disable_tdp();
  6516. /*
  6517. * Only enable PML when hardware supports PML feature, and both EPT
  6518. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6519. */
  6520. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6521. enable_pml = 0;
  6522. if (!enable_pml) {
  6523. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6524. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6525. kvm_x86_ops->flush_log_dirty = NULL;
  6526. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6527. }
  6528. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6529. u64 vmx_msr;
  6530. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6531. cpu_preemption_timer_multi =
  6532. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6533. } else {
  6534. kvm_x86_ops->set_hv_timer = NULL;
  6535. kvm_x86_ops->cancel_hv_timer = NULL;
  6536. }
  6537. if (!cpu_has_vmx_shadow_vmcs())
  6538. enable_shadow_vmcs = 0;
  6539. if (enable_shadow_vmcs)
  6540. init_vmcs_shadow_fields();
  6541. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6542. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6543. kvm_mce_cap_supported |= MCG_LMCE_P;
  6544. return alloc_kvm_area();
  6545. out:
  6546. for (i = 0; i < VMX_BITMAP_NR; i++)
  6547. free_page((unsigned long)vmx_bitmap[i]);
  6548. return r;
  6549. }
  6550. static __exit void hardware_unsetup(void)
  6551. {
  6552. int i;
  6553. for (i = 0; i < VMX_BITMAP_NR; i++)
  6554. free_page((unsigned long)vmx_bitmap[i]);
  6555. free_kvm_area();
  6556. }
  6557. /*
  6558. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6559. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6560. */
  6561. static int handle_pause(struct kvm_vcpu *vcpu)
  6562. {
  6563. if (!kvm_pause_in_guest(vcpu->kvm))
  6564. grow_ple_window(vcpu);
  6565. /*
  6566. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6567. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6568. * never set PAUSE_EXITING and just set PLE if supported,
  6569. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6570. */
  6571. kvm_vcpu_on_spin(vcpu, true);
  6572. return kvm_skip_emulated_instruction(vcpu);
  6573. }
  6574. static int handle_nop(struct kvm_vcpu *vcpu)
  6575. {
  6576. return kvm_skip_emulated_instruction(vcpu);
  6577. }
  6578. static int handle_mwait(struct kvm_vcpu *vcpu)
  6579. {
  6580. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6581. return handle_nop(vcpu);
  6582. }
  6583. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6584. {
  6585. kvm_queue_exception(vcpu, UD_VECTOR);
  6586. return 1;
  6587. }
  6588. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6589. {
  6590. return 1;
  6591. }
  6592. static int handle_monitor(struct kvm_vcpu *vcpu)
  6593. {
  6594. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6595. return handle_nop(vcpu);
  6596. }
  6597. /*
  6598. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6599. * set the success or error code of an emulated VMX instruction, as specified
  6600. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6601. */
  6602. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6603. {
  6604. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6605. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6606. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6607. }
  6608. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6609. {
  6610. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6611. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6612. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6613. | X86_EFLAGS_CF);
  6614. }
  6615. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6616. u32 vm_instruction_error)
  6617. {
  6618. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6619. /*
  6620. * failValid writes the error number to the current VMCS, which
  6621. * can't be done there isn't a current VMCS.
  6622. */
  6623. nested_vmx_failInvalid(vcpu);
  6624. return;
  6625. }
  6626. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6627. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6628. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6629. | X86_EFLAGS_ZF);
  6630. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6631. /*
  6632. * We don't need to force a shadow sync because
  6633. * VM_INSTRUCTION_ERROR is not shadowed
  6634. */
  6635. }
  6636. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6637. {
  6638. /* TODO: not to reset guest simply here. */
  6639. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6640. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6641. }
  6642. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6643. {
  6644. struct vcpu_vmx *vmx =
  6645. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6646. vmx->nested.preemption_timer_expired = true;
  6647. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6648. kvm_vcpu_kick(&vmx->vcpu);
  6649. return HRTIMER_NORESTART;
  6650. }
  6651. /*
  6652. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6653. * exit caused by such an instruction (run by a guest hypervisor).
  6654. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6655. * #UD or #GP.
  6656. */
  6657. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6658. unsigned long exit_qualification,
  6659. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6660. {
  6661. gva_t off;
  6662. bool exn;
  6663. struct kvm_segment s;
  6664. /*
  6665. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6666. * Execution", on an exit, vmx_instruction_info holds most of the
  6667. * addressing components of the operand. Only the displacement part
  6668. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6669. * For how an actual address is calculated from all these components,
  6670. * refer to Vol. 1, "Operand Addressing".
  6671. */
  6672. int scaling = vmx_instruction_info & 3;
  6673. int addr_size = (vmx_instruction_info >> 7) & 7;
  6674. bool is_reg = vmx_instruction_info & (1u << 10);
  6675. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6676. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6677. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6678. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6679. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6680. if (is_reg) {
  6681. kvm_queue_exception(vcpu, UD_VECTOR);
  6682. return 1;
  6683. }
  6684. /* Addr = segment_base + offset */
  6685. /* offset = base + [index * scale] + displacement */
  6686. off = exit_qualification; /* holds the displacement */
  6687. if (base_is_valid)
  6688. off += kvm_register_read(vcpu, base_reg);
  6689. if (index_is_valid)
  6690. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6691. vmx_get_segment(vcpu, &s, seg_reg);
  6692. *ret = s.base + off;
  6693. if (addr_size == 1) /* 32 bit */
  6694. *ret &= 0xffffffff;
  6695. /* Checks for #GP/#SS exceptions. */
  6696. exn = false;
  6697. if (is_long_mode(vcpu)) {
  6698. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6699. * non-canonical form. This is the only check on the memory
  6700. * destination for long mode!
  6701. */
  6702. exn = is_noncanonical_address(*ret, vcpu);
  6703. } else if (is_protmode(vcpu)) {
  6704. /* Protected mode: apply checks for segment validity in the
  6705. * following order:
  6706. * - segment type check (#GP(0) may be thrown)
  6707. * - usability check (#GP(0)/#SS(0))
  6708. * - limit check (#GP(0)/#SS(0))
  6709. */
  6710. if (wr)
  6711. /* #GP(0) if the destination operand is located in a
  6712. * read-only data segment or any code segment.
  6713. */
  6714. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  6715. else
  6716. /* #GP(0) if the source operand is located in an
  6717. * execute-only code segment
  6718. */
  6719. exn = ((s.type & 0xa) == 8);
  6720. if (exn) {
  6721. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  6722. return 1;
  6723. }
  6724. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  6725. */
  6726. exn = (s.unusable != 0);
  6727. /* Protected mode: #GP(0)/#SS(0) if the memory
  6728. * operand is outside the segment limit.
  6729. */
  6730. exn = exn || (off + sizeof(u64) > s.limit);
  6731. }
  6732. if (exn) {
  6733. kvm_queue_exception_e(vcpu,
  6734. seg_reg == VCPU_SREG_SS ?
  6735. SS_VECTOR : GP_VECTOR,
  6736. 0);
  6737. return 1;
  6738. }
  6739. return 0;
  6740. }
  6741. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  6742. {
  6743. gva_t gva;
  6744. struct x86_exception e;
  6745. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6746. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  6747. return 1;
  6748. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  6749. kvm_inject_page_fault(vcpu, &e);
  6750. return 1;
  6751. }
  6752. return 0;
  6753. }
  6754. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  6755. {
  6756. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6757. struct vmcs *shadow_vmcs;
  6758. int r;
  6759. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  6760. if (r < 0)
  6761. goto out_vmcs02;
  6762. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6763. if (!vmx->nested.cached_vmcs12)
  6764. goto out_cached_vmcs12;
  6765. if (enable_shadow_vmcs) {
  6766. shadow_vmcs = alloc_vmcs();
  6767. if (!shadow_vmcs)
  6768. goto out_shadow_vmcs;
  6769. /* mark vmcs as shadow */
  6770. shadow_vmcs->revision_id |= (1u << 31);
  6771. /* init shadow vmcs */
  6772. vmcs_clear(shadow_vmcs);
  6773. vmx->vmcs01.shadow_vmcs = shadow_vmcs;
  6774. }
  6775. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  6776. HRTIMER_MODE_REL_PINNED);
  6777. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  6778. vmx->nested.vmxon = true;
  6779. return 0;
  6780. out_shadow_vmcs:
  6781. kfree(vmx->nested.cached_vmcs12);
  6782. out_cached_vmcs12:
  6783. free_loaded_vmcs(&vmx->nested.vmcs02);
  6784. out_vmcs02:
  6785. return -ENOMEM;
  6786. }
  6787. /*
  6788. * Emulate the VMXON instruction.
  6789. * Currently, we just remember that VMX is active, and do not save or even
  6790. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  6791. * do not currently need to store anything in that guest-allocated memory
  6792. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  6793. * argument is different from the VMXON pointer (which the spec says they do).
  6794. */
  6795. static int handle_vmon(struct kvm_vcpu *vcpu)
  6796. {
  6797. int ret;
  6798. gpa_t vmptr;
  6799. struct page *page;
  6800. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6801. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  6802. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  6803. /*
  6804. * The Intel VMX Instruction Reference lists a bunch of bits that are
  6805. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  6806. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  6807. * Otherwise, we should fail with #UD. But most faulting conditions
  6808. * have already been checked by hardware, prior to the VM-exit for
  6809. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  6810. * that bit set to 1 in non-root mode.
  6811. */
  6812. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  6813. kvm_queue_exception(vcpu, UD_VECTOR);
  6814. return 1;
  6815. }
  6816. /* CPL=0 must be checked manually. */
  6817. if (vmx_get_cpl(vcpu)) {
  6818. kvm_queue_exception(vcpu, UD_VECTOR);
  6819. return 1;
  6820. }
  6821. if (vmx->nested.vmxon) {
  6822. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  6823. return kvm_skip_emulated_instruction(vcpu);
  6824. }
  6825. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  6826. != VMXON_NEEDED_FEATURES) {
  6827. kvm_inject_gp(vcpu, 0);
  6828. return 1;
  6829. }
  6830. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6831. return 1;
  6832. /*
  6833. * SDM 3: 24.11.5
  6834. * The first 4 bytes of VMXON region contain the supported
  6835. * VMCS revision identifier
  6836. *
  6837. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  6838. * which replaces physical address width with 32
  6839. */
  6840. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6841. nested_vmx_failInvalid(vcpu);
  6842. return kvm_skip_emulated_instruction(vcpu);
  6843. }
  6844. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6845. if (is_error_page(page)) {
  6846. nested_vmx_failInvalid(vcpu);
  6847. return kvm_skip_emulated_instruction(vcpu);
  6848. }
  6849. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  6850. kunmap(page);
  6851. kvm_release_page_clean(page);
  6852. nested_vmx_failInvalid(vcpu);
  6853. return kvm_skip_emulated_instruction(vcpu);
  6854. }
  6855. kunmap(page);
  6856. kvm_release_page_clean(page);
  6857. vmx->nested.vmxon_ptr = vmptr;
  6858. ret = enter_vmx_operation(vcpu);
  6859. if (ret)
  6860. return ret;
  6861. nested_vmx_succeed(vcpu);
  6862. return kvm_skip_emulated_instruction(vcpu);
  6863. }
  6864. /*
  6865. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  6866. * for running VMX instructions (except VMXON, whose prerequisites are
  6867. * slightly different). It also specifies what exception to inject otherwise.
  6868. * Note that many of these exceptions have priority over VM exits, so they
  6869. * don't have to be checked again here.
  6870. */
  6871. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  6872. {
  6873. if (vmx_get_cpl(vcpu)) {
  6874. kvm_queue_exception(vcpu, UD_VECTOR);
  6875. return 0;
  6876. }
  6877. if (!to_vmx(vcpu)->nested.vmxon) {
  6878. kvm_queue_exception(vcpu, UD_VECTOR);
  6879. return 0;
  6880. }
  6881. return 1;
  6882. }
  6883. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  6884. {
  6885. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  6886. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  6887. }
  6888. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  6889. {
  6890. if (vmx->nested.current_vmptr == -1ull)
  6891. return;
  6892. if (enable_shadow_vmcs) {
  6893. /* copy to memory all shadowed fields in case
  6894. they were modified */
  6895. copy_shadow_to_vmcs12(vmx);
  6896. vmx->nested.sync_shadow_vmcs = false;
  6897. vmx_disable_shadow_vmcs(vmx);
  6898. }
  6899. vmx->nested.posted_intr_nv = -1;
  6900. /* Flush VMCS12 to guest memory */
  6901. kvm_vcpu_write_guest_page(&vmx->vcpu,
  6902. vmx->nested.current_vmptr >> PAGE_SHIFT,
  6903. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  6904. vmx->nested.current_vmptr = -1ull;
  6905. }
  6906. /*
  6907. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  6908. * just stops using VMX.
  6909. */
  6910. static void free_nested(struct vcpu_vmx *vmx)
  6911. {
  6912. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  6913. return;
  6914. vmx->nested.vmxon = false;
  6915. vmx->nested.smm.vmxon = false;
  6916. free_vpid(vmx->nested.vpid02);
  6917. vmx->nested.posted_intr_nv = -1;
  6918. vmx->nested.current_vmptr = -1ull;
  6919. if (enable_shadow_vmcs) {
  6920. vmx_disable_shadow_vmcs(vmx);
  6921. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  6922. free_vmcs(vmx->vmcs01.shadow_vmcs);
  6923. vmx->vmcs01.shadow_vmcs = NULL;
  6924. }
  6925. kfree(vmx->nested.cached_vmcs12);
  6926. /* Unpin physical memory we referred to in the vmcs02 */
  6927. if (vmx->nested.apic_access_page) {
  6928. kvm_release_page_dirty(vmx->nested.apic_access_page);
  6929. vmx->nested.apic_access_page = NULL;
  6930. }
  6931. if (vmx->nested.virtual_apic_page) {
  6932. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  6933. vmx->nested.virtual_apic_page = NULL;
  6934. }
  6935. if (vmx->nested.pi_desc_page) {
  6936. kunmap(vmx->nested.pi_desc_page);
  6937. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  6938. vmx->nested.pi_desc_page = NULL;
  6939. vmx->nested.pi_desc = NULL;
  6940. }
  6941. free_loaded_vmcs(&vmx->nested.vmcs02);
  6942. }
  6943. /* Emulate the VMXOFF instruction */
  6944. static int handle_vmoff(struct kvm_vcpu *vcpu)
  6945. {
  6946. if (!nested_vmx_check_permission(vcpu))
  6947. return 1;
  6948. free_nested(to_vmx(vcpu));
  6949. nested_vmx_succeed(vcpu);
  6950. return kvm_skip_emulated_instruction(vcpu);
  6951. }
  6952. /* Emulate the VMCLEAR instruction */
  6953. static int handle_vmclear(struct kvm_vcpu *vcpu)
  6954. {
  6955. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6956. u32 zero = 0;
  6957. gpa_t vmptr;
  6958. if (!nested_vmx_check_permission(vcpu))
  6959. return 1;
  6960. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6961. return 1;
  6962. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6963. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  6964. return kvm_skip_emulated_instruction(vcpu);
  6965. }
  6966. if (vmptr == vmx->nested.vmxon_ptr) {
  6967. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  6968. return kvm_skip_emulated_instruction(vcpu);
  6969. }
  6970. if (vmptr == vmx->nested.current_vmptr)
  6971. nested_release_vmcs12(vmx);
  6972. kvm_vcpu_write_guest(vcpu,
  6973. vmptr + offsetof(struct vmcs12, launch_state),
  6974. &zero, sizeof(zero));
  6975. nested_vmx_succeed(vcpu);
  6976. return kvm_skip_emulated_instruction(vcpu);
  6977. }
  6978. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  6979. /* Emulate the VMLAUNCH instruction */
  6980. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  6981. {
  6982. return nested_vmx_run(vcpu, true);
  6983. }
  6984. /* Emulate the VMRESUME instruction */
  6985. static int handle_vmresume(struct kvm_vcpu *vcpu)
  6986. {
  6987. return nested_vmx_run(vcpu, false);
  6988. }
  6989. /*
  6990. * Read a vmcs12 field. Since these can have varying lengths and we return
  6991. * one type, we chose the biggest type (u64) and zero-extend the return value
  6992. * to that size. Note that the caller, handle_vmread, might need to use only
  6993. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  6994. * 64-bit fields are to be returned).
  6995. */
  6996. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  6997. unsigned long field, u64 *ret)
  6998. {
  6999. short offset = vmcs_field_to_offset(field);
  7000. char *p;
  7001. if (offset < 0)
  7002. return offset;
  7003. p = ((char *)(get_vmcs12(vcpu))) + offset;
  7004. switch (vmcs_field_width(field)) {
  7005. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7006. *ret = *((natural_width *)p);
  7007. return 0;
  7008. case VMCS_FIELD_WIDTH_U16:
  7009. *ret = *((u16 *)p);
  7010. return 0;
  7011. case VMCS_FIELD_WIDTH_U32:
  7012. *ret = *((u32 *)p);
  7013. return 0;
  7014. case VMCS_FIELD_WIDTH_U64:
  7015. *ret = *((u64 *)p);
  7016. return 0;
  7017. default:
  7018. WARN_ON(1);
  7019. return -ENOENT;
  7020. }
  7021. }
  7022. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  7023. unsigned long field, u64 field_value){
  7024. short offset = vmcs_field_to_offset(field);
  7025. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  7026. if (offset < 0)
  7027. return offset;
  7028. switch (vmcs_field_width(field)) {
  7029. case VMCS_FIELD_WIDTH_U16:
  7030. *(u16 *)p = field_value;
  7031. return 0;
  7032. case VMCS_FIELD_WIDTH_U32:
  7033. *(u32 *)p = field_value;
  7034. return 0;
  7035. case VMCS_FIELD_WIDTH_U64:
  7036. *(u64 *)p = field_value;
  7037. return 0;
  7038. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7039. *(natural_width *)p = field_value;
  7040. return 0;
  7041. default:
  7042. WARN_ON(1);
  7043. return -ENOENT;
  7044. }
  7045. }
  7046. /*
  7047. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7048. * they have been modified by the L1 guest. Note that the "read-only"
  7049. * VM-exit information fields are actually writable if the vCPU is
  7050. * configured to support "VMWRITE to any supported field in the VMCS."
  7051. */
  7052. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7053. {
  7054. const u16 *fields[] = {
  7055. shadow_read_write_fields,
  7056. shadow_read_only_fields
  7057. };
  7058. const int max_fields[] = {
  7059. max_shadow_read_write_fields,
  7060. max_shadow_read_only_fields
  7061. };
  7062. int i, q;
  7063. unsigned long field;
  7064. u64 field_value;
  7065. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7066. preempt_disable();
  7067. vmcs_load(shadow_vmcs);
  7068. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7069. for (i = 0; i < max_fields[q]; i++) {
  7070. field = fields[q][i];
  7071. field_value = __vmcs_readl(field);
  7072. vmcs12_write_any(&vmx->vcpu, field, field_value);
  7073. }
  7074. /*
  7075. * Skip the VM-exit information fields if they are read-only.
  7076. */
  7077. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7078. break;
  7079. }
  7080. vmcs_clear(shadow_vmcs);
  7081. vmcs_load(vmx->loaded_vmcs->vmcs);
  7082. preempt_enable();
  7083. }
  7084. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7085. {
  7086. const u16 *fields[] = {
  7087. shadow_read_write_fields,
  7088. shadow_read_only_fields
  7089. };
  7090. const int max_fields[] = {
  7091. max_shadow_read_write_fields,
  7092. max_shadow_read_only_fields
  7093. };
  7094. int i, q;
  7095. unsigned long field;
  7096. u64 field_value = 0;
  7097. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7098. vmcs_load(shadow_vmcs);
  7099. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7100. for (i = 0; i < max_fields[q]; i++) {
  7101. field = fields[q][i];
  7102. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  7103. __vmcs_writel(field, field_value);
  7104. }
  7105. }
  7106. vmcs_clear(shadow_vmcs);
  7107. vmcs_load(vmx->loaded_vmcs->vmcs);
  7108. }
  7109. /*
  7110. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7111. * used before) all generate the same failure when it is missing.
  7112. */
  7113. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7114. {
  7115. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7116. if (vmx->nested.current_vmptr == -1ull) {
  7117. nested_vmx_failInvalid(vcpu);
  7118. return 0;
  7119. }
  7120. return 1;
  7121. }
  7122. static int handle_vmread(struct kvm_vcpu *vcpu)
  7123. {
  7124. unsigned long field;
  7125. u64 field_value;
  7126. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7127. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7128. gva_t gva = 0;
  7129. if (!nested_vmx_check_permission(vcpu))
  7130. return 1;
  7131. if (!nested_vmx_check_vmcs12(vcpu))
  7132. return kvm_skip_emulated_instruction(vcpu);
  7133. /* Decode instruction info and find the field to read */
  7134. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7135. /* Read the field, zero-extended to a u64 field_value */
  7136. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  7137. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7138. return kvm_skip_emulated_instruction(vcpu);
  7139. }
  7140. /*
  7141. * Now copy part of this value to register or memory, as requested.
  7142. * Note that the number of bits actually copied is 32 or 64 depending
  7143. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7144. */
  7145. if (vmx_instruction_info & (1u << 10)) {
  7146. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7147. field_value);
  7148. } else {
  7149. if (get_vmx_mem_address(vcpu, exit_qualification,
  7150. vmx_instruction_info, true, &gva))
  7151. return 1;
  7152. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7153. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7154. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7155. }
  7156. nested_vmx_succeed(vcpu);
  7157. return kvm_skip_emulated_instruction(vcpu);
  7158. }
  7159. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7160. {
  7161. unsigned long field;
  7162. gva_t gva;
  7163. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7164. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7165. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7166. /* The value to write might be 32 or 64 bits, depending on L1's long
  7167. * mode, and eventually we need to write that into a field of several
  7168. * possible lengths. The code below first zero-extends the value to 64
  7169. * bit (field_value), and then copies only the appropriate number of
  7170. * bits into the vmcs12 field.
  7171. */
  7172. u64 field_value = 0;
  7173. struct x86_exception e;
  7174. if (!nested_vmx_check_permission(vcpu))
  7175. return 1;
  7176. if (!nested_vmx_check_vmcs12(vcpu))
  7177. return kvm_skip_emulated_instruction(vcpu);
  7178. if (vmx_instruction_info & (1u << 10))
  7179. field_value = kvm_register_readl(vcpu,
  7180. (((vmx_instruction_info) >> 3) & 0xf));
  7181. else {
  7182. if (get_vmx_mem_address(vcpu, exit_qualification,
  7183. vmx_instruction_info, false, &gva))
  7184. return 1;
  7185. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7186. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7187. kvm_inject_page_fault(vcpu, &e);
  7188. return 1;
  7189. }
  7190. }
  7191. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7192. /*
  7193. * If the vCPU supports "VMWRITE to any supported field in the
  7194. * VMCS," then the "read-only" fields are actually read/write.
  7195. */
  7196. if (vmcs_field_readonly(field) &&
  7197. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7198. nested_vmx_failValid(vcpu,
  7199. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7200. return kvm_skip_emulated_instruction(vcpu);
  7201. }
  7202. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  7203. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7204. return kvm_skip_emulated_instruction(vcpu);
  7205. }
  7206. switch (field) {
  7207. #define SHADOW_FIELD_RW(x) case x:
  7208. #include "vmx_shadow_fields.h"
  7209. /*
  7210. * The fields that can be updated by L1 without a vmexit are
  7211. * always updated in the vmcs02, the others go down the slow
  7212. * path of prepare_vmcs02.
  7213. */
  7214. break;
  7215. default:
  7216. vmx->nested.dirty_vmcs12 = true;
  7217. break;
  7218. }
  7219. nested_vmx_succeed(vcpu);
  7220. return kvm_skip_emulated_instruction(vcpu);
  7221. }
  7222. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7223. {
  7224. vmx->nested.current_vmptr = vmptr;
  7225. if (enable_shadow_vmcs) {
  7226. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7227. SECONDARY_EXEC_SHADOW_VMCS);
  7228. vmcs_write64(VMCS_LINK_POINTER,
  7229. __pa(vmx->vmcs01.shadow_vmcs));
  7230. vmx->nested.sync_shadow_vmcs = true;
  7231. }
  7232. vmx->nested.dirty_vmcs12 = true;
  7233. }
  7234. /* Emulate the VMPTRLD instruction */
  7235. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7236. {
  7237. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7238. gpa_t vmptr;
  7239. if (!nested_vmx_check_permission(vcpu))
  7240. return 1;
  7241. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7242. return 1;
  7243. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7244. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7245. return kvm_skip_emulated_instruction(vcpu);
  7246. }
  7247. if (vmptr == vmx->nested.vmxon_ptr) {
  7248. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7249. return kvm_skip_emulated_instruction(vcpu);
  7250. }
  7251. if (vmx->nested.current_vmptr != vmptr) {
  7252. struct vmcs12 *new_vmcs12;
  7253. struct page *page;
  7254. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7255. if (is_error_page(page)) {
  7256. nested_vmx_failInvalid(vcpu);
  7257. return kvm_skip_emulated_instruction(vcpu);
  7258. }
  7259. new_vmcs12 = kmap(page);
  7260. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  7261. kunmap(page);
  7262. kvm_release_page_clean(page);
  7263. nested_vmx_failValid(vcpu,
  7264. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7265. return kvm_skip_emulated_instruction(vcpu);
  7266. }
  7267. nested_release_vmcs12(vmx);
  7268. /*
  7269. * Load VMCS12 from guest memory since it is not already
  7270. * cached.
  7271. */
  7272. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7273. kunmap(page);
  7274. kvm_release_page_clean(page);
  7275. set_current_vmptr(vmx, vmptr);
  7276. }
  7277. nested_vmx_succeed(vcpu);
  7278. return kvm_skip_emulated_instruction(vcpu);
  7279. }
  7280. /* Emulate the VMPTRST instruction */
  7281. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7282. {
  7283. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7284. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7285. gva_t vmcs_gva;
  7286. struct x86_exception e;
  7287. if (!nested_vmx_check_permission(vcpu))
  7288. return 1;
  7289. if (get_vmx_mem_address(vcpu, exit_qualification,
  7290. vmx_instruction_info, true, &vmcs_gva))
  7291. return 1;
  7292. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7293. if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
  7294. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  7295. sizeof(u64), &e)) {
  7296. kvm_inject_page_fault(vcpu, &e);
  7297. return 1;
  7298. }
  7299. nested_vmx_succeed(vcpu);
  7300. return kvm_skip_emulated_instruction(vcpu);
  7301. }
  7302. /* Emulate the INVEPT instruction */
  7303. static int handle_invept(struct kvm_vcpu *vcpu)
  7304. {
  7305. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7306. u32 vmx_instruction_info, types;
  7307. unsigned long type;
  7308. gva_t gva;
  7309. struct x86_exception e;
  7310. struct {
  7311. u64 eptp, gpa;
  7312. } operand;
  7313. if (!(vmx->nested.msrs.secondary_ctls_high &
  7314. SECONDARY_EXEC_ENABLE_EPT) ||
  7315. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7316. kvm_queue_exception(vcpu, UD_VECTOR);
  7317. return 1;
  7318. }
  7319. if (!nested_vmx_check_permission(vcpu))
  7320. return 1;
  7321. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7322. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7323. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7324. if (type >= 32 || !(types & (1 << type))) {
  7325. nested_vmx_failValid(vcpu,
  7326. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7327. return kvm_skip_emulated_instruction(vcpu);
  7328. }
  7329. /* According to the Intel VMX instruction reference, the memory
  7330. * operand is read even if it isn't needed (e.g., for type==global)
  7331. */
  7332. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7333. vmx_instruction_info, false, &gva))
  7334. return 1;
  7335. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7336. kvm_inject_page_fault(vcpu, &e);
  7337. return 1;
  7338. }
  7339. switch (type) {
  7340. case VMX_EPT_EXTENT_GLOBAL:
  7341. /*
  7342. * TODO: track mappings and invalidate
  7343. * single context requests appropriately
  7344. */
  7345. case VMX_EPT_EXTENT_CONTEXT:
  7346. kvm_mmu_sync_roots(vcpu);
  7347. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7348. nested_vmx_succeed(vcpu);
  7349. break;
  7350. default:
  7351. BUG_ON(1);
  7352. break;
  7353. }
  7354. return kvm_skip_emulated_instruction(vcpu);
  7355. }
  7356. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7357. {
  7358. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7359. u32 vmx_instruction_info;
  7360. unsigned long type, types;
  7361. gva_t gva;
  7362. struct x86_exception e;
  7363. struct {
  7364. u64 vpid;
  7365. u64 gla;
  7366. } operand;
  7367. if (!(vmx->nested.msrs.secondary_ctls_high &
  7368. SECONDARY_EXEC_ENABLE_VPID) ||
  7369. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7370. kvm_queue_exception(vcpu, UD_VECTOR);
  7371. return 1;
  7372. }
  7373. if (!nested_vmx_check_permission(vcpu))
  7374. return 1;
  7375. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7376. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7377. types = (vmx->nested.msrs.vpid_caps &
  7378. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7379. if (type >= 32 || !(types & (1 << type))) {
  7380. nested_vmx_failValid(vcpu,
  7381. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7382. return kvm_skip_emulated_instruction(vcpu);
  7383. }
  7384. /* according to the intel vmx instruction reference, the memory
  7385. * operand is read even if it isn't needed (e.g., for type==global)
  7386. */
  7387. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7388. vmx_instruction_info, false, &gva))
  7389. return 1;
  7390. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7391. kvm_inject_page_fault(vcpu, &e);
  7392. return 1;
  7393. }
  7394. if (operand.vpid >> 16) {
  7395. nested_vmx_failValid(vcpu,
  7396. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7397. return kvm_skip_emulated_instruction(vcpu);
  7398. }
  7399. switch (type) {
  7400. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7401. if (!operand.vpid ||
  7402. is_noncanonical_address(operand.gla, vcpu)) {
  7403. nested_vmx_failValid(vcpu,
  7404. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7405. return kvm_skip_emulated_instruction(vcpu);
  7406. }
  7407. if (cpu_has_vmx_invvpid_individual_addr() &&
  7408. vmx->nested.vpid02) {
  7409. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7410. vmx->nested.vpid02, operand.gla);
  7411. } else
  7412. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7413. break;
  7414. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7415. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7416. if (!operand.vpid) {
  7417. nested_vmx_failValid(vcpu,
  7418. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7419. return kvm_skip_emulated_instruction(vcpu);
  7420. }
  7421. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7422. break;
  7423. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7424. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7425. break;
  7426. default:
  7427. WARN_ON_ONCE(1);
  7428. return kvm_skip_emulated_instruction(vcpu);
  7429. }
  7430. nested_vmx_succeed(vcpu);
  7431. return kvm_skip_emulated_instruction(vcpu);
  7432. }
  7433. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7434. {
  7435. unsigned long exit_qualification;
  7436. trace_kvm_pml_full(vcpu->vcpu_id);
  7437. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7438. /*
  7439. * PML buffer FULL happened while executing iret from NMI,
  7440. * "blocked by NMI" bit has to be set before next VM entry.
  7441. */
  7442. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7443. enable_vnmi &&
  7444. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7445. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7446. GUEST_INTR_STATE_NMI);
  7447. /*
  7448. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7449. * here.., and there's no userspace involvement needed for PML.
  7450. */
  7451. return 1;
  7452. }
  7453. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7454. {
  7455. kvm_lapic_expired_hv_timer(vcpu);
  7456. return 1;
  7457. }
  7458. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7459. {
  7460. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7461. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7462. /* Check for memory type validity */
  7463. switch (address & VMX_EPTP_MT_MASK) {
  7464. case VMX_EPTP_MT_UC:
  7465. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7466. return false;
  7467. break;
  7468. case VMX_EPTP_MT_WB:
  7469. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7470. return false;
  7471. break;
  7472. default:
  7473. return false;
  7474. }
  7475. /* only 4 levels page-walk length are valid */
  7476. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7477. return false;
  7478. /* Reserved bits should not be set */
  7479. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7480. return false;
  7481. /* AD, if set, should be supported */
  7482. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7483. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7484. return false;
  7485. }
  7486. return true;
  7487. }
  7488. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7489. struct vmcs12 *vmcs12)
  7490. {
  7491. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7492. u64 address;
  7493. bool accessed_dirty;
  7494. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7495. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7496. !nested_cpu_has_ept(vmcs12))
  7497. return 1;
  7498. if (index >= VMFUNC_EPTP_ENTRIES)
  7499. return 1;
  7500. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7501. &address, index * 8, 8))
  7502. return 1;
  7503. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7504. /*
  7505. * If the (L2) guest does a vmfunc to the currently
  7506. * active ept pointer, we don't have to do anything else
  7507. */
  7508. if (vmcs12->ept_pointer != address) {
  7509. if (!valid_ept_address(vcpu, address))
  7510. return 1;
  7511. kvm_mmu_unload(vcpu);
  7512. mmu->ept_ad = accessed_dirty;
  7513. mmu->base_role.ad_disabled = !accessed_dirty;
  7514. vmcs12->ept_pointer = address;
  7515. /*
  7516. * TODO: Check what's the correct approach in case
  7517. * mmu reload fails. Currently, we just let the next
  7518. * reload potentially fail
  7519. */
  7520. kvm_mmu_reload(vcpu);
  7521. }
  7522. return 0;
  7523. }
  7524. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7525. {
  7526. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7527. struct vmcs12 *vmcs12;
  7528. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7529. /*
  7530. * VMFUNC is only supported for nested guests, but we always enable the
  7531. * secondary control for simplicity; for non-nested mode, fake that we
  7532. * didn't by injecting #UD.
  7533. */
  7534. if (!is_guest_mode(vcpu)) {
  7535. kvm_queue_exception(vcpu, UD_VECTOR);
  7536. return 1;
  7537. }
  7538. vmcs12 = get_vmcs12(vcpu);
  7539. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7540. goto fail;
  7541. switch (function) {
  7542. case 0:
  7543. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7544. goto fail;
  7545. break;
  7546. default:
  7547. goto fail;
  7548. }
  7549. return kvm_skip_emulated_instruction(vcpu);
  7550. fail:
  7551. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7552. vmcs_read32(VM_EXIT_INTR_INFO),
  7553. vmcs_readl(EXIT_QUALIFICATION));
  7554. return 1;
  7555. }
  7556. /*
  7557. * The exit handlers return 1 if the exit was handled fully and guest execution
  7558. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  7559. * to be done to userspace and return 0.
  7560. */
  7561. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  7562. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  7563. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  7564. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  7565. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  7566. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  7567. [EXIT_REASON_CR_ACCESS] = handle_cr,
  7568. [EXIT_REASON_DR_ACCESS] = handle_dr,
  7569. [EXIT_REASON_CPUID] = handle_cpuid,
  7570. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  7571. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  7572. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  7573. [EXIT_REASON_HLT] = handle_halt,
  7574. [EXIT_REASON_INVD] = handle_invd,
  7575. [EXIT_REASON_INVLPG] = handle_invlpg,
  7576. [EXIT_REASON_RDPMC] = handle_rdpmc,
  7577. [EXIT_REASON_VMCALL] = handle_vmcall,
  7578. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  7579. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  7580. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  7581. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  7582. [EXIT_REASON_VMREAD] = handle_vmread,
  7583. [EXIT_REASON_VMRESUME] = handle_vmresume,
  7584. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  7585. [EXIT_REASON_VMOFF] = handle_vmoff,
  7586. [EXIT_REASON_VMON] = handle_vmon,
  7587. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  7588. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  7589. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  7590. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  7591. [EXIT_REASON_WBINVD] = handle_wbinvd,
  7592. [EXIT_REASON_XSETBV] = handle_xsetbv,
  7593. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  7594. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  7595. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  7596. [EXIT_REASON_LDTR_TR] = handle_desc,
  7597. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  7598. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  7599. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  7600. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  7601. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  7602. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  7603. [EXIT_REASON_INVEPT] = handle_invept,
  7604. [EXIT_REASON_INVVPID] = handle_invvpid,
  7605. [EXIT_REASON_RDRAND] = handle_invalid_op,
  7606. [EXIT_REASON_RDSEED] = handle_invalid_op,
  7607. [EXIT_REASON_XSAVES] = handle_xsaves,
  7608. [EXIT_REASON_XRSTORS] = handle_xrstors,
  7609. [EXIT_REASON_PML_FULL] = handle_pml_full,
  7610. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  7611. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  7612. };
  7613. static const int kvm_vmx_max_exit_handlers =
  7614. ARRAY_SIZE(kvm_vmx_exit_handlers);
  7615. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  7616. struct vmcs12 *vmcs12)
  7617. {
  7618. unsigned long exit_qualification;
  7619. gpa_t bitmap, last_bitmap;
  7620. unsigned int port;
  7621. int size;
  7622. u8 b;
  7623. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  7624. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  7625. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7626. port = exit_qualification >> 16;
  7627. size = (exit_qualification & 7) + 1;
  7628. last_bitmap = (gpa_t)-1;
  7629. b = -1;
  7630. while (size > 0) {
  7631. if (port < 0x8000)
  7632. bitmap = vmcs12->io_bitmap_a;
  7633. else if (port < 0x10000)
  7634. bitmap = vmcs12->io_bitmap_b;
  7635. else
  7636. return true;
  7637. bitmap += (port & 0x7fff) / 8;
  7638. if (last_bitmap != bitmap)
  7639. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  7640. return true;
  7641. if (b & (1 << (port & 7)))
  7642. return true;
  7643. port++;
  7644. size--;
  7645. last_bitmap = bitmap;
  7646. }
  7647. return false;
  7648. }
  7649. /*
  7650. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  7651. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  7652. * disinterest in the current event (read or write a specific MSR) by using an
  7653. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  7654. */
  7655. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  7656. struct vmcs12 *vmcs12, u32 exit_reason)
  7657. {
  7658. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  7659. gpa_t bitmap;
  7660. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7661. return true;
  7662. /*
  7663. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  7664. * for the four combinations of read/write and low/high MSR numbers.
  7665. * First we need to figure out which of the four to use:
  7666. */
  7667. bitmap = vmcs12->msr_bitmap;
  7668. if (exit_reason == EXIT_REASON_MSR_WRITE)
  7669. bitmap += 2048;
  7670. if (msr_index >= 0xc0000000) {
  7671. msr_index -= 0xc0000000;
  7672. bitmap += 1024;
  7673. }
  7674. /* Then read the msr_index'th bit from this bitmap: */
  7675. if (msr_index < 1024*8) {
  7676. unsigned char b;
  7677. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  7678. return true;
  7679. return 1 & (b >> (msr_index & 7));
  7680. } else
  7681. return true; /* let L1 handle the wrong parameter */
  7682. }
  7683. /*
  7684. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  7685. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  7686. * intercept (via guest_host_mask etc.) the current event.
  7687. */
  7688. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  7689. struct vmcs12 *vmcs12)
  7690. {
  7691. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7692. int cr = exit_qualification & 15;
  7693. int reg;
  7694. unsigned long val;
  7695. switch ((exit_qualification >> 4) & 3) {
  7696. case 0: /* mov to cr */
  7697. reg = (exit_qualification >> 8) & 15;
  7698. val = kvm_register_readl(vcpu, reg);
  7699. switch (cr) {
  7700. case 0:
  7701. if (vmcs12->cr0_guest_host_mask &
  7702. (val ^ vmcs12->cr0_read_shadow))
  7703. return true;
  7704. break;
  7705. case 3:
  7706. if ((vmcs12->cr3_target_count >= 1 &&
  7707. vmcs12->cr3_target_value0 == val) ||
  7708. (vmcs12->cr3_target_count >= 2 &&
  7709. vmcs12->cr3_target_value1 == val) ||
  7710. (vmcs12->cr3_target_count >= 3 &&
  7711. vmcs12->cr3_target_value2 == val) ||
  7712. (vmcs12->cr3_target_count >= 4 &&
  7713. vmcs12->cr3_target_value3 == val))
  7714. return false;
  7715. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  7716. return true;
  7717. break;
  7718. case 4:
  7719. if (vmcs12->cr4_guest_host_mask &
  7720. (vmcs12->cr4_read_shadow ^ val))
  7721. return true;
  7722. break;
  7723. case 8:
  7724. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  7725. return true;
  7726. break;
  7727. }
  7728. break;
  7729. case 2: /* clts */
  7730. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  7731. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  7732. return true;
  7733. break;
  7734. case 1: /* mov from cr */
  7735. switch (cr) {
  7736. case 3:
  7737. if (vmcs12->cpu_based_vm_exec_control &
  7738. CPU_BASED_CR3_STORE_EXITING)
  7739. return true;
  7740. break;
  7741. case 8:
  7742. if (vmcs12->cpu_based_vm_exec_control &
  7743. CPU_BASED_CR8_STORE_EXITING)
  7744. return true;
  7745. break;
  7746. }
  7747. break;
  7748. case 3: /* lmsw */
  7749. /*
  7750. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  7751. * cr0. Other attempted changes are ignored, with no exit.
  7752. */
  7753. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  7754. if (vmcs12->cr0_guest_host_mask & 0xe &
  7755. (val ^ vmcs12->cr0_read_shadow))
  7756. return true;
  7757. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  7758. !(vmcs12->cr0_read_shadow & 0x1) &&
  7759. (val & 0x1))
  7760. return true;
  7761. break;
  7762. }
  7763. return false;
  7764. }
  7765. /*
  7766. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  7767. * should handle it ourselves in L0 (and then continue L2). Only call this
  7768. * when in is_guest_mode (L2).
  7769. */
  7770. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  7771. {
  7772. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7773. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7774. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7775. if (vmx->nested.nested_run_pending)
  7776. return false;
  7777. if (unlikely(vmx->fail)) {
  7778. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  7779. vmcs_read32(VM_INSTRUCTION_ERROR));
  7780. return true;
  7781. }
  7782. /*
  7783. * The host physical addresses of some pages of guest memory
  7784. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  7785. * Page). The CPU may write to these pages via their host
  7786. * physical address while L2 is running, bypassing any
  7787. * address-translation-based dirty tracking (e.g. EPT write
  7788. * protection).
  7789. *
  7790. * Mark them dirty on every exit from L2 to prevent them from
  7791. * getting out of sync with dirty tracking.
  7792. */
  7793. nested_mark_vmcs12_pages_dirty(vcpu);
  7794. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  7795. vmcs_readl(EXIT_QUALIFICATION),
  7796. vmx->idt_vectoring_info,
  7797. intr_info,
  7798. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7799. KVM_ISA_VMX);
  7800. switch (exit_reason) {
  7801. case EXIT_REASON_EXCEPTION_NMI:
  7802. if (is_nmi(intr_info))
  7803. return false;
  7804. else if (is_page_fault(intr_info))
  7805. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  7806. else if (is_no_device(intr_info) &&
  7807. !(vmcs12->guest_cr0 & X86_CR0_TS))
  7808. return false;
  7809. else if (is_debug(intr_info) &&
  7810. vcpu->guest_debug &
  7811. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  7812. return false;
  7813. else if (is_breakpoint(intr_info) &&
  7814. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  7815. return false;
  7816. return vmcs12->exception_bitmap &
  7817. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  7818. case EXIT_REASON_EXTERNAL_INTERRUPT:
  7819. return false;
  7820. case EXIT_REASON_TRIPLE_FAULT:
  7821. return true;
  7822. case EXIT_REASON_PENDING_INTERRUPT:
  7823. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  7824. case EXIT_REASON_NMI_WINDOW:
  7825. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  7826. case EXIT_REASON_TASK_SWITCH:
  7827. return true;
  7828. case EXIT_REASON_CPUID:
  7829. return true;
  7830. case EXIT_REASON_HLT:
  7831. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  7832. case EXIT_REASON_INVD:
  7833. return true;
  7834. case EXIT_REASON_INVLPG:
  7835. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7836. case EXIT_REASON_RDPMC:
  7837. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  7838. case EXIT_REASON_RDRAND:
  7839. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  7840. case EXIT_REASON_RDSEED:
  7841. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  7842. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  7843. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  7844. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  7845. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  7846. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  7847. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  7848. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  7849. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  7850. /*
  7851. * VMX instructions trap unconditionally. This allows L1 to
  7852. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  7853. */
  7854. return true;
  7855. case EXIT_REASON_CR_ACCESS:
  7856. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  7857. case EXIT_REASON_DR_ACCESS:
  7858. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  7859. case EXIT_REASON_IO_INSTRUCTION:
  7860. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  7861. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  7862. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  7863. case EXIT_REASON_MSR_READ:
  7864. case EXIT_REASON_MSR_WRITE:
  7865. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  7866. case EXIT_REASON_INVALID_STATE:
  7867. return true;
  7868. case EXIT_REASON_MWAIT_INSTRUCTION:
  7869. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  7870. case EXIT_REASON_MONITOR_TRAP_FLAG:
  7871. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  7872. case EXIT_REASON_MONITOR_INSTRUCTION:
  7873. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  7874. case EXIT_REASON_PAUSE_INSTRUCTION:
  7875. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  7876. nested_cpu_has2(vmcs12,
  7877. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  7878. case EXIT_REASON_MCE_DURING_VMENTRY:
  7879. return false;
  7880. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  7881. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  7882. case EXIT_REASON_APIC_ACCESS:
  7883. case EXIT_REASON_APIC_WRITE:
  7884. case EXIT_REASON_EOI_INDUCED:
  7885. /*
  7886. * The controls for "virtualize APIC accesses," "APIC-
  7887. * register virtualization," and "virtual-interrupt
  7888. * delivery" only come from vmcs12.
  7889. */
  7890. return true;
  7891. case EXIT_REASON_EPT_VIOLATION:
  7892. /*
  7893. * L0 always deals with the EPT violation. If nested EPT is
  7894. * used, and the nested mmu code discovers that the address is
  7895. * missing in the guest EPT table (EPT12), the EPT violation
  7896. * will be injected with nested_ept_inject_page_fault()
  7897. */
  7898. return false;
  7899. case EXIT_REASON_EPT_MISCONFIG:
  7900. /*
  7901. * L2 never uses directly L1's EPT, but rather L0's own EPT
  7902. * table (shadow on EPT) or a merged EPT table that L0 built
  7903. * (EPT on EPT). So any problems with the structure of the
  7904. * table is L0's fault.
  7905. */
  7906. return false;
  7907. case EXIT_REASON_INVPCID:
  7908. return
  7909. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  7910. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7911. case EXIT_REASON_WBINVD:
  7912. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  7913. case EXIT_REASON_XSETBV:
  7914. return true;
  7915. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  7916. /*
  7917. * This should never happen, since it is not possible to
  7918. * set XSS to a non-zero value---neither in L1 nor in L2.
  7919. * If if it were, XSS would have to be checked against
  7920. * the XSS exit bitmap in vmcs12.
  7921. */
  7922. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  7923. case EXIT_REASON_PREEMPTION_TIMER:
  7924. return false;
  7925. case EXIT_REASON_PML_FULL:
  7926. /* We emulate PML support to L1. */
  7927. return false;
  7928. case EXIT_REASON_VMFUNC:
  7929. /* VM functions are emulated through L2->L0 vmexits. */
  7930. return false;
  7931. default:
  7932. return true;
  7933. }
  7934. }
  7935. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  7936. {
  7937. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7938. /*
  7939. * At this point, the exit interruption info in exit_intr_info
  7940. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  7941. * we need to query the in-kernel LAPIC.
  7942. */
  7943. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  7944. if ((exit_intr_info &
  7945. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  7946. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  7947. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7948. vmcs12->vm_exit_intr_error_code =
  7949. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  7950. }
  7951. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  7952. vmcs_readl(EXIT_QUALIFICATION));
  7953. return 1;
  7954. }
  7955. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  7956. {
  7957. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  7958. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  7959. }
  7960. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  7961. {
  7962. if (vmx->pml_pg) {
  7963. __free_page(vmx->pml_pg);
  7964. vmx->pml_pg = NULL;
  7965. }
  7966. }
  7967. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  7968. {
  7969. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7970. u64 *pml_buf;
  7971. u16 pml_idx;
  7972. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  7973. /* Do nothing if PML buffer is empty */
  7974. if (pml_idx == (PML_ENTITY_NUM - 1))
  7975. return;
  7976. /* PML index always points to next available PML buffer entity */
  7977. if (pml_idx >= PML_ENTITY_NUM)
  7978. pml_idx = 0;
  7979. else
  7980. pml_idx++;
  7981. pml_buf = page_address(vmx->pml_pg);
  7982. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  7983. u64 gpa;
  7984. gpa = pml_buf[pml_idx];
  7985. WARN_ON(gpa & (PAGE_SIZE - 1));
  7986. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  7987. }
  7988. /* reset PML index */
  7989. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  7990. }
  7991. /*
  7992. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  7993. * Called before reporting dirty_bitmap to userspace.
  7994. */
  7995. static void kvm_flush_pml_buffers(struct kvm *kvm)
  7996. {
  7997. int i;
  7998. struct kvm_vcpu *vcpu;
  7999. /*
  8000. * We only need to kick vcpu out of guest mode here, as PML buffer
  8001. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8002. * vcpus running in guest are possible to have unflushed GPAs in PML
  8003. * buffer.
  8004. */
  8005. kvm_for_each_vcpu(i, vcpu, kvm)
  8006. kvm_vcpu_kick(vcpu);
  8007. }
  8008. static void vmx_dump_sel(char *name, uint32_t sel)
  8009. {
  8010. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8011. name, vmcs_read16(sel),
  8012. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8013. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8014. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8015. }
  8016. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8017. {
  8018. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8019. name, vmcs_read32(limit),
  8020. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8021. }
  8022. static void dump_vmcs(void)
  8023. {
  8024. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8025. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8026. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8027. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8028. u32 secondary_exec_control = 0;
  8029. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8030. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8031. int i, n;
  8032. if (cpu_has_secondary_exec_ctrls())
  8033. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8034. pr_err("*** Guest State ***\n");
  8035. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8036. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8037. vmcs_readl(CR0_GUEST_HOST_MASK));
  8038. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8039. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8040. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8041. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8042. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8043. {
  8044. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8045. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8046. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8047. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8048. }
  8049. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8050. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8051. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8052. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8053. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8054. vmcs_readl(GUEST_SYSENTER_ESP),
  8055. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8056. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8057. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8058. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8059. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8060. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8061. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8062. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8063. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8064. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8065. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8066. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8067. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8068. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8069. efer, vmcs_read64(GUEST_IA32_PAT));
  8070. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8071. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8072. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8073. if (cpu_has_load_perf_global_ctrl &&
  8074. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8075. pr_err("PerfGlobCtl = 0x%016llx\n",
  8076. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8077. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8078. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8079. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8080. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8081. vmcs_read32(GUEST_ACTIVITY_STATE));
  8082. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8083. pr_err("InterruptStatus = %04x\n",
  8084. vmcs_read16(GUEST_INTR_STATUS));
  8085. pr_err("*** Host State ***\n");
  8086. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8087. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8088. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8089. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8090. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8091. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8092. vmcs_read16(HOST_TR_SELECTOR));
  8093. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8094. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8095. vmcs_readl(HOST_TR_BASE));
  8096. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8097. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8098. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8099. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8100. vmcs_readl(HOST_CR4));
  8101. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8102. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8103. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8104. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8105. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8106. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8107. vmcs_read64(HOST_IA32_EFER),
  8108. vmcs_read64(HOST_IA32_PAT));
  8109. if (cpu_has_load_perf_global_ctrl &&
  8110. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8111. pr_err("PerfGlobCtl = 0x%016llx\n",
  8112. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8113. pr_err("*** Control State ***\n");
  8114. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8115. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8116. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8117. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8118. vmcs_read32(EXCEPTION_BITMAP),
  8119. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8120. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8121. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8122. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8123. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8124. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8125. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8126. vmcs_read32(VM_EXIT_INTR_INFO),
  8127. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8128. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8129. pr_err(" reason=%08x qualification=%016lx\n",
  8130. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8131. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8132. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8133. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8134. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8135. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8136. pr_err("TSC Multiplier = 0x%016llx\n",
  8137. vmcs_read64(TSC_MULTIPLIER));
  8138. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8139. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8140. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8141. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8142. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8143. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8144. n = vmcs_read32(CR3_TARGET_COUNT);
  8145. for (i = 0; i + 1 < n; i += 4)
  8146. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8147. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8148. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8149. if (i < n)
  8150. pr_err("CR3 target%u=%016lx\n",
  8151. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8152. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8153. pr_err("PLE Gap=%08x Window=%08x\n",
  8154. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8155. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8156. pr_err("Virtual processor ID = 0x%04x\n",
  8157. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8158. }
  8159. /*
  8160. * The guest has exited. See if we can fix it or if we need userspace
  8161. * assistance.
  8162. */
  8163. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8164. {
  8165. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8166. u32 exit_reason = vmx->exit_reason;
  8167. u32 vectoring_info = vmx->idt_vectoring_info;
  8168. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8169. /*
  8170. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8171. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8172. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8173. * mode as if vcpus is in root mode, the PML buffer must has been
  8174. * flushed already.
  8175. */
  8176. if (enable_pml)
  8177. vmx_flush_pml_buffer(vcpu);
  8178. /* If guest state is invalid, start emulating */
  8179. if (vmx->emulation_required)
  8180. return handle_invalid_guest_state(vcpu);
  8181. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8182. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8183. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8184. dump_vmcs();
  8185. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8186. vcpu->run->fail_entry.hardware_entry_failure_reason
  8187. = exit_reason;
  8188. return 0;
  8189. }
  8190. if (unlikely(vmx->fail)) {
  8191. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8192. vcpu->run->fail_entry.hardware_entry_failure_reason
  8193. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8194. return 0;
  8195. }
  8196. /*
  8197. * Note:
  8198. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8199. * delivery event since it indicates guest is accessing MMIO.
  8200. * The vm-exit can be triggered again after return to guest that
  8201. * will cause infinite loop.
  8202. */
  8203. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8204. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8205. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8206. exit_reason != EXIT_REASON_PML_FULL &&
  8207. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8208. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8209. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8210. vcpu->run->internal.ndata = 3;
  8211. vcpu->run->internal.data[0] = vectoring_info;
  8212. vcpu->run->internal.data[1] = exit_reason;
  8213. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8214. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8215. vcpu->run->internal.ndata++;
  8216. vcpu->run->internal.data[3] =
  8217. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8218. }
  8219. return 0;
  8220. }
  8221. if (unlikely(!enable_vnmi &&
  8222. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8223. if (vmx_interrupt_allowed(vcpu)) {
  8224. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8225. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8226. vcpu->arch.nmi_pending) {
  8227. /*
  8228. * This CPU don't support us in finding the end of an
  8229. * NMI-blocked window if the guest runs with IRQs
  8230. * disabled. So we pull the trigger after 1 s of
  8231. * futile waiting, but inform the user about this.
  8232. */
  8233. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8234. "state on VCPU %d after 1 s timeout\n",
  8235. __func__, vcpu->vcpu_id);
  8236. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8237. }
  8238. }
  8239. if (exit_reason < kvm_vmx_max_exit_handlers
  8240. && kvm_vmx_exit_handlers[exit_reason])
  8241. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8242. else {
  8243. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8244. exit_reason);
  8245. kvm_queue_exception(vcpu, UD_VECTOR);
  8246. return 1;
  8247. }
  8248. }
  8249. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8250. {
  8251. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8252. if (is_guest_mode(vcpu) &&
  8253. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8254. return;
  8255. if (irr == -1 || tpr < irr) {
  8256. vmcs_write32(TPR_THRESHOLD, 0);
  8257. return;
  8258. }
  8259. vmcs_write32(TPR_THRESHOLD, irr);
  8260. }
  8261. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8262. {
  8263. u32 sec_exec_control;
  8264. if (!lapic_in_kernel(vcpu))
  8265. return;
  8266. /* Postpone execution until vmcs01 is the current VMCS. */
  8267. if (is_guest_mode(vcpu)) {
  8268. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8269. return;
  8270. }
  8271. if (!cpu_need_tpr_shadow(vcpu))
  8272. return;
  8273. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8274. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8275. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8276. switch (kvm_get_apic_mode(vcpu)) {
  8277. case LAPIC_MODE_INVALID:
  8278. WARN_ONCE(true, "Invalid local APIC state");
  8279. case LAPIC_MODE_DISABLED:
  8280. break;
  8281. case LAPIC_MODE_XAPIC:
  8282. if (flexpriority_enabled) {
  8283. sec_exec_control |=
  8284. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8285. vmx_flush_tlb(vcpu, true);
  8286. }
  8287. break;
  8288. case LAPIC_MODE_X2APIC:
  8289. if (cpu_has_vmx_virtualize_x2apic_mode())
  8290. sec_exec_control |=
  8291. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8292. break;
  8293. }
  8294. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8295. vmx_update_msr_bitmap(vcpu);
  8296. }
  8297. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8298. {
  8299. if (!is_guest_mode(vcpu)) {
  8300. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8301. vmx_flush_tlb(vcpu, true);
  8302. }
  8303. }
  8304. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8305. {
  8306. u16 status;
  8307. u8 old;
  8308. if (max_isr == -1)
  8309. max_isr = 0;
  8310. status = vmcs_read16(GUEST_INTR_STATUS);
  8311. old = status >> 8;
  8312. if (max_isr != old) {
  8313. status &= 0xff;
  8314. status |= max_isr << 8;
  8315. vmcs_write16(GUEST_INTR_STATUS, status);
  8316. }
  8317. }
  8318. static void vmx_set_rvi(int vector)
  8319. {
  8320. u16 status;
  8321. u8 old;
  8322. if (vector == -1)
  8323. vector = 0;
  8324. status = vmcs_read16(GUEST_INTR_STATUS);
  8325. old = (u8)status & 0xff;
  8326. if ((u8)vector != old) {
  8327. status &= ~0xff;
  8328. status |= (u8)vector;
  8329. vmcs_write16(GUEST_INTR_STATUS, status);
  8330. }
  8331. }
  8332. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8333. {
  8334. /*
  8335. * When running L2, updating RVI is only relevant when
  8336. * vmcs12 virtual-interrupt-delivery enabled.
  8337. * However, it can be enabled only when L1 also
  8338. * intercepts external-interrupts and in that case
  8339. * we should not update vmcs02 RVI but instead intercept
  8340. * interrupt. Therefore, do nothing when running L2.
  8341. */
  8342. if (!is_guest_mode(vcpu))
  8343. vmx_set_rvi(max_irr);
  8344. }
  8345. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8346. {
  8347. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8348. int max_irr;
  8349. bool max_irr_updated;
  8350. WARN_ON(!vcpu->arch.apicv_active);
  8351. if (pi_test_on(&vmx->pi_desc)) {
  8352. pi_clear_on(&vmx->pi_desc);
  8353. /*
  8354. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8355. * But on x86 this is just a compiler barrier anyway.
  8356. */
  8357. smp_mb__after_atomic();
  8358. max_irr_updated =
  8359. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8360. /*
  8361. * If we are running L2 and L1 has a new pending interrupt
  8362. * which can be injected, we should re-evaluate
  8363. * what should be done with this new L1 interrupt.
  8364. * If L1 intercepts external-interrupts, we should
  8365. * exit from L2 to L1. Otherwise, interrupt should be
  8366. * delivered directly to L2.
  8367. */
  8368. if (is_guest_mode(vcpu) && max_irr_updated) {
  8369. if (nested_exit_on_intr(vcpu))
  8370. kvm_vcpu_exiting_guest_mode(vcpu);
  8371. else
  8372. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8373. }
  8374. } else {
  8375. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8376. }
  8377. vmx_hwapic_irr_update(vcpu, max_irr);
  8378. return max_irr;
  8379. }
  8380. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8381. {
  8382. if (!kvm_vcpu_apicv_active(vcpu))
  8383. return;
  8384. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8385. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8386. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8387. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8388. }
  8389. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8390. {
  8391. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8392. pi_clear_on(&vmx->pi_desc);
  8393. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  8394. }
  8395. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  8396. {
  8397. u32 exit_intr_info = 0;
  8398. u16 basic_exit_reason = (u16)vmx->exit_reason;
  8399. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  8400. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  8401. return;
  8402. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8403. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8404. vmx->exit_intr_info = exit_intr_info;
  8405. /* if exit due to PF check for async PF */
  8406. if (is_page_fault(exit_intr_info))
  8407. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  8408. /* Handle machine checks before interrupts are enabled */
  8409. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  8410. is_machine_check(exit_intr_info))
  8411. kvm_machine_check();
  8412. /* We need to handle NMIs before interrupts are enabled */
  8413. if (is_nmi(exit_intr_info)) {
  8414. kvm_before_interrupt(&vmx->vcpu);
  8415. asm("int $2");
  8416. kvm_after_interrupt(&vmx->vcpu);
  8417. }
  8418. }
  8419. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  8420. {
  8421. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8422. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  8423. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  8424. unsigned int vector;
  8425. unsigned long entry;
  8426. gate_desc *desc;
  8427. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8428. #ifdef CONFIG_X86_64
  8429. unsigned long tmp;
  8430. #endif
  8431. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8432. desc = (gate_desc *)vmx->host_idt_base + vector;
  8433. entry = gate_offset(desc);
  8434. asm volatile(
  8435. #ifdef CONFIG_X86_64
  8436. "mov %%" _ASM_SP ", %[sp]\n\t"
  8437. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  8438. "push $%c[ss]\n\t"
  8439. "push %[sp]\n\t"
  8440. #endif
  8441. "pushf\n\t"
  8442. __ASM_SIZE(push) " $%c[cs]\n\t"
  8443. CALL_NOSPEC
  8444. :
  8445. #ifdef CONFIG_X86_64
  8446. [sp]"=&r"(tmp),
  8447. #endif
  8448. ASM_CALL_CONSTRAINT
  8449. :
  8450. THUNK_TARGET(entry),
  8451. [ss]"i"(__KERNEL_DS),
  8452. [cs]"i"(__KERNEL_CS)
  8453. );
  8454. }
  8455. }
  8456. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  8457. static bool vmx_has_emulated_msr(int index)
  8458. {
  8459. switch (index) {
  8460. case MSR_IA32_SMBASE:
  8461. /*
  8462. * We cannot do SMM unless we can run the guest in big
  8463. * real mode.
  8464. */
  8465. return enable_unrestricted_guest || emulate_invalid_guest_state;
  8466. case MSR_AMD64_VIRT_SPEC_CTRL:
  8467. /* This is AMD only. */
  8468. return false;
  8469. default:
  8470. return true;
  8471. }
  8472. }
  8473. static bool vmx_mpx_supported(void)
  8474. {
  8475. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  8476. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  8477. }
  8478. static bool vmx_xsaves_supported(void)
  8479. {
  8480. return vmcs_config.cpu_based_2nd_exec_ctrl &
  8481. SECONDARY_EXEC_XSAVES;
  8482. }
  8483. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  8484. {
  8485. u32 exit_intr_info;
  8486. bool unblock_nmi;
  8487. u8 vector;
  8488. bool idtv_info_valid;
  8489. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8490. if (enable_vnmi) {
  8491. if (vmx->loaded_vmcs->nmi_known_unmasked)
  8492. return;
  8493. /*
  8494. * Can't use vmx->exit_intr_info since we're not sure what
  8495. * the exit reason is.
  8496. */
  8497. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8498. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  8499. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8500. /*
  8501. * SDM 3: 27.7.1.2 (September 2008)
  8502. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  8503. * a guest IRET fault.
  8504. * SDM 3: 23.2.2 (September 2008)
  8505. * Bit 12 is undefined in any of the following cases:
  8506. * If the VM exit sets the valid bit in the IDT-vectoring
  8507. * information field.
  8508. * If the VM exit is due to a double fault.
  8509. */
  8510. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  8511. vector != DF_VECTOR && !idtv_info_valid)
  8512. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8513. GUEST_INTR_STATE_NMI);
  8514. else
  8515. vmx->loaded_vmcs->nmi_known_unmasked =
  8516. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  8517. & GUEST_INTR_STATE_NMI);
  8518. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  8519. vmx->loaded_vmcs->vnmi_blocked_time +=
  8520. ktime_to_ns(ktime_sub(ktime_get(),
  8521. vmx->loaded_vmcs->entry_time));
  8522. }
  8523. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  8524. u32 idt_vectoring_info,
  8525. int instr_len_field,
  8526. int error_code_field)
  8527. {
  8528. u8 vector;
  8529. int type;
  8530. bool idtv_info_valid;
  8531. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8532. vcpu->arch.nmi_injected = false;
  8533. kvm_clear_exception_queue(vcpu);
  8534. kvm_clear_interrupt_queue(vcpu);
  8535. if (!idtv_info_valid)
  8536. return;
  8537. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8538. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  8539. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  8540. switch (type) {
  8541. case INTR_TYPE_NMI_INTR:
  8542. vcpu->arch.nmi_injected = true;
  8543. /*
  8544. * SDM 3: 27.7.1.2 (September 2008)
  8545. * Clear bit "block by NMI" before VM entry if a NMI
  8546. * delivery faulted.
  8547. */
  8548. vmx_set_nmi_mask(vcpu, false);
  8549. break;
  8550. case INTR_TYPE_SOFT_EXCEPTION:
  8551. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8552. /* fall through */
  8553. case INTR_TYPE_HARD_EXCEPTION:
  8554. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  8555. u32 err = vmcs_read32(error_code_field);
  8556. kvm_requeue_exception_e(vcpu, vector, err);
  8557. } else
  8558. kvm_requeue_exception(vcpu, vector);
  8559. break;
  8560. case INTR_TYPE_SOFT_INTR:
  8561. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8562. /* fall through */
  8563. case INTR_TYPE_EXT_INTR:
  8564. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  8565. break;
  8566. default:
  8567. break;
  8568. }
  8569. }
  8570. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  8571. {
  8572. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  8573. VM_EXIT_INSTRUCTION_LEN,
  8574. IDT_VECTORING_ERROR_CODE);
  8575. }
  8576. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  8577. {
  8578. __vmx_complete_interrupts(vcpu,
  8579. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8580. VM_ENTRY_INSTRUCTION_LEN,
  8581. VM_ENTRY_EXCEPTION_ERROR_CODE);
  8582. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  8583. }
  8584. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  8585. {
  8586. int i, nr_msrs;
  8587. struct perf_guest_switch_msr *msrs;
  8588. msrs = perf_guest_get_msrs(&nr_msrs);
  8589. if (!msrs)
  8590. return;
  8591. for (i = 0; i < nr_msrs; i++)
  8592. if (msrs[i].host == msrs[i].guest)
  8593. clear_atomic_switch_msr(vmx, msrs[i].msr);
  8594. else
  8595. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  8596. msrs[i].host);
  8597. }
  8598. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  8599. {
  8600. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8601. u64 tscl;
  8602. u32 delta_tsc;
  8603. if (vmx->hv_deadline_tsc == -1)
  8604. return;
  8605. tscl = rdtsc();
  8606. if (vmx->hv_deadline_tsc > tscl)
  8607. /* sure to be 32 bit only because checked on set_hv_timer */
  8608. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  8609. cpu_preemption_timer_multi);
  8610. else
  8611. delta_tsc = 0;
  8612. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  8613. }
  8614. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  8615. {
  8616. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8617. unsigned long cr3, cr4, evmcs_rsp;
  8618. /* Record the guest's net vcpu time for enforced NMI injections. */
  8619. if (unlikely(!enable_vnmi &&
  8620. vmx->loaded_vmcs->soft_vnmi_blocked))
  8621. vmx->loaded_vmcs->entry_time = ktime_get();
  8622. /* Don't enter VMX if guest state is invalid, let the exit handler
  8623. start emulation until we arrive back to a valid state */
  8624. if (vmx->emulation_required)
  8625. return;
  8626. if (vmx->ple_window_dirty) {
  8627. vmx->ple_window_dirty = false;
  8628. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  8629. }
  8630. if (vmx->nested.sync_shadow_vmcs) {
  8631. copy_vmcs12_to_shadow(vmx);
  8632. vmx->nested.sync_shadow_vmcs = false;
  8633. }
  8634. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  8635. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  8636. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  8637. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  8638. cr3 = __get_current_cr3_fast();
  8639. if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
  8640. vmcs_writel(HOST_CR3, cr3);
  8641. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  8642. }
  8643. cr4 = cr4_read_shadow();
  8644. if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
  8645. vmcs_writel(HOST_CR4, cr4);
  8646. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  8647. }
  8648. /* When single-stepping over STI and MOV SS, we must clear the
  8649. * corresponding interruptibility bits in the guest state. Otherwise
  8650. * vmentry fails as it then expects bit 14 (BS) in pending debug
  8651. * exceptions being set, but that's not correct for the guest debugging
  8652. * case. */
  8653. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  8654. vmx_set_interrupt_shadow(vcpu, 0);
  8655. if (static_cpu_has(X86_FEATURE_PKU) &&
  8656. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  8657. vcpu->arch.pkru != vmx->host_pkru)
  8658. __write_pkru(vcpu->arch.pkru);
  8659. atomic_switch_perf_msrs(vmx);
  8660. vmx_arm_hv_timer(vcpu);
  8661. /*
  8662. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  8663. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  8664. * is no need to worry about the conditional branch over the wrmsr
  8665. * being speculatively taken.
  8666. */
  8667. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  8668. vmx->__launched = vmx->loaded_vmcs->launched;
  8669. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  8670. (unsigned long)&current_evmcs->host_rsp : 0;
  8671. asm(
  8672. /* Store host registers */
  8673. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  8674. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  8675. "push %%" _ASM_CX " \n\t"
  8676. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8677. "je 1f \n\t"
  8678. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8679. /* Avoid VMWRITE when Enlightened VMCS is in use */
  8680. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  8681. "jz 2f \n\t"
  8682. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  8683. "jmp 1f \n\t"
  8684. "2: \n\t"
  8685. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  8686. "1: \n\t"
  8687. /* Reload cr2 if changed */
  8688. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  8689. "mov %%cr2, %%" _ASM_DX " \n\t"
  8690. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  8691. "je 3f \n\t"
  8692. "mov %%" _ASM_AX", %%cr2 \n\t"
  8693. "3: \n\t"
  8694. /* Check if vmlaunch of vmresume is needed */
  8695. "cmpl $0, %c[launched](%0) \n\t"
  8696. /* Load guest registers. Don't clobber flags. */
  8697. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  8698. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  8699. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  8700. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  8701. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  8702. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  8703. #ifdef CONFIG_X86_64
  8704. "mov %c[r8](%0), %%r8 \n\t"
  8705. "mov %c[r9](%0), %%r9 \n\t"
  8706. "mov %c[r10](%0), %%r10 \n\t"
  8707. "mov %c[r11](%0), %%r11 \n\t"
  8708. "mov %c[r12](%0), %%r12 \n\t"
  8709. "mov %c[r13](%0), %%r13 \n\t"
  8710. "mov %c[r14](%0), %%r14 \n\t"
  8711. "mov %c[r15](%0), %%r15 \n\t"
  8712. #endif
  8713. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  8714. /* Enter guest mode */
  8715. "jne 1f \n\t"
  8716. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  8717. "jmp 2f \n\t"
  8718. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  8719. "2: "
  8720. /* Save guest registers, load host registers, keep flags */
  8721. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  8722. "pop %0 \n\t"
  8723. "setbe %c[fail](%0)\n\t"
  8724. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  8725. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  8726. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  8727. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  8728. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  8729. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  8730. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  8731. #ifdef CONFIG_X86_64
  8732. "mov %%r8, %c[r8](%0) \n\t"
  8733. "mov %%r9, %c[r9](%0) \n\t"
  8734. "mov %%r10, %c[r10](%0) \n\t"
  8735. "mov %%r11, %c[r11](%0) \n\t"
  8736. "mov %%r12, %c[r12](%0) \n\t"
  8737. "mov %%r13, %c[r13](%0) \n\t"
  8738. "mov %%r14, %c[r14](%0) \n\t"
  8739. "mov %%r15, %c[r15](%0) \n\t"
  8740. "xor %%r8d, %%r8d \n\t"
  8741. "xor %%r9d, %%r9d \n\t"
  8742. "xor %%r10d, %%r10d \n\t"
  8743. "xor %%r11d, %%r11d \n\t"
  8744. "xor %%r12d, %%r12d \n\t"
  8745. "xor %%r13d, %%r13d \n\t"
  8746. "xor %%r14d, %%r14d \n\t"
  8747. "xor %%r15d, %%r15d \n\t"
  8748. #endif
  8749. "mov %%cr2, %%" _ASM_AX " \n\t"
  8750. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  8751. "xor %%eax, %%eax \n\t"
  8752. "xor %%ebx, %%ebx \n\t"
  8753. "xor %%esi, %%esi \n\t"
  8754. "xor %%edi, %%edi \n\t"
  8755. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  8756. ".pushsection .rodata \n\t"
  8757. ".global vmx_return \n\t"
  8758. "vmx_return: " _ASM_PTR " 2b \n\t"
  8759. ".popsection"
  8760. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  8761. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  8762. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  8763. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  8764. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  8765. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  8766. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  8767. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  8768. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  8769. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  8770. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  8771. #ifdef CONFIG_X86_64
  8772. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  8773. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  8774. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  8775. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  8776. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  8777. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  8778. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  8779. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  8780. #endif
  8781. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  8782. [wordsize]"i"(sizeof(ulong))
  8783. : "cc", "memory"
  8784. #ifdef CONFIG_X86_64
  8785. , "rax", "rbx", "rdi"
  8786. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  8787. #else
  8788. , "eax", "ebx", "edi"
  8789. #endif
  8790. );
  8791. /*
  8792. * We do not use IBRS in the kernel. If this vCPU has used the
  8793. * SPEC_CTRL MSR it may have left it on; save the value and
  8794. * turn it off. This is much more efficient than blindly adding
  8795. * it to the atomic save/restore list. Especially as the former
  8796. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  8797. *
  8798. * For non-nested case:
  8799. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  8800. * save it.
  8801. *
  8802. * For nested case:
  8803. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  8804. * save it.
  8805. */
  8806. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  8807. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  8808. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  8809. /* Eliminate branch target predictions from guest mode */
  8810. vmexit_fill_RSB();
  8811. /* All fields are clean at this point */
  8812. if (static_branch_unlikely(&enable_evmcs))
  8813. current_evmcs->hv_clean_fields |=
  8814. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  8815. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  8816. if (vmx->host_debugctlmsr)
  8817. update_debugctlmsr(vmx->host_debugctlmsr);
  8818. #ifndef CONFIG_X86_64
  8819. /*
  8820. * The sysexit path does not restore ds/es, so we must set them to
  8821. * a reasonable value ourselves.
  8822. *
  8823. * We can't defer this to vmx_load_host_state() since that function
  8824. * may be executed in interrupt context, which saves and restore segments
  8825. * around it, nullifying its effect.
  8826. */
  8827. loadsegment(ds, __USER_DS);
  8828. loadsegment(es, __USER_DS);
  8829. #endif
  8830. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  8831. | (1 << VCPU_EXREG_RFLAGS)
  8832. | (1 << VCPU_EXREG_PDPTR)
  8833. | (1 << VCPU_EXREG_SEGMENTS)
  8834. | (1 << VCPU_EXREG_CR3));
  8835. vcpu->arch.regs_dirty = 0;
  8836. /*
  8837. * eager fpu is enabled if PKEY is supported and CR4 is switched
  8838. * back on host, so it is safe to read guest PKRU from current
  8839. * XSAVE.
  8840. */
  8841. if (static_cpu_has(X86_FEATURE_PKU) &&
  8842. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  8843. vcpu->arch.pkru = __read_pkru();
  8844. if (vcpu->arch.pkru != vmx->host_pkru)
  8845. __write_pkru(vmx->host_pkru);
  8846. }
  8847. vmx->nested.nested_run_pending = 0;
  8848. vmx->idt_vectoring_info = 0;
  8849. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  8850. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8851. return;
  8852. vmx->loaded_vmcs->launched = 1;
  8853. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  8854. vmx_complete_atomic_exit(vmx);
  8855. vmx_recover_nmi_blocking(vmx);
  8856. vmx_complete_interrupts(vmx);
  8857. }
  8858. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  8859. static struct kvm *vmx_vm_alloc(void)
  8860. {
  8861. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  8862. return &kvm_vmx->kvm;
  8863. }
  8864. static void vmx_vm_free(struct kvm *kvm)
  8865. {
  8866. vfree(to_kvm_vmx(kvm));
  8867. }
  8868. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  8869. {
  8870. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8871. int cpu;
  8872. if (vmx->loaded_vmcs == vmcs)
  8873. return;
  8874. cpu = get_cpu();
  8875. vmx->loaded_vmcs = vmcs;
  8876. vmx_vcpu_put(vcpu);
  8877. vmx_vcpu_load(vcpu, cpu);
  8878. put_cpu();
  8879. }
  8880. /*
  8881. * Ensure that the current vmcs of the logical processor is the
  8882. * vmcs01 of the vcpu before calling free_nested().
  8883. */
  8884. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  8885. {
  8886. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8887. vcpu_load(vcpu);
  8888. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  8889. free_nested(vmx);
  8890. vcpu_put(vcpu);
  8891. }
  8892. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  8893. {
  8894. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8895. if (enable_pml)
  8896. vmx_destroy_pml_buffer(vmx);
  8897. free_vpid(vmx->vpid);
  8898. leave_guest_mode(vcpu);
  8899. vmx_free_vcpu_nested(vcpu);
  8900. free_loaded_vmcs(vmx->loaded_vmcs);
  8901. kfree(vmx->guest_msrs);
  8902. kvm_vcpu_uninit(vcpu);
  8903. kmem_cache_free(kvm_vcpu_cache, vmx);
  8904. }
  8905. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  8906. {
  8907. int err;
  8908. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  8909. unsigned long *msr_bitmap;
  8910. int cpu;
  8911. if (!vmx)
  8912. return ERR_PTR(-ENOMEM);
  8913. vmx->vpid = allocate_vpid();
  8914. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  8915. if (err)
  8916. goto free_vcpu;
  8917. err = -ENOMEM;
  8918. /*
  8919. * If PML is turned on, failure on enabling PML just results in failure
  8920. * of creating the vcpu, therefore we can simplify PML logic (by
  8921. * avoiding dealing with cases, such as enabling PML partially on vcpus
  8922. * for the guest, etc.
  8923. */
  8924. if (enable_pml) {
  8925. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  8926. if (!vmx->pml_pg)
  8927. goto uninit_vcpu;
  8928. }
  8929. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  8930. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  8931. > PAGE_SIZE);
  8932. if (!vmx->guest_msrs)
  8933. goto free_pml;
  8934. err = alloc_loaded_vmcs(&vmx->vmcs01);
  8935. if (err < 0)
  8936. goto free_msrs;
  8937. msr_bitmap = vmx->vmcs01.msr_bitmap;
  8938. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  8939. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  8940. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  8941. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  8942. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  8943. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  8944. vmx->msr_bitmap_mode = 0;
  8945. vmx->loaded_vmcs = &vmx->vmcs01;
  8946. cpu = get_cpu();
  8947. vmx_vcpu_load(&vmx->vcpu, cpu);
  8948. vmx->vcpu.cpu = cpu;
  8949. vmx_vcpu_setup(vmx);
  8950. vmx_vcpu_put(&vmx->vcpu);
  8951. put_cpu();
  8952. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8953. err = alloc_apic_access_page(kvm);
  8954. if (err)
  8955. goto free_vmcs;
  8956. }
  8957. if (enable_ept && !enable_unrestricted_guest) {
  8958. err = init_rmode_identity_map(kvm);
  8959. if (err)
  8960. goto free_vmcs;
  8961. }
  8962. if (nested) {
  8963. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  8964. kvm_vcpu_apicv_active(&vmx->vcpu));
  8965. vmx->nested.vpid02 = allocate_vpid();
  8966. }
  8967. vmx->nested.posted_intr_nv = -1;
  8968. vmx->nested.current_vmptr = -1ull;
  8969. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  8970. /*
  8971. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  8972. * or POSTED_INTR_WAKEUP_VECTOR.
  8973. */
  8974. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  8975. vmx->pi_desc.sn = 1;
  8976. return &vmx->vcpu;
  8977. free_vmcs:
  8978. free_vpid(vmx->nested.vpid02);
  8979. free_loaded_vmcs(vmx->loaded_vmcs);
  8980. free_msrs:
  8981. kfree(vmx->guest_msrs);
  8982. free_pml:
  8983. vmx_destroy_pml_buffer(vmx);
  8984. uninit_vcpu:
  8985. kvm_vcpu_uninit(&vmx->vcpu);
  8986. free_vcpu:
  8987. free_vpid(vmx->vpid);
  8988. kmem_cache_free(kvm_vcpu_cache, vmx);
  8989. return ERR_PTR(err);
  8990. }
  8991. static int vmx_vm_init(struct kvm *kvm)
  8992. {
  8993. if (!ple_gap)
  8994. kvm->arch.pause_in_guest = true;
  8995. return 0;
  8996. }
  8997. static void __init vmx_check_processor_compat(void *rtn)
  8998. {
  8999. struct vmcs_config vmcs_conf;
  9000. *(int *)rtn = 0;
  9001. if (setup_vmcs_config(&vmcs_conf) < 0)
  9002. *(int *)rtn = -EIO;
  9003. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  9004. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  9005. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  9006. smp_processor_id());
  9007. *(int *)rtn = -EIO;
  9008. }
  9009. }
  9010. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  9011. {
  9012. u8 cache;
  9013. u64 ipat = 0;
  9014. /* For VT-d and EPT combination
  9015. * 1. MMIO: always map as UC
  9016. * 2. EPT with VT-d:
  9017. * a. VT-d without snooping control feature: can't guarantee the
  9018. * result, try to trust guest.
  9019. * b. VT-d with snooping control feature: snooping control feature of
  9020. * VT-d engine can guarantee the cache correctness. Just set it
  9021. * to WB to keep consistent with host. So the same as item 3.
  9022. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9023. * consistent with host MTRR
  9024. */
  9025. if (is_mmio) {
  9026. cache = MTRR_TYPE_UNCACHABLE;
  9027. goto exit;
  9028. }
  9029. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9030. ipat = VMX_EPT_IPAT_BIT;
  9031. cache = MTRR_TYPE_WRBACK;
  9032. goto exit;
  9033. }
  9034. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9035. ipat = VMX_EPT_IPAT_BIT;
  9036. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9037. cache = MTRR_TYPE_WRBACK;
  9038. else
  9039. cache = MTRR_TYPE_UNCACHABLE;
  9040. goto exit;
  9041. }
  9042. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9043. exit:
  9044. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9045. }
  9046. static int vmx_get_lpage_level(void)
  9047. {
  9048. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9049. return PT_DIRECTORY_LEVEL;
  9050. else
  9051. /* For shadow and EPT supported 1GB page */
  9052. return PT_PDPE_LEVEL;
  9053. }
  9054. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9055. {
  9056. /*
  9057. * These bits in the secondary execution controls field
  9058. * are dynamic, the others are mostly based on the hypervisor
  9059. * architecture and the guest's CPUID. Do not touch the
  9060. * dynamic bits.
  9061. */
  9062. u32 mask =
  9063. SECONDARY_EXEC_SHADOW_VMCS |
  9064. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9065. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9066. SECONDARY_EXEC_DESC;
  9067. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9068. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9069. (new_ctl & ~mask) | (cur_ctl & mask));
  9070. }
  9071. /*
  9072. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9073. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9074. */
  9075. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9076. {
  9077. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9078. struct kvm_cpuid_entry2 *entry;
  9079. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9080. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9081. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9082. if (entry && (entry->_reg & (_cpuid_mask))) \
  9083. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9084. } while (0)
  9085. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9086. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9087. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9088. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9089. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9090. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9091. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9092. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9093. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9094. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9095. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9096. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9097. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9098. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9099. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9100. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9101. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9102. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9103. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9104. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9105. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9106. #undef cr4_fixed1_update
  9107. }
  9108. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9109. {
  9110. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9111. if (cpu_has_secondary_exec_ctrls()) {
  9112. vmx_compute_secondary_exec_control(vmx);
  9113. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9114. }
  9115. if (nested_vmx_allowed(vcpu))
  9116. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9117. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9118. else
  9119. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9120. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9121. if (nested_vmx_allowed(vcpu))
  9122. nested_vmx_cr_fixed1_bits_update(vcpu);
  9123. }
  9124. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9125. {
  9126. if (func == 1 && nested)
  9127. entry->ecx |= bit(X86_FEATURE_VMX);
  9128. }
  9129. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9130. struct x86_exception *fault)
  9131. {
  9132. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9133. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9134. u32 exit_reason;
  9135. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9136. if (vmx->nested.pml_full) {
  9137. exit_reason = EXIT_REASON_PML_FULL;
  9138. vmx->nested.pml_full = false;
  9139. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9140. } else if (fault->error_code & PFERR_RSVD_MASK)
  9141. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9142. else
  9143. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9144. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9145. vmcs12->guest_physical_address = fault->address;
  9146. }
  9147. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9148. {
  9149. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9150. }
  9151. /* Callbacks for nested_ept_init_mmu_context: */
  9152. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9153. {
  9154. /* return the page table to be shadowed - in our case, EPT12 */
  9155. return get_vmcs12(vcpu)->ept_pointer;
  9156. }
  9157. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9158. {
  9159. WARN_ON(mmu_is_nested(vcpu));
  9160. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9161. return 1;
  9162. kvm_mmu_unload(vcpu);
  9163. kvm_init_shadow_ept_mmu(vcpu,
  9164. to_vmx(vcpu)->nested.msrs.ept_caps &
  9165. VMX_EPT_EXECUTE_ONLY_BIT,
  9166. nested_ept_ad_enabled(vcpu));
  9167. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9168. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9169. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9170. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9171. return 0;
  9172. }
  9173. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9174. {
  9175. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9176. }
  9177. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9178. u16 error_code)
  9179. {
  9180. bool inequality, bit;
  9181. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9182. inequality =
  9183. (error_code & vmcs12->page_fault_error_code_mask) !=
  9184. vmcs12->page_fault_error_code_match;
  9185. return inequality ^ bit;
  9186. }
  9187. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9188. struct x86_exception *fault)
  9189. {
  9190. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9191. WARN_ON(!is_guest_mode(vcpu));
  9192. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9193. !to_vmx(vcpu)->nested.nested_run_pending) {
  9194. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9195. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9196. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9197. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9198. fault->address);
  9199. } else {
  9200. kvm_inject_page_fault(vcpu, fault);
  9201. }
  9202. }
  9203. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9204. struct vmcs12 *vmcs12);
  9205. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  9206. struct vmcs12 *vmcs12)
  9207. {
  9208. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9209. struct page *page;
  9210. u64 hpa;
  9211. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9212. /*
  9213. * Translate L1 physical address to host physical
  9214. * address for vmcs02. Keep the page pinned, so this
  9215. * physical address remains valid. We keep a reference
  9216. * to it so we can release it later.
  9217. */
  9218. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9219. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9220. vmx->nested.apic_access_page = NULL;
  9221. }
  9222. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9223. /*
  9224. * If translation failed, no matter: This feature asks
  9225. * to exit when accessing the given address, and if it
  9226. * can never be accessed, this feature won't do
  9227. * anything anyway.
  9228. */
  9229. if (!is_error_page(page)) {
  9230. vmx->nested.apic_access_page = page;
  9231. hpa = page_to_phys(vmx->nested.apic_access_page);
  9232. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9233. } else {
  9234. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9235. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9236. }
  9237. }
  9238. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9239. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9240. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9241. vmx->nested.virtual_apic_page = NULL;
  9242. }
  9243. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9244. /*
  9245. * If translation failed, VM entry will fail because
  9246. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9247. * Failing the vm entry is _not_ what the processor
  9248. * does but it's basically the only possibility we
  9249. * have. We could still enter the guest if CR8 load
  9250. * exits are enabled, CR8 store exits are enabled, and
  9251. * virtualize APIC access is disabled; in this case
  9252. * the processor would never use the TPR shadow and we
  9253. * could simply clear the bit from the execution
  9254. * control. But such a configuration is useless, so
  9255. * let's keep the code simple.
  9256. */
  9257. if (!is_error_page(page)) {
  9258. vmx->nested.virtual_apic_page = page;
  9259. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9260. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9261. }
  9262. }
  9263. if (nested_cpu_has_posted_intr(vmcs12)) {
  9264. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9265. kunmap(vmx->nested.pi_desc_page);
  9266. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9267. vmx->nested.pi_desc_page = NULL;
  9268. }
  9269. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9270. if (is_error_page(page))
  9271. return;
  9272. vmx->nested.pi_desc_page = page;
  9273. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9274. vmx->nested.pi_desc =
  9275. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9276. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9277. (PAGE_SIZE - 1)));
  9278. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9279. page_to_phys(vmx->nested.pi_desc_page) +
  9280. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9281. (PAGE_SIZE - 1)));
  9282. }
  9283. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9284. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9285. CPU_BASED_USE_MSR_BITMAPS);
  9286. else
  9287. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9288. CPU_BASED_USE_MSR_BITMAPS);
  9289. }
  9290. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9291. {
  9292. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9293. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9294. if (vcpu->arch.virtual_tsc_khz == 0)
  9295. return;
  9296. /* Make sure short timeouts reliably trigger an immediate vmexit.
  9297. * hrtimer_start does not guarantee this. */
  9298. if (preemption_timeout <= 1) {
  9299. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9300. return;
  9301. }
  9302. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9303. preemption_timeout *= 1000000;
  9304. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9305. hrtimer_start(&vmx->nested.preemption_timer,
  9306. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9307. }
  9308. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9309. struct vmcs12 *vmcs12)
  9310. {
  9311. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9312. return 0;
  9313. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9314. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9315. return -EINVAL;
  9316. return 0;
  9317. }
  9318. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9319. struct vmcs12 *vmcs12)
  9320. {
  9321. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9322. return 0;
  9323. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9324. return -EINVAL;
  9325. return 0;
  9326. }
  9327. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  9328. struct vmcs12 *vmcs12)
  9329. {
  9330. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9331. return 0;
  9332. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  9333. return -EINVAL;
  9334. return 0;
  9335. }
  9336. /*
  9337. * Merge L0's and L1's MSR bitmap, return false to indicate that
  9338. * we do not use the hardware.
  9339. */
  9340. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9341. struct vmcs12 *vmcs12)
  9342. {
  9343. int msr;
  9344. struct page *page;
  9345. unsigned long *msr_bitmap_l1;
  9346. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  9347. /*
  9348. * pred_cmd & spec_ctrl are trying to verify two things:
  9349. *
  9350. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  9351. * ensures that we do not accidentally generate an L02 MSR bitmap
  9352. * from the L12 MSR bitmap that is too permissive.
  9353. * 2. That L1 or L2s have actually used the MSR. This avoids
  9354. * unnecessarily merging of the bitmap if the MSR is unused. This
  9355. * works properly because we only update the L01 MSR bitmap lazily.
  9356. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  9357. * updated to reflect this when L1 (or its L2s) actually write to
  9358. * the MSR.
  9359. */
  9360. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  9361. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  9362. /* Nothing to do if the MSR bitmap is not in use. */
  9363. if (!cpu_has_vmx_msr_bitmap() ||
  9364. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9365. return false;
  9366. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9367. !pred_cmd && !spec_ctrl)
  9368. return false;
  9369. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  9370. if (is_error_page(page))
  9371. return false;
  9372. msr_bitmap_l1 = (unsigned long *)kmap(page);
  9373. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  9374. /*
  9375. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  9376. * just lets the processor take the value from the virtual-APIC page;
  9377. * take those 256 bits directly from the L1 bitmap.
  9378. */
  9379. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9380. unsigned word = msr / BITS_PER_LONG;
  9381. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  9382. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9383. }
  9384. } else {
  9385. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9386. unsigned word = msr / BITS_PER_LONG;
  9387. msr_bitmap_l0[word] = ~0;
  9388. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9389. }
  9390. }
  9391. nested_vmx_disable_intercept_for_msr(
  9392. msr_bitmap_l1, msr_bitmap_l0,
  9393. X2APIC_MSR(APIC_TASKPRI),
  9394. MSR_TYPE_W);
  9395. if (nested_cpu_has_vid(vmcs12)) {
  9396. nested_vmx_disable_intercept_for_msr(
  9397. msr_bitmap_l1, msr_bitmap_l0,
  9398. X2APIC_MSR(APIC_EOI),
  9399. MSR_TYPE_W);
  9400. nested_vmx_disable_intercept_for_msr(
  9401. msr_bitmap_l1, msr_bitmap_l0,
  9402. X2APIC_MSR(APIC_SELF_IPI),
  9403. MSR_TYPE_W);
  9404. }
  9405. if (spec_ctrl)
  9406. nested_vmx_disable_intercept_for_msr(
  9407. msr_bitmap_l1, msr_bitmap_l0,
  9408. MSR_IA32_SPEC_CTRL,
  9409. MSR_TYPE_R | MSR_TYPE_W);
  9410. if (pred_cmd)
  9411. nested_vmx_disable_intercept_for_msr(
  9412. msr_bitmap_l1, msr_bitmap_l0,
  9413. MSR_IA32_PRED_CMD,
  9414. MSR_TYPE_W);
  9415. kunmap(page);
  9416. kvm_release_page_clean(page);
  9417. return true;
  9418. }
  9419. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  9420. struct vmcs12 *vmcs12)
  9421. {
  9422. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  9423. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  9424. return -EINVAL;
  9425. else
  9426. return 0;
  9427. }
  9428. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  9429. struct vmcs12 *vmcs12)
  9430. {
  9431. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9432. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  9433. !nested_cpu_has_vid(vmcs12) &&
  9434. !nested_cpu_has_posted_intr(vmcs12))
  9435. return 0;
  9436. /*
  9437. * If virtualize x2apic mode is enabled,
  9438. * virtualize apic access must be disabled.
  9439. */
  9440. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9441. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  9442. return -EINVAL;
  9443. /*
  9444. * If virtual interrupt delivery is enabled,
  9445. * we must exit on external interrupts.
  9446. */
  9447. if (nested_cpu_has_vid(vmcs12) &&
  9448. !nested_exit_on_intr(vcpu))
  9449. return -EINVAL;
  9450. /*
  9451. * bits 15:8 should be zero in posted_intr_nv,
  9452. * the descriptor address has been already checked
  9453. * in nested_get_vmcs12_pages.
  9454. */
  9455. if (nested_cpu_has_posted_intr(vmcs12) &&
  9456. (!nested_cpu_has_vid(vmcs12) ||
  9457. !nested_exit_intr_ack_set(vcpu) ||
  9458. vmcs12->posted_intr_nv & 0xff00))
  9459. return -EINVAL;
  9460. /* tpr shadow is needed by all apicv features. */
  9461. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9462. return -EINVAL;
  9463. return 0;
  9464. }
  9465. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  9466. unsigned long count_field,
  9467. unsigned long addr_field)
  9468. {
  9469. int maxphyaddr;
  9470. u64 count, addr;
  9471. if (vmcs12_read_any(vcpu, count_field, &count) ||
  9472. vmcs12_read_any(vcpu, addr_field, &addr)) {
  9473. WARN_ON(1);
  9474. return -EINVAL;
  9475. }
  9476. if (count == 0)
  9477. return 0;
  9478. maxphyaddr = cpuid_maxphyaddr(vcpu);
  9479. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  9480. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  9481. pr_debug_ratelimited(
  9482. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  9483. addr_field, maxphyaddr, count, addr);
  9484. return -EINVAL;
  9485. }
  9486. return 0;
  9487. }
  9488. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  9489. struct vmcs12 *vmcs12)
  9490. {
  9491. if (vmcs12->vm_exit_msr_load_count == 0 &&
  9492. vmcs12->vm_exit_msr_store_count == 0 &&
  9493. vmcs12->vm_entry_msr_load_count == 0)
  9494. return 0; /* Fast path */
  9495. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  9496. VM_EXIT_MSR_LOAD_ADDR) ||
  9497. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  9498. VM_EXIT_MSR_STORE_ADDR) ||
  9499. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  9500. VM_ENTRY_MSR_LOAD_ADDR))
  9501. return -EINVAL;
  9502. return 0;
  9503. }
  9504. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  9505. struct vmcs12 *vmcs12)
  9506. {
  9507. u64 address = vmcs12->pml_address;
  9508. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  9509. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  9510. if (!nested_cpu_has_ept(vmcs12) ||
  9511. !IS_ALIGNED(address, 4096) ||
  9512. address >> maxphyaddr)
  9513. return -EINVAL;
  9514. }
  9515. return 0;
  9516. }
  9517. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  9518. struct vmx_msr_entry *e)
  9519. {
  9520. /* x2APIC MSR accesses are not allowed */
  9521. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  9522. return -EINVAL;
  9523. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  9524. e->index == MSR_IA32_UCODE_REV)
  9525. return -EINVAL;
  9526. if (e->reserved != 0)
  9527. return -EINVAL;
  9528. return 0;
  9529. }
  9530. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  9531. struct vmx_msr_entry *e)
  9532. {
  9533. if (e->index == MSR_FS_BASE ||
  9534. e->index == MSR_GS_BASE ||
  9535. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  9536. nested_vmx_msr_check_common(vcpu, e))
  9537. return -EINVAL;
  9538. return 0;
  9539. }
  9540. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  9541. struct vmx_msr_entry *e)
  9542. {
  9543. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  9544. nested_vmx_msr_check_common(vcpu, e))
  9545. return -EINVAL;
  9546. return 0;
  9547. }
  9548. /*
  9549. * Load guest's/host's msr at nested entry/exit.
  9550. * return 0 for success, entry index for failure.
  9551. */
  9552. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9553. {
  9554. u32 i;
  9555. struct vmx_msr_entry e;
  9556. struct msr_data msr;
  9557. msr.host_initiated = false;
  9558. for (i = 0; i < count; i++) {
  9559. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  9560. &e, sizeof(e))) {
  9561. pr_debug_ratelimited(
  9562. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9563. __func__, i, gpa + i * sizeof(e));
  9564. goto fail;
  9565. }
  9566. if (nested_vmx_load_msr_check(vcpu, &e)) {
  9567. pr_debug_ratelimited(
  9568. "%s check failed (%u, 0x%x, 0x%x)\n",
  9569. __func__, i, e.index, e.reserved);
  9570. goto fail;
  9571. }
  9572. msr.index = e.index;
  9573. msr.data = e.value;
  9574. if (kvm_set_msr(vcpu, &msr)) {
  9575. pr_debug_ratelimited(
  9576. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9577. __func__, i, e.index, e.value);
  9578. goto fail;
  9579. }
  9580. }
  9581. return 0;
  9582. fail:
  9583. return i + 1;
  9584. }
  9585. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9586. {
  9587. u32 i;
  9588. struct vmx_msr_entry e;
  9589. for (i = 0; i < count; i++) {
  9590. struct msr_data msr_info;
  9591. if (kvm_vcpu_read_guest(vcpu,
  9592. gpa + i * sizeof(e),
  9593. &e, 2 * sizeof(u32))) {
  9594. pr_debug_ratelimited(
  9595. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9596. __func__, i, gpa + i * sizeof(e));
  9597. return -EINVAL;
  9598. }
  9599. if (nested_vmx_store_msr_check(vcpu, &e)) {
  9600. pr_debug_ratelimited(
  9601. "%s check failed (%u, 0x%x, 0x%x)\n",
  9602. __func__, i, e.index, e.reserved);
  9603. return -EINVAL;
  9604. }
  9605. msr_info.host_initiated = false;
  9606. msr_info.index = e.index;
  9607. if (kvm_get_msr(vcpu, &msr_info)) {
  9608. pr_debug_ratelimited(
  9609. "%s cannot read MSR (%u, 0x%x)\n",
  9610. __func__, i, e.index);
  9611. return -EINVAL;
  9612. }
  9613. if (kvm_vcpu_write_guest(vcpu,
  9614. gpa + i * sizeof(e) +
  9615. offsetof(struct vmx_msr_entry, value),
  9616. &msr_info.data, sizeof(msr_info.data))) {
  9617. pr_debug_ratelimited(
  9618. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9619. __func__, i, e.index, msr_info.data);
  9620. return -EINVAL;
  9621. }
  9622. }
  9623. return 0;
  9624. }
  9625. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  9626. {
  9627. unsigned long invalid_mask;
  9628. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  9629. return (val & invalid_mask) == 0;
  9630. }
  9631. /*
  9632. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  9633. * emulating VM entry into a guest with EPT enabled.
  9634. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9635. * is assigned to entry_failure_code on failure.
  9636. */
  9637. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  9638. u32 *entry_failure_code)
  9639. {
  9640. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  9641. if (!nested_cr3_valid(vcpu, cr3)) {
  9642. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9643. return 1;
  9644. }
  9645. /*
  9646. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  9647. * must not be dereferenced.
  9648. */
  9649. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  9650. !nested_ept) {
  9651. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  9652. *entry_failure_code = ENTRY_FAIL_PDPTE;
  9653. return 1;
  9654. }
  9655. }
  9656. vcpu->arch.cr3 = cr3;
  9657. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  9658. }
  9659. kvm_mmu_reset_context(vcpu);
  9660. return 0;
  9661. }
  9662. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9663. {
  9664. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9665. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  9666. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  9667. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  9668. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  9669. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  9670. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  9671. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  9672. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  9673. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  9674. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  9675. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  9676. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  9677. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  9678. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  9679. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  9680. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  9681. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  9682. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  9683. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  9684. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  9685. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  9686. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  9687. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  9688. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  9689. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  9690. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  9691. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  9692. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  9693. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  9694. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  9695. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  9696. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  9697. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  9698. vmcs12->guest_pending_dbg_exceptions);
  9699. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  9700. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  9701. if (nested_cpu_has_xsaves(vmcs12))
  9702. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  9703. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  9704. if (cpu_has_vmx_posted_intr())
  9705. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  9706. /*
  9707. * Whether page-faults are trapped is determined by a combination of
  9708. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  9709. * If enable_ept, L0 doesn't care about page faults and we should
  9710. * set all of these to L1's desires. However, if !enable_ept, L0 does
  9711. * care about (at least some) page faults, and because it is not easy
  9712. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  9713. * to exit on each and every L2 page fault. This is done by setting
  9714. * MASK=MATCH=0 and (see below) EB.PF=1.
  9715. * Note that below we don't need special code to set EB.PF beyond the
  9716. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  9717. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  9718. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  9719. */
  9720. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  9721. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  9722. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  9723. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  9724. /* All VMFUNCs are currently emulated through L0 vmexits. */
  9725. if (cpu_has_vmx_vmfunc())
  9726. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  9727. if (cpu_has_vmx_apicv()) {
  9728. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  9729. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  9730. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  9731. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  9732. }
  9733. /*
  9734. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  9735. * Some constant fields are set here by vmx_set_constant_host_state().
  9736. * Other fields are different per CPU, and will be set later when
  9737. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  9738. */
  9739. vmx_set_constant_host_state(vmx);
  9740. /*
  9741. * Set the MSR load/store lists to match L0's settings.
  9742. */
  9743. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  9744. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9745. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  9746. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9747. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  9748. set_cr4_guest_host_mask(vmx);
  9749. if (vmx_mpx_supported())
  9750. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  9751. if (enable_vpid) {
  9752. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  9753. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  9754. else
  9755. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  9756. }
  9757. /*
  9758. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  9759. */
  9760. if (enable_ept) {
  9761. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  9762. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  9763. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  9764. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  9765. }
  9766. if (cpu_has_vmx_msr_bitmap())
  9767. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  9768. }
  9769. /*
  9770. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  9771. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  9772. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  9773. * guest in a way that will both be appropriate to L1's requests, and our
  9774. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  9775. * function also has additional necessary side-effects, like setting various
  9776. * vcpu->arch fields.
  9777. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9778. * is assigned to entry_failure_code on failure.
  9779. */
  9780. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9781. u32 *entry_failure_code)
  9782. {
  9783. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9784. u32 exec_control, vmcs12_exec_ctrl;
  9785. if (vmx->nested.dirty_vmcs12) {
  9786. prepare_vmcs02_full(vcpu, vmcs12);
  9787. vmx->nested.dirty_vmcs12 = false;
  9788. }
  9789. /*
  9790. * First, the fields that are shadowed. This must be kept in sync
  9791. * with vmx_shadow_fields.h.
  9792. */
  9793. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  9794. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  9795. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  9796. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  9797. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  9798. /*
  9799. * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
  9800. * HOST_FS_BASE, HOST_GS_BASE.
  9801. */
  9802. if (vmx->nested.nested_run_pending &&
  9803. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  9804. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  9805. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  9806. } else {
  9807. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  9808. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  9809. }
  9810. if (vmx->nested.nested_run_pending) {
  9811. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  9812. vmcs12->vm_entry_intr_info_field);
  9813. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  9814. vmcs12->vm_entry_exception_error_code);
  9815. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  9816. vmcs12->vm_entry_instruction_len);
  9817. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  9818. vmcs12->guest_interruptibility_info);
  9819. vmx->loaded_vmcs->nmi_known_unmasked =
  9820. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  9821. } else {
  9822. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9823. }
  9824. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  9825. exec_control = vmcs12->pin_based_vm_exec_control;
  9826. /* Preemption timer setting is only taken from vmcs01. */
  9827. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9828. exec_control |= vmcs_config.pin_based_exec_ctrl;
  9829. if (vmx->hv_deadline_tsc == -1)
  9830. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9831. /* Posted interrupts setting is only taken from vmcs12. */
  9832. if (nested_cpu_has_posted_intr(vmcs12)) {
  9833. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  9834. vmx->nested.pi_pending = false;
  9835. } else {
  9836. exec_control &= ~PIN_BASED_POSTED_INTR;
  9837. }
  9838. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  9839. vmx->nested.preemption_timer_expired = false;
  9840. if (nested_cpu_has_preemption_timer(vmcs12))
  9841. vmx_start_preemption_timer(vcpu);
  9842. if (cpu_has_secondary_exec_ctrls()) {
  9843. exec_control = vmx->secondary_exec_control;
  9844. /* Take the following fields only from vmcs12 */
  9845. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9846. SECONDARY_EXEC_ENABLE_INVPCID |
  9847. SECONDARY_EXEC_RDTSCP |
  9848. SECONDARY_EXEC_XSAVES |
  9849. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  9850. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  9851. SECONDARY_EXEC_ENABLE_VMFUNC);
  9852. if (nested_cpu_has(vmcs12,
  9853. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  9854. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  9855. ~SECONDARY_EXEC_ENABLE_PML;
  9856. exec_control |= vmcs12_exec_ctrl;
  9857. }
  9858. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  9859. vmcs_write16(GUEST_INTR_STATUS,
  9860. vmcs12->guest_intr_status);
  9861. /*
  9862. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  9863. * nested_get_vmcs12_pages will either fix it up or
  9864. * remove the VM execution control.
  9865. */
  9866. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  9867. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  9868. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  9869. }
  9870. /*
  9871. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  9872. * entry, but only if the current (host) sp changed from the value
  9873. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  9874. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  9875. * here we just force the write to happen on entry.
  9876. */
  9877. vmx->host_rsp = 0;
  9878. exec_control = vmx_exec_control(vmx); /* L0's desires */
  9879. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  9880. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  9881. exec_control &= ~CPU_BASED_TPR_SHADOW;
  9882. exec_control |= vmcs12->cpu_based_vm_exec_control;
  9883. /*
  9884. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  9885. * nested_get_vmcs12_pages can't fix it up, the illegal value
  9886. * will result in a VM entry failure.
  9887. */
  9888. if (exec_control & CPU_BASED_TPR_SHADOW) {
  9889. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  9890. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  9891. } else {
  9892. #ifdef CONFIG_X86_64
  9893. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  9894. CPU_BASED_CR8_STORE_EXITING;
  9895. #endif
  9896. }
  9897. /*
  9898. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  9899. * for I/O port accesses.
  9900. */
  9901. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  9902. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  9903. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  9904. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  9905. * bitwise-or of what L1 wants to trap for L2, and what we want to
  9906. * trap. Note that CR0.TS also needs updating - we do this later.
  9907. */
  9908. update_exception_bitmap(vcpu);
  9909. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  9910. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  9911. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  9912. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  9913. * bits are further modified by vmx_set_efer() below.
  9914. */
  9915. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  9916. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  9917. * emulated by vmx_set_efer(), below.
  9918. */
  9919. vm_entry_controls_init(vmx,
  9920. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  9921. ~VM_ENTRY_IA32E_MODE) |
  9922. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  9923. if (vmx->nested.nested_run_pending &&
  9924. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  9925. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  9926. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  9927. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  9928. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  9929. }
  9930. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  9931. if (kvm_has_tsc_control)
  9932. decache_tsc_multiplier(vmx);
  9933. if (enable_vpid) {
  9934. /*
  9935. * There is no direct mapping between vpid02 and vpid12, the
  9936. * vpid02 is per-vCPU for L0 and reused while the value of
  9937. * vpid12 is changed w/ one invvpid during nested vmentry.
  9938. * The vpid12 is allocated by L1 for L2, so it will not
  9939. * influence global bitmap(for vpid01 and vpid02 allocation)
  9940. * even if spawn a lot of nested vCPUs.
  9941. */
  9942. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  9943. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  9944. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  9945. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  9946. }
  9947. } else {
  9948. vmx_flush_tlb(vcpu, true);
  9949. }
  9950. }
  9951. if (enable_pml) {
  9952. /*
  9953. * Conceptually we want to copy the PML address and index from
  9954. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  9955. * since we always flush the log on each vmexit, this happens
  9956. * to be equivalent to simply resetting the fields in vmcs02.
  9957. */
  9958. ASSERT(vmx->pml_pg);
  9959. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  9960. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  9961. }
  9962. if (nested_cpu_has_ept(vmcs12)) {
  9963. if (nested_ept_init_mmu_context(vcpu)) {
  9964. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9965. return 1;
  9966. }
  9967. } else if (nested_cpu_has2(vmcs12,
  9968. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9969. vmx_flush_tlb(vcpu, true);
  9970. }
  9971. /*
  9972. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  9973. * bits which we consider mandatory enabled.
  9974. * The CR0_READ_SHADOW is what L2 should have expected to read given
  9975. * the specifications by L1; It's not enough to take
  9976. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  9977. * have more bits than L1 expected.
  9978. */
  9979. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  9980. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  9981. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  9982. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  9983. if (vmx->nested.nested_run_pending &&
  9984. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  9985. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  9986. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  9987. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9988. else
  9989. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9990. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  9991. vmx_set_efer(vcpu, vcpu->arch.efer);
  9992. /*
  9993. * Guest state is invalid and unrestricted guest is disabled,
  9994. * which means L1 attempted VMEntry to L2 with invalid state.
  9995. * Fail the VMEntry.
  9996. */
  9997. if (vmx->emulation_required) {
  9998. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9999. return 1;
  10000. }
  10001. /* Shadow page tables on either EPT or shadow page tables. */
  10002. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  10003. entry_failure_code))
  10004. return 1;
  10005. if (!enable_ept)
  10006. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  10007. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  10008. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  10009. return 0;
  10010. }
  10011. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  10012. {
  10013. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  10014. nested_cpu_has_virtual_nmis(vmcs12))
  10015. return -EINVAL;
  10016. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  10017. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  10018. return -EINVAL;
  10019. return 0;
  10020. }
  10021. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10022. {
  10023. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10024. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10025. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10026. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10027. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10028. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10029. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10030. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10031. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10032. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10033. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10034. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10035. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10036. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10037. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10038. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10039. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10040. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10041. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10042. vmx->nested.msrs.procbased_ctls_low,
  10043. vmx->nested.msrs.procbased_ctls_high) ||
  10044. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10045. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10046. vmx->nested.msrs.secondary_ctls_low,
  10047. vmx->nested.msrs.secondary_ctls_high)) ||
  10048. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10049. vmx->nested.msrs.pinbased_ctls_low,
  10050. vmx->nested.msrs.pinbased_ctls_high) ||
  10051. !vmx_control_verify(vmcs12->vm_exit_controls,
  10052. vmx->nested.msrs.exit_ctls_low,
  10053. vmx->nested.msrs.exit_ctls_high) ||
  10054. !vmx_control_verify(vmcs12->vm_entry_controls,
  10055. vmx->nested.msrs.entry_ctls_low,
  10056. vmx->nested.msrs.entry_ctls_high))
  10057. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10058. if (nested_vmx_check_nmi_controls(vmcs12))
  10059. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10060. if (nested_cpu_has_vmfunc(vmcs12)) {
  10061. if (vmcs12->vm_function_control &
  10062. ~vmx->nested.msrs.vmfunc_controls)
  10063. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10064. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10065. if (!nested_cpu_has_ept(vmcs12) ||
  10066. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10067. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10068. }
  10069. }
  10070. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10071. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10072. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10073. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10074. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10075. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10076. /*
  10077. * From the Intel SDM, volume 3:
  10078. * Fields relevant to VM-entry event injection must be set properly.
  10079. * These fields are the VM-entry interruption-information field, the
  10080. * VM-entry exception error code, and the VM-entry instruction length.
  10081. */
  10082. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10083. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10084. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10085. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10086. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10087. bool should_have_error_code;
  10088. bool urg = nested_cpu_has2(vmcs12,
  10089. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10090. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10091. /* VM-entry interruption-info field: interruption type */
  10092. if (intr_type == INTR_TYPE_RESERVED ||
  10093. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10094. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10095. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10096. /* VM-entry interruption-info field: vector */
  10097. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10098. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10099. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10100. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10101. /* VM-entry interruption-info field: deliver error code */
  10102. should_have_error_code =
  10103. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10104. x86_exception_has_error_code(vector);
  10105. if (has_error_code != should_have_error_code)
  10106. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10107. /* VM-entry exception error code */
  10108. if (has_error_code &&
  10109. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  10110. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10111. /* VM-entry interruption-info field: reserved bits */
  10112. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10113. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10114. /* VM-entry instruction length */
  10115. switch (intr_type) {
  10116. case INTR_TYPE_SOFT_EXCEPTION:
  10117. case INTR_TYPE_SOFT_INTR:
  10118. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10119. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10120. (vmcs12->vm_entry_instruction_len == 0 &&
  10121. !nested_cpu_has_zero_length_injection(vcpu)))
  10122. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10123. }
  10124. }
  10125. return 0;
  10126. }
  10127. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10128. u32 *exit_qual)
  10129. {
  10130. bool ia32e;
  10131. *exit_qual = ENTRY_FAIL_DEFAULT;
  10132. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10133. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10134. return 1;
  10135. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
  10136. vmcs12->vmcs_link_pointer != -1ull) {
  10137. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10138. return 1;
  10139. }
  10140. /*
  10141. * If the load IA32_EFER VM-entry control is 1, the following checks
  10142. * are performed on the field for the IA32_EFER MSR:
  10143. * - Bits reserved in the IA32_EFER MSR must be 0.
  10144. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10145. * the IA-32e mode guest VM-exit control. It must also be identical
  10146. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10147. * CR0.PG) is 1.
  10148. */
  10149. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10150. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10151. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10152. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10153. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10154. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10155. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10156. return 1;
  10157. }
  10158. /*
  10159. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10160. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10161. * the values of the LMA and LME bits in the field must each be that of
  10162. * the host address-space size VM-exit control.
  10163. */
  10164. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10165. ia32e = (vmcs12->vm_exit_controls &
  10166. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10167. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10168. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10169. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10170. return 1;
  10171. }
  10172. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10173. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10174. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10175. return 1;
  10176. return 0;
  10177. }
  10178. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
  10179. {
  10180. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10181. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10182. u32 exit_qual;
  10183. int r;
  10184. enter_guest_mode(vcpu);
  10185. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10186. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10187. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10188. vmx_segment_cache_clear(vmx);
  10189. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10190. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10191. r = EXIT_REASON_INVALID_STATE;
  10192. if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
  10193. goto fail;
  10194. nested_get_vmcs12_pages(vcpu, vmcs12);
  10195. r = EXIT_REASON_MSR_LOAD_FAIL;
  10196. exit_qual = nested_vmx_load_msr(vcpu,
  10197. vmcs12->vm_entry_msr_load_addr,
  10198. vmcs12->vm_entry_msr_load_count);
  10199. if (exit_qual)
  10200. goto fail;
  10201. /*
  10202. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10203. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10204. * returned as far as L1 is concerned. It will only return (and set
  10205. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10206. */
  10207. return 0;
  10208. fail:
  10209. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10210. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10211. leave_guest_mode(vcpu);
  10212. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10213. nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
  10214. return 1;
  10215. }
  10216. /*
  10217. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  10218. * for running an L2 nested guest.
  10219. */
  10220. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  10221. {
  10222. struct vmcs12 *vmcs12;
  10223. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10224. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  10225. u32 exit_qual;
  10226. int ret;
  10227. if (!nested_vmx_check_permission(vcpu))
  10228. return 1;
  10229. if (!nested_vmx_check_vmcs12(vcpu))
  10230. goto out;
  10231. vmcs12 = get_vmcs12(vcpu);
  10232. if (enable_shadow_vmcs)
  10233. copy_shadow_to_vmcs12(vmx);
  10234. /*
  10235. * The nested entry process starts with enforcing various prerequisites
  10236. * on vmcs12 as required by the Intel SDM, and act appropriately when
  10237. * they fail: As the SDM explains, some conditions should cause the
  10238. * instruction to fail, while others will cause the instruction to seem
  10239. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  10240. * To speed up the normal (success) code path, we should avoid checking
  10241. * for misconfigurations which will anyway be caught by the processor
  10242. * when using the merged vmcs02.
  10243. */
  10244. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  10245. nested_vmx_failValid(vcpu,
  10246. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  10247. goto out;
  10248. }
  10249. if (vmcs12->launch_state == launch) {
  10250. nested_vmx_failValid(vcpu,
  10251. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  10252. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  10253. goto out;
  10254. }
  10255. ret = check_vmentry_prereqs(vcpu, vmcs12);
  10256. if (ret) {
  10257. nested_vmx_failValid(vcpu, ret);
  10258. goto out;
  10259. }
  10260. /*
  10261. * After this point, the trap flag no longer triggers a singlestep trap
  10262. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  10263. * This is not 100% correct; for performance reasons, we delegate most
  10264. * of the checks on host state to the processor. If those fail,
  10265. * the singlestep trap is missed.
  10266. */
  10267. skip_emulated_instruction(vcpu);
  10268. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  10269. if (ret) {
  10270. nested_vmx_entry_failure(vcpu, vmcs12,
  10271. EXIT_REASON_INVALID_STATE, exit_qual);
  10272. return 1;
  10273. }
  10274. /*
  10275. * We're finally done with prerequisite checking, and can start with
  10276. * the nested entry.
  10277. */
  10278. vmx->nested.nested_run_pending = 1;
  10279. ret = enter_vmx_non_root_mode(vcpu);
  10280. if (ret) {
  10281. vmx->nested.nested_run_pending = 0;
  10282. return ret;
  10283. }
  10284. /*
  10285. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  10286. * by event injection, halt vcpu.
  10287. */
  10288. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  10289. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  10290. vmx->nested.nested_run_pending = 0;
  10291. return kvm_vcpu_halt(vcpu);
  10292. }
  10293. return 1;
  10294. out:
  10295. return kvm_skip_emulated_instruction(vcpu);
  10296. }
  10297. /*
  10298. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  10299. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  10300. * This function returns the new value we should put in vmcs12.guest_cr0.
  10301. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  10302. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  10303. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  10304. * didn't trap the bit, because if L1 did, so would L0).
  10305. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  10306. * been modified by L2, and L1 knows it. So just leave the old value of
  10307. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  10308. * isn't relevant, because if L0 traps this bit it can set it to anything.
  10309. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  10310. * changed these bits, and therefore they need to be updated, but L0
  10311. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  10312. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  10313. */
  10314. static inline unsigned long
  10315. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10316. {
  10317. return
  10318. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  10319. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  10320. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  10321. vcpu->arch.cr0_guest_owned_bits));
  10322. }
  10323. static inline unsigned long
  10324. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10325. {
  10326. return
  10327. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  10328. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  10329. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  10330. vcpu->arch.cr4_guest_owned_bits));
  10331. }
  10332. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  10333. struct vmcs12 *vmcs12)
  10334. {
  10335. u32 idt_vectoring;
  10336. unsigned int nr;
  10337. if (vcpu->arch.exception.injected) {
  10338. nr = vcpu->arch.exception.nr;
  10339. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10340. if (kvm_exception_is_soft(nr)) {
  10341. vmcs12->vm_exit_instruction_len =
  10342. vcpu->arch.event_exit_inst_len;
  10343. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  10344. } else
  10345. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  10346. if (vcpu->arch.exception.has_error_code) {
  10347. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  10348. vmcs12->idt_vectoring_error_code =
  10349. vcpu->arch.exception.error_code;
  10350. }
  10351. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10352. } else if (vcpu->arch.nmi_injected) {
  10353. vmcs12->idt_vectoring_info_field =
  10354. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  10355. } else if (vcpu->arch.interrupt.injected) {
  10356. nr = vcpu->arch.interrupt.nr;
  10357. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10358. if (vcpu->arch.interrupt.soft) {
  10359. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  10360. vmcs12->vm_entry_instruction_len =
  10361. vcpu->arch.event_exit_inst_len;
  10362. } else
  10363. idt_vectoring |= INTR_TYPE_EXT_INTR;
  10364. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10365. }
  10366. }
  10367. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  10368. {
  10369. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10370. unsigned long exit_qual;
  10371. bool block_nested_events =
  10372. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  10373. if (vcpu->arch.exception.pending &&
  10374. nested_vmx_check_exception(vcpu, &exit_qual)) {
  10375. if (block_nested_events)
  10376. return -EBUSY;
  10377. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  10378. return 0;
  10379. }
  10380. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  10381. vmx->nested.preemption_timer_expired) {
  10382. if (block_nested_events)
  10383. return -EBUSY;
  10384. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  10385. return 0;
  10386. }
  10387. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  10388. if (block_nested_events)
  10389. return -EBUSY;
  10390. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  10391. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  10392. INTR_INFO_VALID_MASK, 0);
  10393. /*
  10394. * The NMI-triggered VM exit counts as injection:
  10395. * clear this one and block further NMIs.
  10396. */
  10397. vcpu->arch.nmi_pending = 0;
  10398. vmx_set_nmi_mask(vcpu, true);
  10399. return 0;
  10400. }
  10401. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  10402. nested_exit_on_intr(vcpu)) {
  10403. if (block_nested_events)
  10404. return -EBUSY;
  10405. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  10406. return 0;
  10407. }
  10408. vmx_complete_nested_posted_interrupt(vcpu);
  10409. return 0;
  10410. }
  10411. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  10412. {
  10413. ktime_t remaining =
  10414. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  10415. u64 value;
  10416. if (ktime_to_ns(remaining) <= 0)
  10417. return 0;
  10418. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  10419. do_div(value, 1000000);
  10420. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  10421. }
  10422. /*
  10423. * Update the guest state fields of vmcs12 to reflect changes that
  10424. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  10425. * VM-entry controls is also updated, since this is really a guest
  10426. * state bit.)
  10427. */
  10428. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10429. {
  10430. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  10431. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  10432. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  10433. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  10434. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  10435. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  10436. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  10437. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  10438. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  10439. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  10440. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  10441. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  10442. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  10443. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  10444. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  10445. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  10446. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  10447. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  10448. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  10449. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  10450. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  10451. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  10452. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  10453. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  10454. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  10455. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  10456. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  10457. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  10458. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  10459. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  10460. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  10461. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  10462. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  10463. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  10464. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  10465. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  10466. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  10467. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  10468. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  10469. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  10470. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  10471. vmcs12->guest_interruptibility_info =
  10472. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  10473. vmcs12->guest_pending_dbg_exceptions =
  10474. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  10475. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  10476. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  10477. else
  10478. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  10479. if (nested_cpu_has_preemption_timer(vmcs12)) {
  10480. if (vmcs12->vm_exit_controls &
  10481. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  10482. vmcs12->vmx_preemption_timer_value =
  10483. vmx_get_preemption_timer_value(vcpu);
  10484. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  10485. }
  10486. /*
  10487. * In some cases (usually, nested EPT), L2 is allowed to change its
  10488. * own CR3 without exiting. If it has changed it, we must keep it.
  10489. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  10490. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  10491. *
  10492. * Additionally, restore L2's PDPTR to vmcs12.
  10493. */
  10494. if (enable_ept) {
  10495. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  10496. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  10497. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  10498. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  10499. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  10500. }
  10501. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  10502. if (nested_cpu_has_vid(vmcs12))
  10503. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  10504. vmcs12->vm_entry_controls =
  10505. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  10506. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  10507. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  10508. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  10509. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10510. }
  10511. /* TODO: These cannot have changed unless we have MSR bitmaps and
  10512. * the relevant bit asks not to trap the change */
  10513. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  10514. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  10515. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  10516. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  10517. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  10518. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  10519. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  10520. if (kvm_mpx_supported())
  10521. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  10522. }
  10523. /*
  10524. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  10525. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  10526. * and this function updates it to reflect the changes to the guest state while
  10527. * L2 was running (and perhaps made some exits which were handled directly by L0
  10528. * without going back to L1), and to reflect the exit reason.
  10529. * Note that we do not have to copy here all VMCS fields, just those that
  10530. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  10531. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  10532. * which already writes to vmcs12 directly.
  10533. */
  10534. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10535. u32 exit_reason, u32 exit_intr_info,
  10536. unsigned long exit_qualification)
  10537. {
  10538. /* update guest state fields: */
  10539. sync_vmcs12(vcpu, vmcs12);
  10540. /* update exit information fields: */
  10541. vmcs12->vm_exit_reason = exit_reason;
  10542. vmcs12->exit_qualification = exit_qualification;
  10543. vmcs12->vm_exit_intr_info = exit_intr_info;
  10544. vmcs12->idt_vectoring_info_field = 0;
  10545. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  10546. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  10547. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  10548. vmcs12->launch_state = 1;
  10549. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  10550. * instead of reading the real value. */
  10551. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  10552. /*
  10553. * Transfer the event that L0 or L1 may wanted to inject into
  10554. * L2 to IDT_VECTORING_INFO_FIELD.
  10555. */
  10556. vmcs12_save_pending_event(vcpu, vmcs12);
  10557. }
  10558. /*
  10559. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  10560. * preserved above and would only end up incorrectly in L1.
  10561. */
  10562. vcpu->arch.nmi_injected = false;
  10563. kvm_clear_exception_queue(vcpu);
  10564. kvm_clear_interrupt_queue(vcpu);
  10565. }
  10566. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  10567. struct vmcs12 *vmcs12)
  10568. {
  10569. u32 entry_failure_code;
  10570. nested_ept_uninit_mmu_context(vcpu);
  10571. /*
  10572. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  10573. * couldn't have changed.
  10574. */
  10575. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  10576. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  10577. if (!enable_ept)
  10578. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  10579. }
  10580. /*
  10581. * A part of what we need to when the nested L2 guest exits and we want to
  10582. * run its L1 parent, is to reset L1's guest state to the host state specified
  10583. * in vmcs12.
  10584. * This function is to be called not only on normal nested exit, but also on
  10585. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  10586. * Failures During or After Loading Guest State").
  10587. * This function should be called when the active VMCS is L1's (vmcs01).
  10588. */
  10589. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  10590. struct vmcs12 *vmcs12)
  10591. {
  10592. struct kvm_segment seg;
  10593. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  10594. vcpu->arch.efer = vmcs12->host_ia32_efer;
  10595. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10596. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10597. else
  10598. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10599. vmx_set_efer(vcpu, vcpu->arch.efer);
  10600. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  10601. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  10602. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  10603. /*
  10604. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  10605. * actually changed, because vmx_set_cr0 refers to efer set above.
  10606. *
  10607. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  10608. * (KVM doesn't change it);
  10609. */
  10610. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  10611. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  10612. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  10613. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  10614. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  10615. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10616. /*
  10617. * If vmcs01 don't use VPID, CPU flushes TLB on every
  10618. * VMEntry/VMExit. Thus, no need to flush TLB.
  10619. *
  10620. * If vmcs12 uses VPID, TLB entries populated by L2 are
  10621. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  10622. * with vmx->vpid. Thus, no need to flush TLB.
  10623. *
  10624. * Therefore, flush TLB only in case vmcs01 uses VPID and
  10625. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  10626. * are both tagged with vmx->vpid.
  10627. */
  10628. if (enable_vpid &&
  10629. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  10630. vmx_flush_tlb(vcpu, true);
  10631. }
  10632. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  10633. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  10634. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  10635. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  10636. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  10637. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  10638. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  10639. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  10640. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  10641. vmcs_write64(GUEST_BNDCFGS, 0);
  10642. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  10643. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  10644. vcpu->arch.pat = vmcs12->host_ia32_pat;
  10645. }
  10646. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  10647. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  10648. vmcs12->host_ia32_perf_global_ctrl);
  10649. /* Set L1 segment info according to Intel SDM
  10650. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  10651. seg = (struct kvm_segment) {
  10652. .base = 0,
  10653. .limit = 0xFFFFFFFF,
  10654. .selector = vmcs12->host_cs_selector,
  10655. .type = 11,
  10656. .present = 1,
  10657. .s = 1,
  10658. .g = 1
  10659. };
  10660. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10661. seg.l = 1;
  10662. else
  10663. seg.db = 1;
  10664. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  10665. seg = (struct kvm_segment) {
  10666. .base = 0,
  10667. .limit = 0xFFFFFFFF,
  10668. .type = 3,
  10669. .present = 1,
  10670. .s = 1,
  10671. .db = 1,
  10672. .g = 1
  10673. };
  10674. seg.selector = vmcs12->host_ds_selector;
  10675. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  10676. seg.selector = vmcs12->host_es_selector;
  10677. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  10678. seg.selector = vmcs12->host_ss_selector;
  10679. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  10680. seg.selector = vmcs12->host_fs_selector;
  10681. seg.base = vmcs12->host_fs_base;
  10682. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  10683. seg.selector = vmcs12->host_gs_selector;
  10684. seg.base = vmcs12->host_gs_base;
  10685. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  10686. seg = (struct kvm_segment) {
  10687. .base = vmcs12->host_tr_base,
  10688. .limit = 0x67,
  10689. .selector = vmcs12->host_tr_selector,
  10690. .type = 11,
  10691. .present = 1
  10692. };
  10693. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  10694. kvm_set_dr(vcpu, 7, 0x400);
  10695. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  10696. if (cpu_has_vmx_msr_bitmap())
  10697. vmx_update_msr_bitmap(vcpu);
  10698. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  10699. vmcs12->vm_exit_msr_load_count))
  10700. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  10701. }
  10702. /*
  10703. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  10704. * and modify vmcs12 to make it see what it would expect to see there if
  10705. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  10706. */
  10707. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  10708. u32 exit_intr_info,
  10709. unsigned long exit_qualification)
  10710. {
  10711. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10712. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10713. /* trying to cancel vmlaunch/vmresume is a bug */
  10714. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  10715. /*
  10716. * The only expected VM-instruction error is "VM entry with
  10717. * invalid control field(s)." Anything else indicates a
  10718. * problem with L0.
  10719. */
  10720. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  10721. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  10722. leave_guest_mode(vcpu);
  10723. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10724. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10725. if (likely(!vmx->fail)) {
  10726. if (exit_reason == -1)
  10727. sync_vmcs12(vcpu, vmcs12);
  10728. else
  10729. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  10730. exit_qualification);
  10731. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  10732. vmcs12->vm_exit_msr_store_count))
  10733. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  10734. }
  10735. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10736. vm_entry_controls_reset_shadow(vmx);
  10737. vm_exit_controls_reset_shadow(vmx);
  10738. vmx_segment_cache_clear(vmx);
  10739. /* Update any VMCS fields that might have changed while L2 ran */
  10740. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10741. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10742. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10743. if (vmx->hv_deadline_tsc == -1)
  10744. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10745. PIN_BASED_VMX_PREEMPTION_TIMER);
  10746. else
  10747. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10748. PIN_BASED_VMX_PREEMPTION_TIMER);
  10749. if (kvm_has_tsc_control)
  10750. decache_tsc_multiplier(vmx);
  10751. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  10752. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  10753. vmx_set_virtual_apic_mode(vcpu);
  10754. } else if (!nested_cpu_has_ept(vmcs12) &&
  10755. nested_cpu_has2(vmcs12,
  10756. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10757. vmx_flush_tlb(vcpu, true);
  10758. }
  10759. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  10760. vmx->host_rsp = 0;
  10761. /* Unpin physical memory we referred to in vmcs02 */
  10762. if (vmx->nested.apic_access_page) {
  10763. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10764. vmx->nested.apic_access_page = NULL;
  10765. }
  10766. if (vmx->nested.virtual_apic_page) {
  10767. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10768. vmx->nested.virtual_apic_page = NULL;
  10769. }
  10770. if (vmx->nested.pi_desc_page) {
  10771. kunmap(vmx->nested.pi_desc_page);
  10772. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10773. vmx->nested.pi_desc_page = NULL;
  10774. vmx->nested.pi_desc = NULL;
  10775. }
  10776. /*
  10777. * We are now running in L2, mmu_notifier will force to reload the
  10778. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  10779. */
  10780. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  10781. if (enable_shadow_vmcs && exit_reason != -1)
  10782. vmx->nested.sync_shadow_vmcs = true;
  10783. /* in case we halted in L2 */
  10784. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  10785. if (likely(!vmx->fail)) {
  10786. /*
  10787. * TODO: SDM says that with acknowledge interrupt on
  10788. * exit, bit 31 of the VM-exit interrupt information
  10789. * (valid interrupt) is always set to 1 on
  10790. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  10791. * need kvm_cpu_has_interrupt(). See the commit
  10792. * message for details.
  10793. */
  10794. if (nested_exit_intr_ack_set(vcpu) &&
  10795. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  10796. kvm_cpu_has_interrupt(vcpu)) {
  10797. int irq = kvm_cpu_get_interrupt(vcpu);
  10798. WARN_ON(irq < 0);
  10799. vmcs12->vm_exit_intr_info = irq |
  10800. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  10801. }
  10802. if (exit_reason != -1)
  10803. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  10804. vmcs12->exit_qualification,
  10805. vmcs12->idt_vectoring_info_field,
  10806. vmcs12->vm_exit_intr_info,
  10807. vmcs12->vm_exit_intr_error_code,
  10808. KVM_ISA_VMX);
  10809. load_vmcs12_host_state(vcpu, vmcs12);
  10810. return;
  10811. }
  10812. /*
  10813. * After an early L2 VM-entry failure, we're now back
  10814. * in L1 which thinks it just finished a VMLAUNCH or
  10815. * VMRESUME instruction, so we need to set the failure
  10816. * flag and the VM-instruction error field of the VMCS
  10817. * accordingly.
  10818. */
  10819. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  10820. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10821. /*
  10822. * The emulated instruction was already skipped in
  10823. * nested_vmx_run, but the updated RIP was never
  10824. * written back to the vmcs01.
  10825. */
  10826. skip_emulated_instruction(vcpu);
  10827. vmx->fail = 0;
  10828. }
  10829. /*
  10830. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  10831. */
  10832. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  10833. {
  10834. if (is_guest_mode(vcpu)) {
  10835. to_vmx(vcpu)->nested.nested_run_pending = 0;
  10836. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10837. }
  10838. free_nested(to_vmx(vcpu));
  10839. }
  10840. /*
  10841. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  10842. * 23.7 "VM-entry failures during or after loading guest state" (this also
  10843. * lists the acceptable exit-reason and exit-qualification parameters).
  10844. * It should only be called before L2 actually succeeded to run, and when
  10845. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  10846. */
  10847. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  10848. struct vmcs12 *vmcs12,
  10849. u32 reason, unsigned long qualification)
  10850. {
  10851. load_vmcs12_host_state(vcpu, vmcs12);
  10852. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  10853. vmcs12->exit_qualification = qualification;
  10854. nested_vmx_succeed(vcpu);
  10855. if (enable_shadow_vmcs)
  10856. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  10857. }
  10858. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  10859. struct x86_instruction_info *info,
  10860. enum x86_intercept_stage stage)
  10861. {
  10862. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10863. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  10864. /*
  10865. * RDPID causes #UD if disabled through secondary execution controls.
  10866. * Because it is marked as EmulateOnUD, we need to intercept it here.
  10867. */
  10868. if (info->intercept == x86_intercept_rdtscp &&
  10869. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  10870. ctxt->exception.vector = UD_VECTOR;
  10871. ctxt->exception.error_code_valid = false;
  10872. return X86EMUL_PROPAGATE_FAULT;
  10873. }
  10874. /* TODO: check more intercepts... */
  10875. return X86EMUL_CONTINUE;
  10876. }
  10877. #ifdef CONFIG_X86_64
  10878. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  10879. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  10880. u64 divisor, u64 *result)
  10881. {
  10882. u64 low = a << shift, high = a >> (64 - shift);
  10883. /* To avoid the overflow on divq */
  10884. if (high >= divisor)
  10885. return 1;
  10886. /* Low hold the result, high hold rem which is discarded */
  10887. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  10888. "rm" (divisor), "0" (low), "1" (high));
  10889. *result = low;
  10890. return 0;
  10891. }
  10892. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  10893. {
  10894. struct vcpu_vmx *vmx;
  10895. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  10896. if (kvm_mwait_in_guest(vcpu->kvm))
  10897. return -EOPNOTSUPP;
  10898. vmx = to_vmx(vcpu);
  10899. tscl = rdtsc();
  10900. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  10901. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  10902. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  10903. if (delta_tsc > lapic_timer_advance_cycles)
  10904. delta_tsc -= lapic_timer_advance_cycles;
  10905. else
  10906. delta_tsc = 0;
  10907. /* Convert to host delta tsc if tsc scaling is enabled */
  10908. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  10909. u64_shl_div_u64(delta_tsc,
  10910. kvm_tsc_scaling_ratio_frac_bits,
  10911. vcpu->arch.tsc_scaling_ratio,
  10912. &delta_tsc))
  10913. return -ERANGE;
  10914. /*
  10915. * If the delta tsc can't fit in the 32 bit after the multi shift,
  10916. * we can't use the preemption timer.
  10917. * It's possible that it fits on later vmentries, but checking
  10918. * on every vmentry is costly so we just use an hrtimer.
  10919. */
  10920. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  10921. return -ERANGE;
  10922. vmx->hv_deadline_tsc = tscl + delta_tsc;
  10923. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10924. PIN_BASED_VMX_PREEMPTION_TIMER);
  10925. return delta_tsc == 0;
  10926. }
  10927. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  10928. {
  10929. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10930. vmx->hv_deadline_tsc = -1;
  10931. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10932. PIN_BASED_VMX_PREEMPTION_TIMER);
  10933. }
  10934. #endif
  10935. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  10936. {
  10937. if (!kvm_pause_in_guest(vcpu->kvm))
  10938. shrink_ple_window(vcpu);
  10939. }
  10940. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  10941. struct kvm_memory_slot *slot)
  10942. {
  10943. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  10944. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  10945. }
  10946. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  10947. struct kvm_memory_slot *slot)
  10948. {
  10949. kvm_mmu_slot_set_dirty(kvm, slot);
  10950. }
  10951. static void vmx_flush_log_dirty(struct kvm *kvm)
  10952. {
  10953. kvm_flush_pml_buffers(kvm);
  10954. }
  10955. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  10956. {
  10957. struct vmcs12 *vmcs12;
  10958. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10959. gpa_t gpa;
  10960. struct page *page = NULL;
  10961. u64 *pml_address;
  10962. if (is_guest_mode(vcpu)) {
  10963. WARN_ON_ONCE(vmx->nested.pml_full);
  10964. /*
  10965. * Check if PML is enabled for the nested guest.
  10966. * Whether eptp bit 6 is set is already checked
  10967. * as part of A/D emulation.
  10968. */
  10969. vmcs12 = get_vmcs12(vcpu);
  10970. if (!nested_cpu_has_pml(vmcs12))
  10971. return 0;
  10972. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  10973. vmx->nested.pml_full = true;
  10974. return 1;
  10975. }
  10976. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  10977. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  10978. if (is_error_page(page))
  10979. return 0;
  10980. pml_address = kmap(page);
  10981. pml_address[vmcs12->guest_pml_index--] = gpa;
  10982. kunmap(page);
  10983. kvm_release_page_clean(page);
  10984. }
  10985. return 0;
  10986. }
  10987. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  10988. struct kvm_memory_slot *memslot,
  10989. gfn_t offset, unsigned long mask)
  10990. {
  10991. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  10992. }
  10993. static void __pi_post_block(struct kvm_vcpu *vcpu)
  10994. {
  10995. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10996. struct pi_desc old, new;
  10997. unsigned int dest;
  10998. do {
  10999. old.control = new.control = pi_desc->control;
  11000. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  11001. "Wakeup handler not enabled while the VCPU is blocked\n");
  11002. dest = cpu_physical_id(vcpu->cpu);
  11003. if (x2apic_enabled())
  11004. new.ndst = dest;
  11005. else
  11006. new.ndst = (dest << 8) & 0xFF00;
  11007. /* set 'NV' to 'notification vector' */
  11008. new.nv = POSTED_INTR_VECTOR;
  11009. } while (cmpxchg64(&pi_desc->control, old.control,
  11010. new.control) != old.control);
  11011. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  11012. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11013. list_del(&vcpu->blocked_vcpu_list);
  11014. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11015. vcpu->pre_pcpu = -1;
  11016. }
  11017. }
  11018. /*
  11019. * This routine does the following things for vCPU which is going
  11020. * to be blocked if VT-d PI is enabled.
  11021. * - Store the vCPU to the wakeup list, so when interrupts happen
  11022. * we can find the right vCPU to wake up.
  11023. * - Change the Posted-interrupt descriptor as below:
  11024. * 'NDST' <-- vcpu->pre_pcpu
  11025. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  11026. * - If 'ON' is set during this process, which means at least one
  11027. * interrupt is posted for this vCPU, we cannot block it, in
  11028. * this case, return 1, otherwise, return 0.
  11029. *
  11030. */
  11031. static int pi_pre_block(struct kvm_vcpu *vcpu)
  11032. {
  11033. unsigned int dest;
  11034. struct pi_desc old, new;
  11035. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11036. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  11037. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11038. !kvm_vcpu_apicv_active(vcpu))
  11039. return 0;
  11040. WARN_ON(irqs_disabled());
  11041. local_irq_disable();
  11042. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  11043. vcpu->pre_pcpu = vcpu->cpu;
  11044. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11045. list_add_tail(&vcpu->blocked_vcpu_list,
  11046. &per_cpu(blocked_vcpu_on_cpu,
  11047. vcpu->pre_pcpu));
  11048. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11049. }
  11050. do {
  11051. old.control = new.control = pi_desc->control;
  11052. WARN((pi_desc->sn == 1),
  11053. "Warning: SN field of posted-interrupts "
  11054. "is set before blocking\n");
  11055. /*
  11056. * Since vCPU can be preempted during this process,
  11057. * vcpu->cpu could be different with pre_pcpu, we
  11058. * need to set pre_pcpu as the destination of wakeup
  11059. * notification event, then we can find the right vCPU
  11060. * to wakeup in wakeup handler if interrupts happen
  11061. * when the vCPU is in blocked state.
  11062. */
  11063. dest = cpu_physical_id(vcpu->pre_pcpu);
  11064. if (x2apic_enabled())
  11065. new.ndst = dest;
  11066. else
  11067. new.ndst = (dest << 8) & 0xFF00;
  11068. /* set 'NV' to 'wakeup vector' */
  11069. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  11070. } while (cmpxchg64(&pi_desc->control, old.control,
  11071. new.control) != old.control);
  11072. /* We should not block the vCPU if an interrupt is posted for it. */
  11073. if (pi_test_on(pi_desc) == 1)
  11074. __pi_post_block(vcpu);
  11075. local_irq_enable();
  11076. return (vcpu->pre_pcpu == -1);
  11077. }
  11078. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  11079. {
  11080. if (pi_pre_block(vcpu))
  11081. return 1;
  11082. if (kvm_lapic_hv_timer_in_use(vcpu))
  11083. kvm_lapic_switch_to_sw_timer(vcpu);
  11084. return 0;
  11085. }
  11086. static void pi_post_block(struct kvm_vcpu *vcpu)
  11087. {
  11088. if (vcpu->pre_pcpu == -1)
  11089. return;
  11090. WARN_ON(irqs_disabled());
  11091. local_irq_disable();
  11092. __pi_post_block(vcpu);
  11093. local_irq_enable();
  11094. }
  11095. static void vmx_post_block(struct kvm_vcpu *vcpu)
  11096. {
  11097. if (kvm_x86_ops->set_hv_timer)
  11098. kvm_lapic_switch_to_hv_timer(vcpu);
  11099. pi_post_block(vcpu);
  11100. }
  11101. /*
  11102. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  11103. *
  11104. * @kvm: kvm
  11105. * @host_irq: host irq of the interrupt
  11106. * @guest_irq: gsi of the interrupt
  11107. * @set: set or unset PI
  11108. * returns 0 on success, < 0 on failure
  11109. */
  11110. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  11111. uint32_t guest_irq, bool set)
  11112. {
  11113. struct kvm_kernel_irq_routing_entry *e;
  11114. struct kvm_irq_routing_table *irq_rt;
  11115. struct kvm_lapic_irq irq;
  11116. struct kvm_vcpu *vcpu;
  11117. struct vcpu_data vcpu_info;
  11118. int idx, ret = 0;
  11119. if (!kvm_arch_has_assigned_device(kvm) ||
  11120. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11121. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  11122. return 0;
  11123. idx = srcu_read_lock(&kvm->irq_srcu);
  11124. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  11125. if (guest_irq >= irq_rt->nr_rt_entries ||
  11126. hlist_empty(&irq_rt->map[guest_irq])) {
  11127. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  11128. guest_irq, irq_rt->nr_rt_entries);
  11129. goto out;
  11130. }
  11131. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  11132. if (e->type != KVM_IRQ_ROUTING_MSI)
  11133. continue;
  11134. /*
  11135. * VT-d PI cannot support posting multicast/broadcast
  11136. * interrupts to a vCPU, we still use interrupt remapping
  11137. * for these kind of interrupts.
  11138. *
  11139. * For lowest-priority interrupts, we only support
  11140. * those with single CPU as the destination, e.g. user
  11141. * configures the interrupts via /proc/irq or uses
  11142. * irqbalance to make the interrupts single-CPU.
  11143. *
  11144. * We will support full lowest-priority interrupt later.
  11145. */
  11146. kvm_set_msi_irq(kvm, e, &irq);
  11147. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  11148. /*
  11149. * Make sure the IRTE is in remapped mode if
  11150. * we don't handle it in posted mode.
  11151. */
  11152. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11153. if (ret < 0) {
  11154. printk(KERN_INFO
  11155. "failed to back to remapped mode, irq: %u\n",
  11156. host_irq);
  11157. goto out;
  11158. }
  11159. continue;
  11160. }
  11161. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  11162. vcpu_info.vector = irq.vector;
  11163. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  11164. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  11165. if (set)
  11166. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  11167. else
  11168. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11169. if (ret < 0) {
  11170. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  11171. __func__);
  11172. goto out;
  11173. }
  11174. }
  11175. ret = 0;
  11176. out:
  11177. srcu_read_unlock(&kvm->irq_srcu, idx);
  11178. return ret;
  11179. }
  11180. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  11181. {
  11182. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  11183. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  11184. FEATURE_CONTROL_LMCE;
  11185. else
  11186. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  11187. ~FEATURE_CONTROL_LMCE;
  11188. }
  11189. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  11190. {
  11191. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  11192. if (to_vmx(vcpu)->nested.nested_run_pending)
  11193. return 0;
  11194. return 1;
  11195. }
  11196. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  11197. {
  11198. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11199. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  11200. if (vmx->nested.smm.guest_mode)
  11201. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11202. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  11203. vmx->nested.vmxon = false;
  11204. vmx_clear_hlt(vcpu);
  11205. return 0;
  11206. }
  11207. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  11208. {
  11209. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11210. int ret;
  11211. if (vmx->nested.smm.vmxon) {
  11212. vmx->nested.vmxon = true;
  11213. vmx->nested.smm.vmxon = false;
  11214. }
  11215. if (vmx->nested.smm.guest_mode) {
  11216. vcpu->arch.hflags &= ~HF_SMM_MASK;
  11217. ret = enter_vmx_non_root_mode(vcpu);
  11218. vcpu->arch.hflags |= HF_SMM_MASK;
  11219. if (ret)
  11220. return ret;
  11221. vmx->nested.smm.guest_mode = false;
  11222. }
  11223. return 0;
  11224. }
  11225. static int enable_smi_window(struct kvm_vcpu *vcpu)
  11226. {
  11227. return 0;
  11228. }
  11229. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  11230. .cpu_has_kvm_support = cpu_has_kvm_support,
  11231. .disabled_by_bios = vmx_disabled_by_bios,
  11232. .hardware_setup = hardware_setup,
  11233. .hardware_unsetup = hardware_unsetup,
  11234. .check_processor_compatibility = vmx_check_processor_compat,
  11235. .hardware_enable = hardware_enable,
  11236. .hardware_disable = hardware_disable,
  11237. .cpu_has_accelerated_tpr = report_flexpriority,
  11238. .has_emulated_msr = vmx_has_emulated_msr,
  11239. .vm_init = vmx_vm_init,
  11240. .vm_alloc = vmx_vm_alloc,
  11241. .vm_free = vmx_vm_free,
  11242. .vcpu_create = vmx_create_vcpu,
  11243. .vcpu_free = vmx_free_vcpu,
  11244. .vcpu_reset = vmx_vcpu_reset,
  11245. .prepare_guest_switch = vmx_save_host_state,
  11246. .vcpu_load = vmx_vcpu_load,
  11247. .vcpu_put = vmx_vcpu_put,
  11248. .update_bp_intercept = update_exception_bitmap,
  11249. .get_msr_feature = vmx_get_msr_feature,
  11250. .get_msr = vmx_get_msr,
  11251. .set_msr = vmx_set_msr,
  11252. .get_segment_base = vmx_get_segment_base,
  11253. .get_segment = vmx_get_segment,
  11254. .set_segment = vmx_set_segment,
  11255. .get_cpl = vmx_get_cpl,
  11256. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  11257. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  11258. .decache_cr3 = vmx_decache_cr3,
  11259. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  11260. .set_cr0 = vmx_set_cr0,
  11261. .set_cr3 = vmx_set_cr3,
  11262. .set_cr4 = vmx_set_cr4,
  11263. .set_efer = vmx_set_efer,
  11264. .get_idt = vmx_get_idt,
  11265. .set_idt = vmx_set_idt,
  11266. .get_gdt = vmx_get_gdt,
  11267. .set_gdt = vmx_set_gdt,
  11268. .get_dr6 = vmx_get_dr6,
  11269. .set_dr6 = vmx_set_dr6,
  11270. .set_dr7 = vmx_set_dr7,
  11271. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  11272. .cache_reg = vmx_cache_reg,
  11273. .get_rflags = vmx_get_rflags,
  11274. .set_rflags = vmx_set_rflags,
  11275. .tlb_flush = vmx_flush_tlb,
  11276. .run = vmx_vcpu_run,
  11277. .handle_exit = vmx_handle_exit,
  11278. .skip_emulated_instruction = skip_emulated_instruction,
  11279. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  11280. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  11281. .patch_hypercall = vmx_patch_hypercall,
  11282. .set_irq = vmx_inject_irq,
  11283. .set_nmi = vmx_inject_nmi,
  11284. .queue_exception = vmx_queue_exception,
  11285. .cancel_injection = vmx_cancel_injection,
  11286. .interrupt_allowed = vmx_interrupt_allowed,
  11287. .nmi_allowed = vmx_nmi_allowed,
  11288. .get_nmi_mask = vmx_get_nmi_mask,
  11289. .set_nmi_mask = vmx_set_nmi_mask,
  11290. .enable_nmi_window = enable_nmi_window,
  11291. .enable_irq_window = enable_irq_window,
  11292. .update_cr8_intercept = update_cr8_intercept,
  11293. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  11294. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  11295. .get_enable_apicv = vmx_get_enable_apicv,
  11296. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  11297. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  11298. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  11299. .hwapic_irr_update = vmx_hwapic_irr_update,
  11300. .hwapic_isr_update = vmx_hwapic_isr_update,
  11301. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  11302. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  11303. .set_tss_addr = vmx_set_tss_addr,
  11304. .set_identity_map_addr = vmx_set_identity_map_addr,
  11305. .get_tdp_level = get_ept_level,
  11306. .get_mt_mask = vmx_get_mt_mask,
  11307. .get_exit_info = vmx_get_exit_info,
  11308. .get_lpage_level = vmx_get_lpage_level,
  11309. .cpuid_update = vmx_cpuid_update,
  11310. .rdtscp_supported = vmx_rdtscp_supported,
  11311. .invpcid_supported = vmx_invpcid_supported,
  11312. .set_supported_cpuid = vmx_set_supported_cpuid,
  11313. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  11314. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  11315. .write_tsc_offset = vmx_write_tsc_offset,
  11316. .set_tdp_cr3 = vmx_set_cr3,
  11317. .check_intercept = vmx_check_intercept,
  11318. .handle_external_intr = vmx_handle_external_intr,
  11319. .mpx_supported = vmx_mpx_supported,
  11320. .xsaves_supported = vmx_xsaves_supported,
  11321. .umip_emulated = vmx_umip_emulated,
  11322. .check_nested_events = vmx_check_nested_events,
  11323. .sched_in = vmx_sched_in,
  11324. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  11325. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  11326. .flush_log_dirty = vmx_flush_log_dirty,
  11327. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  11328. .write_log_dirty = vmx_write_pml_buffer,
  11329. .pre_block = vmx_pre_block,
  11330. .post_block = vmx_post_block,
  11331. .pmu_ops = &intel_pmu_ops,
  11332. .update_pi_irte = vmx_update_pi_irte,
  11333. #ifdef CONFIG_X86_64
  11334. .set_hv_timer = vmx_set_hv_timer,
  11335. .cancel_hv_timer = vmx_cancel_hv_timer,
  11336. #endif
  11337. .setup_mce = vmx_setup_mce,
  11338. .smi_allowed = vmx_smi_allowed,
  11339. .pre_enter_smm = vmx_pre_enter_smm,
  11340. .pre_leave_smm = vmx_pre_leave_smm,
  11341. .enable_smi_window = enable_smi_window,
  11342. };
  11343. static int __init vmx_init(void)
  11344. {
  11345. int r;
  11346. #if IS_ENABLED(CONFIG_HYPERV)
  11347. /*
  11348. * Enlightened VMCS usage should be recommended and the host needs
  11349. * to support eVMCS v1 or above. We can also disable eVMCS support
  11350. * with module parameter.
  11351. */
  11352. if (enlightened_vmcs &&
  11353. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  11354. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  11355. KVM_EVMCS_VERSION) {
  11356. int cpu;
  11357. /* Check that we have assist pages on all online CPUs */
  11358. for_each_online_cpu(cpu) {
  11359. if (!hv_get_vp_assist_page(cpu)) {
  11360. enlightened_vmcs = false;
  11361. break;
  11362. }
  11363. }
  11364. if (enlightened_vmcs) {
  11365. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  11366. static_branch_enable(&enable_evmcs);
  11367. }
  11368. } else {
  11369. enlightened_vmcs = false;
  11370. }
  11371. #endif
  11372. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  11373. __alignof__(struct vcpu_vmx), THIS_MODULE);
  11374. if (r)
  11375. return r;
  11376. #ifdef CONFIG_KEXEC_CORE
  11377. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  11378. crash_vmclear_local_loaded_vmcss);
  11379. #endif
  11380. vmx_check_vmcs12_offsets();
  11381. return 0;
  11382. }
  11383. static void __exit vmx_exit(void)
  11384. {
  11385. #ifdef CONFIG_KEXEC_CORE
  11386. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  11387. synchronize_rcu();
  11388. #endif
  11389. kvm_exit();
  11390. #if IS_ENABLED(CONFIG_HYPERV)
  11391. if (static_branch_unlikely(&enable_evmcs)) {
  11392. int cpu;
  11393. struct hv_vp_assist_page *vp_ap;
  11394. /*
  11395. * Reset everything to support using non-enlightened VMCS
  11396. * access later (e.g. when we reload the module with
  11397. * enlightened_vmcs=0)
  11398. */
  11399. for_each_online_cpu(cpu) {
  11400. vp_ap = hv_get_vp_assist_page(cpu);
  11401. if (!vp_ap)
  11402. continue;
  11403. vp_ap->current_nested_vmcs = 0;
  11404. vp_ap->enlighten_vmentry = 0;
  11405. }
  11406. static_branch_disable(&enable_evmcs);
  11407. }
  11408. #endif
  11409. }
  11410. module_init(vmx_init)
  11411. module_exit(vmx_exit)