book3s_hv.c 162 KB

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