svm.c 175 KB

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