svm.c 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #define pr_fmt(fmt) "SVM: " fmt
  18. #include <linux/kvm_host.h>
  19. #include "irq.h"
  20. #include "mmu.h"
  21. #include "kvm_cache_regs.h"
  22. #include "x86.h"
  23. #include "cpuid.h"
  24. #include "pmu.h"
  25. #include <linux/module.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/kernel.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/highmem.h>
  30. #include <linux/sched.h>
  31. #include <linux/trace_events.h>
  32. #include <linux/slab.h>
  33. #include <linux/amd-iommu.h>
  34. #include <linux/hashtable.h>
  35. #include <linux/frame.h>
  36. #include <linux/psp-sev.h>
  37. #include <linux/file.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/swap.h>
  40. #include <asm/apic.h>
  41. #include <asm/perf_event.h>
  42. #include <asm/tlbflush.h>
  43. #include <asm/desc.h>
  44. #include <asm/debugreg.h>
  45. #include <asm/kvm_para.h>
  46. #include <asm/irq_remapping.h>
  47. #include <asm/spec-ctrl.h>
  48. #include <asm/virtext.h>
  49. #include "trace.h"
  50. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  51. MODULE_AUTHOR("Qumranet");
  52. MODULE_LICENSE("GPL");
  53. static const struct x86_cpu_id svm_cpu_id[] = {
  54. X86_FEATURE_MATCH(X86_FEATURE_SVM),
  55. {}
  56. };
  57. MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
  58. #define IOPM_ALLOC_ORDER 2
  59. #define MSRPM_ALLOC_ORDER 1
  60. #define SEG_TYPE_LDT 2
  61. #define SEG_TYPE_BUSY_TSS16 3
  62. #define SVM_FEATURE_NPT (1 << 0)
  63. #define SVM_FEATURE_LBRV (1 << 1)
  64. #define SVM_FEATURE_SVML (1 << 2)
  65. #define SVM_FEATURE_NRIP (1 << 3)
  66. #define SVM_FEATURE_TSC_RATE (1 << 4)
  67. #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
  68. #define SVM_FEATURE_FLUSH_ASID (1 << 6)
  69. #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
  70. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  71. #define SVM_AVIC_DOORBELL 0xc001011b
  72. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  73. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  74. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  75. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  76. #define TSC_RATIO_RSVD 0xffffff0000000000ULL
  77. #define TSC_RATIO_MIN 0x0000000000000001ULL
  78. #define TSC_RATIO_MAX 0x000000ffffffffffULL
  79. #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
  80. /*
  81. * 0xff is broadcast, so the max index allowed for physical APIC ID
  82. * table is 0xfe. APIC IDs above 0xff are reserved.
  83. */
  84. #define AVIC_MAX_PHYSICAL_ID_COUNT 255
  85. #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
  86. #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
  87. #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
  88. /* AVIC GATAG is encoded using VM and VCPU IDs */
  89. #define AVIC_VCPU_ID_BITS 8
  90. #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
  91. #define AVIC_VM_ID_BITS 24
  92. #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
  93. #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
  94. #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
  95. (y & AVIC_VCPU_ID_MASK))
  96. #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
  97. #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
  98. static bool erratum_383_found __read_mostly;
  99. static const u32 host_save_user_msrs[] = {
  100. #ifdef CONFIG_X86_64
  101. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  102. MSR_FS_BASE,
  103. #endif
  104. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  105. MSR_TSC_AUX,
  106. };
  107. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  108. struct kvm_sev_info {
  109. bool active; /* SEV enabled guest */
  110. unsigned int asid; /* ASID used for this guest */
  111. unsigned int handle; /* SEV firmware handle */
  112. int fd; /* SEV device fd */
  113. unsigned long pages_locked; /* Number of pages locked */
  114. struct list_head regions_list; /* List of registered regions */
  115. };
  116. struct kvm_svm {
  117. struct kvm kvm;
  118. /* Struct members for AVIC */
  119. u32 avic_vm_id;
  120. u32 ldr_mode;
  121. struct page *avic_logical_id_table_page;
  122. struct page *avic_physical_id_table_page;
  123. struct hlist_node hnode;
  124. struct kvm_sev_info sev_info;
  125. };
  126. struct kvm_vcpu;
  127. struct nested_state {
  128. struct vmcb *hsave;
  129. u64 hsave_msr;
  130. u64 vm_cr_msr;
  131. u64 vmcb;
  132. /* These are the merged vectors */
  133. u32 *msrpm;
  134. /* gpa pointers to the real vectors */
  135. u64 vmcb_msrpm;
  136. u64 vmcb_iopm;
  137. /* A VMEXIT is required but not yet emulated */
  138. bool exit_required;
  139. /* cache for intercepts of the guest */
  140. u32 intercept_cr;
  141. u32 intercept_dr;
  142. u32 intercept_exceptions;
  143. u64 intercept;
  144. /* Nested Paging related state */
  145. u64 nested_cr3;
  146. };
  147. #define MSRPM_OFFSETS 16
  148. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  149. /*
  150. * Set osvw_len to higher value when updated Revision Guides
  151. * are published and we know what the new status bits are
  152. */
  153. static uint64_t osvw_len = 4, osvw_status;
  154. struct vcpu_svm {
  155. struct kvm_vcpu vcpu;
  156. struct vmcb *vmcb;
  157. unsigned long vmcb_pa;
  158. struct svm_cpu_data *svm_data;
  159. uint64_t asid_generation;
  160. uint64_t sysenter_esp;
  161. uint64_t sysenter_eip;
  162. uint64_t tsc_aux;
  163. u64 msr_decfg;
  164. u64 next_rip;
  165. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  166. struct {
  167. u16 fs;
  168. u16 gs;
  169. u16 ldt;
  170. u64 gs_base;
  171. } host;
  172. u64 spec_ctrl;
  173. /*
  174. * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
  175. * translated into the appropriate L2_CFG bits on the host to
  176. * perform speculative control.
  177. */
  178. u64 virt_spec_ctrl;
  179. u32 *msrpm;
  180. ulong nmi_iret_rip;
  181. struct nested_state nested;
  182. bool nmi_singlestep;
  183. u64 nmi_singlestep_guest_rflags;
  184. unsigned int3_injected;
  185. unsigned long int3_rip;
  186. /* cached guest cpuid flags for faster access */
  187. bool nrips_enabled : 1;
  188. u32 ldr_reg;
  189. struct page *avic_backing_page;
  190. u64 *avic_physical_id_cache;
  191. bool avic_is_running;
  192. /*
  193. * Per-vcpu list of struct amd_svm_iommu_ir:
  194. * This is used mainly to store interrupt remapping information used
  195. * when update the vcpu affinity. This avoids the need to scan for
  196. * IRTE and try to match ga_tag in the IOMMU driver.
  197. */
  198. struct list_head ir_list;
  199. spinlock_t ir_list_lock;
  200. /* which host CPU was used for running this vcpu */
  201. unsigned int last_cpu;
  202. };
  203. /*
  204. * This is a wrapper of struct amd_iommu_ir_data.
  205. */
  206. struct amd_svm_iommu_ir {
  207. struct list_head node; /* Used by SVM for per-vcpu ir_list */
  208. void *data; /* Storing pointer to struct amd_ir_data */
  209. };
  210. #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
  211. #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
  212. #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
  213. #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
  214. #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
  215. #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
  216. static DEFINE_PER_CPU(u64, current_tsc_ratio);
  217. #define TSC_RATIO_DEFAULT 0x0100000000ULL
  218. #define MSR_INVALID 0xffffffffU
  219. static const struct svm_direct_access_msrs {
  220. u32 index; /* Index of the MSR */
  221. bool always; /* True if intercept is always on */
  222. } direct_access_msrs[] = {
  223. { .index = MSR_STAR, .always = true },
  224. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  225. #ifdef CONFIG_X86_64
  226. { .index = MSR_GS_BASE, .always = true },
  227. { .index = MSR_FS_BASE, .always = true },
  228. { .index = MSR_KERNEL_GS_BASE, .always = true },
  229. { .index = MSR_LSTAR, .always = true },
  230. { .index = MSR_CSTAR, .always = true },
  231. { .index = MSR_SYSCALL_MASK, .always = true },
  232. #endif
  233. { .index = MSR_IA32_SPEC_CTRL, .always = false },
  234. { .index = MSR_IA32_PRED_CMD, .always = false },
  235. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  236. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  237. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  238. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  239. { .index = MSR_INVALID, .always = false },
  240. };
  241. /* enable NPT for AMD64 and X86 with PAE */
  242. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  243. static bool npt_enabled = true;
  244. #else
  245. static bool npt_enabled;
  246. #endif
  247. /*
  248. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  249. * pause_filter_count: On processors that support Pause filtering(indicated
  250. * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
  251. * count value. On VMRUN this value is loaded into an internal counter.
  252. * Each time a pause instruction is executed, this counter is decremented
  253. * until it reaches zero at which time a #VMEXIT is generated if pause
  254. * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
  255. * Intercept Filtering for more details.
  256. * This also indicate if ple logic enabled.
  257. *
  258. * pause_filter_thresh: In addition, some processor families support advanced
  259. * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
  260. * the amount of time a guest is allowed to execute in a pause loop.
  261. * In this mode, a 16-bit pause filter threshold field is added in the
  262. * VMCB. The threshold value is a cycle count that is used to reset the
  263. * pause counter. As with simple pause filtering, VMRUN loads the pause
  264. * count value from VMCB into an internal counter. Then, on each pause
  265. * instruction the hardware checks the elapsed number of cycles since
  266. * the most recent pause instruction against the pause filter threshold.
  267. * If the elapsed cycle count is greater than the pause filter threshold,
  268. * then the internal pause count is reloaded from the VMCB and execution
  269. * continues. If the elapsed cycle count is less than the pause filter
  270. * threshold, then the internal pause count is decremented. If the count
  271. * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
  272. * triggered. If advanced pause filtering is supported and pause filter
  273. * threshold field is set to zero, the filter will operate in the simpler,
  274. * count only mode.
  275. */
  276. static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
  277. module_param(pause_filter_thresh, ushort, 0444);
  278. static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
  279. module_param(pause_filter_count, ushort, 0444);
  280. /* Default doubles per-vcpu window every exit. */
  281. static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  282. module_param(pause_filter_count_grow, ushort, 0444);
  283. /* Default resets per-vcpu window every exit to pause_filter_count. */
  284. static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  285. module_param(pause_filter_count_shrink, ushort, 0444);
  286. /* Default is to compute the maximum so we can never overflow. */
  287. static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
  288. module_param(pause_filter_count_max, ushort, 0444);
  289. /* allow nested paging (virtualized MMU) for all guests */
  290. static int npt = true;
  291. module_param(npt, int, S_IRUGO);
  292. /* allow nested virtualization in KVM/SVM */
  293. static int nested = true;
  294. module_param(nested, int, S_IRUGO);
  295. /* enable / disable AVIC */
  296. static int avic;
  297. #ifdef CONFIG_X86_LOCAL_APIC
  298. module_param(avic, int, S_IRUGO);
  299. #endif
  300. /* enable/disable Virtual VMLOAD VMSAVE */
  301. static int vls = true;
  302. module_param(vls, int, 0444);
  303. /* enable/disable Virtual GIF */
  304. static int vgif = true;
  305. module_param(vgif, int, 0444);
  306. /* enable/disable SEV support */
  307. static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
  308. module_param(sev, int, 0444);
  309. static u8 rsm_ins_bytes[] = "\x0f\xaa";
  310. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  311. static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
  312. static void svm_complete_interrupts(struct vcpu_svm *svm);
  313. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  314. static int nested_svm_intercept(struct vcpu_svm *svm);
  315. static int nested_svm_vmexit(struct vcpu_svm *svm);
  316. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  317. bool has_error_code, u32 error_code);
  318. enum {
  319. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  320. pause filter count */
  321. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  322. VMCB_ASID, /* ASID */
  323. VMCB_INTR, /* int_ctl, int_vector */
  324. VMCB_NPT, /* npt_en, nCR3, gPAT */
  325. VMCB_CR, /* CR0, CR3, CR4, EFER */
  326. VMCB_DR, /* DR6, DR7 */
  327. VMCB_DT, /* GDT, IDT */
  328. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  329. VMCB_CR2, /* CR2 only */
  330. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  331. VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
  332. * AVIC PHYSICAL_TABLE pointer,
  333. * AVIC LOGICAL_TABLE pointer
  334. */
  335. VMCB_DIRTY_MAX,
  336. };
  337. /* TPR and CR2 are always written before VMRUN */
  338. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  339. #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
  340. static unsigned int max_sev_asid;
  341. static unsigned int min_sev_asid;
  342. static unsigned long *sev_asid_bitmap;
  343. #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
  344. struct enc_region {
  345. struct list_head list;
  346. unsigned long npages;
  347. struct page **pages;
  348. unsigned long uaddr;
  349. unsigned long size;
  350. };
  351. static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
  352. {
  353. return container_of(kvm, struct kvm_svm, kvm);
  354. }
  355. static inline bool svm_sev_enabled(void)
  356. {
  357. return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
  358. }
  359. static inline bool sev_guest(struct kvm *kvm)
  360. {
  361. #ifdef CONFIG_KVM_AMD_SEV
  362. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  363. return sev->active;
  364. #else
  365. return false;
  366. #endif
  367. }
  368. static inline int sev_get_asid(struct kvm *kvm)
  369. {
  370. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  371. return sev->asid;
  372. }
  373. static inline void mark_all_dirty(struct vmcb *vmcb)
  374. {
  375. vmcb->control.clean = 0;
  376. }
  377. static inline void mark_all_clean(struct vmcb *vmcb)
  378. {
  379. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  380. & ~VMCB_ALWAYS_DIRTY_MASK;
  381. }
  382. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  383. {
  384. vmcb->control.clean &= ~(1 << bit);
  385. }
  386. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  387. {
  388. return container_of(vcpu, struct vcpu_svm, vcpu);
  389. }
  390. static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
  391. {
  392. svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
  393. mark_dirty(svm->vmcb, VMCB_AVIC);
  394. }
  395. static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
  396. {
  397. struct vcpu_svm *svm = to_svm(vcpu);
  398. u64 *entry = svm->avic_physical_id_cache;
  399. if (!entry)
  400. return false;
  401. return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  402. }
  403. static void recalc_intercepts(struct vcpu_svm *svm)
  404. {
  405. struct vmcb_control_area *c, *h;
  406. struct nested_state *g;
  407. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  408. if (!is_guest_mode(&svm->vcpu))
  409. return;
  410. c = &svm->vmcb->control;
  411. h = &svm->nested.hsave->control;
  412. g = &svm->nested;
  413. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  414. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  415. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  416. c->intercept = h->intercept | g->intercept;
  417. }
  418. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  419. {
  420. if (is_guest_mode(&svm->vcpu))
  421. return svm->nested.hsave;
  422. else
  423. return svm->vmcb;
  424. }
  425. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  426. {
  427. struct vmcb *vmcb = get_host_vmcb(svm);
  428. vmcb->control.intercept_cr |= (1U << bit);
  429. recalc_intercepts(svm);
  430. }
  431. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  432. {
  433. struct vmcb *vmcb = get_host_vmcb(svm);
  434. vmcb->control.intercept_cr &= ~(1U << bit);
  435. recalc_intercepts(svm);
  436. }
  437. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  438. {
  439. struct vmcb *vmcb = get_host_vmcb(svm);
  440. return vmcb->control.intercept_cr & (1U << bit);
  441. }
  442. static inline void set_dr_intercepts(struct vcpu_svm *svm)
  443. {
  444. struct vmcb *vmcb = get_host_vmcb(svm);
  445. vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
  446. | (1 << INTERCEPT_DR1_READ)
  447. | (1 << INTERCEPT_DR2_READ)
  448. | (1 << INTERCEPT_DR3_READ)
  449. | (1 << INTERCEPT_DR4_READ)
  450. | (1 << INTERCEPT_DR5_READ)
  451. | (1 << INTERCEPT_DR6_READ)
  452. | (1 << INTERCEPT_DR7_READ)
  453. | (1 << INTERCEPT_DR0_WRITE)
  454. | (1 << INTERCEPT_DR1_WRITE)
  455. | (1 << INTERCEPT_DR2_WRITE)
  456. | (1 << INTERCEPT_DR3_WRITE)
  457. | (1 << INTERCEPT_DR4_WRITE)
  458. | (1 << INTERCEPT_DR5_WRITE)
  459. | (1 << INTERCEPT_DR6_WRITE)
  460. | (1 << INTERCEPT_DR7_WRITE);
  461. recalc_intercepts(svm);
  462. }
  463. static inline void clr_dr_intercepts(struct vcpu_svm *svm)
  464. {
  465. struct vmcb *vmcb = get_host_vmcb(svm);
  466. vmcb->control.intercept_dr = 0;
  467. recalc_intercepts(svm);
  468. }
  469. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  470. {
  471. struct vmcb *vmcb = get_host_vmcb(svm);
  472. vmcb->control.intercept_exceptions |= (1U << bit);
  473. recalc_intercepts(svm);
  474. }
  475. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  476. {
  477. struct vmcb *vmcb = get_host_vmcb(svm);
  478. vmcb->control.intercept_exceptions &= ~(1U << bit);
  479. recalc_intercepts(svm);
  480. }
  481. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  482. {
  483. struct vmcb *vmcb = get_host_vmcb(svm);
  484. vmcb->control.intercept |= (1ULL << bit);
  485. recalc_intercepts(svm);
  486. }
  487. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  488. {
  489. struct vmcb *vmcb = get_host_vmcb(svm);
  490. vmcb->control.intercept &= ~(1ULL << bit);
  491. recalc_intercepts(svm);
  492. }
  493. static inline bool vgif_enabled(struct vcpu_svm *svm)
  494. {
  495. return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
  496. }
  497. static inline void enable_gif(struct vcpu_svm *svm)
  498. {
  499. if (vgif_enabled(svm))
  500. svm->vmcb->control.int_ctl |= V_GIF_MASK;
  501. else
  502. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  503. }
  504. static inline void disable_gif(struct vcpu_svm *svm)
  505. {
  506. if (vgif_enabled(svm))
  507. svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
  508. else
  509. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  510. }
  511. static inline bool gif_set(struct vcpu_svm *svm)
  512. {
  513. if (vgif_enabled(svm))
  514. return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
  515. else
  516. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  517. }
  518. static unsigned long iopm_base;
  519. struct kvm_ldttss_desc {
  520. u16 limit0;
  521. u16 base0;
  522. unsigned base1:8, type:5, dpl:2, p:1;
  523. unsigned limit1:4, zero0:3, g:1, base2:8;
  524. u32 base3;
  525. u32 zero1;
  526. } __attribute__((packed));
  527. struct svm_cpu_data {
  528. int cpu;
  529. u64 asid_generation;
  530. u32 max_asid;
  531. u32 next_asid;
  532. u32 min_asid;
  533. struct kvm_ldttss_desc *tss_desc;
  534. struct page *save_area;
  535. struct vmcb *current_vmcb;
  536. /* index = sev_asid, value = vmcb pointer */
  537. struct vmcb **sev_vmcbs;
  538. };
  539. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  540. struct svm_init_data {
  541. int cpu;
  542. int r;
  543. };
  544. static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  545. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  546. #define MSRS_RANGE_SIZE 2048
  547. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  548. static u32 svm_msrpm_offset(u32 msr)
  549. {
  550. u32 offset;
  551. int i;
  552. for (i = 0; i < NUM_MSR_MAPS; i++) {
  553. if (msr < msrpm_ranges[i] ||
  554. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  555. continue;
  556. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  557. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  558. /* Now we have the u8 offset - but need the u32 offset */
  559. return offset / 4;
  560. }
  561. /* MSR not in any range */
  562. return MSR_INVALID;
  563. }
  564. #define MAX_INST_SIZE 15
  565. static inline void clgi(void)
  566. {
  567. asm volatile (__ex(SVM_CLGI));
  568. }
  569. static inline void stgi(void)
  570. {
  571. asm volatile (__ex(SVM_STGI));
  572. }
  573. static inline void invlpga(unsigned long addr, u32 asid)
  574. {
  575. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  576. }
  577. static int get_npt_level(struct kvm_vcpu *vcpu)
  578. {
  579. #ifdef CONFIG_X86_64
  580. return PT64_ROOT_4LEVEL;
  581. #else
  582. return PT32E_ROOT_LEVEL;
  583. #endif
  584. }
  585. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  586. {
  587. vcpu->arch.efer = efer;
  588. if (!npt_enabled && !(efer & EFER_LMA))
  589. efer &= ~EFER_LME;
  590. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  591. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  592. }
  593. static int is_external_interrupt(u32 info)
  594. {
  595. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  596. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  597. }
  598. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  599. {
  600. struct vcpu_svm *svm = to_svm(vcpu);
  601. u32 ret = 0;
  602. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  603. ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  604. return ret;
  605. }
  606. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  607. {
  608. struct vcpu_svm *svm = to_svm(vcpu);
  609. if (mask == 0)
  610. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  611. else
  612. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  613. }
  614. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  615. {
  616. struct vcpu_svm *svm = to_svm(vcpu);
  617. if (svm->vmcb->control.next_rip != 0) {
  618. WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
  619. svm->next_rip = svm->vmcb->control.next_rip;
  620. }
  621. if (!svm->next_rip) {
  622. if (kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) !=
  623. EMULATE_DONE)
  624. printk(KERN_DEBUG "%s: NOP\n", __func__);
  625. return;
  626. }
  627. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  628. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  629. __func__, kvm_rip_read(vcpu), svm->next_rip);
  630. kvm_rip_write(vcpu, svm->next_rip);
  631. svm_set_interrupt_shadow(vcpu, 0);
  632. }
  633. static void svm_queue_exception(struct kvm_vcpu *vcpu)
  634. {
  635. struct vcpu_svm *svm = to_svm(vcpu);
  636. unsigned nr = vcpu->arch.exception.nr;
  637. bool has_error_code = vcpu->arch.exception.has_error_code;
  638. bool reinject = vcpu->arch.exception.injected;
  639. u32 error_code = vcpu->arch.exception.error_code;
  640. /*
  641. * If we are within a nested VM we'd better #VMEXIT and let the guest
  642. * handle the exception
  643. */
  644. if (!reinject &&
  645. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  646. return;
  647. kvm_deliver_exception_payload(&svm->vcpu);
  648. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  649. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  650. /*
  651. * For guest debugging where we have to reinject #BP if some
  652. * INT3 is guest-owned:
  653. * Emulate nRIP by moving RIP forward. Will fail if injection
  654. * raises a fault that is not intercepted. Still better than
  655. * failing in all cases.
  656. */
  657. skip_emulated_instruction(&svm->vcpu);
  658. rip = kvm_rip_read(&svm->vcpu);
  659. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  660. svm->int3_injected = rip - old_rip;
  661. }
  662. svm->vmcb->control.event_inj = nr
  663. | SVM_EVTINJ_VALID
  664. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  665. | SVM_EVTINJ_TYPE_EXEPT;
  666. svm->vmcb->control.event_inj_err = error_code;
  667. }
  668. static void svm_init_erratum_383(void)
  669. {
  670. u32 low, high;
  671. int err;
  672. u64 val;
  673. if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
  674. return;
  675. /* Use _safe variants to not break nested virtualization */
  676. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  677. if (err)
  678. return;
  679. val |= (1ULL << 47);
  680. low = lower_32_bits(val);
  681. high = upper_32_bits(val);
  682. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  683. erratum_383_found = true;
  684. }
  685. static void svm_init_osvw(struct kvm_vcpu *vcpu)
  686. {
  687. /*
  688. * Guests should see errata 400 and 415 as fixed (assuming that
  689. * HLT and IO instructions are intercepted).
  690. */
  691. vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
  692. vcpu->arch.osvw.status = osvw_status & ~(6ULL);
  693. /*
  694. * By increasing VCPU's osvw.length to 3 we are telling the guest that
  695. * all osvw.status bits inside that length, including bit 0 (which is
  696. * reserved for erratum 298), are valid. However, if host processor's
  697. * osvw_len is 0 then osvw_status[0] carries no information. We need to
  698. * be conservative here and therefore we tell the guest that erratum 298
  699. * is present (because we really don't know).
  700. */
  701. if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
  702. vcpu->arch.osvw.status |= 1;
  703. }
  704. static int has_svm(void)
  705. {
  706. const char *msg;
  707. if (!cpu_has_svm(&msg)) {
  708. printk(KERN_INFO "has_svm: %s\n", msg);
  709. return 0;
  710. }
  711. return 1;
  712. }
  713. static void svm_hardware_disable(void)
  714. {
  715. /* Make sure we clean up behind us */
  716. if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
  717. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  718. cpu_svm_disable();
  719. amd_pmu_disable_virt();
  720. }
  721. static int svm_hardware_enable(void)
  722. {
  723. struct svm_cpu_data *sd;
  724. uint64_t efer;
  725. struct desc_struct *gdt;
  726. int me = raw_smp_processor_id();
  727. rdmsrl(MSR_EFER, efer);
  728. if (efer & EFER_SVME)
  729. return -EBUSY;
  730. if (!has_svm()) {
  731. pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
  732. return -EINVAL;
  733. }
  734. sd = per_cpu(svm_data, me);
  735. if (!sd) {
  736. pr_err("%s: svm_data is NULL on %d\n", __func__, me);
  737. return -EINVAL;
  738. }
  739. sd->asid_generation = 1;
  740. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  741. sd->next_asid = sd->max_asid + 1;
  742. sd->min_asid = max_sev_asid + 1;
  743. gdt = get_current_gdt_rw();
  744. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  745. wrmsrl(MSR_EFER, efer | EFER_SVME);
  746. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  747. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  748. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  749. __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
  750. }
  751. /*
  752. * Get OSVW bits.
  753. *
  754. * Note that it is possible to have a system with mixed processor
  755. * revisions and therefore different OSVW bits. If bits are not the same
  756. * on different processors then choose the worst case (i.e. if erratum
  757. * is present on one processor and not on another then assume that the
  758. * erratum is present everywhere).
  759. */
  760. if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
  761. uint64_t len, status = 0;
  762. int err;
  763. len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
  764. if (!err)
  765. status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
  766. &err);
  767. if (err)
  768. osvw_status = osvw_len = 0;
  769. else {
  770. if (len < osvw_len)
  771. osvw_len = len;
  772. osvw_status |= status;
  773. osvw_status &= (1ULL << osvw_len) - 1;
  774. }
  775. } else
  776. osvw_status = osvw_len = 0;
  777. svm_init_erratum_383();
  778. amd_pmu_enable_virt();
  779. return 0;
  780. }
  781. static void svm_cpu_uninit(int cpu)
  782. {
  783. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  784. if (!sd)
  785. return;
  786. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  787. kfree(sd->sev_vmcbs);
  788. __free_page(sd->save_area);
  789. kfree(sd);
  790. }
  791. static int svm_cpu_init(int cpu)
  792. {
  793. struct svm_cpu_data *sd;
  794. int r;
  795. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  796. if (!sd)
  797. return -ENOMEM;
  798. sd->cpu = cpu;
  799. r = -ENOMEM;
  800. sd->save_area = alloc_page(GFP_KERNEL);
  801. if (!sd->save_area)
  802. goto err_1;
  803. if (svm_sev_enabled()) {
  804. r = -ENOMEM;
  805. sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
  806. sizeof(void *),
  807. GFP_KERNEL);
  808. if (!sd->sev_vmcbs)
  809. goto err_1;
  810. }
  811. per_cpu(svm_data, cpu) = sd;
  812. return 0;
  813. err_1:
  814. kfree(sd);
  815. return r;
  816. }
  817. static bool valid_msr_intercept(u32 index)
  818. {
  819. int i;
  820. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  821. if (direct_access_msrs[i].index == index)
  822. return true;
  823. return false;
  824. }
  825. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
  826. {
  827. u8 bit_write;
  828. unsigned long tmp;
  829. u32 offset;
  830. u32 *msrpm;
  831. msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
  832. to_svm(vcpu)->msrpm;
  833. offset = svm_msrpm_offset(msr);
  834. bit_write = 2 * (msr & 0x0f) + 1;
  835. tmp = msrpm[offset];
  836. BUG_ON(offset == MSR_INVALID);
  837. return !!test_bit(bit_write, &tmp);
  838. }
  839. static void set_msr_interception(u32 *msrpm, unsigned msr,
  840. int read, int write)
  841. {
  842. u8 bit_read, bit_write;
  843. unsigned long tmp;
  844. u32 offset;
  845. /*
  846. * If this warning triggers extend the direct_access_msrs list at the
  847. * beginning of the file
  848. */
  849. WARN_ON(!valid_msr_intercept(msr));
  850. offset = svm_msrpm_offset(msr);
  851. bit_read = 2 * (msr & 0x0f);
  852. bit_write = 2 * (msr & 0x0f) + 1;
  853. tmp = msrpm[offset];
  854. BUG_ON(offset == MSR_INVALID);
  855. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  856. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  857. msrpm[offset] = tmp;
  858. }
  859. static void svm_vcpu_init_msrpm(u32 *msrpm)
  860. {
  861. int i;
  862. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  863. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  864. if (!direct_access_msrs[i].always)
  865. continue;
  866. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  867. }
  868. }
  869. static void add_msr_offset(u32 offset)
  870. {
  871. int i;
  872. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  873. /* Offset already in list? */
  874. if (msrpm_offsets[i] == offset)
  875. return;
  876. /* Slot used by another offset? */
  877. if (msrpm_offsets[i] != MSR_INVALID)
  878. continue;
  879. /* Add offset to list */
  880. msrpm_offsets[i] = offset;
  881. return;
  882. }
  883. /*
  884. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  885. * increase MSRPM_OFFSETS in this case.
  886. */
  887. BUG();
  888. }
  889. static void init_msrpm_offsets(void)
  890. {
  891. int i;
  892. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  893. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  894. u32 offset;
  895. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  896. BUG_ON(offset == MSR_INVALID);
  897. add_msr_offset(offset);
  898. }
  899. }
  900. static void svm_enable_lbrv(struct vcpu_svm *svm)
  901. {
  902. u32 *msrpm = svm->msrpm;
  903. svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
  904. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  905. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  906. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  907. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  908. }
  909. static void svm_disable_lbrv(struct vcpu_svm *svm)
  910. {
  911. u32 *msrpm = svm->msrpm;
  912. svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
  913. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  914. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  915. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  916. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  917. }
  918. static void disable_nmi_singlestep(struct vcpu_svm *svm)
  919. {
  920. svm->nmi_singlestep = false;
  921. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
  922. /* Clear our flags if they were not set by the guest */
  923. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  924. svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
  925. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  926. svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
  927. }
  928. }
  929. /* Note:
  930. * This hash table is used to map VM_ID to a struct kvm_svm,
  931. * when handling AMD IOMMU GALOG notification to schedule in
  932. * a particular vCPU.
  933. */
  934. #define SVM_VM_DATA_HASH_BITS 8
  935. static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
  936. static u32 next_vm_id = 0;
  937. static bool next_vm_id_wrapped = 0;
  938. static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
  939. /* Note:
  940. * This function is called from IOMMU driver to notify
  941. * SVM to schedule in a particular vCPU of a particular VM.
  942. */
  943. static int avic_ga_log_notifier(u32 ga_tag)
  944. {
  945. unsigned long flags;
  946. struct kvm_svm *kvm_svm;
  947. struct kvm_vcpu *vcpu = NULL;
  948. u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
  949. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
  950. pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
  951. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  952. hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
  953. if (kvm_svm->avic_vm_id != vm_id)
  954. continue;
  955. vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
  956. break;
  957. }
  958. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  959. /* Note:
  960. * At this point, the IOMMU should have already set the pending
  961. * bit in the vAPIC backing page. So, we just need to schedule
  962. * in the vcpu.
  963. */
  964. if (vcpu)
  965. kvm_vcpu_wake_up(vcpu);
  966. return 0;
  967. }
  968. static __init int sev_hardware_setup(void)
  969. {
  970. struct sev_user_data_status *status;
  971. int rc;
  972. /* Maximum number of encrypted guests supported simultaneously */
  973. max_sev_asid = cpuid_ecx(0x8000001F);
  974. if (!max_sev_asid)
  975. return 1;
  976. /* Minimum ASID value that should be used for SEV guest */
  977. min_sev_asid = cpuid_edx(0x8000001F);
  978. /* Initialize SEV ASID bitmap */
  979. sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
  980. if (!sev_asid_bitmap)
  981. return 1;
  982. status = kmalloc(sizeof(*status), GFP_KERNEL);
  983. if (!status)
  984. return 1;
  985. /*
  986. * Check SEV platform status.
  987. *
  988. * PLATFORM_STATUS can be called in any state, if we failed to query
  989. * the PLATFORM status then either PSP firmware does not support SEV
  990. * feature or SEV firmware is dead.
  991. */
  992. rc = sev_platform_status(status, NULL);
  993. if (rc)
  994. goto err;
  995. pr_info("SEV supported\n");
  996. err:
  997. kfree(status);
  998. return rc;
  999. }
  1000. static void grow_ple_window(struct kvm_vcpu *vcpu)
  1001. {
  1002. struct vcpu_svm *svm = to_svm(vcpu);
  1003. struct vmcb_control_area *control = &svm->vmcb->control;
  1004. int old = control->pause_filter_count;
  1005. control->pause_filter_count = __grow_ple_window(old,
  1006. pause_filter_count,
  1007. pause_filter_count_grow,
  1008. pause_filter_count_max);
  1009. if (control->pause_filter_count != old)
  1010. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  1011. trace_kvm_ple_window_grow(vcpu->vcpu_id,
  1012. control->pause_filter_count, old);
  1013. }
  1014. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  1015. {
  1016. struct vcpu_svm *svm = to_svm(vcpu);
  1017. struct vmcb_control_area *control = &svm->vmcb->control;
  1018. int old = control->pause_filter_count;
  1019. control->pause_filter_count =
  1020. __shrink_ple_window(old,
  1021. pause_filter_count,
  1022. pause_filter_count_shrink,
  1023. pause_filter_count);
  1024. if (control->pause_filter_count != old)
  1025. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  1026. trace_kvm_ple_window_shrink(vcpu->vcpu_id,
  1027. control->pause_filter_count, old);
  1028. }
  1029. static __init int svm_hardware_setup(void)
  1030. {
  1031. int cpu;
  1032. struct page *iopm_pages;
  1033. void *iopm_va;
  1034. int r;
  1035. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  1036. if (!iopm_pages)
  1037. return -ENOMEM;
  1038. iopm_va = page_address(iopm_pages);
  1039. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  1040. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  1041. init_msrpm_offsets();
  1042. if (boot_cpu_has(X86_FEATURE_NX))
  1043. kvm_enable_efer_bits(EFER_NX);
  1044. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  1045. kvm_enable_efer_bits(EFER_FFXSR);
  1046. if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  1047. kvm_has_tsc_control = true;
  1048. kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
  1049. kvm_tsc_scaling_ratio_frac_bits = 32;
  1050. }
  1051. /* Check for pause filtering support */
  1052. if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  1053. pause_filter_count = 0;
  1054. pause_filter_thresh = 0;
  1055. } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
  1056. pause_filter_thresh = 0;
  1057. }
  1058. if (nested) {
  1059. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  1060. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  1061. }
  1062. if (sev) {
  1063. if (boot_cpu_has(X86_FEATURE_SEV) &&
  1064. IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
  1065. r = sev_hardware_setup();
  1066. if (r)
  1067. sev = false;
  1068. } else {
  1069. sev = false;
  1070. }
  1071. }
  1072. for_each_possible_cpu(cpu) {
  1073. r = svm_cpu_init(cpu);
  1074. if (r)
  1075. goto err;
  1076. }
  1077. if (!boot_cpu_has(X86_FEATURE_NPT))
  1078. npt_enabled = false;
  1079. if (npt_enabled && !npt) {
  1080. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  1081. npt_enabled = false;
  1082. }
  1083. if (npt_enabled) {
  1084. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  1085. kvm_enable_tdp();
  1086. } else
  1087. kvm_disable_tdp();
  1088. if (avic) {
  1089. if (!npt_enabled ||
  1090. !boot_cpu_has(X86_FEATURE_AVIC) ||
  1091. !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
  1092. avic = false;
  1093. } else {
  1094. pr_info("AVIC enabled\n");
  1095. amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
  1096. }
  1097. }
  1098. if (vls) {
  1099. if (!npt_enabled ||
  1100. !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
  1101. !IS_ENABLED(CONFIG_X86_64)) {
  1102. vls = false;
  1103. } else {
  1104. pr_info("Virtual VMLOAD VMSAVE supported\n");
  1105. }
  1106. }
  1107. if (vgif) {
  1108. if (!boot_cpu_has(X86_FEATURE_VGIF))
  1109. vgif = false;
  1110. else
  1111. pr_info("Virtual GIF supported\n");
  1112. }
  1113. return 0;
  1114. err:
  1115. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  1116. iopm_base = 0;
  1117. return r;
  1118. }
  1119. static __exit void svm_hardware_unsetup(void)
  1120. {
  1121. int cpu;
  1122. if (svm_sev_enabled())
  1123. bitmap_free(sev_asid_bitmap);
  1124. for_each_possible_cpu(cpu)
  1125. svm_cpu_uninit(cpu);
  1126. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  1127. iopm_base = 0;
  1128. }
  1129. static void init_seg(struct vmcb_seg *seg)
  1130. {
  1131. seg->selector = 0;
  1132. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  1133. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  1134. seg->limit = 0xffff;
  1135. seg->base = 0;
  1136. }
  1137. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  1138. {
  1139. seg->selector = 0;
  1140. seg->attrib = SVM_SELECTOR_P_MASK | type;
  1141. seg->limit = 0xffff;
  1142. seg->base = 0;
  1143. }
  1144. static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  1145. {
  1146. struct vcpu_svm *svm = to_svm(vcpu);
  1147. if (is_guest_mode(vcpu))
  1148. return svm->nested.hsave->control.tsc_offset;
  1149. return vcpu->arch.tsc_offset;
  1150. }
  1151. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1152. {
  1153. struct vcpu_svm *svm = to_svm(vcpu);
  1154. u64 g_tsc_offset = 0;
  1155. if (is_guest_mode(vcpu)) {
  1156. /* Write L1's TSC offset. */
  1157. g_tsc_offset = svm->vmcb->control.tsc_offset -
  1158. svm->nested.hsave->control.tsc_offset;
  1159. svm->nested.hsave->control.tsc_offset = offset;
  1160. } else
  1161. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  1162. svm->vmcb->control.tsc_offset,
  1163. offset);
  1164. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  1165. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  1166. }
  1167. static void avic_init_vmcb(struct vcpu_svm *svm)
  1168. {
  1169. struct vmcb *vmcb = svm->vmcb;
  1170. struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
  1171. phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
  1172. phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
  1173. phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
  1174. vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
  1175. vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
  1176. vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
  1177. vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
  1178. vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
  1179. }
  1180. static void init_vmcb(struct vcpu_svm *svm)
  1181. {
  1182. struct vmcb_control_area *control = &svm->vmcb->control;
  1183. struct vmcb_save_area *save = &svm->vmcb->save;
  1184. svm->vcpu.arch.hflags = 0;
  1185. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1186. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  1187. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  1188. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1189. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  1190. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  1191. if (!kvm_vcpu_apicv_active(&svm->vcpu))
  1192. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  1193. set_dr_intercepts(svm);
  1194. set_exception_intercept(svm, PF_VECTOR);
  1195. set_exception_intercept(svm, UD_VECTOR);
  1196. set_exception_intercept(svm, MC_VECTOR);
  1197. set_exception_intercept(svm, AC_VECTOR);
  1198. set_exception_intercept(svm, DB_VECTOR);
  1199. /*
  1200. * Guest access to VMware backdoor ports could legitimately
  1201. * trigger #GP because of TSS I/O permission bitmap.
  1202. * We intercept those #GP and allow access to them anyway
  1203. * as VMware does.
  1204. */
  1205. if (enable_vmware_backdoor)
  1206. set_exception_intercept(svm, GP_VECTOR);
  1207. set_intercept(svm, INTERCEPT_INTR);
  1208. set_intercept(svm, INTERCEPT_NMI);
  1209. set_intercept(svm, INTERCEPT_SMI);
  1210. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  1211. set_intercept(svm, INTERCEPT_RDPMC);
  1212. set_intercept(svm, INTERCEPT_CPUID);
  1213. set_intercept(svm, INTERCEPT_INVD);
  1214. set_intercept(svm, INTERCEPT_INVLPG);
  1215. set_intercept(svm, INTERCEPT_INVLPGA);
  1216. set_intercept(svm, INTERCEPT_IOIO_PROT);
  1217. set_intercept(svm, INTERCEPT_MSR_PROT);
  1218. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  1219. set_intercept(svm, INTERCEPT_SHUTDOWN);
  1220. set_intercept(svm, INTERCEPT_VMRUN);
  1221. set_intercept(svm, INTERCEPT_VMMCALL);
  1222. set_intercept(svm, INTERCEPT_VMLOAD);
  1223. set_intercept(svm, INTERCEPT_VMSAVE);
  1224. set_intercept(svm, INTERCEPT_STGI);
  1225. set_intercept(svm, INTERCEPT_CLGI);
  1226. set_intercept(svm, INTERCEPT_SKINIT);
  1227. set_intercept(svm, INTERCEPT_WBINVD);
  1228. set_intercept(svm, INTERCEPT_XSETBV);
  1229. set_intercept(svm, INTERCEPT_RSM);
  1230. if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
  1231. set_intercept(svm, INTERCEPT_MONITOR);
  1232. set_intercept(svm, INTERCEPT_MWAIT);
  1233. }
  1234. if (!kvm_hlt_in_guest(svm->vcpu.kvm))
  1235. set_intercept(svm, INTERCEPT_HLT);
  1236. control->iopm_base_pa = __sme_set(iopm_base);
  1237. control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
  1238. control->int_ctl = V_INTR_MASKING_MASK;
  1239. init_seg(&save->es);
  1240. init_seg(&save->ss);
  1241. init_seg(&save->ds);
  1242. init_seg(&save->fs);
  1243. init_seg(&save->gs);
  1244. save->cs.selector = 0xf000;
  1245. save->cs.base = 0xffff0000;
  1246. /* Executable/Readable Code Segment */
  1247. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  1248. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  1249. save->cs.limit = 0xffff;
  1250. save->gdtr.limit = 0xffff;
  1251. save->idtr.limit = 0xffff;
  1252. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  1253. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  1254. svm_set_efer(&svm->vcpu, 0);
  1255. save->dr6 = 0xffff0ff0;
  1256. kvm_set_rflags(&svm->vcpu, 2);
  1257. save->rip = 0x0000fff0;
  1258. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  1259. /*
  1260. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  1261. * It also updates the guest-visible cr0 value.
  1262. */
  1263. svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  1264. kvm_mmu_reset_context(&svm->vcpu);
  1265. save->cr4 = X86_CR4_PAE;
  1266. /* rdx = ?? */
  1267. if (npt_enabled) {
  1268. /* Setup VMCB for Nested Paging */
  1269. control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
  1270. clr_intercept(svm, INTERCEPT_INVLPG);
  1271. clr_exception_intercept(svm, PF_VECTOR);
  1272. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  1273. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  1274. save->g_pat = svm->vcpu.arch.pat;
  1275. save->cr3 = 0;
  1276. save->cr4 = 0;
  1277. }
  1278. svm->asid_generation = 0;
  1279. svm->nested.vmcb = 0;
  1280. svm->vcpu.arch.hflags = 0;
  1281. if (pause_filter_count) {
  1282. control->pause_filter_count = pause_filter_count;
  1283. if (pause_filter_thresh)
  1284. control->pause_filter_thresh = pause_filter_thresh;
  1285. set_intercept(svm, INTERCEPT_PAUSE);
  1286. } else {
  1287. clr_intercept(svm, INTERCEPT_PAUSE);
  1288. }
  1289. if (kvm_vcpu_apicv_active(&svm->vcpu))
  1290. avic_init_vmcb(svm);
  1291. /*
  1292. * If hardware supports Virtual VMLOAD VMSAVE then enable it
  1293. * in VMCB and clear intercepts to avoid #VMEXIT.
  1294. */
  1295. if (vls) {
  1296. clr_intercept(svm, INTERCEPT_VMLOAD);
  1297. clr_intercept(svm, INTERCEPT_VMSAVE);
  1298. svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
  1299. }
  1300. if (vgif) {
  1301. clr_intercept(svm, INTERCEPT_STGI);
  1302. clr_intercept(svm, INTERCEPT_CLGI);
  1303. svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
  1304. }
  1305. if (sev_guest(svm->vcpu.kvm)) {
  1306. svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
  1307. clr_exception_intercept(svm, UD_VECTOR);
  1308. }
  1309. mark_all_dirty(svm->vmcb);
  1310. enable_gif(svm);
  1311. }
  1312. static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
  1313. unsigned int index)
  1314. {
  1315. u64 *avic_physical_id_table;
  1316. struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
  1317. if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1318. return NULL;
  1319. avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
  1320. return &avic_physical_id_table[index];
  1321. }
  1322. /**
  1323. * Note:
  1324. * AVIC hardware walks the nested page table to check permissions,
  1325. * but does not use the SPA address specified in the leaf page
  1326. * table entry since it uses address in the AVIC_BACKING_PAGE pointer
  1327. * field of the VMCB. Therefore, we set up the
  1328. * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
  1329. */
  1330. static int avic_init_access_page(struct kvm_vcpu *vcpu)
  1331. {
  1332. struct kvm *kvm = vcpu->kvm;
  1333. int ret;
  1334. if (kvm->arch.apic_access_page_done)
  1335. return 0;
  1336. ret = x86_set_memory_region(kvm,
  1337. APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  1338. APIC_DEFAULT_PHYS_BASE,
  1339. PAGE_SIZE);
  1340. if (ret)
  1341. return ret;
  1342. kvm->arch.apic_access_page_done = true;
  1343. return 0;
  1344. }
  1345. static int avic_init_backing_page(struct kvm_vcpu *vcpu)
  1346. {
  1347. int ret;
  1348. u64 *entry, new_entry;
  1349. int id = vcpu->vcpu_id;
  1350. struct vcpu_svm *svm = to_svm(vcpu);
  1351. ret = avic_init_access_page(vcpu);
  1352. if (ret)
  1353. return ret;
  1354. if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1355. return -EINVAL;
  1356. if (!svm->vcpu.arch.apic->regs)
  1357. return -EINVAL;
  1358. svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
  1359. /* Setting AVIC backing page address in the phy APIC ID table */
  1360. entry = avic_get_physical_id_entry(vcpu, id);
  1361. if (!entry)
  1362. return -EINVAL;
  1363. new_entry = READ_ONCE(*entry);
  1364. new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
  1365. AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
  1366. AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
  1367. WRITE_ONCE(*entry, new_entry);
  1368. svm->avic_physical_id_cache = entry;
  1369. return 0;
  1370. }
  1371. static void __sev_asid_free(int asid)
  1372. {
  1373. struct svm_cpu_data *sd;
  1374. int cpu, pos;
  1375. pos = asid - 1;
  1376. clear_bit(pos, sev_asid_bitmap);
  1377. for_each_possible_cpu(cpu) {
  1378. sd = per_cpu(svm_data, cpu);
  1379. sd->sev_vmcbs[pos] = NULL;
  1380. }
  1381. }
  1382. static void sev_asid_free(struct kvm *kvm)
  1383. {
  1384. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  1385. __sev_asid_free(sev->asid);
  1386. }
  1387. static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
  1388. {
  1389. struct sev_data_decommission *decommission;
  1390. struct sev_data_deactivate *data;
  1391. if (!handle)
  1392. return;
  1393. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1394. if (!data)
  1395. return;
  1396. /* deactivate handle */
  1397. data->handle = handle;
  1398. sev_guest_deactivate(data, NULL);
  1399. wbinvd_on_all_cpus();
  1400. sev_guest_df_flush(NULL);
  1401. kfree(data);
  1402. decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
  1403. if (!decommission)
  1404. return;
  1405. /* decommission handle */
  1406. decommission->handle = handle;
  1407. sev_guest_decommission(decommission, NULL);
  1408. kfree(decommission);
  1409. }
  1410. static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
  1411. unsigned long ulen, unsigned long *n,
  1412. int write)
  1413. {
  1414. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  1415. unsigned long npages, npinned, size;
  1416. unsigned long locked, lock_limit;
  1417. struct page **pages;
  1418. unsigned long first, last;
  1419. if (ulen == 0 || uaddr + ulen < uaddr)
  1420. return NULL;
  1421. /* Calculate number of pages. */
  1422. first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
  1423. last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
  1424. npages = (last - first + 1);
  1425. locked = sev->pages_locked + npages;
  1426. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  1427. if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
  1428. pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
  1429. return NULL;
  1430. }
  1431. /* Avoid using vmalloc for smaller buffers. */
  1432. size = npages * sizeof(struct page *);
  1433. if (size > PAGE_SIZE)
  1434. pages = vmalloc(size);
  1435. else
  1436. pages = kmalloc(size, GFP_KERNEL);
  1437. if (!pages)
  1438. return NULL;
  1439. /* Pin the user virtual address. */
  1440. npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
  1441. if (npinned != npages) {
  1442. pr_err("SEV: Failure locking %lu pages.\n", npages);
  1443. goto err;
  1444. }
  1445. *n = npages;
  1446. sev->pages_locked = locked;
  1447. return pages;
  1448. err:
  1449. if (npinned > 0)
  1450. release_pages(pages, npinned);
  1451. kvfree(pages);
  1452. return NULL;
  1453. }
  1454. static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
  1455. unsigned long npages)
  1456. {
  1457. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  1458. release_pages(pages, npages);
  1459. kvfree(pages);
  1460. sev->pages_locked -= npages;
  1461. }
  1462. static void sev_clflush_pages(struct page *pages[], unsigned long npages)
  1463. {
  1464. uint8_t *page_virtual;
  1465. unsigned long i;
  1466. if (npages == 0 || pages == NULL)
  1467. return;
  1468. for (i = 0; i < npages; i++) {
  1469. page_virtual = kmap_atomic(pages[i]);
  1470. clflush_cache_range(page_virtual, PAGE_SIZE);
  1471. kunmap_atomic(page_virtual);
  1472. }
  1473. }
  1474. static void __unregister_enc_region_locked(struct kvm *kvm,
  1475. struct enc_region *region)
  1476. {
  1477. /*
  1478. * The guest may change the memory encryption attribute from C=0 -> C=1
  1479. * or vice versa for this memory range. Lets make sure caches are
  1480. * flushed to ensure that guest data gets written into memory with
  1481. * correct C-bit.
  1482. */
  1483. sev_clflush_pages(region->pages, region->npages);
  1484. sev_unpin_memory(kvm, region->pages, region->npages);
  1485. list_del(&region->list);
  1486. kfree(region);
  1487. }
  1488. static struct kvm *svm_vm_alloc(void)
  1489. {
  1490. struct kvm_svm *kvm_svm = vzalloc(sizeof(struct kvm_svm));
  1491. return &kvm_svm->kvm;
  1492. }
  1493. static void svm_vm_free(struct kvm *kvm)
  1494. {
  1495. vfree(to_kvm_svm(kvm));
  1496. }
  1497. static void sev_vm_destroy(struct kvm *kvm)
  1498. {
  1499. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  1500. struct list_head *head = &sev->regions_list;
  1501. struct list_head *pos, *q;
  1502. if (!sev_guest(kvm))
  1503. return;
  1504. mutex_lock(&kvm->lock);
  1505. /*
  1506. * if userspace was terminated before unregistering the memory regions
  1507. * then lets unpin all the registered memory.
  1508. */
  1509. if (!list_empty(head)) {
  1510. list_for_each_safe(pos, q, head) {
  1511. __unregister_enc_region_locked(kvm,
  1512. list_entry(pos, struct enc_region, list));
  1513. }
  1514. }
  1515. mutex_unlock(&kvm->lock);
  1516. sev_unbind_asid(kvm, sev->handle);
  1517. sev_asid_free(kvm);
  1518. }
  1519. static void avic_vm_destroy(struct kvm *kvm)
  1520. {
  1521. unsigned long flags;
  1522. struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
  1523. if (!avic)
  1524. return;
  1525. if (kvm_svm->avic_logical_id_table_page)
  1526. __free_page(kvm_svm->avic_logical_id_table_page);
  1527. if (kvm_svm->avic_physical_id_table_page)
  1528. __free_page(kvm_svm->avic_physical_id_table_page);
  1529. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1530. hash_del(&kvm_svm->hnode);
  1531. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1532. }
  1533. static void svm_vm_destroy(struct kvm *kvm)
  1534. {
  1535. avic_vm_destroy(kvm);
  1536. sev_vm_destroy(kvm);
  1537. }
  1538. static int avic_vm_init(struct kvm *kvm)
  1539. {
  1540. unsigned long flags;
  1541. int err = -ENOMEM;
  1542. struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
  1543. struct kvm_svm *k2;
  1544. struct page *p_page;
  1545. struct page *l_page;
  1546. u32 vm_id;
  1547. if (!avic)
  1548. return 0;
  1549. /* Allocating physical APIC ID table (4KB) */
  1550. p_page = alloc_page(GFP_KERNEL);
  1551. if (!p_page)
  1552. goto free_avic;
  1553. kvm_svm->avic_physical_id_table_page = p_page;
  1554. clear_page(page_address(p_page));
  1555. /* Allocating logical APIC ID table (4KB) */
  1556. l_page = alloc_page(GFP_KERNEL);
  1557. if (!l_page)
  1558. goto free_avic;
  1559. kvm_svm->avic_logical_id_table_page = l_page;
  1560. clear_page(page_address(l_page));
  1561. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1562. again:
  1563. vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
  1564. if (vm_id == 0) { /* id is 1-based, zero is not okay */
  1565. next_vm_id_wrapped = 1;
  1566. goto again;
  1567. }
  1568. /* Is it still in use? Only possible if wrapped at least once */
  1569. if (next_vm_id_wrapped) {
  1570. hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
  1571. if (k2->avic_vm_id == vm_id)
  1572. goto again;
  1573. }
  1574. }
  1575. kvm_svm->avic_vm_id = vm_id;
  1576. hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
  1577. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1578. return 0;
  1579. free_avic:
  1580. avic_vm_destroy(kvm);
  1581. return err;
  1582. }
  1583. static inline int
  1584. avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
  1585. {
  1586. int ret = 0;
  1587. unsigned long flags;
  1588. struct amd_svm_iommu_ir *ir;
  1589. struct vcpu_svm *svm = to_svm(vcpu);
  1590. if (!kvm_arch_has_assigned_device(vcpu->kvm))
  1591. return 0;
  1592. /*
  1593. * Here, we go through the per-vcpu ir_list to update all existing
  1594. * interrupt remapping table entry targeting this vcpu.
  1595. */
  1596. spin_lock_irqsave(&svm->ir_list_lock, flags);
  1597. if (list_empty(&svm->ir_list))
  1598. goto out;
  1599. list_for_each_entry(ir, &svm->ir_list, node) {
  1600. ret = amd_iommu_update_ga(cpu, r, ir->data);
  1601. if (ret)
  1602. break;
  1603. }
  1604. out:
  1605. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  1606. return ret;
  1607. }
  1608. static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1609. {
  1610. u64 entry;
  1611. /* ID = 0xff (broadcast), ID > 0xff (reserved) */
  1612. int h_physical_id = kvm_cpu_get_apicid(cpu);
  1613. struct vcpu_svm *svm = to_svm(vcpu);
  1614. if (!kvm_vcpu_apicv_active(vcpu))
  1615. return;
  1616. if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
  1617. return;
  1618. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1619. WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  1620. entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
  1621. entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
  1622. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1623. if (svm->avic_is_running)
  1624. entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1625. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1626. avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
  1627. svm->avic_is_running);
  1628. }
  1629. static void avic_vcpu_put(struct kvm_vcpu *vcpu)
  1630. {
  1631. u64 entry;
  1632. struct vcpu_svm *svm = to_svm(vcpu);
  1633. if (!kvm_vcpu_apicv_active(vcpu))
  1634. return;
  1635. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1636. if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
  1637. avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
  1638. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1639. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1640. }
  1641. /**
  1642. * This function is called during VCPU halt/unhalt.
  1643. */
  1644. static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
  1645. {
  1646. struct vcpu_svm *svm = to_svm(vcpu);
  1647. svm->avic_is_running = is_run;
  1648. if (is_run)
  1649. avic_vcpu_load(vcpu, vcpu->cpu);
  1650. else
  1651. avic_vcpu_put(vcpu);
  1652. }
  1653. static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  1654. {
  1655. struct vcpu_svm *svm = to_svm(vcpu);
  1656. u32 dummy;
  1657. u32 eax = 1;
  1658. vcpu->arch.microcode_version = 0x01000065;
  1659. svm->spec_ctrl = 0;
  1660. svm->virt_spec_ctrl = 0;
  1661. if (!init_event) {
  1662. svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
  1663. MSR_IA32_APICBASE_ENABLE;
  1664. if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
  1665. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  1666. }
  1667. init_vmcb(svm);
  1668. kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
  1669. kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
  1670. if (kvm_vcpu_apicv_active(vcpu) && !init_event)
  1671. avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
  1672. }
  1673. static int avic_init_vcpu(struct vcpu_svm *svm)
  1674. {
  1675. int ret;
  1676. if (!kvm_vcpu_apicv_active(&svm->vcpu))
  1677. return 0;
  1678. ret = avic_init_backing_page(&svm->vcpu);
  1679. if (ret)
  1680. return ret;
  1681. INIT_LIST_HEAD(&svm->ir_list);
  1682. spin_lock_init(&svm->ir_list_lock);
  1683. return ret;
  1684. }
  1685. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  1686. {
  1687. struct vcpu_svm *svm;
  1688. struct page *page;
  1689. struct page *msrpm_pages;
  1690. struct page *hsave_page;
  1691. struct page *nested_msrpm_pages;
  1692. int err;
  1693. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1694. if (!svm) {
  1695. err = -ENOMEM;
  1696. goto out;
  1697. }
  1698. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  1699. if (err)
  1700. goto free_svm;
  1701. err = -ENOMEM;
  1702. page = alloc_page(GFP_KERNEL);
  1703. if (!page)
  1704. goto uninit;
  1705. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1706. if (!msrpm_pages)
  1707. goto free_page1;
  1708. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1709. if (!nested_msrpm_pages)
  1710. goto free_page2;
  1711. hsave_page = alloc_page(GFP_KERNEL);
  1712. if (!hsave_page)
  1713. goto free_page3;
  1714. err = avic_init_vcpu(svm);
  1715. if (err)
  1716. goto free_page4;
  1717. /* We initialize this flag to true to make sure that the is_running
  1718. * bit would be set the first time the vcpu is loaded.
  1719. */
  1720. svm->avic_is_running = true;
  1721. svm->nested.hsave = page_address(hsave_page);
  1722. svm->msrpm = page_address(msrpm_pages);
  1723. svm_vcpu_init_msrpm(svm->msrpm);
  1724. svm->nested.msrpm = page_address(nested_msrpm_pages);
  1725. svm_vcpu_init_msrpm(svm->nested.msrpm);
  1726. svm->vmcb = page_address(page);
  1727. clear_page(svm->vmcb);
  1728. svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
  1729. svm->asid_generation = 0;
  1730. init_vmcb(svm);
  1731. svm_init_osvw(&svm->vcpu);
  1732. return &svm->vcpu;
  1733. free_page4:
  1734. __free_page(hsave_page);
  1735. free_page3:
  1736. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  1737. free_page2:
  1738. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  1739. free_page1:
  1740. __free_page(page);
  1741. uninit:
  1742. kvm_vcpu_uninit(&svm->vcpu);
  1743. free_svm:
  1744. kmem_cache_free(kvm_vcpu_cache, svm);
  1745. out:
  1746. return ERR_PTR(err);
  1747. }
  1748. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  1749. {
  1750. struct vcpu_svm *svm = to_svm(vcpu);
  1751. __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
  1752. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  1753. __free_page(virt_to_page(svm->nested.hsave));
  1754. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  1755. kvm_vcpu_uninit(vcpu);
  1756. kmem_cache_free(kvm_vcpu_cache, svm);
  1757. /*
  1758. * The vmcb page can be recycled, causing a false negative in
  1759. * svm_vcpu_load(). So do a full IBPB now.
  1760. */
  1761. indirect_branch_prediction_barrier();
  1762. }
  1763. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1764. {
  1765. struct vcpu_svm *svm = to_svm(vcpu);
  1766. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  1767. int i;
  1768. if (unlikely(cpu != vcpu->cpu)) {
  1769. svm->asid_generation = 0;
  1770. mark_all_dirty(svm->vmcb);
  1771. }
  1772. #ifdef CONFIG_X86_64
  1773. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  1774. #endif
  1775. savesegment(fs, svm->host.fs);
  1776. savesegment(gs, svm->host.gs);
  1777. svm->host.ldt = kvm_read_ldt();
  1778. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1779. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1780. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  1781. u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
  1782. if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
  1783. __this_cpu_write(current_tsc_ratio, tsc_ratio);
  1784. wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
  1785. }
  1786. }
  1787. /* This assumes that the kernel never uses MSR_TSC_AUX */
  1788. if (static_cpu_has(X86_FEATURE_RDTSCP))
  1789. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  1790. if (sd->current_vmcb != svm->vmcb) {
  1791. sd->current_vmcb = svm->vmcb;
  1792. indirect_branch_prediction_barrier();
  1793. }
  1794. avic_vcpu_load(vcpu, cpu);
  1795. }
  1796. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  1797. {
  1798. struct vcpu_svm *svm = to_svm(vcpu);
  1799. int i;
  1800. avic_vcpu_put(vcpu);
  1801. ++vcpu->stat.host_state_reload;
  1802. kvm_load_ldt(svm->host.ldt);
  1803. #ifdef CONFIG_X86_64
  1804. loadsegment(fs, svm->host.fs);
  1805. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
  1806. load_gs_index(svm->host.gs);
  1807. #else
  1808. #ifdef CONFIG_X86_32_LAZY_GS
  1809. loadsegment(gs, svm->host.gs);
  1810. #endif
  1811. #endif
  1812. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1813. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1814. }
  1815. static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
  1816. {
  1817. avic_set_running(vcpu, false);
  1818. }
  1819. static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
  1820. {
  1821. avic_set_running(vcpu, true);
  1822. }
  1823. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  1824. {
  1825. struct vcpu_svm *svm = to_svm(vcpu);
  1826. unsigned long rflags = svm->vmcb->save.rflags;
  1827. if (svm->nmi_singlestep) {
  1828. /* Hide our flags if they were not set by the guest */
  1829. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  1830. rflags &= ~X86_EFLAGS_TF;
  1831. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  1832. rflags &= ~X86_EFLAGS_RF;
  1833. }
  1834. return rflags;
  1835. }
  1836. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1837. {
  1838. if (to_svm(vcpu)->nmi_singlestep)
  1839. rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  1840. /*
  1841. * Any change of EFLAGS.VM is accompanied by a reload of SS
  1842. * (caused by either a task switch or an inter-privilege IRET),
  1843. * so we do not need to update the CPL here.
  1844. */
  1845. to_svm(vcpu)->vmcb->save.rflags = rflags;
  1846. }
  1847. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1848. {
  1849. switch (reg) {
  1850. case VCPU_EXREG_PDPTR:
  1851. BUG_ON(!npt_enabled);
  1852. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  1853. break;
  1854. default:
  1855. BUG();
  1856. }
  1857. }
  1858. static void svm_set_vintr(struct vcpu_svm *svm)
  1859. {
  1860. set_intercept(svm, INTERCEPT_VINTR);
  1861. }
  1862. static void svm_clear_vintr(struct vcpu_svm *svm)
  1863. {
  1864. clr_intercept(svm, INTERCEPT_VINTR);
  1865. }
  1866. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  1867. {
  1868. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1869. switch (seg) {
  1870. case VCPU_SREG_CS: return &save->cs;
  1871. case VCPU_SREG_DS: return &save->ds;
  1872. case VCPU_SREG_ES: return &save->es;
  1873. case VCPU_SREG_FS: return &save->fs;
  1874. case VCPU_SREG_GS: return &save->gs;
  1875. case VCPU_SREG_SS: return &save->ss;
  1876. case VCPU_SREG_TR: return &save->tr;
  1877. case VCPU_SREG_LDTR: return &save->ldtr;
  1878. }
  1879. BUG();
  1880. return NULL;
  1881. }
  1882. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1883. {
  1884. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1885. return s->base;
  1886. }
  1887. static void svm_get_segment(struct kvm_vcpu *vcpu,
  1888. struct kvm_segment *var, int seg)
  1889. {
  1890. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1891. var->base = s->base;
  1892. var->limit = s->limit;
  1893. var->selector = s->selector;
  1894. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  1895. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  1896. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1897. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  1898. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  1899. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  1900. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  1901. /*
  1902. * AMD CPUs circa 2014 track the G bit for all segments except CS.
  1903. * However, the SVM spec states that the G bit is not observed by the
  1904. * CPU, and some VMware virtual CPUs drop the G bit for all segments.
  1905. * So let's synthesize a legal G bit for all segments, this helps
  1906. * running KVM nested. It also helps cross-vendor migration, because
  1907. * Intel's vmentry has a check on the 'G' bit.
  1908. */
  1909. var->g = s->limit > 0xfffff;
  1910. /*
  1911. * AMD's VMCB does not have an explicit unusable field, so emulate it
  1912. * for cross vendor migration purposes by "not present"
  1913. */
  1914. var->unusable = !var->present;
  1915. switch (seg) {
  1916. case VCPU_SREG_TR:
  1917. /*
  1918. * Work around a bug where the busy flag in the tr selector
  1919. * isn't exposed
  1920. */
  1921. var->type |= 0x2;
  1922. break;
  1923. case VCPU_SREG_DS:
  1924. case VCPU_SREG_ES:
  1925. case VCPU_SREG_FS:
  1926. case VCPU_SREG_GS:
  1927. /*
  1928. * The accessed bit must always be set in the segment
  1929. * descriptor cache, although it can be cleared in the
  1930. * descriptor, the cached bit always remains at 1. Since
  1931. * Intel has a check on this, set it here to support
  1932. * cross-vendor migration.
  1933. */
  1934. if (!var->unusable)
  1935. var->type |= 0x1;
  1936. break;
  1937. case VCPU_SREG_SS:
  1938. /*
  1939. * On AMD CPUs sometimes the DB bit in the segment
  1940. * descriptor is left as 1, although the whole segment has
  1941. * been made unusable. Clear it here to pass an Intel VMX
  1942. * entry check when cross vendor migrating.
  1943. */
  1944. if (var->unusable)
  1945. var->db = 0;
  1946. /* This is symmetric with svm_set_segment() */
  1947. var->dpl = to_svm(vcpu)->vmcb->save.cpl;
  1948. break;
  1949. }
  1950. }
  1951. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1952. {
  1953. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1954. return save->cpl;
  1955. }
  1956. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1957. {
  1958. struct vcpu_svm *svm = to_svm(vcpu);
  1959. dt->size = svm->vmcb->save.idtr.limit;
  1960. dt->address = svm->vmcb->save.idtr.base;
  1961. }
  1962. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1963. {
  1964. struct vcpu_svm *svm = to_svm(vcpu);
  1965. svm->vmcb->save.idtr.limit = dt->size;
  1966. svm->vmcb->save.idtr.base = dt->address ;
  1967. mark_dirty(svm->vmcb, VMCB_DT);
  1968. }
  1969. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1970. {
  1971. struct vcpu_svm *svm = to_svm(vcpu);
  1972. dt->size = svm->vmcb->save.gdtr.limit;
  1973. dt->address = svm->vmcb->save.gdtr.base;
  1974. }
  1975. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1976. {
  1977. struct vcpu_svm *svm = to_svm(vcpu);
  1978. svm->vmcb->save.gdtr.limit = dt->size;
  1979. svm->vmcb->save.gdtr.base = dt->address ;
  1980. mark_dirty(svm->vmcb, VMCB_DT);
  1981. }
  1982. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1983. {
  1984. }
  1985. static void svm_decache_cr3(struct kvm_vcpu *vcpu)
  1986. {
  1987. }
  1988. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1989. {
  1990. }
  1991. static void update_cr0_intercept(struct vcpu_svm *svm)
  1992. {
  1993. ulong gcr0 = svm->vcpu.arch.cr0;
  1994. u64 *hcr0 = &svm->vmcb->save.cr0;
  1995. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1996. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1997. mark_dirty(svm->vmcb, VMCB_CR);
  1998. if (gcr0 == *hcr0) {
  1999. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  2000. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  2001. } else {
  2002. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  2003. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  2004. }
  2005. }
  2006. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  2007. {
  2008. struct vcpu_svm *svm = to_svm(vcpu);
  2009. #ifdef CONFIG_X86_64
  2010. if (vcpu->arch.efer & EFER_LME) {
  2011. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  2012. vcpu->arch.efer |= EFER_LMA;
  2013. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  2014. }
  2015. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  2016. vcpu->arch.efer &= ~EFER_LMA;
  2017. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  2018. }
  2019. }
  2020. #endif
  2021. vcpu->arch.cr0 = cr0;
  2022. if (!npt_enabled)
  2023. cr0 |= X86_CR0_PG | X86_CR0_WP;
  2024. /*
  2025. * re-enable caching here because the QEMU bios
  2026. * does not do it - this results in some delay at
  2027. * reboot
  2028. */
  2029. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  2030. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  2031. svm->vmcb->save.cr0 = cr0;
  2032. mark_dirty(svm->vmcb, VMCB_CR);
  2033. update_cr0_intercept(svm);
  2034. }
  2035. static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  2036. {
  2037. unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
  2038. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  2039. if (cr4 & X86_CR4_VMXE)
  2040. return 1;
  2041. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  2042. svm_flush_tlb(vcpu, true);
  2043. vcpu->arch.cr4 = cr4;
  2044. if (!npt_enabled)
  2045. cr4 |= X86_CR4_PAE;
  2046. cr4 |= host_cr4_mce;
  2047. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  2048. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  2049. return 0;
  2050. }
  2051. static void svm_set_segment(struct kvm_vcpu *vcpu,
  2052. struct kvm_segment *var, int seg)
  2053. {
  2054. struct vcpu_svm *svm = to_svm(vcpu);
  2055. struct vmcb_seg *s = svm_seg(vcpu, seg);
  2056. s->base = var->base;
  2057. s->limit = var->limit;
  2058. s->selector = var->selector;
  2059. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  2060. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  2061. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  2062. s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
  2063. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  2064. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  2065. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  2066. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  2067. /*
  2068. * This is always accurate, except if SYSRET returned to a segment
  2069. * with SS.DPL != 3. Intel does not have this quirk, and always
  2070. * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
  2071. * would entail passing the CPL to userspace and back.
  2072. */
  2073. if (seg == VCPU_SREG_SS)
  2074. /* This is symmetric with svm_get_segment() */
  2075. svm->vmcb->save.cpl = (var->dpl & 3);
  2076. mark_dirty(svm->vmcb, VMCB_SEG);
  2077. }
  2078. static void update_bp_intercept(struct kvm_vcpu *vcpu)
  2079. {
  2080. struct vcpu_svm *svm = to_svm(vcpu);
  2081. clr_exception_intercept(svm, BP_VECTOR);
  2082. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  2083. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  2084. set_exception_intercept(svm, BP_VECTOR);
  2085. } else
  2086. vcpu->guest_debug = 0;
  2087. }
  2088. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  2089. {
  2090. if (sd->next_asid > sd->max_asid) {
  2091. ++sd->asid_generation;
  2092. sd->next_asid = sd->min_asid;
  2093. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  2094. }
  2095. svm->asid_generation = sd->asid_generation;
  2096. svm->vmcb->control.asid = sd->next_asid++;
  2097. mark_dirty(svm->vmcb, VMCB_ASID);
  2098. }
  2099. static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
  2100. {
  2101. return to_svm(vcpu)->vmcb->save.dr6;
  2102. }
  2103. static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
  2104. {
  2105. struct vcpu_svm *svm = to_svm(vcpu);
  2106. svm->vmcb->save.dr6 = value;
  2107. mark_dirty(svm->vmcb, VMCB_DR);
  2108. }
  2109. static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  2110. {
  2111. struct vcpu_svm *svm = to_svm(vcpu);
  2112. get_debugreg(vcpu->arch.db[0], 0);
  2113. get_debugreg(vcpu->arch.db[1], 1);
  2114. get_debugreg(vcpu->arch.db[2], 2);
  2115. get_debugreg(vcpu->arch.db[3], 3);
  2116. vcpu->arch.dr6 = svm_get_dr6(vcpu);
  2117. vcpu->arch.dr7 = svm->vmcb->save.dr7;
  2118. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  2119. set_dr_intercepts(svm);
  2120. }
  2121. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  2122. {
  2123. struct vcpu_svm *svm = to_svm(vcpu);
  2124. svm->vmcb->save.dr7 = value;
  2125. mark_dirty(svm->vmcb, VMCB_DR);
  2126. }
  2127. static int pf_interception(struct vcpu_svm *svm)
  2128. {
  2129. u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
  2130. u64 error_code = svm->vmcb->control.exit_info_1;
  2131. return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
  2132. static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
  2133. svm->vmcb->control.insn_bytes : NULL,
  2134. svm->vmcb->control.insn_len);
  2135. }
  2136. static int npf_interception(struct vcpu_svm *svm)
  2137. {
  2138. u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
  2139. u64 error_code = svm->vmcb->control.exit_info_1;
  2140. trace_kvm_page_fault(fault_address, error_code);
  2141. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
  2142. static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
  2143. svm->vmcb->control.insn_bytes : NULL,
  2144. svm->vmcb->control.insn_len);
  2145. }
  2146. static int db_interception(struct vcpu_svm *svm)
  2147. {
  2148. struct kvm_run *kvm_run = svm->vcpu.run;
  2149. if (!(svm->vcpu.guest_debug &
  2150. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  2151. !svm->nmi_singlestep) {
  2152. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  2153. return 1;
  2154. }
  2155. if (svm->nmi_singlestep) {
  2156. disable_nmi_singlestep(svm);
  2157. }
  2158. if (svm->vcpu.guest_debug &
  2159. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  2160. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2161. kvm_run->debug.arch.pc =
  2162. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  2163. kvm_run->debug.arch.exception = DB_VECTOR;
  2164. return 0;
  2165. }
  2166. return 1;
  2167. }
  2168. static int bp_interception(struct vcpu_svm *svm)
  2169. {
  2170. struct kvm_run *kvm_run = svm->vcpu.run;
  2171. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2172. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  2173. kvm_run->debug.arch.exception = BP_VECTOR;
  2174. return 0;
  2175. }
  2176. static int ud_interception(struct vcpu_svm *svm)
  2177. {
  2178. return handle_ud(&svm->vcpu);
  2179. }
  2180. static int ac_interception(struct vcpu_svm *svm)
  2181. {
  2182. kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
  2183. return 1;
  2184. }
  2185. static int gp_interception(struct vcpu_svm *svm)
  2186. {
  2187. struct kvm_vcpu *vcpu = &svm->vcpu;
  2188. u32 error_code = svm->vmcb->control.exit_info_1;
  2189. int er;
  2190. WARN_ON_ONCE(!enable_vmware_backdoor);
  2191. er = kvm_emulate_instruction(vcpu,
  2192. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  2193. if (er == EMULATE_USER_EXIT)
  2194. return 0;
  2195. else if (er != EMULATE_DONE)
  2196. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  2197. return 1;
  2198. }
  2199. static bool is_erratum_383(void)
  2200. {
  2201. int err, i;
  2202. u64 value;
  2203. if (!erratum_383_found)
  2204. return false;
  2205. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  2206. if (err)
  2207. return false;
  2208. /* Bit 62 may or may not be set for this mce */
  2209. value &= ~(1ULL << 62);
  2210. if (value != 0xb600000000010015ULL)
  2211. return false;
  2212. /* Clear MCi_STATUS registers */
  2213. for (i = 0; i < 6; ++i)
  2214. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  2215. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  2216. if (!err) {
  2217. u32 low, high;
  2218. value &= ~(1ULL << 2);
  2219. low = lower_32_bits(value);
  2220. high = upper_32_bits(value);
  2221. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  2222. }
  2223. /* Flush tlb to evict multi-match entries */
  2224. __flush_tlb_all();
  2225. return true;
  2226. }
  2227. static void svm_handle_mce(struct vcpu_svm *svm)
  2228. {
  2229. if (is_erratum_383()) {
  2230. /*
  2231. * Erratum 383 triggered. Guest state is corrupt so kill the
  2232. * guest.
  2233. */
  2234. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  2235. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  2236. return;
  2237. }
  2238. /*
  2239. * On an #MC intercept the MCE handler is not called automatically in
  2240. * the host. So do it by hand here.
  2241. */
  2242. asm volatile (
  2243. "int $0x12\n");
  2244. /* not sure if we ever come back to this point */
  2245. return;
  2246. }
  2247. static int mc_interception(struct vcpu_svm *svm)
  2248. {
  2249. return 1;
  2250. }
  2251. static int shutdown_interception(struct vcpu_svm *svm)
  2252. {
  2253. struct kvm_run *kvm_run = svm->vcpu.run;
  2254. /*
  2255. * VMCB is undefined after a SHUTDOWN intercept
  2256. * so reinitialize it.
  2257. */
  2258. clear_page(svm->vmcb);
  2259. init_vmcb(svm);
  2260. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2261. return 0;
  2262. }
  2263. static int io_interception(struct vcpu_svm *svm)
  2264. {
  2265. struct kvm_vcpu *vcpu = &svm->vcpu;
  2266. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  2267. int size, in, string;
  2268. unsigned port;
  2269. ++svm->vcpu.stat.io_exits;
  2270. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  2271. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  2272. if (string)
  2273. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2274. port = io_info >> 16;
  2275. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  2276. svm->next_rip = svm->vmcb->control.exit_info_2;
  2277. return kvm_fast_pio(&svm->vcpu, size, port, in);
  2278. }
  2279. static int nmi_interception(struct vcpu_svm *svm)
  2280. {
  2281. return 1;
  2282. }
  2283. static int intr_interception(struct vcpu_svm *svm)
  2284. {
  2285. ++svm->vcpu.stat.irq_exits;
  2286. return 1;
  2287. }
  2288. static int nop_on_interception(struct vcpu_svm *svm)
  2289. {
  2290. return 1;
  2291. }
  2292. static int halt_interception(struct vcpu_svm *svm)
  2293. {
  2294. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  2295. return kvm_emulate_halt(&svm->vcpu);
  2296. }
  2297. static int vmmcall_interception(struct vcpu_svm *svm)
  2298. {
  2299. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2300. return kvm_emulate_hypercall(&svm->vcpu);
  2301. }
  2302. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  2303. {
  2304. struct vcpu_svm *svm = to_svm(vcpu);
  2305. return svm->nested.nested_cr3;
  2306. }
  2307. static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
  2308. {
  2309. struct vcpu_svm *svm = to_svm(vcpu);
  2310. u64 cr3 = svm->nested.nested_cr3;
  2311. u64 pdpte;
  2312. int ret;
  2313. ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
  2314. offset_in_page(cr3) + index * 8, 8);
  2315. if (ret)
  2316. return 0;
  2317. return pdpte;
  2318. }
  2319. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  2320. unsigned long root)
  2321. {
  2322. struct vcpu_svm *svm = to_svm(vcpu);
  2323. svm->vmcb->control.nested_cr3 = __sme_set(root);
  2324. mark_dirty(svm->vmcb, VMCB_NPT);
  2325. }
  2326. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  2327. struct x86_exception *fault)
  2328. {
  2329. struct vcpu_svm *svm = to_svm(vcpu);
  2330. if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
  2331. /*
  2332. * TODO: track the cause of the nested page fault, and
  2333. * correctly fill in the high bits of exit_info_1.
  2334. */
  2335. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  2336. svm->vmcb->control.exit_code_hi = 0;
  2337. svm->vmcb->control.exit_info_1 = (1ULL << 32);
  2338. svm->vmcb->control.exit_info_2 = fault->address;
  2339. }
  2340. svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
  2341. svm->vmcb->control.exit_info_1 |= fault->error_code;
  2342. /*
  2343. * The present bit is always zero for page structure faults on real
  2344. * hardware.
  2345. */
  2346. if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
  2347. svm->vmcb->control.exit_info_1 &= ~1;
  2348. nested_svm_vmexit(svm);
  2349. }
  2350. static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  2351. {
  2352. WARN_ON(mmu_is_nested(vcpu));
  2353. kvm_init_shadow_mmu(vcpu);
  2354. vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3;
  2355. vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3;
  2356. vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
  2357. vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
  2358. vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu);
  2359. reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
  2360. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  2361. }
  2362. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  2363. {
  2364. vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
  2365. }
  2366. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  2367. {
  2368. if (!(svm->vcpu.arch.efer & EFER_SVME) ||
  2369. !is_paging(&svm->vcpu)) {
  2370. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2371. return 1;
  2372. }
  2373. if (svm->vmcb->save.cpl) {
  2374. kvm_inject_gp(&svm->vcpu, 0);
  2375. return 1;
  2376. }
  2377. return 0;
  2378. }
  2379. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  2380. bool has_error_code, u32 error_code)
  2381. {
  2382. int vmexit;
  2383. if (!is_guest_mode(&svm->vcpu))
  2384. return 0;
  2385. vmexit = nested_svm_intercept(svm);
  2386. if (vmexit != NESTED_EXIT_DONE)
  2387. return 0;
  2388. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  2389. svm->vmcb->control.exit_code_hi = 0;
  2390. svm->vmcb->control.exit_info_1 = error_code;
  2391. /*
  2392. * EXITINFO2 is undefined for all exception intercepts other
  2393. * than #PF.
  2394. */
  2395. if (svm->vcpu.arch.exception.nested_apf)
  2396. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
  2397. else if (svm->vcpu.arch.exception.has_payload)
  2398. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
  2399. else
  2400. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  2401. svm->nested.exit_required = true;
  2402. return vmexit;
  2403. }
  2404. /* This function returns true if it is save to enable the irq window */
  2405. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  2406. {
  2407. if (!is_guest_mode(&svm->vcpu))
  2408. return true;
  2409. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  2410. return true;
  2411. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  2412. return false;
  2413. /*
  2414. * if vmexit was already requested (by intercepted exception
  2415. * for instance) do not overwrite it with "external interrupt"
  2416. * vmexit.
  2417. */
  2418. if (svm->nested.exit_required)
  2419. return false;
  2420. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  2421. svm->vmcb->control.exit_info_1 = 0;
  2422. svm->vmcb->control.exit_info_2 = 0;
  2423. if (svm->nested.intercept & 1ULL) {
  2424. /*
  2425. * The #vmexit can't be emulated here directly because this
  2426. * code path runs with irqs and preemption disabled. A
  2427. * #vmexit emulation might sleep. Only signal request for
  2428. * the #vmexit here.
  2429. */
  2430. svm->nested.exit_required = true;
  2431. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  2432. return false;
  2433. }
  2434. return true;
  2435. }
  2436. /* This function returns true if it is save to enable the nmi window */
  2437. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  2438. {
  2439. if (!is_guest_mode(&svm->vcpu))
  2440. return true;
  2441. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  2442. return true;
  2443. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  2444. svm->nested.exit_required = true;
  2445. return false;
  2446. }
  2447. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  2448. {
  2449. struct page *page;
  2450. might_sleep();
  2451. page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
  2452. if (is_error_page(page))
  2453. goto error;
  2454. *_page = page;
  2455. return kmap(page);
  2456. error:
  2457. kvm_inject_gp(&svm->vcpu, 0);
  2458. return NULL;
  2459. }
  2460. static void nested_svm_unmap(struct page *page)
  2461. {
  2462. kunmap(page);
  2463. kvm_release_page_dirty(page);
  2464. }
  2465. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  2466. {
  2467. unsigned port, size, iopm_len;
  2468. u16 val, mask;
  2469. u8 start_bit;
  2470. u64 gpa;
  2471. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  2472. return NESTED_EXIT_HOST;
  2473. port = svm->vmcb->control.exit_info_1 >> 16;
  2474. size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
  2475. SVM_IOIO_SIZE_SHIFT;
  2476. gpa = svm->nested.vmcb_iopm + (port / 8);
  2477. start_bit = port % 8;
  2478. iopm_len = (start_bit + size > 8) ? 2 : 1;
  2479. mask = (0xf >> (4 - size)) << start_bit;
  2480. val = 0;
  2481. if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
  2482. return NESTED_EXIT_DONE;
  2483. return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2484. }
  2485. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  2486. {
  2487. u32 offset, msr, value;
  2488. int write, mask;
  2489. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2490. return NESTED_EXIT_HOST;
  2491. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2492. offset = svm_msrpm_offset(msr);
  2493. write = svm->vmcb->control.exit_info_1 & 1;
  2494. mask = 1 << ((2 * (msr & 0xf)) + write);
  2495. if (offset == MSR_INVALID)
  2496. return NESTED_EXIT_DONE;
  2497. /* Offset is in 32 bit units but need in 8 bit units */
  2498. offset *= 4;
  2499. if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
  2500. return NESTED_EXIT_DONE;
  2501. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2502. }
  2503. /* DB exceptions for our internal use must not cause vmexit */
  2504. static int nested_svm_intercept_db(struct vcpu_svm *svm)
  2505. {
  2506. unsigned long dr6;
  2507. /* if we're not singlestepping, it's not ours */
  2508. if (!svm->nmi_singlestep)
  2509. return NESTED_EXIT_DONE;
  2510. /* if it's not a singlestep exception, it's not ours */
  2511. if (kvm_get_dr(&svm->vcpu, 6, &dr6))
  2512. return NESTED_EXIT_DONE;
  2513. if (!(dr6 & DR6_BS))
  2514. return NESTED_EXIT_DONE;
  2515. /* if the guest is singlestepping, it should get the vmexit */
  2516. if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
  2517. disable_nmi_singlestep(svm);
  2518. return NESTED_EXIT_DONE;
  2519. }
  2520. /* it's ours, the nested hypervisor must not see this one */
  2521. return NESTED_EXIT_HOST;
  2522. }
  2523. static int nested_svm_exit_special(struct vcpu_svm *svm)
  2524. {
  2525. u32 exit_code = svm->vmcb->control.exit_code;
  2526. switch (exit_code) {
  2527. case SVM_EXIT_INTR:
  2528. case SVM_EXIT_NMI:
  2529. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  2530. return NESTED_EXIT_HOST;
  2531. case SVM_EXIT_NPF:
  2532. /* For now we are always handling NPFs when using them */
  2533. if (npt_enabled)
  2534. return NESTED_EXIT_HOST;
  2535. break;
  2536. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  2537. /* When we're shadowing, trap PFs, but not async PF */
  2538. if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
  2539. return NESTED_EXIT_HOST;
  2540. break;
  2541. default:
  2542. break;
  2543. }
  2544. return NESTED_EXIT_CONTINUE;
  2545. }
  2546. /*
  2547. * If this function returns true, this #vmexit was already handled
  2548. */
  2549. static int nested_svm_intercept(struct vcpu_svm *svm)
  2550. {
  2551. u32 exit_code = svm->vmcb->control.exit_code;
  2552. int vmexit = NESTED_EXIT_HOST;
  2553. switch (exit_code) {
  2554. case SVM_EXIT_MSR:
  2555. vmexit = nested_svm_exit_handled_msr(svm);
  2556. break;
  2557. case SVM_EXIT_IOIO:
  2558. vmexit = nested_svm_intercept_ioio(svm);
  2559. break;
  2560. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  2561. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  2562. if (svm->nested.intercept_cr & bit)
  2563. vmexit = NESTED_EXIT_DONE;
  2564. break;
  2565. }
  2566. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  2567. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  2568. if (svm->nested.intercept_dr & bit)
  2569. vmexit = NESTED_EXIT_DONE;
  2570. break;
  2571. }
  2572. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  2573. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  2574. if (svm->nested.intercept_exceptions & excp_bits) {
  2575. if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
  2576. vmexit = nested_svm_intercept_db(svm);
  2577. else
  2578. vmexit = NESTED_EXIT_DONE;
  2579. }
  2580. /* async page fault always cause vmexit */
  2581. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  2582. svm->vcpu.arch.exception.nested_apf != 0)
  2583. vmexit = NESTED_EXIT_DONE;
  2584. break;
  2585. }
  2586. case SVM_EXIT_ERR: {
  2587. vmexit = NESTED_EXIT_DONE;
  2588. break;
  2589. }
  2590. default: {
  2591. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  2592. if (svm->nested.intercept & exit_bits)
  2593. vmexit = NESTED_EXIT_DONE;
  2594. }
  2595. }
  2596. return vmexit;
  2597. }
  2598. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  2599. {
  2600. int vmexit;
  2601. vmexit = nested_svm_intercept(svm);
  2602. if (vmexit == NESTED_EXIT_DONE)
  2603. nested_svm_vmexit(svm);
  2604. return vmexit;
  2605. }
  2606. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  2607. {
  2608. struct vmcb_control_area *dst = &dst_vmcb->control;
  2609. struct vmcb_control_area *from = &from_vmcb->control;
  2610. dst->intercept_cr = from->intercept_cr;
  2611. dst->intercept_dr = from->intercept_dr;
  2612. dst->intercept_exceptions = from->intercept_exceptions;
  2613. dst->intercept = from->intercept;
  2614. dst->iopm_base_pa = from->iopm_base_pa;
  2615. dst->msrpm_base_pa = from->msrpm_base_pa;
  2616. dst->tsc_offset = from->tsc_offset;
  2617. dst->asid = from->asid;
  2618. dst->tlb_ctl = from->tlb_ctl;
  2619. dst->int_ctl = from->int_ctl;
  2620. dst->int_vector = from->int_vector;
  2621. dst->int_state = from->int_state;
  2622. dst->exit_code = from->exit_code;
  2623. dst->exit_code_hi = from->exit_code_hi;
  2624. dst->exit_info_1 = from->exit_info_1;
  2625. dst->exit_info_2 = from->exit_info_2;
  2626. dst->exit_int_info = from->exit_int_info;
  2627. dst->exit_int_info_err = from->exit_int_info_err;
  2628. dst->nested_ctl = from->nested_ctl;
  2629. dst->event_inj = from->event_inj;
  2630. dst->event_inj_err = from->event_inj_err;
  2631. dst->nested_cr3 = from->nested_cr3;
  2632. dst->virt_ext = from->virt_ext;
  2633. }
  2634. static int nested_svm_vmexit(struct vcpu_svm *svm)
  2635. {
  2636. struct vmcb *nested_vmcb;
  2637. struct vmcb *hsave = svm->nested.hsave;
  2638. struct vmcb *vmcb = svm->vmcb;
  2639. struct page *page;
  2640. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  2641. vmcb->control.exit_info_1,
  2642. vmcb->control.exit_info_2,
  2643. vmcb->control.exit_int_info,
  2644. vmcb->control.exit_int_info_err,
  2645. KVM_ISA_SVM);
  2646. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  2647. if (!nested_vmcb)
  2648. return 1;
  2649. /* Exit Guest-Mode */
  2650. leave_guest_mode(&svm->vcpu);
  2651. svm->nested.vmcb = 0;
  2652. /* Give the current vmcb to the guest */
  2653. disable_gif(svm);
  2654. nested_vmcb->save.es = vmcb->save.es;
  2655. nested_vmcb->save.cs = vmcb->save.cs;
  2656. nested_vmcb->save.ss = vmcb->save.ss;
  2657. nested_vmcb->save.ds = vmcb->save.ds;
  2658. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  2659. nested_vmcb->save.idtr = vmcb->save.idtr;
  2660. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  2661. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2662. nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2663. nested_vmcb->save.cr2 = vmcb->save.cr2;
  2664. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  2665. nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
  2666. nested_vmcb->save.rip = vmcb->save.rip;
  2667. nested_vmcb->save.rsp = vmcb->save.rsp;
  2668. nested_vmcb->save.rax = vmcb->save.rax;
  2669. nested_vmcb->save.dr7 = vmcb->save.dr7;
  2670. nested_vmcb->save.dr6 = vmcb->save.dr6;
  2671. nested_vmcb->save.cpl = vmcb->save.cpl;
  2672. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  2673. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  2674. nested_vmcb->control.int_state = vmcb->control.int_state;
  2675. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  2676. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  2677. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  2678. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  2679. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  2680. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  2681. if (svm->nrips_enabled)
  2682. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  2683. /*
  2684. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  2685. * to make sure that we do not lose injected events. So check event_inj
  2686. * here and copy it to exit_int_info if it is valid.
  2687. * Exit_int_info and event_inj can't be both valid because the case
  2688. * below only happens on a VMRUN instruction intercept which has
  2689. * no valid exit_int_info set.
  2690. */
  2691. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  2692. struct vmcb_control_area *nc = &nested_vmcb->control;
  2693. nc->exit_int_info = vmcb->control.event_inj;
  2694. nc->exit_int_info_err = vmcb->control.event_inj_err;
  2695. }
  2696. nested_vmcb->control.tlb_ctl = 0;
  2697. nested_vmcb->control.event_inj = 0;
  2698. nested_vmcb->control.event_inj_err = 0;
  2699. /* We always set V_INTR_MASKING and remember the old value in hflags */
  2700. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  2701. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  2702. /* Restore the original control entries */
  2703. copy_vmcb_control_area(vmcb, hsave);
  2704. svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
  2705. kvm_clear_exception_queue(&svm->vcpu);
  2706. kvm_clear_interrupt_queue(&svm->vcpu);
  2707. svm->nested.nested_cr3 = 0;
  2708. /* Restore selected save entries */
  2709. svm->vmcb->save.es = hsave->save.es;
  2710. svm->vmcb->save.cs = hsave->save.cs;
  2711. svm->vmcb->save.ss = hsave->save.ss;
  2712. svm->vmcb->save.ds = hsave->save.ds;
  2713. svm->vmcb->save.gdtr = hsave->save.gdtr;
  2714. svm->vmcb->save.idtr = hsave->save.idtr;
  2715. kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
  2716. svm_set_efer(&svm->vcpu, hsave->save.efer);
  2717. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  2718. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  2719. if (npt_enabled) {
  2720. svm->vmcb->save.cr3 = hsave->save.cr3;
  2721. svm->vcpu.arch.cr3 = hsave->save.cr3;
  2722. } else {
  2723. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  2724. }
  2725. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  2726. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  2727. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  2728. svm->vmcb->save.dr7 = 0;
  2729. svm->vmcb->save.cpl = 0;
  2730. svm->vmcb->control.exit_int_info = 0;
  2731. mark_all_dirty(svm->vmcb);
  2732. nested_svm_unmap(page);
  2733. nested_svm_uninit_mmu_context(&svm->vcpu);
  2734. kvm_mmu_reset_context(&svm->vcpu);
  2735. kvm_mmu_load(&svm->vcpu);
  2736. return 0;
  2737. }
  2738. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  2739. {
  2740. /*
  2741. * This function merges the msr permission bitmaps of kvm and the
  2742. * nested vmcb. It is optimized in that it only merges the parts where
  2743. * the kvm msr permission bitmap may contain zero bits
  2744. */
  2745. int i;
  2746. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2747. return true;
  2748. for (i = 0; i < MSRPM_OFFSETS; i++) {
  2749. u32 value, p;
  2750. u64 offset;
  2751. if (msrpm_offsets[i] == 0xffffffff)
  2752. break;
  2753. p = msrpm_offsets[i];
  2754. offset = svm->nested.vmcb_msrpm + (p * 4);
  2755. if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
  2756. return false;
  2757. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  2758. }
  2759. svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
  2760. return true;
  2761. }
  2762. static bool nested_vmcb_checks(struct vmcb *vmcb)
  2763. {
  2764. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  2765. return false;
  2766. if (vmcb->control.asid == 0)
  2767. return false;
  2768. if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
  2769. !npt_enabled)
  2770. return false;
  2771. return true;
  2772. }
  2773. static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
  2774. struct vmcb *nested_vmcb, struct page *page)
  2775. {
  2776. if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
  2777. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  2778. else
  2779. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  2780. if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
  2781. kvm_mmu_unload(&svm->vcpu);
  2782. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  2783. nested_svm_init_mmu_context(&svm->vcpu);
  2784. }
  2785. /* Load the nested guest state */
  2786. svm->vmcb->save.es = nested_vmcb->save.es;
  2787. svm->vmcb->save.cs = nested_vmcb->save.cs;
  2788. svm->vmcb->save.ss = nested_vmcb->save.ss;
  2789. svm->vmcb->save.ds = nested_vmcb->save.ds;
  2790. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  2791. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  2792. kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
  2793. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  2794. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  2795. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  2796. if (npt_enabled) {
  2797. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  2798. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  2799. } else
  2800. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  2801. /* Guest paging mode is active - reset mmu */
  2802. kvm_mmu_reset_context(&svm->vcpu);
  2803. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  2804. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  2805. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  2806. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  2807. /* In case we don't even reach vcpu_run, the fields are not updated */
  2808. svm->vmcb->save.rax = nested_vmcb->save.rax;
  2809. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  2810. svm->vmcb->save.rip = nested_vmcb->save.rip;
  2811. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  2812. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  2813. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  2814. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  2815. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  2816. /* cache intercepts */
  2817. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  2818. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  2819. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  2820. svm->nested.intercept = nested_vmcb->control.intercept;
  2821. svm_flush_tlb(&svm->vcpu, true);
  2822. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  2823. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  2824. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  2825. else
  2826. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  2827. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  2828. /* We only want the cr8 intercept bits of the guest */
  2829. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  2830. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2831. }
  2832. /* We don't want to see VMMCALLs from a nested guest */
  2833. clr_intercept(svm, INTERCEPT_VMMCALL);
  2834. svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
  2835. svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
  2836. svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
  2837. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  2838. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  2839. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  2840. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  2841. nested_svm_unmap(page);
  2842. /* Enter Guest-Mode */
  2843. enter_guest_mode(&svm->vcpu);
  2844. /*
  2845. * Merge guest and host intercepts - must be called with vcpu in
  2846. * guest-mode to take affect here
  2847. */
  2848. recalc_intercepts(svm);
  2849. svm->nested.vmcb = vmcb_gpa;
  2850. enable_gif(svm);
  2851. mark_all_dirty(svm->vmcb);
  2852. }
  2853. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  2854. {
  2855. struct vmcb *nested_vmcb;
  2856. struct vmcb *hsave = svm->nested.hsave;
  2857. struct vmcb *vmcb = svm->vmcb;
  2858. struct page *page;
  2859. u64 vmcb_gpa;
  2860. vmcb_gpa = svm->vmcb->save.rax;
  2861. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2862. if (!nested_vmcb)
  2863. return false;
  2864. if (!nested_vmcb_checks(nested_vmcb)) {
  2865. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  2866. nested_vmcb->control.exit_code_hi = 0;
  2867. nested_vmcb->control.exit_info_1 = 0;
  2868. nested_vmcb->control.exit_info_2 = 0;
  2869. nested_svm_unmap(page);
  2870. return false;
  2871. }
  2872. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  2873. nested_vmcb->save.rip,
  2874. nested_vmcb->control.int_ctl,
  2875. nested_vmcb->control.event_inj,
  2876. nested_vmcb->control.nested_ctl);
  2877. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  2878. nested_vmcb->control.intercept_cr >> 16,
  2879. nested_vmcb->control.intercept_exceptions,
  2880. nested_vmcb->control.intercept);
  2881. /* Clear internal status */
  2882. kvm_clear_exception_queue(&svm->vcpu);
  2883. kvm_clear_interrupt_queue(&svm->vcpu);
  2884. /*
  2885. * Save the old vmcb, so we don't need to pick what we save, but can
  2886. * restore everything when a VMEXIT occurs
  2887. */
  2888. hsave->save.es = vmcb->save.es;
  2889. hsave->save.cs = vmcb->save.cs;
  2890. hsave->save.ss = vmcb->save.ss;
  2891. hsave->save.ds = vmcb->save.ds;
  2892. hsave->save.gdtr = vmcb->save.gdtr;
  2893. hsave->save.idtr = vmcb->save.idtr;
  2894. hsave->save.efer = svm->vcpu.arch.efer;
  2895. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2896. hsave->save.cr4 = svm->vcpu.arch.cr4;
  2897. hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
  2898. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  2899. hsave->save.rsp = vmcb->save.rsp;
  2900. hsave->save.rax = vmcb->save.rax;
  2901. if (npt_enabled)
  2902. hsave->save.cr3 = vmcb->save.cr3;
  2903. else
  2904. hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2905. copy_vmcb_control_area(hsave, vmcb);
  2906. enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
  2907. return true;
  2908. }
  2909. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  2910. {
  2911. to_vmcb->save.fs = from_vmcb->save.fs;
  2912. to_vmcb->save.gs = from_vmcb->save.gs;
  2913. to_vmcb->save.tr = from_vmcb->save.tr;
  2914. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  2915. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  2916. to_vmcb->save.star = from_vmcb->save.star;
  2917. to_vmcb->save.lstar = from_vmcb->save.lstar;
  2918. to_vmcb->save.cstar = from_vmcb->save.cstar;
  2919. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  2920. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  2921. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  2922. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  2923. }
  2924. static int vmload_interception(struct vcpu_svm *svm)
  2925. {
  2926. struct vmcb *nested_vmcb;
  2927. struct page *page;
  2928. int ret;
  2929. if (nested_svm_check_permissions(svm))
  2930. return 1;
  2931. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2932. if (!nested_vmcb)
  2933. return 1;
  2934. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2935. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2936. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  2937. nested_svm_unmap(page);
  2938. return ret;
  2939. }
  2940. static int vmsave_interception(struct vcpu_svm *svm)
  2941. {
  2942. struct vmcb *nested_vmcb;
  2943. struct page *page;
  2944. int ret;
  2945. if (nested_svm_check_permissions(svm))
  2946. return 1;
  2947. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2948. if (!nested_vmcb)
  2949. return 1;
  2950. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2951. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2952. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  2953. nested_svm_unmap(page);
  2954. return ret;
  2955. }
  2956. static int vmrun_interception(struct vcpu_svm *svm)
  2957. {
  2958. if (nested_svm_check_permissions(svm))
  2959. return 1;
  2960. /* Save rip after vmrun instruction */
  2961. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  2962. if (!nested_svm_vmrun(svm))
  2963. return 1;
  2964. if (!nested_svm_vmrun_msrpm(svm))
  2965. goto failed;
  2966. return 1;
  2967. failed:
  2968. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  2969. svm->vmcb->control.exit_code_hi = 0;
  2970. svm->vmcb->control.exit_info_1 = 0;
  2971. svm->vmcb->control.exit_info_2 = 0;
  2972. nested_svm_vmexit(svm);
  2973. return 1;
  2974. }
  2975. static int stgi_interception(struct vcpu_svm *svm)
  2976. {
  2977. int ret;
  2978. if (nested_svm_check_permissions(svm))
  2979. return 1;
  2980. /*
  2981. * If VGIF is enabled, the STGI intercept is only added to
  2982. * detect the opening of the SMI/NMI window; remove it now.
  2983. */
  2984. if (vgif_enabled(svm))
  2985. clr_intercept(svm, INTERCEPT_STGI);
  2986. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2987. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2988. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2989. enable_gif(svm);
  2990. return ret;
  2991. }
  2992. static int clgi_interception(struct vcpu_svm *svm)
  2993. {
  2994. int ret;
  2995. if (nested_svm_check_permissions(svm))
  2996. return 1;
  2997. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2998. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2999. disable_gif(svm);
  3000. /* After a CLGI no interrupts should come */
  3001. if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
  3002. svm_clear_vintr(svm);
  3003. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  3004. mark_dirty(svm->vmcb, VMCB_INTR);
  3005. }
  3006. return ret;
  3007. }
  3008. static int invlpga_interception(struct vcpu_svm *svm)
  3009. {
  3010. struct kvm_vcpu *vcpu = &svm->vcpu;
  3011. trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
  3012. kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  3013. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  3014. kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  3015. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  3016. return kvm_skip_emulated_instruction(&svm->vcpu);
  3017. }
  3018. static int skinit_interception(struct vcpu_svm *svm)
  3019. {
  3020. trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  3021. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  3022. return 1;
  3023. }
  3024. static int wbinvd_interception(struct vcpu_svm *svm)
  3025. {
  3026. return kvm_emulate_wbinvd(&svm->vcpu);
  3027. }
  3028. static int xsetbv_interception(struct vcpu_svm *svm)
  3029. {
  3030. u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
  3031. u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  3032. if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
  3033. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  3034. return kvm_skip_emulated_instruction(&svm->vcpu);
  3035. }
  3036. return 1;
  3037. }
  3038. static int task_switch_interception(struct vcpu_svm *svm)
  3039. {
  3040. u16 tss_selector;
  3041. int reason;
  3042. int int_type = svm->vmcb->control.exit_int_info &
  3043. SVM_EXITINTINFO_TYPE_MASK;
  3044. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  3045. uint32_t type =
  3046. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  3047. uint32_t idt_v =
  3048. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  3049. bool has_error_code = false;
  3050. u32 error_code = 0;
  3051. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  3052. if (svm->vmcb->control.exit_info_2 &
  3053. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  3054. reason = TASK_SWITCH_IRET;
  3055. else if (svm->vmcb->control.exit_info_2 &
  3056. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  3057. reason = TASK_SWITCH_JMP;
  3058. else if (idt_v)
  3059. reason = TASK_SWITCH_GATE;
  3060. else
  3061. reason = TASK_SWITCH_CALL;
  3062. if (reason == TASK_SWITCH_GATE) {
  3063. switch (type) {
  3064. case SVM_EXITINTINFO_TYPE_NMI:
  3065. svm->vcpu.arch.nmi_injected = false;
  3066. break;
  3067. case SVM_EXITINTINFO_TYPE_EXEPT:
  3068. if (svm->vmcb->control.exit_info_2 &
  3069. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  3070. has_error_code = true;
  3071. error_code =
  3072. (u32)svm->vmcb->control.exit_info_2;
  3073. }
  3074. kvm_clear_exception_queue(&svm->vcpu);
  3075. break;
  3076. case SVM_EXITINTINFO_TYPE_INTR:
  3077. kvm_clear_interrupt_queue(&svm->vcpu);
  3078. break;
  3079. default:
  3080. break;
  3081. }
  3082. }
  3083. if (reason != TASK_SWITCH_GATE ||
  3084. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  3085. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  3086. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  3087. skip_emulated_instruction(&svm->vcpu);
  3088. if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
  3089. int_vec = -1;
  3090. if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
  3091. has_error_code, error_code) == EMULATE_FAIL) {
  3092. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3093. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3094. svm->vcpu.run->internal.ndata = 0;
  3095. return 0;
  3096. }
  3097. return 1;
  3098. }
  3099. static int cpuid_interception(struct vcpu_svm *svm)
  3100. {
  3101. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  3102. return kvm_emulate_cpuid(&svm->vcpu);
  3103. }
  3104. static int iret_interception(struct vcpu_svm *svm)
  3105. {
  3106. ++svm->vcpu.stat.nmi_window_exits;
  3107. clr_intercept(svm, INTERCEPT_IRET);
  3108. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  3109. svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
  3110. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3111. return 1;
  3112. }
  3113. static int invlpg_interception(struct vcpu_svm *svm)
  3114. {
  3115. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  3116. return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  3117. kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
  3118. return kvm_skip_emulated_instruction(&svm->vcpu);
  3119. }
  3120. static int emulate_on_interception(struct vcpu_svm *svm)
  3121. {
  3122. return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  3123. }
  3124. static int rsm_interception(struct vcpu_svm *svm)
  3125. {
  3126. return kvm_emulate_instruction_from_buffer(&svm->vcpu,
  3127. rsm_ins_bytes, 2) == EMULATE_DONE;
  3128. }
  3129. static int rdpmc_interception(struct vcpu_svm *svm)
  3130. {
  3131. int err;
  3132. if (!static_cpu_has(X86_FEATURE_NRIPS))
  3133. return emulate_on_interception(svm);
  3134. err = kvm_rdpmc(&svm->vcpu);
  3135. return kvm_complete_insn_gp(&svm->vcpu, err);
  3136. }
  3137. static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
  3138. unsigned long val)
  3139. {
  3140. unsigned long cr0 = svm->vcpu.arch.cr0;
  3141. bool ret = false;
  3142. u64 intercept;
  3143. intercept = svm->nested.intercept;
  3144. if (!is_guest_mode(&svm->vcpu) ||
  3145. (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
  3146. return false;
  3147. cr0 &= ~SVM_CR0_SELECTIVE_MASK;
  3148. val &= ~SVM_CR0_SELECTIVE_MASK;
  3149. if (cr0 ^ val) {
  3150. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  3151. ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
  3152. }
  3153. return ret;
  3154. }
  3155. #define CR_VALID (1ULL << 63)
  3156. static int cr_interception(struct vcpu_svm *svm)
  3157. {
  3158. int reg, cr;
  3159. unsigned long val;
  3160. int err;
  3161. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  3162. return emulate_on_interception(svm);
  3163. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  3164. return emulate_on_interception(svm);
  3165. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  3166. if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
  3167. cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
  3168. else
  3169. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  3170. err = 0;
  3171. if (cr >= 16) { /* mov to cr */
  3172. cr -= 16;
  3173. val = kvm_register_read(&svm->vcpu, reg);
  3174. switch (cr) {
  3175. case 0:
  3176. if (!check_selective_cr0_intercepted(svm, val))
  3177. err = kvm_set_cr0(&svm->vcpu, val);
  3178. else
  3179. return 1;
  3180. break;
  3181. case 3:
  3182. err = kvm_set_cr3(&svm->vcpu, val);
  3183. break;
  3184. case 4:
  3185. err = kvm_set_cr4(&svm->vcpu, val);
  3186. break;
  3187. case 8:
  3188. err = kvm_set_cr8(&svm->vcpu, val);
  3189. break;
  3190. default:
  3191. WARN(1, "unhandled write to CR%d", cr);
  3192. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  3193. return 1;
  3194. }
  3195. } else { /* mov from cr */
  3196. switch (cr) {
  3197. case 0:
  3198. val = kvm_read_cr0(&svm->vcpu);
  3199. break;
  3200. case 2:
  3201. val = svm->vcpu.arch.cr2;
  3202. break;
  3203. case 3:
  3204. val = kvm_read_cr3(&svm->vcpu);
  3205. break;
  3206. case 4:
  3207. val = kvm_read_cr4(&svm->vcpu);
  3208. break;
  3209. case 8:
  3210. val = kvm_get_cr8(&svm->vcpu);
  3211. break;
  3212. default:
  3213. WARN(1, "unhandled read from CR%d", cr);
  3214. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  3215. return 1;
  3216. }
  3217. kvm_register_write(&svm->vcpu, reg, val);
  3218. }
  3219. return kvm_complete_insn_gp(&svm->vcpu, err);
  3220. }
  3221. static int dr_interception(struct vcpu_svm *svm)
  3222. {
  3223. int reg, dr;
  3224. unsigned long val;
  3225. if (svm->vcpu.guest_debug == 0) {
  3226. /*
  3227. * No more DR vmexits; force a reload of the debug registers
  3228. * and reenter on this instruction. The next vmexit will
  3229. * retrieve the full state of the debug registers.
  3230. */
  3231. clr_dr_intercepts(svm);
  3232. svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  3233. return 1;
  3234. }
  3235. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  3236. return emulate_on_interception(svm);
  3237. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  3238. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  3239. if (dr >= 16) { /* mov to DRn */
  3240. if (!kvm_require_dr(&svm->vcpu, dr - 16))
  3241. return 1;
  3242. val = kvm_register_read(&svm->vcpu, reg);
  3243. kvm_set_dr(&svm->vcpu, dr - 16, val);
  3244. } else {
  3245. if (!kvm_require_dr(&svm->vcpu, dr))
  3246. return 1;
  3247. kvm_get_dr(&svm->vcpu, dr, &val);
  3248. kvm_register_write(&svm->vcpu, reg, val);
  3249. }
  3250. return kvm_skip_emulated_instruction(&svm->vcpu);
  3251. }
  3252. static int cr8_write_interception(struct vcpu_svm *svm)
  3253. {
  3254. struct kvm_run *kvm_run = svm->vcpu.run;
  3255. int r;
  3256. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  3257. /* instruction emulation calls kvm_set_cr8() */
  3258. r = cr_interception(svm);
  3259. if (lapic_in_kernel(&svm->vcpu))
  3260. return r;
  3261. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  3262. return r;
  3263. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  3264. return 0;
  3265. }
  3266. static int svm_get_msr_feature(struct kvm_msr_entry *msr)
  3267. {
  3268. msr->data = 0;
  3269. switch (msr->index) {
  3270. case MSR_F10H_DECFG:
  3271. if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
  3272. msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
  3273. break;
  3274. default:
  3275. return 1;
  3276. }
  3277. return 0;
  3278. }
  3279. static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3280. {
  3281. struct vcpu_svm *svm = to_svm(vcpu);
  3282. switch (msr_info->index) {
  3283. case MSR_STAR:
  3284. msr_info->data = svm->vmcb->save.star;
  3285. break;
  3286. #ifdef CONFIG_X86_64
  3287. case MSR_LSTAR:
  3288. msr_info->data = svm->vmcb->save.lstar;
  3289. break;
  3290. case MSR_CSTAR:
  3291. msr_info->data = svm->vmcb->save.cstar;
  3292. break;
  3293. case MSR_KERNEL_GS_BASE:
  3294. msr_info->data = svm->vmcb->save.kernel_gs_base;
  3295. break;
  3296. case MSR_SYSCALL_MASK:
  3297. msr_info->data = svm->vmcb->save.sfmask;
  3298. break;
  3299. #endif
  3300. case MSR_IA32_SYSENTER_CS:
  3301. msr_info->data = svm->vmcb->save.sysenter_cs;
  3302. break;
  3303. case MSR_IA32_SYSENTER_EIP:
  3304. msr_info->data = svm->sysenter_eip;
  3305. break;
  3306. case MSR_IA32_SYSENTER_ESP:
  3307. msr_info->data = svm->sysenter_esp;
  3308. break;
  3309. case MSR_TSC_AUX:
  3310. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  3311. return 1;
  3312. msr_info->data = svm->tsc_aux;
  3313. break;
  3314. /*
  3315. * Nobody will change the following 5 values in the VMCB so we can
  3316. * safely return them on rdmsr. They will always be 0 until LBRV is
  3317. * implemented.
  3318. */
  3319. case MSR_IA32_DEBUGCTLMSR:
  3320. msr_info->data = svm->vmcb->save.dbgctl;
  3321. break;
  3322. case MSR_IA32_LASTBRANCHFROMIP:
  3323. msr_info->data = svm->vmcb->save.br_from;
  3324. break;
  3325. case MSR_IA32_LASTBRANCHTOIP:
  3326. msr_info->data = svm->vmcb->save.br_to;
  3327. break;
  3328. case MSR_IA32_LASTINTFROMIP:
  3329. msr_info->data = svm->vmcb->save.last_excp_from;
  3330. break;
  3331. case MSR_IA32_LASTINTTOIP:
  3332. msr_info->data = svm->vmcb->save.last_excp_to;
  3333. break;
  3334. case MSR_VM_HSAVE_PA:
  3335. msr_info->data = svm->nested.hsave_msr;
  3336. break;
  3337. case MSR_VM_CR:
  3338. msr_info->data = svm->nested.vm_cr_msr;
  3339. break;
  3340. case MSR_IA32_SPEC_CTRL:
  3341. if (!msr_info->host_initiated &&
  3342. !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
  3343. !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
  3344. return 1;
  3345. msr_info->data = svm->spec_ctrl;
  3346. break;
  3347. case MSR_AMD64_VIRT_SPEC_CTRL:
  3348. if (!msr_info->host_initiated &&
  3349. !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
  3350. return 1;
  3351. msr_info->data = svm->virt_spec_ctrl;
  3352. break;
  3353. case MSR_F15H_IC_CFG: {
  3354. int family, model;
  3355. family = guest_cpuid_family(vcpu);
  3356. model = guest_cpuid_model(vcpu);
  3357. if (family < 0 || model < 0)
  3358. return kvm_get_msr_common(vcpu, msr_info);
  3359. msr_info->data = 0;
  3360. if (family == 0x15 &&
  3361. (model >= 0x2 && model < 0x20))
  3362. msr_info->data = 0x1E;
  3363. }
  3364. break;
  3365. case MSR_F10H_DECFG:
  3366. msr_info->data = svm->msr_decfg;
  3367. break;
  3368. default:
  3369. return kvm_get_msr_common(vcpu, msr_info);
  3370. }
  3371. return 0;
  3372. }
  3373. static int rdmsr_interception(struct vcpu_svm *svm)
  3374. {
  3375. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  3376. struct msr_data msr_info;
  3377. msr_info.index = ecx;
  3378. msr_info.host_initiated = false;
  3379. if (svm_get_msr(&svm->vcpu, &msr_info)) {
  3380. trace_kvm_msr_read_ex(ecx);
  3381. kvm_inject_gp(&svm->vcpu, 0);
  3382. return 1;
  3383. } else {
  3384. trace_kvm_msr_read(ecx, msr_info.data);
  3385. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
  3386. msr_info.data & 0xffffffff);
  3387. kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
  3388. msr_info.data >> 32);
  3389. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  3390. return kvm_skip_emulated_instruction(&svm->vcpu);
  3391. }
  3392. }
  3393. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  3394. {
  3395. struct vcpu_svm *svm = to_svm(vcpu);
  3396. int svm_dis, chg_mask;
  3397. if (data & ~SVM_VM_CR_VALID_MASK)
  3398. return 1;
  3399. chg_mask = SVM_VM_CR_VALID_MASK;
  3400. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  3401. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  3402. svm->nested.vm_cr_msr &= ~chg_mask;
  3403. svm->nested.vm_cr_msr |= (data & chg_mask);
  3404. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  3405. /* check for svm_disable while efer.svme is set */
  3406. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  3407. return 1;
  3408. return 0;
  3409. }
  3410. static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  3411. {
  3412. struct vcpu_svm *svm = to_svm(vcpu);
  3413. u32 ecx = msr->index;
  3414. u64 data = msr->data;
  3415. switch (ecx) {
  3416. case MSR_IA32_CR_PAT:
  3417. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3418. return 1;
  3419. vcpu->arch.pat = data;
  3420. svm->vmcb->save.g_pat = data;
  3421. mark_dirty(svm->vmcb, VMCB_NPT);
  3422. break;
  3423. case MSR_IA32_SPEC_CTRL:
  3424. if (!msr->host_initiated &&
  3425. !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
  3426. !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
  3427. return 1;
  3428. /* The STIBP bit doesn't fault even if it's not advertised */
  3429. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3430. return 1;
  3431. svm->spec_ctrl = data;
  3432. if (!data)
  3433. break;
  3434. /*
  3435. * For non-nested:
  3436. * When it's written (to non-zero) for the first time, pass
  3437. * it through.
  3438. *
  3439. * For nested:
  3440. * The handling of the MSR bitmap for L2 guests is done in
  3441. * nested_svm_vmrun_msrpm.
  3442. * We update the L1 MSR bit as well since it will end up
  3443. * touching the MSR anyway now.
  3444. */
  3445. set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
  3446. break;
  3447. case MSR_IA32_PRED_CMD:
  3448. if (!msr->host_initiated &&
  3449. !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
  3450. return 1;
  3451. if (data & ~PRED_CMD_IBPB)
  3452. return 1;
  3453. if (!data)
  3454. break;
  3455. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3456. if (is_guest_mode(vcpu))
  3457. break;
  3458. set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
  3459. break;
  3460. case MSR_AMD64_VIRT_SPEC_CTRL:
  3461. if (!msr->host_initiated &&
  3462. !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
  3463. return 1;
  3464. if (data & ~SPEC_CTRL_SSBD)
  3465. return 1;
  3466. svm->virt_spec_ctrl = data;
  3467. break;
  3468. case MSR_STAR:
  3469. svm->vmcb->save.star = data;
  3470. break;
  3471. #ifdef CONFIG_X86_64
  3472. case MSR_LSTAR:
  3473. svm->vmcb->save.lstar = data;
  3474. break;
  3475. case MSR_CSTAR:
  3476. svm->vmcb->save.cstar = data;
  3477. break;
  3478. case MSR_KERNEL_GS_BASE:
  3479. svm->vmcb->save.kernel_gs_base = data;
  3480. break;
  3481. case MSR_SYSCALL_MASK:
  3482. svm->vmcb->save.sfmask = data;
  3483. break;
  3484. #endif
  3485. case MSR_IA32_SYSENTER_CS:
  3486. svm->vmcb->save.sysenter_cs = data;
  3487. break;
  3488. case MSR_IA32_SYSENTER_EIP:
  3489. svm->sysenter_eip = data;
  3490. svm->vmcb->save.sysenter_eip = data;
  3491. break;
  3492. case MSR_IA32_SYSENTER_ESP:
  3493. svm->sysenter_esp = data;
  3494. svm->vmcb->save.sysenter_esp = data;
  3495. break;
  3496. case MSR_TSC_AUX:
  3497. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  3498. return 1;
  3499. /*
  3500. * This is rare, so we update the MSR here instead of using
  3501. * direct_access_msrs. Doing that would require a rdmsr in
  3502. * svm_vcpu_put.
  3503. */
  3504. svm->tsc_aux = data;
  3505. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  3506. break;
  3507. case MSR_IA32_DEBUGCTLMSR:
  3508. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  3509. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  3510. __func__, data);
  3511. break;
  3512. }
  3513. if (data & DEBUGCTL_RESERVED_BITS)
  3514. return 1;
  3515. svm->vmcb->save.dbgctl = data;
  3516. mark_dirty(svm->vmcb, VMCB_LBR);
  3517. if (data & (1ULL<<0))
  3518. svm_enable_lbrv(svm);
  3519. else
  3520. svm_disable_lbrv(svm);
  3521. break;
  3522. case MSR_VM_HSAVE_PA:
  3523. svm->nested.hsave_msr = data;
  3524. break;
  3525. case MSR_VM_CR:
  3526. return svm_set_vm_cr(vcpu, data);
  3527. case MSR_VM_IGNNE:
  3528. vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  3529. break;
  3530. case MSR_F10H_DECFG: {
  3531. struct kvm_msr_entry msr_entry;
  3532. msr_entry.index = msr->index;
  3533. if (svm_get_msr_feature(&msr_entry))
  3534. return 1;
  3535. /* Check the supported bits */
  3536. if (data & ~msr_entry.data)
  3537. return 1;
  3538. /* Don't allow the guest to change a bit, #GP */
  3539. if (!msr->host_initiated && (data ^ msr_entry.data))
  3540. return 1;
  3541. svm->msr_decfg = data;
  3542. break;
  3543. }
  3544. case MSR_IA32_APICBASE:
  3545. if (kvm_vcpu_apicv_active(vcpu))
  3546. avic_update_vapic_bar(to_svm(vcpu), data);
  3547. /* Follow through */
  3548. default:
  3549. return kvm_set_msr_common(vcpu, msr);
  3550. }
  3551. return 0;
  3552. }
  3553. static int wrmsr_interception(struct vcpu_svm *svm)
  3554. {
  3555. struct msr_data msr;
  3556. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  3557. u64 data = kvm_read_edx_eax(&svm->vcpu);
  3558. msr.data = data;
  3559. msr.index = ecx;
  3560. msr.host_initiated = false;
  3561. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  3562. if (kvm_set_msr(&svm->vcpu, &msr)) {
  3563. trace_kvm_msr_write_ex(ecx, data);
  3564. kvm_inject_gp(&svm->vcpu, 0);
  3565. return 1;
  3566. } else {
  3567. trace_kvm_msr_write(ecx, data);
  3568. return kvm_skip_emulated_instruction(&svm->vcpu);
  3569. }
  3570. }
  3571. static int msr_interception(struct vcpu_svm *svm)
  3572. {
  3573. if (svm->vmcb->control.exit_info_1)
  3574. return wrmsr_interception(svm);
  3575. else
  3576. return rdmsr_interception(svm);
  3577. }
  3578. static int interrupt_window_interception(struct vcpu_svm *svm)
  3579. {
  3580. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3581. svm_clear_vintr(svm);
  3582. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  3583. mark_dirty(svm->vmcb, VMCB_INTR);
  3584. ++svm->vcpu.stat.irq_window_exits;
  3585. return 1;
  3586. }
  3587. static int pause_interception(struct vcpu_svm *svm)
  3588. {
  3589. struct kvm_vcpu *vcpu = &svm->vcpu;
  3590. bool in_kernel = (svm_get_cpl(vcpu) == 0);
  3591. if (pause_filter_thresh)
  3592. grow_ple_window(vcpu);
  3593. kvm_vcpu_on_spin(vcpu, in_kernel);
  3594. return 1;
  3595. }
  3596. static int nop_interception(struct vcpu_svm *svm)
  3597. {
  3598. return kvm_skip_emulated_instruction(&(svm->vcpu));
  3599. }
  3600. static int monitor_interception(struct vcpu_svm *svm)
  3601. {
  3602. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  3603. return nop_interception(svm);
  3604. }
  3605. static int mwait_interception(struct vcpu_svm *svm)
  3606. {
  3607. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  3608. return nop_interception(svm);
  3609. }
  3610. enum avic_ipi_failure_cause {
  3611. AVIC_IPI_FAILURE_INVALID_INT_TYPE,
  3612. AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
  3613. AVIC_IPI_FAILURE_INVALID_TARGET,
  3614. AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
  3615. };
  3616. static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
  3617. {
  3618. u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
  3619. u32 icrl = svm->vmcb->control.exit_info_1;
  3620. u32 id = svm->vmcb->control.exit_info_2 >> 32;
  3621. u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
  3622. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3623. trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
  3624. switch (id) {
  3625. case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
  3626. /*
  3627. * AVIC hardware handles the generation of
  3628. * IPIs when the specified Message Type is Fixed
  3629. * (also known as fixed delivery mode) and
  3630. * the Trigger Mode is edge-triggered. The hardware
  3631. * also supports self and broadcast delivery modes
  3632. * specified via the Destination Shorthand(DSH)
  3633. * field of the ICRL. Logical and physical APIC ID
  3634. * formats are supported. All other IPI types cause
  3635. * a #VMEXIT, which needs to emulated.
  3636. */
  3637. kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
  3638. kvm_lapic_reg_write(apic, APIC_ICR, icrl);
  3639. break;
  3640. case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
  3641. int i;
  3642. struct kvm_vcpu *vcpu;
  3643. struct kvm *kvm = svm->vcpu.kvm;
  3644. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3645. /*
  3646. * At this point, we expect that the AVIC HW has already
  3647. * set the appropriate IRR bits on the valid target
  3648. * vcpus. So, we just need to kick the appropriate vcpu.
  3649. */
  3650. kvm_for_each_vcpu(i, vcpu, kvm) {
  3651. bool m = kvm_apic_match_dest(vcpu, apic,
  3652. icrl & KVM_APIC_SHORT_MASK,
  3653. GET_APIC_DEST_FIELD(icrh),
  3654. icrl & KVM_APIC_DEST_MASK);
  3655. if (m && !avic_vcpu_is_running(vcpu))
  3656. kvm_vcpu_wake_up(vcpu);
  3657. }
  3658. break;
  3659. }
  3660. case AVIC_IPI_FAILURE_INVALID_TARGET:
  3661. break;
  3662. case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
  3663. WARN_ONCE(1, "Invalid backing page\n");
  3664. break;
  3665. default:
  3666. pr_err("Unknown IPI interception\n");
  3667. }
  3668. return 1;
  3669. }
  3670. static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
  3671. {
  3672. struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
  3673. int index;
  3674. u32 *logical_apic_id_table;
  3675. int dlid = GET_APIC_LOGICAL_ID(ldr);
  3676. if (!dlid)
  3677. return NULL;
  3678. if (flat) { /* flat */
  3679. index = ffs(dlid) - 1;
  3680. if (index > 7)
  3681. return NULL;
  3682. } else { /* cluster */
  3683. int cluster = (dlid & 0xf0) >> 4;
  3684. int apic = ffs(dlid & 0x0f) - 1;
  3685. if ((apic < 0) || (apic > 7) ||
  3686. (cluster >= 0xf))
  3687. return NULL;
  3688. index = (cluster << 2) + apic;
  3689. }
  3690. logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
  3691. return &logical_apic_id_table[index];
  3692. }
  3693. static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
  3694. bool valid)
  3695. {
  3696. bool flat;
  3697. u32 *entry, new_entry;
  3698. flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
  3699. entry = avic_get_logical_id_entry(vcpu, ldr, flat);
  3700. if (!entry)
  3701. return -EINVAL;
  3702. new_entry = READ_ONCE(*entry);
  3703. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
  3704. new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
  3705. if (valid)
  3706. new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3707. else
  3708. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3709. WRITE_ONCE(*entry, new_entry);
  3710. return 0;
  3711. }
  3712. static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
  3713. {
  3714. int ret;
  3715. struct vcpu_svm *svm = to_svm(vcpu);
  3716. u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
  3717. if (!ldr)
  3718. return 1;
  3719. ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
  3720. if (ret && svm->ldr_reg) {
  3721. avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
  3722. svm->ldr_reg = 0;
  3723. } else {
  3724. svm->ldr_reg = ldr;
  3725. }
  3726. return ret;
  3727. }
  3728. static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
  3729. {
  3730. u64 *old, *new;
  3731. struct vcpu_svm *svm = to_svm(vcpu);
  3732. u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
  3733. u32 id = (apic_id_reg >> 24) & 0xff;
  3734. if (vcpu->vcpu_id == id)
  3735. return 0;
  3736. old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
  3737. new = avic_get_physical_id_entry(vcpu, id);
  3738. if (!new || !old)
  3739. return 1;
  3740. /* We need to move physical_id_entry to new offset */
  3741. *new = *old;
  3742. *old = 0ULL;
  3743. to_svm(vcpu)->avic_physical_id_cache = new;
  3744. /*
  3745. * Also update the guest physical APIC ID in the logical
  3746. * APIC ID table entry if already setup the LDR.
  3747. */
  3748. if (svm->ldr_reg)
  3749. avic_handle_ldr_update(vcpu);
  3750. return 0;
  3751. }
  3752. static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
  3753. {
  3754. struct vcpu_svm *svm = to_svm(vcpu);
  3755. struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
  3756. u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
  3757. u32 mod = (dfr >> 28) & 0xf;
  3758. /*
  3759. * We assume that all local APICs are using the same type.
  3760. * If this changes, we need to flush the AVIC logical
  3761. * APID id table.
  3762. */
  3763. if (kvm_svm->ldr_mode == mod)
  3764. return 0;
  3765. clear_page(page_address(kvm_svm->avic_logical_id_table_page));
  3766. kvm_svm->ldr_mode = mod;
  3767. if (svm->ldr_reg)
  3768. avic_handle_ldr_update(vcpu);
  3769. return 0;
  3770. }
  3771. static int avic_unaccel_trap_write(struct vcpu_svm *svm)
  3772. {
  3773. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3774. u32 offset = svm->vmcb->control.exit_info_1 &
  3775. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3776. switch (offset) {
  3777. case APIC_ID:
  3778. if (avic_handle_apic_id_update(&svm->vcpu))
  3779. return 0;
  3780. break;
  3781. case APIC_LDR:
  3782. if (avic_handle_ldr_update(&svm->vcpu))
  3783. return 0;
  3784. break;
  3785. case APIC_DFR:
  3786. avic_handle_dfr_update(&svm->vcpu);
  3787. break;
  3788. default:
  3789. break;
  3790. }
  3791. kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
  3792. return 1;
  3793. }
  3794. static bool is_avic_unaccelerated_access_trap(u32 offset)
  3795. {
  3796. bool ret = false;
  3797. switch (offset) {
  3798. case APIC_ID:
  3799. case APIC_EOI:
  3800. case APIC_RRR:
  3801. case APIC_LDR:
  3802. case APIC_DFR:
  3803. case APIC_SPIV:
  3804. case APIC_ESR:
  3805. case APIC_ICR:
  3806. case APIC_LVTT:
  3807. case APIC_LVTTHMR:
  3808. case APIC_LVTPC:
  3809. case APIC_LVT0:
  3810. case APIC_LVT1:
  3811. case APIC_LVTERR:
  3812. case APIC_TMICT:
  3813. case APIC_TDCR:
  3814. ret = true;
  3815. break;
  3816. default:
  3817. break;
  3818. }
  3819. return ret;
  3820. }
  3821. static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
  3822. {
  3823. int ret = 0;
  3824. u32 offset = svm->vmcb->control.exit_info_1 &
  3825. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3826. u32 vector = svm->vmcb->control.exit_info_2 &
  3827. AVIC_UNACCEL_ACCESS_VECTOR_MASK;
  3828. bool write = (svm->vmcb->control.exit_info_1 >> 32) &
  3829. AVIC_UNACCEL_ACCESS_WRITE_MASK;
  3830. bool trap = is_avic_unaccelerated_access_trap(offset);
  3831. trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
  3832. trap, write, vector);
  3833. if (trap) {
  3834. /* Handling Trap */
  3835. WARN_ONCE(!write, "svm: Handling trap read.\n");
  3836. ret = avic_unaccel_trap_write(svm);
  3837. } else {
  3838. /* Handling Fault */
  3839. ret = (kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
  3840. }
  3841. return ret;
  3842. }
  3843. static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
  3844. [SVM_EXIT_READ_CR0] = cr_interception,
  3845. [SVM_EXIT_READ_CR3] = cr_interception,
  3846. [SVM_EXIT_READ_CR4] = cr_interception,
  3847. [SVM_EXIT_READ_CR8] = cr_interception,
  3848. [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
  3849. [SVM_EXIT_WRITE_CR0] = cr_interception,
  3850. [SVM_EXIT_WRITE_CR3] = cr_interception,
  3851. [SVM_EXIT_WRITE_CR4] = cr_interception,
  3852. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  3853. [SVM_EXIT_READ_DR0] = dr_interception,
  3854. [SVM_EXIT_READ_DR1] = dr_interception,
  3855. [SVM_EXIT_READ_DR2] = dr_interception,
  3856. [SVM_EXIT_READ_DR3] = dr_interception,
  3857. [SVM_EXIT_READ_DR4] = dr_interception,
  3858. [SVM_EXIT_READ_DR5] = dr_interception,
  3859. [SVM_EXIT_READ_DR6] = dr_interception,
  3860. [SVM_EXIT_READ_DR7] = dr_interception,
  3861. [SVM_EXIT_WRITE_DR0] = dr_interception,
  3862. [SVM_EXIT_WRITE_DR1] = dr_interception,
  3863. [SVM_EXIT_WRITE_DR2] = dr_interception,
  3864. [SVM_EXIT_WRITE_DR3] = dr_interception,
  3865. [SVM_EXIT_WRITE_DR4] = dr_interception,
  3866. [SVM_EXIT_WRITE_DR5] = dr_interception,
  3867. [SVM_EXIT_WRITE_DR6] = dr_interception,
  3868. [SVM_EXIT_WRITE_DR7] = dr_interception,
  3869. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  3870. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  3871. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  3872. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  3873. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  3874. [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
  3875. [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
  3876. [SVM_EXIT_INTR] = intr_interception,
  3877. [SVM_EXIT_NMI] = nmi_interception,
  3878. [SVM_EXIT_SMI] = nop_on_interception,
  3879. [SVM_EXIT_INIT] = nop_on_interception,
  3880. [SVM_EXIT_VINTR] = interrupt_window_interception,
  3881. [SVM_EXIT_RDPMC] = rdpmc_interception,
  3882. [SVM_EXIT_CPUID] = cpuid_interception,
  3883. [SVM_EXIT_IRET] = iret_interception,
  3884. [SVM_EXIT_INVD] = emulate_on_interception,
  3885. [SVM_EXIT_PAUSE] = pause_interception,
  3886. [SVM_EXIT_HLT] = halt_interception,
  3887. [SVM_EXIT_INVLPG] = invlpg_interception,
  3888. [SVM_EXIT_INVLPGA] = invlpga_interception,
  3889. [SVM_EXIT_IOIO] = io_interception,
  3890. [SVM_EXIT_MSR] = msr_interception,
  3891. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  3892. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  3893. [SVM_EXIT_VMRUN] = vmrun_interception,
  3894. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  3895. [SVM_EXIT_VMLOAD] = vmload_interception,
  3896. [SVM_EXIT_VMSAVE] = vmsave_interception,
  3897. [SVM_EXIT_STGI] = stgi_interception,
  3898. [SVM_EXIT_CLGI] = clgi_interception,
  3899. [SVM_EXIT_SKINIT] = skinit_interception,
  3900. [SVM_EXIT_WBINVD] = wbinvd_interception,
  3901. [SVM_EXIT_MONITOR] = monitor_interception,
  3902. [SVM_EXIT_MWAIT] = mwait_interception,
  3903. [SVM_EXIT_XSETBV] = xsetbv_interception,
  3904. [SVM_EXIT_NPF] = npf_interception,
  3905. [SVM_EXIT_RSM] = rsm_interception,
  3906. [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
  3907. [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
  3908. };
  3909. static void dump_vmcb(struct kvm_vcpu *vcpu)
  3910. {
  3911. struct vcpu_svm *svm = to_svm(vcpu);
  3912. struct vmcb_control_area *control = &svm->vmcb->control;
  3913. struct vmcb_save_area *save = &svm->vmcb->save;
  3914. pr_err("VMCB Control Area:\n");
  3915. pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
  3916. pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
  3917. pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
  3918. pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
  3919. pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
  3920. pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
  3921. pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
  3922. pr_err("%-20s%d\n", "pause filter threshold:",
  3923. control->pause_filter_thresh);
  3924. pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
  3925. pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
  3926. pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
  3927. pr_err("%-20s%d\n", "asid:", control->asid);
  3928. pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
  3929. pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
  3930. pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
  3931. pr_err("%-20s%08x\n", "int_state:", control->int_state);
  3932. pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
  3933. pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
  3934. pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
  3935. pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
  3936. pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
  3937. pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
  3938. pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
  3939. pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
  3940. pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
  3941. pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
  3942. pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
  3943. pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
  3944. pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
  3945. pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
  3946. pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
  3947. pr_err("VMCB State Save Area:\n");
  3948. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3949. "es:",
  3950. save->es.selector, save->es.attrib,
  3951. save->es.limit, save->es.base);
  3952. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3953. "cs:",
  3954. save->cs.selector, save->cs.attrib,
  3955. save->cs.limit, save->cs.base);
  3956. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3957. "ss:",
  3958. save->ss.selector, save->ss.attrib,
  3959. save->ss.limit, save->ss.base);
  3960. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3961. "ds:",
  3962. save->ds.selector, save->ds.attrib,
  3963. save->ds.limit, save->ds.base);
  3964. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3965. "fs:",
  3966. save->fs.selector, save->fs.attrib,
  3967. save->fs.limit, save->fs.base);
  3968. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3969. "gs:",
  3970. save->gs.selector, save->gs.attrib,
  3971. save->gs.limit, save->gs.base);
  3972. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3973. "gdtr:",
  3974. save->gdtr.selector, save->gdtr.attrib,
  3975. save->gdtr.limit, save->gdtr.base);
  3976. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3977. "ldtr:",
  3978. save->ldtr.selector, save->ldtr.attrib,
  3979. save->ldtr.limit, save->ldtr.base);
  3980. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3981. "idtr:",
  3982. save->idtr.selector, save->idtr.attrib,
  3983. save->idtr.limit, save->idtr.base);
  3984. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3985. "tr:",
  3986. save->tr.selector, save->tr.attrib,
  3987. save->tr.limit, save->tr.base);
  3988. pr_err("cpl: %d efer: %016llx\n",
  3989. save->cpl, save->efer);
  3990. pr_err("%-15s %016llx %-13s %016llx\n",
  3991. "cr0:", save->cr0, "cr2:", save->cr2);
  3992. pr_err("%-15s %016llx %-13s %016llx\n",
  3993. "cr3:", save->cr3, "cr4:", save->cr4);
  3994. pr_err("%-15s %016llx %-13s %016llx\n",
  3995. "dr6:", save->dr6, "dr7:", save->dr7);
  3996. pr_err("%-15s %016llx %-13s %016llx\n",
  3997. "rip:", save->rip, "rflags:", save->rflags);
  3998. pr_err("%-15s %016llx %-13s %016llx\n",
  3999. "rsp:", save->rsp, "rax:", save->rax);
  4000. pr_err("%-15s %016llx %-13s %016llx\n",
  4001. "star:", save->star, "lstar:", save->lstar);
  4002. pr_err("%-15s %016llx %-13s %016llx\n",
  4003. "cstar:", save->cstar, "sfmask:", save->sfmask);
  4004. pr_err("%-15s %016llx %-13s %016llx\n",
  4005. "kernel_gs_base:", save->kernel_gs_base,
  4006. "sysenter_cs:", save->sysenter_cs);
  4007. pr_err("%-15s %016llx %-13s %016llx\n",
  4008. "sysenter_esp:", save->sysenter_esp,
  4009. "sysenter_eip:", save->sysenter_eip);
  4010. pr_err("%-15s %016llx %-13s %016llx\n",
  4011. "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
  4012. pr_err("%-15s %016llx %-13s %016llx\n",
  4013. "br_from:", save->br_from, "br_to:", save->br_to);
  4014. pr_err("%-15s %016llx %-13s %016llx\n",
  4015. "excp_from:", save->last_excp_from,
  4016. "excp_to:", save->last_excp_to);
  4017. }
  4018. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  4019. {
  4020. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  4021. *info1 = control->exit_info_1;
  4022. *info2 = control->exit_info_2;
  4023. }
  4024. static int handle_exit(struct kvm_vcpu *vcpu)
  4025. {
  4026. struct vcpu_svm *svm = to_svm(vcpu);
  4027. struct kvm_run *kvm_run = vcpu->run;
  4028. u32 exit_code = svm->vmcb->control.exit_code;
  4029. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  4030. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  4031. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  4032. if (npt_enabled)
  4033. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  4034. if (unlikely(svm->nested.exit_required)) {
  4035. nested_svm_vmexit(svm);
  4036. svm->nested.exit_required = false;
  4037. return 1;
  4038. }
  4039. if (is_guest_mode(vcpu)) {
  4040. int vmexit;
  4041. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  4042. svm->vmcb->control.exit_info_1,
  4043. svm->vmcb->control.exit_info_2,
  4044. svm->vmcb->control.exit_int_info,
  4045. svm->vmcb->control.exit_int_info_err,
  4046. KVM_ISA_SVM);
  4047. vmexit = nested_svm_exit_special(svm);
  4048. if (vmexit == NESTED_EXIT_CONTINUE)
  4049. vmexit = nested_svm_exit_handled(svm);
  4050. if (vmexit == NESTED_EXIT_DONE)
  4051. return 1;
  4052. }
  4053. svm_complete_interrupts(svm);
  4054. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  4055. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  4056. kvm_run->fail_entry.hardware_entry_failure_reason
  4057. = svm->vmcb->control.exit_code;
  4058. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  4059. dump_vmcb(vcpu);
  4060. return 0;
  4061. }
  4062. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  4063. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  4064. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  4065. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  4066. printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
  4067. "exit_code 0x%x\n",
  4068. __func__, svm->vmcb->control.exit_int_info,
  4069. exit_code);
  4070. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  4071. || !svm_exit_handlers[exit_code]) {
  4072. WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
  4073. kvm_queue_exception(vcpu, UD_VECTOR);
  4074. return 1;
  4075. }
  4076. return svm_exit_handlers[exit_code](svm);
  4077. }
  4078. static void reload_tss(struct kvm_vcpu *vcpu)
  4079. {
  4080. int cpu = raw_smp_processor_id();
  4081. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  4082. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  4083. load_TR_desc();
  4084. }
  4085. static void pre_sev_run(struct vcpu_svm *svm, int cpu)
  4086. {
  4087. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  4088. int asid = sev_get_asid(svm->vcpu.kvm);
  4089. /* Assign the asid allocated with this SEV guest */
  4090. svm->vmcb->control.asid = asid;
  4091. /*
  4092. * Flush guest TLB:
  4093. *
  4094. * 1) when different VMCB for the same ASID is to be run on the same host CPU.
  4095. * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
  4096. */
  4097. if (sd->sev_vmcbs[asid] == svm->vmcb &&
  4098. svm->last_cpu == cpu)
  4099. return;
  4100. svm->last_cpu = cpu;
  4101. sd->sev_vmcbs[asid] = svm->vmcb;
  4102. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  4103. mark_dirty(svm->vmcb, VMCB_ASID);
  4104. }
  4105. static void pre_svm_run(struct vcpu_svm *svm)
  4106. {
  4107. int cpu = raw_smp_processor_id();
  4108. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  4109. if (sev_guest(svm->vcpu.kvm))
  4110. return pre_sev_run(svm, cpu);
  4111. /* FIXME: handle wraparound of asid_generation */
  4112. if (svm->asid_generation != sd->asid_generation)
  4113. new_asid(svm, sd);
  4114. }
  4115. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  4116. {
  4117. struct vcpu_svm *svm = to_svm(vcpu);
  4118. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  4119. vcpu->arch.hflags |= HF_NMI_MASK;
  4120. set_intercept(svm, INTERCEPT_IRET);
  4121. ++vcpu->stat.nmi_injections;
  4122. }
  4123. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  4124. {
  4125. struct vmcb_control_area *control;
  4126. /* The following fields are ignored when AVIC is enabled */
  4127. control = &svm->vmcb->control;
  4128. control->int_vector = irq;
  4129. control->int_ctl &= ~V_INTR_PRIO_MASK;
  4130. control->int_ctl |= V_IRQ_MASK |
  4131. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  4132. mark_dirty(svm->vmcb, VMCB_INTR);
  4133. }
  4134. static void svm_set_irq(struct kvm_vcpu *vcpu)
  4135. {
  4136. struct vcpu_svm *svm = to_svm(vcpu);
  4137. BUG_ON(!(gif_set(svm)));
  4138. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  4139. ++vcpu->stat.irq_injections;
  4140. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  4141. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  4142. }
  4143. static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
  4144. {
  4145. return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
  4146. }
  4147. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  4148. {
  4149. struct vcpu_svm *svm = to_svm(vcpu);
  4150. if (svm_nested_virtualize_tpr(vcpu) ||
  4151. kvm_vcpu_apicv_active(vcpu))
  4152. return;
  4153. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  4154. if (irr == -1)
  4155. return;
  4156. if (tpr >= irr)
  4157. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  4158. }
  4159. static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  4160. {
  4161. return;
  4162. }
  4163. static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
  4164. {
  4165. return avic && irqchip_split(vcpu->kvm);
  4166. }
  4167. static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  4168. {
  4169. }
  4170. static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  4171. {
  4172. }
  4173. /* Note: Currently only used by Hyper-V. */
  4174. static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  4175. {
  4176. struct vcpu_svm *svm = to_svm(vcpu);
  4177. struct vmcb *vmcb = svm->vmcb;
  4178. if (!kvm_vcpu_apicv_active(&svm->vcpu))
  4179. return;
  4180. vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
  4181. mark_dirty(vmcb, VMCB_INTR);
  4182. }
  4183. static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  4184. {
  4185. return;
  4186. }
  4187. static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
  4188. {
  4189. kvm_lapic_set_irr(vec, vcpu->arch.apic);
  4190. smp_mb__after_atomic();
  4191. if (avic_vcpu_is_running(vcpu))
  4192. wrmsrl(SVM_AVIC_DOORBELL,
  4193. kvm_cpu_get_apicid(vcpu->cpu));
  4194. else
  4195. kvm_vcpu_wake_up(vcpu);
  4196. }
  4197. static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  4198. {
  4199. unsigned long flags;
  4200. struct amd_svm_iommu_ir *cur;
  4201. spin_lock_irqsave(&svm->ir_list_lock, flags);
  4202. list_for_each_entry(cur, &svm->ir_list, node) {
  4203. if (cur->data != pi->ir_data)
  4204. continue;
  4205. list_del(&cur->node);
  4206. kfree(cur);
  4207. break;
  4208. }
  4209. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  4210. }
  4211. static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  4212. {
  4213. int ret = 0;
  4214. unsigned long flags;
  4215. struct amd_svm_iommu_ir *ir;
  4216. /**
  4217. * In some cases, the existing irte is updaed and re-set,
  4218. * so we need to check here if it's already been * added
  4219. * to the ir_list.
  4220. */
  4221. if (pi->ir_data && (pi->prev_ga_tag != 0)) {
  4222. struct kvm *kvm = svm->vcpu.kvm;
  4223. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
  4224. struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  4225. struct vcpu_svm *prev_svm;
  4226. if (!prev_vcpu) {
  4227. ret = -EINVAL;
  4228. goto out;
  4229. }
  4230. prev_svm = to_svm(prev_vcpu);
  4231. svm_ir_list_del(prev_svm, pi);
  4232. }
  4233. /**
  4234. * Allocating new amd_iommu_pi_data, which will get
  4235. * add to the per-vcpu ir_list.
  4236. */
  4237. ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
  4238. if (!ir) {
  4239. ret = -ENOMEM;
  4240. goto out;
  4241. }
  4242. ir->data = pi->ir_data;
  4243. spin_lock_irqsave(&svm->ir_list_lock, flags);
  4244. list_add(&ir->node, &svm->ir_list);
  4245. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  4246. out:
  4247. return ret;
  4248. }
  4249. /**
  4250. * Note:
  4251. * The HW cannot support posting multicast/broadcast
  4252. * interrupts to a vCPU. So, we still use legacy interrupt
  4253. * remapping for these kind of interrupts.
  4254. *
  4255. * For lowest-priority interrupts, we only support
  4256. * those with single CPU as the destination, e.g. user
  4257. * configures the interrupts via /proc/irq or uses
  4258. * irqbalance to make the interrupts single-CPU.
  4259. */
  4260. static int
  4261. get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  4262. struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
  4263. {
  4264. struct kvm_lapic_irq irq;
  4265. struct kvm_vcpu *vcpu = NULL;
  4266. kvm_set_msi_irq(kvm, e, &irq);
  4267. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  4268. pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
  4269. __func__, irq.vector);
  4270. return -1;
  4271. }
  4272. pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
  4273. irq.vector);
  4274. *svm = to_svm(vcpu);
  4275. vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
  4276. vcpu_info->vector = irq.vector;
  4277. return 0;
  4278. }
  4279. /*
  4280. * svm_update_pi_irte - set IRTE for Posted-Interrupts
  4281. *
  4282. * @kvm: kvm
  4283. * @host_irq: host irq of the interrupt
  4284. * @guest_irq: gsi of the interrupt
  4285. * @set: set or unset PI
  4286. * returns 0 on success, < 0 on failure
  4287. */
  4288. static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  4289. uint32_t guest_irq, bool set)
  4290. {
  4291. struct kvm_kernel_irq_routing_entry *e;
  4292. struct kvm_irq_routing_table *irq_rt;
  4293. int idx, ret = -EINVAL;
  4294. if (!kvm_arch_has_assigned_device(kvm) ||
  4295. !irq_remapping_cap(IRQ_POSTING_CAP))
  4296. return 0;
  4297. pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
  4298. __func__, host_irq, guest_irq, set);
  4299. idx = srcu_read_lock(&kvm->irq_srcu);
  4300. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  4301. WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
  4302. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  4303. struct vcpu_data vcpu_info;
  4304. struct vcpu_svm *svm = NULL;
  4305. if (e->type != KVM_IRQ_ROUTING_MSI)
  4306. continue;
  4307. /**
  4308. * Here, we setup with legacy mode in the following cases:
  4309. * 1. When cannot target interrupt to a specific vcpu.
  4310. * 2. Unsetting posted interrupt.
  4311. * 3. APIC virtialization is disabled for the vcpu.
  4312. */
  4313. if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
  4314. kvm_vcpu_apicv_active(&svm->vcpu)) {
  4315. struct amd_iommu_pi_data pi;
  4316. /* Try to enable guest_mode in IRTE */
  4317. pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
  4318. AVIC_HPA_MASK);
  4319. pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
  4320. svm->vcpu.vcpu_id);
  4321. pi.is_guest_mode = true;
  4322. pi.vcpu_data = &vcpu_info;
  4323. ret = irq_set_vcpu_affinity(host_irq, &pi);
  4324. /**
  4325. * Here, we successfully setting up vcpu affinity in
  4326. * IOMMU guest mode. Now, we need to store the posted
  4327. * interrupt information in a per-vcpu ir_list so that
  4328. * we can reference to them directly when we update vcpu
  4329. * scheduling information in IOMMU irte.
  4330. */
  4331. if (!ret && pi.is_guest_mode)
  4332. svm_ir_list_add(svm, &pi);
  4333. } else {
  4334. /* Use legacy mode in IRTE */
  4335. struct amd_iommu_pi_data pi;
  4336. /**
  4337. * Here, pi is used to:
  4338. * - Tell IOMMU to use legacy mode for this interrupt.
  4339. * - Retrieve ga_tag of prior interrupt remapping data.
  4340. */
  4341. pi.is_guest_mode = false;
  4342. ret = irq_set_vcpu_affinity(host_irq, &pi);
  4343. /**
  4344. * Check if the posted interrupt was previously
  4345. * setup with the guest_mode by checking if the ga_tag
  4346. * was cached. If so, we need to clean up the per-vcpu
  4347. * ir_list.
  4348. */
  4349. if (!ret && pi.prev_ga_tag) {
  4350. int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
  4351. struct kvm_vcpu *vcpu;
  4352. vcpu = kvm_get_vcpu_by_id(kvm, id);
  4353. if (vcpu)
  4354. svm_ir_list_del(to_svm(vcpu), &pi);
  4355. }
  4356. }
  4357. if (!ret && svm) {
  4358. trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
  4359. e->gsi, vcpu_info.vector,
  4360. vcpu_info.pi_desc_addr, set);
  4361. }
  4362. if (ret < 0) {
  4363. pr_err("%s: failed to update PI IRTE\n", __func__);
  4364. goto out;
  4365. }
  4366. }
  4367. ret = 0;
  4368. out:
  4369. srcu_read_unlock(&kvm->irq_srcu, idx);
  4370. return ret;
  4371. }
  4372. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  4373. {
  4374. struct vcpu_svm *svm = to_svm(vcpu);
  4375. struct vmcb *vmcb = svm->vmcb;
  4376. int ret;
  4377. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  4378. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  4379. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  4380. return ret;
  4381. }
  4382. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  4383. {
  4384. struct vcpu_svm *svm = to_svm(vcpu);
  4385. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  4386. }
  4387. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  4388. {
  4389. struct vcpu_svm *svm = to_svm(vcpu);
  4390. if (masked) {
  4391. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  4392. set_intercept(svm, INTERCEPT_IRET);
  4393. } else {
  4394. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  4395. clr_intercept(svm, INTERCEPT_IRET);
  4396. }
  4397. }
  4398. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  4399. {
  4400. struct vcpu_svm *svm = to_svm(vcpu);
  4401. struct vmcb *vmcb = svm->vmcb;
  4402. int ret;
  4403. if (!gif_set(svm) ||
  4404. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  4405. return 0;
  4406. ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
  4407. if (is_guest_mode(vcpu))
  4408. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  4409. return ret;
  4410. }
  4411. static void enable_irq_window(struct kvm_vcpu *vcpu)
  4412. {
  4413. struct vcpu_svm *svm = to_svm(vcpu);
  4414. if (kvm_vcpu_apicv_active(vcpu))
  4415. return;
  4416. /*
  4417. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  4418. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  4419. * get that intercept, this function will be called again though and
  4420. * we'll get the vintr intercept. However, if the vGIF feature is
  4421. * enabled, the STGI interception will not occur. Enable the irq
  4422. * window under the assumption that the hardware will set the GIF.
  4423. */
  4424. if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
  4425. svm_set_vintr(svm);
  4426. svm_inject_irq(svm, 0x0);
  4427. }
  4428. }
  4429. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  4430. {
  4431. struct vcpu_svm *svm = to_svm(vcpu);
  4432. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  4433. == HF_NMI_MASK)
  4434. return; /* IRET will cause a vm exit */
  4435. if (!gif_set(svm)) {
  4436. if (vgif_enabled(svm))
  4437. set_intercept(svm, INTERCEPT_STGI);
  4438. return; /* STGI will cause a vm exit */
  4439. }
  4440. if (svm->nested.exit_required)
  4441. return; /* we're not going to run the guest yet */
  4442. /*
  4443. * Something prevents NMI from been injected. Single step over possible
  4444. * problem (IRET or exception injection or interrupt shadow)
  4445. */
  4446. svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
  4447. svm->nmi_singlestep = true;
  4448. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  4449. }
  4450. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  4451. {
  4452. return 0;
  4453. }
  4454. static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  4455. {
  4456. return 0;
  4457. }
  4458. static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4459. {
  4460. struct vcpu_svm *svm = to_svm(vcpu);
  4461. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  4462. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  4463. else
  4464. svm->asid_generation--;
  4465. }
  4466. static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
  4467. {
  4468. struct vcpu_svm *svm = to_svm(vcpu);
  4469. invlpga(gva, svm->vmcb->control.asid);
  4470. }
  4471. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  4472. {
  4473. }
  4474. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  4475. {
  4476. struct vcpu_svm *svm = to_svm(vcpu);
  4477. if (svm_nested_virtualize_tpr(vcpu))
  4478. return;
  4479. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  4480. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  4481. kvm_set_cr8(vcpu, cr8);
  4482. }
  4483. }
  4484. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  4485. {
  4486. struct vcpu_svm *svm = to_svm(vcpu);
  4487. u64 cr8;
  4488. if (svm_nested_virtualize_tpr(vcpu) ||
  4489. kvm_vcpu_apicv_active(vcpu))
  4490. return;
  4491. cr8 = kvm_get_cr8(vcpu);
  4492. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  4493. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  4494. }
  4495. static void svm_complete_interrupts(struct vcpu_svm *svm)
  4496. {
  4497. u8 vector;
  4498. int type;
  4499. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  4500. unsigned int3_injected = svm->int3_injected;
  4501. svm->int3_injected = 0;
  4502. /*
  4503. * If we've made progress since setting HF_IRET_MASK, we've
  4504. * executed an IRET and can allow NMI injection.
  4505. */
  4506. if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
  4507. && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
  4508. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  4509. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  4510. }
  4511. svm->vcpu.arch.nmi_injected = false;
  4512. kvm_clear_exception_queue(&svm->vcpu);
  4513. kvm_clear_interrupt_queue(&svm->vcpu);
  4514. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  4515. return;
  4516. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  4517. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  4518. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  4519. switch (type) {
  4520. case SVM_EXITINTINFO_TYPE_NMI:
  4521. svm->vcpu.arch.nmi_injected = true;
  4522. break;
  4523. case SVM_EXITINTINFO_TYPE_EXEPT:
  4524. /*
  4525. * In case of software exceptions, do not reinject the vector,
  4526. * but re-execute the instruction instead. Rewind RIP first
  4527. * if we emulated INT3 before.
  4528. */
  4529. if (kvm_exception_is_soft(vector)) {
  4530. if (vector == BP_VECTOR && int3_injected &&
  4531. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  4532. kvm_rip_write(&svm->vcpu,
  4533. kvm_rip_read(&svm->vcpu) -
  4534. int3_injected);
  4535. break;
  4536. }
  4537. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  4538. u32 err = svm->vmcb->control.exit_int_info_err;
  4539. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  4540. } else
  4541. kvm_requeue_exception(&svm->vcpu, vector);
  4542. break;
  4543. case SVM_EXITINTINFO_TYPE_INTR:
  4544. kvm_queue_interrupt(&svm->vcpu, vector, false);
  4545. break;
  4546. default:
  4547. break;
  4548. }
  4549. }
  4550. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  4551. {
  4552. struct vcpu_svm *svm = to_svm(vcpu);
  4553. struct vmcb_control_area *control = &svm->vmcb->control;
  4554. control->exit_int_info = control->event_inj;
  4555. control->exit_int_info_err = control->event_inj_err;
  4556. control->event_inj = 0;
  4557. svm_complete_interrupts(svm);
  4558. }
  4559. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  4560. {
  4561. struct vcpu_svm *svm = to_svm(vcpu);
  4562. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  4563. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  4564. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  4565. /*
  4566. * A vmexit emulation is required before the vcpu can be executed
  4567. * again.
  4568. */
  4569. if (unlikely(svm->nested.exit_required))
  4570. return;
  4571. /*
  4572. * Disable singlestep if we're injecting an interrupt/exception.
  4573. * We don't want our modified rflags to be pushed on the stack where
  4574. * we might not be able to easily reset them if we disabled NMI
  4575. * singlestep later.
  4576. */
  4577. if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
  4578. /*
  4579. * Event injection happens before external interrupts cause a
  4580. * vmexit and interrupts are disabled here, so smp_send_reschedule
  4581. * is enough to force an immediate vmexit.
  4582. */
  4583. disable_nmi_singlestep(svm);
  4584. smp_send_reschedule(vcpu->cpu);
  4585. }
  4586. pre_svm_run(svm);
  4587. sync_lapic_to_cr8(vcpu);
  4588. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  4589. clgi();
  4590. /*
  4591. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  4592. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  4593. * is no need to worry about the conditional branch over the wrmsr
  4594. * being speculatively taken.
  4595. */
  4596. x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
  4597. local_irq_enable();
  4598. asm volatile (
  4599. "push %%" _ASM_BP "; \n\t"
  4600. "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
  4601. "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
  4602. "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
  4603. "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
  4604. "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
  4605. "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
  4606. #ifdef CONFIG_X86_64
  4607. "mov %c[r8](%[svm]), %%r8 \n\t"
  4608. "mov %c[r9](%[svm]), %%r9 \n\t"
  4609. "mov %c[r10](%[svm]), %%r10 \n\t"
  4610. "mov %c[r11](%[svm]), %%r11 \n\t"
  4611. "mov %c[r12](%[svm]), %%r12 \n\t"
  4612. "mov %c[r13](%[svm]), %%r13 \n\t"
  4613. "mov %c[r14](%[svm]), %%r14 \n\t"
  4614. "mov %c[r15](%[svm]), %%r15 \n\t"
  4615. #endif
  4616. /* Enter guest mode */
  4617. "push %%" _ASM_AX " \n\t"
  4618. "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
  4619. __ex(SVM_VMLOAD) "\n\t"
  4620. __ex(SVM_VMRUN) "\n\t"
  4621. __ex(SVM_VMSAVE) "\n\t"
  4622. "pop %%" _ASM_AX " \n\t"
  4623. /* Save guest registers, load host registers */
  4624. "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
  4625. "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
  4626. "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
  4627. "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
  4628. "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
  4629. "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
  4630. #ifdef CONFIG_X86_64
  4631. "mov %%r8, %c[r8](%[svm]) \n\t"
  4632. "mov %%r9, %c[r9](%[svm]) \n\t"
  4633. "mov %%r10, %c[r10](%[svm]) \n\t"
  4634. "mov %%r11, %c[r11](%[svm]) \n\t"
  4635. "mov %%r12, %c[r12](%[svm]) \n\t"
  4636. "mov %%r13, %c[r13](%[svm]) \n\t"
  4637. "mov %%r14, %c[r14](%[svm]) \n\t"
  4638. "mov %%r15, %c[r15](%[svm]) \n\t"
  4639. /*
  4640. * Clear host registers marked as clobbered to prevent
  4641. * speculative use.
  4642. */
  4643. "xor %%r8d, %%r8d \n\t"
  4644. "xor %%r9d, %%r9d \n\t"
  4645. "xor %%r10d, %%r10d \n\t"
  4646. "xor %%r11d, %%r11d \n\t"
  4647. "xor %%r12d, %%r12d \n\t"
  4648. "xor %%r13d, %%r13d \n\t"
  4649. "xor %%r14d, %%r14d \n\t"
  4650. "xor %%r15d, %%r15d \n\t"
  4651. #endif
  4652. "xor %%ebx, %%ebx \n\t"
  4653. "xor %%ecx, %%ecx \n\t"
  4654. "xor %%edx, %%edx \n\t"
  4655. "xor %%esi, %%esi \n\t"
  4656. "xor %%edi, %%edi \n\t"
  4657. "pop %%" _ASM_BP
  4658. :
  4659. : [svm]"a"(svm),
  4660. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  4661. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  4662. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  4663. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  4664. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  4665. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  4666. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  4667. #ifdef CONFIG_X86_64
  4668. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  4669. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  4670. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  4671. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  4672. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  4673. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  4674. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  4675. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  4676. #endif
  4677. : "cc", "memory"
  4678. #ifdef CONFIG_X86_64
  4679. , "rbx", "rcx", "rdx", "rsi", "rdi"
  4680. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  4681. #else
  4682. , "ebx", "ecx", "edx", "esi", "edi"
  4683. #endif
  4684. );
  4685. /* Eliminate branch target predictions from guest mode */
  4686. vmexit_fill_RSB();
  4687. #ifdef CONFIG_X86_64
  4688. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  4689. #else
  4690. loadsegment(fs, svm->host.fs);
  4691. #ifndef CONFIG_X86_32_LAZY_GS
  4692. loadsegment(gs, svm->host.gs);
  4693. #endif
  4694. #endif
  4695. /*
  4696. * We do not use IBRS in the kernel. If this vCPU has used the
  4697. * SPEC_CTRL MSR it may have left it on; save the value and
  4698. * turn it off. This is much more efficient than blindly adding
  4699. * it to the atomic save/restore list. Especially as the former
  4700. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  4701. *
  4702. * For non-nested case:
  4703. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  4704. * save it.
  4705. *
  4706. * For nested case:
  4707. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  4708. * save it.
  4709. */
  4710. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  4711. svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  4712. reload_tss(vcpu);
  4713. local_irq_disable();
  4714. x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
  4715. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  4716. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  4717. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  4718. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  4719. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4720. kvm_before_interrupt(&svm->vcpu);
  4721. stgi();
  4722. /* Any pending NMI will happen here */
  4723. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4724. kvm_after_interrupt(&svm->vcpu);
  4725. sync_cr8_to_lapic(vcpu);
  4726. svm->next_rip = 0;
  4727. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  4728. /* if exit due to PF check for async PF */
  4729. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  4730. svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  4731. if (npt_enabled) {
  4732. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  4733. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  4734. }
  4735. /*
  4736. * We need to handle MC intercepts here before the vcpu has a chance to
  4737. * change the physical cpu
  4738. */
  4739. if (unlikely(svm->vmcb->control.exit_code ==
  4740. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  4741. svm_handle_mce(svm);
  4742. mark_all_clean(svm->vmcb);
  4743. }
  4744. STACK_FRAME_NON_STANDARD(svm_vcpu_run);
  4745. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4746. {
  4747. struct vcpu_svm *svm = to_svm(vcpu);
  4748. svm->vmcb->save.cr3 = __sme_set(root);
  4749. mark_dirty(svm->vmcb, VMCB_CR);
  4750. }
  4751. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4752. {
  4753. struct vcpu_svm *svm = to_svm(vcpu);
  4754. svm->vmcb->control.nested_cr3 = __sme_set(root);
  4755. mark_dirty(svm->vmcb, VMCB_NPT);
  4756. /* Also sync guest cr3 here in case we live migrate */
  4757. svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
  4758. mark_dirty(svm->vmcb, VMCB_CR);
  4759. }
  4760. static int is_disabled(void)
  4761. {
  4762. u64 vm_cr;
  4763. rdmsrl(MSR_VM_CR, vm_cr);
  4764. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  4765. return 1;
  4766. return 0;
  4767. }
  4768. static void
  4769. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4770. {
  4771. /*
  4772. * Patch in the VMMCALL instruction:
  4773. */
  4774. hypercall[0] = 0x0f;
  4775. hypercall[1] = 0x01;
  4776. hypercall[2] = 0xd9;
  4777. }
  4778. static void svm_check_processor_compat(void *rtn)
  4779. {
  4780. *(int *)rtn = 0;
  4781. }
  4782. static bool svm_cpu_has_accelerated_tpr(void)
  4783. {
  4784. return false;
  4785. }
  4786. static bool svm_has_emulated_msr(int index)
  4787. {
  4788. return true;
  4789. }
  4790. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  4791. {
  4792. return 0;
  4793. }
  4794. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  4795. {
  4796. struct vcpu_svm *svm = to_svm(vcpu);
  4797. /* Update nrips enabled cache */
  4798. svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
  4799. if (!kvm_vcpu_apicv_active(vcpu))
  4800. return;
  4801. guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
  4802. }
  4803. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  4804. {
  4805. switch (func) {
  4806. case 0x1:
  4807. if (avic)
  4808. entry->ecx &= ~bit(X86_FEATURE_X2APIC);
  4809. break;
  4810. case 0x80000001:
  4811. if (nested)
  4812. entry->ecx |= (1 << 2); /* Set SVM bit */
  4813. break;
  4814. case 0x8000000A:
  4815. entry->eax = 1; /* SVM revision 1 */
  4816. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  4817. ASID emulation to nested SVM */
  4818. entry->ecx = 0; /* Reserved */
  4819. entry->edx = 0; /* Per default do not support any
  4820. additional features */
  4821. /* Support next_rip if host supports it */
  4822. if (boot_cpu_has(X86_FEATURE_NRIPS))
  4823. entry->edx |= SVM_FEATURE_NRIP;
  4824. /* Support NPT for the guest if enabled */
  4825. if (npt_enabled)
  4826. entry->edx |= SVM_FEATURE_NPT;
  4827. break;
  4828. case 0x8000001F:
  4829. /* Support memory encryption cpuid if host supports it */
  4830. if (boot_cpu_has(X86_FEATURE_SEV))
  4831. cpuid(0x8000001f, &entry->eax, &entry->ebx,
  4832. &entry->ecx, &entry->edx);
  4833. }
  4834. }
  4835. static int svm_get_lpage_level(void)
  4836. {
  4837. return PT_PDPE_LEVEL;
  4838. }
  4839. static bool svm_rdtscp_supported(void)
  4840. {
  4841. return boot_cpu_has(X86_FEATURE_RDTSCP);
  4842. }
  4843. static bool svm_invpcid_supported(void)
  4844. {
  4845. return false;
  4846. }
  4847. static bool svm_mpx_supported(void)
  4848. {
  4849. return false;
  4850. }
  4851. static bool svm_xsaves_supported(void)
  4852. {
  4853. return false;
  4854. }
  4855. static bool svm_umip_emulated(void)
  4856. {
  4857. return false;
  4858. }
  4859. static bool svm_has_wbinvd_exit(void)
  4860. {
  4861. return true;
  4862. }
  4863. #define PRE_EX(exit) { .exit_code = (exit), \
  4864. .stage = X86_ICPT_PRE_EXCEPT, }
  4865. #define POST_EX(exit) { .exit_code = (exit), \
  4866. .stage = X86_ICPT_POST_EXCEPT, }
  4867. #define POST_MEM(exit) { .exit_code = (exit), \
  4868. .stage = X86_ICPT_POST_MEMACCESS, }
  4869. static const struct __x86_intercept {
  4870. u32 exit_code;
  4871. enum x86_intercept_stage stage;
  4872. } x86_intercept_map[] = {
  4873. [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
  4874. [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
  4875. [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
  4876. [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
  4877. [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
  4878. [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
  4879. [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
  4880. [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
  4881. [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
  4882. [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
  4883. [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
  4884. [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
  4885. [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
  4886. [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
  4887. [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
  4888. [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
  4889. [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
  4890. [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
  4891. [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
  4892. [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
  4893. [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
  4894. [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
  4895. [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
  4896. [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
  4897. [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
  4898. [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
  4899. [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
  4900. [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
  4901. [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
  4902. [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
  4903. [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
  4904. [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
  4905. [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
  4906. [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
  4907. [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
  4908. [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
  4909. [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
  4910. [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
  4911. [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
  4912. [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
  4913. [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
  4914. [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
  4915. [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
  4916. [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
  4917. [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
  4918. [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
  4919. };
  4920. #undef PRE_EX
  4921. #undef POST_EX
  4922. #undef POST_MEM
  4923. static int svm_check_intercept(struct kvm_vcpu *vcpu,
  4924. struct x86_instruction_info *info,
  4925. enum x86_intercept_stage stage)
  4926. {
  4927. struct vcpu_svm *svm = to_svm(vcpu);
  4928. int vmexit, ret = X86EMUL_CONTINUE;
  4929. struct __x86_intercept icpt_info;
  4930. struct vmcb *vmcb = svm->vmcb;
  4931. if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
  4932. goto out;
  4933. icpt_info = x86_intercept_map[info->intercept];
  4934. if (stage != icpt_info.stage)
  4935. goto out;
  4936. switch (icpt_info.exit_code) {
  4937. case SVM_EXIT_READ_CR0:
  4938. if (info->intercept == x86_intercept_cr_read)
  4939. icpt_info.exit_code += info->modrm_reg;
  4940. break;
  4941. case SVM_EXIT_WRITE_CR0: {
  4942. unsigned long cr0, val;
  4943. u64 intercept;
  4944. if (info->intercept == x86_intercept_cr_write)
  4945. icpt_info.exit_code += info->modrm_reg;
  4946. if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
  4947. info->intercept == x86_intercept_clts)
  4948. break;
  4949. intercept = svm->nested.intercept;
  4950. if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
  4951. break;
  4952. cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
  4953. val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
  4954. if (info->intercept == x86_intercept_lmsw) {
  4955. cr0 &= 0xfUL;
  4956. val &= 0xfUL;
  4957. /* lmsw can't clear PE - catch this here */
  4958. if (cr0 & X86_CR0_PE)
  4959. val |= X86_CR0_PE;
  4960. }
  4961. if (cr0 ^ val)
  4962. icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  4963. break;
  4964. }
  4965. case SVM_EXIT_READ_DR0:
  4966. case SVM_EXIT_WRITE_DR0:
  4967. icpt_info.exit_code += info->modrm_reg;
  4968. break;
  4969. case SVM_EXIT_MSR:
  4970. if (info->intercept == x86_intercept_wrmsr)
  4971. vmcb->control.exit_info_1 = 1;
  4972. else
  4973. vmcb->control.exit_info_1 = 0;
  4974. break;
  4975. case SVM_EXIT_PAUSE:
  4976. /*
  4977. * We get this for NOP only, but pause
  4978. * is rep not, check this here
  4979. */
  4980. if (info->rep_prefix != REPE_PREFIX)
  4981. goto out;
  4982. break;
  4983. case SVM_EXIT_IOIO: {
  4984. u64 exit_info;
  4985. u32 bytes;
  4986. if (info->intercept == x86_intercept_in ||
  4987. info->intercept == x86_intercept_ins) {
  4988. exit_info = ((info->src_val & 0xffff) << 16) |
  4989. SVM_IOIO_TYPE_MASK;
  4990. bytes = info->dst_bytes;
  4991. } else {
  4992. exit_info = (info->dst_val & 0xffff) << 16;
  4993. bytes = info->src_bytes;
  4994. }
  4995. if (info->intercept == x86_intercept_outs ||
  4996. info->intercept == x86_intercept_ins)
  4997. exit_info |= SVM_IOIO_STR_MASK;
  4998. if (info->rep_prefix)
  4999. exit_info |= SVM_IOIO_REP_MASK;
  5000. bytes = min(bytes, 4u);
  5001. exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
  5002. exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
  5003. vmcb->control.exit_info_1 = exit_info;
  5004. vmcb->control.exit_info_2 = info->next_rip;
  5005. break;
  5006. }
  5007. default:
  5008. break;
  5009. }
  5010. /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
  5011. if (static_cpu_has(X86_FEATURE_NRIPS))
  5012. vmcb->control.next_rip = info->next_rip;
  5013. vmcb->control.exit_code = icpt_info.exit_code;
  5014. vmexit = nested_svm_exit_handled(svm);
  5015. ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
  5016. : X86EMUL_CONTINUE;
  5017. out:
  5018. return ret;
  5019. }
  5020. static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
  5021. {
  5022. local_irq_enable();
  5023. /*
  5024. * We must have an instruction with interrupts enabled, so
  5025. * the timer interrupt isn't delayed by the interrupt shadow.
  5026. */
  5027. asm("nop");
  5028. local_irq_disable();
  5029. }
  5030. static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
  5031. {
  5032. if (pause_filter_thresh)
  5033. shrink_ple_window(vcpu);
  5034. }
  5035. static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
  5036. {
  5037. if (avic_handle_apic_id_update(vcpu) != 0)
  5038. return;
  5039. if (avic_handle_dfr_update(vcpu) != 0)
  5040. return;
  5041. avic_handle_ldr_update(vcpu);
  5042. }
  5043. static void svm_setup_mce(struct kvm_vcpu *vcpu)
  5044. {
  5045. /* [63:9] are reserved. */
  5046. vcpu->arch.mcg_cap &= 0x1ff;
  5047. }
  5048. static int svm_smi_allowed(struct kvm_vcpu *vcpu)
  5049. {
  5050. struct vcpu_svm *svm = to_svm(vcpu);
  5051. /* Per APM Vol.2 15.22.2 "Response to SMI" */
  5052. if (!gif_set(svm))
  5053. return 0;
  5054. if (is_guest_mode(&svm->vcpu) &&
  5055. svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
  5056. /* TODO: Might need to set exit_info_1 and exit_info_2 here */
  5057. svm->vmcb->control.exit_code = SVM_EXIT_SMI;
  5058. svm->nested.exit_required = true;
  5059. return 0;
  5060. }
  5061. return 1;
  5062. }
  5063. static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  5064. {
  5065. struct vcpu_svm *svm = to_svm(vcpu);
  5066. int ret;
  5067. if (is_guest_mode(vcpu)) {
  5068. /* FED8h - SVM Guest */
  5069. put_smstate(u64, smstate, 0x7ed8, 1);
  5070. /* FEE0h - SVM Guest VMCB Physical Address */
  5071. put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
  5072. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  5073. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  5074. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  5075. ret = nested_svm_vmexit(svm);
  5076. if (ret)
  5077. return ret;
  5078. }
  5079. return 0;
  5080. }
  5081. static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  5082. {
  5083. struct vcpu_svm *svm = to_svm(vcpu);
  5084. struct vmcb *nested_vmcb;
  5085. struct page *page;
  5086. struct {
  5087. u64 guest;
  5088. u64 vmcb;
  5089. } svm_state_save;
  5090. int ret;
  5091. ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
  5092. sizeof(svm_state_save));
  5093. if (ret)
  5094. return ret;
  5095. if (svm_state_save.guest) {
  5096. vcpu->arch.hflags &= ~HF_SMM_MASK;
  5097. nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
  5098. if (nested_vmcb)
  5099. enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
  5100. else
  5101. ret = 1;
  5102. vcpu->arch.hflags |= HF_SMM_MASK;
  5103. }
  5104. return ret;
  5105. }
  5106. static int enable_smi_window(struct kvm_vcpu *vcpu)
  5107. {
  5108. struct vcpu_svm *svm = to_svm(vcpu);
  5109. if (!gif_set(svm)) {
  5110. if (vgif_enabled(svm))
  5111. set_intercept(svm, INTERCEPT_STGI);
  5112. /* STGI will cause a vm exit */
  5113. return 1;
  5114. }
  5115. return 0;
  5116. }
  5117. static int sev_asid_new(void)
  5118. {
  5119. int pos;
  5120. /*
  5121. * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
  5122. */
  5123. pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
  5124. if (pos >= max_sev_asid)
  5125. return -EBUSY;
  5126. set_bit(pos, sev_asid_bitmap);
  5127. return pos + 1;
  5128. }
  5129. static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5130. {
  5131. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5132. int asid, ret;
  5133. ret = -EBUSY;
  5134. asid = sev_asid_new();
  5135. if (asid < 0)
  5136. return ret;
  5137. ret = sev_platform_init(&argp->error);
  5138. if (ret)
  5139. goto e_free;
  5140. sev->active = true;
  5141. sev->asid = asid;
  5142. INIT_LIST_HEAD(&sev->regions_list);
  5143. return 0;
  5144. e_free:
  5145. __sev_asid_free(asid);
  5146. return ret;
  5147. }
  5148. static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
  5149. {
  5150. struct sev_data_activate *data;
  5151. int asid = sev_get_asid(kvm);
  5152. int ret;
  5153. wbinvd_on_all_cpus();
  5154. ret = sev_guest_df_flush(error);
  5155. if (ret)
  5156. return ret;
  5157. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5158. if (!data)
  5159. return -ENOMEM;
  5160. /* activate ASID on the given handle */
  5161. data->handle = handle;
  5162. data->asid = asid;
  5163. ret = sev_guest_activate(data, error);
  5164. kfree(data);
  5165. return ret;
  5166. }
  5167. static int __sev_issue_cmd(int fd, int id, void *data, int *error)
  5168. {
  5169. struct fd f;
  5170. int ret;
  5171. f = fdget(fd);
  5172. if (!f.file)
  5173. return -EBADF;
  5174. ret = sev_issue_cmd_external_user(f.file, id, data, error);
  5175. fdput(f);
  5176. return ret;
  5177. }
  5178. static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
  5179. {
  5180. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5181. return __sev_issue_cmd(sev->fd, id, data, error);
  5182. }
  5183. static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5184. {
  5185. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5186. struct sev_data_launch_start *start;
  5187. struct kvm_sev_launch_start params;
  5188. void *dh_blob, *session_blob;
  5189. int *error = &argp->error;
  5190. int ret;
  5191. if (!sev_guest(kvm))
  5192. return -ENOTTY;
  5193. if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
  5194. return -EFAULT;
  5195. start = kzalloc(sizeof(*start), GFP_KERNEL);
  5196. if (!start)
  5197. return -ENOMEM;
  5198. dh_blob = NULL;
  5199. if (params.dh_uaddr) {
  5200. dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
  5201. if (IS_ERR(dh_blob)) {
  5202. ret = PTR_ERR(dh_blob);
  5203. goto e_free;
  5204. }
  5205. start->dh_cert_address = __sme_set(__pa(dh_blob));
  5206. start->dh_cert_len = params.dh_len;
  5207. }
  5208. session_blob = NULL;
  5209. if (params.session_uaddr) {
  5210. session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
  5211. if (IS_ERR(session_blob)) {
  5212. ret = PTR_ERR(session_blob);
  5213. goto e_free_dh;
  5214. }
  5215. start->session_address = __sme_set(__pa(session_blob));
  5216. start->session_len = params.session_len;
  5217. }
  5218. start->handle = params.handle;
  5219. start->policy = params.policy;
  5220. /* create memory encryption context */
  5221. ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
  5222. if (ret)
  5223. goto e_free_session;
  5224. /* Bind ASID to this guest */
  5225. ret = sev_bind_asid(kvm, start->handle, error);
  5226. if (ret)
  5227. goto e_free_session;
  5228. /* return handle to userspace */
  5229. params.handle = start->handle;
  5230. if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
  5231. sev_unbind_asid(kvm, start->handle);
  5232. ret = -EFAULT;
  5233. goto e_free_session;
  5234. }
  5235. sev->handle = start->handle;
  5236. sev->fd = argp->sev_fd;
  5237. e_free_session:
  5238. kfree(session_blob);
  5239. e_free_dh:
  5240. kfree(dh_blob);
  5241. e_free:
  5242. kfree(start);
  5243. return ret;
  5244. }
  5245. static int get_num_contig_pages(int idx, struct page **inpages,
  5246. unsigned long npages)
  5247. {
  5248. unsigned long paddr, next_paddr;
  5249. int i = idx + 1, pages = 1;
  5250. /* find the number of contiguous pages starting from idx */
  5251. paddr = __sme_page_pa(inpages[idx]);
  5252. while (i < npages) {
  5253. next_paddr = __sme_page_pa(inpages[i++]);
  5254. if ((paddr + PAGE_SIZE) == next_paddr) {
  5255. pages++;
  5256. paddr = next_paddr;
  5257. continue;
  5258. }
  5259. break;
  5260. }
  5261. return pages;
  5262. }
  5263. static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5264. {
  5265. unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
  5266. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5267. struct kvm_sev_launch_update_data params;
  5268. struct sev_data_launch_update_data *data;
  5269. struct page **inpages;
  5270. int i, ret, pages;
  5271. if (!sev_guest(kvm))
  5272. return -ENOTTY;
  5273. if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
  5274. return -EFAULT;
  5275. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5276. if (!data)
  5277. return -ENOMEM;
  5278. vaddr = params.uaddr;
  5279. size = params.len;
  5280. vaddr_end = vaddr + size;
  5281. /* Lock the user memory. */
  5282. inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
  5283. if (!inpages) {
  5284. ret = -ENOMEM;
  5285. goto e_free;
  5286. }
  5287. /*
  5288. * The LAUNCH_UPDATE command will perform in-place encryption of the
  5289. * memory content (i.e it will write the same memory region with C=1).
  5290. * It's possible that the cache may contain the data with C=0, i.e.,
  5291. * unencrypted so invalidate it first.
  5292. */
  5293. sev_clflush_pages(inpages, npages);
  5294. for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
  5295. int offset, len;
  5296. /*
  5297. * If the user buffer is not page-aligned, calculate the offset
  5298. * within the page.
  5299. */
  5300. offset = vaddr & (PAGE_SIZE - 1);
  5301. /* Calculate the number of pages that can be encrypted in one go. */
  5302. pages = get_num_contig_pages(i, inpages, npages);
  5303. len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
  5304. data->handle = sev->handle;
  5305. data->len = len;
  5306. data->address = __sme_page_pa(inpages[i]) + offset;
  5307. ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
  5308. if (ret)
  5309. goto e_unpin;
  5310. size -= len;
  5311. next_vaddr = vaddr + len;
  5312. }
  5313. e_unpin:
  5314. /* content of memory is updated, mark pages dirty */
  5315. for (i = 0; i < npages; i++) {
  5316. set_page_dirty_lock(inpages[i]);
  5317. mark_page_accessed(inpages[i]);
  5318. }
  5319. /* unlock the user pages */
  5320. sev_unpin_memory(kvm, inpages, npages);
  5321. e_free:
  5322. kfree(data);
  5323. return ret;
  5324. }
  5325. static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5326. {
  5327. void __user *measure = (void __user *)(uintptr_t)argp->data;
  5328. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5329. struct sev_data_launch_measure *data;
  5330. struct kvm_sev_launch_measure params;
  5331. void __user *p = NULL;
  5332. void *blob = NULL;
  5333. int ret;
  5334. if (!sev_guest(kvm))
  5335. return -ENOTTY;
  5336. if (copy_from_user(&params, measure, sizeof(params)))
  5337. return -EFAULT;
  5338. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5339. if (!data)
  5340. return -ENOMEM;
  5341. /* User wants to query the blob length */
  5342. if (!params.len)
  5343. goto cmd;
  5344. p = (void __user *)(uintptr_t)params.uaddr;
  5345. if (p) {
  5346. if (params.len > SEV_FW_BLOB_MAX_SIZE) {
  5347. ret = -EINVAL;
  5348. goto e_free;
  5349. }
  5350. ret = -ENOMEM;
  5351. blob = kmalloc(params.len, GFP_KERNEL);
  5352. if (!blob)
  5353. goto e_free;
  5354. data->address = __psp_pa(blob);
  5355. data->len = params.len;
  5356. }
  5357. cmd:
  5358. data->handle = sev->handle;
  5359. ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
  5360. /*
  5361. * If we query the session length, FW responded with expected data.
  5362. */
  5363. if (!params.len)
  5364. goto done;
  5365. if (ret)
  5366. goto e_free_blob;
  5367. if (blob) {
  5368. if (copy_to_user(p, blob, params.len))
  5369. ret = -EFAULT;
  5370. }
  5371. done:
  5372. params.len = data->len;
  5373. if (copy_to_user(measure, &params, sizeof(params)))
  5374. ret = -EFAULT;
  5375. e_free_blob:
  5376. kfree(blob);
  5377. e_free:
  5378. kfree(data);
  5379. return ret;
  5380. }
  5381. static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5382. {
  5383. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5384. struct sev_data_launch_finish *data;
  5385. int ret;
  5386. if (!sev_guest(kvm))
  5387. return -ENOTTY;
  5388. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5389. if (!data)
  5390. return -ENOMEM;
  5391. data->handle = sev->handle;
  5392. ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
  5393. kfree(data);
  5394. return ret;
  5395. }
  5396. static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5397. {
  5398. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5399. struct kvm_sev_guest_status params;
  5400. struct sev_data_guest_status *data;
  5401. int ret;
  5402. if (!sev_guest(kvm))
  5403. return -ENOTTY;
  5404. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5405. if (!data)
  5406. return -ENOMEM;
  5407. data->handle = sev->handle;
  5408. ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
  5409. if (ret)
  5410. goto e_free;
  5411. params.policy = data->policy;
  5412. params.state = data->state;
  5413. params.handle = data->handle;
  5414. if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
  5415. ret = -EFAULT;
  5416. e_free:
  5417. kfree(data);
  5418. return ret;
  5419. }
  5420. static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
  5421. unsigned long dst, int size,
  5422. int *error, bool enc)
  5423. {
  5424. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5425. struct sev_data_dbg *data;
  5426. int ret;
  5427. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5428. if (!data)
  5429. return -ENOMEM;
  5430. data->handle = sev->handle;
  5431. data->dst_addr = dst;
  5432. data->src_addr = src;
  5433. data->len = size;
  5434. ret = sev_issue_cmd(kvm,
  5435. enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
  5436. data, error);
  5437. kfree(data);
  5438. return ret;
  5439. }
  5440. static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
  5441. unsigned long dst_paddr, int sz, int *err)
  5442. {
  5443. int offset;
  5444. /*
  5445. * Its safe to read more than we are asked, caller should ensure that
  5446. * destination has enough space.
  5447. */
  5448. src_paddr = round_down(src_paddr, 16);
  5449. offset = src_paddr & 15;
  5450. sz = round_up(sz + offset, 16);
  5451. return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
  5452. }
  5453. static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
  5454. unsigned long __user dst_uaddr,
  5455. unsigned long dst_paddr,
  5456. int size, int *err)
  5457. {
  5458. struct page *tpage = NULL;
  5459. int ret, offset;
  5460. /* if inputs are not 16-byte then use intermediate buffer */
  5461. if (!IS_ALIGNED(dst_paddr, 16) ||
  5462. !IS_ALIGNED(paddr, 16) ||
  5463. !IS_ALIGNED(size, 16)) {
  5464. tpage = (void *)alloc_page(GFP_KERNEL);
  5465. if (!tpage)
  5466. return -ENOMEM;
  5467. dst_paddr = __sme_page_pa(tpage);
  5468. }
  5469. ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
  5470. if (ret)
  5471. goto e_free;
  5472. if (tpage) {
  5473. offset = paddr & 15;
  5474. if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
  5475. page_address(tpage) + offset, size))
  5476. ret = -EFAULT;
  5477. }
  5478. e_free:
  5479. if (tpage)
  5480. __free_page(tpage);
  5481. return ret;
  5482. }
  5483. static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
  5484. unsigned long __user vaddr,
  5485. unsigned long dst_paddr,
  5486. unsigned long __user dst_vaddr,
  5487. int size, int *error)
  5488. {
  5489. struct page *src_tpage = NULL;
  5490. struct page *dst_tpage = NULL;
  5491. int ret, len = size;
  5492. /* If source buffer is not aligned then use an intermediate buffer */
  5493. if (!IS_ALIGNED(vaddr, 16)) {
  5494. src_tpage = alloc_page(GFP_KERNEL);
  5495. if (!src_tpage)
  5496. return -ENOMEM;
  5497. if (copy_from_user(page_address(src_tpage),
  5498. (void __user *)(uintptr_t)vaddr, size)) {
  5499. __free_page(src_tpage);
  5500. return -EFAULT;
  5501. }
  5502. paddr = __sme_page_pa(src_tpage);
  5503. }
  5504. /*
  5505. * If destination buffer or length is not aligned then do read-modify-write:
  5506. * - decrypt destination in an intermediate buffer
  5507. * - copy the source buffer in an intermediate buffer
  5508. * - use the intermediate buffer as source buffer
  5509. */
  5510. if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
  5511. int dst_offset;
  5512. dst_tpage = alloc_page(GFP_KERNEL);
  5513. if (!dst_tpage) {
  5514. ret = -ENOMEM;
  5515. goto e_free;
  5516. }
  5517. ret = __sev_dbg_decrypt(kvm, dst_paddr,
  5518. __sme_page_pa(dst_tpage), size, error);
  5519. if (ret)
  5520. goto e_free;
  5521. /*
  5522. * If source is kernel buffer then use memcpy() otherwise
  5523. * copy_from_user().
  5524. */
  5525. dst_offset = dst_paddr & 15;
  5526. if (src_tpage)
  5527. memcpy(page_address(dst_tpage) + dst_offset,
  5528. page_address(src_tpage), size);
  5529. else {
  5530. if (copy_from_user(page_address(dst_tpage) + dst_offset,
  5531. (void __user *)(uintptr_t)vaddr, size)) {
  5532. ret = -EFAULT;
  5533. goto e_free;
  5534. }
  5535. }
  5536. paddr = __sme_page_pa(dst_tpage);
  5537. dst_paddr = round_down(dst_paddr, 16);
  5538. len = round_up(size, 16);
  5539. }
  5540. ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
  5541. e_free:
  5542. if (src_tpage)
  5543. __free_page(src_tpage);
  5544. if (dst_tpage)
  5545. __free_page(dst_tpage);
  5546. return ret;
  5547. }
  5548. static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
  5549. {
  5550. unsigned long vaddr, vaddr_end, next_vaddr;
  5551. unsigned long dst_vaddr;
  5552. struct page **src_p, **dst_p;
  5553. struct kvm_sev_dbg debug;
  5554. unsigned long n;
  5555. int ret, size;
  5556. if (!sev_guest(kvm))
  5557. return -ENOTTY;
  5558. if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
  5559. return -EFAULT;
  5560. vaddr = debug.src_uaddr;
  5561. size = debug.len;
  5562. vaddr_end = vaddr + size;
  5563. dst_vaddr = debug.dst_uaddr;
  5564. for (; vaddr < vaddr_end; vaddr = next_vaddr) {
  5565. int len, s_off, d_off;
  5566. /* lock userspace source and destination page */
  5567. src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
  5568. if (!src_p)
  5569. return -EFAULT;
  5570. dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
  5571. if (!dst_p) {
  5572. sev_unpin_memory(kvm, src_p, n);
  5573. return -EFAULT;
  5574. }
  5575. /*
  5576. * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
  5577. * memory content (i.e it will write the same memory region with C=1).
  5578. * It's possible that the cache may contain the data with C=0, i.e.,
  5579. * unencrypted so invalidate it first.
  5580. */
  5581. sev_clflush_pages(src_p, 1);
  5582. sev_clflush_pages(dst_p, 1);
  5583. /*
  5584. * Since user buffer may not be page aligned, calculate the
  5585. * offset within the page.
  5586. */
  5587. s_off = vaddr & ~PAGE_MASK;
  5588. d_off = dst_vaddr & ~PAGE_MASK;
  5589. len = min_t(size_t, (PAGE_SIZE - s_off), size);
  5590. if (dec)
  5591. ret = __sev_dbg_decrypt_user(kvm,
  5592. __sme_page_pa(src_p[0]) + s_off,
  5593. dst_vaddr,
  5594. __sme_page_pa(dst_p[0]) + d_off,
  5595. len, &argp->error);
  5596. else
  5597. ret = __sev_dbg_encrypt_user(kvm,
  5598. __sme_page_pa(src_p[0]) + s_off,
  5599. vaddr,
  5600. __sme_page_pa(dst_p[0]) + d_off,
  5601. dst_vaddr,
  5602. len, &argp->error);
  5603. sev_unpin_memory(kvm, src_p, 1);
  5604. sev_unpin_memory(kvm, dst_p, 1);
  5605. if (ret)
  5606. goto err;
  5607. next_vaddr = vaddr + len;
  5608. dst_vaddr = dst_vaddr + len;
  5609. size -= len;
  5610. }
  5611. err:
  5612. return ret;
  5613. }
  5614. static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
  5615. {
  5616. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5617. struct sev_data_launch_secret *data;
  5618. struct kvm_sev_launch_secret params;
  5619. struct page **pages;
  5620. void *blob, *hdr;
  5621. unsigned long n;
  5622. int ret, offset;
  5623. if (!sev_guest(kvm))
  5624. return -ENOTTY;
  5625. if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
  5626. return -EFAULT;
  5627. pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
  5628. if (!pages)
  5629. return -ENOMEM;
  5630. /*
  5631. * The secret must be copied into contiguous memory region, lets verify
  5632. * that userspace memory pages are contiguous before we issue command.
  5633. */
  5634. if (get_num_contig_pages(0, pages, n) != n) {
  5635. ret = -EINVAL;
  5636. goto e_unpin_memory;
  5637. }
  5638. ret = -ENOMEM;
  5639. data = kzalloc(sizeof(*data), GFP_KERNEL);
  5640. if (!data)
  5641. goto e_unpin_memory;
  5642. offset = params.guest_uaddr & (PAGE_SIZE - 1);
  5643. data->guest_address = __sme_page_pa(pages[0]) + offset;
  5644. data->guest_len = params.guest_len;
  5645. blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
  5646. if (IS_ERR(blob)) {
  5647. ret = PTR_ERR(blob);
  5648. goto e_free;
  5649. }
  5650. data->trans_address = __psp_pa(blob);
  5651. data->trans_len = params.trans_len;
  5652. hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
  5653. if (IS_ERR(hdr)) {
  5654. ret = PTR_ERR(hdr);
  5655. goto e_free_blob;
  5656. }
  5657. data->hdr_address = __psp_pa(hdr);
  5658. data->hdr_len = params.hdr_len;
  5659. data->handle = sev->handle;
  5660. ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
  5661. kfree(hdr);
  5662. e_free_blob:
  5663. kfree(blob);
  5664. e_free:
  5665. kfree(data);
  5666. e_unpin_memory:
  5667. sev_unpin_memory(kvm, pages, n);
  5668. return ret;
  5669. }
  5670. static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
  5671. {
  5672. struct kvm_sev_cmd sev_cmd;
  5673. int r;
  5674. if (!svm_sev_enabled())
  5675. return -ENOTTY;
  5676. if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
  5677. return -EFAULT;
  5678. mutex_lock(&kvm->lock);
  5679. switch (sev_cmd.id) {
  5680. case KVM_SEV_INIT:
  5681. r = sev_guest_init(kvm, &sev_cmd);
  5682. break;
  5683. case KVM_SEV_LAUNCH_START:
  5684. r = sev_launch_start(kvm, &sev_cmd);
  5685. break;
  5686. case KVM_SEV_LAUNCH_UPDATE_DATA:
  5687. r = sev_launch_update_data(kvm, &sev_cmd);
  5688. break;
  5689. case KVM_SEV_LAUNCH_MEASURE:
  5690. r = sev_launch_measure(kvm, &sev_cmd);
  5691. break;
  5692. case KVM_SEV_LAUNCH_FINISH:
  5693. r = sev_launch_finish(kvm, &sev_cmd);
  5694. break;
  5695. case KVM_SEV_GUEST_STATUS:
  5696. r = sev_guest_status(kvm, &sev_cmd);
  5697. break;
  5698. case KVM_SEV_DBG_DECRYPT:
  5699. r = sev_dbg_crypt(kvm, &sev_cmd, true);
  5700. break;
  5701. case KVM_SEV_DBG_ENCRYPT:
  5702. r = sev_dbg_crypt(kvm, &sev_cmd, false);
  5703. break;
  5704. case KVM_SEV_LAUNCH_SECRET:
  5705. r = sev_launch_secret(kvm, &sev_cmd);
  5706. break;
  5707. default:
  5708. r = -EINVAL;
  5709. goto out;
  5710. }
  5711. if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
  5712. r = -EFAULT;
  5713. out:
  5714. mutex_unlock(&kvm->lock);
  5715. return r;
  5716. }
  5717. static int svm_register_enc_region(struct kvm *kvm,
  5718. struct kvm_enc_region *range)
  5719. {
  5720. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5721. struct enc_region *region;
  5722. int ret = 0;
  5723. if (!sev_guest(kvm))
  5724. return -ENOTTY;
  5725. if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
  5726. return -EINVAL;
  5727. region = kzalloc(sizeof(*region), GFP_KERNEL);
  5728. if (!region)
  5729. return -ENOMEM;
  5730. region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
  5731. if (!region->pages) {
  5732. ret = -ENOMEM;
  5733. goto e_free;
  5734. }
  5735. /*
  5736. * The guest may change the memory encryption attribute from C=0 -> C=1
  5737. * or vice versa for this memory range. Lets make sure caches are
  5738. * flushed to ensure that guest data gets written into memory with
  5739. * correct C-bit.
  5740. */
  5741. sev_clflush_pages(region->pages, region->npages);
  5742. region->uaddr = range->addr;
  5743. region->size = range->size;
  5744. mutex_lock(&kvm->lock);
  5745. list_add_tail(&region->list, &sev->regions_list);
  5746. mutex_unlock(&kvm->lock);
  5747. return ret;
  5748. e_free:
  5749. kfree(region);
  5750. return ret;
  5751. }
  5752. static struct enc_region *
  5753. find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
  5754. {
  5755. struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  5756. struct list_head *head = &sev->regions_list;
  5757. struct enc_region *i;
  5758. list_for_each_entry(i, head, list) {
  5759. if (i->uaddr == range->addr &&
  5760. i->size == range->size)
  5761. return i;
  5762. }
  5763. return NULL;
  5764. }
  5765. static int svm_unregister_enc_region(struct kvm *kvm,
  5766. struct kvm_enc_region *range)
  5767. {
  5768. struct enc_region *region;
  5769. int ret;
  5770. mutex_lock(&kvm->lock);
  5771. if (!sev_guest(kvm)) {
  5772. ret = -ENOTTY;
  5773. goto failed;
  5774. }
  5775. region = find_enc_region(kvm, range);
  5776. if (!region) {
  5777. ret = -EINVAL;
  5778. goto failed;
  5779. }
  5780. __unregister_enc_region_locked(kvm, region);
  5781. mutex_unlock(&kvm->lock);
  5782. return 0;
  5783. failed:
  5784. mutex_unlock(&kvm->lock);
  5785. return ret;
  5786. }
  5787. static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
  5788. uint16_t *vmcs_version)
  5789. {
  5790. /* Intel-only feature */
  5791. return -ENODEV;
  5792. }
  5793. static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
  5794. .cpu_has_kvm_support = has_svm,
  5795. .disabled_by_bios = is_disabled,
  5796. .hardware_setup = svm_hardware_setup,
  5797. .hardware_unsetup = svm_hardware_unsetup,
  5798. .check_processor_compatibility = svm_check_processor_compat,
  5799. .hardware_enable = svm_hardware_enable,
  5800. .hardware_disable = svm_hardware_disable,
  5801. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  5802. .has_emulated_msr = svm_has_emulated_msr,
  5803. .vcpu_create = svm_create_vcpu,
  5804. .vcpu_free = svm_free_vcpu,
  5805. .vcpu_reset = svm_vcpu_reset,
  5806. .vm_alloc = svm_vm_alloc,
  5807. .vm_free = svm_vm_free,
  5808. .vm_init = avic_vm_init,
  5809. .vm_destroy = svm_vm_destroy,
  5810. .prepare_guest_switch = svm_prepare_guest_switch,
  5811. .vcpu_load = svm_vcpu_load,
  5812. .vcpu_put = svm_vcpu_put,
  5813. .vcpu_blocking = svm_vcpu_blocking,
  5814. .vcpu_unblocking = svm_vcpu_unblocking,
  5815. .update_bp_intercept = update_bp_intercept,
  5816. .get_msr_feature = svm_get_msr_feature,
  5817. .get_msr = svm_get_msr,
  5818. .set_msr = svm_set_msr,
  5819. .get_segment_base = svm_get_segment_base,
  5820. .get_segment = svm_get_segment,
  5821. .set_segment = svm_set_segment,
  5822. .get_cpl = svm_get_cpl,
  5823. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  5824. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  5825. .decache_cr3 = svm_decache_cr3,
  5826. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  5827. .set_cr0 = svm_set_cr0,
  5828. .set_cr3 = svm_set_cr3,
  5829. .set_cr4 = svm_set_cr4,
  5830. .set_efer = svm_set_efer,
  5831. .get_idt = svm_get_idt,
  5832. .set_idt = svm_set_idt,
  5833. .get_gdt = svm_get_gdt,
  5834. .set_gdt = svm_set_gdt,
  5835. .get_dr6 = svm_get_dr6,
  5836. .set_dr6 = svm_set_dr6,
  5837. .set_dr7 = svm_set_dr7,
  5838. .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
  5839. .cache_reg = svm_cache_reg,
  5840. .get_rflags = svm_get_rflags,
  5841. .set_rflags = svm_set_rflags,
  5842. .tlb_flush = svm_flush_tlb,
  5843. .tlb_flush_gva = svm_flush_tlb_gva,
  5844. .run = svm_vcpu_run,
  5845. .handle_exit = handle_exit,
  5846. .skip_emulated_instruction = skip_emulated_instruction,
  5847. .set_interrupt_shadow = svm_set_interrupt_shadow,
  5848. .get_interrupt_shadow = svm_get_interrupt_shadow,
  5849. .patch_hypercall = svm_patch_hypercall,
  5850. .set_irq = svm_set_irq,
  5851. .set_nmi = svm_inject_nmi,
  5852. .queue_exception = svm_queue_exception,
  5853. .cancel_injection = svm_cancel_injection,
  5854. .interrupt_allowed = svm_interrupt_allowed,
  5855. .nmi_allowed = svm_nmi_allowed,
  5856. .get_nmi_mask = svm_get_nmi_mask,
  5857. .set_nmi_mask = svm_set_nmi_mask,
  5858. .enable_nmi_window = enable_nmi_window,
  5859. .enable_irq_window = enable_irq_window,
  5860. .update_cr8_intercept = update_cr8_intercept,
  5861. .set_virtual_apic_mode = svm_set_virtual_apic_mode,
  5862. .get_enable_apicv = svm_get_enable_apicv,
  5863. .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
  5864. .load_eoi_exitmap = svm_load_eoi_exitmap,
  5865. .hwapic_irr_update = svm_hwapic_irr_update,
  5866. .hwapic_isr_update = svm_hwapic_isr_update,
  5867. .sync_pir_to_irr = kvm_lapic_find_highest_irr,
  5868. .apicv_post_state_restore = avic_post_state_restore,
  5869. .set_tss_addr = svm_set_tss_addr,
  5870. .set_identity_map_addr = svm_set_identity_map_addr,
  5871. .get_tdp_level = get_npt_level,
  5872. .get_mt_mask = svm_get_mt_mask,
  5873. .get_exit_info = svm_get_exit_info,
  5874. .get_lpage_level = svm_get_lpage_level,
  5875. .cpuid_update = svm_cpuid_update,
  5876. .rdtscp_supported = svm_rdtscp_supported,
  5877. .invpcid_supported = svm_invpcid_supported,
  5878. .mpx_supported = svm_mpx_supported,
  5879. .xsaves_supported = svm_xsaves_supported,
  5880. .umip_emulated = svm_umip_emulated,
  5881. .set_supported_cpuid = svm_set_supported_cpuid,
  5882. .has_wbinvd_exit = svm_has_wbinvd_exit,
  5883. .read_l1_tsc_offset = svm_read_l1_tsc_offset,
  5884. .write_tsc_offset = svm_write_tsc_offset,
  5885. .set_tdp_cr3 = set_tdp_cr3,
  5886. .check_intercept = svm_check_intercept,
  5887. .handle_external_intr = svm_handle_external_intr,
  5888. .request_immediate_exit = __kvm_request_immediate_exit,
  5889. .sched_in = svm_sched_in,
  5890. .pmu_ops = &amd_pmu_ops,
  5891. .deliver_posted_interrupt = svm_deliver_avic_intr,
  5892. .update_pi_irte = svm_update_pi_irte,
  5893. .setup_mce = svm_setup_mce,
  5894. .smi_allowed = svm_smi_allowed,
  5895. .pre_enter_smm = svm_pre_enter_smm,
  5896. .pre_leave_smm = svm_pre_leave_smm,
  5897. .enable_smi_window = enable_smi_window,
  5898. .mem_enc_op = svm_mem_enc_op,
  5899. .mem_enc_reg_region = svm_register_enc_region,
  5900. .mem_enc_unreg_region = svm_unregister_enc_region,
  5901. .nested_enable_evmcs = nested_enable_evmcs,
  5902. };
  5903. static int __init svm_init(void)
  5904. {
  5905. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  5906. __alignof__(struct vcpu_svm), THIS_MODULE);
  5907. }
  5908. static void __exit svm_exit(void)
  5909. {
  5910. kvm_exit();
  5911. }
  5912. module_init(svm_init)
  5913. module_exit(svm_exit)