svm.c 176 KB

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