kvm-s390.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * hosting IBM Z kernel virtual machines (s390x)
  4. *
  5. * Copyright IBM Corp. 2008, 2020
  6. *
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Christian Borntraeger <borntraeger@de.ibm.com>
  9. * Christian Ehrhardt <ehrhardt@de.ibm.com>
  10. * Jason J. Herne <jjherne@us.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "kvm-s390"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/compiler.h>
  15. #include <linux/err.h>
  16. #include <linux/fs.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/init.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/mman.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/random.h>
  25. #include <linux/slab.h>
  26. #include <linux/timer.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/bitmap.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/string.h>
  31. #include <linux/pgtable.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <asm/access-regs.h>
  34. #include <asm/asm-offsets.h>
  35. #include <asm/lowcore.h>
  36. #include <asm/stp.h>
  37. #include <asm/gmap.h>
  38. #include <asm/nmi.h>
  39. #include <asm/isc.h>
  40. #include <asm/sclp.h>
  41. #include <asm/cpacf.h>
  42. #include <asm/timex.h>
  43. #include <asm/fpu.h>
  44. #include <asm/ap.h>
  45. #include <asm/uv.h>
  46. #include "kvm-s390.h"
  47. #include "gaccess.h"
  48. #include "pci.h"
  49. #define CREATE_TRACE_POINTS
  50. #include "trace.h"
  51. #include "trace-s390.h"
  52. #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
  53. #define LOCAL_IRQS 32
  54. #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
  55. (KVM_MAX_VCPUS + LOCAL_IRQS))
  56. const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
  57. KVM_GENERIC_VM_STATS(),
  58. STATS_DESC_COUNTER(VM, inject_io),
  59. STATS_DESC_COUNTER(VM, inject_float_mchk),
  60. STATS_DESC_COUNTER(VM, inject_pfault_done),
  61. STATS_DESC_COUNTER(VM, inject_service_signal),
  62. STATS_DESC_COUNTER(VM, inject_virtio),
  63. STATS_DESC_COUNTER(VM, aen_forward),
  64. STATS_DESC_COUNTER(VM, gmap_shadow_reuse),
  65. STATS_DESC_COUNTER(VM, gmap_shadow_create),
  66. STATS_DESC_COUNTER(VM, gmap_shadow_r1_entry),
  67. STATS_DESC_COUNTER(VM, gmap_shadow_r2_entry),
  68. STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
  69. STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
  70. STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
  71. };
  72. const struct kvm_stats_header kvm_vm_stats_header = {
  73. .name_size = KVM_STATS_NAME_SIZE,
  74. .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
  75. .id_offset = sizeof(struct kvm_stats_header),
  76. .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
  77. .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
  78. sizeof(kvm_vm_stats_desc),
  79. };
  80. const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
  81. KVM_GENERIC_VCPU_STATS(),
  82. STATS_DESC_COUNTER(VCPU, exit_userspace),
  83. STATS_DESC_COUNTER(VCPU, exit_null),
  84. STATS_DESC_COUNTER(VCPU, exit_external_request),
  85. STATS_DESC_COUNTER(VCPU, exit_io_request),
  86. STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
  87. STATS_DESC_COUNTER(VCPU, exit_stop_request),
  88. STATS_DESC_COUNTER(VCPU, exit_validity),
  89. STATS_DESC_COUNTER(VCPU, exit_instruction),
  90. STATS_DESC_COUNTER(VCPU, exit_pei),
  91. STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
  92. STATS_DESC_COUNTER(VCPU, instruction_lctl),
  93. STATS_DESC_COUNTER(VCPU, instruction_lctlg),
  94. STATS_DESC_COUNTER(VCPU, instruction_stctl),
  95. STATS_DESC_COUNTER(VCPU, instruction_stctg),
  96. STATS_DESC_COUNTER(VCPU, exit_program_interruption),
  97. STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
  98. STATS_DESC_COUNTER(VCPU, exit_operation_exception),
  99. STATS_DESC_COUNTER(VCPU, deliver_ckc),
  100. STATS_DESC_COUNTER(VCPU, deliver_cputm),
  101. STATS_DESC_COUNTER(VCPU, deliver_external_call),
  102. STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
  103. STATS_DESC_COUNTER(VCPU, deliver_service_signal),
  104. STATS_DESC_COUNTER(VCPU, deliver_virtio),
  105. STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
  106. STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
  107. STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
  108. STATS_DESC_COUNTER(VCPU, deliver_program),
  109. STATS_DESC_COUNTER(VCPU, deliver_io),
  110. STATS_DESC_COUNTER(VCPU, deliver_machine_check),
  111. STATS_DESC_COUNTER(VCPU, exit_wait_state),
  112. STATS_DESC_COUNTER(VCPU, inject_ckc),
  113. STATS_DESC_COUNTER(VCPU, inject_cputm),
  114. STATS_DESC_COUNTER(VCPU, inject_external_call),
  115. STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
  116. STATS_DESC_COUNTER(VCPU, inject_mchk),
  117. STATS_DESC_COUNTER(VCPU, inject_pfault_init),
  118. STATS_DESC_COUNTER(VCPU, inject_program),
  119. STATS_DESC_COUNTER(VCPU, inject_restart),
  120. STATS_DESC_COUNTER(VCPU, inject_set_prefix),
  121. STATS_DESC_COUNTER(VCPU, inject_stop_signal),
  122. STATS_DESC_COUNTER(VCPU, instruction_epsw),
  123. STATS_DESC_COUNTER(VCPU, instruction_gs),
  124. STATS_DESC_COUNTER(VCPU, instruction_io_other),
  125. STATS_DESC_COUNTER(VCPU, instruction_lpsw),
  126. STATS_DESC_COUNTER(VCPU, instruction_lpswe),
  127. STATS_DESC_COUNTER(VCPU, instruction_lpswey),
  128. STATS_DESC_COUNTER(VCPU, instruction_pfmf),
  129. STATS_DESC_COUNTER(VCPU, instruction_ptff),
  130. STATS_DESC_COUNTER(VCPU, instruction_sck),
  131. STATS_DESC_COUNTER(VCPU, instruction_sckpf),
  132. STATS_DESC_COUNTER(VCPU, instruction_stidp),
  133. STATS_DESC_COUNTER(VCPU, instruction_spx),
  134. STATS_DESC_COUNTER(VCPU, instruction_stpx),
  135. STATS_DESC_COUNTER(VCPU, instruction_stap),
  136. STATS_DESC_COUNTER(VCPU, instruction_iske),
  137. STATS_DESC_COUNTER(VCPU, instruction_ri),
  138. STATS_DESC_COUNTER(VCPU, instruction_rrbe),
  139. STATS_DESC_COUNTER(VCPU, instruction_sske),
  140. STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
  141. STATS_DESC_COUNTER(VCPU, instruction_stsi),
  142. STATS_DESC_COUNTER(VCPU, instruction_stfl),
  143. STATS_DESC_COUNTER(VCPU, instruction_tb),
  144. STATS_DESC_COUNTER(VCPU, instruction_tpi),
  145. STATS_DESC_COUNTER(VCPU, instruction_tprot),
  146. STATS_DESC_COUNTER(VCPU, instruction_tsch),
  147. STATS_DESC_COUNTER(VCPU, instruction_sie),
  148. STATS_DESC_COUNTER(VCPU, instruction_essa),
  149. STATS_DESC_COUNTER(VCPU, instruction_sthyi),
  150. STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
  151. STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
  152. STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
  153. STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
  154. STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
  155. STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
  156. STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
  157. STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
  158. STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
  159. STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
  160. STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
  161. STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
  162. STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
  163. STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
  164. STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
  165. STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
  166. STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
  167. STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
  168. STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
  169. STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
  170. STATS_DESC_COUNTER(VCPU, diag_9c_forward),
  171. STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
  172. STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
  173. STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
  174. STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
  175. STATS_DESC_COUNTER(VCPU, pfault_sync)
  176. };
  177. const struct kvm_stats_header kvm_vcpu_stats_header = {
  178. .name_size = KVM_STATS_NAME_SIZE,
  179. .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
  180. .id_offset = sizeof(struct kvm_stats_header),
  181. .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
  182. .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
  183. sizeof(kvm_vcpu_stats_desc),
  184. };
  185. /* allow nested virtualization in KVM (if enabled by user space) */
  186. static int nested;
  187. module_param(nested, int, S_IRUGO);
  188. MODULE_PARM_DESC(nested, "Nested virtualization support");
  189. /* allow 1m huge page guest backing, if !nested */
  190. static int hpage;
  191. module_param(hpage, int, 0444);
  192. MODULE_PARM_DESC(hpage, "1m huge page backing support");
  193. /* maximum percentage of steal time for polling. >100 is treated like 100 */
  194. static u8 halt_poll_max_steal = 10;
  195. module_param(halt_poll_max_steal, byte, 0644);
  196. MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
  197. /* if set to true, the GISA will be initialized and used if available */
  198. static bool use_gisa = true;
  199. module_param(use_gisa, bool, 0644);
  200. MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
  201. /* maximum diag9c forwarding per second */
  202. unsigned int diag9c_forwarding_hz;
  203. module_param(diag9c_forwarding_hz, uint, 0644);
  204. MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
  205. /*
  206. * allow asynchronous deinit for protected guests; enable by default since
  207. * the feature is opt-in anyway
  208. */
  209. static int async_destroy = 1;
  210. module_param(async_destroy, int, 0444);
  211. MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
  212. /*
  213. * For now we handle at most 16 double words as this is what the s390 base
  214. * kernel handles and stores in the prefix page. If we ever need to go beyond
  215. * this, this requires changes to code, but the external uapi can stay.
  216. */
  217. #define SIZE_INTERNAL 16
  218. /*
  219. * Base feature mask that defines default mask for facilities. Consists of the
  220. * defines in FACILITIES_KVM and the non-hypervisor managed bits.
  221. */
  222. static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
  223. /*
  224. * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
  225. * and defines the facilities that can be enabled via a cpu model.
  226. */
  227. static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
  228. static unsigned long kvm_s390_fac_size(void)
  229. {
  230. BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
  231. BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
  232. BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
  233. sizeof(stfle_fac_list));
  234. return SIZE_INTERNAL;
  235. }
  236. /* available cpu features supported by kvm */
  237. static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
  238. /* available subfunctions indicated via query / "test bit" */
  239. static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
  240. static struct gmap_notifier gmap_notifier;
  241. static struct gmap_notifier vsie_gmap_notifier;
  242. debug_info_t *kvm_s390_dbf;
  243. debug_info_t *kvm_s390_dbf_uv;
  244. /* Section: not file related */
  245. /* forward declarations */
  246. static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
  247. unsigned long end);
  248. static int sca_switch_to_extended(struct kvm *kvm);
  249. static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
  250. {
  251. u8 delta_idx = 0;
  252. /*
  253. * The TOD jumps by delta, we have to compensate this by adding
  254. * -delta to the epoch.
  255. */
  256. delta = -delta;
  257. /* sign-extension - we're adding to signed values below */
  258. if ((s64)delta < 0)
  259. delta_idx = -1;
  260. scb->epoch += delta;
  261. if (scb->ecd & ECD_MEF) {
  262. scb->epdx += delta_idx;
  263. if (scb->epoch < delta)
  264. scb->epdx += 1;
  265. }
  266. }
  267. /*
  268. * This callback is executed during stop_machine(). All CPUs are therefore
  269. * temporarily stopped. In order not to change guest behavior, we have to
  270. * disable preemption whenever we touch the epoch of kvm and the VCPUs,
  271. * so a CPU won't be stopped while calculating with the epoch.
  272. */
  273. static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
  274. void *v)
  275. {
  276. struct kvm *kvm;
  277. struct kvm_vcpu *vcpu;
  278. unsigned long i;
  279. unsigned long long *delta = v;
  280. list_for_each_entry(kvm, &vm_list, vm_list) {
  281. kvm_for_each_vcpu(i, vcpu, kvm) {
  282. kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
  283. if (i == 0) {
  284. kvm->arch.epoch = vcpu->arch.sie_block->epoch;
  285. kvm->arch.epdx = vcpu->arch.sie_block->epdx;
  286. }
  287. if (vcpu->arch.cputm_enabled)
  288. vcpu->arch.cputm_start += *delta;
  289. if (vcpu->arch.vsie_block)
  290. kvm_clock_sync_scb(vcpu->arch.vsie_block,
  291. *delta);
  292. }
  293. }
  294. return NOTIFY_OK;
  295. }
  296. static struct notifier_block kvm_clock_notifier = {
  297. .notifier_call = kvm_clock_sync,
  298. };
  299. static void allow_cpu_feat(unsigned long nr)
  300. {
  301. set_bit_inv(nr, kvm_s390_available_cpu_feat);
  302. }
  303. static inline int plo_test_bit(unsigned char nr)
  304. {
  305. unsigned long function = (unsigned long)nr | 0x100;
  306. int cc;
  307. asm volatile(
  308. " lgr 0,%[function]\n"
  309. /* Parameter registers are ignored for "test bit" */
  310. " plo 0,0,0,0(0)\n"
  311. " ipm %0\n"
  312. " srl %0,28\n"
  313. : "=d" (cc)
  314. : [function] "d" (function)
  315. : "cc", "0");
  316. return cc == 0;
  317. }
  318. static __always_inline void __sortl_query(u8 (*query)[32])
  319. {
  320. asm volatile(
  321. " lghi 0,0\n"
  322. " la 1,%[query]\n"
  323. /* Parameter registers are ignored */
  324. " .insn rre,0xb9380000,2,4\n"
  325. : [query] "=R" (*query)
  326. :
  327. : "cc", "0", "1");
  328. }
  329. static __always_inline void __dfltcc_query(u8 (*query)[32])
  330. {
  331. asm volatile(
  332. " lghi 0,0\n"
  333. " la 1,%[query]\n"
  334. /* Parameter registers are ignored */
  335. " .insn rrf,0xb9390000,2,4,6,0\n"
  336. : [query] "=R" (*query)
  337. :
  338. : "cc", "0", "1");
  339. }
  340. static void __init kvm_s390_cpu_feat_init(void)
  341. {
  342. int i;
  343. for (i = 0; i < 256; ++i) {
  344. if (plo_test_bit(i))
  345. kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
  346. }
  347. if (test_facility(28)) /* TOD-clock steering */
  348. ptff(kvm_s390_available_subfunc.ptff,
  349. sizeof(kvm_s390_available_subfunc.ptff),
  350. PTFF_QAF);
  351. if (test_facility(17)) { /* MSA */
  352. __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
  353. kvm_s390_available_subfunc.kmac);
  354. __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
  355. kvm_s390_available_subfunc.kmc);
  356. __cpacf_query(CPACF_KM, (cpacf_mask_t *)
  357. kvm_s390_available_subfunc.km);
  358. __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
  359. kvm_s390_available_subfunc.kimd);
  360. __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
  361. kvm_s390_available_subfunc.klmd);
  362. }
  363. if (test_facility(76)) /* MSA3 */
  364. __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
  365. kvm_s390_available_subfunc.pckmo);
  366. if (test_facility(77)) { /* MSA4 */
  367. __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
  368. kvm_s390_available_subfunc.kmctr);
  369. __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
  370. kvm_s390_available_subfunc.kmf);
  371. __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
  372. kvm_s390_available_subfunc.kmo);
  373. __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
  374. kvm_s390_available_subfunc.pcc);
  375. }
  376. if (test_facility(57)) /* MSA5 */
  377. __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
  378. kvm_s390_available_subfunc.ppno);
  379. if (test_facility(146)) /* MSA8 */
  380. __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
  381. kvm_s390_available_subfunc.kma);
  382. if (test_facility(155)) /* MSA9 */
  383. __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
  384. kvm_s390_available_subfunc.kdsa);
  385. if (test_facility(150)) /* SORTL */
  386. __sortl_query(&kvm_s390_available_subfunc.sortl);
  387. if (test_facility(151)) /* DFLTCC */
  388. __dfltcc_query(&kvm_s390_available_subfunc.dfltcc);
  389. if (MACHINE_HAS_ESOP)
  390. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
  391. /*
  392. * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
  393. * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
  394. */
  395. if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
  396. !test_facility(3) || !nested)
  397. return;
  398. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
  399. if (sclp.has_64bscao)
  400. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
  401. if (sclp.has_siif)
  402. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
  403. if (sclp.has_gpere)
  404. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
  405. if (sclp.has_gsls)
  406. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
  407. if (sclp.has_ib)
  408. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
  409. if (sclp.has_cei)
  410. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
  411. if (sclp.has_ibs)
  412. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
  413. if (sclp.has_kss)
  414. allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
  415. /*
  416. * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
  417. * all skey handling functions read/set the skey from the PGSTE
  418. * instead of the real storage key.
  419. *
  420. * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
  421. * pages being detected as preserved although they are resident.
  422. *
  423. * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
  424. * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
  425. *
  426. * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
  427. * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
  428. * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
  429. *
  430. * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
  431. * cannot easily shadow the SCA because of the ipte lock.
  432. */
  433. }
  434. static int __init __kvm_s390_init(void)
  435. {
  436. int rc = -ENOMEM;
  437. kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
  438. if (!kvm_s390_dbf)
  439. return -ENOMEM;
  440. kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
  441. if (!kvm_s390_dbf_uv)
  442. goto err_kvm_uv;
  443. if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
  444. debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
  445. goto err_debug_view;
  446. kvm_s390_cpu_feat_init();
  447. /* Register floating interrupt controller interface. */
  448. rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
  449. if (rc) {
  450. pr_err("A FLIC registration call failed with rc=%d\n", rc);
  451. goto err_flic;
  452. }
  453. if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
  454. rc = kvm_s390_pci_init();
  455. if (rc) {
  456. pr_err("Unable to allocate AIFT for PCI\n");
  457. goto err_pci;
  458. }
  459. }
  460. rc = kvm_s390_gib_init(GAL_ISC);
  461. if (rc)
  462. goto err_gib;
  463. gmap_notifier.notifier_call = kvm_gmap_notifier;
  464. gmap_register_pte_notifier(&gmap_notifier);
  465. vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
  466. gmap_register_pte_notifier(&vsie_gmap_notifier);
  467. atomic_notifier_chain_register(&s390_epoch_delta_notifier,
  468. &kvm_clock_notifier);
  469. return 0;
  470. err_gib:
  471. if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
  472. kvm_s390_pci_exit();
  473. err_pci:
  474. err_flic:
  475. err_debug_view:
  476. debug_unregister(kvm_s390_dbf_uv);
  477. err_kvm_uv:
  478. debug_unregister(kvm_s390_dbf);
  479. return rc;
  480. }
  481. static void __kvm_s390_exit(void)
  482. {
  483. gmap_unregister_pte_notifier(&gmap_notifier);
  484. gmap_unregister_pte_notifier(&vsie_gmap_notifier);
  485. atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
  486. &kvm_clock_notifier);
  487. kvm_s390_gib_destroy();
  488. if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
  489. kvm_s390_pci_exit();
  490. debug_unregister(kvm_s390_dbf);
  491. debug_unregister(kvm_s390_dbf_uv);
  492. }
  493. /* Section: device related */
  494. long kvm_arch_dev_ioctl(struct file *filp,
  495. unsigned int ioctl, unsigned long arg)
  496. {
  497. if (ioctl == KVM_S390_ENABLE_SIE)
  498. return s390_enable_sie();
  499. return -EINVAL;
  500. }
  501. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  502. {
  503. int r;
  504. switch (ext) {
  505. case KVM_CAP_S390_PSW:
  506. case KVM_CAP_S390_GMAP:
  507. case KVM_CAP_SYNC_MMU:
  508. #ifdef CONFIG_KVM_S390_UCONTROL
  509. case KVM_CAP_S390_UCONTROL:
  510. #endif
  511. case KVM_CAP_ASYNC_PF:
  512. case KVM_CAP_SYNC_REGS:
  513. case KVM_CAP_ONE_REG:
  514. case KVM_CAP_ENABLE_CAP:
  515. case KVM_CAP_S390_CSS_SUPPORT:
  516. case KVM_CAP_IOEVENTFD:
  517. case KVM_CAP_S390_IRQCHIP:
  518. case KVM_CAP_VM_ATTRIBUTES:
  519. case KVM_CAP_MP_STATE:
  520. case KVM_CAP_IMMEDIATE_EXIT:
  521. case KVM_CAP_S390_INJECT_IRQ:
  522. case KVM_CAP_S390_USER_SIGP:
  523. case KVM_CAP_S390_USER_STSI:
  524. case KVM_CAP_S390_SKEYS:
  525. case KVM_CAP_S390_IRQ_STATE:
  526. case KVM_CAP_S390_USER_INSTR0:
  527. case KVM_CAP_S390_CMMA_MIGRATION:
  528. case KVM_CAP_S390_AIS:
  529. case KVM_CAP_S390_AIS_MIGRATION:
  530. case KVM_CAP_S390_VCPU_RESETS:
  531. case KVM_CAP_SET_GUEST_DEBUG:
  532. case KVM_CAP_S390_DIAG318:
  533. case KVM_CAP_IRQFD_RESAMPLE:
  534. r = 1;
  535. break;
  536. case KVM_CAP_SET_GUEST_DEBUG2:
  537. r = KVM_GUESTDBG_VALID_MASK;
  538. break;
  539. case KVM_CAP_S390_HPAGE_1M:
  540. r = 0;
  541. if (hpage && !(kvm && kvm_is_ucontrol(kvm)))
  542. r = 1;
  543. break;
  544. case KVM_CAP_S390_MEM_OP:
  545. r = MEM_OP_MAX_SIZE;
  546. break;
  547. case KVM_CAP_S390_MEM_OP_EXTENSION:
  548. /*
  549. * Flag bits indicating which extensions are supported.
  550. * If r > 0, the base extension must also be supported/indicated,
  551. * in order to maintain backwards compatibility.
  552. */
  553. r = KVM_S390_MEMOP_EXTENSION_CAP_BASE |
  554. KVM_S390_MEMOP_EXTENSION_CAP_CMPXCHG;
  555. break;
  556. case KVM_CAP_NR_VCPUS:
  557. case KVM_CAP_MAX_VCPUS:
  558. case KVM_CAP_MAX_VCPU_ID:
  559. r = KVM_S390_BSCA_CPU_SLOTS;
  560. if (!kvm_s390_use_sca_entries())
  561. r = KVM_MAX_VCPUS;
  562. else if (sclp.has_esca && sclp.has_64bscao)
  563. r = KVM_S390_ESCA_CPU_SLOTS;
  564. if (ext == KVM_CAP_NR_VCPUS)
  565. r = min_t(unsigned int, num_online_cpus(), r);
  566. break;
  567. case KVM_CAP_S390_COW:
  568. r = MACHINE_HAS_ESOP;
  569. break;
  570. case KVM_CAP_S390_VECTOR_REGISTERS:
  571. r = test_facility(129);
  572. break;
  573. case KVM_CAP_S390_RI:
  574. r = test_facility(64);
  575. break;
  576. case KVM_CAP_S390_GS:
  577. r = test_facility(133);
  578. break;
  579. case KVM_CAP_S390_BPB:
  580. r = test_facility(82);
  581. break;
  582. case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE:
  583. r = async_destroy && is_prot_virt_host();
  584. break;
  585. case KVM_CAP_S390_PROTECTED:
  586. r = is_prot_virt_host();
  587. break;
  588. case KVM_CAP_S390_PROTECTED_DUMP: {
  589. u64 pv_cmds_dump[] = {
  590. BIT_UVC_CMD_DUMP_INIT,
  591. BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE,
  592. BIT_UVC_CMD_DUMP_CPU,
  593. BIT_UVC_CMD_DUMP_COMPLETE,
  594. };
  595. int i;
  596. r = is_prot_virt_host();
  597. for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) {
  598. if (!test_bit_inv(pv_cmds_dump[i],
  599. (unsigned long *)&uv_info.inst_calls_list)) {
  600. r = 0;
  601. break;
  602. }
  603. }
  604. break;
  605. }
  606. case KVM_CAP_S390_ZPCI_OP:
  607. r = kvm_s390_pci_interp_allowed();
  608. break;
  609. case KVM_CAP_S390_CPU_TOPOLOGY:
  610. r = test_facility(11);
  611. break;
  612. default:
  613. r = 0;
  614. }
  615. return r;
  616. }
  617. void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  618. {
  619. int i;
  620. gfn_t cur_gfn, last_gfn;
  621. unsigned long gaddr, vmaddr;
  622. struct gmap *gmap = kvm->arch.gmap;
  623. DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
  624. /* Loop over all guest segments */
  625. cur_gfn = memslot->base_gfn;
  626. last_gfn = memslot->base_gfn + memslot->npages;
  627. for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
  628. gaddr = gfn_to_gpa(cur_gfn);
  629. vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
  630. if (kvm_is_error_hva(vmaddr))
  631. continue;
  632. bitmap_zero(bitmap, _PAGE_ENTRIES);
  633. gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
  634. for (i = 0; i < _PAGE_ENTRIES; i++) {
  635. if (test_bit(i, bitmap))
  636. mark_page_dirty(kvm, cur_gfn + i);
  637. }
  638. if (fatal_signal_pending(current))
  639. return;
  640. cond_resched();
  641. }
  642. }
  643. /* Section: vm related */
  644. static void sca_del_vcpu(struct kvm_vcpu *vcpu);
  645. /*
  646. * Get (and clear) the dirty memory log for a memory slot.
  647. */
  648. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  649. struct kvm_dirty_log *log)
  650. {
  651. int r;
  652. unsigned long n;
  653. struct kvm_memory_slot *memslot;
  654. int is_dirty;
  655. if (kvm_is_ucontrol(kvm))
  656. return -EINVAL;
  657. mutex_lock(&kvm->slots_lock);
  658. r = -EINVAL;
  659. if (log->slot >= KVM_USER_MEM_SLOTS)
  660. goto out;
  661. r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
  662. if (r)
  663. goto out;
  664. /* Clear the dirty log */
  665. if (is_dirty) {
  666. n = kvm_dirty_bitmap_bytes(memslot);
  667. memset(memslot->dirty_bitmap, 0, n);
  668. }
  669. r = 0;
  670. out:
  671. mutex_unlock(&kvm->slots_lock);
  672. return r;
  673. }
  674. static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
  675. {
  676. unsigned long i;
  677. struct kvm_vcpu *vcpu;
  678. kvm_for_each_vcpu(i, vcpu, kvm) {
  679. kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
  680. }
  681. }
  682. int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
  683. {
  684. int r;
  685. if (cap->flags)
  686. return -EINVAL;
  687. switch (cap->cap) {
  688. case KVM_CAP_S390_IRQCHIP:
  689. VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
  690. kvm->arch.use_irqchip = 1;
  691. r = 0;
  692. break;
  693. case KVM_CAP_S390_USER_SIGP:
  694. VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
  695. kvm->arch.user_sigp = 1;
  696. r = 0;
  697. break;
  698. case KVM_CAP_S390_VECTOR_REGISTERS:
  699. mutex_lock(&kvm->lock);
  700. if (kvm->created_vcpus) {
  701. r = -EBUSY;
  702. } else if (cpu_has_vx()) {
  703. set_kvm_facility(kvm->arch.model.fac_mask, 129);
  704. set_kvm_facility(kvm->arch.model.fac_list, 129);
  705. if (test_facility(134)) {
  706. set_kvm_facility(kvm->arch.model.fac_mask, 134);
  707. set_kvm_facility(kvm->arch.model.fac_list, 134);
  708. }
  709. if (test_facility(135)) {
  710. set_kvm_facility(kvm->arch.model.fac_mask, 135);
  711. set_kvm_facility(kvm->arch.model.fac_list, 135);
  712. }
  713. if (test_facility(148)) {
  714. set_kvm_facility(kvm->arch.model.fac_mask, 148);
  715. set_kvm_facility(kvm->arch.model.fac_list, 148);
  716. }
  717. if (test_facility(152)) {
  718. set_kvm_facility(kvm->arch.model.fac_mask, 152);
  719. set_kvm_facility(kvm->arch.model.fac_list, 152);
  720. }
  721. if (test_facility(192)) {
  722. set_kvm_facility(kvm->arch.model.fac_mask, 192);
  723. set_kvm_facility(kvm->arch.model.fac_list, 192);
  724. }
  725. r = 0;
  726. } else
  727. r = -EINVAL;
  728. mutex_unlock(&kvm->lock);
  729. VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
  730. r ? "(not available)" : "(success)");
  731. break;
  732. case KVM_CAP_S390_RI:
  733. r = -EINVAL;
  734. mutex_lock(&kvm->lock);
  735. if (kvm->created_vcpus) {
  736. r = -EBUSY;
  737. } else if (test_facility(64)) {
  738. set_kvm_facility(kvm->arch.model.fac_mask, 64);
  739. set_kvm_facility(kvm->arch.model.fac_list, 64);
  740. r = 0;
  741. }
  742. mutex_unlock(&kvm->lock);
  743. VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
  744. r ? "(not available)" : "(success)");
  745. break;
  746. case KVM_CAP_S390_AIS:
  747. mutex_lock(&kvm->lock);
  748. if (kvm->created_vcpus) {
  749. r = -EBUSY;
  750. } else {
  751. set_kvm_facility(kvm->arch.model.fac_mask, 72);
  752. set_kvm_facility(kvm->arch.model.fac_list, 72);
  753. r = 0;
  754. }
  755. mutex_unlock(&kvm->lock);
  756. VM_EVENT(kvm, 3, "ENABLE: AIS %s",
  757. r ? "(not available)" : "(success)");
  758. break;
  759. case KVM_CAP_S390_GS:
  760. r = -EINVAL;
  761. mutex_lock(&kvm->lock);
  762. if (kvm->created_vcpus) {
  763. r = -EBUSY;
  764. } else if (test_facility(133)) {
  765. set_kvm_facility(kvm->arch.model.fac_mask, 133);
  766. set_kvm_facility(kvm->arch.model.fac_list, 133);
  767. r = 0;
  768. }
  769. mutex_unlock(&kvm->lock);
  770. VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
  771. r ? "(not available)" : "(success)");
  772. break;
  773. case KVM_CAP_S390_HPAGE_1M:
  774. mutex_lock(&kvm->lock);
  775. if (kvm->created_vcpus)
  776. r = -EBUSY;
  777. else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
  778. r = -EINVAL;
  779. else {
  780. r = 0;
  781. mmap_write_lock(kvm->mm);
  782. kvm->mm->context.allow_gmap_hpage_1m = 1;
  783. mmap_write_unlock(kvm->mm);
  784. /*
  785. * We might have to create fake 4k page
  786. * tables. To avoid that the hardware works on
  787. * stale PGSTEs, we emulate these instructions.
  788. */
  789. kvm->arch.use_skf = 0;
  790. kvm->arch.use_pfmfi = 0;
  791. }
  792. mutex_unlock(&kvm->lock);
  793. VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
  794. r ? "(not available)" : "(success)");
  795. break;
  796. case KVM_CAP_S390_USER_STSI:
  797. VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
  798. kvm->arch.user_stsi = 1;
  799. r = 0;
  800. break;
  801. case KVM_CAP_S390_USER_INSTR0:
  802. VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
  803. kvm->arch.user_instr0 = 1;
  804. icpt_operexc_on_all_vcpus(kvm);
  805. r = 0;
  806. break;
  807. case KVM_CAP_S390_CPU_TOPOLOGY:
  808. r = -EINVAL;
  809. mutex_lock(&kvm->lock);
  810. if (kvm->created_vcpus) {
  811. r = -EBUSY;
  812. } else if (test_facility(11)) {
  813. set_kvm_facility(kvm->arch.model.fac_mask, 11);
  814. set_kvm_facility(kvm->arch.model.fac_list, 11);
  815. r = 0;
  816. }
  817. mutex_unlock(&kvm->lock);
  818. VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s",
  819. r ? "(not available)" : "(success)");
  820. break;
  821. default:
  822. r = -EINVAL;
  823. break;
  824. }
  825. return r;
  826. }
  827. static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
  828. {
  829. int ret;
  830. switch (attr->attr) {
  831. case KVM_S390_VM_MEM_LIMIT_SIZE:
  832. ret = 0;
  833. VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
  834. kvm->arch.mem_limit);
  835. if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
  836. ret = -EFAULT;
  837. break;
  838. default:
  839. ret = -ENXIO;
  840. break;
  841. }
  842. return ret;
  843. }
  844. static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
  845. {
  846. int ret;
  847. unsigned int idx;
  848. switch (attr->attr) {
  849. case KVM_S390_VM_MEM_ENABLE_CMMA:
  850. ret = -ENXIO;
  851. if (!sclp.has_cmma)
  852. break;
  853. VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
  854. mutex_lock(&kvm->lock);
  855. if (kvm->created_vcpus)
  856. ret = -EBUSY;
  857. else if (kvm->mm->context.allow_gmap_hpage_1m)
  858. ret = -EINVAL;
  859. else {
  860. kvm->arch.use_cmma = 1;
  861. /* Not compatible with cmma. */
  862. kvm->arch.use_pfmfi = 0;
  863. ret = 0;
  864. }
  865. mutex_unlock(&kvm->lock);
  866. break;
  867. case KVM_S390_VM_MEM_CLR_CMMA:
  868. ret = -ENXIO;
  869. if (!sclp.has_cmma)
  870. break;
  871. ret = -EINVAL;
  872. if (!kvm->arch.use_cmma)
  873. break;
  874. VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
  875. mutex_lock(&kvm->lock);
  876. idx = srcu_read_lock(&kvm->srcu);
  877. s390_reset_cmma(kvm->arch.gmap->mm);
  878. srcu_read_unlock(&kvm->srcu, idx);
  879. mutex_unlock(&kvm->lock);
  880. ret = 0;
  881. break;
  882. case KVM_S390_VM_MEM_LIMIT_SIZE: {
  883. unsigned long new_limit;
  884. if (kvm_is_ucontrol(kvm))
  885. return -EINVAL;
  886. if (get_user(new_limit, (u64 __user *)attr->addr))
  887. return -EFAULT;
  888. if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
  889. new_limit > kvm->arch.mem_limit)
  890. return -E2BIG;
  891. if (!new_limit)
  892. return -EINVAL;
  893. /* gmap_create takes last usable address */
  894. if (new_limit != KVM_S390_NO_MEM_LIMIT)
  895. new_limit -= 1;
  896. ret = -EBUSY;
  897. mutex_lock(&kvm->lock);
  898. if (!kvm->created_vcpus) {
  899. /* gmap_create will round the limit up */
  900. struct gmap *new = gmap_create(current->mm, new_limit);
  901. if (!new) {
  902. ret = -ENOMEM;
  903. } else {
  904. gmap_remove(kvm->arch.gmap);
  905. new->private = kvm;
  906. kvm->arch.gmap = new;
  907. ret = 0;
  908. }
  909. }
  910. mutex_unlock(&kvm->lock);
  911. VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
  912. VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
  913. (void *) kvm->arch.gmap->asce);
  914. break;
  915. }
  916. default:
  917. ret = -ENXIO;
  918. break;
  919. }
  920. return ret;
  921. }
  922. static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
  923. void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
  924. {
  925. struct kvm_vcpu *vcpu;
  926. unsigned long i;
  927. kvm_s390_vcpu_block_all(kvm);
  928. kvm_for_each_vcpu(i, vcpu, kvm) {
  929. kvm_s390_vcpu_crypto_setup(vcpu);
  930. /* recreate the shadow crycb by leaving the VSIE handler */
  931. kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
  932. }
  933. kvm_s390_vcpu_unblock_all(kvm);
  934. }
  935. static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
  936. {
  937. mutex_lock(&kvm->lock);
  938. switch (attr->attr) {
  939. case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
  940. if (!test_kvm_facility(kvm, 76)) {
  941. mutex_unlock(&kvm->lock);
  942. return -EINVAL;
  943. }
  944. get_random_bytes(
  945. kvm->arch.crypto.crycb->aes_wrapping_key_mask,
  946. sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
  947. kvm->arch.crypto.aes_kw = 1;
  948. VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
  949. break;
  950. case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
  951. if (!test_kvm_facility(kvm, 76)) {
  952. mutex_unlock(&kvm->lock);
  953. return -EINVAL;
  954. }
  955. get_random_bytes(
  956. kvm->arch.crypto.crycb->dea_wrapping_key_mask,
  957. sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
  958. kvm->arch.crypto.dea_kw = 1;
  959. VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
  960. break;
  961. case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
  962. if (!test_kvm_facility(kvm, 76)) {
  963. mutex_unlock(&kvm->lock);
  964. return -EINVAL;
  965. }
  966. kvm->arch.crypto.aes_kw = 0;
  967. memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
  968. sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
  969. VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
  970. break;
  971. case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
  972. if (!test_kvm_facility(kvm, 76)) {
  973. mutex_unlock(&kvm->lock);
  974. return -EINVAL;
  975. }
  976. kvm->arch.crypto.dea_kw = 0;
  977. memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
  978. sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
  979. VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
  980. break;
  981. case KVM_S390_VM_CRYPTO_ENABLE_APIE:
  982. if (!ap_instructions_available()) {
  983. mutex_unlock(&kvm->lock);
  984. return -EOPNOTSUPP;
  985. }
  986. kvm->arch.crypto.apie = 1;
  987. break;
  988. case KVM_S390_VM_CRYPTO_DISABLE_APIE:
  989. if (!ap_instructions_available()) {
  990. mutex_unlock(&kvm->lock);
  991. return -EOPNOTSUPP;
  992. }
  993. kvm->arch.crypto.apie = 0;
  994. break;
  995. default:
  996. mutex_unlock(&kvm->lock);
  997. return -ENXIO;
  998. }
  999. kvm_s390_vcpu_crypto_reset_all(kvm);
  1000. mutex_unlock(&kvm->lock);
  1001. return 0;
  1002. }
  1003. static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
  1004. {
  1005. /* Only set the ECB bits after guest requests zPCI interpretation */
  1006. if (!vcpu->kvm->arch.use_zpci_interp)
  1007. return;
  1008. vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
  1009. vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
  1010. }
  1011. void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
  1012. {
  1013. struct kvm_vcpu *vcpu;
  1014. unsigned long i;
  1015. lockdep_assert_held(&kvm->lock);
  1016. if (!kvm_s390_pci_interp_allowed())
  1017. return;
  1018. /*
  1019. * If host is configured for PCI and the necessary facilities are
  1020. * available, turn on interpretation for the life of this guest
  1021. */
  1022. kvm->arch.use_zpci_interp = 1;
  1023. kvm_s390_vcpu_block_all(kvm);
  1024. kvm_for_each_vcpu(i, vcpu, kvm) {
  1025. kvm_s390_vcpu_pci_setup(vcpu);
  1026. kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
  1027. }
  1028. kvm_s390_vcpu_unblock_all(kvm);
  1029. }
  1030. static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
  1031. {
  1032. unsigned long cx;
  1033. struct kvm_vcpu *vcpu;
  1034. kvm_for_each_vcpu(cx, vcpu, kvm)
  1035. kvm_s390_sync_request(req, vcpu);
  1036. }
  1037. /*
  1038. * Must be called with kvm->srcu held to avoid races on memslots, and with
  1039. * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
  1040. */
  1041. static int kvm_s390_vm_start_migration(struct kvm *kvm)
  1042. {
  1043. struct kvm_memory_slot *ms;
  1044. struct kvm_memslots *slots;
  1045. unsigned long ram_pages = 0;
  1046. int bkt;
  1047. /* migration mode already enabled */
  1048. if (kvm->arch.migration_mode)
  1049. return 0;
  1050. slots = kvm_memslots(kvm);
  1051. if (!slots || kvm_memslots_empty(slots))
  1052. return -EINVAL;
  1053. if (!kvm->arch.use_cmma) {
  1054. kvm->arch.migration_mode = 1;
  1055. return 0;
  1056. }
  1057. /* mark all the pages in active slots as dirty */
  1058. kvm_for_each_memslot(ms, bkt, slots) {
  1059. if (!ms->dirty_bitmap)
  1060. return -EINVAL;
  1061. /*
  1062. * The second half of the bitmap is only used on x86,
  1063. * and would be wasted otherwise, so we put it to good
  1064. * use here to keep track of the state of the storage
  1065. * attributes.
  1066. */
  1067. memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
  1068. ram_pages += ms->npages;
  1069. }
  1070. atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
  1071. kvm->arch.migration_mode = 1;
  1072. kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
  1073. return 0;
  1074. }
  1075. /*
  1076. * Must be called with kvm->slots_lock to avoid races with ourselves and
  1077. * kvm_s390_vm_start_migration.
  1078. */
  1079. static int kvm_s390_vm_stop_migration(struct kvm *kvm)
  1080. {
  1081. /* migration mode already disabled */
  1082. if (!kvm->arch.migration_mode)
  1083. return 0;
  1084. kvm->arch.migration_mode = 0;
  1085. if (kvm->arch.use_cmma)
  1086. kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
  1087. return 0;
  1088. }
  1089. static int kvm_s390_vm_set_migration(struct kvm *kvm,
  1090. struct kvm_device_attr *attr)
  1091. {
  1092. int res = -ENXIO;
  1093. mutex_lock(&kvm->slots_lock);
  1094. switch (attr->attr) {
  1095. case KVM_S390_VM_MIGRATION_START:
  1096. res = kvm_s390_vm_start_migration(kvm);
  1097. break;
  1098. case KVM_S390_VM_MIGRATION_STOP:
  1099. res = kvm_s390_vm_stop_migration(kvm);
  1100. break;
  1101. default:
  1102. break;
  1103. }
  1104. mutex_unlock(&kvm->slots_lock);
  1105. return res;
  1106. }
  1107. static int kvm_s390_vm_get_migration(struct kvm *kvm,
  1108. struct kvm_device_attr *attr)
  1109. {
  1110. u64 mig = kvm->arch.migration_mode;
  1111. if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
  1112. return -ENXIO;
  1113. if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
  1114. return -EFAULT;
  1115. return 0;
  1116. }
  1117. static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
  1118. static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
  1119. {
  1120. struct kvm_s390_vm_tod_clock gtod;
  1121. if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
  1122. return -EFAULT;
  1123. if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
  1124. return -EINVAL;
  1125. __kvm_s390_set_tod_clock(kvm, &gtod);
  1126. VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
  1127. gtod.epoch_idx, gtod.tod);
  1128. return 0;
  1129. }
  1130. static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
  1131. {
  1132. u8 gtod_high;
  1133. if (copy_from_user(&gtod_high, (void __user *)attr->addr,
  1134. sizeof(gtod_high)))
  1135. return -EFAULT;
  1136. if (gtod_high != 0)
  1137. return -EINVAL;
  1138. VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
  1139. return 0;
  1140. }
  1141. static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
  1142. {
  1143. struct kvm_s390_vm_tod_clock gtod = { 0 };
  1144. if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
  1145. sizeof(gtod.tod)))
  1146. return -EFAULT;
  1147. __kvm_s390_set_tod_clock(kvm, &gtod);
  1148. VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
  1149. return 0;
  1150. }
  1151. static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
  1152. {
  1153. int ret;
  1154. if (attr->flags)
  1155. return -EINVAL;
  1156. mutex_lock(&kvm->lock);
  1157. /*
  1158. * For protected guests, the TOD is managed by the ultravisor, so trying
  1159. * to change it will never bring the expected results.
  1160. */
  1161. if (kvm_s390_pv_is_protected(kvm)) {
  1162. ret = -EOPNOTSUPP;
  1163. goto out_unlock;
  1164. }
  1165. switch (attr->attr) {
  1166. case KVM_S390_VM_TOD_EXT:
  1167. ret = kvm_s390_set_tod_ext(kvm, attr);
  1168. break;
  1169. case KVM_S390_VM_TOD_HIGH:
  1170. ret = kvm_s390_set_tod_high(kvm, attr);
  1171. break;
  1172. case KVM_S390_VM_TOD_LOW:
  1173. ret = kvm_s390_set_tod_low(kvm, attr);
  1174. break;
  1175. default:
  1176. ret = -ENXIO;
  1177. break;
  1178. }
  1179. out_unlock:
  1180. mutex_unlock(&kvm->lock);
  1181. return ret;
  1182. }
  1183. static void kvm_s390_get_tod_clock(struct kvm *kvm,
  1184. struct kvm_s390_vm_tod_clock *gtod)
  1185. {
  1186. union tod_clock clk;
  1187. preempt_disable();
  1188. store_tod_clock_ext(&clk);
  1189. gtod->tod = clk.tod + kvm->arch.epoch;
  1190. gtod->epoch_idx = 0;
  1191. if (test_kvm_facility(kvm, 139)) {
  1192. gtod->epoch_idx = clk.ei + kvm->arch.epdx;
  1193. if (gtod->tod < clk.tod)
  1194. gtod->epoch_idx += 1;
  1195. }
  1196. preempt_enable();
  1197. }
  1198. static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
  1199. {
  1200. struct kvm_s390_vm_tod_clock gtod;
  1201. memset(&gtod, 0, sizeof(gtod));
  1202. kvm_s390_get_tod_clock(kvm, &gtod);
  1203. if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
  1204. return -EFAULT;
  1205. VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
  1206. gtod.epoch_idx, gtod.tod);
  1207. return 0;
  1208. }
  1209. static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
  1210. {
  1211. u8 gtod_high = 0;
  1212. if (copy_to_user((void __user *)attr->addr, &gtod_high,
  1213. sizeof(gtod_high)))
  1214. return -EFAULT;
  1215. VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
  1216. return 0;
  1217. }
  1218. static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
  1219. {
  1220. u64 gtod;
  1221. gtod = kvm_s390_get_tod_clock_fast(kvm);
  1222. if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
  1223. return -EFAULT;
  1224. VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
  1225. return 0;
  1226. }
  1227. static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
  1228. {
  1229. int ret;
  1230. if (attr->flags)
  1231. return -EINVAL;
  1232. switch (attr->attr) {
  1233. case KVM_S390_VM_TOD_EXT:
  1234. ret = kvm_s390_get_tod_ext(kvm, attr);
  1235. break;
  1236. case KVM_S390_VM_TOD_HIGH:
  1237. ret = kvm_s390_get_tod_high(kvm, attr);
  1238. break;
  1239. case KVM_S390_VM_TOD_LOW:
  1240. ret = kvm_s390_get_tod_low(kvm, attr);
  1241. break;
  1242. default:
  1243. ret = -ENXIO;
  1244. break;
  1245. }
  1246. return ret;
  1247. }
  1248. static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
  1249. {
  1250. struct kvm_s390_vm_cpu_processor *proc;
  1251. u16 lowest_ibc, unblocked_ibc;
  1252. int ret = 0;
  1253. mutex_lock(&kvm->lock);
  1254. if (kvm->created_vcpus) {
  1255. ret = -EBUSY;
  1256. goto out;
  1257. }
  1258. proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
  1259. if (!proc) {
  1260. ret = -ENOMEM;
  1261. goto out;
  1262. }
  1263. if (!copy_from_user(proc, (void __user *)attr->addr,
  1264. sizeof(*proc))) {
  1265. kvm->arch.model.cpuid = proc->cpuid;
  1266. lowest_ibc = sclp.ibc >> 16 & 0xfff;
  1267. unblocked_ibc = sclp.ibc & 0xfff;
  1268. if (lowest_ibc && proc->ibc) {
  1269. if (proc->ibc > unblocked_ibc)
  1270. kvm->arch.model.ibc = unblocked_ibc;
  1271. else if (proc->ibc < lowest_ibc)
  1272. kvm->arch.model.ibc = lowest_ibc;
  1273. else
  1274. kvm->arch.model.ibc = proc->ibc;
  1275. }
  1276. memcpy(kvm->arch.model.fac_list, proc->fac_list,
  1277. S390_ARCH_FAC_LIST_SIZE_BYTE);
  1278. VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
  1279. kvm->arch.model.ibc,
  1280. kvm->arch.model.cpuid);
  1281. VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
  1282. kvm->arch.model.fac_list[0],
  1283. kvm->arch.model.fac_list[1],
  1284. kvm->arch.model.fac_list[2]);
  1285. } else
  1286. ret = -EFAULT;
  1287. kfree(proc);
  1288. out:
  1289. mutex_unlock(&kvm->lock);
  1290. return ret;
  1291. }
  1292. static int kvm_s390_set_processor_feat(struct kvm *kvm,
  1293. struct kvm_device_attr *attr)
  1294. {
  1295. struct kvm_s390_vm_cpu_feat data;
  1296. if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
  1297. return -EFAULT;
  1298. if (!bitmap_subset((unsigned long *) data.feat,
  1299. kvm_s390_available_cpu_feat,
  1300. KVM_S390_VM_CPU_FEAT_NR_BITS))
  1301. return -EINVAL;
  1302. mutex_lock(&kvm->lock);
  1303. if (kvm->created_vcpus) {
  1304. mutex_unlock(&kvm->lock);
  1305. return -EBUSY;
  1306. }
  1307. bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
  1308. mutex_unlock(&kvm->lock);
  1309. VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
  1310. data.feat[0],
  1311. data.feat[1],
  1312. data.feat[2]);
  1313. return 0;
  1314. }
  1315. static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
  1316. struct kvm_device_attr *attr)
  1317. {
  1318. mutex_lock(&kvm->lock);
  1319. if (kvm->created_vcpus) {
  1320. mutex_unlock(&kvm->lock);
  1321. return -EBUSY;
  1322. }
  1323. if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
  1324. sizeof(struct kvm_s390_vm_cpu_subfunc))) {
  1325. mutex_unlock(&kvm->lock);
  1326. return -EFAULT;
  1327. }
  1328. mutex_unlock(&kvm->lock);
  1329. VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1330. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
  1331. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
  1332. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
  1333. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
  1334. VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
  1335. ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
  1336. ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
  1337. VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
  1338. ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
  1339. ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
  1340. VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
  1341. ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
  1342. ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
  1343. VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
  1344. ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
  1345. ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
  1346. VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
  1347. ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
  1348. ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
  1349. VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
  1350. ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
  1351. ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
  1352. VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
  1353. ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
  1354. ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
  1355. VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
  1356. ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
  1357. ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
  1358. VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
  1359. ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
  1360. ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
  1361. VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
  1362. ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
  1363. ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
  1364. VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
  1365. ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
  1366. ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
  1367. VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
  1368. ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
  1369. ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
  1370. VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
  1371. ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
  1372. ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
  1373. VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
  1374. ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
  1375. ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
  1376. VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1377. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
  1378. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
  1379. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
  1380. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
  1381. VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1382. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
  1383. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
  1384. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
  1385. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
  1386. return 0;
  1387. }
  1388. #define KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK \
  1389. ( \
  1390. ((struct kvm_s390_vm_cpu_uv_feat){ \
  1391. .ap = 1, \
  1392. .ap_intr = 1, \
  1393. }) \
  1394. .feat \
  1395. )
  1396. static int kvm_s390_set_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
  1397. {
  1398. struct kvm_s390_vm_cpu_uv_feat __user *ptr = (void __user *)attr->addr;
  1399. unsigned long data, filter;
  1400. filter = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
  1401. if (get_user(data, &ptr->feat))
  1402. return -EFAULT;
  1403. if (!bitmap_subset(&data, &filter, KVM_S390_VM_CPU_UV_FEAT_NR_BITS))
  1404. return -EINVAL;
  1405. mutex_lock(&kvm->lock);
  1406. if (kvm->created_vcpus) {
  1407. mutex_unlock(&kvm->lock);
  1408. return -EBUSY;
  1409. }
  1410. kvm->arch.model.uv_feat_guest.feat = data;
  1411. mutex_unlock(&kvm->lock);
  1412. VM_EVENT(kvm, 3, "SET: guest UV-feat: 0x%16.16lx", data);
  1413. return 0;
  1414. }
  1415. static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
  1416. {
  1417. int ret = -ENXIO;
  1418. switch (attr->attr) {
  1419. case KVM_S390_VM_CPU_PROCESSOR:
  1420. ret = kvm_s390_set_processor(kvm, attr);
  1421. break;
  1422. case KVM_S390_VM_CPU_PROCESSOR_FEAT:
  1423. ret = kvm_s390_set_processor_feat(kvm, attr);
  1424. break;
  1425. case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
  1426. ret = kvm_s390_set_processor_subfunc(kvm, attr);
  1427. break;
  1428. case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
  1429. ret = kvm_s390_set_uv_feat(kvm, attr);
  1430. break;
  1431. }
  1432. return ret;
  1433. }
  1434. static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
  1435. {
  1436. struct kvm_s390_vm_cpu_processor *proc;
  1437. int ret = 0;
  1438. proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
  1439. if (!proc) {
  1440. ret = -ENOMEM;
  1441. goto out;
  1442. }
  1443. proc->cpuid = kvm->arch.model.cpuid;
  1444. proc->ibc = kvm->arch.model.ibc;
  1445. memcpy(&proc->fac_list, kvm->arch.model.fac_list,
  1446. S390_ARCH_FAC_LIST_SIZE_BYTE);
  1447. VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
  1448. kvm->arch.model.ibc,
  1449. kvm->arch.model.cpuid);
  1450. VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
  1451. kvm->arch.model.fac_list[0],
  1452. kvm->arch.model.fac_list[1],
  1453. kvm->arch.model.fac_list[2]);
  1454. if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
  1455. ret = -EFAULT;
  1456. kfree(proc);
  1457. out:
  1458. return ret;
  1459. }
  1460. static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
  1461. {
  1462. struct kvm_s390_vm_cpu_machine *mach;
  1463. int ret = 0;
  1464. mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
  1465. if (!mach) {
  1466. ret = -ENOMEM;
  1467. goto out;
  1468. }
  1469. get_cpu_id((struct cpuid *) &mach->cpuid);
  1470. mach->ibc = sclp.ibc;
  1471. memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
  1472. S390_ARCH_FAC_LIST_SIZE_BYTE);
  1473. memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
  1474. sizeof(stfle_fac_list));
  1475. VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
  1476. kvm->arch.model.ibc,
  1477. kvm->arch.model.cpuid);
  1478. VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
  1479. mach->fac_mask[0],
  1480. mach->fac_mask[1],
  1481. mach->fac_mask[2]);
  1482. VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
  1483. mach->fac_list[0],
  1484. mach->fac_list[1],
  1485. mach->fac_list[2]);
  1486. if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
  1487. ret = -EFAULT;
  1488. kfree(mach);
  1489. out:
  1490. return ret;
  1491. }
  1492. static int kvm_s390_get_processor_feat(struct kvm *kvm,
  1493. struct kvm_device_attr *attr)
  1494. {
  1495. struct kvm_s390_vm_cpu_feat data;
  1496. bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
  1497. if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
  1498. return -EFAULT;
  1499. VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
  1500. data.feat[0],
  1501. data.feat[1],
  1502. data.feat[2]);
  1503. return 0;
  1504. }
  1505. static int kvm_s390_get_machine_feat(struct kvm *kvm,
  1506. struct kvm_device_attr *attr)
  1507. {
  1508. struct kvm_s390_vm_cpu_feat data;
  1509. bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
  1510. if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
  1511. return -EFAULT;
  1512. VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
  1513. data.feat[0],
  1514. data.feat[1],
  1515. data.feat[2]);
  1516. return 0;
  1517. }
  1518. static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
  1519. struct kvm_device_attr *attr)
  1520. {
  1521. if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
  1522. sizeof(struct kvm_s390_vm_cpu_subfunc)))
  1523. return -EFAULT;
  1524. VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1525. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
  1526. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
  1527. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
  1528. ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
  1529. VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
  1530. ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
  1531. ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
  1532. VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
  1533. ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
  1534. ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
  1535. VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
  1536. ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
  1537. ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
  1538. VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
  1539. ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
  1540. ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
  1541. VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
  1542. ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
  1543. ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
  1544. VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
  1545. ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
  1546. ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
  1547. VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
  1548. ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
  1549. ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
  1550. VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
  1551. ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
  1552. ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
  1553. VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
  1554. ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
  1555. ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
  1556. VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
  1557. ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
  1558. ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
  1559. VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
  1560. ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
  1561. ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
  1562. VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
  1563. ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
  1564. ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
  1565. VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
  1566. ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
  1567. ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
  1568. VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
  1569. ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
  1570. ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
  1571. VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1572. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
  1573. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
  1574. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
  1575. ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
  1576. VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1577. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
  1578. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
  1579. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
  1580. ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
  1581. return 0;
  1582. }
  1583. static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
  1584. struct kvm_device_attr *attr)
  1585. {
  1586. if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
  1587. sizeof(struct kvm_s390_vm_cpu_subfunc)))
  1588. return -EFAULT;
  1589. VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1590. ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
  1591. ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
  1592. ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
  1593. ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
  1594. VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
  1595. ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
  1596. ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
  1597. VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
  1598. ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
  1599. ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
  1600. VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
  1601. ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
  1602. ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
  1603. VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
  1604. ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
  1605. ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
  1606. VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
  1607. ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
  1608. ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
  1609. VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
  1610. ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
  1611. ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
  1612. VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
  1613. ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
  1614. ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
  1615. VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
  1616. ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
  1617. ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
  1618. VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
  1619. ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
  1620. ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
  1621. VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
  1622. ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
  1623. ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
  1624. VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
  1625. ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
  1626. ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
  1627. VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
  1628. ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
  1629. ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
  1630. VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
  1631. ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
  1632. ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
  1633. VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
  1634. ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
  1635. ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
  1636. VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1637. ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
  1638. ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
  1639. ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
  1640. ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
  1641. VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
  1642. ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
  1643. ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
  1644. ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
  1645. ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
  1646. return 0;
  1647. }
  1648. static int kvm_s390_get_processor_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
  1649. {
  1650. struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
  1651. unsigned long feat = kvm->arch.model.uv_feat_guest.feat;
  1652. if (put_user(feat, &dst->feat))
  1653. return -EFAULT;
  1654. VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
  1655. return 0;
  1656. }
  1657. static int kvm_s390_get_machine_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
  1658. {
  1659. struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
  1660. unsigned long feat;
  1661. BUILD_BUG_ON(sizeof(*dst) != sizeof(uv_info.uv_feature_indications));
  1662. feat = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
  1663. if (put_user(feat, &dst->feat))
  1664. return -EFAULT;
  1665. VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
  1666. return 0;
  1667. }
  1668. static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
  1669. {
  1670. int ret = -ENXIO;
  1671. switch (attr->attr) {
  1672. case KVM_S390_VM_CPU_PROCESSOR:
  1673. ret = kvm_s390_get_processor(kvm, attr);
  1674. break;
  1675. case KVM_S390_VM_CPU_MACHINE:
  1676. ret = kvm_s390_get_machine(kvm, attr);
  1677. break;
  1678. case KVM_S390_VM_CPU_PROCESSOR_FEAT:
  1679. ret = kvm_s390_get_processor_feat(kvm, attr);
  1680. break;
  1681. case KVM_S390_VM_CPU_MACHINE_FEAT:
  1682. ret = kvm_s390_get_machine_feat(kvm, attr);
  1683. break;
  1684. case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
  1685. ret = kvm_s390_get_processor_subfunc(kvm, attr);
  1686. break;
  1687. case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
  1688. ret = kvm_s390_get_machine_subfunc(kvm, attr);
  1689. break;
  1690. case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
  1691. ret = kvm_s390_get_processor_uv_feat(kvm, attr);
  1692. break;
  1693. case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
  1694. ret = kvm_s390_get_machine_uv_feat(kvm, attr);
  1695. break;
  1696. }
  1697. return ret;
  1698. }
  1699. /**
  1700. * kvm_s390_update_topology_change_report - update CPU topology change report
  1701. * @kvm: guest KVM description
  1702. * @val: set or clear the MTCR bit
  1703. *
  1704. * Updates the Multiprocessor Topology-Change-Report bit to signal
  1705. * the guest with a topology change.
  1706. * This is only relevant if the topology facility is present.
  1707. *
  1708. * The SCA version, bsca or esca, doesn't matter as offset is the same.
  1709. */
  1710. static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
  1711. {
  1712. union sca_utility new, old;
  1713. struct bsca_block *sca;
  1714. read_lock(&kvm->arch.sca_lock);
  1715. sca = kvm->arch.sca;
  1716. do {
  1717. old = READ_ONCE(sca->utility);
  1718. new = old;
  1719. new.mtcr = val;
  1720. } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val);
  1721. read_unlock(&kvm->arch.sca_lock);
  1722. }
  1723. static int kvm_s390_set_topo_change_indication(struct kvm *kvm,
  1724. struct kvm_device_attr *attr)
  1725. {
  1726. if (!test_kvm_facility(kvm, 11))
  1727. return -ENXIO;
  1728. kvm_s390_update_topology_change_report(kvm, !!attr->attr);
  1729. return 0;
  1730. }
  1731. static int kvm_s390_get_topo_change_indication(struct kvm *kvm,
  1732. struct kvm_device_attr *attr)
  1733. {
  1734. u8 topo;
  1735. if (!test_kvm_facility(kvm, 11))
  1736. return -ENXIO;
  1737. read_lock(&kvm->arch.sca_lock);
  1738. topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr;
  1739. read_unlock(&kvm->arch.sca_lock);
  1740. return put_user(topo, (u8 __user *)attr->addr);
  1741. }
  1742. static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
  1743. {
  1744. int ret;
  1745. switch (attr->group) {
  1746. case KVM_S390_VM_MEM_CTRL:
  1747. ret = kvm_s390_set_mem_control(kvm, attr);
  1748. break;
  1749. case KVM_S390_VM_TOD:
  1750. ret = kvm_s390_set_tod(kvm, attr);
  1751. break;
  1752. case KVM_S390_VM_CPU_MODEL:
  1753. ret = kvm_s390_set_cpu_model(kvm, attr);
  1754. break;
  1755. case KVM_S390_VM_CRYPTO:
  1756. ret = kvm_s390_vm_set_crypto(kvm, attr);
  1757. break;
  1758. case KVM_S390_VM_MIGRATION:
  1759. ret = kvm_s390_vm_set_migration(kvm, attr);
  1760. break;
  1761. case KVM_S390_VM_CPU_TOPOLOGY:
  1762. ret = kvm_s390_set_topo_change_indication(kvm, attr);
  1763. break;
  1764. default:
  1765. ret = -ENXIO;
  1766. break;
  1767. }
  1768. return ret;
  1769. }
  1770. static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
  1771. {
  1772. int ret;
  1773. switch (attr->group) {
  1774. case KVM_S390_VM_MEM_CTRL:
  1775. ret = kvm_s390_get_mem_control(kvm, attr);
  1776. break;
  1777. case KVM_S390_VM_TOD:
  1778. ret = kvm_s390_get_tod(kvm, attr);
  1779. break;
  1780. case KVM_S390_VM_CPU_MODEL:
  1781. ret = kvm_s390_get_cpu_model(kvm, attr);
  1782. break;
  1783. case KVM_S390_VM_MIGRATION:
  1784. ret = kvm_s390_vm_get_migration(kvm, attr);
  1785. break;
  1786. case KVM_S390_VM_CPU_TOPOLOGY:
  1787. ret = kvm_s390_get_topo_change_indication(kvm, attr);
  1788. break;
  1789. default:
  1790. ret = -ENXIO;
  1791. break;
  1792. }
  1793. return ret;
  1794. }
  1795. static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
  1796. {
  1797. int ret;
  1798. switch (attr->group) {
  1799. case KVM_S390_VM_MEM_CTRL:
  1800. switch (attr->attr) {
  1801. case KVM_S390_VM_MEM_ENABLE_CMMA:
  1802. case KVM_S390_VM_MEM_CLR_CMMA:
  1803. ret = sclp.has_cmma ? 0 : -ENXIO;
  1804. break;
  1805. case KVM_S390_VM_MEM_LIMIT_SIZE:
  1806. ret = 0;
  1807. break;
  1808. default:
  1809. ret = -ENXIO;
  1810. break;
  1811. }
  1812. break;
  1813. case KVM_S390_VM_TOD:
  1814. switch (attr->attr) {
  1815. case KVM_S390_VM_TOD_LOW:
  1816. case KVM_S390_VM_TOD_HIGH:
  1817. ret = 0;
  1818. break;
  1819. default:
  1820. ret = -ENXIO;
  1821. break;
  1822. }
  1823. break;
  1824. case KVM_S390_VM_CPU_MODEL:
  1825. switch (attr->attr) {
  1826. case KVM_S390_VM_CPU_PROCESSOR:
  1827. case KVM_S390_VM_CPU_MACHINE:
  1828. case KVM_S390_VM_CPU_PROCESSOR_FEAT:
  1829. case KVM_S390_VM_CPU_MACHINE_FEAT:
  1830. case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
  1831. case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
  1832. case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
  1833. case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
  1834. ret = 0;
  1835. break;
  1836. default:
  1837. ret = -ENXIO;
  1838. break;
  1839. }
  1840. break;
  1841. case KVM_S390_VM_CRYPTO:
  1842. switch (attr->attr) {
  1843. case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
  1844. case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
  1845. case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
  1846. case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
  1847. ret = 0;
  1848. break;
  1849. case KVM_S390_VM_CRYPTO_ENABLE_APIE:
  1850. case KVM_S390_VM_CRYPTO_DISABLE_APIE:
  1851. ret = ap_instructions_available() ? 0 : -ENXIO;
  1852. break;
  1853. default:
  1854. ret = -ENXIO;
  1855. break;
  1856. }
  1857. break;
  1858. case KVM_S390_VM_MIGRATION:
  1859. ret = 0;
  1860. break;
  1861. case KVM_S390_VM_CPU_TOPOLOGY:
  1862. ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO;
  1863. break;
  1864. default:
  1865. ret = -ENXIO;
  1866. break;
  1867. }
  1868. return ret;
  1869. }
  1870. static int kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
  1871. {
  1872. uint8_t *keys;
  1873. uint64_t hva;
  1874. int srcu_idx, i, r = 0;
  1875. if (args->flags != 0)
  1876. return -EINVAL;
  1877. /* Is this guest using storage keys? */
  1878. if (!mm_uses_skeys(current->mm))
  1879. return KVM_S390_GET_SKEYS_NONE;
  1880. /* Enforce sane limit on memory allocation */
  1881. if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
  1882. return -EINVAL;
  1883. keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
  1884. if (!keys)
  1885. return -ENOMEM;
  1886. mmap_read_lock(current->mm);
  1887. srcu_idx = srcu_read_lock(&kvm->srcu);
  1888. for (i = 0; i < args->count; i++) {
  1889. hva = gfn_to_hva(kvm, args->start_gfn + i);
  1890. if (kvm_is_error_hva(hva)) {
  1891. r = -EFAULT;
  1892. break;
  1893. }
  1894. r = get_guest_storage_key(current->mm, hva, &keys[i]);
  1895. if (r)
  1896. break;
  1897. }
  1898. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1899. mmap_read_unlock(current->mm);
  1900. if (!r) {
  1901. r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
  1902. sizeof(uint8_t) * args->count);
  1903. if (r)
  1904. r = -EFAULT;
  1905. }
  1906. kvfree(keys);
  1907. return r;
  1908. }
  1909. static int kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
  1910. {
  1911. uint8_t *keys;
  1912. uint64_t hva;
  1913. int srcu_idx, i, r = 0;
  1914. bool unlocked;
  1915. if (args->flags != 0)
  1916. return -EINVAL;
  1917. /* Enforce sane limit on memory allocation */
  1918. if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
  1919. return -EINVAL;
  1920. keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
  1921. if (!keys)
  1922. return -ENOMEM;
  1923. r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
  1924. sizeof(uint8_t) * args->count);
  1925. if (r) {
  1926. r = -EFAULT;
  1927. goto out;
  1928. }
  1929. /* Enable storage key handling for the guest */
  1930. r = s390_enable_skey();
  1931. if (r)
  1932. goto out;
  1933. i = 0;
  1934. mmap_read_lock(current->mm);
  1935. srcu_idx = srcu_read_lock(&kvm->srcu);
  1936. while (i < args->count) {
  1937. unlocked = false;
  1938. hva = gfn_to_hva(kvm, args->start_gfn + i);
  1939. if (kvm_is_error_hva(hva)) {
  1940. r = -EFAULT;
  1941. break;
  1942. }
  1943. /* Lowest order bit is reserved */
  1944. if (keys[i] & 0x01) {
  1945. r = -EINVAL;
  1946. break;
  1947. }
  1948. r = set_guest_storage_key(current->mm, hva, keys[i], 0);
  1949. if (r) {
  1950. r = fixup_user_fault(current->mm, hva,
  1951. FAULT_FLAG_WRITE, &unlocked);
  1952. if (r)
  1953. break;
  1954. }
  1955. if (!r)
  1956. i++;
  1957. }
  1958. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1959. mmap_read_unlock(current->mm);
  1960. out:
  1961. kvfree(keys);
  1962. return r;
  1963. }
  1964. /*
  1965. * Base address and length must be sent at the start of each block, therefore
  1966. * it's cheaper to send some clean data, as long as it's less than the size of
  1967. * two longs.
  1968. */
  1969. #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
  1970. /* for consistency */
  1971. #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
  1972. static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
  1973. u8 *res, unsigned long bufsize)
  1974. {
  1975. unsigned long pgstev, hva, cur_gfn = args->start_gfn;
  1976. args->count = 0;
  1977. while (args->count < bufsize) {
  1978. hva = gfn_to_hva(kvm, cur_gfn);
  1979. /*
  1980. * We return an error if the first value was invalid, but we
  1981. * return successfully if at least one value was copied.
  1982. */
  1983. if (kvm_is_error_hva(hva))
  1984. return args->count ? 0 : -EFAULT;
  1985. if (get_pgste(kvm->mm, hva, &pgstev) < 0)
  1986. pgstev = 0;
  1987. res[args->count++] = (pgstev >> 24) & 0x43;
  1988. cur_gfn++;
  1989. }
  1990. return 0;
  1991. }
  1992. static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots,
  1993. gfn_t gfn)
  1994. {
  1995. return ____gfn_to_memslot(slots, gfn, true);
  1996. }
  1997. static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
  1998. unsigned long cur_gfn)
  1999. {
  2000. struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn);
  2001. unsigned long ofs = cur_gfn - ms->base_gfn;
  2002. struct rb_node *mnode = &ms->gfn_node[slots->node_idx];
  2003. if (ms->base_gfn + ms->npages <= cur_gfn) {
  2004. mnode = rb_next(mnode);
  2005. /* If we are above the highest slot, wrap around */
  2006. if (!mnode)
  2007. mnode = rb_first(&slots->gfn_tree);
  2008. ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
  2009. ofs = 0;
  2010. }
  2011. if (cur_gfn < ms->base_gfn)
  2012. ofs = 0;
  2013. ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
  2014. while (ofs >= ms->npages && (mnode = rb_next(mnode))) {
  2015. ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
  2016. ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages);
  2017. }
  2018. return ms->base_gfn + ofs;
  2019. }
  2020. static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
  2021. u8 *res, unsigned long bufsize)
  2022. {
  2023. unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
  2024. struct kvm_memslots *slots = kvm_memslots(kvm);
  2025. struct kvm_memory_slot *ms;
  2026. if (unlikely(kvm_memslots_empty(slots)))
  2027. return 0;
  2028. cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
  2029. ms = gfn_to_memslot(kvm, cur_gfn);
  2030. args->count = 0;
  2031. args->start_gfn = cur_gfn;
  2032. if (!ms)
  2033. return 0;
  2034. next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
  2035. mem_end = kvm_s390_get_gfn_end(slots);
  2036. while (args->count < bufsize) {
  2037. hva = gfn_to_hva(kvm, cur_gfn);
  2038. if (kvm_is_error_hva(hva))
  2039. return 0;
  2040. /* Decrement only if we actually flipped the bit to 0 */
  2041. if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
  2042. atomic64_dec(&kvm->arch.cmma_dirty_pages);
  2043. if (get_pgste(kvm->mm, hva, &pgstev) < 0)
  2044. pgstev = 0;
  2045. /* Save the value */
  2046. res[args->count++] = (pgstev >> 24) & 0x43;
  2047. /* If the next bit is too far away, stop. */
  2048. if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
  2049. return 0;
  2050. /* If we reached the previous "next", find the next one */
  2051. if (cur_gfn == next_gfn)
  2052. next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
  2053. /* Reached the end of memory or of the buffer, stop */
  2054. if ((next_gfn >= mem_end) ||
  2055. (next_gfn - args->start_gfn >= bufsize))
  2056. return 0;
  2057. cur_gfn++;
  2058. /* Reached the end of the current memslot, take the next one. */
  2059. if (cur_gfn - ms->base_gfn >= ms->npages) {
  2060. ms = gfn_to_memslot(kvm, cur_gfn);
  2061. if (!ms)
  2062. return 0;
  2063. }
  2064. }
  2065. return 0;
  2066. }
  2067. /*
  2068. * This function searches for the next page with dirty CMMA attributes, and
  2069. * saves the attributes in the buffer up to either the end of the buffer or
  2070. * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
  2071. * no trailing clean bytes are saved.
  2072. * In case no dirty bits were found, or if CMMA was not enabled or used, the
  2073. * output buffer will indicate 0 as length.
  2074. */
  2075. static int kvm_s390_get_cmma_bits(struct kvm *kvm,
  2076. struct kvm_s390_cmma_log *args)
  2077. {
  2078. unsigned long bufsize;
  2079. int srcu_idx, peek, ret;
  2080. u8 *values;
  2081. if (!kvm->arch.use_cmma)
  2082. return -ENXIO;
  2083. /* Invalid/unsupported flags were specified */
  2084. if (args->flags & ~KVM_S390_CMMA_PEEK)
  2085. return -EINVAL;
  2086. /* Migration mode query, and we are not doing a migration */
  2087. peek = !!(args->flags & KVM_S390_CMMA_PEEK);
  2088. if (!peek && !kvm->arch.migration_mode)
  2089. return -EINVAL;
  2090. /* CMMA is disabled or was not used, or the buffer has length zero */
  2091. bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
  2092. if (!bufsize || !kvm->mm->context.uses_cmm) {
  2093. memset(args, 0, sizeof(*args));
  2094. return 0;
  2095. }
  2096. /* We are not peeking, and there are no dirty pages */
  2097. if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
  2098. memset(args, 0, sizeof(*args));
  2099. return 0;
  2100. }
  2101. values = vmalloc(bufsize);
  2102. if (!values)
  2103. return -ENOMEM;
  2104. mmap_read_lock(kvm->mm);
  2105. srcu_idx = srcu_read_lock(&kvm->srcu);
  2106. if (peek)
  2107. ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
  2108. else
  2109. ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
  2110. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2111. mmap_read_unlock(kvm->mm);
  2112. if (kvm->arch.migration_mode)
  2113. args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
  2114. else
  2115. args->remaining = 0;
  2116. if (copy_to_user((void __user *)args->values, values, args->count))
  2117. ret = -EFAULT;
  2118. vfree(values);
  2119. return ret;
  2120. }
  2121. /*
  2122. * This function sets the CMMA attributes for the given pages. If the input
  2123. * buffer has zero length, no action is taken, otherwise the attributes are
  2124. * set and the mm->context.uses_cmm flag is set.
  2125. */
  2126. static int kvm_s390_set_cmma_bits(struct kvm *kvm,
  2127. const struct kvm_s390_cmma_log *args)
  2128. {
  2129. unsigned long hva, mask, pgstev, i;
  2130. uint8_t *bits;
  2131. int srcu_idx, r = 0;
  2132. mask = args->mask;
  2133. if (!kvm->arch.use_cmma)
  2134. return -ENXIO;
  2135. /* invalid/unsupported flags */
  2136. if (args->flags != 0)
  2137. return -EINVAL;
  2138. /* Enforce sane limit on memory allocation */
  2139. if (args->count > KVM_S390_CMMA_SIZE_MAX)
  2140. return -EINVAL;
  2141. /* Nothing to do */
  2142. if (args->count == 0)
  2143. return 0;
  2144. bits = vmalloc(array_size(sizeof(*bits), args->count));
  2145. if (!bits)
  2146. return -ENOMEM;
  2147. r = copy_from_user(bits, (void __user *)args->values, args->count);
  2148. if (r) {
  2149. r = -EFAULT;
  2150. goto out;
  2151. }
  2152. mmap_read_lock(kvm->mm);
  2153. srcu_idx = srcu_read_lock(&kvm->srcu);
  2154. for (i = 0; i < args->count; i++) {
  2155. hva = gfn_to_hva(kvm, args->start_gfn + i);
  2156. if (kvm_is_error_hva(hva)) {
  2157. r = -EFAULT;
  2158. break;
  2159. }
  2160. pgstev = bits[i];
  2161. pgstev = pgstev << 24;
  2162. mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
  2163. set_pgste_bits(kvm->mm, hva, mask, pgstev);
  2164. }
  2165. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2166. mmap_read_unlock(kvm->mm);
  2167. if (!kvm->mm->context.uses_cmm) {
  2168. mmap_write_lock(kvm->mm);
  2169. kvm->mm->context.uses_cmm = 1;
  2170. mmap_write_unlock(kvm->mm);
  2171. }
  2172. out:
  2173. vfree(bits);
  2174. return r;
  2175. }
  2176. /**
  2177. * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to
  2178. * non protected.
  2179. * @kvm: the VM whose protected vCPUs are to be converted
  2180. * @rc: return value for the RC field of the UVC (in case of error)
  2181. * @rrc: return value for the RRC field of the UVC (in case of error)
  2182. *
  2183. * Does not stop in case of error, tries to convert as many
  2184. * CPUs as possible. In case of error, the RC and RRC of the last error are
  2185. * returned.
  2186. *
  2187. * Return: 0 in case of success, otherwise -EIO
  2188. */
  2189. int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
  2190. {
  2191. struct kvm_vcpu *vcpu;
  2192. unsigned long i;
  2193. u16 _rc, _rrc;
  2194. int ret = 0;
  2195. /*
  2196. * We ignore failures and try to destroy as many CPUs as possible.
  2197. * At the same time we must not free the assigned resources when
  2198. * this fails, as the ultravisor has still access to that memory.
  2199. * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
  2200. * behind.
  2201. * We want to return the first failure rc and rrc, though.
  2202. */
  2203. kvm_for_each_vcpu(i, vcpu, kvm) {
  2204. mutex_lock(&vcpu->mutex);
  2205. if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) {
  2206. *rc = _rc;
  2207. *rrc = _rrc;
  2208. ret = -EIO;
  2209. }
  2210. mutex_unlock(&vcpu->mutex);
  2211. }
  2212. /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */
  2213. if (use_gisa)
  2214. kvm_s390_gisa_enable(kvm);
  2215. return ret;
  2216. }
  2217. /**
  2218. * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM
  2219. * to protected.
  2220. * @kvm: the VM whose protected vCPUs are to be converted
  2221. * @rc: return value for the RC field of the UVC (in case of error)
  2222. * @rrc: return value for the RRC field of the UVC (in case of error)
  2223. *
  2224. * Tries to undo the conversion in case of error.
  2225. *
  2226. * Return: 0 in case of success, otherwise -EIO
  2227. */
  2228. static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
  2229. {
  2230. unsigned long i;
  2231. int r = 0;
  2232. u16 dummy;
  2233. struct kvm_vcpu *vcpu;
  2234. /* Disable the GISA if the ultravisor does not support AIV. */
  2235. if (!uv_has_feature(BIT_UV_FEAT_AIV))
  2236. kvm_s390_gisa_disable(kvm);
  2237. kvm_for_each_vcpu(i, vcpu, kvm) {
  2238. mutex_lock(&vcpu->mutex);
  2239. r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
  2240. mutex_unlock(&vcpu->mutex);
  2241. if (r)
  2242. break;
  2243. }
  2244. if (r)
  2245. kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
  2246. return r;
  2247. }
  2248. /*
  2249. * Here we provide user space with a direct interface to query UV
  2250. * related data like UV maxima and available features as well as
  2251. * feature specific data.
  2252. *
  2253. * To facilitate future extension of the data structures we'll try to
  2254. * write data up to the maximum requested length.
  2255. */
  2256. static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info)
  2257. {
  2258. ssize_t len_min;
  2259. switch (info->header.id) {
  2260. case KVM_PV_INFO_VM: {
  2261. len_min = sizeof(info->header) + sizeof(info->vm);
  2262. if (info->header.len_max < len_min)
  2263. return -EINVAL;
  2264. memcpy(info->vm.inst_calls_list,
  2265. uv_info.inst_calls_list,
  2266. sizeof(uv_info.inst_calls_list));
  2267. /* It's max cpuid not max cpus, so it's off by one */
  2268. info->vm.max_cpus = uv_info.max_guest_cpu_id + 1;
  2269. info->vm.max_guests = uv_info.max_num_sec_conf;
  2270. info->vm.max_guest_addr = uv_info.max_sec_stor_addr;
  2271. info->vm.feature_indication = uv_info.uv_feature_indications;
  2272. return len_min;
  2273. }
  2274. case KVM_PV_INFO_DUMP: {
  2275. len_min = sizeof(info->header) + sizeof(info->dump);
  2276. if (info->header.len_max < len_min)
  2277. return -EINVAL;
  2278. info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len;
  2279. info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len;
  2280. info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len;
  2281. return len_min;
  2282. }
  2283. default:
  2284. return -EINVAL;
  2285. }
  2286. }
  2287. static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
  2288. struct kvm_s390_pv_dmp dmp)
  2289. {
  2290. int r = -EINVAL;
  2291. void __user *result_buff = (void __user *)dmp.buff_addr;
  2292. switch (dmp.subcmd) {
  2293. case KVM_PV_DUMP_INIT: {
  2294. if (kvm->arch.pv.dumping)
  2295. break;
  2296. /*
  2297. * Block SIE entry as concurrent dump UVCs could lead
  2298. * to validities.
  2299. */
  2300. kvm_s390_vcpu_block_all(kvm);
  2301. r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
  2302. UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc);
  2303. KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x",
  2304. cmd->rc, cmd->rrc);
  2305. if (!r) {
  2306. kvm->arch.pv.dumping = true;
  2307. } else {
  2308. kvm_s390_vcpu_unblock_all(kvm);
  2309. r = -EINVAL;
  2310. }
  2311. break;
  2312. }
  2313. case KVM_PV_DUMP_CONFIG_STOR_STATE: {
  2314. if (!kvm->arch.pv.dumping)
  2315. break;
  2316. /*
  2317. * gaddr is an output parameter since we might stop
  2318. * early. As dmp will be copied back in our caller, we
  2319. * don't need to do it ourselves.
  2320. */
  2321. r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len,
  2322. &cmd->rc, &cmd->rrc);
  2323. break;
  2324. }
  2325. case KVM_PV_DUMP_COMPLETE: {
  2326. if (!kvm->arch.pv.dumping)
  2327. break;
  2328. r = -EINVAL;
  2329. if (dmp.buff_len < uv_info.conf_dump_finalize_len)
  2330. break;
  2331. r = kvm_s390_pv_dump_complete(kvm, result_buff,
  2332. &cmd->rc, &cmd->rrc);
  2333. break;
  2334. }
  2335. default:
  2336. r = -ENOTTY;
  2337. break;
  2338. }
  2339. return r;
  2340. }
  2341. static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
  2342. {
  2343. const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM);
  2344. void __user *argp = (void __user *)cmd->data;
  2345. int r = 0;
  2346. u16 dummy;
  2347. if (need_lock)
  2348. mutex_lock(&kvm->lock);
  2349. switch (cmd->cmd) {
  2350. case KVM_PV_ENABLE: {
  2351. r = -EINVAL;
  2352. if (kvm_s390_pv_is_protected(kvm))
  2353. break;
  2354. /*
  2355. * FMT 4 SIE needs esca. As we never switch back to bsca from
  2356. * esca, we need no cleanup in the error cases below
  2357. */
  2358. r = sca_switch_to_extended(kvm);
  2359. if (r)
  2360. break;
  2361. r = s390_disable_cow_sharing();
  2362. if (r)
  2363. break;
  2364. r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
  2365. if (r)
  2366. break;
  2367. r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
  2368. if (r)
  2369. kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
  2370. /* we need to block service interrupts from now on */
  2371. set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
  2372. break;
  2373. }
  2374. case KVM_PV_ASYNC_CLEANUP_PREPARE:
  2375. r = -EINVAL;
  2376. if (!kvm_s390_pv_is_protected(kvm) || !async_destroy)
  2377. break;
  2378. r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
  2379. /*
  2380. * If a CPU could not be destroyed, destroy VM will also fail.
  2381. * There is no point in trying to destroy it. Instead return
  2382. * the rc and rrc from the first CPU that failed destroying.
  2383. */
  2384. if (r)
  2385. break;
  2386. r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc);
  2387. /* no need to block service interrupts any more */
  2388. clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
  2389. break;
  2390. case KVM_PV_ASYNC_CLEANUP_PERFORM:
  2391. r = -EINVAL;
  2392. if (!async_destroy)
  2393. break;
  2394. /* kvm->lock must not be held; this is asserted inside the function. */
  2395. r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc);
  2396. break;
  2397. case KVM_PV_DISABLE: {
  2398. r = -EINVAL;
  2399. if (!kvm_s390_pv_is_protected(kvm))
  2400. break;
  2401. r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
  2402. /*
  2403. * If a CPU could not be destroyed, destroy VM will also fail.
  2404. * There is no point in trying to destroy it. Instead return
  2405. * the rc and rrc from the first CPU that failed destroying.
  2406. */
  2407. if (r)
  2408. break;
  2409. r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc);
  2410. /* no need to block service interrupts any more */
  2411. clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
  2412. break;
  2413. }
  2414. case KVM_PV_SET_SEC_PARMS: {
  2415. struct kvm_s390_pv_sec_parm parms = {};
  2416. void *hdr;
  2417. r = -EINVAL;
  2418. if (!kvm_s390_pv_is_protected(kvm))
  2419. break;
  2420. r = -EFAULT;
  2421. if (copy_from_user(&parms, argp, sizeof(parms)))
  2422. break;
  2423. /* Currently restricted to 8KB */
  2424. r = -EINVAL;
  2425. if (parms.length > PAGE_SIZE * 2)
  2426. break;
  2427. r = -ENOMEM;
  2428. hdr = vmalloc(parms.length);
  2429. if (!hdr)
  2430. break;
  2431. r = -EFAULT;
  2432. if (!copy_from_user(hdr, (void __user *)parms.origin,
  2433. parms.length))
  2434. r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
  2435. &cmd->rc, &cmd->rrc);
  2436. vfree(hdr);
  2437. break;
  2438. }
  2439. case KVM_PV_UNPACK: {
  2440. struct kvm_s390_pv_unp unp = {};
  2441. r = -EINVAL;
  2442. if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
  2443. break;
  2444. r = -EFAULT;
  2445. if (copy_from_user(&unp, argp, sizeof(unp)))
  2446. break;
  2447. r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
  2448. &cmd->rc, &cmd->rrc);
  2449. break;
  2450. }
  2451. case KVM_PV_VERIFY: {
  2452. r = -EINVAL;
  2453. if (!kvm_s390_pv_is_protected(kvm))
  2454. break;
  2455. r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
  2456. UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
  2457. KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
  2458. cmd->rrc);
  2459. break;
  2460. }
  2461. case KVM_PV_PREP_RESET: {
  2462. r = -EINVAL;
  2463. if (!kvm_s390_pv_is_protected(kvm))
  2464. break;
  2465. r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
  2466. UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
  2467. KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
  2468. cmd->rc, cmd->rrc);
  2469. break;
  2470. }
  2471. case KVM_PV_UNSHARE_ALL: {
  2472. r = -EINVAL;
  2473. if (!kvm_s390_pv_is_protected(kvm))
  2474. break;
  2475. r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
  2476. UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
  2477. KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
  2478. cmd->rc, cmd->rrc);
  2479. break;
  2480. }
  2481. case KVM_PV_INFO: {
  2482. struct kvm_s390_pv_info info = {};
  2483. ssize_t data_len;
  2484. /*
  2485. * No need to check the VM protection here.
  2486. *
  2487. * Maybe user space wants to query some of the data
  2488. * when the VM is still unprotected. If we see the
  2489. * need to fence a new data command we can still
  2490. * return an error in the info handler.
  2491. */
  2492. r = -EFAULT;
  2493. if (copy_from_user(&info, argp, sizeof(info.header)))
  2494. break;
  2495. r = -EINVAL;
  2496. if (info.header.len_max < sizeof(info.header))
  2497. break;
  2498. data_len = kvm_s390_handle_pv_info(&info);
  2499. if (data_len < 0) {
  2500. r = data_len;
  2501. break;
  2502. }
  2503. /*
  2504. * If a data command struct is extended (multiple
  2505. * times) this can be used to determine how much of it
  2506. * is valid.
  2507. */
  2508. info.header.len_written = data_len;
  2509. r = -EFAULT;
  2510. if (copy_to_user(argp, &info, data_len))
  2511. break;
  2512. r = 0;
  2513. break;
  2514. }
  2515. case KVM_PV_DUMP: {
  2516. struct kvm_s390_pv_dmp dmp;
  2517. r = -EINVAL;
  2518. if (!kvm_s390_pv_is_protected(kvm))
  2519. break;
  2520. r = -EFAULT;
  2521. if (copy_from_user(&dmp, argp, sizeof(dmp)))
  2522. break;
  2523. r = kvm_s390_pv_dmp(kvm, cmd, dmp);
  2524. if (r)
  2525. break;
  2526. if (copy_to_user(argp, &dmp, sizeof(dmp))) {
  2527. r = -EFAULT;
  2528. break;
  2529. }
  2530. break;
  2531. }
  2532. default:
  2533. r = -ENOTTY;
  2534. }
  2535. if (need_lock)
  2536. mutex_unlock(&kvm->lock);
  2537. return r;
  2538. }
  2539. static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags)
  2540. {
  2541. if (mop->flags & ~supported_flags || !mop->size)
  2542. return -EINVAL;
  2543. if (mop->size > MEM_OP_MAX_SIZE)
  2544. return -E2BIG;
  2545. if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
  2546. if (mop->key > 0xf)
  2547. return -EINVAL;
  2548. } else {
  2549. mop->key = 0;
  2550. }
  2551. return 0;
  2552. }
  2553. static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
  2554. {
  2555. void __user *uaddr = (void __user *)mop->buf;
  2556. enum gacc_mode acc_mode;
  2557. void *tmpbuf = NULL;
  2558. int r, srcu_idx;
  2559. r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION |
  2560. KVM_S390_MEMOP_F_CHECK_ONLY);
  2561. if (r)
  2562. return r;
  2563. if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
  2564. tmpbuf = vmalloc(mop->size);
  2565. if (!tmpbuf)
  2566. return -ENOMEM;
  2567. }
  2568. srcu_idx = srcu_read_lock(&kvm->srcu);
  2569. if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr)) {
  2570. r = PGM_ADDRESSING;
  2571. goto out_unlock;
  2572. }
  2573. acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
  2574. if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
  2575. r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
  2576. goto out_unlock;
  2577. }
  2578. if (acc_mode == GACC_FETCH) {
  2579. r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
  2580. mop->size, GACC_FETCH, mop->key);
  2581. if (r)
  2582. goto out_unlock;
  2583. if (copy_to_user(uaddr, tmpbuf, mop->size))
  2584. r = -EFAULT;
  2585. } else {
  2586. if (copy_from_user(tmpbuf, uaddr, mop->size)) {
  2587. r = -EFAULT;
  2588. goto out_unlock;
  2589. }
  2590. r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
  2591. mop->size, GACC_STORE, mop->key);
  2592. }
  2593. out_unlock:
  2594. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2595. vfree(tmpbuf);
  2596. return r;
  2597. }
  2598. static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop)
  2599. {
  2600. void __user *uaddr = (void __user *)mop->buf;
  2601. void __user *old_addr = (void __user *)mop->old_addr;
  2602. union {
  2603. __uint128_t quad;
  2604. char raw[sizeof(__uint128_t)];
  2605. } old = { .quad = 0}, new = { .quad = 0 };
  2606. unsigned int off_in_quad = sizeof(new) - mop->size;
  2607. int r, srcu_idx;
  2608. bool success;
  2609. r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION);
  2610. if (r)
  2611. return r;
  2612. /*
  2613. * This validates off_in_quad. Checking that size is a power
  2614. * of two is not necessary, as cmpxchg_guest_abs_with_key
  2615. * takes care of that
  2616. */
  2617. if (mop->size > sizeof(new))
  2618. return -EINVAL;
  2619. if (copy_from_user(&new.raw[off_in_quad], uaddr, mop->size))
  2620. return -EFAULT;
  2621. if (copy_from_user(&old.raw[off_in_quad], old_addr, mop->size))
  2622. return -EFAULT;
  2623. srcu_idx = srcu_read_lock(&kvm->srcu);
  2624. if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr)) {
  2625. r = PGM_ADDRESSING;
  2626. goto out_unlock;
  2627. }
  2628. r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old.quad,
  2629. new.quad, mop->key, &success);
  2630. if (!success && copy_to_user(old_addr, &old.raw[off_in_quad], mop->size))
  2631. r = -EFAULT;
  2632. out_unlock:
  2633. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2634. return r;
  2635. }
  2636. static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
  2637. {
  2638. /*
  2639. * This is technically a heuristic only, if the kvm->lock is not
  2640. * taken, it is not guaranteed that the vm is/remains non-protected.
  2641. * This is ok from a kernel perspective, wrongdoing is detected
  2642. * on the access, -EFAULT is returned and the vm may crash the
  2643. * next time it accesses the memory in question.
  2644. * There is no sane usecase to do switching and a memop on two
  2645. * different CPUs at the same time.
  2646. */
  2647. if (kvm_s390_pv_get_handle(kvm))
  2648. return -EINVAL;
  2649. switch (mop->op) {
  2650. case KVM_S390_MEMOP_ABSOLUTE_READ:
  2651. case KVM_S390_MEMOP_ABSOLUTE_WRITE:
  2652. return kvm_s390_vm_mem_op_abs(kvm, mop);
  2653. case KVM_S390_MEMOP_ABSOLUTE_CMPXCHG:
  2654. return kvm_s390_vm_mem_op_cmpxchg(kvm, mop);
  2655. default:
  2656. return -EINVAL;
  2657. }
  2658. }
  2659. int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  2660. {
  2661. struct kvm *kvm = filp->private_data;
  2662. void __user *argp = (void __user *)arg;
  2663. struct kvm_device_attr attr;
  2664. int r;
  2665. switch (ioctl) {
  2666. case KVM_S390_INTERRUPT: {
  2667. struct kvm_s390_interrupt s390int;
  2668. r = -EFAULT;
  2669. if (copy_from_user(&s390int, argp, sizeof(s390int)))
  2670. break;
  2671. r = kvm_s390_inject_vm(kvm, &s390int);
  2672. break;
  2673. }
  2674. case KVM_CREATE_IRQCHIP: {
  2675. r = -EINVAL;
  2676. if (kvm->arch.use_irqchip)
  2677. r = 0;
  2678. break;
  2679. }
  2680. case KVM_SET_DEVICE_ATTR: {
  2681. r = -EFAULT;
  2682. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  2683. break;
  2684. r = kvm_s390_vm_set_attr(kvm, &attr);
  2685. break;
  2686. }
  2687. case KVM_GET_DEVICE_ATTR: {
  2688. r = -EFAULT;
  2689. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  2690. break;
  2691. r = kvm_s390_vm_get_attr(kvm, &attr);
  2692. break;
  2693. }
  2694. case KVM_HAS_DEVICE_ATTR: {
  2695. r = -EFAULT;
  2696. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  2697. break;
  2698. r = kvm_s390_vm_has_attr(kvm, &attr);
  2699. break;
  2700. }
  2701. case KVM_S390_GET_SKEYS: {
  2702. struct kvm_s390_skeys args;
  2703. r = -EFAULT;
  2704. if (copy_from_user(&args, argp,
  2705. sizeof(struct kvm_s390_skeys)))
  2706. break;
  2707. r = kvm_s390_get_skeys(kvm, &args);
  2708. break;
  2709. }
  2710. case KVM_S390_SET_SKEYS: {
  2711. struct kvm_s390_skeys args;
  2712. r = -EFAULT;
  2713. if (copy_from_user(&args, argp,
  2714. sizeof(struct kvm_s390_skeys)))
  2715. break;
  2716. r = kvm_s390_set_skeys(kvm, &args);
  2717. break;
  2718. }
  2719. case KVM_S390_GET_CMMA_BITS: {
  2720. struct kvm_s390_cmma_log args;
  2721. r = -EFAULT;
  2722. if (copy_from_user(&args, argp, sizeof(args)))
  2723. break;
  2724. mutex_lock(&kvm->slots_lock);
  2725. r = kvm_s390_get_cmma_bits(kvm, &args);
  2726. mutex_unlock(&kvm->slots_lock);
  2727. if (!r) {
  2728. r = copy_to_user(argp, &args, sizeof(args));
  2729. if (r)
  2730. r = -EFAULT;
  2731. }
  2732. break;
  2733. }
  2734. case KVM_S390_SET_CMMA_BITS: {
  2735. struct kvm_s390_cmma_log args;
  2736. r = -EFAULT;
  2737. if (copy_from_user(&args, argp, sizeof(args)))
  2738. break;
  2739. mutex_lock(&kvm->slots_lock);
  2740. r = kvm_s390_set_cmma_bits(kvm, &args);
  2741. mutex_unlock(&kvm->slots_lock);
  2742. break;
  2743. }
  2744. case KVM_S390_PV_COMMAND: {
  2745. struct kvm_pv_cmd args;
  2746. /* protvirt means user cpu state */
  2747. kvm_s390_set_user_cpu_state_ctrl(kvm);
  2748. r = 0;
  2749. if (!is_prot_virt_host()) {
  2750. r = -EINVAL;
  2751. break;
  2752. }
  2753. if (copy_from_user(&args, argp, sizeof(args))) {
  2754. r = -EFAULT;
  2755. break;
  2756. }
  2757. if (args.flags) {
  2758. r = -EINVAL;
  2759. break;
  2760. }
  2761. /* must be called without kvm->lock */
  2762. r = kvm_s390_handle_pv(kvm, &args);
  2763. if (copy_to_user(argp, &args, sizeof(args))) {
  2764. r = -EFAULT;
  2765. break;
  2766. }
  2767. break;
  2768. }
  2769. case KVM_S390_MEM_OP: {
  2770. struct kvm_s390_mem_op mem_op;
  2771. if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
  2772. r = kvm_s390_vm_mem_op(kvm, &mem_op);
  2773. else
  2774. r = -EFAULT;
  2775. break;
  2776. }
  2777. case KVM_S390_ZPCI_OP: {
  2778. struct kvm_s390_zpci_op args;
  2779. r = -EINVAL;
  2780. if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
  2781. break;
  2782. if (copy_from_user(&args, argp, sizeof(args))) {
  2783. r = -EFAULT;
  2784. break;
  2785. }
  2786. r = kvm_s390_pci_zpci_op(kvm, &args);
  2787. break;
  2788. }
  2789. default:
  2790. r = -ENOTTY;
  2791. }
  2792. return r;
  2793. }
  2794. static int kvm_s390_apxa_installed(void)
  2795. {
  2796. struct ap_config_info info;
  2797. if (ap_instructions_available()) {
  2798. if (ap_qci(&info) == 0)
  2799. return info.apxa;
  2800. }
  2801. return 0;
  2802. }
  2803. /*
  2804. * The format of the crypto control block (CRYCB) is specified in the 3 low
  2805. * order bits of the CRYCB designation (CRYCBD) field as follows:
  2806. * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
  2807. * AP extended addressing (APXA) facility are installed.
  2808. * Format 1: The APXA facility is not installed but the MSAX3 facility is.
  2809. * Format 2: Both the APXA and MSAX3 facilities are installed
  2810. */
  2811. static void kvm_s390_set_crycb_format(struct kvm *kvm)
  2812. {
  2813. kvm->arch.crypto.crycbd = virt_to_phys(kvm->arch.crypto.crycb);
  2814. /* Clear the CRYCB format bits - i.e., set format 0 by default */
  2815. kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
  2816. /* Check whether MSAX3 is installed */
  2817. if (!test_kvm_facility(kvm, 76))
  2818. return;
  2819. if (kvm_s390_apxa_installed())
  2820. kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
  2821. else
  2822. kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
  2823. }
  2824. /*
  2825. * kvm_arch_crypto_set_masks
  2826. *
  2827. * @kvm: pointer to the target guest's KVM struct containing the crypto masks
  2828. * to be set.
  2829. * @apm: the mask identifying the accessible AP adapters
  2830. * @aqm: the mask identifying the accessible AP domains
  2831. * @adm: the mask identifying the accessible AP control domains
  2832. *
  2833. * Set the masks that identify the adapters, domains and control domains to
  2834. * which the KVM guest is granted access.
  2835. *
  2836. * Note: The kvm->lock mutex must be locked by the caller before invoking this
  2837. * function.
  2838. */
  2839. void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
  2840. unsigned long *aqm, unsigned long *adm)
  2841. {
  2842. struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
  2843. kvm_s390_vcpu_block_all(kvm);
  2844. switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
  2845. case CRYCB_FORMAT2: /* APCB1 use 256 bits */
  2846. memcpy(crycb->apcb1.apm, apm, 32);
  2847. VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
  2848. apm[0], apm[1], apm[2], apm[3]);
  2849. memcpy(crycb->apcb1.aqm, aqm, 32);
  2850. VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
  2851. aqm[0], aqm[1], aqm[2], aqm[3]);
  2852. memcpy(crycb->apcb1.adm, adm, 32);
  2853. VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
  2854. adm[0], adm[1], adm[2], adm[3]);
  2855. break;
  2856. case CRYCB_FORMAT1:
  2857. case CRYCB_FORMAT0: /* Fall through both use APCB0 */
  2858. memcpy(crycb->apcb0.apm, apm, 8);
  2859. memcpy(crycb->apcb0.aqm, aqm, 2);
  2860. memcpy(crycb->apcb0.adm, adm, 2);
  2861. VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
  2862. apm[0], *((unsigned short *)aqm),
  2863. *((unsigned short *)adm));
  2864. break;
  2865. default: /* Can not happen */
  2866. break;
  2867. }
  2868. /* recreate the shadow crycb for each vcpu */
  2869. kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
  2870. kvm_s390_vcpu_unblock_all(kvm);
  2871. }
  2872. EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
  2873. /*
  2874. * kvm_arch_crypto_clear_masks
  2875. *
  2876. * @kvm: pointer to the target guest's KVM struct containing the crypto masks
  2877. * to be cleared.
  2878. *
  2879. * Clear the masks that identify the adapters, domains and control domains to
  2880. * which the KVM guest is granted access.
  2881. *
  2882. * Note: The kvm->lock mutex must be locked by the caller before invoking this
  2883. * function.
  2884. */
  2885. void kvm_arch_crypto_clear_masks(struct kvm *kvm)
  2886. {
  2887. kvm_s390_vcpu_block_all(kvm);
  2888. memset(&kvm->arch.crypto.crycb->apcb0, 0,
  2889. sizeof(kvm->arch.crypto.crycb->apcb0));
  2890. memset(&kvm->arch.crypto.crycb->apcb1, 0,
  2891. sizeof(kvm->arch.crypto.crycb->apcb1));
  2892. VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
  2893. /* recreate the shadow crycb for each vcpu */
  2894. kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
  2895. kvm_s390_vcpu_unblock_all(kvm);
  2896. }
  2897. EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
  2898. static u64 kvm_s390_get_initial_cpuid(void)
  2899. {
  2900. struct cpuid cpuid;
  2901. get_cpu_id(&cpuid);
  2902. cpuid.version = 0xff;
  2903. return *((u64 *) &cpuid);
  2904. }
  2905. static void kvm_s390_crypto_init(struct kvm *kvm)
  2906. {
  2907. kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
  2908. kvm_s390_set_crycb_format(kvm);
  2909. init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
  2910. if (!test_kvm_facility(kvm, 76))
  2911. return;
  2912. /* Enable AES/DEA protected key functions by default */
  2913. kvm->arch.crypto.aes_kw = 1;
  2914. kvm->arch.crypto.dea_kw = 1;
  2915. get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
  2916. sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
  2917. get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
  2918. sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
  2919. }
  2920. static void sca_dispose(struct kvm *kvm)
  2921. {
  2922. if (kvm->arch.use_esca)
  2923. free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
  2924. else
  2925. free_page((unsigned long)(kvm->arch.sca));
  2926. kvm->arch.sca = NULL;
  2927. }
  2928. void kvm_arch_free_vm(struct kvm *kvm)
  2929. {
  2930. if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
  2931. kvm_s390_pci_clear_list(kvm);
  2932. __kvm_arch_free_vm(kvm);
  2933. }
  2934. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  2935. {
  2936. gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
  2937. int i, rc;
  2938. char debug_name[16];
  2939. static unsigned long sca_offset;
  2940. rc = -EINVAL;
  2941. #ifdef CONFIG_KVM_S390_UCONTROL
  2942. if (type & ~KVM_VM_S390_UCONTROL)
  2943. goto out_err;
  2944. if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
  2945. goto out_err;
  2946. #else
  2947. if (type)
  2948. goto out_err;
  2949. #endif
  2950. rc = s390_enable_sie();
  2951. if (rc)
  2952. goto out_err;
  2953. rc = -ENOMEM;
  2954. if (!sclp.has_64bscao)
  2955. alloc_flags |= GFP_DMA;
  2956. rwlock_init(&kvm->arch.sca_lock);
  2957. /* start with basic SCA */
  2958. kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
  2959. if (!kvm->arch.sca)
  2960. goto out_err;
  2961. mutex_lock(&kvm_lock);
  2962. sca_offset += 16;
  2963. if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
  2964. sca_offset = 0;
  2965. kvm->arch.sca = (struct bsca_block *)
  2966. ((char *) kvm->arch.sca + sca_offset);
  2967. mutex_unlock(&kvm_lock);
  2968. sprintf(debug_name, "kvm-%u", current->pid);
  2969. kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
  2970. if (!kvm->arch.dbf)
  2971. goto out_err;
  2972. BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
  2973. kvm->arch.sie_page2 =
  2974. (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
  2975. if (!kvm->arch.sie_page2)
  2976. goto out_err;
  2977. kvm->arch.sie_page2->kvm = kvm;
  2978. kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
  2979. for (i = 0; i < kvm_s390_fac_size(); i++) {
  2980. kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
  2981. (kvm_s390_fac_base[i] |
  2982. kvm_s390_fac_ext[i]);
  2983. kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
  2984. kvm_s390_fac_base[i];
  2985. }
  2986. kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
  2987. /* we are always in czam mode - even on pre z14 machines */
  2988. set_kvm_facility(kvm->arch.model.fac_mask, 138);
  2989. set_kvm_facility(kvm->arch.model.fac_list, 138);
  2990. /* we emulate STHYI in kvm */
  2991. set_kvm_facility(kvm->arch.model.fac_mask, 74);
  2992. set_kvm_facility(kvm->arch.model.fac_list, 74);
  2993. if (MACHINE_HAS_TLB_GUEST) {
  2994. set_kvm_facility(kvm->arch.model.fac_mask, 147);
  2995. set_kvm_facility(kvm->arch.model.fac_list, 147);
  2996. }
  2997. if (css_general_characteristics.aiv && test_facility(65))
  2998. set_kvm_facility(kvm->arch.model.fac_mask, 65);
  2999. kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
  3000. kvm->arch.model.ibc = sclp.ibc & 0x0fff;
  3001. kvm->arch.model.uv_feat_guest.feat = 0;
  3002. kvm_s390_crypto_init(kvm);
  3003. if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
  3004. mutex_lock(&kvm->lock);
  3005. kvm_s390_pci_init_list(kvm);
  3006. kvm_s390_vcpu_pci_enable_interp(kvm);
  3007. mutex_unlock(&kvm->lock);
  3008. }
  3009. mutex_init(&kvm->arch.float_int.ais_lock);
  3010. spin_lock_init(&kvm->arch.float_int.lock);
  3011. for (i = 0; i < FIRQ_LIST_COUNT; i++)
  3012. INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
  3013. init_waitqueue_head(&kvm->arch.ipte_wq);
  3014. mutex_init(&kvm->arch.ipte_mutex);
  3015. debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
  3016. VM_EVENT(kvm, 3, "vm created with type %lu", type);
  3017. if (type & KVM_VM_S390_UCONTROL) {
  3018. kvm->arch.gmap = NULL;
  3019. kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
  3020. } else {
  3021. if (sclp.hamax == U64_MAX)
  3022. kvm->arch.mem_limit = TASK_SIZE_MAX;
  3023. else
  3024. kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
  3025. sclp.hamax + 1);
  3026. kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
  3027. if (!kvm->arch.gmap)
  3028. goto out_err;
  3029. kvm->arch.gmap->private = kvm;
  3030. kvm->arch.gmap->pfault_enabled = 0;
  3031. }
  3032. kvm->arch.use_pfmfi = sclp.has_pfmfi;
  3033. kvm->arch.use_skf = sclp.has_skey;
  3034. spin_lock_init(&kvm->arch.start_stop_lock);
  3035. kvm_s390_vsie_init(kvm);
  3036. if (use_gisa)
  3037. kvm_s390_gisa_init(kvm);
  3038. INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup);
  3039. kvm->arch.pv.set_aside = NULL;
  3040. KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
  3041. return 0;
  3042. out_err:
  3043. free_page((unsigned long)kvm->arch.sie_page2);
  3044. debug_unregister(kvm->arch.dbf);
  3045. sca_dispose(kvm);
  3046. KVM_EVENT(3, "creation of vm failed: %d", rc);
  3047. return rc;
  3048. }
  3049. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  3050. {
  3051. u16 rc, rrc;
  3052. VCPU_EVENT(vcpu, 3, "%s", "free cpu");
  3053. trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
  3054. kvm_s390_clear_local_irqs(vcpu);
  3055. kvm_clear_async_pf_completion_queue(vcpu);
  3056. if (!kvm_is_ucontrol(vcpu->kvm))
  3057. sca_del_vcpu(vcpu);
  3058. kvm_s390_update_topology_change_report(vcpu->kvm, 1);
  3059. if (kvm_is_ucontrol(vcpu->kvm))
  3060. gmap_remove(vcpu->arch.gmap);
  3061. if (vcpu->kvm->arch.use_cmma)
  3062. kvm_s390_vcpu_unsetup_cmma(vcpu);
  3063. /* We can not hold the vcpu mutex here, we are already dying */
  3064. if (kvm_s390_pv_cpu_get_handle(vcpu))
  3065. kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
  3066. free_page((unsigned long)(vcpu->arch.sie_block));
  3067. }
  3068. void kvm_arch_destroy_vm(struct kvm *kvm)
  3069. {
  3070. u16 rc, rrc;
  3071. kvm_destroy_vcpus(kvm);
  3072. sca_dispose(kvm);
  3073. kvm_s390_gisa_destroy(kvm);
  3074. /*
  3075. * We are already at the end of life and kvm->lock is not taken.
  3076. * This is ok as the file descriptor is closed by now and nobody
  3077. * can mess with the pv state.
  3078. */
  3079. kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc);
  3080. /*
  3081. * Remove the mmu notifier only when the whole KVM VM is torn down,
  3082. * and only if one was registered to begin with. If the VM is
  3083. * currently not protected, but has been previously been protected,
  3084. * then it's possible that the notifier is still registered.
  3085. */
  3086. if (kvm->arch.pv.mmu_notifier.ops)
  3087. mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm);
  3088. debug_unregister(kvm->arch.dbf);
  3089. free_page((unsigned long)kvm->arch.sie_page2);
  3090. if (!kvm_is_ucontrol(kvm))
  3091. gmap_remove(kvm->arch.gmap);
  3092. kvm_s390_destroy_adapters(kvm);
  3093. kvm_s390_clear_float_irqs(kvm);
  3094. kvm_s390_vsie_destroy(kvm);
  3095. KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
  3096. }
  3097. /* Section: vcpu related */
  3098. static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
  3099. {
  3100. vcpu->arch.gmap = gmap_create(current->mm, -1UL);
  3101. if (!vcpu->arch.gmap)
  3102. return -ENOMEM;
  3103. vcpu->arch.gmap->private = vcpu->kvm;
  3104. return 0;
  3105. }
  3106. static void sca_del_vcpu(struct kvm_vcpu *vcpu)
  3107. {
  3108. if (!kvm_s390_use_sca_entries())
  3109. return;
  3110. read_lock(&vcpu->kvm->arch.sca_lock);
  3111. if (vcpu->kvm->arch.use_esca) {
  3112. struct esca_block *sca = vcpu->kvm->arch.sca;
  3113. clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
  3114. sca->cpu[vcpu->vcpu_id].sda = 0;
  3115. } else {
  3116. struct bsca_block *sca = vcpu->kvm->arch.sca;
  3117. clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
  3118. sca->cpu[vcpu->vcpu_id].sda = 0;
  3119. }
  3120. read_unlock(&vcpu->kvm->arch.sca_lock);
  3121. }
  3122. static void sca_add_vcpu(struct kvm_vcpu *vcpu)
  3123. {
  3124. if (!kvm_s390_use_sca_entries()) {
  3125. phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca);
  3126. /* we still need the basic sca for the ipte control */
  3127. vcpu->arch.sie_block->scaoh = sca_phys >> 32;
  3128. vcpu->arch.sie_block->scaol = sca_phys;
  3129. return;
  3130. }
  3131. read_lock(&vcpu->kvm->arch.sca_lock);
  3132. if (vcpu->kvm->arch.use_esca) {
  3133. struct esca_block *sca = vcpu->kvm->arch.sca;
  3134. phys_addr_t sca_phys = virt_to_phys(sca);
  3135. sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
  3136. vcpu->arch.sie_block->scaoh = sca_phys >> 32;
  3137. vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK;
  3138. vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
  3139. set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
  3140. } else {
  3141. struct bsca_block *sca = vcpu->kvm->arch.sca;
  3142. phys_addr_t sca_phys = virt_to_phys(sca);
  3143. sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
  3144. vcpu->arch.sie_block->scaoh = sca_phys >> 32;
  3145. vcpu->arch.sie_block->scaol = sca_phys;
  3146. set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
  3147. }
  3148. read_unlock(&vcpu->kvm->arch.sca_lock);
  3149. }
  3150. /* Basic SCA to Extended SCA data copy routines */
  3151. static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
  3152. {
  3153. d->sda = s->sda;
  3154. d->sigp_ctrl.c = s->sigp_ctrl.c;
  3155. d->sigp_ctrl.scn = s->sigp_ctrl.scn;
  3156. }
  3157. static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
  3158. {
  3159. int i;
  3160. d->ipte_control = s->ipte_control;
  3161. d->mcn[0] = s->mcn;
  3162. for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
  3163. sca_copy_entry(&d->cpu[i], &s->cpu[i]);
  3164. }
  3165. static int sca_switch_to_extended(struct kvm *kvm)
  3166. {
  3167. struct bsca_block *old_sca = kvm->arch.sca;
  3168. struct esca_block *new_sca;
  3169. struct kvm_vcpu *vcpu;
  3170. unsigned long vcpu_idx;
  3171. u32 scaol, scaoh;
  3172. phys_addr_t new_sca_phys;
  3173. if (kvm->arch.use_esca)
  3174. return 0;
  3175. new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  3176. if (!new_sca)
  3177. return -ENOMEM;
  3178. new_sca_phys = virt_to_phys(new_sca);
  3179. scaoh = new_sca_phys >> 32;
  3180. scaol = new_sca_phys & ESCA_SCAOL_MASK;
  3181. kvm_s390_vcpu_block_all(kvm);
  3182. write_lock(&kvm->arch.sca_lock);
  3183. sca_copy_b_to_e(new_sca, old_sca);
  3184. kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
  3185. vcpu->arch.sie_block->scaoh = scaoh;
  3186. vcpu->arch.sie_block->scaol = scaol;
  3187. vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
  3188. }
  3189. kvm->arch.sca = new_sca;
  3190. kvm->arch.use_esca = 1;
  3191. write_unlock(&kvm->arch.sca_lock);
  3192. kvm_s390_vcpu_unblock_all(kvm);
  3193. free_page((unsigned long)old_sca);
  3194. VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
  3195. old_sca, kvm->arch.sca);
  3196. return 0;
  3197. }
  3198. static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
  3199. {
  3200. int rc;
  3201. if (!kvm_s390_use_sca_entries()) {
  3202. if (id < KVM_MAX_VCPUS)
  3203. return true;
  3204. return false;
  3205. }
  3206. if (id < KVM_S390_BSCA_CPU_SLOTS)
  3207. return true;
  3208. if (!sclp.has_esca || !sclp.has_64bscao)
  3209. return false;
  3210. rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
  3211. return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
  3212. }
  3213. /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
  3214. static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3215. {
  3216. WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
  3217. raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
  3218. vcpu->arch.cputm_start = get_tod_clock_fast();
  3219. raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
  3220. }
  3221. /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
  3222. static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3223. {
  3224. WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
  3225. raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
  3226. vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
  3227. vcpu->arch.cputm_start = 0;
  3228. raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
  3229. }
  3230. /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
  3231. static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3232. {
  3233. WARN_ON_ONCE(vcpu->arch.cputm_enabled);
  3234. vcpu->arch.cputm_enabled = true;
  3235. __start_cpu_timer_accounting(vcpu);
  3236. }
  3237. /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
  3238. static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3239. {
  3240. WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
  3241. __stop_cpu_timer_accounting(vcpu);
  3242. vcpu->arch.cputm_enabled = false;
  3243. }
  3244. static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3245. {
  3246. preempt_disable(); /* protect from TOD sync and vcpu_load/put */
  3247. __enable_cpu_timer_accounting(vcpu);
  3248. preempt_enable();
  3249. }
  3250. static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
  3251. {
  3252. preempt_disable(); /* protect from TOD sync and vcpu_load/put */
  3253. __disable_cpu_timer_accounting(vcpu);
  3254. preempt_enable();
  3255. }
  3256. /* set the cpu timer - may only be called from the VCPU thread itself */
  3257. void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
  3258. {
  3259. preempt_disable(); /* protect from TOD sync and vcpu_load/put */
  3260. raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
  3261. if (vcpu->arch.cputm_enabled)
  3262. vcpu->arch.cputm_start = get_tod_clock_fast();
  3263. vcpu->arch.sie_block->cputm = cputm;
  3264. raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
  3265. preempt_enable();
  3266. }
  3267. /* update and get the cpu timer - can also be called from other VCPU threads */
  3268. __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
  3269. {
  3270. unsigned int seq;
  3271. __u64 value;
  3272. if (unlikely(!vcpu->arch.cputm_enabled))
  3273. return vcpu->arch.sie_block->cputm;
  3274. preempt_disable(); /* protect from TOD sync and vcpu_load/put */
  3275. do {
  3276. seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
  3277. /*
  3278. * If the writer would ever execute a read in the critical
  3279. * section, e.g. in irq context, we have a deadlock.
  3280. */
  3281. WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
  3282. value = vcpu->arch.sie_block->cputm;
  3283. /* if cputm_start is 0, accounting is being started/stopped */
  3284. if (likely(vcpu->arch.cputm_start))
  3285. value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
  3286. } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
  3287. preempt_enable();
  3288. return value;
  3289. }
  3290. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  3291. {
  3292. gmap_enable(vcpu->arch.enabled_gmap);
  3293. kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
  3294. if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
  3295. __start_cpu_timer_accounting(vcpu);
  3296. vcpu->cpu = cpu;
  3297. }
  3298. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  3299. {
  3300. vcpu->cpu = -1;
  3301. if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
  3302. __stop_cpu_timer_accounting(vcpu);
  3303. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
  3304. vcpu->arch.enabled_gmap = gmap_get_enabled();
  3305. gmap_disable(vcpu->arch.enabled_gmap);
  3306. }
  3307. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  3308. {
  3309. mutex_lock(&vcpu->kvm->lock);
  3310. preempt_disable();
  3311. vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
  3312. vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
  3313. preempt_enable();
  3314. mutex_unlock(&vcpu->kvm->lock);
  3315. if (!kvm_is_ucontrol(vcpu->kvm)) {
  3316. vcpu->arch.gmap = vcpu->kvm->arch.gmap;
  3317. sca_add_vcpu(vcpu);
  3318. }
  3319. if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
  3320. vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
  3321. /* make vcpu_load load the right gmap on the first trigger */
  3322. vcpu->arch.enabled_gmap = vcpu->arch.gmap;
  3323. }
  3324. static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
  3325. {
  3326. if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
  3327. test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
  3328. return true;
  3329. return false;
  3330. }
  3331. static bool kvm_has_pckmo_ecc(struct kvm *kvm)
  3332. {
  3333. /* At least one ECC subfunction must be present */
  3334. return kvm_has_pckmo_subfunc(kvm, 32) ||
  3335. kvm_has_pckmo_subfunc(kvm, 33) ||
  3336. kvm_has_pckmo_subfunc(kvm, 34) ||
  3337. kvm_has_pckmo_subfunc(kvm, 40) ||
  3338. kvm_has_pckmo_subfunc(kvm, 41);
  3339. }
  3340. static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
  3341. {
  3342. /*
  3343. * If the AP instructions are not being interpreted and the MSAX3
  3344. * facility is not configured for the guest, there is nothing to set up.
  3345. */
  3346. if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
  3347. return;
  3348. vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
  3349. vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
  3350. vcpu->arch.sie_block->eca &= ~ECA_APIE;
  3351. vcpu->arch.sie_block->ecd &= ~ECD_ECC;
  3352. if (vcpu->kvm->arch.crypto.apie)
  3353. vcpu->arch.sie_block->eca |= ECA_APIE;
  3354. /* Set up protected key support */
  3355. if (vcpu->kvm->arch.crypto.aes_kw) {
  3356. vcpu->arch.sie_block->ecb3 |= ECB3_AES;
  3357. /* ecc is also wrapped with AES key */
  3358. if (kvm_has_pckmo_ecc(vcpu->kvm))
  3359. vcpu->arch.sie_block->ecd |= ECD_ECC;
  3360. }
  3361. if (vcpu->kvm->arch.crypto.dea_kw)
  3362. vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
  3363. }
  3364. void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
  3365. {
  3366. free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo));
  3367. vcpu->arch.sie_block->cbrlo = 0;
  3368. }
  3369. int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
  3370. {
  3371. void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
  3372. if (!cbrlo_page)
  3373. return -ENOMEM;
  3374. vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page);
  3375. return 0;
  3376. }
  3377. static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
  3378. {
  3379. struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
  3380. vcpu->arch.sie_block->ibc = model->ibc;
  3381. if (test_kvm_facility(vcpu->kvm, 7))
  3382. vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list);
  3383. }
  3384. static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
  3385. {
  3386. int rc = 0;
  3387. u16 uvrc, uvrrc;
  3388. atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
  3389. CPUSTAT_SM |
  3390. CPUSTAT_STOPPED);
  3391. if (test_kvm_facility(vcpu->kvm, 78))
  3392. kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
  3393. else if (test_kvm_facility(vcpu->kvm, 8))
  3394. kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
  3395. kvm_s390_vcpu_setup_model(vcpu);
  3396. /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
  3397. if (MACHINE_HAS_ESOP)
  3398. vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
  3399. if (test_kvm_facility(vcpu->kvm, 9))
  3400. vcpu->arch.sie_block->ecb |= ECB_SRSI;
  3401. if (test_kvm_facility(vcpu->kvm, 11))
  3402. vcpu->arch.sie_block->ecb |= ECB_PTF;
  3403. if (test_kvm_facility(vcpu->kvm, 73))
  3404. vcpu->arch.sie_block->ecb |= ECB_TE;
  3405. if (!kvm_is_ucontrol(vcpu->kvm))
  3406. vcpu->arch.sie_block->ecb |= ECB_SPECI;
  3407. if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
  3408. vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
  3409. if (test_kvm_facility(vcpu->kvm, 130))
  3410. vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
  3411. vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
  3412. if (sclp.has_cei)
  3413. vcpu->arch.sie_block->eca |= ECA_CEI;
  3414. if (sclp.has_ib)
  3415. vcpu->arch.sie_block->eca |= ECA_IB;
  3416. if (sclp.has_siif)
  3417. vcpu->arch.sie_block->eca |= ECA_SII;
  3418. if (sclp.has_sigpif)
  3419. vcpu->arch.sie_block->eca |= ECA_SIGPI;
  3420. if (test_kvm_facility(vcpu->kvm, 129)) {
  3421. vcpu->arch.sie_block->eca |= ECA_VX;
  3422. vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
  3423. }
  3424. if (test_kvm_facility(vcpu->kvm, 139))
  3425. vcpu->arch.sie_block->ecd |= ECD_MEF;
  3426. if (test_kvm_facility(vcpu->kvm, 156))
  3427. vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
  3428. if (vcpu->arch.sie_block->gd) {
  3429. vcpu->arch.sie_block->eca |= ECA_AIV;
  3430. VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
  3431. vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
  3432. }
  3433. vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC;
  3434. vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb);
  3435. if (sclp.has_kss)
  3436. kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
  3437. else
  3438. vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
  3439. if (vcpu->kvm->arch.use_cmma) {
  3440. rc = kvm_s390_vcpu_setup_cmma(vcpu);
  3441. if (rc)
  3442. return rc;
  3443. }
  3444. hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3445. vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
  3446. vcpu->arch.sie_block->hpid = HPID_KVM;
  3447. kvm_s390_vcpu_crypto_setup(vcpu);
  3448. kvm_s390_vcpu_pci_setup(vcpu);
  3449. mutex_lock(&vcpu->kvm->lock);
  3450. if (kvm_s390_pv_is_protected(vcpu->kvm)) {
  3451. rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
  3452. if (rc)
  3453. kvm_s390_vcpu_unsetup_cmma(vcpu);
  3454. }
  3455. mutex_unlock(&vcpu->kvm->lock);
  3456. return rc;
  3457. }
  3458. int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
  3459. {
  3460. if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
  3461. return -EINVAL;
  3462. return 0;
  3463. }
  3464. int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  3465. {
  3466. struct sie_page *sie_page;
  3467. int rc;
  3468. BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
  3469. sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
  3470. if (!sie_page)
  3471. return -ENOMEM;
  3472. vcpu->arch.sie_block = &sie_page->sie_block;
  3473. vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb);
  3474. /* the real guest size will always be smaller than msl */
  3475. vcpu->arch.sie_block->mso = 0;
  3476. vcpu->arch.sie_block->msl = sclp.hamax;
  3477. vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
  3478. spin_lock_init(&vcpu->arch.local_int.lock);
  3479. vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm);
  3480. seqcount_init(&vcpu->arch.cputm_seqcount);
  3481. vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
  3482. kvm_clear_async_pf_completion_queue(vcpu);
  3483. vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
  3484. KVM_SYNC_GPRS |
  3485. KVM_SYNC_ACRS |
  3486. KVM_SYNC_CRS |
  3487. KVM_SYNC_ARCH0 |
  3488. KVM_SYNC_PFAULT |
  3489. KVM_SYNC_DIAG318;
  3490. vcpu->arch.acrs_loaded = false;
  3491. kvm_s390_set_prefix(vcpu, 0);
  3492. if (test_kvm_facility(vcpu->kvm, 64))
  3493. vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
  3494. if (test_kvm_facility(vcpu->kvm, 82))
  3495. vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
  3496. if (test_kvm_facility(vcpu->kvm, 133))
  3497. vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
  3498. if (test_kvm_facility(vcpu->kvm, 156))
  3499. vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
  3500. /* fprs can be synchronized via vrs, even if the guest has no vx. With
  3501. * cpu_has_vx(), (load|store)_fpu_regs() will work with vrs format.
  3502. */
  3503. if (cpu_has_vx())
  3504. vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
  3505. else
  3506. vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
  3507. if (kvm_is_ucontrol(vcpu->kvm)) {
  3508. rc = __kvm_ucontrol_vcpu_init(vcpu);
  3509. if (rc)
  3510. goto out_free_sie_block;
  3511. }
  3512. VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
  3513. vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
  3514. trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
  3515. rc = kvm_s390_vcpu_setup(vcpu);
  3516. if (rc)
  3517. goto out_ucontrol_uninit;
  3518. kvm_s390_update_topology_change_report(vcpu->kvm, 1);
  3519. return 0;
  3520. out_ucontrol_uninit:
  3521. if (kvm_is_ucontrol(vcpu->kvm))
  3522. gmap_remove(vcpu->arch.gmap);
  3523. out_free_sie_block:
  3524. free_page((unsigned long)(vcpu->arch.sie_block));
  3525. return rc;
  3526. }
  3527. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  3528. {
  3529. clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
  3530. return kvm_s390_vcpu_has_irq(vcpu, 0);
  3531. }
  3532. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  3533. {
  3534. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
  3535. }
  3536. void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
  3537. {
  3538. atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
  3539. exit_sie(vcpu);
  3540. }
  3541. void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
  3542. {
  3543. atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
  3544. }
  3545. static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
  3546. {
  3547. atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
  3548. exit_sie(vcpu);
  3549. }
  3550. bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
  3551. {
  3552. return atomic_read(&vcpu->arch.sie_block->prog20) &
  3553. (PROG_BLOCK_SIE | PROG_REQUEST);
  3554. }
  3555. static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
  3556. {
  3557. atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
  3558. }
  3559. /*
  3560. * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
  3561. * If the CPU is not running (e.g. waiting as idle) the function will
  3562. * return immediately. */
  3563. void exit_sie(struct kvm_vcpu *vcpu)
  3564. {
  3565. kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
  3566. kvm_s390_vsie_kick(vcpu);
  3567. while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
  3568. cpu_relax();
  3569. }
  3570. /* Kick a guest cpu out of SIE to process a request synchronously */
  3571. void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
  3572. {
  3573. __kvm_make_request(req, vcpu);
  3574. kvm_s390_vcpu_request(vcpu);
  3575. }
  3576. static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
  3577. unsigned long end)
  3578. {
  3579. struct kvm *kvm = gmap->private;
  3580. struct kvm_vcpu *vcpu;
  3581. unsigned long prefix;
  3582. unsigned long i;
  3583. trace_kvm_s390_gmap_notifier(start, end, gmap_is_shadow(gmap));
  3584. if (gmap_is_shadow(gmap))
  3585. return;
  3586. if (start >= 1UL << 31)
  3587. /* We are only interested in prefix pages */
  3588. return;
  3589. kvm_for_each_vcpu(i, vcpu, kvm) {
  3590. /* match against both prefix pages */
  3591. prefix = kvm_s390_get_prefix(vcpu);
  3592. if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
  3593. VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
  3594. start, end);
  3595. kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
  3596. }
  3597. }
  3598. }
  3599. bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
  3600. {
  3601. /* do not poll with more than halt_poll_max_steal percent of steal time */
  3602. if (get_lowcore()->avg_steal_timer * 100 / (TICK_USEC << 12) >=
  3603. READ_ONCE(halt_poll_max_steal)) {
  3604. vcpu->stat.halt_no_poll_steal++;
  3605. return true;
  3606. }
  3607. return false;
  3608. }
  3609. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  3610. {
  3611. /* kvm common code refers to this, but never calls it */
  3612. BUG();
  3613. return 0;
  3614. }
  3615. static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
  3616. struct kvm_one_reg *reg)
  3617. {
  3618. int r = -EINVAL;
  3619. switch (reg->id) {
  3620. case KVM_REG_S390_TODPR:
  3621. r = put_user(vcpu->arch.sie_block->todpr,
  3622. (u32 __user *)reg->addr);
  3623. break;
  3624. case KVM_REG_S390_EPOCHDIFF:
  3625. r = put_user(vcpu->arch.sie_block->epoch,
  3626. (u64 __user *)reg->addr);
  3627. break;
  3628. case KVM_REG_S390_CPU_TIMER:
  3629. r = put_user(kvm_s390_get_cpu_timer(vcpu),
  3630. (u64 __user *)reg->addr);
  3631. break;
  3632. case KVM_REG_S390_CLOCK_COMP:
  3633. r = put_user(vcpu->arch.sie_block->ckc,
  3634. (u64 __user *)reg->addr);
  3635. break;
  3636. case KVM_REG_S390_PFTOKEN:
  3637. r = put_user(vcpu->arch.pfault_token,
  3638. (u64 __user *)reg->addr);
  3639. break;
  3640. case KVM_REG_S390_PFCOMPARE:
  3641. r = put_user(vcpu->arch.pfault_compare,
  3642. (u64 __user *)reg->addr);
  3643. break;
  3644. case KVM_REG_S390_PFSELECT:
  3645. r = put_user(vcpu->arch.pfault_select,
  3646. (u64 __user *)reg->addr);
  3647. break;
  3648. case KVM_REG_S390_PP:
  3649. r = put_user(vcpu->arch.sie_block->pp,
  3650. (u64 __user *)reg->addr);
  3651. break;
  3652. case KVM_REG_S390_GBEA:
  3653. r = put_user(vcpu->arch.sie_block->gbea,
  3654. (u64 __user *)reg->addr);
  3655. break;
  3656. default:
  3657. break;
  3658. }
  3659. return r;
  3660. }
  3661. static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
  3662. struct kvm_one_reg *reg)
  3663. {
  3664. int r = -EINVAL;
  3665. __u64 val;
  3666. switch (reg->id) {
  3667. case KVM_REG_S390_TODPR:
  3668. r = get_user(vcpu->arch.sie_block->todpr,
  3669. (u32 __user *)reg->addr);
  3670. break;
  3671. case KVM_REG_S390_EPOCHDIFF:
  3672. r = get_user(vcpu->arch.sie_block->epoch,
  3673. (u64 __user *)reg->addr);
  3674. break;
  3675. case KVM_REG_S390_CPU_TIMER:
  3676. r = get_user(val, (u64 __user *)reg->addr);
  3677. if (!r)
  3678. kvm_s390_set_cpu_timer(vcpu, val);
  3679. break;
  3680. case KVM_REG_S390_CLOCK_COMP:
  3681. r = get_user(vcpu->arch.sie_block->ckc,
  3682. (u64 __user *)reg->addr);
  3683. break;
  3684. case KVM_REG_S390_PFTOKEN:
  3685. r = get_user(vcpu->arch.pfault_token,
  3686. (u64 __user *)reg->addr);
  3687. if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
  3688. kvm_clear_async_pf_completion_queue(vcpu);
  3689. break;
  3690. case KVM_REG_S390_PFCOMPARE:
  3691. r = get_user(vcpu->arch.pfault_compare,
  3692. (u64 __user *)reg->addr);
  3693. break;
  3694. case KVM_REG_S390_PFSELECT:
  3695. r = get_user(vcpu->arch.pfault_select,
  3696. (u64 __user *)reg->addr);
  3697. break;
  3698. case KVM_REG_S390_PP:
  3699. r = get_user(vcpu->arch.sie_block->pp,
  3700. (u64 __user *)reg->addr);
  3701. break;
  3702. case KVM_REG_S390_GBEA:
  3703. r = get_user(vcpu->arch.sie_block->gbea,
  3704. (u64 __user *)reg->addr);
  3705. break;
  3706. default:
  3707. break;
  3708. }
  3709. return r;
  3710. }
  3711. static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
  3712. {
  3713. vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
  3714. vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
  3715. memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
  3716. kvm_clear_async_pf_completion_queue(vcpu);
  3717. if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
  3718. kvm_s390_vcpu_stop(vcpu);
  3719. kvm_s390_clear_local_irqs(vcpu);
  3720. }
  3721. static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
  3722. {
  3723. /* Initial reset is a superset of the normal reset */
  3724. kvm_arch_vcpu_ioctl_normal_reset(vcpu);
  3725. /*
  3726. * This equals initial cpu reset in pop, but we don't switch to ESA.
  3727. * We do not only reset the internal data, but also ...
  3728. */
  3729. vcpu->arch.sie_block->gpsw.mask = 0;
  3730. vcpu->arch.sie_block->gpsw.addr = 0;
  3731. kvm_s390_set_prefix(vcpu, 0);
  3732. kvm_s390_set_cpu_timer(vcpu, 0);
  3733. vcpu->arch.sie_block->ckc = 0;
  3734. memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
  3735. vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
  3736. vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
  3737. /* ... the data in sync regs */
  3738. memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
  3739. vcpu->run->s.regs.ckc = 0;
  3740. vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
  3741. vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
  3742. vcpu->run->psw_addr = 0;
  3743. vcpu->run->psw_mask = 0;
  3744. vcpu->run->s.regs.todpr = 0;
  3745. vcpu->run->s.regs.cputm = 0;
  3746. vcpu->run->s.regs.ckc = 0;
  3747. vcpu->run->s.regs.pp = 0;
  3748. vcpu->run->s.regs.gbea = 1;
  3749. vcpu->run->s.regs.fpc = 0;
  3750. /*
  3751. * Do not reset these registers in the protected case, as some of
  3752. * them are overlaid and they are not accessible in this case
  3753. * anyway.
  3754. */
  3755. if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
  3756. vcpu->arch.sie_block->gbea = 1;
  3757. vcpu->arch.sie_block->pp = 0;
  3758. vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
  3759. vcpu->arch.sie_block->todpr = 0;
  3760. }
  3761. }
  3762. static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
  3763. {
  3764. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  3765. /* Clear reset is a superset of the initial reset */
  3766. kvm_arch_vcpu_ioctl_initial_reset(vcpu);
  3767. memset(&regs->gprs, 0, sizeof(regs->gprs));
  3768. memset(&regs->vrs, 0, sizeof(regs->vrs));
  3769. memset(&regs->acrs, 0, sizeof(regs->acrs));
  3770. memset(&regs->gscb, 0, sizeof(regs->gscb));
  3771. regs->etoken = 0;
  3772. regs->etoken_extension = 0;
  3773. }
  3774. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3775. {
  3776. vcpu_load(vcpu);
  3777. memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
  3778. vcpu_put(vcpu);
  3779. return 0;
  3780. }
  3781. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3782. {
  3783. vcpu_load(vcpu);
  3784. memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
  3785. vcpu_put(vcpu);
  3786. return 0;
  3787. }
  3788. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  3789. struct kvm_sregs *sregs)
  3790. {
  3791. vcpu_load(vcpu);
  3792. memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
  3793. memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
  3794. vcpu_put(vcpu);
  3795. return 0;
  3796. }
  3797. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  3798. struct kvm_sregs *sregs)
  3799. {
  3800. vcpu_load(vcpu);
  3801. memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
  3802. memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
  3803. vcpu_put(vcpu);
  3804. return 0;
  3805. }
  3806. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3807. {
  3808. int ret = 0;
  3809. vcpu_load(vcpu);
  3810. vcpu->run->s.regs.fpc = fpu->fpc;
  3811. if (cpu_has_vx())
  3812. convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
  3813. (freg_t *) fpu->fprs);
  3814. else
  3815. memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
  3816. vcpu_put(vcpu);
  3817. return ret;
  3818. }
  3819. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3820. {
  3821. vcpu_load(vcpu);
  3822. if (cpu_has_vx())
  3823. convert_vx_to_fp((freg_t *) fpu->fprs,
  3824. (__vector128 *) vcpu->run->s.regs.vrs);
  3825. else
  3826. memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
  3827. fpu->fpc = vcpu->run->s.regs.fpc;
  3828. vcpu_put(vcpu);
  3829. return 0;
  3830. }
  3831. static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
  3832. {
  3833. int rc = 0;
  3834. if (!is_vcpu_stopped(vcpu))
  3835. rc = -EBUSY;
  3836. else {
  3837. vcpu->run->psw_mask = psw.mask;
  3838. vcpu->run->psw_addr = psw.addr;
  3839. }
  3840. return rc;
  3841. }
  3842. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  3843. struct kvm_translation *tr)
  3844. {
  3845. return -EINVAL; /* not implemented yet */
  3846. }
  3847. #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
  3848. KVM_GUESTDBG_USE_HW_BP | \
  3849. KVM_GUESTDBG_ENABLE)
  3850. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  3851. struct kvm_guest_debug *dbg)
  3852. {
  3853. int rc = 0;
  3854. vcpu_load(vcpu);
  3855. vcpu->guest_debug = 0;
  3856. kvm_s390_clear_bp_data(vcpu);
  3857. if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
  3858. rc = -EINVAL;
  3859. goto out;
  3860. }
  3861. if (!sclp.has_gpere) {
  3862. rc = -EINVAL;
  3863. goto out;
  3864. }
  3865. if (dbg->control & KVM_GUESTDBG_ENABLE) {
  3866. vcpu->guest_debug = dbg->control;
  3867. /* enforce guest PER */
  3868. kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
  3869. if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
  3870. rc = kvm_s390_import_bp_data(vcpu, dbg);
  3871. } else {
  3872. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
  3873. vcpu->arch.guestdbg.last_bp = 0;
  3874. }
  3875. if (rc) {
  3876. vcpu->guest_debug = 0;
  3877. kvm_s390_clear_bp_data(vcpu);
  3878. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
  3879. }
  3880. out:
  3881. vcpu_put(vcpu);
  3882. return rc;
  3883. }
  3884. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  3885. struct kvm_mp_state *mp_state)
  3886. {
  3887. int ret;
  3888. vcpu_load(vcpu);
  3889. /* CHECK_STOP and LOAD are not supported yet */
  3890. ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
  3891. KVM_MP_STATE_OPERATING;
  3892. vcpu_put(vcpu);
  3893. return ret;
  3894. }
  3895. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  3896. struct kvm_mp_state *mp_state)
  3897. {
  3898. int rc = 0;
  3899. vcpu_load(vcpu);
  3900. /* user space knows about this interface - let it control the state */
  3901. kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
  3902. switch (mp_state->mp_state) {
  3903. case KVM_MP_STATE_STOPPED:
  3904. rc = kvm_s390_vcpu_stop(vcpu);
  3905. break;
  3906. case KVM_MP_STATE_OPERATING:
  3907. rc = kvm_s390_vcpu_start(vcpu);
  3908. break;
  3909. case KVM_MP_STATE_LOAD:
  3910. if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
  3911. rc = -ENXIO;
  3912. break;
  3913. }
  3914. rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
  3915. break;
  3916. case KVM_MP_STATE_CHECK_STOP:
  3917. fallthrough; /* CHECK_STOP and LOAD are not supported yet */
  3918. default:
  3919. rc = -ENXIO;
  3920. }
  3921. vcpu_put(vcpu);
  3922. return rc;
  3923. }
  3924. static bool ibs_enabled(struct kvm_vcpu *vcpu)
  3925. {
  3926. return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
  3927. }
  3928. static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
  3929. {
  3930. retry:
  3931. kvm_s390_vcpu_request_handled(vcpu);
  3932. if (!kvm_request_pending(vcpu))
  3933. return 0;
  3934. /*
  3935. * If the guest prefix changed, re-arm the ipte notifier for the
  3936. * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
  3937. * This ensures that the ipte instruction for this request has
  3938. * already finished. We might race against a second unmapper that
  3939. * wants to set the blocking bit. Lets just retry the request loop.
  3940. */
  3941. if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) {
  3942. int rc;
  3943. rc = gmap_mprotect_notify(vcpu->arch.gmap,
  3944. kvm_s390_get_prefix(vcpu),
  3945. PAGE_SIZE * 2, PROT_WRITE);
  3946. if (rc) {
  3947. kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
  3948. return rc;
  3949. }
  3950. goto retry;
  3951. }
  3952. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
  3953. vcpu->arch.sie_block->ihcpu = 0xffff;
  3954. goto retry;
  3955. }
  3956. if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
  3957. if (!ibs_enabled(vcpu)) {
  3958. trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
  3959. kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
  3960. }
  3961. goto retry;
  3962. }
  3963. if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
  3964. if (ibs_enabled(vcpu)) {
  3965. trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
  3966. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
  3967. }
  3968. goto retry;
  3969. }
  3970. if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
  3971. vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
  3972. goto retry;
  3973. }
  3974. if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
  3975. /*
  3976. * Disable CMM virtualization; we will emulate the ESSA
  3977. * instruction manually, in order to provide additional
  3978. * functionalities needed for live migration.
  3979. */
  3980. vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
  3981. goto retry;
  3982. }
  3983. if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
  3984. /*
  3985. * Re-enable CMM virtualization if CMMA is available and
  3986. * CMM has been used.
  3987. */
  3988. if ((vcpu->kvm->arch.use_cmma) &&
  3989. (vcpu->kvm->mm->context.uses_cmm))
  3990. vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
  3991. goto retry;
  3992. }
  3993. /* we left the vsie handler, nothing to do, just clear the request */
  3994. kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
  3995. return 0;
  3996. }
  3997. static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
  3998. {
  3999. struct kvm_vcpu *vcpu;
  4000. union tod_clock clk;
  4001. unsigned long i;
  4002. preempt_disable();
  4003. store_tod_clock_ext(&clk);
  4004. kvm->arch.epoch = gtod->tod - clk.tod;
  4005. kvm->arch.epdx = 0;
  4006. if (test_kvm_facility(kvm, 139)) {
  4007. kvm->arch.epdx = gtod->epoch_idx - clk.ei;
  4008. if (kvm->arch.epoch > gtod->tod)
  4009. kvm->arch.epdx -= 1;
  4010. }
  4011. kvm_s390_vcpu_block_all(kvm);
  4012. kvm_for_each_vcpu(i, vcpu, kvm) {
  4013. vcpu->arch.sie_block->epoch = kvm->arch.epoch;
  4014. vcpu->arch.sie_block->epdx = kvm->arch.epdx;
  4015. }
  4016. kvm_s390_vcpu_unblock_all(kvm);
  4017. preempt_enable();
  4018. }
  4019. int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
  4020. {
  4021. if (!mutex_trylock(&kvm->lock))
  4022. return 0;
  4023. __kvm_s390_set_tod_clock(kvm, gtod);
  4024. mutex_unlock(&kvm->lock);
  4025. return 1;
  4026. }
  4027. /**
  4028. * kvm_arch_fault_in_page - fault-in guest page if necessary
  4029. * @vcpu: The corresponding virtual cpu
  4030. * @gpa: Guest physical address
  4031. * @writable: Whether the page should be writable or not
  4032. *
  4033. * Make sure that a guest page has been faulted-in on the host.
  4034. *
  4035. * Return: Zero on success, negative error code otherwise.
  4036. */
  4037. long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
  4038. {
  4039. return gmap_fault(vcpu->arch.gmap, gpa,
  4040. writable ? FAULT_FLAG_WRITE : 0);
  4041. }
  4042. static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
  4043. unsigned long token)
  4044. {
  4045. struct kvm_s390_interrupt inti;
  4046. struct kvm_s390_irq irq;
  4047. if (start_token) {
  4048. irq.u.ext.ext_params2 = token;
  4049. irq.type = KVM_S390_INT_PFAULT_INIT;
  4050. WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
  4051. } else {
  4052. inti.type = KVM_S390_INT_PFAULT_DONE;
  4053. inti.parm64 = token;
  4054. WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
  4055. }
  4056. }
  4057. bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  4058. struct kvm_async_pf *work)
  4059. {
  4060. trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
  4061. __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
  4062. return true;
  4063. }
  4064. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  4065. struct kvm_async_pf *work)
  4066. {
  4067. trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
  4068. __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
  4069. }
  4070. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
  4071. struct kvm_async_pf *work)
  4072. {
  4073. /* s390 will always inject the page directly */
  4074. }
  4075. bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
  4076. {
  4077. /*
  4078. * s390 will always inject the page directly,
  4079. * but we still want check_async_completion to cleanup
  4080. */
  4081. return true;
  4082. }
  4083. static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
  4084. {
  4085. hva_t hva;
  4086. struct kvm_arch_async_pf arch;
  4087. if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
  4088. return false;
  4089. if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
  4090. vcpu->arch.pfault_compare)
  4091. return false;
  4092. if (psw_extint_disabled(vcpu))
  4093. return false;
  4094. if (kvm_s390_vcpu_has_irq(vcpu, 0))
  4095. return false;
  4096. if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
  4097. return false;
  4098. if (!vcpu->arch.gmap->pfault_enabled)
  4099. return false;
  4100. hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
  4101. hva += current->thread.gmap_addr & ~PAGE_MASK;
  4102. if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
  4103. return false;
  4104. return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
  4105. }
  4106. static int vcpu_pre_run(struct kvm_vcpu *vcpu)
  4107. {
  4108. int rc, cpuflags;
  4109. /*
  4110. * On s390 notifications for arriving pages will be delivered directly
  4111. * to the guest but the house keeping for completed pfaults is
  4112. * handled outside the worker.
  4113. */
  4114. kvm_check_async_pf_completion(vcpu);
  4115. vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
  4116. vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
  4117. if (need_resched())
  4118. schedule();
  4119. if (!kvm_is_ucontrol(vcpu->kvm)) {
  4120. rc = kvm_s390_deliver_pending_interrupts(vcpu);
  4121. if (rc || guestdbg_exit_pending(vcpu))
  4122. return rc;
  4123. }
  4124. rc = kvm_s390_handle_requests(vcpu);
  4125. if (rc)
  4126. return rc;
  4127. if (guestdbg_enabled(vcpu)) {
  4128. kvm_s390_backup_guest_per_regs(vcpu);
  4129. kvm_s390_patch_guest_per_regs(vcpu);
  4130. }
  4131. clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
  4132. vcpu->arch.sie_block->icptcode = 0;
  4133. cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
  4134. VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
  4135. trace_kvm_s390_sie_enter(vcpu, cpuflags);
  4136. return 0;
  4137. }
  4138. static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
  4139. {
  4140. struct kvm_s390_pgm_info pgm_info = {
  4141. .code = PGM_ADDRESSING,
  4142. };
  4143. u8 opcode, ilen;
  4144. int rc;
  4145. VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
  4146. trace_kvm_s390_sie_fault(vcpu);
  4147. /*
  4148. * We want to inject an addressing exception, which is defined as a
  4149. * suppressing or terminating exception. However, since we came here
  4150. * by a DAT access exception, the PSW still points to the faulting
  4151. * instruction since DAT exceptions are nullifying. So we've got
  4152. * to look up the current opcode to get the length of the instruction
  4153. * to be able to forward the PSW.
  4154. */
  4155. rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
  4156. ilen = insn_length(opcode);
  4157. if (rc < 0) {
  4158. return rc;
  4159. } else if (rc) {
  4160. /* Instruction-Fetching Exceptions - we can't detect the ilen.
  4161. * Forward by arbitrary ilc, injection will take care of
  4162. * nullification if necessary.
  4163. */
  4164. pgm_info = vcpu->arch.pgm;
  4165. ilen = 4;
  4166. }
  4167. pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
  4168. kvm_s390_forward_psw(vcpu, ilen);
  4169. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  4170. }
  4171. static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
  4172. {
  4173. struct mcck_volatile_info *mcck_info;
  4174. struct sie_page *sie_page;
  4175. VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
  4176. vcpu->arch.sie_block->icptcode);
  4177. trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
  4178. if (guestdbg_enabled(vcpu))
  4179. kvm_s390_restore_guest_per_regs(vcpu);
  4180. vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
  4181. vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
  4182. if (exit_reason == -EINTR) {
  4183. VCPU_EVENT(vcpu, 3, "%s", "machine check");
  4184. sie_page = container_of(vcpu->arch.sie_block,
  4185. struct sie_page, sie_block);
  4186. mcck_info = &sie_page->mcck_info;
  4187. kvm_s390_reinject_machine_check(vcpu, mcck_info);
  4188. return 0;
  4189. }
  4190. if (vcpu->arch.sie_block->icptcode > 0) {
  4191. int rc = kvm_handle_sie_intercept(vcpu);
  4192. if (rc != -EOPNOTSUPP)
  4193. return rc;
  4194. vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
  4195. vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
  4196. vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
  4197. vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
  4198. return -EREMOTE;
  4199. } else if (exit_reason != -EFAULT) {
  4200. vcpu->stat.exit_null++;
  4201. return 0;
  4202. } else if (kvm_is_ucontrol(vcpu->kvm)) {
  4203. vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
  4204. vcpu->run->s390_ucontrol.trans_exc_code =
  4205. current->thread.gmap_addr;
  4206. vcpu->run->s390_ucontrol.pgm_code = 0x10;
  4207. return -EREMOTE;
  4208. } else if (current->thread.gmap_pfault) {
  4209. trace_kvm_s390_major_guest_pfault(vcpu);
  4210. current->thread.gmap_pfault = 0;
  4211. if (kvm_arch_setup_async_pf(vcpu))
  4212. return 0;
  4213. vcpu->stat.pfault_sync++;
  4214. return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
  4215. }
  4216. return vcpu_post_run_fault_in_sie(vcpu);
  4217. }
  4218. #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
  4219. static int __vcpu_run(struct kvm_vcpu *vcpu)
  4220. {
  4221. int rc, exit_reason;
  4222. struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
  4223. /*
  4224. * We try to hold kvm->srcu during most of vcpu_run (except when run-
  4225. * ning the guest), so that memslots (and other stuff) are protected
  4226. */
  4227. kvm_vcpu_srcu_read_lock(vcpu);
  4228. do {
  4229. rc = vcpu_pre_run(vcpu);
  4230. if (rc || guestdbg_exit_pending(vcpu))
  4231. break;
  4232. kvm_vcpu_srcu_read_unlock(vcpu);
  4233. /*
  4234. * As PF_VCPU will be used in fault handler, between
  4235. * guest_enter and guest_exit should be no uaccess.
  4236. */
  4237. local_irq_disable();
  4238. guest_enter_irqoff();
  4239. __disable_cpu_timer_accounting(vcpu);
  4240. local_irq_enable();
  4241. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4242. memcpy(sie_page->pv_grregs,
  4243. vcpu->run->s.regs.gprs,
  4244. sizeof(sie_page->pv_grregs));
  4245. }
  4246. exit_reason = sie64a(vcpu->arch.sie_block,
  4247. vcpu->run->s.regs.gprs,
  4248. gmap_get_enabled()->asce);
  4249. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4250. memcpy(vcpu->run->s.regs.gprs,
  4251. sie_page->pv_grregs,
  4252. sizeof(sie_page->pv_grregs));
  4253. /*
  4254. * We're not allowed to inject interrupts on intercepts
  4255. * that leave the guest state in an "in-between" state
  4256. * where the next SIE entry will do a continuation.
  4257. * Fence interrupts in our "internal" PSW.
  4258. */
  4259. if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
  4260. vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
  4261. vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
  4262. }
  4263. }
  4264. local_irq_disable();
  4265. __enable_cpu_timer_accounting(vcpu);
  4266. guest_exit_irqoff();
  4267. local_irq_enable();
  4268. kvm_vcpu_srcu_read_lock(vcpu);
  4269. rc = vcpu_post_run(vcpu, exit_reason);
  4270. } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
  4271. kvm_vcpu_srcu_read_unlock(vcpu);
  4272. return rc;
  4273. }
  4274. static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
  4275. {
  4276. struct kvm_run *kvm_run = vcpu->run;
  4277. struct runtime_instr_cb *riccb;
  4278. struct gs_cb *gscb;
  4279. riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
  4280. gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
  4281. vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
  4282. vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
  4283. if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
  4284. vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
  4285. vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
  4286. vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
  4287. }
  4288. if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
  4289. vcpu->arch.pfault_token = kvm_run->s.regs.pft;
  4290. vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
  4291. vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
  4292. if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
  4293. kvm_clear_async_pf_completion_queue(vcpu);
  4294. }
  4295. if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
  4296. vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
  4297. vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
  4298. VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
  4299. }
  4300. /*
  4301. * If userspace sets the riccb (e.g. after migration) to a valid state,
  4302. * we should enable RI here instead of doing the lazy enablement.
  4303. */
  4304. if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
  4305. test_kvm_facility(vcpu->kvm, 64) &&
  4306. riccb->v &&
  4307. !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
  4308. VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
  4309. vcpu->arch.sie_block->ecb3 |= ECB3_RI;
  4310. }
  4311. /*
  4312. * If userspace sets the gscb (e.g. after migration) to non-zero,
  4313. * we should enable GS here instead of doing the lazy enablement.
  4314. */
  4315. if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
  4316. test_kvm_facility(vcpu->kvm, 133) &&
  4317. gscb->gssm &&
  4318. !vcpu->arch.gs_enabled) {
  4319. VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
  4320. vcpu->arch.sie_block->ecb |= ECB_GS;
  4321. vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
  4322. vcpu->arch.gs_enabled = 1;
  4323. }
  4324. if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
  4325. test_kvm_facility(vcpu->kvm, 82)) {
  4326. vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
  4327. vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
  4328. }
  4329. if (MACHINE_HAS_GS) {
  4330. preempt_disable();
  4331. local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
  4332. if (current->thread.gs_cb) {
  4333. vcpu->arch.host_gscb = current->thread.gs_cb;
  4334. save_gs_cb(vcpu->arch.host_gscb);
  4335. }
  4336. if (vcpu->arch.gs_enabled) {
  4337. current->thread.gs_cb = (struct gs_cb *)
  4338. &vcpu->run->s.regs.gscb;
  4339. restore_gs_cb(current->thread.gs_cb);
  4340. }
  4341. preempt_enable();
  4342. }
  4343. /* SIE will load etoken directly from SDNX and therefore kvm_run */
  4344. }
  4345. static void sync_regs(struct kvm_vcpu *vcpu)
  4346. {
  4347. struct kvm_run *kvm_run = vcpu->run;
  4348. if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
  4349. kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
  4350. if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
  4351. memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
  4352. /* some control register changes require a tlb flush */
  4353. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  4354. }
  4355. if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
  4356. kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
  4357. vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
  4358. }
  4359. save_access_regs(vcpu->arch.host_acrs);
  4360. restore_access_regs(vcpu->run->s.regs.acrs);
  4361. vcpu->arch.acrs_loaded = true;
  4362. kvm_s390_fpu_load(vcpu->run);
  4363. /* Sync fmt2 only data */
  4364. if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
  4365. sync_regs_fmt2(vcpu);
  4366. } else {
  4367. /*
  4368. * In several places we have to modify our internal view to
  4369. * not do things that are disallowed by the ultravisor. For
  4370. * example we must not inject interrupts after specific exits
  4371. * (e.g. 112 prefix page not secure). We do this by turning
  4372. * off the machine check, external and I/O interrupt bits
  4373. * of our PSW copy. To avoid getting validity intercepts, we
  4374. * do only accept the condition code from userspace.
  4375. */
  4376. vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
  4377. vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
  4378. PSW_MASK_CC;
  4379. }
  4380. kvm_run->kvm_dirty_regs = 0;
  4381. }
  4382. static void store_regs_fmt2(struct kvm_vcpu *vcpu)
  4383. {
  4384. struct kvm_run *kvm_run = vcpu->run;
  4385. kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
  4386. kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
  4387. kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
  4388. kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
  4389. kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
  4390. if (MACHINE_HAS_GS) {
  4391. preempt_disable();
  4392. local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
  4393. if (vcpu->arch.gs_enabled)
  4394. save_gs_cb(current->thread.gs_cb);
  4395. current->thread.gs_cb = vcpu->arch.host_gscb;
  4396. restore_gs_cb(vcpu->arch.host_gscb);
  4397. if (!vcpu->arch.host_gscb)
  4398. local_ctl_clear_bit(2, CR2_GUARDED_STORAGE_BIT);
  4399. vcpu->arch.host_gscb = NULL;
  4400. preempt_enable();
  4401. }
  4402. /* SIE will save etoken directly into SDNX and therefore kvm_run */
  4403. }
  4404. static void store_regs(struct kvm_vcpu *vcpu)
  4405. {
  4406. struct kvm_run *kvm_run = vcpu->run;
  4407. kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
  4408. kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
  4409. kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
  4410. memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
  4411. kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
  4412. kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
  4413. kvm_run->s.regs.pft = vcpu->arch.pfault_token;
  4414. kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
  4415. kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
  4416. save_access_regs(vcpu->run->s.regs.acrs);
  4417. restore_access_regs(vcpu->arch.host_acrs);
  4418. vcpu->arch.acrs_loaded = false;
  4419. kvm_s390_fpu_store(vcpu->run);
  4420. if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
  4421. store_regs_fmt2(vcpu);
  4422. }
  4423. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  4424. {
  4425. struct kvm_run *kvm_run = vcpu->run;
  4426. DECLARE_KERNEL_FPU_ONSTACK32(fpu);
  4427. int rc;
  4428. /*
  4429. * Running a VM while dumping always has the potential to
  4430. * produce inconsistent dump data. But for PV vcpus a SIE
  4431. * entry while dumping could also lead to a fatal validity
  4432. * intercept which we absolutely want to avoid.
  4433. */
  4434. if (vcpu->kvm->arch.pv.dumping)
  4435. return -EINVAL;
  4436. if (!vcpu->wants_to_run)
  4437. return -EINTR;
  4438. if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
  4439. kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
  4440. return -EINVAL;
  4441. vcpu_load(vcpu);
  4442. if (guestdbg_exit_pending(vcpu)) {
  4443. kvm_s390_prepare_debug_exit(vcpu);
  4444. rc = 0;
  4445. goto out;
  4446. }
  4447. kvm_sigset_activate(vcpu);
  4448. /*
  4449. * no need to check the return value of vcpu_start as it can only have
  4450. * an error for protvirt, but protvirt means user cpu state
  4451. */
  4452. if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
  4453. kvm_s390_vcpu_start(vcpu);
  4454. } else if (is_vcpu_stopped(vcpu)) {
  4455. pr_err_ratelimited("can't run stopped vcpu %d\n",
  4456. vcpu->vcpu_id);
  4457. rc = -EINVAL;
  4458. goto out;
  4459. }
  4460. kernel_fpu_begin(&fpu, KERNEL_FPC | KERNEL_VXR);
  4461. sync_regs(vcpu);
  4462. enable_cpu_timer_accounting(vcpu);
  4463. might_fault();
  4464. rc = __vcpu_run(vcpu);
  4465. if (signal_pending(current) && !rc) {
  4466. kvm_run->exit_reason = KVM_EXIT_INTR;
  4467. rc = -EINTR;
  4468. }
  4469. if (guestdbg_exit_pending(vcpu) && !rc) {
  4470. kvm_s390_prepare_debug_exit(vcpu);
  4471. rc = 0;
  4472. }
  4473. if (rc == -EREMOTE) {
  4474. /* userspace support is needed, kvm_run has been prepared */
  4475. rc = 0;
  4476. }
  4477. disable_cpu_timer_accounting(vcpu);
  4478. store_regs(vcpu);
  4479. kernel_fpu_end(&fpu, KERNEL_FPC | KERNEL_VXR);
  4480. kvm_sigset_deactivate(vcpu);
  4481. vcpu->stat.exit_userspace++;
  4482. out:
  4483. vcpu_put(vcpu);
  4484. return rc;
  4485. }
  4486. /*
  4487. * store status at address
  4488. * we use have two special cases:
  4489. * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
  4490. * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
  4491. */
  4492. int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
  4493. {
  4494. unsigned char archmode = 1;
  4495. freg_t fprs[NUM_FPRS];
  4496. unsigned int px;
  4497. u64 clkcomp, cputm;
  4498. int rc;
  4499. px = kvm_s390_get_prefix(vcpu);
  4500. if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
  4501. if (write_guest_abs(vcpu, 163, &archmode, 1))
  4502. return -EFAULT;
  4503. gpa = 0;
  4504. } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
  4505. if (write_guest_real(vcpu, 163, &archmode, 1))
  4506. return -EFAULT;
  4507. gpa = px;
  4508. } else
  4509. gpa -= __LC_FPREGS_SAVE_AREA;
  4510. /* manually convert vector registers if necessary */
  4511. if (cpu_has_vx()) {
  4512. convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
  4513. rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
  4514. fprs, 128);
  4515. } else {
  4516. rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
  4517. vcpu->run->s.regs.fprs, 128);
  4518. }
  4519. rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
  4520. vcpu->run->s.regs.gprs, 128);
  4521. rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
  4522. &vcpu->arch.sie_block->gpsw, 16);
  4523. rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
  4524. &px, 4);
  4525. rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
  4526. &vcpu->run->s.regs.fpc, 4);
  4527. rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
  4528. &vcpu->arch.sie_block->todpr, 4);
  4529. cputm = kvm_s390_get_cpu_timer(vcpu);
  4530. rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
  4531. &cputm, 8);
  4532. clkcomp = vcpu->arch.sie_block->ckc >> 8;
  4533. rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
  4534. &clkcomp, 8);
  4535. rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
  4536. &vcpu->run->s.regs.acrs, 64);
  4537. rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
  4538. &vcpu->arch.sie_block->gcr, 128);
  4539. return rc ? -EFAULT : 0;
  4540. }
  4541. int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
  4542. {
  4543. /*
  4544. * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
  4545. * switch in the run ioctl. Let's update our copies before we save
  4546. * it into the save area
  4547. */
  4548. kvm_s390_fpu_store(vcpu->run);
  4549. save_access_regs(vcpu->run->s.regs.acrs);
  4550. return kvm_s390_store_status_unloaded(vcpu, addr);
  4551. }
  4552. static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
  4553. {
  4554. kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
  4555. kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
  4556. }
  4557. static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
  4558. {
  4559. unsigned long i;
  4560. struct kvm_vcpu *vcpu;
  4561. kvm_for_each_vcpu(i, vcpu, kvm) {
  4562. __disable_ibs_on_vcpu(vcpu);
  4563. }
  4564. }
  4565. static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
  4566. {
  4567. if (!sclp.has_ibs)
  4568. return;
  4569. kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
  4570. kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
  4571. }
  4572. int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
  4573. {
  4574. int i, online_vcpus, r = 0, started_vcpus = 0;
  4575. if (!is_vcpu_stopped(vcpu))
  4576. return 0;
  4577. trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
  4578. /* Only one cpu at a time may enter/leave the STOPPED state. */
  4579. spin_lock(&vcpu->kvm->arch.start_stop_lock);
  4580. online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
  4581. /* Let's tell the UV that we want to change into the operating state */
  4582. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4583. r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
  4584. if (r) {
  4585. spin_unlock(&vcpu->kvm->arch.start_stop_lock);
  4586. return r;
  4587. }
  4588. }
  4589. for (i = 0; i < online_vcpus; i++) {
  4590. if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
  4591. started_vcpus++;
  4592. }
  4593. if (started_vcpus == 0) {
  4594. /* we're the only active VCPU -> speed it up */
  4595. __enable_ibs_on_vcpu(vcpu);
  4596. } else if (started_vcpus == 1) {
  4597. /*
  4598. * As we are starting a second VCPU, we have to disable
  4599. * the IBS facility on all VCPUs to remove potentially
  4600. * outstanding ENABLE requests.
  4601. */
  4602. __disable_ibs_on_all_vcpus(vcpu->kvm);
  4603. }
  4604. kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
  4605. /*
  4606. * The real PSW might have changed due to a RESTART interpreted by the
  4607. * ultravisor. We block all interrupts and let the next sie exit
  4608. * refresh our view.
  4609. */
  4610. if (kvm_s390_pv_cpu_is_protected(vcpu))
  4611. vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
  4612. /*
  4613. * Another VCPU might have used IBS while we were offline.
  4614. * Let's play safe and flush the VCPU at startup.
  4615. */
  4616. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  4617. spin_unlock(&vcpu->kvm->arch.start_stop_lock);
  4618. return 0;
  4619. }
  4620. int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
  4621. {
  4622. int i, online_vcpus, r = 0, started_vcpus = 0;
  4623. struct kvm_vcpu *started_vcpu = NULL;
  4624. if (is_vcpu_stopped(vcpu))
  4625. return 0;
  4626. trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
  4627. /* Only one cpu at a time may enter/leave the STOPPED state. */
  4628. spin_lock(&vcpu->kvm->arch.start_stop_lock);
  4629. online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
  4630. /* Let's tell the UV that we want to change into the stopped state */
  4631. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4632. r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
  4633. if (r) {
  4634. spin_unlock(&vcpu->kvm->arch.start_stop_lock);
  4635. return r;
  4636. }
  4637. }
  4638. /*
  4639. * Set the VCPU to STOPPED and THEN clear the interrupt flag,
  4640. * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders
  4641. * have been fully processed. This will ensure that the VCPU
  4642. * is kept BUSY if another VCPU is inquiring with SIGP SENSE.
  4643. */
  4644. kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
  4645. kvm_s390_clear_stop_irq(vcpu);
  4646. __disable_ibs_on_vcpu(vcpu);
  4647. for (i = 0; i < online_vcpus; i++) {
  4648. struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
  4649. if (!is_vcpu_stopped(tmp)) {
  4650. started_vcpus++;
  4651. started_vcpu = tmp;
  4652. }
  4653. }
  4654. if (started_vcpus == 1) {
  4655. /*
  4656. * As we only have one VCPU left, we want to enable the
  4657. * IBS facility for that VCPU to speed it up.
  4658. */
  4659. __enable_ibs_on_vcpu(started_vcpu);
  4660. }
  4661. spin_unlock(&vcpu->kvm->arch.start_stop_lock);
  4662. return 0;
  4663. }
  4664. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  4665. struct kvm_enable_cap *cap)
  4666. {
  4667. int r;
  4668. if (cap->flags)
  4669. return -EINVAL;
  4670. switch (cap->cap) {
  4671. case KVM_CAP_S390_CSS_SUPPORT:
  4672. if (!vcpu->kvm->arch.css_support) {
  4673. vcpu->kvm->arch.css_support = 1;
  4674. VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
  4675. trace_kvm_s390_enable_css(vcpu->kvm);
  4676. }
  4677. r = 0;
  4678. break;
  4679. default:
  4680. r = -EINVAL;
  4681. break;
  4682. }
  4683. return r;
  4684. }
  4685. static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu,
  4686. struct kvm_s390_mem_op *mop)
  4687. {
  4688. void __user *uaddr = (void __user *)mop->buf;
  4689. void *sida_addr;
  4690. int r = 0;
  4691. if (mop->flags || !mop->size)
  4692. return -EINVAL;
  4693. if (mop->size + mop->sida_offset < mop->size)
  4694. return -EINVAL;
  4695. if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
  4696. return -E2BIG;
  4697. if (!kvm_s390_pv_cpu_is_protected(vcpu))
  4698. return -EINVAL;
  4699. sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset;
  4700. switch (mop->op) {
  4701. case KVM_S390_MEMOP_SIDA_READ:
  4702. if (copy_to_user(uaddr, sida_addr, mop->size))
  4703. r = -EFAULT;
  4704. break;
  4705. case KVM_S390_MEMOP_SIDA_WRITE:
  4706. if (copy_from_user(sida_addr, uaddr, mop->size))
  4707. r = -EFAULT;
  4708. break;
  4709. }
  4710. return r;
  4711. }
  4712. static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu,
  4713. struct kvm_s390_mem_op *mop)
  4714. {
  4715. void __user *uaddr = (void __user *)mop->buf;
  4716. enum gacc_mode acc_mode;
  4717. void *tmpbuf = NULL;
  4718. int r;
  4719. r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION |
  4720. KVM_S390_MEMOP_F_CHECK_ONLY |
  4721. KVM_S390_MEMOP_F_SKEY_PROTECTION);
  4722. if (r)
  4723. return r;
  4724. if (mop->ar >= NUM_ACRS)
  4725. return -EINVAL;
  4726. if (kvm_s390_pv_cpu_is_protected(vcpu))
  4727. return -EINVAL;
  4728. if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
  4729. tmpbuf = vmalloc(mop->size);
  4730. if (!tmpbuf)
  4731. return -ENOMEM;
  4732. }
  4733. acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE;
  4734. if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
  4735. r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
  4736. acc_mode, mop->key);
  4737. goto out_inject;
  4738. }
  4739. if (acc_mode == GACC_FETCH) {
  4740. r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
  4741. mop->size, mop->key);
  4742. if (r)
  4743. goto out_inject;
  4744. if (copy_to_user(uaddr, tmpbuf, mop->size)) {
  4745. r = -EFAULT;
  4746. goto out_free;
  4747. }
  4748. } else {
  4749. if (copy_from_user(tmpbuf, uaddr, mop->size)) {
  4750. r = -EFAULT;
  4751. goto out_free;
  4752. }
  4753. r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
  4754. mop->size, mop->key);
  4755. }
  4756. out_inject:
  4757. if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
  4758. kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  4759. out_free:
  4760. vfree(tmpbuf);
  4761. return r;
  4762. }
  4763. static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu,
  4764. struct kvm_s390_mem_op *mop)
  4765. {
  4766. int r, srcu_idx;
  4767. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4768. switch (mop->op) {
  4769. case KVM_S390_MEMOP_LOGICAL_READ:
  4770. case KVM_S390_MEMOP_LOGICAL_WRITE:
  4771. r = kvm_s390_vcpu_mem_op(vcpu, mop);
  4772. break;
  4773. case KVM_S390_MEMOP_SIDA_READ:
  4774. case KVM_S390_MEMOP_SIDA_WRITE:
  4775. /* we are locked against sida going away by the vcpu->mutex */
  4776. r = kvm_s390_vcpu_sida_op(vcpu, mop);
  4777. break;
  4778. default:
  4779. r = -EINVAL;
  4780. }
  4781. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  4782. return r;
  4783. }
  4784. long kvm_arch_vcpu_async_ioctl(struct file *filp,
  4785. unsigned int ioctl, unsigned long arg)
  4786. {
  4787. struct kvm_vcpu *vcpu = filp->private_data;
  4788. void __user *argp = (void __user *)arg;
  4789. int rc;
  4790. switch (ioctl) {
  4791. case KVM_S390_IRQ: {
  4792. struct kvm_s390_irq s390irq;
  4793. if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
  4794. return -EFAULT;
  4795. rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
  4796. break;
  4797. }
  4798. case KVM_S390_INTERRUPT: {
  4799. struct kvm_s390_interrupt s390int;
  4800. struct kvm_s390_irq s390irq = {};
  4801. if (copy_from_user(&s390int, argp, sizeof(s390int)))
  4802. return -EFAULT;
  4803. if (s390int_to_s390irq(&s390int, &s390irq))
  4804. return -EINVAL;
  4805. rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
  4806. break;
  4807. }
  4808. default:
  4809. rc = -ENOIOCTLCMD;
  4810. break;
  4811. }
  4812. /*
  4813. * To simplify single stepping of userspace-emulated instructions,
  4814. * KVM_EXIT_S390_SIEIC exit sets KVM_GUESTDBG_EXIT_PENDING (see
  4815. * should_handle_per_ifetch()). However, if userspace emulation injects
  4816. * an interrupt, it needs to be cleared, so that KVM_EXIT_DEBUG happens
  4817. * after (and not before) the interrupt delivery.
  4818. */
  4819. if (!rc)
  4820. vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
  4821. return rc;
  4822. }
  4823. static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu,
  4824. struct kvm_pv_cmd *cmd)
  4825. {
  4826. struct kvm_s390_pv_dmp dmp;
  4827. void *data;
  4828. int ret;
  4829. /* Dump initialization is a prerequisite */
  4830. if (!vcpu->kvm->arch.pv.dumping)
  4831. return -EINVAL;
  4832. if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp)))
  4833. return -EFAULT;
  4834. /* We only handle this subcmd right now */
  4835. if (dmp.subcmd != KVM_PV_DUMP_CPU)
  4836. return -EINVAL;
  4837. /* CPU dump length is the same as create cpu storage donation. */
  4838. if (dmp.buff_len != uv_info.guest_cpu_stor_len)
  4839. return -EINVAL;
  4840. data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL);
  4841. if (!data)
  4842. return -ENOMEM;
  4843. ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc);
  4844. VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x",
  4845. vcpu->vcpu_id, cmd->rc, cmd->rrc);
  4846. if (ret)
  4847. ret = -EINVAL;
  4848. /* On success copy over the dump data */
  4849. if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len))
  4850. ret = -EFAULT;
  4851. kvfree(data);
  4852. return ret;
  4853. }
  4854. long kvm_arch_vcpu_ioctl(struct file *filp,
  4855. unsigned int ioctl, unsigned long arg)
  4856. {
  4857. struct kvm_vcpu *vcpu = filp->private_data;
  4858. void __user *argp = (void __user *)arg;
  4859. int idx;
  4860. long r;
  4861. u16 rc, rrc;
  4862. vcpu_load(vcpu);
  4863. switch (ioctl) {
  4864. case KVM_S390_STORE_STATUS:
  4865. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4866. r = kvm_s390_store_status_unloaded(vcpu, arg);
  4867. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4868. break;
  4869. case KVM_S390_SET_INITIAL_PSW: {
  4870. psw_t psw;
  4871. r = -EFAULT;
  4872. if (copy_from_user(&psw, argp, sizeof(psw)))
  4873. break;
  4874. r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
  4875. break;
  4876. }
  4877. case KVM_S390_CLEAR_RESET:
  4878. r = 0;
  4879. kvm_arch_vcpu_ioctl_clear_reset(vcpu);
  4880. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4881. r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
  4882. UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
  4883. VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
  4884. rc, rrc);
  4885. }
  4886. break;
  4887. case KVM_S390_INITIAL_RESET:
  4888. r = 0;
  4889. kvm_arch_vcpu_ioctl_initial_reset(vcpu);
  4890. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4891. r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
  4892. UVC_CMD_CPU_RESET_INITIAL,
  4893. &rc, &rrc);
  4894. VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
  4895. rc, rrc);
  4896. }
  4897. break;
  4898. case KVM_S390_NORMAL_RESET:
  4899. r = 0;
  4900. kvm_arch_vcpu_ioctl_normal_reset(vcpu);
  4901. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  4902. r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
  4903. UVC_CMD_CPU_RESET, &rc, &rrc);
  4904. VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
  4905. rc, rrc);
  4906. }
  4907. break;
  4908. case KVM_SET_ONE_REG:
  4909. case KVM_GET_ONE_REG: {
  4910. struct kvm_one_reg reg;
  4911. r = -EINVAL;
  4912. if (kvm_s390_pv_cpu_is_protected(vcpu))
  4913. break;
  4914. r = -EFAULT;
  4915. if (copy_from_user(&reg, argp, sizeof(reg)))
  4916. break;
  4917. if (ioctl == KVM_SET_ONE_REG)
  4918. r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
  4919. else
  4920. r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
  4921. break;
  4922. }
  4923. #ifdef CONFIG_KVM_S390_UCONTROL
  4924. case KVM_S390_UCAS_MAP: {
  4925. struct kvm_s390_ucas_mapping ucasmap;
  4926. if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
  4927. r = -EFAULT;
  4928. break;
  4929. }
  4930. if (!kvm_is_ucontrol(vcpu->kvm)) {
  4931. r = -EINVAL;
  4932. break;
  4933. }
  4934. r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
  4935. ucasmap.vcpu_addr, ucasmap.length);
  4936. break;
  4937. }
  4938. case KVM_S390_UCAS_UNMAP: {
  4939. struct kvm_s390_ucas_mapping ucasmap;
  4940. if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
  4941. r = -EFAULT;
  4942. break;
  4943. }
  4944. if (!kvm_is_ucontrol(vcpu->kvm)) {
  4945. r = -EINVAL;
  4946. break;
  4947. }
  4948. r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
  4949. ucasmap.length);
  4950. break;
  4951. }
  4952. #endif
  4953. case KVM_S390_VCPU_FAULT: {
  4954. r = gmap_fault(vcpu->arch.gmap, arg, 0);
  4955. break;
  4956. }
  4957. case KVM_ENABLE_CAP:
  4958. {
  4959. struct kvm_enable_cap cap;
  4960. r = -EFAULT;
  4961. if (copy_from_user(&cap, argp, sizeof(cap)))
  4962. break;
  4963. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  4964. break;
  4965. }
  4966. case KVM_S390_MEM_OP: {
  4967. struct kvm_s390_mem_op mem_op;
  4968. if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
  4969. r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op);
  4970. else
  4971. r = -EFAULT;
  4972. break;
  4973. }
  4974. case KVM_S390_SET_IRQ_STATE: {
  4975. struct kvm_s390_irq_state irq_state;
  4976. r = -EFAULT;
  4977. if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
  4978. break;
  4979. if (irq_state.len > VCPU_IRQS_MAX_BUF ||
  4980. irq_state.len == 0 ||
  4981. irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
  4982. r = -EINVAL;
  4983. break;
  4984. }
  4985. /* do not use irq_state.flags, it will break old QEMUs */
  4986. r = kvm_s390_set_irq_state(vcpu,
  4987. (void __user *) irq_state.buf,
  4988. irq_state.len);
  4989. break;
  4990. }
  4991. case KVM_S390_GET_IRQ_STATE: {
  4992. struct kvm_s390_irq_state irq_state;
  4993. r = -EFAULT;
  4994. if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
  4995. break;
  4996. if (irq_state.len == 0) {
  4997. r = -EINVAL;
  4998. break;
  4999. }
  5000. /* do not use irq_state.flags, it will break old QEMUs */
  5001. r = kvm_s390_get_irq_state(vcpu,
  5002. (__u8 __user *) irq_state.buf,
  5003. irq_state.len);
  5004. break;
  5005. }
  5006. case KVM_S390_PV_CPU_COMMAND: {
  5007. struct kvm_pv_cmd cmd;
  5008. r = -EINVAL;
  5009. if (!is_prot_virt_host())
  5010. break;
  5011. r = -EFAULT;
  5012. if (copy_from_user(&cmd, argp, sizeof(cmd)))
  5013. break;
  5014. r = -EINVAL;
  5015. if (cmd.flags)
  5016. break;
  5017. /* We only handle this cmd right now */
  5018. if (cmd.cmd != KVM_PV_DUMP)
  5019. break;
  5020. r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd);
  5021. /* Always copy over UV rc / rrc data */
  5022. if (copy_to_user((__u8 __user *)argp, &cmd.rc,
  5023. sizeof(cmd.rc) + sizeof(cmd.rrc)))
  5024. r = -EFAULT;
  5025. break;
  5026. }
  5027. default:
  5028. r = -ENOTTY;
  5029. }
  5030. vcpu_put(vcpu);
  5031. return r;
  5032. }
  5033. vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  5034. {
  5035. #ifdef CONFIG_KVM_S390_UCONTROL
  5036. if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
  5037. && (kvm_is_ucontrol(vcpu->kvm))) {
  5038. vmf->page = virt_to_page(vcpu->arch.sie_block);
  5039. get_page(vmf->page);
  5040. return 0;
  5041. }
  5042. #endif
  5043. return VM_FAULT_SIGBUS;
  5044. }
  5045. bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
  5046. {
  5047. return true;
  5048. }
  5049. /* Section: memory related */
  5050. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  5051. const struct kvm_memory_slot *old,
  5052. struct kvm_memory_slot *new,
  5053. enum kvm_mr_change change)
  5054. {
  5055. gpa_t size;
  5056. if (kvm_is_ucontrol(kvm))
  5057. return -EINVAL;
  5058. /* When we are protected, we should not change the memory slots */
  5059. if (kvm_s390_pv_get_handle(kvm))
  5060. return -EINVAL;
  5061. if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) {
  5062. /*
  5063. * A few sanity checks. We can have memory slots which have to be
  5064. * located/ended at a segment boundary (1MB). The memory in userland is
  5065. * ok to be fragmented into various different vmas. It is okay to mmap()
  5066. * and munmap() stuff in this slot after doing this call at any time
  5067. */
  5068. if (new->userspace_addr & 0xffffful)
  5069. return -EINVAL;
  5070. size = new->npages * PAGE_SIZE;
  5071. if (size & 0xffffful)
  5072. return -EINVAL;
  5073. if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit)
  5074. return -EINVAL;
  5075. }
  5076. if (!kvm->arch.migration_mode)
  5077. return 0;
  5078. /*
  5079. * Turn off migration mode when:
  5080. * - userspace creates a new memslot with dirty logging off,
  5081. * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and
  5082. * dirty logging is turned off.
  5083. * Migration mode expects dirty page logging being enabled to store
  5084. * its dirty bitmap.
  5085. */
  5086. if (change != KVM_MR_DELETE &&
  5087. !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
  5088. WARN(kvm_s390_vm_stop_migration(kvm),
  5089. "Failed to stop migration mode");
  5090. return 0;
  5091. }
  5092. void kvm_arch_commit_memory_region(struct kvm *kvm,
  5093. struct kvm_memory_slot *old,
  5094. const struct kvm_memory_slot *new,
  5095. enum kvm_mr_change change)
  5096. {
  5097. int rc = 0;
  5098. switch (change) {
  5099. case KVM_MR_DELETE:
  5100. rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
  5101. old->npages * PAGE_SIZE);
  5102. break;
  5103. case KVM_MR_MOVE:
  5104. rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
  5105. old->npages * PAGE_SIZE);
  5106. if (rc)
  5107. break;
  5108. fallthrough;
  5109. case KVM_MR_CREATE:
  5110. rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr,
  5111. new->base_gfn * PAGE_SIZE,
  5112. new->npages * PAGE_SIZE);
  5113. break;
  5114. case KVM_MR_FLAGS_ONLY:
  5115. break;
  5116. default:
  5117. WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
  5118. }
  5119. if (rc)
  5120. pr_warn("failed to commit memory region\n");
  5121. return;
  5122. }
  5123. static inline unsigned long nonhyp_mask(int i)
  5124. {
  5125. unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
  5126. return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
  5127. }
  5128. static int __init kvm_s390_init(void)
  5129. {
  5130. int i, r;
  5131. if (!sclp.has_sief2) {
  5132. pr_info("SIE is not available\n");
  5133. return -ENODEV;
  5134. }
  5135. if (nested && hpage) {
  5136. pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
  5137. return -EINVAL;
  5138. }
  5139. for (i = 0; i < 16; i++)
  5140. kvm_s390_fac_base[i] |=
  5141. stfle_fac_list[i] & nonhyp_mask(i);
  5142. r = __kvm_s390_init();
  5143. if (r)
  5144. return r;
  5145. r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  5146. if (r) {
  5147. __kvm_s390_exit();
  5148. return r;
  5149. }
  5150. return 0;
  5151. }
  5152. static void __exit kvm_s390_exit(void)
  5153. {
  5154. kvm_exit();
  5155. __kvm_s390_exit();
  5156. }
  5157. module_init(kvm_s390_init);
  5158. module_exit(kvm_s390_exit);
  5159. /*
  5160. * Enable autoloading of the kvm module.
  5161. * Note that we add the module alias here instead of virt/kvm/kvm_main.c
  5162. * since x86 takes a different approach.
  5163. */
  5164. #include <linux/miscdevice.h>
  5165. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  5166. MODULE_ALIAS("devname:kvm");