vmx.c 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446
  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/microcode.h>
  52. #include <asm/nospec-branch.h>
  53. #include "trace.h"
  54. #include "pmu.h"
  55. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  56. #define __ex_clear(x, reg) \
  57. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  58. MODULE_AUTHOR("Qumranet");
  59. MODULE_LICENSE("GPL");
  60. static const struct x86_cpu_id vmx_cpu_id[] = {
  61. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  62. {}
  63. };
  64. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  65. static bool __read_mostly enable_vpid = 1;
  66. module_param_named(vpid, enable_vpid, bool, 0444);
  67. static bool __read_mostly enable_vnmi = 1;
  68. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  69. static bool __read_mostly flexpriority_enabled = 1;
  70. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  71. static bool __read_mostly enable_ept = 1;
  72. module_param_named(ept, enable_ept, bool, S_IRUGO);
  73. static bool __read_mostly enable_unrestricted_guest = 1;
  74. module_param_named(unrestricted_guest,
  75. enable_unrestricted_guest, bool, S_IRUGO);
  76. static bool __read_mostly enable_ept_ad_bits = 1;
  77. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  78. static bool __read_mostly emulate_invalid_guest_state = true;
  79. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  80. static bool __read_mostly fasteoi = 1;
  81. module_param(fasteoi, bool, S_IRUGO);
  82. static bool __read_mostly enable_apicv = 1;
  83. module_param(enable_apicv, bool, S_IRUGO);
  84. static bool __read_mostly enable_shadow_vmcs = 1;
  85. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  86. /*
  87. * If nested=1, nested virtualization is supported, i.e., guests may use
  88. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  89. * use VMX instructions.
  90. */
  91. static bool __read_mostly nested = 0;
  92. module_param(nested, bool, S_IRUGO);
  93. static u64 __read_mostly host_xss;
  94. static bool __read_mostly enable_pml = 1;
  95. module_param_named(pml, enable_pml, bool, S_IRUGO);
  96. #define MSR_TYPE_R 1
  97. #define MSR_TYPE_W 2
  98. #define MSR_TYPE_RW 3
  99. #define MSR_BITMAP_MODE_X2APIC 1
  100. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  101. #define MSR_BITMAP_MODE_LM 4
  102. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  103. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  104. static int __read_mostly cpu_preemption_timer_multi;
  105. static bool __read_mostly enable_preemption_timer = 1;
  106. #ifdef CONFIG_X86_64
  107. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  108. #endif
  109. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  110. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
  111. #define KVM_VM_CR0_ALWAYS_ON \
  112. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  113. #define KVM_CR4_GUEST_OWNED_BITS \
  114. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  115. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  116. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  117. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  118. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  119. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  120. /*
  121. * Hyper-V requires all of these, so mark them as supported even though
  122. * they are just treated the same as all-context.
  123. */
  124. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  125. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  126. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  127. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  128. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  129. /*
  130. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  131. * ple_gap: upper bound on the amount of time between two successive
  132. * executions of PAUSE in a loop. Also indicate if ple enabled.
  133. * According to test, this time is usually smaller than 128 cycles.
  134. * ple_window: upper bound on the amount of time a guest is allowed to execute
  135. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  136. * less than 2^12 cycles
  137. * Time is measured based on a counter that runs at the same rate as the TSC,
  138. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  139. */
  140. #define KVM_VMX_DEFAULT_PLE_GAP 128
  141. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  142. #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
  143. #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
  144. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
  145. INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
  146. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  147. module_param(ple_gap, int, S_IRUGO);
  148. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  149. module_param(ple_window, int, S_IRUGO);
  150. /* Default doubles per-vcpu window every exit. */
  151. static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
  152. module_param(ple_window_grow, int, S_IRUGO);
  153. /* Default resets per-vcpu window every exit to ple_window. */
  154. static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
  155. module_param(ple_window_shrink, int, S_IRUGO);
  156. /* Default is to compute the maximum so we can never overflow. */
  157. static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  158. static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  159. module_param(ple_window_max, int, S_IRUGO);
  160. extern const ulong vmx_return;
  161. #define NR_AUTOLOAD_MSRS 8
  162. struct vmcs {
  163. u32 revision_id;
  164. u32 abort;
  165. char data[0];
  166. };
  167. /*
  168. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  169. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  170. * loaded on this CPU (so we can clear them if the CPU goes down).
  171. */
  172. struct loaded_vmcs {
  173. struct vmcs *vmcs;
  174. struct vmcs *shadow_vmcs;
  175. int cpu;
  176. bool launched;
  177. bool nmi_known_unmasked;
  178. unsigned long vmcs_host_cr3; /* May not match real cr3 */
  179. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  180. /* Support for vnmi-less CPUs */
  181. int soft_vnmi_blocked;
  182. ktime_t entry_time;
  183. s64 vnmi_blocked_time;
  184. unsigned long *msr_bitmap;
  185. struct list_head loaded_vmcss_on_cpu_link;
  186. };
  187. struct shared_msr_entry {
  188. unsigned index;
  189. u64 data;
  190. u64 mask;
  191. };
  192. /*
  193. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  194. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  195. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  196. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  197. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  198. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  199. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  200. * underlying hardware which will be used to run L2.
  201. * This structure is packed to ensure that its layout is identical across
  202. * machines (necessary for live migration).
  203. * If there are changes in this struct, VMCS12_REVISION must be changed.
  204. */
  205. typedef u64 natural_width;
  206. struct __packed vmcs12 {
  207. /* According to the Intel spec, a VMCS region must start with the
  208. * following two fields. Then follow implementation-specific data.
  209. */
  210. u32 revision_id;
  211. u32 abort;
  212. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  213. u32 padding[7]; /* room for future expansion */
  214. u64 io_bitmap_a;
  215. u64 io_bitmap_b;
  216. u64 msr_bitmap;
  217. u64 vm_exit_msr_store_addr;
  218. u64 vm_exit_msr_load_addr;
  219. u64 vm_entry_msr_load_addr;
  220. u64 tsc_offset;
  221. u64 virtual_apic_page_addr;
  222. u64 apic_access_addr;
  223. u64 posted_intr_desc_addr;
  224. u64 vm_function_control;
  225. u64 ept_pointer;
  226. u64 eoi_exit_bitmap0;
  227. u64 eoi_exit_bitmap1;
  228. u64 eoi_exit_bitmap2;
  229. u64 eoi_exit_bitmap3;
  230. u64 eptp_list_address;
  231. u64 xss_exit_bitmap;
  232. u64 guest_physical_address;
  233. u64 vmcs_link_pointer;
  234. u64 pml_address;
  235. u64 guest_ia32_debugctl;
  236. u64 guest_ia32_pat;
  237. u64 guest_ia32_efer;
  238. u64 guest_ia32_perf_global_ctrl;
  239. u64 guest_pdptr0;
  240. u64 guest_pdptr1;
  241. u64 guest_pdptr2;
  242. u64 guest_pdptr3;
  243. u64 guest_bndcfgs;
  244. u64 host_ia32_pat;
  245. u64 host_ia32_efer;
  246. u64 host_ia32_perf_global_ctrl;
  247. u64 padding64[8]; /* room for future expansion */
  248. /*
  249. * To allow migration of L1 (complete with its L2 guests) between
  250. * machines of different natural widths (32 or 64 bit), we cannot have
  251. * unsigned long fields with no explict size. We use u64 (aliased
  252. * natural_width) instead. Luckily, x86 is little-endian.
  253. */
  254. natural_width cr0_guest_host_mask;
  255. natural_width cr4_guest_host_mask;
  256. natural_width cr0_read_shadow;
  257. natural_width cr4_read_shadow;
  258. natural_width cr3_target_value0;
  259. natural_width cr3_target_value1;
  260. natural_width cr3_target_value2;
  261. natural_width cr3_target_value3;
  262. natural_width exit_qualification;
  263. natural_width guest_linear_address;
  264. natural_width guest_cr0;
  265. natural_width guest_cr3;
  266. natural_width guest_cr4;
  267. natural_width guest_es_base;
  268. natural_width guest_cs_base;
  269. natural_width guest_ss_base;
  270. natural_width guest_ds_base;
  271. natural_width guest_fs_base;
  272. natural_width guest_gs_base;
  273. natural_width guest_ldtr_base;
  274. natural_width guest_tr_base;
  275. natural_width guest_gdtr_base;
  276. natural_width guest_idtr_base;
  277. natural_width guest_dr7;
  278. natural_width guest_rsp;
  279. natural_width guest_rip;
  280. natural_width guest_rflags;
  281. natural_width guest_pending_dbg_exceptions;
  282. natural_width guest_sysenter_esp;
  283. natural_width guest_sysenter_eip;
  284. natural_width host_cr0;
  285. natural_width host_cr3;
  286. natural_width host_cr4;
  287. natural_width host_fs_base;
  288. natural_width host_gs_base;
  289. natural_width host_tr_base;
  290. natural_width host_gdtr_base;
  291. natural_width host_idtr_base;
  292. natural_width host_ia32_sysenter_esp;
  293. natural_width host_ia32_sysenter_eip;
  294. natural_width host_rsp;
  295. natural_width host_rip;
  296. natural_width paddingl[8]; /* room for future expansion */
  297. u32 pin_based_vm_exec_control;
  298. u32 cpu_based_vm_exec_control;
  299. u32 exception_bitmap;
  300. u32 page_fault_error_code_mask;
  301. u32 page_fault_error_code_match;
  302. u32 cr3_target_count;
  303. u32 vm_exit_controls;
  304. u32 vm_exit_msr_store_count;
  305. u32 vm_exit_msr_load_count;
  306. u32 vm_entry_controls;
  307. u32 vm_entry_msr_load_count;
  308. u32 vm_entry_intr_info_field;
  309. u32 vm_entry_exception_error_code;
  310. u32 vm_entry_instruction_len;
  311. u32 tpr_threshold;
  312. u32 secondary_vm_exec_control;
  313. u32 vm_instruction_error;
  314. u32 vm_exit_reason;
  315. u32 vm_exit_intr_info;
  316. u32 vm_exit_intr_error_code;
  317. u32 idt_vectoring_info_field;
  318. u32 idt_vectoring_error_code;
  319. u32 vm_exit_instruction_len;
  320. u32 vmx_instruction_info;
  321. u32 guest_es_limit;
  322. u32 guest_cs_limit;
  323. u32 guest_ss_limit;
  324. u32 guest_ds_limit;
  325. u32 guest_fs_limit;
  326. u32 guest_gs_limit;
  327. u32 guest_ldtr_limit;
  328. u32 guest_tr_limit;
  329. u32 guest_gdtr_limit;
  330. u32 guest_idtr_limit;
  331. u32 guest_es_ar_bytes;
  332. u32 guest_cs_ar_bytes;
  333. u32 guest_ss_ar_bytes;
  334. u32 guest_ds_ar_bytes;
  335. u32 guest_fs_ar_bytes;
  336. u32 guest_gs_ar_bytes;
  337. u32 guest_ldtr_ar_bytes;
  338. u32 guest_tr_ar_bytes;
  339. u32 guest_interruptibility_info;
  340. u32 guest_activity_state;
  341. u32 guest_sysenter_cs;
  342. u32 host_ia32_sysenter_cs;
  343. u32 vmx_preemption_timer_value;
  344. u32 padding32[7]; /* room for future expansion */
  345. u16 virtual_processor_id;
  346. u16 posted_intr_nv;
  347. u16 guest_es_selector;
  348. u16 guest_cs_selector;
  349. u16 guest_ss_selector;
  350. u16 guest_ds_selector;
  351. u16 guest_fs_selector;
  352. u16 guest_gs_selector;
  353. u16 guest_ldtr_selector;
  354. u16 guest_tr_selector;
  355. u16 guest_intr_status;
  356. u16 guest_pml_index;
  357. u16 host_es_selector;
  358. u16 host_cs_selector;
  359. u16 host_ss_selector;
  360. u16 host_ds_selector;
  361. u16 host_fs_selector;
  362. u16 host_gs_selector;
  363. u16 host_tr_selector;
  364. };
  365. /*
  366. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  367. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  368. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  369. */
  370. #define VMCS12_REVISION 0x11e57ed0
  371. /*
  372. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  373. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  374. * current implementation, 4K are reserved to avoid future complications.
  375. */
  376. #define VMCS12_SIZE 0x1000
  377. /*
  378. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  379. * supported VMCS12 field encoding.
  380. */
  381. #define VMCS12_MAX_FIELD_INDEX 0x17
  382. /*
  383. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  384. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  385. */
  386. struct nested_vmx {
  387. /* Has the level1 guest done vmxon? */
  388. bool vmxon;
  389. gpa_t vmxon_ptr;
  390. bool pml_full;
  391. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  392. gpa_t current_vmptr;
  393. /*
  394. * Cache of the guest's VMCS, existing outside of guest memory.
  395. * Loaded from guest memory during VMPTRLD. Flushed to guest
  396. * memory during VMCLEAR and VMPTRLD.
  397. */
  398. struct vmcs12 *cached_vmcs12;
  399. /*
  400. * Indicates if the shadow vmcs must be updated with the
  401. * data hold by vmcs12
  402. */
  403. bool sync_shadow_vmcs;
  404. bool dirty_vmcs12;
  405. bool change_vmcs01_virtual_x2apic_mode;
  406. /* L2 must run next, and mustn't decide to exit to L1. */
  407. bool nested_run_pending;
  408. struct loaded_vmcs vmcs02;
  409. /*
  410. * Guest pages referred to in the vmcs02 with host-physical
  411. * pointers, so we must keep them pinned while L2 runs.
  412. */
  413. struct page *apic_access_page;
  414. struct page *virtual_apic_page;
  415. struct page *pi_desc_page;
  416. struct pi_desc *pi_desc;
  417. bool pi_pending;
  418. u16 posted_intr_nv;
  419. struct hrtimer preemption_timer;
  420. bool preemption_timer_expired;
  421. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  422. u64 vmcs01_debugctl;
  423. u16 vpid02;
  424. u16 last_vpid;
  425. /*
  426. * We only store the "true" versions of the VMX capability MSRs. We
  427. * generate the "non-true" versions by setting the must-be-1 bits
  428. * according to the SDM.
  429. */
  430. u32 nested_vmx_procbased_ctls_low;
  431. u32 nested_vmx_procbased_ctls_high;
  432. u32 nested_vmx_secondary_ctls_low;
  433. u32 nested_vmx_secondary_ctls_high;
  434. u32 nested_vmx_pinbased_ctls_low;
  435. u32 nested_vmx_pinbased_ctls_high;
  436. u32 nested_vmx_exit_ctls_low;
  437. u32 nested_vmx_exit_ctls_high;
  438. u32 nested_vmx_entry_ctls_low;
  439. u32 nested_vmx_entry_ctls_high;
  440. u32 nested_vmx_misc_low;
  441. u32 nested_vmx_misc_high;
  442. u32 nested_vmx_ept_caps;
  443. u32 nested_vmx_vpid_caps;
  444. u64 nested_vmx_basic;
  445. u64 nested_vmx_cr0_fixed0;
  446. u64 nested_vmx_cr0_fixed1;
  447. u64 nested_vmx_cr4_fixed0;
  448. u64 nested_vmx_cr4_fixed1;
  449. u64 nested_vmx_vmcs_enum;
  450. u64 nested_vmx_vmfunc_controls;
  451. /* SMM related state */
  452. struct {
  453. /* in VMX operation on SMM entry? */
  454. bool vmxon;
  455. /* in guest mode on SMM entry? */
  456. bool guest_mode;
  457. } smm;
  458. };
  459. #define POSTED_INTR_ON 0
  460. #define POSTED_INTR_SN 1
  461. /* Posted-Interrupt Descriptor */
  462. struct pi_desc {
  463. u32 pir[8]; /* Posted interrupt requested */
  464. union {
  465. struct {
  466. /* bit 256 - Outstanding Notification */
  467. u16 on : 1,
  468. /* bit 257 - Suppress Notification */
  469. sn : 1,
  470. /* bit 271:258 - Reserved */
  471. rsvd_1 : 14;
  472. /* bit 279:272 - Notification Vector */
  473. u8 nv;
  474. /* bit 287:280 - Reserved */
  475. u8 rsvd_2;
  476. /* bit 319:288 - Notification Destination */
  477. u32 ndst;
  478. };
  479. u64 control;
  480. };
  481. u32 rsvd[6];
  482. } __aligned(64);
  483. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  484. {
  485. return test_and_set_bit(POSTED_INTR_ON,
  486. (unsigned long *)&pi_desc->control);
  487. }
  488. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  489. {
  490. return test_and_clear_bit(POSTED_INTR_ON,
  491. (unsigned long *)&pi_desc->control);
  492. }
  493. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  494. {
  495. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  496. }
  497. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  498. {
  499. return clear_bit(POSTED_INTR_SN,
  500. (unsigned long *)&pi_desc->control);
  501. }
  502. static inline void pi_set_sn(struct pi_desc *pi_desc)
  503. {
  504. return set_bit(POSTED_INTR_SN,
  505. (unsigned long *)&pi_desc->control);
  506. }
  507. static inline void pi_clear_on(struct pi_desc *pi_desc)
  508. {
  509. clear_bit(POSTED_INTR_ON,
  510. (unsigned long *)&pi_desc->control);
  511. }
  512. static inline int pi_test_on(struct pi_desc *pi_desc)
  513. {
  514. return test_bit(POSTED_INTR_ON,
  515. (unsigned long *)&pi_desc->control);
  516. }
  517. static inline int pi_test_sn(struct pi_desc *pi_desc)
  518. {
  519. return test_bit(POSTED_INTR_SN,
  520. (unsigned long *)&pi_desc->control);
  521. }
  522. struct vcpu_vmx {
  523. struct kvm_vcpu vcpu;
  524. unsigned long host_rsp;
  525. u8 fail;
  526. u8 msr_bitmap_mode;
  527. u32 exit_intr_info;
  528. u32 idt_vectoring_info;
  529. ulong rflags;
  530. struct shared_msr_entry *guest_msrs;
  531. int nmsrs;
  532. int save_nmsrs;
  533. unsigned long host_idt_base;
  534. #ifdef CONFIG_X86_64
  535. u64 msr_host_kernel_gs_base;
  536. u64 msr_guest_kernel_gs_base;
  537. #endif
  538. u64 arch_capabilities;
  539. u64 spec_ctrl;
  540. u32 vm_entry_controls_shadow;
  541. u32 vm_exit_controls_shadow;
  542. u32 secondary_exec_control;
  543. /*
  544. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  545. * non-nested (L1) guest, it always points to vmcs01. For a nested
  546. * guest (L2), it points to a different VMCS.
  547. */
  548. struct loaded_vmcs vmcs01;
  549. struct loaded_vmcs *loaded_vmcs;
  550. bool __launched; /* temporary, used in vmx_vcpu_run */
  551. struct msr_autoload {
  552. unsigned nr;
  553. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  554. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  555. } msr_autoload;
  556. struct {
  557. int loaded;
  558. u16 fs_sel, gs_sel, ldt_sel;
  559. #ifdef CONFIG_X86_64
  560. u16 ds_sel, es_sel;
  561. #endif
  562. int gs_ldt_reload_needed;
  563. int fs_reload_needed;
  564. u64 msr_host_bndcfgs;
  565. } host_state;
  566. struct {
  567. int vm86_active;
  568. ulong save_rflags;
  569. struct kvm_segment segs[8];
  570. } rmode;
  571. struct {
  572. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  573. struct kvm_save_segment {
  574. u16 selector;
  575. unsigned long base;
  576. u32 limit;
  577. u32 ar;
  578. } seg[8];
  579. } segment_cache;
  580. int vpid;
  581. bool emulation_required;
  582. u32 exit_reason;
  583. /* Posted interrupt descriptor */
  584. struct pi_desc pi_desc;
  585. /* Support for a guest hypervisor (nested VMX) */
  586. struct nested_vmx nested;
  587. /* Dynamic PLE window. */
  588. int ple_window;
  589. bool ple_window_dirty;
  590. /* Support for PML */
  591. #define PML_ENTITY_NUM 512
  592. struct page *pml_pg;
  593. /* apic deadline value in host tsc */
  594. u64 hv_deadline_tsc;
  595. u64 current_tsc_ratio;
  596. u32 host_pkru;
  597. unsigned long host_debugctlmsr;
  598. /*
  599. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  600. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  601. * in msr_ia32_feature_control_valid_bits.
  602. */
  603. u64 msr_ia32_feature_control;
  604. u64 msr_ia32_feature_control_valid_bits;
  605. };
  606. enum segment_cache_field {
  607. SEG_FIELD_SEL = 0,
  608. SEG_FIELD_BASE = 1,
  609. SEG_FIELD_LIMIT = 2,
  610. SEG_FIELD_AR = 3,
  611. SEG_FIELD_NR = 4
  612. };
  613. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  614. {
  615. return container_of(vcpu, struct vcpu_vmx, vcpu);
  616. }
  617. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  618. {
  619. return &(to_vmx(vcpu)->pi_desc);
  620. }
  621. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  622. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  623. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  624. #define FIELD64(number, name) \
  625. FIELD(number, name), \
  626. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  627. static u16 shadow_read_only_fields[] = {
  628. #define SHADOW_FIELD_RO(x) x,
  629. #include "vmx_shadow_fields.h"
  630. };
  631. static int max_shadow_read_only_fields =
  632. ARRAY_SIZE(shadow_read_only_fields);
  633. static u16 shadow_read_write_fields[] = {
  634. #define SHADOW_FIELD_RW(x) x,
  635. #include "vmx_shadow_fields.h"
  636. };
  637. static int max_shadow_read_write_fields =
  638. ARRAY_SIZE(shadow_read_write_fields);
  639. static const unsigned short vmcs_field_to_offset_table[] = {
  640. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  641. FIELD(POSTED_INTR_NV, posted_intr_nv),
  642. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  643. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  644. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  645. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  646. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  647. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  648. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  649. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  650. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  651. FIELD(GUEST_PML_INDEX, guest_pml_index),
  652. FIELD(HOST_ES_SELECTOR, host_es_selector),
  653. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  654. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  655. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  656. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  657. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  658. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  659. FIELD64(IO_BITMAP_A, io_bitmap_a),
  660. FIELD64(IO_BITMAP_B, io_bitmap_b),
  661. FIELD64(MSR_BITMAP, msr_bitmap),
  662. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  663. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  664. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  665. FIELD64(TSC_OFFSET, tsc_offset),
  666. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  667. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  668. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  669. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  670. FIELD64(EPT_POINTER, ept_pointer),
  671. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  672. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  673. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  674. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  675. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  676. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  677. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  678. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  679. FIELD64(PML_ADDRESS, pml_address),
  680. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  681. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  682. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  683. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  684. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  685. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  686. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  687. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  688. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  689. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  690. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  691. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  692. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  693. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  694. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  695. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  696. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  697. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  698. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  699. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  700. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  701. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  702. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  703. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  704. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  705. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  706. FIELD(TPR_THRESHOLD, tpr_threshold),
  707. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  708. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  709. FIELD(VM_EXIT_REASON, vm_exit_reason),
  710. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  711. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  712. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  713. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  714. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  715. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  716. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  717. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  718. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  719. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  720. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  721. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  722. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  723. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  724. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  725. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  726. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  727. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  728. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  729. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  730. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  731. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  732. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  733. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  734. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  735. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  736. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  737. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  738. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  739. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  740. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  741. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  742. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  743. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  744. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  745. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  746. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  747. FIELD(EXIT_QUALIFICATION, exit_qualification),
  748. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  749. FIELD(GUEST_CR0, guest_cr0),
  750. FIELD(GUEST_CR3, guest_cr3),
  751. FIELD(GUEST_CR4, guest_cr4),
  752. FIELD(GUEST_ES_BASE, guest_es_base),
  753. FIELD(GUEST_CS_BASE, guest_cs_base),
  754. FIELD(GUEST_SS_BASE, guest_ss_base),
  755. FIELD(GUEST_DS_BASE, guest_ds_base),
  756. FIELD(GUEST_FS_BASE, guest_fs_base),
  757. FIELD(GUEST_GS_BASE, guest_gs_base),
  758. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  759. FIELD(GUEST_TR_BASE, guest_tr_base),
  760. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  761. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  762. FIELD(GUEST_DR7, guest_dr7),
  763. FIELD(GUEST_RSP, guest_rsp),
  764. FIELD(GUEST_RIP, guest_rip),
  765. FIELD(GUEST_RFLAGS, guest_rflags),
  766. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  767. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  768. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  769. FIELD(HOST_CR0, host_cr0),
  770. FIELD(HOST_CR3, host_cr3),
  771. FIELD(HOST_CR4, host_cr4),
  772. FIELD(HOST_FS_BASE, host_fs_base),
  773. FIELD(HOST_GS_BASE, host_gs_base),
  774. FIELD(HOST_TR_BASE, host_tr_base),
  775. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  776. FIELD(HOST_IDTR_BASE, host_idtr_base),
  777. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  778. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  779. FIELD(HOST_RSP, host_rsp),
  780. FIELD(HOST_RIP, host_rip),
  781. };
  782. static inline short vmcs_field_to_offset(unsigned long field)
  783. {
  784. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  785. unsigned short offset;
  786. unsigned index;
  787. if (field >> 15)
  788. return -ENOENT;
  789. index = ROL16(field, 6);
  790. if (index >= size)
  791. return -ENOENT;
  792. index = array_index_nospec(index, size);
  793. offset = vmcs_field_to_offset_table[index];
  794. if (offset == 0)
  795. return -ENOENT;
  796. return offset;
  797. }
  798. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  799. {
  800. return to_vmx(vcpu)->nested.cached_vmcs12;
  801. }
  802. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  803. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  804. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  805. static bool vmx_xsaves_supported(void);
  806. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  807. struct kvm_segment *var, int seg);
  808. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  809. struct kvm_segment *var, int seg);
  810. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  811. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  812. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  813. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  814. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  815. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  816. u16 error_code);
  817. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  818. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  819. u32 msr, int type);
  820. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  821. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  822. /*
  823. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  824. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  825. */
  826. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  827. /*
  828. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  829. * can find which vCPU should be waken up.
  830. */
  831. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  832. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  833. enum {
  834. VMX_VMREAD_BITMAP,
  835. VMX_VMWRITE_BITMAP,
  836. VMX_BITMAP_NR
  837. };
  838. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  839. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  840. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  841. static bool cpu_has_load_ia32_efer;
  842. static bool cpu_has_load_perf_global_ctrl;
  843. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  844. static DEFINE_SPINLOCK(vmx_vpid_lock);
  845. static struct vmcs_config {
  846. int size;
  847. int order;
  848. u32 basic_cap;
  849. u32 revision_id;
  850. u32 pin_based_exec_ctrl;
  851. u32 cpu_based_exec_ctrl;
  852. u32 cpu_based_2nd_exec_ctrl;
  853. u32 vmexit_ctrl;
  854. u32 vmentry_ctrl;
  855. } vmcs_config;
  856. static struct vmx_capability {
  857. u32 ept;
  858. u32 vpid;
  859. } vmx_capability;
  860. #define VMX_SEGMENT_FIELD(seg) \
  861. [VCPU_SREG_##seg] = { \
  862. .selector = GUEST_##seg##_SELECTOR, \
  863. .base = GUEST_##seg##_BASE, \
  864. .limit = GUEST_##seg##_LIMIT, \
  865. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  866. }
  867. static const struct kvm_vmx_segment_field {
  868. unsigned selector;
  869. unsigned base;
  870. unsigned limit;
  871. unsigned ar_bytes;
  872. } kvm_vmx_segment_fields[] = {
  873. VMX_SEGMENT_FIELD(CS),
  874. VMX_SEGMENT_FIELD(DS),
  875. VMX_SEGMENT_FIELD(ES),
  876. VMX_SEGMENT_FIELD(FS),
  877. VMX_SEGMENT_FIELD(GS),
  878. VMX_SEGMENT_FIELD(SS),
  879. VMX_SEGMENT_FIELD(TR),
  880. VMX_SEGMENT_FIELD(LDTR),
  881. };
  882. static u64 host_efer;
  883. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  884. /*
  885. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  886. * away by decrementing the array size.
  887. */
  888. static const u32 vmx_msr_index[] = {
  889. #ifdef CONFIG_X86_64
  890. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  891. #endif
  892. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  893. };
  894. static inline bool is_exception_n(u32 intr_info, u8 vector)
  895. {
  896. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  897. INTR_INFO_VALID_MASK)) ==
  898. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  899. }
  900. static inline bool is_debug(u32 intr_info)
  901. {
  902. return is_exception_n(intr_info, DB_VECTOR);
  903. }
  904. static inline bool is_breakpoint(u32 intr_info)
  905. {
  906. return is_exception_n(intr_info, BP_VECTOR);
  907. }
  908. static inline bool is_page_fault(u32 intr_info)
  909. {
  910. return is_exception_n(intr_info, PF_VECTOR);
  911. }
  912. static inline bool is_no_device(u32 intr_info)
  913. {
  914. return is_exception_n(intr_info, NM_VECTOR);
  915. }
  916. static inline bool is_invalid_opcode(u32 intr_info)
  917. {
  918. return is_exception_n(intr_info, UD_VECTOR);
  919. }
  920. static inline bool is_external_interrupt(u32 intr_info)
  921. {
  922. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  923. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  924. }
  925. static inline bool is_machine_check(u32 intr_info)
  926. {
  927. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  928. INTR_INFO_VALID_MASK)) ==
  929. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  930. }
  931. static inline bool cpu_has_vmx_msr_bitmap(void)
  932. {
  933. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  934. }
  935. static inline bool cpu_has_vmx_tpr_shadow(void)
  936. {
  937. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  938. }
  939. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  940. {
  941. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  942. }
  943. static inline bool cpu_has_secondary_exec_ctrls(void)
  944. {
  945. return vmcs_config.cpu_based_exec_ctrl &
  946. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  947. }
  948. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  949. {
  950. return vmcs_config.cpu_based_2nd_exec_ctrl &
  951. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  952. }
  953. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  954. {
  955. return vmcs_config.cpu_based_2nd_exec_ctrl &
  956. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  957. }
  958. static inline bool cpu_has_vmx_apic_register_virt(void)
  959. {
  960. return vmcs_config.cpu_based_2nd_exec_ctrl &
  961. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  962. }
  963. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  964. {
  965. return vmcs_config.cpu_based_2nd_exec_ctrl &
  966. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  967. }
  968. /*
  969. * Comment's format: document - errata name - stepping - processor name.
  970. * Refer from
  971. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  972. */
  973. static u32 vmx_preemption_cpu_tfms[] = {
  974. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  975. 0x000206E6,
  976. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  977. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  978. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  979. 0x00020652,
  980. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  981. 0x00020655,
  982. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  983. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  984. /*
  985. * 320767.pdf - AAP86 - B1 -
  986. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  987. */
  988. 0x000106E5,
  989. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  990. 0x000106A0,
  991. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  992. 0x000106A1,
  993. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  994. 0x000106A4,
  995. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  996. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  997. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  998. 0x000106A5,
  999. };
  1000. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1001. {
  1002. u32 eax = cpuid_eax(0x00000001), i;
  1003. /* Clear the reserved bits */
  1004. eax &= ~(0x3U << 14 | 0xfU << 28);
  1005. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1006. if (eax == vmx_preemption_cpu_tfms[i])
  1007. return true;
  1008. return false;
  1009. }
  1010. static inline bool cpu_has_vmx_preemption_timer(void)
  1011. {
  1012. return vmcs_config.pin_based_exec_ctrl &
  1013. PIN_BASED_VMX_PREEMPTION_TIMER;
  1014. }
  1015. static inline bool cpu_has_vmx_posted_intr(void)
  1016. {
  1017. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1018. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1019. }
  1020. static inline bool cpu_has_vmx_apicv(void)
  1021. {
  1022. return cpu_has_vmx_apic_register_virt() &&
  1023. cpu_has_vmx_virtual_intr_delivery() &&
  1024. cpu_has_vmx_posted_intr();
  1025. }
  1026. static inline bool cpu_has_vmx_flexpriority(void)
  1027. {
  1028. return cpu_has_vmx_tpr_shadow() &&
  1029. cpu_has_vmx_virtualize_apic_accesses();
  1030. }
  1031. static inline bool cpu_has_vmx_ept_execute_only(void)
  1032. {
  1033. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1034. }
  1035. static inline bool cpu_has_vmx_ept_2m_page(void)
  1036. {
  1037. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1038. }
  1039. static inline bool cpu_has_vmx_ept_1g_page(void)
  1040. {
  1041. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1042. }
  1043. static inline bool cpu_has_vmx_ept_4levels(void)
  1044. {
  1045. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1046. }
  1047. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1048. {
  1049. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1050. }
  1051. static inline bool cpu_has_vmx_ept_5levels(void)
  1052. {
  1053. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1054. }
  1055. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1056. {
  1057. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1058. }
  1059. static inline bool cpu_has_vmx_invept_context(void)
  1060. {
  1061. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1062. }
  1063. static inline bool cpu_has_vmx_invept_global(void)
  1064. {
  1065. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1066. }
  1067. static inline bool cpu_has_vmx_invvpid_single(void)
  1068. {
  1069. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1070. }
  1071. static inline bool cpu_has_vmx_invvpid_global(void)
  1072. {
  1073. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1074. }
  1075. static inline bool cpu_has_vmx_invvpid(void)
  1076. {
  1077. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1078. }
  1079. static inline bool cpu_has_vmx_ept(void)
  1080. {
  1081. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1082. SECONDARY_EXEC_ENABLE_EPT;
  1083. }
  1084. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1085. {
  1086. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1087. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1088. }
  1089. static inline bool cpu_has_vmx_ple(void)
  1090. {
  1091. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1092. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1093. }
  1094. static inline bool cpu_has_vmx_basic_inout(void)
  1095. {
  1096. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1097. }
  1098. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1099. {
  1100. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1101. }
  1102. static inline bool cpu_has_vmx_vpid(void)
  1103. {
  1104. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1105. SECONDARY_EXEC_ENABLE_VPID;
  1106. }
  1107. static inline bool cpu_has_vmx_rdtscp(void)
  1108. {
  1109. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1110. SECONDARY_EXEC_RDTSCP;
  1111. }
  1112. static inline bool cpu_has_vmx_invpcid(void)
  1113. {
  1114. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1115. SECONDARY_EXEC_ENABLE_INVPCID;
  1116. }
  1117. static inline bool cpu_has_virtual_nmis(void)
  1118. {
  1119. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1120. }
  1121. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1122. {
  1123. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1124. SECONDARY_EXEC_WBINVD_EXITING;
  1125. }
  1126. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1127. {
  1128. u64 vmx_msr;
  1129. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1130. /* check if the cpu supports writing r/o exit information fields */
  1131. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1132. return false;
  1133. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1134. SECONDARY_EXEC_SHADOW_VMCS;
  1135. }
  1136. static inline bool cpu_has_vmx_pml(void)
  1137. {
  1138. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1139. }
  1140. static inline bool cpu_has_vmx_tsc_scaling(void)
  1141. {
  1142. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1143. SECONDARY_EXEC_TSC_SCALING;
  1144. }
  1145. static inline bool cpu_has_vmx_vmfunc(void)
  1146. {
  1147. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1148. SECONDARY_EXEC_ENABLE_VMFUNC;
  1149. }
  1150. static inline bool report_flexpriority(void)
  1151. {
  1152. return flexpriority_enabled;
  1153. }
  1154. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1155. {
  1156. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
  1157. }
  1158. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1159. {
  1160. return vmcs12->cpu_based_vm_exec_control & bit;
  1161. }
  1162. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1163. {
  1164. return (vmcs12->cpu_based_vm_exec_control &
  1165. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1166. (vmcs12->secondary_vm_exec_control & bit);
  1167. }
  1168. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1169. {
  1170. return vmcs12->pin_based_vm_exec_control &
  1171. PIN_BASED_VMX_PREEMPTION_TIMER;
  1172. }
  1173. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1174. {
  1175. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1176. }
  1177. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1178. {
  1179. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1180. }
  1181. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1182. {
  1183. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1184. }
  1185. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1186. {
  1187. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1188. }
  1189. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1190. {
  1191. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1192. }
  1193. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1194. {
  1195. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1196. }
  1197. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1198. {
  1199. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1200. }
  1201. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1202. {
  1203. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1204. }
  1205. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1206. {
  1207. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1208. }
  1209. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1210. {
  1211. return nested_cpu_has_vmfunc(vmcs12) &&
  1212. (vmcs12->vm_function_control &
  1213. VMX_VMFUNC_EPTP_SWITCHING);
  1214. }
  1215. static inline bool is_nmi(u32 intr_info)
  1216. {
  1217. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1218. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1219. }
  1220. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1221. u32 exit_intr_info,
  1222. unsigned long exit_qualification);
  1223. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1224. struct vmcs12 *vmcs12,
  1225. u32 reason, unsigned long qualification);
  1226. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1227. {
  1228. int i;
  1229. for (i = 0; i < vmx->nmsrs; ++i)
  1230. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1231. return i;
  1232. return -1;
  1233. }
  1234. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1235. {
  1236. struct {
  1237. u64 vpid : 16;
  1238. u64 rsvd : 48;
  1239. u64 gva;
  1240. } operand = { vpid, 0, gva };
  1241. asm volatile (__ex(ASM_VMX_INVVPID)
  1242. /* CF==1 or ZF==1 --> rc = -1 */
  1243. "; ja 1f ; ud2 ; 1:"
  1244. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1245. }
  1246. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1247. {
  1248. struct {
  1249. u64 eptp, gpa;
  1250. } operand = {eptp, gpa};
  1251. asm volatile (__ex(ASM_VMX_INVEPT)
  1252. /* CF==1 or ZF==1 --> rc = -1 */
  1253. "; ja 1f ; ud2 ; 1:\n"
  1254. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1255. }
  1256. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1257. {
  1258. int i;
  1259. i = __find_msr_index(vmx, msr);
  1260. if (i >= 0)
  1261. return &vmx->guest_msrs[i];
  1262. return NULL;
  1263. }
  1264. static void vmcs_clear(struct vmcs *vmcs)
  1265. {
  1266. u64 phys_addr = __pa(vmcs);
  1267. u8 error;
  1268. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1269. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1270. : "cc", "memory");
  1271. if (error)
  1272. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1273. vmcs, phys_addr);
  1274. }
  1275. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1276. {
  1277. vmcs_clear(loaded_vmcs->vmcs);
  1278. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1279. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1280. loaded_vmcs->cpu = -1;
  1281. loaded_vmcs->launched = 0;
  1282. }
  1283. static void vmcs_load(struct vmcs *vmcs)
  1284. {
  1285. u64 phys_addr = __pa(vmcs);
  1286. u8 error;
  1287. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1288. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1289. : "cc", "memory");
  1290. if (error)
  1291. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1292. vmcs, phys_addr);
  1293. }
  1294. #ifdef CONFIG_KEXEC_CORE
  1295. /*
  1296. * This bitmap is used to indicate whether the vmclear
  1297. * operation is enabled on all cpus. All disabled by
  1298. * default.
  1299. */
  1300. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1301. static inline void crash_enable_local_vmclear(int cpu)
  1302. {
  1303. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1304. }
  1305. static inline void crash_disable_local_vmclear(int cpu)
  1306. {
  1307. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1308. }
  1309. static inline int crash_local_vmclear_enabled(int cpu)
  1310. {
  1311. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1312. }
  1313. static void crash_vmclear_local_loaded_vmcss(void)
  1314. {
  1315. int cpu = raw_smp_processor_id();
  1316. struct loaded_vmcs *v;
  1317. if (!crash_local_vmclear_enabled(cpu))
  1318. return;
  1319. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1320. loaded_vmcss_on_cpu_link)
  1321. vmcs_clear(v->vmcs);
  1322. }
  1323. #else
  1324. static inline void crash_enable_local_vmclear(int cpu) { }
  1325. static inline void crash_disable_local_vmclear(int cpu) { }
  1326. #endif /* CONFIG_KEXEC_CORE */
  1327. static void __loaded_vmcs_clear(void *arg)
  1328. {
  1329. struct loaded_vmcs *loaded_vmcs = arg;
  1330. int cpu = raw_smp_processor_id();
  1331. if (loaded_vmcs->cpu != cpu)
  1332. return; /* vcpu migration can race with cpu offline */
  1333. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1334. per_cpu(current_vmcs, cpu) = NULL;
  1335. crash_disable_local_vmclear(cpu);
  1336. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1337. /*
  1338. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1339. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1340. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1341. * then adds the vmcs into percpu list before it is deleted.
  1342. */
  1343. smp_wmb();
  1344. loaded_vmcs_init(loaded_vmcs);
  1345. crash_enable_local_vmclear(cpu);
  1346. }
  1347. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1348. {
  1349. int cpu = loaded_vmcs->cpu;
  1350. if (cpu != -1)
  1351. smp_call_function_single(cpu,
  1352. __loaded_vmcs_clear, loaded_vmcs, 1);
  1353. }
  1354. static inline void vpid_sync_vcpu_single(int vpid)
  1355. {
  1356. if (vpid == 0)
  1357. return;
  1358. if (cpu_has_vmx_invvpid_single())
  1359. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1360. }
  1361. static inline void vpid_sync_vcpu_global(void)
  1362. {
  1363. if (cpu_has_vmx_invvpid_global())
  1364. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1365. }
  1366. static inline void vpid_sync_context(int vpid)
  1367. {
  1368. if (cpu_has_vmx_invvpid_single())
  1369. vpid_sync_vcpu_single(vpid);
  1370. else
  1371. vpid_sync_vcpu_global();
  1372. }
  1373. static inline void ept_sync_global(void)
  1374. {
  1375. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1376. }
  1377. static inline void ept_sync_context(u64 eptp)
  1378. {
  1379. if (cpu_has_vmx_invept_context())
  1380. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1381. else
  1382. ept_sync_global();
  1383. }
  1384. static __always_inline void vmcs_check16(unsigned long field)
  1385. {
  1386. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1387. "16-bit accessor invalid for 64-bit field");
  1388. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1389. "16-bit accessor invalid for 64-bit high field");
  1390. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1391. "16-bit accessor invalid for 32-bit high field");
  1392. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1393. "16-bit accessor invalid for natural width field");
  1394. }
  1395. static __always_inline void vmcs_check32(unsigned long field)
  1396. {
  1397. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1398. "32-bit accessor invalid for 16-bit field");
  1399. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1400. "32-bit accessor invalid for natural width field");
  1401. }
  1402. static __always_inline void vmcs_check64(unsigned long field)
  1403. {
  1404. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1405. "64-bit accessor invalid for 16-bit field");
  1406. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1407. "64-bit accessor invalid for 64-bit high field");
  1408. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1409. "64-bit accessor invalid for 32-bit field");
  1410. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1411. "64-bit accessor invalid for natural width field");
  1412. }
  1413. static __always_inline void vmcs_checkl(unsigned long field)
  1414. {
  1415. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1416. "Natural width accessor invalid for 16-bit field");
  1417. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1418. "Natural width accessor invalid for 64-bit field");
  1419. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1420. "Natural width accessor invalid for 64-bit high field");
  1421. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1422. "Natural width accessor invalid for 32-bit field");
  1423. }
  1424. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1425. {
  1426. unsigned long value;
  1427. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1428. : "=a"(value) : "d"(field) : "cc");
  1429. return value;
  1430. }
  1431. static __always_inline u16 vmcs_read16(unsigned long field)
  1432. {
  1433. vmcs_check16(field);
  1434. return __vmcs_readl(field);
  1435. }
  1436. static __always_inline u32 vmcs_read32(unsigned long field)
  1437. {
  1438. vmcs_check32(field);
  1439. return __vmcs_readl(field);
  1440. }
  1441. static __always_inline u64 vmcs_read64(unsigned long field)
  1442. {
  1443. vmcs_check64(field);
  1444. #ifdef CONFIG_X86_64
  1445. return __vmcs_readl(field);
  1446. #else
  1447. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  1448. #endif
  1449. }
  1450. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1451. {
  1452. vmcs_checkl(field);
  1453. return __vmcs_readl(field);
  1454. }
  1455. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1456. {
  1457. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1458. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1459. dump_stack();
  1460. }
  1461. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  1462. {
  1463. u8 error;
  1464. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1465. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1466. if (unlikely(error))
  1467. vmwrite_error(field, value);
  1468. }
  1469. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  1470. {
  1471. vmcs_check16(field);
  1472. __vmcs_writel(field, value);
  1473. }
  1474. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  1475. {
  1476. vmcs_check32(field);
  1477. __vmcs_writel(field, value);
  1478. }
  1479. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  1480. {
  1481. vmcs_check64(field);
  1482. __vmcs_writel(field, value);
  1483. #ifndef CONFIG_X86_64
  1484. asm volatile ("");
  1485. __vmcs_writel(field+1, value >> 32);
  1486. #endif
  1487. }
  1488. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  1489. {
  1490. vmcs_checkl(field);
  1491. __vmcs_writel(field, value);
  1492. }
  1493. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  1494. {
  1495. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1496. "vmcs_clear_bits does not support 64-bit fields");
  1497. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  1498. }
  1499. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  1500. {
  1501. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1502. "vmcs_set_bits does not support 64-bit fields");
  1503. __vmcs_writel(field, __vmcs_readl(field) | mask);
  1504. }
  1505. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  1506. {
  1507. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  1508. }
  1509. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1510. {
  1511. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1512. vmx->vm_entry_controls_shadow = val;
  1513. }
  1514. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1515. {
  1516. if (vmx->vm_entry_controls_shadow != val)
  1517. vm_entry_controls_init(vmx, val);
  1518. }
  1519. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1520. {
  1521. return vmx->vm_entry_controls_shadow;
  1522. }
  1523. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1524. {
  1525. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1526. }
  1527. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1528. {
  1529. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1530. }
  1531. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  1532. {
  1533. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  1534. }
  1535. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1536. {
  1537. vmcs_write32(VM_EXIT_CONTROLS, val);
  1538. vmx->vm_exit_controls_shadow = val;
  1539. }
  1540. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1541. {
  1542. if (vmx->vm_exit_controls_shadow != val)
  1543. vm_exit_controls_init(vmx, val);
  1544. }
  1545. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1546. {
  1547. return vmx->vm_exit_controls_shadow;
  1548. }
  1549. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1550. {
  1551. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1552. }
  1553. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1554. {
  1555. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1556. }
  1557. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1558. {
  1559. vmx->segment_cache.bitmask = 0;
  1560. }
  1561. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1562. unsigned field)
  1563. {
  1564. bool ret;
  1565. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1566. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1567. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1568. vmx->segment_cache.bitmask = 0;
  1569. }
  1570. ret = vmx->segment_cache.bitmask & mask;
  1571. vmx->segment_cache.bitmask |= mask;
  1572. return ret;
  1573. }
  1574. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1575. {
  1576. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1577. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1578. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1579. return *p;
  1580. }
  1581. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1582. {
  1583. ulong *p = &vmx->segment_cache.seg[seg].base;
  1584. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1585. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1586. return *p;
  1587. }
  1588. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1589. {
  1590. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1591. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1592. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1593. return *p;
  1594. }
  1595. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1596. {
  1597. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1598. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1599. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1600. return *p;
  1601. }
  1602. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1603. {
  1604. u32 eb;
  1605. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1606. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  1607. if ((vcpu->guest_debug &
  1608. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1609. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1610. eb |= 1u << BP_VECTOR;
  1611. if (to_vmx(vcpu)->rmode.vm86_active)
  1612. eb = ~0;
  1613. if (enable_ept)
  1614. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1615. /* When we are running a nested L2 guest and L1 specified for it a
  1616. * certain exception bitmap, we must trap the same exceptions and pass
  1617. * them to L1. When running L2, we will only handle the exceptions
  1618. * specified above if L1 did not want them.
  1619. */
  1620. if (is_guest_mode(vcpu))
  1621. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1622. vmcs_write32(EXCEPTION_BITMAP, eb);
  1623. }
  1624. /*
  1625. * Check if MSR is intercepted for currently loaded MSR bitmap.
  1626. */
  1627. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  1628. {
  1629. unsigned long *msr_bitmap;
  1630. int f = sizeof(unsigned long);
  1631. if (!cpu_has_vmx_msr_bitmap())
  1632. return true;
  1633. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  1634. if (msr <= 0x1fff) {
  1635. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  1636. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1637. msr &= 0x1fff;
  1638. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  1639. }
  1640. return true;
  1641. }
  1642. /*
  1643. * Check if MSR is intercepted for L01 MSR bitmap.
  1644. */
  1645. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  1646. {
  1647. unsigned long *msr_bitmap;
  1648. int f = sizeof(unsigned long);
  1649. if (!cpu_has_vmx_msr_bitmap())
  1650. return true;
  1651. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  1652. if (msr <= 0x1fff) {
  1653. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  1654. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1655. msr &= 0x1fff;
  1656. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  1657. }
  1658. return true;
  1659. }
  1660. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1661. unsigned long entry, unsigned long exit)
  1662. {
  1663. vm_entry_controls_clearbit(vmx, entry);
  1664. vm_exit_controls_clearbit(vmx, exit);
  1665. }
  1666. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1667. {
  1668. unsigned i;
  1669. struct msr_autoload *m = &vmx->msr_autoload;
  1670. switch (msr) {
  1671. case MSR_EFER:
  1672. if (cpu_has_load_ia32_efer) {
  1673. clear_atomic_switch_msr_special(vmx,
  1674. VM_ENTRY_LOAD_IA32_EFER,
  1675. VM_EXIT_LOAD_IA32_EFER);
  1676. return;
  1677. }
  1678. break;
  1679. case MSR_CORE_PERF_GLOBAL_CTRL:
  1680. if (cpu_has_load_perf_global_ctrl) {
  1681. clear_atomic_switch_msr_special(vmx,
  1682. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1683. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1684. return;
  1685. }
  1686. break;
  1687. }
  1688. for (i = 0; i < m->nr; ++i)
  1689. if (m->guest[i].index == msr)
  1690. break;
  1691. if (i == m->nr)
  1692. return;
  1693. --m->nr;
  1694. m->guest[i] = m->guest[m->nr];
  1695. m->host[i] = m->host[m->nr];
  1696. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1697. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1698. }
  1699. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1700. unsigned long entry, unsigned long exit,
  1701. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  1702. u64 guest_val, u64 host_val)
  1703. {
  1704. vmcs_write64(guest_val_vmcs, guest_val);
  1705. vmcs_write64(host_val_vmcs, host_val);
  1706. vm_entry_controls_setbit(vmx, entry);
  1707. vm_exit_controls_setbit(vmx, exit);
  1708. }
  1709. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1710. u64 guest_val, u64 host_val)
  1711. {
  1712. unsigned i;
  1713. struct msr_autoload *m = &vmx->msr_autoload;
  1714. switch (msr) {
  1715. case MSR_EFER:
  1716. if (cpu_has_load_ia32_efer) {
  1717. add_atomic_switch_msr_special(vmx,
  1718. VM_ENTRY_LOAD_IA32_EFER,
  1719. VM_EXIT_LOAD_IA32_EFER,
  1720. GUEST_IA32_EFER,
  1721. HOST_IA32_EFER,
  1722. guest_val, host_val);
  1723. return;
  1724. }
  1725. break;
  1726. case MSR_CORE_PERF_GLOBAL_CTRL:
  1727. if (cpu_has_load_perf_global_ctrl) {
  1728. add_atomic_switch_msr_special(vmx,
  1729. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1730. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1731. GUEST_IA32_PERF_GLOBAL_CTRL,
  1732. HOST_IA32_PERF_GLOBAL_CTRL,
  1733. guest_val, host_val);
  1734. return;
  1735. }
  1736. break;
  1737. case MSR_IA32_PEBS_ENABLE:
  1738. /* PEBS needs a quiescent period after being disabled (to write
  1739. * a record). Disabling PEBS through VMX MSR swapping doesn't
  1740. * provide that period, so a CPU could write host's record into
  1741. * guest's memory.
  1742. */
  1743. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  1744. }
  1745. for (i = 0; i < m->nr; ++i)
  1746. if (m->guest[i].index == msr)
  1747. break;
  1748. if (i == NR_AUTOLOAD_MSRS) {
  1749. printk_once(KERN_WARNING "Not enough msr switch entries. "
  1750. "Can't add msr %x\n", msr);
  1751. return;
  1752. } else if (i == m->nr) {
  1753. ++m->nr;
  1754. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1755. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1756. }
  1757. m->guest[i].index = msr;
  1758. m->guest[i].value = guest_val;
  1759. m->host[i].index = msr;
  1760. m->host[i].value = host_val;
  1761. }
  1762. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1763. {
  1764. u64 guest_efer = vmx->vcpu.arch.efer;
  1765. u64 ignore_bits = 0;
  1766. if (!enable_ept) {
  1767. /*
  1768. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  1769. * host CPUID is more efficient than testing guest CPUID
  1770. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  1771. */
  1772. if (boot_cpu_has(X86_FEATURE_SMEP))
  1773. guest_efer |= EFER_NX;
  1774. else if (!(guest_efer & EFER_NX))
  1775. ignore_bits |= EFER_NX;
  1776. }
  1777. /*
  1778. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  1779. */
  1780. ignore_bits |= EFER_SCE;
  1781. #ifdef CONFIG_X86_64
  1782. ignore_bits |= EFER_LMA | EFER_LME;
  1783. /* SCE is meaningful only in long mode on Intel */
  1784. if (guest_efer & EFER_LMA)
  1785. ignore_bits &= ~(u64)EFER_SCE;
  1786. #endif
  1787. clear_atomic_switch_msr(vmx, MSR_EFER);
  1788. /*
  1789. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  1790. * On CPUs that support "load IA32_EFER", always switch EFER
  1791. * atomically, since it's faster than switching it manually.
  1792. */
  1793. if (cpu_has_load_ia32_efer ||
  1794. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  1795. if (!(guest_efer & EFER_LMA))
  1796. guest_efer &= ~EFER_LME;
  1797. if (guest_efer != host_efer)
  1798. add_atomic_switch_msr(vmx, MSR_EFER,
  1799. guest_efer, host_efer);
  1800. return false;
  1801. } else {
  1802. guest_efer &= ~ignore_bits;
  1803. guest_efer |= host_efer & ignore_bits;
  1804. vmx->guest_msrs[efer_offset].data = guest_efer;
  1805. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1806. return true;
  1807. }
  1808. }
  1809. #ifdef CONFIG_X86_32
  1810. /*
  1811. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  1812. * VMCS rather than the segment table. KVM uses this helper to figure
  1813. * out the current bases to poke them into the VMCS before entry.
  1814. */
  1815. static unsigned long segment_base(u16 selector)
  1816. {
  1817. struct desc_struct *table;
  1818. unsigned long v;
  1819. if (!(selector & ~SEGMENT_RPL_MASK))
  1820. return 0;
  1821. table = get_current_gdt_ro();
  1822. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  1823. u16 ldt_selector = kvm_read_ldt();
  1824. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  1825. return 0;
  1826. table = (struct desc_struct *)segment_base(ldt_selector);
  1827. }
  1828. v = get_desc_base(&table[selector >> 3]);
  1829. return v;
  1830. }
  1831. #endif
  1832. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1833. {
  1834. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1835. int i;
  1836. if (vmx->host_state.loaded)
  1837. return;
  1838. vmx->host_state.loaded = 1;
  1839. /*
  1840. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1841. * allow segment selectors with cpl > 0 or ti == 1.
  1842. */
  1843. vmx->host_state.ldt_sel = kvm_read_ldt();
  1844. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1845. savesegment(fs, vmx->host_state.fs_sel);
  1846. if (!(vmx->host_state.fs_sel & 7)) {
  1847. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1848. vmx->host_state.fs_reload_needed = 0;
  1849. } else {
  1850. vmcs_write16(HOST_FS_SELECTOR, 0);
  1851. vmx->host_state.fs_reload_needed = 1;
  1852. }
  1853. savesegment(gs, vmx->host_state.gs_sel);
  1854. if (!(vmx->host_state.gs_sel & 7))
  1855. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1856. else {
  1857. vmcs_write16(HOST_GS_SELECTOR, 0);
  1858. vmx->host_state.gs_ldt_reload_needed = 1;
  1859. }
  1860. #ifdef CONFIG_X86_64
  1861. savesegment(ds, vmx->host_state.ds_sel);
  1862. savesegment(es, vmx->host_state.es_sel);
  1863. #endif
  1864. #ifdef CONFIG_X86_64
  1865. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1866. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1867. #else
  1868. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1869. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1870. #endif
  1871. #ifdef CONFIG_X86_64
  1872. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1873. if (is_long_mode(&vmx->vcpu))
  1874. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1875. #endif
  1876. if (boot_cpu_has(X86_FEATURE_MPX))
  1877. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1878. for (i = 0; i < vmx->save_nmsrs; ++i)
  1879. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1880. vmx->guest_msrs[i].data,
  1881. vmx->guest_msrs[i].mask);
  1882. }
  1883. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1884. {
  1885. if (!vmx->host_state.loaded)
  1886. return;
  1887. ++vmx->vcpu.stat.host_state_reload;
  1888. vmx->host_state.loaded = 0;
  1889. #ifdef CONFIG_X86_64
  1890. if (is_long_mode(&vmx->vcpu))
  1891. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1892. #endif
  1893. if (vmx->host_state.gs_ldt_reload_needed) {
  1894. kvm_load_ldt(vmx->host_state.ldt_sel);
  1895. #ifdef CONFIG_X86_64
  1896. load_gs_index(vmx->host_state.gs_sel);
  1897. #else
  1898. loadsegment(gs, vmx->host_state.gs_sel);
  1899. #endif
  1900. }
  1901. if (vmx->host_state.fs_reload_needed)
  1902. loadsegment(fs, vmx->host_state.fs_sel);
  1903. #ifdef CONFIG_X86_64
  1904. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1905. loadsegment(ds, vmx->host_state.ds_sel);
  1906. loadsegment(es, vmx->host_state.es_sel);
  1907. }
  1908. #endif
  1909. invalidate_tss_limit();
  1910. #ifdef CONFIG_X86_64
  1911. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1912. #endif
  1913. if (vmx->host_state.msr_host_bndcfgs)
  1914. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1915. load_fixmap_gdt(raw_smp_processor_id());
  1916. }
  1917. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1918. {
  1919. preempt_disable();
  1920. __vmx_load_host_state(vmx);
  1921. preempt_enable();
  1922. }
  1923. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  1924. {
  1925. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  1926. struct pi_desc old, new;
  1927. unsigned int dest;
  1928. /*
  1929. * In case of hot-plug or hot-unplug, we may have to undo
  1930. * vmx_vcpu_pi_put even if there is no assigned device. And we
  1931. * always keep PI.NDST up to date for simplicity: it makes the
  1932. * code easier, and CPU migration is not a fast path.
  1933. */
  1934. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  1935. return;
  1936. /*
  1937. * First handle the simple case where no cmpxchg is necessary; just
  1938. * allow posting non-urgent interrupts.
  1939. *
  1940. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  1941. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  1942. * expects the VCPU to be on the blocked_vcpu_list that matches
  1943. * PI.NDST.
  1944. */
  1945. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  1946. vcpu->cpu == cpu) {
  1947. pi_clear_sn(pi_desc);
  1948. return;
  1949. }
  1950. /* The full case. */
  1951. do {
  1952. old.control = new.control = pi_desc->control;
  1953. dest = cpu_physical_id(cpu);
  1954. if (x2apic_enabled())
  1955. new.ndst = dest;
  1956. else
  1957. new.ndst = (dest << 8) & 0xFF00;
  1958. new.sn = 0;
  1959. } while (cmpxchg64(&pi_desc->control, old.control,
  1960. new.control) != old.control);
  1961. }
  1962. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  1963. {
  1964. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  1965. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  1966. }
  1967. /*
  1968. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1969. * vcpu mutex is already taken.
  1970. */
  1971. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1972. {
  1973. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1974. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  1975. if (!already_loaded) {
  1976. loaded_vmcs_clear(vmx->loaded_vmcs);
  1977. local_irq_disable();
  1978. crash_disable_local_vmclear(cpu);
  1979. /*
  1980. * Read loaded_vmcs->cpu should be before fetching
  1981. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  1982. * See the comments in __loaded_vmcs_clear().
  1983. */
  1984. smp_rmb();
  1985. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1986. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1987. crash_enable_local_vmclear(cpu);
  1988. local_irq_enable();
  1989. }
  1990. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1991. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1992. vmcs_load(vmx->loaded_vmcs->vmcs);
  1993. indirect_branch_prediction_barrier();
  1994. }
  1995. if (!already_loaded) {
  1996. void *gdt = get_current_gdt_ro();
  1997. unsigned long sysenter_esp;
  1998. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1999. /*
  2000. * Linux uses per-cpu TSS and GDT, so set these when switching
  2001. * processors. See 22.2.4.
  2002. */
  2003. vmcs_writel(HOST_TR_BASE,
  2004. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2005. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2006. /*
  2007. * VM exits change the host TR limit to 0x67 after a VM
  2008. * exit. This is okay, since 0x67 covers everything except
  2009. * the IO bitmap and have have code to handle the IO bitmap
  2010. * being lost after a VM exit.
  2011. */
  2012. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2013. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2014. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2015. vmx->loaded_vmcs->cpu = cpu;
  2016. }
  2017. /* Setup TSC multiplier */
  2018. if (kvm_has_tsc_control &&
  2019. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2020. decache_tsc_multiplier(vmx);
  2021. vmx_vcpu_pi_load(vcpu, cpu);
  2022. vmx->host_pkru = read_pkru();
  2023. vmx->host_debugctlmsr = get_debugctlmsr();
  2024. }
  2025. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2026. {
  2027. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2028. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2029. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2030. !kvm_vcpu_apicv_active(vcpu))
  2031. return;
  2032. /* Set SN when the vCPU is preempted */
  2033. if (vcpu->preempted)
  2034. pi_set_sn(pi_desc);
  2035. }
  2036. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2037. {
  2038. vmx_vcpu_pi_put(vcpu);
  2039. __vmx_load_host_state(to_vmx(vcpu));
  2040. }
  2041. static bool emulation_required(struct kvm_vcpu *vcpu)
  2042. {
  2043. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2044. }
  2045. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2046. /*
  2047. * Return the cr0 value that a nested guest would read. This is a combination
  2048. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2049. * its hypervisor (cr0_read_shadow).
  2050. */
  2051. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2052. {
  2053. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2054. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2055. }
  2056. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2057. {
  2058. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2059. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2060. }
  2061. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2062. {
  2063. unsigned long rflags, save_rflags;
  2064. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2065. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2066. rflags = vmcs_readl(GUEST_RFLAGS);
  2067. if (to_vmx(vcpu)->rmode.vm86_active) {
  2068. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2069. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2070. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2071. }
  2072. to_vmx(vcpu)->rflags = rflags;
  2073. }
  2074. return to_vmx(vcpu)->rflags;
  2075. }
  2076. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2077. {
  2078. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2079. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2080. to_vmx(vcpu)->rflags = rflags;
  2081. if (to_vmx(vcpu)->rmode.vm86_active) {
  2082. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2083. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2084. }
  2085. vmcs_writel(GUEST_RFLAGS, rflags);
  2086. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2087. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2088. }
  2089. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2090. {
  2091. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2092. int ret = 0;
  2093. if (interruptibility & GUEST_INTR_STATE_STI)
  2094. ret |= KVM_X86_SHADOW_INT_STI;
  2095. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2096. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2097. return ret;
  2098. }
  2099. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2100. {
  2101. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2102. u32 interruptibility = interruptibility_old;
  2103. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2104. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2105. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2106. else if (mask & KVM_X86_SHADOW_INT_STI)
  2107. interruptibility |= GUEST_INTR_STATE_STI;
  2108. if ((interruptibility != interruptibility_old))
  2109. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2110. }
  2111. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2112. {
  2113. unsigned long rip;
  2114. rip = kvm_rip_read(vcpu);
  2115. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2116. kvm_rip_write(vcpu, rip);
  2117. /* skipping an emulated instruction also counts */
  2118. vmx_set_interrupt_shadow(vcpu, 0);
  2119. }
  2120. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2121. unsigned long exit_qual)
  2122. {
  2123. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2124. unsigned int nr = vcpu->arch.exception.nr;
  2125. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2126. if (vcpu->arch.exception.has_error_code) {
  2127. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2128. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2129. }
  2130. if (kvm_exception_is_soft(nr))
  2131. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2132. else
  2133. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2134. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2135. vmx_get_nmi_mask(vcpu))
  2136. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2137. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2138. }
  2139. /*
  2140. * KVM wants to inject page-faults which it got to the guest. This function
  2141. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2142. */
  2143. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2144. {
  2145. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2146. unsigned int nr = vcpu->arch.exception.nr;
  2147. if (nr == PF_VECTOR) {
  2148. if (vcpu->arch.exception.nested_apf) {
  2149. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2150. return 1;
  2151. }
  2152. /*
  2153. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2154. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2155. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2156. * can be written only when inject_pending_event runs. This should be
  2157. * conditional on a new capability---if the capability is disabled,
  2158. * kvm_multiple_exception would write the ancillary information to
  2159. * CR2 or DR6, for backwards ABI-compatibility.
  2160. */
  2161. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2162. vcpu->arch.exception.error_code)) {
  2163. *exit_qual = vcpu->arch.cr2;
  2164. return 1;
  2165. }
  2166. } else {
  2167. if (vmcs12->exception_bitmap & (1u << nr)) {
  2168. if (nr == DB_VECTOR)
  2169. *exit_qual = vcpu->arch.dr6;
  2170. else
  2171. *exit_qual = 0;
  2172. return 1;
  2173. }
  2174. }
  2175. return 0;
  2176. }
  2177. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2178. {
  2179. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2180. unsigned nr = vcpu->arch.exception.nr;
  2181. bool has_error_code = vcpu->arch.exception.has_error_code;
  2182. u32 error_code = vcpu->arch.exception.error_code;
  2183. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2184. if (has_error_code) {
  2185. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2186. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2187. }
  2188. if (vmx->rmode.vm86_active) {
  2189. int inc_eip = 0;
  2190. if (kvm_exception_is_soft(nr))
  2191. inc_eip = vcpu->arch.event_exit_inst_len;
  2192. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2193. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2194. return;
  2195. }
  2196. if (kvm_exception_is_soft(nr)) {
  2197. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2198. vmx->vcpu.arch.event_exit_inst_len);
  2199. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2200. } else
  2201. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2202. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2203. }
  2204. static bool vmx_rdtscp_supported(void)
  2205. {
  2206. return cpu_has_vmx_rdtscp();
  2207. }
  2208. static bool vmx_invpcid_supported(void)
  2209. {
  2210. return cpu_has_vmx_invpcid() && enable_ept;
  2211. }
  2212. /*
  2213. * Swap MSR entry in host/guest MSR entry array.
  2214. */
  2215. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2216. {
  2217. struct shared_msr_entry tmp;
  2218. tmp = vmx->guest_msrs[to];
  2219. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2220. vmx->guest_msrs[from] = tmp;
  2221. }
  2222. /*
  2223. * Set up the vmcs to automatically save and restore system
  2224. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2225. * mode, as fiddling with msrs is very expensive.
  2226. */
  2227. static void setup_msrs(struct vcpu_vmx *vmx)
  2228. {
  2229. int save_nmsrs, index;
  2230. save_nmsrs = 0;
  2231. #ifdef CONFIG_X86_64
  2232. if (is_long_mode(&vmx->vcpu)) {
  2233. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2234. if (index >= 0)
  2235. move_msr_up(vmx, index, save_nmsrs++);
  2236. index = __find_msr_index(vmx, MSR_LSTAR);
  2237. if (index >= 0)
  2238. move_msr_up(vmx, index, save_nmsrs++);
  2239. index = __find_msr_index(vmx, MSR_CSTAR);
  2240. if (index >= 0)
  2241. move_msr_up(vmx, index, save_nmsrs++);
  2242. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2243. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2244. move_msr_up(vmx, index, save_nmsrs++);
  2245. /*
  2246. * MSR_STAR is only needed on long mode guests, and only
  2247. * if efer.sce is enabled.
  2248. */
  2249. index = __find_msr_index(vmx, MSR_STAR);
  2250. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2251. move_msr_up(vmx, index, save_nmsrs++);
  2252. }
  2253. #endif
  2254. index = __find_msr_index(vmx, MSR_EFER);
  2255. if (index >= 0 && update_transition_efer(vmx, index))
  2256. move_msr_up(vmx, index, save_nmsrs++);
  2257. vmx->save_nmsrs = save_nmsrs;
  2258. if (cpu_has_vmx_msr_bitmap())
  2259. vmx_update_msr_bitmap(&vmx->vcpu);
  2260. }
  2261. /*
  2262. * reads and returns guest's timestamp counter "register"
  2263. * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
  2264. * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
  2265. */
  2266. static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
  2267. {
  2268. u64 host_tsc, tsc_offset;
  2269. host_tsc = rdtsc();
  2270. tsc_offset = vmcs_read64(TSC_OFFSET);
  2271. return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
  2272. }
  2273. /*
  2274. * writes 'offset' into guest's timestamp counter offset register
  2275. */
  2276. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2277. {
  2278. if (is_guest_mode(vcpu)) {
  2279. /*
  2280. * We're here if L1 chose not to trap WRMSR to TSC. According
  2281. * to the spec, this should set L1's TSC; The offset that L1
  2282. * set for L2 remains unchanged, and still needs to be added
  2283. * to the newly set TSC to get L2's TSC.
  2284. */
  2285. struct vmcs12 *vmcs12;
  2286. /* recalculate vmcs02.TSC_OFFSET: */
  2287. vmcs12 = get_vmcs12(vcpu);
  2288. vmcs_write64(TSC_OFFSET, offset +
  2289. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2290. vmcs12->tsc_offset : 0));
  2291. } else {
  2292. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2293. vmcs_read64(TSC_OFFSET), offset);
  2294. vmcs_write64(TSC_OFFSET, offset);
  2295. }
  2296. }
  2297. /*
  2298. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2299. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2300. * all guests if the "nested" module option is off, and can also be disabled
  2301. * for a single guest by disabling its VMX cpuid bit.
  2302. */
  2303. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2304. {
  2305. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2306. }
  2307. /*
  2308. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2309. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2310. * The same values should also be used to verify that vmcs12 control fields are
  2311. * valid during nested entry from L1 to L2.
  2312. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2313. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2314. * bit in the high half is on if the corresponding bit in the control field
  2315. * may be on. See also vmx_control_verify().
  2316. */
  2317. static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
  2318. {
  2319. /*
  2320. * Note that as a general rule, the high half of the MSRs (bits in
  2321. * the control fields which may be 1) should be initialized by the
  2322. * intersection of the underlying hardware's MSR (i.e., features which
  2323. * can be supported) and the list of features we want to expose -
  2324. * because they are known to be properly supported in our code.
  2325. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2326. * be set to 0, meaning that L1 may turn off any of these bits. The
  2327. * reason is that if one of these bits is necessary, it will appear
  2328. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2329. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2330. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2331. * These rules have exceptions below.
  2332. */
  2333. /* pin-based controls */
  2334. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2335. vmx->nested.nested_vmx_pinbased_ctls_low,
  2336. vmx->nested.nested_vmx_pinbased_ctls_high);
  2337. vmx->nested.nested_vmx_pinbased_ctls_low |=
  2338. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2339. vmx->nested.nested_vmx_pinbased_ctls_high &=
  2340. PIN_BASED_EXT_INTR_MASK |
  2341. PIN_BASED_NMI_EXITING |
  2342. PIN_BASED_VIRTUAL_NMIS;
  2343. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2344. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2345. PIN_BASED_VMX_PREEMPTION_TIMER;
  2346. if (kvm_vcpu_apicv_active(&vmx->vcpu))
  2347. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2348. PIN_BASED_POSTED_INTR;
  2349. /* exit controls */
  2350. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2351. vmx->nested.nested_vmx_exit_ctls_low,
  2352. vmx->nested.nested_vmx_exit_ctls_high);
  2353. vmx->nested.nested_vmx_exit_ctls_low =
  2354. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2355. vmx->nested.nested_vmx_exit_ctls_high &=
  2356. #ifdef CONFIG_X86_64
  2357. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2358. #endif
  2359. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2360. vmx->nested.nested_vmx_exit_ctls_high |=
  2361. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2362. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2363. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2364. if (kvm_mpx_supported())
  2365. vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2366. /* We support free control of debug control saving. */
  2367. vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2368. /* entry controls */
  2369. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2370. vmx->nested.nested_vmx_entry_ctls_low,
  2371. vmx->nested.nested_vmx_entry_ctls_high);
  2372. vmx->nested.nested_vmx_entry_ctls_low =
  2373. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2374. vmx->nested.nested_vmx_entry_ctls_high &=
  2375. #ifdef CONFIG_X86_64
  2376. VM_ENTRY_IA32E_MODE |
  2377. #endif
  2378. VM_ENTRY_LOAD_IA32_PAT;
  2379. vmx->nested.nested_vmx_entry_ctls_high |=
  2380. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2381. if (kvm_mpx_supported())
  2382. vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2383. /* We support free control of debug control loading. */
  2384. vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2385. /* cpu-based controls */
  2386. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2387. vmx->nested.nested_vmx_procbased_ctls_low,
  2388. vmx->nested.nested_vmx_procbased_ctls_high);
  2389. vmx->nested.nested_vmx_procbased_ctls_low =
  2390. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2391. vmx->nested.nested_vmx_procbased_ctls_high &=
  2392. CPU_BASED_VIRTUAL_INTR_PENDING |
  2393. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2394. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2395. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2396. CPU_BASED_CR3_STORE_EXITING |
  2397. #ifdef CONFIG_X86_64
  2398. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2399. #endif
  2400. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2401. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2402. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2403. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2404. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2405. /*
  2406. * We can allow some features even when not supported by the
  2407. * hardware. For example, L1 can specify an MSR bitmap - and we
  2408. * can use it to avoid exits to L1 - even when L0 runs L2
  2409. * without MSR bitmaps.
  2410. */
  2411. vmx->nested.nested_vmx_procbased_ctls_high |=
  2412. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2413. CPU_BASED_USE_MSR_BITMAPS;
  2414. /* We support free control of CR3 access interception. */
  2415. vmx->nested.nested_vmx_procbased_ctls_low &=
  2416. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2417. /*
  2418. * secondary cpu-based controls. Do not include those that
  2419. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  2420. */
  2421. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2422. vmx->nested.nested_vmx_secondary_ctls_low,
  2423. vmx->nested.nested_vmx_secondary_ctls_high);
  2424. vmx->nested.nested_vmx_secondary_ctls_low = 0;
  2425. vmx->nested.nested_vmx_secondary_ctls_high &=
  2426. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2427. SECONDARY_EXEC_DESC |
  2428. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2429. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2430. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2431. SECONDARY_EXEC_WBINVD_EXITING;
  2432. if (enable_ept) {
  2433. /* nested EPT: emulate EPT also to L1 */
  2434. vmx->nested.nested_vmx_secondary_ctls_high |=
  2435. SECONDARY_EXEC_ENABLE_EPT;
  2436. vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2437. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  2438. if (cpu_has_vmx_ept_execute_only())
  2439. vmx->nested.nested_vmx_ept_caps |=
  2440. VMX_EPT_EXECUTE_ONLY_BIT;
  2441. vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
  2442. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  2443. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  2444. VMX_EPT_1GB_PAGE_BIT;
  2445. if (enable_ept_ad_bits) {
  2446. vmx->nested.nested_vmx_secondary_ctls_high |=
  2447. SECONDARY_EXEC_ENABLE_PML;
  2448. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
  2449. }
  2450. }
  2451. if (cpu_has_vmx_vmfunc()) {
  2452. vmx->nested.nested_vmx_secondary_ctls_high |=
  2453. SECONDARY_EXEC_ENABLE_VMFUNC;
  2454. /*
  2455. * Advertise EPTP switching unconditionally
  2456. * since we emulate it
  2457. */
  2458. if (enable_ept)
  2459. vmx->nested.nested_vmx_vmfunc_controls =
  2460. VMX_VMFUNC_EPTP_SWITCHING;
  2461. }
  2462. /*
  2463. * Old versions of KVM use the single-context version without
  2464. * checking for support, so declare that it is supported even
  2465. * though it is treated as global context. The alternative is
  2466. * not failing the single-context invvpid, and it is worse.
  2467. */
  2468. if (enable_vpid) {
  2469. vmx->nested.nested_vmx_secondary_ctls_high |=
  2470. SECONDARY_EXEC_ENABLE_VPID;
  2471. vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
  2472. VMX_VPID_EXTENT_SUPPORTED_MASK;
  2473. }
  2474. if (enable_unrestricted_guest)
  2475. vmx->nested.nested_vmx_secondary_ctls_high |=
  2476. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2477. /* miscellaneous data */
  2478. rdmsr(MSR_IA32_VMX_MISC,
  2479. vmx->nested.nested_vmx_misc_low,
  2480. vmx->nested.nested_vmx_misc_high);
  2481. vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2482. vmx->nested.nested_vmx_misc_low |=
  2483. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2484. VMX_MISC_ACTIVITY_HLT;
  2485. vmx->nested.nested_vmx_misc_high = 0;
  2486. /*
  2487. * This MSR reports some information about VMX support. We
  2488. * should return information about the VMX we emulate for the
  2489. * guest, and the VMCS structure we give it - not about the
  2490. * VMX support of the underlying hardware.
  2491. */
  2492. vmx->nested.nested_vmx_basic =
  2493. VMCS12_REVISION |
  2494. VMX_BASIC_TRUE_CTLS |
  2495. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2496. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2497. if (cpu_has_vmx_basic_inout())
  2498. vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
  2499. /*
  2500. * These MSRs specify bits which the guest must keep fixed on
  2501. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2502. * We picked the standard core2 setting.
  2503. */
  2504. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2505. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2506. vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
  2507. vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
  2508. /* These MSRs specify bits which the guest must keep fixed off. */
  2509. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
  2510. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
  2511. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2512. vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  2513. }
  2514. /*
  2515. * if fixed0[i] == 1: val[i] must be 1
  2516. * if fixed1[i] == 0: val[i] must be 0
  2517. */
  2518. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  2519. {
  2520. return ((val & fixed1) | fixed0) == val;
  2521. }
  2522. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2523. {
  2524. return fixed_bits_valid(control, low, high);
  2525. }
  2526. static inline u64 vmx_control_msr(u32 low, u32 high)
  2527. {
  2528. return low | ((u64)high << 32);
  2529. }
  2530. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  2531. {
  2532. superset &= mask;
  2533. subset &= mask;
  2534. return (superset | subset) == superset;
  2535. }
  2536. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  2537. {
  2538. const u64 feature_and_reserved =
  2539. /* feature (except bit 48; see below) */
  2540. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  2541. /* reserved */
  2542. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  2543. u64 vmx_basic = vmx->nested.nested_vmx_basic;
  2544. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  2545. return -EINVAL;
  2546. /*
  2547. * KVM does not emulate a version of VMX that constrains physical
  2548. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  2549. */
  2550. if (data & BIT_ULL(48))
  2551. return -EINVAL;
  2552. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  2553. vmx_basic_vmcs_revision_id(data))
  2554. return -EINVAL;
  2555. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  2556. return -EINVAL;
  2557. vmx->nested.nested_vmx_basic = data;
  2558. return 0;
  2559. }
  2560. static int
  2561. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2562. {
  2563. u64 supported;
  2564. u32 *lowp, *highp;
  2565. switch (msr_index) {
  2566. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2567. lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
  2568. highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
  2569. break;
  2570. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2571. lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
  2572. highp = &vmx->nested.nested_vmx_procbased_ctls_high;
  2573. break;
  2574. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2575. lowp = &vmx->nested.nested_vmx_exit_ctls_low;
  2576. highp = &vmx->nested.nested_vmx_exit_ctls_high;
  2577. break;
  2578. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2579. lowp = &vmx->nested.nested_vmx_entry_ctls_low;
  2580. highp = &vmx->nested.nested_vmx_entry_ctls_high;
  2581. break;
  2582. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2583. lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
  2584. highp = &vmx->nested.nested_vmx_secondary_ctls_high;
  2585. break;
  2586. default:
  2587. BUG();
  2588. }
  2589. supported = vmx_control_msr(*lowp, *highp);
  2590. /* Check must-be-1 bits are still 1. */
  2591. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  2592. return -EINVAL;
  2593. /* Check must-be-0 bits are still 0. */
  2594. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  2595. return -EINVAL;
  2596. *lowp = data;
  2597. *highp = data >> 32;
  2598. return 0;
  2599. }
  2600. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  2601. {
  2602. const u64 feature_and_reserved_bits =
  2603. /* feature */
  2604. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  2605. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  2606. /* reserved */
  2607. GENMASK_ULL(13, 9) | BIT_ULL(31);
  2608. u64 vmx_misc;
  2609. vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
  2610. vmx->nested.nested_vmx_misc_high);
  2611. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  2612. return -EINVAL;
  2613. if ((vmx->nested.nested_vmx_pinbased_ctls_high &
  2614. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  2615. vmx_misc_preemption_timer_rate(data) !=
  2616. vmx_misc_preemption_timer_rate(vmx_misc))
  2617. return -EINVAL;
  2618. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  2619. return -EINVAL;
  2620. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  2621. return -EINVAL;
  2622. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  2623. return -EINVAL;
  2624. vmx->nested.nested_vmx_misc_low = data;
  2625. vmx->nested.nested_vmx_misc_high = data >> 32;
  2626. return 0;
  2627. }
  2628. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  2629. {
  2630. u64 vmx_ept_vpid_cap;
  2631. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
  2632. vmx->nested.nested_vmx_vpid_caps);
  2633. /* Every bit is either reserved or a feature bit. */
  2634. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  2635. return -EINVAL;
  2636. vmx->nested.nested_vmx_ept_caps = data;
  2637. vmx->nested.nested_vmx_vpid_caps = data >> 32;
  2638. return 0;
  2639. }
  2640. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2641. {
  2642. u64 *msr;
  2643. switch (msr_index) {
  2644. case MSR_IA32_VMX_CR0_FIXED0:
  2645. msr = &vmx->nested.nested_vmx_cr0_fixed0;
  2646. break;
  2647. case MSR_IA32_VMX_CR4_FIXED0:
  2648. msr = &vmx->nested.nested_vmx_cr4_fixed0;
  2649. break;
  2650. default:
  2651. BUG();
  2652. }
  2653. /*
  2654. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  2655. * must be 1 in the restored value.
  2656. */
  2657. if (!is_bitwise_subset(data, *msr, -1ULL))
  2658. return -EINVAL;
  2659. *msr = data;
  2660. return 0;
  2661. }
  2662. /*
  2663. * Called when userspace is restoring VMX MSRs.
  2664. *
  2665. * Returns 0 on success, non-0 otherwise.
  2666. */
  2667. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  2668. {
  2669. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2670. switch (msr_index) {
  2671. case MSR_IA32_VMX_BASIC:
  2672. return vmx_restore_vmx_basic(vmx, data);
  2673. case MSR_IA32_VMX_PINBASED_CTLS:
  2674. case MSR_IA32_VMX_PROCBASED_CTLS:
  2675. case MSR_IA32_VMX_EXIT_CTLS:
  2676. case MSR_IA32_VMX_ENTRY_CTLS:
  2677. /*
  2678. * The "non-true" VMX capability MSRs are generated from the
  2679. * "true" MSRs, so we do not support restoring them directly.
  2680. *
  2681. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  2682. * should restore the "true" MSRs with the must-be-1 bits
  2683. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  2684. * DEFAULT SETTINGS".
  2685. */
  2686. return -EINVAL;
  2687. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2688. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2689. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2690. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2691. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2692. return vmx_restore_control_msr(vmx, msr_index, data);
  2693. case MSR_IA32_VMX_MISC:
  2694. return vmx_restore_vmx_misc(vmx, data);
  2695. case MSR_IA32_VMX_CR0_FIXED0:
  2696. case MSR_IA32_VMX_CR4_FIXED0:
  2697. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  2698. case MSR_IA32_VMX_CR0_FIXED1:
  2699. case MSR_IA32_VMX_CR4_FIXED1:
  2700. /*
  2701. * These MSRs are generated based on the vCPU's CPUID, so we
  2702. * do not support restoring them directly.
  2703. */
  2704. return -EINVAL;
  2705. case MSR_IA32_VMX_EPT_VPID_CAP:
  2706. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  2707. case MSR_IA32_VMX_VMCS_ENUM:
  2708. vmx->nested.nested_vmx_vmcs_enum = data;
  2709. return 0;
  2710. default:
  2711. /*
  2712. * The rest of the VMX capability MSRs do not support restore.
  2713. */
  2714. return -EINVAL;
  2715. }
  2716. }
  2717. /* Returns 0 on success, non-0 otherwise. */
  2718. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2719. {
  2720. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2721. switch (msr_index) {
  2722. case MSR_IA32_VMX_BASIC:
  2723. *pdata = vmx->nested.nested_vmx_basic;
  2724. break;
  2725. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2726. case MSR_IA32_VMX_PINBASED_CTLS:
  2727. *pdata = vmx_control_msr(
  2728. vmx->nested.nested_vmx_pinbased_ctls_low,
  2729. vmx->nested.nested_vmx_pinbased_ctls_high);
  2730. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  2731. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2732. break;
  2733. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2734. case MSR_IA32_VMX_PROCBASED_CTLS:
  2735. *pdata = vmx_control_msr(
  2736. vmx->nested.nested_vmx_procbased_ctls_low,
  2737. vmx->nested.nested_vmx_procbased_ctls_high);
  2738. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  2739. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2740. break;
  2741. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2742. case MSR_IA32_VMX_EXIT_CTLS:
  2743. *pdata = vmx_control_msr(
  2744. vmx->nested.nested_vmx_exit_ctls_low,
  2745. vmx->nested.nested_vmx_exit_ctls_high);
  2746. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  2747. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2748. break;
  2749. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2750. case MSR_IA32_VMX_ENTRY_CTLS:
  2751. *pdata = vmx_control_msr(
  2752. vmx->nested.nested_vmx_entry_ctls_low,
  2753. vmx->nested.nested_vmx_entry_ctls_high);
  2754. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  2755. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2756. break;
  2757. case MSR_IA32_VMX_MISC:
  2758. *pdata = vmx_control_msr(
  2759. vmx->nested.nested_vmx_misc_low,
  2760. vmx->nested.nested_vmx_misc_high);
  2761. break;
  2762. case MSR_IA32_VMX_CR0_FIXED0:
  2763. *pdata = vmx->nested.nested_vmx_cr0_fixed0;
  2764. break;
  2765. case MSR_IA32_VMX_CR0_FIXED1:
  2766. *pdata = vmx->nested.nested_vmx_cr0_fixed1;
  2767. break;
  2768. case MSR_IA32_VMX_CR4_FIXED0:
  2769. *pdata = vmx->nested.nested_vmx_cr4_fixed0;
  2770. break;
  2771. case MSR_IA32_VMX_CR4_FIXED1:
  2772. *pdata = vmx->nested.nested_vmx_cr4_fixed1;
  2773. break;
  2774. case MSR_IA32_VMX_VMCS_ENUM:
  2775. *pdata = vmx->nested.nested_vmx_vmcs_enum;
  2776. break;
  2777. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2778. *pdata = vmx_control_msr(
  2779. vmx->nested.nested_vmx_secondary_ctls_low,
  2780. vmx->nested.nested_vmx_secondary_ctls_high);
  2781. break;
  2782. case MSR_IA32_VMX_EPT_VPID_CAP:
  2783. *pdata = vmx->nested.nested_vmx_ept_caps |
  2784. ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
  2785. break;
  2786. case MSR_IA32_VMX_VMFUNC:
  2787. *pdata = vmx->nested.nested_vmx_vmfunc_controls;
  2788. break;
  2789. default:
  2790. return 1;
  2791. }
  2792. return 0;
  2793. }
  2794. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  2795. uint64_t val)
  2796. {
  2797. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  2798. return !(val & ~valid_bits);
  2799. }
  2800. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  2801. {
  2802. return 1;
  2803. }
  2804. /*
  2805. * Reads an msr value (of 'msr_index') into 'pdata'.
  2806. * Returns 0 on success, non-0 otherwise.
  2807. * Assumes vcpu_load() was already called.
  2808. */
  2809. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2810. {
  2811. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2812. struct shared_msr_entry *msr;
  2813. switch (msr_info->index) {
  2814. #ifdef CONFIG_X86_64
  2815. case MSR_FS_BASE:
  2816. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  2817. break;
  2818. case MSR_GS_BASE:
  2819. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  2820. break;
  2821. case MSR_KERNEL_GS_BASE:
  2822. vmx_load_host_state(vmx);
  2823. msr_info->data = vmx->msr_guest_kernel_gs_base;
  2824. break;
  2825. #endif
  2826. case MSR_EFER:
  2827. return kvm_get_msr_common(vcpu, msr_info);
  2828. case MSR_IA32_TSC:
  2829. msr_info->data = guest_read_tsc(vcpu);
  2830. break;
  2831. case MSR_IA32_SPEC_CTRL:
  2832. if (!msr_info->host_initiated &&
  2833. !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
  2834. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2835. return 1;
  2836. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  2837. break;
  2838. case MSR_IA32_ARCH_CAPABILITIES:
  2839. if (!msr_info->host_initiated &&
  2840. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  2841. return 1;
  2842. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  2843. break;
  2844. case MSR_IA32_SYSENTER_CS:
  2845. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  2846. break;
  2847. case MSR_IA32_SYSENTER_EIP:
  2848. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  2849. break;
  2850. case MSR_IA32_SYSENTER_ESP:
  2851. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  2852. break;
  2853. case MSR_IA32_BNDCFGS:
  2854. if (!kvm_mpx_supported() ||
  2855. (!msr_info->host_initiated &&
  2856. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  2857. return 1;
  2858. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  2859. break;
  2860. case MSR_IA32_MCG_EXT_CTL:
  2861. if (!msr_info->host_initiated &&
  2862. !(vmx->msr_ia32_feature_control &
  2863. FEATURE_CONTROL_LMCE))
  2864. return 1;
  2865. msr_info->data = vcpu->arch.mcg_ext_ctl;
  2866. break;
  2867. case MSR_IA32_FEATURE_CONTROL:
  2868. msr_info->data = vmx->msr_ia32_feature_control;
  2869. break;
  2870. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2871. if (!nested_vmx_allowed(vcpu))
  2872. return 1;
  2873. return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
  2874. case MSR_IA32_XSS:
  2875. if (!vmx_xsaves_supported())
  2876. return 1;
  2877. msr_info->data = vcpu->arch.ia32_xss;
  2878. break;
  2879. case MSR_TSC_AUX:
  2880. if (!msr_info->host_initiated &&
  2881. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  2882. return 1;
  2883. /* Otherwise falls through */
  2884. default:
  2885. msr = find_msr_entry(vmx, msr_info->index);
  2886. if (msr) {
  2887. msr_info->data = msr->data;
  2888. break;
  2889. }
  2890. return kvm_get_msr_common(vcpu, msr_info);
  2891. }
  2892. return 0;
  2893. }
  2894. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  2895. /*
  2896. * Writes msr value into into the appropriate "register".
  2897. * Returns 0 on success, non-0 otherwise.
  2898. * Assumes vcpu_load() was already called.
  2899. */
  2900. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2901. {
  2902. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2903. struct shared_msr_entry *msr;
  2904. int ret = 0;
  2905. u32 msr_index = msr_info->index;
  2906. u64 data = msr_info->data;
  2907. switch (msr_index) {
  2908. case MSR_EFER:
  2909. ret = kvm_set_msr_common(vcpu, msr_info);
  2910. break;
  2911. #ifdef CONFIG_X86_64
  2912. case MSR_FS_BASE:
  2913. vmx_segment_cache_clear(vmx);
  2914. vmcs_writel(GUEST_FS_BASE, data);
  2915. break;
  2916. case MSR_GS_BASE:
  2917. vmx_segment_cache_clear(vmx);
  2918. vmcs_writel(GUEST_GS_BASE, data);
  2919. break;
  2920. case MSR_KERNEL_GS_BASE:
  2921. vmx_load_host_state(vmx);
  2922. vmx->msr_guest_kernel_gs_base = data;
  2923. break;
  2924. #endif
  2925. case MSR_IA32_SYSENTER_CS:
  2926. vmcs_write32(GUEST_SYSENTER_CS, data);
  2927. break;
  2928. case MSR_IA32_SYSENTER_EIP:
  2929. vmcs_writel(GUEST_SYSENTER_EIP, data);
  2930. break;
  2931. case MSR_IA32_SYSENTER_ESP:
  2932. vmcs_writel(GUEST_SYSENTER_ESP, data);
  2933. break;
  2934. case MSR_IA32_BNDCFGS:
  2935. if (!kvm_mpx_supported() ||
  2936. (!msr_info->host_initiated &&
  2937. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  2938. return 1;
  2939. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  2940. (data & MSR_IA32_BNDCFGS_RSVD))
  2941. return 1;
  2942. vmcs_write64(GUEST_BNDCFGS, data);
  2943. break;
  2944. case MSR_IA32_TSC:
  2945. kvm_write_tsc(vcpu, msr_info);
  2946. break;
  2947. case MSR_IA32_SPEC_CTRL:
  2948. if (!msr_info->host_initiated &&
  2949. !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
  2950. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2951. return 1;
  2952. /* The STIBP bit doesn't fault even if it's not advertised */
  2953. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
  2954. return 1;
  2955. vmx->spec_ctrl = data;
  2956. if (!data)
  2957. break;
  2958. /*
  2959. * For non-nested:
  2960. * When it's written (to non-zero) for the first time, pass
  2961. * it through.
  2962. *
  2963. * For nested:
  2964. * The handling of the MSR bitmap for L2 guests is done in
  2965. * nested_vmx_merge_msr_bitmap. We should not touch the
  2966. * vmcs02.msr_bitmap here since it gets completely overwritten
  2967. * in the merging. We update the vmcs01 here for L1 as well
  2968. * since it will end up touching the MSR anyway now.
  2969. */
  2970. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  2971. MSR_IA32_SPEC_CTRL,
  2972. MSR_TYPE_RW);
  2973. break;
  2974. case MSR_IA32_PRED_CMD:
  2975. if (!msr_info->host_initiated &&
  2976. !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
  2977. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2978. return 1;
  2979. if (data & ~PRED_CMD_IBPB)
  2980. return 1;
  2981. if (!data)
  2982. break;
  2983. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  2984. /*
  2985. * For non-nested:
  2986. * When it's written (to non-zero) for the first time, pass
  2987. * it through.
  2988. *
  2989. * For nested:
  2990. * The handling of the MSR bitmap for L2 guests is done in
  2991. * nested_vmx_merge_msr_bitmap. We should not touch the
  2992. * vmcs02.msr_bitmap here since it gets completely overwritten
  2993. * in the merging.
  2994. */
  2995. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  2996. MSR_TYPE_W);
  2997. break;
  2998. case MSR_IA32_ARCH_CAPABILITIES:
  2999. if (!msr_info->host_initiated)
  3000. return 1;
  3001. vmx->arch_capabilities = data;
  3002. break;
  3003. case MSR_IA32_CR_PAT:
  3004. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3005. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3006. return 1;
  3007. vmcs_write64(GUEST_IA32_PAT, data);
  3008. vcpu->arch.pat = data;
  3009. break;
  3010. }
  3011. ret = kvm_set_msr_common(vcpu, msr_info);
  3012. break;
  3013. case MSR_IA32_TSC_ADJUST:
  3014. ret = kvm_set_msr_common(vcpu, msr_info);
  3015. break;
  3016. case MSR_IA32_MCG_EXT_CTL:
  3017. if ((!msr_info->host_initiated &&
  3018. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3019. FEATURE_CONTROL_LMCE)) ||
  3020. (data & ~MCG_EXT_CTL_LMCE_EN))
  3021. return 1;
  3022. vcpu->arch.mcg_ext_ctl = data;
  3023. break;
  3024. case MSR_IA32_FEATURE_CONTROL:
  3025. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3026. (to_vmx(vcpu)->msr_ia32_feature_control &
  3027. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3028. return 1;
  3029. vmx->msr_ia32_feature_control = data;
  3030. if (msr_info->host_initiated && data == 0)
  3031. vmx_leave_nested(vcpu);
  3032. break;
  3033. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3034. if (!msr_info->host_initiated)
  3035. return 1; /* they are read-only */
  3036. if (!nested_vmx_allowed(vcpu))
  3037. return 1;
  3038. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3039. case MSR_IA32_XSS:
  3040. if (!vmx_xsaves_supported())
  3041. return 1;
  3042. /*
  3043. * The only supported bit as of Skylake is bit 8, but
  3044. * it is not supported on KVM.
  3045. */
  3046. if (data != 0)
  3047. return 1;
  3048. vcpu->arch.ia32_xss = data;
  3049. if (vcpu->arch.ia32_xss != host_xss)
  3050. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3051. vcpu->arch.ia32_xss, host_xss);
  3052. else
  3053. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3054. break;
  3055. case MSR_TSC_AUX:
  3056. if (!msr_info->host_initiated &&
  3057. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3058. return 1;
  3059. /* Check reserved bit, higher 32 bits should be zero */
  3060. if ((data >> 32) != 0)
  3061. return 1;
  3062. /* Otherwise falls through */
  3063. default:
  3064. msr = find_msr_entry(vmx, msr_index);
  3065. if (msr) {
  3066. u64 old_msr_data = msr->data;
  3067. msr->data = data;
  3068. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3069. preempt_disable();
  3070. ret = kvm_set_shared_msr(msr->index, msr->data,
  3071. msr->mask);
  3072. preempt_enable();
  3073. if (ret)
  3074. msr->data = old_msr_data;
  3075. }
  3076. break;
  3077. }
  3078. ret = kvm_set_msr_common(vcpu, msr_info);
  3079. }
  3080. return ret;
  3081. }
  3082. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3083. {
  3084. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3085. switch (reg) {
  3086. case VCPU_REGS_RSP:
  3087. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3088. break;
  3089. case VCPU_REGS_RIP:
  3090. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3091. break;
  3092. case VCPU_EXREG_PDPTR:
  3093. if (enable_ept)
  3094. ept_save_pdptrs(vcpu);
  3095. break;
  3096. default:
  3097. break;
  3098. }
  3099. }
  3100. static __init int cpu_has_kvm_support(void)
  3101. {
  3102. return cpu_has_vmx();
  3103. }
  3104. static __init int vmx_disabled_by_bios(void)
  3105. {
  3106. u64 msr;
  3107. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3108. if (msr & FEATURE_CONTROL_LOCKED) {
  3109. /* launched w/ TXT and VMX disabled */
  3110. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3111. && tboot_enabled())
  3112. return 1;
  3113. /* launched w/o TXT and VMX only enabled w/ TXT */
  3114. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3115. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3116. && !tboot_enabled()) {
  3117. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3118. "activate TXT before enabling KVM\n");
  3119. return 1;
  3120. }
  3121. /* launched w/o TXT and VMX disabled */
  3122. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3123. && !tboot_enabled())
  3124. return 1;
  3125. }
  3126. return 0;
  3127. }
  3128. static void kvm_cpu_vmxon(u64 addr)
  3129. {
  3130. cr4_set_bits(X86_CR4_VMXE);
  3131. intel_pt_handle_vmx(1);
  3132. asm volatile (ASM_VMX_VMXON_RAX
  3133. : : "a"(&addr), "m"(addr)
  3134. : "memory", "cc");
  3135. }
  3136. static int hardware_enable(void)
  3137. {
  3138. int cpu = raw_smp_processor_id();
  3139. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3140. u64 old, test_bits;
  3141. if (cr4_read_shadow() & X86_CR4_VMXE)
  3142. return -EBUSY;
  3143. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3144. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3145. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3146. /*
  3147. * Now we can enable the vmclear operation in kdump
  3148. * since the loaded_vmcss_on_cpu list on this cpu
  3149. * has been initialized.
  3150. *
  3151. * Though the cpu is not in VMX operation now, there
  3152. * is no problem to enable the vmclear operation
  3153. * for the loaded_vmcss_on_cpu list is empty!
  3154. */
  3155. crash_enable_local_vmclear(cpu);
  3156. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3157. test_bits = FEATURE_CONTROL_LOCKED;
  3158. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3159. if (tboot_enabled())
  3160. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3161. if ((old & test_bits) != test_bits) {
  3162. /* enable and lock */
  3163. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3164. }
  3165. kvm_cpu_vmxon(phys_addr);
  3166. if (enable_ept)
  3167. ept_sync_global();
  3168. return 0;
  3169. }
  3170. static void vmclear_local_loaded_vmcss(void)
  3171. {
  3172. int cpu = raw_smp_processor_id();
  3173. struct loaded_vmcs *v, *n;
  3174. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3175. loaded_vmcss_on_cpu_link)
  3176. __loaded_vmcs_clear(v);
  3177. }
  3178. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3179. * tricks.
  3180. */
  3181. static void kvm_cpu_vmxoff(void)
  3182. {
  3183. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3184. intel_pt_handle_vmx(0);
  3185. cr4_clear_bits(X86_CR4_VMXE);
  3186. }
  3187. static void hardware_disable(void)
  3188. {
  3189. vmclear_local_loaded_vmcss();
  3190. kvm_cpu_vmxoff();
  3191. }
  3192. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3193. u32 msr, u32 *result)
  3194. {
  3195. u32 vmx_msr_low, vmx_msr_high;
  3196. u32 ctl = ctl_min | ctl_opt;
  3197. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3198. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3199. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3200. /* Ensure minimum (required) set of control bits are supported. */
  3201. if (ctl_min & ~ctl)
  3202. return -EIO;
  3203. *result = ctl;
  3204. return 0;
  3205. }
  3206. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3207. {
  3208. u32 vmx_msr_low, vmx_msr_high;
  3209. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3210. return vmx_msr_high & ctl;
  3211. }
  3212. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3213. {
  3214. u32 vmx_msr_low, vmx_msr_high;
  3215. u32 min, opt, min2, opt2;
  3216. u32 _pin_based_exec_control = 0;
  3217. u32 _cpu_based_exec_control = 0;
  3218. u32 _cpu_based_2nd_exec_control = 0;
  3219. u32 _vmexit_control = 0;
  3220. u32 _vmentry_control = 0;
  3221. min = CPU_BASED_HLT_EXITING |
  3222. #ifdef CONFIG_X86_64
  3223. CPU_BASED_CR8_LOAD_EXITING |
  3224. CPU_BASED_CR8_STORE_EXITING |
  3225. #endif
  3226. CPU_BASED_CR3_LOAD_EXITING |
  3227. CPU_BASED_CR3_STORE_EXITING |
  3228. CPU_BASED_UNCOND_IO_EXITING |
  3229. CPU_BASED_MOV_DR_EXITING |
  3230. CPU_BASED_USE_TSC_OFFSETING |
  3231. CPU_BASED_INVLPG_EXITING |
  3232. CPU_BASED_RDPMC_EXITING;
  3233. if (!kvm_mwait_in_guest())
  3234. min |= CPU_BASED_MWAIT_EXITING |
  3235. CPU_BASED_MONITOR_EXITING;
  3236. opt = CPU_BASED_TPR_SHADOW |
  3237. CPU_BASED_USE_MSR_BITMAPS |
  3238. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3239. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3240. &_cpu_based_exec_control) < 0)
  3241. return -EIO;
  3242. #ifdef CONFIG_X86_64
  3243. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3244. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3245. ~CPU_BASED_CR8_STORE_EXITING;
  3246. #endif
  3247. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3248. min2 = 0;
  3249. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3250. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3251. SECONDARY_EXEC_WBINVD_EXITING |
  3252. SECONDARY_EXEC_ENABLE_VPID |
  3253. SECONDARY_EXEC_ENABLE_EPT |
  3254. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3255. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3256. SECONDARY_EXEC_DESC |
  3257. SECONDARY_EXEC_RDTSCP |
  3258. SECONDARY_EXEC_ENABLE_INVPCID |
  3259. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3260. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3261. SECONDARY_EXEC_SHADOW_VMCS |
  3262. SECONDARY_EXEC_XSAVES |
  3263. SECONDARY_EXEC_RDSEED_EXITING |
  3264. SECONDARY_EXEC_RDRAND_EXITING |
  3265. SECONDARY_EXEC_ENABLE_PML |
  3266. SECONDARY_EXEC_TSC_SCALING |
  3267. SECONDARY_EXEC_ENABLE_VMFUNC;
  3268. if (adjust_vmx_controls(min2, opt2,
  3269. MSR_IA32_VMX_PROCBASED_CTLS2,
  3270. &_cpu_based_2nd_exec_control) < 0)
  3271. return -EIO;
  3272. }
  3273. #ifndef CONFIG_X86_64
  3274. if (!(_cpu_based_2nd_exec_control &
  3275. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3276. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3277. #endif
  3278. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3279. _cpu_based_2nd_exec_control &= ~(
  3280. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3281. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3282. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3283. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3284. &vmx_capability.ept, &vmx_capability.vpid);
  3285. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3286. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3287. enabled */
  3288. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3289. CPU_BASED_CR3_STORE_EXITING |
  3290. CPU_BASED_INVLPG_EXITING);
  3291. } else if (vmx_capability.ept) {
  3292. vmx_capability.ept = 0;
  3293. pr_warn_once("EPT CAP should not exist if not support "
  3294. "1-setting enable EPT VM-execution control\n");
  3295. }
  3296. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3297. vmx_capability.vpid) {
  3298. vmx_capability.vpid = 0;
  3299. pr_warn_once("VPID CAP should not exist if not support "
  3300. "1-setting enable VPID VM-execution control\n");
  3301. }
  3302. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3303. #ifdef CONFIG_X86_64
  3304. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3305. #endif
  3306. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3307. VM_EXIT_CLEAR_BNDCFGS;
  3308. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3309. &_vmexit_control) < 0)
  3310. return -EIO;
  3311. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3312. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3313. PIN_BASED_VMX_PREEMPTION_TIMER;
  3314. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3315. &_pin_based_exec_control) < 0)
  3316. return -EIO;
  3317. if (cpu_has_broken_vmx_preemption_timer())
  3318. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3319. if (!(_cpu_based_2nd_exec_control &
  3320. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3321. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3322. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3323. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3324. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3325. &_vmentry_control) < 0)
  3326. return -EIO;
  3327. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3328. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3329. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3330. return -EIO;
  3331. #ifdef CONFIG_X86_64
  3332. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3333. if (vmx_msr_high & (1u<<16))
  3334. return -EIO;
  3335. #endif
  3336. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3337. if (((vmx_msr_high >> 18) & 15) != 6)
  3338. return -EIO;
  3339. vmcs_conf->size = vmx_msr_high & 0x1fff;
  3340. vmcs_conf->order = get_order(vmcs_conf->size);
  3341. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  3342. vmcs_conf->revision_id = vmx_msr_low;
  3343. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  3344. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  3345. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  3346. vmcs_conf->vmexit_ctrl = _vmexit_control;
  3347. vmcs_conf->vmentry_ctrl = _vmentry_control;
  3348. cpu_has_load_ia32_efer =
  3349. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3350. VM_ENTRY_LOAD_IA32_EFER)
  3351. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3352. VM_EXIT_LOAD_IA32_EFER);
  3353. cpu_has_load_perf_global_ctrl =
  3354. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3355. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  3356. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3357. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  3358. /*
  3359. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  3360. * but due to errata below it can't be used. Workaround is to use
  3361. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  3362. *
  3363. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  3364. *
  3365. * AAK155 (model 26)
  3366. * AAP115 (model 30)
  3367. * AAT100 (model 37)
  3368. * BC86,AAY89,BD102 (model 44)
  3369. * BA97 (model 46)
  3370. *
  3371. */
  3372. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  3373. switch (boot_cpu_data.x86_model) {
  3374. case 26:
  3375. case 30:
  3376. case 37:
  3377. case 44:
  3378. case 46:
  3379. cpu_has_load_perf_global_ctrl = false;
  3380. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  3381. "does not work properly. Using workaround\n");
  3382. break;
  3383. default:
  3384. break;
  3385. }
  3386. }
  3387. if (boot_cpu_has(X86_FEATURE_XSAVES))
  3388. rdmsrl(MSR_IA32_XSS, host_xss);
  3389. return 0;
  3390. }
  3391. static struct vmcs *alloc_vmcs_cpu(int cpu)
  3392. {
  3393. int node = cpu_to_node(cpu);
  3394. struct page *pages;
  3395. struct vmcs *vmcs;
  3396. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  3397. if (!pages)
  3398. return NULL;
  3399. vmcs = page_address(pages);
  3400. memset(vmcs, 0, vmcs_config.size);
  3401. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  3402. return vmcs;
  3403. }
  3404. static void free_vmcs(struct vmcs *vmcs)
  3405. {
  3406. free_pages((unsigned long)vmcs, vmcs_config.order);
  3407. }
  3408. /*
  3409. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  3410. */
  3411. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3412. {
  3413. if (!loaded_vmcs->vmcs)
  3414. return;
  3415. loaded_vmcs_clear(loaded_vmcs);
  3416. free_vmcs(loaded_vmcs->vmcs);
  3417. loaded_vmcs->vmcs = NULL;
  3418. if (loaded_vmcs->msr_bitmap)
  3419. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  3420. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  3421. }
  3422. static struct vmcs *alloc_vmcs(void)
  3423. {
  3424. return alloc_vmcs_cpu(raw_smp_processor_id());
  3425. }
  3426. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3427. {
  3428. loaded_vmcs->vmcs = alloc_vmcs();
  3429. if (!loaded_vmcs->vmcs)
  3430. return -ENOMEM;
  3431. loaded_vmcs->shadow_vmcs = NULL;
  3432. loaded_vmcs_init(loaded_vmcs);
  3433. if (cpu_has_vmx_msr_bitmap()) {
  3434. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  3435. if (!loaded_vmcs->msr_bitmap)
  3436. goto out_vmcs;
  3437. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  3438. }
  3439. return 0;
  3440. out_vmcs:
  3441. free_loaded_vmcs(loaded_vmcs);
  3442. return -ENOMEM;
  3443. }
  3444. static void free_kvm_area(void)
  3445. {
  3446. int cpu;
  3447. for_each_possible_cpu(cpu) {
  3448. free_vmcs(per_cpu(vmxarea, cpu));
  3449. per_cpu(vmxarea, cpu) = NULL;
  3450. }
  3451. }
  3452. enum vmcs_field_width {
  3453. VMCS_FIELD_WIDTH_U16 = 0,
  3454. VMCS_FIELD_WIDTH_U64 = 1,
  3455. VMCS_FIELD_WIDTH_U32 = 2,
  3456. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  3457. };
  3458. static inline int vmcs_field_width(unsigned long field)
  3459. {
  3460. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  3461. return VMCS_FIELD_WIDTH_U32;
  3462. return (field >> 13) & 0x3 ;
  3463. }
  3464. static inline int vmcs_field_readonly(unsigned long field)
  3465. {
  3466. return (((field >> 10) & 0x3) == 1);
  3467. }
  3468. static void init_vmcs_shadow_fields(void)
  3469. {
  3470. int i, j;
  3471. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  3472. u16 field = shadow_read_only_fields[i];
  3473. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3474. (i + 1 == max_shadow_read_only_fields ||
  3475. shadow_read_only_fields[i + 1] != field + 1))
  3476. pr_err("Missing field from shadow_read_only_field %x\n",
  3477. field + 1);
  3478. clear_bit(field, vmx_vmread_bitmap);
  3479. #ifdef CONFIG_X86_64
  3480. if (field & 1)
  3481. continue;
  3482. #endif
  3483. if (j < i)
  3484. shadow_read_only_fields[j] = field;
  3485. j++;
  3486. }
  3487. max_shadow_read_only_fields = j;
  3488. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  3489. u16 field = shadow_read_write_fields[i];
  3490. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3491. (i + 1 == max_shadow_read_write_fields ||
  3492. shadow_read_write_fields[i + 1] != field + 1))
  3493. pr_err("Missing field from shadow_read_write_field %x\n",
  3494. field + 1);
  3495. /*
  3496. * PML and the preemption timer can be emulated, but the
  3497. * processor cannot vmwrite to fields that don't exist
  3498. * on bare metal.
  3499. */
  3500. switch (field) {
  3501. case GUEST_PML_INDEX:
  3502. if (!cpu_has_vmx_pml())
  3503. continue;
  3504. break;
  3505. case VMX_PREEMPTION_TIMER_VALUE:
  3506. if (!cpu_has_vmx_preemption_timer())
  3507. continue;
  3508. break;
  3509. case GUEST_INTR_STATUS:
  3510. if (!cpu_has_vmx_apicv())
  3511. continue;
  3512. break;
  3513. default:
  3514. break;
  3515. }
  3516. clear_bit(field, vmx_vmwrite_bitmap);
  3517. clear_bit(field, vmx_vmread_bitmap);
  3518. #ifdef CONFIG_X86_64
  3519. if (field & 1)
  3520. continue;
  3521. #endif
  3522. if (j < i)
  3523. shadow_read_write_fields[j] = field;
  3524. j++;
  3525. }
  3526. max_shadow_read_write_fields = j;
  3527. }
  3528. static __init int alloc_kvm_area(void)
  3529. {
  3530. int cpu;
  3531. for_each_possible_cpu(cpu) {
  3532. struct vmcs *vmcs;
  3533. vmcs = alloc_vmcs_cpu(cpu);
  3534. if (!vmcs) {
  3535. free_kvm_area();
  3536. return -ENOMEM;
  3537. }
  3538. per_cpu(vmxarea, cpu) = vmcs;
  3539. }
  3540. return 0;
  3541. }
  3542. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  3543. struct kvm_segment *save)
  3544. {
  3545. if (!emulate_invalid_guest_state) {
  3546. /*
  3547. * CS and SS RPL should be equal during guest entry according
  3548. * to VMX spec, but in reality it is not always so. Since vcpu
  3549. * is in the middle of the transition from real mode to
  3550. * protected mode it is safe to assume that RPL 0 is a good
  3551. * default value.
  3552. */
  3553. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  3554. save->selector &= ~SEGMENT_RPL_MASK;
  3555. save->dpl = save->selector & SEGMENT_RPL_MASK;
  3556. save->s = 1;
  3557. }
  3558. vmx_set_segment(vcpu, save, seg);
  3559. }
  3560. static void enter_pmode(struct kvm_vcpu *vcpu)
  3561. {
  3562. unsigned long flags;
  3563. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3564. /*
  3565. * Update real mode segment cache. It may be not up-to-date if sement
  3566. * register was written while vcpu was in a guest mode.
  3567. */
  3568. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  3569. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  3570. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  3571. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  3572. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  3573. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  3574. vmx->rmode.vm86_active = 0;
  3575. vmx_segment_cache_clear(vmx);
  3576. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  3577. flags = vmcs_readl(GUEST_RFLAGS);
  3578. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  3579. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  3580. vmcs_writel(GUEST_RFLAGS, flags);
  3581. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  3582. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  3583. update_exception_bitmap(vcpu);
  3584. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  3585. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  3586. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  3587. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  3588. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  3589. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  3590. }
  3591. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  3592. {
  3593. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3594. struct kvm_segment var = *save;
  3595. var.dpl = 0x3;
  3596. if (seg == VCPU_SREG_CS)
  3597. var.type = 0x3;
  3598. if (!emulate_invalid_guest_state) {
  3599. var.selector = var.base >> 4;
  3600. var.base = var.base & 0xffff0;
  3601. var.limit = 0xffff;
  3602. var.g = 0;
  3603. var.db = 0;
  3604. var.present = 1;
  3605. var.s = 1;
  3606. var.l = 0;
  3607. var.unusable = 0;
  3608. var.type = 0x3;
  3609. var.avl = 0;
  3610. if (save->base & 0xf)
  3611. printk_once(KERN_WARNING "kvm: segment base is not "
  3612. "paragraph aligned when entering "
  3613. "protected mode (seg=%d)", seg);
  3614. }
  3615. vmcs_write16(sf->selector, var.selector);
  3616. vmcs_writel(sf->base, var.base);
  3617. vmcs_write32(sf->limit, var.limit);
  3618. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  3619. }
  3620. static void enter_rmode(struct kvm_vcpu *vcpu)
  3621. {
  3622. unsigned long flags;
  3623. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3624. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  3625. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  3626. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  3627. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  3628. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  3629. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  3630. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  3631. vmx->rmode.vm86_active = 1;
  3632. /*
  3633. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  3634. * vcpu. Warn the user that an update is overdue.
  3635. */
  3636. if (!vcpu->kvm->arch.tss_addr)
  3637. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  3638. "called before entering vcpu\n");
  3639. vmx_segment_cache_clear(vmx);
  3640. vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
  3641. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  3642. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  3643. flags = vmcs_readl(GUEST_RFLAGS);
  3644. vmx->rmode.save_rflags = flags;
  3645. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  3646. vmcs_writel(GUEST_RFLAGS, flags);
  3647. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  3648. update_exception_bitmap(vcpu);
  3649. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  3650. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  3651. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  3652. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  3653. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  3654. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  3655. kvm_mmu_reset_context(vcpu);
  3656. }
  3657. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  3658. {
  3659. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3660. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  3661. if (!msr)
  3662. return;
  3663. /*
  3664. * Force kernel_gs_base reloading before EFER changes, as control
  3665. * of this msr depends on is_long_mode().
  3666. */
  3667. vmx_load_host_state(to_vmx(vcpu));
  3668. vcpu->arch.efer = efer;
  3669. if (efer & EFER_LMA) {
  3670. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3671. msr->data = efer;
  3672. } else {
  3673. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3674. msr->data = efer & ~EFER_LME;
  3675. }
  3676. setup_msrs(vmx);
  3677. }
  3678. #ifdef CONFIG_X86_64
  3679. static void enter_lmode(struct kvm_vcpu *vcpu)
  3680. {
  3681. u32 guest_tr_ar;
  3682. vmx_segment_cache_clear(to_vmx(vcpu));
  3683. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  3684. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  3685. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  3686. __func__);
  3687. vmcs_write32(GUEST_TR_AR_BYTES,
  3688. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  3689. | VMX_AR_TYPE_BUSY_64_TSS);
  3690. }
  3691. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  3692. }
  3693. static void exit_lmode(struct kvm_vcpu *vcpu)
  3694. {
  3695. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3696. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  3697. }
  3698. #endif
  3699. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  3700. bool invalidate_gpa)
  3701. {
  3702. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  3703. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3704. return;
  3705. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  3706. } else {
  3707. vpid_sync_context(vpid);
  3708. }
  3709. }
  3710. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  3711. {
  3712. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  3713. }
  3714. static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
  3715. {
  3716. if (enable_ept)
  3717. vmx_flush_tlb(vcpu, true);
  3718. }
  3719. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  3720. {
  3721. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  3722. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  3723. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  3724. }
  3725. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  3726. {
  3727. if (enable_ept && is_paging(vcpu))
  3728. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  3729. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  3730. }
  3731. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  3732. {
  3733. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  3734. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  3735. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  3736. }
  3737. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  3738. {
  3739. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3740. if (!test_bit(VCPU_EXREG_PDPTR,
  3741. (unsigned long *)&vcpu->arch.regs_dirty))
  3742. return;
  3743. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3744. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  3745. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  3746. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  3747. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  3748. }
  3749. }
  3750. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  3751. {
  3752. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3753. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3754. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  3755. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  3756. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  3757. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  3758. }
  3759. __set_bit(VCPU_EXREG_PDPTR,
  3760. (unsigned long *)&vcpu->arch.regs_avail);
  3761. __set_bit(VCPU_EXREG_PDPTR,
  3762. (unsigned long *)&vcpu->arch.regs_dirty);
  3763. }
  3764. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3765. {
  3766. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
  3767. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
  3768. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  3769. if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
  3770. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  3771. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  3772. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  3773. return fixed_bits_valid(val, fixed0, fixed1);
  3774. }
  3775. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3776. {
  3777. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
  3778. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
  3779. return fixed_bits_valid(val, fixed0, fixed1);
  3780. }
  3781. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3782. {
  3783. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
  3784. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
  3785. return fixed_bits_valid(val, fixed0, fixed1);
  3786. }
  3787. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  3788. #define nested_guest_cr4_valid nested_cr4_valid
  3789. #define nested_host_cr4_valid nested_cr4_valid
  3790. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  3791. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  3792. unsigned long cr0,
  3793. struct kvm_vcpu *vcpu)
  3794. {
  3795. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  3796. vmx_decache_cr3(vcpu);
  3797. if (!(cr0 & X86_CR0_PG)) {
  3798. /* From paging/starting to nonpaging */
  3799. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3800. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  3801. (CPU_BASED_CR3_LOAD_EXITING |
  3802. CPU_BASED_CR3_STORE_EXITING));
  3803. vcpu->arch.cr0 = cr0;
  3804. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3805. } else if (!is_paging(vcpu)) {
  3806. /* From nonpaging to paging */
  3807. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3808. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  3809. ~(CPU_BASED_CR3_LOAD_EXITING |
  3810. CPU_BASED_CR3_STORE_EXITING));
  3811. vcpu->arch.cr0 = cr0;
  3812. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3813. }
  3814. if (!(cr0 & X86_CR0_WP))
  3815. *hw_cr0 &= ~X86_CR0_WP;
  3816. }
  3817. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  3818. {
  3819. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3820. unsigned long hw_cr0;
  3821. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  3822. if (enable_unrestricted_guest)
  3823. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  3824. else {
  3825. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  3826. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  3827. enter_pmode(vcpu);
  3828. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  3829. enter_rmode(vcpu);
  3830. }
  3831. #ifdef CONFIG_X86_64
  3832. if (vcpu->arch.efer & EFER_LME) {
  3833. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  3834. enter_lmode(vcpu);
  3835. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  3836. exit_lmode(vcpu);
  3837. }
  3838. #endif
  3839. if (enable_ept)
  3840. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  3841. vmcs_writel(CR0_READ_SHADOW, cr0);
  3842. vmcs_writel(GUEST_CR0, hw_cr0);
  3843. vcpu->arch.cr0 = cr0;
  3844. /* depends on vcpu->arch.cr0 to be set to a new value */
  3845. vmx->emulation_required = emulation_required(vcpu);
  3846. }
  3847. static int get_ept_level(struct kvm_vcpu *vcpu)
  3848. {
  3849. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  3850. return 5;
  3851. return 4;
  3852. }
  3853. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  3854. {
  3855. u64 eptp = VMX_EPTP_MT_WB;
  3856. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  3857. if (enable_ept_ad_bits &&
  3858. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  3859. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  3860. eptp |= (root_hpa & PAGE_MASK);
  3861. return eptp;
  3862. }
  3863. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  3864. {
  3865. unsigned long guest_cr3;
  3866. u64 eptp;
  3867. guest_cr3 = cr3;
  3868. if (enable_ept) {
  3869. eptp = construct_eptp(vcpu, cr3);
  3870. vmcs_write64(EPT_POINTER, eptp);
  3871. if (is_paging(vcpu) || is_guest_mode(vcpu))
  3872. guest_cr3 = kvm_read_cr3(vcpu);
  3873. else
  3874. guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
  3875. ept_load_pdptrs(vcpu);
  3876. }
  3877. vmx_flush_tlb(vcpu, true);
  3878. vmcs_writel(GUEST_CR3, guest_cr3);
  3879. }
  3880. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  3881. {
  3882. /*
  3883. * Pass through host's Machine Check Enable value to hw_cr4, which
  3884. * is in force while we are in guest mode. Do not let guests control
  3885. * this bit, even if host CR4.MCE == 0.
  3886. */
  3887. unsigned long hw_cr4 =
  3888. (cr4_read_shadow() & X86_CR4_MCE) |
  3889. (cr4 & ~X86_CR4_MCE) |
  3890. (to_vmx(vcpu)->rmode.vm86_active ?
  3891. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  3892. if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
  3893. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  3894. SECONDARY_EXEC_DESC);
  3895. hw_cr4 &= ~X86_CR4_UMIP;
  3896. } else if (!is_guest_mode(vcpu) ||
  3897. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  3898. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  3899. SECONDARY_EXEC_DESC);
  3900. if (cr4 & X86_CR4_VMXE) {
  3901. /*
  3902. * To use VMXON (and later other VMX instructions), a guest
  3903. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  3904. * So basically the check on whether to allow nested VMX
  3905. * is here.
  3906. */
  3907. if (!nested_vmx_allowed(vcpu))
  3908. return 1;
  3909. }
  3910. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  3911. return 1;
  3912. vcpu->arch.cr4 = cr4;
  3913. if (enable_ept) {
  3914. if (!is_paging(vcpu)) {
  3915. hw_cr4 &= ~X86_CR4_PAE;
  3916. hw_cr4 |= X86_CR4_PSE;
  3917. } else if (!(cr4 & X86_CR4_PAE)) {
  3918. hw_cr4 &= ~X86_CR4_PAE;
  3919. }
  3920. }
  3921. if (!enable_unrestricted_guest && !is_paging(vcpu))
  3922. /*
  3923. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  3924. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  3925. * to be manually disabled when guest switches to non-paging
  3926. * mode.
  3927. *
  3928. * If !enable_unrestricted_guest, the CPU is always running
  3929. * with CR0.PG=1 and CR4 needs to be modified.
  3930. * If enable_unrestricted_guest, the CPU automatically
  3931. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  3932. */
  3933. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  3934. vmcs_writel(CR4_READ_SHADOW, cr4);
  3935. vmcs_writel(GUEST_CR4, hw_cr4);
  3936. return 0;
  3937. }
  3938. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  3939. struct kvm_segment *var, int seg)
  3940. {
  3941. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3942. u32 ar;
  3943. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3944. *var = vmx->rmode.segs[seg];
  3945. if (seg == VCPU_SREG_TR
  3946. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  3947. return;
  3948. var->base = vmx_read_guest_seg_base(vmx, seg);
  3949. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3950. return;
  3951. }
  3952. var->base = vmx_read_guest_seg_base(vmx, seg);
  3953. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  3954. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3955. ar = vmx_read_guest_seg_ar(vmx, seg);
  3956. var->unusable = (ar >> 16) & 1;
  3957. var->type = ar & 15;
  3958. var->s = (ar >> 4) & 1;
  3959. var->dpl = (ar >> 5) & 3;
  3960. /*
  3961. * Some userspaces do not preserve unusable property. Since usable
  3962. * segment has to be present according to VMX spec we can use present
  3963. * property to amend userspace bug by making unusable segment always
  3964. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  3965. * segment as unusable.
  3966. */
  3967. var->present = !var->unusable;
  3968. var->avl = (ar >> 12) & 1;
  3969. var->l = (ar >> 13) & 1;
  3970. var->db = (ar >> 14) & 1;
  3971. var->g = (ar >> 15) & 1;
  3972. }
  3973. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3974. {
  3975. struct kvm_segment s;
  3976. if (to_vmx(vcpu)->rmode.vm86_active) {
  3977. vmx_get_segment(vcpu, &s, seg);
  3978. return s.base;
  3979. }
  3980. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  3981. }
  3982. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  3983. {
  3984. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3985. if (unlikely(vmx->rmode.vm86_active))
  3986. return 0;
  3987. else {
  3988. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  3989. return VMX_AR_DPL(ar);
  3990. }
  3991. }
  3992. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  3993. {
  3994. u32 ar;
  3995. if (var->unusable || !var->present)
  3996. ar = 1 << 16;
  3997. else {
  3998. ar = var->type & 15;
  3999. ar |= (var->s & 1) << 4;
  4000. ar |= (var->dpl & 3) << 5;
  4001. ar |= (var->present & 1) << 7;
  4002. ar |= (var->avl & 1) << 12;
  4003. ar |= (var->l & 1) << 13;
  4004. ar |= (var->db & 1) << 14;
  4005. ar |= (var->g & 1) << 15;
  4006. }
  4007. return ar;
  4008. }
  4009. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4010. struct kvm_segment *var, int seg)
  4011. {
  4012. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4013. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4014. vmx_segment_cache_clear(vmx);
  4015. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4016. vmx->rmode.segs[seg] = *var;
  4017. if (seg == VCPU_SREG_TR)
  4018. vmcs_write16(sf->selector, var->selector);
  4019. else if (var->s)
  4020. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4021. goto out;
  4022. }
  4023. vmcs_writel(sf->base, var->base);
  4024. vmcs_write32(sf->limit, var->limit);
  4025. vmcs_write16(sf->selector, var->selector);
  4026. /*
  4027. * Fix the "Accessed" bit in AR field of segment registers for older
  4028. * qemu binaries.
  4029. * IA32 arch specifies that at the time of processor reset the
  4030. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4031. * is setting it to 0 in the userland code. This causes invalid guest
  4032. * state vmexit when "unrestricted guest" mode is turned on.
  4033. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4034. * tree. Newer qemu binaries with that qemu fix would not need this
  4035. * kvm hack.
  4036. */
  4037. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4038. var->type |= 0x1; /* Accessed */
  4039. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4040. out:
  4041. vmx->emulation_required = emulation_required(vcpu);
  4042. }
  4043. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4044. {
  4045. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4046. *db = (ar >> 14) & 1;
  4047. *l = (ar >> 13) & 1;
  4048. }
  4049. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4050. {
  4051. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4052. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4053. }
  4054. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4055. {
  4056. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4057. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4058. }
  4059. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4060. {
  4061. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4062. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4063. }
  4064. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4065. {
  4066. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4067. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4068. }
  4069. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4070. {
  4071. struct kvm_segment var;
  4072. u32 ar;
  4073. vmx_get_segment(vcpu, &var, seg);
  4074. var.dpl = 0x3;
  4075. if (seg == VCPU_SREG_CS)
  4076. var.type = 0x3;
  4077. ar = vmx_segment_access_rights(&var);
  4078. if (var.base != (var.selector << 4))
  4079. return false;
  4080. if (var.limit != 0xffff)
  4081. return false;
  4082. if (ar != 0xf3)
  4083. return false;
  4084. return true;
  4085. }
  4086. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4087. {
  4088. struct kvm_segment cs;
  4089. unsigned int cs_rpl;
  4090. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4091. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4092. if (cs.unusable)
  4093. return false;
  4094. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4095. return false;
  4096. if (!cs.s)
  4097. return false;
  4098. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4099. if (cs.dpl > cs_rpl)
  4100. return false;
  4101. } else {
  4102. if (cs.dpl != cs_rpl)
  4103. return false;
  4104. }
  4105. if (!cs.present)
  4106. return false;
  4107. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4108. return true;
  4109. }
  4110. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4111. {
  4112. struct kvm_segment ss;
  4113. unsigned int ss_rpl;
  4114. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4115. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4116. if (ss.unusable)
  4117. return true;
  4118. if (ss.type != 3 && ss.type != 7)
  4119. return false;
  4120. if (!ss.s)
  4121. return false;
  4122. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4123. return false;
  4124. if (!ss.present)
  4125. return false;
  4126. return true;
  4127. }
  4128. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4129. {
  4130. struct kvm_segment var;
  4131. unsigned int rpl;
  4132. vmx_get_segment(vcpu, &var, seg);
  4133. rpl = var.selector & SEGMENT_RPL_MASK;
  4134. if (var.unusable)
  4135. return true;
  4136. if (!var.s)
  4137. return false;
  4138. if (!var.present)
  4139. return false;
  4140. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4141. if (var.dpl < rpl) /* DPL < RPL */
  4142. return false;
  4143. }
  4144. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4145. * rights flags
  4146. */
  4147. return true;
  4148. }
  4149. static bool tr_valid(struct kvm_vcpu *vcpu)
  4150. {
  4151. struct kvm_segment tr;
  4152. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4153. if (tr.unusable)
  4154. return false;
  4155. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4156. return false;
  4157. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4158. return false;
  4159. if (!tr.present)
  4160. return false;
  4161. return true;
  4162. }
  4163. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4164. {
  4165. struct kvm_segment ldtr;
  4166. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4167. if (ldtr.unusable)
  4168. return true;
  4169. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4170. return false;
  4171. if (ldtr.type != 2)
  4172. return false;
  4173. if (!ldtr.present)
  4174. return false;
  4175. return true;
  4176. }
  4177. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4178. {
  4179. struct kvm_segment cs, ss;
  4180. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4181. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4182. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4183. (ss.selector & SEGMENT_RPL_MASK));
  4184. }
  4185. /*
  4186. * Check if guest state is valid. Returns true if valid, false if
  4187. * not.
  4188. * We assume that registers are always usable
  4189. */
  4190. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4191. {
  4192. if (enable_unrestricted_guest)
  4193. return true;
  4194. /* real mode guest state checks */
  4195. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4196. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4197. return false;
  4198. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4199. return false;
  4200. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4201. return false;
  4202. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4203. return false;
  4204. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4205. return false;
  4206. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4207. return false;
  4208. } else {
  4209. /* protected mode guest state checks */
  4210. if (!cs_ss_rpl_check(vcpu))
  4211. return false;
  4212. if (!code_segment_valid(vcpu))
  4213. return false;
  4214. if (!stack_segment_valid(vcpu))
  4215. return false;
  4216. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4217. return false;
  4218. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4219. return false;
  4220. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4221. return false;
  4222. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4223. return false;
  4224. if (!tr_valid(vcpu))
  4225. return false;
  4226. if (!ldtr_valid(vcpu))
  4227. return false;
  4228. }
  4229. /* TODO:
  4230. * - Add checks on RIP
  4231. * - Add checks on RFLAGS
  4232. */
  4233. return true;
  4234. }
  4235. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4236. {
  4237. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4238. }
  4239. static int init_rmode_tss(struct kvm *kvm)
  4240. {
  4241. gfn_t fn;
  4242. u16 data = 0;
  4243. int idx, r;
  4244. idx = srcu_read_lock(&kvm->srcu);
  4245. fn = kvm->arch.tss_addr >> PAGE_SHIFT;
  4246. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4247. if (r < 0)
  4248. goto out;
  4249. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4250. r = kvm_write_guest_page(kvm, fn++, &data,
  4251. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4252. if (r < 0)
  4253. goto out;
  4254. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4255. if (r < 0)
  4256. goto out;
  4257. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4258. if (r < 0)
  4259. goto out;
  4260. data = ~0;
  4261. r = kvm_write_guest_page(kvm, fn, &data,
  4262. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4263. sizeof(u8));
  4264. out:
  4265. srcu_read_unlock(&kvm->srcu, idx);
  4266. return r;
  4267. }
  4268. static int init_rmode_identity_map(struct kvm *kvm)
  4269. {
  4270. int i, idx, r = 0;
  4271. kvm_pfn_t identity_map_pfn;
  4272. u32 tmp;
  4273. /* Protect kvm->arch.ept_identity_pagetable_done. */
  4274. mutex_lock(&kvm->slots_lock);
  4275. if (likely(kvm->arch.ept_identity_pagetable_done))
  4276. goto out2;
  4277. if (!kvm->arch.ept_identity_map_addr)
  4278. kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4279. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  4280. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  4281. kvm->arch.ept_identity_map_addr, PAGE_SIZE);
  4282. if (r < 0)
  4283. goto out2;
  4284. idx = srcu_read_lock(&kvm->srcu);
  4285. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  4286. if (r < 0)
  4287. goto out;
  4288. /* Set up identity-mapping pagetable for EPT in real mode */
  4289. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  4290. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  4291. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  4292. r = kvm_write_guest_page(kvm, identity_map_pfn,
  4293. &tmp, i * sizeof(tmp), sizeof(tmp));
  4294. if (r < 0)
  4295. goto out;
  4296. }
  4297. kvm->arch.ept_identity_pagetable_done = true;
  4298. out:
  4299. srcu_read_unlock(&kvm->srcu, idx);
  4300. out2:
  4301. mutex_unlock(&kvm->slots_lock);
  4302. return r;
  4303. }
  4304. static void seg_setup(int seg)
  4305. {
  4306. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4307. unsigned int ar;
  4308. vmcs_write16(sf->selector, 0);
  4309. vmcs_writel(sf->base, 0);
  4310. vmcs_write32(sf->limit, 0xffff);
  4311. ar = 0x93;
  4312. if (seg == VCPU_SREG_CS)
  4313. ar |= 0x08; /* code segment */
  4314. vmcs_write32(sf->ar_bytes, ar);
  4315. }
  4316. static int alloc_apic_access_page(struct kvm *kvm)
  4317. {
  4318. struct page *page;
  4319. int r = 0;
  4320. mutex_lock(&kvm->slots_lock);
  4321. if (kvm->arch.apic_access_page_done)
  4322. goto out;
  4323. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  4324. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  4325. if (r)
  4326. goto out;
  4327. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  4328. if (is_error_page(page)) {
  4329. r = -EFAULT;
  4330. goto out;
  4331. }
  4332. /*
  4333. * Do not pin the page in memory, so that memory hot-unplug
  4334. * is able to migrate it.
  4335. */
  4336. put_page(page);
  4337. kvm->arch.apic_access_page_done = true;
  4338. out:
  4339. mutex_unlock(&kvm->slots_lock);
  4340. return r;
  4341. }
  4342. static int allocate_vpid(void)
  4343. {
  4344. int vpid;
  4345. if (!enable_vpid)
  4346. return 0;
  4347. spin_lock(&vmx_vpid_lock);
  4348. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  4349. if (vpid < VMX_NR_VPIDS)
  4350. __set_bit(vpid, vmx_vpid_bitmap);
  4351. else
  4352. vpid = 0;
  4353. spin_unlock(&vmx_vpid_lock);
  4354. return vpid;
  4355. }
  4356. static void free_vpid(int vpid)
  4357. {
  4358. if (!enable_vpid || vpid == 0)
  4359. return;
  4360. spin_lock(&vmx_vpid_lock);
  4361. __clear_bit(vpid, vmx_vpid_bitmap);
  4362. spin_unlock(&vmx_vpid_lock);
  4363. }
  4364. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  4365. u32 msr, int type)
  4366. {
  4367. int f = sizeof(unsigned long);
  4368. if (!cpu_has_vmx_msr_bitmap())
  4369. return;
  4370. /*
  4371. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4372. * have the write-low and read-high bitmap offsets the wrong way round.
  4373. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4374. */
  4375. if (msr <= 0x1fff) {
  4376. if (type & MSR_TYPE_R)
  4377. /* read-low */
  4378. __clear_bit(msr, msr_bitmap + 0x000 / f);
  4379. if (type & MSR_TYPE_W)
  4380. /* write-low */
  4381. __clear_bit(msr, msr_bitmap + 0x800 / f);
  4382. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4383. msr &= 0x1fff;
  4384. if (type & MSR_TYPE_R)
  4385. /* read-high */
  4386. __clear_bit(msr, msr_bitmap + 0x400 / f);
  4387. if (type & MSR_TYPE_W)
  4388. /* write-high */
  4389. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  4390. }
  4391. }
  4392. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  4393. u32 msr, int type)
  4394. {
  4395. int f = sizeof(unsigned long);
  4396. if (!cpu_has_vmx_msr_bitmap())
  4397. return;
  4398. /*
  4399. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4400. * have the write-low and read-high bitmap offsets the wrong way round.
  4401. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4402. */
  4403. if (msr <= 0x1fff) {
  4404. if (type & MSR_TYPE_R)
  4405. /* read-low */
  4406. __set_bit(msr, msr_bitmap + 0x000 / f);
  4407. if (type & MSR_TYPE_W)
  4408. /* write-low */
  4409. __set_bit(msr, msr_bitmap + 0x800 / f);
  4410. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4411. msr &= 0x1fff;
  4412. if (type & MSR_TYPE_R)
  4413. /* read-high */
  4414. __set_bit(msr, msr_bitmap + 0x400 / f);
  4415. if (type & MSR_TYPE_W)
  4416. /* write-high */
  4417. __set_bit(msr, msr_bitmap + 0xc00 / f);
  4418. }
  4419. }
  4420. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  4421. u32 msr, int type, bool value)
  4422. {
  4423. if (value)
  4424. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  4425. else
  4426. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  4427. }
  4428. /*
  4429. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  4430. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  4431. */
  4432. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  4433. unsigned long *msr_bitmap_nested,
  4434. u32 msr, int type)
  4435. {
  4436. int f = sizeof(unsigned long);
  4437. /*
  4438. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4439. * have the write-low and read-high bitmap offsets the wrong way round.
  4440. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4441. */
  4442. if (msr <= 0x1fff) {
  4443. if (type & MSR_TYPE_R &&
  4444. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  4445. /* read-low */
  4446. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  4447. if (type & MSR_TYPE_W &&
  4448. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  4449. /* write-low */
  4450. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  4451. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4452. msr &= 0x1fff;
  4453. if (type & MSR_TYPE_R &&
  4454. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  4455. /* read-high */
  4456. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  4457. if (type & MSR_TYPE_W &&
  4458. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  4459. /* write-high */
  4460. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  4461. }
  4462. }
  4463. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  4464. {
  4465. u8 mode = 0;
  4466. if (cpu_has_secondary_exec_ctrls() &&
  4467. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  4468. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  4469. mode |= MSR_BITMAP_MODE_X2APIC;
  4470. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  4471. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  4472. }
  4473. if (is_long_mode(vcpu))
  4474. mode |= MSR_BITMAP_MODE_LM;
  4475. return mode;
  4476. }
  4477. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  4478. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  4479. u8 mode)
  4480. {
  4481. int msr;
  4482. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  4483. unsigned word = msr / BITS_PER_LONG;
  4484. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  4485. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  4486. }
  4487. if (mode & MSR_BITMAP_MODE_X2APIC) {
  4488. /*
  4489. * TPR reads and writes can be virtualized even if virtual interrupt
  4490. * delivery is not in use.
  4491. */
  4492. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  4493. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  4494. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  4495. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  4496. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  4497. }
  4498. }
  4499. }
  4500. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  4501. {
  4502. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4503. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  4504. u8 mode = vmx_msr_bitmap_mode(vcpu);
  4505. u8 changed = mode ^ vmx->msr_bitmap_mode;
  4506. if (!changed)
  4507. return;
  4508. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  4509. !(mode & MSR_BITMAP_MODE_LM));
  4510. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  4511. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  4512. vmx->msr_bitmap_mode = mode;
  4513. }
  4514. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  4515. {
  4516. return enable_apicv;
  4517. }
  4518. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  4519. {
  4520. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4521. gfn_t gfn;
  4522. /*
  4523. * Don't need to mark the APIC access page dirty; it is never
  4524. * written to by the CPU during APIC virtualization.
  4525. */
  4526. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  4527. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  4528. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4529. }
  4530. if (nested_cpu_has_posted_intr(vmcs12)) {
  4531. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  4532. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4533. }
  4534. }
  4535. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  4536. {
  4537. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4538. int max_irr;
  4539. void *vapic_page;
  4540. u16 status;
  4541. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  4542. return;
  4543. vmx->nested.pi_pending = false;
  4544. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  4545. return;
  4546. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  4547. if (max_irr != 256) {
  4548. vapic_page = kmap(vmx->nested.virtual_apic_page);
  4549. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  4550. vapic_page, &max_irr);
  4551. kunmap(vmx->nested.virtual_apic_page);
  4552. status = vmcs_read16(GUEST_INTR_STATUS);
  4553. if ((u8)max_irr > ((u8)status & 0xff)) {
  4554. status &= ~0xff;
  4555. status |= (u8)max_irr;
  4556. vmcs_write16(GUEST_INTR_STATUS, status);
  4557. }
  4558. }
  4559. nested_mark_vmcs12_pages_dirty(vcpu);
  4560. }
  4561. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  4562. bool nested)
  4563. {
  4564. #ifdef CONFIG_SMP
  4565. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  4566. if (vcpu->mode == IN_GUEST_MODE) {
  4567. /*
  4568. * The vector of interrupt to be delivered to vcpu had
  4569. * been set in PIR before this function.
  4570. *
  4571. * Following cases will be reached in this block, and
  4572. * we always send a notification event in all cases as
  4573. * explained below.
  4574. *
  4575. * Case 1: vcpu keeps in non-root mode. Sending a
  4576. * notification event posts the interrupt to vcpu.
  4577. *
  4578. * Case 2: vcpu exits to root mode and is still
  4579. * runnable. PIR will be synced to vIRR before the
  4580. * next vcpu entry. Sending a notification event in
  4581. * this case has no effect, as vcpu is not in root
  4582. * mode.
  4583. *
  4584. * Case 3: vcpu exits to root mode and is blocked.
  4585. * vcpu_block() has already synced PIR to vIRR and
  4586. * never blocks vcpu if vIRR is not cleared. Therefore,
  4587. * a blocked vcpu here does not wait for any requested
  4588. * interrupts in PIR, and sending a notification event
  4589. * which has no effect is safe here.
  4590. */
  4591. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  4592. return true;
  4593. }
  4594. #endif
  4595. return false;
  4596. }
  4597. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  4598. int vector)
  4599. {
  4600. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4601. if (is_guest_mode(vcpu) &&
  4602. vector == vmx->nested.posted_intr_nv) {
  4603. /*
  4604. * If a posted intr is not recognized by hardware,
  4605. * we will accomplish it in the next vmentry.
  4606. */
  4607. vmx->nested.pi_pending = true;
  4608. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4609. /* the PIR and ON have been set by L1. */
  4610. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  4611. kvm_vcpu_kick(vcpu);
  4612. return 0;
  4613. }
  4614. return -1;
  4615. }
  4616. /*
  4617. * Send interrupt to vcpu via posted interrupt way.
  4618. * 1. If target vcpu is running(non-root mode), send posted interrupt
  4619. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  4620. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  4621. * interrupt from PIR in next vmentry.
  4622. */
  4623. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  4624. {
  4625. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4626. int r;
  4627. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  4628. if (!r)
  4629. return;
  4630. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  4631. return;
  4632. /* If a previous notification has sent the IPI, nothing to do. */
  4633. if (pi_test_and_set_on(&vmx->pi_desc))
  4634. return;
  4635. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  4636. kvm_vcpu_kick(vcpu);
  4637. }
  4638. /*
  4639. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  4640. * will not change in the lifetime of the guest.
  4641. * Note that host-state that does change is set elsewhere. E.g., host-state
  4642. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  4643. */
  4644. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  4645. {
  4646. u32 low32, high32;
  4647. unsigned long tmpl;
  4648. struct desc_ptr dt;
  4649. unsigned long cr0, cr3, cr4;
  4650. cr0 = read_cr0();
  4651. WARN_ON(cr0 & X86_CR0_TS);
  4652. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  4653. /*
  4654. * Save the most likely value for this task's CR3 in the VMCS.
  4655. * We can't use __get_current_cr3_fast() because we're not atomic.
  4656. */
  4657. cr3 = __read_cr3();
  4658. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  4659. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  4660. /* Save the most likely value for this task's CR4 in the VMCS. */
  4661. cr4 = cr4_read_shadow();
  4662. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  4663. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  4664. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  4665. #ifdef CONFIG_X86_64
  4666. /*
  4667. * Load null selectors, so we can avoid reloading them in
  4668. * __vmx_load_host_state(), in case userspace uses the null selectors
  4669. * too (the expected case).
  4670. */
  4671. vmcs_write16(HOST_DS_SELECTOR, 0);
  4672. vmcs_write16(HOST_ES_SELECTOR, 0);
  4673. #else
  4674. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4675. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4676. #endif
  4677. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4678. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  4679. store_idt(&dt);
  4680. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  4681. vmx->host_idt_base = dt.address;
  4682. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  4683. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  4684. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  4685. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  4686. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  4687. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  4688. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  4689. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  4690. }
  4691. }
  4692. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  4693. {
  4694. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  4695. if (enable_ept)
  4696. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  4697. if (is_guest_mode(&vmx->vcpu))
  4698. vmx->vcpu.arch.cr4_guest_owned_bits &=
  4699. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  4700. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  4701. }
  4702. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  4703. {
  4704. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  4705. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  4706. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  4707. if (!enable_vnmi)
  4708. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  4709. /* Enable the preemption timer dynamically */
  4710. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4711. return pin_based_exec_ctrl;
  4712. }
  4713. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  4714. {
  4715. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4716. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4717. if (cpu_has_secondary_exec_ctrls()) {
  4718. if (kvm_vcpu_apicv_active(vcpu))
  4719. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4720. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4721. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4722. else
  4723. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4724. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4725. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4726. }
  4727. if (cpu_has_vmx_msr_bitmap())
  4728. vmx_update_msr_bitmap(vcpu);
  4729. }
  4730. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  4731. {
  4732. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  4733. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  4734. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  4735. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  4736. exec_control &= ~CPU_BASED_TPR_SHADOW;
  4737. #ifdef CONFIG_X86_64
  4738. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  4739. CPU_BASED_CR8_LOAD_EXITING;
  4740. #endif
  4741. }
  4742. if (!enable_ept)
  4743. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  4744. CPU_BASED_CR3_LOAD_EXITING |
  4745. CPU_BASED_INVLPG_EXITING;
  4746. return exec_control;
  4747. }
  4748. static bool vmx_rdrand_supported(void)
  4749. {
  4750. return vmcs_config.cpu_based_2nd_exec_ctrl &
  4751. SECONDARY_EXEC_RDRAND_EXITING;
  4752. }
  4753. static bool vmx_rdseed_supported(void)
  4754. {
  4755. return vmcs_config.cpu_based_2nd_exec_ctrl &
  4756. SECONDARY_EXEC_RDSEED_EXITING;
  4757. }
  4758. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  4759. {
  4760. struct kvm_vcpu *vcpu = &vmx->vcpu;
  4761. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  4762. if (!cpu_need_virtualize_apic_accesses(vcpu))
  4763. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  4764. if (vmx->vpid == 0)
  4765. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  4766. if (!enable_ept) {
  4767. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  4768. enable_unrestricted_guest = 0;
  4769. /* Enable INVPCID for non-ept guests may cause performance regression. */
  4770. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  4771. }
  4772. if (!enable_unrestricted_guest)
  4773. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  4774. if (!ple_gap)
  4775. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  4776. if (!kvm_vcpu_apicv_active(vcpu))
  4777. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4778. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4779. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  4780. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  4781. * in vmx_set_cr4. */
  4782. exec_control &= ~SECONDARY_EXEC_DESC;
  4783. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  4784. (handle_vmptrld).
  4785. We can NOT enable shadow_vmcs here because we don't have yet
  4786. a current VMCS12
  4787. */
  4788. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  4789. if (!enable_pml)
  4790. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  4791. if (vmx_xsaves_supported()) {
  4792. /* Exposing XSAVES only when XSAVE is exposed */
  4793. bool xsaves_enabled =
  4794. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  4795. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  4796. if (!xsaves_enabled)
  4797. exec_control &= ~SECONDARY_EXEC_XSAVES;
  4798. if (nested) {
  4799. if (xsaves_enabled)
  4800. vmx->nested.nested_vmx_secondary_ctls_high |=
  4801. SECONDARY_EXEC_XSAVES;
  4802. else
  4803. vmx->nested.nested_vmx_secondary_ctls_high &=
  4804. ~SECONDARY_EXEC_XSAVES;
  4805. }
  4806. }
  4807. if (vmx_rdtscp_supported()) {
  4808. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  4809. if (!rdtscp_enabled)
  4810. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  4811. if (nested) {
  4812. if (rdtscp_enabled)
  4813. vmx->nested.nested_vmx_secondary_ctls_high |=
  4814. SECONDARY_EXEC_RDTSCP;
  4815. else
  4816. vmx->nested.nested_vmx_secondary_ctls_high &=
  4817. ~SECONDARY_EXEC_RDTSCP;
  4818. }
  4819. }
  4820. if (vmx_invpcid_supported()) {
  4821. /* Exposing INVPCID only when PCID is exposed */
  4822. bool invpcid_enabled =
  4823. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  4824. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  4825. if (!invpcid_enabled) {
  4826. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  4827. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  4828. }
  4829. if (nested) {
  4830. if (invpcid_enabled)
  4831. vmx->nested.nested_vmx_secondary_ctls_high |=
  4832. SECONDARY_EXEC_ENABLE_INVPCID;
  4833. else
  4834. vmx->nested.nested_vmx_secondary_ctls_high &=
  4835. ~SECONDARY_EXEC_ENABLE_INVPCID;
  4836. }
  4837. }
  4838. if (vmx_rdrand_supported()) {
  4839. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  4840. if (rdrand_enabled)
  4841. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  4842. if (nested) {
  4843. if (rdrand_enabled)
  4844. vmx->nested.nested_vmx_secondary_ctls_high |=
  4845. SECONDARY_EXEC_RDRAND_EXITING;
  4846. else
  4847. vmx->nested.nested_vmx_secondary_ctls_high &=
  4848. ~SECONDARY_EXEC_RDRAND_EXITING;
  4849. }
  4850. }
  4851. if (vmx_rdseed_supported()) {
  4852. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  4853. if (rdseed_enabled)
  4854. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  4855. if (nested) {
  4856. if (rdseed_enabled)
  4857. vmx->nested.nested_vmx_secondary_ctls_high |=
  4858. SECONDARY_EXEC_RDSEED_EXITING;
  4859. else
  4860. vmx->nested.nested_vmx_secondary_ctls_high &=
  4861. ~SECONDARY_EXEC_RDSEED_EXITING;
  4862. }
  4863. }
  4864. vmx->secondary_exec_control = exec_control;
  4865. }
  4866. static void ept_set_mmio_spte_mask(void)
  4867. {
  4868. /*
  4869. * EPT Misconfigurations can be generated if the value of bits 2:0
  4870. * of an EPT paging-structure entry is 110b (write/execute).
  4871. */
  4872. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  4873. VMX_EPT_MISCONFIG_WX_VALUE);
  4874. }
  4875. #define VMX_XSS_EXIT_BITMAP 0
  4876. /*
  4877. * Sets up the vmcs for emulated real mode.
  4878. */
  4879. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  4880. {
  4881. #ifdef CONFIG_X86_64
  4882. unsigned long a;
  4883. #endif
  4884. int i;
  4885. if (enable_shadow_vmcs) {
  4886. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  4887. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  4888. }
  4889. if (cpu_has_vmx_msr_bitmap())
  4890. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  4891. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  4892. /* Control */
  4893. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4894. vmx->hv_deadline_tsc = -1;
  4895. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  4896. if (cpu_has_secondary_exec_ctrls()) {
  4897. vmx_compute_secondary_exec_control(vmx);
  4898. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  4899. vmx->secondary_exec_control);
  4900. }
  4901. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  4902. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  4903. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  4904. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  4905. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  4906. vmcs_write16(GUEST_INTR_STATUS, 0);
  4907. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  4908. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  4909. }
  4910. if (ple_gap) {
  4911. vmcs_write32(PLE_GAP, ple_gap);
  4912. vmx->ple_window = ple_window;
  4913. vmx->ple_window_dirty = true;
  4914. }
  4915. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  4916. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  4917. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  4918. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  4919. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  4920. vmx_set_constant_host_state(vmx);
  4921. #ifdef CONFIG_X86_64
  4922. rdmsrl(MSR_FS_BASE, a);
  4923. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  4924. rdmsrl(MSR_GS_BASE, a);
  4925. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  4926. #else
  4927. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  4928. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  4929. #endif
  4930. if (cpu_has_vmx_vmfunc())
  4931. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  4932. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  4933. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  4934. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  4935. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  4936. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  4937. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  4938. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  4939. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  4940. u32 index = vmx_msr_index[i];
  4941. u32 data_low, data_high;
  4942. int j = vmx->nmsrs;
  4943. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  4944. continue;
  4945. if (wrmsr_safe(index, data_low, data_high) < 0)
  4946. continue;
  4947. vmx->guest_msrs[j].index = i;
  4948. vmx->guest_msrs[j].data = 0;
  4949. vmx->guest_msrs[j].mask = -1ull;
  4950. ++vmx->nmsrs;
  4951. }
  4952. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  4953. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
  4954. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  4955. /* 22.2.1, 20.8.1 */
  4956. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  4957. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  4958. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  4959. set_cr4_guest_host_mask(vmx);
  4960. if (vmx_xsaves_supported())
  4961. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  4962. if (enable_pml) {
  4963. ASSERT(vmx->pml_pg);
  4964. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  4965. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  4966. }
  4967. }
  4968. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  4969. {
  4970. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4971. struct msr_data apic_base_msr;
  4972. u64 cr0;
  4973. vmx->rmode.vm86_active = 0;
  4974. vmx->spec_ctrl = 0;
  4975. vcpu->arch.microcode_version = 0x100000000ULL;
  4976. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  4977. kvm_set_cr8(vcpu, 0);
  4978. if (!init_event) {
  4979. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  4980. MSR_IA32_APICBASE_ENABLE;
  4981. if (kvm_vcpu_is_reset_bsp(vcpu))
  4982. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  4983. apic_base_msr.host_initiated = true;
  4984. kvm_set_apic_base(vcpu, &apic_base_msr);
  4985. }
  4986. vmx_segment_cache_clear(vmx);
  4987. seg_setup(VCPU_SREG_CS);
  4988. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  4989. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  4990. seg_setup(VCPU_SREG_DS);
  4991. seg_setup(VCPU_SREG_ES);
  4992. seg_setup(VCPU_SREG_FS);
  4993. seg_setup(VCPU_SREG_GS);
  4994. seg_setup(VCPU_SREG_SS);
  4995. vmcs_write16(GUEST_TR_SELECTOR, 0);
  4996. vmcs_writel(GUEST_TR_BASE, 0);
  4997. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  4998. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4999. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5000. vmcs_writel(GUEST_LDTR_BASE, 0);
  5001. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5002. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5003. if (!init_event) {
  5004. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5005. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5006. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5007. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5008. }
  5009. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5010. kvm_rip_write(vcpu, 0xfff0);
  5011. vmcs_writel(GUEST_GDTR_BASE, 0);
  5012. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5013. vmcs_writel(GUEST_IDTR_BASE, 0);
  5014. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5015. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5016. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5017. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5018. if (kvm_mpx_supported())
  5019. vmcs_write64(GUEST_BNDCFGS, 0);
  5020. setup_msrs(vmx);
  5021. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5022. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5023. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5024. if (cpu_need_tpr_shadow(vcpu))
  5025. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5026. __pa(vcpu->arch.apic->regs));
  5027. vmcs_write32(TPR_THRESHOLD, 0);
  5028. }
  5029. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5030. if (vmx->vpid != 0)
  5031. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5032. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5033. vmx->vcpu.arch.cr0 = cr0;
  5034. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5035. vmx_set_cr4(vcpu, 0);
  5036. vmx_set_efer(vcpu, 0);
  5037. update_exception_bitmap(vcpu);
  5038. vpid_sync_context(vmx->vpid);
  5039. }
  5040. /*
  5041. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5042. * For most existing hypervisors, this will always return true.
  5043. */
  5044. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5045. {
  5046. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5047. PIN_BASED_EXT_INTR_MASK;
  5048. }
  5049. /*
  5050. * In nested virtualization, check if L1 has set
  5051. * VM_EXIT_ACK_INTR_ON_EXIT
  5052. */
  5053. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5054. {
  5055. return get_vmcs12(vcpu)->vm_exit_controls &
  5056. VM_EXIT_ACK_INTR_ON_EXIT;
  5057. }
  5058. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5059. {
  5060. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5061. PIN_BASED_NMI_EXITING;
  5062. }
  5063. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5064. {
  5065. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5066. CPU_BASED_VIRTUAL_INTR_PENDING);
  5067. }
  5068. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5069. {
  5070. if (!enable_vnmi ||
  5071. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5072. enable_irq_window(vcpu);
  5073. return;
  5074. }
  5075. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5076. CPU_BASED_VIRTUAL_NMI_PENDING);
  5077. }
  5078. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5079. {
  5080. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5081. uint32_t intr;
  5082. int irq = vcpu->arch.interrupt.nr;
  5083. trace_kvm_inj_virq(irq);
  5084. ++vcpu->stat.irq_injections;
  5085. if (vmx->rmode.vm86_active) {
  5086. int inc_eip = 0;
  5087. if (vcpu->arch.interrupt.soft)
  5088. inc_eip = vcpu->arch.event_exit_inst_len;
  5089. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5090. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5091. return;
  5092. }
  5093. intr = irq | INTR_INFO_VALID_MASK;
  5094. if (vcpu->arch.interrupt.soft) {
  5095. intr |= INTR_TYPE_SOFT_INTR;
  5096. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5097. vmx->vcpu.arch.event_exit_inst_len);
  5098. } else
  5099. intr |= INTR_TYPE_EXT_INTR;
  5100. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5101. }
  5102. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5103. {
  5104. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5105. if (!enable_vnmi) {
  5106. /*
  5107. * Tracking the NMI-blocked state in software is built upon
  5108. * finding the next open IRQ window. This, in turn, depends on
  5109. * well-behaving guests: They have to keep IRQs disabled at
  5110. * least as long as the NMI handler runs. Otherwise we may
  5111. * cause NMI nesting, maybe breaking the guest. But as this is
  5112. * highly unlikely, we can live with the residual risk.
  5113. */
  5114. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5115. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5116. }
  5117. ++vcpu->stat.nmi_injections;
  5118. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5119. if (vmx->rmode.vm86_active) {
  5120. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5121. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5122. return;
  5123. }
  5124. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5125. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5126. }
  5127. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5128. {
  5129. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5130. bool masked;
  5131. if (!enable_vnmi)
  5132. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5133. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5134. return false;
  5135. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5136. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5137. return masked;
  5138. }
  5139. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5140. {
  5141. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5142. if (!enable_vnmi) {
  5143. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5144. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5145. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5146. }
  5147. } else {
  5148. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5149. if (masked)
  5150. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5151. GUEST_INTR_STATE_NMI);
  5152. else
  5153. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5154. GUEST_INTR_STATE_NMI);
  5155. }
  5156. }
  5157. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5158. {
  5159. if (to_vmx(vcpu)->nested.nested_run_pending)
  5160. return 0;
  5161. if (!enable_vnmi &&
  5162. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5163. return 0;
  5164. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5165. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5166. | GUEST_INTR_STATE_NMI));
  5167. }
  5168. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5169. {
  5170. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5171. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5172. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5173. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5174. }
  5175. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5176. {
  5177. int ret;
  5178. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5179. PAGE_SIZE * 3);
  5180. if (ret)
  5181. return ret;
  5182. kvm->arch.tss_addr = addr;
  5183. return init_rmode_tss(kvm);
  5184. }
  5185. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5186. {
  5187. switch (vec) {
  5188. case BP_VECTOR:
  5189. /*
  5190. * Update instruction length as we may reinject the exception
  5191. * from user space while in guest debugging mode.
  5192. */
  5193. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5194. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5195. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5196. return false;
  5197. /* fall through */
  5198. case DB_VECTOR:
  5199. if (vcpu->guest_debug &
  5200. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5201. return false;
  5202. /* fall through */
  5203. case DE_VECTOR:
  5204. case OF_VECTOR:
  5205. case BR_VECTOR:
  5206. case UD_VECTOR:
  5207. case DF_VECTOR:
  5208. case SS_VECTOR:
  5209. case GP_VECTOR:
  5210. case MF_VECTOR:
  5211. return true;
  5212. break;
  5213. }
  5214. return false;
  5215. }
  5216. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5217. int vec, u32 err_code)
  5218. {
  5219. /*
  5220. * Instruction with address size override prefix opcode 0x67
  5221. * Cause the #SS fault with 0 error code in VM86 mode.
  5222. */
  5223. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5224. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5225. if (vcpu->arch.halt_request) {
  5226. vcpu->arch.halt_request = 0;
  5227. return kvm_vcpu_halt(vcpu);
  5228. }
  5229. return 1;
  5230. }
  5231. return 0;
  5232. }
  5233. /*
  5234. * Forward all other exceptions that are valid in real mode.
  5235. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5236. * the required debugging infrastructure rework.
  5237. */
  5238. kvm_queue_exception(vcpu, vec);
  5239. return 1;
  5240. }
  5241. /*
  5242. * Trigger machine check on the host. We assume all the MSRs are already set up
  5243. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  5244. * We pass a fake environment to the machine check handler because we want
  5245. * the guest to be always treated like user space, no matter what context
  5246. * it used internally.
  5247. */
  5248. static void kvm_machine_check(void)
  5249. {
  5250. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  5251. struct pt_regs regs = {
  5252. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  5253. .flags = X86_EFLAGS_IF,
  5254. };
  5255. do_machine_check(&regs, 0);
  5256. #endif
  5257. }
  5258. static int handle_machine_check(struct kvm_vcpu *vcpu)
  5259. {
  5260. /* already handled by vcpu_run */
  5261. return 1;
  5262. }
  5263. static int handle_exception(struct kvm_vcpu *vcpu)
  5264. {
  5265. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5266. struct kvm_run *kvm_run = vcpu->run;
  5267. u32 intr_info, ex_no, error_code;
  5268. unsigned long cr2, rip, dr6;
  5269. u32 vect_info;
  5270. enum emulation_result er;
  5271. vect_info = vmx->idt_vectoring_info;
  5272. intr_info = vmx->exit_intr_info;
  5273. if (is_machine_check(intr_info))
  5274. return handle_machine_check(vcpu);
  5275. if (is_nmi(intr_info))
  5276. return 1; /* already handled by vmx_vcpu_run() */
  5277. if (is_invalid_opcode(intr_info)) {
  5278. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  5279. if (er == EMULATE_USER_EXIT)
  5280. return 0;
  5281. if (er != EMULATE_DONE)
  5282. kvm_queue_exception(vcpu, UD_VECTOR);
  5283. return 1;
  5284. }
  5285. error_code = 0;
  5286. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  5287. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  5288. /*
  5289. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  5290. * MMIO, it is better to report an internal error.
  5291. * See the comments in vmx_handle_exit.
  5292. */
  5293. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  5294. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  5295. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5296. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  5297. vcpu->run->internal.ndata = 3;
  5298. vcpu->run->internal.data[0] = vect_info;
  5299. vcpu->run->internal.data[1] = intr_info;
  5300. vcpu->run->internal.data[2] = error_code;
  5301. return 0;
  5302. }
  5303. if (is_page_fault(intr_info)) {
  5304. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  5305. /* EPT won't cause page fault directly */
  5306. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  5307. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  5308. }
  5309. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  5310. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  5311. return handle_rmode_exception(vcpu, ex_no, error_code);
  5312. switch (ex_no) {
  5313. case AC_VECTOR:
  5314. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  5315. return 1;
  5316. case DB_VECTOR:
  5317. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  5318. if (!(vcpu->guest_debug &
  5319. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  5320. vcpu->arch.dr6 &= ~15;
  5321. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  5322. if (!(dr6 & ~DR6_RESERVED)) /* icebp */
  5323. skip_emulated_instruction(vcpu);
  5324. kvm_queue_exception(vcpu, DB_VECTOR);
  5325. return 1;
  5326. }
  5327. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  5328. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  5329. /* fall through */
  5330. case BP_VECTOR:
  5331. /*
  5332. * Update instruction length as we may reinject #BP from
  5333. * user space while in guest debugging mode. Reading it for
  5334. * #DB as well causes no harm, it is not used in that case.
  5335. */
  5336. vmx->vcpu.arch.event_exit_inst_len =
  5337. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5338. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  5339. rip = kvm_rip_read(vcpu);
  5340. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  5341. kvm_run->debug.arch.exception = ex_no;
  5342. break;
  5343. default:
  5344. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  5345. kvm_run->ex.exception = ex_no;
  5346. kvm_run->ex.error_code = error_code;
  5347. break;
  5348. }
  5349. return 0;
  5350. }
  5351. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  5352. {
  5353. ++vcpu->stat.irq_exits;
  5354. return 1;
  5355. }
  5356. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  5357. {
  5358. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5359. vcpu->mmio_needed = 0;
  5360. return 0;
  5361. }
  5362. static int handle_io(struct kvm_vcpu *vcpu)
  5363. {
  5364. unsigned long exit_qualification;
  5365. int size, in, string, ret;
  5366. unsigned port;
  5367. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5368. string = (exit_qualification & 16) != 0;
  5369. in = (exit_qualification & 8) != 0;
  5370. ++vcpu->stat.io_exits;
  5371. if (string || in)
  5372. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5373. port = exit_qualification >> 16;
  5374. size = (exit_qualification & 7) + 1;
  5375. ret = kvm_skip_emulated_instruction(vcpu);
  5376. /*
  5377. * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
  5378. * KVM_EXIT_DEBUG here.
  5379. */
  5380. return kvm_fast_pio_out(vcpu, size, port) && ret;
  5381. }
  5382. static void
  5383. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  5384. {
  5385. /*
  5386. * Patch in the VMCALL instruction:
  5387. */
  5388. hypercall[0] = 0x0f;
  5389. hypercall[1] = 0x01;
  5390. hypercall[2] = 0xc1;
  5391. }
  5392. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  5393. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  5394. {
  5395. if (is_guest_mode(vcpu)) {
  5396. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5397. unsigned long orig_val = val;
  5398. /*
  5399. * We get here when L2 changed cr0 in a way that did not change
  5400. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  5401. * but did change L0 shadowed bits. So we first calculate the
  5402. * effective cr0 value that L1 would like to write into the
  5403. * hardware. It consists of the L2-owned bits from the new
  5404. * value combined with the L1-owned bits from L1's guest_cr0.
  5405. */
  5406. val = (val & ~vmcs12->cr0_guest_host_mask) |
  5407. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  5408. if (!nested_guest_cr0_valid(vcpu, val))
  5409. return 1;
  5410. if (kvm_set_cr0(vcpu, val))
  5411. return 1;
  5412. vmcs_writel(CR0_READ_SHADOW, orig_val);
  5413. return 0;
  5414. } else {
  5415. if (to_vmx(vcpu)->nested.vmxon &&
  5416. !nested_host_cr0_valid(vcpu, val))
  5417. return 1;
  5418. return kvm_set_cr0(vcpu, val);
  5419. }
  5420. }
  5421. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  5422. {
  5423. if (is_guest_mode(vcpu)) {
  5424. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5425. unsigned long orig_val = val;
  5426. /* analogously to handle_set_cr0 */
  5427. val = (val & ~vmcs12->cr4_guest_host_mask) |
  5428. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  5429. if (kvm_set_cr4(vcpu, val))
  5430. return 1;
  5431. vmcs_writel(CR4_READ_SHADOW, orig_val);
  5432. return 0;
  5433. } else
  5434. return kvm_set_cr4(vcpu, val);
  5435. }
  5436. static int handle_desc(struct kvm_vcpu *vcpu)
  5437. {
  5438. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  5439. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5440. }
  5441. static int handle_cr(struct kvm_vcpu *vcpu)
  5442. {
  5443. unsigned long exit_qualification, val;
  5444. int cr;
  5445. int reg;
  5446. int err;
  5447. int ret;
  5448. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5449. cr = exit_qualification & 15;
  5450. reg = (exit_qualification >> 8) & 15;
  5451. switch ((exit_qualification >> 4) & 3) {
  5452. case 0: /* mov to cr */
  5453. val = kvm_register_readl(vcpu, reg);
  5454. trace_kvm_cr_write(cr, val);
  5455. switch (cr) {
  5456. case 0:
  5457. err = handle_set_cr0(vcpu, val);
  5458. return kvm_complete_insn_gp(vcpu, err);
  5459. case 3:
  5460. err = kvm_set_cr3(vcpu, val);
  5461. return kvm_complete_insn_gp(vcpu, err);
  5462. case 4:
  5463. err = handle_set_cr4(vcpu, val);
  5464. return kvm_complete_insn_gp(vcpu, err);
  5465. case 8: {
  5466. u8 cr8_prev = kvm_get_cr8(vcpu);
  5467. u8 cr8 = (u8)val;
  5468. err = kvm_set_cr8(vcpu, cr8);
  5469. ret = kvm_complete_insn_gp(vcpu, err);
  5470. if (lapic_in_kernel(vcpu))
  5471. return ret;
  5472. if (cr8_prev <= cr8)
  5473. return ret;
  5474. /*
  5475. * TODO: we might be squashing a
  5476. * KVM_GUESTDBG_SINGLESTEP-triggered
  5477. * KVM_EXIT_DEBUG here.
  5478. */
  5479. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  5480. return 0;
  5481. }
  5482. }
  5483. break;
  5484. case 2: /* clts */
  5485. WARN_ONCE(1, "Guest should always own CR0.TS");
  5486. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  5487. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  5488. return kvm_skip_emulated_instruction(vcpu);
  5489. case 1: /*mov from cr*/
  5490. switch (cr) {
  5491. case 3:
  5492. val = kvm_read_cr3(vcpu);
  5493. kvm_register_write(vcpu, reg, val);
  5494. trace_kvm_cr_read(cr, val);
  5495. return kvm_skip_emulated_instruction(vcpu);
  5496. case 8:
  5497. val = kvm_get_cr8(vcpu);
  5498. kvm_register_write(vcpu, reg, val);
  5499. trace_kvm_cr_read(cr, val);
  5500. return kvm_skip_emulated_instruction(vcpu);
  5501. }
  5502. break;
  5503. case 3: /* lmsw */
  5504. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  5505. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  5506. kvm_lmsw(vcpu, val);
  5507. return kvm_skip_emulated_instruction(vcpu);
  5508. default:
  5509. break;
  5510. }
  5511. vcpu->run->exit_reason = 0;
  5512. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  5513. (int)(exit_qualification >> 4) & 3, cr);
  5514. return 0;
  5515. }
  5516. static int handle_dr(struct kvm_vcpu *vcpu)
  5517. {
  5518. unsigned long exit_qualification;
  5519. int dr, dr7, reg;
  5520. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5521. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  5522. /* First, if DR does not exist, trigger UD */
  5523. if (!kvm_require_dr(vcpu, dr))
  5524. return 1;
  5525. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  5526. if (!kvm_require_cpl(vcpu, 0))
  5527. return 1;
  5528. dr7 = vmcs_readl(GUEST_DR7);
  5529. if (dr7 & DR7_GD) {
  5530. /*
  5531. * As the vm-exit takes precedence over the debug trap, we
  5532. * need to emulate the latter, either for the host or the
  5533. * guest debugging itself.
  5534. */
  5535. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  5536. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  5537. vcpu->run->debug.arch.dr7 = dr7;
  5538. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  5539. vcpu->run->debug.arch.exception = DB_VECTOR;
  5540. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  5541. return 0;
  5542. } else {
  5543. vcpu->arch.dr6 &= ~15;
  5544. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  5545. kvm_queue_exception(vcpu, DB_VECTOR);
  5546. return 1;
  5547. }
  5548. }
  5549. if (vcpu->guest_debug == 0) {
  5550. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5551. CPU_BASED_MOV_DR_EXITING);
  5552. /*
  5553. * No more DR vmexits; force a reload of the debug registers
  5554. * and reenter on this instruction. The next vmexit will
  5555. * retrieve the full state of the debug registers.
  5556. */
  5557. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  5558. return 1;
  5559. }
  5560. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  5561. if (exit_qualification & TYPE_MOV_FROM_DR) {
  5562. unsigned long val;
  5563. if (kvm_get_dr(vcpu, dr, &val))
  5564. return 1;
  5565. kvm_register_write(vcpu, reg, val);
  5566. } else
  5567. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  5568. return 1;
  5569. return kvm_skip_emulated_instruction(vcpu);
  5570. }
  5571. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  5572. {
  5573. return vcpu->arch.dr6;
  5574. }
  5575. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  5576. {
  5577. }
  5578. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  5579. {
  5580. get_debugreg(vcpu->arch.db[0], 0);
  5581. get_debugreg(vcpu->arch.db[1], 1);
  5582. get_debugreg(vcpu->arch.db[2], 2);
  5583. get_debugreg(vcpu->arch.db[3], 3);
  5584. get_debugreg(vcpu->arch.dr6, 6);
  5585. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  5586. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  5587. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  5588. }
  5589. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  5590. {
  5591. vmcs_writel(GUEST_DR7, val);
  5592. }
  5593. static int handle_cpuid(struct kvm_vcpu *vcpu)
  5594. {
  5595. return kvm_emulate_cpuid(vcpu);
  5596. }
  5597. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  5598. {
  5599. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  5600. struct msr_data msr_info;
  5601. msr_info.index = ecx;
  5602. msr_info.host_initiated = false;
  5603. if (vmx_get_msr(vcpu, &msr_info)) {
  5604. trace_kvm_msr_read_ex(ecx);
  5605. kvm_inject_gp(vcpu, 0);
  5606. return 1;
  5607. }
  5608. trace_kvm_msr_read(ecx, msr_info.data);
  5609. /* FIXME: handling of bits 32:63 of rax, rdx */
  5610. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  5611. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  5612. return kvm_skip_emulated_instruction(vcpu);
  5613. }
  5614. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  5615. {
  5616. struct msr_data msr;
  5617. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  5618. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  5619. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  5620. msr.data = data;
  5621. msr.index = ecx;
  5622. msr.host_initiated = false;
  5623. if (kvm_set_msr(vcpu, &msr) != 0) {
  5624. trace_kvm_msr_write_ex(ecx, data);
  5625. kvm_inject_gp(vcpu, 0);
  5626. return 1;
  5627. }
  5628. trace_kvm_msr_write(ecx, data);
  5629. return kvm_skip_emulated_instruction(vcpu);
  5630. }
  5631. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  5632. {
  5633. kvm_apic_update_ppr(vcpu);
  5634. return 1;
  5635. }
  5636. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  5637. {
  5638. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5639. CPU_BASED_VIRTUAL_INTR_PENDING);
  5640. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5641. ++vcpu->stat.irq_window_exits;
  5642. return 1;
  5643. }
  5644. static int handle_halt(struct kvm_vcpu *vcpu)
  5645. {
  5646. return kvm_emulate_halt(vcpu);
  5647. }
  5648. static int handle_vmcall(struct kvm_vcpu *vcpu)
  5649. {
  5650. return kvm_emulate_hypercall(vcpu);
  5651. }
  5652. static int handle_invd(struct kvm_vcpu *vcpu)
  5653. {
  5654. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5655. }
  5656. static int handle_invlpg(struct kvm_vcpu *vcpu)
  5657. {
  5658. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5659. kvm_mmu_invlpg(vcpu, exit_qualification);
  5660. return kvm_skip_emulated_instruction(vcpu);
  5661. }
  5662. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  5663. {
  5664. int err;
  5665. err = kvm_rdpmc(vcpu);
  5666. return kvm_complete_insn_gp(vcpu, err);
  5667. }
  5668. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  5669. {
  5670. return kvm_emulate_wbinvd(vcpu);
  5671. }
  5672. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  5673. {
  5674. u64 new_bv = kvm_read_edx_eax(vcpu);
  5675. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  5676. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  5677. return kvm_skip_emulated_instruction(vcpu);
  5678. return 1;
  5679. }
  5680. static int handle_xsaves(struct kvm_vcpu *vcpu)
  5681. {
  5682. kvm_skip_emulated_instruction(vcpu);
  5683. WARN(1, "this should never happen\n");
  5684. return 1;
  5685. }
  5686. static int handle_xrstors(struct kvm_vcpu *vcpu)
  5687. {
  5688. kvm_skip_emulated_instruction(vcpu);
  5689. WARN(1, "this should never happen\n");
  5690. return 1;
  5691. }
  5692. static int handle_apic_access(struct kvm_vcpu *vcpu)
  5693. {
  5694. if (likely(fasteoi)) {
  5695. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5696. int access_type, offset;
  5697. access_type = exit_qualification & APIC_ACCESS_TYPE;
  5698. offset = exit_qualification & APIC_ACCESS_OFFSET;
  5699. /*
  5700. * Sane guest uses MOV to write EOI, with written value
  5701. * not cared. So make a short-circuit here by avoiding
  5702. * heavy instruction emulation.
  5703. */
  5704. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  5705. (offset == APIC_EOI)) {
  5706. kvm_lapic_set_eoi(vcpu);
  5707. return kvm_skip_emulated_instruction(vcpu);
  5708. }
  5709. }
  5710. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5711. }
  5712. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  5713. {
  5714. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5715. int vector = exit_qualification & 0xff;
  5716. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  5717. kvm_apic_set_eoi_accelerated(vcpu, vector);
  5718. return 1;
  5719. }
  5720. static int handle_apic_write(struct kvm_vcpu *vcpu)
  5721. {
  5722. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5723. u32 offset = exit_qualification & 0xfff;
  5724. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  5725. kvm_apic_write_nodecode(vcpu, offset);
  5726. return 1;
  5727. }
  5728. static int handle_task_switch(struct kvm_vcpu *vcpu)
  5729. {
  5730. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5731. unsigned long exit_qualification;
  5732. bool has_error_code = false;
  5733. u32 error_code = 0;
  5734. u16 tss_selector;
  5735. int reason, type, idt_v, idt_index;
  5736. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  5737. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  5738. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  5739. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5740. reason = (u32)exit_qualification >> 30;
  5741. if (reason == TASK_SWITCH_GATE && idt_v) {
  5742. switch (type) {
  5743. case INTR_TYPE_NMI_INTR:
  5744. vcpu->arch.nmi_injected = false;
  5745. vmx_set_nmi_mask(vcpu, true);
  5746. break;
  5747. case INTR_TYPE_EXT_INTR:
  5748. case INTR_TYPE_SOFT_INTR:
  5749. kvm_clear_interrupt_queue(vcpu);
  5750. break;
  5751. case INTR_TYPE_HARD_EXCEPTION:
  5752. if (vmx->idt_vectoring_info &
  5753. VECTORING_INFO_DELIVER_CODE_MASK) {
  5754. has_error_code = true;
  5755. error_code =
  5756. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  5757. }
  5758. /* fall through */
  5759. case INTR_TYPE_SOFT_EXCEPTION:
  5760. kvm_clear_exception_queue(vcpu);
  5761. break;
  5762. default:
  5763. break;
  5764. }
  5765. }
  5766. tss_selector = exit_qualification;
  5767. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  5768. type != INTR_TYPE_EXT_INTR &&
  5769. type != INTR_TYPE_NMI_INTR))
  5770. skip_emulated_instruction(vcpu);
  5771. if (kvm_task_switch(vcpu, tss_selector,
  5772. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  5773. has_error_code, error_code) == EMULATE_FAIL) {
  5774. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5775. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5776. vcpu->run->internal.ndata = 0;
  5777. return 0;
  5778. }
  5779. /*
  5780. * TODO: What about debug traps on tss switch?
  5781. * Are we supposed to inject them and update dr6?
  5782. */
  5783. return 1;
  5784. }
  5785. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  5786. {
  5787. unsigned long exit_qualification;
  5788. gpa_t gpa;
  5789. u64 error_code;
  5790. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5791. /*
  5792. * EPT violation happened while executing iret from NMI,
  5793. * "blocked by NMI" bit has to be set before next VM entry.
  5794. * There are errata that may cause this bit to not be set:
  5795. * AAK134, BY25.
  5796. */
  5797. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  5798. enable_vnmi &&
  5799. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  5800. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  5801. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  5802. trace_kvm_page_fault(gpa, exit_qualification);
  5803. /* Is it a read fault? */
  5804. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  5805. ? PFERR_USER_MASK : 0;
  5806. /* Is it a write fault? */
  5807. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  5808. ? PFERR_WRITE_MASK : 0;
  5809. /* Is it a fetch fault? */
  5810. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  5811. ? PFERR_FETCH_MASK : 0;
  5812. /* ept page table entry is present? */
  5813. error_code |= (exit_qualification &
  5814. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  5815. EPT_VIOLATION_EXECUTABLE))
  5816. ? PFERR_PRESENT_MASK : 0;
  5817. error_code |= (exit_qualification & 0x100) != 0 ?
  5818. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  5819. vcpu->arch.exit_qualification = exit_qualification;
  5820. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  5821. }
  5822. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  5823. {
  5824. int ret;
  5825. gpa_t gpa;
  5826. /*
  5827. * A nested guest cannot optimize MMIO vmexits, because we have an
  5828. * nGPA here instead of the required GPA.
  5829. */
  5830. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  5831. if (!is_guest_mode(vcpu) &&
  5832. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  5833. trace_kvm_fast_mmio(gpa);
  5834. /*
  5835. * Doing kvm_skip_emulated_instruction() depends on undefined
  5836. * behavior: Intel's manual doesn't mandate
  5837. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  5838. * occurs and while on real hardware it was observed to be set,
  5839. * other hypervisors (namely Hyper-V) don't set it, we end up
  5840. * advancing IP with some random value. Disable fast mmio when
  5841. * running nested and keep it for real hardware in hope that
  5842. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  5843. */
  5844. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  5845. return kvm_skip_emulated_instruction(vcpu);
  5846. else
  5847. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  5848. NULL, 0) == EMULATE_DONE;
  5849. }
  5850. ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  5851. if (ret >= 0)
  5852. return ret;
  5853. /* It is the real ept misconfig */
  5854. WARN_ON(1);
  5855. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5856. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  5857. return 0;
  5858. }
  5859. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  5860. {
  5861. WARN_ON_ONCE(!enable_vnmi);
  5862. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5863. CPU_BASED_VIRTUAL_NMI_PENDING);
  5864. ++vcpu->stat.nmi_window_exits;
  5865. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5866. return 1;
  5867. }
  5868. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  5869. {
  5870. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5871. enum emulation_result err = EMULATE_DONE;
  5872. int ret = 1;
  5873. u32 cpu_exec_ctrl;
  5874. bool intr_window_requested;
  5875. unsigned count = 130;
  5876. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5877. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  5878. while (vmx->emulation_required && count-- != 0) {
  5879. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  5880. return handle_interrupt_window(&vmx->vcpu);
  5881. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  5882. return 1;
  5883. err = emulate_instruction(vcpu, 0);
  5884. if (err == EMULATE_USER_EXIT) {
  5885. ++vcpu->stat.mmio_exits;
  5886. ret = 0;
  5887. goto out;
  5888. }
  5889. if (err != EMULATE_DONE) {
  5890. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5891. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5892. vcpu->run->internal.ndata = 0;
  5893. return 0;
  5894. }
  5895. if (vcpu->arch.halt_request) {
  5896. vcpu->arch.halt_request = 0;
  5897. ret = kvm_vcpu_halt(vcpu);
  5898. goto out;
  5899. }
  5900. if (signal_pending(current))
  5901. goto out;
  5902. if (need_resched())
  5903. schedule();
  5904. }
  5905. out:
  5906. return ret;
  5907. }
  5908. static int __grow_ple_window(int val)
  5909. {
  5910. if (ple_window_grow < 1)
  5911. return ple_window;
  5912. val = min(val, ple_window_actual_max);
  5913. if (ple_window_grow < ple_window)
  5914. val *= ple_window_grow;
  5915. else
  5916. val += ple_window_grow;
  5917. return val;
  5918. }
  5919. static int __shrink_ple_window(int val, int modifier, int minimum)
  5920. {
  5921. if (modifier < 1)
  5922. return ple_window;
  5923. if (modifier < ple_window)
  5924. val /= modifier;
  5925. else
  5926. val -= modifier;
  5927. return max(val, minimum);
  5928. }
  5929. static void grow_ple_window(struct kvm_vcpu *vcpu)
  5930. {
  5931. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5932. int old = vmx->ple_window;
  5933. vmx->ple_window = __grow_ple_window(old);
  5934. if (vmx->ple_window != old)
  5935. vmx->ple_window_dirty = true;
  5936. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  5937. }
  5938. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  5939. {
  5940. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5941. int old = vmx->ple_window;
  5942. vmx->ple_window = __shrink_ple_window(old,
  5943. ple_window_shrink, ple_window);
  5944. if (vmx->ple_window != old)
  5945. vmx->ple_window_dirty = true;
  5946. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  5947. }
  5948. /*
  5949. * ple_window_actual_max is computed to be one grow_ple_window() below
  5950. * ple_window_max. (See __grow_ple_window for the reason.)
  5951. * This prevents overflows, because ple_window_max is int.
  5952. * ple_window_max effectively rounded down to a multiple of ple_window_grow in
  5953. * this process.
  5954. * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
  5955. */
  5956. static void update_ple_window_actual_max(void)
  5957. {
  5958. ple_window_actual_max =
  5959. __shrink_ple_window(max(ple_window_max, ple_window),
  5960. ple_window_grow, INT_MIN);
  5961. }
  5962. /*
  5963. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  5964. */
  5965. static void wakeup_handler(void)
  5966. {
  5967. struct kvm_vcpu *vcpu;
  5968. int cpu = smp_processor_id();
  5969. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  5970. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  5971. blocked_vcpu_list) {
  5972. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  5973. if (pi_test_on(pi_desc) == 1)
  5974. kvm_vcpu_kick(vcpu);
  5975. }
  5976. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  5977. }
  5978. void vmx_enable_tdp(void)
  5979. {
  5980. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  5981. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  5982. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  5983. 0ull, VMX_EPT_EXECUTABLE_MASK,
  5984. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  5985. VMX_EPT_RWX_MASK, 0ull);
  5986. ept_set_mmio_spte_mask();
  5987. kvm_enable_tdp();
  5988. }
  5989. static __init int hardware_setup(void)
  5990. {
  5991. int r = -ENOMEM, i;
  5992. rdmsrl_safe(MSR_EFER, &host_efer);
  5993. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  5994. kvm_define_shared_msr(i, vmx_msr_index[i]);
  5995. for (i = 0; i < VMX_BITMAP_NR; i++) {
  5996. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  5997. if (!vmx_bitmap[i])
  5998. goto out;
  5999. }
  6000. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6001. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6002. if (setup_vmcs_config(&vmcs_config) < 0) {
  6003. r = -EIO;
  6004. goto out;
  6005. }
  6006. if (boot_cpu_has(X86_FEATURE_NX))
  6007. kvm_enable_efer_bits(EFER_NX);
  6008. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6009. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6010. enable_vpid = 0;
  6011. if (!cpu_has_vmx_ept() ||
  6012. !cpu_has_vmx_ept_4levels() ||
  6013. !cpu_has_vmx_ept_mt_wb() ||
  6014. !cpu_has_vmx_invept_global())
  6015. enable_ept = 0;
  6016. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6017. enable_ept_ad_bits = 0;
  6018. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6019. enable_unrestricted_guest = 0;
  6020. if (!cpu_has_vmx_flexpriority())
  6021. flexpriority_enabled = 0;
  6022. if (!cpu_has_virtual_nmis())
  6023. enable_vnmi = 0;
  6024. /*
  6025. * set_apic_access_page_addr() is used to reload apic access
  6026. * page upon invalidation. No need to do anything if not
  6027. * using the APIC_ACCESS_ADDR VMCS field.
  6028. */
  6029. if (!flexpriority_enabled)
  6030. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6031. if (!cpu_has_vmx_tpr_shadow())
  6032. kvm_x86_ops->update_cr8_intercept = NULL;
  6033. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6034. kvm_disable_largepages();
  6035. if (!cpu_has_vmx_ple()) {
  6036. ple_gap = 0;
  6037. ple_window = 0;
  6038. ple_window_grow = 0;
  6039. ple_window_max = 0;
  6040. ple_window_shrink = 0;
  6041. }
  6042. if (!cpu_has_vmx_apicv()) {
  6043. enable_apicv = 0;
  6044. kvm_x86_ops->sync_pir_to_irr = NULL;
  6045. }
  6046. if (cpu_has_vmx_tsc_scaling()) {
  6047. kvm_has_tsc_control = true;
  6048. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6049. kvm_tsc_scaling_ratio_frac_bits = 48;
  6050. }
  6051. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6052. if (enable_ept)
  6053. vmx_enable_tdp();
  6054. else
  6055. kvm_disable_tdp();
  6056. update_ple_window_actual_max();
  6057. /*
  6058. * Only enable PML when hardware supports PML feature, and both EPT
  6059. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6060. */
  6061. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6062. enable_pml = 0;
  6063. if (!enable_pml) {
  6064. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6065. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6066. kvm_x86_ops->flush_log_dirty = NULL;
  6067. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6068. }
  6069. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6070. u64 vmx_msr;
  6071. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6072. cpu_preemption_timer_multi =
  6073. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6074. } else {
  6075. kvm_x86_ops->set_hv_timer = NULL;
  6076. kvm_x86_ops->cancel_hv_timer = NULL;
  6077. }
  6078. if (!cpu_has_vmx_shadow_vmcs())
  6079. enable_shadow_vmcs = 0;
  6080. if (enable_shadow_vmcs)
  6081. init_vmcs_shadow_fields();
  6082. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6083. kvm_mce_cap_supported |= MCG_LMCE_P;
  6084. return alloc_kvm_area();
  6085. out:
  6086. for (i = 0; i < VMX_BITMAP_NR; i++)
  6087. free_page((unsigned long)vmx_bitmap[i]);
  6088. return r;
  6089. }
  6090. static __exit void hardware_unsetup(void)
  6091. {
  6092. int i;
  6093. for (i = 0; i < VMX_BITMAP_NR; i++)
  6094. free_page((unsigned long)vmx_bitmap[i]);
  6095. free_kvm_area();
  6096. }
  6097. /*
  6098. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6099. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6100. */
  6101. static int handle_pause(struct kvm_vcpu *vcpu)
  6102. {
  6103. if (ple_gap)
  6104. grow_ple_window(vcpu);
  6105. /*
  6106. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6107. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6108. * never set PAUSE_EXITING and just set PLE if supported,
  6109. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6110. */
  6111. kvm_vcpu_on_spin(vcpu, true);
  6112. return kvm_skip_emulated_instruction(vcpu);
  6113. }
  6114. static int handle_nop(struct kvm_vcpu *vcpu)
  6115. {
  6116. return kvm_skip_emulated_instruction(vcpu);
  6117. }
  6118. static int handle_mwait(struct kvm_vcpu *vcpu)
  6119. {
  6120. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6121. return handle_nop(vcpu);
  6122. }
  6123. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6124. {
  6125. kvm_queue_exception(vcpu, UD_VECTOR);
  6126. return 1;
  6127. }
  6128. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6129. {
  6130. return 1;
  6131. }
  6132. static int handle_monitor(struct kvm_vcpu *vcpu)
  6133. {
  6134. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6135. return handle_nop(vcpu);
  6136. }
  6137. /*
  6138. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6139. * set the success or error code of an emulated VMX instruction, as specified
  6140. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6141. */
  6142. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6143. {
  6144. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6145. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6146. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6147. }
  6148. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6149. {
  6150. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6151. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6152. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6153. | X86_EFLAGS_CF);
  6154. }
  6155. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6156. u32 vm_instruction_error)
  6157. {
  6158. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6159. /*
  6160. * failValid writes the error number to the current VMCS, which
  6161. * can't be done there isn't a current VMCS.
  6162. */
  6163. nested_vmx_failInvalid(vcpu);
  6164. return;
  6165. }
  6166. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6167. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6168. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6169. | X86_EFLAGS_ZF);
  6170. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6171. /*
  6172. * We don't need to force a shadow sync because
  6173. * VM_INSTRUCTION_ERROR is not shadowed
  6174. */
  6175. }
  6176. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6177. {
  6178. /* TODO: not to reset guest simply here. */
  6179. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6180. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6181. }
  6182. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6183. {
  6184. struct vcpu_vmx *vmx =
  6185. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6186. vmx->nested.preemption_timer_expired = true;
  6187. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6188. kvm_vcpu_kick(&vmx->vcpu);
  6189. return HRTIMER_NORESTART;
  6190. }
  6191. /*
  6192. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6193. * exit caused by such an instruction (run by a guest hypervisor).
  6194. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6195. * #UD or #GP.
  6196. */
  6197. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6198. unsigned long exit_qualification,
  6199. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6200. {
  6201. gva_t off;
  6202. bool exn;
  6203. struct kvm_segment s;
  6204. /*
  6205. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6206. * Execution", on an exit, vmx_instruction_info holds most of the
  6207. * addressing components of the operand. Only the displacement part
  6208. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6209. * For how an actual address is calculated from all these components,
  6210. * refer to Vol. 1, "Operand Addressing".
  6211. */
  6212. int scaling = vmx_instruction_info & 3;
  6213. int addr_size = (vmx_instruction_info >> 7) & 7;
  6214. bool is_reg = vmx_instruction_info & (1u << 10);
  6215. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6216. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6217. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6218. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6219. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6220. if (is_reg) {
  6221. kvm_queue_exception(vcpu, UD_VECTOR);
  6222. return 1;
  6223. }
  6224. /* Addr = segment_base + offset */
  6225. /* offset = base + [index * scale] + displacement */
  6226. off = exit_qualification; /* holds the displacement */
  6227. if (base_is_valid)
  6228. off += kvm_register_read(vcpu, base_reg);
  6229. if (index_is_valid)
  6230. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6231. vmx_get_segment(vcpu, &s, seg_reg);
  6232. *ret = s.base + off;
  6233. if (addr_size == 1) /* 32 bit */
  6234. *ret &= 0xffffffff;
  6235. /* Checks for #GP/#SS exceptions. */
  6236. exn = false;
  6237. if (is_long_mode(vcpu)) {
  6238. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6239. * non-canonical form. This is the only check on the memory
  6240. * destination for long mode!
  6241. */
  6242. exn = is_noncanonical_address(*ret, vcpu);
  6243. } else if (is_protmode(vcpu)) {
  6244. /* Protected mode: apply checks for segment validity in the
  6245. * following order:
  6246. * - segment type check (#GP(0) may be thrown)
  6247. * - usability check (#GP(0)/#SS(0))
  6248. * - limit check (#GP(0)/#SS(0))
  6249. */
  6250. if (wr)
  6251. /* #GP(0) if the destination operand is located in a
  6252. * read-only data segment or any code segment.
  6253. */
  6254. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  6255. else
  6256. /* #GP(0) if the source operand is located in an
  6257. * execute-only code segment
  6258. */
  6259. exn = ((s.type & 0xa) == 8);
  6260. if (exn) {
  6261. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  6262. return 1;
  6263. }
  6264. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  6265. */
  6266. exn = (s.unusable != 0);
  6267. /* Protected mode: #GP(0)/#SS(0) if the memory
  6268. * operand is outside the segment limit.
  6269. */
  6270. exn = exn || (off + sizeof(u64) > s.limit);
  6271. }
  6272. if (exn) {
  6273. kvm_queue_exception_e(vcpu,
  6274. seg_reg == VCPU_SREG_SS ?
  6275. SS_VECTOR : GP_VECTOR,
  6276. 0);
  6277. return 1;
  6278. }
  6279. return 0;
  6280. }
  6281. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  6282. {
  6283. gva_t gva;
  6284. struct x86_exception e;
  6285. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6286. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  6287. return 1;
  6288. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
  6289. sizeof(*vmpointer), &e)) {
  6290. kvm_inject_page_fault(vcpu, &e);
  6291. return 1;
  6292. }
  6293. return 0;
  6294. }
  6295. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  6296. {
  6297. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6298. struct vmcs *shadow_vmcs;
  6299. int r;
  6300. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  6301. if (r < 0)
  6302. goto out_vmcs02;
  6303. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6304. if (!vmx->nested.cached_vmcs12)
  6305. goto out_cached_vmcs12;
  6306. if (enable_shadow_vmcs) {
  6307. shadow_vmcs = alloc_vmcs();
  6308. if (!shadow_vmcs)
  6309. goto out_shadow_vmcs;
  6310. /* mark vmcs as shadow */
  6311. shadow_vmcs->revision_id |= (1u << 31);
  6312. /* init shadow vmcs */
  6313. vmcs_clear(shadow_vmcs);
  6314. vmx->vmcs01.shadow_vmcs = shadow_vmcs;
  6315. }
  6316. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  6317. HRTIMER_MODE_REL_PINNED);
  6318. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  6319. vmx->nested.vmxon = true;
  6320. return 0;
  6321. out_shadow_vmcs:
  6322. kfree(vmx->nested.cached_vmcs12);
  6323. out_cached_vmcs12:
  6324. free_loaded_vmcs(&vmx->nested.vmcs02);
  6325. out_vmcs02:
  6326. return -ENOMEM;
  6327. }
  6328. /*
  6329. * Emulate the VMXON instruction.
  6330. * Currently, we just remember that VMX is active, and do not save or even
  6331. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  6332. * do not currently need to store anything in that guest-allocated memory
  6333. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  6334. * argument is different from the VMXON pointer (which the spec says they do).
  6335. */
  6336. static int handle_vmon(struct kvm_vcpu *vcpu)
  6337. {
  6338. int ret;
  6339. gpa_t vmptr;
  6340. struct page *page;
  6341. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6342. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  6343. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  6344. /*
  6345. * The Intel VMX Instruction Reference lists a bunch of bits that are
  6346. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  6347. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  6348. * Otherwise, we should fail with #UD. But most faulting conditions
  6349. * have already been checked by hardware, prior to the VM-exit for
  6350. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  6351. * that bit set to 1 in non-root mode.
  6352. */
  6353. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  6354. kvm_queue_exception(vcpu, UD_VECTOR);
  6355. return 1;
  6356. }
  6357. if (vmx->nested.vmxon) {
  6358. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  6359. return kvm_skip_emulated_instruction(vcpu);
  6360. }
  6361. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  6362. != VMXON_NEEDED_FEATURES) {
  6363. kvm_inject_gp(vcpu, 0);
  6364. return 1;
  6365. }
  6366. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6367. return 1;
  6368. /*
  6369. * SDM 3: 24.11.5
  6370. * The first 4 bytes of VMXON region contain the supported
  6371. * VMCS revision identifier
  6372. *
  6373. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  6374. * which replaces physical address width with 32
  6375. */
  6376. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6377. nested_vmx_failInvalid(vcpu);
  6378. return kvm_skip_emulated_instruction(vcpu);
  6379. }
  6380. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6381. if (is_error_page(page)) {
  6382. nested_vmx_failInvalid(vcpu);
  6383. return kvm_skip_emulated_instruction(vcpu);
  6384. }
  6385. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  6386. kunmap(page);
  6387. kvm_release_page_clean(page);
  6388. nested_vmx_failInvalid(vcpu);
  6389. return kvm_skip_emulated_instruction(vcpu);
  6390. }
  6391. kunmap(page);
  6392. kvm_release_page_clean(page);
  6393. vmx->nested.vmxon_ptr = vmptr;
  6394. ret = enter_vmx_operation(vcpu);
  6395. if (ret)
  6396. return ret;
  6397. nested_vmx_succeed(vcpu);
  6398. return kvm_skip_emulated_instruction(vcpu);
  6399. }
  6400. /*
  6401. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  6402. * for running VMX instructions (except VMXON, whose prerequisites are
  6403. * slightly different). It also specifies what exception to inject otherwise.
  6404. * Note that many of these exceptions have priority over VM exits, so they
  6405. * don't have to be checked again here.
  6406. */
  6407. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  6408. {
  6409. if (!to_vmx(vcpu)->nested.vmxon) {
  6410. kvm_queue_exception(vcpu, UD_VECTOR);
  6411. return 0;
  6412. }
  6413. return 1;
  6414. }
  6415. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  6416. {
  6417. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  6418. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  6419. }
  6420. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  6421. {
  6422. if (vmx->nested.current_vmptr == -1ull)
  6423. return;
  6424. if (enable_shadow_vmcs) {
  6425. /* copy to memory all shadowed fields in case
  6426. they were modified */
  6427. copy_shadow_to_vmcs12(vmx);
  6428. vmx->nested.sync_shadow_vmcs = false;
  6429. vmx_disable_shadow_vmcs(vmx);
  6430. }
  6431. vmx->nested.posted_intr_nv = -1;
  6432. /* Flush VMCS12 to guest memory */
  6433. kvm_vcpu_write_guest_page(&vmx->vcpu,
  6434. vmx->nested.current_vmptr >> PAGE_SHIFT,
  6435. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  6436. vmx->nested.current_vmptr = -1ull;
  6437. }
  6438. /*
  6439. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  6440. * just stops using VMX.
  6441. */
  6442. static void free_nested(struct vcpu_vmx *vmx)
  6443. {
  6444. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  6445. return;
  6446. vmx->nested.vmxon = false;
  6447. vmx->nested.smm.vmxon = false;
  6448. free_vpid(vmx->nested.vpid02);
  6449. vmx->nested.posted_intr_nv = -1;
  6450. vmx->nested.current_vmptr = -1ull;
  6451. if (enable_shadow_vmcs) {
  6452. vmx_disable_shadow_vmcs(vmx);
  6453. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  6454. free_vmcs(vmx->vmcs01.shadow_vmcs);
  6455. vmx->vmcs01.shadow_vmcs = NULL;
  6456. }
  6457. kfree(vmx->nested.cached_vmcs12);
  6458. /* Unpin physical memory we referred to in the vmcs02 */
  6459. if (vmx->nested.apic_access_page) {
  6460. kvm_release_page_dirty(vmx->nested.apic_access_page);
  6461. vmx->nested.apic_access_page = NULL;
  6462. }
  6463. if (vmx->nested.virtual_apic_page) {
  6464. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  6465. vmx->nested.virtual_apic_page = NULL;
  6466. }
  6467. if (vmx->nested.pi_desc_page) {
  6468. kunmap(vmx->nested.pi_desc_page);
  6469. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  6470. vmx->nested.pi_desc_page = NULL;
  6471. vmx->nested.pi_desc = NULL;
  6472. }
  6473. free_loaded_vmcs(&vmx->nested.vmcs02);
  6474. }
  6475. /* Emulate the VMXOFF instruction */
  6476. static int handle_vmoff(struct kvm_vcpu *vcpu)
  6477. {
  6478. if (!nested_vmx_check_permission(vcpu))
  6479. return 1;
  6480. free_nested(to_vmx(vcpu));
  6481. nested_vmx_succeed(vcpu);
  6482. return kvm_skip_emulated_instruction(vcpu);
  6483. }
  6484. /* Emulate the VMCLEAR instruction */
  6485. static int handle_vmclear(struct kvm_vcpu *vcpu)
  6486. {
  6487. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6488. u32 zero = 0;
  6489. gpa_t vmptr;
  6490. if (!nested_vmx_check_permission(vcpu))
  6491. return 1;
  6492. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6493. return 1;
  6494. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6495. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  6496. return kvm_skip_emulated_instruction(vcpu);
  6497. }
  6498. if (vmptr == vmx->nested.vmxon_ptr) {
  6499. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  6500. return kvm_skip_emulated_instruction(vcpu);
  6501. }
  6502. if (vmptr == vmx->nested.current_vmptr)
  6503. nested_release_vmcs12(vmx);
  6504. kvm_vcpu_write_guest(vcpu,
  6505. vmptr + offsetof(struct vmcs12, launch_state),
  6506. &zero, sizeof(zero));
  6507. nested_vmx_succeed(vcpu);
  6508. return kvm_skip_emulated_instruction(vcpu);
  6509. }
  6510. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  6511. /* Emulate the VMLAUNCH instruction */
  6512. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  6513. {
  6514. return nested_vmx_run(vcpu, true);
  6515. }
  6516. /* Emulate the VMRESUME instruction */
  6517. static int handle_vmresume(struct kvm_vcpu *vcpu)
  6518. {
  6519. return nested_vmx_run(vcpu, false);
  6520. }
  6521. /*
  6522. * Read a vmcs12 field. Since these can have varying lengths and we return
  6523. * one type, we chose the biggest type (u64) and zero-extend the return value
  6524. * to that size. Note that the caller, handle_vmread, might need to use only
  6525. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  6526. * 64-bit fields are to be returned).
  6527. */
  6528. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  6529. unsigned long field, u64 *ret)
  6530. {
  6531. short offset = vmcs_field_to_offset(field);
  6532. char *p;
  6533. if (offset < 0)
  6534. return offset;
  6535. p = ((char *)(get_vmcs12(vcpu))) + offset;
  6536. switch (vmcs_field_width(field)) {
  6537. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  6538. *ret = *((natural_width *)p);
  6539. return 0;
  6540. case VMCS_FIELD_WIDTH_U16:
  6541. *ret = *((u16 *)p);
  6542. return 0;
  6543. case VMCS_FIELD_WIDTH_U32:
  6544. *ret = *((u32 *)p);
  6545. return 0;
  6546. case VMCS_FIELD_WIDTH_U64:
  6547. *ret = *((u64 *)p);
  6548. return 0;
  6549. default:
  6550. WARN_ON(1);
  6551. return -ENOENT;
  6552. }
  6553. }
  6554. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  6555. unsigned long field, u64 field_value){
  6556. short offset = vmcs_field_to_offset(field);
  6557. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  6558. if (offset < 0)
  6559. return offset;
  6560. switch (vmcs_field_width(field)) {
  6561. case VMCS_FIELD_WIDTH_U16:
  6562. *(u16 *)p = field_value;
  6563. return 0;
  6564. case VMCS_FIELD_WIDTH_U32:
  6565. *(u32 *)p = field_value;
  6566. return 0;
  6567. case VMCS_FIELD_WIDTH_U64:
  6568. *(u64 *)p = field_value;
  6569. return 0;
  6570. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  6571. *(natural_width *)p = field_value;
  6572. return 0;
  6573. default:
  6574. WARN_ON(1);
  6575. return -ENOENT;
  6576. }
  6577. }
  6578. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  6579. {
  6580. int i;
  6581. unsigned long field;
  6582. u64 field_value;
  6583. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  6584. const u16 *fields = shadow_read_write_fields;
  6585. const int num_fields = max_shadow_read_write_fields;
  6586. preempt_disable();
  6587. vmcs_load(shadow_vmcs);
  6588. for (i = 0; i < num_fields; i++) {
  6589. field = fields[i];
  6590. field_value = __vmcs_readl(field);
  6591. vmcs12_write_any(&vmx->vcpu, field, field_value);
  6592. }
  6593. vmcs_clear(shadow_vmcs);
  6594. vmcs_load(vmx->loaded_vmcs->vmcs);
  6595. preempt_enable();
  6596. }
  6597. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  6598. {
  6599. const u16 *fields[] = {
  6600. shadow_read_write_fields,
  6601. shadow_read_only_fields
  6602. };
  6603. const int max_fields[] = {
  6604. max_shadow_read_write_fields,
  6605. max_shadow_read_only_fields
  6606. };
  6607. int i, q;
  6608. unsigned long field;
  6609. u64 field_value = 0;
  6610. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  6611. vmcs_load(shadow_vmcs);
  6612. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  6613. for (i = 0; i < max_fields[q]; i++) {
  6614. field = fields[q][i];
  6615. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  6616. __vmcs_writel(field, field_value);
  6617. }
  6618. }
  6619. vmcs_clear(shadow_vmcs);
  6620. vmcs_load(vmx->loaded_vmcs->vmcs);
  6621. }
  6622. /*
  6623. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  6624. * used before) all generate the same failure when it is missing.
  6625. */
  6626. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  6627. {
  6628. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6629. if (vmx->nested.current_vmptr == -1ull) {
  6630. nested_vmx_failInvalid(vcpu);
  6631. return 0;
  6632. }
  6633. return 1;
  6634. }
  6635. static int handle_vmread(struct kvm_vcpu *vcpu)
  6636. {
  6637. unsigned long field;
  6638. u64 field_value;
  6639. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6640. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6641. gva_t gva = 0;
  6642. if (!nested_vmx_check_permission(vcpu))
  6643. return 1;
  6644. if (!nested_vmx_check_vmcs12(vcpu))
  6645. return kvm_skip_emulated_instruction(vcpu);
  6646. /* Decode instruction info and find the field to read */
  6647. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6648. /* Read the field, zero-extended to a u64 field_value */
  6649. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  6650. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6651. return kvm_skip_emulated_instruction(vcpu);
  6652. }
  6653. /*
  6654. * Now copy part of this value to register or memory, as requested.
  6655. * Note that the number of bits actually copied is 32 or 64 depending
  6656. * on the guest's mode (32 or 64 bit), not on the given field's length.
  6657. */
  6658. if (vmx_instruction_info & (1u << 10)) {
  6659. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  6660. field_value);
  6661. } else {
  6662. if (get_vmx_mem_address(vcpu, exit_qualification,
  6663. vmx_instruction_info, true, &gva))
  6664. return 1;
  6665. /* _system ok, as hardware has verified cpl=0 */
  6666. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  6667. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  6668. }
  6669. nested_vmx_succeed(vcpu);
  6670. return kvm_skip_emulated_instruction(vcpu);
  6671. }
  6672. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  6673. {
  6674. unsigned long field;
  6675. gva_t gva;
  6676. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6677. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6678. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6679. /* The value to write might be 32 or 64 bits, depending on L1's long
  6680. * mode, and eventually we need to write that into a field of several
  6681. * possible lengths. The code below first zero-extends the value to 64
  6682. * bit (field_value), and then copies only the appropriate number of
  6683. * bits into the vmcs12 field.
  6684. */
  6685. u64 field_value = 0;
  6686. struct x86_exception e;
  6687. if (!nested_vmx_check_permission(vcpu))
  6688. return 1;
  6689. if (!nested_vmx_check_vmcs12(vcpu))
  6690. return kvm_skip_emulated_instruction(vcpu);
  6691. if (vmx_instruction_info & (1u << 10))
  6692. field_value = kvm_register_readl(vcpu,
  6693. (((vmx_instruction_info) >> 3) & 0xf));
  6694. else {
  6695. if (get_vmx_mem_address(vcpu, exit_qualification,
  6696. vmx_instruction_info, false, &gva))
  6697. return 1;
  6698. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  6699. &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  6700. kvm_inject_page_fault(vcpu, &e);
  6701. return 1;
  6702. }
  6703. }
  6704. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6705. if (vmcs_field_readonly(field)) {
  6706. nested_vmx_failValid(vcpu,
  6707. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  6708. return kvm_skip_emulated_instruction(vcpu);
  6709. }
  6710. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  6711. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6712. return kvm_skip_emulated_instruction(vcpu);
  6713. }
  6714. switch (field) {
  6715. #define SHADOW_FIELD_RW(x) case x:
  6716. #include "vmx_shadow_fields.h"
  6717. /*
  6718. * The fields that can be updated by L1 without a vmexit are
  6719. * always updated in the vmcs02, the others go down the slow
  6720. * path of prepare_vmcs02.
  6721. */
  6722. break;
  6723. default:
  6724. vmx->nested.dirty_vmcs12 = true;
  6725. break;
  6726. }
  6727. nested_vmx_succeed(vcpu);
  6728. return kvm_skip_emulated_instruction(vcpu);
  6729. }
  6730. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  6731. {
  6732. vmx->nested.current_vmptr = vmptr;
  6733. if (enable_shadow_vmcs) {
  6734. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  6735. SECONDARY_EXEC_SHADOW_VMCS);
  6736. vmcs_write64(VMCS_LINK_POINTER,
  6737. __pa(vmx->vmcs01.shadow_vmcs));
  6738. vmx->nested.sync_shadow_vmcs = true;
  6739. }
  6740. vmx->nested.dirty_vmcs12 = true;
  6741. }
  6742. /* Emulate the VMPTRLD instruction */
  6743. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  6744. {
  6745. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6746. gpa_t vmptr;
  6747. if (!nested_vmx_check_permission(vcpu))
  6748. return 1;
  6749. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6750. return 1;
  6751. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6752. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  6753. return kvm_skip_emulated_instruction(vcpu);
  6754. }
  6755. if (vmptr == vmx->nested.vmxon_ptr) {
  6756. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  6757. return kvm_skip_emulated_instruction(vcpu);
  6758. }
  6759. if (vmx->nested.current_vmptr != vmptr) {
  6760. struct vmcs12 *new_vmcs12;
  6761. struct page *page;
  6762. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6763. if (is_error_page(page)) {
  6764. nested_vmx_failInvalid(vcpu);
  6765. return kvm_skip_emulated_instruction(vcpu);
  6766. }
  6767. new_vmcs12 = kmap(page);
  6768. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  6769. kunmap(page);
  6770. kvm_release_page_clean(page);
  6771. nested_vmx_failValid(vcpu,
  6772. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  6773. return kvm_skip_emulated_instruction(vcpu);
  6774. }
  6775. nested_release_vmcs12(vmx);
  6776. /*
  6777. * Load VMCS12 from guest memory since it is not already
  6778. * cached.
  6779. */
  6780. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  6781. kunmap(page);
  6782. kvm_release_page_clean(page);
  6783. set_current_vmptr(vmx, vmptr);
  6784. }
  6785. nested_vmx_succeed(vcpu);
  6786. return kvm_skip_emulated_instruction(vcpu);
  6787. }
  6788. /* Emulate the VMPTRST instruction */
  6789. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  6790. {
  6791. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6792. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6793. gva_t vmcs_gva;
  6794. struct x86_exception e;
  6795. if (!nested_vmx_check_permission(vcpu))
  6796. return 1;
  6797. if (get_vmx_mem_address(vcpu, exit_qualification,
  6798. vmx_instruction_info, true, &vmcs_gva))
  6799. return 1;
  6800. /* ok to use *_system, as hardware has verified cpl=0 */
  6801. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  6802. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  6803. sizeof(u64), &e)) {
  6804. kvm_inject_page_fault(vcpu, &e);
  6805. return 1;
  6806. }
  6807. nested_vmx_succeed(vcpu);
  6808. return kvm_skip_emulated_instruction(vcpu);
  6809. }
  6810. /* Emulate the INVEPT instruction */
  6811. static int handle_invept(struct kvm_vcpu *vcpu)
  6812. {
  6813. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6814. u32 vmx_instruction_info, types;
  6815. unsigned long type;
  6816. gva_t gva;
  6817. struct x86_exception e;
  6818. struct {
  6819. u64 eptp, gpa;
  6820. } operand;
  6821. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6822. SECONDARY_EXEC_ENABLE_EPT) ||
  6823. !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
  6824. kvm_queue_exception(vcpu, UD_VECTOR);
  6825. return 1;
  6826. }
  6827. if (!nested_vmx_check_permission(vcpu))
  6828. return 1;
  6829. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6830. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6831. types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  6832. if (type >= 32 || !(types & (1 << type))) {
  6833. nested_vmx_failValid(vcpu,
  6834. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6835. return kvm_skip_emulated_instruction(vcpu);
  6836. }
  6837. /* According to the Intel VMX instruction reference, the memory
  6838. * operand is read even if it isn't needed (e.g., for type==global)
  6839. */
  6840. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6841. vmx_instruction_info, false, &gva))
  6842. return 1;
  6843. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6844. sizeof(operand), &e)) {
  6845. kvm_inject_page_fault(vcpu, &e);
  6846. return 1;
  6847. }
  6848. switch (type) {
  6849. case VMX_EPT_EXTENT_GLOBAL:
  6850. /*
  6851. * TODO: track mappings and invalidate
  6852. * single context requests appropriately
  6853. */
  6854. case VMX_EPT_EXTENT_CONTEXT:
  6855. kvm_mmu_sync_roots(vcpu);
  6856. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  6857. nested_vmx_succeed(vcpu);
  6858. break;
  6859. default:
  6860. BUG_ON(1);
  6861. break;
  6862. }
  6863. return kvm_skip_emulated_instruction(vcpu);
  6864. }
  6865. static int handle_invvpid(struct kvm_vcpu *vcpu)
  6866. {
  6867. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6868. u32 vmx_instruction_info;
  6869. unsigned long type, types;
  6870. gva_t gva;
  6871. struct x86_exception e;
  6872. struct {
  6873. u64 vpid;
  6874. u64 gla;
  6875. } operand;
  6876. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6877. SECONDARY_EXEC_ENABLE_VPID) ||
  6878. !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
  6879. kvm_queue_exception(vcpu, UD_VECTOR);
  6880. return 1;
  6881. }
  6882. if (!nested_vmx_check_permission(vcpu))
  6883. return 1;
  6884. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6885. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6886. types = (vmx->nested.nested_vmx_vpid_caps &
  6887. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  6888. if (type >= 32 || !(types & (1 << type))) {
  6889. nested_vmx_failValid(vcpu,
  6890. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6891. return kvm_skip_emulated_instruction(vcpu);
  6892. }
  6893. /* according to the intel vmx instruction reference, the memory
  6894. * operand is read even if it isn't needed (e.g., for type==global)
  6895. */
  6896. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6897. vmx_instruction_info, false, &gva))
  6898. return 1;
  6899. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6900. sizeof(operand), &e)) {
  6901. kvm_inject_page_fault(vcpu, &e);
  6902. return 1;
  6903. }
  6904. if (operand.vpid >> 16) {
  6905. nested_vmx_failValid(vcpu,
  6906. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6907. return kvm_skip_emulated_instruction(vcpu);
  6908. }
  6909. switch (type) {
  6910. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  6911. if (is_noncanonical_address(operand.gla, vcpu)) {
  6912. nested_vmx_failValid(vcpu,
  6913. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6914. return kvm_skip_emulated_instruction(vcpu);
  6915. }
  6916. /* fall through */
  6917. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  6918. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  6919. if (!operand.vpid) {
  6920. nested_vmx_failValid(vcpu,
  6921. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6922. return kvm_skip_emulated_instruction(vcpu);
  6923. }
  6924. break;
  6925. case VMX_VPID_EXTENT_ALL_CONTEXT:
  6926. break;
  6927. default:
  6928. WARN_ON_ONCE(1);
  6929. return kvm_skip_emulated_instruction(vcpu);
  6930. }
  6931. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  6932. nested_vmx_succeed(vcpu);
  6933. return kvm_skip_emulated_instruction(vcpu);
  6934. }
  6935. static int handle_pml_full(struct kvm_vcpu *vcpu)
  6936. {
  6937. unsigned long exit_qualification;
  6938. trace_kvm_pml_full(vcpu->vcpu_id);
  6939. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6940. /*
  6941. * PML buffer FULL happened while executing iret from NMI,
  6942. * "blocked by NMI" bit has to be set before next VM entry.
  6943. */
  6944. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6945. enable_vnmi &&
  6946. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6947. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6948. GUEST_INTR_STATE_NMI);
  6949. /*
  6950. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  6951. * here.., and there's no userspace involvement needed for PML.
  6952. */
  6953. return 1;
  6954. }
  6955. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  6956. {
  6957. kvm_lapic_expired_hv_timer(vcpu);
  6958. return 1;
  6959. }
  6960. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  6961. {
  6962. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6963. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  6964. /* Check for memory type validity */
  6965. switch (address & VMX_EPTP_MT_MASK) {
  6966. case VMX_EPTP_MT_UC:
  6967. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
  6968. return false;
  6969. break;
  6970. case VMX_EPTP_MT_WB:
  6971. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
  6972. return false;
  6973. break;
  6974. default:
  6975. return false;
  6976. }
  6977. /* only 4 levels page-walk length are valid */
  6978. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  6979. return false;
  6980. /* Reserved bits should not be set */
  6981. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  6982. return false;
  6983. /* AD, if set, should be supported */
  6984. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  6985. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
  6986. return false;
  6987. }
  6988. return true;
  6989. }
  6990. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  6991. struct vmcs12 *vmcs12)
  6992. {
  6993. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  6994. u64 address;
  6995. bool accessed_dirty;
  6996. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  6997. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  6998. !nested_cpu_has_ept(vmcs12))
  6999. return 1;
  7000. if (index >= VMFUNC_EPTP_ENTRIES)
  7001. return 1;
  7002. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7003. &address, index * 8, 8))
  7004. return 1;
  7005. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7006. /*
  7007. * If the (L2) guest does a vmfunc to the currently
  7008. * active ept pointer, we don't have to do anything else
  7009. */
  7010. if (vmcs12->ept_pointer != address) {
  7011. if (!valid_ept_address(vcpu, address))
  7012. return 1;
  7013. kvm_mmu_unload(vcpu);
  7014. mmu->ept_ad = accessed_dirty;
  7015. mmu->base_role.ad_disabled = !accessed_dirty;
  7016. vmcs12->ept_pointer = address;
  7017. /*
  7018. * TODO: Check what's the correct approach in case
  7019. * mmu reload fails. Currently, we just let the next
  7020. * reload potentially fail
  7021. */
  7022. kvm_mmu_reload(vcpu);
  7023. }
  7024. return 0;
  7025. }
  7026. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7027. {
  7028. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7029. struct vmcs12 *vmcs12;
  7030. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7031. /*
  7032. * VMFUNC is only supported for nested guests, but we always enable the
  7033. * secondary control for simplicity; for non-nested mode, fake that we
  7034. * didn't by injecting #UD.
  7035. */
  7036. if (!is_guest_mode(vcpu)) {
  7037. kvm_queue_exception(vcpu, UD_VECTOR);
  7038. return 1;
  7039. }
  7040. vmcs12 = get_vmcs12(vcpu);
  7041. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7042. goto fail;
  7043. switch (function) {
  7044. case 0:
  7045. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7046. goto fail;
  7047. break;
  7048. default:
  7049. goto fail;
  7050. }
  7051. return kvm_skip_emulated_instruction(vcpu);
  7052. fail:
  7053. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7054. vmcs_read32(VM_EXIT_INTR_INFO),
  7055. vmcs_readl(EXIT_QUALIFICATION));
  7056. return 1;
  7057. }
  7058. /*
  7059. * The exit handlers return 1 if the exit was handled fully and guest execution
  7060. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  7061. * to be done to userspace and return 0.
  7062. */
  7063. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  7064. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  7065. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  7066. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  7067. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  7068. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  7069. [EXIT_REASON_CR_ACCESS] = handle_cr,
  7070. [EXIT_REASON_DR_ACCESS] = handle_dr,
  7071. [EXIT_REASON_CPUID] = handle_cpuid,
  7072. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  7073. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  7074. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  7075. [EXIT_REASON_HLT] = handle_halt,
  7076. [EXIT_REASON_INVD] = handle_invd,
  7077. [EXIT_REASON_INVLPG] = handle_invlpg,
  7078. [EXIT_REASON_RDPMC] = handle_rdpmc,
  7079. [EXIT_REASON_VMCALL] = handle_vmcall,
  7080. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  7081. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  7082. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  7083. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  7084. [EXIT_REASON_VMREAD] = handle_vmread,
  7085. [EXIT_REASON_VMRESUME] = handle_vmresume,
  7086. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  7087. [EXIT_REASON_VMOFF] = handle_vmoff,
  7088. [EXIT_REASON_VMON] = handle_vmon,
  7089. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  7090. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  7091. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  7092. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  7093. [EXIT_REASON_WBINVD] = handle_wbinvd,
  7094. [EXIT_REASON_XSETBV] = handle_xsetbv,
  7095. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  7096. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  7097. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  7098. [EXIT_REASON_LDTR_TR] = handle_desc,
  7099. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  7100. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  7101. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  7102. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  7103. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  7104. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  7105. [EXIT_REASON_INVEPT] = handle_invept,
  7106. [EXIT_REASON_INVVPID] = handle_invvpid,
  7107. [EXIT_REASON_RDRAND] = handle_invalid_op,
  7108. [EXIT_REASON_RDSEED] = handle_invalid_op,
  7109. [EXIT_REASON_XSAVES] = handle_xsaves,
  7110. [EXIT_REASON_XRSTORS] = handle_xrstors,
  7111. [EXIT_REASON_PML_FULL] = handle_pml_full,
  7112. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  7113. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  7114. };
  7115. static const int kvm_vmx_max_exit_handlers =
  7116. ARRAY_SIZE(kvm_vmx_exit_handlers);
  7117. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  7118. struct vmcs12 *vmcs12)
  7119. {
  7120. unsigned long exit_qualification;
  7121. gpa_t bitmap, last_bitmap;
  7122. unsigned int port;
  7123. int size;
  7124. u8 b;
  7125. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  7126. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  7127. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7128. port = exit_qualification >> 16;
  7129. size = (exit_qualification & 7) + 1;
  7130. last_bitmap = (gpa_t)-1;
  7131. b = -1;
  7132. while (size > 0) {
  7133. if (port < 0x8000)
  7134. bitmap = vmcs12->io_bitmap_a;
  7135. else if (port < 0x10000)
  7136. bitmap = vmcs12->io_bitmap_b;
  7137. else
  7138. return true;
  7139. bitmap += (port & 0x7fff) / 8;
  7140. if (last_bitmap != bitmap)
  7141. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  7142. return true;
  7143. if (b & (1 << (port & 7)))
  7144. return true;
  7145. port++;
  7146. size--;
  7147. last_bitmap = bitmap;
  7148. }
  7149. return false;
  7150. }
  7151. /*
  7152. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  7153. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  7154. * disinterest in the current event (read or write a specific MSR) by using an
  7155. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  7156. */
  7157. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  7158. struct vmcs12 *vmcs12, u32 exit_reason)
  7159. {
  7160. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  7161. gpa_t bitmap;
  7162. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7163. return true;
  7164. /*
  7165. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  7166. * for the four combinations of read/write and low/high MSR numbers.
  7167. * First we need to figure out which of the four to use:
  7168. */
  7169. bitmap = vmcs12->msr_bitmap;
  7170. if (exit_reason == EXIT_REASON_MSR_WRITE)
  7171. bitmap += 2048;
  7172. if (msr_index >= 0xc0000000) {
  7173. msr_index -= 0xc0000000;
  7174. bitmap += 1024;
  7175. }
  7176. /* Then read the msr_index'th bit from this bitmap: */
  7177. if (msr_index < 1024*8) {
  7178. unsigned char b;
  7179. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  7180. return true;
  7181. return 1 & (b >> (msr_index & 7));
  7182. } else
  7183. return true; /* let L1 handle the wrong parameter */
  7184. }
  7185. /*
  7186. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  7187. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  7188. * intercept (via guest_host_mask etc.) the current event.
  7189. */
  7190. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  7191. struct vmcs12 *vmcs12)
  7192. {
  7193. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7194. int cr = exit_qualification & 15;
  7195. int reg;
  7196. unsigned long val;
  7197. switch ((exit_qualification >> 4) & 3) {
  7198. case 0: /* mov to cr */
  7199. reg = (exit_qualification >> 8) & 15;
  7200. val = kvm_register_readl(vcpu, reg);
  7201. switch (cr) {
  7202. case 0:
  7203. if (vmcs12->cr0_guest_host_mask &
  7204. (val ^ vmcs12->cr0_read_shadow))
  7205. return true;
  7206. break;
  7207. case 3:
  7208. if ((vmcs12->cr3_target_count >= 1 &&
  7209. vmcs12->cr3_target_value0 == val) ||
  7210. (vmcs12->cr3_target_count >= 2 &&
  7211. vmcs12->cr3_target_value1 == val) ||
  7212. (vmcs12->cr3_target_count >= 3 &&
  7213. vmcs12->cr3_target_value2 == val) ||
  7214. (vmcs12->cr3_target_count >= 4 &&
  7215. vmcs12->cr3_target_value3 == val))
  7216. return false;
  7217. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  7218. return true;
  7219. break;
  7220. case 4:
  7221. if (vmcs12->cr4_guest_host_mask &
  7222. (vmcs12->cr4_read_shadow ^ val))
  7223. return true;
  7224. break;
  7225. case 8:
  7226. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  7227. return true;
  7228. break;
  7229. }
  7230. break;
  7231. case 2: /* clts */
  7232. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  7233. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  7234. return true;
  7235. break;
  7236. case 1: /* mov from cr */
  7237. switch (cr) {
  7238. case 3:
  7239. if (vmcs12->cpu_based_vm_exec_control &
  7240. CPU_BASED_CR3_STORE_EXITING)
  7241. return true;
  7242. break;
  7243. case 8:
  7244. if (vmcs12->cpu_based_vm_exec_control &
  7245. CPU_BASED_CR8_STORE_EXITING)
  7246. return true;
  7247. break;
  7248. }
  7249. break;
  7250. case 3: /* lmsw */
  7251. /*
  7252. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  7253. * cr0. Other attempted changes are ignored, with no exit.
  7254. */
  7255. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  7256. if (vmcs12->cr0_guest_host_mask & 0xe &
  7257. (val ^ vmcs12->cr0_read_shadow))
  7258. return true;
  7259. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  7260. !(vmcs12->cr0_read_shadow & 0x1) &&
  7261. (val & 0x1))
  7262. return true;
  7263. break;
  7264. }
  7265. return false;
  7266. }
  7267. /*
  7268. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  7269. * should handle it ourselves in L0 (and then continue L2). Only call this
  7270. * when in is_guest_mode (L2).
  7271. */
  7272. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  7273. {
  7274. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7275. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7276. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7277. if (vmx->nested.nested_run_pending)
  7278. return false;
  7279. if (unlikely(vmx->fail)) {
  7280. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  7281. vmcs_read32(VM_INSTRUCTION_ERROR));
  7282. return true;
  7283. }
  7284. /*
  7285. * The host physical addresses of some pages of guest memory
  7286. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  7287. * Page). The CPU may write to these pages via their host
  7288. * physical address while L2 is running, bypassing any
  7289. * address-translation-based dirty tracking (e.g. EPT write
  7290. * protection).
  7291. *
  7292. * Mark them dirty on every exit from L2 to prevent them from
  7293. * getting out of sync with dirty tracking.
  7294. */
  7295. nested_mark_vmcs12_pages_dirty(vcpu);
  7296. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  7297. vmcs_readl(EXIT_QUALIFICATION),
  7298. vmx->idt_vectoring_info,
  7299. intr_info,
  7300. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7301. KVM_ISA_VMX);
  7302. switch (exit_reason) {
  7303. case EXIT_REASON_EXCEPTION_NMI:
  7304. if (is_nmi(intr_info))
  7305. return false;
  7306. else if (is_page_fault(intr_info))
  7307. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  7308. else if (is_no_device(intr_info) &&
  7309. !(vmcs12->guest_cr0 & X86_CR0_TS))
  7310. return false;
  7311. else if (is_debug(intr_info) &&
  7312. vcpu->guest_debug &
  7313. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  7314. return false;
  7315. else if (is_breakpoint(intr_info) &&
  7316. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  7317. return false;
  7318. return vmcs12->exception_bitmap &
  7319. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  7320. case EXIT_REASON_EXTERNAL_INTERRUPT:
  7321. return false;
  7322. case EXIT_REASON_TRIPLE_FAULT:
  7323. return true;
  7324. case EXIT_REASON_PENDING_INTERRUPT:
  7325. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  7326. case EXIT_REASON_NMI_WINDOW:
  7327. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  7328. case EXIT_REASON_TASK_SWITCH:
  7329. return true;
  7330. case EXIT_REASON_CPUID:
  7331. return true;
  7332. case EXIT_REASON_HLT:
  7333. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  7334. case EXIT_REASON_INVD:
  7335. return true;
  7336. case EXIT_REASON_INVLPG:
  7337. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7338. case EXIT_REASON_RDPMC:
  7339. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  7340. case EXIT_REASON_RDRAND:
  7341. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  7342. case EXIT_REASON_RDSEED:
  7343. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  7344. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  7345. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  7346. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  7347. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  7348. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  7349. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  7350. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  7351. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  7352. /*
  7353. * VMX instructions trap unconditionally. This allows L1 to
  7354. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  7355. */
  7356. return true;
  7357. case EXIT_REASON_CR_ACCESS:
  7358. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  7359. case EXIT_REASON_DR_ACCESS:
  7360. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  7361. case EXIT_REASON_IO_INSTRUCTION:
  7362. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  7363. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  7364. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  7365. case EXIT_REASON_MSR_READ:
  7366. case EXIT_REASON_MSR_WRITE:
  7367. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  7368. case EXIT_REASON_INVALID_STATE:
  7369. return true;
  7370. case EXIT_REASON_MWAIT_INSTRUCTION:
  7371. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  7372. case EXIT_REASON_MONITOR_TRAP_FLAG:
  7373. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  7374. case EXIT_REASON_MONITOR_INSTRUCTION:
  7375. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  7376. case EXIT_REASON_PAUSE_INSTRUCTION:
  7377. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  7378. nested_cpu_has2(vmcs12,
  7379. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  7380. case EXIT_REASON_MCE_DURING_VMENTRY:
  7381. return false;
  7382. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  7383. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  7384. case EXIT_REASON_APIC_ACCESS:
  7385. return nested_cpu_has2(vmcs12,
  7386. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  7387. case EXIT_REASON_APIC_WRITE:
  7388. case EXIT_REASON_EOI_INDUCED:
  7389. /* apic_write and eoi_induced should exit unconditionally. */
  7390. return true;
  7391. case EXIT_REASON_EPT_VIOLATION:
  7392. /*
  7393. * L0 always deals with the EPT violation. If nested EPT is
  7394. * used, and the nested mmu code discovers that the address is
  7395. * missing in the guest EPT table (EPT12), the EPT violation
  7396. * will be injected with nested_ept_inject_page_fault()
  7397. */
  7398. return false;
  7399. case EXIT_REASON_EPT_MISCONFIG:
  7400. /*
  7401. * L2 never uses directly L1's EPT, but rather L0's own EPT
  7402. * table (shadow on EPT) or a merged EPT table that L0 built
  7403. * (EPT on EPT). So any problems with the structure of the
  7404. * table is L0's fault.
  7405. */
  7406. return false;
  7407. case EXIT_REASON_INVPCID:
  7408. return
  7409. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  7410. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7411. case EXIT_REASON_WBINVD:
  7412. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  7413. case EXIT_REASON_XSETBV:
  7414. return true;
  7415. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  7416. /*
  7417. * This should never happen, since it is not possible to
  7418. * set XSS to a non-zero value---neither in L1 nor in L2.
  7419. * If if it were, XSS would have to be checked against
  7420. * the XSS exit bitmap in vmcs12.
  7421. */
  7422. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  7423. case EXIT_REASON_PREEMPTION_TIMER:
  7424. return false;
  7425. case EXIT_REASON_PML_FULL:
  7426. /* We emulate PML support to L1. */
  7427. return false;
  7428. case EXIT_REASON_VMFUNC:
  7429. /* VM functions are emulated through L2->L0 vmexits. */
  7430. return false;
  7431. default:
  7432. return true;
  7433. }
  7434. }
  7435. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  7436. {
  7437. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7438. /*
  7439. * At this point, the exit interruption info in exit_intr_info
  7440. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  7441. * we need to query the in-kernel LAPIC.
  7442. */
  7443. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  7444. if ((exit_intr_info &
  7445. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  7446. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  7447. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7448. vmcs12->vm_exit_intr_error_code =
  7449. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  7450. }
  7451. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  7452. vmcs_readl(EXIT_QUALIFICATION));
  7453. return 1;
  7454. }
  7455. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  7456. {
  7457. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  7458. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  7459. }
  7460. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  7461. {
  7462. if (vmx->pml_pg) {
  7463. __free_page(vmx->pml_pg);
  7464. vmx->pml_pg = NULL;
  7465. }
  7466. }
  7467. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  7468. {
  7469. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7470. u64 *pml_buf;
  7471. u16 pml_idx;
  7472. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  7473. /* Do nothing if PML buffer is empty */
  7474. if (pml_idx == (PML_ENTITY_NUM - 1))
  7475. return;
  7476. /* PML index always points to next available PML buffer entity */
  7477. if (pml_idx >= PML_ENTITY_NUM)
  7478. pml_idx = 0;
  7479. else
  7480. pml_idx++;
  7481. pml_buf = page_address(vmx->pml_pg);
  7482. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  7483. u64 gpa;
  7484. gpa = pml_buf[pml_idx];
  7485. WARN_ON(gpa & (PAGE_SIZE - 1));
  7486. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  7487. }
  7488. /* reset PML index */
  7489. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  7490. }
  7491. /*
  7492. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  7493. * Called before reporting dirty_bitmap to userspace.
  7494. */
  7495. static void kvm_flush_pml_buffers(struct kvm *kvm)
  7496. {
  7497. int i;
  7498. struct kvm_vcpu *vcpu;
  7499. /*
  7500. * We only need to kick vcpu out of guest mode here, as PML buffer
  7501. * is flushed at beginning of all VMEXITs, and it's obvious that only
  7502. * vcpus running in guest are possible to have unflushed GPAs in PML
  7503. * buffer.
  7504. */
  7505. kvm_for_each_vcpu(i, vcpu, kvm)
  7506. kvm_vcpu_kick(vcpu);
  7507. }
  7508. static void vmx_dump_sel(char *name, uint32_t sel)
  7509. {
  7510. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  7511. name, vmcs_read16(sel),
  7512. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  7513. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  7514. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  7515. }
  7516. static void vmx_dump_dtsel(char *name, uint32_t limit)
  7517. {
  7518. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  7519. name, vmcs_read32(limit),
  7520. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  7521. }
  7522. static void dump_vmcs(void)
  7523. {
  7524. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  7525. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  7526. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  7527. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  7528. u32 secondary_exec_control = 0;
  7529. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  7530. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  7531. int i, n;
  7532. if (cpu_has_secondary_exec_ctrls())
  7533. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7534. pr_err("*** Guest State ***\n");
  7535. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  7536. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  7537. vmcs_readl(CR0_GUEST_HOST_MASK));
  7538. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  7539. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  7540. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  7541. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  7542. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  7543. {
  7544. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  7545. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  7546. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  7547. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  7548. }
  7549. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  7550. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  7551. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  7552. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  7553. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  7554. vmcs_readl(GUEST_SYSENTER_ESP),
  7555. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  7556. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  7557. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  7558. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  7559. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  7560. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  7561. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  7562. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  7563. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  7564. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  7565. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  7566. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  7567. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  7568. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  7569. efer, vmcs_read64(GUEST_IA32_PAT));
  7570. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  7571. vmcs_read64(GUEST_IA32_DEBUGCTL),
  7572. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  7573. if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  7574. pr_err("PerfGlobCtl = 0x%016llx\n",
  7575. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  7576. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  7577. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  7578. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  7579. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  7580. vmcs_read32(GUEST_ACTIVITY_STATE));
  7581. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  7582. pr_err("InterruptStatus = %04x\n",
  7583. vmcs_read16(GUEST_INTR_STATUS));
  7584. pr_err("*** Host State ***\n");
  7585. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  7586. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  7587. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  7588. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  7589. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  7590. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  7591. vmcs_read16(HOST_TR_SELECTOR));
  7592. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  7593. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  7594. vmcs_readl(HOST_TR_BASE));
  7595. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  7596. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  7597. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  7598. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  7599. vmcs_readl(HOST_CR4));
  7600. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  7601. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  7602. vmcs_read32(HOST_IA32_SYSENTER_CS),
  7603. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  7604. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  7605. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  7606. vmcs_read64(HOST_IA32_EFER),
  7607. vmcs_read64(HOST_IA32_PAT));
  7608. if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  7609. pr_err("PerfGlobCtl = 0x%016llx\n",
  7610. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  7611. pr_err("*** Control State ***\n");
  7612. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  7613. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  7614. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  7615. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  7616. vmcs_read32(EXCEPTION_BITMAP),
  7617. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  7618. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  7619. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  7620. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  7621. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  7622. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  7623. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  7624. vmcs_read32(VM_EXIT_INTR_INFO),
  7625. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7626. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  7627. pr_err(" reason=%08x qualification=%016lx\n",
  7628. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  7629. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  7630. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  7631. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  7632. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  7633. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  7634. pr_err("TSC Multiplier = 0x%016llx\n",
  7635. vmcs_read64(TSC_MULTIPLIER));
  7636. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  7637. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  7638. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  7639. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  7640. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  7641. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  7642. n = vmcs_read32(CR3_TARGET_COUNT);
  7643. for (i = 0; i + 1 < n; i += 4)
  7644. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  7645. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  7646. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  7647. if (i < n)
  7648. pr_err("CR3 target%u=%016lx\n",
  7649. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  7650. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  7651. pr_err("PLE Gap=%08x Window=%08x\n",
  7652. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  7653. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  7654. pr_err("Virtual processor ID = 0x%04x\n",
  7655. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  7656. }
  7657. /*
  7658. * The guest has exited. See if we can fix it or if we need userspace
  7659. * assistance.
  7660. */
  7661. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  7662. {
  7663. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7664. u32 exit_reason = vmx->exit_reason;
  7665. u32 vectoring_info = vmx->idt_vectoring_info;
  7666. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  7667. /*
  7668. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  7669. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  7670. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  7671. * mode as if vcpus is in root mode, the PML buffer must has been
  7672. * flushed already.
  7673. */
  7674. if (enable_pml)
  7675. vmx_flush_pml_buffer(vcpu);
  7676. /* If guest state is invalid, start emulating */
  7677. if (vmx->emulation_required)
  7678. return handle_invalid_guest_state(vcpu);
  7679. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  7680. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  7681. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  7682. dump_vmcs();
  7683. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  7684. vcpu->run->fail_entry.hardware_entry_failure_reason
  7685. = exit_reason;
  7686. return 0;
  7687. }
  7688. if (unlikely(vmx->fail)) {
  7689. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  7690. vcpu->run->fail_entry.hardware_entry_failure_reason
  7691. = vmcs_read32(VM_INSTRUCTION_ERROR);
  7692. return 0;
  7693. }
  7694. /*
  7695. * Note:
  7696. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  7697. * delivery event since it indicates guest is accessing MMIO.
  7698. * The vm-exit can be triggered again after return to guest that
  7699. * will cause infinite loop.
  7700. */
  7701. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7702. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  7703. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  7704. exit_reason != EXIT_REASON_PML_FULL &&
  7705. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  7706. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  7707. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  7708. vcpu->run->internal.ndata = 3;
  7709. vcpu->run->internal.data[0] = vectoring_info;
  7710. vcpu->run->internal.data[1] = exit_reason;
  7711. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  7712. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  7713. vcpu->run->internal.ndata++;
  7714. vcpu->run->internal.data[3] =
  7715. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  7716. }
  7717. return 0;
  7718. }
  7719. if (unlikely(!enable_vnmi &&
  7720. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  7721. if (vmx_interrupt_allowed(vcpu)) {
  7722. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  7723. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  7724. vcpu->arch.nmi_pending) {
  7725. /*
  7726. * This CPU don't support us in finding the end of an
  7727. * NMI-blocked window if the guest runs with IRQs
  7728. * disabled. So we pull the trigger after 1 s of
  7729. * futile waiting, but inform the user about this.
  7730. */
  7731. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  7732. "state on VCPU %d after 1 s timeout\n",
  7733. __func__, vcpu->vcpu_id);
  7734. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  7735. }
  7736. }
  7737. if (exit_reason < kvm_vmx_max_exit_handlers
  7738. && kvm_vmx_exit_handlers[exit_reason])
  7739. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  7740. else {
  7741. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  7742. exit_reason);
  7743. kvm_queue_exception(vcpu, UD_VECTOR);
  7744. return 1;
  7745. }
  7746. }
  7747. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  7748. {
  7749. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7750. if (is_guest_mode(vcpu) &&
  7751. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  7752. return;
  7753. if (irr == -1 || tpr < irr) {
  7754. vmcs_write32(TPR_THRESHOLD, 0);
  7755. return;
  7756. }
  7757. vmcs_write32(TPR_THRESHOLD, irr);
  7758. }
  7759. static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  7760. {
  7761. u32 sec_exec_control;
  7762. /* Postpone execution until vmcs01 is the current VMCS. */
  7763. if (is_guest_mode(vcpu)) {
  7764. to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
  7765. return;
  7766. }
  7767. if (!cpu_has_vmx_virtualize_x2apic_mode())
  7768. return;
  7769. if (!cpu_need_tpr_shadow(vcpu))
  7770. return;
  7771. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7772. if (set) {
  7773. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7774. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  7775. } else {
  7776. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  7777. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7778. vmx_flush_tlb_ept_only(vcpu);
  7779. }
  7780. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  7781. vmx_update_msr_bitmap(vcpu);
  7782. }
  7783. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  7784. {
  7785. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7786. /*
  7787. * Currently we do not handle the nested case where L2 has an
  7788. * APIC access page of its own; that page is still pinned.
  7789. * Hence, we skip the case where the VCPU is in guest mode _and_
  7790. * L1 prepared an APIC access page for L2.
  7791. *
  7792. * For the case where L1 and L2 share the same APIC access page
  7793. * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
  7794. * in the vmcs12), this function will only update either the vmcs01
  7795. * or the vmcs02. If the former, the vmcs02 will be updated by
  7796. * prepare_vmcs02. If the latter, the vmcs01 will be updated in
  7797. * the next L2->L1 exit.
  7798. */
  7799. if (!is_guest_mode(vcpu) ||
  7800. !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
  7801. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  7802. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  7803. vmx_flush_tlb_ept_only(vcpu);
  7804. }
  7805. }
  7806. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  7807. {
  7808. u16 status;
  7809. u8 old;
  7810. if (max_isr == -1)
  7811. max_isr = 0;
  7812. status = vmcs_read16(GUEST_INTR_STATUS);
  7813. old = status >> 8;
  7814. if (max_isr != old) {
  7815. status &= 0xff;
  7816. status |= max_isr << 8;
  7817. vmcs_write16(GUEST_INTR_STATUS, status);
  7818. }
  7819. }
  7820. static void vmx_set_rvi(int vector)
  7821. {
  7822. u16 status;
  7823. u8 old;
  7824. if (vector == -1)
  7825. vector = 0;
  7826. status = vmcs_read16(GUEST_INTR_STATUS);
  7827. old = (u8)status & 0xff;
  7828. if ((u8)vector != old) {
  7829. status &= ~0xff;
  7830. status |= (u8)vector;
  7831. vmcs_write16(GUEST_INTR_STATUS, status);
  7832. }
  7833. }
  7834. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  7835. {
  7836. /*
  7837. * When running L2, updating RVI is only relevant when
  7838. * vmcs12 virtual-interrupt-delivery enabled.
  7839. * However, it can be enabled only when L1 also
  7840. * intercepts external-interrupts and in that case
  7841. * we should not update vmcs02 RVI but instead intercept
  7842. * interrupt. Therefore, do nothing when running L2.
  7843. */
  7844. if (!is_guest_mode(vcpu))
  7845. vmx_set_rvi(max_irr);
  7846. }
  7847. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  7848. {
  7849. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7850. int max_irr;
  7851. bool max_irr_updated;
  7852. WARN_ON(!vcpu->arch.apicv_active);
  7853. if (pi_test_on(&vmx->pi_desc)) {
  7854. pi_clear_on(&vmx->pi_desc);
  7855. /*
  7856. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  7857. * But on x86 this is just a compiler barrier anyway.
  7858. */
  7859. smp_mb__after_atomic();
  7860. max_irr_updated =
  7861. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  7862. /*
  7863. * If we are running L2 and L1 has a new pending interrupt
  7864. * which can be injected, we should re-evaluate
  7865. * what should be done with this new L1 interrupt.
  7866. * If L1 intercepts external-interrupts, we should
  7867. * exit from L2 to L1. Otherwise, interrupt should be
  7868. * delivered directly to L2.
  7869. */
  7870. if (is_guest_mode(vcpu) && max_irr_updated) {
  7871. if (nested_exit_on_intr(vcpu))
  7872. kvm_vcpu_exiting_guest_mode(vcpu);
  7873. else
  7874. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7875. }
  7876. } else {
  7877. max_irr = kvm_lapic_find_highest_irr(vcpu);
  7878. }
  7879. vmx_hwapic_irr_update(vcpu, max_irr);
  7880. return max_irr;
  7881. }
  7882. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  7883. {
  7884. if (!kvm_vcpu_apicv_active(vcpu))
  7885. return;
  7886. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  7887. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  7888. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  7889. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  7890. }
  7891. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  7892. {
  7893. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7894. pi_clear_on(&vmx->pi_desc);
  7895. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  7896. }
  7897. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  7898. {
  7899. u32 exit_intr_info = 0;
  7900. u16 basic_exit_reason = (u16)vmx->exit_reason;
  7901. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  7902. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  7903. return;
  7904. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  7905. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7906. vmx->exit_intr_info = exit_intr_info;
  7907. /* if exit due to PF check for async PF */
  7908. if (is_page_fault(exit_intr_info))
  7909. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  7910. /* Handle machine checks before interrupts are enabled */
  7911. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  7912. is_machine_check(exit_intr_info))
  7913. kvm_machine_check();
  7914. /* We need to handle NMIs before interrupts are enabled */
  7915. if (is_nmi(exit_intr_info)) {
  7916. kvm_before_handle_nmi(&vmx->vcpu);
  7917. asm("int $2");
  7918. kvm_after_handle_nmi(&vmx->vcpu);
  7919. }
  7920. }
  7921. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  7922. {
  7923. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7924. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  7925. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  7926. unsigned int vector;
  7927. unsigned long entry;
  7928. gate_desc *desc;
  7929. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7930. #ifdef CONFIG_X86_64
  7931. unsigned long tmp;
  7932. #endif
  7933. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7934. desc = (gate_desc *)vmx->host_idt_base + vector;
  7935. entry = gate_offset(desc);
  7936. asm volatile(
  7937. #ifdef CONFIG_X86_64
  7938. "mov %%" _ASM_SP ", %[sp]\n\t"
  7939. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  7940. "push $%c[ss]\n\t"
  7941. "push %[sp]\n\t"
  7942. #endif
  7943. "pushf\n\t"
  7944. __ASM_SIZE(push) " $%c[cs]\n\t"
  7945. CALL_NOSPEC
  7946. :
  7947. #ifdef CONFIG_X86_64
  7948. [sp]"=&r"(tmp),
  7949. #endif
  7950. ASM_CALL_CONSTRAINT
  7951. :
  7952. THUNK_TARGET(entry),
  7953. [ss]"i"(__KERNEL_DS),
  7954. [cs]"i"(__KERNEL_CS)
  7955. );
  7956. }
  7957. }
  7958. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  7959. static bool vmx_has_high_real_mode_segbase(void)
  7960. {
  7961. return enable_unrestricted_guest || emulate_invalid_guest_state;
  7962. }
  7963. static bool vmx_mpx_supported(void)
  7964. {
  7965. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  7966. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  7967. }
  7968. static bool vmx_xsaves_supported(void)
  7969. {
  7970. return vmcs_config.cpu_based_2nd_exec_ctrl &
  7971. SECONDARY_EXEC_XSAVES;
  7972. }
  7973. static bool vmx_umip_emulated(void)
  7974. {
  7975. return vmcs_config.cpu_based_2nd_exec_ctrl &
  7976. SECONDARY_EXEC_DESC;
  7977. }
  7978. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  7979. {
  7980. u32 exit_intr_info;
  7981. bool unblock_nmi;
  7982. u8 vector;
  7983. bool idtv_info_valid;
  7984. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  7985. if (enable_vnmi) {
  7986. if (vmx->loaded_vmcs->nmi_known_unmasked)
  7987. return;
  7988. /*
  7989. * Can't use vmx->exit_intr_info since we're not sure what
  7990. * the exit reason is.
  7991. */
  7992. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7993. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  7994. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7995. /*
  7996. * SDM 3: 27.7.1.2 (September 2008)
  7997. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  7998. * a guest IRET fault.
  7999. * SDM 3: 23.2.2 (September 2008)
  8000. * Bit 12 is undefined in any of the following cases:
  8001. * If the VM exit sets the valid bit in the IDT-vectoring
  8002. * information field.
  8003. * If the VM exit is due to a double fault.
  8004. */
  8005. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  8006. vector != DF_VECTOR && !idtv_info_valid)
  8007. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8008. GUEST_INTR_STATE_NMI);
  8009. else
  8010. vmx->loaded_vmcs->nmi_known_unmasked =
  8011. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  8012. & GUEST_INTR_STATE_NMI);
  8013. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  8014. vmx->loaded_vmcs->vnmi_blocked_time +=
  8015. ktime_to_ns(ktime_sub(ktime_get(),
  8016. vmx->loaded_vmcs->entry_time));
  8017. }
  8018. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  8019. u32 idt_vectoring_info,
  8020. int instr_len_field,
  8021. int error_code_field)
  8022. {
  8023. u8 vector;
  8024. int type;
  8025. bool idtv_info_valid;
  8026. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8027. vcpu->arch.nmi_injected = false;
  8028. kvm_clear_exception_queue(vcpu);
  8029. kvm_clear_interrupt_queue(vcpu);
  8030. if (!idtv_info_valid)
  8031. return;
  8032. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8033. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  8034. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  8035. switch (type) {
  8036. case INTR_TYPE_NMI_INTR:
  8037. vcpu->arch.nmi_injected = true;
  8038. /*
  8039. * SDM 3: 27.7.1.2 (September 2008)
  8040. * Clear bit "block by NMI" before VM entry if a NMI
  8041. * delivery faulted.
  8042. */
  8043. vmx_set_nmi_mask(vcpu, false);
  8044. break;
  8045. case INTR_TYPE_SOFT_EXCEPTION:
  8046. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8047. /* fall through */
  8048. case INTR_TYPE_HARD_EXCEPTION:
  8049. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  8050. u32 err = vmcs_read32(error_code_field);
  8051. kvm_requeue_exception_e(vcpu, vector, err);
  8052. } else
  8053. kvm_requeue_exception(vcpu, vector);
  8054. break;
  8055. case INTR_TYPE_SOFT_INTR:
  8056. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8057. /* fall through */
  8058. case INTR_TYPE_EXT_INTR:
  8059. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  8060. break;
  8061. default:
  8062. break;
  8063. }
  8064. }
  8065. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  8066. {
  8067. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  8068. VM_EXIT_INSTRUCTION_LEN,
  8069. IDT_VECTORING_ERROR_CODE);
  8070. }
  8071. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  8072. {
  8073. __vmx_complete_interrupts(vcpu,
  8074. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8075. VM_ENTRY_INSTRUCTION_LEN,
  8076. VM_ENTRY_EXCEPTION_ERROR_CODE);
  8077. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  8078. }
  8079. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  8080. {
  8081. int i, nr_msrs;
  8082. struct perf_guest_switch_msr *msrs;
  8083. msrs = perf_guest_get_msrs(&nr_msrs);
  8084. if (!msrs)
  8085. return;
  8086. for (i = 0; i < nr_msrs; i++)
  8087. if (msrs[i].host == msrs[i].guest)
  8088. clear_atomic_switch_msr(vmx, msrs[i].msr);
  8089. else
  8090. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  8091. msrs[i].host);
  8092. }
  8093. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  8094. {
  8095. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8096. u64 tscl;
  8097. u32 delta_tsc;
  8098. if (vmx->hv_deadline_tsc == -1)
  8099. return;
  8100. tscl = rdtsc();
  8101. if (vmx->hv_deadline_tsc > tscl)
  8102. /* sure to be 32 bit only because checked on set_hv_timer */
  8103. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  8104. cpu_preemption_timer_multi);
  8105. else
  8106. delta_tsc = 0;
  8107. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  8108. }
  8109. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  8110. {
  8111. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8112. unsigned long cr3, cr4;
  8113. /* Record the guest's net vcpu time for enforced NMI injections. */
  8114. if (unlikely(!enable_vnmi &&
  8115. vmx->loaded_vmcs->soft_vnmi_blocked))
  8116. vmx->loaded_vmcs->entry_time = ktime_get();
  8117. /* Don't enter VMX if guest state is invalid, let the exit handler
  8118. start emulation until we arrive back to a valid state */
  8119. if (vmx->emulation_required)
  8120. return;
  8121. if (vmx->ple_window_dirty) {
  8122. vmx->ple_window_dirty = false;
  8123. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  8124. }
  8125. if (vmx->nested.sync_shadow_vmcs) {
  8126. copy_vmcs12_to_shadow(vmx);
  8127. vmx->nested.sync_shadow_vmcs = false;
  8128. }
  8129. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  8130. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  8131. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  8132. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  8133. cr3 = __get_current_cr3_fast();
  8134. if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
  8135. vmcs_writel(HOST_CR3, cr3);
  8136. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  8137. }
  8138. cr4 = cr4_read_shadow();
  8139. if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
  8140. vmcs_writel(HOST_CR4, cr4);
  8141. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  8142. }
  8143. /* When single-stepping over STI and MOV SS, we must clear the
  8144. * corresponding interruptibility bits in the guest state. Otherwise
  8145. * vmentry fails as it then expects bit 14 (BS) in pending debug
  8146. * exceptions being set, but that's not correct for the guest debugging
  8147. * case. */
  8148. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  8149. vmx_set_interrupt_shadow(vcpu, 0);
  8150. if (static_cpu_has(X86_FEATURE_PKU) &&
  8151. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  8152. vcpu->arch.pkru != vmx->host_pkru)
  8153. __write_pkru(vcpu->arch.pkru);
  8154. atomic_switch_perf_msrs(vmx);
  8155. vmx_arm_hv_timer(vcpu);
  8156. /*
  8157. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  8158. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  8159. * is no need to worry about the conditional branch over the wrmsr
  8160. * being speculatively taken.
  8161. */
  8162. if (vmx->spec_ctrl)
  8163. native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
  8164. vmx->__launched = vmx->loaded_vmcs->launched;
  8165. asm(
  8166. /* Store host registers */
  8167. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  8168. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  8169. "push %%" _ASM_CX " \n\t"
  8170. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8171. "je 1f \n\t"
  8172. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8173. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  8174. "1: \n\t"
  8175. /* Reload cr2 if changed */
  8176. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  8177. "mov %%cr2, %%" _ASM_DX " \n\t"
  8178. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  8179. "je 2f \n\t"
  8180. "mov %%" _ASM_AX", %%cr2 \n\t"
  8181. "2: \n\t"
  8182. /* Check if vmlaunch of vmresume is needed */
  8183. "cmpl $0, %c[launched](%0) \n\t"
  8184. /* Load guest registers. Don't clobber flags. */
  8185. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  8186. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  8187. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  8188. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  8189. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  8190. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  8191. #ifdef CONFIG_X86_64
  8192. "mov %c[r8](%0), %%r8 \n\t"
  8193. "mov %c[r9](%0), %%r9 \n\t"
  8194. "mov %c[r10](%0), %%r10 \n\t"
  8195. "mov %c[r11](%0), %%r11 \n\t"
  8196. "mov %c[r12](%0), %%r12 \n\t"
  8197. "mov %c[r13](%0), %%r13 \n\t"
  8198. "mov %c[r14](%0), %%r14 \n\t"
  8199. "mov %c[r15](%0), %%r15 \n\t"
  8200. #endif
  8201. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  8202. /* Enter guest mode */
  8203. "jne 1f \n\t"
  8204. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  8205. "jmp 2f \n\t"
  8206. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  8207. "2: "
  8208. /* Save guest registers, load host registers, keep flags */
  8209. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  8210. "pop %0 \n\t"
  8211. "setbe %c[fail](%0)\n\t"
  8212. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  8213. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  8214. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  8215. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  8216. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  8217. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  8218. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  8219. #ifdef CONFIG_X86_64
  8220. "mov %%r8, %c[r8](%0) \n\t"
  8221. "mov %%r9, %c[r9](%0) \n\t"
  8222. "mov %%r10, %c[r10](%0) \n\t"
  8223. "mov %%r11, %c[r11](%0) \n\t"
  8224. "mov %%r12, %c[r12](%0) \n\t"
  8225. "mov %%r13, %c[r13](%0) \n\t"
  8226. "mov %%r14, %c[r14](%0) \n\t"
  8227. "mov %%r15, %c[r15](%0) \n\t"
  8228. "xor %%r8d, %%r8d \n\t"
  8229. "xor %%r9d, %%r9d \n\t"
  8230. "xor %%r10d, %%r10d \n\t"
  8231. "xor %%r11d, %%r11d \n\t"
  8232. "xor %%r12d, %%r12d \n\t"
  8233. "xor %%r13d, %%r13d \n\t"
  8234. "xor %%r14d, %%r14d \n\t"
  8235. "xor %%r15d, %%r15d \n\t"
  8236. #endif
  8237. "mov %%cr2, %%" _ASM_AX " \n\t"
  8238. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  8239. "xor %%eax, %%eax \n\t"
  8240. "xor %%ebx, %%ebx \n\t"
  8241. "xor %%esi, %%esi \n\t"
  8242. "xor %%edi, %%edi \n\t"
  8243. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  8244. ".pushsection .rodata \n\t"
  8245. ".global vmx_return \n\t"
  8246. "vmx_return: " _ASM_PTR " 2b \n\t"
  8247. ".popsection"
  8248. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  8249. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  8250. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  8251. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  8252. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  8253. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  8254. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  8255. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  8256. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  8257. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  8258. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  8259. #ifdef CONFIG_X86_64
  8260. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  8261. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  8262. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  8263. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  8264. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  8265. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  8266. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  8267. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  8268. #endif
  8269. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  8270. [wordsize]"i"(sizeof(ulong))
  8271. : "cc", "memory"
  8272. #ifdef CONFIG_X86_64
  8273. , "rax", "rbx", "rdi", "rsi"
  8274. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  8275. #else
  8276. , "eax", "ebx", "edi", "esi"
  8277. #endif
  8278. );
  8279. /*
  8280. * We do not use IBRS in the kernel. If this vCPU has used the
  8281. * SPEC_CTRL MSR it may have left it on; save the value and
  8282. * turn it off. This is much more efficient than blindly adding
  8283. * it to the atomic save/restore list. Especially as the former
  8284. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  8285. *
  8286. * For non-nested case:
  8287. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  8288. * save it.
  8289. *
  8290. * For nested case:
  8291. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  8292. * save it.
  8293. */
  8294. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  8295. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  8296. if (vmx->spec_ctrl)
  8297. native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
  8298. /* Eliminate branch target predictions from guest mode */
  8299. vmexit_fill_RSB();
  8300. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  8301. if (vmx->host_debugctlmsr)
  8302. update_debugctlmsr(vmx->host_debugctlmsr);
  8303. #ifndef CONFIG_X86_64
  8304. /*
  8305. * The sysexit path does not restore ds/es, so we must set them to
  8306. * a reasonable value ourselves.
  8307. *
  8308. * We can't defer this to vmx_load_host_state() since that function
  8309. * may be executed in interrupt context, which saves and restore segments
  8310. * around it, nullifying its effect.
  8311. */
  8312. loadsegment(ds, __USER_DS);
  8313. loadsegment(es, __USER_DS);
  8314. #endif
  8315. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  8316. | (1 << VCPU_EXREG_RFLAGS)
  8317. | (1 << VCPU_EXREG_PDPTR)
  8318. | (1 << VCPU_EXREG_SEGMENTS)
  8319. | (1 << VCPU_EXREG_CR3));
  8320. vcpu->arch.regs_dirty = 0;
  8321. /*
  8322. * eager fpu is enabled if PKEY is supported and CR4 is switched
  8323. * back on host, so it is safe to read guest PKRU from current
  8324. * XSAVE.
  8325. */
  8326. if (static_cpu_has(X86_FEATURE_PKU) &&
  8327. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  8328. vcpu->arch.pkru = __read_pkru();
  8329. if (vcpu->arch.pkru != vmx->host_pkru)
  8330. __write_pkru(vmx->host_pkru);
  8331. }
  8332. /*
  8333. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  8334. * we did not inject a still-pending event to L1 now because of
  8335. * nested_run_pending, we need to re-enable this bit.
  8336. */
  8337. if (vmx->nested.nested_run_pending)
  8338. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8339. vmx->nested.nested_run_pending = 0;
  8340. vmx->idt_vectoring_info = 0;
  8341. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  8342. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8343. return;
  8344. vmx->loaded_vmcs->launched = 1;
  8345. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  8346. vmx_complete_atomic_exit(vmx);
  8347. vmx_recover_nmi_blocking(vmx);
  8348. vmx_complete_interrupts(vmx);
  8349. }
  8350. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  8351. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  8352. {
  8353. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8354. int cpu;
  8355. if (vmx->loaded_vmcs == vmcs)
  8356. return;
  8357. cpu = get_cpu();
  8358. vmx->loaded_vmcs = vmcs;
  8359. vmx_vcpu_put(vcpu);
  8360. vmx_vcpu_load(vcpu, cpu);
  8361. put_cpu();
  8362. }
  8363. /*
  8364. * Ensure that the current vmcs of the logical processor is the
  8365. * vmcs01 of the vcpu before calling free_nested().
  8366. */
  8367. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  8368. {
  8369. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8370. vcpu_load(vcpu);
  8371. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  8372. free_nested(vmx);
  8373. vcpu_put(vcpu);
  8374. }
  8375. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  8376. {
  8377. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8378. if (enable_pml)
  8379. vmx_destroy_pml_buffer(vmx);
  8380. free_vpid(vmx->vpid);
  8381. leave_guest_mode(vcpu);
  8382. vmx_free_vcpu_nested(vcpu);
  8383. free_loaded_vmcs(vmx->loaded_vmcs);
  8384. kfree(vmx->guest_msrs);
  8385. kvm_vcpu_uninit(vcpu);
  8386. kmem_cache_free(kvm_vcpu_cache, vmx);
  8387. }
  8388. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  8389. {
  8390. int err;
  8391. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  8392. unsigned long *msr_bitmap;
  8393. int cpu;
  8394. if (!vmx)
  8395. return ERR_PTR(-ENOMEM);
  8396. vmx->vpid = allocate_vpid();
  8397. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  8398. if (err)
  8399. goto free_vcpu;
  8400. err = -ENOMEM;
  8401. /*
  8402. * If PML is turned on, failure on enabling PML just results in failure
  8403. * of creating the vcpu, therefore we can simplify PML logic (by
  8404. * avoiding dealing with cases, such as enabling PML partially on vcpus
  8405. * for the guest, etc.
  8406. */
  8407. if (enable_pml) {
  8408. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  8409. if (!vmx->pml_pg)
  8410. goto uninit_vcpu;
  8411. }
  8412. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  8413. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  8414. > PAGE_SIZE);
  8415. if (!vmx->guest_msrs)
  8416. goto free_pml;
  8417. err = alloc_loaded_vmcs(&vmx->vmcs01);
  8418. if (err < 0)
  8419. goto free_msrs;
  8420. msr_bitmap = vmx->vmcs01.msr_bitmap;
  8421. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  8422. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  8423. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  8424. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  8425. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  8426. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  8427. vmx->msr_bitmap_mode = 0;
  8428. vmx->loaded_vmcs = &vmx->vmcs01;
  8429. cpu = get_cpu();
  8430. vmx_vcpu_load(&vmx->vcpu, cpu);
  8431. vmx->vcpu.cpu = cpu;
  8432. vmx_vcpu_setup(vmx);
  8433. vmx_vcpu_put(&vmx->vcpu);
  8434. put_cpu();
  8435. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8436. err = alloc_apic_access_page(kvm);
  8437. if (err)
  8438. goto free_vmcs;
  8439. }
  8440. if (enable_ept) {
  8441. err = init_rmode_identity_map(kvm);
  8442. if (err)
  8443. goto free_vmcs;
  8444. }
  8445. if (nested) {
  8446. nested_vmx_setup_ctls_msrs(vmx);
  8447. vmx->nested.vpid02 = allocate_vpid();
  8448. }
  8449. vmx->nested.posted_intr_nv = -1;
  8450. vmx->nested.current_vmptr = -1ull;
  8451. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  8452. /*
  8453. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  8454. * or POSTED_INTR_WAKEUP_VECTOR.
  8455. */
  8456. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  8457. vmx->pi_desc.sn = 1;
  8458. return &vmx->vcpu;
  8459. free_vmcs:
  8460. free_vpid(vmx->nested.vpid02);
  8461. free_loaded_vmcs(vmx->loaded_vmcs);
  8462. free_msrs:
  8463. kfree(vmx->guest_msrs);
  8464. free_pml:
  8465. vmx_destroy_pml_buffer(vmx);
  8466. uninit_vcpu:
  8467. kvm_vcpu_uninit(&vmx->vcpu);
  8468. free_vcpu:
  8469. free_vpid(vmx->vpid);
  8470. kmem_cache_free(kvm_vcpu_cache, vmx);
  8471. return ERR_PTR(err);
  8472. }
  8473. static void __init vmx_check_processor_compat(void *rtn)
  8474. {
  8475. struct vmcs_config vmcs_conf;
  8476. *(int *)rtn = 0;
  8477. if (setup_vmcs_config(&vmcs_conf) < 0)
  8478. *(int *)rtn = -EIO;
  8479. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  8480. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  8481. smp_processor_id());
  8482. *(int *)rtn = -EIO;
  8483. }
  8484. }
  8485. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  8486. {
  8487. u8 cache;
  8488. u64 ipat = 0;
  8489. /* For VT-d and EPT combination
  8490. * 1. MMIO: always map as UC
  8491. * 2. EPT with VT-d:
  8492. * a. VT-d without snooping control feature: can't guarantee the
  8493. * result, try to trust guest.
  8494. * b. VT-d with snooping control feature: snooping control feature of
  8495. * VT-d engine can guarantee the cache correctness. Just set it
  8496. * to WB to keep consistent with host. So the same as item 3.
  8497. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  8498. * consistent with host MTRR
  8499. */
  8500. if (is_mmio) {
  8501. cache = MTRR_TYPE_UNCACHABLE;
  8502. goto exit;
  8503. }
  8504. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  8505. ipat = VMX_EPT_IPAT_BIT;
  8506. cache = MTRR_TYPE_WRBACK;
  8507. goto exit;
  8508. }
  8509. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  8510. ipat = VMX_EPT_IPAT_BIT;
  8511. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  8512. cache = MTRR_TYPE_WRBACK;
  8513. else
  8514. cache = MTRR_TYPE_UNCACHABLE;
  8515. goto exit;
  8516. }
  8517. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  8518. exit:
  8519. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  8520. }
  8521. static int vmx_get_lpage_level(void)
  8522. {
  8523. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  8524. return PT_DIRECTORY_LEVEL;
  8525. else
  8526. /* For shadow and EPT supported 1GB page */
  8527. return PT_PDPE_LEVEL;
  8528. }
  8529. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  8530. {
  8531. /*
  8532. * These bits in the secondary execution controls field
  8533. * are dynamic, the others are mostly based on the hypervisor
  8534. * architecture and the guest's CPUID. Do not touch the
  8535. * dynamic bits.
  8536. */
  8537. u32 mask =
  8538. SECONDARY_EXEC_SHADOW_VMCS |
  8539. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  8540. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8541. SECONDARY_EXEC_DESC;
  8542. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8543. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  8544. (new_ctl & ~mask) | (cur_ctl & mask));
  8545. }
  8546. /*
  8547. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  8548. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  8549. */
  8550. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  8551. {
  8552. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8553. struct kvm_cpuid_entry2 *entry;
  8554. vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
  8555. vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
  8556. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  8557. if (entry && (entry->_reg & (_cpuid_mask))) \
  8558. vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
  8559. } while (0)
  8560. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  8561. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  8562. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  8563. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  8564. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  8565. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  8566. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  8567. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  8568. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  8569. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  8570. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  8571. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  8572. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  8573. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  8574. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  8575. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  8576. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  8577. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  8578. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  8579. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  8580. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  8581. #undef cr4_fixed1_update
  8582. }
  8583. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  8584. {
  8585. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8586. if (cpu_has_secondary_exec_ctrls()) {
  8587. vmx_compute_secondary_exec_control(vmx);
  8588. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  8589. }
  8590. if (nested_vmx_allowed(vcpu))
  8591. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  8592. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  8593. else
  8594. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  8595. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  8596. if (nested_vmx_allowed(vcpu))
  8597. nested_vmx_cr_fixed1_bits_update(vcpu);
  8598. }
  8599. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  8600. {
  8601. if (func == 1 && nested)
  8602. entry->ecx |= bit(X86_FEATURE_VMX);
  8603. }
  8604. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  8605. struct x86_exception *fault)
  8606. {
  8607. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8608. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8609. u32 exit_reason;
  8610. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  8611. if (vmx->nested.pml_full) {
  8612. exit_reason = EXIT_REASON_PML_FULL;
  8613. vmx->nested.pml_full = false;
  8614. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  8615. } else if (fault->error_code & PFERR_RSVD_MASK)
  8616. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  8617. else
  8618. exit_reason = EXIT_REASON_EPT_VIOLATION;
  8619. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  8620. vmcs12->guest_physical_address = fault->address;
  8621. }
  8622. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  8623. {
  8624. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  8625. }
  8626. /* Callbacks for nested_ept_init_mmu_context: */
  8627. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  8628. {
  8629. /* return the page table to be shadowed - in our case, EPT12 */
  8630. return get_vmcs12(vcpu)->ept_pointer;
  8631. }
  8632. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  8633. {
  8634. WARN_ON(mmu_is_nested(vcpu));
  8635. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  8636. return 1;
  8637. kvm_mmu_unload(vcpu);
  8638. kvm_init_shadow_ept_mmu(vcpu,
  8639. to_vmx(vcpu)->nested.nested_vmx_ept_caps &
  8640. VMX_EPT_EXECUTE_ONLY_BIT,
  8641. nested_ept_ad_enabled(vcpu));
  8642. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  8643. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  8644. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  8645. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  8646. return 0;
  8647. }
  8648. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  8649. {
  8650. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  8651. }
  8652. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  8653. u16 error_code)
  8654. {
  8655. bool inequality, bit;
  8656. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  8657. inequality =
  8658. (error_code & vmcs12->page_fault_error_code_mask) !=
  8659. vmcs12->page_fault_error_code_match;
  8660. return inequality ^ bit;
  8661. }
  8662. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  8663. struct x86_exception *fault)
  8664. {
  8665. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8666. WARN_ON(!is_guest_mode(vcpu));
  8667. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  8668. !to_vmx(vcpu)->nested.nested_run_pending) {
  8669. vmcs12->vm_exit_intr_error_code = fault->error_code;
  8670. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  8671. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  8672. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  8673. fault->address);
  8674. } else {
  8675. kvm_inject_page_fault(vcpu, fault);
  8676. }
  8677. }
  8678. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  8679. struct vmcs12 *vmcs12);
  8680. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  8681. struct vmcs12 *vmcs12)
  8682. {
  8683. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8684. struct page *page;
  8685. u64 hpa;
  8686. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  8687. /*
  8688. * Translate L1 physical address to host physical
  8689. * address for vmcs02. Keep the page pinned, so this
  8690. * physical address remains valid. We keep a reference
  8691. * to it so we can release it later.
  8692. */
  8693. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  8694. kvm_release_page_dirty(vmx->nested.apic_access_page);
  8695. vmx->nested.apic_access_page = NULL;
  8696. }
  8697. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  8698. /*
  8699. * If translation failed, no matter: This feature asks
  8700. * to exit when accessing the given address, and if it
  8701. * can never be accessed, this feature won't do
  8702. * anything anyway.
  8703. */
  8704. if (!is_error_page(page)) {
  8705. vmx->nested.apic_access_page = page;
  8706. hpa = page_to_phys(vmx->nested.apic_access_page);
  8707. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8708. } else {
  8709. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  8710. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  8711. }
  8712. } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
  8713. cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8714. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  8715. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  8716. kvm_vcpu_reload_apic_access_page(vcpu);
  8717. }
  8718. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  8719. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  8720. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  8721. vmx->nested.virtual_apic_page = NULL;
  8722. }
  8723. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  8724. /*
  8725. * If translation failed, VM entry will fail because
  8726. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  8727. * Failing the vm entry is _not_ what the processor
  8728. * does but it's basically the only possibility we
  8729. * have. We could still enter the guest if CR8 load
  8730. * exits are enabled, CR8 store exits are enabled, and
  8731. * virtualize APIC access is disabled; in this case
  8732. * the processor would never use the TPR shadow and we
  8733. * could simply clear the bit from the execution
  8734. * control. But such a configuration is useless, so
  8735. * let's keep the code simple.
  8736. */
  8737. if (!is_error_page(page)) {
  8738. vmx->nested.virtual_apic_page = page;
  8739. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  8740. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  8741. }
  8742. }
  8743. if (nested_cpu_has_posted_intr(vmcs12)) {
  8744. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  8745. kunmap(vmx->nested.pi_desc_page);
  8746. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  8747. vmx->nested.pi_desc_page = NULL;
  8748. }
  8749. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  8750. if (is_error_page(page))
  8751. return;
  8752. vmx->nested.pi_desc_page = page;
  8753. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  8754. vmx->nested.pi_desc =
  8755. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  8756. (unsigned long)(vmcs12->posted_intr_desc_addr &
  8757. (PAGE_SIZE - 1)));
  8758. vmcs_write64(POSTED_INTR_DESC_ADDR,
  8759. page_to_phys(vmx->nested.pi_desc_page) +
  8760. (unsigned long)(vmcs12->posted_intr_desc_addr &
  8761. (PAGE_SIZE - 1)));
  8762. }
  8763. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  8764. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  8765. CPU_BASED_USE_MSR_BITMAPS);
  8766. else
  8767. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  8768. CPU_BASED_USE_MSR_BITMAPS);
  8769. }
  8770. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  8771. {
  8772. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  8773. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8774. if (vcpu->arch.virtual_tsc_khz == 0)
  8775. return;
  8776. /* Make sure short timeouts reliably trigger an immediate vmexit.
  8777. * hrtimer_start does not guarantee this. */
  8778. if (preemption_timeout <= 1) {
  8779. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  8780. return;
  8781. }
  8782. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  8783. preemption_timeout *= 1000000;
  8784. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  8785. hrtimer_start(&vmx->nested.preemption_timer,
  8786. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  8787. }
  8788. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  8789. struct vmcs12 *vmcs12)
  8790. {
  8791. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  8792. return 0;
  8793. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  8794. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  8795. return -EINVAL;
  8796. return 0;
  8797. }
  8798. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  8799. struct vmcs12 *vmcs12)
  8800. {
  8801. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8802. return 0;
  8803. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  8804. return -EINVAL;
  8805. return 0;
  8806. }
  8807. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  8808. struct vmcs12 *vmcs12)
  8809. {
  8810. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8811. return 0;
  8812. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  8813. return -EINVAL;
  8814. return 0;
  8815. }
  8816. /*
  8817. * Merge L0's and L1's MSR bitmap, return false to indicate that
  8818. * we do not use the hardware.
  8819. */
  8820. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  8821. struct vmcs12 *vmcs12)
  8822. {
  8823. int msr;
  8824. struct page *page;
  8825. unsigned long *msr_bitmap_l1;
  8826. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  8827. /*
  8828. * pred_cmd & spec_ctrl are trying to verify two things:
  8829. *
  8830. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  8831. * ensures that we do not accidentally generate an L02 MSR bitmap
  8832. * from the L12 MSR bitmap that is too permissive.
  8833. * 2. That L1 or L2s have actually used the MSR. This avoids
  8834. * unnecessarily merging of the bitmap if the MSR is unused. This
  8835. * works properly because we only update the L01 MSR bitmap lazily.
  8836. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  8837. * updated to reflect this when L1 (or its L2s) actually write to
  8838. * the MSR.
  8839. */
  8840. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  8841. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  8842. /* Nothing to do if the MSR bitmap is not in use. */
  8843. if (!cpu_has_vmx_msr_bitmap() ||
  8844. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8845. return false;
  8846. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8847. !pred_cmd && !spec_ctrl)
  8848. return false;
  8849. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  8850. if (is_error_page(page))
  8851. return false;
  8852. msr_bitmap_l1 = (unsigned long *)kmap(page);
  8853. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  8854. /*
  8855. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  8856. * just lets the processor take the value from the virtual-APIC page;
  8857. * take those 256 bits directly from the L1 bitmap.
  8858. */
  8859. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  8860. unsigned word = msr / BITS_PER_LONG;
  8861. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  8862. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  8863. }
  8864. } else {
  8865. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  8866. unsigned word = msr / BITS_PER_LONG;
  8867. msr_bitmap_l0[word] = ~0;
  8868. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  8869. }
  8870. }
  8871. nested_vmx_disable_intercept_for_msr(
  8872. msr_bitmap_l1, msr_bitmap_l0,
  8873. X2APIC_MSR(APIC_TASKPRI),
  8874. MSR_TYPE_W);
  8875. if (nested_cpu_has_vid(vmcs12)) {
  8876. nested_vmx_disable_intercept_for_msr(
  8877. msr_bitmap_l1, msr_bitmap_l0,
  8878. X2APIC_MSR(APIC_EOI),
  8879. MSR_TYPE_W);
  8880. nested_vmx_disable_intercept_for_msr(
  8881. msr_bitmap_l1, msr_bitmap_l0,
  8882. X2APIC_MSR(APIC_SELF_IPI),
  8883. MSR_TYPE_W);
  8884. }
  8885. if (spec_ctrl)
  8886. nested_vmx_disable_intercept_for_msr(
  8887. msr_bitmap_l1, msr_bitmap_l0,
  8888. MSR_IA32_SPEC_CTRL,
  8889. MSR_TYPE_R | MSR_TYPE_W);
  8890. if (pred_cmd)
  8891. nested_vmx_disable_intercept_for_msr(
  8892. msr_bitmap_l1, msr_bitmap_l0,
  8893. MSR_IA32_PRED_CMD,
  8894. MSR_TYPE_W);
  8895. kunmap(page);
  8896. kvm_release_page_clean(page);
  8897. return true;
  8898. }
  8899. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  8900. struct vmcs12 *vmcs12)
  8901. {
  8902. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8903. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  8904. !nested_cpu_has_vid(vmcs12) &&
  8905. !nested_cpu_has_posted_intr(vmcs12))
  8906. return 0;
  8907. /*
  8908. * If virtualize x2apic mode is enabled,
  8909. * virtualize apic access must be disabled.
  8910. */
  8911. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8912. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  8913. return -EINVAL;
  8914. /*
  8915. * If virtual interrupt delivery is enabled,
  8916. * we must exit on external interrupts.
  8917. */
  8918. if (nested_cpu_has_vid(vmcs12) &&
  8919. !nested_exit_on_intr(vcpu))
  8920. return -EINVAL;
  8921. /*
  8922. * bits 15:8 should be zero in posted_intr_nv,
  8923. * the descriptor address has been already checked
  8924. * in nested_get_vmcs12_pages.
  8925. */
  8926. if (nested_cpu_has_posted_intr(vmcs12) &&
  8927. (!nested_cpu_has_vid(vmcs12) ||
  8928. !nested_exit_intr_ack_set(vcpu) ||
  8929. vmcs12->posted_intr_nv & 0xff00))
  8930. return -EINVAL;
  8931. /* tpr shadow is needed by all apicv features. */
  8932. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8933. return -EINVAL;
  8934. return 0;
  8935. }
  8936. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  8937. unsigned long count_field,
  8938. unsigned long addr_field)
  8939. {
  8940. int maxphyaddr;
  8941. u64 count, addr;
  8942. if (vmcs12_read_any(vcpu, count_field, &count) ||
  8943. vmcs12_read_any(vcpu, addr_field, &addr)) {
  8944. WARN_ON(1);
  8945. return -EINVAL;
  8946. }
  8947. if (count == 0)
  8948. return 0;
  8949. maxphyaddr = cpuid_maxphyaddr(vcpu);
  8950. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  8951. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  8952. pr_debug_ratelimited(
  8953. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  8954. addr_field, maxphyaddr, count, addr);
  8955. return -EINVAL;
  8956. }
  8957. return 0;
  8958. }
  8959. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  8960. struct vmcs12 *vmcs12)
  8961. {
  8962. if (vmcs12->vm_exit_msr_load_count == 0 &&
  8963. vmcs12->vm_exit_msr_store_count == 0 &&
  8964. vmcs12->vm_entry_msr_load_count == 0)
  8965. return 0; /* Fast path */
  8966. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  8967. VM_EXIT_MSR_LOAD_ADDR) ||
  8968. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  8969. VM_EXIT_MSR_STORE_ADDR) ||
  8970. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  8971. VM_ENTRY_MSR_LOAD_ADDR))
  8972. return -EINVAL;
  8973. return 0;
  8974. }
  8975. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  8976. struct vmcs12 *vmcs12)
  8977. {
  8978. u64 address = vmcs12->pml_address;
  8979. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  8980. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  8981. if (!nested_cpu_has_ept(vmcs12) ||
  8982. !IS_ALIGNED(address, 4096) ||
  8983. address >> maxphyaddr)
  8984. return -EINVAL;
  8985. }
  8986. return 0;
  8987. }
  8988. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  8989. struct vmx_msr_entry *e)
  8990. {
  8991. /* x2APIC MSR accesses are not allowed */
  8992. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  8993. return -EINVAL;
  8994. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  8995. e->index == MSR_IA32_UCODE_REV)
  8996. return -EINVAL;
  8997. if (e->reserved != 0)
  8998. return -EINVAL;
  8999. return 0;
  9000. }
  9001. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  9002. struct vmx_msr_entry *e)
  9003. {
  9004. if (e->index == MSR_FS_BASE ||
  9005. e->index == MSR_GS_BASE ||
  9006. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  9007. nested_vmx_msr_check_common(vcpu, e))
  9008. return -EINVAL;
  9009. return 0;
  9010. }
  9011. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  9012. struct vmx_msr_entry *e)
  9013. {
  9014. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  9015. nested_vmx_msr_check_common(vcpu, e))
  9016. return -EINVAL;
  9017. return 0;
  9018. }
  9019. /*
  9020. * Load guest's/host's msr at nested entry/exit.
  9021. * return 0 for success, entry index for failure.
  9022. */
  9023. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9024. {
  9025. u32 i;
  9026. struct vmx_msr_entry e;
  9027. struct msr_data msr;
  9028. msr.host_initiated = false;
  9029. for (i = 0; i < count; i++) {
  9030. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  9031. &e, sizeof(e))) {
  9032. pr_debug_ratelimited(
  9033. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9034. __func__, i, gpa + i * sizeof(e));
  9035. goto fail;
  9036. }
  9037. if (nested_vmx_load_msr_check(vcpu, &e)) {
  9038. pr_debug_ratelimited(
  9039. "%s check failed (%u, 0x%x, 0x%x)\n",
  9040. __func__, i, e.index, e.reserved);
  9041. goto fail;
  9042. }
  9043. msr.index = e.index;
  9044. msr.data = e.value;
  9045. if (kvm_set_msr(vcpu, &msr)) {
  9046. pr_debug_ratelimited(
  9047. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9048. __func__, i, e.index, e.value);
  9049. goto fail;
  9050. }
  9051. }
  9052. return 0;
  9053. fail:
  9054. return i + 1;
  9055. }
  9056. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9057. {
  9058. u32 i;
  9059. struct vmx_msr_entry e;
  9060. for (i = 0; i < count; i++) {
  9061. struct msr_data msr_info;
  9062. if (kvm_vcpu_read_guest(vcpu,
  9063. gpa + i * sizeof(e),
  9064. &e, 2 * sizeof(u32))) {
  9065. pr_debug_ratelimited(
  9066. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9067. __func__, i, gpa + i * sizeof(e));
  9068. return -EINVAL;
  9069. }
  9070. if (nested_vmx_store_msr_check(vcpu, &e)) {
  9071. pr_debug_ratelimited(
  9072. "%s check failed (%u, 0x%x, 0x%x)\n",
  9073. __func__, i, e.index, e.reserved);
  9074. return -EINVAL;
  9075. }
  9076. msr_info.host_initiated = false;
  9077. msr_info.index = e.index;
  9078. if (kvm_get_msr(vcpu, &msr_info)) {
  9079. pr_debug_ratelimited(
  9080. "%s cannot read MSR (%u, 0x%x)\n",
  9081. __func__, i, e.index);
  9082. return -EINVAL;
  9083. }
  9084. if (kvm_vcpu_write_guest(vcpu,
  9085. gpa + i * sizeof(e) +
  9086. offsetof(struct vmx_msr_entry, value),
  9087. &msr_info.data, sizeof(msr_info.data))) {
  9088. pr_debug_ratelimited(
  9089. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9090. __func__, i, e.index, msr_info.data);
  9091. return -EINVAL;
  9092. }
  9093. }
  9094. return 0;
  9095. }
  9096. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  9097. {
  9098. unsigned long invalid_mask;
  9099. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  9100. return (val & invalid_mask) == 0;
  9101. }
  9102. /*
  9103. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  9104. * emulating VM entry into a guest with EPT enabled.
  9105. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9106. * is assigned to entry_failure_code on failure.
  9107. */
  9108. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  9109. u32 *entry_failure_code)
  9110. {
  9111. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  9112. if (!nested_cr3_valid(vcpu, cr3)) {
  9113. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9114. return 1;
  9115. }
  9116. /*
  9117. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  9118. * must not be dereferenced.
  9119. */
  9120. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  9121. !nested_ept) {
  9122. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  9123. *entry_failure_code = ENTRY_FAIL_PDPTE;
  9124. return 1;
  9125. }
  9126. }
  9127. vcpu->arch.cr3 = cr3;
  9128. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  9129. }
  9130. kvm_mmu_reset_context(vcpu);
  9131. return 0;
  9132. }
  9133. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9134. bool from_vmentry)
  9135. {
  9136. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9137. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  9138. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  9139. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  9140. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  9141. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  9142. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  9143. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  9144. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  9145. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  9146. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  9147. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  9148. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  9149. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  9150. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  9151. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  9152. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  9153. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  9154. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  9155. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  9156. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  9157. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  9158. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  9159. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  9160. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  9161. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  9162. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  9163. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  9164. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  9165. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  9166. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  9167. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  9168. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  9169. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  9170. vmcs12->guest_pending_dbg_exceptions);
  9171. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  9172. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  9173. if (nested_cpu_has_xsaves(vmcs12))
  9174. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  9175. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  9176. if (cpu_has_vmx_posted_intr())
  9177. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  9178. /*
  9179. * Whether page-faults are trapped is determined by a combination of
  9180. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  9181. * If enable_ept, L0 doesn't care about page faults and we should
  9182. * set all of these to L1's desires. However, if !enable_ept, L0 does
  9183. * care about (at least some) page faults, and because it is not easy
  9184. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  9185. * to exit on each and every L2 page fault. This is done by setting
  9186. * MASK=MATCH=0 and (see below) EB.PF=1.
  9187. * Note that below we don't need special code to set EB.PF beyond the
  9188. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  9189. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  9190. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  9191. */
  9192. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  9193. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  9194. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  9195. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  9196. /* All VMFUNCs are currently emulated through L0 vmexits. */
  9197. if (cpu_has_vmx_vmfunc())
  9198. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  9199. if (cpu_has_vmx_apicv()) {
  9200. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  9201. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  9202. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  9203. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  9204. }
  9205. /*
  9206. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  9207. * Some constant fields are set here by vmx_set_constant_host_state().
  9208. * Other fields are different per CPU, and will be set later when
  9209. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  9210. */
  9211. vmx_set_constant_host_state(vmx);
  9212. /*
  9213. * Set the MSR load/store lists to match L0's settings.
  9214. */
  9215. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  9216. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9217. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  9218. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9219. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  9220. set_cr4_guest_host_mask(vmx);
  9221. if (vmx_mpx_supported())
  9222. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  9223. if (enable_vpid) {
  9224. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  9225. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  9226. else
  9227. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  9228. }
  9229. /*
  9230. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  9231. */
  9232. if (enable_ept) {
  9233. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  9234. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  9235. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  9236. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  9237. }
  9238. if (cpu_has_vmx_msr_bitmap())
  9239. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  9240. }
  9241. /*
  9242. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  9243. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  9244. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  9245. * guest in a way that will both be appropriate to L1's requests, and our
  9246. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  9247. * function also has additional necessary side-effects, like setting various
  9248. * vcpu->arch fields.
  9249. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9250. * is assigned to entry_failure_code on failure.
  9251. */
  9252. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9253. bool from_vmentry, u32 *entry_failure_code)
  9254. {
  9255. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9256. u32 exec_control, vmcs12_exec_ctrl;
  9257. /*
  9258. * First, the fields that are shadowed. This must be kept in sync
  9259. * with vmx_shadow_fields.h.
  9260. */
  9261. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  9262. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  9263. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  9264. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  9265. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  9266. /*
  9267. * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
  9268. * HOST_FS_BASE, HOST_GS_BASE.
  9269. */
  9270. if (from_vmentry &&
  9271. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  9272. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  9273. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  9274. } else {
  9275. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  9276. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  9277. }
  9278. if (from_vmentry) {
  9279. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  9280. vmcs12->vm_entry_intr_info_field);
  9281. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  9282. vmcs12->vm_entry_exception_error_code);
  9283. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  9284. vmcs12->vm_entry_instruction_len);
  9285. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  9286. vmcs12->guest_interruptibility_info);
  9287. vmx->loaded_vmcs->nmi_known_unmasked =
  9288. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  9289. } else {
  9290. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9291. }
  9292. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  9293. exec_control = vmcs12->pin_based_vm_exec_control;
  9294. /* Preemption timer setting is only taken from vmcs01. */
  9295. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9296. exec_control |= vmcs_config.pin_based_exec_ctrl;
  9297. if (vmx->hv_deadline_tsc == -1)
  9298. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9299. /* Posted interrupts setting is only taken from vmcs12. */
  9300. if (nested_cpu_has_posted_intr(vmcs12)) {
  9301. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  9302. vmx->nested.pi_pending = false;
  9303. } else {
  9304. exec_control &= ~PIN_BASED_POSTED_INTR;
  9305. }
  9306. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  9307. vmx->nested.preemption_timer_expired = false;
  9308. if (nested_cpu_has_preemption_timer(vmcs12))
  9309. vmx_start_preemption_timer(vcpu);
  9310. if (cpu_has_secondary_exec_ctrls()) {
  9311. exec_control = vmx->secondary_exec_control;
  9312. /* Take the following fields only from vmcs12 */
  9313. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9314. SECONDARY_EXEC_ENABLE_INVPCID |
  9315. SECONDARY_EXEC_RDTSCP |
  9316. SECONDARY_EXEC_XSAVES |
  9317. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  9318. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  9319. SECONDARY_EXEC_ENABLE_VMFUNC);
  9320. if (nested_cpu_has(vmcs12,
  9321. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  9322. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  9323. ~SECONDARY_EXEC_ENABLE_PML;
  9324. exec_control |= vmcs12_exec_ctrl;
  9325. }
  9326. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  9327. vmcs_write16(GUEST_INTR_STATUS,
  9328. vmcs12->guest_intr_status);
  9329. /*
  9330. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  9331. * nested_get_vmcs12_pages will either fix it up or
  9332. * remove the VM execution control.
  9333. */
  9334. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  9335. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  9336. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  9337. }
  9338. /*
  9339. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  9340. * entry, but only if the current (host) sp changed from the value
  9341. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  9342. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  9343. * here we just force the write to happen on entry.
  9344. */
  9345. vmx->host_rsp = 0;
  9346. exec_control = vmx_exec_control(vmx); /* L0's desires */
  9347. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  9348. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  9349. exec_control &= ~CPU_BASED_TPR_SHADOW;
  9350. exec_control |= vmcs12->cpu_based_vm_exec_control;
  9351. /*
  9352. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  9353. * nested_get_vmcs12_pages can't fix it up, the illegal value
  9354. * will result in a VM entry failure.
  9355. */
  9356. if (exec_control & CPU_BASED_TPR_SHADOW) {
  9357. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  9358. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  9359. } else {
  9360. #ifdef CONFIG_X86_64
  9361. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  9362. CPU_BASED_CR8_STORE_EXITING;
  9363. #endif
  9364. }
  9365. /*
  9366. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  9367. * for I/O port accesses.
  9368. */
  9369. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  9370. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  9371. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  9372. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  9373. * bitwise-or of what L1 wants to trap for L2, and what we want to
  9374. * trap. Note that CR0.TS also needs updating - we do this later.
  9375. */
  9376. update_exception_bitmap(vcpu);
  9377. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  9378. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  9379. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  9380. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  9381. * bits are further modified by vmx_set_efer() below.
  9382. */
  9383. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  9384. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  9385. * emulated by vmx_set_efer(), below.
  9386. */
  9387. vm_entry_controls_init(vmx,
  9388. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  9389. ~VM_ENTRY_IA32E_MODE) |
  9390. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  9391. if (from_vmentry &&
  9392. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  9393. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  9394. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  9395. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  9396. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  9397. }
  9398. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  9399. vmcs_write64(TSC_OFFSET,
  9400. vcpu->arch.tsc_offset + vmcs12->tsc_offset);
  9401. else
  9402. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  9403. if (kvm_has_tsc_control)
  9404. decache_tsc_multiplier(vmx);
  9405. if (enable_vpid) {
  9406. /*
  9407. * There is no direct mapping between vpid02 and vpid12, the
  9408. * vpid02 is per-vCPU for L0 and reused while the value of
  9409. * vpid12 is changed w/ one invvpid during nested vmentry.
  9410. * The vpid12 is allocated by L1 for L2, so it will not
  9411. * influence global bitmap(for vpid01 and vpid02 allocation)
  9412. * even if spawn a lot of nested vCPUs.
  9413. */
  9414. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  9415. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  9416. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  9417. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
  9418. }
  9419. } else {
  9420. vmx_flush_tlb(vcpu, true);
  9421. }
  9422. }
  9423. if (enable_pml) {
  9424. /*
  9425. * Conceptually we want to copy the PML address and index from
  9426. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  9427. * since we always flush the log on each vmexit, this happens
  9428. * to be equivalent to simply resetting the fields in vmcs02.
  9429. */
  9430. ASSERT(vmx->pml_pg);
  9431. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  9432. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  9433. }
  9434. if (nested_cpu_has_ept(vmcs12)) {
  9435. if (nested_ept_init_mmu_context(vcpu)) {
  9436. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9437. return 1;
  9438. }
  9439. } else if (nested_cpu_has2(vmcs12,
  9440. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9441. vmx_flush_tlb_ept_only(vcpu);
  9442. }
  9443. /*
  9444. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  9445. * bits which we consider mandatory enabled.
  9446. * The CR0_READ_SHADOW is what L2 should have expected to read given
  9447. * the specifications by L1; It's not enough to take
  9448. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  9449. * have more bits than L1 expected.
  9450. */
  9451. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  9452. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  9453. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  9454. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  9455. if (from_vmentry &&
  9456. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  9457. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  9458. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  9459. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9460. else
  9461. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9462. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  9463. vmx_set_efer(vcpu, vcpu->arch.efer);
  9464. if (vmx->nested.dirty_vmcs12) {
  9465. prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
  9466. vmx->nested.dirty_vmcs12 = false;
  9467. }
  9468. /* Shadow page tables on either EPT or shadow page tables. */
  9469. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  9470. entry_failure_code))
  9471. return 1;
  9472. if (!enable_ept)
  9473. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  9474. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  9475. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  9476. return 0;
  9477. }
  9478. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9479. {
  9480. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9481. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  9482. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  9483. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9484. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  9485. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9486. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  9487. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9488. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  9489. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9490. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  9491. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9492. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  9493. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9494. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  9495. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9496. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  9497. vmx->nested.nested_vmx_procbased_ctls_low,
  9498. vmx->nested.nested_vmx_procbased_ctls_high) ||
  9499. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  9500. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  9501. vmx->nested.nested_vmx_secondary_ctls_low,
  9502. vmx->nested.nested_vmx_secondary_ctls_high)) ||
  9503. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  9504. vmx->nested.nested_vmx_pinbased_ctls_low,
  9505. vmx->nested.nested_vmx_pinbased_ctls_high) ||
  9506. !vmx_control_verify(vmcs12->vm_exit_controls,
  9507. vmx->nested.nested_vmx_exit_ctls_low,
  9508. vmx->nested.nested_vmx_exit_ctls_high) ||
  9509. !vmx_control_verify(vmcs12->vm_entry_controls,
  9510. vmx->nested.nested_vmx_entry_ctls_low,
  9511. vmx->nested.nested_vmx_entry_ctls_high))
  9512. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9513. if (nested_cpu_has_vmfunc(vmcs12)) {
  9514. if (vmcs12->vm_function_control &
  9515. ~vmx->nested.nested_vmx_vmfunc_controls)
  9516. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9517. if (nested_cpu_has_eptp_switching(vmcs12)) {
  9518. if (!nested_cpu_has_ept(vmcs12) ||
  9519. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  9520. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9521. }
  9522. }
  9523. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  9524. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9525. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  9526. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  9527. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  9528. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  9529. return 0;
  9530. }
  9531. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9532. u32 *exit_qual)
  9533. {
  9534. bool ia32e;
  9535. *exit_qual = ENTRY_FAIL_DEFAULT;
  9536. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  9537. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  9538. return 1;
  9539. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
  9540. vmcs12->vmcs_link_pointer != -1ull) {
  9541. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  9542. return 1;
  9543. }
  9544. /*
  9545. * If the load IA32_EFER VM-entry control is 1, the following checks
  9546. * are performed on the field for the IA32_EFER MSR:
  9547. * - Bits reserved in the IA32_EFER MSR must be 0.
  9548. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  9549. * the IA-32e mode guest VM-exit control. It must also be identical
  9550. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  9551. * CR0.PG) is 1.
  9552. */
  9553. if (to_vmx(vcpu)->nested.nested_run_pending &&
  9554. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  9555. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  9556. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  9557. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  9558. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  9559. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  9560. return 1;
  9561. }
  9562. /*
  9563. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  9564. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  9565. * the values of the LMA and LME bits in the field must each be that of
  9566. * the host address-space size VM-exit control.
  9567. */
  9568. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  9569. ia32e = (vmcs12->vm_exit_controls &
  9570. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  9571. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  9572. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  9573. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  9574. return 1;
  9575. }
  9576. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  9577. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  9578. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  9579. return 1;
  9580. return 0;
  9581. }
  9582. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
  9583. {
  9584. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9585. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9586. u32 msr_entry_idx;
  9587. u32 exit_qual;
  9588. enter_guest_mode(vcpu);
  9589. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  9590. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  9591. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  9592. vmx_segment_cache_clear(vmx);
  9593. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
  9594. leave_guest_mode(vcpu);
  9595. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9596. nested_vmx_entry_failure(vcpu, vmcs12,
  9597. EXIT_REASON_INVALID_STATE, exit_qual);
  9598. return 1;
  9599. }
  9600. nested_get_vmcs12_pages(vcpu, vmcs12);
  9601. msr_entry_idx = nested_vmx_load_msr(vcpu,
  9602. vmcs12->vm_entry_msr_load_addr,
  9603. vmcs12->vm_entry_msr_load_count);
  9604. if (msr_entry_idx) {
  9605. leave_guest_mode(vcpu);
  9606. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9607. nested_vmx_entry_failure(vcpu, vmcs12,
  9608. EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
  9609. return 1;
  9610. }
  9611. /*
  9612. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  9613. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  9614. * returned as far as L1 is concerned. It will only return (and set
  9615. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  9616. */
  9617. return 0;
  9618. }
  9619. /*
  9620. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  9621. * for running an L2 nested guest.
  9622. */
  9623. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  9624. {
  9625. struct vmcs12 *vmcs12;
  9626. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9627. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  9628. u32 exit_qual;
  9629. int ret;
  9630. if (!nested_vmx_check_permission(vcpu))
  9631. return 1;
  9632. if (!nested_vmx_check_vmcs12(vcpu))
  9633. goto out;
  9634. vmcs12 = get_vmcs12(vcpu);
  9635. if (enable_shadow_vmcs)
  9636. copy_shadow_to_vmcs12(vmx);
  9637. /*
  9638. * The nested entry process starts with enforcing various prerequisites
  9639. * on vmcs12 as required by the Intel SDM, and act appropriately when
  9640. * they fail: As the SDM explains, some conditions should cause the
  9641. * instruction to fail, while others will cause the instruction to seem
  9642. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  9643. * To speed up the normal (success) code path, we should avoid checking
  9644. * for misconfigurations which will anyway be caught by the processor
  9645. * when using the merged vmcs02.
  9646. */
  9647. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  9648. nested_vmx_failValid(vcpu,
  9649. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  9650. goto out;
  9651. }
  9652. if (vmcs12->launch_state == launch) {
  9653. nested_vmx_failValid(vcpu,
  9654. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  9655. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  9656. goto out;
  9657. }
  9658. ret = check_vmentry_prereqs(vcpu, vmcs12);
  9659. if (ret) {
  9660. nested_vmx_failValid(vcpu, ret);
  9661. goto out;
  9662. }
  9663. /*
  9664. * After this point, the trap flag no longer triggers a singlestep trap
  9665. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  9666. * This is not 100% correct; for performance reasons, we delegate most
  9667. * of the checks on host state to the processor. If those fail,
  9668. * the singlestep trap is missed.
  9669. */
  9670. skip_emulated_instruction(vcpu);
  9671. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  9672. if (ret) {
  9673. nested_vmx_entry_failure(vcpu, vmcs12,
  9674. EXIT_REASON_INVALID_STATE, exit_qual);
  9675. return 1;
  9676. }
  9677. /*
  9678. * We're finally done with prerequisite checking, and can start with
  9679. * the nested entry.
  9680. */
  9681. ret = enter_vmx_non_root_mode(vcpu, true);
  9682. if (ret)
  9683. return ret;
  9684. /*
  9685. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  9686. * by event injection, halt vcpu.
  9687. */
  9688. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  9689. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
  9690. return kvm_vcpu_halt(vcpu);
  9691. vmx->nested.nested_run_pending = 1;
  9692. return 1;
  9693. out:
  9694. return kvm_skip_emulated_instruction(vcpu);
  9695. }
  9696. /*
  9697. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  9698. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  9699. * This function returns the new value we should put in vmcs12.guest_cr0.
  9700. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  9701. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  9702. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  9703. * didn't trap the bit, because if L1 did, so would L0).
  9704. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  9705. * been modified by L2, and L1 knows it. So just leave the old value of
  9706. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  9707. * isn't relevant, because if L0 traps this bit it can set it to anything.
  9708. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  9709. * changed these bits, and therefore they need to be updated, but L0
  9710. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  9711. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  9712. */
  9713. static inline unsigned long
  9714. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9715. {
  9716. return
  9717. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  9718. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  9719. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  9720. vcpu->arch.cr0_guest_owned_bits));
  9721. }
  9722. static inline unsigned long
  9723. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9724. {
  9725. return
  9726. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  9727. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  9728. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  9729. vcpu->arch.cr4_guest_owned_bits));
  9730. }
  9731. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  9732. struct vmcs12 *vmcs12)
  9733. {
  9734. u32 idt_vectoring;
  9735. unsigned int nr;
  9736. if (vcpu->arch.exception.injected) {
  9737. nr = vcpu->arch.exception.nr;
  9738. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  9739. if (kvm_exception_is_soft(nr)) {
  9740. vmcs12->vm_exit_instruction_len =
  9741. vcpu->arch.event_exit_inst_len;
  9742. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  9743. } else
  9744. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  9745. if (vcpu->arch.exception.has_error_code) {
  9746. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  9747. vmcs12->idt_vectoring_error_code =
  9748. vcpu->arch.exception.error_code;
  9749. }
  9750. vmcs12->idt_vectoring_info_field = idt_vectoring;
  9751. } else if (vcpu->arch.nmi_injected) {
  9752. vmcs12->idt_vectoring_info_field =
  9753. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  9754. } else if (vcpu->arch.interrupt.pending) {
  9755. nr = vcpu->arch.interrupt.nr;
  9756. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  9757. if (vcpu->arch.interrupt.soft) {
  9758. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  9759. vmcs12->vm_entry_instruction_len =
  9760. vcpu->arch.event_exit_inst_len;
  9761. } else
  9762. idt_vectoring |= INTR_TYPE_EXT_INTR;
  9763. vmcs12->idt_vectoring_info_field = idt_vectoring;
  9764. }
  9765. }
  9766. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  9767. {
  9768. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9769. unsigned long exit_qual;
  9770. bool block_nested_events =
  9771. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  9772. if (vcpu->arch.exception.pending &&
  9773. nested_vmx_check_exception(vcpu, &exit_qual)) {
  9774. if (block_nested_events)
  9775. return -EBUSY;
  9776. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  9777. return 0;
  9778. }
  9779. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  9780. vmx->nested.preemption_timer_expired) {
  9781. if (block_nested_events)
  9782. return -EBUSY;
  9783. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  9784. return 0;
  9785. }
  9786. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  9787. if (block_nested_events)
  9788. return -EBUSY;
  9789. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9790. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  9791. INTR_INFO_VALID_MASK, 0);
  9792. /*
  9793. * The NMI-triggered VM exit counts as injection:
  9794. * clear this one and block further NMIs.
  9795. */
  9796. vcpu->arch.nmi_pending = 0;
  9797. vmx_set_nmi_mask(vcpu, true);
  9798. return 0;
  9799. }
  9800. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  9801. nested_exit_on_intr(vcpu)) {
  9802. if (block_nested_events)
  9803. return -EBUSY;
  9804. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  9805. return 0;
  9806. }
  9807. vmx_complete_nested_posted_interrupt(vcpu);
  9808. return 0;
  9809. }
  9810. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  9811. {
  9812. ktime_t remaining =
  9813. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  9814. u64 value;
  9815. if (ktime_to_ns(remaining) <= 0)
  9816. return 0;
  9817. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  9818. do_div(value, 1000000);
  9819. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9820. }
  9821. /*
  9822. * Update the guest state fields of vmcs12 to reflect changes that
  9823. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  9824. * VM-entry controls is also updated, since this is really a guest
  9825. * state bit.)
  9826. */
  9827. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9828. {
  9829. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  9830. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  9831. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  9832. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  9833. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  9834. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  9835. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  9836. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  9837. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  9838. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  9839. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  9840. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  9841. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  9842. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  9843. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  9844. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  9845. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  9846. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  9847. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  9848. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  9849. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  9850. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  9851. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  9852. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  9853. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  9854. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  9855. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  9856. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  9857. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  9858. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  9859. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  9860. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  9861. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  9862. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  9863. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  9864. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  9865. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  9866. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  9867. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  9868. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  9869. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  9870. vmcs12->guest_interruptibility_info =
  9871. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  9872. vmcs12->guest_pending_dbg_exceptions =
  9873. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  9874. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  9875. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  9876. else
  9877. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  9878. if (nested_cpu_has_preemption_timer(vmcs12)) {
  9879. if (vmcs12->vm_exit_controls &
  9880. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  9881. vmcs12->vmx_preemption_timer_value =
  9882. vmx_get_preemption_timer_value(vcpu);
  9883. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  9884. }
  9885. /*
  9886. * In some cases (usually, nested EPT), L2 is allowed to change its
  9887. * own CR3 without exiting. If it has changed it, we must keep it.
  9888. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  9889. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  9890. *
  9891. * Additionally, restore L2's PDPTR to vmcs12.
  9892. */
  9893. if (enable_ept) {
  9894. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  9895. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  9896. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  9897. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  9898. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  9899. }
  9900. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  9901. if (nested_cpu_has_vid(vmcs12))
  9902. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  9903. vmcs12->vm_entry_controls =
  9904. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  9905. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  9906. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  9907. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  9908. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  9909. }
  9910. /* TODO: These cannot have changed unless we have MSR bitmaps and
  9911. * the relevant bit asks not to trap the change */
  9912. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  9913. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  9914. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  9915. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  9916. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  9917. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  9918. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  9919. if (kvm_mpx_supported())
  9920. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  9921. }
  9922. /*
  9923. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  9924. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  9925. * and this function updates it to reflect the changes to the guest state while
  9926. * L2 was running (and perhaps made some exits which were handled directly by L0
  9927. * without going back to L1), and to reflect the exit reason.
  9928. * Note that we do not have to copy here all VMCS fields, just those that
  9929. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  9930. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  9931. * which already writes to vmcs12 directly.
  9932. */
  9933. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9934. u32 exit_reason, u32 exit_intr_info,
  9935. unsigned long exit_qualification)
  9936. {
  9937. /* update guest state fields: */
  9938. sync_vmcs12(vcpu, vmcs12);
  9939. /* update exit information fields: */
  9940. vmcs12->vm_exit_reason = exit_reason;
  9941. vmcs12->exit_qualification = exit_qualification;
  9942. vmcs12->vm_exit_intr_info = exit_intr_info;
  9943. vmcs12->idt_vectoring_info_field = 0;
  9944. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  9945. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  9946. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  9947. vmcs12->launch_state = 1;
  9948. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  9949. * instead of reading the real value. */
  9950. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  9951. /*
  9952. * Transfer the event that L0 or L1 may wanted to inject into
  9953. * L2 to IDT_VECTORING_INFO_FIELD.
  9954. */
  9955. vmcs12_save_pending_event(vcpu, vmcs12);
  9956. }
  9957. /*
  9958. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  9959. * preserved above and would only end up incorrectly in L1.
  9960. */
  9961. vcpu->arch.nmi_injected = false;
  9962. kvm_clear_exception_queue(vcpu);
  9963. kvm_clear_interrupt_queue(vcpu);
  9964. }
  9965. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  9966. struct vmcs12 *vmcs12)
  9967. {
  9968. u32 entry_failure_code;
  9969. nested_ept_uninit_mmu_context(vcpu);
  9970. /*
  9971. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  9972. * couldn't have changed.
  9973. */
  9974. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  9975. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  9976. if (!enable_ept)
  9977. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  9978. }
  9979. /*
  9980. * A part of what we need to when the nested L2 guest exits and we want to
  9981. * run its L1 parent, is to reset L1's guest state to the host state specified
  9982. * in vmcs12.
  9983. * This function is to be called not only on normal nested exit, but also on
  9984. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  9985. * Failures During or After Loading Guest State").
  9986. * This function should be called when the active VMCS is L1's (vmcs01).
  9987. */
  9988. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  9989. struct vmcs12 *vmcs12)
  9990. {
  9991. struct kvm_segment seg;
  9992. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  9993. vcpu->arch.efer = vmcs12->host_ia32_efer;
  9994. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  9995. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9996. else
  9997. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9998. vmx_set_efer(vcpu, vcpu->arch.efer);
  9999. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  10000. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  10001. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  10002. /*
  10003. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  10004. * actually changed, because vmx_set_cr0 refers to efer set above.
  10005. *
  10006. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  10007. * (KVM doesn't change it);
  10008. */
  10009. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  10010. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  10011. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  10012. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  10013. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  10014. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10015. if (enable_vpid) {
  10016. /*
  10017. * Trivially support vpid by letting L2s share their parent
  10018. * L1's vpid. TODO: move to a more elaborate solution, giving
  10019. * each L2 its own vpid and exposing the vpid feature to L1.
  10020. */
  10021. vmx_flush_tlb(vcpu, true);
  10022. }
  10023. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  10024. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  10025. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  10026. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  10027. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  10028. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  10029. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  10030. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  10031. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  10032. vmcs_write64(GUEST_BNDCFGS, 0);
  10033. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  10034. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  10035. vcpu->arch.pat = vmcs12->host_ia32_pat;
  10036. }
  10037. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  10038. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  10039. vmcs12->host_ia32_perf_global_ctrl);
  10040. /* Set L1 segment info according to Intel SDM
  10041. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  10042. seg = (struct kvm_segment) {
  10043. .base = 0,
  10044. .limit = 0xFFFFFFFF,
  10045. .selector = vmcs12->host_cs_selector,
  10046. .type = 11,
  10047. .present = 1,
  10048. .s = 1,
  10049. .g = 1
  10050. };
  10051. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10052. seg.l = 1;
  10053. else
  10054. seg.db = 1;
  10055. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  10056. seg = (struct kvm_segment) {
  10057. .base = 0,
  10058. .limit = 0xFFFFFFFF,
  10059. .type = 3,
  10060. .present = 1,
  10061. .s = 1,
  10062. .db = 1,
  10063. .g = 1
  10064. };
  10065. seg.selector = vmcs12->host_ds_selector;
  10066. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  10067. seg.selector = vmcs12->host_es_selector;
  10068. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  10069. seg.selector = vmcs12->host_ss_selector;
  10070. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  10071. seg.selector = vmcs12->host_fs_selector;
  10072. seg.base = vmcs12->host_fs_base;
  10073. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  10074. seg.selector = vmcs12->host_gs_selector;
  10075. seg.base = vmcs12->host_gs_base;
  10076. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  10077. seg = (struct kvm_segment) {
  10078. .base = vmcs12->host_tr_base,
  10079. .limit = 0x67,
  10080. .selector = vmcs12->host_tr_selector,
  10081. .type = 11,
  10082. .present = 1
  10083. };
  10084. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  10085. kvm_set_dr(vcpu, 7, 0x400);
  10086. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  10087. if (cpu_has_vmx_msr_bitmap())
  10088. vmx_update_msr_bitmap(vcpu);
  10089. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  10090. vmcs12->vm_exit_msr_load_count))
  10091. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  10092. }
  10093. /*
  10094. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  10095. * and modify vmcs12 to make it see what it would expect to see there if
  10096. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  10097. */
  10098. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  10099. u32 exit_intr_info,
  10100. unsigned long exit_qualification)
  10101. {
  10102. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10103. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10104. /* trying to cancel vmlaunch/vmresume is a bug */
  10105. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  10106. /*
  10107. * The only expected VM-instruction error is "VM entry with
  10108. * invalid control field(s)." Anything else indicates a
  10109. * problem with L0.
  10110. */
  10111. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  10112. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  10113. leave_guest_mode(vcpu);
  10114. if (likely(!vmx->fail)) {
  10115. if (exit_reason == -1)
  10116. sync_vmcs12(vcpu, vmcs12);
  10117. else
  10118. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  10119. exit_qualification);
  10120. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  10121. vmcs12->vm_exit_msr_store_count))
  10122. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  10123. }
  10124. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10125. vm_entry_controls_reset_shadow(vmx);
  10126. vm_exit_controls_reset_shadow(vmx);
  10127. vmx_segment_cache_clear(vmx);
  10128. /* Update any VMCS fields that might have changed while L2 ran */
  10129. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10130. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10131. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10132. if (vmx->hv_deadline_tsc == -1)
  10133. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10134. PIN_BASED_VMX_PREEMPTION_TIMER);
  10135. else
  10136. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10137. PIN_BASED_VMX_PREEMPTION_TIMER);
  10138. if (kvm_has_tsc_control)
  10139. decache_tsc_multiplier(vmx);
  10140. if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
  10141. vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
  10142. vmx_set_virtual_x2apic_mode(vcpu,
  10143. vcpu->arch.apic_base & X2APIC_ENABLE);
  10144. } else if (!nested_cpu_has_ept(vmcs12) &&
  10145. nested_cpu_has2(vmcs12,
  10146. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10147. vmx_flush_tlb_ept_only(vcpu);
  10148. }
  10149. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  10150. vmx->host_rsp = 0;
  10151. /* Unpin physical memory we referred to in vmcs02 */
  10152. if (vmx->nested.apic_access_page) {
  10153. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10154. vmx->nested.apic_access_page = NULL;
  10155. }
  10156. if (vmx->nested.virtual_apic_page) {
  10157. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10158. vmx->nested.virtual_apic_page = NULL;
  10159. }
  10160. if (vmx->nested.pi_desc_page) {
  10161. kunmap(vmx->nested.pi_desc_page);
  10162. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10163. vmx->nested.pi_desc_page = NULL;
  10164. vmx->nested.pi_desc = NULL;
  10165. }
  10166. /*
  10167. * We are now running in L2, mmu_notifier will force to reload the
  10168. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  10169. */
  10170. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  10171. if (enable_shadow_vmcs && exit_reason != -1)
  10172. vmx->nested.sync_shadow_vmcs = true;
  10173. /* in case we halted in L2 */
  10174. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  10175. if (likely(!vmx->fail)) {
  10176. /*
  10177. * TODO: SDM says that with acknowledge interrupt on
  10178. * exit, bit 31 of the VM-exit interrupt information
  10179. * (valid interrupt) is always set to 1 on
  10180. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  10181. * need kvm_cpu_has_interrupt(). See the commit
  10182. * message for details.
  10183. */
  10184. if (nested_exit_intr_ack_set(vcpu) &&
  10185. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  10186. kvm_cpu_has_interrupt(vcpu)) {
  10187. int irq = kvm_cpu_get_interrupt(vcpu);
  10188. WARN_ON(irq < 0);
  10189. vmcs12->vm_exit_intr_info = irq |
  10190. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  10191. }
  10192. if (exit_reason != -1)
  10193. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  10194. vmcs12->exit_qualification,
  10195. vmcs12->idt_vectoring_info_field,
  10196. vmcs12->vm_exit_intr_info,
  10197. vmcs12->vm_exit_intr_error_code,
  10198. KVM_ISA_VMX);
  10199. load_vmcs12_host_state(vcpu, vmcs12);
  10200. return;
  10201. }
  10202. /*
  10203. * After an early L2 VM-entry failure, we're now back
  10204. * in L1 which thinks it just finished a VMLAUNCH or
  10205. * VMRESUME instruction, so we need to set the failure
  10206. * flag and the VM-instruction error field of the VMCS
  10207. * accordingly.
  10208. */
  10209. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  10210. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10211. /*
  10212. * The emulated instruction was already skipped in
  10213. * nested_vmx_run, but the updated RIP was never
  10214. * written back to the vmcs01.
  10215. */
  10216. skip_emulated_instruction(vcpu);
  10217. vmx->fail = 0;
  10218. }
  10219. /*
  10220. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  10221. */
  10222. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  10223. {
  10224. if (is_guest_mode(vcpu)) {
  10225. to_vmx(vcpu)->nested.nested_run_pending = 0;
  10226. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10227. }
  10228. free_nested(to_vmx(vcpu));
  10229. }
  10230. /*
  10231. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  10232. * 23.7 "VM-entry failures during or after loading guest state" (this also
  10233. * lists the acceptable exit-reason and exit-qualification parameters).
  10234. * It should only be called before L2 actually succeeded to run, and when
  10235. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  10236. */
  10237. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  10238. struct vmcs12 *vmcs12,
  10239. u32 reason, unsigned long qualification)
  10240. {
  10241. load_vmcs12_host_state(vcpu, vmcs12);
  10242. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  10243. vmcs12->exit_qualification = qualification;
  10244. nested_vmx_succeed(vcpu);
  10245. if (enable_shadow_vmcs)
  10246. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  10247. }
  10248. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  10249. struct x86_instruction_info *info,
  10250. enum x86_intercept_stage stage)
  10251. {
  10252. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10253. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  10254. /*
  10255. * RDPID causes #UD if disabled through secondary execution controls.
  10256. * Because it is marked as EmulateOnUD, we need to intercept it here.
  10257. */
  10258. if (info->intercept == x86_intercept_rdtscp &&
  10259. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  10260. ctxt->exception.vector = UD_VECTOR;
  10261. ctxt->exception.error_code_valid = false;
  10262. return X86EMUL_PROPAGATE_FAULT;
  10263. }
  10264. /* TODO: check more intercepts... */
  10265. return X86EMUL_CONTINUE;
  10266. }
  10267. #ifdef CONFIG_X86_64
  10268. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  10269. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  10270. u64 divisor, u64 *result)
  10271. {
  10272. u64 low = a << shift, high = a >> (64 - shift);
  10273. /* To avoid the overflow on divq */
  10274. if (high >= divisor)
  10275. return 1;
  10276. /* Low hold the result, high hold rem which is discarded */
  10277. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  10278. "rm" (divisor), "0" (low), "1" (high));
  10279. *result = low;
  10280. return 0;
  10281. }
  10282. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  10283. {
  10284. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10285. u64 tscl = rdtsc();
  10286. u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  10287. u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  10288. /* Convert to host delta tsc if tsc scaling is enabled */
  10289. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  10290. u64_shl_div_u64(delta_tsc,
  10291. kvm_tsc_scaling_ratio_frac_bits,
  10292. vcpu->arch.tsc_scaling_ratio,
  10293. &delta_tsc))
  10294. return -ERANGE;
  10295. /*
  10296. * If the delta tsc can't fit in the 32 bit after the multi shift,
  10297. * we can't use the preemption timer.
  10298. * It's possible that it fits on later vmentries, but checking
  10299. * on every vmentry is costly so we just use an hrtimer.
  10300. */
  10301. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  10302. return -ERANGE;
  10303. vmx->hv_deadline_tsc = tscl + delta_tsc;
  10304. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10305. PIN_BASED_VMX_PREEMPTION_TIMER);
  10306. return delta_tsc == 0;
  10307. }
  10308. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  10309. {
  10310. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10311. vmx->hv_deadline_tsc = -1;
  10312. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10313. PIN_BASED_VMX_PREEMPTION_TIMER);
  10314. }
  10315. #endif
  10316. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  10317. {
  10318. if (ple_gap)
  10319. shrink_ple_window(vcpu);
  10320. }
  10321. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  10322. struct kvm_memory_slot *slot)
  10323. {
  10324. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  10325. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  10326. }
  10327. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  10328. struct kvm_memory_slot *slot)
  10329. {
  10330. kvm_mmu_slot_set_dirty(kvm, slot);
  10331. }
  10332. static void vmx_flush_log_dirty(struct kvm *kvm)
  10333. {
  10334. kvm_flush_pml_buffers(kvm);
  10335. }
  10336. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  10337. {
  10338. struct vmcs12 *vmcs12;
  10339. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10340. gpa_t gpa;
  10341. struct page *page = NULL;
  10342. u64 *pml_address;
  10343. if (is_guest_mode(vcpu)) {
  10344. WARN_ON_ONCE(vmx->nested.pml_full);
  10345. /*
  10346. * Check if PML is enabled for the nested guest.
  10347. * Whether eptp bit 6 is set is already checked
  10348. * as part of A/D emulation.
  10349. */
  10350. vmcs12 = get_vmcs12(vcpu);
  10351. if (!nested_cpu_has_pml(vmcs12))
  10352. return 0;
  10353. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  10354. vmx->nested.pml_full = true;
  10355. return 1;
  10356. }
  10357. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  10358. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  10359. if (is_error_page(page))
  10360. return 0;
  10361. pml_address = kmap(page);
  10362. pml_address[vmcs12->guest_pml_index--] = gpa;
  10363. kunmap(page);
  10364. kvm_release_page_clean(page);
  10365. }
  10366. return 0;
  10367. }
  10368. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  10369. struct kvm_memory_slot *memslot,
  10370. gfn_t offset, unsigned long mask)
  10371. {
  10372. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  10373. }
  10374. static void __pi_post_block(struct kvm_vcpu *vcpu)
  10375. {
  10376. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10377. struct pi_desc old, new;
  10378. unsigned int dest;
  10379. do {
  10380. old.control = new.control = pi_desc->control;
  10381. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  10382. "Wakeup handler not enabled while the VCPU is blocked\n");
  10383. dest = cpu_physical_id(vcpu->cpu);
  10384. if (x2apic_enabled())
  10385. new.ndst = dest;
  10386. else
  10387. new.ndst = (dest << 8) & 0xFF00;
  10388. /* set 'NV' to 'notification vector' */
  10389. new.nv = POSTED_INTR_VECTOR;
  10390. } while (cmpxchg64(&pi_desc->control, old.control,
  10391. new.control) != old.control);
  10392. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  10393. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10394. list_del(&vcpu->blocked_vcpu_list);
  10395. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10396. vcpu->pre_pcpu = -1;
  10397. }
  10398. }
  10399. /*
  10400. * This routine does the following things for vCPU which is going
  10401. * to be blocked if VT-d PI is enabled.
  10402. * - Store the vCPU to the wakeup list, so when interrupts happen
  10403. * we can find the right vCPU to wake up.
  10404. * - Change the Posted-interrupt descriptor as below:
  10405. * 'NDST' <-- vcpu->pre_pcpu
  10406. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  10407. * - If 'ON' is set during this process, which means at least one
  10408. * interrupt is posted for this vCPU, we cannot block it, in
  10409. * this case, return 1, otherwise, return 0.
  10410. *
  10411. */
  10412. static int pi_pre_block(struct kvm_vcpu *vcpu)
  10413. {
  10414. unsigned int dest;
  10415. struct pi_desc old, new;
  10416. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10417. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  10418. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  10419. !kvm_vcpu_apicv_active(vcpu))
  10420. return 0;
  10421. WARN_ON(irqs_disabled());
  10422. local_irq_disable();
  10423. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  10424. vcpu->pre_pcpu = vcpu->cpu;
  10425. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10426. list_add_tail(&vcpu->blocked_vcpu_list,
  10427. &per_cpu(blocked_vcpu_on_cpu,
  10428. vcpu->pre_pcpu));
  10429. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10430. }
  10431. do {
  10432. old.control = new.control = pi_desc->control;
  10433. WARN((pi_desc->sn == 1),
  10434. "Warning: SN field of posted-interrupts "
  10435. "is set before blocking\n");
  10436. /*
  10437. * Since vCPU can be preempted during this process,
  10438. * vcpu->cpu could be different with pre_pcpu, we
  10439. * need to set pre_pcpu as the destination of wakeup
  10440. * notification event, then we can find the right vCPU
  10441. * to wakeup in wakeup handler if interrupts happen
  10442. * when the vCPU is in blocked state.
  10443. */
  10444. dest = cpu_physical_id(vcpu->pre_pcpu);
  10445. if (x2apic_enabled())
  10446. new.ndst = dest;
  10447. else
  10448. new.ndst = (dest << 8) & 0xFF00;
  10449. /* set 'NV' to 'wakeup vector' */
  10450. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  10451. } while (cmpxchg64(&pi_desc->control, old.control,
  10452. new.control) != old.control);
  10453. /* We should not block the vCPU if an interrupt is posted for it. */
  10454. if (pi_test_on(pi_desc) == 1)
  10455. __pi_post_block(vcpu);
  10456. local_irq_enable();
  10457. return (vcpu->pre_pcpu == -1);
  10458. }
  10459. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  10460. {
  10461. if (pi_pre_block(vcpu))
  10462. return 1;
  10463. if (kvm_lapic_hv_timer_in_use(vcpu))
  10464. kvm_lapic_switch_to_sw_timer(vcpu);
  10465. return 0;
  10466. }
  10467. static void pi_post_block(struct kvm_vcpu *vcpu)
  10468. {
  10469. if (vcpu->pre_pcpu == -1)
  10470. return;
  10471. WARN_ON(irqs_disabled());
  10472. local_irq_disable();
  10473. __pi_post_block(vcpu);
  10474. local_irq_enable();
  10475. }
  10476. static void vmx_post_block(struct kvm_vcpu *vcpu)
  10477. {
  10478. if (kvm_x86_ops->set_hv_timer)
  10479. kvm_lapic_switch_to_hv_timer(vcpu);
  10480. pi_post_block(vcpu);
  10481. }
  10482. /*
  10483. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  10484. *
  10485. * @kvm: kvm
  10486. * @host_irq: host irq of the interrupt
  10487. * @guest_irq: gsi of the interrupt
  10488. * @set: set or unset PI
  10489. * returns 0 on success, < 0 on failure
  10490. */
  10491. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  10492. uint32_t guest_irq, bool set)
  10493. {
  10494. struct kvm_kernel_irq_routing_entry *e;
  10495. struct kvm_irq_routing_table *irq_rt;
  10496. struct kvm_lapic_irq irq;
  10497. struct kvm_vcpu *vcpu;
  10498. struct vcpu_data vcpu_info;
  10499. int idx, ret = 0;
  10500. if (!kvm_arch_has_assigned_device(kvm) ||
  10501. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  10502. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  10503. return 0;
  10504. idx = srcu_read_lock(&kvm->irq_srcu);
  10505. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  10506. if (guest_irq >= irq_rt->nr_rt_entries ||
  10507. hlist_empty(&irq_rt->map[guest_irq])) {
  10508. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  10509. guest_irq, irq_rt->nr_rt_entries);
  10510. goto out;
  10511. }
  10512. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  10513. if (e->type != KVM_IRQ_ROUTING_MSI)
  10514. continue;
  10515. /*
  10516. * VT-d PI cannot support posting multicast/broadcast
  10517. * interrupts to a vCPU, we still use interrupt remapping
  10518. * for these kind of interrupts.
  10519. *
  10520. * For lowest-priority interrupts, we only support
  10521. * those with single CPU as the destination, e.g. user
  10522. * configures the interrupts via /proc/irq or uses
  10523. * irqbalance to make the interrupts single-CPU.
  10524. *
  10525. * We will support full lowest-priority interrupt later.
  10526. */
  10527. kvm_set_msi_irq(kvm, e, &irq);
  10528. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  10529. /*
  10530. * Make sure the IRTE is in remapped mode if
  10531. * we don't handle it in posted mode.
  10532. */
  10533. ret = irq_set_vcpu_affinity(host_irq, NULL);
  10534. if (ret < 0) {
  10535. printk(KERN_INFO
  10536. "failed to back to remapped mode, irq: %u\n",
  10537. host_irq);
  10538. goto out;
  10539. }
  10540. continue;
  10541. }
  10542. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  10543. vcpu_info.vector = irq.vector;
  10544. trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
  10545. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  10546. if (set)
  10547. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  10548. else
  10549. ret = irq_set_vcpu_affinity(host_irq, NULL);
  10550. if (ret < 0) {
  10551. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  10552. __func__);
  10553. goto out;
  10554. }
  10555. }
  10556. ret = 0;
  10557. out:
  10558. srcu_read_unlock(&kvm->irq_srcu, idx);
  10559. return ret;
  10560. }
  10561. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  10562. {
  10563. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  10564. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  10565. FEATURE_CONTROL_LMCE;
  10566. else
  10567. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  10568. ~FEATURE_CONTROL_LMCE;
  10569. }
  10570. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  10571. {
  10572. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  10573. if (to_vmx(vcpu)->nested.nested_run_pending)
  10574. return 0;
  10575. return 1;
  10576. }
  10577. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  10578. {
  10579. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10580. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  10581. if (vmx->nested.smm.guest_mode)
  10582. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10583. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  10584. vmx->nested.vmxon = false;
  10585. return 0;
  10586. }
  10587. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  10588. {
  10589. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10590. int ret;
  10591. if (vmx->nested.smm.vmxon) {
  10592. vmx->nested.vmxon = true;
  10593. vmx->nested.smm.vmxon = false;
  10594. }
  10595. if (vmx->nested.smm.guest_mode) {
  10596. vcpu->arch.hflags &= ~HF_SMM_MASK;
  10597. ret = enter_vmx_non_root_mode(vcpu, false);
  10598. vcpu->arch.hflags |= HF_SMM_MASK;
  10599. if (ret)
  10600. return ret;
  10601. vmx->nested.smm.guest_mode = false;
  10602. }
  10603. return 0;
  10604. }
  10605. static int enable_smi_window(struct kvm_vcpu *vcpu)
  10606. {
  10607. return 0;
  10608. }
  10609. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  10610. .cpu_has_kvm_support = cpu_has_kvm_support,
  10611. .disabled_by_bios = vmx_disabled_by_bios,
  10612. .hardware_setup = hardware_setup,
  10613. .hardware_unsetup = hardware_unsetup,
  10614. .check_processor_compatibility = vmx_check_processor_compat,
  10615. .hardware_enable = hardware_enable,
  10616. .hardware_disable = hardware_disable,
  10617. .cpu_has_accelerated_tpr = report_flexpriority,
  10618. .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
  10619. .vcpu_create = vmx_create_vcpu,
  10620. .vcpu_free = vmx_free_vcpu,
  10621. .vcpu_reset = vmx_vcpu_reset,
  10622. .prepare_guest_switch = vmx_save_host_state,
  10623. .vcpu_load = vmx_vcpu_load,
  10624. .vcpu_put = vmx_vcpu_put,
  10625. .update_bp_intercept = update_exception_bitmap,
  10626. .get_msr_feature = vmx_get_msr_feature,
  10627. .get_msr = vmx_get_msr,
  10628. .set_msr = vmx_set_msr,
  10629. .get_segment_base = vmx_get_segment_base,
  10630. .get_segment = vmx_get_segment,
  10631. .set_segment = vmx_set_segment,
  10632. .get_cpl = vmx_get_cpl,
  10633. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  10634. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  10635. .decache_cr3 = vmx_decache_cr3,
  10636. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  10637. .set_cr0 = vmx_set_cr0,
  10638. .set_cr3 = vmx_set_cr3,
  10639. .set_cr4 = vmx_set_cr4,
  10640. .set_efer = vmx_set_efer,
  10641. .get_idt = vmx_get_idt,
  10642. .set_idt = vmx_set_idt,
  10643. .get_gdt = vmx_get_gdt,
  10644. .set_gdt = vmx_set_gdt,
  10645. .get_dr6 = vmx_get_dr6,
  10646. .set_dr6 = vmx_set_dr6,
  10647. .set_dr7 = vmx_set_dr7,
  10648. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  10649. .cache_reg = vmx_cache_reg,
  10650. .get_rflags = vmx_get_rflags,
  10651. .set_rflags = vmx_set_rflags,
  10652. .tlb_flush = vmx_flush_tlb,
  10653. .run = vmx_vcpu_run,
  10654. .handle_exit = vmx_handle_exit,
  10655. .skip_emulated_instruction = skip_emulated_instruction,
  10656. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  10657. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  10658. .patch_hypercall = vmx_patch_hypercall,
  10659. .set_irq = vmx_inject_irq,
  10660. .set_nmi = vmx_inject_nmi,
  10661. .queue_exception = vmx_queue_exception,
  10662. .cancel_injection = vmx_cancel_injection,
  10663. .interrupt_allowed = vmx_interrupt_allowed,
  10664. .nmi_allowed = vmx_nmi_allowed,
  10665. .get_nmi_mask = vmx_get_nmi_mask,
  10666. .set_nmi_mask = vmx_set_nmi_mask,
  10667. .enable_nmi_window = enable_nmi_window,
  10668. .enable_irq_window = enable_irq_window,
  10669. .update_cr8_intercept = update_cr8_intercept,
  10670. .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
  10671. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  10672. .get_enable_apicv = vmx_get_enable_apicv,
  10673. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  10674. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  10675. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  10676. .hwapic_irr_update = vmx_hwapic_irr_update,
  10677. .hwapic_isr_update = vmx_hwapic_isr_update,
  10678. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  10679. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  10680. .set_tss_addr = vmx_set_tss_addr,
  10681. .get_tdp_level = get_ept_level,
  10682. .get_mt_mask = vmx_get_mt_mask,
  10683. .get_exit_info = vmx_get_exit_info,
  10684. .get_lpage_level = vmx_get_lpage_level,
  10685. .cpuid_update = vmx_cpuid_update,
  10686. .rdtscp_supported = vmx_rdtscp_supported,
  10687. .invpcid_supported = vmx_invpcid_supported,
  10688. .set_supported_cpuid = vmx_set_supported_cpuid,
  10689. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  10690. .write_tsc_offset = vmx_write_tsc_offset,
  10691. .set_tdp_cr3 = vmx_set_cr3,
  10692. .check_intercept = vmx_check_intercept,
  10693. .handle_external_intr = vmx_handle_external_intr,
  10694. .mpx_supported = vmx_mpx_supported,
  10695. .xsaves_supported = vmx_xsaves_supported,
  10696. .umip_emulated = vmx_umip_emulated,
  10697. .check_nested_events = vmx_check_nested_events,
  10698. .sched_in = vmx_sched_in,
  10699. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  10700. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  10701. .flush_log_dirty = vmx_flush_log_dirty,
  10702. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  10703. .write_log_dirty = vmx_write_pml_buffer,
  10704. .pre_block = vmx_pre_block,
  10705. .post_block = vmx_post_block,
  10706. .pmu_ops = &intel_pmu_ops,
  10707. .update_pi_irte = vmx_update_pi_irte,
  10708. #ifdef CONFIG_X86_64
  10709. .set_hv_timer = vmx_set_hv_timer,
  10710. .cancel_hv_timer = vmx_cancel_hv_timer,
  10711. #endif
  10712. .setup_mce = vmx_setup_mce,
  10713. .smi_allowed = vmx_smi_allowed,
  10714. .pre_enter_smm = vmx_pre_enter_smm,
  10715. .pre_leave_smm = vmx_pre_leave_smm,
  10716. .enable_smi_window = enable_smi_window,
  10717. };
  10718. static int __init vmx_init(void)
  10719. {
  10720. int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  10721. __alignof__(struct vcpu_vmx), THIS_MODULE);
  10722. if (r)
  10723. return r;
  10724. #ifdef CONFIG_KEXEC_CORE
  10725. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  10726. crash_vmclear_local_loaded_vmcss);
  10727. #endif
  10728. return 0;
  10729. }
  10730. static void __exit vmx_exit(void)
  10731. {
  10732. #ifdef CONFIG_KEXEC_CORE
  10733. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  10734. synchronize_rcu();
  10735. #endif
  10736. kvm_exit();
  10737. }
  10738. module_init(vmx_init)
  10739. module_exit(vmx_exit)