book3s_hv.c 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  4. * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  5. *
  6. * Authors:
  7. * Paul Mackerras <paulus@au1.ibm.com>
  8. * Alexander Graf <agraf@suse.de>
  9. * Kevin Wolf <mail@kevin-wolf.de>
  10. *
  11. * Description: KVM functions specific to running on Book 3S
  12. * processors in hypervisor mode (specifically POWER7 and later).
  13. *
  14. * This file is derived from arch/powerpc/kvm/book3s.c,
  15. * by Alexander Graf <agraf@suse.de>.
  16. */
  17. #include <linux/kvm_host.h>
  18. #include <linux/kernel.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/preempt.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/stat.h>
  24. #include <linux/delay.h>
  25. #include <linux/export.h>
  26. #include <linux/fs.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/cpu.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/page-flags.h>
  32. #include <linux/srcu.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/gfp.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/highmem.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/kvm_irqfd.h>
  40. #include <linux/irqbypass.h>
  41. #include <linux/module.h>
  42. #include <linux/compiler.h>
  43. #include <linux/of.h>
  44. #include <linux/irqdomain.h>
  45. #include <linux/smp.h>
  46. #include <asm/ftrace.h>
  47. #include <asm/reg.h>
  48. #include <asm/ppc-opcode.h>
  49. #include <asm/asm-prototypes.h>
  50. #include <asm/archrandom.h>
  51. #include <asm/debug.h>
  52. #include <asm/disassemble.h>
  53. #include <asm/cputable.h>
  54. #include <asm/cacheflush.h>
  55. #include <linux/uaccess.h>
  56. #include <asm/interrupt.h>
  57. #include <asm/io.h>
  58. #include <asm/kvm_ppc.h>
  59. #include <asm/kvm_book3s.h>
  60. #include <asm/mmu_context.h>
  61. #include <asm/lppaca.h>
  62. #include <asm/pmc.h>
  63. #include <asm/processor.h>
  64. #include <asm/cputhreads.h>
  65. #include <asm/page.h>
  66. #include <asm/hvcall.h>
  67. #include <asm/switch_to.h>
  68. #include <asm/smp.h>
  69. #include <asm/dbell.h>
  70. #include <asm/hmi.h>
  71. #include <asm/pnv-pci.h>
  72. #include <asm/mmu.h>
  73. #include <asm/opal.h>
  74. #include <asm/xics.h>
  75. #include <asm/xive.h>
  76. #include <asm/hw_breakpoint.h>
  77. #include <asm/kvm_book3s_uvmem.h>
  78. #include <asm/ultravisor.h>
  79. #include <asm/dtl.h>
  80. #include <asm/plpar_wrappers.h>
  81. #include <trace/events/ipi.h>
  82. #include "book3s.h"
  83. #include "book3s_hv.h"
  84. #define CREATE_TRACE_POINTS
  85. #include "trace_hv.h"
  86. /* #define EXIT_DEBUG */
  87. /* #define EXIT_DEBUG_SIMPLE */
  88. /* #define EXIT_DEBUG_INT */
  89. /* Used to indicate that a guest page fault needs to be handled */
  90. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  91. /* Used to indicate that a guest passthrough interrupt needs to be handled */
  92. #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
  93. /* Used as a "null" value for timebase values */
  94. #define TB_NIL (~(u64)0)
  95. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  96. static int dynamic_mt_modes = 6;
  97. module_param(dynamic_mt_modes, int, 0644);
  98. MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
  99. static int target_smt_mode;
  100. module_param(target_smt_mode, int, 0644);
  101. MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
  102. static bool one_vm_per_core;
  103. module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
  104. MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
  105. #ifdef CONFIG_KVM_XICS
  106. static const struct kernel_param_ops module_param_ops = {
  107. .set = param_set_int,
  108. .get = param_get_int,
  109. };
  110. module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
  111. MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
  112. module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
  113. MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
  114. #endif
  115. /* If set, guests are allowed to create and control nested guests */
  116. static bool nested = true;
  117. module_param(nested, bool, S_IRUGO | S_IWUSR);
  118. MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
  119. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  120. /*
  121. * RWMR values for POWER8. These control the rate at which PURR
  122. * and SPURR count and should be set according to the number of
  123. * online threads in the vcore being run.
  124. */
  125. #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
  126. #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
  127. #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
  128. #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
  129. #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
  130. #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
  131. #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
  132. #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
  133. static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
  134. RWMR_RPA_P8_1THREAD,
  135. RWMR_RPA_P8_1THREAD,
  136. RWMR_RPA_P8_2THREAD,
  137. RWMR_RPA_P8_3THREAD,
  138. RWMR_RPA_P8_4THREAD,
  139. RWMR_RPA_P8_5THREAD,
  140. RWMR_RPA_P8_6THREAD,
  141. RWMR_RPA_P8_7THREAD,
  142. RWMR_RPA_P8_8THREAD,
  143. };
  144. static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
  145. int *ip)
  146. {
  147. int i = *ip;
  148. struct kvm_vcpu *vcpu;
  149. while (++i < MAX_SMT_THREADS) {
  150. vcpu = READ_ONCE(vc->runnable_threads[i]);
  151. if (vcpu) {
  152. *ip = i;
  153. return vcpu;
  154. }
  155. }
  156. return NULL;
  157. }
  158. /* Used to traverse the list of runnable threads for a given vcore */
  159. #define for_each_runnable_thread(i, vcpu, vc) \
  160. for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
  161. static bool kvmppc_ipi_thread(int cpu)
  162. {
  163. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  164. /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
  165. if (kvmhv_on_pseries())
  166. return false;
  167. /* On POWER9 we can use msgsnd to IPI any cpu */
  168. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  169. msg |= get_hard_smp_processor_id(cpu);
  170. smp_mb();
  171. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  172. return true;
  173. }
  174. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  175. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  176. preempt_disable();
  177. if (cpu_first_thread_sibling(cpu) ==
  178. cpu_first_thread_sibling(smp_processor_id())) {
  179. msg |= cpu_thread_in_core(cpu);
  180. smp_mb();
  181. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  182. preempt_enable();
  183. return true;
  184. }
  185. preempt_enable();
  186. }
  187. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  188. if (cpu >= 0 && cpu < nr_cpu_ids) {
  189. if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
  190. xics_wake_cpu(cpu);
  191. return true;
  192. }
  193. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  194. return true;
  195. }
  196. #endif
  197. return false;
  198. }
  199. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  200. {
  201. int cpu;
  202. struct rcuwait *waitp;
  203. /*
  204. * rcuwait_wake_up contains smp_mb() which orders prior stores that
  205. * create pending work vs below loads of cpu fields. The other side
  206. * is the barrier in vcpu run that orders setting the cpu fields vs
  207. * testing for pending work.
  208. */
  209. waitp = kvm_arch_vcpu_get_wait(vcpu);
  210. if (rcuwait_wake_up(waitp))
  211. ++vcpu->stat.generic.halt_wakeup;
  212. cpu = READ_ONCE(vcpu->arch.thread_cpu);
  213. if (cpu >= 0 && kvmppc_ipi_thread(cpu))
  214. return;
  215. /* CPU points to the first thread of the core */
  216. cpu = vcpu->cpu;
  217. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  218. smp_send_reschedule(cpu);
  219. }
  220. /*
  221. * We use the vcpu_load/put functions to measure stolen time.
  222. *
  223. * Stolen time is counted as time when either the vcpu is able to
  224. * run as part of a virtual core, but the task running the vcore
  225. * is preempted or sleeping, or when the vcpu needs something done
  226. * in the kernel by the task running the vcpu, but that task is
  227. * preempted or sleeping. Those two things have to be counted
  228. * separately, since one of the vcpu tasks will take on the job
  229. * of running the core, and the other vcpu tasks in the vcore will
  230. * sleep waiting for it to do that, but that sleep shouldn't count
  231. * as stolen time.
  232. *
  233. * Hence we accumulate stolen time when the vcpu can run as part of
  234. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  235. * needs its task to do other things in the kernel (for example,
  236. * service a page fault) in busy_stolen. We don't accumulate
  237. * stolen time for a vcore when it is inactive, or for a vcpu
  238. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  239. * a misnomer; it means that the vcpu task is not executing in
  240. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  241. * the kernel. We don't have any way of dividing up that time
  242. * between time that the vcpu is genuinely stopped, time that
  243. * the task is actively working on behalf of the vcpu, and time
  244. * that the task is preempted, so we don't count any of it as
  245. * stolen.
  246. *
  247. * Updates to busy_stolen are protected by arch.tbacct_lock;
  248. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  249. * lock. The stolen times are measured in units of timebase ticks.
  250. * (Note that the != TB_NIL checks below are purely defensive;
  251. * they should never fail.)
  252. *
  253. * The POWER9 path is simpler, one vcpu per virtual core so the
  254. * former case does not exist. If a vcpu is preempted when it is
  255. * BUSY_IN_HOST and not ceded or otherwise blocked, then accumulate
  256. * the stolen cycles in busy_stolen. RUNNING is not a preemptible
  257. * state in the P9 path.
  258. */
  259. static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc, u64 tb)
  260. {
  261. unsigned long flags;
  262. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  263. spin_lock_irqsave(&vc->stoltb_lock, flags);
  264. vc->preempt_tb = tb;
  265. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  266. }
  267. static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc, u64 tb)
  268. {
  269. unsigned long flags;
  270. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  271. spin_lock_irqsave(&vc->stoltb_lock, flags);
  272. if (vc->preempt_tb != TB_NIL) {
  273. vc->stolen_tb += tb - vc->preempt_tb;
  274. vc->preempt_tb = TB_NIL;
  275. }
  276. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  277. }
  278. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  279. {
  280. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  281. unsigned long flags;
  282. u64 now;
  283. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  284. if (vcpu->arch.busy_preempt != TB_NIL) {
  285. WARN_ON_ONCE(vcpu->arch.state != KVMPPC_VCPU_BUSY_IN_HOST);
  286. vc->stolen_tb += mftb() - vcpu->arch.busy_preempt;
  287. vcpu->arch.busy_preempt = TB_NIL;
  288. }
  289. return;
  290. }
  291. now = mftb();
  292. /*
  293. * We can test vc->runner without taking the vcore lock,
  294. * because only this task ever sets vc->runner to this
  295. * vcpu, and once it is set to this vcpu, only this task
  296. * ever sets it to NULL.
  297. */
  298. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  299. kvmppc_core_end_stolen(vc, now);
  300. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  301. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  302. vcpu->arch.busy_preempt != TB_NIL) {
  303. vcpu->arch.busy_stolen += now - vcpu->arch.busy_preempt;
  304. vcpu->arch.busy_preempt = TB_NIL;
  305. }
  306. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  307. }
  308. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  309. {
  310. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  311. unsigned long flags;
  312. u64 now;
  313. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  314. /*
  315. * In the P9 path, RUNNABLE is not preemptible
  316. * (nor takes host interrupts)
  317. */
  318. WARN_ON_ONCE(vcpu->arch.state == KVMPPC_VCPU_RUNNABLE);
  319. /*
  320. * Account stolen time when preempted while the vcpu task is
  321. * running in the kernel (but not in qemu, which is INACTIVE).
  322. */
  323. if (task_is_running(current) &&
  324. vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  325. vcpu->arch.busy_preempt = mftb();
  326. return;
  327. }
  328. now = mftb();
  329. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  330. kvmppc_core_start_stolen(vc, now);
  331. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  332. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  333. vcpu->arch.busy_preempt = now;
  334. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  335. }
  336. static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  337. {
  338. vcpu->arch.pvr = pvr;
  339. }
  340. /* Dummy value used in computing PCR value below */
  341. #define PCR_ARCH_31 (PCR_ARCH_300 << 1)
  342. static inline unsigned long map_pcr_to_cap(unsigned long pcr)
  343. {
  344. unsigned long cap = 0;
  345. switch (pcr) {
  346. case PCR_ARCH_300:
  347. cap = H_GUEST_CAP_POWER9;
  348. break;
  349. case PCR_ARCH_31:
  350. cap = H_GUEST_CAP_POWER10;
  351. break;
  352. default:
  353. break;
  354. }
  355. return cap;
  356. }
  357. static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  358. {
  359. unsigned long host_pcr_bit = 0, guest_pcr_bit = 0, cap = 0;
  360. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  361. /* We can (emulate) our own architecture version and anything older */
  362. if (cpu_has_feature(CPU_FTR_ARCH_31))
  363. host_pcr_bit = PCR_ARCH_31;
  364. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  365. host_pcr_bit = PCR_ARCH_300;
  366. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  367. host_pcr_bit = PCR_ARCH_207;
  368. else if (cpu_has_feature(CPU_FTR_ARCH_206))
  369. host_pcr_bit = PCR_ARCH_206;
  370. else
  371. host_pcr_bit = PCR_ARCH_205;
  372. /* Determine lowest PCR bit needed to run guest in given PVR level */
  373. guest_pcr_bit = host_pcr_bit;
  374. if (arch_compat) {
  375. switch (arch_compat) {
  376. case PVR_ARCH_205:
  377. guest_pcr_bit = PCR_ARCH_205;
  378. break;
  379. case PVR_ARCH_206:
  380. case PVR_ARCH_206p:
  381. guest_pcr_bit = PCR_ARCH_206;
  382. break;
  383. case PVR_ARCH_207:
  384. guest_pcr_bit = PCR_ARCH_207;
  385. break;
  386. case PVR_ARCH_300:
  387. guest_pcr_bit = PCR_ARCH_300;
  388. break;
  389. case PVR_ARCH_31:
  390. case PVR_ARCH_31_P11:
  391. guest_pcr_bit = PCR_ARCH_31;
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. }
  397. /* Check requested PCR bits don't exceed our capabilities */
  398. if (guest_pcr_bit > host_pcr_bit)
  399. return -EINVAL;
  400. if (kvmhv_on_pseries() && kvmhv_is_nestedv2()) {
  401. /*
  402. * 'arch_compat == 0' would mean the guest should default to
  403. * L1's compatibility. In this case, the guest would pick
  404. * host's PCR and evaluate the corresponding capabilities.
  405. */
  406. cap = map_pcr_to_cap(guest_pcr_bit);
  407. if (!(cap & nested_capabilities))
  408. return -EINVAL;
  409. }
  410. spin_lock(&vc->lock);
  411. vc->arch_compat = arch_compat;
  412. kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LOGICAL_PVR);
  413. /*
  414. * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
  415. * Also set all reserved PCR bits
  416. */
  417. vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
  418. spin_unlock(&vc->lock);
  419. return 0;
  420. }
  421. static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  422. {
  423. int r;
  424. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  425. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  426. vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
  427. for (r = 0; r < 16; ++r)
  428. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  429. r, kvmppc_get_gpr(vcpu, r),
  430. r+16, kvmppc_get_gpr(vcpu, r+16));
  431. pr_err("ctr = %.16lx lr = %.16lx\n",
  432. vcpu->arch.regs.ctr, vcpu->arch.regs.link);
  433. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  434. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  435. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  436. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  437. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  438. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  439. pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
  440. vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
  441. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  442. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  443. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  444. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  445. for (r = 0; r < vcpu->arch.slb_max; ++r)
  446. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  447. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  448. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.16lx\n",
  449. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  450. vcpu->arch.last_inst);
  451. }
  452. static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  453. {
  454. return kvm_get_vcpu_by_id(kvm, id);
  455. }
  456. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  457. {
  458. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  459. vpa->yield_count = cpu_to_be32(1);
  460. }
  461. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  462. unsigned long addr, unsigned long len)
  463. {
  464. /* check address is cacheline aligned */
  465. if (addr & (L1_CACHE_BYTES - 1))
  466. return -EINVAL;
  467. spin_lock(&vcpu->arch.vpa_update_lock);
  468. if (v->next_gpa != addr || v->len != len) {
  469. v->next_gpa = addr;
  470. v->len = addr ? len : 0;
  471. v->update_pending = 1;
  472. }
  473. spin_unlock(&vcpu->arch.vpa_update_lock);
  474. return 0;
  475. }
  476. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  477. struct reg_vpa {
  478. u32 dummy;
  479. union {
  480. __be16 hword;
  481. __be32 word;
  482. } length;
  483. };
  484. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  485. {
  486. if (vpap->update_pending)
  487. return vpap->next_gpa != 0;
  488. return vpap->pinned_addr != NULL;
  489. }
  490. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  491. unsigned long flags,
  492. unsigned long vcpuid, unsigned long vpa)
  493. {
  494. struct kvm *kvm = vcpu->kvm;
  495. unsigned long len, nb;
  496. void *va;
  497. struct kvm_vcpu *tvcpu;
  498. int err;
  499. int subfunc;
  500. struct kvmppc_vpa *vpap;
  501. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  502. if (!tvcpu)
  503. return H_PARAMETER;
  504. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  505. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  506. subfunc == H_VPA_REG_SLB) {
  507. /* Registering new area - address must be cache-line aligned */
  508. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  509. return H_PARAMETER;
  510. /* convert logical addr to kernel addr and read length */
  511. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  512. if (va == NULL)
  513. return H_PARAMETER;
  514. if (subfunc == H_VPA_REG_VPA)
  515. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  516. else
  517. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  518. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  519. /* Check length */
  520. if (len > nb || len < sizeof(struct reg_vpa))
  521. return H_PARAMETER;
  522. } else {
  523. vpa = 0;
  524. len = 0;
  525. }
  526. err = H_PARAMETER;
  527. vpap = NULL;
  528. spin_lock(&tvcpu->arch.vpa_update_lock);
  529. switch (subfunc) {
  530. case H_VPA_REG_VPA: /* register VPA */
  531. /*
  532. * The size of our lppaca is 1kB because of the way we align
  533. * it for the guest to avoid crossing a 4kB boundary. We only
  534. * use 640 bytes of the structure though, so we should accept
  535. * clients that set a size of 640.
  536. */
  537. BUILD_BUG_ON(sizeof(struct lppaca) != 640);
  538. if (len < sizeof(struct lppaca))
  539. break;
  540. vpap = &tvcpu->arch.vpa;
  541. err = 0;
  542. break;
  543. case H_VPA_REG_DTL: /* register DTL */
  544. if (len < sizeof(struct dtl_entry))
  545. break;
  546. len -= len % sizeof(struct dtl_entry);
  547. /* Check that they have previously registered a VPA */
  548. err = H_RESOURCE;
  549. if (!vpa_is_registered(&tvcpu->arch.vpa))
  550. break;
  551. vpap = &tvcpu->arch.dtl;
  552. err = 0;
  553. break;
  554. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  555. /* Check that they have previously registered a VPA */
  556. err = H_RESOURCE;
  557. if (!vpa_is_registered(&tvcpu->arch.vpa))
  558. break;
  559. vpap = &tvcpu->arch.slb_shadow;
  560. err = 0;
  561. break;
  562. case H_VPA_DEREG_VPA: /* deregister VPA */
  563. /* Check they don't still have a DTL or SLB buf registered */
  564. err = H_RESOURCE;
  565. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  566. vpa_is_registered(&tvcpu->arch.slb_shadow))
  567. break;
  568. vpap = &tvcpu->arch.vpa;
  569. err = 0;
  570. break;
  571. case H_VPA_DEREG_DTL: /* deregister DTL */
  572. vpap = &tvcpu->arch.dtl;
  573. err = 0;
  574. break;
  575. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  576. vpap = &tvcpu->arch.slb_shadow;
  577. err = 0;
  578. break;
  579. }
  580. if (vpap) {
  581. vpap->next_gpa = vpa;
  582. vpap->len = len;
  583. vpap->update_pending = 1;
  584. }
  585. spin_unlock(&tvcpu->arch.vpa_update_lock);
  586. return err;
  587. }
  588. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap,
  589. struct kvmppc_vpa *old_vpap)
  590. {
  591. struct kvm *kvm = vcpu->kvm;
  592. void *va;
  593. unsigned long nb;
  594. unsigned long gpa;
  595. /*
  596. * We need to pin the page pointed to by vpap->next_gpa,
  597. * but we can't call kvmppc_pin_guest_page under the lock
  598. * as it does get_user_pages() and down_read(). So we
  599. * have to drop the lock, pin the page, then get the lock
  600. * again and check that a new area didn't get registered
  601. * in the meantime.
  602. */
  603. for (;;) {
  604. gpa = vpap->next_gpa;
  605. spin_unlock(&vcpu->arch.vpa_update_lock);
  606. va = NULL;
  607. nb = 0;
  608. if (gpa)
  609. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  610. spin_lock(&vcpu->arch.vpa_update_lock);
  611. if (gpa == vpap->next_gpa)
  612. break;
  613. /* sigh... unpin that one and try again */
  614. if (va)
  615. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  616. }
  617. vpap->update_pending = 0;
  618. if (va && nb < vpap->len) {
  619. /*
  620. * If it's now too short, it must be that userspace
  621. * has changed the mappings underlying guest memory,
  622. * so unregister the region.
  623. */
  624. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  625. va = NULL;
  626. }
  627. *old_vpap = *vpap;
  628. vpap->gpa = gpa;
  629. vpap->pinned_addr = va;
  630. vpap->dirty = false;
  631. if (va)
  632. vpap->pinned_end = va + vpap->len;
  633. }
  634. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  635. {
  636. struct kvm *kvm = vcpu->kvm;
  637. struct kvmppc_vpa old_vpa = { 0 };
  638. if (!(vcpu->arch.vpa.update_pending ||
  639. vcpu->arch.slb_shadow.update_pending ||
  640. vcpu->arch.dtl.update_pending))
  641. return;
  642. spin_lock(&vcpu->arch.vpa_update_lock);
  643. if (vcpu->arch.vpa.update_pending) {
  644. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa, &old_vpa);
  645. if (old_vpa.pinned_addr) {
  646. if (kvmhv_is_nestedv2())
  647. kvmhv_nestedv2_set_vpa(vcpu, ~0ull);
  648. kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
  649. old_vpa.dirty);
  650. }
  651. if (vcpu->arch.vpa.pinned_addr) {
  652. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  653. if (kvmhv_is_nestedv2())
  654. kvmhv_nestedv2_set_vpa(vcpu, __pa(vcpu->arch.vpa.pinned_addr));
  655. }
  656. }
  657. if (vcpu->arch.dtl.update_pending) {
  658. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl, &old_vpa);
  659. if (old_vpa.pinned_addr)
  660. kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
  661. old_vpa.dirty);
  662. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  663. vcpu->arch.dtl_index = 0;
  664. }
  665. if (vcpu->arch.slb_shadow.update_pending) {
  666. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow, &old_vpa);
  667. if (old_vpa.pinned_addr)
  668. kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
  669. old_vpa.dirty);
  670. }
  671. spin_unlock(&vcpu->arch.vpa_update_lock);
  672. }
  673. /*
  674. * Return the accumulated stolen time for the vcore up until `now'.
  675. * The caller should hold the vcore lock.
  676. */
  677. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  678. {
  679. u64 p;
  680. unsigned long flags;
  681. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  682. spin_lock_irqsave(&vc->stoltb_lock, flags);
  683. p = vc->stolen_tb;
  684. if (vc->vcore_state != VCORE_INACTIVE &&
  685. vc->preempt_tb != TB_NIL)
  686. p += now - vc->preempt_tb;
  687. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  688. return p;
  689. }
  690. static void __kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  691. struct lppaca *vpa,
  692. unsigned int pcpu, u64 now,
  693. unsigned long stolen)
  694. {
  695. struct dtl_entry *dt;
  696. dt = vcpu->arch.dtl_ptr;
  697. if (!dt)
  698. return;
  699. dt->dispatch_reason = 7;
  700. dt->preempt_reason = 0;
  701. dt->processor_id = cpu_to_be16(pcpu + vcpu->arch.ptid);
  702. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  703. dt->ready_to_enqueue_time = 0;
  704. dt->waiting_to_ready_time = 0;
  705. dt->timebase = cpu_to_be64(now);
  706. dt->fault_addr = 0;
  707. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  708. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  709. ++dt;
  710. if (dt == vcpu->arch.dtl.pinned_end)
  711. dt = vcpu->arch.dtl.pinned_addr;
  712. vcpu->arch.dtl_ptr = dt;
  713. /* order writing *dt vs. writing vpa->dtl_idx */
  714. smp_wmb();
  715. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  716. /* vcpu->arch.dtl.dirty is set by the caller */
  717. }
  718. static void kvmppc_update_vpa_dispatch(struct kvm_vcpu *vcpu,
  719. struct kvmppc_vcore *vc)
  720. {
  721. struct lppaca *vpa;
  722. unsigned long stolen;
  723. unsigned long core_stolen;
  724. u64 now;
  725. unsigned long flags;
  726. vpa = vcpu->arch.vpa.pinned_addr;
  727. if (!vpa)
  728. return;
  729. now = mftb();
  730. core_stolen = vcore_stolen_time(vc, now);
  731. stolen = core_stolen - vcpu->arch.stolen_logged;
  732. vcpu->arch.stolen_logged = core_stolen;
  733. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  734. stolen += vcpu->arch.busy_stolen;
  735. vcpu->arch.busy_stolen = 0;
  736. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  737. vpa->enqueue_dispatch_tb = cpu_to_be64(be64_to_cpu(vpa->enqueue_dispatch_tb) + stolen);
  738. __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now + kvmppc_get_tb_offset(vcpu), stolen);
  739. vcpu->arch.vpa.dirty = true;
  740. }
  741. static void kvmppc_update_vpa_dispatch_p9(struct kvm_vcpu *vcpu,
  742. struct kvmppc_vcore *vc,
  743. u64 now)
  744. {
  745. struct lppaca *vpa;
  746. unsigned long stolen;
  747. unsigned long stolen_delta;
  748. vpa = vcpu->arch.vpa.pinned_addr;
  749. if (!vpa)
  750. return;
  751. stolen = vc->stolen_tb;
  752. stolen_delta = stolen - vcpu->arch.stolen_logged;
  753. vcpu->arch.stolen_logged = stolen;
  754. vpa->enqueue_dispatch_tb = cpu_to_be64(stolen);
  755. __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now, stolen_delta);
  756. vcpu->arch.vpa.dirty = true;
  757. }
  758. /* See if there is a doorbell interrupt pending for a vcpu */
  759. static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
  760. {
  761. int thr;
  762. struct kvmppc_vcore *vc;
  763. if (vcpu->arch.doorbell_request)
  764. return true;
  765. if (cpu_has_feature(CPU_FTR_ARCH_300))
  766. return false;
  767. /*
  768. * Ensure that the read of vcore->dpdes comes after the read
  769. * of vcpu->doorbell_request. This barrier matches the
  770. * smp_wmb() in kvmppc_guest_entry_inject().
  771. */
  772. smp_rmb();
  773. vc = vcpu->arch.vcore;
  774. thr = vcpu->vcpu_id - vc->first_vcpuid;
  775. return !!(vc->dpdes & (1 << thr));
  776. }
  777. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  778. {
  779. if (kvmppc_get_arch_compat(vcpu) >= PVR_ARCH_207)
  780. return true;
  781. if ((!kvmppc_get_arch_compat(vcpu)) &&
  782. cpu_has_feature(CPU_FTR_ARCH_207S))
  783. return true;
  784. return false;
  785. }
  786. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  787. unsigned long resource, unsigned long value1,
  788. unsigned long value2)
  789. {
  790. switch (resource) {
  791. case H_SET_MODE_RESOURCE_SET_CIABR:
  792. if (!kvmppc_power8_compatible(vcpu))
  793. return H_P2;
  794. if (value2)
  795. return H_P4;
  796. if (mflags)
  797. return H_UNSUPPORTED_FLAG_START;
  798. /* Guests can't breakpoint the hypervisor */
  799. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  800. return H_P3;
  801. kvmppc_set_ciabr_hv(vcpu, value1);
  802. return H_SUCCESS;
  803. case H_SET_MODE_RESOURCE_SET_DAWR0:
  804. if (!kvmppc_power8_compatible(vcpu))
  805. return H_P2;
  806. if (!ppc_breakpoint_available())
  807. return H_P2;
  808. if (mflags)
  809. return H_UNSUPPORTED_FLAG_START;
  810. if (value2 & DABRX_HYP)
  811. return H_P4;
  812. kvmppc_set_dawr0_hv(vcpu, value1);
  813. kvmppc_set_dawrx0_hv(vcpu, value2);
  814. return H_SUCCESS;
  815. case H_SET_MODE_RESOURCE_SET_DAWR1:
  816. if (!kvmppc_power8_compatible(vcpu))
  817. return H_P2;
  818. if (!ppc_breakpoint_available())
  819. return H_P2;
  820. if (!cpu_has_feature(CPU_FTR_DAWR1))
  821. return H_P2;
  822. if (!vcpu->kvm->arch.dawr1_enabled)
  823. return H_FUNCTION;
  824. if (mflags)
  825. return H_UNSUPPORTED_FLAG_START;
  826. if (value2 & DABRX_HYP)
  827. return H_P4;
  828. kvmppc_set_dawr1_hv(vcpu, value1);
  829. kvmppc_set_dawrx1_hv(vcpu, value2);
  830. return H_SUCCESS;
  831. case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
  832. /*
  833. * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
  834. * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
  835. */
  836. if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
  837. kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
  838. return H_UNSUPPORTED_FLAG_START;
  839. return H_TOO_HARD;
  840. default:
  841. return H_TOO_HARD;
  842. }
  843. }
  844. /* Copy guest memory in place - must reside within a single memslot */
  845. static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
  846. unsigned long len)
  847. {
  848. struct kvm_memory_slot *to_memslot = NULL;
  849. struct kvm_memory_slot *from_memslot = NULL;
  850. unsigned long to_addr, from_addr;
  851. int r;
  852. /* Get HPA for from address */
  853. from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
  854. if (!from_memslot)
  855. return -EFAULT;
  856. if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
  857. << PAGE_SHIFT))
  858. return -EINVAL;
  859. from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
  860. if (kvm_is_error_hva(from_addr))
  861. return -EFAULT;
  862. from_addr |= (from & (PAGE_SIZE - 1));
  863. /* Get HPA for to address */
  864. to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
  865. if (!to_memslot)
  866. return -EFAULT;
  867. if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
  868. << PAGE_SHIFT))
  869. return -EINVAL;
  870. to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
  871. if (kvm_is_error_hva(to_addr))
  872. return -EFAULT;
  873. to_addr |= (to & (PAGE_SIZE - 1));
  874. /* Perform copy */
  875. r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
  876. len);
  877. if (r)
  878. return -EFAULT;
  879. mark_page_dirty(kvm, to >> PAGE_SHIFT);
  880. return 0;
  881. }
  882. static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
  883. unsigned long dest, unsigned long src)
  884. {
  885. u64 pg_sz = SZ_4K; /* 4K page size */
  886. u64 pg_mask = SZ_4K - 1;
  887. int ret;
  888. /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
  889. if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
  890. H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
  891. return H_PARAMETER;
  892. /* dest (and src if copy_page flag set) must be page aligned */
  893. if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
  894. return H_PARAMETER;
  895. /* zero and/or copy the page as determined by the flags */
  896. if (flags & H_COPY_PAGE) {
  897. ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
  898. if (ret < 0)
  899. return H_PARAMETER;
  900. } else if (flags & H_ZERO_PAGE) {
  901. ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
  902. if (ret < 0)
  903. return H_PARAMETER;
  904. }
  905. /* We can ignore the remaining flags */
  906. return H_SUCCESS;
  907. }
  908. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  909. {
  910. struct kvmppc_vcore *vcore = target->arch.vcore;
  911. /*
  912. * We expect to have been called by the real mode handler
  913. * (kvmppc_rm_h_confer()) which would have directly returned
  914. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  915. * have useful work to do and should not confer) so we don't
  916. * recheck that here.
  917. *
  918. * In the case of the P9 single vcpu per vcore case, the real
  919. * mode handler is not called but no other threads are in the
  920. * source vcore.
  921. */
  922. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  923. spin_lock(&vcore->lock);
  924. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  925. vcore->vcore_state != VCORE_INACTIVE &&
  926. vcore->runner)
  927. target = vcore->runner;
  928. spin_unlock(&vcore->lock);
  929. }
  930. return kvm_vcpu_yield_to(target);
  931. }
  932. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  933. {
  934. int yield_count = 0;
  935. struct lppaca *lppaca;
  936. spin_lock(&vcpu->arch.vpa_update_lock);
  937. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  938. if (lppaca)
  939. yield_count = be32_to_cpu(lppaca->yield_count);
  940. spin_unlock(&vcpu->arch.vpa_update_lock);
  941. return yield_count;
  942. }
  943. /*
  944. * H_RPT_INVALIDATE hcall handler for nested guests.
  945. *
  946. * Handles only nested process-scoped invalidation requests in L0.
  947. */
  948. static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
  949. {
  950. unsigned long type = kvmppc_get_gpr(vcpu, 6);
  951. unsigned long pid, pg_sizes, start, end;
  952. /*
  953. * The partition-scoped invalidations aren't handled here in L0.
  954. */
  955. if (type & H_RPTI_TYPE_NESTED)
  956. return RESUME_HOST;
  957. pid = kvmppc_get_gpr(vcpu, 4);
  958. pg_sizes = kvmppc_get_gpr(vcpu, 7);
  959. start = kvmppc_get_gpr(vcpu, 8);
  960. end = kvmppc_get_gpr(vcpu, 9);
  961. do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
  962. type, pg_sizes, start, end);
  963. kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
  964. return RESUME_GUEST;
  965. }
  966. static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
  967. unsigned long id, unsigned long target,
  968. unsigned long type, unsigned long pg_sizes,
  969. unsigned long start, unsigned long end)
  970. {
  971. if (!kvm_is_radix(vcpu->kvm))
  972. return H_UNSUPPORTED;
  973. if (end < start)
  974. return H_P5;
  975. /*
  976. * Partition-scoped invalidation for nested guests.
  977. */
  978. if (type & H_RPTI_TYPE_NESTED) {
  979. if (!nesting_enabled(vcpu->kvm))
  980. return H_FUNCTION;
  981. /* Support only cores as target */
  982. if (target != H_RPTI_TARGET_CMMU)
  983. return H_P2;
  984. return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
  985. start, end);
  986. }
  987. /*
  988. * Process-scoped invalidation for L1 guests.
  989. */
  990. do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
  991. type, pg_sizes, start, end);
  992. return H_SUCCESS;
  993. }
  994. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  995. {
  996. struct kvm *kvm = vcpu->kvm;
  997. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  998. unsigned long target, ret = H_SUCCESS;
  999. int yield_count;
  1000. struct kvm_vcpu *tvcpu;
  1001. int idx, rc;
  1002. if (req <= MAX_HCALL_OPCODE &&
  1003. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  1004. return RESUME_HOST;
  1005. switch (req) {
  1006. case H_REMOVE:
  1007. ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4),
  1008. kvmppc_get_gpr(vcpu, 5),
  1009. kvmppc_get_gpr(vcpu, 6));
  1010. if (ret == H_TOO_HARD)
  1011. return RESUME_HOST;
  1012. break;
  1013. case H_ENTER:
  1014. ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
  1015. kvmppc_get_gpr(vcpu, 5),
  1016. kvmppc_get_gpr(vcpu, 6),
  1017. kvmppc_get_gpr(vcpu, 7));
  1018. if (ret == H_TOO_HARD)
  1019. return RESUME_HOST;
  1020. break;
  1021. case H_READ:
  1022. ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
  1023. kvmppc_get_gpr(vcpu, 5));
  1024. if (ret == H_TOO_HARD)
  1025. return RESUME_HOST;
  1026. break;
  1027. case H_CLEAR_MOD:
  1028. ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
  1029. kvmppc_get_gpr(vcpu, 5));
  1030. if (ret == H_TOO_HARD)
  1031. return RESUME_HOST;
  1032. break;
  1033. case H_CLEAR_REF:
  1034. ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
  1035. kvmppc_get_gpr(vcpu, 5));
  1036. if (ret == H_TOO_HARD)
  1037. return RESUME_HOST;
  1038. break;
  1039. case H_PROTECT:
  1040. ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
  1041. kvmppc_get_gpr(vcpu, 5),
  1042. kvmppc_get_gpr(vcpu, 6));
  1043. if (ret == H_TOO_HARD)
  1044. return RESUME_HOST;
  1045. break;
  1046. case H_BULK_REMOVE:
  1047. ret = kvmppc_h_bulk_remove(vcpu);
  1048. if (ret == H_TOO_HARD)
  1049. return RESUME_HOST;
  1050. break;
  1051. case H_CEDE:
  1052. break;
  1053. case H_PROD:
  1054. target = kvmppc_get_gpr(vcpu, 4);
  1055. tvcpu = kvmppc_find_vcpu(kvm, target);
  1056. if (!tvcpu) {
  1057. ret = H_PARAMETER;
  1058. break;
  1059. }
  1060. tvcpu->arch.prodded = 1;
  1061. smp_mb(); /* This orders prodded store vs ceded load */
  1062. if (tvcpu->arch.ceded)
  1063. kvmppc_fast_vcpu_kick_hv(tvcpu);
  1064. break;
  1065. case H_CONFER:
  1066. target = kvmppc_get_gpr(vcpu, 4);
  1067. if (target == -1)
  1068. break;
  1069. tvcpu = kvmppc_find_vcpu(kvm, target);
  1070. if (!tvcpu) {
  1071. ret = H_PARAMETER;
  1072. break;
  1073. }
  1074. yield_count = kvmppc_get_gpr(vcpu, 5);
  1075. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  1076. break;
  1077. kvm_arch_vcpu_yield_to(tvcpu);
  1078. break;
  1079. case H_REGISTER_VPA:
  1080. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  1081. kvmppc_get_gpr(vcpu, 5),
  1082. kvmppc_get_gpr(vcpu, 6));
  1083. break;
  1084. case H_RTAS:
  1085. if (list_empty(&kvm->arch.rtas_tokens))
  1086. return RESUME_HOST;
  1087. idx = srcu_read_lock(&kvm->srcu);
  1088. rc = kvmppc_rtas_hcall(vcpu);
  1089. srcu_read_unlock(&kvm->srcu, idx);
  1090. if (rc == -ENOENT)
  1091. return RESUME_HOST;
  1092. else if (rc == 0)
  1093. break;
  1094. /* Send the error out to userspace via KVM_RUN */
  1095. return rc;
  1096. case H_LOGICAL_CI_LOAD:
  1097. ret = kvmppc_h_logical_ci_load(vcpu);
  1098. if (ret == H_TOO_HARD)
  1099. return RESUME_HOST;
  1100. break;
  1101. case H_LOGICAL_CI_STORE:
  1102. ret = kvmppc_h_logical_ci_store(vcpu);
  1103. if (ret == H_TOO_HARD)
  1104. return RESUME_HOST;
  1105. break;
  1106. case H_SET_MODE:
  1107. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  1108. kvmppc_get_gpr(vcpu, 5),
  1109. kvmppc_get_gpr(vcpu, 6),
  1110. kvmppc_get_gpr(vcpu, 7));
  1111. if (ret == H_TOO_HARD)
  1112. return RESUME_HOST;
  1113. break;
  1114. case H_XIRR:
  1115. case H_CPPR:
  1116. case H_EOI:
  1117. case H_IPI:
  1118. case H_IPOLL:
  1119. case H_XIRR_X:
  1120. if (kvmppc_xics_enabled(vcpu)) {
  1121. if (xics_on_xive()) {
  1122. ret = H_NOT_AVAILABLE;
  1123. return RESUME_GUEST;
  1124. }
  1125. ret = kvmppc_xics_hcall(vcpu, req);
  1126. break;
  1127. }
  1128. return RESUME_HOST;
  1129. case H_SET_DABR:
  1130. ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
  1131. break;
  1132. case H_SET_XDABR:
  1133. ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
  1134. kvmppc_get_gpr(vcpu, 5));
  1135. break;
  1136. #ifdef CONFIG_SPAPR_TCE_IOMMU
  1137. case H_GET_TCE:
  1138. ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  1139. kvmppc_get_gpr(vcpu, 5));
  1140. if (ret == H_TOO_HARD)
  1141. return RESUME_HOST;
  1142. break;
  1143. case H_PUT_TCE:
  1144. ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  1145. kvmppc_get_gpr(vcpu, 5),
  1146. kvmppc_get_gpr(vcpu, 6));
  1147. if (ret == H_TOO_HARD)
  1148. return RESUME_HOST;
  1149. break;
  1150. case H_PUT_TCE_INDIRECT:
  1151. ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
  1152. kvmppc_get_gpr(vcpu, 5),
  1153. kvmppc_get_gpr(vcpu, 6),
  1154. kvmppc_get_gpr(vcpu, 7));
  1155. if (ret == H_TOO_HARD)
  1156. return RESUME_HOST;
  1157. break;
  1158. case H_STUFF_TCE:
  1159. ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  1160. kvmppc_get_gpr(vcpu, 5),
  1161. kvmppc_get_gpr(vcpu, 6),
  1162. kvmppc_get_gpr(vcpu, 7));
  1163. if (ret == H_TOO_HARD)
  1164. return RESUME_HOST;
  1165. break;
  1166. #endif
  1167. case H_RANDOM: {
  1168. unsigned long rand;
  1169. if (!arch_get_random_seed_longs(&rand, 1))
  1170. ret = H_HARDWARE;
  1171. kvmppc_set_gpr(vcpu, 4, rand);
  1172. break;
  1173. }
  1174. case H_RPT_INVALIDATE:
  1175. ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
  1176. kvmppc_get_gpr(vcpu, 5),
  1177. kvmppc_get_gpr(vcpu, 6),
  1178. kvmppc_get_gpr(vcpu, 7),
  1179. kvmppc_get_gpr(vcpu, 8),
  1180. kvmppc_get_gpr(vcpu, 9));
  1181. break;
  1182. case H_SET_PARTITION_TABLE:
  1183. ret = H_FUNCTION;
  1184. if (nesting_enabled(kvm))
  1185. ret = kvmhv_set_partition_table(vcpu);
  1186. break;
  1187. case H_ENTER_NESTED:
  1188. ret = H_FUNCTION;
  1189. if (!nesting_enabled(kvm))
  1190. break;
  1191. ret = kvmhv_enter_nested_guest(vcpu);
  1192. if (ret == H_INTERRUPT) {
  1193. kvmppc_set_gpr(vcpu, 3, 0);
  1194. vcpu->arch.hcall_needed = 0;
  1195. return -EINTR;
  1196. } else if (ret == H_TOO_HARD) {
  1197. kvmppc_set_gpr(vcpu, 3, 0);
  1198. vcpu->arch.hcall_needed = 0;
  1199. return RESUME_HOST;
  1200. }
  1201. break;
  1202. case H_TLB_INVALIDATE:
  1203. ret = H_FUNCTION;
  1204. if (nesting_enabled(kvm))
  1205. ret = kvmhv_do_nested_tlbie(vcpu);
  1206. break;
  1207. case H_COPY_TOFROM_GUEST:
  1208. ret = H_FUNCTION;
  1209. if (nesting_enabled(kvm))
  1210. ret = kvmhv_copy_tofrom_guest_nested(vcpu);
  1211. break;
  1212. case H_PAGE_INIT:
  1213. ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
  1214. kvmppc_get_gpr(vcpu, 5),
  1215. kvmppc_get_gpr(vcpu, 6));
  1216. break;
  1217. case H_SVM_PAGE_IN:
  1218. ret = H_UNSUPPORTED;
  1219. if (kvmppc_get_srr1(vcpu) & MSR_S)
  1220. ret = kvmppc_h_svm_page_in(kvm,
  1221. kvmppc_get_gpr(vcpu, 4),
  1222. kvmppc_get_gpr(vcpu, 5),
  1223. kvmppc_get_gpr(vcpu, 6));
  1224. break;
  1225. case H_SVM_PAGE_OUT:
  1226. ret = H_UNSUPPORTED;
  1227. if (kvmppc_get_srr1(vcpu) & MSR_S)
  1228. ret = kvmppc_h_svm_page_out(kvm,
  1229. kvmppc_get_gpr(vcpu, 4),
  1230. kvmppc_get_gpr(vcpu, 5),
  1231. kvmppc_get_gpr(vcpu, 6));
  1232. break;
  1233. case H_SVM_INIT_START:
  1234. ret = H_UNSUPPORTED;
  1235. if (kvmppc_get_srr1(vcpu) & MSR_S)
  1236. ret = kvmppc_h_svm_init_start(kvm);
  1237. break;
  1238. case H_SVM_INIT_DONE:
  1239. ret = H_UNSUPPORTED;
  1240. if (kvmppc_get_srr1(vcpu) & MSR_S)
  1241. ret = kvmppc_h_svm_init_done(kvm);
  1242. break;
  1243. case H_SVM_INIT_ABORT:
  1244. /*
  1245. * Even if that call is made by the Ultravisor, the SSR1 value
  1246. * is the guest context one, with the secure bit clear as it has
  1247. * not yet been secured. So we can't check it here.
  1248. * Instead the kvm->arch.secure_guest flag is checked inside
  1249. * kvmppc_h_svm_init_abort().
  1250. */
  1251. ret = kvmppc_h_svm_init_abort(kvm);
  1252. break;
  1253. default:
  1254. return RESUME_HOST;
  1255. }
  1256. WARN_ON_ONCE(ret == H_TOO_HARD);
  1257. kvmppc_set_gpr(vcpu, 3, ret);
  1258. vcpu->arch.hcall_needed = 0;
  1259. return RESUME_GUEST;
  1260. }
  1261. /*
  1262. * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
  1263. * handlers in book3s_hv_rmhandlers.S.
  1264. *
  1265. * This has to be done early, not in kvmppc_pseries_do_hcall(), so
  1266. * that the cede logic in kvmppc_run_single_vcpu() works properly.
  1267. */
  1268. static void kvmppc_cede(struct kvm_vcpu *vcpu)
  1269. {
  1270. __kvmppc_set_msr_hv(vcpu, __kvmppc_get_msr_hv(vcpu) | MSR_EE);
  1271. vcpu->arch.ceded = 1;
  1272. smp_mb();
  1273. if (vcpu->arch.prodded) {
  1274. vcpu->arch.prodded = 0;
  1275. smp_mb();
  1276. vcpu->arch.ceded = 0;
  1277. }
  1278. }
  1279. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  1280. {
  1281. switch (cmd) {
  1282. case H_CEDE:
  1283. case H_PROD:
  1284. case H_CONFER:
  1285. case H_REGISTER_VPA:
  1286. case H_SET_MODE:
  1287. #ifdef CONFIG_SPAPR_TCE_IOMMU
  1288. case H_GET_TCE:
  1289. case H_PUT_TCE:
  1290. case H_PUT_TCE_INDIRECT:
  1291. case H_STUFF_TCE:
  1292. #endif
  1293. case H_LOGICAL_CI_LOAD:
  1294. case H_LOGICAL_CI_STORE:
  1295. #ifdef CONFIG_KVM_XICS
  1296. case H_XIRR:
  1297. case H_CPPR:
  1298. case H_EOI:
  1299. case H_IPI:
  1300. case H_IPOLL:
  1301. case H_XIRR_X:
  1302. #endif
  1303. case H_PAGE_INIT:
  1304. case H_RPT_INVALIDATE:
  1305. return 1;
  1306. }
  1307. /* See if it's in the real-mode table */
  1308. return kvmppc_hcall_impl_hv_realmode(cmd);
  1309. }
  1310. static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
  1311. {
  1312. ppc_inst_t last_inst;
  1313. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  1314. EMULATE_DONE) {
  1315. /*
  1316. * Fetch failed, so return to guest and
  1317. * try executing it again.
  1318. */
  1319. return RESUME_GUEST;
  1320. }
  1321. if (ppc_inst_val(last_inst) == KVMPPC_INST_SW_BREAKPOINT) {
  1322. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  1323. vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
  1324. return RESUME_HOST;
  1325. } else {
  1326. kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
  1327. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
  1328. return RESUME_GUEST;
  1329. }
  1330. }
  1331. static void do_nothing(void *x)
  1332. {
  1333. }
  1334. static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
  1335. {
  1336. int thr, cpu, pcpu, nthreads;
  1337. struct kvm_vcpu *v;
  1338. unsigned long dpdes;
  1339. nthreads = vcpu->kvm->arch.emul_smt_mode;
  1340. dpdes = 0;
  1341. cpu = vcpu->vcpu_id & ~(nthreads - 1);
  1342. for (thr = 0; thr < nthreads; ++thr, ++cpu) {
  1343. v = kvmppc_find_vcpu(vcpu->kvm, cpu);
  1344. if (!v)
  1345. continue;
  1346. /*
  1347. * If the vcpu is currently running on a physical cpu thread,
  1348. * interrupt it in order to pull it out of the guest briefly,
  1349. * which will update its vcore->dpdes value.
  1350. */
  1351. pcpu = READ_ONCE(v->cpu);
  1352. if (pcpu >= 0)
  1353. smp_call_function_single(pcpu, do_nothing, NULL, 1);
  1354. if (kvmppc_doorbell_pending(v))
  1355. dpdes |= 1 << thr;
  1356. }
  1357. return dpdes;
  1358. }
  1359. /*
  1360. * On POWER9, emulate doorbell-related instructions in order to
  1361. * give the guest the illusion of running on a multi-threaded core.
  1362. * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
  1363. * and mfspr DPDES.
  1364. */
  1365. static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
  1366. {
  1367. u32 inst, rb, thr;
  1368. unsigned long arg;
  1369. struct kvm *kvm = vcpu->kvm;
  1370. struct kvm_vcpu *tvcpu;
  1371. ppc_inst_t pinst;
  1372. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst) != EMULATE_DONE)
  1373. return RESUME_GUEST;
  1374. inst = ppc_inst_val(pinst);
  1375. if (get_op(inst) != 31)
  1376. return EMULATE_FAIL;
  1377. rb = get_rb(inst);
  1378. thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
  1379. switch (get_xop(inst)) {
  1380. case OP_31_XOP_MSGSNDP:
  1381. arg = kvmppc_get_gpr(vcpu, rb);
  1382. if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
  1383. break;
  1384. arg &= 0x7f;
  1385. if (arg >= kvm->arch.emul_smt_mode)
  1386. break;
  1387. tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
  1388. if (!tvcpu)
  1389. break;
  1390. if (!tvcpu->arch.doorbell_request) {
  1391. tvcpu->arch.doorbell_request = 1;
  1392. kvmppc_fast_vcpu_kick_hv(tvcpu);
  1393. }
  1394. break;
  1395. case OP_31_XOP_MSGCLRP:
  1396. arg = kvmppc_get_gpr(vcpu, rb);
  1397. if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
  1398. break;
  1399. vcpu->arch.vcore->dpdes = 0;
  1400. vcpu->arch.doorbell_request = 0;
  1401. break;
  1402. case OP_31_XOP_MFSPR:
  1403. switch (get_sprn(inst)) {
  1404. case SPRN_TIR:
  1405. arg = thr;
  1406. break;
  1407. case SPRN_DPDES:
  1408. arg = kvmppc_read_dpdes(vcpu);
  1409. break;
  1410. default:
  1411. return EMULATE_FAIL;
  1412. }
  1413. kvmppc_set_gpr(vcpu, get_rt(inst), arg);
  1414. break;
  1415. default:
  1416. return EMULATE_FAIL;
  1417. }
  1418. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  1419. return RESUME_GUEST;
  1420. }
  1421. /*
  1422. * If the lppaca had pmcregs_in_use clear when we exited the guest, then
  1423. * HFSCR_PM is cleared for next entry. If the guest then tries to access
  1424. * the PMU SPRs, we get this facility unavailable interrupt. Putting HFSCR_PM
  1425. * back in the guest HFSCR will cause the next entry to load the PMU SPRs and
  1426. * allow the guest access to continue.
  1427. */
  1428. static int kvmppc_pmu_unavailable(struct kvm_vcpu *vcpu)
  1429. {
  1430. if (!(vcpu->arch.hfscr_permitted & HFSCR_PM))
  1431. return EMULATE_FAIL;
  1432. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PM);
  1433. return RESUME_GUEST;
  1434. }
  1435. static int kvmppc_ebb_unavailable(struct kvm_vcpu *vcpu)
  1436. {
  1437. if (!(vcpu->arch.hfscr_permitted & HFSCR_EBB))
  1438. return EMULATE_FAIL;
  1439. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_EBB);
  1440. return RESUME_GUEST;
  1441. }
  1442. static int kvmppc_tm_unavailable(struct kvm_vcpu *vcpu)
  1443. {
  1444. if (!(vcpu->arch.hfscr_permitted & HFSCR_TM))
  1445. return EMULATE_FAIL;
  1446. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
  1447. return RESUME_GUEST;
  1448. }
  1449. static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
  1450. struct task_struct *tsk)
  1451. {
  1452. struct kvm_run *run = vcpu->run;
  1453. int r = RESUME_HOST;
  1454. vcpu->stat.sum_exits++;
  1455. /*
  1456. * This can happen if an interrupt occurs in the last stages
  1457. * of guest entry or the first stages of guest exit (i.e. after
  1458. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  1459. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  1460. * That can happen due to a bug, or due to a machine check
  1461. * occurring at just the wrong time.
  1462. */
  1463. if (!kvmhv_is_nestedv2() && (__kvmppc_get_msr_hv(vcpu) & MSR_HV)) {
  1464. printk(KERN_EMERG "KVM trap in HV mode!\n");
  1465. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1466. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1467. vcpu->arch.shregs.msr);
  1468. kvmppc_dump_regs(vcpu);
  1469. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1470. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1471. return RESUME_HOST;
  1472. }
  1473. run->exit_reason = KVM_EXIT_UNKNOWN;
  1474. run->ready_for_interrupt_injection = 1;
  1475. switch (vcpu->arch.trap) {
  1476. /* We're good on these - the host merely wanted to get our attention */
  1477. case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
  1478. WARN_ON_ONCE(1); /* Should never happen */
  1479. vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
  1480. fallthrough;
  1481. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  1482. vcpu->stat.dec_exits++;
  1483. r = RESUME_GUEST;
  1484. break;
  1485. case BOOK3S_INTERRUPT_EXTERNAL:
  1486. case BOOK3S_INTERRUPT_H_DOORBELL:
  1487. case BOOK3S_INTERRUPT_H_VIRT:
  1488. vcpu->stat.ext_intr_exits++;
  1489. r = RESUME_GUEST;
  1490. break;
  1491. /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
  1492. case BOOK3S_INTERRUPT_HMI:
  1493. case BOOK3S_INTERRUPT_PERFMON:
  1494. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  1495. r = RESUME_GUEST;
  1496. break;
  1497. case BOOK3S_INTERRUPT_MACHINE_CHECK: {
  1498. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  1499. DEFAULT_RATELIMIT_BURST);
  1500. /*
  1501. * Print the MCE event to host console. Ratelimit so the guest
  1502. * can't flood the host log.
  1503. */
  1504. if (__ratelimit(&rs))
  1505. machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
  1506. /*
  1507. * If the guest can do FWNMI, exit to userspace so it can
  1508. * deliver a FWNMI to the guest.
  1509. * Otherwise we synthesize a machine check for the guest
  1510. * so that it knows that the machine check occurred.
  1511. */
  1512. if (!vcpu->kvm->arch.fwnmi_enabled) {
  1513. ulong flags = (__kvmppc_get_msr_hv(vcpu) & 0x083c0000) |
  1514. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
  1515. kvmppc_core_queue_machine_check(vcpu, flags);
  1516. r = RESUME_GUEST;
  1517. break;
  1518. }
  1519. /* Exit to guest with KVM_EXIT_NMI as exit reason */
  1520. run->exit_reason = KVM_EXIT_NMI;
  1521. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1522. /* Clear out the old NMI status from run->flags */
  1523. run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
  1524. /* Now set the NMI status */
  1525. if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
  1526. run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
  1527. else
  1528. run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
  1529. r = RESUME_HOST;
  1530. break;
  1531. }
  1532. case BOOK3S_INTERRUPT_PROGRAM:
  1533. {
  1534. ulong flags;
  1535. /*
  1536. * Normally program interrupts are delivered directly
  1537. * to the guest by the hardware, but we can get here
  1538. * as a result of a hypervisor emulation interrupt
  1539. * (e40) getting turned into a 700 by BML RTAS.
  1540. */
  1541. flags = (__kvmppc_get_msr_hv(vcpu) & 0x1f0000ull) |
  1542. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
  1543. kvmppc_core_queue_program(vcpu, flags);
  1544. r = RESUME_GUEST;
  1545. break;
  1546. }
  1547. case BOOK3S_INTERRUPT_SYSCALL:
  1548. {
  1549. int i;
  1550. if (!kvmhv_is_nestedv2() && unlikely(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
  1551. /*
  1552. * Guest userspace executed sc 1. This can only be
  1553. * reached by the P9 path because the old path
  1554. * handles this case in realmode hcall handlers.
  1555. */
  1556. if (!kvmhv_vcpu_is_radix(vcpu)) {
  1557. /*
  1558. * A guest could be running PR KVM, so this
  1559. * may be a PR KVM hcall. It must be reflected
  1560. * to the guest kernel as a sc interrupt.
  1561. */
  1562. kvmppc_core_queue_syscall(vcpu);
  1563. } else {
  1564. /*
  1565. * Radix guests can not run PR KVM or nested HV
  1566. * hash guests which might run PR KVM, so this
  1567. * is always a privilege fault. Send a program
  1568. * check to guest kernel.
  1569. */
  1570. kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
  1571. }
  1572. r = RESUME_GUEST;
  1573. break;
  1574. }
  1575. /*
  1576. * hcall - gather args and set exit_reason. This will next be
  1577. * handled by kvmppc_pseries_do_hcall which may be able to deal
  1578. * with it and resume guest, or may punt to userspace.
  1579. */
  1580. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  1581. for (i = 0; i < 9; ++i)
  1582. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  1583. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  1584. vcpu->arch.hcall_needed = 1;
  1585. r = RESUME_HOST;
  1586. break;
  1587. }
  1588. /*
  1589. * We get these next two if the guest accesses a page which it thinks
  1590. * it has mapped but which is not actually present, either because
  1591. * it is for an emulated I/O device or because the corresonding
  1592. * host page has been paged out.
  1593. *
  1594. * Any other HDSI/HISI interrupts have been handled already for P7/8
  1595. * guests. For POWER9 hash guests not using rmhandlers, basic hash
  1596. * fault handling is done here.
  1597. */
  1598. case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
  1599. unsigned long vsid;
  1600. long err;
  1601. if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
  1602. unlikely(vcpu->arch.fault_dsisr == HDSISR_CANARY)) {
  1603. r = RESUME_GUEST; /* Just retry if it's the canary */
  1604. break;
  1605. }
  1606. if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
  1607. /*
  1608. * Radix doesn't require anything, and pre-ISAv3.0 hash
  1609. * already attempted to handle this in rmhandlers. The
  1610. * hash fault handling below is v3 only (it uses ASDR
  1611. * via fault_gpa).
  1612. */
  1613. r = RESUME_PAGE_FAULT;
  1614. break;
  1615. }
  1616. if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
  1617. kvmppc_core_queue_data_storage(vcpu,
  1618. kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
  1619. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  1620. r = RESUME_GUEST;
  1621. break;
  1622. }
  1623. if (!(__kvmppc_get_msr_hv(vcpu) & MSR_DR))
  1624. vsid = vcpu->kvm->arch.vrma_slb_v;
  1625. else
  1626. vsid = vcpu->arch.fault_gpa;
  1627. err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
  1628. vsid, vcpu->arch.fault_dsisr, true);
  1629. if (err == 0) {
  1630. r = RESUME_GUEST;
  1631. } else if (err == -1 || err == -2) {
  1632. r = RESUME_PAGE_FAULT;
  1633. } else {
  1634. kvmppc_core_queue_data_storage(vcpu,
  1635. kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
  1636. vcpu->arch.fault_dar, err);
  1637. r = RESUME_GUEST;
  1638. }
  1639. break;
  1640. }
  1641. case BOOK3S_INTERRUPT_H_INST_STORAGE: {
  1642. unsigned long vsid;
  1643. long err;
  1644. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  1645. vcpu->arch.fault_dsisr = __kvmppc_get_msr_hv(vcpu) &
  1646. DSISR_SRR1_MATCH_64S;
  1647. if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
  1648. /*
  1649. * Radix doesn't require anything, and pre-ISAv3.0 hash
  1650. * already attempted to handle this in rmhandlers. The
  1651. * hash fault handling below is v3 only (it uses ASDR
  1652. * via fault_gpa).
  1653. */
  1654. if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
  1655. vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
  1656. r = RESUME_PAGE_FAULT;
  1657. break;
  1658. }
  1659. if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
  1660. kvmppc_core_queue_inst_storage(vcpu,
  1661. vcpu->arch.fault_dsisr |
  1662. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
  1663. r = RESUME_GUEST;
  1664. break;
  1665. }
  1666. if (!(__kvmppc_get_msr_hv(vcpu) & MSR_IR))
  1667. vsid = vcpu->kvm->arch.vrma_slb_v;
  1668. else
  1669. vsid = vcpu->arch.fault_gpa;
  1670. err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
  1671. vsid, vcpu->arch.fault_dsisr, false);
  1672. if (err == 0) {
  1673. r = RESUME_GUEST;
  1674. } else if (err == -1) {
  1675. r = RESUME_PAGE_FAULT;
  1676. } else {
  1677. kvmppc_core_queue_inst_storage(vcpu,
  1678. err | (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
  1679. r = RESUME_GUEST;
  1680. }
  1681. break;
  1682. }
  1683. /*
  1684. * This occurs if the guest executes an illegal instruction.
  1685. * If the guest debug is disabled, generate a program interrupt
  1686. * to the guest. If guest debug is enabled, we need to check
  1687. * whether the instruction is a software breakpoint instruction.
  1688. * Accordingly return to Guest or Host.
  1689. */
  1690. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  1691. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  1692. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  1693. swab32(vcpu->arch.emul_inst) :
  1694. vcpu->arch.emul_inst;
  1695. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  1696. r = kvmppc_emulate_debug_inst(vcpu);
  1697. } else {
  1698. kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
  1699. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
  1700. r = RESUME_GUEST;
  1701. }
  1702. break;
  1703. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1704. case BOOK3S_INTERRUPT_HV_SOFTPATCH:
  1705. /*
  1706. * This occurs for various TM-related instructions that
  1707. * we need to emulate on POWER9 DD2.2. We have already
  1708. * handled the cases where the guest was in real-suspend
  1709. * mode and was transitioning to transactional state.
  1710. */
  1711. r = kvmhv_p9_tm_emulation(vcpu);
  1712. if (r != -1)
  1713. break;
  1714. fallthrough; /* go to facility unavailable handler */
  1715. #endif
  1716. /*
  1717. * This occurs if the guest (kernel or userspace), does something that
  1718. * is prohibited by HFSCR.
  1719. * On POWER9, this could be a doorbell instruction that we need
  1720. * to emulate.
  1721. * Otherwise, we just generate a program interrupt to the guest.
  1722. */
  1723. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
  1724. u64 cause = kvmppc_get_hfscr_hv(vcpu) >> 56;
  1725. r = EMULATE_FAIL;
  1726. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1727. switch (cause) {
  1728. case FSCR_MSGP_LG:
  1729. r = kvmppc_emulate_doorbell_instr(vcpu);
  1730. break;
  1731. case FSCR_PM_LG:
  1732. r = kvmppc_pmu_unavailable(vcpu);
  1733. break;
  1734. case FSCR_EBB_LG:
  1735. r = kvmppc_ebb_unavailable(vcpu);
  1736. break;
  1737. case FSCR_TM_LG:
  1738. r = kvmppc_tm_unavailable(vcpu);
  1739. break;
  1740. default:
  1741. break;
  1742. }
  1743. }
  1744. if (r == EMULATE_FAIL) {
  1745. kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
  1746. (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
  1747. r = RESUME_GUEST;
  1748. }
  1749. break;
  1750. }
  1751. case BOOK3S_INTERRUPT_HV_RM_HARD:
  1752. r = RESUME_PASSTHROUGH;
  1753. break;
  1754. default:
  1755. kvmppc_dump_regs(vcpu);
  1756. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1757. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1758. __kvmppc_get_msr_hv(vcpu));
  1759. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1760. r = RESUME_HOST;
  1761. break;
  1762. }
  1763. return r;
  1764. }
  1765. static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
  1766. {
  1767. int r;
  1768. int srcu_idx;
  1769. vcpu->stat.sum_exits++;
  1770. /*
  1771. * This can happen if an interrupt occurs in the last stages
  1772. * of guest entry or the first stages of guest exit (i.e. after
  1773. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  1774. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  1775. * That can happen due to a bug, or due to a machine check
  1776. * occurring at just the wrong time.
  1777. */
  1778. if (__kvmppc_get_msr_hv(vcpu) & MSR_HV) {
  1779. pr_emerg("KVM trap in HV mode while nested!\n");
  1780. pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1781. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1782. __kvmppc_get_msr_hv(vcpu));
  1783. kvmppc_dump_regs(vcpu);
  1784. return RESUME_HOST;
  1785. }
  1786. switch (vcpu->arch.trap) {
  1787. /* We're good on these - the host merely wanted to get our attention */
  1788. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  1789. vcpu->stat.dec_exits++;
  1790. r = RESUME_GUEST;
  1791. break;
  1792. case BOOK3S_INTERRUPT_EXTERNAL:
  1793. vcpu->stat.ext_intr_exits++;
  1794. r = RESUME_HOST;
  1795. break;
  1796. case BOOK3S_INTERRUPT_H_DOORBELL:
  1797. case BOOK3S_INTERRUPT_H_VIRT:
  1798. vcpu->stat.ext_intr_exits++;
  1799. r = RESUME_GUEST;
  1800. break;
  1801. /* These need to go to the nested HV */
  1802. case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
  1803. vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
  1804. vcpu->stat.dec_exits++;
  1805. r = RESUME_HOST;
  1806. break;
  1807. /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
  1808. case BOOK3S_INTERRUPT_HMI:
  1809. case BOOK3S_INTERRUPT_PERFMON:
  1810. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  1811. r = RESUME_GUEST;
  1812. break;
  1813. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  1814. {
  1815. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  1816. DEFAULT_RATELIMIT_BURST);
  1817. /* Pass the machine check to the L1 guest */
  1818. r = RESUME_HOST;
  1819. /* Print the MCE event to host console. */
  1820. if (__ratelimit(&rs))
  1821. machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
  1822. break;
  1823. }
  1824. /*
  1825. * We get these next two if the guest accesses a page which it thinks
  1826. * it has mapped but which is not actually present, either because
  1827. * it is for an emulated I/O device or because the corresonding
  1828. * host page has been paged out.
  1829. */
  1830. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  1831. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1832. r = kvmhv_nested_page_fault(vcpu);
  1833. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1834. break;
  1835. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  1836. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  1837. vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
  1838. DSISR_SRR1_MATCH_64S;
  1839. if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
  1840. vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
  1841. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1842. r = kvmhv_nested_page_fault(vcpu);
  1843. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1844. break;
  1845. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1846. case BOOK3S_INTERRUPT_HV_SOFTPATCH:
  1847. /*
  1848. * This occurs for various TM-related instructions that
  1849. * we need to emulate on POWER9 DD2.2. We have already
  1850. * handled the cases where the guest was in real-suspend
  1851. * mode and was transitioning to transactional state.
  1852. */
  1853. r = kvmhv_p9_tm_emulation(vcpu);
  1854. if (r != -1)
  1855. break;
  1856. fallthrough; /* go to facility unavailable handler */
  1857. #endif
  1858. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
  1859. u64 cause = vcpu->arch.hfscr >> 56;
  1860. /*
  1861. * Only pass HFU interrupts to the L1 if the facility is
  1862. * permitted but disabled by the L1's HFSCR, otherwise
  1863. * the interrupt does not make sense to the L1 so turn
  1864. * it into a HEAI.
  1865. */
  1866. if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) ||
  1867. (vcpu->arch.nested_hfscr & (1UL << cause))) {
  1868. ppc_inst_t pinst;
  1869. vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST;
  1870. /*
  1871. * If the fetch failed, return to guest and
  1872. * try executing it again.
  1873. */
  1874. r = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
  1875. vcpu->arch.emul_inst = ppc_inst_val(pinst);
  1876. if (r != EMULATE_DONE)
  1877. r = RESUME_GUEST;
  1878. else
  1879. r = RESUME_HOST;
  1880. } else {
  1881. r = RESUME_HOST;
  1882. }
  1883. break;
  1884. }
  1885. case BOOK3S_INTERRUPT_HV_RM_HARD:
  1886. vcpu->arch.trap = 0;
  1887. r = RESUME_GUEST;
  1888. if (!xics_on_xive())
  1889. kvmppc_xics_rm_complete(vcpu, 0);
  1890. break;
  1891. case BOOK3S_INTERRUPT_SYSCALL:
  1892. {
  1893. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  1894. /*
  1895. * The H_RPT_INVALIDATE hcalls issued by nested
  1896. * guests for process-scoped invalidations when
  1897. * GTSE=0, are handled here in L0.
  1898. */
  1899. if (req == H_RPT_INVALIDATE) {
  1900. r = kvmppc_nested_h_rpt_invalidate(vcpu);
  1901. break;
  1902. }
  1903. r = RESUME_HOST;
  1904. break;
  1905. }
  1906. default:
  1907. r = RESUME_HOST;
  1908. break;
  1909. }
  1910. return r;
  1911. }
  1912. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  1913. struct kvm_sregs *sregs)
  1914. {
  1915. int i;
  1916. memset(sregs, 0, sizeof(struct kvm_sregs));
  1917. sregs->pvr = vcpu->arch.pvr;
  1918. for (i = 0; i < vcpu->arch.slb_max; i++) {
  1919. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  1920. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  1921. }
  1922. return 0;
  1923. }
  1924. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  1925. struct kvm_sregs *sregs)
  1926. {
  1927. int i, j;
  1928. /* Only accept the same PVR as the host's, since we can't spoof it */
  1929. if (sregs->pvr != vcpu->arch.pvr)
  1930. return -EINVAL;
  1931. j = 0;
  1932. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  1933. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  1934. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  1935. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  1936. ++j;
  1937. }
  1938. }
  1939. vcpu->arch.slb_max = j;
  1940. return 0;
  1941. }
  1942. /*
  1943. * Enforce limits on guest LPCR values based on hardware availability,
  1944. * guest configuration, and possibly hypervisor support and security
  1945. * concerns.
  1946. */
  1947. unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
  1948. {
  1949. /* LPCR_TC only applies to HPT guests */
  1950. if (kvm_is_radix(kvm))
  1951. lpcr &= ~LPCR_TC;
  1952. /* On POWER8 and above, userspace can modify AIL */
  1953. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1954. lpcr &= ~LPCR_AIL;
  1955. if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
  1956. lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
  1957. /*
  1958. * On some POWER9s we force AIL off for radix guests to prevent
  1959. * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
  1960. * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
  1961. * be cached, which the host TLB management does not expect.
  1962. */
  1963. if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
  1964. lpcr &= ~LPCR_AIL;
  1965. /*
  1966. * On POWER9, allow userspace to enable large decrementer for the
  1967. * guest, whether or not the host has it enabled.
  1968. */
  1969. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1970. lpcr &= ~LPCR_LD;
  1971. return lpcr;
  1972. }
  1973. static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
  1974. {
  1975. if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
  1976. WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
  1977. lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
  1978. }
  1979. }
  1980. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  1981. bool preserve_top32)
  1982. {
  1983. struct kvm *kvm = vcpu->kvm;
  1984. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  1985. u64 mask;
  1986. spin_lock(&vc->lock);
  1987. /*
  1988. * Userspace can only modify
  1989. * DPFD (default prefetch depth), ILE (interrupt little-endian),
  1990. * TC (translation control), AIL (alternate interrupt location),
  1991. * LD (large decrementer).
  1992. * These are subject to restrictions from kvmppc_filter_lcpr_hv().
  1993. */
  1994. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
  1995. /* Broken 32-bit version of LPCR must not clear top bits */
  1996. if (preserve_top32)
  1997. mask &= 0xFFFFFFFF;
  1998. new_lpcr = kvmppc_filter_lpcr_hv(kvm,
  1999. (vc->lpcr & ~mask) | (new_lpcr & mask));
  2000. /*
  2001. * If ILE (interrupt little-endian) has changed, update the
  2002. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  2003. */
  2004. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  2005. struct kvm_vcpu *vcpu;
  2006. unsigned long i;
  2007. kvm_for_each_vcpu(i, vcpu, kvm) {
  2008. if (vcpu->arch.vcore != vc)
  2009. continue;
  2010. if (new_lpcr & LPCR_ILE)
  2011. vcpu->arch.intr_msr |= MSR_LE;
  2012. else
  2013. vcpu->arch.intr_msr &= ~MSR_LE;
  2014. }
  2015. }
  2016. vc->lpcr = new_lpcr;
  2017. kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
  2018. spin_unlock(&vc->lock);
  2019. }
  2020. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  2021. union kvmppc_one_reg *val)
  2022. {
  2023. int r = 0;
  2024. long int i;
  2025. switch (id) {
  2026. case KVM_REG_PPC_DEBUG_INST:
  2027. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  2028. break;
  2029. case KVM_REG_PPC_HIOR:
  2030. *val = get_reg_val(id, 0);
  2031. break;
  2032. case KVM_REG_PPC_DABR:
  2033. *val = get_reg_val(id, vcpu->arch.dabr);
  2034. break;
  2035. case KVM_REG_PPC_DABRX:
  2036. *val = get_reg_val(id, vcpu->arch.dabrx);
  2037. break;
  2038. case KVM_REG_PPC_DSCR:
  2039. *val = get_reg_val(id, kvmppc_get_dscr_hv(vcpu));
  2040. break;
  2041. case KVM_REG_PPC_PURR:
  2042. *val = get_reg_val(id, kvmppc_get_purr_hv(vcpu));
  2043. break;
  2044. case KVM_REG_PPC_SPURR:
  2045. *val = get_reg_val(id, kvmppc_get_spurr_hv(vcpu));
  2046. break;
  2047. case KVM_REG_PPC_AMR:
  2048. *val = get_reg_val(id, kvmppc_get_amr_hv(vcpu));
  2049. break;
  2050. case KVM_REG_PPC_UAMOR:
  2051. *val = get_reg_val(id, kvmppc_get_uamor_hv(vcpu));
  2052. break;
  2053. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
  2054. i = id - KVM_REG_PPC_MMCR0;
  2055. *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, i));
  2056. break;
  2057. case KVM_REG_PPC_MMCR2:
  2058. *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 2));
  2059. break;
  2060. case KVM_REG_PPC_MMCRA:
  2061. *val = get_reg_val(id, kvmppc_get_mmcra_hv(vcpu));
  2062. break;
  2063. case KVM_REG_PPC_MMCRS:
  2064. *val = get_reg_val(id, vcpu->arch.mmcrs);
  2065. break;
  2066. case KVM_REG_PPC_MMCR3:
  2067. *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 3));
  2068. break;
  2069. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  2070. i = id - KVM_REG_PPC_PMC1;
  2071. *val = get_reg_val(id, kvmppc_get_pmc_hv(vcpu, i));
  2072. break;
  2073. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  2074. i = id - KVM_REG_PPC_SPMC1;
  2075. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  2076. break;
  2077. case KVM_REG_PPC_SIAR:
  2078. *val = get_reg_val(id, kvmppc_get_siar_hv(vcpu));
  2079. break;
  2080. case KVM_REG_PPC_SDAR:
  2081. *val = get_reg_val(id, kvmppc_get_sdar_hv(vcpu));
  2082. break;
  2083. case KVM_REG_PPC_SIER:
  2084. *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 0));
  2085. break;
  2086. case KVM_REG_PPC_SIER2:
  2087. *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 1));
  2088. break;
  2089. case KVM_REG_PPC_SIER3:
  2090. *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 2));
  2091. break;
  2092. case KVM_REG_PPC_IAMR:
  2093. *val = get_reg_val(id, kvmppc_get_iamr_hv(vcpu));
  2094. break;
  2095. case KVM_REG_PPC_PSPB:
  2096. *val = get_reg_val(id, kvmppc_get_pspb_hv(vcpu));
  2097. break;
  2098. case KVM_REG_PPC_DPDES:
  2099. /*
  2100. * On POWER9, where we are emulating msgsndp etc.,
  2101. * we return 1 bit for each vcpu, which can come from
  2102. * either vcore->dpdes or doorbell_request.
  2103. * On POWER8, doorbell_request is 0.
  2104. */
  2105. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2106. *val = get_reg_val(id, vcpu->arch.doorbell_request);
  2107. else
  2108. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  2109. break;
  2110. case KVM_REG_PPC_VTB:
  2111. *val = get_reg_val(id, kvmppc_get_vtb(vcpu));
  2112. break;
  2113. case KVM_REG_PPC_DAWR:
  2114. *val = get_reg_val(id, kvmppc_get_dawr0_hv(vcpu));
  2115. break;
  2116. case KVM_REG_PPC_DAWRX:
  2117. *val = get_reg_val(id, kvmppc_get_dawrx0_hv(vcpu));
  2118. break;
  2119. case KVM_REG_PPC_DAWR1:
  2120. *val = get_reg_val(id, kvmppc_get_dawr1_hv(vcpu));
  2121. break;
  2122. case KVM_REG_PPC_DAWRX1:
  2123. *val = get_reg_val(id, kvmppc_get_dawrx1_hv(vcpu));
  2124. break;
  2125. case KVM_REG_PPC_DEXCR:
  2126. *val = get_reg_val(id, kvmppc_get_dexcr_hv(vcpu));
  2127. break;
  2128. case KVM_REG_PPC_HASHKEYR:
  2129. *val = get_reg_val(id, kvmppc_get_hashkeyr_hv(vcpu));
  2130. break;
  2131. case KVM_REG_PPC_HASHPKEYR:
  2132. *val = get_reg_val(id, kvmppc_get_hashpkeyr_hv(vcpu));
  2133. break;
  2134. case KVM_REG_PPC_CIABR:
  2135. *val = get_reg_val(id, kvmppc_get_ciabr_hv(vcpu));
  2136. break;
  2137. case KVM_REG_PPC_CSIGR:
  2138. *val = get_reg_val(id, vcpu->arch.csigr);
  2139. break;
  2140. case KVM_REG_PPC_TACR:
  2141. *val = get_reg_val(id, vcpu->arch.tacr);
  2142. break;
  2143. case KVM_REG_PPC_TCSCR:
  2144. *val = get_reg_val(id, vcpu->arch.tcscr);
  2145. break;
  2146. case KVM_REG_PPC_PID:
  2147. *val = get_reg_val(id, kvmppc_get_pid(vcpu));
  2148. break;
  2149. case KVM_REG_PPC_ACOP:
  2150. *val = get_reg_val(id, vcpu->arch.acop);
  2151. break;
  2152. case KVM_REG_PPC_WORT:
  2153. *val = get_reg_val(id, kvmppc_get_wort_hv(vcpu));
  2154. break;
  2155. case KVM_REG_PPC_TIDR:
  2156. *val = get_reg_val(id, vcpu->arch.tid);
  2157. break;
  2158. case KVM_REG_PPC_PSSCR:
  2159. *val = get_reg_val(id, vcpu->arch.psscr);
  2160. break;
  2161. case KVM_REG_PPC_VPA_ADDR:
  2162. spin_lock(&vcpu->arch.vpa_update_lock);
  2163. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  2164. spin_unlock(&vcpu->arch.vpa_update_lock);
  2165. break;
  2166. case KVM_REG_PPC_VPA_SLB:
  2167. spin_lock(&vcpu->arch.vpa_update_lock);
  2168. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  2169. val->vpaval.length = vcpu->arch.slb_shadow.len;
  2170. spin_unlock(&vcpu->arch.vpa_update_lock);
  2171. break;
  2172. case KVM_REG_PPC_VPA_DTL:
  2173. spin_lock(&vcpu->arch.vpa_update_lock);
  2174. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  2175. val->vpaval.length = vcpu->arch.dtl.len;
  2176. spin_unlock(&vcpu->arch.vpa_update_lock);
  2177. break;
  2178. case KVM_REG_PPC_TB_OFFSET:
  2179. *val = get_reg_val(id, kvmppc_get_tb_offset(vcpu));
  2180. break;
  2181. case KVM_REG_PPC_LPCR:
  2182. case KVM_REG_PPC_LPCR_64:
  2183. *val = get_reg_val(id, kvmppc_get_lpcr(vcpu));
  2184. break;
  2185. case KVM_REG_PPC_PPR:
  2186. *val = get_reg_val(id, kvmppc_get_ppr_hv(vcpu));
  2187. break;
  2188. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2189. case KVM_REG_PPC_TFHAR:
  2190. *val = get_reg_val(id, vcpu->arch.tfhar);
  2191. break;
  2192. case KVM_REG_PPC_TFIAR:
  2193. *val = get_reg_val(id, vcpu->arch.tfiar);
  2194. break;
  2195. case KVM_REG_PPC_TEXASR:
  2196. *val = get_reg_val(id, vcpu->arch.texasr);
  2197. break;
  2198. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  2199. i = id - KVM_REG_PPC_TM_GPR0;
  2200. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  2201. break;
  2202. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  2203. {
  2204. int j;
  2205. i = id - KVM_REG_PPC_TM_VSR0;
  2206. if (i < 32)
  2207. for (j = 0; j < TS_FPRWIDTH; j++)
  2208. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  2209. else {
  2210. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  2211. val->vval = vcpu->arch.vr_tm.vr[i-32];
  2212. else
  2213. r = -ENXIO;
  2214. }
  2215. break;
  2216. }
  2217. case KVM_REG_PPC_TM_CR:
  2218. *val = get_reg_val(id, vcpu->arch.cr_tm);
  2219. break;
  2220. case KVM_REG_PPC_TM_XER:
  2221. *val = get_reg_val(id, vcpu->arch.xer_tm);
  2222. break;
  2223. case KVM_REG_PPC_TM_LR:
  2224. *val = get_reg_val(id, vcpu->arch.lr_tm);
  2225. break;
  2226. case KVM_REG_PPC_TM_CTR:
  2227. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  2228. break;
  2229. case KVM_REG_PPC_TM_FPSCR:
  2230. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  2231. break;
  2232. case KVM_REG_PPC_TM_AMR:
  2233. *val = get_reg_val(id, vcpu->arch.amr_tm);
  2234. break;
  2235. case KVM_REG_PPC_TM_PPR:
  2236. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  2237. break;
  2238. case KVM_REG_PPC_TM_VRSAVE:
  2239. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  2240. break;
  2241. case KVM_REG_PPC_TM_VSCR:
  2242. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  2243. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  2244. else
  2245. r = -ENXIO;
  2246. break;
  2247. case KVM_REG_PPC_TM_DSCR:
  2248. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  2249. break;
  2250. case KVM_REG_PPC_TM_TAR:
  2251. *val = get_reg_val(id, vcpu->arch.tar_tm);
  2252. break;
  2253. #endif
  2254. case KVM_REG_PPC_ARCH_COMPAT:
  2255. *val = get_reg_val(id, kvmppc_get_arch_compat(vcpu));
  2256. break;
  2257. case KVM_REG_PPC_DEC_EXPIRY:
  2258. *val = get_reg_val(id, kvmppc_get_dec_expires(vcpu));
  2259. break;
  2260. case KVM_REG_PPC_ONLINE:
  2261. *val = get_reg_val(id, vcpu->arch.online);
  2262. break;
  2263. case KVM_REG_PPC_PTCR:
  2264. *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
  2265. break;
  2266. case KVM_REG_PPC_FSCR:
  2267. *val = get_reg_val(id, kvmppc_get_fscr_hv(vcpu));
  2268. break;
  2269. default:
  2270. r = -EINVAL;
  2271. break;
  2272. }
  2273. return r;
  2274. }
  2275. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  2276. union kvmppc_one_reg *val)
  2277. {
  2278. int r = 0;
  2279. long int i;
  2280. unsigned long addr, len;
  2281. switch (id) {
  2282. case KVM_REG_PPC_HIOR:
  2283. /* Only allow this to be set to zero */
  2284. if (set_reg_val(id, *val))
  2285. r = -EINVAL;
  2286. break;
  2287. case KVM_REG_PPC_DABR:
  2288. vcpu->arch.dabr = set_reg_val(id, *val);
  2289. break;
  2290. case KVM_REG_PPC_DABRX:
  2291. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  2292. break;
  2293. case KVM_REG_PPC_DSCR:
  2294. kvmppc_set_dscr_hv(vcpu, set_reg_val(id, *val));
  2295. break;
  2296. case KVM_REG_PPC_PURR:
  2297. kvmppc_set_purr_hv(vcpu, set_reg_val(id, *val));
  2298. break;
  2299. case KVM_REG_PPC_SPURR:
  2300. kvmppc_set_spurr_hv(vcpu, set_reg_val(id, *val));
  2301. break;
  2302. case KVM_REG_PPC_AMR:
  2303. kvmppc_set_amr_hv(vcpu, set_reg_val(id, *val));
  2304. break;
  2305. case KVM_REG_PPC_UAMOR:
  2306. kvmppc_set_uamor_hv(vcpu, set_reg_val(id, *val));
  2307. break;
  2308. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
  2309. i = id - KVM_REG_PPC_MMCR0;
  2310. kvmppc_set_mmcr_hv(vcpu, i, set_reg_val(id, *val));
  2311. break;
  2312. case KVM_REG_PPC_MMCR2:
  2313. kvmppc_set_mmcr_hv(vcpu, 2, set_reg_val(id, *val));
  2314. break;
  2315. case KVM_REG_PPC_MMCRA:
  2316. kvmppc_set_mmcra_hv(vcpu, set_reg_val(id, *val));
  2317. break;
  2318. case KVM_REG_PPC_MMCRS:
  2319. vcpu->arch.mmcrs = set_reg_val(id, *val);
  2320. break;
  2321. case KVM_REG_PPC_MMCR3:
  2322. kvmppc_set_mmcr_hv(vcpu, 3, set_reg_val(id, *val));
  2323. break;
  2324. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  2325. i = id - KVM_REG_PPC_PMC1;
  2326. kvmppc_set_pmc_hv(vcpu, i, set_reg_val(id, *val));
  2327. break;
  2328. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  2329. i = id - KVM_REG_PPC_SPMC1;
  2330. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  2331. break;
  2332. case KVM_REG_PPC_SIAR:
  2333. kvmppc_set_siar_hv(vcpu, set_reg_val(id, *val));
  2334. break;
  2335. case KVM_REG_PPC_SDAR:
  2336. kvmppc_set_sdar_hv(vcpu, set_reg_val(id, *val));
  2337. break;
  2338. case KVM_REG_PPC_SIER:
  2339. kvmppc_set_sier_hv(vcpu, 0, set_reg_val(id, *val));
  2340. break;
  2341. case KVM_REG_PPC_SIER2:
  2342. kvmppc_set_sier_hv(vcpu, 1, set_reg_val(id, *val));
  2343. break;
  2344. case KVM_REG_PPC_SIER3:
  2345. kvmppc_set_sier_hv(vcpu, 2, set_reg_val(id, *val));
  2346. break;
  2347. case KVM_REG_PPC_IAMR:
  2348. kvmppc_set_iamr_hv(vcpu, set_reg_val(id, *val));
  2349. break;
  2350. case KVM_REG_PPC_PSPB:
  2351. kvmppc_set_pspb_hv(vcpu, set_reg_val(id, *val));
  2352. break;
  2353. case KVM_REG_PPC_DPDES:
  2354. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2355. vcpu->arch.doorbell_request = set_reg_val(id, *val) & 1;
  2356. else
  2357. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  2358. break;
  2359. case KVM_REG_PPC_VTB:
  2360. kvmppc_set_vtb(vcpu, set_reg_val(id, *val));
  2361. break;
  2362. case KVM_REG_PPC_DAWR:
  2363. kvmppc_set_dawr0_hv(vcpu, set_reg_val(id, *val));
  2364. break;
  2365. case KVM_REG_PPC_DAWRX:
  2366. kvmppc_set_dawrx0_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
  2367. break;
  2368. case KVM_REG_PPC_DAWR1:
  2369. kvmppc_set_dawr1_hv(vcpu, set_reg_val(id, *val));
  2370. break;
  2371. case KVM_REG_PPC_DAWRX1:
  2372. kvmppc_set_dawrx1_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
  2373. break;
  2374. case KVM_REG_PPC_DEXCR:
  2375. kvmppc_set_dexcr_hv(vcpu, set_reg_val(id, *val));
  2376. break;
  2377. case KVM_REG_PPC_HASHKEYR:
  2378. kvmppc_set_hashkeyr_hv(vcpu, set_reg_val(id, *val));
  2379. break;
  2380. case KVM_REG_PPC_HASHPKEYR:
  2381. kvmppc_set_hashpkeyr_hv(vcpu, set_reg_val(id, *val));
  2382. break;
  2383. case KVM_REG_PPC_CIABR:
  2384. kvmppc_set_ciabr_hv(vcpu, set_reg_val(id, *val));
  2385. /* Don't allow setting breakpoints in hypervisor code */
  2386. if ((kvmppc_get_ciabr_hv(vcpu) & CIABR_PRIV) == CIABR_PRIV_HYPER)
  2387. kvmppc_set_ciabr_hv(vcpu, kvmppc_get_ciabr_hv(vcpu) & ~CIABR_PRIV);
  2388. break;
  2389. case KVM_REG_PPC_CSIGR:
  2390. vcpu->arch.csigr = set_reg_val(id, *val);
  2391. break;
  2392. case KVM_REG_PPC_TACR:
  2393. vcpu->arch.tacr = set_reg_val(id, *val);
  2394. break;
  2395. case KVM_REG_PPC_TCSCR:
  2396. vcpu->arch.tcscr = set_reg_val(id, *val);
  2397. break;
  2398. case KVM_REG_PPC_PID:
  2399. kvmppc_set_pid(vcpu, set_reg_val(id, *val));
  2400. break;
  2401. case KVM_REG_PPC_ACOP:
  2402. vcpu->arch.acop = set_reg_val(id, *val);
  2403. break;
  2404. case KVM_REG_PPC_WORT:
  2405. kvmppc_set_wort_hv(vcpu, set_reg_val(id, *val));
  2406. break;
  2407. case KVM_REG_PPC_TIDR:
  2408. vcpu->arch.tid = set_reg_val(id, *val);
  2409. break;
  2410. case KVM_REG_PPC_PSSCR:
  2411. vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
  2412. break;
  2413. case KVM_REG_PPC_VPA_ADDR:
  2414. addr = set_reg_val(id, *val);
  2415. r = -EINVAL;
  2416. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  2417. vcpu->arch.dtl.next_gpa))
  2418. break;
  2419. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  2420. break;
  2421. case KVM_REG_PPC_VPA_SLB:
  2422. addr = val->vpaval.addr;
  2423. len = val->vpaval.length;
  2424. r = -EINVAL;
  2425. if (addr && !vcpu->arch.vpa.next_gpa)
  2426. break;
  2427. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  2428. break;
  2429. case KVM_REG_PPC_VPA_DTL:
  2430. addr = val->vpaval.addr;
  2431. len = val->vpaval.length;
  2432. r = -EINVAL;
  2433. if (addr && (len < sizeof(struct dtl_entry) ||
  2434. !vcpu->arch.vpa.next_gpa))
  2435. break;
  2436. len -= len % sizeof(struct dtl_entry);
  2437. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  2438. break;
  2439. case KVM_REG_PPC_TB_OFFSET:
  2440. {
  2441. /* round up to multiple of 2^24 */
  2442. u64 tb_offset = ALIGN(set_reg_val(id, *val), 1UL << 24);
  2443. /*
  2444. * Now that we know the timebase offset, update the
  2445. * decrementer expiry with a guest timebase value. If
  2446. * the userspace does not set DEC_EXPIRY, this ensures
  2447. * a migrated vcpu at least starts with an expired
  2448. * decrementer, which is better than a large one that
  2449. * causes a hang.
  2450. */
  2451. kvmppc_set_tb_offset(vcpu, tb_offset);
  2452. if (!kvmppc_get_dec_expires(vcpu) && tb_offset)
  2453. kvmppc_set_dec_expires(vcpu, get_tb() + tb_offset);
  2454. kvmppc_set_tb_offset(vcpu, tb_offset);
  2455. break;
  2456. }
  2457. case KVM_REG_PPC_LPCR:
  2458. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  2459. break;
  2460. case KVM_REG_PPC_LPCR_64:
  2461. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  2462. break;
  2463. case KVM_REG_PPC_PPR:
  2464. kvmppc_set_ppr_hv(vcpu, set_reg_val(id, *val));
  2465. break;
  2466. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2467. case KVM_REG_PPC_TFHAR:
  2468. vcpu->arch.tfhar = set_reg_val(id, *val);
  2469. break;
  2470. case KVM_REG_PPC_TFIAR:
  2471. vcpu->arch.tfiar = set_reg_val(id, *val);
  2472. break;
  2473. case KVM_REG_PPC_TEXASR:
  2474. vcpu->arch.texasr = set_reg_val(id, *val);
  2475. break;
  2476. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  2477. i = id - KVM_REG_PPC_TM_GPR0;
  2478. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  2479. break;
  2480. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  2481. {
  2482. int j;
  2483. i = id - KVM_REG_PPC_TM_VSR0;
  2484. if (i < 32)
  2485. for (j = 0; j < TS_FPRWIDTH; j++)
  2486. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  2487. else
  2488. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  2489. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  2490. else
  2491. r = -ENXIO;
  2492. break;
  2493. }
  2494. case KVM_REG_PPC_TM_CR:
  2495. vcpu->arch.cr_tm = set_reg_val(id, *val);
  2496. break;
  2497. case KVM_REG_PPC_TM_XER:
  2498. vcpu->arch.xer_tm = set_reg_val(id, *val);
  2499. break;
  2500. case KVM_REG_PPC_TM_LR:
  2501. vcpu->arch.lr_tm = set_reg_val(id, *val);
  2502. break;
  2503. case KVM_REG_PPC_TM_CTR:
  2504. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  2505. break;
  2506. case KVM_REG_PPC_TM_FPSCR:
  2507. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  2508. break;
  2509. case KVM_REG_PPC_TM_AMR:
  2510. vcpu->arch.amr_tm = set_reg_val(id, *val);
  2511. break;
  2512. case KVM_REG_PPC_TM_PPR:
  2513. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  2514. break;
  2515. case KVM_REG_PPC_TM_VRSAVE:
  2516. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  2517. break;
  2518. case KVM_REG_PPC_TM_VSCR:
  2519. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  2520. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  2521. else
  2522. r = - ENXIO;
  2523. break;
  2524. case KVM_REG_PPC_TM_DSCR:
  2525. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  2526. break;
  2527. case KVM_REG_PPC_TM_TAR:
  2528. vcpu->arch.tar_tm = set_reg_val(id, *val);
  2529. break;
  2530. #endif
  2531. case KVM_REG_PPC_ARCH_COMPAT:
  2532. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  2533. break;
  2534. case KVM_REG_PPC_DEC_EXPIRY:
  2535. kvmppc_set_dec_expires(vcpu, set_reg_val(id, *val));
  2536. break;
  2537. case KVM_REG_PPC_ONLINE:
  2538. i = set_reg_val(id, *val);
  2539. if (i && !vcpu->arch.online)
  2540. atomic_inc(&vcpu->arch.vcore->online_count);
  2541. else if (!i && vcpu->arch.online)
  2542. atomic_dec(&vcpu->arch.vcore->online_count);
  2543. vcpu->arch.online = i;
  2544. break;
  2545. case KVM_REG_PPC_PTCR:
  2546. vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
  2547. break;
  2548. case KVM_REG_PPC_FSCR:
  2549. kvmppc_set_fscr_hv(vcpu, set_reg_val(id, *val));
  2550. break;
  2551. default:
  2552. r = -EINVAL;
  2553. break;
  2554. }
  2555. return r;
  2556. }
  2557. /*
  2558. * On POWER9, threads are independent and can be in different partitions.
  2559. * Therefore we consider each thread to be a subcore.
  2560. * There is a restriction that all threads have to be in the same
  2561. * MMU mode (radix or HPT), unfortunately, but since we only support
  2562. * HPT guests on a HPT host so far, that isn't an impediment yet.
  2563. */
  2564. static int threads_per_vcore(struct kvm *kvm)
  2565. {
  2566. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2567. return 1;
  2568. return threads_per_subcore;
  2569. }
  2570. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
  2571. {
  2572. struct kvmppc_vcore *vcore;
  2573. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  2574. if (vcore == NULL)
  2575. return NULL;
  2576. spin_lock_init(&vcore->lock);
  2577. spin_lock_init(&vcore->stoltb_lock);
  2578. rcuwait_init(&vcore->wait);
  2579. vcore->preempt_tb = TB_NIL;
  2580. vcore->lpcr = kvm->arch.lpcr;
  2581. vcore->first_vcpuid = id;
  2582. vcore->kvm = kvm;
  2583. INIT_LIST_HEAD(&vcore->preempt_list);
  2584. return vcore;
  2585. }
  2586. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2587. static struct debugfs_timings_element {
  2588. const char *name;
  2589. size_t offset;
  2590. } timings[] = {
  2591. #ifdef CONFIG_KVM_BOOK3S_HV_P9_TIMING
  2592. {"vcpu_entry", offsetof(struct kvm_vcpu, arch.vcpu_entry)},
  2593. {"guest_entry", offsetof(struct kvm_vcpu, arch.guest_entry)},
  2594. {"in_guest", offsetof(struct kvm_vcpu, arch.in_guest)},
  2595. {"guest_exit", offsetof(struct kvm_vcpu, arch.guest_exit)},
  2596. {"vcpu_exit", offsetof(struct kvm_vcpu, arch.vcpu_exit)},
  2597. {"hypercall", offsetof(struct kvm_vcpu, arch.hcall)},
  2598. {"page_fault", offsetof(struct kvm_vcpu, arch.pg_fault)},
  2599. #else
  2600. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  2601. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  2602. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  2603. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  2604. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  2605. #endif
  2606. };
  2607. #define N_TIMINGS (ARRAY_SIZE(timings))
  2608. struct debugfs_timings_state {
  2609. struct kvm_vcpu *vcpu;
  2610. unsigned int buflen;
  2611. char buf[N_TIMINGS * 100];
  2612. };
  2613. static int debugfs_timings_open(struct inode *inode, struct file *file)
  2614. {
  2615. struct kvm_vcpu *vcpu = inode->i_private;
  2616. struct debugfs_timings_state *p;
  2617. p = kzalloc(sizeof(*p), GFP_KERNEL);
  2618. if (!p)
  2619. return -ENOMEM;
  2620. kvm_get_kvm(vcpu->kvm);
  2621. p->vcpu = vcpu;
  2622. file->private_data = p;
  2623. return nonseekable_open(inode, file);
  2624. }
  2625. static int debugfs_timings_release(struct inode *inode, struct file *file)
  2626. {
  2627. struct debugfs_timings_state *p = file->private_data;
  2628. kvm_put_kvm(p->vcpu->kvm);
  2629. kfree(p);
  2630. return 0;
  2631. }
  2632. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  2633. size_t len, loff_t *ppos)
  2634. {
  2635. struct debugfs_timings_state *p = file->private_data;
  2636. struct kvm_vcpu *vcpu = p->vcpu;
  2637. char *s, *buf_end;
  2638. struct kvmhv_tb_accumulator tb;
  2639. u64 count;
  2640. loff_t pos;
  2641. ssize_t n;
  2642. int i, loops;
  2643. bool ok;
  2644. if (!p->buflen) {
  2645. s = p->buf;
  2646. buf_end = s + sizeof(p->buf);
  2647. for (i = 0; i < N_TIMINGS; ++i) {
  2648. struct kvmhv_tb_accumulator *acc;
  2649. acc = (struct kvmhv_tb_accumulator *)
  2650. ((unsigned long)vcpu + timings[i].offset);
  2651. ok = false;
  2652. for (loops = 0; loops < 1000; ++loops) {
  2653. count = acc->seqcount;
  2654. if (!(count & 1)) {
  2655. smp_rmb();
  2656. tb = *acc;
  2657. smp_rmb();
  2658. if (count == acc->seqcount) {
  2659. ok = true;
  2660. break;
  2661. }
  2662. }
  2663. udelay(1);
  2664. }
  2665. if (!ok)
  2666. snprintf(s, buf_end - s, "%s: stuck\n",
  2667. timings[i].name);
  2668. else
  2669. snprintf(s, buf_end - s,
  2670. "%s: %llu %llu %llu %llu\n",
  2671. timings[i].name, count / 2,
  2672. tb_to_ns(tb.tb_total),
  2673. tb_to_ns(tb.tb_min),
  2674. tb_to_ns(tb.tb_max));
  2675. s += strlen(s);
  2676. }
  2677. p->buflen = s - p->buf;
  2678. }
  2679. pos = *ppos;
  2680. if (pos >= p->buflen)
  2681. return 0;
  2682. if (len > p->buflen - pos)
  2683. len = p->buflen - pos;
  2684. n = copy_to_user(buf, p->buf + pos, len);
  2685. if (n) {
  2686. if (n == len)
  2687. return -EFAULT;
  2688. len -= n;
  2689. }
  2690. *ppos = pos + len;
  2691. return len;
  2692. }
  2693. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  2694. size_t len, loff_t *ppos)
  2695. {
  2696. return -EACCES;
  2697. }
  2698. static const struct file_operations debugfs_timings_ops = {
  2699. .owner = THIS_MODULE,
  2700. .open = debugfs_timings_open,
  2701. .release = debugfs_timings_release,
  2702. .read = debugfs_timings_read,
  2703. .write = debugfs_timings_write,
  2704. .llseek = generic_file_llseek,
  2705. };
  2706. /* Create a debugfs directory for the vcpu */
  2707. static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
  2708. {
  2709. if (cpu_has_feature(CPU_FTR_ARCH_300) == IS_ENABLED(CONFIG_KVM_BOOK3S_HV_P9_TIMING))
  2710. debugfs_create_file("timings", 0444, debugfs_dentry, vcpu,
  2711. &debugfs_timings_ops);
  2712. return 0;
  2713. }
  2714. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  2715. static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
  2716. {
  2717. return 0;
  2718. }
  2719. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  2720. static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
  2721. {
  2722. int err;
  2723. int core;
  2724. struct kvmppc_vcore *vcore;
  2725. struct kvm *kvm;
  2726. unsigned int id;
  2727. kvm = vcpu->kvm;
  2728. id = vcpu->vcpu_id;
  2729. vcpu->arch.shared = &vcpu->arch.shregs;
  2730. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  2731. /*
  2732. * The shared struct is never shared on HV,
  2733. * so we can always use host endianness
  2734. */
  2735. #ifdef __BIG_ENDIAN__
  2736. vcpu->arch.shared_big_endian = true;
  2737. #else
  2738. vcpu->arch.shared_big_endian = false;
  2739. #endif
  2740. #endif
  2741. if (kvmhv_is_nestedv2()) {
  2742. err = kvmhv_nestedv2_vcpu_create(vcpu, &vcpu->arch.nestedv2_io);
  2743. if (err < 0)
  2744. return err;
  2745. }
  2746. kvmppc_set_mmcr_hv(vcpu, 0, MMCR0_FC);
  2747. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  2748. kvmppc_set_mmcr_hv(vcpu, 0, kvmppc_get_mmcr_hv(vcpu, 0) | MMCR0_PMCCEXT);
  2749. kvmppc_set_mmcra_hv(vcpu, MMCRA_BHRB_DISABLE);
  2750. }
  2751. kvmppc_set_ctrl_hv(vcpu, CTRL_RUNLATCH);
  2752. /* default to host PVR, since we can't spoof it */
  2753. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  2754. spin_lock_init(&vcpu->arch.vpa_update_lock);
  2755. spin_lock_init(&vcpu->arch.tbacct_lock);
  2756. vcpu->arch.busy_preempt = TB_NIL;
  2757. __kvmppc_set_msr_hv(vcpu, MSR_ME);
  2758. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  2759. /*
  2760. * Set the default HFSCR for the guest from the host value.
  2761. * This value is only used on POWER9 and later.
  2762. * On >= POWER9, we want to virtualize the doorbell facility, so we
  2763. * don't set the HFSCR_MSGP bit, and that causes those instructions
  2764. * to trap and then we emulate them.
  2765. */
  2766. kvmppc_set_hfscr_hv(vcpu, HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
  2767. HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP);
  2768. /* On POWER10 and later, allow prefixed instructions */
  2769. if (cpu_has_feature(CPU_FTR_ARCH_31))
  2770. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PREFIX);
  2771. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  2772. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & mfspr(SPRN_HFSCR));
  2773. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2774. if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
  2775. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
  2776. #endif
  2777. }
  2778. if (cpu_has_feature(CPU_FTR_TM_COMP))
  2779. vcpu->arch.hfscr |= HFSCR_TM;
  2780. vcpu->arch.hfscr_permitted = kvmppc_get_hfscr_hv(vcpu);
  2781. /*
  2782. * PM, EBB, TM are demand-faulted so start with it clear.
  2783. */
  2784. kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & ~(HFSCR_PM | HFSCR_EBB | HFSCR_TM));
  2785. kvmppc_mmu_book3s_hv_init(vcpu);
  2786. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2787. init_waitqueue_head(&vcpu->arch.cpu_run);
  2788. mutex_lock(&kvm->lock);
  2789. vcore = NULL;
  2790. err = -EINVAL;
  2791. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  2792. if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
  2793. pr_devel("KVM: VCPU ID too high\n");
  2794. core = KVM_MAX_VCORES;
  2795. } else {
  2796. BUG_ON(kvm->arch.smt_mode != 1);
  2797. core = kvmppc_pack_vcpu_id(kvm, id);
  2798. }
  2799. } else {
  2800. core = id / kvm->arch.smt_mode;
  2801. }
  2802. if (core < KVM_MAX_VCORES) {
  2803. vcore = kvm->arch.vcores[core];
  2804. if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
  2805. pr_devel("KVM: collision on id %u", id);
  2806. vcore = NULL;
  2807. } else if (!vcore) {
  2808. /*
  2809. * Take mmu_setup_lock for mutual exclusion
  2810. * with kvmppc_update_lpcr().
  2811. */
  2812. err = -ENOMEM;
  2813. vcore = kvmppc_vcore_create(kvm,
  2814. id & ~(kvm->arch.smt_mode - 1));
  2815. mutex_lock(&kvm->arch.mmu_setup_lock);
  2816. kvm->arch.vcores[core] = vcore;
  2817. kvm->arch.online_vcores++;
  2818. mutex_unlock(&kvm->arch.mmu_setup_lock);
  2819. }
  2820. }
  2821. mutex_unlock(&kvm->lock);
  2822. if (!vcore)
  2823. return err;
  2824. spin_lock(&vcore->lock);
  2825. ++vcore->num_threads;
  2826. spin_unlock(&vcore->lock);
  2827. vcpu->arch.vcore = vcore;
  2828. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  2829. vcpu->arch.thread_cpu = -1;
  2830. vcpu->arch.prev_cpu = -1;
  2831. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  2832. kvmppc_sanity_check(vcpu);
  2833. return 0;
  2834. }
  2835. static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
  2836. unsigned long flags)
  2837. {
  2838. int err;
  2839. int esmt = 0;
  2840. if (flags)
  2841. return -EINVAL;
  2842. if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
  2843. return -EINVAL;
  2844. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  2845. /*
  2846. * On POWER8 (or POWER7), the threading mode is "strict",
  2847. * so we pack smt_mode vcpus per vcore.
  2848. */
  2849. if (smt_mode > threads_per_subcore)
  2850. return -EINVAL;
  2851. } else {
  2852. /*
  2853. * On POWER9, the threading mode is "loose",
  2854. * so each vcpu gets its own vcore.
  2855. */
  2856. esmt = smt_mode;
  2857. smt_mode = 1;
  2858. }
  2859. mutex_lock(&kvm->lock);
  2860. err = -EBUSY;
  2861. if (!kvm->arch.online_vcores) {
  2862. kvm->arch.smt_mode = smt_mode;
  2863. kvm->arch.emul_smt_mode = esmt;
  2864. err = 0;
  2865. }
  2866. mutex_unlock(&kvm->lock);
  2867. return err;
  2868. }
  2869. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  2870. {
  2871. if (vpa->pinned_addr)
  2872. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  2873. vpa->dirty);
  2874. }
  2875. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  2876. {
  2877. spin_lock(&vcpu->arch.vpa_update_lock);
  2878. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  2879. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  2880. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  2881. spin_unlock(&vcpu->arch.vpa_update_lock);
  2882. if (kvmhv_is_nestedv2())
  2883. kvmhv_nestedv2_vcpu_free(vcpu, &vcpu->arch.nestedv2_io);
  2884. }
  2885. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  2886. {
  2887. /* Indicate we want to get back into the guest */
  2888. return 1;
  2889. }
  2890. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  2891. {
  2892. unsigned long dec_nsec, now;
  2893. now = get_tb();
  2894. if (now > kvmppc_dec_expires_host_tb(vcpu)) {
  2895. /* decrementer has already gone negative */
  2896. kvmppc_core_queue_dec(vcpu);
  2897. kvmppc_core_prepare_to_enter(vcpu);
  2898. return;
  2899. }
  2900. dec_nsec = tb_to_ns(kvmppc_dec_expires_host_tb(vcpu) - now);
  2901. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  2902. vcpu->arch.timer_running = 1;
  2903. }
  2904. extern int __kvmppc_vcore_entry(void);
  2905. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  2906. struct kvm_vcpu *vcpu, u64 tb)
  2907. {
  2908. u64 now;
  2909. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  2910. return;
  2911. spin_lock_irq(&vcpu->arch.tbacct_lock);
  2912. now = tb;
  2913. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  2914. vcpu->arch.stolen_logged;
  2915. vcpu->arch.busy_preempt = now;
  2916. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2917. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  2918. --vc->n_runnable;
  2919. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  2920. }
  2921. static int kvmppc_grab_hwthread(int cpu)
  2922. {
  2923. struct paca_struct *tpaca;
  2924. long timeout = 10000;
  2925. tpaca = paca_ptrs[cpu];
  2926. /* Ensure the thread won't go into the kernel if it wakes */
  2927. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2928. tpaca->kvm_hstate.kvm_vcore = NULL;
  2929. tpaca->kvm_hstate.napping = 0;
  2930. smp_wmb();
  2931. tpaca->kvm_hstate.hwthread_req = 1;
  2932. /*
  2933. * If the thread is already executing in the kernel (e.g. handling
  2934. * a stray interrupt), wait for it to get back to nap mode.
  2935. * The smp_mb() is to ensure that our setting of hwthread_req
  2936. * is visible before we look at hwthread_state, so if this
  2937. * races with the code at system_reset_pSeries and the thread
  2938. * misses our setting of hwthread_req, we are sure to see its
  2939. * setting of hwthread_state, and vice versa.
  2940. */
  2941. smp_mb();
  2942. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  2943. if (--timeout <= 0) {
  2944. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  2945. return -EBUSY;
  2946. }
  2947. udelay(1);
  2948. }
  2949. return 0;
  2950. }
  2951. static void kvmppc_release_hwthread(int cpu)
  2952. {
  2953. struct paca_struct *tpaca;
  2954. tpaca = paca_ptrs[cpu];
  2955. tpaca->kvm_hstate.hwthread_req = 0;
  2956. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2957. tpaca->kvm_hstate.kvm_vcore = NULL;
  2958. tpaca->kvm_hstate.kvm_split_mode = NULL;
  2959. }
  2960. static DEFINE_PER_CPU(struct kvm *, cpu_in_guest);
  2961. static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
  2962. {
  2963. struct kvm_nested_guest *nested = vcpu->arch.nested;
  2964. cpumask_t *need_tlb_flush;
  2965. int i;
  2966. if (nested)
  2967. need_tlb_flush = &nested->need_tlb_flush;
  2968. else
  2969. need_tlb_flush = &kvm->arch.need_tlb_flush;
  2970. cpu = cpu_first_tlb_thread_sibling(cpu);
  2971. for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
  2972. i += cpu_tlb_thread_sibling_step())
  2973. cpumask_set_cpu(i, need_tlb_flush);
  2974. /*
  2975. * Make sure setting of bit in need_tlb_flush precedes testing of
  2976. * cpu_in_guest. The matching barrier on the other side is hwsync
  2977. * when switching to guest MMU mode, which happens between
  2978. * cpu_in_guest being set to the guest kvm, and need_tlb_flush bit
  2979. * being tested.
  2980. */
  2981. smp_mb();
  2982. for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
  2983. i += cpu_tlb_thread_sibling_step()) {
  2984. struct kvm *running = *per_cpu_ptr(&cpu_in_guest, i);
  2985. if (running == kvm)
  2986. smp_call_function_single(i, do_nothing, NULL, 1);
  2987. }
  2988. }
  2989. static void do_migrate_away_vcpu(void *arg)
  2990. {
  2991. struct kvm_vcpu *vcpu = arg;
  2992. struct kvm *kvm = vcpu->kvm;
  2993. /*
  2994. * If the guest has GTSE, it may execute tlbie, so do a eieio; tlbsync;
  2995. * ptesync sequence on the old CPU before migrating to a new one, in
  2996. * case we interrupted the guest between a tlbie ; eieio ;
  2997. * tlbsync; ptesync sequence.
  2998. *
  2999. * Otherwise, ptesync is sufficient for ordering tlbiel sequences.
  3000. */
  3001. if (kvm->arch.lpcr & LPCR_GTSE)
  3002. asm volatile("eieio; tlbsync; ptesync");
  3003. else
  3004. asm volatile("ptesync");
  3005. }
  3006. static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
  3007. {
  3008. struct kvm_nested_guest *nested = vcpu->arch.nested;
  3009. struct kvm *kvm = vcpu->kvm;
  3010. int prev_cpu;
  3011. if (!cpu_has_feature(CPU_FTR_HVMODE))
  3012. return;
  3013. if (nested)
  3014. prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
  3015. else
  3016. prev_cpu = vcpu->arch.prev_cpu;
  3017. /*
  3018. * With radix, the guest can do TLB invalidations itself,
  3019. * and it could choose to use the local form (tlbiel) if
  3020. * it is invalidating a translation that has only ever been
  3021. * used on one vcpu. However, that doesn't mean it has
  3022. * only ever been used on one physical cpu, since vcpus
  3023. * can move around between pcpus. To cope with this, when
  3024. * a vcpu moves from one pcpu to another, we need to tell
  3025. * any vcpus running on the same core as this vcpu previously
  3026. * ran to flush the TLB.
  3027. */
  3028. if (prev_cpu != pcpu) {
  3029. if (prev_cpu >= 0) {
  3030. if (cpu_first_tlb_thread_sibling(prev_cpu) !=
  3031. cpu_first_tlb_thread_sibling(pcpu))
  3032. radix_flush_cpu(kvm, prev_cpu, vcpu);
  3033. smp_call_function_single(prev_cpu,
  3034. do_migrate_away_vcpu, vcpu, 1);
  3035. }
  3036. if (nested)
  3037. nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
  3038. else
  3039. vcpu->arch.prev_cpu = pcpu;
  3040. }
  3041. }
  3042. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  3043. {
  3044. int cpu;
  3045. struct paca_struct *tpaca;
  3046. cpu = vc->pcpu;
  3047. if (vcpu) {
  3048. if (vcpu->arch.timer_running) {
  3049. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  3050. vcpu->arch.timer_running = 0;
  3051. }
  3052. cpu += vcpu->arch.ptid;
  3053. vcpu->cpu = vc->pcpu;
  3054. vcpu->arch.thread_cpu = cpu;
  3055. }
  3056. tpaca = paca_ptrs[cpu];
  3057. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  3058. tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
  3059. tpaca->kvm_hstate.fake_suspend = 0;
  3060. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  3061. smp_wmb();
  3062. tpaca->kvm_hstate.kvm_vcore = vc;
  3063. if (cpu != smp_processor_id())
  3064. kvmppc_ipi_thread(cpu);
  3065. }
  3066. static void kvmppc_wait_for_nap(int n_threads)
  3067. {
  3068. int cpu = smp_processor_id();
  3069. int i, loops;
  3070. if (n_threads <= 1)
  3071. return;
  3072. for (loops = 0; loops < 1000000; ++loops) {
  3073. /*
  3074. * Check if all threads are finished.
  3075. * We set the vcore pointer when starting a thread
  3076. * and the thread clears it when finished, so we look
  3077. * for any threads that still have a non-NULL vcore ptr.
  3078. */
  3079. for (i = 1; i < n_threads; ++i)
  3080. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  3081. break;
  3082. if (i == n_threads) {
  3083. HMT_medium();
  3084. return;
  3085. }
  3086. HMT_low();
  3087. }
  3088. HMT_medium();
  3089. for (i = 1; i < n_threads; ++i)
  3090. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  3091. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  3092. }
  3093. /*
  3094. * Check that we are on thread 0 and that any other threads in
  3095. * this core are off-line. Then grab the threads so they can't
  3096. * enter the kernel.
  3097. */
  3098. static int on_primary_thread(void)
  3099. {
  3100. int cpu = smp_processor_id();
  3101. int thr;
  3102. /* Are we on a primary subcore? */
  3103. if (cpu_thread_in_subcore(cpu))
  3104. return 0;
  3105. thr = 0;
  3106. while (++thr < threads_per_subcore)
  3107. if (cpu_online(cpu + thr))
  3108. return 0;
  3109. /* Grab all hw threads so they can't go into the kernel */
  3110. for (thr = 1; thr < threads_per_subcore; ++thr) {
  3111. if (kvmppc_grab_hwthread(cpu + thr)) {
  3112. /* Couldn't grab one; let the others go */
  3113. do {
  3114. kvmppc_release_hwthread(cpu + thr);
  3115. } while (--thr > 0);
  3116. return 0;
  3117. }
  3118. }
  3119. return 1;
  3120. }
  3121. /*
  3122. * A list of virtual cores for each physical CPU.
  3123. * These are vcores that could run but their runner VCPU tasks are
  3124. * (or may be) preempted.
  3125. */
  3126. struct preempted_vcore_list {
  3127. struct list_head list;
  3128. spinlock_t lock;
  3129. };
  3130. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  3131. static void init_vcore_lists(void)
  3132. {
  3133. int cpu;
  3134. for_each_possible_cpu(cpu) {
  3135. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  3136. spin_lock_init(&lp->lock);
  3137. INIT_LIST_HEAD(&lp->list);
  3138. }
  3139. }
  3140. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  3141. {
  3142. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  3143. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  3144. vc->vcore_state = VCORE_PREEMPT;
  3145. vc->pcpu = smp_processor_id();
  3146. if (vc->num_threads < threads_per_vcore(vc->kvm)) {
  3147. spin_lock(&lp->lock);
  3148. list_add_tail(&vc->preempt_list, &lp->list);
  3149. spin_unlock(&lp->lock);
  3150. }
  3151. /* Start accumulating stolen time */
  3152. kvmppc_core_start_stolen(vc, mftb());
  3153. }
  3154. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  3155. {
  3156. struct preempted_vcore_list *lp;
  3157. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  3158. kvmppc_core_end_stolen(vc, mftb());
  3159. if (!list_empty(&vc->preempt_list)) {
  3160. lp = &per_cpu(preempted_vcores, vc->pcpu);
  3161. spin_lock(&lp->lock);
  3162. list_del_init(&vc->preempt_list);
  3163. spin_unlock(&lp->lock);
  3164. }
  3165. vc->vcore_state = VCORE_INACTIVE;
  3166. }
  3167. /*
  3168. * This stores information about the virtual cores currently
  3169. * assigned to a physical core.
  3170. */
  3171. struct core_info {
  3172. int n_subcores;
  3173. int max_subcore_threads;
  3174. int total_threads;
  3175. int subcore_threads[MAX_SUBCORES];
  3176. struct kvmppc_vcore *vc[MAX_SUBCORES];
  3177. };
  3178. /*
  3179. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  3180. * respectively in 2-way micro-threading (split-core) mode on POWER8.
  3181. */
  3182. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  3183. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  3184. {
  3185. memset(cip, 0, sizeof(*cip));
  3186. cip->n_subcores = 1;
  3187. cip->max_subcore_threads = vc->num_threads;
  3188. cip->total_threads = vc->num_threads;
  3189. cip->subcore_threads[0] = vc->num_threads;
  3190. cip->vc[0] = vc;
  3191. }
  3192. static bool subcore_config_ok(int n_subcores, int n_threads)
  3193. {
  3194. /*
  3195. * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
  3196. * split-core mode, with one thread per subcore.
  3197. */
  3198. if (cpu_has_feature(CPU_FTR_ARCH_300))
  3199. return n_subcores <= 4 && n_threads == 1;
  3200. /* On POWER8, can only dynamically split if unsplit to begin with */
  3201. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  3202. return false;
  3203. if (n_subcores > MAX_SUBCORES)
  3204. return false;
  3205. if (n_subcores > 1) {
  3206. if (!(dynamic_mt_modes & 2))
  3207. n_subcores = 4;
  3208. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  3209. return false;
  3210. }
  3211. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  3212. }
  3213. static void init_vcore_to_run(struct kvmppc_vcore *vc)
  3214. {
  3215. vc->entry_exit_map = 0;
  3216. vc->in_guest = 0;
  3217. vc->napping_threads = 0;
  3218. vc->conferring_threads = 0;
  3219. vc->tb_offset_applied = 0;
  3220. }
  3221. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  3222. {
  3223. int n_threads = vc->num_threads;
  3224. int sub;
  3225. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  3226. return false;
  3227. /* In one_vm_per_core mode, require all vcores to be from the same vm */
  3228. if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
  3229. return false;
  3230. if (n_threads < cip->max_subcore_threads)
  3231. n_threads = cip->max_subcore_threads;
  3232. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  3233. return false;
  3234. cip->max_subcore_threads = n_threads;
  3235. sub = cip->n_subcores;
  3236. ++cip->n_subcores;
  3237. cip->total_threads += vc->num_threads;
  3238. cip->subcore_threads[sub] = vc->num_threads;
  3239. cip->vc[sub] = vc;
  3240. init_vcore_to_run(vc);
  3241. list_del_init(&vc->preempt_list);
  3242. return true;
  3243. }
  3244. /*
  3245. * Work out whether it is possible to piggyback the execution of
  3246. * vcore *pvc onto the execution of the other vcores described in *cip.
  3247. */
  3248. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  3249. int target_threads)
  3250. {
  3251. if (cip->total_threads + pvc->num_threads > target_threads)
  3252. return false;
  3253. return can_dynamic_split(pvc, cip);
  3254. }
  3255. static void prepare_threads(struct kvmppc_vcore *vc)
  3256. {
  3257. int i;
  3258. struct kvm_vcpu *vcpu;
  3259. for_each_runnable_thread(i, vcpu, vc) {
  3260. if (signal_pending(vcpu->arch.run_task))
  3261. vcpu->arch.ret = -EINTR;
  3262. else if (vcpu->arch.vpa.update_pending ||
  3263. vcpu->arch.slb_shadow.update_pending ||
  3264. vcpu->arch.dtl.update_pending)
  3265. vcpu->arch.ret = RESUME_GUEST;
  3266. else
  3267. continue;
  3268. kvmppc_remove_runnable(vc, vcpu, mftb());
  3269. wake_up(&vcpu->arch.cpu_run);
  3270. }
  3271. }
  3272. static void collect_piggybacks(struct core_info *cip, int target_threads)
  3273. {
  3274. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  3275. struct kvmppc_vcore *pvc, *vcnext;
  3276. spin_lock(&lp->lock);
  3277. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  3278. if (!spin_trylock(&pvc->lock))
  3279. continue;
  3280. prepare_threads(pvc);
  3281. if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
  3282. list_del_init(&pvc->preempt_list);
  3283. if (pvc->runner == NULL) {
  3284. pvc->vcore_state = VCORE_INACTIVE;
  3285. kvmppc_core_end_stolen(pvc, mftb());
  3286. }
  3287. spin_unlock(&pvc->lock);
  3288. continue;
  3289. }
  3290. if (!can_piggyback(pvc, cip, target_threads)) {
  3291. spin_unlock(&pvc->lock);
  3292. continue;
  3293. }
  3294. kvmppc_core_end_stolen(pvc, mftb());
  3295. pvc->vcore_state = VCORE_PIGGYBACK;
  3296. if (cip->total_threads >= target_threads)
  3297. break;
  3298. }
  3299. spin_unlock(&lp->lock);
  3300. }
  3301. static bool recheck_signals_and_mmu(struct core_info *cip)
  3302. {
  3303. int sub, i;
  3304. struct kvm_vcpu *vcpu;
  3305. struct kvmppc_vcore *vc;
  3306. for (sub = 0; sub < cip->n_subcores; ++sub) {
  3307. vc = cip->vc[sub];
  3308. if (!vc->kvm->arch.mmu_ready)
  3309. return true;
  3310. for_each_runnable_thread(i, vcpu, vc)
  3311. if (signal_pending(vcpu->arch.run_task))
  3312. return true;
  3313. }
  3314. return false;
  3315. }
  3316. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  3317. {
  3318. int still_running = 0, i;
  3319. u64 now;
  3320. long ret;
  3321. struct kvm_vcpu *vcpu;
  3322. spin_lock(&vc->lock);
  3323. now = get_tb();
  3324. for_each_runnable_thread(i, vcpu, vc) {
  3325. /*
  3326. * It's safe to unlock the vcore in the loop here, because
  3327. * for_each_runnable_thread() is safe against removal of
  3328. * the vcpu, and the vcore state is VCORE_EXITING here,
  3329. * so any vcpus becoming runnable will have their arch.trap
  3330. * set to zero and can't actually run in the guest.
  3331. */
  3332. spin_unlock(&vc->lock);
  3333. /* cancel pending dec exception if dec is positive */
  3334. if (now < kvmppc_dec_expires_host_tb(vcpu) &&
  3335. kvmppc_core_pending_dec(vcpu))
  3336. kvmppc_core_dequeue_dec(vcpu);
  3337. trace_kvm_guest_exit(vcpu);
  3338. ret = RESUME_GUEST;
  3339. if (vcpu->arch.trap)
  3340. ret = kvmppc_handle_exit_hv(vcpu,
  3341. vcpu->arch.run_task);
  3342. vcpu->arch.ret = ret;
  3343. vcpu->arch.trap = 0;
  3344. spin_lock(&vc->lock);
  3345. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  3346. if (vcpu->arch.pending_exceptions)
  3347. kvmppc_core_prepare_to_enter(vcpu);
  3348. if (vcpu->arch.ceded)
  3349. kvmppc_set_timer(vcpu);
  3350. else
  3351. ++still_running;
  3352. } else {
  3353. kvmppc_remove_runnable(vc, vcpu, mftb());
  3354. wake_up(&vcpu->arch.cpu_run);
  3355. }
  3356. }
  3357. if (!is_master) {
  3358. if (still_running > 0) {
  3359. kvmppc_vcore_preempt(vc);
  3360. } else if (vc->runner) {
  3361. vc->vcore_state = VCORE_PREEMPT;
  3362. kvmppc_core_start_stolen(vc, mftb());
  3363. } else {
  3364. vc->vcore_state = VCORE_INACTIVE;
  3365. }
  3366. if (vc->n_runnable > 0 && vc->runner == NULL) {
  3367. /* make sure there's a candidate runner awake */
  3368. i = -1;
  3369. vcpu = next_runnable_thread(vc, &i);
  3370. wake_up(&vcpu->arch.cpu_run);
  3371. }
  3372. }
  3373. spin_unlock(&vc->lock);
  3374. }
  3375. /*
  3376. * Clear core from the list of active host cores as we are about to
  3377. * enter the guest. Only do this if it is the primary thread of the
  3378. * core (not if a subcore) that is entering the guest.
  3379. */
  3380. static inline int kvmppc_clear_host_core(unsigned int cpu)
  3381. {
  3382. int core;
  3383. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  3384. return 0;
  3385. /*
  3386. * Memory barrier can be omitted here as we will do a smp_wmb()
  3387. * later in kvmppc_start_thread and we need ensure that state is
  3388. * visible to other CPUs only after we enter guest.
  3389. */
  3390. core = cpu >> threads_shift;
  3391. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  3392. return 0;
  3393. }
  3394. /*
  3395. * Advertise this core as an active host core since we exited the guest
  3396. * Only need to do this if it is the primary thread of the core that is
  3397. * exiting.
  3398. */
  3399. static inline int kvmppc_set_host_core(unsigned int cpu)
  3400. {
  3401. int core;
  3402. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  3403. return 0;
  3404. /*
  3405. * Memory barrier can be omitted here because we do a spin_unlock
  3406. * immediately after this which provides the memory barrier.
  3407. */
  3408. core = cpu >> threads_shift;
  3409. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  3410. return 0;
  3411. }
  3412. static void set_irq_happened(int trap)
  3413. {
  3414. switch (trap) {
  3415. case BOOK3S_INTERRUPT_EXTERNAL:
  3416. local_paca->irq_happened |= PACA_IRQ_EE;
  3417. break;
  3418. case BOOK3S_INTERRUPT_H_DOORBELL:
  3419. local_paca->irq_happened |= PACA_IRQ_DBELL;
  3420. break;
  3421. case BOOK3S_INTERRUPT_HMI:
  3422. local_paca->irq_happened |= PACA_IRQ_HMI;
  3423. break;
  3424. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  3425. replay_system_reset();
  3426. break;
  3427. }
  3428. }
  3429. /*
  3430. * Run a set of guest threads on a physical core.
  3431. * Called with vc->lock held.
  3432. */
  3433. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  3434. {
  3435. struct kvm_vcpu *vcpu;
  3436. int i;
  3437. int srcu_idx;
  3438. struct core_info core_info;
  3439. struct kvmppc_vcore *pvc;
  3440. struct kvm_split_mode split_info, *sip;
  3441. int split, subcore_size, active;
  3442. int sub;
  3443. bool thr0_done;
  3444. unsigned long cmd_bit, stat_bit;
  3445. int pcpu, thr;
  3446. int target_threads;
  3447. int controlled_threads;
  3448. int trap;
  3449. bool is_power8;
  3450. if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
  3451. return;
  3452. /*
  3453. * Remove from the list any threads that have a signal pending
  3454. * or need a VPA update done
  3455. */
  3456. prepare_threads(vc);
  3457. /* if the runner is no longer runnable, let the caller pick a new one */
  3458. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  3459. return;
  3460. /*
  3461. * Initialize *vc.
  3462. */
  3463. init_vcore_to_run(vc);
  3464. vc->preempt_tb = TB_NIL;
  3465. /*
  3466. * Number of threads that we will be controlling: the same as
  3467. * the number of threads per subcore, except on POWER9,
  3468. * where it's 1 because the threads are (mostly) independent.
  3469. */
  3470. controlled_threads = threads_per_vcore(vc->kvm);
  3471. /*
  3472. * Make sure we are running on primary threads, and that secondary
  3473. * threads are offline. Also check if the number of threads in this
  3474. * guest are greater than the current system threads per guest.
  3475. */
  3476. if ((controlled_threads > 1) &&
  3477. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
  3478. for_each_runnable_thread(i, vcpu, vc) {
  3479. vcpu->arch.ret = -EBUSY;
  3480. kvmppc_remove_runnable(vc, vcpu, mftb());
  3481. wake_up(&vcpu->arch.cpu_run);
  3482. }
  3483. goto out;
  3484. }
  3485. /*
  3486. * See if we could run any other vcores on the physical core
  3487. * along with this one.
  3488. */
  3489. init_core_info(&core_info, vc);
  3490. pcpu = smp_processor_id();
  3491. target_threads = controlled_threads;
  3492. if (target_smt_mode && target_smt_mode < target_threads)
  3493. target_threads = target_smt_mode;
  3494. if (vc->num_threads < target_threads)
  3495. collect_piggybacks(&core_info, target_threads);
  3496. /*
  3497. * Hard-disable interrupts, and check resched flag and signals.
  3498. * If we need to reschedule or deliver a signal, clean up
  3499. * and return without going into the guest(s).
  3500. * If the mmu_ready flag has been cleared, don't go into the
  3501. * guest because that means a HPT resize operation is in progress.
  3502. */
  3503. local_irq_disable();
  3504. hard_irq_disable();
  3505. if (lazy_irq_pending() || need_resched() ||
  3506. recheck_signals_and_mmu(&core_info)) {
  3507. local_irq_enable();
  3508. vc->vcore_state = VCORE_INACTIVE;
  3509. /* Unlock all except the primary vcore */
  3510. for (sub = 1; sub < core_info.n_subcores; ++sub) {
  3511. pvc = core_info.vc[sub];
  3512. /* Put back on to the preempted vcores list */
  3513. kvmppc_vcore_preempt(pvc);
  3514. spin_unlock(&pvc->lock);
  3515. }
  3516. for (i = 0; i < controlled_threads; ++i)
  3517. kvmppc_release_hwthread(pcpu + i);
  3518. return;
  3519. }
  3520. kvmppc_clear_host_core(pcpu);
  3521. /* Decide on micro-threading (split-core) mode */
  3522. subcore_size = threads_per_subcore;
  3523. cmd_bit = stat_bit = 0;
  3524. split = core_info.n_subcores;
  3525. sip = NULL;
  3526. is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
  3527. if (split > 1) {
  3528. sip = &split_info;
  3529. memset(&split_info, 0, sizeof(split_info));
  3530. for (sub = 0; sub < core_info.n_subcores; ++sub)
  3531. split_info.vc[sub] = core_info.vc[sub];
  3532. if (is_power8) {
  3533. if (split == 2 && (dynamic_mt_modes & 2)) {
  3534. cmd_bit = HID0_POWER8_1TO2LPAR;
  3535. stat_bit = HID0_POWER8_2LPARMODE;
  3536. } else {
  3537. split = 4;
  3538. cmd_bit = HID0_POWER8_1TO4LPAR;
  3539. stat_bit = HID0_POWER8_4LPARMODE;
  3540. }
  3541. subcore_size = MAX_SMT_THREADS / split;
  3542. split_info.rpr = mfspr(SPRN_RPR);
  3543. split_info.pmmar = mfspr(SPRN_PMMAR);
  3544. split_info.ldbar = mfspr(SPRN_LDBAR);
  3545. split_info.subcore_size = subcore_size;
  3546. } else {
  3547. split_info.subcore_size = 1;
  3548. }
  3549. /* order writes to split_info before kvm_split_mode pointer */
  3550. smp_wmb();
  3551. }
  3552. for (thr = 0; thr < controlled_threads; ++thr) {
  3553. struct paca_struct *paca = paca_ptrs[pcpu + thr];
  3554. paca->kvm_hstate.napping = 0;
  3555. paca->kvm_hstate.kvm_split_mode = sip;
  3556. }
  3557. /* Initiate micro-threading (split-core) on POWER8 if required */
  3558. if (cmd_bit) {
  3559. unsigned long hid0 = mfspr(SPRN_HID0);
  3560. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  3561. mb();
  3562. mtspr(SPRN_HID0, hid0);
  3563. isync();
  3564. for (;;) {
  3565. hid0 = mfspr(SPRN_HID0);
  3566. if (hid0 & stat_bit)
  3567. break;
  3568. cpu_relax();
  3569. }
  3570. }
  3571. /*
  3572. * On POWER8, set RWMR register.
  3573. * Since it only affects PURR and SPURR, it doesn't affect
  3574. * the host, so we don't save/restore the host value.
  3575. */
  3576. if (is_power8) {
  3577. unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
  3578. int n_online = atomic_read(&vc->online_count);
  3579. /*
  3580. * Use the 8-thread value if we're doing split-core
  3581. * or if the vcore's online count looks bogus.
  3582. */
  3583. if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
  3584. n_online >= 1 && n_online <= MAX_SMT_THREADS)
  3585. rwmr_val = p8_rwmr_values[n_online];
  3586. mtspr(SPRN_RWMR, rwmr_val);
  3587. }
  3588. /* Start all the threads */
  3589. active = 0;
  3590. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  3591. thr = is_power8 ? subcore_thread_map[sub] : sub;
  3592. thr0_done = false;
  3593. active |= 1 << thr;
  3594. pvc = core_info.vc[sub];
  3595. pvc->pcpu = pcpu + thr;
  3596. for_each_runnable_thread(i, vcpu, pvc) {
  3597. /*
  3598. * XXX: is kvmppc_start_thread called too late here?
  3599. * It updates vcpu->cpu and vcpu->arch.thread_cpu
  3600. * which are used by kvmppc_fast_vcpu_kick_hv(), but
  3601. * kick is called after new exceptions become available
  3602. * and exceptions are checked earlier than here, by
  3603. * kvmppc_core_prepare_to_enter.
  3604. */
  3605. kvmppc_start_thread(vcpu, pvc);
  3606. kvmppc_update_vpa_dispatch(vcpu, pvc);
  3607. trace_kvm_guest_enter(vcpu);
  3608. if (!vcpu->arch.ptid)
  3609. thr0_done = true;
  3610. active |= 1 << (thr + vcpu->arch.ptid);
  3611. }
  3612. /*
  3613. * We need to start the first thread of each subcore
  3614. * even if it doesn't have a vcpu.
  3615. */
  3616. if (!thr0_done)
  3617. kvmppc_start_thread(NULL, pvc);
  3618. }
  3619. /*
  3620. * Ensure that split_info.do_nap is set after setting
  3621. * the vcore pointer in the PACA of the secondaries.
  3622. */
  3623. smp_mb();
  3624. /*
  3625. * When doing micro-threading, poke the inactive threads as well.
  3626. * This gets them to the nap instruction after kvm_do_nap,
  3627. * which reduces the time taken to unsplit later.
  3628. */
  3629. if (cmd_bit) {
  3630. split_info.do_nap = 1; /* ask secondaries to nap when done */
  3631. for (thr = 1; thr < threads_per_subcore; ++thr)
  3632. if (!(active & (1 << thr)))
  3633. kvmppc_ipi_thread(pcpu + thr);
  3634. }
  3635. vc->vcore_state = VCORE_RUNNING;
  3636. preempt_disable();
  3637. trace_kvmppc_run_core(vc, 0);
  3638. for (sub = 0; sub < core_info.n_subcores; ++sub)
  3639. spin_unlock(&core_info.vc[sub]->lock);
  3640. guest_timing_enter_irqoff();
  3641. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  3642. guest_state_enter_irqoff();
  3643. this_cpu_disable_ftrace();
  3644. trap = __kvmppc_vcore_entry();
  3645. this_cpu_enable_ftrace();
  3646. guest_state_exit_irqoff();
  3647. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  3648. set_irq_happened(trap);
  3649. spin_lock(&vc->lock);
  3650. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  3651. vc->vcore_state = VCORE_EXITING;
  3652. /* wait for secondary threads to finish writing their state to memory */
  3653. kvmppc_wait_for_nap(controlled_threads);
  3654. /* Return to whole-core mode if we split the core earlier */
  3655. if (cmd_bit) {
  3656. unsigned long hid0 = mfspr(SPRN_HID0);
  3657. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  3658. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  3659. mb();
  3660. mtspr(SPRN_HID0, hid0);
  3661. isync();
  3662. for (;;) {
  3663. hid0 = mfspr(SPRN_HID0);
  3664. if (!(hid0 & stat_bit))
  3665. break;
  3666. cpu_relax();
  3667. }
  3668. split_info.do_nap = 0;
  3669. }
  3670. kvmppc_set_host_core(pcpu);
  3671. if (!vtime_accounting_enabled_this_cpu()) {
  3672. local_irq_enable();
  3673. /*
  3674. * Service IRQs here before guest_timing_exit_irqoff() so any
  3675. * ticks that occurred while running the guest are accounted to
  3676. * the guest. If vtime accounting is enabled, accounting uses
  3677. * TB rather than ticks, so it can be done without enabling
  3678. * interrupts here, which has the problem that it accounts
  3679. * interrupt processing overhead to the host.
  3680. */
  3681. local_irq_disable();
  3682. }
  3683. guest_timing_exit_irqoff();
  3684. local_irq_enable();
  3685. /* Let secondaries go back to the offline loop */
  3686. for (i = 0; i < controlled_threads; ++i) {
  3687. kvmppc_release_hwthread(pcpu + i);
  3688. if (sip && sip->napped[i])
  3689. kvmppc_ipi_thread(pcpu + i);
  3690. }
  3691. spin_unlock(&vc->lock);
  3692. /* make sure updates to secondary vcpu structs are visible now */
  3693. smp_mb();
  3694. preempt_enable();
  3695. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  3696. pvc = core_info.vc[sub];
  3697. post_guest_process(pvc, pvc == vc);
  3698. }
  3699. spin_lock(&vc->lock);
  3700. out:
  3701. vc->vcore_state = VCORE_INACTIVE;
  3702. trace_kvmppc_run_core(vc, 1);
  3703. }
  3704. static inline bool hcall_is_xics(unsigned long req)
  3705. {
  3706. return req == H_EOI || req == H_CPPR || req == H_IPI ||
  3707. req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
  3708. }
  3709. static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu)
  3710. {
  3711. struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
  3712. if (lp) {
  3713. u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
  3714. lp->yield_count = cpu_to_be32(yield_count);
  3715. vcpu->arch.vpa.dirty = 1;
  3716. }
  3717. }
  3718. /* Helper functions for reading L2's stats from L1's VPA */
  3719. #ifdef CONFIG_PPC_PSERIES
  3720. static DEFINE_PER_CPU(u64, l1_to_l2_cs);
  3721. static DEFINE_PER_CPU(u64, l2_to_l1_cs);
  3722. static DEFINE_PER_CPU(u64, l2_runtime_agg);
  3723. int kvmhv_get_l2_counters_status(void)
  3724. {
  3725. return firmware_has_feature(FW_FEATURE_LPAR) &&
  3726. get_lppaca()->l2_counters_enable;
  3727. }
  3728. void kvmhv_set_l2_counters_status(int cpu, bool status)
  3729. {
  3730. if (!firmware_has_feature(FW_FEATURE_LPAR))
  3731. return;
  3732. if (status)
  3733. lppaca_of(cpu).l2_counters_enable = 1;
  3734. else
  3735. lppaca_of(cpu).l2_counters_enable = 0;
  3736. }
  3737. int kvmhv_counters_tracepoint_regfunc(void)
  3738. {
  3739. int cpu;
  3740. for_each_present_cpu(cpu) {
  3741. kvmhv_set_l2_counters_status(cpu, true);
  3742. }
  3743. return 0;
  3744. }
  3745. void kvmhv_counters_tracepoint_unregfunc(void)
  3746. {
  3747. int cpu;
  3748. for_each_present_cpu(cpu) {
  3749. kvmhv_set_l2_counters_status(cpu, false);
  3750. }
  3751. }
  3752. static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
  3753. {
  3754. struct lppaca *lp = get_lppaca();
  3755. u64 l1_to_l2_ns, l2_to_l1_ns, l2_runtime_ns;
  3756. u64 *l1_to_l2_cs_ptr = this_cpu_ptr(&l1_to_l2_cs);
  3757. u64 *l2_to_l1_cs_ptr = this_cpu_ptr(&l2_to_l1_cs);
  3758. u64 *l2_runtime_agg_ptr = this_cpu_ptr(&l2_runtime_agg);
  3759. l1_to_l2_ns = tb_to_ns(be64_to_cpu(lp->l1_to_l2_cs_tb));
  3760. l2_to_l1_ns = tb_to_ns(be64_to_cpu(lp->l2_to_l1_cs_tb));
  3761. l2_runtime_ns = tb_to_ns(be64_to_cpu(lp->l2_runtime_tb));
  3762. trace_kvmppc_vcpu_stats(vcpu, l1_to_l2_ns - *l1_to_l2_cs_ptr,
  3763. l2_to_l1_ns - *l2_to_l1_cs_ptr,
  3764. l2_runtime_ns - *l2_runtime_agg_ptr);
  3765. *l1_to_l2_cs_ptr = l1_to_l2_ns;
  3766. *l2_to_l1_cs_ptr = l2_to_l1_ns;
  3767. *l2_runtime_agg_ptr = l2_runtime_ns;
  3768. }
  3769. #else
  3770. int kvmhv_get_l2_counters_status(void)
  3771. {
  3772. return 0;
  3773. }
  3774. static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
  3775. {
  3776. }
  3777. #endif
  3778. static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit,
  3779. unsigned long lpcr, u64 *tb)
  3780. {
  3781. struct kvmhv_nestedv2_io *io;
  3782. unsigned long msr, i;
  3783. int trap;
  3784. long rc;
  3785. if (vcpu->arch.doorbell_request) {
  3786. vcpu->arch.doorbell_request = 0;
  3787. kvmppc_set_dpdes(vcpu, 1);
  3788. }
  3789. io = &vcpu->arch.nestedv2_io;
  3790. msr = mfmsr();
  3791. kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
  3792. if (lazy_irq_pending())
  3793. return 0;
  3794. rc = kvmhv_nestedv2_flush_vcpu(vcpu, time_limit);
  3795. if (rc < 0)
  3796. return -EINVAL;
  3797. kvmppc_gse_put_u64(io->vcpu_run_input, KVMPPC_GSID_LPCR, lpcr);
  3798. accumulate_time(vcpu, &vcpu->arch.in_guest);
  3799. rc = plpar_guest_run_vcpu(0, vcpu->kvm->arch.lpid, vcpu->vcpu_id,
  3800. &trap, &i);
  3801. if (rc != H_SUCCESS) {
  3802. pr_err("KVM Guest Run VCPU hcall failed\n");
  3803. if (rc == H_INVALID_ELEMENT_ID)
  3804. pr_err("KVM: Guest Run VCPU invalid element id at %ld\n", i);
  3805. else if (rc == H_INVALID_ELEMENT_SIZE)
  3806. pr_err("KVM: Guest Run VCPU invalid element size at %ld\n", i);
  3807. else if (rc == H_INVALID_ELEMENT_VALUE)
  3808. pr_err("KVM: Guest Run VCPU invalid element value at %ld\n", i);
  3809. return -EINVAL;
  3810. }
  3811. accumulate_time(vcpu, &vcpu->arch.guest_exit);
  3812. *tb = mftb();
  3813. kvmppc_gsm_reset(io->vcpu_message);
  3814. kvmppc_gsm_reset(io->vcore_message);
  3815. kvmppc_gsbm_zero(&io->valids);
  3816. rc = kvmhv_nestedv2_parse_output(vcpu);
  3817. if (rc < 0)
  3818. return -EINVAL;
  3819. timer_rearm_host_dec(*tb);
  3820. /* Record context switch and guest_run_time data */
  3821. if (kvmhv_get_l2_counters_status())
  3822. do_trace_nested_cs_time(vcpu);
  3823. return trap;
  3824. }
  3825. /* call our hypervisor to load up HV regs and go */
  3826. static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb)
  3827. {
  3828. unsigned long host_psscr;
  3829. unsigned long msr;
  3830. struct hv_guest_state hvregs;
  3831. struct p9_host_os_sprs host_os_sprs;
  3832. s64 dec;
  3833. int trap;
  3834. msr = mfmsr();
  3835. save_p9_host_os_sprs(&host_os_sprs);
  3836. /*
  3837. * We need to save and restore the guest visible part of the
  3838. * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
  3839. * doesn't do this for us. Note only required if pseries since
  3840. * this is done in kvmhv_vcpu_entry_p9() below otherwise.
  3841. */
  3842. host_psscr = mfspr(SPRN_PSSCR_PR);
  3843. kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
  3844. if (lazy_irq_pending())
  3845. return 0;
  3846. if (unlikely(load_vcpu_state(vcpu, &host_os_sprs)))
  3847. msr = mfmsr(); /* TM restore can update msr */
  3848. if (vcpu->arch.psscr != host_psscr)
  3849. mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
  3850. kvmhv_save_hv_regs(vcpu, &hvregs);
  3851. hvregs.lpcr = lpcr;
  3852. hvregs.amor = ~0;
  3853. vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
  3854. hvregs.version = HV_GUEST_STATE_VERSION;
  3855. if (vcpu->arch.nested) {
  3856. hvregs.lpid = vcpu->arch.nested->shadow_lpid;
  3857. hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
  3858. } else {
  3859. hvregs.lpid = vcpu->kvm->arch.lpid;
  3860. hvregs.vcpu_token = vcpu->vcpu_id;
  3861. }
  3862. hvregs.hdec_expiry = time_limit;
  3863. /*
  3864. * hvregs has the doorbell status, so zero it here which
  3865. * enables us to receive doorbells when H_ENTER_NESTED is
  3866. * in progress for this vCPU
  3867. */
  3868. if (vcpu->arch.doorbell_request)
  3869. vcpu->arch.doorbell_request = 0;
  3870. /*
  3871. * When setting DEC, we must always deal with irq_work_raise
  3872. * via NMI vs setting DEC. The problem occurs right as we
  3873. * switch into guest mode if a NMI hits and sets pending work
  3874. * and sets DEC, then that will apply to the guest and not
  3875. * bring us back to the host.
  3876. *
  3877. * irq_work_raise could check a flag (or possibly LPCR[HDICE]
  3878. * for example) and set HDEC to 1? That wouldn't solve the
  3879. * nested hv case which needs to abort the hcall or zero the
  3880. * time limit.
  3881. *
  3882. * XXX: Another day's problem.
  3883. */
  3884. mtspr(SPRN_DEC, kvmppc_dec_expires_host_tb(vcpu) - *tb);
  3885. mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
  3886. mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
  3887. switch_pmu_to_guest(vcpu, &host_os_sprs);
  3888. accumulate_time(vcpu, &vcpu->arch.in_guest);
  3889. trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
  3890. __pa(&vcpu->arch.regs));
  3891. accumulate_time(vcpu, &vcpu->arch.guest_exit);
  3892. kvmhv_restore_hv_return_state(vcpu, &hvregs);
  3893. switch_pmu_to_host(vcpu, &host_os_sprs);
  3894. vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
  3895. vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
  3896. vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
  3897. vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
  3898. store_vcpu_state(vcpu);
  3899. dec = mfspr(SPRN_DEC);
  3900. if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
  3901. dec = (s32) dec;
  3902. *tb = mftb();
  3903. vcpu->arch.dec_expires = dec + (*tb + kvmppc_get_tb_offset(vcpu));
  3904. timer_rearm_host_dec(*tb);
  3905. restore_p9_host_os_sprs(vcpu, &host_os_sprs);
  3906. if (vcpu->arch.psscr != host_psscr)
  3907. mtspr(SPRN_PSSCR_PR, host_psscr);
  3908. return trap;
  3909. }
  3910. /*
  3911. * Guest entry for POWER9 and later CPUs.
  3912. */
  3913. static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
  3914. unsigned long lpcr, u64 *tb)
  3915. {
  3916. struct kvm *kvm = vcpu->kvm;
  3917. struct kvm_nested_guest *nested = vcpu->arch.nested;
  3918. u64 next_timer;
  3919. int trap;
  3920. next_timer = timer_get_next_tb();
  3921. if (*tb >= next_timer)
  3922. return BOOK3S_INTERRUPT_HV_DECREMENTER;
  3923. if (next_timer < time_limit)
  3924. time_limit = next_timer;
  3925. else if (*tb >= time_limit) /* nested time limit */
  3926. return BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER;
  3927. vcpu->arch.ceded = 0;
  3928. vcpu_vpa_increment_dispatch(vcpu);
  3929. if (kvmhv_on_pseries()) {
  3930. if (kvmhv_is_nestedv1())
  3931. trap = kvmhv_vcpu_entry_p9_nested(vcpu, time_limit, lpcr, tb);
  3932. else
  3933. trap = kvmhv_vcpu_entry_nestedv2(vcpu, time_limit, lpcr, tb);
  3934. /* H_CEDE has to be handled now, not later */
  3935. if (trap == BOOK3S_INTERRUPT_SYSCALL && !nested &&
  3936. kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
  3937. kvmppc_cede(vcpu);
  3938. kvmppc_set_gpr(vcpu, 3, 0);
  3939. trap = 0;
  3940. }
  3941. } else if (nested) {
  3942. __this_cpu_write(cpu_in_guest, kvm);
  3943. trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
  3944. __this_cpu_write(cpu_in_guest, NULL);
  3945. } else {
  3946. kvmppc_xive_push_vcpu(vcpu);
  3947. __this_cpu_write(cpu_in_guest, kvm);
  3948. trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
  3949. __this_cpu_write(cpu_in_guest, NULL);
  3950. if (trap == BOOK3S_INTERRUPT_SYSCALL &&
  3951. !(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
  3952. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  3953. /*
  3954. * XIVE rearm and XICS hcalls must be handled
  3955. * before xive context is pulled (is this
  3956. * true?)
  3957. */
  3958. if (req == H_CEDE) {
  3959. /* H_CEDE has to be handled now */
  3960. kvmppc_cede(vcpu);
  3961. if (!kvmppc_xive_rearm_escalation(vcpu)) {
  3962. /*
  3963. * Pending escalation so abort
  3964. * the cede.
  3965. */
  3966. vcpu->arch.ceded = 0;
  3967. }
  3968. kvmppc_set_gpr(vcpu, 3, 0);
  3969. trap = 0;
  3970. } else if (req == H_ENTER_NESTED) {
  3971. /*
  3972. * L2 should not run with the L1
  3973. * context so rearm and pull it.
  3974. */
  3975. if (!kvmppc_xive_rearm_escalation(vcpu)) {
  3976. /*
  3977. * Pending escalation so abort
  3978. * H_ENTER_NESTED.
  3979. */
  3980. kvmppc_set_gpr(vcpu, 3, 0);
  3981. trap = 0;
  3982. }
  3983. } else if (hcall_is_xics(req)) {
  3984. int ret;
  3985. ret = kvmppc_xive_xics_hcall(vcpu, req);
  3986. if (ret != H_TOO_HARD) {
  3987. kvmppc_set_gpr(vcpu, 3, ret);
  3988. trap = 0;
  3989. }
  3990. }
  3991. }
  3992. kvmppc_xive_pull_vcpu(vcpu);
  3993. if (kvm_is_radix(kvm))
  3994. vcpu->arch.slb_max = 0;
  3995. }
  3996. vcpu_vpa_increment_dispatch(vcpu);
  3997. return trap;
  3998. }
  3999. /*
  4000. * Wait for some other vcpu thread to execute us, and
  4001. * wake us up when we need to handle something in the host.
  4002. */
  4003. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  4004. struct kvm_vcpu *vcpu, int wait_state)
  4005. {
  4006. DEFINE_WAIT(wait);
  4007. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  4008. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  4009. spin_unlock(&vc->lock);
  4010. schedule();
  4011. spin_lock(&vc->lock);
  4012. }
  4013. finish_wait(&vcpu->arch.cpu_run, &wait);
  4014. }
  4015. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  4016. {
  4017. if (!halt_poll_ns_grow)
  4018. return;
  4019. vc->halt_poll_ns *= halt_poll_ns_grow;
  4020. if (vc->halt_poll_ns < halt_poll_ns_grow_start)
  4021. vc->halt_poll_ns = halt_poll_ns_grow_start;
  4022. }
  4023. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  4024. {
  4025. if (halt_poll_ns_shrink == 0)
  4026. vc->halt_poll_ns = 0;
  4027. else
  4028. vc->halt_poll_ns /= halt_poll_ns_shrink;
  4029. }
  4030. #ifdef CONFIG_KVM_XICS
  4031. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  4032. {
  4033. if (!xics_on_xive())
  4034. return false;
  4035. return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
  4036. vcpu->arch.xive_saved_state.cppr;
  4037. }
  4038. #else
  4039. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  4040. {
  4041. return false;
  4042. }
  4043. #endif /* CONFIG_KVM_XICS */
  4044. static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
  4045. {
  4046. if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
  4047. kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
  4048. return true;
  4049. return false;
  4050. }
  4051. static bool kvmppc_vcpu_check_block(struct kvm_vcpu *vcpu)
  4052. {
  4053. if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
  4054. return true;
  4055. return false;
  4056. }
  4057. /*
  4058. * Check to see if any of the runnable vcpus on the vcore have pending
  4059. * exceptions or are no longer ceded
  4060. */
  4061. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  4062. {
  4063. struct kvm_vcpu *vcpu;
  4064. int i;
  4065. for_each_runnable_thread(i, vcpu, vc) {
  4066. if (kvmppc_vcpu_check_block(vcpu))
  4067. return 1;
  4068. }
  4069. return 0;
  4070. }
  4071. /*
  4072. * All the vcpus in this vcore are idle, so wait for a decrementer
  4073. * or external interrupt to one of the vcpus. vc->lock is held.
  4074. */
  4075. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  4076. {
  4077. ktime_t cur, start_poll, start_wait;
  4078. int do_sleep = 1;
  4079. u64 block_ns;
  4080. WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
  4081. /* Poll for pending exceptions and ceded state */
  4082. cur = start_poll = ktime_get();
  4083. if (vc->halt_poll_ns) {
  4084. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  4085. ++vc->runner->stat.generic.halt_attempted_poll;
  4086. vc->vcore_state = VCORE_POLLING;
  4087. spin_unlock(&vc->lock);
  4088. do {
  4089. if (kvmppc_vcore_check_block(vc)) {
  4090. do_sleep = 0;
  4091. break;
  4092. }
  4093. cur = ktime_get();
  4094. } while (kvm_vcpu_can_poll(cur, stop));
  4095. spin_lock(&vc->lock);
  4096. vc->vcore_state = VCORE_INACTIVE;
  4097. if (!do_sleep) {
  4098. ++vc->runner->stat.generic.halt_successful_poll;
  4099. goto out;
  4100. }
  4101. }
  4102. prepare_to_rcuwait(&vc->wait);
  4103. set_current_state(TASK_INTERRUPTIBLE);
  4104. if (kvmppc_vcore_check_block(vc)) {
  4105. finish_rcuwait(&vc->wait);
  4106. do_sleep = 0;
  4107. /* If we polled, count this as a successful poll */
  4108. if (vc->halt_poll_ns)
  4109. ++vc->runner->stat.generic.halt_successful_poll;
  4110. goto out;
  4111. }
  4112. start_wait = ktime_get();
  4113. vc->vcore_state = VCORE_SLEEPING;
  4114. trace_kvmppc_vcore_blocked(vc->runner, 0);
  4115. spin_unlock(&vc->lock);
  4116. schedule();
  4117. finish_rcuwait(&vc->wait);
  4118. spin_lock(&vc->lock);
  4119. vc->vcore_state = VCORE_INACTIVE;
  4120. trace_kvmppc_vcore_blocked(vc->runner, 1);
  4121. ++vc->runner->stat.halt_successful_wait;
  4122. cur = ktime_get();
  4123. out:
  4124. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  4125. /* Attribute wait time */
  4126. if (do_sleep) {
  4127. vc->runner->stat.generic.halt_wait_ns +=
  4128. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  4129. KVM_STATS_LOG_HIST_UPDATE(
  4130. vc->runner->stat.generic.halt_wait_hist,
  4131. ktime_to_ns(cur) - ktime_to_ns(start_wait));
  4132. /* Attribute failed poll time */
  4133. if (vc->halt_poll_ns) {
  4134. vc->runner->stat.generic.halt_poll_fail_ns +=
  4135. ktime_to_ns(start_wait) -
  4136. ktime_to_ns(start_poll);
  4137. KVM_STATS_LOG_HIST_UPDATE(
  4138. vc->runner->stat.generic.halt_poll_fail_hist,
  4139. ktime_to_ns(start_wait) -
  4140. ktime_to_ns(start_poll));
  4141. }
  4142. } else {
  4143. /* Attribute successful poll time */
  4144. if (vc->halt_poll_ns) {
  4145. vc->runner->stat.generic.halt_poll_success_ns +=
  4146. ktime_to_ns(cur) -
  4147. ktime_to_ns(start_poll);
  4148. KVM_STATS_LOG_HIST_UPDATE(
  4149. vc->runner->stat.generic.halt_poll_success_hist,
  4150. ktime_to_ns(cur) - ktime_to_ns(start_poll));
  4151. }
  4152. }
  4153. /* Adjust poll time */
  4154. if (halt_poll_ns) {
  4155. if (block_ns <= vc->halt_poll_ns)
  4156. ;
  4157. /* We slept and blocked for longer than the max halt time */
  4158. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  4159. shrink_halt_poll_ns(vc);
  4160. /* We slept and our poll time is too small */
  4161. else if (vc->halt_poll_ns < halt_poll_ns &&
  4162. block_ns < halt_poll_ns)
  4163. grow_halt_poll_ns(vc);
  4164. if (vc->halt_poll_ns > halt_poll_ns)
  4165. vc->halt_poll_ns = halt_poll_ns;
  4166. } else
  4167. vc->halt_poll_ns = 0;
  4168. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  4169. }
  4170. /*
  4171. * This never fails for a radix guest, as none of the operations it does
  4172. * for a radix guest can fail or have a way to report failure.
  4173. */
  4174. static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
  4175. {
  4176. int r = 0;
  4177. struct kvm *kvm = vcpu->kvm;
  4178. mutex_lock(&kvm->arch.mmu_setup_lock);
  4179. if (!kvm->arch.mmu_ready) {
  4180. if (!kvm_is_radix(kvm))
  4181. r = kvmppc_hv_setup_htab_rma(vcpu);
  4182. if (!r) {
  4183. if (cpu_has_feature(CPU_FTR_ARCH_300))
  4184. kvmppc_setup_partition_table(kvm);
  4185. kvm->arch.mmu_ready = 1;
  4186. }
  4187. }
  4188. mutex_unlock(&kvm->arch.mmu_setup_lock);
  4189. return r;
  4190. }
  4191. static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
  4192. {
  4193. struct kvm_run *run = vcpu->run;
  4194. int n_ceded, i, r;
  4195. struct kvmppc_vcore *vc;
  4196. struct kvm_vcpu *v;
  4197. trace_kvmppc_run_vcpu_enter(vcpu);
  4198. run->exit_reason = 0;
  4199. vcpu->arch.ret = RESUME_GUEST;
  4200. vcpu->arch.trap = 0;
  4201. kvmppc_update_vpas(vcpu);
  4202. /*
  4203. * Synchronize with other threads in this virtual core
  4204. */
  4205. vc = vcpu->arch.vcore;
  4206. spin_lock(&vc->lock);
  4207. vcpu->arch.ceded = 0;
  4208. vcpu->arch.run_task = current;
  4209. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  4210. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  4211. vcpu->arch.busy_preempt = TB_NIL;
  4212. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  4213. ++vc->n_runnable;
  4214. /*
  4215. * This happens the first time this is called for a vcpu.
  4216. * If the vcore is already running, we may be able to start
  4217. * this thread straight away and have it join in.
  4218. */
  4219. if (!signal_pending(current)) {
  4220. if ((vc->vcore_state == VCORE_PIGGYBACK ||
  4221. vc->vcore_state == VCORE_RUNNING) &&
  4222. !VCORE_IS_EXITING(vc)) {
  4223. kvmppc_update_vpa_dispatch(vcpu, vc);
  4224. kvmppc_start_thread(vcpu, vc);
  4225. trace_kvm_guest_enter(vcpu);
  4226. } else if (vc->vcore_state == VCORE_SLEEPING) {
  4227. rcuwait_wake_up(&vc->wait);
  4228. }
  4229. }
  4230. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  4231. !signal_pending(current)) {
  4232. /* See if the MMU is ready to go */
  4233. if (!vcpu->kvm->arch.mmu_ready) {
  4234. spin_unlock(&vc->lock);
  4235. r = kvmhv_setup_mmu(vcpu);
  4236. spin_lock(&vc->lock);
  4237. if (r) {
  4238. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  4239. run->fail_entry.
  4240. hardware_entry_failure_reason = 0;
  4241. vcpu->arch.ret = r;
  4242. break;
  4243. }
  4244. }
  4245. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  4246. kvmppc_vcore_end_preempt(vc);
  4247. if (vc->vcore_state != VCORE_INACTIVE) {
  4248. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  4249. continue;
  4250. }
  4251. for_each_runnable_thread(i, v, vc) {
  4252. kvmppc_core_prepare_to_enter(v);
  4253. if (signal_pending(v->arch.run_task)) {
  4254. kvmppc_remove_runnable(vc, v, mftb());
  4255. v->stat.signal_exits++;
  4256. v->run->exit_reason = KVM_EXIT_INTR;
  4257. v->arch.ret = -EINTR;
  4258. wake_up(&v->arch.cpu_run);
  4259. }
  4260. }
  4261. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  4262. break;
  4263. n_ceded = 0;
  4264. for_each_runnable_thread(i, v, vc) {
  4265. if (!kvmppc_vcpu_woken(v))
  4266. n_ceded += v->arch.ceded;
  4267. else
  4268. v->arch.ceded = 0;
  4269. }
  4270. vc->runner = vcpu;
  4271. if (n_ceded == vc->n_runnable) {
  4272. kvmppc_vcore_blocked(vc);
  4273. } else if (need_resched()) {
  4274. kvmppc_vcore_preempt(vc);
  4275. /* Let something else run */
  4276. cond_resched_lock(&vc->lock);
  4277. if (vc->vcore_state == VCORE_PREEMPT)
  4278. kvmppc_vcore_end_preempt(vc);
  4279. } else {
  4280. kvmppc_run_core(vc);
  4281. }
  4282. vc->runner = NULL;
  4283. }
  4284. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  4285. (vc->vcore_state == VCORE_RUNNING ||
  4286. vc->vcore_state == VCORE_EXITING ||
  4287. vc->vcore_state == VCORE_PIGGYBACK))
  4288. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  4289. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  4290. kvmppc_vcore_end_preempt(vc);
  4291. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  4292. kvmppc_remove_runnable(vc, vcpu, mftb());
  4293. vcpu->stat.signal_exits++;
  4294. run->exit_reason = KVM_EXIT_INTR;
  4295. vcpu->arch.ret = -EINTR;
  4296. }
  4297. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  4298. /* Wake up some vcpu to run the core */
  4299. i = -1;
  4300. v = next_runnable_thread(vc, &i);
  4301. wake_up(&v->arch.cpu_run);
  4302. }
  4303. trace_kvmppc_run_vcpu_exit(vcpu);
  4304. spin_unlock(&vc->lock);
  4305. return vcpu->arch.ret;
  4306. }
  4307. int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
  4308. unsigned long lpcr)
  4309. {
  4310. struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
  4311. struct kvm_run *run = vcpu->run;
  4312. int trap, r, pcpu;
  4313. int srcu_idx;
  4314. struct kvmppc_vcore *vc;
  4315. struct kvm *kvm = vcpu->kvm;
  4316. struct kvm_nested_guest *nested = vcpu->arch.nested;
  4317. unsigned long flags;
  4318. u64 tb;
  4319. trace_kvmppc_run_vcpu_enter(vcpu);
  4320. run->exit_reason = 0;
  4321. vcpu->arch.ret = RESUME_GUEST;
  4322. vcpu->arch.trap = 0;
  4323. vc = vcpu->arch.vcore;
  4324. vcpu->arch.ceded = 0;
  4325. vcpu->arch.run_task = current;
  4326. vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
  4327. /* See if the MMU is ready to go */
  4328. if (unlikely(!kvm->arch.mmu_ready)) {
  4329. r = kvmhv_setup_mmu(vcpu);
  4330. if (r) {
  4331. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  4332. run->fail_entry.hardware_entry_failure_reason = 0;
  4333. vcpu->arch.ret = r;
  4334. return r;
  4335. }
  4336. }
  4337. if (need_resched())
  4338. cond_resched();
  4339. kvmppc_update_vpas(vcpu);
  4340. preempt_disable();
  4341. pcpu = smp_processor_id();
  4342. if (kvm_is_radix(kvm))
  4343. kvmppc_prepare_radix_vcpu(vcpu, pcpu);
  4344. /* flags save not required, but irq_pmu has no disable/enable API */
  4345. powerpc_local_irq_pmu_save(flags);
  4346. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  4347. if (signal_pending(current))
  4348. goto sigpend;
  4349. if (need_resched() || !kvm->arch.mmu_ready)
  4350. goto out;
  4351. vcpu->cpu = pcpu;
  4352. vcpu->arch.thread_cpu = pcpu;
  4353. vc->pcpu = pcpu;
  4354. local_paca->kvm_hstate.kvm_vcpu = vcpu;
  4355. local_paca->kvm_hstate.ptid = 0;
  4356. local_paca->kvm_hstate.fake_suspend = 0;
  4357. /*
  4358. * Orders set cpu/thread_cpu vs testing for pending interrupts and
  4359. * doorbells below. The other side is when these fields are set vs
  4360. * kvmppc_fast_vcpu_kick_hv reading the cpu/thread_cpu fields to
  4361. * kick a vCPU to notice the pending interrupt.
  4362. */
  4363. smp_mb();
  4364. if (!nested) {
  4365. kvmppc_core_prepare_to_enter(vcpu);
  4366. if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
  4367. &vcpu->arch.pending_exceptions) ||
  4368. xive_interrupt_pending(vcpu)) {
  4369. /*
  4370. * For nested HV, don't synthesize but always pass MER,
  4371. * the L0 will be able to optimise that more
  4372. * effectively than manipulating registers directly.
  4373. */
  4374. if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
  4375. kvmppc_inject_interrupt_hv(vcpu,
  4376. BOOK3S_INTERRUPT_EXTERNAL, 0);
  4377. else
  4378. lpcr |= LPCR_MER;
  4379. } else {
  4380. /*
  4381. * L1's copy of L2's LPCR (vcpu->arch.vcore->lpcr) can get its MER bit
  4382. * unexpectedly set - for e.g. during NMI handling when all register
  4383. * states are synchronized from L0 to L1. L1 needs to inform L0 about
  4384. * MER=1 only when there are pending external interrupts.
  4385. * In the above if check, MER bit is set if there are pending
  4386. * external interrupts. Hence, explicity mask off MER bit
  4387. * here as otherwise it may generate spurious interrupts in L2 KVM
  4388. * causing an endless loop, which results in L2 guest getting hung.
  4389. */
  4390. lpcr &= ~LPCR_MER;
  4391. }
  4392. } else if (vcpu->arch.pending_exceptions ||
  4393. xive_interrupt_pending(vcpu)) {
  4394. vcpu->arch.ret = RESUME_HOST;
  4395. goto out;
  4396. }
  4397. if (vcpu->arch.timer_running) {
  4398. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  4399. vcpu->arch.timer_running = 0;
  4400. }
  4401. tb = mftb();
  4402. kvmppc_update_vpa_dispatch_p9(vcpu, vc, tb + kvmppc_get_tb_offset(vcpu));
  4403. trace_kvm_guest_enter(vcpu);
  4404. guest_timing_enter_irqoff();
  4405. srcu_idx = srcu_read_lock(&kvm->srcu);
  4406. guest_state_enter_irqoff();
  4407. this_cpu_disable_ftrace();
  4408. trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr, &tb);
  4409. vcpu->arch.trap = trap;
  4410. this_cpu_enable_ftrace();
  4411. guest_state_exit_irqoff();
  4412. srcu_read_unlock(&kvm->srcu, srcu_idx);
  4413. set_irq_happened(trap);
  4414. vcpu->cpu = -1;
  4415. vcpu->arch.thread_cpu = -1;
  4416. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  4417. if (!vtime_accounting_enabled_this_cpu()) {
  4418. powerpc_local_irq_pmu_restore(flags);
  4419. /*
  4420. * Service IRQs here before guest_timing_exit_irqoff() so any
  4421. * ticks that occurred while running the guest are accounted to
  4422. * the guest. If vtime accounting is enabled, accounting uses
  4423. * TB rather than ticks, so it can be done without enabling
  4424. * interrupts here, which has the problem that it accounts
  4425. * interrupt processing overhead to the host.
  4426. */
  4427. powerpc_local_irq_pmu_save(flags);
  4428. }
  4429. guest_timing_exit_irqoff();
  4430. powerpc_local_irq_pmu_restore(flags);
  4431. preempt_enable();
  4432. /*
  4433. * cancel pending decrementer exception if DEC is now positive, or if
  4434. * entering a nested guest in which case the decrementer is now owned
  4435. * by L2 and the L1 decrementer is provided in hdec_expires
  4436. */
  4437. if (kvmppc_core_pending_dec(vcpu) &&
  4438. ((tb < kvmppc_dec_expires_host_tb(vcpu)) ||
  4439. (trap == BOOK3S_INTERRUPT_SYSCALL &&
  4440. kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
  4441. kvmppc_core_dequeue_dec(vcpu);
  4442. trace_kvm_guest_exit(vcpu);
  4443. r = RESUME_GUEST;
  4444. if (trap) {
  4445. if (!nested)
  4446. r = kvmppc_handle_exit_hv(vcpu, current);
  4447. else
  4448. r = kvmppc_handle_nested_exit(vcpu);
  4449. }
  4450. vcpu->arch.ret = r;
  4451. if (is_kvmppc_resume_guest(r) && !kvmppc_vcpu_check_block(vcpu)) {
  4452. kvmppc_set_timer(vcpu);
  4453. prepare_to_rcuwait(wait);
  4454. for (;;) {
  4455. set_current_state(TASK_INTERRUPTIBLE);
  4456. if (signal_pending(current)) {
  4457. vcpu->stat.signal_exits++;
  4458. run->exit_reason = KVM_EXIT_INTR;
  4459. vcpu->arch.ret = -EINTR;
  4460. break;
  4461. }
  4462. if (kvmppc_vcpu_check_block(vcpu))
  4463. break;
  4464. trace_kvmppc_vcore_blocked(vcpu, 0);
  4465. schedule();
  4466. trace_kvmppc_vcore_blocked(vcpu, 1);
  4467. }
  4468. finish_rcuwait(wait);
  4469. }
  4470. vcpu->arch.ceded = 0;
  4471. done:
  4472. trace_kvmppc_run_vcpu_exit(vcpu);
  4473. return vcpu->arch.ret;
  4474. sigpend:
  4475. vcpu->stat.signal_exits++;
  4476. run->exit_reason = KVM_EXIT_INTR;
  4477. vcpu->arch.ret = -EINTR;
  4478. out:
  4479. vcpu->cpu = -1;
  4480. vcpu->arch.thread_cpu = -1;
  4481. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  4482. powerpc_local_irq_pmu_restore(flags);
  4483. preempt_enable();
  4484. goto done;
  4485. }
  4486. static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
  4487. {
  4488. struct kvm_run *run = vcpu->run;
  4489. int r;
  4490. int srcu_idx;
  4491. struct kvm *kvm;
  4492. unsigned long msr;
  4493. start_timing(vcpu, &vcpu->arch.vcpu_entry);
  4494. if (!vcpu->arch.sane) {
  4495. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4496. return -EINVAL;
  4497. }
  4498. /* No need to go into the guest when all we'll do is come back out */
  4499. if (signal_pending(current)) {
  4500. run->exit_reason = KVM_EXIT_INTR;
  4501. return -EINTR;
  4502. }
  4503. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  4504. /*
  4505. * Don't allow entry with a suspended transaction, because
  4506. * the guest entry/exit code will lose it.
  4507. */
  4508. if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
  4509. (current->thread.regs->msr & MSR_TM)) {
  4510. if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
  4511. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  4512. run->fail_entry.hardware_entry_failure_reason = 0;
  4513. return -EINVAL;
  4514. }
  4515. }
  4516. #endif
  4517. /*
  4518. * Force online to 1 for the sake of old userspace which doesn't
  4519. * set it.
  4520. */
  4521. if (!vcpu->arch.online) {
  4522. atomic_inc(&vcpu->arch.vcore->online_count);
  4523. vcpu->arch.online = 1;
  4524. }
  4525. kvmppc_core_prepare_to_enter(vcpu);
  4526. kvm = vcpu->kvm;
  4527. atomic_inc(&kvm->arch.vcpus_running);
  4528. /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
  4529. smp_mb();
  4530. msr = 0;
  4531. if (IS_ENABLED(CONFIG_PPC_FPU))
  4532. msr |= MSR_FP;
  4533. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  4534. msr |= MSR_VEC;
  4535. if (cpu_has_feature(CPU_FTR_VSX))
  4536. msr |= MSR_VSX;
  4537. if ((cpu_has_feature(CPU_FTR_TM) ||
  4538. cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) &&
  4539. (kvmppc_get_hfscr_hv(vcpu) & HFSCR_TM))
  4540. msr |= MSR_TM;
  4541. msr = msr_check_and_set(msr);
  4542. kvmppc_save_user_regs();
  4543. kvmppc_save_current_sprs();
  4544. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  4545. vcpu->arch.waitp = &vcpu->arch.vcore->wait;
  4546. vcpu->arch.pgdir = kvm->mm->pgd;
  4547. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  4548. do {
  4549. accumulate_time(vcpu, &vcpu->arch.guest_entry);
  4550. if (cpu_has_feature(CPU_FTR_ARCH_300))
  4551. r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
  4552. vcpu->arch.vcore->lpcr);
  4553. else
  4554. r = kvmppc_run_vcpu(vcpu);
  4555. if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
  4556. accumulate_time(vcpu, &vcpu->arch.hcall);
  4557. if (!kvmhv_is_nestedv2() && WARN_ON_ONCE(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
  4558. /*
  4559. * These should have been caught reflected
  4560. * into the guest by now. Final sanity check:
  4561. * don't allow userspace to execute hcalls in
  4562. * the hypervisor.
  4563. */
  4564. r = RESUME_GUEST;
  4565. continue;
  4566. }
  4567. trace_kvm_hcall_enter(vcpu);
  4568. r = kvmppc_pseries_do_hcall(vcpu);
  4569. trace_kvm_hcall_exit(vcpu, r);
  4570. kvmppc_core_prepare_to_enter(vcpu);
  4571. } else if (r == RESUME_PAGE_FAULT) {
  4572. accumulate_time(vcpu, &vcpu->arch.pg_fault);
  4573. srcu_idx = srcu_read_lock(&kvm->srcu);
  4574. r = kvmppc_book3s_hv_page_fault(vcpu,
  4575. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  4576. srcu_read_unlock(&kvm->srcu, srcu_idx);
  4577. } else if (r == RESUME_PASSTHROUGH) {
  4578. if (WARN_ON(xics_on_xive()))
  4579. r = H_SUCCESS;
  4580. else
  4581. r = kvmppc_xics_rm_complete(vcpu, 0);
  4582. }
  4583. } while (is_kvmppc_resume_guest(r));
  4584. accumulate_time(vcpu, &vcpu->arch.vcpu_exit);
  4585. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  4586. atomic_dec(&kvm->arch.vcpus_running);
  4587. srr_regs_clobbered();
  4588. end_timing(vcpu);
  4589. return r;
  4590. }
  4591. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  4592. int shift, int sllp)
  4593. {
  4594. (*sps)->page_shift = shift;
  4595. (*sps)->slb_enc = sllp;
  4596. (*sps)->enc[0].page_shift = shift;
  4597. (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
  4598. /*
  4599. * Add 16MB MPSS support (may get filtered out by userspace)
  4600. */
  4601. if (shift != 24) {
  4602. int penc = kvmppc_pgsize_lp_encoding(shift, 24);
  4603. if (penc != -1) {
  4604. (*sps)->enc[1].page_shift = 24;
  4605. (*sps)->enc[1].pte_enc = penc;
  4606. }
  4607. }
  4608. (*sps)++;
  4609. }
  4610. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  4611. struct kvm_ppc_smmu_info *info)
  4612. {
  4613. struct kvm_ppc_one_seg_page_size *sps;
  4614. /*
  4615. * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
  4616. * POWER7 doesn't support keys for instruction accesses,
  4617. * POWER8 and POWER9 do.
  4618. */
  4619. info->data_keys = 32;
  4620. info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
  4621. /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
  4622. info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
  4623. info->slb_size = 32;
  4624. /* We only support these sizes for now, and no muti-size segments */
  4625. sps = &info->sps[0];
  4626. kvmppc_add_seg_page_size(&sps, 12, 0);
  4627. kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
  4628. kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
  4629. /* If running as a nested hypervisor, we don't support HPT guests */
  4630. if (kvmhv_on_pseries())
  4631. info->flags |= KVM_PPC_NO_HASH;
  4632. return 0;
  4633. }
  4634. /*
  4635. * Get (and clear) the dirty memory log for a memory slot.
  4636. */
  4637. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  4638. struct kvm_dirty_log *log)
  4639. {
  4640. struct kvm_memslots *slots;
  4641. struct kvm_memory_slot *memslot;
  4642. int r;
  4643. unsigned long n, i;
  4644. unsigned long *buf, *p;
  4645. struct kvm_vcpu *vcpu;
  4646. mutex_lock(&kvm->slots_lock);
  4647. r = -EINVAL;
  4648. if (log->slot >= KVM_USER_MEM_SLOTS)
  4649. goto out;
  4650. slots = kvm_memslots(kvm);
  4651. memslot = id_to_memslot(slots, log->slot);
  4652. r = -ENOENT;
  4653. if (!memslot || !memslot->dirty_bitmap)
  4654. goto out;
  4655. /*
  4656. * Use second half of bitmap area because both HPT and radix
  4657. * accumulate bits in the first half.
  4658. */
  4659. n = kvm_dirty_bitmap_bytes(memslot);
  4660. buf = memslot->dirty_bitmap + n / sizeof(long);
  4661. memset(buf, 0, n);
  4662. if (kvm_is_radix(kvm))
  4663. r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
  4664. else
  4665. r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
  4666. if (r)
  4667. goto out;
  4668. /*
  4669. * We accumulate dirty bits in the first half of the
  4670. * memslot's dirty_bitmap area, for when pages are paged
  4671. * out or modified by the host directly. Pick up these
  4672. * bits and add them to the map.
  4673. */
  4674. p = memslot->dirty_bitmap;
  4675. for (i = 0; i < n / sizeof(long); ++i)
  4676. buf[i] |= xchg(&p[i], 0);
  4677. /* Harvest dirty bits from VPA and DTL updates */
  4678. /* Note: we never modify the SLB shadow buffer areas */
  4679. kvm_for_each_vcpu(i, vcpu, kvm) {
  4680. spin_lock(&vcpu->arch.vpa_update_lock);
  4681. kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
  4682. kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
  4683. spin_unlock(&vcpu->arch.vpa_update_lock);
  4684. }
  4685. r = -EFAULT;
  4686. if (copy_to_user(log->dirty_bitmap, buf, n))
  4687. goto out;
  4688. r = 0;
  4689. out:
  4690. mutex_unlock(&kvm->slots_lock);
  4691. return r;
  4692. }
  4693. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
  4694. {
  4695. vfree(slot->arch.rmap);
  4696. slot->arch.rmap = NULL;
  4697. }
  4698. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  4699. const struct kvm_memory_slot *old,
  4700. struct kvm_memory_slot *new,
  4701. enum kvm_mr_change change)
  4702. {
  4703. if (change == KVM_MR_CREATE) {
  4704. unsigned long size = array_size(new->npages, sizeof(*new->arch.rmap));
  4705. if ((size >> PAGE_SHIFT) > totalram_pages())
  4706. return -ENOMEM;
  4707. new->arch.rmap = vzalloc(size);
  4708. if (!new->arch.rmap)
  4709. return -ENOMEM;
  4710. } else if (change != KVM_MR_DELETE) {
  4711. new->arch.rmap = old->arch.rmap;
  4712. }
  4713. return 0;
  4714. }
  4715. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  4716. struct kvm_memory_slot *old,
  4717. const struct kvm_memory_slot *new,
  4718. enum kvm_mr_change change)
  4719. {
  4720. /*
  4721. * If we are creating or modifying a memslot, it might make
  4722. * some address that was previously cached as emulated
  4723. * MMIO be no longer emulated MMIO, so invalidate
  4724. * all the caches of emulated MMIO translations.
  4725. */
  4726. if (change != KVM_MR_DELETE)
  4727. atomic64_inc(&kvm->arch.mmio_update);
  4728. /*
  4729. * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
  4730. * have already called kvm_arch_flush_shadow_memslot() to
  4731. * flush shadow mappings. For KVM_MR_CREATE we have no
  4732. * previous mappings. So the only case to handle is
  4733. * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
  4734. * has been changed.
  4735. * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
  4736. * to get rid of any THP PTEs in the partition-scoped page tables
  4737. * so we can track dirtiness at the page level; we flush when
  4738. * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
  4739. * using THP PTEs.
  4740. */
  4741. if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
  4742. ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
  4743. kvmppc_radix_flush_memslot(kvm, old);
  4744. /*
  4745. * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
  4746. */
  4747. if (!kvm->arch.secure_guest)
  4748. return;
  4749. switch (change) {
  4750. case KVM_MR_CREATE:
  4751. /*
  4752. * @TODO kvmppc_uvmem_memslot_create() can fail and
  4753. * return error. Fix this.
  4754. */
  4755. kvmppc_uvmem_memslot_create(kvm, new);
  4756. break;
  4757. case KVM_MR_DELETE:
  4758. kvmppc_uvmem_memslot_delete(kvm, old);
  4759. break;
  4760. default:
  4761. /* TODO: Handle KVM_MR_MOVE */
  4762. break;
  4763. }
  4764. }
  4765. /*
  4766. * Update LPCR values in kvm->arch and in vcores.
  4767. * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
  4768. * of kvm->arch.lpcr update).
  4769. */
  4770. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  4771. {
  4772. long int i;
  4773. u32 cores_done = 0;
  4774. if ((kvm->arch.lpcr & mask) == lpcr)
  4775. return;
  4776. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  4777. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  4778. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  4779. if (!vc)
  4780. continue;
  4781. spin_lock(&vc->lock);
  4782. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  4783. verify_lpcr(kvm, vc->lpcr);
  4784. spin_unlock(&vc->lock);
  4785. if (++cores_done >= kvm->arch.online_vcores)
  4786. break;
  4787. }
  4788. if (kvmhv_is_nestedv2()) {
  4789. struct kvm_vcpu *vcpu;
  4790. kvm_for_each_vcpu(i, vcpu, kvm) {
  4791. kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
  4792. }
  4793. }
  4794. }
  4795. void kvmppc_setup_partition_table(struct kvm *kvm)
  4796. {
  4797. unsigned long dw0, dw1;
  4798. if (!kvm_is_radix(kvm)) {
  4799. /* PS field - page size for VRMA */
  4800. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  4801. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  4802. /* HTABSIZE and HTABORG fields */
  4803. dw0 |= kvm->arch.sdr1;
  4804. /* Second dword as set by userspace */
  4805. dw1 = kvm->arch.process_table;
  4806. } else {
  4807. dw0 = PATB_HR | radix__get_tree_size() |
  4808. __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
  4809. dw1 = PATB_GR | kvm->arch.process_table;
  4810. }
  4811. kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
  4812. }
  4813. /*
  4814. * Set up HPT (hashed page table) and RMA (real-mode area).
  4815. * Must be called with kvm->arch.mmu_setup_lock held.
  4816. */
  4817. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  4818. {
  4819. int err = 0;
  4820. struct kvm *kvm = vcpu->kvm;
  4821. unsigned long hva;
  4822. struct kvm_memory_slot *memslot;
  4823. struct vm_area_struct *vma;
  4824. unsigned long lpcr = 0, senc;
  4825. unsigned long psize, porder;
  4826. int srcu_idx;
  4827. /* Allocate hashed page table (if not done already) and reset it */
  4828. if (!kvm->arch.hpt.virt) {
  4829. int order = KVM_DEFAULT_HPT_ORDER;
  4830. struct kvm_hpt_info info;
  4831. err = kvmppc_allocate_hpt(&info, order);
  4832. /* If we get here, it means userspace didn't specify a
  4833. * size explicitly. So, try successively smaller
  4834. * sizes if the default failed. */
  4835. while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
  4836. err = kvmppc_allocate_hpt(&info, order);
  4837. if (err < 0) {
  4838. pr_err("KVM: Couldn't alloc HPT\n");
  4839. goto out;
  4840. }
  4841. kvmppc_set_hpt(kvm, &info);
  4842. }
  4843. /* Look up the memslot for guest physical address 0 */
  4844. srcu_idx = srcu_read_lock(&kvm->srcu);
  4845. memslot = gfn_to_memslot(kvm, 0);
  4846. /* We must have some memory at 0 by now */
  4847. err = -EINVAL;
  4848. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  4849. goto out_srcu;
  4850. /* Look up the VMA for the start of this memory slot */
  4851. hva = memslot->userspace_addr;
  4852. mmap_read_lock(kvm->mm);
  4853. vma = vma_lookup(kvm->mm, hva);
  4854. if (!vma || (vma->vm_flags & VM_IO))
  4855. goto up_out;
  4856. psize = vma_kernel_pagesize(vma);
  4857. mmap_read_unlock(kvm->mm);
  4858. /* We can handle 4k, 64k or 16M pages in the VRMA */
  4859. if (psize >= 0x1000000)
  4860. psize = 0x1000000;
  4861. else if (psize >= 0x10000)
  4862. psize = 0x10000;
  4863. else
  4864. psize = 0x1000;
  4865. porder = __ilog2(psize);
  4866. senc = slb_pgsize_encoding(psize);
  4867. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  4868. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  4869. /* Create HPTEs in the hash page table for the VRMA */
  4870. kvmppc_map_vrma(vcpu, memslot, porder);
  4871. /* Update VRMASD field in the LPCR */
  4872. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  4873. /* the -4 is to account for senc values starting at 0x10 */
  4874. lpcr = senc << (LPCR_VRMASD_SH - 4);
  4875. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  4876. }
  4877. /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
  4878. smp_wmb();
  4879. err = 0;
  4880. out_srcu:
  4881. srcu_read_unlock(&kvm->srcu, srcu_idx);
  4882. out:
  4883. return err;
  4884. up_out:
  4885. mmap_read_unlock(kvm->mm);
  4886. goto out_srcu;
  4887. }
  4888. /*
  4889. * Must be called with kvm->arch.mmu_setup_lock held and
  4890. * mmu_ready = 0 and no vcpus running.
  4891. */
  4892. int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
  4893. {
  4894. unsigned long lpcr, lpcr_mask;
  4895. if (nesting_enabled(kvm))
  4896. kvmhv_release_all_nested(kvm);
  4897. kvmppc_rmap_reset(kvm);
  4898. kvm->arch.process_table = 0;
  4899. /* Mutual exclusion with kvm_unmap_gfn_range etc. */
  4900. spin_lock(&kvm->mmu_lock);
  4901. kvm->arch.radix = 0;
  4902. spin_unlock(&kvm->mmu_lock);
  4903. kvmppc_free_radix(kvm);
  4904. lpcr = LPCR_VPM1;
  4905. lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  4906. if (cpu_has_feature(CPU_FTR_ARCH_31))
  4907. lpcr_mask |= LPCR_HAIL;
  4908. kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
  4909. return 0;
  4910. }
  4911. /*
  4912. * Must be called with kvm->arch.mmu_setup_lock held and
  4913. * mmu_ready = 0 and no vcpus running.
  4914. */
  4915. int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
  4916. {
  4917. unsigned long lpcr, lpcr_mask;
  4918. int err;
  4919. err = kvmppc_init_vm_radix(kvm);
  4920. if (err)
  4921. return err;
  4922. kvmppc_rmap_reset(kvm);
  4923. /* Mutual exclusion with kvm_unmap_gfn_range etc. */
  4924. spin_lock(&kvm->mmu_lock);
  4925. kvm->arch.radix = 1;
  4926. spin_unlock(&kvm->mmu_lock);
  4927. kvmppc_free_hpt(&kvm->arch.hpt);
  4928. lpcr = LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  4929. lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  4930. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  4931. lpcr_mask |= LPCR_HAIL;
  4932. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  4933. (kvm->arch.host_lpcr & LPCR_HAIL))
  4934. lpcr |= LPCR_HAIL;
  4935. }
  4936. kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
  4937. return 0;
  4938. }
  4939. #ifdef CONFIG_KVM_XICS
  4940. /*
  4941. * Allocate a per-core structure for managing state about which cores are
  4942. * running in the host versus the guest and for exchanging data between
  4943. * real mode KVM and CPU running in the host.
  4944. * This is only done for the first VM.
  4945. * The allocated structure stays even if all VMs have stopped.
  4946. * It is only freed when the kvm-hv module is unloaded.
  4947. * It's OK for this routine to fail, we just don't support host
  4948. * core operations like redirecting H_IPI wakeups.
  4949. */
  4950. void kvmppc_alloc_host_rm_ops(void)
  4951. {
  4952. struct kvmppc_host_rm_ops *ops;
  4953. unsigned long l_ops;
  4954. int cpu, core;
  4955. int size;
  4956. if (cpu_has_feature(CPU_FTR_ARCH_300))
  4957. return;
  4958. /* Not the first time here ? */
  4959. if (kvmppc_host_rm_ops_hv != NULL)
  4960. return;
  4961. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  4962. if (!ops)
  4963. return;
  4964. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  4965. ops->rm_core = kzalloc(size, GFP_KERNEL);
  4966. if (!ops->rm_core) {
  4967. kfree(ops);
  4968. return;
  4969. }
  4970. cpus_read_lock();
  4971. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  4972. if (!cpu_online(cpu))
  4973. continue;
  4974. core = cpu >> threads_shift;
  4975. ops->rm_core[core].rm_state.in_host = 1;
  4976. }
  4977. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  4978. /*
  4979. * Make the contents of the kvmppc_host_rm_ops structure visible
  4980. * to other CPUs before we assign it to the global variable.
  4981. * Do an atomic assignment (no locks used here), but if someone
  4982. * beats us to it, just free our copy and return.
  4983. */
  4984. smp_wmb();
  4985. l_ops = (unsigned long) ops;
  4986. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  4987. cpus_read_unlock();
  4988. kfree(ops->rm_core);
  4989. kfree(ops);
  4990. return;
  4991. }
  4992. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  4993. "ppc/kvm_book3s:prepare",
  4994. kvmppc_set_host_core,
  4995. kvmppc_clear_host_core);
  4996. cpus_read_unlock();
  4997. }
  4998. void kvmppc_free_host_rm_ops(void)
  4999. {
  5000. if (kvmppc_host_rm_ops_hv) {
  5001. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  5002. kfree(kvmppc_host_rm_ops_hv->rm_core);
  5003. kfree(kvmppc_host_rm_ops_hv);
  5004. kvmppc_host_rm_ops_hv = NULL;
  5005. }
  5006. }
  5007. #endif
  5008. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  5009. {
  5010. unsigned long lpcr, lpid;
  5011. int ret;
  5012. mutex_init(&kvm->arch.uvmem_lock);
  5013. INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
  5014. mutex_init(&kvm->arch.mmu_setup_lock);
  5015. /* Allocate the guest's logical partition ID */
  5016. if (!kvmhv_is_nestedv2()) {
  5017. lpid = kvmppc_alloc_lpid();
  5018. if ((long)lpid < 0)
  5019. return -ENOMEM;
  5020. kvm->arch.lpid = lpid;
  5021. }
  5022. kvmppc_alloc_host_rm_ops();
  5023. kvmhv_vm_nested_init(kvm);
  5024. if (kvmhv_is_nestedv2()) {
  5025. long rc;
  5026. unsigned long guest_id;
  5027. rc = plpar_guest_create(0, &guest_id);
  5028. if (rc != H_SUCCESS)
  5029. pr_err("KVM: Create Guest hcall failed, rc=%ld\n", rc);
  5030. switch (rc) {
  5031. case H_PARAMETER:
  5032. case H_FUNCTION:
  5033. case H_STATE:
  5034. return -EINVAL;
  5035. case H_NOT_ENOUGH_RESOURCES:
  5036. case H_ABORTED:
  5037. return -ENOMEM;
  5038. case H_AUTHORITY:
  5039. return -EPERM;
  5040. case H_NOT_AVAILABLE:
  5041. return -EBUSY;
  5042. }
  5043. kvm->arch.lpid = guest_id;
  5044. }
  5045. /*
  5046. * Since we don't flush the TLB when tearing down a VM,
  5047. * and this lpid might have previously been used,
  5048. * make sure we flush on each core before running the new VM.
  5049. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  5050. * does this flush for us.
  5051. */
  5052. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5053. cpumask_setall(&kvm->arch.need_tlb_flush);
  5054. /* Start out with the default set of hcalls enabled */
  5055. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  5056. sizeof(kvm->arch.enabled_hcalls));
  5057. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5058. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  5059. /* Init LPCR for virtual RMA mode */
  5060. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  5061. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  5062. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  5063. lpcr &= LPCR_PECE | LPCR_LPES;
  5064. } else {
  5065. /*
  5066. * The L2 LPES mode will be set by the L0 according to whether
  5067. * or not it needs to take external interrupts in HV mode.
  5068. */
  5069. lpcr = 0;
  5070. }
  5071. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  5072. LPCR_VPM0 | LPCR_VPM1;
  5073. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  5074. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  5075. /* On POWER8 turn on online bit to enable PURR/SPURR */
  5076. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  5077. lpcr |= LPCR_ONL;
  5078. /*
  5079. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  5080. * Set HVICE bit to enable hypervisor virtualization interrupts.
  5081. * Set HEIC to prevent OS interrupts to go to hypervisor (should
  5082. * be unnecessary but better safe than sorry in case we re-enable
  5083. * EE in HV mode with this LPCR still set)
  5084. */
  5085. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  5086. lpcr &= ~LPCR_VPM0;
  5087. lpcr |= LPCR_HVICE | LPCR_HEIC;
  5088. /*
  5089. * If xive is enabled, we route 0x500 interrupts directly
  5090. * to the guest.
  5091. */
  5092. if (xics_on_xive())
  5093. lpcr |= LPCR_LPES;
  5094. }
  5095. /*
  5096. * If the host uses radix, the guest starts out as radix.
  5097. */
  5098. if (radix_enabled()) {
  5099. kvm->arch.radix = 1;
  5100. kvm->arch.mmu_ready = 1;
  5101. lpcr &= ~LPCR_VPM1;
  5102. lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  5103. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  5104. cpu_has_feature(CPU_FTR_ARCH_31) &&
  5105. (kvm->arch.host_lpcr & LPCR_HAIL))
  5106. lpcr |= LPCR_HAIL;
  5107. ret = kvmppc_init_vm_radix(kvm);
  5108. if (ret) {
  5109. if (kvmhv_is_nestedv2())
  5110. plpar_guest_delete(0, kvm->arch.lpid);
  5111. else
  5112. kvmppc_free_lpid(kvm->arch.lpid);
  5113. return ret;
  5114. }
  5115. kvmppc_setup_partition_table(kvm);
  5116. }
  5117. verify_lpcr(kvm, lpcr);
  5118. kvm->arch.lpcr = lpcr;
  5119. /* Initialization for future HPT resizes */
  5120. kvm->arch.resize_hpt = NULL;
  5121. /*
  5122. * Work out how many sets the TLB has, for the use of
  5123. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  5124. */
  5125. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  5126. /*
  5127. * P10 will flush all the congruence class with a single tlbiel
  5128. */
  5129. kvm->arch.tlb_sets = 1;
  5130. } else if (radix_enabled())
  5131. kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
  5132. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  5133. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  5134. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  5135. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  5136. else
  5137. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  5138. /*
  5139. * Track that we now have a HV mode VM active. This blocks secondary
  5140. * CPU threads from coming online.
  5141. */
  5142. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5143. kvm_hv_vm_activated();
  5144. /*
  5145. * Initialize smt_mode depending on processor.
  5146. * POWER8 and earlier have to use "strict" threading, where
  5147. * all vCPUs in a vcore have to run on the same (sub)core,
  5148. * whereas on POWER9 the threads can each run a different
  5149. * guest.
  5150. */
  5151. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5152. kvm->arch.smt_mode = threads_per_subcore;
  5153. else
  5154. kvm->arch.smt_mode = 1;
  5155. kvm->arch.emul_smt_mode = 1;
  5156. return 0;
  5157. }
  5158. static int kvmppc_arch_create_vm_debugfs_hv(struct kvm *kvm)
  5159. {
  5160. kvmppc_mmu_debugfs_init(kvm);
  5161. if (radix_enabled())
  5162. kvmhv_radix_debugfs_init(kvm);
  5163. return 0;
  5164. }
  5165. static void kvmppc_free_vcores(struct kvm *kvm)
  5166. {
  5167. long int i;
  5168. for (i = 0; i < KVM_MAX_VCORES; ++i)
  5169. kfree(kvm->arch.vcores[i]);
  5170. kvm->arch.online_vcores = 0;
  5171. }
  5172. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  5173. {
  5174. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5175. kvm_hv_vm_deactivated();
  5176. kvmppc_free_vcores(kvm);
  5177. if (kvm_is_radix(kvm))
  5178. kvmppc_free_radix(kvm);
  5179. else
  5180. kvmppc_free_hpt(&kvm->arch.hpt);
  5181. /* Perform global invalidation and return lpid to the pool */
  5182. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  5183. if (nesting_enabled(kvm))
  5184. kvmhv_release_all_nested(kvm);
  5185. kvm->arch.process_table = 0;
  5186. if (kvm->arch.secure_guest)
  5187. uv_svm_terminate(kvm->arch.lpid);
  5188. if (!kvmhv_is_nestedv2())
  5189. kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
  5190. }
  5191. if (kvmhv_is_nestedv2()) {
  5192. kvmhv_flush_lpid(kvm->arch.lpid);
  5193. plpar_guest_delete(0, kvm->arch.lpid);
  5194. } else {
  5195. kvmppc_free_lpid(kvm->arch.lpid);
  5196. }
  5197. kvmppc_free_pimap(kvm);
  5198. }
  5199. /* We don't need to emulate any privileged instructions or dcbz */
  5200. static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
  5201. unsigned int inst, int *advance)
  5202. {
  5203. return EMULATE_FAIL;
  5204. }
  5205. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  5206. ulong spr_val)
  5207. {
  5208. return EMULATE_FAIL;
  5209. }
  5210. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  5211. ulong *spr_val)
  5212. {
  5213. return EMULATE_FAIL;
  5214. }
  5215. static int kvmppc_core_check_processor_compat_hv(void)
  5216. {
  5217. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  5218. cpu_has_feature(CPU_FTR_ARCH_206))
  5219. return 0;
  5220. /* POWER9 in radix mode is capable of being a nested hypervisor. */
  5221. if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
  5222. return 0;
  5223. return -EIO;
  5224. }
  5225. #ifdef CONFIG_KVM_XICS
  5226. void kvmppc_free_pimap(struct kvm *kvm)
  5227. {
  5228. kfree(kvm->arch.pimap);
  5229. }
  5230. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  5231. {
  5232. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  5233. }
  5234. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  5235. {
  5236. struct irq_desc *desc;
  5237. struct kvmppc_irq_map *irq_map;
  5238. struct kvmppc_passthru_irqmap *pimap;
  5239. struct irq_chip *chip;
  5240. int i, rc = 0;
  5241. struct irq_data *host_data;
  5242. if (!kvm_irq_bypass)
  5243. return 1;
  5244. desc = irq_to_desc(host_irq);
  5245. if (!desc)
  5246. return -EIO;
  5247. mutex_lock(&kvm->lock);
  5248. pimap = kvm->arch.pimap;
  5249. if (pimap == NULL) {
  5250. /* First call, allocate structure to hold IRQ map */
  5251. pimap = kvmppc_alloc_pimap();
  5252. if (pimap == NULL) {
  5253. mutex_unlock(&kvm->lock);
  5254. return -ENOMEM;
  5255. }
  5256. kvm->arch.pimap = pimap;
  5257. }
  5258. /*
  5259. * For now, we only support interrupts for which the EOI operation
  5260. * is an OPAL call followed by a write to XIRR, since that's
  5261. * what our real-mode EOI code does, or a XIVE interrupt
  5262. */
  5263. chip = irq_data_get_irq_chip(&desc->irq_data);
  5264. if (!chip || !is_pnv_opal_msi(chip)) {
  5265. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  5266. host_irq, guest_gsi);
  5267. mutex_unlock(&kvm->lock);
  5268. return -ENOENT;
  5269. }
  5270. /*
  5271. * See if we already have an entry for this guest IRQ number.
  5272. * If it's mapped to a hardware IRQ number, that's an error,
  5273. * otherwise re-use this entry.
  5274. */
  5275. for (i = 0; i < pimap->n_mapped; i++) {
  5276. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  5277. if (pimap->mapped[i].r_hwirq) {
  5278. mutex_unlock(&kvm->lock);
  5279. return -EINVAL;
  5280. }
  5281. break;
  5282. }
  5283. }
  5284. if (i == KVMPPC_PIRQ_MAPPED) {
  5285. mutex_unlock(&kvm->lock);
  5286. return -EAGAIN; /* table is full */
  5287. }
  5288. irq_map = &pimap->mapped[i];
  5289. irq_map->v_hwirq = guest_gsi;
  5290. irq_map->desc = desc;
  5291. /*
  5292. * Order the above two stores before the next to serialize with
  5293. * the KVM real mode handler.
  5294. */
  5295. smp_wmb();
  5296. /*
  5297. * The 'host_irq' number is mapped in the PCI-MSI domain but
  5298. * the underlying calls, which will EOI the interrupt in real
  5299. * mode, need an HW IRQ number mapped in the XICS IRQ domain.
  5300. */
  5301. host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
  5302. irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
  5303. if (i == pimap->n_mapped)
  5304. pimap->n_mapped++;
  5305. if (xics_on_xive())
  5306. rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
  5307. else
  5308. kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
  5309. if (rc)
  5310. irq_map->r_hwirq = 0;
  5311. mutex_unlock(&kvm->lock);
  5312. return 0;
  5313. }
  5314. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  5315. {
  5316. struct irq_desc *desc;
  5317. struct kvmppc_passthru_irqmap *pimap;
  5318. int i, rc = 0;
  5319. if (!kvm_irq_bypass)
  5320. return 0;
  5321. desc = irq_to_desc(host_irq);
  5322. if (!desc)
  5323. return -EIO;
  5324. mutex_lock(&kvm->lock);
  5325. if (!kvm->arch.pimap)
  5326. goto unlock;
  5327. pimap = kvm->arch.pimap;
  5328. for (i = 0; i < pimap->n_mapped; i++) {
  5329. if (guest_gsi == pimap->mapped[i].v_hwirq)
  5330. break;
  5331. }
  5332. if (i == pimap->n_mapped) {
  5333. mutex_unlock(&kvm->lock);
  5334. return -ENODEV;
  5335. }
  5336. if (xics_on_xive())
  5337. rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
  5338. else
  5339. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  5340. /* invalidate the entry (what to do on error from the above ?) */
  5341. pimap->mapped[i].r_hwirq = 0;
  5342. /*
  5343. * We don't free this structure even when the count goes to
  5344. * zero. The structure is freed when we destroy the VM.
  5345. */
  5346. unlock:
  5347. mutex_unlock(&kvm->lock);
  5348. return rc;
  5349. }
  5350. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  5351. struct irq_bypass_producer *prod)
  5352. {
  5353. int ret = 0;
  5354. struct kvm_kernel_irqfd *irqfd =
  5355. container_of(cons, struct kvm_kernel_irqfd, consumer);
  5356. irqfd->producer = prod;
  5357. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  5358. if (ret)
  5359. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  5360. prod->irq, irqfd->gsi, ret);
  5361. return ret;
  5362. }
  5363. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  5364. struct irq_bypass_producer *prod)
  5365. {
  5366. int ret;
  5367. struct kvm_kernel_irqfd *irqfd =
  5368. container_of(cons, struct kvm_kernel_irqfd, consumer);
  5369. irqfd->producer = NULL;
  5370. /*
  5371. * When producer of consumer is unregistered, we change back to
  5372. * default external interrupt handling mode - KVM real mode
  5373. * will switch back to host.
  5374. */
  5375. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  5376. if (ret)
  5377. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  5378. prod->irq, irqfd->gsi, ret);
  5379. }
  5380. #endif
  5381. static int kvm_arch_vm_ioctl_hv(struct file *filp,
  5382. unsigned int ioctl, unsigned long arg)
  5383. {
  5384. struct kvm *kvm __maybe_unused = filp->private_data;
  5385. void __user *argp = (void __user *)arg;
  5386. int r;
  5387. switch (ioctl) {
  5388. case KVM_PPC_ALLOCATE_HTAB: {
  5389. u32 htab_order;
  5390. /* If we're a nested hypervisor, we currently only support radix */
  5391. if (kvmhv_on_pseries()) {
  5392. r = -EOPNOTSUPP;
  5393. break;
  5394. }
  5395. r = -EFAULT;
  5396. if (get_user(htab_order, (u32 __user *)argp))
  5397. break;
  5398. r = kvmppc_alloc_reset_hpt(kvm, htab_order);
  5399. if (r)
  5400. break;
  5401. r = 0;
  5402. break;
  5403. }
  5404. case KVM_PPC_GET_HTAB_FD: {
  5405. struct kvm_get_htab_fd ghf;
  5406. r = -EFAULT;
  5407. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  5408. break;
  5409. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  5410. break;
  5411. }
  5412. case KVM_PPC_RESIZE_HPT_PREPARE: {
  5413. struct kvm_ppc_resize_hpt rhpt;
  5414. r = -EFAULT;
  5415. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  5416. break;
  5417. r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
  5418. break;
  5419. }
  5420. case KVM_PPC_RESIZE_HPT_COMMIT: {
  5421. struct kvm_ppc_resize_hpt rhpt;
  5422. r = -EFAULT;
  5423. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  5424. break;
  5425. r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
  5426. break;
  5427. }
  5428. default:
  5429. r = -ENOTTY;
  5430. }
  5431. return r;
  5432. }
  5433. /*
  5434. * List of hcall numbers to enable by default.
  5435. * For compatibility with old userspace, we enable by default
  5436. * all hcalls that were implemented before the hcall-enabling
  5437. * facility was added. Note this list should not include H_RTAS.
  5438. */
  5439. static unsigned int default_hcall_list[] = {
  5440. H_REMOVE,
  5441. H_ENTER,
  5442. H_READ,
  5443. H_PROTECT,
  5444. H_BULK_REMOVE,
  5445. #ifdef CONFIG_SPAPR_TCE_IOMMU
  5446. H_GET_TCE,
  5447. H_PUT_TCE,
  5448. #endif
  5449. H_SET_DABR,
  5450. H_SET_XDABR,
  5451. H_CEDE,
  5452. H_PROD,
  5453. H_CONFER,
  5454. H_REGISTER_VPA,
  5455. #ifdef CONFIG_KVM_XICS
  5456. H_EOI,
  5457. H_CPPR,
  5458. H_IPI,
  5459. H_IPOLL,
  5460. H_XIRR,
  5461. H_XIRR_X,
  5462. #endif
  5463. 0
  5464. };
  5465. static void init_default_hcalls(void)
  5466. {
  5467. int i;
  5468. unsigned int hcall;
  5469. for (i = 0; default_hcall_list[i]; ++i) {
  5470. hcall = default_hcall_list[i];
  5471. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  5472. __set_bit(hcall / 4, default_enabled_hcalls);
  5473. }
  5474. }
  5475. static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
  5476. {
  5477. unsigned long lpcr;
  5478. int radix;
  5479. int err;
  5480. /* If not on a POWER9, reject it */
  5481. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5482. return -ENODEV;
  5483. /* If any unknown flags set, reject it */
  5484. if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
  5485. return -EINVAL;
  5486. /* GR (guest radix) bit in process_table field must match */
  5487. radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
  5488. if (!!(cfg->process_table & PATB_GR) != radix)
  5489. return -EINVAL;
  5490. /* Process table size field must be reasonable, i.e. <= 24 */
  5491. if ((cfg->process_table & PRTS_MASK) > 24)
  5492. return -EINVAL;
  5493. /* We can change a guest to/from radix now, if the host is radix */
  5494. if (radix && !radix_enabled())
  5495. return -EINVAL;
  5496. /* If we're a nested hypervisor, we currently only support radix */
  5497. if (kvmhv_on_pseries() && !radix)
  5498. return -EINVAL;
  5499. mutex_lock(&kvm->arch.mmu_setup_lock);
  5500. if (radix != kvm_is_radix(kvm)) {
  5501. if (kvm->arch.mmu_ready) {
  5502. kvm->arch.mmu_ready = 0;
  5503. /* order mmu_ready vs. vcpus_running */
  5504. smp_mb();
  5505. if (atomic_read(&kvm->arch.vcpus_running)) {
  5506. kvm->arch.mmu_ready = 1;
  5507. err = -EBUSY;
  5508. goto out_unlock;
  5509. }
  5510. }
  5511. if (radix)
  5512. err = kvmppc_switch_mmu_to_radix(kvm);
  5513. else
  5514. err = kvmppc_switch_mmu_to_hpt(kvm);
  5515. if (err)
  5516. goto out_unlock;
  5517. }
  5518. kvm->arch.process_table = cfg->process_table;
  5519. kvmppc_setup_partition_table(kvm);
  5520. lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
  5521. kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
  5522. err = 0;
  5523. out_unlock:
  5524. mutex_unlock(&kvm->arch.mmu_setup_lock);
  5525. return err;
  5526. }
  5527. static int kvmhv_enable_nested(struct kvm *kvm)
  5528. {
  5529. if (!nested)
  5530. return -EPERM;
  5531. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5532. return -ENODEV;
  5533. if (!radix_enabled())
  5534. return -ENODEV;
  5535. if (kvmhv_is_nestedv2())
  5536. return -ENODEV;
  5537. /* kvm == NULL means the caller is testing if the capability exists */
  5538. if (kvm)
  5539. kvm->arch.nested_enable = true;
  5540. return 0;
  5541. }
  5542. static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
  5543. int size)
  5544. {
  5545. int rc = -EINVAL;
  5546. if (kvmhv_vcpu_is_radix(vcpu)) {
  5547. rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
  5548. if (rc > 0)
  5549. rc = -EINVAL;
  5550. }
  5551. /* For now quadrants are the only way to access nested guest memory */
  5552. if (rc && vcpu->arch.nested)
  5553. rc = -EAGAIN;
  5554. return rc;
  5555. }
  5556. static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
  5557. int size)
  5558. {
  5559. int rc = -EINVAL;
  5560. if (kvmhv_vcpu_is_radix(vcpu)) {
  5561. rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
  5562. if (rc > 0)
  5563. rc = -EINVAL;
  5564. }
  5565. /* For now quadrants are the only way to access nested guest memory */
  5566. if (rc && vcpu->arch.nested)
  5567. rc = -EAGAIN;
  5568. return rc;
  5569. }
  5570. static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
  5571. {
  5572. unpin_vpa(kvm, vpa);
  5573. vpa->gpa = 0;
  5574. vpa->pinned_addr = NULL;
  5575. vpa->dirty = false;
  5576. vpa->update_pending = 0;
  5577. }
  5578. /*
  5579. * Enable a guest to become a secure VM, or test whether
  5580. * that could be enabled.
  5581. * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
  5582. * tested (kvm == NULL) or enabled (kvm != NULL).
  5583. */
  5584. static int kvmhv_enable_svm(struct kvm *kvm)
  5585. {
  5586. if (!kvmppc_uvmem_available())
  5587. return -EINVAL;
  5588. if (kvm)
  5589. kvm->arch.svm_enabled = 1;
  5590. return 0;
  5591. }
  5592. /*
  5593. * IOCTL handler to turn off secure mode of guest
  5594. *
  5595. * - Release all device pages
  5596. * - Issue ucall to terminate the guest on the UV side
  5597. * - Unpin the VPA pages.
  5598. * - Reinit the partition scoped page tables
  5599. */
  5600. static int kvmhv_svm_off(struct kvm *kvm)
  5601. {
  5602. struct kvm_vcpu *vcpu;
  5603. int mmu_was_ready;
  5604. int srcu_idx;
  5605. int ret = 0;
  5606. unsigned long i;
  5607. if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
  5608. return ret;
  5609. mutex_lock(&kvm->arch.mmu_setup_lock);
  5610. mmu_was_ready = kvm->arch.mmu_ready;
  5611. if (kvm->arch.mmu_ready) {
  5612. kvm->arch.mmu_ready = 0;
  5613. /* order mmu_ready vs. vcpus_running */
  5614. smp_mb();
  5615. if (atomic_read(&kvm->arch.vcpus_running)) {
  5616. kvm->arch.mmu_ready = 1;
  5617. ret = -EBUSY;
  5618. goto out;
  5619. }
  5620. }
  5621. srcu_idx = srcu_read_lock(&kvm->srcu);
  5622. for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
  5623. struct kvm_memory_slot *memslot;
  5624. struct kvm_memslots *slots = __kvm_memslots(kvm, i);
  5625. int bkt;
  5626. if (!slots)
  5627. continue;
  5628. kvm_for_each_memslot(memslot, bkt, slots) {
  5629. kvmppc_uvmem_drop_pages(memslot, kvm, true);
  5630. uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
  5631. }
  5632. }
  5633. srcu_read_unlock(&kvm->srcu, srcu_idx);
  5634. ret = uv_svm_terminate(kvm->arch.lpid);
  5635. if (ret != U_SUCCESS) {
  5636. ret = -EINVAL;
  5637. goto out;
  5638. }
  5639. /*
  5640. * When secure guest is reset, all the guest pages are sent
  5641. * to UV via UV_PAGE_IN before the non-boot vcpus get a
  5642. * chance to run and unpin their VPA pages. Unpinning of all
  5643. * VPA pages is done here explicitly so that VPA pages
  5644. * can be migrated to the secure side.
  5645. *
  5646. * This is required to for the secure SMP guest to reboot
  5647. * correctly.
  5648. */
  5649. kvm_for_each_vcpu(i, vcpu, kvm) {
  5650. spin_lock(&vcpu->arch.vpa_update_lock);
  5651. unpin_vpa_reset(kvm, &vcpu->arch.dtl);
  5652. unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
  5653. unpin_vpa_reset(kvm, &vcpu->arch.vpa);
  5654. spin_unlock(&vcpu->arch.vpa_update_lock);
  5655. }
  5656. kvmppc_setup_partition_table(kvm);
  5657. kvm->arch.secure_guest = 0;
  5658. kvm->arch.mmu_ready = mmu_was_ready;
  5659. out:
  5660. mutex_unlock(&kvm->arch.mmu_setup_lock);
  5661. return ret;
  5662. }
  5663. static int kvmhv_enable_dawr1(struct kvm *kvm)
  5664. {
  5665. if (!cpu_has_feature(CPU_FTR_DAWR1))
  5666. return -ENODEV;
  5667. /* kvm == NULL means the caller is testing if the capability exists */
  5668. if (kvm)
  5669. kvm->arch.dawr1_enabled = true;
  5670. return 0;
  5671. }
  5672. static bool kvmppc_hash_v3_possible(void)
  5673. {
  5674. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  5675. return false;
  5676. if (!cpu_has_feature(CPU_FTR_HVMODE))
  5677. return false;
  5678. /*
  5679. * POWER9 chips before version 2.02 can't have some threads in
  5680. * HPT mode and some in radix mode on the same core.
  5681. */
  5682. if (radix_enabled()) {
  5683. unsigned int pvr = mfspr(SPRN_PVR);
  5684. if ((pvr >> 16) == PVR_POWER9 &&
  5685. (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
  5686. ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
  5687. return false;
  5688. }
  5689. return true;
  5690. }
  5691. static struct kvmppc_ops kvm_ops_hv = {
  5692. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  5693. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  5694. .get_one_reg = kvmppc_get_one_reg_hv,
  5695. .set_one_reg = kvmppc_set_one_reg_hv,
  5696. .vcpu_load = kvmppc_core_vcpu_load_hv,
  5697. .vcpu_put = kvmppc_core_vcpu_put_hv,
  5698. .inject_interrupt = kvmppc_inject_interrupt_hv,
  5699. .set_msr = kvmppc_set_msr_hv,
  5700. .vcpu_run = kvmppc_vcpu_run_hv,
  5701. .vcpu_create = kvmppc_core_vcpu_create_hv,
  5702. .vcpu_free = kvmppc_core_vcpu_free_hv,
  5703. .check_requests = kvmppc_core_check_requests_hv,
  5704. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  5705. .flush_memslot = kvmppc_core_flush_memslot_hv,
  5706. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  5707. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  5708. .unmap_gfn_range = kvm_unmap_gfn_range_hv,
  5709. .age_gfn = kvm_age_gfn_hv,
  5710. .test_age_gfn = kvm_test_age_gfn_hv,
  5711. .free_memslot = kvmppc_core_free_memslot_hv,
  5712. .init_vm = kvmppc_core_init_vm_hv,
  5713. .destroy_vm = kvmppc_core_destroy_vm_hv,
  5714. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  5715. .emulate_op = kvmppc_core_emulate_op_hv,
  5716. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  5717. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  5718. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  5719. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  5720. .hcall_implemented = kvmppc_hcall_impl_hv,
  5721. #ifdef CONFIG_KVM_XICS
  5722. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  5723. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  5724. #endif
  5725. .configure_mmu = kvmhv_configure_mmu,
  5726. .get_rmmu_info = kvmhv_get_rmmu_info,
  5727. .set_smt_mode = kvmhv_set_smt_mode,
  5728. .enable_nested = kvmhv_enable_nested,
  5729. .load_from_eaddr = kvmhv_load_from_eaddr,
  5730. .store_to_eaddr = kvmhv_store_to_eaddr,
  5731. .enable_svm = kvmhv_enable_svm,
  5732. .svm_off = kvmhv_svm_off,
  5733. .enable_dawr1 = kvmhv_enable_dawr1,
  5734. .hash_v3_possible = kvmppc_hash_v3_possible,
  5735. .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv,
  5736. .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv,
  5737. };
  5738. static int kvm_init_subcore_bitmap(void)
  5739. {
  5740. int i, j;
  5741. int nr_cores = cpu_nr_cores();
  5742. struct sibling_subcore_state *sibling_subcore_state;
  5743. for (i = 0; i < nr_cores; i++) {
  5744. int first_cpu = i * threads_per_core;
  5745. int node = cpu_to_node(first_cpu);
  5746. /* Ignore if it is already allocated. */
  5747. if (paca_ptrs[first_cpu]->sibling_subcore_state)
  5748. continue;
  5749. sibling_subcore_state =
  5750. kzalloc_node(sizeof(struct sibling_subcore_state),
  5751. GFP_KERNEL, node);
  5752. if (!sibling_subcore_state)
  5753. return -ENOMEM;
  5754. for (j = 0; j < threads_per_core; j++) {
  5755. int cpu = first_cpu + j;
  5756. paca_ptrs[cpu]->sibling_subcore_state =
  5757. sibling_subcore_state;
  5758. }
  5759. }
  5760. return 0;
  5761. }
  5762. static int kvmppc_radix_possible(void)
  5763. {
  5764. return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
  5765. }
  5766. static int kvmppc_book3s_init_hv(void)
  5767. {
  5768. int r;
  5769. if (!tlbie_capable) {
  5770. pr_err("KVM-HV: Host does not support TLBIE\n");
  5771. return -ENODEV;
  5772. }
  5773. /*
  5774. * FIXME!! Do we need to check on all cpus ?
  5775. */
  5776. r = kvmppc_core_check_processor_compat_hv();
  5777. if (r < 0)
  5778. return -ENODEV;
  5779. r = kvmhv_nested_init();
  5780. if (r)
  5781. return r;
  5782. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  5783. r = kvm_init_subcore_bitmap();
  5784. if (r)
  5785. goto err;
  5786. }
  5787. /*
  5788. * We need a way of accessing the XICS interrupt controller,
  5789. * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
  5790. * indirectly, via OPAL.
  5791. */
  5792. #ifdef CONFIG_SMP
  5793. if (!xics_on_xive() && !kvmhv_on_pseries() &&
  5794. !local_paca->kvm_hstate.xics_phys) {
  5795. struct device_node *np;
  5796. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  5797. if (!np) {
  5798. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  5799. r = -ENODEV;
  5800. goto err;
  5801. }
  5802. /* presence of intc confirmed - node can be dropped again */
  5803. of_node_put(np);
  5804. }
  5805. #endif
  5806. init_default_hcalls();
  5807. init_vcore_lists();
  5808. r = kvmppc_mmu_hv_init();
  5809. if (r)
  5810. goto err;
  5811. if (kvmppc_radix_possible()) {
  5812. r = kvmppc_radix_init();
  5813. if (r)
  5814. goto err;
  5815. }
  5816. r = kvmppc_uvmem_init();
  5817. if (r < 0) {
  5818. pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
  5819. return r;
  5820. }
  5821. kvm_ops_hv.owner = THIS_MODULE;
  5822. kvmppc_hv_ops = &kvm_ops_hv;
  5823. return 0;
  5824. err:
  5825. kvmhv_nested_exit();
  5826. kvmppc_radix_exit();
  5827. return r;
  5828. }
  5829. static void kvmppc_book3s_exit_hv(void)
  5830. {
  5831. kvmppc_uvmem_free();
  5832. kvmppc_free_host_rm_ops();
  5833. if (kvmppc_radix_possible())
  5834. kvmppc_radix_exit();
  5835. kvmppc_hv_ops = NULL;
  5836. kvmhv_nested_exit();
  5837. }
  5838. module_init(kvmppc_book3s_init_hv);
  5839. module_exit(kvmppc_book3s_exit_hv);
  5840. MODULE_DESCRIPTION("KVM on Book3S (POWER8 and later) in hypervisor mode");
  5841. MODULE_LICENSE("GPL");
  5842. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  5843. MODULE_ALIAS("devname:kvm");