vmx.c 261 KB

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