svm.c 182 KB

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