svm.c 185 KB

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