vmx.c 256 KB

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