vmx.c 329 KB

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