vmx.c 207 KB

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