vmx.c 255 KB

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