vmx.c 306 KB

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