svm.c 183 KB

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