svm.c 182 KB

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