svm.c 185 KB

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