vmx.c 263 KB

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