vmx.c 288 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286
  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 <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/ftrace_event.h>
  30. #include <linux/slab.h>
  31. #include <linux/tboot.h>
  32. #include <linux/hrtimer.h>
  33. #include "kvm_cache_regs.h"
  34. #include "x86.h"
  35. #include <asm/io.h>
  36. #include <asm/desc.h>
  37. #include <asm/vmx.h>
  38. #include <asm/virtext.h>
  39. #include <asm/mce.h>
  40. #include <asm/i387.h>
  41. #include <asm/xcr.h>
  42. #include <asm/perf_event.h>
  43. #include <asm/debugreg.h>
  44. #include <asm/kexec.h>
  45. #include <asm/apic.h>
  46. #include "trace.h"
  47. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  48. #define __ex_clear(x, reg) \
  49. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  50. MODULE_AUTHOR("Qumranet");
  51. MODULE_LICENSE("GPL");
  52. static const struct x86_cpu_id vmx_cpu_id[] = {
  53. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  54. {}
  55. };
  56. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  57. static bool __read_mostly enable_vpid = 1;
  58. module_param_named(vpid, enable_vpid, bool, 0444);
  59. static bool __read_mostly flexpriority_enabled = 1;
  60. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  61. static bool __read_mostly enable_ept = 1;
  62. module_param_named(ept, enable_ept, bool, S_IRUGO);
  63. static bool __read_mostly enable_unrestricted_guest = 1;
  64. module_param_named(unrestricted_guest,
  65. enable_unrestricted_guest, bool, S_IRUGO);
  66. static bool __read_mostly enable_ept_ad_bits = 1;
  67. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  68. static bool __read_mostly emulate_invalid_guest_state = true;
  69. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  70. static bool __read_mostly vmm_exclusive = 1;
  71. module_param(vmm_exclusive, bool, S_IRUGO);
  72. static bool __read_mostly fasteoi = 1;
  73. module_param(fasteoi, bool, S_IRUGO);
  74. static bool __read_mostly enable_apicv = 1;
  75. module_param(enable_apicv, bool, S_IRUGO);
  76. static bool __read_mostly enable_shadow_vmcs = 1;
  77. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  78. /*
  79. * If nested=1, nested virtualization is supported, i.e., guests may use
  80. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  81. * use VMX instructions.
  82. */
  83. static bool __read_mostly nested = 0;
  84. module_param(nested, bool, S_IRUGO);
  85. static u64 __read_mostly host_xss;
  86. static bool __read_mostly enable_pml = 1;
  87. module_param_named(pml, enable_pml, bool, S_IRUGO);
  88. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  89. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
  90. #define KVM_VM_CR0_ALWAYS_ON \
  91. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  92. #define KVM_CR4_GUEST_OWNED_BITS \
  93. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  94. | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
  95. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  96. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  97. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  98. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  99. /*
  100. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  101. * ple_gap: upper bound on the amount of time between two successive
  102. * executions of PAUSE in a loop. Also indicate if ple enabled.
  103. * According to test, this time is usually smaller than 128 cycles.
  104. * ple_window: upper bound on the amount of time a guest is allowed to execute
  105. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  106. * less than 2^12 cycles
  107. * Time is measured based on a counter that runs at the same rate as the TSC,
  108. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  109. */
  110. #define KVM_VMX_DEFAULT_PLE_GAP 128
  111. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  112. #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
  113. #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
  114. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
  115. INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
  116. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  117. module_param(ple_gap, int, S_IRUGO);
  118. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  119. module_param(ple_window, int, S_IRUGO);
  120. /* Default doubles per-vcpu window every exit. */
  121. static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
  122. module_param(ple_window_grow, int, S_IRUGO);
  123. /* Default resets per-vcpu window every exit to ple_window. */
  124. static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
  125. module_param(ple_window_shrink, int, S_IRUGO);
  126. /* Default is to compute the maximum so we can never overflow. */
  127. static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  128. static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  129. module_param(ple_window_max, int, S_IRUGO);
  130. extern const ulong vmx_return;
  131. #define NR_AUTOLOAD_MSRS 8
  132. #define VMCS02_POOL_SIZE 1
  133. struct vmcs {
  134. u32 revision_id;
  135. u32 abort;
  136. char data[0];
  137. };
  138. /*
  139. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  140. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  141. * loaded on this CPU (so we can clear them if the CPU goes down).
  142. */
  143. struct loaded_vmcs {
  144. struct vmcs *vmcs;
  145. int cpu;
  146. int launched;
  147. struct list_head loaded_vmcss_on_cpu_link;
  148. };
  149. struct shared_msr_entry {
  150. unsigned index;
  151. u64 data;
  152. u64 mask;
  153. };
  154. /*
  155. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  156. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  157. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  158. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  159. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  160. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  161. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  162. * underlying hardware which will be used to run L2.
  163. * This structure is packed to ensure that its layout is identical across
  164. * machines (necessary for live migration).
  165. * If there are changes in this struct, VMCS12_REVISION must be changed.
  166. */
  167. typedef u64 natural_width;
  168. struct __packed vmcs12 {
  169. /* According to the Intel spec, a VMCS region must start with the
  170. * following two fields. Then follow implementation-specific data.
  171. */
  172. u32 revision_id;
  173. u32 abort;
  174. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  175. u32 padding[7]; /* room for future expansion */
  176. u64 io_bitmap_a;
  177. u64 io_bitmap_b;
  178. u64 msr_bitmap;
  179. u64 vm_exit_msr_store_addr;
  180. u64 vm_exit_msr_load_addr;
  181. u64 vm_entry_msr_load_addr;
  182. u64 tsc_offset;
  183. u64 virtual_apic_page_addr;
  184. u64 apic_access_addr;
  185. u64 posted_intr_desc_addr;
  186. u64 ept_pointer;
  187. u64 eoi_exit_bitmap0;
  188. u64 eoi_exit_bitmap1;
  189. u64 eoi_exit_bitmap2;
  190. u64 eoi_exit_bitmap3;
  191. u64 xss_exit_bitmap;
  192. u64 guest_physical_address;
  193. u64 vmcs_link_pointer;
  194. u64 guest_ia32_debugctl;
  195. u64 guest_ia32_pat;
  196. u64 guest_ia32_efer;
  197. u64 guest_ia32_perf_global_ctrl;
  198. u64 guest_pdptr0;
  199. u64 guest_pdptr1;
  200. u64 guest_pdptr2;
  201. u64 guest_pdptr3;
  202. u64 guest_bndcfgs;
  203. u64 host_ia32_pat;
  204. u64 host_ia32_efer;
  205. u64 host_ia32_perf_global_ctrl;
  206. u64 padding64[8]; /* room for future expansion */
  207. /*
  208. * To allow migration of L1 (complete with its L2 guests) between
  209. * machines of different natural widths (32 or 64 bit), we cannot have
  210. * unsigned long fields with no explict size. We use u64 (aliased
  211. * natural_width) instead. Luckily, x86 is little-endian.
  212. */
  213. natural_width cr0_guest_host_mask;
  214. natural_width cr4_guest_host_mask;
  215. natural_width cr0_read_shadow;
  216. natural_width cr4_read_shadow;
  217. natural_width cr3_target_value0;
  218. natural_width cr3_target_value1;
  219. natural_width cr3_target_value2;
  220. natural_width cr3_target_value3;
  221. natural_width exit_qualification;
  222. natural_width guest_linear_address;
  223. natural_width guest_cr0;
  224. natural_width guest_cr3;
  225. natural_width guest_cr4;
  226. natural_width guest_es_base;
  227. natural_width guest_cs_base;
  228. natural_width guest_ss_base;
  229. natural_width guest_ds_base;
  230. natural_width guest_fs_base;
  231. natural_width guest_gs_base;
  232. natural_width guest_ldtr_base;
  233. natural_width guest_tr_base;
  234. natural_width guest_gdtr_base;
  235. natural_width guest_idtr_base;
  236. natural_width guest_dr7;
  237. natural_width guest_rsp;
  238. natural_width guest_rip;
  239. natural_width guest_rflags;
  240. natural_width guest_pending_dbg_exceptions;
  241. natural_width guest_sysenter_esp;
  242. natural_width guest_sysenter_eip;
  243. natural_width host_cr0;
  244. natural_width host_cr3;
  245. natural_width host_cr4;
  246. natural_width host_fs_base;
  247. natural_width host_gs_base;
  248. natural_width host_tr_base;
  249. natural_width host_gdtr_base;
  250. natural_width host_idtr_base;
  251. natural_width host_ia32_sysenter_esp;
  252. natural_width host_ia32_sysenter_eip;
  253. natural_width host_rsp;
  254. natural_width host_rip;
  255. natural_width paddingl[8]; /* room for future expansion */
  256. u32 pin_based_vm_exec_control;
  257. u32 cpu_based_vm_exec_control;
  258. u32 exception_bitmap;
  259. u32 page_fault_error_code_mask;
  260. u32 page_fault_error_code_match;
  261. u32 cr3_target_count;
  262. u32 vm_exit_controls;
  263. u32 vm_exit_msr_store_count;
  264. u32 vm_exit_msr_load_count;
  265. u32 vm_entry_controls;
  266. u32 vm_entry_msr_load_count;
  267. u32 vm_entry_intr_info_field;
  268. u32 vm_entry_exception_error_code;
  269. u32 vm_entry_instruction_len;
  270. u32 tpr_threshold;
  271. u32 secondary_vm_exec_control;
  272. u32 vm_instruction_error;
  273. u32 vm_exit_reason;
  274. u32 vm_exit_intr_info;
  275. u32 vm_exit_intr_error_code;
  276. u32 idt_vectoring_info_field;
  277. u32 idt_vectoring_error_code;
  278. u32 vm_exit_instruction_len;
  279. u32 vmx_instruction_info;
  280. u32 guest_es_limit;
  281. u32 guest_cs_limit;
  282. u32 guest_ss_limit;
  283. u32 guest_ds_limit;
  284. u32 guest_fs_limit;
  285. u32 guest_gs_limit;
  286. u32 guest_ldtr_limit;
  287. u32 guest_tr_limit;
  288. u32 guest_gdtr_limit;
  289. u32 guest_idtr_limit;
  290. u32 guest_es_ar_bytes;
  291. u32 guest_cs_ar_bytes;
  292. u32 guest_ss_ar_bytes;
  293. u32 guest_ds_ar_bytes;
  294. u32 guest_fs_ar_bytes;
  295. u32 guest_gs_ar_bytes;
  296. u32 guest_ldtr_ar_bytes;
  297. u32 guest_tr_ar_bytes;
  298. u32 guest_interruptibility_info;
  299. u32 guest_activity_state;
  300. u32 guest_sysenter_cs;
  301. u32 host_ia32_sysenter_cs;
  302. u32 vmx_preemption_timer_value;
  303. u32 padding32[7]; /* room for future expansion */
  304. u16 virtual_processor_id;
  305. u16 posted_intr_nv;
  306. u16 guest_es_selector;
  307. u16 guest_cs_selector;
  308. u16 guest_ss_selector;
  309. u16 guest_ds_selector;
  310. u16 guest_fs_selector;
  311. u16 guest_gs_selector;
  312. u16 guest_ldtr_selector;
  313. u16 guest_tr_selector;
  314. u16 guest_intr_status;
  315. u16 host_es_selector;
  316. u16 host_cs_selector;
  317. u16 host_ss_selector;
  318. u16 host_ds_selector;
  319. u16 host_fs_selector;
  320. u16 host_gs_selector;
  321. u16 host_tr_selector;
  322. };
  323. /*
  324. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  325. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  326. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  327. */
  328. #define VMCS12_REVISION 0x11e57ed0
  329. /*
  330. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  331. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  332. * current implementation, 4K are reserved to avoid future complications.
  333. */
  334. #define VMCS12_SIZE 0x1000
  335. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  336. struct vmcs02_list {
  337. struct list_head list;
  338. gpa_t vmptr;
  339. struct loaded_vmcs vmcs02;
  340. };
  341. /*
  342. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  343. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  344. */
  345. struct nested_vmx {
  346. /* Has the level1 guest done vmxon? */
  347. bool vmxon;
  348. gpa_t vmxon_ptr;
  349. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  350. gpa_t current_vmptr;
  351. /* The host-usable pointer to the above */
  352. struct page *current_vmcs12_page;
  353. struct vmcs12 *current_vmcs12;
  354. struct vmcs *current_shadow_vmcs;
  355. /*
  356. * Indicates if the shadow vmcs must be updated with the
  357. * data hold by vmcs12
  358. */
  359. bool sync_shadow_vmcs;
  360. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  361. struct list_head vmcs02_pool;
  362. int vmcs02_num;
  363. u64 vmcs01_tsc_offset;
  364. /* L2 must run next, and mustn't decide to exit to L1. */
  365. bool nested_run_pending;
  366. /*
  367. * Guest pages referred to in vmcs02 with host-physical pointers, so
  368. * we must keep them pinned while L2 runs.
  369. */
  370. struct page *apic_access_page;
  371. struct page *virtual_apic_page;
  372. struct page *pi_desc_page;
  373. struct pi_desc *pi_desc;
  374. bool pi_pending;
  375. u16 posted_intr_nv;
  376. u64 msr_ia32_feature_control;
  377. struct hrtimer preemption_timer;
  378. bool preemption_timer_expired;
  379. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  380. u64 vmcs01_debugctl;
  381. u32 nested_vmx_procbased_ctls_low;
  382. u32 nested_vmx_procbased_ctls_high;
  383. u32 nested_vmx_true_procbased_ctls_low;
  384. u32 nested_vmx_secondary_ctls_low;
  385. u32 nested_vmx_secondary_ctls_high;
  386. u32 nested_vmx_pinbased_ctls_low;
  387. u32 nested_vmx_pinbased_ctls_high;
  388. u32 nested_vmx_exit_ctls_low;
  389. u32 nested_vmx_exit_ctls_high;
  390. u32 nested_vmx_true_exit_ctls_low;
  391. u32 nested_vmx_entry_ctls_low;
  392. u32 nested_vmx_entry_ctls_high;
  393. u32 nested_vmx_true_entry_ctls_low;
  394. u32 nested_vmx_misc_low;
  395. u32 nested_vmx_misc_high;
  396. u32 nested_vmx_ept_caps;
  397. };
  398. #define POSTED_INTR_ON 0
  399. /* Posted-Interrupt Descriptor */
  400. struct pi_desc {
  401. u32 pir[8]; /* Posted interrupt requested */
  402. u32 control; /* bit 0 of control is outstanding notification bit */
  403. u32 rsvd[7];
  404. } __aligned(64);
  405. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  406. {
  407. return test_and_set_bit(POSTED_INTR_ON,
  408. (unsigned long *)&pi_desc->control);
  409. }
  410. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  411. {
  412. return test_and_clear_bit(POSTED_INTR_ON,
  413. (unsigned long *)&pi_desc->control);
  414. }
  415. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  416. {
  417. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  418. }
  419. struct vcpu_vmx {
  420. struct kvm_vcpu vcpu;
  421. unsigned long host_rsp;
  422. u8 fail;
  423. bool nmi_known_unmasked;
  424. u32 exit_intr_info;
  425. u32 idt_vectoring_info;
  426. ulong rflags;
  427. struct shared_msr_entry *guest_msrs;
  428. int nmsrs;
  429. int save_nmsrs;
  430. unsigned long host_idt_base;
  431. #ifdef CONFIG_X86_64
  432. u64 msr_host_kernel_gs_base;
  433. u64 msr_guest_kernel_gs_base;
  434. #endif
  435. u32 vm_entry_controls_shadow;
  436. u32 vm_exit_controls_shadow;
  437. /*
  438. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  439. * non-nested (L1) guest, it always points to vmcs01. For a nested
  440. * guest (L2), it points to a different VMCS.
  441. */
  442. struct loaded_vmcs vmcs01;
  443. struct loaded_vmcs *loaded_vmcs;
  444. bool __launched; /* temporary, used in vmx_vcpu_run */
  445. struct msr_autoload {
  446. unsigned nr;
  447. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  448. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  449. } msr_autoload;
  450. struct {
  451. int loaded;
  452. u16 fs_sel, gs_sel, ldt_sel;
  453. #ifdef CONFIG_X86_64
  454. u16 ds_sel, es_sel;
  455. #endif
  456. int gs_ldt_reload_needed;
  457. int fs_reload_needed;
  458. u64 msr_host_bndcfgs;
  459. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  460. } host_state;
  461. struct {
  462. int vm86_active;
  463. ulong save_rflags;
  464. struct kvm_segment segs[8];
  465. } rmode;
  466. struct {
  467. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  468. struct kvm_save_segment {
  469. u16 selector;
  470. unsigned long base;
  471. u32 limit;
  472. u32 ar;
  473. } seg[8];
  474. } segment_cache;
  475. int vpid;
  476. bool emulation_required;
  477. /* Support for vnmi-less CPUs */
  478. int soft_vnmi_blocked;
  479. ktime_t entry_time;
  480. s64 vnmi_blocked_time;
  481. u32 exit_reason;
  482. bool rdtscp_enabled;
  483. /* Posted interrupt descriptor */
  484. struct pi_desc pi_desc;
  485. /* Support for a guest hypervisor (nested VMX) */
  486. struct nested_vmx nested;
  487. /* Dynamic PLE window. */
  488. int ple_window;
  489. bool ple_window_dirty;
  490. /* Support for PML */
  491. #define PML_ENTITY_NUM 512
  492. struct page *pml_pg;
  493. };
  494. enum segment_cache_field {
  495. SEG_FIELD_SEL = 0,
  496. SEG_FIELD_BASE = 1,
  497. SEG_FIELD_LIMIT = 2,
  498. SEG_FIELD_AR = 3,
  499. SEG_FIELD_NR = 4
  500. };
  501. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  502. {
  503. return container_of(vcpu, struct vcpu_vmx, vcpu);
  504. }
  505. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  506. #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
  507. #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
  508. [number##_HIGH] = VMCS12_OFFSET(name)+4
  509. static unsigned long shadow_read_only_fields[] = {
  510. /*
  511. * We do NOT shadow fields that are modified when L0
  512. * traps and emulates any vmx instruction (e.g. VMPTRLD,
  513. * VMXON...) executed by L1.
  514. * For example, VM_INSTRUCTION_ERROR is read
  515. * by L1 if a vmx instruction fails (part of the error path).
  516. * Note the code assumes this logic. If for some reason
  517. * we start shadowing these fields then we need to
  518. * force a shadow sync when L0 emulates vmx instructions
  519. * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
  520. * by nested_vmx_failValid)
  521. */
  522. VM_EXIT_REASON,
  523. VM_EXIT_INTR_INFO,
  524. VM_EXIT_INSTRUCTION_LEN,
  525. IDT_VECTORING_INFO_FIELD,
  526. IDT_VECTORING_ERROR_CODE,
  527. VM_EXIT_INTR_ERROR_CODE,
  528. EXIT_QUALIFICATION,
  529. GUEST_LINEAR_ADDRESS,
  530. GUEST_PHYSICAL_ADDRESS
  531. };
  532. static int max_shadow_read_only_fields =
  533. ARRAY_SIZE(shadow_read_only_fields);
  534. static unsigned long shadow_read_write_fields[] = {
  535. TPR_THRESHOLD,
  536. GUEST_RIP,
  537. GUEST_RSP,
  538. GUEST_CR0,
  539. GUEST_CR3,
  540. GUEST_CR4,
  541. GUEST_INTERRUPTIBILITY_INFO,
  542. GUEST_RFLAGS,
  543. GUEST_CS_SELECTOR,
  544. GUEST_CS_AR_BYTES,
  545. GUEST_CS_LIMIT,
  546. GUEST_CS_BASE,
  547. GUEST_ES_BASE,
  548. GUEST_BNDCFGS,
  549. CR0_GUEST_HOST_MASK,
  550. CR0_READ_SHADOW,
  551. CR4_READ_SHADOW,
  552. TSC_OFFSET,
  553. EXCEPTION_BITMAP,
  554. CPU_BASED_VM_EXEC_CONTROL,
  555. VM_ENTRY_EXCEPTION_ERROR_CODE,
  556. VM_ENTRY_INTR_INFO_FIELD,
  557. VM_ENTRY_INSTRUCTION_LEN,
  558. VM_ENTRY_EXCEPTION_ERROR_CODE,
  559. HOST_FS_BASE,
  560. HOST_GS_BASE,
  561. HOST_FS_SELECTOR,
  562. HOST_GS_SELECTOR
  563. };
  564. static int max_shadow_read_write_fields =
  565. ARRAY_SIZE(shadow_read_write_fields);
  566. static const unsigned short vmcs_field_to_offset_table[] = {
  567. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  568. FIELD(POSTED_INTR_NV, posted_intr_nv),
  569. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  570. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  571. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  572. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  573. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  574. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  575. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  576. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  577. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  578. FIELD(HOST_ES_SELECTOR, host_es_selector),
  579. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  580. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  581. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  582. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  583. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  584. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  585. FIELD64(IO_BITMAP_A, io_bitmap_a),
  586. FIELD64(IO_BITMAP_B, io_bitmap_b),
  587. FIELD64(MSR_BITMAP, msr_bitmap),
  588. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  589. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  590. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  591. FIELD64(TSC_OFFSET, tsc_offset),
  592. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  593. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  594. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  595. FIELD64(EPT_POINTER, ept_pointer),
  596. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  597. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  598. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  599. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  600. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  601. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  602. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  603. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  604. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  605. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  606. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  607. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  608. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  609. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  610. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  611. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  612. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  613. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  614. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  615. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  616. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  617. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  618. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  619. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  620. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  621. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  622. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  623. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  624. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  625. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  626. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  627. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  628. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  629. FIELD(TPR_THRESHOLD, tpr_threshold),
  630. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  631. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  632. FIELD(VM_EXIT_REASON, vm_exit_reason),
  633. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  634. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  635. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  636. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  637. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  638. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  639. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  640. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  641. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  642. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  643. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  644. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  645. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  646. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  647. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  648. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  649. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  650. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  651. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  652. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  653. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  654. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  655. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  656. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  657. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  658. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  659. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  660. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  661. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  662. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  663. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  664. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  665. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  666. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  667. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  668. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  669. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  670. FIELD(EXIT_QUALIFICATION, exit_qualification),
  671. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  672. FIELD(GUEST_CR0, guest_cr0),
  673. FIELD(GUEST_CR3, guest_cr3),
  674. FIELD(GUEST_CR4, guest_cr4),
  675. FIELD(GUEST_ES_BASE, guest_es_base),
  676. FIELD(GUEST_CS_BASE, guest_cs_base),
  677. FIELD(GUEST_SS_BASE, guest_ss_base),
  678. FIELD(GUEST_DS_BASE, guest_ds_base),
  679. FIELD(GUEST_FS_BASE, guest_fs_base),
  680. FIELD(GUEST_GS_BASE, guest_gs_base),
  681. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  682. FIELD(GUEST_TR_BASE, guest_tr_base),
  683. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  684. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  685. FIELD(GUEST_DR7, guest_dr7),
  686. FIELD(GUEST_RSP, guest_rsp),
  687. FIELD(GUEST_RIP, guest_rip),
  688. FIELD(GUEST_RFLAGS, guest_rflags),
  689. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  690. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  691. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  692. FIELD(HOST_CR0, host_cr0),
  693. FIELD(HOST_CR3, host_cr3),
  694. FIELD(HOST_CR4, host_cr4),
  695. FIELD(HOST_FS_BASE, host_fs_base),
  696. FIELD(HOST_GS_BASE, host_gs_base),
  697. FIELD(HOST_TR_BASE, host_tr_base),
  698. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  699. FIELD(HOST_IDTR_BASE, host_idtr_base),
  700. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  701. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  702. FIELD(HOST_RSP, host_rsp),
  703. FIELD(HOST_RIP, host_rip),
  704. };
  705. static inline short vmcs_field_to_offset(unsigned long field)
  706. {
  707. BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
  708. if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
  709. vmcs_field_to_offset_table[field] == 0)
  710. return -ENOENT;
  711. return vmcs_field_to_offset_table[field];
  712. }
  713. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  714. {
  715. return to_vmx(vcpu)->nested.current_vmcs12;
  716. }
  717. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  718. {
  719. struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
  720. if (is_error_page(page))
  721. return NULL;
  722. return page;
  723. }
  724. static void nested_release_page(struct page *page)
  725. {
  726. kvm_release_page_dirty(page);
  727. }
  728. static void nested_release_page_clean(struct page *page)
  729. {
  730. kvm_release_page_clean(page);
  731. }
  732. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  733. static u64 construct_eptp(unsigned long root_hpa);
  734. static void kvm_cpu_vmxon(u64 addr);
  735. static void kvm_cpu_vmxoff(void);
  736. static bool vmx_mpx_supported(void);
  737. static bool vmx_xsaves_supported(void);
  738. static int vmx_vm_has_apicv(struct kvm *kvm);
  739. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  740. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  741. struct kvm_segment *var, int seg);
  742. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  743. struct kvm_segment *var, int seg);
  744. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  745. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  746. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
  747. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
  748. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  749. static int alloc_identity_pagetable(struct kvm *kvm);
  750. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  751. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  752. /*
  753. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  754. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  755. */
  756. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  757. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  758. static unsigned long *vmx_io_bitmap_a;
  759. static unsigned long *vmx_io_bitmap_b;
  760. static unsigned long *vmx_msr_bitmap_legacy;
  761. static unsigned long *vmx_msr_bitmap_longmode;
  762. static unsigned long *vmx_msr_bitmap_legacy_x2apic;
  763. static unsigned long *vmx_msr_bitmap_longmode_x2apic;
  764. static unsigned long *vmx_msr_bitmap_nested;
  765. static unsigned long *vmx_vmread_bitmap;
  766. static unsigned long *vmx_vmwrite_bitmap;
  767. static bool cpu_has_load_ia32_efer;
  768. static bool cpu_has_load_perf_global_ctrl;
  769. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  770. static DEFINE_SPINLOCK(vmx_vpid_lock);
  771. static struct vmcs_config {
  772. int size;
  773. int order;
  774. u32 revision_id;
  775. u32 pin_based_exec_ctrl;
  776. u32 cpu_based_exec_ctrl;
  777. u32 cpu_based_2nd_exec_ctrl;
  778. u32 vmexit_ctrl;
  779. u32 vmentry_ctrl;
  780. } vmcs_config;
  781. static struct vmx_capability {
  782. u32 ept;
  783. u32 vpid;
  784. } vmx_capability;
  785. #define VMX_SEGMENT_FIELD(seg) \
  786. [VCPU_SREG_##seg] = { \
  787. .selector = GUEST_##seg##_SELECTOR, \
  788. .base = GUEST_##seg##_BASE, \
  789. .limit = GUEST_##seg##_LIMIT, \
  790. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  791. }
  792. static const struct kvm_vmx_segment_field {
  793. unsigned selector;
  794. unsigned base;
  795. unsigned limit;
  796. unsigned ar_bytes;
  797. } kvm_vmx_segment_fields[] = {
  798. VMX_SEGMENT_FIELD(CS),
  799. VMX_SEGMENT_FIELD(DS),
  800. VMX_SEGMENT_FIELD(ES),
  801. VMX_SEGMENT_FIELD(FS),
  802. VMX_SEGMENT_FIELD(GS),
  803. VMX_SEGMENT_FIELD(SS),
  804. VMX_SEGMENT_FIELD(TR),
  805. VMX_SEGMENT_FIELD(LDTR),
  806. };
  807. static u64 host_efer;
  808. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  809. /*
  810. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  811. * away by decrementing the array size.
  812. */
  813. static const u32 vmx_msr_index[] = {
  814. #ifdef CONFIG_X86_64
  815. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  816. #endif
  817. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  818. };
  819. static inline bool is_page_fault(u32 intr_info)
  820. {
  821. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  822. INTR_INFO_VALID_MASK)) ==
  823. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  824. }
  825. static inline bool is_no_device(u32 intr_info)
  826. {
  827. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  828. INTR_INFO_VALID_MASK)) ==
  829. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  830. }
  831. static inline bool is_invalid_opcode(u32 intr_info)
  832. {
  833. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  834. INTR_INFO_VALID_MASK)) ==
  835. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  836. }
  837. static inline bool is_external_interrupt(u32 intr_info)
  838. {
  839. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  840. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  841. }
  842. static inline bool is_machine_check(u32 intr_info)
  843. {
  844. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  845. INTR_INFO_VALID_MASK)) ==
  846. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  847. }
  848. static inline bool cpu_has_vmx_msr_bitmap(void)
  849. {
  850. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  851. }
  852. static inline bool cpu_has_vmx_tpr_shadow(void)
  853. {
  854. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  855. }
  856. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  857. {
  858. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  859. }
  860. static inline bool cpu_has_secondary_exec_ctrls(void)
  861. {
  862. return vmcs_config.cpu_based_exec_ctrl &
  863. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  864. }
  865. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  866. {
  867. return vmcs_config.cpu_based_2nd_exec_ctrl &
  868. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  869. }
  870. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  871. {
  872. return vmcs_config.cpu_based_2nd_exec_ctrl &
  873. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  874. }
  875. static inline bool cpu_has_vmx_apic_register_virt(void)
  876. {
  877. return vmcs_config.cpu_based_2nd_exec_ctrl &
  878. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  879. }
  880. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  881. {
  882. return vmcs_config.cpu_based_2nd_exec_ctrl &
  883. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  884. }
  885. static inline bool cpu_has_vmx_posted_intr(void)
  886. {
  887. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  888. }
  889. static inline bool cpu_has_vmx_apicv(void)
  890. {
  891. return cpu_has_vmx_apic_register_virt() &&
  892. cpu_has_vmx_virtual_intr_delivery() &&
  893. cpu_has_vmx_posted_intr();
  894. }
  895. static inline bool cpu_has_vmx_flexpriority(void)
  896. {
  897. return cpu_has_vmx_tpr_shadow() &&
  898. cpu_has_vmx_virtualize_apic_accesses();
  899. }
  900. static inline bool cpu_has_vmx_ept_execute_only(void)
  901. {
  902. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  903. }
  904. static inline bool cpu_has_vmx_ept_2m_page(void)
  905. {
  906. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  907. }
  908. static inline bool cpu_has_vmx_ept_1g_page(void)
  909. {
  910. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  911. }
  912. static inline bool cpu_has_vmx_ept_4levels(void)
  913. {
  914. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  915. }
  916. static inline bool cpu_has_vmx_ept_ad_bits(void)
  917. {
  918. return vmx_capability.ept & VMX_EPT_AD_BIT;
  919. }
  920. static inline bool cpu_has_vmx_invept_context(void)
  921. {
  922. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  923. }
  924. static inline bool cpu_has_vmx_invept_global(void)
  925. {
  926. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  927. }
  928. static inline bool cpu_has_vmx_invvpid_single(void)
  929. {
  930. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  931. }
  932. static inline bool cpu_has_vmx_invvpid_global(void)
  933. {
  934. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  935. }
  936. static inline bool cpu_has_vmx_ept(void)
  937. {
  938. return vmcs_config.cpu_based_2nd_exec_ctrl &
  939. SECONDARY_EXEC_ENABLE_EPT;
  940. }
  941. static inline bool cpu_has_vmx_unrestricted_guest(void)
  942. {
  943. return vmcs_config.cpu_based_2nd_exec_ctrl &
  944. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  945. }
  946. static inline bool cpu_has_vmx_ple(void)
  947. {
  948. return vmcs_config.cpu_based_2nd_exec_ctrl &
  949. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  950. }
  951. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  952. {
  953. return flexpriority_enabled && irqchip_in_kernel(kvm);
  954. }
  955. static inline bool cpu_has_vmx_vpid(void)
  956. {
  957. return vmcs_config.cpu_based_2nd_exec_ctrl &
  958. SECONDARY_EXEC_ENABLE_VPID;
  959. }
  960. static inline bool cpu_has_vmx_rdtscp(void)
  961. {
  962. return vmcs_config.cpu_based_2nd_exec_ctrl &
  963. SECONDARY_EXEC_RDTSCP;
  964. }
  965. static inline bool cpu_has_vmx_invpcid(void)
  966. {
  967. return vmcs_config.cpu_based_2nd_exec_ctrl &
  968. SECONDARY_EXEC_ENABLE_INVPCID;
  969. }
  970. static inline bool cpu_has_virtual_nmis(void)
  971. {
  972. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  973. }
  974. static inline bool cpu_has_vmx_wbinvd_exit(void)
  975. {
  976. return vmcs_config.cpu_based_2nd_exec_ctrl &
  977. SECONDARY_EXEC_WBINVD_EXITING;
  978. }
  979. static inline bool cpu_has_vmx_shadow_vmcs(void)
  980. {
  981. u64 vmx_msr;
  982. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  983. /* check if the cpu supports writing r/o exit information fields */
  984. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  985. return false;
  986. return vmcs_config.cpu_based_2nd_exec_ctrl &
  987. SECONDARY_EXEC_SHADOW_VMCS;
  988. }
  989. static inline bool cpu_has_vmx_pml(void)
  990. {
  991. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  992. }
  993. static inline bool report_flexpriority(void)
  994. {
  995. return flexpriority_enabled;
  996. }
  997. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  998. {
  999. return vmcs12->cpu_based_vm_exec_control & bit;
  1000. }
  1001. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1002. {
  1003. return (vmcs12->cpu_based_vm_exec_control &
  1004. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1005. (vmcs12->secondary_vm_exec_control & bit);
  1006. }
  1007. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1008. {
  1009. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1010. }
  1011. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1012. {
  1013. return vmcs12->pin_based_vm_exec_control &
  1014. PIN_BASED_VMX_PREEMPTION_TIMER;
  1015. }
  1016. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1017. {
  1018. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1019. }
  1020. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1021. {
  1022. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
  1023. vmx_xsaves_supported();
  1024. }
  1025. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1026. {
  1027. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1028. }
  1029. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1030. {
  1031. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1032. }
  1033. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1034. {
  1035. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1036. }
  1037. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1038. {
  1039. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1040. }
  1041. static inline bool is_exception(u32 intr_info)
  1042. {
  1043. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1044. == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
  1045. }
  1046. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1047. u32 exit_intr_info,
  1048. unsigned long exit_qualification);
  1049. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1050. struct vmcs12 *vmcs12,
  1051. u32 reason, unsigned long qualification);
  1052. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1053. {
  1054. int i;
  1055. for (i = 0; i < vmx->nmsrs; ++i)
  1056. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1057. return i;
  1058. return -1;
  1059. }
  1060. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1061. {
  1062. struct {
  1063. u64 vpid : 16;
  1064. u64 rsvd : 48;
  1065. u64 gva;
  1066. } operand = { vpid, 0, gva };
  1067. asm volatile (__ex(ASM_VMX_INVVPID)
  1068. /* CF==1 or ZF==1 --> rc = -1 */
  1069. "; ja 1f ; ud2 ; 1:"
  1070. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1071. }
  1072. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1073. {
  1074. struct {
  1075. u64 eptp, gpa;
  1076. } operand = {eptp, gpa};
  1077. asm volatile (__ex(ASM_VMX_INVEPT)
  1078. /* CF==1 or ZF==1 --> rc = -1 */
  1079. "; ja 1f ; ud2 ; 1:\n"
  1080. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1081. }
  1082. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1083. {
  1084. int i;
  1085. i = __find_msr_index(vmx, msr);
  1086. if (i >= 0)
  1087. return &vmx->guest_msrs[i];
  1088. return NULL;
  1089. }
  1090. static void vmcs_clear(struct vmcs *vmcs)
  1091. {
  1092. u64 phys_addr = __pa(vmcs);
  1093. u8 error;
  1094. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1095. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1096. : "cc", "memory");
  1097. if (error)
  1098. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1099. vmcs, phys_addr);
  1100. }
  1101. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1102. {
  1103. vmcs_clear(loaded_vmcs->vmcs);
  1104. loaded_vmcs->cpu = -1;
  1105. loaded_vmcs->launched = 0;
  1106. }
  1107. static void vmcs_load(struct vmcs *vmcs)
  1108. {
  1109. u64 phys_addr = __pa(vmcs);
  1110. u8 error;
  1111. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1112. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1113. : "cc", "memory");
  1114. if (error)
  1115. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1116. vmcs, phys_addr);
  1117. }
  1118. #ifdef CONFIG_KEXEC
  1119. /*
  1120. * This bitmap is used to indicate whether the vmclear
  1121. * operation is enabled on all cpus. All disabled by
  1122. * default.
  1123. */
  1124. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1125. static inline void crash_enable_local_vmclear(int cpu)
  1126. {
  1127. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1128. }
  1129. static inline void crash_disable_local_vmclear(int cpu)
  1130. {
  1131. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1132. }
  1133. static inline int crash_local_vmclear_enabled(int cpu)
  1134. {
  1135. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1136. }
  1137. static void crash_vmclear_local_loaded_vmcss(void)
  1138. {
  1139. int cpu = raw_smp_processor_id();
  1140. struct loaded_vmcs *v;
  1141. if (!crash_local_vmclear_enabled(cpu))
  1142. return;
  1143. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1144. loaded_vmcss_on_cpu_link)
  1145. vmcs_clear(v->vmcs);
  1146. }
  1147. #else
  1148. static inline void crash_enable_local_vmclear(int cpu) { }
  1149. static inline void crash_disable_local_vmclear(int cpu) { }
  1150. #endif /* CONFIG_KEXEC */
  1151. static void __loaded_vmcs_clear(void *arg)
  1152. {
  1153. struct loaded_vmcs *loaded_vmcs = arg;
  1154. int cpu = raw_smp_processor_id();
  1155. if (loaded_vmcs->cpu != cpu)
  1156. return; /* vcpu migration can race with cpu offline */
  1157. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1158. per_cpu(current_vmcs, cpu) = NULL;
  1159. crash_disable_local_vmclear(cpu);
  1160. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1161. /*
  1162. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1163. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1164. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1165. * then adds the vmcs into percpu list before it is deleted.
  1166. */
  1167. smp_wmb();
  1168. loaded_vmcs_init(loaded_vmcs);
  1169. crash_enable_local_vmclear(cpu);
  1170. }
  1171. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1172. {
  1173. int cpu = loaded_vmcs->cpu;
  1174. if (cpu != -1)
  1175. smp_call_function_single(cpu,
  1176. __loaded_vmcs_clear, loaded_vmcs, 1);
  1177. }
  1178. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  1179. {
  1180. if (vmx->vpid == 0)
  1181. return;
  1182. if (cpu_has_vmx_invvpid_single())
  1183. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  1184. }
  1185. static inline void vpid_sync_vcpu_global(void)
  1186. {
  1187. if (cpu_has_vmx_invvpid_global())
  1188. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1189. }
  1190. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  1191. {
  1192. if (cpu_has_vmx_invvpid_single())
  1193. vpid_sync_vcpu_single(vmx);
  1194. else
  1195. vpid_sync_vcpu_global();
  1196. }
  1197. static inline void ept_sync_global(void)
  1198. {
  1199. if (cpu_has_vmx_invept_global())
  1200. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1201. }
  1202. static inline void ept_sync_context(u64 eptp)
  1203. {
  1204. if (enable_ept) {
  1205. if (cpu_has_vmx_invept_context())
  1206. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1207. else
  1208. ept_sync_global();
  1209. }
  1210. }
  1211. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1212. {
  1213. unsigned long value;
  1214. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1215. : "=a"(value) : "d"(field) : "cc");
  1216. return value;
  1217. }
  1218. static __always_inline u16 vmcs_read16(unsigned long field)
  1219. {
  1220. return vmcs_readl(field);
  1221. }
  1222. static __always_inline u32 vmcs_read32(unsigned long field)
  1223. {
  1224. return vmcs_readl(field);
  1225. }
  1226. static __always_inline u64 vmcs_read64(unsigned long field)
  1227. {
  1228. #ifdef CONFIG_X86_64
  1229. return vmcs_readl(field);
  1230. #else
  1231. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  1232. #endif
  1233. }
  1234. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1235. {
  1236. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1237. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1238. dump_stack();
  1239. }
  1240. static void vmcs_writel(unsigned long field, unsigned long value)
  1241. {
  1242. u8 error;
  1243. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1244. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1245. if (unlikely(error))
  1246. vmwrite_error(field, value);
  1247. }
  1248. static void vmcs_write16(unsigned long field, u16 value)
  1249. {
  1250. vmcs_writel(field, value);
  1251. }
  1252. static void vmcs_write32(unsigned long field, u32 value)
  1253. {
  1254. vmcs_writel(field, value);
  1255. }
  1256. static void vmcs_write64(unsigned long field, u64 value)
  1257. {
  1258. vmcs_writel(field, value);
  1259. #ifndef CONFIG_X86_64
  1260. asm volatile ("");
  1261. vmcs_writel(field+1, value >> 32);
  1262. #endif
  1263. }
  1264. static void vmcs_clear_bits(unsigned long field, u32 mask)
  1265. {
  1266. vmcs_writel(field, vmcs_readl(field) & ~mask);
  1267. }
  1268. static void vmcs_set_bits(unsigned long field, u32 mask)
  1269. {
  1270. vmcs_writel(field, vmcs_readl(field) | mask);
  1271. }
  1272. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1273. {
  1274. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1275. vmx->vm_entry_controls_shadow = val;
  1276. }
  1277. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1278. {
  1279. if (vmx->vm_entry_controls_shadow != val)
  1280. vm_entry_controls_init(vmx, val);
  1281. }
  1282. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1283. {
  1284. return vmx->vm_entry_controls_shadow;
  1285. }
  1286. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1287. {
  1288. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1289. }
  1290. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1291. {
  1292. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1293. }
  1294. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1295. {
  1296. vmcs_write32(VM_EXIT_CONTROLS, val);
  1297. vmx->vm_exit_controls_shadow = val;
  1298. }
  1299. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1300. {
  1301. if (vmx->vm_exit_controls_shadow != val)
  1302. vm_exit_controls_init(vmx, val);
  1303. }
  1304. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1305. {
  1306. return vmx->vm_exit_controls_shadow;
  1307. }
  1308. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1309. {
  1310. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1311. }
  1312. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1313. {
  1314. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1315. }
  1316. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1317. {
  1318. vmx->segment_cache.bitmask = 0;
  1319. }
  1320. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1321. unsigned field)
  1322. {
  1323. bool ret;
  1324. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1325. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1326. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1327. vmx->segment_cache.bitmask = 0;
  1328. }
  1329. ret = vmx->segment_cache.bitmask & mask;
  1330. vmx->segment_cache.bitmask |= mask;
  1331. return ret;
  1332. }
  1333. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1334. {
  1335. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1336. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1337. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1338. return *p;
  1339. }
  1340. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1341. {
  1342. ulong *p = &vmx->segment_cache.seg[seg].base;
  1343. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1344. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1345. return *p;
  1346. }
  1347. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1348. {
  1349. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1350. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1351. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1352. return *p;
  1353. }
  1354. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1355. {
  1356. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1357. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1358. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1359. return *p;
  1360. }
  1361. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1362. {
  1363. u32 eb;
  1364. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1365. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  1366. if ((vcpu->guest_debug &
  1367. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1368. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1369. eb |= 1u << BP_VECTOR;
  1370. if (to_vmx(vcpu)->rmode.vm86_active)
  1371. eb = ~0;
  1372. if (enable_ept)
  1373. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1374. if (vcpu->fpu_active)
  1375. eb &= ~(1u << NM_VECTOR);
  1376. /* When we are running a nested L2 guest and L1 specified for it a
  1377. * certain exception bitmap, we must trap the same exceptions and pass
  1378. * them to L1. When running L2, we will only handle the exceptions
  1379. * specified above if L1 did not want them.
  1380. */
  1381. if (is_guest_mode(vcpu))
  1382. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1383. vmcs_write32(EXCEPTION_BITMAP, eb);
  1384. }
  1385. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1386. unsigned long entry, unsigned long exit)
  1387. {
  1388. vm_entry_controls_clearbit(vmx, entry);
  1389. vm_exit_controls_clearbit(vmx, exit);
  1390. }
  1391. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1392. {
  1393. unsigned i;
  1394. struct msr_autoload *m = &vmx->msr_autoload;
  1395. switch (msr) {
  1396. case MSR_EFER:
  1397. if (cpu_has_load_ia32_efer) {
  1398. clear_atomic_switch_msr_special(vmx,
  1399. VM_ENTRY_LOAD_IA32_EFER,
  1400. VM_EXIT_LOAD_IA32_EFER);
  1401. return;
  1402. }
  1403. break;
  1404. case MSR_CORE_PERF_GLOBAL_CTRL:
  1405. if (cpu_has_load_perf_global_ctrl) {
  1406. clear_atomic_switch_msr_special(vmx,
  1407. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1408. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1409. return;
  1410. }
  1411. break;
  1412. }
  1413. for (i = 0; i < m->nr; ++i)
  1414. if (m->guest[i].index == msr)
  1415. break;
  1416. if (i == m->nr)
  1417. return;
  1418. --m->nr;
  1419. m->guest[i] = m->guest[m->nr];
  1420. m->host[i] = m->host[m->nr];
  1421. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1422. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1423. }
  1424. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1425. unsigned long entry, unsigned long exit,
  1426. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  1427. u64 guest_val, u64 host_val)
  1428. {
  1429. vmcs_write64(guest_val_vmcs, guest_val);
  1430. vmcs_write64(host_val_vmcs, host_val);
  1431. vm_entry_controls_setbit(vmx, entry);
  1432. vm_exit_controls_setbit(vmx, exit);
  1433. }
  1434. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1435. u64 guest_val, u64 host_val)
  1436. {
  1437. unsigned i;
  1438. struct msr_autoload *m = &vmx->msr_autoload;
  1439. switch (msr) {
  1440. case MSR_EFER:
  1441. if (cpu_has_load_ia32_efer) {
  1442. add_atomic_switch_msr_special(vmx,
  1443. VM_ENTRY_LOAD_IA32_EFER,
  1444. VM_EXIT_LOAD_IA32_EFER,
  1445. GUEST_IA32_EFER,
  1446. HOST_IA32_EFER,
  1447. guest_val, host_val);
  1448. return;
  1449. }
  1450. break;
  1451. case MSR_CORE_PERF_GLOBAL_CTRL:
  1452. if (cpu_has_load_perf_global_ctrl) {
  1453. add_atomic_switch_msr_special(vmx,
  1454. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1455. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1456. GUEST_IA32_PERF_GLOBAL_CTRL,
  1457. HOST_IA32_PERF_GLOBAL_CTRL,
  1458. guest_val, host_val);
  1459. return;
  1460. }
  1461. break;
  1462. }
  1463. for (i = 0; i < m->nr; ++i)
  1464. if (m->guest[i].index == msr)
  1465. break;
  1466. if (i == NR_AUTOLOAD_MSRS) {
  1467. printk_once(KERN_WARNING "Not enough msr switch entries. "
  1468. "Can't add msr %x\n", msr);
  1469. return;
  1470. } else if (i == m->nr) {
  1471. ++m->nr;
  1472. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1473. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1474. }
  1475. m->guest[i].index = msr;
  1476. m->guest[i].value = guest_val;
  1477. m->host[i].index = msr;
  1478. m->host[i].value = host_val;
  1479. }
  1480. static void reload_tss(void)
  1481. {
  1482. /*
  1483. * VT restores TR but not its size. Useless.
  1484. */
  1485. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1486. struct desc_struct *descs;
  1487. descs = (void *)gdt->address;
  1488. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  1489. load_TR_desc();
  1490. }
  1491. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1492. {
  1493. u64 guest_efer;
  1494. u64 ignore_bits;
  1495. guest_efer = vmx->vcpu.arch.efer;
  1496. /*
  1497. * NX is emulated; LMA and LME handled by hardware; SCE meaningless
  1498. * outside long mode
  1499. */
  1500. ignore_bits = EFER_NX | EFER_SCE;
  1501. #ifdef CONFIG_X86_64
  1502. ignore_bits |= EFER_LMA | EFER_LME;
  1503. /* SCE is meaningful only in long mode on Intel */
  1504. if (guest_efer & EFER_LMA)
  1505. ignore_bits &= ~(u64)EFER_SCE;
  1506. #endif
  1507. guest_efer &= ~ignore_bits;
  1508. guest_efer |= host_efer & ignore_bits;
  1509. vmx->guest_msrs[efer_offset].data = guest_efer;
  1510. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1511. clear_atomic_switch_msr(vmx, MSR_EFER);
  1512. /*
  1513. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  1514. * On CPUs that support "load IA32_EFER", always switch EFER
  1515. * atomically, since it's faster than switching it manually.
  1516. */
  1517. if (cpu_has_load_ia32_efer ||
  1518. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  1519. guest_efer = vmx->vcpu.arch.efer;
  1520. if (!(guest_efer & EFER_LMA))
  1521. guest_efer &= ~EFER_LME;
  1522. if (guest_efer != host_efer)
  1523. add_atomic_switch_msr(vmx, MSR_EFER,
  1524. guest_efer, host_efer);
  1525. return false;
  1526. }
  1527. return true;
  1528. }
  1529. static unsigned long segment_base(u16 selector)
  1530. {
  1531. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1532. struct desc_struct *d;
  1533. unsigned long table_base;
  1534. unsigned long v;
  1535. if (!(selector & ~3))
  1536. return 0;
  1537. table_base = gdt->address;
  1538. if (selector & 4) { /* from ldt */
  1539. u16 ldt_selector = kvm_read_ldt();
  1540. if (!(ldt_selector & ~3))
  1541. return 0;
  1542. table_base = segment_base(ldt_selector);
  1543. }
  1544. d = (struct desc_struct *)(table_base + (selector & ~7));
  1545. v = get_desc_base(d);
  1546. #ifdef CONFIG_X86_64
  1547. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  1548. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  1549. #endif
  1550. return v;
  1551. }
  1552. static inline unsigned long kvm_read_tr_base(void)
  1553. {
  1554. u16 tr;
  1555. asm("str %0" : "=g"(tr));
  1556. return segment_base(tr);
  1557. }
  1558. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1559. {
  1560. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1561. int i;
  1562. if (vmx->host_state.loaded)
  1563. return;
  1564. vmx->host_state.loaded = 1;
  1565. /*
  1566. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1567. * allow segment selectors with cpl > 0 or ti == 1.
  1568. */
  1569. vmx->host_state.ldt_sel = kvm_read_ldt();
  1570. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1571. savesegment(fs, vmx->host_state.fs_sel);
  1572. if (!(vmx->host_state.fs_sel & 7)) {
  1573. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1574. vmx->host_state.fs_reload_needed = 0;
  1575. } else {
  1576. vmcs_write16(HOST_FS_SELECTOR, 0);
  1577. vmx->host_state.fs_reload_needed = 1;
  1578. }
  1579. savesegment(gs, vmx->host_state.gs_sel);
  1580. if (!(vmx->host_state.gs_sel & 7))
  1581. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1582. else {
  1583. vmcs_write16(HOST_GS_SELECTOR, 0);
  1584. vmx->host_state.gs_ldt_reload_needed = 1;
  1585. }
  1586. #ifdef CONFIG_X86_64
  1587. savesegment(ds, vmx->host_state.ds_sel);
  1588. savesegment(es, vmx->host_state.es_sel);
  1589. #endif
  1590. #ifdef CONFIG_X86_64
  1591. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1592. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1593. #else
  1594. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1595. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1596. #endif
  1597. #ifdef CONFIG_X86_64
  1598. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1599. if (is_long_mode(&vmx->vcpu))
  1600. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1601. #endif
  1602. if (boot_cpu_has(X86_FEATURE_MPX))
  1603. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1604. for (i = 0; i < vmx->save_nmsrs; ++i)
  1605. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1606. vmx->guest_msrs[i].data,
  1607. vmx->guest_msrs[i].mask);
  1608. }
  1609. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1610. {
  1611. if (!vmx->host_state.loaded)
  1612. return;
  1613. ++vmx->vcpu.stat.host_state_reload;
  1614. vmx->host_state.loaded = 0;
  1615. #ifdef CONFIG_X86_64
  1616. if (is_long_mode(&vmx->vcpu))
  1617. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1618. #endif
  1619. if (vmx->host_state.gs_ldt_reload_needed) {
  1620. kvm_load_ldt(vmx->host_state.ldt_sel);
  1621. #ifdef CONFIG_X86_64
  1622. load_gs_index(vmx->host_state.gs_sel);
  1623. #else
  1624. loadsegment(gs, vmx->host_state.gs_sel);
  1625. #endif
  1626. }
  1627. if (vmx->host_state.fs_reload_needed)
  1628. loadsegment(fs, vmx->host_state.fs_sel);
  1629. #ifdef CONFIG_X86_64
  1630. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1631. loadsegment(ds, vmx->host_state.ds_sel);
  1632. loadsegment(es, vmx->host_state.es_sel);
  1633. }
  1634. #endif
  1635. reload_tss();
  1636. #ifdef CONFIG_X86_64
  1637. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1638. #endif
  1639. if (vmx->host_state.msr_host_bndcfgs)
  1640. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1641. /*
  1642. * If the FPU is not active (through the host task or
  1643. * the guest vcpu), then restore the cr0.TS bit.
  1644. */
  1645. if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
  1646. stts();
  1647. load_gdt(this_cpu_ptr(&host_gdt));
  1648. }
  1649. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1650. {
  1651. preempt_disable();
  1652. __vmx_load_host_state(vmx);
  1653. preempt_enable();
  1654. }
  1655. /*
  1656. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1657. * vcpu mutex is already taken.
  1658. */
  1659. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1660. {
  1661. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1662. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1663. if (!vmm_exclusive)
  1664. kvm_cpu_vmxon(phys_addr);
  1665. else if (vmx->loaded_vmcs->cpu != cpu)
  1666. loaded_vmcs_clear(vmx->loaded_vmcs);
  1667. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1668. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1669. vmcs_load(vmx->loaded_vmcs->vmcs);
  1670. }
  1671. if (vmx->loaded_vmcs->cpu != cpu) {
  1672. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1673. unsigned long sysenter_esp;
  1674. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1675. local_irq_disable();
  1676. crash_disable_local_vmclear(cpu);
  1677. /*
  1678. * Read loaded_vmcs->cpu should be before fetching
  1679. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  1680. * See the comments in __loaded_vmcs_clear().
  1681. */
  1682. smp_rmb();
  1683. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1684. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1685. crash_enable_local_vmclear(cpu);
  1686. local_irq_enable();
  1687. /*
  1688. * Linux uses per-cpu TSS and GDT, so set these when switching
  1689. * processors.
  1690. */
  1691. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  1692. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  1693. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  1694. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  1695. vmx->loaded_vmcs->cpu = cpu;
  1696. }
  1697. }
  1698. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  1699. {
  1700. __vmx_load_host_state(to_vmx(vcpu));
  1701. if (!vmm_exclusive) {
  1702. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  1703. vcpu->cpu = -1;
  1704. kvm_cpu_vmxoff();
  1705. }
  1706. }
  1707. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  1708. {
  1709. ulong cr0;
  1710. if (vcpu->fpu_active)
  1711. return;
  1712. vcpu->fpu_active = 1;
  1713. cr0 = vmcs_readl(GUEST_CR0);
  1714. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  1715. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  1716. vmcs_writel(GUEST_CR0, cr0);
  1717. update_exception_bitmap(vcpu);
  1718. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  1719. if (is_guest_mode(vcpu))
  1720. vcpu->arch.cr0_guest_owned_bits &=
  1721. ~get_vmcs12(vcpu)->cr0_guest_host_mask;
  1722. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1723. }
  1724. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  1725. /*
  1726. * Return the cr0 value that a nested guest would read. This is a combination
  1727. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  1728. * its hypervisor (cr0_read_shadow).
  1729. */
  1730. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  1731. {
  1732. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  1733. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  1734. }
  1735. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  1736. {
  1737. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  1738. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  1739. }
  1740. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  1741. {
  1742. /* Note that there is no vcpu->fpu_active = 0 here. The caller must
  1743. * set this *before* calling this function.
  1744. */
  1745. vmx_decache_cr0_guest_bits(vcpu);
  1746. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  1747. update_exception_bitmap(vcpu);
  1748. vcpu->arch.cr0_guest_owned_bits = 0;
  1749. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1750. if (is_guest_mode(vcpu)) {
  1751. /*
  1752. * L1's specified read shadow might not contain the TS bit,
  1753. * so now that we turned on shadowing of this bit, we need to
  1754. * set this bit of the shadow. Like in nested_vmx_run we need
  1755. * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
  1756. * up-to-date here because we just decached cr0.TS (and we'll
  1757. * only update vmcs12->guest_cr0 on nested exit).
  1758. */
  1759. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1760. vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
  1761. (vcpu->arch.cr0 & X86_CR0_TS);
  1762. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  1763. } else
  1764. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  1765. }
  1766. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  1767. {
  1768. unsigned long rflags, save_rflags;
  1769. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  1770. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1771. rflags = vmcs_readl(GUEST_RFLAGS);
  1772. if (to_vmx(vcpu)->rmode.vm86_active) {
  1773. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1774. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1775. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1776. }
  1777. to_vmx(vcpu)->rflags = rflags;
  1778. }
  1779. return to_vmx(vcpu)->rflags;
  1780. }
  1781. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1782. {
  1783. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1784. to_vmx(vcpu)->rflags = rflags;
  1785. if (to_vmx(vcpu)->rmode.vm86_active) {
  1786. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1787. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1788. }
  1789. vmcs_writel(GUEST_RFLAGS, rflags);
  1790. }
  1791. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  1792. {
  1793. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1794. int ret = 0;
  1795. if (interruptibility & GUEST_INTR_STATE_STI)
  1796. ret |= KVM_X86_SHADOW_INT_STI;
  1797. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1798. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1799. return ret;
  1800. }
  1801. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1802. {
  1803. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1804. u32 interruptibility = interruptibility_old;
  1805. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1806. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1807. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1808. else if (mask & KVM_X86_SHADOW_INT_STI)
  1809. interruptibility |= GUEST_INTR_STATE_STI;
  1810. if ((interruptibility != interruptibility_old))
  1811. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1812. }
  1813. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1814. {
  1815. unsigned long rip;
  1816. rip = kvm_rip_read(vcpu);
  1817. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1818. kvm_rip_write(vcpu, rip);
  1819. /* skipping an emulated instruction also counts */
  1820. vmx_set_interrupt_shadow(vcpu, 0);
  1821. }
  1822. /*
  1823. * KVM wants to inject page-faults which it got to the guest. This function
  1824. * checks whether in a nested guest, we need to inject them to L1 or L2.
  1825. */
  1826. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
  1827. {
  1828. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1829. if (!(vmcs12->exception_bitmap & (1u << nr)))
  1830. return 0;
  1831. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  1832. vmcs_read32(VM_EXIT_INTR_INFO),
  1833. vmcs_readl(EXIT_QUALIFICATION));
  1834. return 1;
  1835. }
  1836. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1837. bool has_error_code, u32 error_code,
  1838. bool reinject)
  1839. {
  1840. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1841. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1842. if (!reinject && is_guest_mode(vcpu) &&
  1843. nested_vmx_check_exception(vcpu, nr))
  1844. return;
  1845. if (has_error_code) {
  1846. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1847. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1848. }
  1849. if (vmx->rmode.vm86_active) {
  1850. int inc_eip = 0;
  1851. if (kvm_exception_is_soft(nr))
  1852. inc_eip = vcpu->arch.event_exit_inst_len;
  1853. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1854. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1855. return;
  1856. }
  1857. if (kvm_exception_is_soft(nr)) {
  1858. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1859. vmx->vcpu.arch.event_exit_inst_len);
  1860. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1861. } else
  1862. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1863. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1864. }
  1865. static bool vmx_rdtscp_supported(void)
  1866. {
  1867. return cpu_has_vmx_rdtscp();
  1868. }
  1869. static bool vmx_invpcid_supported(void)
  1870. {
  1871. return cpu_has_vmx_invpcid() && enable_ept;
  1872. }
  1873. /*
  1874. * Swap MSR entry in host/guest MSR entry array.
  1875. */
  1876. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1877. {
  1878. struct shared_msr_entry tmp;
  1879. tmp = vmx->guest_msrs[to];
  1880. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1881. vmx->guest_msrs[from] = tmp;
  1882. }
  1883. static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
  1884. {
  1885. unsigned long *msr_bitmap;
  1886. if (is_guest_mode(vcpu))
  1887. msr_bitmap = vmx_msr_bitmap_nested;
  1888. else if (irqchip_in_kernel(vcpu->kvm) &&
  1889. apic_x2apic_mode(vcpu->arch.apic)) {
  1890. if (is_long_mode(vcpu))
  1891. msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
  1892. else
  1893. msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
  1894. } else {
  1895. if (is_long_mode(vcpu))
  1896. msr_bitmap = vmx_msr_bitmap_longmode;
  1897. else
  1898. msr_bitmap = vmx_msr_bitmap_legacy;
  1899. }
  1900. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1901. }
  1902. /*
  1903. * Set up the vmcs to automatically save and restore system
  1904. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1905. * mode, as fiddling with msrs is very expensive.
  1906. */
  1907. static void setup_msrs(struct vcpu_vmx *vmx)
  1908. {
  1909. int save_nmsrs, index;
  1910. save_nmsrs = 0;
  1911. #ifdef CONFIG_X86_64
  1912. if (is_long_mode(&vmx->vcpu)) {
  1913. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1914. if (index >= 0)
  1915. move_msr_up(vmx, index, save_nmsrs++);
  1916. index = __find_msr_index(vmx, MSR_LSTAR);
  1917. if (index >= 0)
  1918. move_msr_up(vmx, index, save_nmsrs++);
  1919. index = __find_msr_index(vmx, MSR_CSTAR);
  1920. if (index >= 0)
  1921. move_msr_up(vmx, index, save_nmsrs++);
  1922. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1923. if (index >= 0 && vmx->rdtscp_enabled)
  1924. move_msr_up(vmx, index, save_nmsrs++);
  1925. /*
  1926. * MSR_STAR is only needed on long mode guests, and only
  1927. * if efer.sce is enabled.
  1928. */
  1929. index = __find_msr_index(vmx, MSR_STAR);
  1930. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1931. move_msr_up(vmx, index, save_nmsrs++);
  1932. }
  1933. #endif
  1934. index = __find_msr_index(vmx, MSR_EFER);
  1935. if (index >= 0 && update_transition_efer(vmx, index))
  1936. move_msr_up(vmx, index, save_nmsrs++);
  1937. vmx->save_nmsrs = save_nmsrs;
  1938. if (cpu_has_vmx_msr_bitmap())
  1939. vmx_set_msr_bitmap(&vmx->vcpu);
  1940. }
  1941. /*
  1942. * reads and returns guest's timestamp counter "register"
  1943. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1944. */
  1945. static u64 guest_read_tsc(void)
  1946. {
  1947. u64 host_tsc, tsc_offset;
  1948. rdtscll(host_tsc);
  1949. tsc_offset = vmcs_read64(TSC_OFFSET);
  1950. return host_tsc + tsc_offset;
  1951. }
  1952. /*
  1953. * Like guest_read_tsc, but always returns L1's notion of the timestamp
  1954. * counter, even if a nested guest (L2) is currently running.
  1955. */
  1956. static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
  1957. {
  1958. u64 tsc_offset;
  1959. tsc_offset = is_guest_mode(vcpu) ?
  1960. to_vmx(vcpu)->nested.vmcs01_tsc_offset :
  1961. vmcs_read64(TSC_OFFSET);
  1962. return host_tsc + tsc_offset;
  1963. }
  1964. /*
  1965. * Engage any workarounds for mis-matched TSC rates. Currently limited to
  1966. * software catchup for faster rates on slower CPUs.
  1967. */
  1968. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1969. {
  1970. if (!scale)
  1971. return;
  1972. if (user_tsc_khz > tsc_khz) {
  1973. vcpu->arch.tsc_catchup = 1;
  1974. vcpu->arch.tsc_always_catchup = 1;
  1975. } else
  1976. WARN(1, "user requested TSC rate below hardware speed\n");
  1977. }
  1978. static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
  1979. {
  1980. return vmcs_read64(TSC_OFFSET);
  1981. }
  1982. /*
  1983. * writes 'offset' into guest's timestamp counter offset register
  1984. */
  1985. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1986. {
  1987. if (is_guest_mode(vcpu)) {
  1988. /*
  1989. * We're here if L1 chose not to trap WRMSR to TSC. According
  1990. * to the spec, this should set L1's TSC; The offset that L1
  1991. * set for L2 remains unchanged, and still needs to be added
  1992. * to the newly set TSC to get L2's TSC.
  1993. */
  1994. struct vmcs12 *vmcs12;
  1995. to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
  1996. /* recalculate vmcs02.TSC_OFFSET: */
  1997. vmcs12 = get_vmcs12(vcpu);
  1998. vmcs_write64(TSC_OFFSET, offset +
  1999. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2000. vmcs12->tsc_offset : 0));
  2001. } else {
  2002. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2003. vmcs_read64(TSC_OFFSET), offset);
  2004. vmcs_write64(TSC_OFFSET, offset);
  2005. }
  2006. }
  2007. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
  2008. {
  2009. u64 offset = vmcs_read64(TSC_OFFSET);
  2010. vmcs_write64(TSC_OFFSET, offset + adjustment);
  2011. if (is_guest_mode(vcpu)) {
  2012. /* Even when running L2, the adjustment needs to apply to L1 */
  2013. to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
  2014. } else
  2015. trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
  2016. offset + adjustment);
  2017. }
  2018. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  2019. {
  2020. return target_tsc - native_read_tsc();
  2021. }
  2022. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  2023. {
  2024. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  2025. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  2026. }
  2027. /*
  2028. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2029. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2030. * all guests if the "nested" module option is off, and can also be disabled
  2031. * for a single guest by disabling its VMX cpuid bit.
  2032. */
  2033. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2034. {
  2035. return nested && guest_cpuid_has_vmx(vcpu);
  2036. }
  2037. /*
  2038. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2039. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2040. * The same values should also be used to verify that vmcs12 control fields are
  2041. * valid during nested entry from L1 to L2.
  2042. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2043. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2044. * bit in the high half is on if the corresponding bit in the control field
  2045. * may be on. See also vmx_control_verify().
  2046. */
  2047. static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
  2048. {
  2049. /*
  2050. * Note that as a general rule, the high half of the MSRs (bits in
  2051. * the control fields which may be 1) should be initialized by the
  2052. * intersection of the underlying hardware's MSR (i.e., features which
  2053. * can be supported) and the list of features we want to expose -
  2054. * because they are known to be properly supported in our code.
  2055. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2056. * be set to 0, meaning that L1 may turn off any of these bits. The
  2057. * reason is that if one of these bits is necessary, it will appear
  2058. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2059. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2060. * nested_vmx_exit_handled() will not pass related exits to L1.
  2061. * These rules have exceptions below.
  2062. */
  2063. /* pin-based controls */
  2064. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2065. vmx->nested.nested_vmx_pinbased_ctls_low,
  2066. vmx->nested.nested_vmx_pinbased_ctls_high);
  2067. vmx->nested.nested_vmx_pinbased_ctls_low |=
  2068. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2069. vmx->nested.nested_vmx_pinbased_ctls_high &=
  2070. PIN_BASED_EXT_INTR_MASK |
  2071. PIN_BASED_NMI_EXITING |
  2072. PIN_BASED_VIRTUAL_NMIS;
  2073. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2074. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2075. PIN_BASED_VMX_PREEMPTION_TIMER;
  2076. if (vmx_vm_has_apicv(vmx->vcpu.kvm))
  2077. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2078. PIN_BASED_POSTED_INTR;
  2079. /* exit controls */
  2080. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2081. vmx->nested.nested_vmx_exit_ctls_low,
  2082. vmx->nested.nested_vmx_exit_ctls_high);
  2083. vmx->nested.nested_vmx_exit_ctls_low =
  2084. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2085. vmx->nested.nested_vmx_exit_ctls_high &=
  2086. #ifdef CONFIG_X86_64
  2087. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2088. #endif
  2089. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2090. vmx->nested.nested_vmx_exit_ctls_high |=
  2091. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2092. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2093. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2094. if (vmx_mpx_supported())
  2095. vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2096. /* We support free control of debug control saving. */
  2097. vmx->nested.nested_vmx_true_exit_ctls_low =
  2098. vmx->nested.nested_vmx_exit_ctls_low &
  2099. ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2100. /* entry controls */
  2101. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2102. vmx->nested.nested_vmx_entry_ctls_low,
  2103. vmx->nested.nested_vmx_entry_ctls_high);
  2104. vmx->nested.nested_vmx_entry_ctls_low =
  2105. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2106. vmx->nested.nested_vmx_entry_ctls_high &=
  2107. #ifdef CONFIG_X86_64
  2108. VM_ENTRY_IA32E_MODE |
  2109. #endif
  2110. VM_ENTRY_LOAD_IA32_PAT;
  2111. vmx->nested.nested_vmx_entry_ctls_high |=
  2112. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2113. if (vmx_mpx_supported())
  2114. vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2115. /* We support free control of debug control loading. */
  2116. vmx->nested.nested_vmx_true_entry_ctls_low =
  2117. vmx->nested.nested_vmx_entry_ctls_low &
  2118. ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2119. /* cpu-based controls */
  2120. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2121. vmx->nested.nested_vmx_procbased_ctls_low,
  2122. vmx->nested.nested_vmx_procbased_ctls_high);
  2123. vmx->nested.nested_vmx_procbased_ctls_low =
  2124. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2125. vmx->nested.nested_vmx_procbased_ctls_high &=
  2126. CPU_BASED_VIRTUAL_INTR_PENDING |
  2127. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2128. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2129. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2130. CPU_BASED_CR3_STORE_EXITING |
  2131. #ifdef CONFIG_X86_64
  2132. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2133. #endif
  2134. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2135. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
  2136. CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
  2137. CPU_BASED_PAUSE_EXITING | CPU_BASED_TPR_SHADOW |
  2138. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2139. /*
  2140. * We can allow some features even when not supported by the
  2141. * hardware. For example, L1 can specify an MSR bitmap - and we
  2142. * can use it to avoid exits to L1 - even when L0 runs L2
  2143. * without MSR bitmaps.
  2144. */
  2145. vmx->nested.nested_vmx_procbased_ctls_high |=
  2146. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2147. CPU_BASED_USE_MSR_BITMAPS;
  2148. /* We support free control of CR3 access interception. */
  2149. vmx->nested.nested_vmx_true_procbased_ctls_low =
  2150. vmx->nested.nested_vmx_procbased_ctls_low &
  2151. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2152. /* secondary cpu-based controls */
  2153. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2154. vmx->nested.nested_vmx_secondary_ctls_low,
  2155. vmx->nested.nested_vmx_secondary_ctls_high);
  2156. vmx->nested.nested_vmx_secondary_ctls_low = 0;
  2157. vmx->nested.nested_vmx_secondary_ctls_high &=
  2158. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2159. SECONDARY_EXEC_RDTSCP |
  2160. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2161. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2162. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2163. SECONDARY_EXEC_WBINVD_EXITING |
  2164. SECONDARY_EXEC_XSAVES;
  2165. if (enable_ept) {
  2166. /* nested EPT: emulate EPT also to L1 */
  2167. vmx->nested.nested_vmx_secondary_ctls_high |=
  2168. SECONDARY_EXEC_ENABLE_EPT;
  2169. vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2170. VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
  2171. VMX_EPT_INVEPT_BIT;
  2172. vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
  2173. /*
  2174. * For nested guests, we don't do anything specific
  2175. * for single context invalidation. Hence, only advertise
  2176. * support for global context invalidation.
  2177. */
  2178. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
  2179. } else
  2180. vmx->nested.nested_vmx_ept_caps = 0;
  2181. if (enable_unrestricted_guest)
  2182. vmx->nested.nested_vmx_secondary_ctls_high |=
  2183. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2184. /* miscellaneous data */
  2185. rdmsr(MSR_IA32_VMX_MISC,
  2186. vmx->nested.nested_vmx_misc_low,
  2187. vmx->nested.nested_vmx_misc_high);
  2188. vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2189. vmx->nested.nested_vmx_misc_low |=
  2190. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2191. VMX_MISC_ACTIVITY_HLT;
  2192. vmx->nested.nested_vmx_misc_high = 0;
  2193. }
  2194. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2195. {
  2196. /*
  2197. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  2198. */
  2199. return ((control & high) | low) == control;
  2200. }
  2201. static inline u64 vmx_control_msr(u32 low, u32 high)
  2202. {
  2203. return low | ((u64)high << 32);
  2204. }
  2205. /* Returns 0 on success, non-0 otherwise. */
  2206. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2207. {
  2208. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2209. switch (msr_index) {
  2210. case MSR_IA32_VMX_BASIC:
  2211. /*
  2212. * This MSR reports some information about VMX support. We
  2213. * should return information about the VMX we emulate for the
  2214. * guest, and the VMCS structure we give it - not about the
  2215. * VMX support of the underlying hardware.
  2216. */
  2217. *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
  2218. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2219. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2220. break;
  2221. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2222. case MSR_IA32_VMX_PINBASED_CTLS:
  2223. *pdata = vmx_control_msr(
  2224. vmx->nested.nested_vmx_pinbased_ctls_low,
  2225. vmx->nested.nested_vmx_pinbased_ctls_high);
  2226. break;
  2227. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2228. *pdata = vmx_control_msr(
  2229. vmx->nested.nested_vmx_true_procbased_ctls_low,
  2230. vmx->nested.nested_vmx_procbased_ctls_high);
  2231. break;
  2232. case MSR_IA32_VMX_PROCBASED_CTLS:
  2233. *pdata = vmx_control_msr(
  2234. vmx->nested.nested_vmx_procbased_ctls_low,
  2235. vmx->nested.nested_vmx_procbased_ctls_high);
  2236. break;
  2237. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2238. *pdata = vmx_control_msr(
  2239. vmx->nested.nested_vmx_true_exit_ctls_low,
  2240. vmx->nested.nested_vmx_exit_ctls_high);
  2241. break;
  2242. case MSR_IA32_VMX_EXIT_CTLS:
  2243. *pdata = vmx_control_msr(
  2244. vmx->nested.nested_vmx_exit_ctls_low,
  2245. vmx->nested.nested_vmx_exit_ctls_high);
  2246. break;
  2247. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2248. *pdata = vmx_control_msr(
  2249. vmx->nested.nested_vmx_true_entry_ctls_low,
  2250. vmx->nested.nested_vmx_entry_ctls_high);
  2251. break;
  2252. case MSR_IA32_VMX_ENTRY_CTLS:
  2253. *pdata = vmx_control_msr(
  2254. vmx->nested.nested_vmx_entry_ctls_low,
  2255. vmx->nested.nested_vmx_entry_ctls_high);
  2256. break;
  2257. case MSR_IA32_VMX_MISC:
  2258. *pdata = vmx_control_msr(
  2259. vmx->nested.nested_vmx_misc_low,
  2260. vmx->nested.nested_vmx_misc_high);
  2261. break;
  2262. /*
  2263. * These MSRs specify bits which the guest must keep fixed (on or off)
  2264. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2265. * We picked the standard core2 setting.
  2266. */
  2267. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2268. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2269. case MSR_IA32_VMX_CR0_FIXED0:
  2270. *pdata = VMXON_CR0_ALWAYSON;
  2271. break;
  2272. case MSR_IA32_VMX_CR0_FIXED1:
  2273. *pdata = -1ULL;
  2274. break;
  2275. case MSR_IA32_VMX_CR4_FIXED0:
  2276. *pdata = VMXON_CR4_ALWAYSON;
  2277. break;
  2278. case MSR_IA32_VMX_CR4_FIXED1:
  2279. *pdata = -1ULL;
  2280. break;
  2281. case MSR_IA32_VMX_VMCS_ENUM:
  2282. *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2283. break;
  2284. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2285. *pdata = vmx_control_msr(
  2286. vmx->nested.nested_vmx_secondary_ctls_low,
  2287. vmx->nested.nested_vmx_secondary_ctls_high);
  2288. break;
  2289. case MSR_IA32_VMX_EPT_VPID_CAP:
  2290. /* Currently, no nested vpid support */
  2291. *pdata = vmx->nested.nested_vmx_ept_caps;
  2292. break;
  2293. default:
  2294. return 1;
  2295. }
  2296. return 0;
  2297. }
  2298. /*
  2299. * Reads an msr value (of 'msr_index') into 'pdata'.
  2300. * Returns 0 on success, non-0 otherwise.
  2301. * Assumes vcpu_load() was already called.
  2302. */
  2303. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2304. {
  2305. u64 data;
  2306. struct shared_msr_entry *msr;
  2307. if (!pdata) {
  2308. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  2309. return -EINVAL;
  2310. }
  2311. switch (msr_index) {
  2312. #ifdef CONFIG_X86_64
  2313. case MSR_FS_BASE:
  2314. data = vmcs_readl(GUEST_FS_BASE);
  2315. break;
  2316. case MSR_GS_BASE:
  2317. data = vmcs_readl(GUEST_GS_BASE);
  2318. break;
  2319. case MSR_KERNEL_GS_BASE:
  2320. vmx_load_host_state(to_vmx(vcpu));
  2321. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  2322. break;
  2323. #endif
  2324. case MSR_EFER:
  2325. return kvm_get_msr_common(vcpu, msr_index, pdata);
  2326. case MSR_IA32_TSC:
  2327. data = guest_read_tsc();
  2328. break;
  2329. case MSR_IA32_SYSENTER_CS:
  2330. data = vmcs_read32(GUEST_SYSENTER_CS);
  2331. break;
  2332. case MSR_IA32_SYSENTER_EIP:
  2333. data = vmcs_readl(GUEST_SYSENTER_EIP);
  2334. break;
  2335. case MSR_IA32_SYSENTER_ESP:
  2336. data = vmcs_readl(GUEST_SYSENTER_ESP);
  2337. break;
  2338. case MSR_IA32_BNDCFGS:
  2339. if (!vmx_mpx_supported())
  2340. return 1;
  2341. data = vmcs_read64(GUEST_BNDCFGS);
  2342. break;
  2343. case MSR_IA32_FEATURE_CONTROL:
  2344. if (!nested_vmx_allowed(vcpu))
  2345. return 1;
  2346. data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
  2347. break;
  2348. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2349. if (!nested_vmx_allowed(vcpu))
  2350. return 1;
  2351. return vmx_get_vmx_msr(vcpu, msr_index, pdata);
  2352. case MSR_IA32_XSS:
  2353. if (!vmx_xsaves_supported())
  2354. return 1;
  2355. data = vcpu->arch.ia32_xss;
  2356. break;
  2357. case MSR_TSC_AUX:
  2358. if (!to_vmx(vcpu)->rdtscp_enabled)
  2359. return 1;
  2360. /* Otherwise falls through */
  2361. default:
  2362. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  2363. if (msr) {
  2364. data = msr->data;
  2365. break;
  2366. }
  2367. return kvm_get_msr_common(vcpu, msr_index, pdata);
  2368. }
  2369. *pdata = data;
  2370. return 0;
  2371. }
  2372. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  2373. /*
  2374. * Writes msr value into into the appropriate "register".
  2375. * Returns 0 on success, non-0 otherwise.
  2376. * Assumes vcpu_load() was already called.
  2377. */
  2378. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2379. {
  2380. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2381. struct shared_msr_entry *msr;
  2382. int ret = 0;
  2383. u32 msr_index = msr_info->index;
  2384. u64 data = msr_info->data;
  2385. switch (msr_index) {
  2386. case MSR_EFER:
  2387. ret = kvm_set_msr_common(vcpu, msr_info);
  2388. break;
  2389. #ifdef CONFIG_X86_64
  2390. case MSR_FS_BASE:
  2391. vmx_segment_cache_clear(vmx);
  2392. vmcs_writel(GUEST_FS_BASE, data);
  2393. break;
  2394. case MSR_GS_BASE:
  2395. vmx_segment_cache_clear(vmx);
  2396. vmcs_writel(GUEST_GS_BASE, data);
  2397. break;
  2398. case MSR_KERNEL_GS_BASE:
  2399. vmx_load_host_state(vmx);
  2400. vmx->msr_guest_kernel_gs_base = data;
  2401. break;
  2402. #endif
  2403. case MSR_IA32_SYSENTER_CS:
  2404. vmcs_write32(GUEST_SYSENTER_CS, data);
  2405. break;
  2406. case MSR_IA32_SYSENTER_EIP:
  2407. vmcs_writel(GUEST_SYSENTER_EIP, data);
  2408. break;
  2409. case MSR_IA32_SYSENTER_ESP:
  2410. vmcs_writel(GUEST_SYSENTER_ESP, data);
  2411. break;
  2412. case MSR_IA32_BNDCFGS:
  2413. if (!vmx_mpx_supported())
  2414. return 1;
  2415. vmcs_write64(GUEST_BNDCFGS, data);
  2416. break;
  2417. case MSR_IA32_TSC:
  2418. kvm_write_tsc(vcpu, msr_info);
  2419. break;
  2420. case MSR_IA32_CR_PAT:
  2421. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2422. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  2423. return 1;
  2424. vmcs_write64(GUEST_IA32_PAT, data);
  2425. vcpu->arch.pat = data;
  2426. break;
  2427. }
  2428. ret = kvm_set_msr_common(vcpu, msr_info);
  2429. break;
  2430. case MSR_IA32_TSC_ADJUST:
  2431. ret = kvm_set_msr_common(vcpu, msr_info);
  2432. break;
  2433. case MSR_IA32_FEATURE_CONTROL:
  2434. if (!nested_vmx_allowed(vcpu) ||
  2435. (to_vmx(vcpu)->nested.msr_ia32_feature_control &
  2436. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  2437. return 1;
  2438. vmx->nested.msr_ia32_feature_control = data;
  2439. if (msr_info->host_initiated && data == 0)
  2440. vmx_leave_nested(vcpu);
  2441. break;
  2442. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2443. return 1; /* they are read-only */
  2444. case MSR_IA32_XSS:
  2445. if (!vmx_xsaves_supported())
  2446. return 1;
  2447. /*
  2448. * The only supported bit as of Skylake is bit 8, but
  2449. * it is not supported on KVM.
  2450. */
  2451. if (data != 0)
  2452. return 1;
  2453. vcpu->arch.ia32_xss = data;
  2454. if (vcpu->arch.ia32_xss != host_xss)
  2455. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  2456. vcpu->arch.ia32_xss, host_xss);
  2457. else
  2458. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  2459. break;
  2460. case MSR_TSC_AUX:
  2461. if (!vmx->rdtscp_enabled)
  2462. return 1;
  2463. /* Check reserved bit, higher 32 bits should be zero */
  2464. if ((data >> 32) != 0)
  2465. return 1;
  2466. /* Otherwise falls through */
  2467. default:
  2468. msr = find_msr_entry(vmx, msr_index);
  2469. if (msr) {
  2470. u64 old_msr_data = msr->data;
  2471. msr->data = data;
  2472. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  2473. preempt_disable();
  2474. ret = kvm_set_shared_msr(msr->index, msr->data,
  2475. msr->mask);
  2476. preempt_enable();
  2477. if (ret)
  2478. msr->data = old_msr_data;
  2479. }
  2480. break;
  2481. }
  2482. ret = kvm_set_msr_common(vcpu, msr_info);
  2483. }
  2484. return ret;
  2485. }
  2486. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  2487. {
  2488. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  2489. switch (reg) {
  2490. case VCPU_REGS_RSP:
  2491. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  2492. break;
  2493. case VCPU_REGS_RIP:
  2494. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  2495. break;
  2496. case VCPU_EXREG_PDPTR:
  2497. if (enable_ept)
  2498. ept_save_pdptrs(vcpu);
  2499. break;
  2500. default:
  2501. break;
  2502. }
  2503. }
  2504. static __init int cpu_has_kvm_support(void)
  2505. {
  2506. return cpu_has_vmx();
  2507. }
  2508. static __init int vmx_disabled_by_bios(void)
  2509. {
  2510. u64 msr;
  2511. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  2512. if (msr & FEATURE_CONTROL_LOCKED) {
  2513. /* launched w/ TXT and VMX disabled */
  2514. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2515. && tboot_enabled())
  2516. return 1;
  2517. /* launched w/o TXT and VMX only enabled w/ TXT */
  2518. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2519. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2520. && !tboot_enabled()) {
  2521. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  2522. "activate TXT before enabling KVM\n");
  2523. return 1;
  2524. }
  2525. /* launched w/o TXT and VMX disabled */
  2526. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2527. && !tboot_enabled())
  2528. return 1;
  2529. }
  2530. return 0;
  2531. }
  2532. static void kvm_cpu_vmxon(u64 addr)
  2533. {
  2534. asm volatile (ASM_VMX_VMXON_RAX
  2535. : : "a"(&addr), "m"(addr)
  2536. : "memory", "cc");
  2537. }
  2538. static int hardware_enable(void)
  2539. {
  2540. int cpu = raw_smp_processor_id();
  2541. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  2542. u64 old, test_bits;
  2543. if (cr4_read_shadow() & X86_CR4_VMXE)
  2544. return -EBUSY;
  2545. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  2546. /*
  2547. * Now we can enable the vmclear operation in kdump
  2548. * since the loaded_vmcss_on_cpu list on this cpu
  2549. * has been initialized.
  2550. *
  2551. * Though the cpu is not in VMX operation now, there
  2552. * is no problem to enable the vmclear operation
  2553. * for the loaded_vmcss_on_cpu list is empty!
  2554. */
  2555. crash_enable_local_vmclear(cpu);
  2556. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  2557. test_bits = FEATURE_CONTROL_LOCKED;
  2558. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  2559. if (tboot_enabled())
  2560. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  2561. if ((old & test_bits) != test_bits) {
  2562. /* enable and lock */
  2563. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  2564. }
  2565. cr4_set_bits(X86_CR4_VMXE);
  2566. if (vmm_exclusive) {
  2567. kvm_cpu_vmxon(phys_addr);
  2568. ept_sync_global();
  2569. }
  2570. native_store_gdt(this_cpu_ptr(&host_gdt));
  2571. return 0;
  2572. }
  2573. static void vmclear_local_loaded_vmcss(void)
  2574. {
  2575. int cpu = raw_smp_processor_id();
  2576. struct loaded_vmcs *v, *n;
  2577. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  2578. loaded_vmcss_on_cpu_link)
  2579. __loaded_vmcs_clear(v);
  2580. }
  2581. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  2582. * tricks.
  2583. */
  2584. static void kvm_cpu_vmxoff(void)
  2585. {
  2586. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  2587. }
  2588. static void hardware_disable(void)
  2589. {
  2590. if (vmm_exclusive) {
  2591. vmclear_local_loaded_vmcss();
  2592. kvm_cpu_vmxoff();
  2593. }
  2594. cr4_clear_bits(X86_CR4_VMXE);
  2595. }
  2596. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  2597. u32 msr, u32 *result)
  2598. {
  2599. u32 vmx_msr_low, vmx_msr_high;
  2600. u32 ctl = ctl_min | ctl_opt;
  2601. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2602. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  2603. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  2604. /* Ensure minimum (required) set of control bits are supported. */
  2605. if (ctl_min & ~ctl)
  2606. return -EIO;
  2607. *result = ctl;
  2608. return 0;
  2609. }
  2610. static __init bool allow_1_setting(u32 msr, u32 ctl)
  2611. {
  2612. u32 vmx_msr_low, vmx_msr_high;
  2613. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2614. return vmx_msr_high & ctl;
  2615. }
  2616. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  2617. {
  2618. u32 vmx_msr_low, vmx_msr_high;
  2619. u32 min, opt, min2, opt2;
  2620. u32 _pin_based_exec_control = 0;
  2621. u32 _cpu_based_exec_control = 0;
  2622. u32 _cpu_based_2nd_exec_control = 0;
  2623. u32 _vmexit_control = 0;
  2624. u32 _vmentry_control = 0;
  2625. min = CPU_BASED_HLT_EXITING |
  2626. #ifdef CONFIG_X86_64
  2627. CPU_BASED_CR8_LOAD_EXITING |
  2628. CPU_BASED_CR8_STORE_EXITING |
  2629. #endif
  2630. CPU_BASED_CR3_LOAD_EXITING |
  2631. CPU_BASED_CR3_STORE_EXITING |
  2632. CPU_BASED_USE_IO_BITMAPS |
  2633. CPU_BASED_MOV_DR_EXITING |
  2634. CPU_BASED_USE_TSC_OFFSETING |
  2635. CPU_BASED_MWAIT_EXITING |
  2636. CPU_BASED_MONITOR_EXITING |
  2637. CPU_BASED_INVLPG_EXITING |
  2638. CPU_BASED_RDPMC_EXITING;
  2639. opt = CPU_BASED_TPR_SHADOW |
  2640. CPU_BASED_USE_MSR_BITMAPS |
  2641. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2642. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  2643. &_cpu_based_exec_control) < 0)
  2644. return -EIO;
  2645. #ifdef CONFIG_X86_64
  2646. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2647. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  2648. ~CPU_BASED_CR8_STORE_EXITING;
  2649. #endif
  2650. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  2651. min2 = 0;
  2652. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2653. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2654. SECONDARY_EXEC_WBINVD_EXITING |
  2655. SECONDARY_EXEC_ENABLE_VPID |
  2656. SECONDARY_EXEC_ENABLE_EPT |
  2657. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2658. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  2659. SECONDARY_EXEC_RDTSCP |
  2660. SECONDARY_EXEC_ENABLE_INVPCID |
  2661. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2662. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2663. SECONDARY_EXEC_SHADOW_VMCS |
  2664. SECONDARY_EXEC_XSAVES |
  2665. SECONDARY_EXEC_ENABLE_PML;
  2666. if (adjust_vmx_controls(min2, opt2,
  2667. MSR_IA32_VMX_PROCBASED_CTLS2,
  2668. &_cpu_based_2nd_exec_control) < 0)
  2669. return -EIO;
  2670. }
  2671. #ifndef CONFIG_X86_64
  2672. if (!(_cpu_based_2nd_exec_control &
  2673. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  2674. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  2675. #endif
  2676. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2677. _cpu_based_2nd_exec_control &= ~(
  2678. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2679. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2680. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  2681. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  2682. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  2683. enabled */
  2684. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  2685. CPU_BASED_CR3_STORE_EXITING |
  2686. CPU_BASED_INVLPG_EXITING);
  2687. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  2688. vmx_capability.ept, vmx_capability.vpid);
  2689. }
  2690. min = VM_EXIT_SAVE_DEBUG_CONTROLS;
  2691. #ifdef CONFIG_X86_64
  2692. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  2693. #endif
  2694. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  2695. VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
  2696. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  2697. &_vmexit_control) < 0)
  2698. return -EIO;
  2699. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  2700. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
  2701. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  2702. &_pin_based_exec_control) < 0)
  2703. return -EIO;
  2704. if (!(_cpu_based_2nd_exec_control &
  2705. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
  2706. !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
  2707. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  2708. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2709. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  2710. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  2711. &_vmentry_control) < 0)
  2712. return -EIO;
  2713. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  2714. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  2715. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  2716. return -EIO;
  2717. #ifdef CONFIG_X86_64
  2718. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  2719. if (vmx_msr_high & (1u<<16))
  2720. return -EIO;
  2721. #endif
  2722. /* Require Write-Back (WB) memory type for VMCS accesses. */
  2723. if (((vmx_msr_high >> 18) & 15) != 6)
  2724. return -EIO;
  2725. vmcs_conf->size = vmx_msr_high & 0x1fff;
  2726. vmcs_conf->order = get_order(vmcs_config.size);
  2727. vmcs_conf->revision_id = vmx_msr_low;
  2728. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  2729. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  2730. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  2731. vmcs_conf->vmexit_ctrl = _vmexit_control;
  2732. vmcs_conf->vmentry_ctrl = _vmentry_control;
  2733. cpu_has_load_ia32_efer =
  2734. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2735. VM_ENTRY_LOAD_IA32_EFER)
  2736. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2737. VM_EXIT_LOAD_IA32_EFER);
  2738. cpu_has_load_perf_global_ctrl =
  2739. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2740. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  2741. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2742. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2743. /*
  2744. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  2745. * but due to arrata below it can't be used. Workaround is to use
  2746. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  2747. *
  2748. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  2749. *
  2750. * AAK155 (model 26)
  2751. * AAP115 (model 30)
  2752. * AAT100 (model 37)
  2753. * BC86,AAY89,BD102 (model 44)
  2754. * BA97 (model 46)
  2755. *
  2756. */
  2757. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  2758. switch (boot_cpu_data.x86_model) {
  2759. case 26:
  2760. case 30:
  2761. case 37:
  2762. case 44:
  2763. case 46:
  2764. cpu_has_load_perf_global_ctrl = false;
  2765. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  2766. "does not work properly. Using workaround\n");
  2767. break;
  2768. default:
  2769. break;
  2770. }
  2771. }
  2772. if (cpu_has_xsaves)
  2773. rdmsrl(MSR_IA32_XSS, host_xss);
  2774. return 0;
  2775. }
  2776. static struct vmcs *alloc_vmcs_cpu(int cpu)
  2777. {
  2778. int node = cpu_to_node(cpu);
  2779. struct page *pages;
  2780. struct vmcs *vmcs;
  2781. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  2782. if (!pages)
  2783. return NULL;
  2784. vmcs = page_address(pages);
  2785. memset(vmcs, 0, vmcs_config.size);
  2786. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  2787. return vmcs;
  2788. }
  2789. static struct vmcs *alloc_vmcs(void)
  2790. {
  2791. return alloc_vmcs_cpu(raw_smp_processor_id());
  2792. }
  2793. static void free_vmcs(struct vmcs *vmcs)
  2794. {
  2795. free_pages((unsigned long)vmcs, vmcs_config.order);
  2796. }
  2797. /*
  2798. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  2799. */
  2800. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  2801. {
  2802. if (!loaded_vmcs->vmcs)
  2803. return;
  2804. loaded_vmcs_clear(loaded_vmcs);
  2805. free_vmcs(loaded_vmcs->vmcs);
  2806. loaded_vmcs->vmcs = NULL;
  2807. }
  2808. static void free_kvm_area(void)
  2809. {
  2810. int cpu;
  2811. for_each_possible_cpu(cpu) {
  2812. free_vmcs(per_cpu(vmxarea, cpu));
  2813. per_cpu(vmxarea, cpu) = NULL;
  2814. }
  2815. }
  2816. static void init_vmcs_shadow_fields(void)
  2817. {
  2818. int i, j;
  2819. /* No checks for read only fields yet */
  2820. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  2821. switch (shadow_read_write_fields[i]) {
  2822. case GUEST_BNDCFGS:
  2823. if (!vmx_mpx_supported())
  2824. continue;
  2825. break;
  2826. default:
  2827. break;
  2828. }
  2829. if (j < i)
  2830. shadow_read_write_fields[j] =
  2831. shadow_read_write_fields[i];
  2832. j++;
  2833. }
  2834. max_shadow_read_write_fields = j;
  2835. /* shadowed fields guest access without vmexit */
  2836. for (i = 0; i < max_shadow_read_write_fields; i++) {
  2837. clear_bit(shadow_read_write_fields[i],
  2838. vmx_vmwrite_bitmap);
  2839. clear_bit(shadow_read_write_fields[i],
  2840. vmx_vmread_bitmap);
  2841. }
  2842. for (i = 0; i < max_shadow_read_only_fields; i++)
  2843. clear_bit(shadow_read_only_fields[i],
  2844. vmx_vmread_bitmap);
  2845. }
  2846. static __init int alloc_kvm_area(void)
  2847. {
  2848. int cpu;
  2849. for_each_possible_cpu(cpu) {
  2850. struct vmcs *vmcs;
  2851. vmcs = alloc_vmcs_cpu(cpu);
  2852. if (!vmcs) {
  2853. free_kvm_area();
  2854. return -ENOMEM;
  2855. }
  2856. per_cpu(vmxarea, cpu) = vmcs;
  2857. }
  2858. return 0;
  2859. }
  2860. static bool emulation_required(struct kvm_vcpu *vcpu)
  2861. {
  2862. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2863. }
  2864. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  2865. struct kvm_segment *save)
  2866. {
  2867. if (!emulate_invalid_guest_state) {
  2868. /*
  2869. * CS and SS RPL should be equal during guest entry according
  2870. * to VMX spec, but in reality it is not always so. Since vcpu
  2871. * is in the middle of the transition from real mode to
  2872. * protected mode it is safe to assume that RPL 0 is a good
  2873. * default value.
  2874. */
  2875. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  2876. save->selector &= ~SEGMENT_RPL_MASK;
  2877. save->dpl = save->selector & SEGMENT_RPL_MASK;
  2878. save->s = 1;
  2879. }
  2880. vmx_set_segment(vcpu, save, seg);
  2881. }
  2882. static void enter_pmode(struct kvm_vcpu *vcpu)
  2883. {
  2884. unsigned long flags;
  2885. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2886. /*
  2887. * Update real mode segment cache. It may be not up-to-date if sement
  2888. * register was written while vcpu was in a guest mode.
  2889. */
  2890. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2891. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2892. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2893. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2894. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2895. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2896. vmx->rmode.vm86_active = 0;
  2897. vmx_segment_cache_clear(vmx);
  2898. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2899. flags = vmcs_readl(GUEST_RFLAGS);
  2900. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2901. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2902. vmcs_writel(GUEST_RFLAGS, flags);
  2903. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  2904. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  2905. update_exception_bitmap(vcpu);
  2906. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2907. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2908. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2909. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2910. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2911. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2912. }
  2913. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  2914. {
  2915. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2916. struct kvm_segment var = *save;
  2917. var.dpl = 0x3;
  2918. if (seg == VCPU_SREG_CS)
  2919. var.type = 0x3;
  2920. if (!emulate_invalid_guest_state) {
  2921. var.selector = var.base >> 4;
  2922. var.base = var.base & 0xffff0;
  2923. var.limit = 0xffff;
  2924. var.g = 0;
  2925. var.db = 0;
  2926. var.present = 1;
  2927. var.s = 1;
  2928. var.l = 0;
  2929. var.unusable = 0;
  2930. var.type = 0x3;
  2931. var.avl = 0;
  2932. if (save->base & 0xf)
  2933. printk_once(KERN_WARNING "kvm: segment base is not "
  2934. "paragraph aligned when entering "
  2935. "protected mode (seg=%d)", seg);
  2936. }
  2937. vmcs_write16(sf->selector, var.selector);
  2938. vmcs_write32(sf->base, var.base);
  2939. vmcs_write32(sf->limit, var.limit);
  2940. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  2941. }
  2942. static void enter_rmode(struct kvm_vcpu *vcpu)
  2943. {
  2944. unsigned long flags;
  2945. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2946. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2947. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2948. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2949. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2950. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2951. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2952. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2953. vmx->rmode.vm86_active = 1;
  2954. /*
  2955. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  2956. * vcpu. Warn the user that an update is overdue.
  2957. */
  2958. if (!vcpu->kvm->arch.tss_addr)
  2959. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  2960. "called before entering vcpu\n");
  2961. vmx_segment_cache_clear(vmx);
  2962. vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
  2963. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  2964. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2965. flags = vmcs_readl(GUEST_RFLAGS);
  2966. vmx->rmode.save_rflags = flags;
  2967. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2968. vmcs_writel(GUEST_RFLAGS, flags);
  2969. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  2970. update_exception_bitmap(vcpu);
  2971. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2972. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2973. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2974. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2975. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2976. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2977. kvm_mmu_reset_context(vcpu);
  2978. }
  2979. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  2980. {
  2981. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2982. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  2983. if (!msr)
  2984. return;
  2985. /*
  2986. * Force kernel_gs_base reloading before EFER changes, as control
  2987. * of this msr depends on is_long_mode().
  2988. */
  2989. vmx_load_host_state(to_vmx(vcpu));
  2990. vcpu->arch.efer = efer;
  2991. if (efer & EFER_LMA) {
  2992. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2993. msr->data = efer;
  2994. } else {
  2995. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2996. msr->data = efer & ~EFER_LME;
  2997. }
  2998. setup_msrs(vmx);
  2999. }
  3000. #ifdef CONFIG_X86_64
  3001. static void enter_lmode(struct kvm_vcpu *vcpu)
  3002. {
  3003. u32 guest_tr_ar;
  3004. vmx_segment_cache_clear(to_vmx(vcpu));
  3005. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  3006. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  3007. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  3008. __func__);
  3009. vmcs_write32(GUEST_TR_AR_BYTES,
  3010. (guest_tr_ar & ~AR_TYPE_MASK)
  3011. | AR_TYPE_BUSY_64_TSS);
  3012. }
  3013. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  3014. }
  3015. static void exit_lmode(struct kvm_vcpu *vcpu)
  3016. {
  3017. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3018. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  3019. }
  3020. #endif
  3021. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  3022. {
  3023. vpid_sync_context(to_vmx(vcpu));
  3024. if (enable_ept) {
  3025. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3026. return;
  3027. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  3028. }
  3029. }
  3030. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  3031. {
  3032. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  3033. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  3034. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  3035. }
  3036. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  3037. {
  3038. if (enable_ept && is_paging(vcpu))
  3039. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  3040. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  3041. }
  3042. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  3043. {
  3044. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  3045. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  3046. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  3047. }
  3048. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  3049. {
  3050. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3051. if (!test_bit(VCPU_EXREG_PDPTR,
  3052. (unsigned long *)&vcpu->arch.regs_dirty))
  3053. return;
  3054. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3055. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  3056. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  3057. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  3058. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  3059. }
  3060. }
  3061. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  3062. {
  3063. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3064. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3065. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  3066. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  3067. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  3068. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  3069. }
  3070. __set_bit(VCPU_EXREG_PDPTR,
  3071. (unsigned long *)&vcpu->arch.regs_avail);
  3072. __set_bit(VCPU_EXREG_PDPTR,
  3073. (unsigned long *)&vcpu->arch.regs_dirty);
  3074. }
  3075. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  3076. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  3077. unsigned long cr0,
  3078. struct kvm_vcpu *vcpu)
  3079. {
  3080. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  3081. vmx_decache_cr3(vcpu);
  3082. if (!(cr0 & X86_CR0_PG)) {
  3083. /* From paging/starting to nonpaging */
  3084. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3085. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  3086. (CPU_BASED_CR3_LOAD_EXITING |
  3087. CPU_BASED_CR3_STORE_EXITING));
  3088. vcpu->arch.cr0 = cr0;
  3089. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3090. } else if (!is_paging(vcpu)) {
  3091. /* From nonpaging to paging */
  3092. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3093. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  3094. ~(CPU_BASED_CR3_LOAD_EXITING |
  3095. CPU_BASED_CR3_STORE_EXITING));
  3096. vcpu->arch.cr0 = cr0;
  3097. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3098. }
  3099. if (!(cr0 & X86_CR0_WP))
  3100. *hw_cr0 &= ~X86_CR0_WP;
  3101. }
  3102. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  3103. {
  3104. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3105. unsigned long hw_cr0;
  3106. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  3107. if (enable_unrestricted_guest)
  3108. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  3109. else {
  3110. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  3111. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  3112. enter_pmode(vcpu);
  3113. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  3114. enter_rmode(vcpu);
  3115. }
  3116. #ifdef CONFIG_X86_64
  3117. if (vcpu->arch.efer & EFER_LME) {
  3118. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  3119. enter_lmode(vcpu);
  3120. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  3121. exit_lmode(vcpu);
  3122. }
  3123. #endif
  3124. if (enable_ept)
  3125. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  3126. if (!vcpu->fpu_active)
  3127. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  3128. vmcs_writel(CR0_READ_SHADOW, cr0);
  3129. vmcs_writel(GUEST_CR0, hw_cr0);
  3130. vcpu->arch.cr0 = cr0;
  3131. /* depends on vcpu->arch.cr0 to be set to a new value */
  3132. vmx->emulation_required = emulation_required(vcpu);
  3133. }
  3134. static u64 construct_eptp(unsigned long root_hpa)
  3135. {
  3136. u64 eptp;
  3137. /* TODO write the value reading from MSR */
  3138. eptp = VMX_EPT_DEFAULT_MT |
  3139. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  3140. if (enable_ept_ad_bits)
  3141. eptp |= VMX_EPT_AD_ENABLE_BIT;
  3142. eptp |= (root_hpa & PAGE_MASK);
  3143. return eptp;
  3144. }
  3145. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  3146. {
  3147. unsigned long guest_cr3;
  3148. u64 eptp;
  3149. guest_cr3 = cr3;
  3150. if (enable_ept) {
  3151. eptp = construct_eptp(cr3);
  3152. vmcs_write64(EPT_POINTER, eptp);
  3153. if (is_paging(vcpu) || is_guest_mode(vcpu))
  3154. guest_cr3 = kvm_read_cr3(vcpu);
  3155. else
  3156. guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
  3157. ept_load_pdptrs(vcpu);
  3158. }
  3159. vmx_flush_tlb(vcpu);
  3160. vmcs_writel(GUEST_CR3, guest_cr3);
  3161. }
  3162. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  3163. {
  3164. /*
  3165. * Pass through host's Machine Check Enable value to hw_cr4, which
  3166. * is in force while we are in guest mode. Do not let guests control
  3167. * this bit, even if host CR4.MCE == 0.
  3168. */
  3169. unsigned long hw_cr4 =
  3170. (cr4_read_shadow() & X86_CR4_MCE) |
  3171. (cr4 & ~X86_CR4_MCE) |
  3172. (to_vmx(vcpu)->rmode.vm86_active ?
  3173. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  3174. if (cr4 & X86_CR4_VMXE) {
  3175. /*
  3176. * To use VMXON (and later other VMX instructions), a guest
  3177. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  3178. * So basically the check on whether to allow nested VMX
  3179. * is here.
  3180. */
  3181. if (!nested_vmx_allowed(vcpu))
  3182. return 1;
  3183. }
  3184. if (to_vmx(vcpu)->nested.vmxon &&
  3185. ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
  3186. return 1;
  3187. vcpu->arch.cr4 = cr4;
  3188. if (enable_ept) {
  3189. if (!is_paging(vcpu)) {
  3190. hw_cr4 &= ~X86_CR4_PAE;
  3191. hw_cr4 |= X86_CR4_PSE;
  3192. /*
  3193. * SMEP/SMAP is disabled if CPU is in non-paging mode
  3194. * in hardware. However KVM always uses paging mode to
  3195. * emulate guest non-paging mode with TDP.
  3196. * To emulate this behavior, SMEP/SMAP needs to be
  3197. * manually disabled when guest switches to non-paging
  3198. * mode.
  3199. */
  3200. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
  3201. } else if (!(cr4 & X86_CR4_PAE)) {
  3202. hw_cr4 &= ~X86_CR4_PAE;
  3203. }
  3204. }
  3205. vmcs_writel(CR4_READ_SHADOW, cr4);
  3206. vmcs_writel(GUEST_CR4, hw_cr4);
  3207. return 0;
  3208. }
  3209. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  3210. struct kvm_segment *var, int seg)
  3211. {
  3212. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3213. u32 ar;
  3214. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3215. *var = vmx->rmode.segs[seg];
  3216. if (seg == VCPU_SREG_TR
  3217. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  3218. return;
  3219. var->base = vmx_read_guest_seg_base(vmx, seg);
  3220. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3221. return;
  3222. }
  3223. var->base = vmx_read_guest_seg_base(vmx, seg);
  3224. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  3225. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3226. ar = vmx_read_guest_seg_ar(vmx, seg);
  3227. var->unusable = (ar >> 16) & 1;
  3228. var->type = ar & 15;
  3229. var->s = (ar >> 4) & 1;
  3230. var->dpl = (ar >> 5) & 3;
  3231. /*
  3232. * Some userspaces do not preserve unusable property. Since usable
  3233. * segment has to be present according to VMX spec we can use present
  3234. * property to amend userspace bug by making unusable segment always
  3235. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  3236. * segment as unusable.
  3237. */
  3238. var->present = !var->unusable;
  3239. var->avl = (ar >> 12) & 1;
  3240. var->l = (ar >> 13) & 1;
  3241. var->db = (ar >> 14) & 1;
  3242. var->g = (ar >> 15) & 1;
  3243. }
  3244. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3245. {
  3246. struct kvm_segment s;
  3247. if (to_vmx(vcpu)->rmode.vm86_active) {
  3248. vmx_get_segment(vcpu, &s, seg);
  3249. return s.base;
  3250. }
  3251. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  3252. }
  3253. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  3254. {
  3255. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3256. if (unlikely(vmx->rmode.vm86_active))
  3257. return 0;
  3258. else {
  3259. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  3260. return AR_DPL(ar);
  3261. }
  3262. }
  3263. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  3264. {
  3265. u32 ar;
  3266. if (var->unusable || !var->present)
  3267. ar = 1 << 16;
  3268. else {
  3269. ar = var->type & 15;
  3270. ar |= (var->s & 1) << 4;
  3271. ar |= (var->dpl & 3) << 5;
  3272. ar |= (var->present & 1) << 7;
  3273. ar |= (var->avl & 1) << 12;
  3274. ar |= (var->l & 1) << 13;
  3275. ar |= (var->db & 1) << 14;
  3276. ar |= (var->g & 1) << 15;
  3277. }
  3278. return ar;
  3279. }
  3280. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  3281. struct kvm_segment *var, int seg)
  3282. {
  3283. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3284. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3285. vmx_segment_cache_clear(vmx);
  3286. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3287. vmx->rmode.segs[seg] = *var;
  3288. if (seg == VCPU_SREG_TR)
  3289. vmcs_write16(sf->selector, var->selector);
  3290. else if (var->s)
  3291. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  3292. goto out;
  3293. }
  3294. vmcs_writel(sf->base, var->base);
  3295. vmcs_write32(sf->limit, var->limit);
  3296. vmcs_write16(sf->selector, var->selector);
  3297. /*
  3298. * Fix the "Accessed" bit in AR field of segment registers for older
  3299. * qemu binaries.
  3300. * IA32 arch specifies that at the time of processor reset the
  3301. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  3302. * is setting it to 0 in the userland code. This causes invalid guest
  3303. * state vmexit when "unrestricted guest" mode is turned on.
  3304. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  3305. * tree. Newer qemu binaries with that qemu fix would not need this
  3306. * kvm hack.
  3307. */
  3308. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  3309. var->type |= 0x1; /* Accessed */
  3310. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  3311. out:
  3312. vmx->emulation_required = emulation_required(vcpu);
  3313. }
  3314. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  3315. {
  3316. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  3317. *db = (ar >> 14) & 1;
  3318. *l = (ar >> 13) & 1;
  3319. }
  3320. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3321. {
  3322. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  3323. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  3324. }
  3325. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3326. {
  3327. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  3328. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  3329. }
  3330. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3331. {
  3332. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  3333. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  3334. }
  3335. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3336. {
  3337. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  3338. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  3339. }
  3340. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3341. {
  3342. struct kvm_segment var;
  3343. u32 ar;
  3344. vmx_get_segment(vcpu, &var, seg);
  3345. var.dpl = 0x3;
  3346. if (seg == VCPU_SREG_CS)
  3347. var.type = 0x3;
  3348. ar = vmx_segment_access_rights(&var);
  3349. if (var.base != (var.selector << 4))
  3350. return false;
  3351. if (var.limit != 0xffff)
  3352. return false;
  3353. if (ar != 0xf3)
  3354. return false;
  3355. return true;
  3356. }
  3357. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  3358. {
  3359. struct kvm_segment cs;
  3360. unsigned int cs_rpl;
  3361. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3362. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  3363. if (cs.unusable)
  3364. return false;
  3365. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  3366. return false;
  3367. if (!cs.s)
  3368. return false;
  3369. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  3370. if (cs.dpl > cs_rpl)
  3371. return false;
  3372. } else {
  3373. if (cs.dpl != cs_rpl)
  3374. return false;
  3375. }
  3376. if (!cs.present)
  3377. return false;
  3378. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  3379. return true;
  3380. }
  3381. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  3382. {
  3383. struct kvm_segment ss;
  3384. unsigned int ss_rpl;
  3385. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3386. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  3387. if (ss.unusable)
  3388. return true;
  3389. if (ss.type != 3 && ss.type != 7)
  3390. return false;
  3391. if (!ss.s)
  3392. return false;
  3393. if (ss.dpl != ss_rpl) /* DPL != RPL */
  3394. return false;
  3395. if (!ss.present)
  3396. return false;
  3397. return true;
  3398. }
  3399. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3400. {
  3401. struct kvm_segment var;
  3402. unsigned int rpl;
  3403. vmx_get_segment(vcpu, &var, seg);
  3404. rpl = var.selector & SEGMENT_RPL_MASK;
  3405. if (var.unusable)
  3406. return true;
  3407. if (!var.s)
  3408. return false;
  3409. if (!var.present)
  3410. return false;
  3411. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  3412. if (var.dpl < rpl) /* DPL < RPL */
  3413. return false;
  3414. }
  3415. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  3416. * rights flags
  3417. */
  3418. return true;
  3419. }
  3420. static bool tr_valid(struct kvm_vcpu *vcpu)
  3421. {
  3422. struct kvm_segment tr;
  3423. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  3424. if (tr.unusable)
  3425. return false;
  3426. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  3427. return false;
  3428. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  3429. return false;
  3430. if (!tr.present)
  3431. return false;
  3432. return true;
  3433. }
  3434. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  3435. {
  3436. struct kvm_segment ldtr;
  3437. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  3438. if (ldtr.unusable)
  3439. return true;
  3440. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  3441. return false;
  3442. if (ldtr.type != 2)
  3443. return false;
  3444. if (!ldtr.present)
  3445. return false;
  3446. return true;
  3447. }
  3448. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  3449. {
  3450. struct kvm_segment cs, ss;
  3451. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3452. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3453. return ((cs.selector & SEGMENT_RPL_MASK) ==
  3454. (ss.selector & SEGMENT_RPL_MASK));
  3455. }
  3456. /*
  3457. * Check if guest state is valid. Returns true if valid, false if
  3458. * not.
  3459. * We assume that registers are always usable
  3460. */
  3461. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  3462. {
  3463. if (enable_unrestricted_guest)
  3464. return true;
  3465. /* real mode guest state checks */
  3466. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  3467. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  3468. return false;
  3469. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  3470. return false;
  3471. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  3472. return false;
  3473. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  3474. return false;
  3475. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  3476. return false;
  3477. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  3478. return false;
  3479. } else {
  3480. /* protected mode guest state checks */
  3481. if (!cs_ss_rpl_check(vcpu))
  3482. return false;
  3483. if (!code_segment_valid(vcpu))
  3484. return false;
  3485. if (!stack_segment_valid(vcpu))
  3486. return false;
  3487. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  3488. return false;
  3489. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  3490. return false;
  3491. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  3492. return false;
  3493. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  3494. return false;
  3495. if (!tr_valid(vcpu))
  3496. return false;
  3497. if (!ldtr_valid(vcpu))
  3498. return false;
  3499. }
  3500. /* TODO:
  3501. * - Add checks on RIP
  3502. * - Add checks on RFLAGS
  3503. */
  3504. return true;
  3505. }
  3506. static int init_rmode_tss(struct kvm *kvm)
  3507. {
  3508. gfn_t fn;
  3509. u16 data = 0;
  3510. int idx, r;
  3511. idx = srcu_read_lock(&kvm->srcu);
  3512. fn = kvm->arch.tss_addr >> PAGE_SHIFT;
  3513. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3514. if (r < 0)
  3515. goto out;
  3516. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  3517. r = kvm_write_guest_page(kvm, fn++, &data,
  3518. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  3519. if (r < 0)
  3520. goto out;
  3521. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  3522. if (r < 0)
  3523. goto out;
  3524. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3525. if (r < 0)
  3526. goto out;
  3527. data = ~0;
  3528. r = kvm_write_guest_page(kvm, fn, &data,
  3529. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  3530. sizeof(u8));
  3531. out:
  3532. srcu_read_unlock(&kvm->srcu, idx);
  3533. return r;
  3534. }
  3535. static int init_rmode_identity_map(struct kvm *kvm)
  3536. {
  3537. int i, idx, r = 0;
  3538. pfn_t identity_map_pfn;
  3539. u32 tmp;
  3540. if (!enable_ept)
  3541. return 0;
  3542. /* Protect kvm->arch.ept_identity_pagetable_done. */
  3543. mutex_lock(&kvm->slots_lock);
  3544. if (likely(kvm->arch.ept_identity_pagetable_done))
  3545. goto out2;
  3546. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  3547. r = alloc_identity_pagetable(kvm);
  3548. if (r < 0)
  3549. goto out2;
  3550. idx = srcu_read_lock(&kvm->srcu);
  3551. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  3552. if (r < 0)
  3553. goto out;
  3554. /* Set up identity-mapping pagetable for EPT in real mode */
  3555. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  3556. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  3557. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  3558. r = kvm_write_guest_page(kvm, identity_map_pfn,
  3559. &tmp, i * sizeof(tmp), sizeof(tmp));
  3560. if (r < 0)
  3561. goto out;
  3562. }
  3563. kvm->arch.ept_identity_pagetable_done = true;
  3564. out:
  3565. srcu_read_unlock(&kvm->srcu, idx);
  3566. out2:
  3567. mutex_unlock(&kvm->slots_lock);
  3568. return r;
  3569. }
  3570. static void seg_setup(int seg)
  3571. {
  3572. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3573. unsigned int ar;
  3574. vmcs_write16(sf->selector, 0);
  3575. vmcs_writel(sf->base, 0);
  3576. vmcs_write32(sf->limit, 0xffff);
  3577. ar = 0x93;
  3578. if (seg == VCPU_SREG_CS)
  3579. ar |= 0x08; /* code segment */
  3580. vmcs_write32(sf->ar_bytes, ar);
  3581. }
  3582. static int alloc_apic_access_page(struct kvm *kvm)
  3583. {
  3584. struct page *page;
  3585. struct kvm_userspace_memory_region kvm_userspace_mem;
  3586. int r = 0;
  3587. mutex_lock(&kvm->slots_lock);
  3588. if (kvm->arch.apic_access_page_done)
  3589. goto out;
  3590. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  3591. kvm_userspace_mem.flags = 0;
  3592. kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE;
  3593. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3594. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
  3595. if (r)
  3596. goto out;
  3597. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  3598. if (is_error_page(page)) {
  3599. r = -EFAULT;
  3600. goto out;
  3601. }
  3602. /*
  3603. * Do not pin the page in memory, so that memory hot-unplug
  3604. * is able to migrate it.
  3605. */
  3606. put_page(page);
  3607. kvm->arch.apic_access_page_done = true;
  3608. out:
  3609. mutex_unlock(&kvm->slots_lock);
  3610. return r;
  3611. }
  3612. static int alloc_identity_pagetable(struct kvm *kvm)
  3613. {
  3614. /* Called with kvm->slots_lock held. */
  3615. struct kvm_userspace_memory_region kvm_userspace_mem;
  3616. int r = 0;
  3617. BUG_ON(kvm->arch.ept_identity_pagetable_done);
  3618. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  3619. kvm_userspace_mem.flags = 0;
  3620. kvm_userspace_mem.guest_phys_addr =
  3621. kvm->arch.ept_identity_map_addr;
  3622. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3623. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
  3624. return r;
  3625. }
  3626. static void allocate_vpid(struct vcpu_vmx *vmx)
  3627. {
  3628. int vpid;
  3629. vmx->vpid = 0;
  3630. if (!enable_vpid)
  3631. return;
  3632. spin_lock(&vmx_vpid_lock);
  3633. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  3634. if (vpid < VMX_NR_VPIDS) {
  3635. vmx->vpid = vpid;
  3636. __set_bit(vpid, vmx_vpid_bitmap);
  3637. }
  3638. spin_unlock(&vmx_vpid_lock);
  3639. }
  3640. static void free_vpid(struct vcpu_vmx *vmx)
  3641. {
  3642. if (!enable_vpid)
  3643. return;
  3644. spin_lock(&vmx_vpid_lock);
  3645. if (vmx->vpid != 0)
  3646. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  3647. spin_unlock(&vmx_vpid_lock);
  3648. }
  3649. #define MSR_TYPE_R 1
  3650. #define MSR_TYPE_W 2
  3651. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  3652. u32 msr, int type)
  3653. {
  3654. int f = sizeof(unsigned long);
  3655. if (!cpu_has_vmx_msr_bitmap())
  3656. return;
  3657. /*
  3658. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3659. * have the write-low and read-high bitmap offsets the wrong way round.
  3660. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3661. */
  3662. if (msr <= 0x1fff) {
  3663. if (type & MSR_TYPE_R)
  3664. /* read-low */
  3665. __clear_bit(msr, msr_bitmap + 0x000 / f);
  3666. if (type & MSR_TYPE_W)
  3667. /* write-low */
  3668. __clear_bit(msr, msr_bitmap + 0x800 / f);
  3669. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3670. msr &= 0x1fff;
  3671. if (type & MSR_TYPE_R)
  3672. /* read-high */
  3673. __clear_bit(msr, msr_bitmap + 0x400 / f);
  3674. if (type & MSR_TYPE_W)
  3675. /* write-high */
  3676. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  3677. }
  3678. }
  3679. static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  3680. u32 msr, int type)
  3681. {
  3682. int f = sizeof(unsigned long);
  3683. if (!cpu_has_vmx_msr_bitmap())
  3684. return;
  3685. /*
  3686. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3687. * have the write-low and read-high bitmap offsets the wrong way round.
  3688. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3689. */
  3690. if (msr <= 0x1fff) {
  3691. if (type & MSR_TYPE_R)
  3692. /* read-low */
  3693. __set_bit(msr, msr_bitmap + 0x000 / f);
  3694. if (type & MSR_TYPE_W)
  3695. /* write-low */
  3696. __set_bit(msr, msr_bitmap + 0x800 / f);
  3697. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3698. msr &= 0x1fff;
  3699. if (type & MSR_TYPE_R)
  3700. /* read-high */
  3701. __set_bit(msr, msr_bitmap + 0x400 / f);
  3702. if (type & MSR_TYPE_W)
  3703. /* write-high */
  3704. __set_bit(msr, msr_bitmap + 0xc00 / f);
  3705. }
  3706. }
  3707. /*
  3708. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  3709. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  3710. */
  3711. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  3712. unsigned long *msr_bitmap_nested,
  3713. u32 msr, int type)
  3714. {
  3715. int f = sizeof(unsigned long);
  3716. if (!cpu_has_vmx_msr_bitmap()) {
  3717. WARN_ON(1);
  3718. return;
  3719. }
  3720. /*
  3721. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3722. * have the write-low and read-high bitmap offsets the wrong way round.
  3723. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3724. */
  3725. if (msr <= 0x1fff) {
  3726. if (type & MSR_TYPE_R &&
  3727. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  3728. /* read-low */
  3729. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  3730. if (type & MSR_TYPE_W &&
  3731. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  3732. /* write-low */
  3733. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  3734. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3735. msr &= 0x1fff;
  3736. if (type & MSR_TYPE_R &&
  3737. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  3738. /* read-high */
  3739. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  3740. if (type & MSR_TYPE_W &&
  3741. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  3742. /* write-high */
  3743. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  3744. }
  3745. }
  3746. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  3747. {
  3748. if (!longmode_only)
  3749. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
  3750. msr, MSR_TYPE_R | MSR_TYPE_W);
  3751. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
  3752. msr, MSR_TYPE_R | MSR_TYPE_W);
  3753. }
  3754. static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
  3755. {
  3756. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3757. msr, MSR_TYPE_R);
  3758. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3759. msr, MSR_TYPE_R);
  3760. }
  3761. static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
  3762. {
  3763. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3764. msr, MSR_TYPE_R);
  3765. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3766. msr, MSR_TYPE_R);
  3767. }
  3768. static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
  3769. {
  3770. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3771. msr, MSR_TYPE_W);
  3772. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3773. msr, MSR_TYPE_W);
  3774. }
  3775. static int vmx_vm_has_apicv(struct kvm *kvm)
  3776. {
  3777. return enable_apicv && irqchip_in_kernel(kvm);
  3778. }
  3779. static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  3780. {
  3781. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3782. int max_irr;
  3783. void *vapic_page;
  3784. u16 status;
  3785. if (vmx->nested.pi_desc &&
  3786. vmx->nested.pi_pending) {
  3787. vmx->nested.pi_pending = false;
  3788. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  3789. return 0;
  3790. max_irr = find_last_bit(
  3791. (unsigned long *)vmx->nested.pi_desc->pir, 256);
  3792. if (max_irr == 256)
  3793. return 0;
  3794. vapic_page = kmap(vmx->nested.virtual_apic_page);
  3795. if (!vapic_page) {
  3796. WARN_ON(1);
  3797. return -ENOMEM;
  3798. }
  3799. __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
  3800. kunmap(vmx->nested.virtual_apic_page);
  3801. status = vmcs_read16(GUEST_INTR_STATUS);
  3802. if ((u8)max_irr > ((u8)status & 0xff)) {
  3803. status &= ~0xff;
  3804. status |= (u8)max_irr;
  3805. vmcs_write16(GUEST_INTR_STATUS, status);
  3806. }
  3807. }
  3808. return 0;
  3809. }
  3810. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
  3811. {
  3812. #ifdef CONFIG_SMP
  3813. if (vcpu->mode == IN_GUEST_MODE) {
  3814. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
  3815. POSTED_INTR_VECTOR);
  3816. return true;
  3817. }
  3818. #endif
  3819. return false;
  3820. }
  3821. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  3822. int vector)
  3823. {
  3824. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3825. if (is_guest_mode(vcpu) &&
  3826. vector == vmx->nested.posted_intr_nv) {
  3827. /* the PIR and ON have been set by L1. */
  3828. kvm_vcpu_trigger_posted_interrupt(vcpu);
  3829. /*
  3830. * If a posted intr is not recognized by hardware,
  3831. * we will accomplish it in the next vmentry.
  3832. */
  3833. vmx->nested.pi_pending = true;
  3834. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3835. return 0;
  3836. }
  3837. return -1;
  3838. }
  3839. /*
  3840. * Send interrupt to vcpu via posted interrupt way.
  3841. * 1. If target vcpu is running(non-root mode), send posted interrupt
  3842. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  3843. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  3844. * interrupt from PIR in next vmentry.
  3845. */
  3846. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  3847. {
  3848. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3849. int r;
  3850. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  3851. if (!r)
  3852. return;
  3853. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  3854. return;
  3855. r = pi_test_and_set_on(&vmx->pi_desc);
  3856. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3857. if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
  3858. kvm_vcpu_kick(vcpu);
  3859. }
  3860. static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  3861. {
  3862. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3863. if (!pi_test_and_clear_on(&vmx->pi_desc))
  3864. return;
  3865. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
  3866. }
  3867. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
  3868. {
  3869. return;
  3870. }
  3871. /*
  3872. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  3873. * will not change in the lifetime of the guest.
  3874. * Note that host-state that does change is set elsewhere. E.g., host-state
  3875. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  3876. */
  3877. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  3878. {
  3879. u32 low32, high32;
  3880. unsigned long tmpl;
  3881. struct desc_ptr dt;
  3882. unsigned long cr4;
  3883. vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
  3884. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  3885. /* Save the most likely value for this task's CR4 in the VMCS. */
  3886. cr4 = cr4_read_shadow();
  3887. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  3888. vmx->host_state.vmcs_host_cr4 = cr4;
  3889. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  3890. #ifdef CONFIG_X86_64
  3891. /*
  3892. * Load null selectors, so we can avoid reloading them in
  3893. * __vmx_load_host_state(), in case userspace uses the null selectors
  3894. * too (the expected case).
  3895. */
  3896. vmcs_write16(HOST_DS_SELECTOR, 0);
  3897. vmcs_write16(HOST_ES_SELECTOR, 0);
  3898. #else
  3899. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3900. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3901. #endif
  3902. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3903. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  3904. native_store_idt(&dt);
  3905. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  3906. vmx->host_idt_base = dt.address;
  3907. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  3908. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  3909. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  3910. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  3911. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  3912. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  3913. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  3914. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  3915. }
  3916. }
  3917. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  3918. {
  3919. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  3920. if (enable_ept)
  3921. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  3922. if (is_guest_mode(&vmx->vcpu))
  3923. vmx->vcpu.arch.cr4_guest_owned_bits &=
  3924. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  3925. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  3926. }
  3927. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  3928. {
  3929. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  3930. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3931. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  3932. return pin_based_exec_ctrl;
  3933. }
  3934. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  3935. {
  3936. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  3937. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  3938. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  3939. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  3940. exec_control &= ~CPU_BASED_TPR_SHADOW;
  3941. #ifdef CONFIG_X86_64
  3942. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  3943. CPU_BASED_CR8_LOAD_EXITING;
  3944. #endif
  3945. }
  3946. if (!enable_ept)
  3947. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  3948. CPU_BASED_CR3_LOAD_EXITING |
  3949. CPU_BASED_INVLPG_EXITING;
  3950. return exec_control;
  3951. }
  3952. static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
  3953. {
  3954. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  3955. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3956. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3957. if (vmx->vpid == 0)
  3958. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  3959. if (!enable_ept) {
  3960. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  3961. enable_unrestricted_guest = 0;
  3962. /* Enable INVPCID for non-ept guests may cause performance regression. */
  3963. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  3964. }
  3965. if (!enable_unrestricted_guest)
  3966. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3967. if (!ple_gap)
  3968. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  3969. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3970. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3971. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3972. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  3973. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  3974. (handle_vmptrld).
  3975. We can NOT enable shadow_vmcs here because we don't have yet
  3976. a current VMCS12
  3977. */
  3978. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  3979. /* PML is enabled/disabled in creating/destorying vcpu */
  3980. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  3981. return exec_control;
  3982. }
  3983. static void ept_set_mmio_spte_mask(void)
  3984. {
  3985. /*
  3986. * EPT Misconfigurations can be generated if the value of bits 2:0
  3987. * of an EPT paging-structure entry is 110b (write/execute).
  3988. * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
  3989. * spte.
  3990. */
  3991. kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
  3992. }
  3993. #define VMX_XSS_EXIT_BITMAP 0
  3994. /*
  3995. * Sets up the vmcs for emulated real mode.
  3996. */
  3997. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  3998. {
  3999. #ifdef CONFIG_X86_64
  4000. unsigned long a;
  4001. #endif
  4002. int i;
  4003. /* I/O */
  4004. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  4005. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  4006. if (enable_shadow_vmcs) {
  4007. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  4008. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  4009. }
  4010. if (cpu_has_vmx_msr_bitmap())
  4011. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  4012. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  4013. /* Control */
  4014. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4015. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  4016. if (cpu_has_secondary_exec_ctrls()) {
  4017. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  4018. vmx_secondary_exec_control(vmx));
  4019. }
  4020. if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
  4021. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  4022. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  4023. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  4024. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  4025. vmcs_write16(GUEST_INTR_STATUS, 0);
  4026. vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  4027. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  4028. }
  4029. if (ple_gap) {
  4030. vmcs_write32(PLE_GAP, ple_gap);
  4031. vmx->ple_window = ple_window;
  4032. vmx->ple_window_dirty = true;
  4033. }
  4034. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  4035. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  4036. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  4037. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  4038. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  4039. vmx_set_constant_host_state(vmx);
  4040. #ifdef CONFIG_X86_64
  4041. rdmsrl(MSR_FS_BASE, a);
  4042. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  4043. rdmsrl(MSR_GS_BASE, a);
  4044. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  4045. #else
  4046. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  4047. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  4048. #endif
  4049. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  4050. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  4051. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  4052. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  4053. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  4054. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  4055. u32 msr_low, msr_high;
  4056. u64 host_pat;
  4057. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  4058. host_pat = msr_low | ((u64) msr_high << 32);
  4059. /* Write the default value follow host pat */
  4060. vmcs_write64(GUEST_IA32_PAT, host_pat);
  4061. /* Keep arch.pat sync with GUEST_IA32_PAT */
  4062. vmx->vcpu.arch.pat = host_pat;
  4063. }
  4064. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  4065. u32 index = vmx_msr_index[i];
  4066. u32 data_low, data_high;
  4067. int j = vmx->nmsrs;
  4068. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  4069. continue;
  4070. if (wrmsr_safe(index, data_low, data_high) < 0)
  4071. continue;
  4072. vmx->guest_msrs[j].index = i;
  4073. vmx->guest_msrs[j].data = 0;
  4074. vmx->guest_msrs[j].mask = -1ull;
  4075. ++vmx->nmsrs;
  4076. }
  4077. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  4078. /* 22.2.1, 20.8.1 */
  4079. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  4080. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  4081. set_cr4_guest_host_mask(vmx);
  4082. if (vmx_xsaves_supported())
  4083. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  4084. return 0;
  4085. }
  4086. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  4087. {
  4088. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4089. struct msr_data apic_base_msr;
  4090. vmx->rmode.vm86_active = 0;
  4091. vmx->soft_vnmi_blocked = 0;
  4092. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  4093. kvm_set_cr8(&vmx->vcpu, 0);
  4094. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
  4095. if (kvm_vcpu_is_reset_bsp(&vmx->vcpu))
  4096. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  4097. apic_base_msr.host_initiated = true;
  4098. kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
  4099. vmx_segment_cache_clear(vmx);
  4100. seg_setup(VCPU_SREG_CS);
  4101. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  4102. vmcs_write32(GUEST_CS_BASE, 0xffff0000);
  4103. seg_setup(VCPU_SREG_DS);
  4104. seg_setup(VCPU_SREG_ES);
  4105. seg_setup(VCPU_SREG_FS);
  4106. seg_setup(VCPU_SREG_GS);
  4107. seg_setup(VCPU_SREG_SS);
  4108. vmcs_write16(GUEST_TR_SELECTOR, 0);
  4109. vmcs_writel(GUEST_TR_BASE, 0);
  4110. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  4111. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4112. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  4113. vmcs_writel(GUEST_LDTR_BASE, 0);
  4114. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  4115. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  4116. vmcs_write32(GUEST_SYSENTER_CS, 0);
  4117. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  4118. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  4119. vmcs_writel(GUEST_RFLAGS, 0x02);
  4120. kvm_rip_write(vcpu, 0xfff0);
  4121. vmcs_writel(GUEST_GDTR_BASE, 0);
  4122. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  4123. vmcs_writel(GUEST_IDTR_BASE, 0);
  4124. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  4125. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  4126. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  4127. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  4128. /* Special registers */
  4129. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  4130. setup_msrs(vmx);
  4131. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  4132. if (cpu_has_vmx_tpr_shadow()) {
  4133. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  4134. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  4135. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  4136. __pa(vmx->vcpu.arch.apic->regs));
  4137. vmcs_write32(TPR_THRESHOLD, 0);
  4138. }
  4139. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  4140. if (vmx_vm_has_apicv(vcpu->kvm))
  4141. memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
  4142. if (vmx->vpid != 0)
  4143. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  4144. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  4145. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  4146. vmx_set_cr4(&vmx->vcpu, 0);
  4147. vmx_set_efer(&vmx->vcpu, 0);
  4148. vmx_fpu_activate(&vmx->vcpu);
  4149. update_exception_bitmap(&vmx->vcpu);
  4150. vpid_sync_context(vmx);
  4151. }
  4152. /*
  4153. * In nested virtualization, check if L1 asked to exit on external interrupts.
  4154. * For most existing hypervisors, this will always return true.
  4155. */
  4156. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  4157. {
  4158. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  4159. PIN_BASED_EXT_INTR_MASK;
  4160. }
  4161. /*
  4162. * In nested virtualization, check if L1 has set
  4163. * VM_EXIT_ACK_INTR_ON_EXIT
  4164. */
  4165. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  4166. {
  4167. return get_vmcs12(vcpu)->vm_exit_controls &
  4168. VM_EXIT_ACK_INTR_ON_EXIT;
  4169. }
  4170. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  4171. {
  4172. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  4173. PIN_BASED_NMI_EXITING;
  4174. }
  4175. static void enable_irq_window(struct kvm_vcpu *vcpu)
  4176. {
  4177. u32 cpu_based_vm_exec_control;
  4178. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4179. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  4180. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4181. }
  4182. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  4183. {
  4184. u32 cpu_based_vm_exec_control;
  4185. if (!cpu_has_virtual_nmis() ||
  4186. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  4187. enable_irq_window(vcpu);
  4188. return;
  4189. }
  4190. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4191. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  4192. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4193. }
  4194. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  4195. {
  4196. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4197. uint32_t intr;
  4198. int irq = vcpu->arch.interrupt.nr;
  4199. trace_kvm_inj_virq(irq);
  4200. ++vcpu->stat.irq_injections;
  4201. if (vmx->rmode.vm86_active) {
  4202. int inc_eip = 0;
  4203. if (vcpu->arch.interrupt.soft)
  4204. inc_eip = vcpu->arch.event_exit_inst_len;
  4205. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  4206. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4207. return;
  4208. }
  4209. intr = irq | INTR_INFO_VALID_MASK;
  4210. if (vcpu->arch.interrupt.soft) {
  4211. intr |= INTR_TYPE_SOFT_INTR;
  4212. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  4213. vmx->vcpu.arch.event_exit_inst_len);
  4214. } else
  4215. intr |= INTR_TYPE_EXT_INTR;
  4216. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  4217. }
  4218. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  4219. {
  4220. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4221. if (is_guest_mode(vcpu))
  4222. return;
  4223. if (!cpu_has_virtual_nmis()) {
  4224. /*
  4225. * Tracking the NMI-blocked state in software is built upon
  4226. * finding the next open IRQ window. This, in turn, depends on
  4227. * well-behaving guests: They have to keep IRQs disabled at
  4228. * least as long as the NMI handler runs. Otherwise we may
  4229. * cause NMI nesting, maybe breaking the guest. But as this is
  4230. * highly unlikely, we can live with the residual risk.
  4231. */
  4232. vmx->soft_vnmi_blocked = 1;
  4233. vmx->vnmi_blocked_time = 0;
  4234. }
  4235. ++vcpu->stat.nmi_injections;
  4236. vmx->nmi_known_unmasked = false;
  4237. if (vmx->rmode.vm86_active) {
  4238. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  4239. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4240. return;
  4241. }
  4242. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  4243. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  4244. }
  4245. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  4246. {
  4247. if (!cpu_has_virtual_nmis())
  4248. return to_vmx(vcpu)->soft_vnmi_blocked;
  4249. if (to_vmx(vcpu)->nmi_known_unmasked)
  4250. return false;
  4251. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  4252. }
  4253. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  4254. {
  4255. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4256. if (!cpu_has_virtual_nmis()) {
  4257. if (vmx->soft_vnmi_blocked != masked) {
  4258. vmx->soft_vnmi_blocked = masked;
  4259. vmx->vnmi_blocked_time = 0;
  4260. }
  4261. } else {
  4262. vmx->nmi_known_unmasked = !masked;
  4263. if (masked)
  4264. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  4265. GUEST_INTR_STATE_NMI);
  4266. else
  4267. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  4268. GUEST_INTR_STATE_NMI);
  4269. }
  4270. }
  4271. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  4272. {
  4273. if (to_vmx(vcpu)->nested.nested_run_pending)
  4274. return 0;
  4275. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  4276. return 0;
  4277. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4278. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  4279. | GUEST_INTR_STATE_NMI));
  4280. }
  4281. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  4282. {
  4283. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  4284. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  4285. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4286. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  4287. }
  4288. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  4289. {
  4290. int ret;
  4291. struct kvm_userspace_memory_region tss_mem = {
  4292. .slot = TSS_PRIVATE_MEMSLOT,
  4293. .guest_phys_addr = addr,
  4294. .memory_size = PAGE_SIZE * 3,
  4295. .flags = 0,
  4296. };
  4297. ret = kvm_set_memory_region(kvm, &tss_mem);
  4298. if (ret)
  4299. return ret;
  4300. kvm->arch.tss_addr = addr;
  4301. return init_rmode_tss(kvm);
  4302. }
  4303. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  4304. {
  4305. switch (vec) {
  4306. case BP_VECTOR:
  4307. /*
  4308. * Update instruction length as we may reinject the exception
  4309. * from user space while in guest debugging mode.
  4310. */
  4311. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  4312. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4313. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  4314. return false;
  4315. /* fall through */
  4316. case DB_VECTOR:
  4317. if (vcpu->guest_debug &
  4318. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  4319. return false;
  4320. /* fall through */
  4321. case DE_VECTOR:
  4322. case OF_VECTOR:
  4323. case BR_VECTOR:
  4324. case UD_VECTOR:
  4325. case DF_VECTOR:
  4326. case SS_VECTOR:
  4327. case GP_VECTOR:
  4328. case MF_VECTOR:
  4329. return true;
  4330. break;
  4331. }
  4332. return false;
  4333. }
  4334. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  4335. int vec, u32 err_code)
  4336. {
  4337. /*
  4338. * Instruction with address size override prefix opcode 0x67
  4339. * Cause the #SS fault with 0 error code in VM86 mode.
  4340. */
  4341. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  4342. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  4343. if (vcpu->arch.halt_request) {
  4344. vcpu->arch.halt_request = 0;
  4345. return kvm_vcpu_halt(vcpu);
  4346. }
  4347. return 1;
  4348. }
  4349. return 0;
  4350. }
  4351. /*
  4352. * Forward all other exceptions that are valid in real mode.
  4353. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  4354. * the required debugging infrastructure rework.
  4355. */
  4356. kvm_queue_exception(vcpu, vec);
  4357. return 1;
  4358. }
  4359. /*
  4360. * Trigger machine check on the host. We assume all the MSRs are already set up
  4361. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  4362. * We pass a fake environment to the machine check handler because we want
  4363. * the guest to be always treated like user space, no matter what context
  4364. * it used internally.
  4365. */
  4366. static void kvm_machine_check(void)
  4367. {
  4368. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  4369. struct pt_regs regs = {
  4370. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  4371. .flags = X86_EFLAGS_IF,
  4372. };
  4373. do_machine_check(&regs, 0);
  4374. #endif
  4375. }
  4376. static int handle_machine_check(struct kvm_vcpu *vcpu)
  4377. {
  4378. /* already handled by vcpu_run */
  4379. return 1;
  4380. }
  4381. static int handle_exception(struct kvm_vcpu *vcpu)
  4382. {
  4383. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4384. struct kvm_run *kvm_run = vcpu->run;
  4385. u32 intr_info, ex_no, error_code;
  4386. unsigned long cr2, rip, dr6;
  4387. u32 vect_info;
  4388. enum emulation_result er;
  4389. vect_info = vmx->idt_vectoring_info;
  4390. intr_info = vmx->exit_intr_info;
  4391. if (is_machine_check(intr_info))
  4392. return handle_machine_check(vcpu);
  4393. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  4394. return 1; /* already handled by vmx_vcpu_run() */
  4395. if (is_no_device(intr_info)) {
  4396. vmx_fpu_activate(vcpu);
  4397. return 1;
  4398. }
  4399. if (is_invalid_opcode(intr_info)) {
  4400. if (is_guest_mode(vcpu)) {
  4401. kvm_queue_exception(vcpu, UD_VECTOR);
  4402. return 1;
  4403. }
  4404. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  4405. if (er != EMULATE_DONE)
  4406. kvm_queue_exception(vcpu, UD_VECTOR);
  4407. return 1;
  4408. }
  4409. error_code = 0;
  4410. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  4411. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  4412. /*
  4413. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  4414. * MMIO, it is better to report an internal error.
  4415. * See the comments in vmx_handle_exit.
  4416. */
  4417. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  4418. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  4419. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4420. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  4421. vcpu->run->internal.ndata = 3;
  4422. vcpu->run->internal.data[0] = vect_info;
  4423. vcpu->run->internal.data[1] = intr_info;
  4424. vcpu->run->internal.data[2] = error_code;
  4425. return 0;
  4426. }
  4427. if (is_page_fault(intr_info)) {
  4428. /* EPT won't cause page fault directly */
  4429. BUG_ON(enable_ept);
  4430. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  4431. trace_kvm_page_fault(cr2, error_code);
  4432. if (kvm_event_needs_reinjection(vcpu))
  4433. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  4434. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  4435. }
  4436. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  4437. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  4438. return handle_rmode_exception(vcpu, ex_no, error_code);
  4439. switch (ex_no) {
  4440. case DB_VECTOR:
  4441. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  4442. if (!(vcpu->guest_debug &
  4443. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  4444. vcpu->arch.dr6 &= ~15;
  4445. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  4446. if (!(dr6 & ~DR6_RESERVED)) /* icebp */
  4447. skip_emulated_instruction(vcpu);
  4448. kvm_queue_exception(vcpu, DB_VECTOR);
  4449. return 1;
  4450. }
  4451. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  4452. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  4453. /* fall through */
  4454. case BP_VECTOR:
  4455. /*
  4456. * Update instruction length as we may reinject #BP from
  4457. * user space while in guest debugging mode. Reading it for
  4458. * #DB as well causes no harm, it is not used in that case.
  4459. */
  4460. vmx->vcpu.arch.event_exit_inst_len =
  4461. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4462. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  4463. rip = kvm_rip_read(vcpu);
  4464. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  4465. kvm_run->debug.arch.exception = ex_no;
  4466. break;
  4467. default:
  4468. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  4469. kvm_run->ex.exception = ex_no;
  4470. kvm_run->ex.error_code = error_code;
  4471. break;
  4472. }
  4473. return 0;
  4474. }
  4475. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  4476. {
  4477. ++vcpu->stat.irq_exits;
  4478. return 1;
  4479. }
  4480. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  4481. {
  4482. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4483. return 0;
  4484. }
  4485. static int handle_io(struct kvm_vcpu *vcpu)
  4486. {
  4487. unsigned long exit_qualification;
  4488. int size, in, string;
  4489. unsigned port;
  4490. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4491. string = (exit_qualification & 16) != 0;
  4492. in = (exit_qualification & 8) != 0;
  4493. ++vcpu->stat.io_exits;
  4494. if (string || in)
  4495. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4496. port = exit_qualification >> 16;
  4497. size = (exit_qualification & 7) + 1;
  4498. skip_emulated_instruction(vcpu);
  4499. return kvm_fast_pio_out(vcpu, size, port);
  4500. }
  4501. static void
  4502. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4503. {
  4504. /*
  4505. * Patch in the VMCALL instruction:
  4506. */
  4507. hypercall[0] = 0x0f;
  4508. hypercall[1] = 0x01;
  4509. hypercall[2] = 0xc1;
  4510. }
  4511. static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4512. {
  4513. unsigned long always_on = VMXON_CR0_ALWAYSON;
  4514. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4515. if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
  4516. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4517. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4518. always_on &= ~(X86_CR0_PE | X86_CR0_PG);
  4519. return (val & always_on) == always_on;
  4520. }
  4521. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  4522. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  4523. {
  4524. if (is_guest_mode(vcpu)) {
  4525. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4526. unsigned long orig_val = val;
  4527. /*
  4528. * We get here when L2 changed cr0 in a way that did not change
  4529. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  4530. * but did change L0 shadowed bits. So we first calculate the
  4531. * effective cr0 value that L1 would like to write into the
  4532. * hardware. It consists of the L2-owned bits from the new
  4533. * value combined with the L1-owned bits from L1's guest_cr0.
  4534. */
  4535. val = (val & ~vmcs12->cr0_guest_host_mask) |
  4536. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  4537. if (!nested_cr0_valid(vcpu, val))
  4538. return 1;
  4539. if (kvm_set_cr0(vcpu, val))
  4540. return 1;
  4541. vmcs_writel(CR0_READ_SHADOW, orig_val);
  4542. return 0;
  4543. } else {
  4544. if (to_vmx(vcpu)->nested.vmxon &&
  4545. ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
  4546. return 1;
  4547. return kvm_set_cr0(vcpu, val);
  4548. }
  4549. }
  4550. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  4551. {
  4552. if (is_guest_mode(vcpu)) {
  4553. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4554. unsigned long orig_val = val;
  4555. /* analogously to handle_set_cr0 */
  4556. val = (val & ~vmcs12->cr4_guest_host_mask) |
  4557. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  4558. if (kvm_set_cr4(vcpu, val))
  4559. return 1;
  4560. vmcs_writel(CR4_READ_SHADOW, orig_val);
  4561. return 0;
  4562. } else
  4563. return kvm_set_cr4(vcpu, val);
  4564. }
  4565. /* called to set cr0 as approriate for clts instruction exit. */
  4566. static void handle_clts(struct kvm_vcpu *vcpu)
  4567. {
  4568. if (is_guest_mode(vcpu)) {
  4569. /*
  4570. * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
  4571. * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
  4572. * just pretend it's off (also in arch.cr0 for fpu_activate).
  4573. */
  4574. vmcs_writel(CR0_READ_SHADOW,
  4575. vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
  4576. vcpu->arch.cr0 &= ~X86_CR0_TS;
  4577. } else
  4578. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  4579. }
  4580. static int handle_cr(struct kvm_vcpu *vcpu)
  4581. {
  4582. unsigned long exit_qualification, val;
  4583. int cr;
  4584. int reg;
  4585. int err;
  4586. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4587. cr = exit_qualification & 15;
  4588. reg = (exit_qualification >> 8) & 15;
  4589. switch ((exit_qualification >> 4) & 3) {
  4590. case 0: /* mov to cr */
  4591. val = kvm_register_readl(vcpu, reg);
  4592. trace_kvm_cr_write(cr, val);
  4593. switch (cr) {
  4594. case 0:
  4595. err = handle_set_cr0(vcpu, val);
  4596. kvm_complete_insn_gp(vcpu, err);
  4597. return 1;
  4598. case 3:
  4599. err = kvm_set_cr3(vcpu, val);
  4600. kvm_complete_insn_gp(vcpu, err);
  4601. return 1;
  4602. case 4:
  4603. err = handle_set_cr4(vcpu, val);
  4604. kvm_complete_insn_gp(vcpu, err);
  4605. return 1;
  4606. case 8: {
  4607. u8 cr8_prev = kvm_get_cr8(vcpu);
  4608. u8 cr8 = (u8)val;
  4609. err = kvm_set_cr8(vcpu, cr8);
  4610. kvm_complete_insn_gp(vcpu, err);
  4611. if (irqchip_in_kernel(vcpu->kvm))
  4612. return 1;
  4613. if (cr8_prev <= cr8)
  4614. return 1;
  4615. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  4616. return 0;
  4617. }
  4618. }
  4619. break;
  4620. case 2: /* clts */
  4621. handle_clts(vcpu);
  4622. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  4623. skip_emulated_instruction(vcpu);
  4624. vmx_fpu_activate(vcpu);
  4625. return 1;
  4626. case 1: /*mov from cr*/
  4627. switch (cr) {
  4628. case 3:
  4629. val = kvm_read_cr3(vcpu);
  4630. kvm_register_write(vcpu, reg, val);
  4631. trace_kvm_cr_read(cr, val);
  4632. skip_emulated_instruction(vcpu);
  4633. return 1;
  4634. case 8:
  4635. val = kvm_get_cr8(vcpu);
  4636. kvm_register_write(vcpu, reg, val);
  4637. trace_kvm_cr_read(cr, val);
  4638. skip_emulated_instruction(vcpu);
  4639. return 1;
  4640. }
  4641. break;
  4642. case 3: /* lmsw */
  4643. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  4644. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  4645. kvm_lmsw(vcpu, val);
  4646. skip_emulated_instruction(vcpu);
  4647. return 1;
  4648. default:
  4649. break;
  4650. }
  4651. vcpu->run->exit_reason = 0;
  4652. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  4653. (int)(exit_qualification >> 4) & 3, cr);
  4654. return 0;
  4655. }
  4656. static int handle_dr(struct kvm_vcpu *vcpu)
  4657. {
  4658. unsigned long exit_qualification;
  4659. int dr, dr7, reg;
  4660. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4661. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  4662. /* First, if DR does not exist, trigger UD */
  4663. if (!kvm_require_dr(vcpu, dr))
  4664. return 1;
  4665. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  4666. if (!kvm_require_cpl(vcpu, 0))
  4667. return 1;
  4668. dr7 = vmcs_readl(GUEST_DR7);
  4669. if (dr7 & DR7_GD) {
  4670. /*
  4671. * As the vm-exit takes precedence over the debug trap, we
  4672. * need to emulate the latter, either for the host or the
  4673. * guest debugging itself.
  4674. */
  4675. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4676. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  4677. vcpu->run->debug.arch.dr7 = dr7;
  4678. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  4679. vcpu->run->debug.arch.exception = DB_VECTOR;
  4680. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  4681. return 0;
  4682. } else {
  4683. vcpu->arch.dr6 &= ~15;
  4684. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  4685. kvm_queue_exception(vcpu, DB_VECTOR);
  4686. return 1;
  4687. }
  4688. }
  4689. if (vcpu->guest_debug == 0) {
  4690. u32 cpu_based_vm_exec_control;
  4691. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4692. cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  4693. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4694. /*
  4695. * No more DR vmexits; force a reload of the debug registers
  4696. * and reenter on this instruction. The next vmexit will
  4697. * retrieve the full state of the debug registers.
  4698. */
  4699. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  4700. return 1;
  4701. }
  4702. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  4703. if (exit_qualification & TYPE_MOV_FROM_DR) {
  4704. unsigned long val;
  4705. if (kvm_get_dr(vcpu, dr, &val))
  4706. return 1;
  4707. kvm_register_write(vcpu, reg, val);
  4708. } else
  4709. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  4710. return 1;
  4711. skip_emulated_instruction(vcpu);
  4712. return 1;
  4713. }
  4714. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  4715. {
  4716. return vcpu->arch.dr6;
  4717. }
  4718. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  4719. {
  4720. }
  4721. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  4722. {
  4723. u32 cpu_based_vm_exec_control;
  4724. get_debugreg(vcpu->arch.db[0], 0);
  4725. get_debugreg(vcpu->arch.db[1], 1);
  4726. get_debugreg(vcpu->arch.db[2], 2);
  4727. get_debugreg(vcpu->arch.db[3], 3);
  4728. get_debugreg(vcpu->arch.dr6, 6);
  4729. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  4730. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  4731. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4732. cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
  4733. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4734. }
  4735. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  4736. {
  4737. vmcs_writel(GUEST_DR7, val);
  4738. }
  4739. static int handle_cpuid(struct kvm_vcpu *vcpu)
  4740. {
  4741. kvm_emulate_cpuid(vcpu);
  4742. return 1;
  4743. }
  4744. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  4745. {
  4746. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4747. u64 data;
  4748. if (vmx_get_msr(vcpu, ecx, &data)) {
  4749. trace_kvm_msr_read_ex(ecx);
  4750. kvm_inject_gp(vcpu, 0);
  4751. return 1;
  4752. }
  4753. trace_kvm_msr_read(ecx, data);
  4754. /* FIXME: handling of bits 32:63 of rax, rdx */
  4755. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  4756. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  4757. skip_emulated_instruction(vcpu);
  4758. return 1;
  4759. }
  4760. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  4761. {
  4762. struct msr_data msr;
  4763. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4764. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  4765. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  4766. msr.data = data;
  4767. msr.index = ecx;
  4768. msr.host_initiated = false;
  4769. if (kvm_set_msr(vcpu, &msr) != 0) {
  4770. trace_kvm_msr_write_ex(ecx, data);
  4771. kvm_inject_gp(vcpu, 0);
  4772. return 1;
  4773. }
  4774. trace_kvm_msr_write(ecx, data);
  4775. skip_emulated_instruction(vcpu);
  4776. return 1;
  4777. }
  4778. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  4779. {
  4780. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4781. return 1;
  4782. }
  4783. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  4784. {
  4785. u32 cpu_based_vm_exec_control;
  4786. /* clear pending irq */
  4787. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4788. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  4789. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4790. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4791. ++vcpu->stat.irq_window_exits;
  4792. /*
  4793. * If the user space waits to inject interrupts, exit as soon as
  4794. * possible
  4795. */
  4796. if (!irqchip_in_kernel(vcpu->kvm) &&
  4797. vcpu->run->request_interrupt_window &&
  4798. !kvm_cpu_has_interrupt(vcpu)) {
  4799. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  4800. return 0;
  4801. }
  4802. return 1;
  4803. }
  4804. static int handle_halt(struct kvm_vcpu *vcpu)
  4805. {
  4806. return kvm_emulate_halt(vcpu);
  4807. }
  4808. static int handle_vmcall(struct kvm_vcpu *vcpu)
  4809. {
  4810. kvm_emulate_hypercall(vcpu);
  4811. return 1;
  4812. }
  4813. static int handle_invd(struct kvm_vcpu *vcpu)
  4814. {
  4815. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4816. }
  4817. static int handle_invlpg(struct kvm_vcpu *vcpu)
  4818. {
  4819. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4820. kvm_mmu_invlpg(vcpu, exit_qualification);
  4821. skip_emulated_instruction(vcpu);
  4822. return 1;
  4823. }
  4824. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  4825. {
  4826. int err;
  4827. err = kvm_rdpmc(vcpu);
  4828. kvm_complete_insn_gp(vcpu, err);
  4829. return 1;
  4830. }
  4831. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  4832. {
  4833. kvm_emulate_wbinvd(vcpu);
  4834. return 1;
  4835. }
  4836. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  4837. {
  4838. u64 new_bv = kvm_read_edx_eax(vcpu);
  4839. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4840. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  4841. skip_emulated_instruction(vcpu);
  4842. return 1;
  4843. }
  4844. static int handle_xsaves(struct kvm_vcpu *vcpu)
  4845. {
  4846. skip_emulated_instruction(vcpu);
  4847. WARN(1, "this should never happen\n");
  4848. return 1;
  4849. }
  4850. static int handle_xrstors(struct kvm_vcpu *vcpu)
  4851. {
  4852. skip_emulated_instruction(vcpu);
  4853. WARN(1, "this should never happen\n");
  4854. return 1;
  4855. }
  4856. static int handle_apic_access(struct kvm_vcpu *vcpu)
  4857. {
  4858. if (likely(fasteoi)) {
  4859. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4860. int access_type, offset;
  4861. access_type = exit_qualification & APIC_ACCESS_TYPE;
  4862. offset = exit_qualification & APIC_ACCESS_OFFSET;
  4863. /*
  4864. * Sane guest uses MOV to write EOI, with written value
  4865. * not cared. So make a short-circuit here by avoiding
  4866. * heavy instruction emulation.
  4867. */
  4868. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  4869. (offset == APIC_EOI)) {
  4870. kvm_lapic_set_eoi(vcpu);
  4871. skip_emulated_instruction(vcpu);
  4872. return 1;
  4873. }
  4874. }
  4875. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4876. }
  4877. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  4878. {
  4879. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4880. int vector = exit_qualification & 0xff;
  4881. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  4882. kvm_apic_set_eoi_accelerated(vcpu, vector);
  4883. return 1;
  4884. }
  4885. static int handle_apic_write(struct kvm_vcpu *vcpu)
  4886. {
  4887. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4888. u32 offset = exit_qualification & 0xfff;
  4889. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  4890. kvm_apic_write_nodecode(vcpu, offset);
  4891. return 1;
  4892. }
  4893. static int handle_task_switch(struct kvm_vcpu *vcpu)
  4894. {
  4895. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4896. unsigned long exit_qualification;
  4897. bool has_error_code = false;
  4898. u32 error_code = 0;
  4899. u16 tss_selector;
  4900. int reason, type, idt_v, idt_index;
  4901. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  4902. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  4903. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  4904. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4905. reason = (u32)exit_qualification >> 30;
  4906. if (reason == TASK_SWITCH_GATE && idt_v) {
  4907. switch (type) {
  4908. case INTR_TYPE_NMI_INTR:
  4909. vcpu->arch.nmi_injected = false;
  4910. vmx_set_nmi_mask(vcpu, true);
  4911. break;
  4912. case INTR_TYPE_EXT_INTR:
  4913. case INTR_TYPE_SOFT_INTR:
  4914. kvm_clear_interrupt_queue(vcpu);
  4915. break;
  4916. case INTR_TYPE_HARD_EXCEPTION:
  4917. if (vmx->idt_vectoring_info &
  4918. VECTORING_INFO_DELIVER_CODE_MASK) {
  4919. has_error_code = true;
  4920. error_code =
  4921. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  4922. }
  4923. /* fall through */
  4924. case INTR_TYPE_SOFT_EXCEPTION:
  4925. kvm_clear_exception_queue(vcpu);
  4926. break;
  4927. default:
  4928. break;
  4929. }
  4930. }
  4931. tss_selector = exit_qualification;
  4932. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  4933. type != INTR_TYPE_EXT_INTR &&
  4934. type != INTR_TYPE_NMI_INTR))
  4935. skip_emulated_instruction(vcpu);
  4936. if (kvm_task_switch(vcpu, tss_selector,
  4937. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  4938. has_error_code, error_code) == EMULATE_FAIL) {
  4939. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4940. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4941. vcpu->run->internal.ndata = 0;
  4942. return 0;
  4943. }
  4944. /* clear all local breakpoint enable flags */
  4945. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~0x155);
  4946. /*
  4947. * TODO: What about debug traps on tss switch?
  4948. * Are we supposed to inject them and update dr6?
  4949. */
  4950. return 1;
  4951. }
  4952. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  4953. {
  4954. unsigned long exit_qualification;
  4955. gpa_t gpa;
  4956. u32 error_code;
  4957. int gla_validity;
  4958. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4959. gla_validity = (exit_qualification >> 7) & 0x3;
  4960. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  4961. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  4962. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  4963. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  4964. vmcs_readl(GUEST_LINEAR_ADDRESS));
  4965. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  4966. (long unsigned int)exit_qualification);
  4967. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4968. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  4969. return 0;
  4970. }
  4971. /*
  4972. * EPT violation happened while executing iret from NMI,
  4973. * "blocked by NMI" bit has to be set before next VM entry.
  4974. * There are errata that may cause this bit to not be set:
  4975. * AAK134, BY25.
  4976. */
  4977. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  4978. cpu_has_virtual_nmis() &&
  4979. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  4980. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  4981. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4982. trace_kvm_page_fault(gpa, exit_qualification);
  4983. /* It is a write fault? */
  4984. error_code = exit_qualification & PFERR_WRITE_MASK;
  4985. /* It is a fetch fault? */
  4986. error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
  4987. /* ept page table is present? */
  4988. error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK;
  4989. vcpu->arch.exit_qualification = exit_qualification;
  4990. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  4991. }
  4992. static u64 ept_rsvd_mask(u64 spte, int level)
  4993. {
  4994. int i;
  4995. u64 mask = 0;
  4996. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  4997. mask |= (1ULL << i);
  4998. if (level == 4)
  4999. /* bits 7:3 reserved */
  5000. mask |= 0xf8;
  5001. else if (spte & (1ULL << 7))
  5002. /*
  5003. * 1GB/2MB page, bits 29:12 or 20:12 reserved respectively,
  5004. * level == 1 if the hypervisor is using the ignored bit 7.
  5005. */
  5006. mask |= (PAGE_SIZE << ((level - 1) * 9)) - PAGE_SIZE;
  5007. else if (level > 1)
  5008. /* bits 6:3 reserved */
  5009. mask |= 0x78;
  5010. return mask;
  5011. }
  5012. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  5013. int level)
  5014. {
  5015. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  5016. /* 010b (write-only) */
  5017. WARN_ON((spte & 0x7) == 0x2);
  5018. /* 110b (write/execute) */
  5019. WARN_ON((spte & 0x7) == 0x6);
  5020. /* 100b (execute-only) and value not supported by logical processor */
  5021. if (!cpu_has_vmx_ept_execute_only())
  5022. WARN_ON((spte & 0x7) == 0x4);
  5023. /* not 000b */
  5024. if ((spte & 0x7)) {
  5025. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  5026. if (rsvd_bits != 0) {
  5027. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  5028. __func__, rsvd_bits);
  5029. WARN_ON(1);
  5030. }
  5031. /* bits 5:3 are _not_ reserved for large page or leaf page */
  5032. if ((rsvd_bits & 0x38) == 0) {
  5033. u64 ept_mem_type = (spte & 0x38) >> 3;
  5034. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  5035. ept_mem_type == 7) {
  5036. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  5037. __func__, ept_mem_type);
  5038. WARN_ON(1);
  5039. }
  5040. }
  5041. }
  5042. }
  5043. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  5044. {
  5045. u64 sptes[4];
  5046. int nr_sptes, i, ret;
  5047. gpa_t gpa;
  5048. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  5049. if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  5050. skip_emulated_instruction(vcpu);
  5051. return 1;
  5052. }
  5053. ret = handle_mmio_page_fault_common(vcpu, gpa, true);
  5054. if (likely(ret == RET_MMIO_PF_EMULATE))
  5055. return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
  5056. EMULATE_DONE;
  5057. if (unlikely(ret == RET_MMIO_PF_INVALID))
  5058. return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
  5059. if (unlikely(ret == RET_MMIO_PF_RETRY))
  5060. return 1;
  5061. /* It is the real ept misconfig */
  5062. printk(KERN_ERR "EPT: Misconfiguration.\n");
  5063. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  5064. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  5065. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  5066. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  5067. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5068. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  5069. return 0;
  5070. }
  5071. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  5072. {
  5073. u32 cpu_based_vm_exec_control;
  5074. /* clear pending NMI */
  5075. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5076. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  5077. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  5078. ++vcpu->stat.nmi_window_exits;
  5079. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5080. return 1;
  5081. }
  5082. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  5083. {
  5084. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5085. enum emulation_result err = EMULATE_DONE;
  5086. int ret = 1;
  5087. u32 cpu_exec_ctrl;
  5088. bool intr_window_requested;
  5089. unsigned count = 130;
  5090. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5091. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  5092. while (vmx->emulation_required && count-- != 0) {
  5093. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  5094. return handle_interrupt_window(&vmx->vcpu);
  5095. if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
  5096. return 1;
  5097. err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
  5098. if (err == EMULATE_USER_EXIT) {
  5099. ++vcpu->stat.mmio_exits;
  5100. ret = 0;
  5101. goto out;
  5102. }
  5103. if (err != EMULATE_DONE) {
  5104. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5105. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5106. vcpu->run->internal.ndata = 0;
  5107. return 0;
  5108. }
  5109. if (vcpu->arch.halt_request) {
  5110. vcpu->arch.halt_request = 0;
  5111. ret = kvm_vcpu_halt(vcpu);
  5112. goto out;
  5113. }
  5114. if (signal_pending(current))
  5115. goto out;
  5116. if (need_resched())
  5117. schedule();
  5118. }
  5119. out:
  5120. return ret;
  5121. }
  5122. static int __grow_ple_window(int val)
  5123. {
  5124. if (ple_window_grow < 1)
  5125. return ple_window;
  5126. val = min(val, ple_window_actual_max);
  5127. if (ple_window_grow < ple_window)
  5128. val *= ple_window_grow;
  5129. else
  5130. val += ple_window_grow;
  5131. return val;
  5132. }
  5133. static int __shrink_ple_window(int val, int modifier, int minimum)
  5134. {
  5135. if (modifier < 1)
  5136. return ple_window;
  5137. if (modifier < ple_window)
  5138. val /= modifier;
  5139. else
  5140. val -= modifier;
  5141. return max(val, minimum);
  5142. }
  5143. static void grow_ple_window(struct kvm_vcpu *vcpu)
  5144. {
  5145. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5146. int old = vmx->ple_window;
  5147. vmx->ple_window = __grow_ple_window(old);
  5148. if (vmx->ple_window != old)
  5149. vmx->ple_window_dirty = true;
  5150. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  5151. }
  5152. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  5153. {
  5154. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5155. int old = vmx->ple_window;
  5156. vmx->ple_window = __shrink_ple_window(old,
  5157. ple_window_shrink, ple_window);
  5158. if (vmx->ple_window != old)
  5159. vmx->ple_window_dirty = true;
  5160. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  5161. }
  5162. /*
  5163. * ple_window_actual_max is computed to be one grow_ple_window() below
  5164. * ple_window_max. (See __grow_ple_window for the reason.)
  5165. * This prevents overflows, because ple_window_max is int.
  5166. * ple_window_max effectively rounded down to a multiple of ple_window_grow in
  5167. * this process.
  5168. * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
  5169. */
  5170. static void update_ple_window_actual_max(void)
  5171. {
  5172. ple_window_actual_max =
  5173. __shrink_ple_window(max(ple_window_max, ple_window),
  5174. ple_window_grow, INT_MIN);
  5175. }
  5176. static __init int hardware_setup(void)
  5177. {
  5178. int r = -ENOMEM, i, msr;
  5179. rdmsrl_safe(MSR_EFER, &host_efer);
  5180. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  5181. kvm_define_shared_msr(i, vmx_msr_index[i]);
  5182. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  5183. if (!vmx_io_bitmap_a)
  5184. return r;
  5185. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  5186. if (!vmx_io_bitmap_b)
  5187. goto out;
  5188. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  5189. if (!vmx_msr_bitmap_legacy)
  5190. goto out1;
  5191. vmx_msr_bitmap_legacy_x2apic =
  5192. (unsigned long *)__get_free_page(GFP_KERNEL);
  5193. if (!vmx_msr_bitmap_legacy_x2apic)
  5194. goto out2;
  5195. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  5196. if (!vmx_msr_bitmap_longmode)
  5197. goto out3;
  5198. vmx_msr_bitmap_longmode_x2apic =
  5199. (unsigned long *)__get_free_page(GFP_KERNEL);
  5200. if (!vmx_msr_bitmap_longmode_x2apic)
  5201. goto out4;
  5202. if (nested) {
  5203. vmx_msr_bitmap_nested =
  5204. (unsigned long *)__get_free_page(GFP_KERNEL);
  5205. if (!vmx_msr_bitmap_nested)
  5206. goto out5;
  5207. }
  5208. vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  5209. if (!vmx_vmread_bitmap)
  5210. goto out6;
  5211. vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  5212. if (!vmx_vmwrite_bitmap)
  5213. goto out7;
  5214. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  5215. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  5216. /*
  5217. * Allow direct access to the PC debug port (it is often used for I/O
  5218. * delays, but the vmexits simply slow things down).
  5219. */
  5220. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  5221. clear_bit(0x80, vmx_io_bitmap_a);
  5222. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  5223. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  5224. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  5225. if (nested)
  5226. memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
  5227. if (setup_vmcs_config(&vmcs_config) < 0) {
  5228. r = -EIO;
  5229. goto out8;
  5230. }
  5231. if (boot_cpu_has(X86_FEATURE_NX))
  5232. kvm_enable_efer_bits(EFER_NX);
  5233. if (!cpu_has_vmx_vpid())
  5234. enable_vpid = 0;
  5235. if (!cpu_has_vmx_shadow_vmcs())
  5236. enable_shadow_vmcs = 0;
  5237. if (enable_shadow_vmcs)
  5238. init_vmcs_shadow_fields();
  5239. if (!cpu_has_vmx_ept() ||
  5240. !cpu_has_vmx_ept_4levels()) {
  5241. enable_ept = 0;
  5242. enable_unrestricted_guest = 0;
  5243. enable_ept_ad_bits = 0;
  5244. }
  5245. if (!cpu_has_vmx_ept_ad_bits())
  5246. enable_ept_ad_bits = 0;
  5247. if (!cpu_has_vmx_unrestricted_guest())
  5248. enable_unrestricted_guest = 0;
  5249. if (!cpu_has_vmx_flexpriority())
  5250. flexpriority_enabled = 0;
  5251. /*
  5252. * set_apic_access_page_addr() is used to reload apic access
  5253. * page upon invalidation. No need to do anything if not
  5254. * using the APIC_ACCESS_ADDR VMCS field.
  5255. */
  5256. if (!flexpriority_enabled)
  5257. kvm_x86_ops->set_apic_access_page_addr = NULL;
  5258. if (!cpu_has_vmx_tpr_shadow())
  5259. kvm_x86_ops->update_cr8_intercept = NULL;
  5260. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  5261. kvm_disable_largepages();
  5262. if (!cpu_has_vmx_ple())
  5263. ple_gap = 0;
  5264. if (!cpu_has_vmx_apicv())
  5265. enable_apicv = 0;
  5266. if (enable_apicv)
  5267. kvm_x86_ops->update_cr8_intercept = NULL;
  5268. else {
  5269. kvm_x86_ops->hwapic_irr_update = NULL;
  5270. kvm_x86_ops->hwapic_isr_update = NULL;
  5271. kvm_x86_ops->deliver_posted_interrupt = NULL;
  5272. kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
  5273. }
  5274. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  5275. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  5276. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  5277. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  5278. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  5279. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  5280. vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
  5281. memcpy(vmx_msr_bitmap_legacy_x2apic,
  5282. vmx_msr_bitmap_legacy, PAGE_SIZE);
  5283. memcpy(vmx_msr_bitmap_longmode_x2apic,
  5284. vmx_msr_bitmap_longmode, PAGE_SIZE);
  5285. if (enable_apicv) {
  5286. for (msr = 0x800; msr <= 0x8ff; msr++)
  5287. vmx_disable_intercept_msr_read_x2apic(msr);
  5288. /* According SDM, in x2apic mode, the whole id reg is used.
  5289. * But in KVM, it only use the highest eight bits. Need to
  5290. * intercept it */
  5291. vmx_enable_intercept_msr_read_x2apic(0x802);
  5292. /* TMCCT */
  5293. vmx_enable_intercept_msr_read_x2apic(0x839);
  5294. /* TPR */
  5295. vmx_disable_intercept_msr_write_x2apic(0x808);
  5296. /* EOI */
  5297. vmx_disable_intercept_msr_write_x2apic(0x80b);
  5298. /* SELF-IPI */
  5299. vmx_disable_intercept_msr_write_x2apic(0x83f);
  5300. }
  5301. if (enable_ept) {
  5302. kvm_mmu_set_mask_ptes(0ull,
  5303. (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
  5304. (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
  5305. 0ull, VMX_EPT_EXECUTABLE_MASK);
  5306. ept_set_mmio_spte_mask();
  5307. kvm_enable_tdp();
  5308. } else
  5309. kvm_disable_tdp();
  5310. update_ple_window_actual_max();
  5311. /*
  5312. * Only enable PML when hardware supports PML feature, and both EPT
  5313. * and EPT A/D bit features are enabled -- PML depends on them to work.
  5314. */
  5315. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  5316. enable_pml = 0;
  5317. if (!enable_pml) {
  5318. kvm_x86_ops->slot_enable_log_dirty = NULL;
  5319. kvm_x86_ops->slot_disable_log_dirty = NULL;
  5320. kvm_x86_ops->flush_log_dirty = NULL;
  5321. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  5322. }
  5323. return alloc_kvm_area();
  5324. out8:
  5325. free_page((unsigned long)vmx_vmwrite_bitmap);
  5326. out7:
  5327. free_page((unsigned long)vmx_vmread_bitmap);
  5328. out6:
  5329. if (nested)
  5330. free_page((unsigned long)vmx_msr_bitmap_nested);
  5331. out5:
  5332. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  5333. out4:
  5334. free_page((unsigned long)vmx_msr_bitmap_longmode);
  5335. out3:
  5336. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  5337. out2:
  5338. free_page((unsigned long)vmx_msr_bitmap_legacy);
  5339. out1:
  5340. free_page((unsigned long)vmx_io_bitmap_b);
  5341. out:
  5342. free_page((unsigned long)vmx_io_bitmap_a);
  5343. return r;
  5344. }
  5345. static __exit void hardware_unsetup(void)
  5346. {
  5347. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  5348. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  5349. free_page((unsigned long)vmx_msr_bitmap_legacy);
  5350. free_page((unsigned long)vmx_msr_bitmap_longmode);
  5351. free_page((unsigned long)vmx_io_bitmap_b);
  5352. free_page((unsigned long)vmx_io_bitmap_a);
  5353. free_page((unsigned long)vmx_vmwrite_bitmap);
  5354. free_page((unsigned long)vmx_vmread_bitmap);
  5355. if (nested)
  5356. free_page((unsigned long)vmx_msr_bitmap_nested);
  5357. free_kvm_area();
  5358. }
  5359. /*
  5360. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  5361. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  5362. */
  5363. static int handle_pause(struct kvm_vcpu *vcpu)
  5364. {
  5365. if (ple_gap)
  5366. grow_ple_window(vcpu);
  5367. skip_emulated_instruction(vcpu);
  5368. kvm_vcpu_on_spin(vcpu);
  5369. return 1;
  5370. }
  5371. static int handle_nop(struct kvm_vcpu *vcpu)
  5372. {
  5373. skip_emulated_instruction(vcpu);
  5374. return 1;
  5375. }
  5376. static int handle_mwait(struct kvm_vcpu *vcpu)
  5377. {
  5378. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  5379. return handle_nop(vcpu);
  5380. }
  5381. static int handle_monitor(struct kvm_vcpu *vcpu)
  5382. {
  5383. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  5384. return handle_nop(vcpu);
  5385. }
  5386. /*
  5387. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  5388. * We could reuse a single VMCS for all the L2 guests, but we also want the
  5389. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  5390. * allows keeping them loaded on the processor, and in the future will allow
  5391. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  5392. * every entry if they never change.
  5393. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  5394. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  5395. *
  5396. * The following functions allocate and free a vmcs02 in this pool.
  5397. */
  5398. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  5399. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  5400. {
  5401. struct vmcs02_list *item;
  5402. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5403. if (item->vmptr == vmx->nested.current_vmptr) {
  5404. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5405. return &item->vmcs02;
  5406. }
  5407. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  5408. /* Recycle the least recently used VMCS. */
  5409. item = list_entry(vmx->nested.vmcs02_pool.prev,
  5410. struct vmcs02_list, list);
  5411. item->vmptr = vmx->nested.current_vmptr;
  5412. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5413. return &item->vmcs02;
  5414. }
  5415. /* Create a new VMCS */
  5416. item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  5417. if (!item)
  5418. return NULL;
  5419. item->vmcs02.vmcs = alloc_vmcs();
  5420. if (!item->vmcs02.vmcs) {
  5421. kfree(item);
  5422. return NULL;
  5423. }
  5424. loaded_vmcs_init(&item->vmcs02);
  5425. item->vmptr = vmx->nested.current_vmptr;
  5426. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  5427. vmx->nested.vmcs02_num++;
  5428. return &item->vmcs02;
  5429. }
  5430. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  5431. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  5432. {
  5433. struct vmcs02_list *item;
  5434. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5435. if (item->vmptr == vmptr) {
  5436. free_loaded_vmcs(&item->vmcs02);
  5437. list_del(&item->list);
  5438. kfree(item);
  5439. vmx->nested.vmcs02_num--;
  5440. return;
  5441. }
  5442. }
  5443. /*
  5444. * Free all VMCSs saved for this vcpu, except the one pointed by
  5445. * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
  5446. * must be &vmx->vmcs01.
  5447. */
  5448. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  5449. {
  5450. struct vmcs02_list *item, *n;
  5451. WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
  5452. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  5453. /*
  5454. * Something will leak if the above WARN triggers. Better than
  5455. * a use-after-free.
  5456. */
  5457. if (vmx->loaded_vmcs == &item->vmcs02)
  5458. continue;
  5459. free_loaded_vmcs(&item->vmcs02);
  5460. list_del(&item->list);
  5461. kfree(item);
  5462. vmx->nested.vmcs02_num--;
  5463. }
  5464. }
  5465. /*
  5466. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  5467. * set the success or error code of an emulated VMX instruction, as specified
  5468. * by Vol 2B, VMX Instruction Reference, "Conventions".
  5469. */
  5470. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  5471. {
  5472. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  5473. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5474. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  5475. }
  5476. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  5477. {
  5478. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5479. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  5480. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5481. | X86_EFLAGS_CF);
  5482. }
  5483. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  5484. u32 vm_instruction_error)
  5485. {
  5486. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  5487. /*
  5488. * failValid writes the error number to the current VMCS, which
  5489. * can't be done there isn't a current VMCS.
  5490. */
  5491. nested_vmx_failInvalid(vcpu);
  5492. return;
  5493. }
  5494. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5495. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5496. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5497. | X86_EFLAGS_ZF);
  5498. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  5499. /*
  5500. * We don't need to force a shadow sync because
  5501. * VM_INSTRUCTION_ERROR is not shadowed
  5502. */
  5503. }
  5504. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  5505. {
  5506. /* TODO: not to reset guest simply here. */
  5507. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5508. pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
  5509. }
  5510. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  5511. {
  5512. struct vcpu_vmx *vmx =
  5513. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  5514. vmx->nested.preemption_timer_expired = true;
  5515. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  5516. kvm_vcpu_kick(&vmx->vcpu);
  5517. return HRTIMER_NORESTART;
  5518. }
  5519. /*
  5520. * Decode the memory-address operand of a vmx instruction, as recorded on an
  5521. * exit caused by such an instruction (run by a guest hypervisor).
  5522. * On success, returns 0. When the operand is invalid, returns 1 and throws
  5523. * #UD or #GP.
  5524. */
  5525. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  5526. unsigned long exit_qualification,
  5527. u32 vmx_instruction_info, gva_t *ret)
  5528. {
  5529. /*
  5530. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  5531. * Execution", on an exit, vmx_instruction_info holds most of the
  5532. * addressing components of the operand. Only the displacement part
  5533. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  5534. * For how an actual address is calculated from all these components,
  5535. * refer to Vol. 1, "Operand Addressing".
  5536. */
  5537. int scaling = vmx_instruction_info & 3;
  5538. int addr_size = (vmx_instruction_info >> 7) & 7;
  5539. bool is_reg = vmx_instruction_info & (1u << 10);
  5540. int seg_reg = (vmx_instruction_info >> 15) & 7;
  5541. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  5542. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  5543. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  5544. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  5545. if (is_reg) {
  5546. kvm_queue_exception(vcpu, UD_VECTOR);
  5547. return 1;
  5548. }
  5549. /* Addr = segment_base + offset */
  5550. /* offset = base + [index * scale] + displacement */
  5551. *ret = vmx_get_segment_base(vcpu, seg_reg);
  5552. if (base_is_valid)
  5553. *ret += kvm_register_read(vcpu, base_reg);
  5554. if (index_is_valid)
  5555. *ret += kvm_register_read(vcpu, index_reg)<<scaling;
  5556. *ret += exit_qualification; /* holds the displacement */
  5557. if (addr_size == 1) /* 32 bit */
  5558. *ret &= 0xffffffff;
  5559. /*
  5560. * TODO: throw #GP (and return 1) in various cases that the VM*
  5561. * instructions require it - e.g., offset beyond segment limit,
  5562. * unusable or unreadable/unwritable segment, non-canonical 64-bit
  5563. * address, and so on. Currently these are not checked.
  5564. */
  5565. return 0;
  5566. }
  5567. /*
  5568. * This function performs the various checks including
  5569. * - if it's 4KB aligned
  5570. * - No bits beyond the physical address width are set
  5571. * - Returns 0 on success or else 1
  5572. * (Intel SDM Section 30.3)
  5573. */
  5574. static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
  5575. gpa_t *vmpointer)
  5576. {
  5577. gva_t gva;
  5578. gpa_t vmptr;
  5579. struct x86_exception e;
  5580. struct page *page;
  5581. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5582. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  5583. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  5584. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  5585. return 1;
  5586. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  5587. sizeof(vmptr), &e)) {
  5588. kvm_inject_page_fault(vcpu, &e);
  5589. return 1;
  5590. }
  5591. switch (exit_reason) {
  5592. case EXIT_REASON_VMON:
  5593. /*
  5594. * SDM 3: 24.11.5
  5595. * The first 4 bytes of VMXON region contain the supported
  5596. * VMCS revision identifier
  5597. *
  5598. * Note - IA32_VMX_BASIC[48] will never be 1
  5599. * for the nested case;
  5600. * which replaces physical address width with 32
  5601. *
  5602. */
  5603. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5604. nested_vmx_failInvalid(vcpu);
  5605. skip_emulated_instruction(vcpu);
  5606. return 1;
  5607. }
  5608. page = nested_get_page(vcpu, vmptr);
  5609. if (page == NULL ||
  5610. *(u32 *)kmap(page) != VMCS12_REVISION) {
  5611. nested_vmx_failInvalid(vcpu);
  5612. kunmap(page);
  5613. skip_emulated_instruction(vcpu);
  5614. return 1;
  5615. }
  5616. kunmap(page);
  5617. vmx->nested.vmxon_ptr = vmptr;
  5618. break;
  5619. case EXIT_REASON_VMCLEAR:
  5620. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5621. nested_vmx_failValid(vcpu,
  5622. VMXERR_VMCLEAR_INVALID_ADDRESS);
  5623. skip_emulated_instruction(vcpu);
  5624. return 1;
  5625. }
  5626. if (vmptr == vmx->nested.vmxon_ptr) {
  5627. nested_vmx_failValid(vcpu,
  5628. VMXERR_VMCLEAR_VMXON_POINTER);
  5629. skip_emulated_instruction(vcpu);
  5630. return 1;
  5631. }
  5632. break;
  5633. case EXIT_REASON_VMPTRLD:
  5634. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5635. nested_vmx_failValid(vcpu,
  5636. VMXERR_VMPTRLD_INVALID_ADDRESS);
  5637. skip_emulated_instruction(vcpu);
  5638. return 1;
  5639. }
  5640. if (vmptr == vmx->nested.vmxon_ptr) {
  5641. nested_vmx_failValid(vcpu,
  5642. VMXERR_VMCLEAR_VMXON_POINTER);
  5643. skip_emulated_instruction(vcpu);
  5644. return 1;
  5645. }
  5646. break;
  5647. default:
  5648. return 1; /* shouldn't happen */
  5649. }
  5650. if (vmpointer)
  5651. *vmpointer = vmptr;
  5652. return 0;
  5653. }
  5654. /*
  5655. * Emulate the VMXON instruction.
  5656. * Currently, we just remember that VMX is active, and do not save or even
  5657. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  5658. * do not currently need to store anything in that guest-allocated memory
  5659. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  5660. * argument is different from the VMXON pointer (which the spec says they do).
  5661. */
  5662. static int handle_vmon(struct kvm_vcpu *vcpu)
  5663. {
  5664. struct kvm_segment cs;
  5665. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5666. struct vmcs *shadow_vmcs;
  5667. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  5668. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  5669. /* The Intel VMX Instruction Reference lists a bunch of bits that
  5670. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  5671. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  5672. * Otherwise, we should fail with #UD. We test these now:
  5673. */
  5674. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  5675. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  5676. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  5677. kvm_queue_exception(vcpu, UD_VECTOR);
  5678. return 1;
  5679. }
  5680. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5681. if (is_long_mode(vcpu) && !cs.l) {
  5682. kvm_queue_exception(vcpu, UD_VECTOR);
  5683. return 1;
  5684. }
  5685. if (vmx_get_cpl(vcpu)) {
  5686. kvm_inject_gp(vcpu, 0);
  5687. return 1;
  5688. }
  5689. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
  5690. return 1;
  5691. if (vmx->nested.vmxon) {
  5692. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  5693. skip_emulated_instruction(vcpu);
  5694. return 1;
  5695. }
  5696. if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  5697. != VMXON_NEEDED_FEATURES) {
  5698. kvm_inject_gp(vcpu, 0);
  5699. return 1;
  5700. }
  5701. if (enable_shadow_vmcs) {
  5702. shadow_vmcs = alloc_vmcs();
  5703. if (!shadow_vmcs)
  5704. return -ENOMEM;
  5705. /* mark vmcs as shadow */
  5706. shadow_vmcs->revision_id |= (1u << 31);
  5707. /* init shadow vmcs */
  5708. vmcs_clear(shadow_vmcs);
  5709. vmx->nested.current_shadow_vmcs = shadow_vmcs;
  5710. }
  5711. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  5712. vmx->nested.vmcs02_num = 0;
  5713. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  5714. HRTIMER_MODE_REL);
  5715. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  5716. vmx->nested.vmxon = true;
  5717. skip_emulated_instruction(vcpu);
  5718. nested_vmx_succeed(vcpu);
  5719. return 1;
  5720. }
  5721. /*
  5722. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  5723. * for running VMX instructions (except VMXON, whose prerequisites are
  5724. * slightly different). It also specifies what exception to inject otherwise.
  5725. */
  5726. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  5727. {
  5728. struct kvm_segment cs;
  5729. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5730. if (!vmx->nested.vmxon) {
  5731. kvm_queue_exception(vcpu, UD_VECTOR);
  5732. return 0;
  5733. }
  5734. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5735. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  5736. (is_long_mode(vcpu) && !cs.l)) {
  5737. kvm_queue_exception(vcpu, UD_VECTOR);
  5738. return 0;
  5739. }
  5740. if (vmx_get_cpl(vcpu)) {
  5741. kvm_inject_gp(vcpu, 0);
  5742. return 0;
  5743. }
  5744. return 1;
  5745. }
  5746. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  5747. {
  5748. u32 exec_control;
  5749. if (vmx->nested.current_vmptr == -1ull)
  5750. return;
  5751. /* current_vmptr and current_vmcs12 are always set/reset together */
  5752. if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
  5753. return;
  5754. if (enable_shadow_vmcs) {
  5755. /* copy to memory all shadowed fields in case
  5756. they were modified */
  5757. copy_shadow_to_vmcs12(vmx);
  5758. vmx->nested.sync_shadow_vmcs = false;
  5759. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5760. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5761. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5762. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  5763. }
  5764. vmx->nested.posted_intr_nv = -1;
  5765. kunmap(vmx->nested.current_vmcs12_page);
  5766. nested_release_page(vmx->nested.current_vmcs12_page);
  5767. vmx->nested.current_vmptr = -1ull;
  5768. vmx->nested.current_vmcs12 = NULL;
  5769. }
  5770. /*
  5771. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  5772. * just stops using VMX.
  5773. */
  5774. static void free_nested(struct vcpu_vmx *vmx)
  5775. {
  5776. if (!vmx->nested.vmxon)
  5777. return;
  5778. vmx->nested.vmxon = false;
  5779. nested_release_vmcs12(vmx);
  5780. if (enable_shadow_vmcs)
  5781. free_vmcs(vmx->nested.current_shadow_vmcs);
  5782. /* Unpin physical memory we referred to in current vmcs02 */
  5783. if (vmx->nested.apic_access_page) {
  5784. nested_release_page(vmx->nested.apic_access_page);
  5785. vmx->nested.apic_access_page = NULL;
  5786. }
  5787. if (vmx->nested.virtual_apic_page) {
  5788. nested_release_page(vmx->nested.virtual_apic_page);
  5789. vmx->nested.virtual_apic_page = NULL;
  5790. }
  5791. if (vmx->nested.pi_desc_page) {
  5792. kunmap(vmx->nested.pi_desc_page);
  5793. nested_release_page(vmx->nested.pi_desc_page);
  5794. vmx->nested.pi_desc_page = NULL;
  5795. vmx->nested.pi_desc = NULL;
  5796. }
  5797. nested_free_all_saved_vmcss(vmx);
  5798. }
  5799. /* Emulate the VMXOFF instruction */
  5800. static int handle_vmoff(struct kvm_vcpu *vcpu)
  5801. {
  5802. if (!nested_vmx_check_permission(vcpu))
  5803. return 1;
  5804. free_nested(to_vmx(vcpu));
  5805. skip_emulated_instruction(vcpu);
  5806. nested_vmx_succeed(vcpu);
  5807. return 1;
  5808. }
  5809. /* Emulate the VMCLEAR instruction */
  5810. static int handle_vmclear(struct kvm_vcpu *vcpu)
  5811. {
  5812. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5813. gpa_t vmptr;
  5814. struct vmcs12 *vmcs12;
  5815. struct page *page;
  5816. if (!nested_vmx_check_permission(vcpu))
  5817. return 1;
  5818. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
  5819. return 1;
  5820. if (vmptr == vmx->nested.current_vmptr)
  5821. nested_release_vmcs12(vmx);
  5822. page = nested_get_page(vcpu, vmptr);
  5823. if (page == NULL) {
  5824. /*
  5825. * For accurate processor emulation, VMCLEAR beyond available
  5826. * physical memory should do nothing at all. However, it is
  5827. * possible that a nested vmx bug, not a guest hypervisor bug,
  5828. * resulted in this case, so let's shut down before doing any
  5829. * more damage:
  5830. */
  5831. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5832. return 1;
  5833. }
  5834. vmcs12 = kmap(page);
  5835. vmcs12->launch_state = 0;
  5836. kunmap(page);
  5837. nested_release_page(page);
  5838. nested_free_vmcs02(vmx, vmptr);
  5839. skip_emulated_instruction(vcpu);
  5840. nested_vmx_succeed(vcpu);
  5841. return 1;
  5842. }
  5843. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  5844. /* Emulate the VMLAUNCH instruction */
  5845. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  5846. {
  5847. return nested_vmx_run(vcpu, true);
  5848. }
  5849. /* Emulate the VMRESUME instruction */
  5850. static int handle_vmresume(struct kvm_vcpu *vcpu)
  5851. {
  5852. return nested_vmx_run(vcpu, false);
  5853. }
  5854. enum vmcs_field_type {
  5855. VMCS_FIELD_TYPE_U16 = 0,
  5856. VMCS_FIELD_TYPE_U64 = 1,
  5857. VMCS_FIELD_TYPE_U32 = 2,
  5858. VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
  5859. };
  5860. static inline int vmcs_field_type(unsigned long field)
  5861. {
  5862. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  5863. return VMCS_FIELD_TYPE_U32;
  5864. return (field >> 13) & 0x3 ;
  5865. }
  5866. static inline int vmcs_field_readonly(unsigned long field)
  5867. {
  5868. return (((field >> 10) & 0x3) == 1);
  5869. }
  5870. /*
  5871. * Read a vmcs12 field. Since these can have varying lengths and we return
  5872. * one type, we chose the biggest type (u64) and zero-extend the return value
  5873. * to that size. Note that the caller, handle_vmread, might need to use only
  5874. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  5875. * 64-bit fields are to be returned).
  5876. */
  5877. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  5878. unsigned long field, u64 *ret)
  5879. {
  5880. short offset = vmcs_field_to_offset(field);
  5881. char *p;
  5882. if (offset < 0)
  5883. return offset;
  5884. p = ((char *)(get_vmcs12(vcpu))) + offset;
  5885. switch (vmcs_field_type(field)) {
  5886. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5887. *ret = *((natural_width *)p);
  5888. return 0;
  5889. case VMCS_FIELD_TYPE_U16:
  5890. *ret = *((u16 *)p);
  5891. return 0;
  5892. case VMCS_FIELD_TYPE_U32:
  5893. *ret = *((u32 *)p);
  5894. return 0;
  5895. case VMCS_FIELD_TYPE_U64:
  5896. *ret = *((u64 *)p);
  5897. return 0;
  5898. default:
  5899. WARN_ON(1);
  5900. return -ENOENT;
  5901. }
  5902. }
  5903. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  5904. unsigned long field, u64 field_value){
  5905. short offset = vmcs_field_to_offset(field);
  5906. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  5907. if (offset < 0)
  5908. return offset;
  5909. switch (vmcs_field_type(field)) {
  5910. case VMCS_FIELD_TYPE_U16:
  5911. *(u16 *)p = field_value;
  5912. return 0;
  5913. case VMCS_FIELD_TYPE_U32:
  5914. *(u32 *)p = field_value;
  5915. return 0;
  5916. case VMCS_FIELD_TYPE_U64:
  5917. *(u64 *)p = field_value;
  5918. return 0;
  5919. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5920. *(natural_width *)p = field_value;
  5921. return 0;
  5922. default:
  5923. WARN_ON(1);
  5924. return -ENOENT;
  5925. }
  5926. }
  5927. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  5928. {
  5929. int i;
  5930. unsigned long field;
  5931. u64 field_value;
  5932. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5933. const unsigned long *fields = shadow_read_write_fields;
  5934. const int num_fields = max_shadow_read_write_fields;
  5935. preempt_disable();
  5936. vmcs_load(shadow_vmcs);
  5937. for (i = 0; i < num_fields; i++) {
  5938. field = fields[i];
  5939. switch (vmcs_field_type(field)) {
  5940. case VMCS_FIELD_TYPE_U16:
  5941. field_value = vmcs_read16(field);
  5942. break;
  5943. case VMCS_FIELD_TYPE_U32:
  5944. field_value = vmcs_read32(field);
  5945. break;
  5946. case VMCS_FIELD_TYPE_U64:
  5947. field_value = vmcs_read64(field);
  5948. break;
  5949. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5950. field_value = vmcs_readl(field);
  5951. break;
  5952. default:
  5953. WARN_ON(1);
  5954. continue;
  5955. }
  5956. vmcs12_write_any(&vmx->vcpu, field, field_value);
  5957. }
  5958. vmcs_clear(shadow_vmcs);
  5959. vmcs_load(vmx->loaded_vmcs->vmcs);
  5960. preempt_enable();
  5961. }
  5962. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  5963. {
  5964. const unsigned long *fields[] = {
  5965. shadow_read_write_fields,
  5966. shadow_read_only_fields
  5967. };
  5968. const int max_fields[] = {
  5969. max_shadow_read_write_fields,
  5970. max_shadow_read_only_fields
  5971. };
  5972. int i, q;
  5973. unsigned long field;
  5974. u64 field_value = 0;
  5975. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5976. vmcs_load(shadow_vmcs);
  5977. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  5978. for (i = 0; i < max_fields[q]; i++) {
  5979. field = fields[q][i];
  5980. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  5981. switch (vmcs_field_type(field)) {
  5982. case VMCS_FIELD_TYPE_U16:
  5983. vmcs_write16(field, (u16)field_value);
  5984. break;
  5985. case VMCS_FIELD_TYPE_U32:
  5986. vmcs_write32(field, (u32)field_value);
  5987. break;
  5988. case VMCS_FIELD_TYPE_U64:
  5989. vmcs_write64(field, (u64)field_value);
  5990. break;
  5991. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5992. vmcs_writel(field, (long)field_value);
  5993. break;
  5994. default:
  5995. WARN_ON(1);
  5996. break;
  5997. }
  5998. }
  5999. }
  6000. vmcs_clear(shadow_vmcs);
  6001. vmcs_load(vmx->loaded_vmcs->vmcs);
  6002. }
  6003. /*
  6004. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  6005. * used before) all generate the same failure when it is missing.
  6006. */
  6007. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  6008. {
  6009. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6010. if (vmx->nested.current_vmptr == -1ull) {
  6011. nested_vmx_failInvalid(vcpu);
  6012. skip_emulated_instruction(vcpu);
  6013. return 0;
  6014. }
  6015. return 1;
  6016. }
  6017. static int handle_vmread(struct kvm_vcpu *vcpu)
  6018. {
  6019. unsigned long field;
  6020. u64 field_value;
  6021. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6022. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6023. gva_t gva = 0;
  6024. if (!nested_vmx_check_permission(vcpu) ||
  6025. !nested_vmx_check_vmcs12(vcpu))
  6026. return 1;
  6027. /* Decode instruction info and find the field to read */
  6028. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6029. /* Read the field, zero-extended to a u64 field_value */
  6030. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  6031. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6032. skip_emulated_instruction(vcpu);
  6033. return 1;
  6034. }
  6035. /*
  6036. * Now copy part of this value to register or memory, as requested.
  6037. * Note that the number of bits actually copied is 32 or 64 depending
  6038. * on the guest's mode (32 or 64 bit), not on the given field's length.
  6039. */
  6040. if (vmx_instruction_info & (1u << 10)) {
  6041. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  6042. field_value);
  6043. } else {
  6044. if (get_vmx_mem_address(vcpu, exit_qualification,
  6045. vmx_instruction_info, &gva))
  6046. return 1;
  6047. /* _system ok, as nested_vmx_check_permission verified cpl=0 */
  6048. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  6049. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  6050. }
  6051. nested_vmx_succeed(vcpu);
  6052. skip_emulated_instruction(vcpu);
  6053. return 1;
  6054. }
  6055. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  6056. {
  6057. unsigned long field;
  6058. gva_t gva;
  6059. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6060. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6061. /* The value to write might be 32 or 64 bits, depending on L1's long
  6062. * mode, and eventually we need to write that into a field of several
  6063. * possible lengths. The code below first zero-extends the value to 64
  6064. * bit (field_value), and then copies only the approriate number of
  6065. * bits into the vmcs12 field.
  6066. */
  6067. u64 field_value = 0;
  6068. struct x86_exception e;
  6069. if (!nested_vmx_check_permission(vcpu) ||
  6070. !nested_vmx_check_vmcs12(vcpu))
  6071. return 1;
  6072. if (vmx_instruction_info & (1u << 10))
  6073. field_value = kvm_register_readl(vcpu,
  6074. (((vmx_instruction_info) >> 3) & 0xf));
  6075. else {
  6076. if (get_vmx_mem_address(vcpu, exit_qualification,
  6077. vmx_instruction_info, &gva))
  6078. return 1;
  6079. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  6080. &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  6081. kvm_inject_page_fault(vcpu, &e);
  6082. return 1;
  6083. }
  6084. }
  6085. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6086. if (vmcs_field_readonly(field)) {
  6087. nested_vmx_failValid(vcpu,
  6088. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  6089. skip_emulated_instruction(vcpu);
  6090. return 1;
  6091. }
  6092. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  6093. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6094. skip_emulated_instruction(vcpu);
  6095. return 1;
  6096. }
  6097. nested_vmx_succeed(vcpu);
  6098. skip_emulated_instruction(vcpu);
  6099. return 1;
  6100. }
  6101. /* Emulate the VMPTRLD instruction */
  6102. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  6103. {
  6104. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6105. gpa_t vmptr;
  6106. u32 exec_control;
  6107. if (!nested_vmx_check_permission(vcpu))
  6108. return 1;
  6109. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
  6110. return 1;
  6111. if (vmx->nested.current_vmptr != vmptr) {
  6112. struct vmcs12 *new_vmcs12;
  6113. struct page *page;
  6114. page = nested_get_page(vcpu, vmptr);
  6115. if (page == NULL) {
  6116. nested_vmx_failInvalid(vcpu);
  6117. skip_emulated_instruction(vcpu);
  6118. return 1;
  6119. }
  6120. new_vmcs12 = kmap(page);
  6121. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  6122. kunmap(page);
  6123. nested_release_page_clean(page);
  6124. nested_vmx_failValid(vcpu,
  6125. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  6126. skip_emulated_instruction(vcpu);
  6127. return 1;
  6128. }
  6129. nested_release_vmcs12(vmx);
  6130. vmx->nested.current_vmptr = vmptr;
  6131. vmx->nested.current_vmcs12 = new_vmcs12;
  6132. vmx->nested.current_vmcs12_page = page;
  6133. if (enable_shadow_vmcs) {
  6134. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6135. exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
  6136. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6137. vmcs_write64(VMCS_LINK_POINTER,
  6138. __pa(vmx->nested.current_shadow_vmcs));
  6139. vmx->nested.sync_shadow_vmcs = true;
  6140. }
  6141. }
  6142. nested_vmx_succeed(vcpu);
  6143. skip_emulated_instruction(vcpu);
  6144. return 1;
  6145. }
  6146. /* Emulate the VMPTRST instruction */
  6147. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  6148. {
  6149. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6150. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6151. gva_t vmcs_gva;
  6152. struct x86_exception e;
  6153. if (!nested_vmx_check_permission(vcpu))
  6154. return 1;
  6155. if (get_vmx_mem_address(vcpu, exit_qualification,
  6156. vmx_instruction_info, &vmcs_gva))
  6157. return 1;
  6158. /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
  6159. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  6160. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  6161. sizeof(u64), &e)) {
  6162. kvm_inject_page_fault(vcpu, &e);
  6163. return 1;
  6164. }
  6165. nested_vmx_succeed(vcpu);
  6166. skip_emulated_instruction(vcpu);
  6167. return 1;
  6168. }
  6169. /* Emulate the INVEPT instruction */
  6170. static int handle_invept(struct kvm_vcpu *vcpu)
  6171. {
  6172. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6173. u32 vmx_instruction_info, types;
  6174. unsigned long type;
  6175. gva_t gva;
  6176. struct x86_exception e;
  6177. struct {
  6178. u64 eptp, gpa;
  6179. } operand;
  6180. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6181. SECONDARY_EXEC_ENABLE_EPT) ||
  6182. !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
  6183. kvm_queue_exception(vcpu, UD_VECTOR);
  6184. return 1;
  6185. }
  6186. if (!nested_vmx_check_permission(vcpu))
  6187. return 1;
  6188. if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
  6189. kvm_queue_exception(vcpu, UD_VECTOR);
  6190. return 1;
  6191. }
  6192. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6193. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6194. types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  6195. if (!(types & (1UL << type))) {
  6196. nested_vmx_failValid(vcpu,
  6197. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6198. return 1;
  6199. }
  6200. /* According to the Intel VMX instruction reference, the memory
  6201. * operand is read even if it isn't needed (e.g., for type==global)
  6202. */
  6203. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6204. vmx_instruction_info, &gva))
  6205. return 1;
  6206. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6207. sizeof(operand), &e)) {
  6208. kvm_inject_page_fault(vcpu, &e);
  6209. return 1;
  6210. }
  6211. switch (type) {
  6212. case VMX_EPT_EXTENT_GLOBAL:
  6213. kvm_mmu_sync_roots(vcpu);
  6214. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  6215. nested_vmx_succeed(vcpu);
  6216. break;
  6217. default:
  6218. /* Trap single context invalidation invept calls */
  6219. BUG_ON(1);
  6220. break;
  6221. }
  6222. skip_emulated_instruction(vcpu);
  6223. return 1;
  6224. }
  6225. static int handle_invvpid(struct kvm_vcpu *vcpu)
  6226. {
  6227. kvm_queue_exception(vcpu, UD_VECTOR);
  6228. return 1;
  6229. }
  6230. static int handle_pml_full(struct kvm_vcpu *vcpu)
  6231. {
  6232. unsigned long exit_qualification;
  6233. trace_kvm_pml_full(vcpu->vcpu_id);
  6234. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6235. /*
  6236. * PML buffer FULL happened while executing iret from NMI,
  6237. * "blocked by NMI" bit has to be set before next VM entry.
  6238. */
  6239. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6240. cpu_has_virtual_nmis() &&
  6241. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6242. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6243. GUEST_INTR_STATE_NMI);
  6244. /*
  6245. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  6246. * here.., and there's no userspace involvement needed for PML.
  6247. */
  6248. return 1;
  6249. }
  6250. /*
  6251. * The exit handlers return 1 if the exit was handled fully and guest execution
  6252. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  6253. * to be done to userspace and return 0.
  6254. */
  6255. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  6256. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  6257. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  6258. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  6259. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  6260. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  6261. [EXIT_REASON_CR_ACCESS] = handle_cr,
  6262. [EXIT_REASON_DR_ACCESS] = handle_dr,
  6263. [EXIT_REASON_CPUID] = handle_cpuid,
  6264. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  6265. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  6266. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  6267. [EXIT_REASON_HLT] = handle_halt,
  6268. [EXIT_REASON_INVD] = handle_invd,
  6269. [EXIT_REASON_INVLPG] = handle_invlpg,
  6270. [EXIT_REASON_RDPMC] = handle_rdpmc,
  6271. [EXIT_REASON_VMCALL] = handle_vmcall,
  6272. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  6273. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  6274. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  6275. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  6276. [EXIT_REASON_VMREAD] = handle_vmread,
  6277. [EXIT_REASON_VMRESUME] = handle_vmresume,
  6278. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  6279. [EXIT_REASON_VMOFF] = handle_vmoff,
  6280. [EXIT_REASON_VMON] = handle_vmon,
  6281. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  6282. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  6283. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  6284. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  6285. [EXIT_REASON_WBINVD] = handle_wbinvd,
  6286. [EXIT_REASON_XSETBV] = handle_xsetbv,
  6287. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  6288. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  6289. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  6290. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  6291. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  6292. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  6293. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  6294. [EXIT_REASON_INVEPT] = handle_invept,
  6295. [EXIT_REASON_INVVPID] = handle_invvpid,
  6296. [EXIT_REASON_XSAVES] = handle_xsaves,
  6297. [EXIT_REASON_XRSTORS] = handle_xrstors,
  6298. [EXIT_REASON_PML_FULL] = handle_pml_full,
  6299. };
  6300. static const int kvm_vmx_max_exit_handlers =
  6301. ARRAY_SIZE(kvm_vmx_exit_handlers);
  6302. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  6303. struct vmcs12 *vmcs12)
  6304. {
  6305. unsigned long exit_qualification;
  6306. gpa_t bitmap, last_bitmap;
  6307. unsigned int port;
  6308. int size;
  6309. u8 b;
  6310. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  6311. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  6312. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6313. port = exit_qualification >> 16;
  6314. size = (exit_qualification & 7) + 1;
  6315. last_bitmap = (gpa_t)-1;
  6316. b = -1;
  6317. while (size > 0) {
  6318. if (port < 0x8000)
  6319. bitmap = vmcs12->io_bitmap_a;
  6320. else if (port < 0x10000)
  6321. bitmap = vmcs12->io_bitmap_b;
  6322. else
  6323. return true;
  6324. bitmap += (port & 0x7fff) / 8;
  6325. if (last_bitmap != bitmap)
  6326. if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
  6327. return true;
  6328. if (b & (1 << (port & 7)))
  6329. return true;
  6330. port++;
  6331. size--;
  6332. last_bitmap = bitmap;
  6333. }
  6334. return false;
  6335. }
  6336. /*
  6337. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  6338. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  6339. * disinterest in the current event (read or write a specific MSR) by using an
  6340. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  6341. */
  6342. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  6343. struct vmcs12 *vmcs12, u32 exit_reason)
  6344. {
  6345. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  6346. gpa_t bitmap;
  6347. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  6348. return true;
  6349. /*
  6350. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  6351. * for the four combinations of read/write and low/high MSR numbers.
  6352. * First we need to figure out which of the four to use:
  6353. */
  6354. bitmap = vmcs12->msr_bitmap;
  6355. if (exit_reason == EXIT_REASON_MSR_WRITE)
  6356. bitmap += 2048;
  6357. if (msr_index >= 0xc0000000) {
  6358. msr_index -= 0xc0000000;
  6359. bitmap += 1024;
  6360. }
  6361. /* Then read the msr_index'th bit from this bitmap: */
  6362. if (msr_index < 1024*8) {
  6363. unsigned char b;
  6364. if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
  6365. return true;
  6366. return 1 & (b >> (msr_index & 7));
  6367. } else
  6368. return true; /* let L1 handle the wrong parameter */
  6369. }
  6370. /*
  6371. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  6372. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  6373. * intercept (via guest_host_mask etc.) the current event.
  6374. */
  6375. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  6376. struct vmcs12 *vmcs12)
  6377. {
  6378. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6379. int cr = exit_qualification & 15;
  6380. int reg = (exit_qualification >> 8) & 15;
  6381. unsigned long val = kvm_register_readl(vcpu, reg);
  6382. switch ((exit_qualification >> 4) & 3) {
  6383. case 0: /* mov to cr */
  6384. switch (cr) {
  6385. case 0:
  6386. if (vmcs12->cr0_guest_host_mask &
  6387. (val ^ vmcs12->cr0_read_shadow))
  6388. return true;
  6389. break;
  6390. case 3:
  6391. if ((vmcs12->cr3_target_count >= 1 &&
  6392. vmcs12->cr3_target_value0 == val) ||
  6393. (vmcs12->cr3_target_count >= 2 &&
  6394. vmcs12->cr3_target_value1 == val) ||
  6395. (vmcs12->cr3_target_count >= 3 &&
  6396. vmcs12->cr3_target_value2 == val) ||
  6397. (vmcs12->cr3_target_count >= 4 &&
  6398. vmcs12->cr3_target_value3 == val))
  6399. return false;
  6400. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  6401. return true;
  6402. break;
  6403. case 4:
  6404. if (vmcs12->cr4_guest_host_mask &
  6405. (vmcs12->cr4_read_shadow ^ val))
  6406. return true;
  6407. break;
  6408. case 8:
  6409. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  6410. return true;
  6411. break;
  6412. }
  6413. break;
  6414. case 2: /* clts */
  6415. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  6416. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  6417. return true;
  6418. break;
  6419. case 1: /* mov from cr */
  6420. switch (cr) {
  6421. case 3:
  6422. if (vmcs12->cpu_based_vm_exec_control &
  6423. CPU_BASED_CR3_STORE_EXITING)
  6424. return true;
  6425. break;
  6426. case 8:
  6427. if (vmcs12->cpu_based_vm_exec_control &
  6428. CPU_BASED_CR8_STORE_EXITING)
  6429. return true;
  6430. break;
  6431. }
  6432. break;
  6433. case 3: /* lmsw */
  6434. /*
  6435. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  6436. * cr0. Other attempted changes are ignored, with no exit.
  6437. */
  6438. if (vmcs12->cr0_guest_host_mask & 0xe &
  6439. (val ^ vmcs12->cr0_read_shadow))
  6440. return true;
  6441. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  6442. !(vmcs12->cr0_read_shadow & 0x1) &&
  6443. (val & 0x1))
  6444. return true;
  6445. break;
  6446. }
  6447. return false;
  6448. }
  6449. /*
  6450. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  6451. * should handle it ourselves in L0 (and then continue L2). Only call this
  6452. * when in is_guest_mode (L2).
  6453. */
  6454. static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
  6455. {
  6456. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6457. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6458. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6459. u32 exit_reason = vmx->exit_reason;
  6460. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  6461. vmcs_readl(EXIT_QUALIFICATION),
  6462. vmx->idt_vectoring_info,
  6463. intr_info,
  6464. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  6465. KVM_ISA_VMX);
  6466. if (vmx->nested.nested_run_pending)
  6467. return false;
  6468. if (unlikely(vmx->fail)) {
  6469. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  6470. vmcs_read32(VM_INSTRUCTION_ERROR));
  6471. return true;
  6472. }
  6473. switch (exit_reason) {
  6474. case EXIT_REASON_EXCEPTION_NMI:
  6475. if (!is_exception(intr_info))
  6476. return false;
  6477. else if (is_page_fault(intr_info))
  6478. return enable_ept;
  6479. else if (is_no_device(intr_info) &&
  6480. !(vmcs12->guest_cr0 & X86_CR0_TS))
  6481. return false;
  6482. return vmcs12->exception_bitmap &
  6483. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  6484. case EXIT_REASON_EXTERNAL_INTERRUPT:
  6485. return false;
  6486. case EXIT_REASON_TRIPLE_FAULT:
  6487. return true;
  6488. case EXIT_REASON_PENDING_INTERRUPT:
  6489. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  6490. case EXIT_REASON_NMI_WINDOW:
  6491. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  6492. case EXIT_REASON_TASK_SWITCH:
  6493. return true;
  6494. case EXIT_REASON_CPUID:
  6495. if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
  6496. return false;
  6497. return true;
  6498. case EXIT_REASON_HLT:
  6499. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  6500. case EXIT_REASON_INVD:
  6501. return true;
  6502. case EXIT_REASON_INVLPG:
  6503. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  6504. case EXIT_REASON_RDPMC:
  6505. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  6506. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  6507. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  6508. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  6509. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  6510. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  6511. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  6512. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  6513. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  6514. /*
  6515. * VMX instructions trap unconditionally. This allows L1 to
  6516. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  6517. */
  6518. return true;
  6519. case EXIT_REASON_CR_ACCESS:
  6520. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  6521. case EXIT_REASON_DR_ACCESS:
  6522. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  6523. case EXIT_REASON_IO_INSTRUCTION:
  6524. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  6525. case EXIT_REASON_MSR_READ:
  6526. case EXIT_REASON_MSR_WRITE:
  6527. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  6528. case EXIT_REASON_INVALID_STATE:
  6529. return true;
  6530. case EXIT_REASON_MWAIT_INSTRUCTION:
  6531. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  6532. case EXIT_REASON_MONITOR_INSTRUCTION:
  6533. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  6534. case EXIT_REASON_PAUSE_INSTRUCTION:
  6535. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  6536. nested_cpu_has2(vmcs12,
  6537. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  6538. case EXIT_REASON_MCE_DURING_VMENTRY:
  6539. return false;
  6540. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  6541. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  6542. case EXIT_REASON_APIC_ACCESS:
  6543. return nested_cpu_has2(vmcs12,
  6544. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  6545. case EXIT_REASON_APIC_WRITE:
  6546. case EXIT_REASON_EOI_INDUCED:
  6547. /* apic_write and eoi_induced should exit unconditionally. */
  6548. return true;
  6549. case EXIT_REASON_EPT_VIOLATION:
  6550. /*
  6551. * L0 always deals with the EPT violation. If nested EPT is
  6552. * used, and the nested mmu code discovers that the address is
  6553. * missing in the guest EPT table (EPT12), the EPT violation
  6554. * will be injected with nested_ept_inject_page_fault()
  6555. */
  6556. return false;
  6557. case EXIT_REASON_EPT_MISCONFIG:
  6558. /*
  6559. * L2 never uses directly L1's EPT, but rather L0's own EPT
  6560. * table (shadow on EPT) or a merged EPT table that L0 built
  6561. * (EPT on EPT). So any problems with the structure of the
  6562. * table is L0's fault.
  6563. */
  6564. return false;
  6565. case EXIT_REASON_WBINVD:
  6566. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  6567. case EXIT_REASON_XSETBV:
  6568. return true;
  6569. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  6570. /*
  6571. * This should never happen, since it is not possible to
  6572. * set XSS to a non-zero value---neither in L1 nor in L2.
  6573. * If if it were, XSS would have to be checked against
  6574. * the XSS exit bitmap in vmcs12.
  6575. */
  6576. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  6577. default:
  6578. return true;
  6579. }
  6580. }
  6581. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  6582. {
  6583. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  6584. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  6585. }
  6586. static int vmx_enable_pml(struct vcpu_vmx *vmx)
  6587. {
  6588. struct page *pml_pg;
  6589. u32 exec_control;
  6590. pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  6591. if (!pml_pg)
  6592. return -ENOMEM;
  6593. vmx->pml_pg = pml_pg;
  6594. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  6595. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  6596. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6597. exec_control |= SECONDARY_EXEC_ENABLE_PML;
  6598. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6599. return 0;
  6600. }
  6601. static void vmx_disable_pml(struct vcpu_vmx *vmx)
  6602. {
  6603. u32 exec_control;
  6604. ASSERT(vmx->pml_pg);
  6605. __free_page(vmx->pml_pg);
  6606. vmx->pml_pg = NULL;
  6607. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6608. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  6609. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6610. }
  6611. static void vmx_flush_pml_buffer(struct vcpu_vmx *vmx)
  6612. {
  6613. struct kvm *kvm = vmx->vcpu.kvm;
  6614. u64 *pml_buf;
  6615. u16 pml_idx;
  6616. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  6617. /* Do nothing if PML buffer is empty */
  6618. if (pml_idx == (PML_ENTITY_NUM - 1))
  6619. return;
  6620. /* PML index always points to next available PML buffer entity */
  6621. if (pml_idx >= PML_ENTITY_NUM)
  6622. pml_idx = 0;
  6623. else
  6624. pml_idx++;
  6625. pml_buf = page_address(vmx->pml_pg);
  6626. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  6627. u64 gpa;
  6628. gpa = pml_buf[pml_idx];
  6629. WARN_ON(gpa & (PAGE_SIZE - 1));
  6630. mark_page_dirty(kvm, gpa >> PAGE_SHIFT);
  6631. }
  6632. /* reset PML index */
  6633. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  6634. }
  6635. /*
  6636. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  6637. * Called before reporting dirty_bitmap to userspace.
  6638. */
  6639. static void kvm_flush_pml_buffers(struct kvm *kvm)
  6640. {
  6641. int i;
  6642. struct kvm_vcpu *vcpu;
  6643. /*
  6644. * We only need to kick vcpu out of guest mode here, as PML buffer
  6645. * is flushed at beginning of all VMEXITs, and it's obvious that only
  6646. * vcpus running in guest are possible to have unflushed GPAs in PML
  6647. * buffer.
  6648. */
  6649. kvm_for_each_vcpu(i, vcpu, kvm)
  6650. kvm_vcpu_kick(vcpu);
  6651. }
  6652. /*
  6653. * The guest has exited. See if we can fix it or if we need userspace
  6654. * assistance.
  6655. */
  6656. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  6657. {
  6658. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6659. u32 exit_reason = vmx->exit_reason;
  6660. u32 vectoring_info = vmx->idt_vectoring_info;
  6661. /*
  6662. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  6663. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  6664. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  6665. * mode as if vcpus is in root mode, the PML buffer must has been
  6666. * flushed already.
  6667. */
  6668. if (enable_pml)
  6669. vmx_flush_pml_buffer(vmx);
  6670. /* If guest state is invalid, start emulating */
  6671. if (vmx->emulation_required)
  6672. return handle_invalid_guest_state(vcpu);
  6673. if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
  6674. nested_vmx_vmexit(vcpu, exit_reason,
  6675. vmcs_read32(VM_EXIT_INTR_INFO),
  6676. vmcs_readl(EXIT_QUALIFICATION));
  6677. return 1;
  6678. }
  6679. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  6680. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6681. vcpu->run->fail_entry.hardware_entry_failure_reason
  6682. = exit_reason;
  6683. return 0;
  6684. }
  6685. if (unlikely(vmx->fail)) {
  6686. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6687. vcpu->run->fail_entry.hardware_entry_failure_reason
  6688. = vmcs_read32(VM_INSTRUCTION_ERROR);
  6689. return 0;
  6690. }
  6691. /*
  6692. * Note:
  6693. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  6694. * delivery event since it indicates guest is accessing MMIO.
  6695. * The vm-exit can be triggered again after return to guest that
  6696. * will cause infinite loop.
  6697. */
  6698. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6699. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  6700. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  6701. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  6702. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6703. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  6704. vcpu->run->internal.ndata = 2;
  6705. vcpu->run->internal.data[0] = vectoring_info;
  6706. vcpu->run->internal.data[1] = exit_reason;
  6707. return 0;
  6708. }
  6709. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
  6710. !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
  6711. get_vmcs12(vcpu))))) {
  6712. if (vmx_interrupt_allowed(vcpu)) {
  6713. vmx->soft_vnmi_blocked = 0;
  6714. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  6715. vcpu->arch.nmi_pending) {
  6716. /*
  6717. * This CPU don't support us in finding the end of an
  6718. * NMI-blocked window if the guest runs with IRQs
  6719. * disabled. So we pull the trigger after 1 s of
  6720. * futile waiting, but inform the user about this.
  6721. */
  6722. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  6723. "state on VCPU %d after 1 s timeout\n",
  6724. __func__, vcpu->vcpu_id);
  6725. vmx->soft_vnmi_blocked = 0;
  6726. }
  6727. }
  6728. if (exit_reason < kvm_vmx_max_exit_handlers
  6729. && kvm_vmx_exit_handlers[exit_reason])
  6730. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  6731. else {
  6732. WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
  6733. kvm_queue_exception(vcpu, UD_VECTOR);
  6734. return 1;
  6735. }
  6736. }
  6737. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  6738. {
  6739. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6740. if (is_guest_mode(vcpu) &&
  6741. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  6742. return;
  6743. if (irr == -1 || tpr < irr) {
  6744. vmcs_write32(TPR_THRESHOLD, 0);
  6745. return;
  6746. }
  6747. vmcs_write32(TPR_THRESHOLD, irr);
  6748. }
  6749. static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  6750. {
  6751. u32 sec_exec_control;
  6752. /*
  6753. * There is not point to enable virtualize x2apic without enable
  6754. * apicv
  6755. */
  6756. if (!cpu_has_vmx_virtualize_x2apic_mode() ||
  6757. !vmx_vm_has_apicv(vcpu->kvm))
  6758. return;
  6759. if (!vm_need_tpr_shadow(vcpu->kvm))
  6760. return;
  6761. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6762. if (set) {
  6763. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6764. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6765. } else {
  6766. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6767. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6768. }
  6769. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  6770. vmx_set_msr_bitmap(vcpu);
  6771. }
  6772. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  6773. {
  6774. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6775. /*
  6776. * Currently we do not handle the nested case where L2 has an
  6777. * APIC access page of its own; that page is still pinned.
  6778. * Hence, we skip the case where the VCPU is in guest mode _and_
  6779. * L1 prepared an APIC access page for L2.
  6780. *
  6781. * For the case where L1 and L2 share the same APIC access page
  6782. * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
  6783. * in the vmcs12), this function will only update either the vmcs01
  6784. * or the vmcs02. If the former, the vmcs02 will be updated by
  6785. * prepare_vmcs02. If the latter, the vmcs01 will be updated in
  6786. * the next L2->L1 exit.
  6787. */
  6788. if (!is_guest_mode(vcpu) ||
  6789. !nested_cpu_has2(vmx->nested.current_vmcs12,
  6790. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  6791. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  6792. }
  6793. static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
  6794. {
  6795. u16 status;
  6796. u8 old;
  6797. if (isr == -1)
  6798. isr = 0;
  6799. status = vmcs_read16(GUEST_INTR_STATUS);
  6800. old = status >> 8;
  6801. if (isr != old) {
  6802. status &= 0xff;
  6803. status |= isr << 8;
  6804. vmcs_write16(GUEST_INTR_STATUS, status);
  6805. }
  6806. }
  6807. static void vmx_set_rvi(int vector)
  6808. {
  6809. u16 status;
  6810. u8 old;
  6811. if (vector == -1)
  6812. vector = 0;
  6813. status = vmcs_read16(GUEST_INTR_STATUS);
  6814. old = (u8)status & 0xff;
  6815. if ((u8)vector != old) {
  6816. status &= ~0xff;
  6817. status |= (u8)vector;
  6818. vmcs_write16(GUEST_INTR_STATUS, status);
  6819. }
  6820. }
  6821. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  6822. {
  6823. if (!is_guest_mode(vcpu)) {
  6824. vmx_set_rvi(max_irr);
  6825. return;
  6826. }
  6827. if (max_irr == -1)
  6828. return;
  6829. /*
  6830. * In guest mode. If a vmexit is needed, vmx_check_nested_events
  6831. * handles it.
  6832. */
  6833. if (nested_exit_on_intr(vcpu))
  6834. return;
  6835. /*
  6836. * Else, fall back to pre-APICv interrupt injection since L2
  6837. * is run without virtual interrupt delivery.
  6838. */
  6839. if (!kvm_event_needs_reinjection(vcpu) &&
  6840. vmx_interrupt_allowed(vcpu)) {
  6841. kvm_queue_interrupt(vcpu, max_irr, false);
  6842. vmx_inject_irq(vcpu);
  6843. }
  6844. }
  6845. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  6846. {
  6847. if (!vmx_vm_has_apicv(vcpu->kvm))
  6848. return;
  6849. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  6850. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  6851. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  6852. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  6853. }
  6854. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  6855. {
  6856. u32 exit_intr_info;
  6857. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  6858. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  6859. return;
  6860. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6861. exit_intr_info = vmx->exit_intr_info;
  6862. /* Handle machine checks before interrupts are enabled */
  6863. if (is_machine_check(exit_intr_info))
  6864. kvm_machine_check();
  6865. /* We need to handle NMIs before interrupts are enabled */
  6866. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  6867. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  6868. kvm_before_handle_nmi(&vmx->vcpu);
  6869. asm("int $2");
  6870. kvm_after_handle_nmi(&vmx->vcpu);
  6871. }
  6872. }
  6873. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  6874. {
  6875. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6876. /*
  6877. * If external interrupt exists, IF bit is set in rflags/eflags on the
  6878. * interrupt stack frame, and interrupt will be enabled on a return
  6879. * from interrupt handler.
  6880. */
  6881. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  6882. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  6883. unsigned int vector;
  6884. unsigned long entry;
  6885. gate_desc *desc;
  6886. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6887. #ifdef CONFIG_X86_64
  6888. unsigned long tmp;
  6889. #endif
  6890. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  6891. desc = (gate_desc *)vmx->host_idt_base + vector;
  6892. entry = gate_offset(*desc);
  6893. asm volatile(
  6894. #ifdef CONFIG_X86_64
  6895. "mov %%" _ASM_SP ", %[sp]\n\t"
  6896. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  6897. "push $%c[ss]\n\t"
  6898. "push %[sp]\n\t"
  6899. #endif
  6900. "pushf\n\t"
  6901. "orl $0x200, (%%" _ASM_SP ")\n\t"
  6902. __ASM_SIZE(push) " $%c[cs]\n\t"
  6903. "call *%[entry]\n\t"
  6904. :
  6905. #ifdef CONFIG_X86_64
  6906. [sp]"=&r"(tmp)
  6907. #endif
  6908. :
  6909. [entry]"r"(entry),
  6910. [ss]"i"(__KERNEL_DS),
  6911. [cs]"i"(__KERNEL_CS)
  6912. );
  6913. } else
  6914. local_irq_enable();
  6915. }
  6916. static bool vmx_mpx_supported(void)
  6917. {
  6918. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  6919. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  6920. }
  6921. static bool vmx_xsaves_supported(void)
  6922. {
  6923. return vmcs_config.cpu_based_2nd_exec_ctrl &
  6924. SECONDARY_EXEC_XSAVES;
  6925. }
  6926. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  6927. {
  6928. u32 exit_intr_info;
  6929. bool unblock_nmi;
  6930. u8 vector;
  6931. bool idtv_info_valid;
  6932. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  6933. if (cpu_has_virtual_nmis()) {
  6934. if (vmx->nmi_known_unmasked)
  6935. return;
  6936. /*
  6937. * Can't use vmx->exit_intr_info since we're not sure what
  6938. * the exit reason is.
  6939. */
  6940. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6941. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  6942. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  6943. /*
  6944. * SDM 3: 27.7.1.2 (September 2008)
  6945. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  6946. * a guest IRET fault.
  6947. * SDM 3: 23.2.2 (September 2008)
  6948. * Bit 12 is undefined in any of the following cases:
  6949. * If the VM exit sets the valid bit in the IDT-vectoring
  6950. * information field.
  6951. * If the VM exit is due to a double fault.
  6952. */
  6953. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  6954. vector != DF_VECTOR && !idtv_info_valid)
  6955. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6956. GUEST_INTR_STATE_NMI);
  6957. else
  6958. vmx->nmi_known_unmasked =
  6959. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  6960. & GUEST_INTR_STATE_NMI);
  6961. } else if (unlikely(vmx->soft_vnmi_blocked))
  6962. vmx->vnmi_blocked_time +=
  6963. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  6964. }
  6965. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  6966. u32 idt_vectoring_info,
  6967. int instr_len_field,
  6968. int error_code_field)
  6969. {
  6970. u8 vector;
  6971. int type;
  6972. bool idtv_info_valid;
  6973. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  6974. vcpu->arch.nmi_injected = false;
  6975. kvm_clear_exception_queue(vcpu);
  6976. kvm_clear_interrupt_queue(vcpu);
  6977. if (!idtv_info_valid)
  6978. return;
  6979. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6980. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  6981. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  6982. switch (type) {
  6983. case INTR_TYPE_NMI_INTR:
  6984. vcpu->arch.nmi_injected = true;
  6985. /*
  6986. * SDM 3: 27.7.1.2 (September 2008)
  6987. * Clear bit "block by NMI" before VM entry if a NMI
  6988. * delivery faulted.
  6989. */
  6990. vmx_set_nmi_mask(vcpu, false);
  6991. break;
  6992. case INTR_TYPE_SOFT_EXCEPTION:
  6993. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  6994. /* fall through */
  6995. case INTR_TYPE_HARD_EXCEPTION:
  6996. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  6997. u32 err = vmcs_read32(error_code_field);
  6998. kvm_requeue_exception_e(vcpu, vector, err);
  6999. } else
  7000. kvm_requeue_exception(vcpu, vector);
  7001. break;
  7002. case INTR_TYPE_SOFT_INTR:
  7003. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  7004. /* fall through */
  7005. case INTR_TYPE_EXT_INTR:
  7006. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  7007. break;
  7008. default:
  7009. break;
  7010. }
  7011. }
  7012. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  7013. {
  7014. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  7015. VM_EXIT_INSTRUCTION_LEN,
  7016. IDT_VECTORING_ERROR_CODE);
  7017. }
  7018. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  7019. {
  7020. __vmx_complete_interrupts(vcpu,
  7021. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  7022. VM_ENTRY_INSTRUCTION_LEN,
  7023. VM_ENTRY_EXCEPTION_ERROR_CODE);
  7024. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  7025. }
  7026. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  7027. {
  7028. int i, nr_msrs;
  7029. struct perf_guest_switch_msr *msrs;
  7030. msrs = perf_guest_get_msrs(&nr_msrs);
  7031. if (!msrs)
  7032. return;
  7033. for (i = 0; i < nr_msrs; i++)
  7034. if (msrs[i].host == msrs[i].guest)
  7035. clear_atomic_switch_msr(vmx, msrs[i].msr);
  7036. else
  7037. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  7038. msrs[i].host);
  7039. }
  7040. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  7041. {
  7042. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7043. unsigned long debugctlmsr, cr4;
  7044. /* Record the guest's net vcpu time for enforced NMI injections. */
  7045. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  7046. vmx->entry_time = ktime_get();
  7047. /* Don't enter VMX if guest state is invalid, let the exit handler
  7048. start emulation until we arrive back to a valid state */
  7049. if (vmx->emulation_required)
  7050. return;
  7051. if (vmx->ple_window_dirty) {
  7052. vmx->ple_window_dirty = false;
  7053. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  7054. }
  7055. if (vmx->nested.sync_shadow_vmcs) {
  7056. copy_vmcs12_to_shadow(vmx);
  7057. vmx->nested.sync_shadow_vmcs = false;
  7058. }
  7059. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  7060. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  7061. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  7062. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  7063. cr4 = cr4_read_shadow();
  7064. if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
  7065. vmcs_writel(HOST_CR4, cr4);
  7066. vmx->host_state.vmcs_host_cr4 = cr4;
  7067. }
  7068. /* When single-stepping over STI and MOV SS, we must clear the
  7069. * corresponding interruptibility bits in the guest state. Otherwise
  7070. * vmentry fails as it then expects bit 14 (BS) in pending debug
  7071. * exceptions being set, but that's not correct for the guest debugging
  7072. * case. */
  7073. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  7074. vmx_set_interrupt_shadow(vcpu, 0);
  7075. atomic_switch_perf_msrs(vmx);
  7076. debugctlmsr = get_debugctlmsr();
  7077. vmx->__launched = vmx->loaded_vmcs->launched;
  7078. asm(
  7079. /* Store host registers */
  7080. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  7081. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  7082. "push %%" _ASM_CX " \n\t"
  7083. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  7084. "je 1f \n\t"
  7085. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  7086. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  7087. "1: \n\t"
  7088. /* Reload cr2 if changed */
  7089. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  7090. "mov %%cr2, %%" _ASM_DX " \n\t"
  7091. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  7092. "je 2f \n\t"
  7093. "mov %%" _ASM_AX", %%cr2 \n\t"
  7094. "2: \n\t"
  7095. /* Check if vmlaunch of vmresume is needed */
  7096. "cmpl $0, %c[launched](%0) \n\t"
  7097. /* Load guest registers. Don't clobber flags. */
  7098. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  7099. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  7100. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  7101. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  7102. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  7103. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  7104. #ifdef CONFIG_X86_64
  7105. "mov %c[r8](%0), %%r8 \n\t"
  7106. "mov %c[r9](%0), %%r9 \n\t"
  7107. "mov %c[r10](%0), %%r10 \n\t"
  7108. "mov %c[r11](%0), %%r11 \n\t"
  7109. "mov %c[r12](%0), %%r12 \n\t"
  7110. "mov %c[r13](%0), %%r13 \n\t"
  7111. "mov %c[r14](%0), %%r14 \n\t"
  7112. "mov %c[r15](%0), %%r15 \n\t"
  7113. #endif
  7114. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  7115. /* Enter guest mode */
  7116. "jne 1f \n\t"
  7117. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  7118. "jmp 2f \n\t"
  7119. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  7120. "2: "
  7121. /* Save guest registers, load host registers, keep flags */
  7122. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  7123. "pop %0 \n\t"
  7124. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  7125. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  7126. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  7127. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  7128. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  7129. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  7130. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  7131. #ifdef CONFIG_X86_64
  7132. "mov %%r8, %c[r8](%0) \n\t"
  7133. "mov %%r9, %c[r9](%0) \n\t"
  7134. "mov %%r10, %c[r10](%0) \n\t"
  7135. "mov %%r11, %c[r11](%0) \n\t"
  7136. "mov %%r12, %c[r12](%0) \n\t"
  7137. "mov %%r13, %c[r13](%0) \n\t"
  7138. "mov %%r14, %c[r14](%0) \n\t"
  7139. "mov %%r15, %c[r15](%0) \n\t"
  7140. #endif
  7141. "mov %%cr2, %%" _ASM_AX " \n\t"
  7142. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  7143. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  7144. "setbe %c[fail](%0) \n\t"
  7145. ".pushsection .rodata \n\t"
  7146. ".global vmx_return \n\t"
  7147. "vmx_return: " _ASM_PTR " 2b \n\t"
  7148. ".popsection"
  7149. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  7150. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  7151. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  7152. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  7153. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  7154. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  7155. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  7156. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  7157. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  7158. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  7159. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  7160. #ifdef CONFIG_X86_64
  7161. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  7162. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  7163. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  7164. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  7165. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  7166. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  7167. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  7168. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  7169. #endif
  7170. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  7171. [wordsize]"i"(sizeof(ulong))
  7172. : "cc", "memory"
  7173. #ifdef CONFIG_X86_64
  7174. , "rax", "rbx", "rdi", "rsi"
  7175. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  7176. #else
  7177. , "eax", "ebx", "edi", "esi"
  7178. #endif
  7179. );
  7180. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  7181. if (debugctlmsr)
  7182. update_debugctlmsr(debugctlmsr);
  7183. #ifndef CONFIG_X86_64
  7184. /*
  7185. * The sysexit path does not restore ds/es, so we must set them to
  7186. * a reasonable value ourselves.
  7187. *
  7188. * We can't defer this to vmx_load_host_state() since that function
  7189. * may be executed in interrupt context, which saves and restore segments
  7190. * around it, nullifying its effect.
  7191. */
  7192. loadsegment(ds, __USER_DS);
  7193. loadsegment(es, __USER_DS);
  7194. #endif
  7195. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  7196. | (1 << VCPU_EXREG_RFLAGS)
  7197. | (1 << VCPU_EXREG_PDPTR)
  7198. | (1 << VCPU_EXREG_SEGMENTS)
  7199. | (1 << VCPU_EXREG_CR3));
  7200. vcpu->arch.regs_dirty = 0;
  7201. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  7202. vmx->loaded_vmcs->launched = 1;
  7203. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  7204. trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
  7205. /*
  7206. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  7207. * we did not inject a still-pending event to L1 now because of
  7208. * nested_run_pending, we need to re-enable this bit.
  7209. */
  7210. if (vmx->nested.nested_run_pending)
  7211. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7212. vmx->nested.nested_run_pending = 0;
  7213. vmx_complete_atomic_exit(vmx);
  7214. vmx_recover_nmi_blocking(vmx);
  7215. vmx_complete_interrupts(vmx);
  7216. }
  7217. static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
  7218. {
  7219. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7220. int cpu;
  7221. if (vmx->loaded_vmcs == &vmx->vmcs01)
  7222. return;
  7223. cpu = get_cpu();
  7224. vmx->loaded_vmcs = &vmx->vmcs01;
  7225. vmx_vcpu_put(vcpu);
  7226. vmx_vcpu_load(vcpu, cpu);
  7227. vcpu->cpu = cpu;
  7228. put_cpu();
  7229. }
  7230. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  7231. {
  7232. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7233. if (enable_pml)
  7234. vmx_disable_pml(vmx);
  7235. free_vpid(vmx);
  7236. leave_guest_mode(vcpu);
  7237. vmx_load_vmcs01(vcpu);
  7238. free_nested(vmx);
  7239. free_loaded_vmcs(vmx->loaded_vmcs);
  7240. kfree(vmx->guest_msrs);
  7241. kvm_vcpu_uninit(vcpu);
  7242. kmem_cache_free(kvm_vcpu_cache, vmx);
  7243. }
  7244. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  7245. {
  7246. int err;
  7247. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  7248. int cpu;
  7249. if (!vmx)
  7250. return ERR_PTR(-ENOMEM);
  7251. allocate_vpid(vmx);
  7252. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  7253. if (err)
  7254. goto free_vcpu;
  7255. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  7256. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  7257. > PAGE_SIZE);
  7258. err = -ENOMEM;
  7259. if (!vmx->guest_msrs) {
  7260. goto uninit_vcpu;
  7261. }
  7262. vmx->loaded_vmcs = &vmx->vmcs01;
  7263. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  7264. if (!vmx->loaded_vmcs->vmcs)
  7265. goto free_msrs;
  7266. if (!vmm_exclusive)
  7267. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  7268. loaded_vmcs_init(vmx->loaded_vmcs);
  7269. if (!vmm_exclusive)
  7270. kvm_cpu_vmxoff();
  7271. cpu = get_cpu();
  7272. vmx_vcpu_load(&vmx->vcpu, cpu);
  7273. vmx->vcpu.cpu = cpu;
  7274. err = vmx_vcpu_setup(vmx);
  7275. vmx_vcpu_put(&vmx->vcpu);
  7276. put_cpu();
  7277. if (err)
  7278. goto free_vmcs;
  7279. if (vm_need_virtualize_apic_accesses(kvm)) {
  7280. err = alloc_apic_access_page(kvm);
  7281. if (err)
  7282. goto free_vmcs;
  7283. }
  7284. if (enable_ept) {
  7285. if (!kvm->arch.ept_identity_map_addr)
  7286. kvm->arch.ept_identity_map_addr =
  7287. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  7288. err = init_rmode_identity_map(kvm);
  7289. if (err)
  7290. goto free_vmcs;
  7291. }
  7292. if (nested)
  7293. nested_vmx_setup_ctls_msrs(vmx);
  7294. vmx->nested.posted_intr_nv = -1;
  7295. vmx->nested.current_vmptr = -1ull;
  7296. vmx->nested.current_vmcs12 = NULL;
  7297. /*
  7298. * If PML is turned on, failure on enabling PML just results in failure
  7299. * of creating the vcpu, therefore we can simplify PML logic (by
  7300. * avoiding dealing with cases, such as enabling PML partially on vcpus
  7301. * for the guest, etc.
  7302. */
  7303. if (enable_pml) {
  7304. err = vmx_enable_pml(vmx);
  7305. if (err)
  7306. goto free_vmcs;
  7307. }
  7308. return &vmx->vcpu;
  7309. free_vmcs:
  7310. free_loaded_vmcs(vmx->loaded_vmcs);
  7311. free_msrs:
  7312. kfree(vmx->guest_msrs);
  7313. uninit_vcpu:
  7314. kvm_vcpu_uninit(&vmx->vcpu);
  7315. free_vcpu:
  7316. free_vpid(vmx);
  7317. kmem_cache_free(kvm_vcpu_cache, vmx);
  7318. return ERR_PTR(err);
  7319. }
  7320. static void __init vmx_check_processor_compat(void *rtn)
  7321. {
  7322. struct vmcs_config vmcs_conf;
  7323. *(int *)rtn = 0;
  7324. if (setup_vmcs_config(&vmcs_conf) < 0)
  7325. *(int *)rtn = -EIO;
  7326. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  7327. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  7328. smp_processor_id());
  7329. *(int *)rtn = -EIO;
  7330. }
  7331. }
  7332. static int get_ept_level(void)
  7333. {
  7334. return VMX_EPT_DEFAULT_GAW + 1;
  7335. }
  7336. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  7337. {
  7338. u64 ret;
  7339. /* For VT-d and EPT combination
  7340. * 1. MMIO: always map as UC
  7341. * 2. EPT with VT-d:
  7342. * a. VT-d without snooping control feature: can't guarantee the
  7343. * result, try to trust guest.
  7344. * b. VT-d with snooping control feature: snooping control feature of
  7345. * VT-d engine can guarantee the cache correctness. Just set it
  7346. * to WB to keep consistent with host. So the same as item 3.
  7347. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  7348. * consistent with host MTRR
  7349. */
  7350. if (is_mmio)
  7351. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  7352. else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
  7353. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  7354. VMX_EPT_MT_EPTE_SHIFT;
  7355. else
  7356. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  7357. | VMX_EPT_IPAT_BIT;
  7358. return ret;
  7359. }
  7360. static int vmx_get_lpage_level(void)
  7361. {
  7362. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  7363. return PT_DIRECTORY_LEVEL;
  7364. else
  7365. /* For shadow and EPT supported 1GB page */
  7366. return PT_PDPE_LEVEL;
  7367. }
  7368. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  7369. {
  7370. struct kvm_cpuid_entry2 *best;
  7371. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7372. u32 exec_control;
  7373. vmx->rdtscp_enabled = false;
  7374. if (vmx_rdtscp_supported()) {
  7375. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7376. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  7377. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  7378. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  7379. vmx->rdtscp_enabled = true;
  7380. else {
  7381. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  7382. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7383. exec_control);
  7384. }
  7385. }
  7386. if (nested && !vmx->rdtscp_enabled)
  7387. vmx->nested.nested_vmx_secondary_ctls_high &=
  7388. ~SECONDARY_EXEC_RDTSCP;
  7389. }
  7390. /* Exposing INVPCID only when PCID is exposed */
  7391. best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  7392. if (vmx_invpcid_supported() &&
  7393. best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
  7394. guest_cpuid_has_pcid(vcpu)) {
  7395. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7396. exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
  7397. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7398. exec_control);
  7399. } else {
  7400. if (cpu_has_secondary_exec_ctrls()) {
  7401. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7402. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  7403. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7404. exec_control);
  7405. }
  7406. if (best)
  7407. best->ebx &= ~bit(X86_FEATURE_INVPCID);
  7408. }
  7409. }
  7410. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  7411. {
  7412. if (func == 1 && nested)
  7413. entry->ecx |= bit(X86_FEATURE_VMX);
  7414. }
  7415. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  7416. struct x86_exception *fault)
  7417. {
  7418. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7419. u32 exit_reason;
  7420. if (fault->error_code & PFERR_RSVD_MASK)
  7421. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  7422. else
  7423. exit_reason = EXIT_REASON_EPT_VIOLATION;
  7424. nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
  7425. vmcs12->guest_physical_address = fault->address;
  7426. }
  7427. /* Callbacks for nested_ept_init_mmu_context: */
  7428. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  7429. {
  7430. /* return the page table to be shadowed - in our case, EPT12 */
  7431. return get_vmcs12(vcpu)->ept_pointer;
  7432. }
  7433. static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  7434. {
  7435. WARN_ON(mmu_is_nested(vcpu));
  7436. kvm_init_shadow_ept_mmu(vcpu,
  7437. to_vmx(vcpu)->nested.nested_vmx_ept_caps &
  7438. VMX_EPT_EXECUTE_ONLY_BIT);
  7439. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  7440. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  7441. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  7442. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  7443. }
  7444. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  7445. {
  7446. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  7447. }
  7448. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  7449. u16 error_code)
  7450. {
  7451. bool inequality, bit;
  7452. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  7453. inequality =
  7454. (error_code & vmcs12->page_fault_error_code_mask) !=
  7455. vmcs12->page_fault_error_code_match;
  7456. return inequality ^ bit;
  7457. }
  7458. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  7459. struct x86_exception *fault)
  7460. {
  7461. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7462. WARN_ON(!is_guest_mode(vcpu));
  7463. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
  7464. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  7465. vmcs_read32(VM_EXIT_INTR_INFO),
  7466. vmcs_readl(EXIT_QUALIFICATION));
  7467. else
  7468. kvm_inject_page_fault(vcpu, fault);
  7469. }
  7470. static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  7471. struct vmcs12 *vmcs12)
  7472. {
  7473. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7474. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7475. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  7476. if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
  7477. vmcs12->apic_access_addr >> maxphyaddr)
  7478. return false;
  7479. /*
  7480. * Translate L1 physical address to host physical
  7481. * address for vmcs02. Keep the page pinned, so this
  7482. * physical address remains valid. We keep a reference
  7483. * to it so we can release it later.
  7484. */
  7485. if (vmx->nested.apic_access_page) /* shouldn't happen */
  7486. nested_release_page(vmx->nested.apic_access_page);
  7487. vmx->nested.apic_access_page =
  7488. nested_get_page(vcpu, vmcs12->apic_access_addr);
  7489. }
  7490. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  7491. if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
  7492. vmcs12->virtual_apic_page_addr >> maxphyaddr)
  7493. return false;
  7494. if (vmx->nested.virtual_apic_page) /* shouldn't happen */
  7495. nested_release_page(vmx->nested.virtual_apic_page);
  7496. vmx->nested.virtual_apic_page =
  7497. nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
  7498. /*
  7499. * Failing the vm entry is _not_ what the processor does
  7500. * but it's basically the only possibility we have.
  7501. * We could still enter the guest if CR8 load exits are
  7502. * enabled, CR8 store exits are enabled, and virtualize APIC
  7503. * access is disabled; in this case the processor would never
  7504. * use the TPR shadow and we could simply clear the bit from
  7505. * the execution control. But such a configuration is useless,
  7506. * so let's keep the code simple.
  7507. */
  7508. if (!vmx->nested.virtual_apic_page)
  7509. return false;
  7510. }
  7511. if (nested_cpu_has_posted_intr(vmcs12)) {
  7512. if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
  7513. vmcs12->posted_intr_desc_addr >> maxphyaddr)
  7514. return false;
  7515. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  7516. kunmap(vmx->nested.pi_desc_page);
  7517. nested_release_page(vmx->nested.pi_desc_page);
  7518. }
  7519. vmx->nested.pi_desc_page =
  7520. nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
  7521. if (!vmx->nested.pi_desc_page)
  7522. return false;
  7523. vmx->nested.pi_desc =
  7524. (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
  7525. if (!vmx->nested.pi_desc) {
  7526. nested_release_page_clean(vmx->nested.pi_desc_page);
  7527. return false;
  7528. }
  7529. vmx->nested.pi_desc =
  7530. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  7531. (unsigned long)(vmcs12->posted_intr_desc_addr &
  7532. (PAGE_SIZE - 1)));
  7533. }
  7534. return true;
  7535. }
  7536. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  7537. {
  7538. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  7539. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7540. if (vcpu->arch.virtual_tsc_khz == 0)
  7541. return;
  7542. /* Make sure short timeouts reliably trigger an immediate vmexit.
  7543. * hrtimer_start does not guarantee this. */
  7544. if (preemption_timeout <= 1) {
  7545. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  7546. return;
  7547. }
  7548. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  7549. preemption_timeout *= 1000000;
  7550. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  7551. hrtimer_start(&vmx->nested.preemption_timer,
  7552. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  7553. }
  7554. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  7555. struct vmcs12 *vmcs12)
  7556. {
  7557. int maxphyaddr;
  7558. u64 addr;
  7559. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7560. return 0;
  7561. if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
  7562. WARN_ON(1);
  7563. return -EINVAL;
  7564. }
  7565. maxphyaddr = cpuid_maxphyaddr(vcpu);
  7566. if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
  7567. ((addr + PAGE_SIZE) >> maxphyaddr))
  7568. return -EINVAL;
  7569. return 0;
  7570. }
  7571. /*
  7572. * Merge L0's and L1's MSR bitmap, return false to indicate that
  7573. * we do not use the hardware.
  7574. */
  7575. static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
  7576. struct vmcs12 *vmcs12)
  7577. {
  7578. int msr;
  7579. struct page *page;
  7580. unsigned long *msr_bitmap;
  7581. if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
  7582. return false;
  7583. page = nested_get_page(vcpu, vmcs12->msr_bitmap);
  7584. if (!page) {
  7585. WARN_ON(1);
  7586. return false;
  7587. }
  7588. msr_bitmap = (unsigned long *)kmap(page);
  7589. if (!msr_bitmap) {
  7590. nested_release_page_clean(page);
  7591. WARN_ON(1);
  7592. return false;
  7593. }
  7594. if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
  7595. if (nested_cpu_has_apic_reg_virt(vmcs12))
  7596. for (msr = 0x800; msr <= 0x8ff; msr++)
  7597. nested_vmx_disable_intercept_for_msr(
  7598. msr_bitmap,
  7599. vmx_msr_bitmap_nested,
  7600. msr, MSR_TYPE_R);
  7601. /* TPR is allowed */
  7602. nested_vmx_disable_intercept_for_msr(msr_bitmap,
  7603. vmx_msr_bitmap_nested,
  7604. APIC_BASE_MSR + (APIC_TASKPRI >> 4),
  7605. MSR_TYPE_R | MSR_TYPE_W);
  7606. if (nested_cpu_has_vid(vmcs12)) {
  7607. /* EOI and self-IPI are allowed */
  7608. nested_vmx_disable_intercept_for_msr(
  7609. msr_bitmap,
  7610. vmx_msr_bitmap_nested,
  7611. APIC_BASE_MSR + (APIC_EOI >> 4),
  7612. MSR_TYPE_W);
  7613. nested_vmx_disable_intercept_for_msr(
  7614. msr_bitmap,
  7615. vmx_msr_bitmap_nested,
  7616. APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
  7617. MSR_TYPE_W);
  7618. }
  7619. } else {
  7620. /*
  7621. * Enable reading intercept of all the x2apic
  7622. * MSRs. We should not rely on vmcs12 to do any
  7623. * optimizations here, it may have been modified
  7624. * by L1.
  7625. */
  7626. for (msr = 0x800; msr <= 0x8ff; msr++)
  7627. __vmx_enable_intercept_for_msr(
  7628. vmx_msr_bitmap_nested,
  7629. msr,
  7630. MSR_TYPE_R);
  7631. __vmx_enable_intercept_for_msr(
  7632. vmx_msr_bitmap_nested,
  7633. APIC_BASE_MSR + (APIC_TASKPRI >> 4),
  7634. MSR_TYPE_W);
  7635. __vmx_enable_intercept_for_msr(
  7636. vmx_msr_bitmap_nested,
  7637. APIC_BASE_MSR + (APIC_EOI >> 4),
  7638. MSR_TYPE_W);
  7639. __vmx_enable_intercept_for_msr(
  7640. vmx_msr_bitmap_nested,
  7641. APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
  7642. MSR_TYPE_W);
  7643. }
  7644. kunmap(page);
  7645. nested_release_page_clean(page);
  7646. return true;
  7647. }
  7648. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  7649. struct vmcs12 *vmcs12)
  7650. {
  7651. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  7652. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  7653. !nested_cpu_has_vid(vmcs12) &&
  7654. !nested_cpu_has_posted_intr(vmcs12))
  7655. return 0;
  7656. /*
  7657. * If virtualize x2apic mode is enabled,
  7658. * virtualize apic access must be disabled.
  7659. */
  7660. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  7661. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  7662. return -EINVAL;
  7663. /*
  7664. * If virtual interrupt delivery is enabled,
  7665. * we must exit on external interrupts.
  7666. */
  7667. if (nested_cpu_has_vid(vmcs12) &&
  7668. !nested_exit_on_intr(vcpu))
  7669. return -EINVAL;
  7670. /*
  7671. * bits 15:8 should be zero in posted_intr_nv,
  7672. * the descriptor address has been already checked
  7673. * in nested_get_vmcs12_pages.
  7674. */
  7675. if (nested_cpu_has_posted_intr(vmcs12) &&
  7676. (!nested_cpu_has_vid(vmcs12) ||
  7677. !nested_exit_intr_ack_set(vcpu) ||
  7678. vmcs12->posted_intr_nv & 0xff00))
  7679. return -EINVAL;
  7680. /* tpr shadow is needed by all apicv features. */
  7681. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  7682. return -EINVAL;
  7683. return 0;
  7684. }
  7685. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  7686. unsigned long count_field,
  7687. unsigned long addr_field)
  7688. {
  7689. int maxphyaddr;
  7690. u64 count, addr;
  7691. if (vmcs12_read_any(vcpu, count_field, &count) ||
  7692. vmcs12_read_any(vcpu, addr_field, &addr)) {
  7693. WARN_ON(1);
  7694. return -EINVAL;
  7695. }
  7696. if (count == 0)
  7697. return 0;
  7698. maxphyaddr = cpuid_maxphyaddr(vcpu);
  7699. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  7700. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  7701. pr_warn_ratelimited(
  7702. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  7703. addr_field, maxphyaddr, count, addr);
  7704. return -EINVAL;
  7705. }
  7706. return 0;
  7707. }
  7708. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  7709. struct vmcs12 *vmcs12)
  7710. {
  7711. if (vmcs12->vm_exit_msr_load_count == 0 &&
  7712. vmcs12->vm_exit_msr_store_count == 0 &&
  7713. vmcs12->vm_entry_msr_load_count == 0)
  7714. return 0; /* Fast path */
  7715. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  7716. VM_EXIT_MSR_LOAD_ADDR) ||
  7717. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  7718. VM_EXIT_MSR_STORE_ADDR) ||
  7719. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  7720. VM_ENTRY_MSR_LOAD_ADDR))
  7721. return -EINVAL;
  7722. return 0;
  7723. }
  7724. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  7725. struct vmx_msr_entry *e)
  7726. {
  7727. /* x2APIC MSR accesses are not allowed */
  7728. if (apic_x2apic_mode(vcpu->arch.apic) && e->index >> 8 == 0x8)
  7729. return -EINVAL;
  7730. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  7731. e->index == MSR_IA32_UCODE_REV)
  7732. return -EINVAL;
  7733. if (e->reserved != 0)
  7734. return -EINVAL;
  7735. return 0;
  7736. }
  7737. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  7738. struct vmx_msr_entry *e)
  7739. {
  7740. if (e->index == MSR_FS_BASE ||
  7741. e->index == MSR_GS_BASE ||
  7742. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  7743. nested_vmx_msr_check_common(vcpu, e))
  7744. return -EINVAL;
  7745. return 0;
  7746. }
  7747. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  7748. struct vmx_msr_entry *e)
  7749. {
  7750. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  7751. nested_vmx_msr_check_common(vcpu, e))
  7752. return -EINVAL;
  7753. return 0;
  7754. }
  7755. /*
  7756. * Load guest's/host's msr at nested entry/exit.
  7757. * return 0 for success, entry index for failure.
  7758. */
  7759. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  7760. {
  7761. u32 i;
  7762. struct vmx_msr_entry e;
  7763. struct msr_data msr;
  7764. msr.host_initiated = false;
  7765. for (i = 0; i < count; i++) {
  7766. if (kvm_read_guest(vcpu->kvm, gpa + i * sizeof(e),
  7767. &e, sizeof(e))) {
  7768. pr_warn_ratelimited(
  7769. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  7770. __func__, i, gpa + i * sizeof(e));
  7771. goto fail;
  7772. }
  7773. if (nested_vmx_load_msr_check(vcpu, &e)) {
  7774. pr_warn_ratelimited(
  7775. "%s check failed (%u, 0x%x, 0x%x)\n",
  7776. __func__, i, e.index, e.reserved);
  7777. goto fail;
  7778. }
  7779. msr.index = e.index;
  7780. msr.data = e.value;
  7781. if (kvm_set_msr(vcpu, &msr)) {
  7782. pr_warn_ratelimited(
  7783. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  7784. __func__, i, e.index, e.value);
  7785. goto fail;
  7786. }
  7787. }
  7788. return 0;
  7789. fail:
  7790. return i + 1;
  7791. }
  7792. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  7793. {
  7794. u32 i;
  7795. struct vmx_msr_entry e;
  7796. for (i = 0; i < count; i++) {
  7797. if (kvm_read_guest(vcpu->kvm,
  7798. gpa + i * sizeof(e),
  7799. &e, 2 * sizeof(u32))) {
  7800. pr_warn_ratelimited(
  7801. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  7802. __func__, i, gpa + i * sizeof(e));
  7803. return -EINVAL;
  7804. }
  7805. if (nested_vmx_store_msr_check(vcpu, &e)) {
  7806. pr_warn_ratelimited(
  7807. "%s check failed (%u, 0x%x, 0x%x)\n",
  7808. __func__, i, e.index, e.reserved);
  7809. return -EINVAL;
  7810. }
  7811. if (kvm_get_msr(vcpu, e.index, &e.value)) {
  7812. pr_warn_ratelimited(
  7813. "%s cannot read MSR (%u, 0x%x)\n",
  7814. __func__, i, e.index);
  7815. return -EINVAL;
  7816. }
  7817. if (kvm_write_guest(vcpu->kvm,
  7818. gpa + i * sizeof(e) +
  7819. offsetof(struct vmx_msr_entry, value),
  7820. &e.value, sizeof(e.value))) {
  7821. pr_warn_ratelimited(
  7822. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  7823. __func__, i, e.index, e.value);
  7824. return -EINVAL;
  7825. }
  7826. }
  7827. return 0;
  7828. }
  7829. /*
  7830. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  7831. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  7832. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  7833. * guest in a way that will both be appropriate to L1's requests, and our
  7834. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  7835. * function also has additional necessary side-effects, like setting various
  7836. * vcpu->arch fields.
  7837. */
  7838. static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  7839. {
  7840. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7841. u32 exec_control;
  7842. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  7843. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  7844. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  7845. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  7846. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  7847. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  7848. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  7849. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  7850. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  7851. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  7852. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  7853. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  7854. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  7855. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  7856. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  7857. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  7858. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  7859. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  7860. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  7861. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  7862. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  7863. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  7864. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  7865. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  7866. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  7867. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  7868. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  7869. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  7870. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  7871. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  7872. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  7873. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  7874. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  7875. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  7876. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  7877. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  7878. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  7879. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  7880. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  7881. } else {
  7882. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  7883. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  7884. }
  7885. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  7886. vmcs12->vm_entry_intr_info_field);
  7887. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  7888. vmcs12->vm_entry_exception_error_code);
  7889. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  7890. vmcs12->vm_entry_instruction_len);
  7891. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  7892. vmcs12->guest_interruptibility_info);
  7893. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  7894. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  7895. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  7896. vmcs12->guest_pending_dbg_exceptions);
  7897. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  7898. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  7899. if (nested_cpu_has_xsaves(vmcs12))
  7900. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  7901. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7902. exec_control = vmcs12->pin_based_vm_exec_control;
  7903. exec_control |= vmcs_config.pin_based_exec_ctrl;
  7904. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  7905. if (nested_cpu_has_posted_intr(vmcs12)) {
  7906. /*
  7907. * Note that we use L0's vector here and in
  7908. * vmx_deliver_nested_posted_interrupt.
  7909. */
  7910. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  7911. vmx->nested.pi_pending = false;
  7912. vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  7913. vmcs_write64(POSTED_INTR_DESC_ADDR,
  7914. page_to_phys(vmx->nested.pi_desc_page) +
  7915. (unsigned long)(vmcs12->posted_intr_desc_addr &
  7916. (PAGE_SIZE - 1)));
  7917. } else
  7918. exec_control &= ~PIN_BASED_POSTED_INTR;
  7919. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  7920. vmx->nested.preemption_timer_expired = false;
  7921. if (nested_cpu_has_preemption_timer(vmcs12))
  7922. vmx_start_preemption_timer(vcpu);
  7923. /*
  7924. * Whether page-faults are trapped is determined by a combination of
  7925. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  7926. * If enable_ept, L0 doesn't care about page faults and we should
  7927. * set all of these to L1's desires. However, if !enable_ept, L0 does
  7928. * care about (at least some) page faults, and because it is not easy
  7929. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  7930. * to exit on each and every L2 page fault. This is done by setting
  7931. * MASK=MATCH=0 and (see below) EB.PF=1.
  7932. * Note that below we don't need special code to set EB.PF beyond the
  7933. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  7934. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  7935. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  7936. *
  7937. * A problem with this approach (when !enable_ept) is that L1 may be
  7938. * injected with more page faults than it asked for. This could have
  7939. * caused problems, but in practice existing hypervisors don't care.
  7940. * To fix this, we will need to emulate the PFEC checking (on the L1
  7941. * page tables), using walk_addr(), when injecting PFs to L1.
  7942. */
  7943. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  7944. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  7945. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  7946. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  7947. if (cpu_has_secondary_exec_ctrls()) {
  7948. exec_control = vmx_secondary_exec_control(vmx);
  7949. if (!vmx->rdtscp_enabled)
  7950. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  7951. /* Take the following fields only from vmcs12 */
  7952. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  7953. SECONDARY_EXEC_RDTSCP |
  7954. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  7955. SECONDARY_EXEC_APIC_REGISTER_VIRT);
  7956. if (nested_cpu_has(vmcs12,
  7957. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
  7958. exec_control |= vmcs12->secondary_vm_exec_control;
  7959. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
  7960. /*
  7961. * If translation failed, no matter: This feature asks
  7962. * to exit when accessing the given address, and if it
  7963. * can never be accessed, this feature won't do
  7964. * anything anyway.
  7965. */
  7966. if (!vmx->nested.apic_access_page)
  7967. exec_control &=
  7968. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7969. else
  7970. vmcs_write64(APIC_ACCESS_ADDR,
  7971. page_to_phys(vmx->nested.apic_access_page));
  7972. } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
  7973. (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))) {
  7974. exec_control |=
  7975. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7976. kvm_vcpu_reload_apic_access_page(vcpu);
  7977. }
  7978. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
  7979. vmcs_write64(EOI_EXIT_BITMAP0,
  7980. vmcs12->eoi_exit_bitmap0);
  7981. vmcs_write64(EOI_EXIT_BITMAP1,
  7982. vmcs12->eoi_exit_bitmap1);
  7983. vmcs_write64(EOI_EXIT_BITMAP2,
  7984. vmcs12->eoi_exit_bitmap2);
  7985. vmcs_write64(EOI_EXIT_BITMAP3,
  7986. vmcs12->eoi_exit_bitmap3);
  7987. vmcs_write16(GUEST_INTR_STATUS,
  7988. vmcs12->guest_intr_status);
  7989. }
  7990. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  7991. }
  7992. /*
  7993. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  7994. * Some constant fields are set here by vmx_set_constant_host_state().
  7995. * Other fields are different per CPU, and will be set later when
  7996. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  7997. */
  7998. vmx_set_constant_host_state(vmx);
  7999. /*
  8000. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  8001. * entry, but only if the current (host) sp changed from the value
  8002. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  8003. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  8004. * here we just force the write to happen on entry.
  8005. */
  8006. vmx->host_rsp = 0;
  8007. exec_control = vmx_exec_control(vmx); /* L0's desires */
  8008. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  8009. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  8010. exec_control &= ~CPU_BASED_TPR_SHADOW;
  8011. exec_control |= vmcs12->cpu_based_vm_exec_control;
  8012. if (exec_control & CPU_BASED_TPR_SHADOW) {
  8013. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  8014. page_to_phys(vmx->nested.virtual_apic_page));
  8015. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  8016. }
  8017. if (cpu_has_vmx_msr_bitmap() &&
  8018. exec_control & CPU_BASED_USE_MSR_BITMAPS) {
  8019. nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
  8020. /* MSR_BITMAP will be set by following vmx_set_efer. */
  8021. } else
  8022. exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
  8023. /*
  8024. * Merging of IO bitmap not currently supported.
  8025. * Rather, exit every time.
  8026. */
  8027. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  8028. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  8029. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  8030. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  8031. * bitwise-or of what L1 wants to trap for L2, and what we want to
  8032. * trap. Note that CR0.TS also needs updating - we do this later.
  8033. */
  8034. update_exception_bitmap(vcpu);
  8035. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  8036. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  8037. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  8038. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  8039. * bits are further modified by vmx_set_efer() below.
  8040. */
  8041. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  8042. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  8043. * emulated by vmx_set_efer(), below.
  8044. */
  8045. vm_entry_controls_init(vmx,
  8046. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  8047. ~VM_ENTRY_IA32E_MODE) |
  8048. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  8049. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
  8050. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  8051. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  8052. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  8053. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  8054. set_cr4_guest_host_mask(vmx);
  8055. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
  8056. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  8057. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  8058. vmcs_write64(TSC_OFFSET,
  8059. vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
  8060. else
  8061. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  8062. if (enable_vpid) {
  8063. /*
  8064. * Trivially support vpid by letting L2s share their parent
  8065. * L1's vpid. TODO: move to a more elaborate solution, giving
  8066. * each L2 its own vpid and exposing the vpid feature to L1.
  8067. */
  8068. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  8069. vmx_flush_tlb(vcpu);
  8070. }
  8071. if (nested_cpu_has_ept(vmcs12)) {
  8072. kvm_mmu_unload(vcpu);
  8073. nested_ept_init_mmu_context(vcpu);
  8074. }
  8075. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
  8076. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  8077. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  8078. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  8079. else
  8080. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  8081. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  8082. vmx_set_efer(vcpu, vcpu->arch.efer);
  8083. /*
  8084. * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
  8085. * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
  8086. * The CR0_READ_SHADOW is what L2 should have expected to read given
  8087. * the specifications by L1; It's not enough to take
  8088. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  8089. * have more bits than L1 expected.
  8090. */
  8091. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  8092. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  8093. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  8094. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  8095. /* shadow page tables on either EPT or shadow page tables */
  8096. kvm_set_cr3(vcpu, vmcs12->guest_cr3);
  8097. kvm_mmu_reset_context(vcpu);
  8098. if (!enable_ept)
  8099. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  8100. /*
  8101. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  8102. */
  8103. if (enable_ept) {
  8104. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  8105. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  8106. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  8107. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  8108. }
  8109. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  8110. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  8111. }
  8112. /*
  8113. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  8114. * for running an L2 nested guest.
  8115. */
  8116. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  8117. {
  8118. struct vmcs12 *vmcs12;
  8119. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8120. int cpu;
  8121. struct loaded_vmcs *vmcs02;
  8122. bool ia32e;
  8123. u32 msr_entry_idx;
  8124. if (!nested_vmx_check_permission(vcpu) ||
  8125. !nested_vmx_check_vmcs12(vcpu))
  8126. return 1;
  8127. skip_emulated_instruction(vcpu);
  8128. vmcs12 = get_vmcs12(vcpu);
  8129. if (enable_shadow_vmcs)
  8130. copy_shadow_to_vmcs12(vmx);
  8131. /*
  8132. * The nested entry process starts with enforcing various prerequisites
  8133. * on vmcs12 as required by the Intel SDM, and act appropriately when
  8134. * they fail: As the SDM explains, some conditions should cause the
  8135. * instruction to fail, while others will cause the instruction to seem
  8136. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  8137. * To speed up the normal (success) code path, we should avoid checking
  8138. * for misconfigurations which will anyway be caught by the processor
  8139. * when using the merged vmcs02.
  8140. */
  8141. if (vmcs12->launch_state == launch) {
  8142. nested_vmx_failValid(vcpu,
  8143. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  8144. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  8145. return 1;
  8146. }
  8147. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  8148. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
  8149. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8150. return 1;
  8151. }
  8152. if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
  8153. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8154. return 1;
  8155. }
  8156. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
  8157. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8158. return 1;
  8159. }
  8160. if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
  8161. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8162. return 1;
  8163. }
  8164. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
  8165. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8166. return 1;
  8167. }
  8168. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  8169. vmx->nested.nested_vmx_true_procbased_ctls_low,
  8170. vmx->nested.nested_vmx_procbased_ctls_high) ||
  8171. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  8172. vmx->nested.nested_vmx_secondary_ctls_low,
  8173. vmx->nested.nested_vmx_secondary_ctls_high) ||
  8174. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  8175. vmx->nested.nested_vmx_pinbased_ctls_low,
  8176. vmx->nested.nested_vmx_pinbased_ctls_high) ||
  8177. !vmx_control_verify(vmcs12->vm_exit_controls,
  8178. vmx->nested.nested_vmx_true_exit_ctls_low,
  8179. vmx->nested.nested_vmx_exit_ctls_high) ||
  8180. !vmx_control_verify(vmcs12->vm_entry_controls,
  8181. vmx->nested.nested_vmx_true_entry_ctls_low,
  8182. vmx->nested.nested_vmx_entry_ctls_high))
  8183. {
  8184. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8185. return 1;
  8186. }
  8187. if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  8188. ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  8189. nested_vmx_failValid(vcpu,
  8190. VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
  8191. return 1;
  8192. }
  8193. if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  8194. ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  8195. nested_vmx_entry_failure(vcpu, vmcs12,
  8196. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8197. return 1;
  8198. }
  8199. if (vmcs12->vmcs_link_pointer != -1ull) {
  8200. nested_vmx_entry_failure(vcpu, vmcs12,
  8201. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
  8202. return 1;
  8203. }
  8204. /*
  8205. * If the load IA32_EFER VM-entry control is 1, the following checks
  8206. * are performed on the field for the IA32_EFER MSR:
  8207. * - Bits reserved in the IA32_EFER MSR must be 0.
  8208. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  8209. * the IA-32e mode guest VM-exit control. It must also be identical
  8210. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  8211. * CR0.PG) is 1.
  8212. */
  8213. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
  8214. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  8215. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  8216. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  8217. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  8218. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
  8219. nested_vmx_entry_failure(vcpu, vmcs12,
  8220. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8221. return 1;
  8222. }
  8223. }
  8224. /*
  8225. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  8226. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  8227. * the values of the LMA and LME bits in the field must each be that of
  8228. * the host address-space size VM-exit control.
  8229. */
  8230. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  8231. ia32e = (vmcs12->vm_exit_controls &
  8232. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  8233. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  8234. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  8235. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
  8236. nested_vmx_entry_failure(vcpu, vmcs12,
  8237. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8238. return 1;
  8239. }
  8240. }
  8241. /*
  8242. * We're finally done with prerequisite checking, and can start with
  8243. * the nested entry.
  8244. */
  8245. vmcs02 = nested_get_current_vmcs02(vmx);
  8246. if (!vmcs02)
  8247. return -ENOMEM;
  8248. enter_guest_mode(vcpu);
  8249. vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
  8250. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  8251. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  8252. cpu = get_cpu();
  8253. vmx->loaded_vmcs = vmcs02;
  8254. vmx_vcpu_put(vcpu);
  8255. vmx_vcpu_load(vcpu, cpu);
  8256. vcpu->cpu = cpu;
  8257. put_cpu();
  8258. vmx_segment_cache_clear(vmx);
  8259. prepare_vmcs02(vcpu, vmcs12);
  8260. msr_entry_idx = nested_vmx_load_msr(vcpu,
  8261. vmcs12->vm_entry_msr_load_addr,
  8262. vmcs12->vm_entry_msr_load_count);
  8263. if (msr_entry_idx) {
  8264. leave_guest_mode(vcpu);
  8265. vmx_load_vmcs01(vcpu);
  8266. nested_vmx_entry_failure(vcpu, vmcs12,
  8267. EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
  8268. return 1;
  8269. }
  8270. vmcs12->launch_state = 1;
  8271. if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
  8272. return kvm_vcpu_halt(vcpu);
  8273. vmx->nested.nested_run_pending = 1;
  8274. /*
  8275. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  8276. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  8277. * returned as far as L1 is concerned. It will only return (and set
  8278. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  8279. */
  8280. return 1;
  8281. }
  8282. /*
  8283. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  8284. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  8285. * This function returns the new value we should put in vmcs12.guest_cr0.
  8286. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  8287. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  8288. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  8289. * didn't trap the bit, because if L1 did, so would L0).
  8290. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  8291. * been modified by L2, and L1 knows it. So just leave the old value of
  8292. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  8293. * isn't relevant, because if L0 traps this bit it can set it to anything.
  8294. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  8295. * changed these bits, and therefore they need to be updated, but L0
  8296. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  8297. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  8298. */
  8299. static inline unsigned long
  8300. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  8301. {
  8302. return
  8303. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  8304. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  8305. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  8306. vcpu->arch.cr0_guest_owned_bits));
  8307. }
  8308. static inline unsigned long
  8309. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  8310. {
  8311. return
  8312. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  8313. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  8314. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  8315. vcpu->arch.cr4_guest_owned_bits));
  8316. }
  8317. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  8318. struct vmcs12 *vmcs12)
  8319. {
  8320. u32 idt_vectoring;
  8321. unsigned int nr;
  8322. if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
  8323. nr = vcpu->arch.exception.nr;
  8324. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  8325. if (kvm_exception_is_soft(nr)) {
  8326. vmcs12->vm_exit_instruction_len =
  8327. vcpu->arch.event_exit_inst_len;
  8328. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  8329. } else
  8330. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  8331. if (vcpu->arch.exception.has_error_code) {
  8332. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  8333. vmcs12->idt_vectoring_error_code =
  8334. vcpu->arch.exception.error_code;
  8335. }
  8336. vmcs12->idt_vectoring_info_field = idt_vectoring;
  8337. } else if (vcpu->arch.nmi_injected) {
  8338. vmcs12->idt_vectoring_info_field =
  8339. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  8340. } else if (vcpu->arch.interrupt.pending) {
  8341. nr = vcpu->arch.interrupt.nr;
  8342. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  8343. if (vcpu->arch.interrupt.soft) {
  8344. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  8345. vmcs12->vm_entry_instruction_len =
  8346. vcpu->arch.event_exit_inst_len;
  8347. } else
  8348. idt_vectoring |= INTR_TYPE_EXT_INTR;
  8349. vmcs12->idt_vectoring_info_field = idt_vectoring;
  8350. }
  8351. }
  8352. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  8353. {
  8354. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8355. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  8356. vmx->nested.preemption_timer_expired) {
  8357. if (vmx->nested.nested_run_pending)
  8358. return -EBUSY;
  8359. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  8360. return 0;
  8361. }
  8362. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  8363. if (vmx->nested.nested_run_pending ||
  8364. vcpu->arch.interrupt.pending)
  8365. return -EBUSY;
  8366. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  8367. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  8368. INTR_INFO_VALID_MASK, 0);
  8369. /*
  8370. * The NMI-triggered VM exit counts as injection:
  8371. * clear this one and block further NMIs.
  8372. */
  8373. vcpu->arch.nmi_pending = 0;
  8374. vmx_set_nmi_mask(vcpu, true);
  8375. return 0;
  8376. }
  8377. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  8378. nested_exit_on_intr(vcpu)) {
  8379. if (vmx->nested.nested_run_pending)
  8380. return -EBUSY;
  8381. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  8382. return 0;
  8383. }
  8384. return vmx_complete_nested_posted_interrupt(vcpu);
  8385. }
  8386. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  8387. {
  8388. ktime_t remaining =
  8389. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  8390. u64 value;
  8391. if (ktime_to_ns(remaining) <= 0)
  8392. return 0;
  8393. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  8394. do_div(value, 1000000);
  8395. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  8396. }
  8397. /*
  8398. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  8399. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  8400. * and this function updates it to reflect the changes to the guest state while
  8401. * L2 was running (and perhaps made some exits which were handled directly by L0
  8402. * without going back to L1), and to reflect the exit reason.
  8403. * Note that we do not have to copy here all VMCS fields, just those that
  8404. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  8405. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  8406. * which already writes to vmcs12 directly.
  8407. */
  8408. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  8409. u32 exit_reason, u32 exit_intr_info,
  8410. unsigned long exit_qualification)
  8411. {
  8412. /* update guest state fields: */
  8413. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  8414. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  8415. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  8416. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  8417. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  8418. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  8419. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  8420. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  8421. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  8422. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  8423. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  8424. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  8425. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  8426. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  8427. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  8428. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  8429. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  8430. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  8431. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  8432. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  8433. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  8434. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  8435. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  8436. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  8437. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  8438. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  8439. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  8440. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  8441. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  8442. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  8443. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  8444. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  8445. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  8446. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  8447. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  8448. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  8449. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  8450. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  8451. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  8452. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  8453. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  8454. vmcs12->guest_interruptibility_info =
  8455. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  8456. vmcs12->guest_pending_dbg_exceptions =
  8457. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  8458. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  8459. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  8460. else
  8461. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  8462. if (nested_cpu_has_preemption_timer(vmcs12)) {
  8463. if (vmcs12->vm_exit_controls &
  8464. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  8465. vmcs12->vmx_preemption_timer_value =
  8466. vmx_get_preemption_timer_value(vcpu);
  8467. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  8468. }
  8469. /*
  8470. * In some cases (usually, nested EPT), L2 is allowed to change its
  8471. * own CR3 without exiting. If it has changed it, we must keep it.
  8472. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  8473. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  8474. *
  8475. * Additionally, restore L2's PDPTR to vmcs12.
  8476. */
  8477. if (enable_ept) {
  8478. vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
  8479. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  8480. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  8481. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  8482. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  8483. }
  8484. if (nested_cpu_has_vid(vmcs12))
  8485. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  8486. vmcs12->vm_entry_controls =
  8487. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  8488. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  8489. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  8490. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  8491. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  8492. }
  8493. /* TODO: These cannot have changed unless we have MSR bitmaps and
  8494. * the relevant bit asks not to trap the change */
  8495. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  8496. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  8497. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  8498. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  8499. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  8500. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  8501. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  8502. if (vmx_mpx_supported())
  8503. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  8504. if (nested_cpu_has_xsaves(vmcs12))
  8505. vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
  8506. /* update exit information fields: */
  8507. vmcs12->vm_exit_reason = exit_reason;
  8508. vmcs12->exit_qualification = exit_qualification;
  8509. vmcs12->vm_exit_intr_info = exit_intr_info;
  8510. if ((vmcs12->vm_exit_intr_info &
  8511. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8512. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
  8513. vmcs12->vm_exit_intr_error_code =
  8514. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8515. vmcs12->idt_vectoring_info_field = 0;
  8516. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  8517. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8518. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  8519. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  8520. * instead of reading the real value. */
  8521. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  8522. /*
  8523. * Transfer the event that L0 or L1 may wanted to inject into
  8524. * L2 to IDT_VECTORING_INFO_FIELD.
  8525. */
  8526. vmcs12_save_pending_event(vcpu, vmcs12);
  8527. }
  8528. /*
  8529. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  8530. * preserved above and would only end up incorrectly in L1.
  8531. */
  8532. vcpu->arch.nmi_injected = false;
  8533. kvm_clear_exception_queue(vcpu);
  8534. kvm_clear_interrupt_queue(vcpu);
  8535. }
  8536. /*
  8537. * A part of what we need to when the nested L2 guest exits and we want to
  8538. * run its L1 parent, is to reset L1's guest state to the host state specified
  8539. * in vmcs12.
  8540. * This function is to be called not only on normal nested exit, but also on
  8541. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  8542. * Failures During or After Loading Guest State").
  8543. * This function should be called when the active VMCS is L1's (vmcs01).
  8544. */
  8545. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  8546. struct vmcs12 *vmcs12)
  8547. {
  8548. struct kvm_segment seg;
  8549. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  8550. vcpu->arch.efer = vmcs12->host_ia32_efer;
  8551. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  8552. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  8553. else
  8554. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  8555. vmx_set_efer(vcpu, vcpu->arch.efer);
  8556. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  8557. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  8558. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  8559. /*
  8560. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  8561. * actually changed, because it depends on the current state of
  8562. * fpu_active (which may have changed).
  8563. * Note that vmx_set_cr0 refers to efer set above.
  8564. */
  8565. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  8566. /*
  8567. * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
  8568. * to apply the same changes to L1's vmcs. We just set cr0 correctly,
  8569. * but we also need to update cr0_guest_host_mask and exception_bitmap.
  8570. */
  8571. update_exception_bitmap(vcpu);
  8572. vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
  8573. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  8574. /*
  8575. * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
  8576. * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
  8577. */
  8578. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  8579. kvm_set_cr4(vcpu, vmcs12->host_cr4);
  8580. nested_ept_uninit_mmu_context(vcpu);
  8581. kvm_set_cr3(vcpu, vmcs12->host_cr3);
  8582. kvm_mmu_reset_context(vcpu);
  8583. if (!enable_ept)
  8584. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  8585. if (enable_vpid) {
  8586. /*
  8587. * Trivially support vpid by letting L2s share their parent
  8588. * L1's vpid. TODO: move to a more elaborate solution, giving
  8589. * each L2 its own vpid and exposing the vpid feature to L1.
  8590. */
  8591. vmx_flush_tlb(vcpu);
  8592. }
  8593. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  8594. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  8595. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  8596. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  8597. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  8598. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  8599. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  8600. vmcs_write64(GUEST_BNDCFGS, 0);
  8601. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  8602. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  8603. vcpu->arch.pat = vmcs12->host_ia32_pat;
  8604. }
  8605. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8606. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  8607. vmcs12->host_ia32_perf_global_ctrl);
  8608. /* Set L1 segment info according to Intel SDM
  8609. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  8610. seg = (struct kvm_segment) {
  8611. .base = 0,
  8612. .limit = 0xFFFFFFFF,
  8613. .selector = vmcs12->host_cs_selector,
  8614. .type = 11,
  8615. .present = 1,
  8616. .s = 1,
  8617. .g = 1
  8618. };
  8619. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  8620. seg.l = 1;
  8621. else
  8622. seg.db = 1;
  8623. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  8624. seg = (struct kvm_segment) {
  8625. .base = 0,
  8626. .limit = 0xFFFFFFFF,
  8627. .type = 3,
  8628. .present = 1,
  8629. .s = 1,
  8630. .db = 1,
  8631. .g = 1
  8632. };
  8633. seg.selector = vmcs12->host_ds_selector;
  8634. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  8635. seg.selector = vmcs12->host_es_selector;
  8636. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  8637. seg.selector = vmcs12->host_ss_selector;
  8638. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  8639. seg.selector = vmcs12->host_fs_selector;
  8640. seg.base = vmcs12->host_fs_base;
  8641. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  8642. seg.selector = vmcs12->host_gs_selector;
  8643. seg.base = vmcs12->host_gs_base;
  8644. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  8645. seg = (struct kvm_segment) {
  8646. .base = vmcs12->host_tr_base,
  8647. .limit = 0x67,
  8648. .selector = vmcs12->host_tr_selector,
  8649. .type = 11,
  8650. .present = 1
  8651. };
  8652. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  8653. kvm_set_dr(vcpu, 7, 0x400);
  8654. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  8655. if (cpu_has_vmx_msr_bitmap())
  8656. vmx_set_msr_bitmap(vcpu);
  8657. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  8658. vmcs12->vm_exit_msr_load_count))
  8659. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  8660. }
  8661. /*
  8662. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  8663. * and modify vmcs12 to make it see what it would expect to see there if
  8664. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  8665. */
  8666. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  8667. u32 exit_intr_info,
  8668. unsigned long exit_qualification)
  8669. {
  8670. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8671. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8672. /* trying to cancel vmlaunch/vmresume is a bug */
  8673. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  8674. leave_guest_mode(vcpu);
  8675. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  8676. exit_qualification);
  8677. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  8678. vmcs12->vm_exit_msr_store_count))
  8679. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  8680. vmx_load_vmcs01(vcpu);
  8681. if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
  8682. && nested_exit_intr_ack_set(vcpu)) {
  8683. int irq = kvm_cpu_get_interrupt(vcpu);
  8684. WARN_ON(irq < 0);
  8685. vmcs12->vm_exit_intr_info = irq |
  8686. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  8687. }
  8688. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  8689. vmcs12->exit_qualification,
  8690. vmcs12->idt_vectoring_info_field,
  8691. vmcs12->vm_exit_intr_info,
  8692. vmcs12->vm_exit_intr_error_code,
  8693. KVM_ISA_VMX);
  8694. vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
  8695. vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
  8696. vmx_segment_cache_clear(vmx);
  8697. /* if no vmcs02 cache requested, remove the one we used */
  8698. if (VMCS02_POOL_SIZE == 0)
  8699. nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
  8700. load_vmcs12_host_state(vcpu, vmcs12);
  8701. /* Update TSC_OFFSET if TSC was changed while L2 ran */
  8702. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  8703. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  8704. vmx->host_rsp = 0;
  8705. /* Unpin physical memory we referred to in vmcs02 */
  8706. if (vmx->nested.apic_access_page) {
  8707. nested_release_page(vmx->nested.apic_access_page);
  8708. vmx->nested.apic_access_page = NULL;
  8709. }
  8710. if (vmx->nested.virtual_apic_page) {
  8711. nested_release_page(vmx->nested.virtual_apic_page);
  8712. vmx->nested.virtual_apic_page = NULL;
  8713. }
  8714. if (vmx->nested.pi_desc_page) {
  8715. kunmap(vmx->nested.pi_desc_page);
  8716. nested_release_page(vmx->nested.pi_desc_page);
  8717. vmx->nested.pi_desc_page = NULL;
  8718. vmx->nested.pi_desc = NULL;
  8719. }
  8720. /*
  8721. * We are now running in L2, mmu_notifier will force to reload the
  8722. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  8723. */
  8724. kvm_vcpu_reload_apic_access_page(vcpu);
  8725. /*
  8726. * Exiting from L2 to L1, we're now back to L1 which thinks it just
  8727. * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
  8728. * success or failure flag accordingly.
  8729. */
  8730. if (unlikely(vmx->fail)) {
  8731. vmx->fail = 0;
  8732. nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
  8733. } else
  8734. nested_vmx_succeed(vcpu);
  8735. if (enable_shadow_vmcs)
  8736. vmx->nested.sync_shadow_vmcs = true;
  8737. /* in case we halted in L2 */
  8738. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  8739. }
  8740. /*
  8741. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  8742. */
  8743. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  8744. {
  8745. if (is_guest_mode(vcpu))
  8746. nested_vmx_vmexit(vcpu, -1, 0, 0);
  8747. free_nested(to_vmx(vcpu));
  8748. }
  8749. /*
  8750. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  8751. * 23.7 "VM-entry failures during or after loading guest state" (this also
  8752. * lists the acceptable exit-reason and exit-qualification parameters).
  8753. * It should only be called before L2 actually succeeded to run, and when
  8754. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  8755. */
  8756. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  8757. struct vmcs12 *vmcs12,
  8758. u32 reason, unsigned long qualification)
  8759. {
  8760. load_vmcs12_host_state(vcpu, vmcs12);
  8761. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  8762. vmcs12->exit_qualification = qualification;
  8763. nested_vmx_succeed(vcpu);
  8764. if (enable_shadow_vmcs)
  8765. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  8766. }
  8767. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  8768. struct x86_instruction_info *info,
  8769. enum x86_intercept_stage stage)
  8770. {
  8771. return X86EMUL_CONTINUE;
  8772. }
  8773. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  8774. {
  8775. if (ple_gap)
  8776. shrink_ple_window(vcpu);
  8777. }
  8778. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  8779. struct kvm_memory_slot *slot)
  8780. {
  8781. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  8782. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  8783. }
  8784. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  8785. struct kvm_memory_slot *slot)
  8786. {
  8787. kvm_mmu_slot_set_dirty(kvm, slot);
  8788. }
  8789. static void vmx_flush_log_dirty(struct kvm *kvm)
  8790. {
  8791. kvm_flush_pml_buffers(kvm);
  8792. }
  8793. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  8794. struct kvm_memory_slot *memslot,
  8795. gfn_t offset, unsigned long mask)
  8796. {
  8797. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  8798. }
  8799. static struct kvm_x86_ops vmx_x86_ops = {
  8800. .cpu_has_kvm_support = cpu_has_kvm_support,
  8801. .disabled_by_bios = vmx_disabled_by_bios,
  8802. .hardware_setup = hardware_setup,
  8803. .hardware_unsetup = hardware_unsetup,
  8804. .check_processor_compatibility = vmx_check_processor_compat,
  8805. .hardware_enable = hardware_enable,
  8806. .hardware_disable = hardware_disable,
  8807. .cpu_has_accelerated_tpr = report_flexpriority,
  8808. .vcpu_create = vmx_create_vcpu,
  8809. .vcpu_free = vmx_free_vcpu,
  8810. .vcpu_reset = vmx_vcpu_reset,
  8811. .prepare_guest_switch = vmx_save_host_state,
  8812. .vcpu_load = vmx_vcpu_load,
  8813. .vcpu_put = vmx_vcpu_put,
  8814. .update_db_bp_intercept = update_exception_bitmap,
  8815. .get_msr = vmx_get_msr,
  8816. .set_msr = vmx_set_msr,
  8817. .get_segment_base = vmx_get_segment_base,
  8818. .get_segment = vmx_get_segment,
  8819. .set_segment = vmx_set_segment,
  8820. .get_cpl = vmx_get_cpl,
  8821. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  8822. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  8823. .decache_cr3 = vmx_decache_cr3,
  8824. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  8825. .set_cr0 = vmx_set_cr0,
  8826. .set_cr3 = vmx_set_cr3,
  8827. .set_cr4 = vmx_set_cr4,
  8828. .set_efer = vmx_set_efer,
  8829. .get_idt = vmx_get_idt,
  8830. .set_idt = vmx_set_idt,
  8831. .get_gdt = vmx_get_gdt,
  8832. .set_gdt = vmx_set_gdt,
  8833. .get_dr6 = vmx_get_dr6,
  8834. .set_dr6 = vmx_set_dr6,
  8835. .set_dr7 = vmx_set_dr7,
  8836. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  8837. .cache_reg = vmx_cache_reg,
  8838. .get_rflags = vmx_get_rflags,
  8839. .set_rflags = vmx_set_rflags,
  8840. .fpu_activate = vmx_fpu_activate,
  8841. .fpu_deactivate = vmx_fpu_deactivate,
  8842. .tlb_flush = vmx_flush_tlb,
  8843. .run = vmx_vcpu_run,
  8844. .handle_exit = vmx_handle_exit,
  8845. .skip_emulated_instruction = skip_emulated_instruction,
  8846. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  8847. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  8848. .patch_hypercall = vmx_patch_hypercall,
  8849. .set_irq = vmx_inject_irq,
  8850. .set_nmi = vmx_inject_nmi,
  8851. .queue_exception = vmx_queue_exception,
  8852. .cancel_injection = vmx_cancel_injection,
  8853. .interrupt_allowed = vmx_interrupt_allowed,
  8854. .nmi_allowed = vmx_nmi_allowed,
  8855. .get_nmi_mask = vmx_get_nmi_mask,
  8856. .set_nmi_mask = vmx_set_nmi_mask,
  8857. .enable_nmi_window = enable_nmi_window,
  8858. .enable_irq_window = enable_irq_window,
  8859. .update_cr8_intercept = update_cr8_intercept,
  8860. .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
  8861. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  8862. .vm_has_apicv = vmx_vm_has_apicv,
  8863. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  8864. .hwapic_irr_update = vmx_hwapic_irr_update,
  8865. .hwapic_isr_update = vmx_hwapic_isr_update,
  8866. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  8867. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  8868. .set_tss_addr = vmx_set_tss_addr,
  8869. .get_tdp_level = get_ept_level,
  8870. .get_mt_mask = vmx_get_mt_mask,
  8871. .get_exit_info = vmx_get_exit_info,
  8872. .get_lpage_level = vmx_get_lpage_level,
  8873. .cpuid_update = vmx_cpuid_update,
  8874. .rdtscp_supported = vmx_rdtscp_supported,
  8875. .invpcid_supported = vmx_invpcid_supported,
  8876. .set_supported_cpuid = vmx_set_supported_cpuid,
  8877. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  8878. .set_tsc_khz = vmx_set_tsc_khz,
  8879. .read_tsc_offset = vmx_read_tsc_offset,
  8880. .write_tsc_offset = vmx_write_tsc_offset,
  8881. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  8882. .compute_tsc_offset = vmx_compute_tsc_offset,
  8883. .read_l1_tsc = vmx_read_l1_tsc,
  8884. .set_tdp_cr3 = vmx_set_cr3,
  8885. .check_intercept = vmx_check_intercept,
  8886. .handle_external_intr = vmx_handle_external_intr,
  8887. .mpx_supported = vmx_mpx_supported,
  8888. .xsaves_supported = vmx_xsaves_supported,
  8889. .check_nested_events = vmx_check_nested_events,
  8890. .sched_in = vmx_sched_in,
  8891. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  8892. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  8893. .flush_log_dirty = vmx_flush_log_dirty,
  8894. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  8895. };
  8896. static int __init vmx_init(void)
  8897. {
  8898. int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  8899. __alignof__(struct vcpu_vmx), THIS_MODULE);
  8900. if (r)
  8901. return r;
  8902. #ifdef CONFIG_KEXEC
  8903. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  8904. crash_vmclear_local_loaded_vmcss);
  8905. #endif
  8906. return 0;
  8907. }
  8908. static void __exit vmx_exit(void)
  8909. {
  8910. #ifdef CONFIG_KEXEC
  8911. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  8912. synchronize_rcu();
  8913. #endif
  8914. kvm_exit();
  8915. }
  8916. module_init(vmx_init)
  8917. module_exit(vmx_exit)