2
0

helper.c 410 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484
  1. /*
  2. * ARM generic helpers.
  3. *
  4. * This code is licensed under the GNU GPL v2 or later.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/log.h"
  10. #include "trace.h"
  11. #include "cpu.h"
  12. #include "internals.h"
  13. #include "cpu-features.h"
  14. #include "exec/helper-proto.h"
  15. #include "exec/page-protection.h"
  16. #include "qemu/main-loop.h"
  17. #include "qemu/timer.h"
  18. #include "qemu/bitops.h"
  19. #include "qemu/qemu-print.h"
  20. #include "exec/cputlb.h"
  21. #include "exec/exec-all.h"
  22. #include "exec/translation-block.h"
  23. #include "hw/irq.h"
  24. #include "system/cpu-timers.h"
  25. #include "system/kvm.h"
  26. #include "system/tcg.h"
  27. #include "qapi/error.h"
  28. #include "qemu/guest-random.h"
  29. #ifdef CONFIG_TCG
  30. #include "semihosting/common-semi.h"
  31. #endif
  32. #include "cpregs.h"
  33. #include "target/arm/gtimer.h"
  34. #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
  35. static void switch_mode(CPUARMState *env, int mode);
  36. static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
  37. {
  38. assert(ri->fieldoffset);
  39. if (cpreg_field_is_64bit(ri)) {
  40. return CPREG_FIELD64(env, ri);
  41. } else {
  42. return CPREG_FIELD32(env, ri);
  43. }
  44. }
  45. void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  46. {
  47. assert(ri->fieldoffset);
  48. if (cpreg_field_is_64bit(ri)) {
  49. CPREG_FIELD64(env, ri) = value;
  50. } else {
  51. CPREG_FIELD32(env, ri) = value;
  52. }
  53. }
  54. static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
  55. {
  56. return (char *)env + ri->fieldoffset;
  57. }
  58. uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
  59. {
  60. /* Raw read of a coprocessor register (as needed for migration, etc). */
  61. if (ri->type & ARM_CP_CONST) {
  62. return ri->resetvalue;
  63. } else if (ri->raw_readfn) {
  64. return ri->raw_readfn(env, ri);
  65. } else if (ri->readfn) {
  66. return ri->readfn(env, ri);
  67. } else {
  68. return raw_read(env, ri);
  69. }
  70. }
  71. static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
  72. uint64_t v)
  73. {
  74. /*
  75. * Raw write of a coprocessor register (as needed for migration, etc).
  76. * Note that constant registers are treated as write-ignored; the
  77. * caller should check for success by whether a readback gives the
  78. * value written.
  79. */
  80. if (ri->type & ARM_CP_CONST) {
  81. return;
  82. } else if (ri->raw_writefn) {
  83. ri->raw_writefn(env, ri, v);
  84. } else if (ri->writefn) {
  85. ri->writefn(env, ri, v);
  86. } else {
  87. raw_write(env, ri, v);
  88. }
  89. }
  90. static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
  91. {
  92. /*
  93. * Return true if the regdef would cause an assertion if you called
  94. * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
  95. * program bug for it not to have the NO_RAW flag).
  96. * NB that returning false here doesn't necessarily mean that calling
  97. * read/write_raw_cp_reg() is safe, because we can't distinguish "has
  98. * read/write access functions which are safe for raw use" from "has
  99. * read/write access functions which have side effects but has forgotten
  100. * to provide raw access functions".
  101. * The tests here line up with the conditions in read/write_raw_cp_reg()
  102. * and assertions in raw_read()/raw_write().
  103. */
  104. if ((ri->type & ARM_CP_CONST) ||
  105. ri->fieldoffset ||
  106. ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
  112. {
  113. /* Write the coprocessor state from cpu->env to the (index,value) list. */
  114. int i;
  115. bool ok = true;
  116. for (i = 0; i < cpu->cpreg_array_len; i++) {
  117. uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
  118. const ARMCPRegInfo *ri;
  119. uint64_t newval;
  120. ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
  121. if (!ri) {
  122. ok = false;
  123. continue;
  124. }
  125. if (ri->type & ARM_CP_NO_RAW) {
  126. continue;
  127. }
  128. newval = read_raw_cp_reg(&cpu->env, ri);
  129. if (kvm_sync) {
  130. /*
  131. * Only sync if the previous list->cpustate sync succeeded.
  132. * Rather than tracking the success/failure state for every
  133. * item in the list, we just recheck "does the raw write we must
  134. * have made in write_list_to_cpustate() read back OK" here.
  135. */
  136. uint64_t oldval = cpu->cpreg_values[i];
  137. if (oldval == newval) {
  138. continue;
  139. }
  140. write_raw_cp_reg(&cpu->env, ri, oldval);
  141. if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
  142. continue;
  143. }
  144. write_raw_cp_reg(&cpu->env, ri, newval);
  145. }
  146. cpu->cpreg_values[i] = newval;
  147. }
  148. return ok;
  149. }
  150. bool write_list_to_cpustate(ARMCPU *cpu)
  151. {
  152. int i;
  153. bool ok = true;
  154. for (i = 0; i < cpu->cpreg_array_len; i++) {
  155. uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
  156. uint64_t v = cpu->cpreg_values[i];
  157. const ARMCPRegInfo *ri;
  158. ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
  159. if (!ri) {
  160. ok = false;
  161. continue;
  162. }
  163. if (ri->type & ARM_CP_NO_RAW) {
  164. continue;
  165. }
  166. /*
  167. * Write value and confirm it reads back as written
  168. * (to catch read-only registers and partially read-only
  169. * registers where the incoming migration value doesn't match)
  170. */
  171. write_raw_cp_reg(&cpu->env, ri, v);
  172. if (read_raw_cp_reg(&cpu->env, ri) != v) {
  173. ok = false;
  174. }
  175. }
  176. return ok;
  177. }
  178. static void add_cpreg_to_list(gpointer key, gpointer opaque)
  179. {
  180. ARMCPU *cpu = opaque;
  181. uint32_t regidx = (uintptr_t)key;
  182. const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
  183. if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
  184. cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
  185. /* The value array need not be initialized at this point */
  186. cpu->cpreg_array_len++;
  187. }
  188. }
  189. static void count_cpreg(gpointer key, gpointer opaque)
  190. {
  191. ARMCPU *cpu = opaque;
  192. const ARMCPRegInfo *ri;
  193. ri = g_hash_table_lookup(cpu->cp_regs, key);
  194. if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
  195. cpu->cpreg_array_len++;
  196. }
  197. }
  198. static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
  199. {
  200. uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
  201. uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
  202. if (aidx > bidx) {
  203. return 1;
  204. }
  205. if (aidx < bidx) {
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. void init_cpreg_list(ARMCPU *cpu)
  211. {
  212. /*
  213. * Initialise the cpreg_tuples[] array based on the cp_regs hash.
  214. * Note that we require cpreg_tuples[] to be sorted by key ID.
  215. */
  216. GList *keys;
  217. int arraylen;
  218. keys = g_hash_table_get_keys(cpu->cp_regs);
  219. keys = g_list_sort(keys, cpreg_key_compare);
  220. cpu->cpreg_array_len = 0;
  221. g_list_foreach(keys, count_cpreg, cpu);
  222. arraylen = cpu->cpreg_array_len;
  223. cpu->cpreg_indexes = g_new(uint64_t, arraylen);
  224. cpu->cpreg_values = g_new(uint64_t, arraylen);
  225. cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
  226. cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
  227. cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
  228. cpu->cpreg_array_len = 0;
  229. g_list_foreach(keys, add_cpreg_to_list, cpu);
  230. assert(cpu->cpreg_array_len == arraylen);
  231. g_list_free(keys);
  232. }
  233. static bool arm_pan_enabled(CPUARMState *env)
  234. {
  235. if (is_a64(env)) {
  236. if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
  237. return false;
  238. }
  239. return env->pstate & PSTATE_PAN;
  240. } else {
  241. return env->uncached_cpsr & CPSR_PAN;
  242. }
  243. }
  244. /*
  245. * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
  246. */
  247. static CPAccessResult access_el3_aa32ns(CPUARMState *env,
  248. const ARMCPRegInfo *ri,
  249. bool isread)
  250. {
  251. if (!is_a64(env) && arm_current_el(env) == 3 &&
  252. arm_is_secure_below_el3(env)) {
  253. return CP_ACCESS_UNDEFINED;
  254. }
  255. return CP_ACCESS_OK;
  256. }
  257. /*
  258. * Some secure-only AArch32 registers trap to EL3 if used from
  259. * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
  260. * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
  261. * We assume that the .access field is set to PL1_RW.
  262. */
  263. static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
  264. const ARMCPRegInfo *ri,
  265. bool isread)
  266. {
  267. if (arm_current_el(env) == 3) {
  268. return CP_ACCESS_OK;
  269. }
  270. if (arm_is_secure_below_el3(env)) {
  271. if (env->cp15.scr_el3 & SCR_EEL2) {
  272. return CP_ACCESS_TRAP_EL2;
  273. }
  274. return CP_ACCESS_TRAP_EL3;
  275. }
  276. /* This will be EL1 NS and EL2 NS, which just UNDEF */
  277. return CP_ACCESS_UNDEFINED;
  278. }
  279. /*
  280. * Check for traps to performance monitor registers, which are controlled
  281. * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
  282. */
  283. static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
  284. bool isread)
  285. {
  286. int el = arm_current_el(env);
  287. uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
  288. if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
  289. return CP_ACCESS_TRAP_EL2;
  290. }
  291. if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
  292. return CP_ACCESS_TRAP_EL3;
  293. }
  294. return CP_ACCESS_OK;
  295. }
  296. /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
  297. CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
  298. bool isread)
  299. {
  300. if (arm_current_el(env) == 1) {
  301. uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
  302. if (arm_hcr_el2_eff(env) & trap) {
  303. return CP_ACCESS_TRAP_EL2;
  304. }
  305. }
  306. return CP_ACCESS_OK;
  307. }
  308. /* Check for traps from EL1 due to HCR_EL2.TSW. */
  309. static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
  310. bool isread)
  311. {
  312. if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
  313. return CP_ACCESS_TRAP_EL2;
  314. }
  315. return CP_ACCESS_OK;
  316. }
  317. /* Check for traps from EL1 due to HCR_EL2.TACR. */
  318. static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
  319. bool isread)
  320. {
  321. if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
  322. return CP_ACCESS_TRAP_EL2;
  323. }
  324. return CP_ACCESS_OK;
  325. }
  326. static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  327. {
  328. ARMCPU *cpu = env_archcpu(env);
  329. raw_write(env, ri, value);
  330. tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
  331. }
  332. static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  333. {
  334. ARMCPU *cpu = env_archcpu(env);
  335. if (raw_read(env, ri) != value) {
  336. /*
  337. * Unlike real hardware the qemu TLB uses virtual addresses,
  338. * not modified virtual addresses, so this causes a TLB flush.
  339. */
  340. tlb_flush(CPU(cpu));
  341. raw_write(env, ri, value);
  342. }
  343. }
  344. static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  345. uint64_t value)
  346. {
  347. ARMCPU *cpu = env_archcpu(env);
  348. if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
  349. && !extended_addresses_enabled(env)) {
  350. /*
  351. * For VMSA (when not using the LPAE long descriptor page table
  352. * format) this register includes the ASID, so do a TLB flush.
  353. * For PMSA it is purely a process ID and no action is needed.
  354. */
  355. tlb_flush(CPU(cpu));
  356. }
  357. raw_write(env, ri, value);
  358. }
  359. int alle1_tlbmask(CPUARMState *env)
  360. {
  361. /*
  362. * Note that the 'ALL' scope must invalidate both stage 1 and
  363. * stage 2 translations, whereas most other scopes only invalidate
  364. * stage 1 translations.
  365. *
  366. * For AArch32 this is only used for TLBIALLNSNH and VTTBR
  367. * writes, so only needs to apply to NS PL1&0, not S PL1&0.
  368. */
  369. return (ARMMMUIdxBit_E10_1 |
  370. ARMMMUIdxBit_E10_1_PAN |
  371. ARMMMUIdxBit_E10_0 |
  372. ARMMMUIdxBit_Stage2 |
  373. ARMMMUIdxBit_Stage2_S);
  374. }
  375. static const ARMCPRegInfo cp_reginfo[] = {
  376. /*
  377. * Define the secure and non-secure FCSE identifier CP registers
  378. * separately because there is no secure bank in V8 (no _EL3). This allows
  379. * the secure register to be properly reset and migrated. There is also no
  380. * v8 EL1 version of the register so the non-secure instance stands alone.
  381. */
  382. { .name = "FCSEIDR",
  383. .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
  384. .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
  385. .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
  386. .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
  387. { .name = "FCSEIDR_S",
  388. .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
  389. .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
  390. .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
  391. .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
  392. /*
  393. * Define the secure and non-secure context identifier CP registers
  394. * separately because there is no secure bank in V8 (no _EL3). This allows
  395. * the secure register to be properly reset and migrated. In the
  396. * non-secure case, the 32-bit register will have reset and migration
  397. * disabled during registration as it is handled by the 64-bit instance.
  398. */
  399. { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
  400. .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
  401. .access = PL1_RW, .accessfn = access_tvm_trvm,
  402. .fgt = FGT_CONTEXTIDR_EL1,
  403. .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
  404. .secure = ARM_CP_SECSTATE_NS,
  405. .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
  406. .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
  407. { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
  408. .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
  409. .access = PL1_RW, .accessfn = access_tvm_trvm,
  410. .secure = ARM_CP_SECSTATE_S,
  411. .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
  412. .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
  413. };
  414. static const ARMCPRegInfo not_v8_cp_reginfo[] = {
  415. /*
  416. * NB: Some of these registers exist in v8 but with more precise
  417. * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
  418. */
  419. /* MMU Domain access control / MPU write buffer control */
  420. { .name = "DACR",
  421. .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
  422. .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
  423. .writefn = dacr_write, .raw_writefn = raw_write,
  424. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
  425. offsetoflow32(CPUARMState, cp15.dacr_ns) } },
  426. /*
  427. * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
  428. * For v6 and v5, these mappings are overly broad.
  429. */
  430. { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
  431. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
  432. { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
  433. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
  434. { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
  435. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
  436. { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
  437. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
  438. /* Cache maintenance ops; some of this space may be overridden later. */
  439. { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
  440. .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
  441. .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
  442. };
  443. static const ARMCPRegInfo not_v6_cp_reginfo[] = {
  444. /*
  445. * Not all pre-v6 cores implemented this WFI, so this is slightly
  446. * over-broad.
  447. */
  448. { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
  449. .access = PL1_W, .type = ARM_CP_WFI },
  450. };
  451. static const ARMCPRegInfo not_v7_cp_reginfo[] = {
  452. /*
  453. * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
  454. * is UNPREDICTABLE; we choose to NOP as most implementations do).
  455. */
  456. { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
  457. .access = PL1_W, .type = ARM_CP_WFI },
  458. /*
  459. * L1 cache lockdown. Not architectural in v6 and earlier but in practice
  460. * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
  461. * OMAPCP will override this space.
  462. */
  463. { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
  464. .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
  465. .resetvalue = 0 },
  466. { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
  467. .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
  468. .resetvalue = 0 },
  469. /* v6 doesn't have the cache ID registers but Linux reads them anyway */
  470. { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
  471. .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
  472. .resetvalue = 0 },
  473. /*
  474. * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
  475. * implementing it as RAZ means the "debug architecture version" bits
  476. * will read as a reserved value, which should cause Linux to not try
  477. * to use the debug hardware.
  478. */
  479. { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
  480. .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  481. { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
  482. .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
  483. { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
  484. .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
  485. };
  486. static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  487. uint64_t value)
  488. {
  489. uint32_t mask = 0;
  490. /* In ARMv8 most bits of CPACR_EL1 are RES0. */
  491. if (!arm_feature(env, ARM_FEATURE_V8)) {
  492. /*
  493. * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
  494. * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
  495. * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
  496. */
  497. if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
  498. /* VFP coprocessor: cp10 & cp11 [23:20] */
  499. mask |= R_CPACR_ASEDIS_MASK |
  500. R_CPACR_D32DIS_MASK |
  501. R_CPACR_CP11_MASK |
  502. R_CPACR_CP10_MASK;
  503. if (!arm_feature(env, ARM_FEATURE_NEON)) {
  504. /* ASEDIS [31] bit is RAO/WI */
  505. value |= R_CPACR_ASEDIS_MASK;
  506. }
  507. /*
  508. * VFPv3 and upwards with NEON implement 32 double precision
  509. * registers (D0-D31).
  510. */
  511. if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
  512. /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
  513. value |= R_CPACR_D32DIS_MASK;
  514. }
  515. }
  516. value &= mask;
  517. }
  518. /*
  519. * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
  520. * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
  521. */
  522. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
  523. !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
  524. mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
  525. value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
  526. }
  527. env->cp15.cpacr_el1 = value;
  528. }
  529. static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  530. {
  531. /*
  532. * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
  533. * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
  534. */
  535. uint64_t value = env->cp15.cpacr_el1;
  536. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
  537. !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
  538. value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
  539. }
  540. return value;
  541. }
  542. static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  543. {
  544. /*
  545. * Call cpacr_write() so that we reset with the correct RAO bits set
  546. * for our CPU features.
  547. */
  548. cpacr_write(env, ri, 0);
  549. }
  550. static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
  551. bool isread)
  552. {
  553. if (arm_feature(env, ARM_FEATURE_V8)) {
  554. /* Check if CPACR accesses are to be trapped to EL2 */
  555. if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
  556. FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
  557. return CP_ACCESS_TRAP_EL2;
  558. /* Check if CPACR accesses are to be trapped to EL3 */
  559. } else if (arm_current_el(env) < 3 &&
  560. FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
  561. return CP_ACCESS_TRAP_EL3;
  562. }
  563. }
  564. return CP_ACCESS_OK;
  565. }
  566. static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
  567. bool isread)
  568. {
  569. /* Check if CPTR accesses are set to trap to EL3 */
  570. if (arm_current_el(env) == 2 &&
  571. FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
  572. return CP_ACCESS_TRAP_EL3;
  573. }
  574. return CP_ACCESS_OK;
  575. }
  576. static const ARMCPRegInfo v6_cp_reginfo[] = {
  577. /* prefetch by MVA in v6, NOP in v7 */
  578. { .name = "MVA_prefetch",
  579. .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
  580. .access = PL1_W, .type = ARM_CP_NOP },
  581. /*
  582. * We need to break the TB after ISB to execute self-modifying code
  583. * correctly and also to take any pending interrupts immediately.
  584. * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
  585. */
  586. { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
  587. .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
  588. { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
  589. .access = PL0_W, .type = ARM_CP_NOP },
  590. { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
  591. .access = PL0_W, .type = ARM_CP_NOP },
  592. { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
  593. .access = PL1_RW, .accessfn = access_tvm_trvm,
  594. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
  595. offsetof(CPUARMState, cp15.ifar_ns) },
  596. .resetvalue = 0, },
  597. /*
  598. * Watchpoint Fault Address Register : should actually only be present
  599. * for 1136, 1176, 11MPCore.
  600. */
  601. { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
  602. .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
  603. { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
  604. .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
  605. .fgt = FGT_CPACR_EL1,
  606. .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
  607. .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
  608. .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
  609. };
  610. typedef struct pm_event {
  611. uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
  612. /* If the event is supported on this CPU (used to generate PMCEID[01]) */
  613. bool (*supported)(CPUARMState *);
  614. /*
  615. * Retrieve the current count of the underlying event. The programmed
  616. * counters hold a difference from the return value from this function
  617. */
  618. uint64_t (*get_count)(CPUARMState *);
  619. /*
  620. * Return how many nanoseconds it will take (at a minimum) for count events
  621. * to occur. A negative value indicates the counter will never overflow, or
  622. * that the counter has otherwise arranged for the overflow bit to be set
  623. * and the PMU interrupt to be raised on overflow.
  624. */
  625. int64_t (*ns_per_count)(uint64_t);
  626. } pm_event;
  627. static bool event_always_supported(CPUARMState *env)
  628. {
  629. return true;
  630. }
  631. static uint64_t swinc_get_count(CPUARMState *env)
  632. {
  633. /*
  634. * SW_INCR events are written directly to the pmevcntr's by writes to
  635. * PMSWINC, so there is no underlying count maintained by the PMU itself
  636. */
  637. return 0;
  638. }
  639. static int64_t swinc_ns_per(uint64_t ignored)
  640. {
  641. return -1;
  642. }
  643. /*
  644. * Return the underlying cycle count for the PMU cycle counters. If we're in
  645. * usermode, simply return 0.
  646. */
  647. static uint64_t cycles_get_count(CPUARMState *env)
  648. {
  649. #ifndef CONFIG_USER_ONLY
  650. return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
  651. ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
  652. #else
  653. return cpu_get_host_ticks();
  654. #endif
  655. }
  656. #ifndef CONFIG_USER_ONLY
  657. static int64_t cycles_ns_per(uint64_t cycles)
  658. {
  659. return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
  660. }
  661. static bool instructions_supported(CPUARMState *env)
  662. {
  663. /* Precise instruction counting */
  664. return icount_enabled() == ICOUNT_PRECISE;
  665. }
  666. static uint64_t instructions_get_count(CPUARMState *env)
  667. {
  668. assert(icount_enabled() == ICOUNT_PRECISE);
  669. return (uint64_t)icount_get_raw();
  670. }
  671. static int64_t instructions_ns_per(uint64_t icount)
  672. {
  673. assert(icount_enabled() == ICOUNT_PRECISE);
  674. return icount_to_ns((int64_t)icount);
  675. }
  676. #endif
  677. static bool pmuv3p1_events_supported(CPUARMState *env)
  678. {
  679. /* For events which are supported in any v8.1 PMU */
  680. return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
  681. }
  682. static bool pmuv3p4_events_supported(CPUARMState *env)
  683. {
  684. /* For events which are supported in any v8.1 PMU */
  685. return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
  686. }
  687. static uint64_t zero_event_get_count(CPUARMState *env)
  688. {
  689. /* For events which on QEMU never fire, so their count is always zero */
  690. return 0;
  691. }
  692. static int64_t zero_event_ns_per(uint64_t cycles)
  693. {
  694. /* An event which never fires can never overflow */
  695. return -1;
  696. }
  697. static const pm_event pm_events[] = {
  698. { .number = 0x000, /* SW_INCR */
  699. .supported = event_always_supported,
  700. .get_count = swinc_get_count,
  701. .ns_per_count = swinc_ns_per,
  702. },
  703. #ifndef CONFIG_USER_ONLY
  704. { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
  705. .supported = instructions_supported,
  706. .get_count = instructions_get_count,
  707. .ns_per_count = instructions_ns_per,
  708. },
  709. { .number = 0x011, /* CPU_CYCLES, Cycle */
  710. .supported = event_always_supported,
  711. .get_count = cycles_get_count,
  712. .ns_per_count = cycles_ns_per,
  713. },
  714. #endif
  715. { .number = 0x023, /* STALL_FRONTEND */
  716. .supported = pmuv3p1_events_supported,
  717. .get_count = zero_event_get_count,
  718. .ns_per_count = zero_event_ns_per,
  719. },
  720. { .number = 0x024, /* STALL_BACKEND */
  721. .supported = pmuv3p1_events_supported,
  722. .get_count = zero_event_get_count,
  723. .ns_per_count = zero_event_ns_per,
  724. },
  725. { .number = 0x03c, /* STALL */
  726. .supported = pmuv3p4_events_supported,
  727. .get_count = zero_event_get_count,
  728. .ns_per_count = zero_event_ns_per,
  729. },
  730. };
  731. /*
  732. * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
  733. * events (i.e. the statistical profiling extension), this implementation
  734. * should first be updated to something sparse instead of the current
  735. * supported_event_map[] array.
  736. */
  737. #define MAX_EVENT_ID 0x3c
  738. #define UNSUPPORTED_EVENT UINT16_MAX
  739. static uint16_t supported_event_map[MAX_EVENT_ID + 1];
  740. /*
  741. * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
  742. * of ARM event numbers to indices in our pm_events array.
  743. *
  744. * Note: Events in the 0x40XX range are not currently supported.
  745. */
  746. void pmu_init(ARMCPU *cpu)
  747. {
  748. unsigned int i;
  749. /*
  750. * Empty supported_event_map and cpu->pmceid[01] before adding supported
  751. * events to them
  752. */
  753. for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
  754. supported_event_map[i] = UNSUPPORTED_EVENT;
  755. }
  756. cpu->pmceid0 = 0;
  757. cpu->pmceid1 = 0;
  758. for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
  759. const pm_event *cnt = &pm_events[i];
  760. assert(cnt->number <= MAX_EVENT_ID);
  761. /* We do not currently support events in the 0x40xx range */
  762. assert(cnt->number <= 0x3f);
  763. if (cnt->supported(&cpu->env)) {
  764. supported_event_map[cnt->number] = i;
  765. uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
  766. if (cnt->number & 0x20) {
  767. cpu->pmceid1 |= event_mask;
  768. } else {
  769. cpu->pmceid0 |= event_mask;
  770. }
  771. }
  772. }
  773. }
  774. /*
  775. * Check at runtime whether a PMU event is supported for the current machine
  776. */
  777. static bool event_supported(uint16_t number)
  778. {
  779. if (number > MAX_EVENT_ID) {
  780. return false;
  781. }
  782. return supported_event_map[number] != UNSUPPORTED_EVENT;
  783. }
  784. static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
  785. bool isread)
  786. {
  787. /*
  788. * Performance monitor registers user accessibility is controlled
  789. * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
  790. * trapping to EL2 or EL3 for other accesses.
  791. */
  792. int el = arm_current_el(env);
  793. uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
  794. if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
  795. return CP_ACCESS_TRAP_EL1;
  796. }
  797. if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
  798. return CP_ACCESS_TRAP_EL2;
  799. }
  800. if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
  801. return CP_ACCESS_TRAP_EL3;
  802. }
  803. return CP_ACCESS_OK;
  804. }
  805. static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
  806. const ARMCPRegInfo *ri,
  807. bool isread)
  808. {
  809. /* ER: event counter read trap control */
  810. if (arm_feature(env, ARM_FEATURE_V8)
  811. && arm_current_el(env) == 0
  812. && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
  813. && isread) {
  814. return CP_ACCESS_OK;
  815. }
  816. return pmreg_access(env, ri, isread);
  817. }
  818. static CPAccessResult pmreg_access_swinc(CPUARMState *env,
  819. const ARMCPRegInfo *ri,
  820. bool isread)
  821. {
  822. /* SW: software increment write trap control */
  823. if (arm_feature(env, ARM_FEATURE_V8)
  824. && arm_current_el(env) == 0
  825. && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
  826. && !isread) {
  827. return CP_ACCESS_OK;
  828. }
  829. return pmreg_access(env, ri, isread);
  830. }
  831. static CPAccessResult pmreg_access_selr(CPUARMState *env,
  832. const ARMCPRegInfo *ri,
  833. bool isread)
  834. {
  835. /* ER: event counter read trap control */
  836. if (arm_feature(env, ARM_FEATURE_V8)
  837. && arm_current_el(env) == 0
  838. && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
  839. return CP_ACCESS_OK;
  840. }
  841. return pmreg_access(env, ri, isread);
  842. }
  843. static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
  844. const ARMCPRegInfo *ri,
  845. bool isread)
  846. {
  847. /* CR: cycle counter read trap control */
  848. if (arm_feature(env, ARM_FEATURE_V8)
  849. && arm_current_el(env) == 0
  850. && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
  851. && isread) {
  852. return CP_ACCESS_OK;
  853. }
  854. return pmreg_access(env, ri, isread);
  855. }
  856. /*
  857. * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
  858. * We use these to decide whether we need to wrap a write to MDCR_EL2
  859. * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
  860. */
  861. #define MDCR_EL2_PMU_ENABLE_BITS \
  862. (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
  863. #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
  864. /*
  865. * Returns true if the counter (pass 31 for PMCCNTR) should count events using
  866. * the current EL, security state, and register configuration.
  867. */
  868. static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
  869. {
  870. uint64_t filter;
  871. bool e, p, u, nsk, nsu, nsh, m;
  872. bool enabled, prohibited = false, filtered;
  873. bool secure = arm_is_secure(env);
  874. int el = arm_current_el(env);
  875. uint64_t mdcr_el2;
  876. uint8_t hpmn;
  877. /*
  878. * We might be called for M-profile cores where MDCR_EL2 doesn't
  879. * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
  880. * must be before we read that value.
  881. */
  882. if (!arm_feature(env, ARM_FEATURE_PMU)) {
  883. return false;
  884. }
  885. mdcr_el2 = arm_mdcr_el2_eff(env);
  886. hpmn = mdcr_el2 & MDCR_HPMN;
  887. if (!arm_feature(env, ARM_FEATURE_EL2) ||
  888. (counter < hpmn || counter == 31)) {
  889. e = env->cp15.c9_pmcr & PMCRE;
  890. } else {
  891. e = mdcr_el2 & MDCR_HPME;
  892. }
  893. enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
  894. /* Is event counting prohibited? */
  895. if (el == 2 && (counter < hpmn || counter == 31)) {
  896. prohibited = mdcr_el2 & MDCR_HPMD;
  897. }
  898. if (secure) {
  899. prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
  900. }
  901. if (counter == 31) {
  902. /*
  903. * The cycle counter defaults to running. PMCR.DP says "disable
  904. * the cycle counter when event counting is prohibited".
  905. * Some MDCR bits disable the cycle counter specifically.
  906. */
  907. prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
  908. if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
  909. if (secure) {
  910. prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
  911. }
  912. if (el == 2) {
  913. prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
  914. }
  915. }
  916. }
  917. if (counter == 31) {
  918. filter = env->cp15.pmccfiltr_el0;
  919. } else {
  920. filter = env->cp15.c14_pmevtyper[counter];
  921. }
  922. p = filter & PMXEVTYPER_P;
  923. u = filter & PMXEVTYPER_U;
  924. nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
  925. nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
  926. nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
  927. m = arm_el_is_aa64(env, 1) &&
  928. arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
  929. if (el == 0) {
  930. filtered = secure ? u : u != nsu;
  931. } else if (el == 1) {
  932. filtered = secure ? p : p != nsk;
  933. } else if (el == 2) {
  934. filtered = !nsh;
  935. } else { /* EL3 */
  936. filtered = m != p;
  937. }
  938. if (counter != 31) {
  939. /*
  940. * If not checking PMCCNTR, ensure the counter is setup to an event we
  941. * support
  942. */
  943. uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
  944. if (!event_supported(event)) {
  945. return false;
  946. }
  947. }
  948. return enabled && !prohibited && !filtered;
  949. }
  950. static void pmu_update_irq(CPUARMState *env)
  951. {
  952. ARMCPU *cpu = env_archcpu(env);
  953. qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
  954. (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
  955. }
  956. static bool pmccntr_clockdiv_enabled(CPUARMState *env)
  957. {
  958. /*
  959. * Return true if the clock divider is enabled and the cycle counter
  960. * is supposed to tick only once every 64 clock cycles. This is
  961. * controlled by PMCR.D, but if PMCR.LC is set to enable the long
  962. * (64-bit) cycle counter PMCR.D has no effect.
  963. */
  964. return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
  965. }
  966. static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
  967. {
  968. /* Return true if the specified event counter is configured to be 64 bit */
  969. /* This isn't intended to be used with the cycle counter */
  970. assert(counter < 31);
  971. if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
  972. return false;
  973. }
  974. if (arm_feature(env, ARM_FEATURE_EL2)) {
  975. /*
  976. * MDCR_EL2.HLP still applies even when EL2 is disabled in the
  977. * current security state, so we don't use arm_mdcr_el2_eff() here.
  978. */
  979. bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
  980. int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
  981. if (counter >= hpmn) {
  982. return hlp;
  983. }
  984. }
  985. return env->cp15.c9_pmcr & PMCRLP;
  986. }
  987. /*
  988. * Ensure c15_ccnt is the guest-visible count so that operations such as
  989. * enabling/disabling the counter or filtering, modifying the count itself,
  990. * etc. can be done logically. This is essentially a no-op if the counter is
  991. * not enabled at the time of the call.
  992. */
  993. static void pmccntr_op_start(CPUARMState *env)
  994. {
  995. uint64_t cycles = cycles_get_count(env);
  996. if (pmu_counter_enabled(env, 31)) {
  997. uint64_t eff_cycles = cycles;
  998. if (pmccntr_clockdiv_enabled(env)) {
  999. eff_cycles /= 64;
  1000. }
  1001. uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
  1002. uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
  1003. 1ull << 63 : 1ull << 31;
  1004. if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
  1005. env->cp15.c9_pmovsr |= (1ULL << 31);
  1006. pmu_update_irq(env);
  1007. }
  1008. env->cp15.c15_ccnt = new_pmccntr;
  1009. }
  1010. env->cp15.c15_ccnt_delta = cycles;
  1011. }
  1012. /*
  1013. * If PMCCNTR is enabled, recalculate the delta between the clock and the
  1014. * guest-visible count. A call to pmccntr_op_finish should follow every call to
  1015. * pmccntr_op_start.
  1016. */
  1017. static void pmccntr_op_finish(CPUARMState *env)
  1018. {
  1019. if (pmu_counter_enabled(env, 31)) {
  1020. #ifndef CONFIG_USER_ONLY
  1021. /* Calculate when the counter will next overflow */
  1022. uint64_t remaining_cycles = -env->cp15.c15_ccnt;
  1023. if (!(env->cp15.c9_pmcr & PMCRLC)) {
  1024. remaining_cycles = (uint32_t)remaining_cycles;
  1025. }
  1026. int64_t overflow_in = cycles_ns_per(remaining_cycles);
  1027. if (overflow_in > 0) {
  1028. int64_t overflow_at;
  1029. if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
  1030. overflow_in, &overflow_at)) {
  1031. ARMCPU *cpu = env_archcpu(env);
  1032. timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
  1033. }
  1034. }
  1035. #endif
  1036. uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
  1037. if (pmccntr_clockdiv_enabled(env)) {
  1038. prev_cycles /= 64;
  1039. }
  1040. env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
  1041. }
  1042. }
  1043. static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
  1044. {
  1045. uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
  1046. uint64_t count = 0;
  1047. if (event_supported(event)) {
  1048. uint16_t event_idx = supported_event_map[event];
  1049. count = pm_events[event_idx].get_count(env);
  1050. }
  1051. if (pmu_counter_enabled(env, counter)) {
  1052. uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
  1053. uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
  1054. 1ULL << 63 : 1ULL << 31;
  1055. if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
  1056. env->cp15.c9_pmovsr |= (1 << counter);
  1057. pmu_update_irq(env);
  1058. }
  1059. env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
  1060. }
  1061. env->cp15.c14_pmevcntr_delta[counter] = count;
  1062. }
  1063. static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
  1064. {
  1065. if (pmu_counter_enabled(env, counter)) {
  1066. #ifndef CONFIG_USER_ONLY
  1067. uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
  1068. uint16_t event_idx = supported_event_map[event];
  1069. uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
  1070. int64_t overflow_in;
  1071. if (!pmevcntr_is_64_bit(env, counter)) {
  1072. delta = (uint32_t)delta;
  1073. }
  1074. overflow_in = pm_events[event_idx].ns_per_count(delta);
  1075. if (overflow_in > 0) {
  1076. int64_t overflow_at;
  1077. if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
  1078. overflow_in, &overflow_at)) {
  1079. ARMCPU *cpu = env_archcpu(env);
  1080. timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
  1081. }
  1082. }
  1083. #endif
  1084. env->cp15.c14_pmevcntr_delta[counter] -=
  1085. env->cp15.c14_pmevcntr[counter];
  1086. }
  1087. }
  1088. void pmu_op_start(CPUARMState *env)
  1089. {
  1090. unsigned int i;
  1091. pmccntr_op_start(env);
  1092. for (i = 0; i < pmu_num_counters(env); i++) {
  1093. pmevcntr_op_start(env, i);
  1094. }
  1095. }
  1096. void pmu_op_finish(CPUARMState *env)
  1097. {
  1098. unsigned int i;
  1099. pmccntr_op_finish(env);
  1100. for (i = 0; i < pmu_num_counters(env); i++) {
  1101. pmevcntr_op_finish(env, i);
  1102. }
  1103. }
  1104. void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
  1105. {
  1106. pmu_op_start(&cpu->env);
  1107. }
  1108. void pmu_post_el_change(ARMCPU *cpu, void *ignored)
  1109. {
  1110. pmu_op_finish(&cpu->env);
  1111. }
  1112. void arm_pmu_timer_cb(void *opaque)
  1113. {
  1114. ARMCPU *cpu = opaque;
  1115. /*
  1116. * Update all the counter values based on the current underlying counts,
  1117. * triggering interrupts to be raised, if necessary. pmu_op_finish() also
  1118. * has the effect of setting the cpu->pmu_timer to the next earliest time a
  1119. * counter may expire.
  1120. */
  1121. pmu_op_start(&cpu->env);
  1122. pmu_op_finish(&cpu->env);
  1123. }
  1124. static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1125. uint64_t value)
  1126. {
  1127. pmu_op_start(env);
  1128. if (value & PMCRC) {
  1129. /* The counter has been reset */
  1130. env->cp15.c15_ccnt = 0;
  1131. }
  1132. if (value & PMCRP) {
  1133. unsigned int i;
  1134. for (i = 0; i < pmu_num_counters(env); i++) {
  1135. env->cp15.c14_pmevcntr[i] = 0;
  1136. }
  1137. }
  1138. env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
  1139. env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
  1140. pmu_op_finish(env);
  1141. }
  1142. static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1143. {
  1144. uint64_t pmcr = env->cp15.c9_pmcr;
  1145. /*
  1146. * If EL2 is implemented and enabled for the current security state, reads
  1147. * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
  1148. */
  1149. if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
  1150. pmcr &= ~PMCRN_MASK;
  1151. pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
  1152. }
  1153. return pmcr;
  1154. }
  1155. static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1156. uint64_t value)
  1157. {
  1158. unsigned int i;
  1159. uint64_t overflow_mask, new_pmswinc;
  1160. for (i = 0; i < pmu_num_counters(env); i++) {
  1161. /* Increment a counter's count iff: */
  1162. if ((value & (1 << i)) && /* counter's bit is set */
  1163. /* counter is enabled and not filtered */
  1164. pmu_counter_enabled(env, i) &&
  1165. /* counter is SW_INCR */
  1166. (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
  1167. pmevcntr_op_start(env, i);
  1168. /*
  1169. * Detect if this write causes an overflow since we can't predict
  1170. * PMSWINC overflows like we can for other events
  1171. */
  1172. new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
  1173. overflow_mask = pmevcntr_is_64_bit(env, i) ?
  1174. 1ULL << 63 : 1ULL << 31;
  1175. if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
  1176. env->cp15.c9_pmovsr |= (1 << i);
  1177. pmu_update_irq(env);
  1178. }
  1179. env->cp15.c14_pmevcntr[i] = new_pmswinc;
  1180. pmevcntr_op_finish(env, i);
  1181. }
  1182. }
  1183. }
  1184. static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1185. {
  1186. uint64_t ret;
  1187. pmccntr_op_start(env);
  1188. ret = env->cp15.c15_ccnt;
  1189. pmccntr_op_finish(env);
  1190. return ret;
  1191. }
  1192. static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1193. uint64_t value)
  1194. {
  1195. /*
  1196. * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
  1197. * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
  1198. * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
  1199. * accessed.
  1200. */
  1201. env->cp15.c9_pmselr = value & 0x1f;
  1202. }
  1203. static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1204. uint64_t value)
  1205. {
  1206. pmccntr_op_start(env);
  1207. env->cp15.c15_ccnt = value;
  1208. pmccntr_op_finish(env);
  1209. }
  1210. static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
  1211. uint64_t value)
  1212. {
  1213. uint64_t cur_val = pmccntr_read(env, NULL);
  1214. pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
  1215. }
  1216. static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1217. uint64_t value)
  1218. {
  1219. pmccntr_op_start(env);
  1220. env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
  1221. pmccntr_op_finish(env);
  1222. }
  1223. static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
  1224. uint64_t value)
  1225. {
  1226. pmccntr_op_start(env);
  1227. /* M is not accessible from AArch32 */
  1228. env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
  1229. (value & PMCCFILTR);
  1230. pmccntr_op_finish(env);
  1231. }
  1232. static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
  1233. {
  1234. /* M is not visible in AArch32 */
  1235. return env->cp15.pmccfiltr_el0 & PMCCFILTR;
  1236. }
  1237. static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1238. uint64_t value)
  1239. {
  1240. pmu_op_start(env);
  1241. value &= pmu_counter_mask(env);
  1242. env->cp15.c9_pmcnten |= value;
  1243. pmu_op_finish(env);
  1244. }
  1245. static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1246. uint64_t value)
  1247. {
  1248. pmu_op_start(env);
  1249. value &= pmu_counter_mask(env);
  1250. env->cp15.c9_pmcnten &= ~value;
  1251. pmu_op_finish(env);
  1252. }
  1253. static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1254. uint64_t value)
  1255. {
  1256. value &= pmu_counter_mask(env);
  1257. env->cp15.c9_pmovsr &= ~value;
  1258. pmu_update_irq(env);
  1259. }
  1260. static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1261. uint64_t value)
  1262. {
  1263. value &= pmu_counter_mask(env);
  1264. env->cp15.c9_pmovsr |= value;
  1265. pmu_update_irq(env);
  1266. }
  1267. static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1268. uint64_t value, const uint8_t counter)
  1269. {
  1270. if (counter == 31) {
  1271. pmccfiltr_write(env, ri, value);
  1272. } else if (counter < pmu_num_counters(env)) {
  1273. pmevcntr_op_start(env, counter);
  1274. /*
  1275. * If this counter's event type is changing, store the current
  1276. * underlying count for the new type in c14_pmevcntr_delta[counter] so
  1277. * pmevcntr_op_finish has the correct baseline when it converts back to
  1278. * a delta.
  1279. */
  1280. uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
  1281. PMXEVTYPER_EVTCOUNT;
  1282. uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
  1283. if (old_event != new_event) {
  1284. uint64_t count = 0;
  1285. if (event_supported(new_event)) {
  1286. uint16_t event_idx = supported_event_map[new_event];
  1287. count = pm_events[event_idx].get_count(env);
  1288. }
  1289. env->cp15.c14_pmevcntr_delta[counter] = count;
  1290. }
  1291. env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
  1292. pmevcntr_op_finish(env, counter);
  1293. }
  1294. /*
  1295. * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
  1296. * PMSELR value is equal to or greater than the number of implemented
  1297. * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
  1298. */
  1299. }
  1300. static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
  1301. const uint8_t counter)
  1302. {
  1303. if (counter == 31) {
  1304. return env->cp15.pmccfiltr_el0;
  1305. } else if (counter < pmu_num_counters(env)) {
  1306. return env->cp15.c14_pmevtyper[counter];
  1307. } else {
  1308. /*
  1309. * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
  1310. * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
  1311. */
  1312. return 0;
  1313. }
  1314. }
  1315. static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
  1316. uint64_t value)
  1317. {
  1318. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1319. pmevtyper_write(env, ri, value, counter);
  1320. }
  1321. static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
  1322. uint64_t value)
  1323. {
  1324. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1325. env->cp15.c14_pmevtyper[counter] = value;
  1326. /*
  1327. * pmevtyper_rawwrite is called between a pair of pmu_op_start and
  1328. * pmu_op_finish calls when loading saved state for a migration. Because
  1329. * we're potentially updating the type of event here, the value written to
  1330. * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
  1331. * different counter type. Therefore, we need to set this value to the
  1332. * current count for the counter type we're writing so that pmu_op_finish
  1333. * has the correct count for its calculation.
  1334. */
  1335. uint16_t event = value & PMXEVTYPER_EVTCOUNT;
  1336. if (event_supported(event)) {
  1337. uint16_t event_idx = supported_event_map[event];
  1338. env->cp15.c14_pmevcntr_delta[counter] =
  1339. pm_events[event_idx].get_count(env);
  1340. }
  1341. }
  1342. static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
  1343. {
  1344. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1345. return pmevtyper_read(env, ri, counter);
  1346. }
  1347. static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1348. uint64_t value)
  1349. {
  1350. pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
  1351. }
  1352. static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1353. {
  1354. return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
  1355. }
  1356. static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1357. uint64_t value, uint8_t counter)
  1358. {
  1359. if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
  1360. /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
  1361. value &= MAKE_64BIT_MASK(0, 32);
  1362. }
  1363. if (counter < pmu_num_counters(env)) {
  1364. pmevcntr_op_start(env, counter);
  1365. env->cp15.c14_pmevcntr[counter] = value;
  1366. pmevcntr_op_finish(env, counter);
  1367. }
  1368. /*
  1369. * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
  1370. * are CONSTRAINED UNPREDICTABLE.
  1371. */
  1372. }
  1373. static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
  1374. uint8_t counter)
  1375. {
  1376. if (counter < pmu_num_counters(env)) {
  1377. uint64_t ret;
  1378. pmevcntr_op_start(env, counter);
  1379. ret = env->cp15.c14_pmevcntr[counter];
  1380. pmevcntr_op_finish(env, counter);
  1381. if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
  1382. /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
  1383. ret &= MAKE_64BIT_MASK(0, 32);
  1384. }
  1385. return ret;
  1386. } else {
  1387. /*
  1388. * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
  1389. * are CONSTRAINED UNPREDICTABLE.
  1390. */
  1391. return 0;
  1392. }
  1393. }
  1394. static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
  1395. uint64_t value)
  1396. {
  1397. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1398. pmevcntr_write(env, ri, value, counter);
  1399. }
  1400. static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
  1401. {
  1402. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1403. return pmevcntr_read(env, ri, counter);
  1404. }
  1405. static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
  1406. uint64_t value)
  1407. {
  1408. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1409. assert(counter < pmu_num_counters(env));
  1410. env->cp15.c14_pmevcntr[counter] = value;
  1411. pmevcntr_write(env, ri, value, counter);
  1412. }
  1413. static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
  1414. {
  1415. uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
  1416. assert(counter < pmu_num_counters(env));
  1417. return env->cp15.c14_pmevcntr[counter];
  1418. }
  1419. static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1420. uint64_t value)
  1421. {
  1422. pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
  1423. }
  1424. static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1425. {
  1426. return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
  1427. }
  1428. static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1429. uint64_t value)
  1430. {
  1431. if (arm_feature(env, ARM_FEATURE_V8)) {
  1432. env->cp15.c9_pmuserenr = value & 0xf;
  1433. } else {
  1434. env->cp15.c9_pmuserenr = value & 1;
  1435. }
  1436. }
  1437. static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1438. uint64_t value)
  1439. {
  1440. /* We have no event counters so only the C bit can be changed */
  1441. value &= pmu_counter_mask(env);
  1442. env->cp15.c9_pminten |= value;
  1443. pmu_update_irq(env);
  1444. }
  1445. static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1446. uint64_t value)
  1447. {
  1448. value &= pmu_counter_mask(env);
  1449. env->cp15.c9_pminten &= ~value;
  1450. pmu_update_irq(env);
  1451. }
  1452. static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1453. uint64_t value)
  1454. {
  1455. /*
  1456. * Note that even though the AArch64 view of this register has bits
  1457. * [10:0] all RES0 we can only mask the bottom 5, to comply with the
  1458. * architectural requirements for bits which are RES0 only in some
  1459. * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
  1460. * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
  1461. */
  1462. raw_write(env, ri, value & ~0x1FULL);
  1463. }
  1464. static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  1465. {
  1466. /* Begin with base v8.0 state. */
  1467. uint64_t valid_mask = 0x3fff;
  1468. ARMCPU *cpu = env_archcpu(env);
  1469. uint64_t changed;
  1470. /*
  1471. * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
  1472. * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
  1473. * Instead, choose the format based on the mode of EL3.
  1474. */
  1475. if (arm_el_is_aa64(env, 3)) {
  1476. value |= SCR_FW | SCR_AW; /* RES1 */
  1477. valid_mask &= ~SCR_NET; /* RES0 */
  1478. if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
  1479. !cpu_isar_feature(aa64_aa32_el2, cpu)) {
  1480. value |= SCR_RW; /* RAO/WI */
  1481. }
  1482. if (cpu_isar_feature(aa64_ras, cpu)) {
  1483. valid_mask |= SCR_TERR;
  1484. }
  1485. if (cpu_isar_feature(aa64_lor, cpu)) {
  1486. valid_mask |= SCR_TLOR;
  1487. }
  1488. if (cpu_isar_feature(aa64_pauth, cpu)) {
  1489. valid_mask |= SCR_API | SCR_APK;
  1490. }
  1491. if (cpu_isar_feature(aa64_sel2, cpu)) {
  1492. valid_mask |= SCR_EEL2;
  1493. } else if (cpu_isar_feature(aa64_rme, cpu)) {
  1494. /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
  1495. value |= SCR_NS;
  1496. }
  1497. if (cpu_isar_feature(aa64_mte, cpu)) {
  1498. valid_mask |= SCR_ATA;
  1499. }
  1500. if (cpu_isar_feature(aa64_scxtnum, cpu)) {
  1501. valid_mask |= SCR_ENSCXT;
  1502. }
  1503. if (cpu_isar_feature(aa64_doublefault, cpu)) {
  1504. valid_mask |= SCR_EASE | SCR_NMEA;
  1505. }
  1506. if (cpu_isar_feature(aa64_sme, cpu)) {
  1507. valid_mask |= SCR_ENTP2;
  1508. }
  1509. if (cpu_isar_feature(aa64_hcx, cpu)) {
  1510. valid_mask |= SCR_HXEN;
  1511. }
  1512. if (cpu_isar_feature(aa64_fgt, cpu)) {
  1513. valid_mask |= SCR_FGTEN;
  1514. }
  1515. if (cpu_isar_feature(aa64_rme, cpu)) {
  1516. valid_mask |= SCR_NSE | SCR_GPF;
  1517. }
  1518. if (cpu_isar_feature(aa64_ecv, cpu)) {
  1519. valid_mask |= SCR_ECVEN;
  1520. }
  1521. } else {
  1522. valid_mask &= ~(SCR_RW | SCR_ST);
  1523. if (cpu_isar_feature(aa32_ras, cpu)) {
  1524. valid_mask |= SCR_TERR;
  1525. }
  1526. }
  1527. if (!arm_feature(env, ARM_FEATURE_EL2)) {
  1528. valid_mask &= ~SCR_HCE;
  1529. /*
  1530. * On ARMv7, SMD (or SCD as it is called in v7) is only
  1531. * supported if EL2 exists. The bit is UNK/SBZP when
  1532. * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
  1533. * when EL2 is unavailable.
  1534. * On ARMv8, this bit is always available.
  1535. */
  1536. if (arm_feature(env, ARM_FEATURE_V7) &&
  1537. !arm_feature(env, ARM_FEATURE_V8)) {
  1538. valid_mask &= ~SCR_SMD;
  1539. }
  1540. }
  1541. /* Clear all-context RES0 bits. */
  1542. value &= valid_mask;
  1543. changed = env->cp15.scr_el3 ^ value;
  1544. env->cp15.scr_el3 = value;
  1545. /*
  1546. * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
  1547. * we must invalidate all TLBs below EL3.
  1548. */
  1549. if (changed & (SCR_NS | SCR_NSE)) {
  1550. tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
  1551. ARMMMUIdxBit_E20_0 |
  1552. ARMMMUIdxBit_E10_1 |
  1553. ARMMMUIdxBit_E20_2 |
  1554. ARMMMUIdxBit_E10_1_PAN |
  1555. ARMMMUIdxBit_E20_2_PAN |
  1556. ARMMMUIdxBit_E2));
  1557. }
  1558. }
  1559. static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  1560. {
  1561. /*
  1562. * scr_write will set the RES1 bits on an AArch64-only CPU.
  1563. * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
  1564. */
  1565. scr_write(env, ri, 0);
  1566. }
  1567. static CPAccessResult access_tid4(CPUARMState *env,
  1568. const ARMCPRegInfo *ri,
  1569. bool isread)
  1570. {
  1571. if (arm_current_el(env) == 1 &&
  1572. (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
  1573. return CP_ACCESS_TRAP_EL2;
  1574. }
  1575. return CP_ACCESS_OK;
  1576. }
  1577. static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1578. {
  1579. ARMCPU *cpu = env_archcpu(env);
  1580. /*
  1581. * Acquire the CSSELR index from the bank corresponding to the CCSIDR
  1582. * bank
  1583. */
  1584. uint32_t index = A32_BANKED_REG_GET(env, csselr,
  1585. ri->secure & ARM_CP_SECSTATE_S);
  1586. return cpu->ccsidr[index];
  1587. }
  1588. static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1589. uint64_t value)
  1590. {
  1591. raw_write(env, ri, value & 0xf);
  1592. }
  1593. static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1594. {
  1595. CPUState *cs = env_cpu(env);
  1596. bool el1 = arm_current_el(env) == 1;
  1597. uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
  1598. uint64_t ret = 0;
  1599. if (hcr_el2 & HCR_IMO) {
  1600. if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
  1601. ret |= CPSR_I;
  1602. }
  1603. if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
  1604. ret |= ISR_IS;
  1605. ret |= CPSR_I;
  1606. }
  1607. } else {
  1608. if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
  1609. ret |= CPSR_I;
  1610. }
  1611. if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
  1612. ret |= ISR_IS;
  1613. ret |= CPSR_I;
  1614. }
  1615. }
  1616. if (hcr_el2 & HCR_FMO) {
  1617. if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
  1618. ret |= CPSR_F;
  1619. }
  1620. if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
  1621. ret |= ISR_FS;
  1622. ret |= CPSR_F;
  1623. }
  1624. } else {
  1625. if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
  1626. ret |= CPSR_F;
  1627. }
  1628. }
  1629. if (hcr_el2 & HCR_AMO) {
  1630. if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
  1631. ret |= CPSR_A;
  1632. }
  1633. }
  1634. return ret;
  1635. }
  1636. static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
  1637. bool isread)
  1638. {
  1639. if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
  1640. return CP_ACCESS_TRAP_EL2;
  1641. }
  1642. return CP_ACCESS_OK;
  1643. }
  1644. static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
  1645. bool isread)
  1646. {
  1647. if (arm_feature(env, ARM_FEATURE_V8)) {
  1648. return access_aa64_tid1(env, ri, isread);
  1649. }
  1650. return CP_ACCESS_OK;
  1651. }
  1652. static const ARMCPRegInfo v7_cp_reginfo[] = {
  1653. /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
  1654. { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
  1655. .access = PL1_W, .type = ARM_CP_NOP },
  1656. /*
  1657. * Performance monitors are implementation defined in v7,
  1658. * but with an ARM recommended set of registers, which we
  1659. * follow.
  1660. *
  1661. * Performance registers fall into three categories:
  1662. * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
  1663. * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
  1664. * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
  1665. * For the cases controlled by PMUSERENR we must set .access to PL0_RW
  1666. * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
  1667. */
  1668. { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
  1669. .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
  1670. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
  1671. .writefn = pmcntenset_write,
  1672. .accessfn = pmreg_access,
  1673. .fgt = FGT_PMCNTEN,
  1674. .raw_writefn = raw_write },
  1675. { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
  1676. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
  1677. .access = PL0_RW, .accessfn = pmreg_access,
  1678. .fgt = FGT_PMCNTEN,
  1679. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
  1680. .writefn = pmcntenset_write, .raw_writefn = raw_write },
  1681. { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
  1682. .access = PL0_RW,
  1683. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
  1684. .accessfn = pmreg_access,
  1685. .fgt = FGT_PMCNTEN,
  1686. .writefn = pmcntenclr_write,
  1687. .type = ARM_CP_ALIAS | ARM_CP_IO },
  1688. { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
  1689. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
  1690. .access = PL0_RW, .accessfn = pmreg_access,
  1691. .fgt = FGT_PMCNTEN,
  1692. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1693. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
  1694. .writefn = pmcntenclr_write },
  1695. { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
  1696. .access = PL0_RW, .type = ARM_CP_IO,
  1697. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
  1698. .accessfn = pmreg_access,
  1699. .fgt = FGT_PMOVS,
  1700. .writefn = pmovsr_write,
  1701. .raw_writefn = raw_write },
  1702. { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
  1703. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
  1704. .access = PL0_RW, .accessfn = pmreg_access,
  1705. .fgt = FGT_PMOVS,
  1706. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1707. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
  1708. .writefn = pmovsr_write,
  1709. .raw_writefn = raw_write },
  1710. { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
  1711. .access = PL0_W, .accessfn = pmreg_access_swinc,
  1712. .fgt = FGT_PMSWINC_EL0,
  1713. .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1714. .writefn = pmswinc_write },
  1715. { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
  1716. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
  1717. .access = PL0_W, .accessfn = pmreg_access_swinc,
  1718. .fgt = FGT_PMSWINC_EL0,
  1719. .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1720. .writefn = pmswinc_write },
  1721. { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
  1722. .access = PL0_RW, .type = ARM_CP_ALIAS,
  1723. .fgt = FGT_PMSELR_EL0,
  1724. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
  1725. .accessfn = pmreg_access_selr, .writefn = pmselr_write,
  1726. .raw_writefn = raw_write},
  1727. { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
  1728. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
  1729. .access = PL0_RW, .accessfn = pmreg_access_selr,
  1730. .fgt = FGT_PMSELR_EL0,
  1731. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
  1732. .writefn = pmselr_write, .raw_writefn = raw_write, },
  1733. { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
  1734. .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
  1735. .fgt = FGT_PMCCNTR_EL0,
  1736. .readfn = pmccntr_read, .writefn = pmccntr_write32,
  1737. .accessfn = pmreg_access_ccntr },
  1738. { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
  1739. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
  1740. .access = PL0_RW, .accessfn = pmreg_access_ccntr,
  1741. .fgt = FGT_PMCCNTR_EL0,
  1742. .type = ARM_CP_IO,
  1743. .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
  1744. .readfn = pmccntr_read, .writefn = pmccntr_write,
  1745. .raw_readfn = raw_read, .raw_writefn = raw_write, },
  1746. { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
  1747. .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
  1748. .access = PL0_RW, .accessfn = pmreg_access,
  1749. .fgt = FGT_PMCCFILTR_EL0,
  1750. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1751. .resetvalue = 0, },
  1752. { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
  1753. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
  1754. .writefn = pmccfiltr_write, .raw_writefn = raw_write,
  1755. .access = PL0_RW, .accessfn = pmreg_access,
  1756. .fgt = FGT_PMCCFILTR_EL0,
  1757. .type = ARM_CP_IO,
  1758. .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
  1759. .resetvalue = 0, },
  1760. { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
  1761. .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1762. .accessfn = pmreg_access,
  1763. .fgt = FGT_PMEVTYPERN_EL0,
  1764. .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
  1765. { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
  1766. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
  1767. .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1768. .accessfn = pmreg_access,
  1769. .fgt = FGT_PMEVTYPERN_EL0,
  1770. .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
  1771. { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
  1772. .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1773. .accessfn = pmreg_access_xevcntr,
  1774. .fgt = FGT_PMEVCNTRN_EL0,
  1775. .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
  1776. { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
  1777. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
  1778. .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  1779. .accessfn = pmreg_access_xevcntr,
  1780. .fgt = FGT_PMEVCNTRN_EL0,
  1781. .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
  1782. { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
  1783. .access = PL0_R | PL1_RW, .accessfn = access_tpm,
  1784. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
  1785. .resetvalue = 0,
  1786. .writefn = pmuserenr_write, .raw_writefn = raw_write },
  1787. { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
  1788. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
  1789. .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
  1790. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
  1791. .resetvalue = 0,
  1792. .writefn = pmuserenr_write, .raw_writefn = raw_write },
  1793. { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
  1794. .access = PL1_RW, .accessfn = access_tpm,
  1795. .fgt = FGT_PMINTEN,
  1796. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1797. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
  1798. .resetvalue = 0,
  1799. .writefn = pmintenset_write, .raw_writefn = raw_write },
  1800. { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
  1801. .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
  1802. .access = PL1_RW, .accessfn = access_tpm,
  1803. .fgt = FGT_PMINTEN,
  1804. .type = ARM_CP_IO,
  1805. .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
  1806. .writefn = pmintenset_write, .raw_writefn = raw_write,
  1807. .resetvalue = 0x0 },
  1808. { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
  1809. .access = PL1_RW, .accessfn = access_tpm,
  1810. .fgt = FGT_PMINTEN,
  1811. .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
  1812. .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
  1813. .writefn = pmintenclr_write, },
  1814. { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
  1815. .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
  1816. .access = PL1_RW, .accessfn = access_tpm,
  1817. .fgt = FGT_PMINTEN,
  1818. .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
  1819. .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
  1820. .writefn = pmintenclr_write },
  1821. { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
  1822. .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
  1823. .access = PL1_R,
  1824. .accessfn = access_tid4,
  1825. .fgt = FGT_CCSIDR_EL1,
  1826. .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
  1827. { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
  1828. .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
  1829. .access = PL1_RW,
  1830. .accessfn = access_tid4,
  1831. .fgt = FGT_CSSELR_EL1,
  1832. .writefn = csselr_write, .resetvalue = 0,
  1833. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
  1834. offsetof(CPUARMState, cp15.csselr_ns) } },
  1835. /*
  1836. * Auxiliary ID register: this actually has an IMPDEF value but for now
  1837. * just RAZ for all cores:
  1838. */
  1839. { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
  1840. .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
  1841. .access = PL1_R, .type = ARM_CP_CONST,
  1842. .accessfn = access_aa64_tid1,
  1843. .fgt = FGT_AIDR_EL1,
  1844. .resetvalue = 0 },
  1845. /*
  1846. * Auxiliary fault status registers: these also are IMPDEF, and we
  1847. * choose to RAZ/WI for all cores.
  1848. */
  1849. { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
  1850. .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
  1851. .access = PL1_RW, .accessfn = access_tvm_trvm,
  1852. .fgt = FGT_AFSR0_EL1,
  1853. .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
  1854. .type = ARM_CP_CONST, .resetvalue = 0 },
  1855. { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
  1856. .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
  1857. .access = PL1_RW, .accessfn = access_tvm_trvm,
  1858. .fgt = FGT_AFSR1_EL1,
  1859. .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
  1860. .type = ARM_CP_CONST, .resetvalue = 0 },
  1861. /*
  1862. * MAIR can just read-as-written because we don't implement caches
  1863. * and so don't need to care about memory attributes.
  1864. */
  1865. { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
  1866. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
  1867. .access = PL1_RW, .accessfn = access_tvm_trvm,
  1868. .fgt = FGT_MAIR_EL1,
  1869. .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
  1870. .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
  1871. .resetvalue = 0 },
  1872. { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
  1873. .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
  1874. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
  1875. .resetvalue = 0 },
  1876. /*
  1877. * For non-long-descriptor page tables these are PRRR and NMRR;
  1878. * regardless they still act as reads-as-written for QEMU.
  1879. */
  1880. /*
  1881. * MAIR0/1 are defined separately from their 64-bit counterpart which
  1882. * allows them to assign the correct fieldoffset based on the endianness
  1883. * handled in the field definitions.
  1884. */
  1885. { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
  1886. .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
  1887. .access = PL1_RW, .accessfn = access_tvm_trvm,
  1888. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
  1889. offsetof(CPUARMState, cp15.mair0_ns) },
  1890. .resetfn = arm_cp_reset_ignore },
  1891. { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
  1892. .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
  1893. .access = PL1_RW, .accessfn = access_tvm_trvm,
  1894. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
  1895. offsetof(CPUARMState, cp15.mair1_ns) },
  1896. .resetfn = arm_cp_reset_ignore },
  1897. { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
  1898. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
  1899. .fgt = FGT_ISR_EL1,
  1900. .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
  1901. };
  1902. static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
  1903. /* PMOVSSET is not implemented in v7 before v7ve */
  1904. { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
  1905. .access = PL0_RW, .accessfn = pmreg_access,
  1906. .fgt = FGT_PMOVS,
  1907. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1908. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
  1909. .writefn = pmovsset_write,
  1910. .raw_writefn = raw_write },
  1911. { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
  1912. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
  1913. .access = PL0_RW, .accessfn = pmreg_access,
  1914. .fgt = FGT_PMOVS,
  1915. .type = ARM_CP_ALIAS | ARM_CP_IO,
  1916. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
  1917. .writefn = pmovsset_write,
  1918. .raw_writefn = raw_write },
  1919. };
  1920. static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1921. uint64_t value)
  1922. {
  1923. value &= 1;
  1924. env->teecr = value;
  1925. }
  1926. static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
  1927. bool isread)
  1928. {
  1929. /*
  1930. * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
  1931. * at all, so we don't need to check whether we're v8A.
  1932. */
  1933. if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
  1934. (env->cp15.hstr_el2 & HSTR_TTEE)) {
  1935. return CP_ACCESS_TRAP_EL2;
  1936. }
  1937. return CP_ACCESS_OK;
  1938. }
  1939. static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
  1940. bool isread)
  1941. {
  1942. if (arm_current_el(env) == 0 && (env->teecr & 1)) {
  1943. return CP_ACCESS_TRAP_EL1;
  1944. }
  1945. return teecr_access(env, ri, isread);
  1946. }
  1947. static const ARMCPRegInfo t2ee_cp_reginfo[] = {
  1948. { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
  1949. .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
  1950. .resetvalue = 0,
  1951. .writefn = teecr_write, .accessfn = teecr_access },
  1952. { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
  1953. .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
  1954. .accessfn = teehbr_access, .resetvalue = 0 },
  1955. };
  1956. static const ARMCPRegInfo v6k_cp_reginfo[] = {
  1957. { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
  1958. .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
  1959. .access = PL0_RW,
  1960. .fgt = FGT_TPIDR_EL0,
  1961. .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
  1962. { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
  1963. .access = PL0_RW,
  1964. .fgt = FGT_TPIDR_EL0,
  1965. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
  1966. offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
  1967. .resetfn = arm_cp_reset_ignore },
  1968. { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
  1969. .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
  1970. .access = PL0_R | PL1_W,
  1971. .fgt = FGT_TPIDRRO_EL0,
  1972. .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
  1973. .resetvalue = 0},
  1974. { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
  1975. .access = PL0_R | PL1_W,
  1976. .fgt = FGT_TPIDRRO_EL0,
  1977. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
  1978. offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
  1979. .resetfn = arm_cp_reset_ignore },
  1980. { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
  1981. .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
  1982. .access = PL1_RW,
  1983. .fgt = FGT_TPIDR_EL1,
  1984. .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
  1985. { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
  1986. .access = PL1_RW,
  1987. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
  1988. offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
  1989. .resetvalue = 0 },
  1990. };
  1991. static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
  1992. {
  1993. ARMCPU *cpu = env_archcpu(env);
  1994. cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
  1995. }
  1996. #ifndef CONFIG_USER_ONLY
  1997. static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
  1998. bool isread)
  1999. {
  2000. /*
  2001. * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
  2002. * Writable only at the highest implemented exception level.
  2003. */
  2004. int el = arm_current_el(env);
  2005. uint64_t hcr;
  2006. uint32_t cntkctl;
  2007. switch (el) {
  2008. case 0:
  2009. hcr = arm_hcr_el2_eff(env);
  2010. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  2011. cntkctl = env->cp15.cnthctl_el2;
  2012. } else {
  2013. cntkctl = env->cp15.c14_cntkctl;
  2014. }
  2015. if (!extract32(cntkctl, 0, 2)) {
  2016. return CP_ACCESS_TRAP_EL1;
  2017. }
  2018. break;
  2019. case 1:
  2020. if (!isread && ri->state == ARM_CP_STATE_AA32 &&
  2021. arm_is_secure_below_el3(env)) {
  2022. /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
  2023. return CP_ACCESS_UNDEFINED;
  2024. }
  2025. break;
  2026. case 2:
  2027. case 3:
  2028. break;
  2029. }
  2030. if (!isread && el < arm_highest_el(env)) {
  2031. return CP_ACCESS_UNDEFINED;
  2032. }
  2033. return CP_ACCESS_OK;
  2034. }
  2035. static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
  2036. bool isread)
  2037. {
  2038. unsigned int cur_el = arm_current_el(env);
  2039. bool has_el2 = arm_is_el2_enabled(env);
  2040. uint64_t hcr = arm_hcr_el2_eff(env);
  2041. switch (cur_el) {
  2042. case 0:
  2043. /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
  2044. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  2045. return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
  2046. ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
  2047. }
  2048. /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
  2049. if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
  2050. return CP_ACCESS_TRAP_EL1;
  2051. }
  2052. /* fall through */
  2053. case 1:
  2054. /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
  2055. if (has_el2 && timeridx == GTIMER_PHYS &&
  2056. (hcr & HCR_E2H
  2057. ? !extract32(env->cp15.cnthctl_el2, 10, 1)
  2058. : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
  2059. return CP_ACCESS_TRAP_EL2;
  2060. }
  2061. if (has_el2 && timeridx == GTIMER_VIRT) {
  2062. if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
  2063. return CP_ACCESS_TRAP_EL2;
  2064. }
  2065. }
  2066. break;
  2067. }
  2068. return CP_ACCESS_OK;
  2069. }
  2070. static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
  2071. bool isread)
  2072. {
  2073. unsigned int cur_el = arm_current_el(env);
  2074. bool has_el2 = arm_is_el2_enabled(env);
  2075. uint64_t hcr = arm_hcr_el2_eff(env);
  2076. switch (cur_el) {
  2077. case 0:
  2078. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  2079. /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
  2080. return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
  2081. ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
  2082. }
  2083. /*
  2084. * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
  2085. * EL0 if EL0[PV]TEN is zero.
  2086. */
  2087. if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
  2088. return CP_ACCESS_TRAP_EL1;
  2089. }
  2090. /* fall through */
  2091. case 1:
  2092. if (has_el2 && timeridx == GTIMER_PHYS) {
  2093. if (hcr & HCR_E2H) {
  2094. /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
  2095. if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
  2096. return CP_ACCESS_TRAP_EL2;
  2097. }
  2098. } else {
  2099. /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
  2100. if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
  2101. return CP_ACCESS_TRAP_EL2;
  2102. }
  2103. }
  2104. }
  2105. if (has_el2 && timeridx == GTIMER_VIRT) {
  2106. if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
  2107. return CP_ACCESS_TRAP_EL2;
  2108. }
  2109. }
  2110. break;
  2111. }
  2112. return CP_ACCESS_OK;
  2113. }
  2114. static CPAccessResult gt_pct_access(CPUARMState *env,
  2115. const ARMCPRegInfo *ri,
  2116. bool isread)
  2117. {
  2118. return gt_counter_access(env, GTIMER_PHYS, isread);
  2119. }
  2120. static CPAccessResult gt_vct_access(CPUARMState *env,
  2121. const ARMCPRegInfo *ri,
  2122. bool isread)
  2123. {
  2124. return gt_counter_access(env, GTIMER_VIRT, isread);
  2125. }
  2126. static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
  2127. bool isread)
  2128. {
  2129. return gt_timer_access(env, GTIMER_PHYS, isread);
  2130. }
  2131. static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
  2132. bool isread)
  2133. {
  2134. return gt_timer_access(env, GTIMER_VIRT, isread);
  2135. }
  2136. static CPAccessResult gt_stimer_access(CPUARMState *env,
  2137. const ARMCPRegInfo *ri,
  2138. bool isread)
  2139. {
  2140. /*
  2141. * The AArch64 register view of the secure physical timer is
  2142. * always accessible from EL3, and configurably accessible from
  2143. * Secure EL1.
  2144. */
  2145. switch (arm_current_el(env)) {
  2146. case 1:
  2147. if (!arm_is_secure(env)) {
  2148. return CP_ACCESS_UNDEFINED;
  2149. }
  2150. if (!(env->cp15.scr_el3 & SCR_ST)) {
  2151. return CP_ACCESS_TRAP_EL3;
  2152. }
  2153. return CP_ACCESS_OK;
  2154. case 0:
  2155. case 2:
  2156. return CP_ACCESS_UNDEFINED;
  2157. case 3:
  2158. return CP_ACCESS_OK;
  2159. default:
  2160. g_assert_not_reached();
  2161. }
  2162. }
  2163. uint64_t gt_get_countervalue(CPUARMState *env)
  2164. {
  2165. ARMCPU *cpu = env_archcpu(env);
  2166. return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
  2167. }
  2168. static void gt_update_irq(ARMCPU *cpu, int timeridx)
  2169. {
  2170. CPUARMState *env = &cpu->env;
  2171. uint64_t cnthctl = env->cp15.cnthctl_el2;
  2172. ARMSecuritySpace ss = arm_security_space(env);
  2173. /* ISTATUS && !IMASK */
  2174. int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
  2175. /*
  2176. * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
  2177. * It is RES0 in Secure and NonSecure state.
  2178. */
  2179. if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
  2180. ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
  2181. (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
  2182. irqstate = 0;
  2183. }
  2184. qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
  2185. trace_arm_gt_update_irq(timeridx, irqstate);
  2186. }
  2187. void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
  2188. {
  2189. /*
  2190. * Changing security state between Root and Secure/NonSecure, which may
  2191. * happen when switching EL, can change the effective value of CNTHCTL_EL2
  2192. * mask bits. Update the IRQ state accordingly.
  2193. */
  2194. gt_update_irq(cpu, GTIMER_VIRT);
  2195. gt_update_irq(cpu, GTIMER_PHYS);
  2196. }
  2197. static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
  2198. {
  2199. if ((env->cp15.scr_el3 & SCR_ECVEN) &&
  2200. FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
  2201. arm_is_el2_enabled(env) &&
  2202. (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
  2203. return env->cp15.cntpoff_el2;
  2204. }
  2205. return 0;
  2206. }
  2207. static uint64_t gt_phys_cnt_offset(CPUARMState *env)
  2208. {
  2209. if (arm_current_el(env) >= 2) {
  2210. return 0;
  2211. }
  2212. return gt_phys_raw_cnt_offset(env);
  2213. }
  2214. static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
  2215. {
  2216. ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
  2217. if (gt->ctl & 1) {
  2218. /*
  2219. * Timer enabled: calculate and set current ISTATUS, irq, and
  2220. * reset timer to when ISTATUS next has to change
  2221. */
  2222. uint64_t offset = timeridx == GTIMER_VIRT ?
  2223. cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
  2224. uint64_t count = gt_get_countervalue(&cpu->env);
  2225. /* Note that this must be unsigned 64 bit arithmetic: */
  2226. int istatus = count - offset >= gt->cval;
  2227. uint64_t nexttick;
  2228. gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
  2229. if (istatus) {
  2230. /*
  2231. * Next transition is when (count - offset) rolls back over to 0.
  2232. * If offset > count then this is when count == offset;
  2233. * if offset <= count then this is when count == offset + 2^64
  2234. * For the latter case we set nexttick to an "as far in future
  2235. * as possible" value and let the code below handle it.
  2236. */
  2237. if (offset > count) {
  2238. nexttick = offset;
  2239. } else {
  2240. nexttick = UINT64_MAX;
  2241. }
  2242. } else {
  2243. /*
  2244. * Next transition is when (count - offset) == cval, i.e.
  2245. * when count == (cval + offset).
  2246. * If that would overflow, then again we set up the next interrupt
  2247. * for "as far in the future as possible" for the code below.
  2248. */
  2249. if (uadd64_overflow(gt->cval, offset, &nexttick)) {
  2250. nexttick = UINT64_MAX;
  2251. }
  2252. }
  2253. /*
  2254. * Note that the desired next expiry time might be beyond the
  2255. * signed-64-bit range of a QEMUTimer -- in this case we just
  2256. * set the timer for as far in the future as possible. When the
  2257. * timer expires we will reset the timer for any remaining period.
  2258. */
  2259. if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
  2260. timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
  2261. } else {
  2262. timer_mod(cpu->gt_timer[timeridx], nexttick);
  2263. }
  2264. trace_arm_gt_recalc(timeridx, nexttick);
  2265. } else {
  2266. /* Timer disabled: ISTATUS and timer output always clear */
  2267. gt->ctl &= ~4;
  2268. timer_del(cpu->gt_timer[timeridx]);
  2269. trace_arm_gt_recalc_disabled(timeridx);
  2270. }
  2271. gt_update_irq(cpu, timeridx);
  2272. }
  2273. static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
  2274. int timeridx)
  2275. {
  2276. ARMCPU *cpu = env_archcpu(env);
  2277. timer_del(cpu->gt_timer[timeridx]);
  2278. }
  2279. static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2280. {
  2281. return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
  2282. }
  2283. uint64_t gt_virt_cnt_offset(CPUARMState *env)
  2284. {
  2285. uint64_t hcr;
  2286. switch (arm_current_el(env)) {
  2287. case 2:
  2288. hcr = arm_hcr_el2_eff(env);
  2289. if (hcr & HCR_E2H) {
  2290. return 0;
  2291. }
  2292. break;
  2293. case 0:
  2294. hcr = arm_hcr_el2_eff(env);
  2295. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  2296. return 0;
  2297. }
  2298. break;
  2299. }
  2300. return env->cp15.cntvoff_el2;
  2301. }
  2302. static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2303. {
  2304. return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
  2305. }
  2306. static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2307. int timeridx,
  2308. uint64_t value)
  2309. {
  2310. trace_arm_gt_cval_write(timeridx, value);
  2311. env->cp15.c14_timer[timeridx].cval = value;
  2312. gt_recalc_timer(env_archcpu(env), timeridx);
  2313. }
  2314. static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
  2315. int timeridx)
  2316. {
  2317. uint64_t offset = 0;
  2318. switch (timeridx) {
  2319. case GTIMER_VIRT:
  2320. case GTIMER_HYPVIRT:
  2321. offset = gt_virt_cnt_offset(env);
  2322. break;
  2323. case GTIMER_PHYS:
  2324. offset = gt_phys_cnt_offset(env);
  2325. break;
  2326. }
  2327. return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
  2328. (gt_get_countervalue(env) - offset));
  2329. }
  2330. static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2331. int timeridx,
  2332. uint64_t value)
  2333. {
  2334. uint64_t offset = 0;
  2335. switch (timeridx) {
  2336. case GTIMER_VIRT:
  2337. case GTIMER_HYPVIRT:
  2338. offset = gt_virt_cnt_offset(env);
  2339. break;
  2340. case GTIMER_PHYS:
  2341. offset = gt_phys_cnt_offset(env);
  2342. break;
  2343. }
  2344. trace_arm_gt_tval_write(timeridx, value);
  2345. env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
  2346. sextract64(value, 0, 32);
  2347. gt_recalc_timer(env_archcpu(env), timeridx);
  2348. }
  2349. static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2350. int timeridx,
  2351. uint64_t value)
  2352. {
  2353. ARMCPU *cpu = env_archcpu(env);
  2354. uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
  2355. trace_arm_gt_ctl_write(timeridx, value);
  2356. env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
  2357. if ((oldval ^ value) & 1) {
  2358. /* Enable toggled */
  2359. gt_recalc_timer(cpu, timeridx);
  2360. } else if ((oldval ^ value) & 2) {
  2361. /*
  2362. * IMASK toggled: don't need to recalculate,
  2363. * just set the interrupt line based on ISTATUS
  2364. */
  2365. trace_arm_gt_imask_toggle(timeridx);
  2366. gt_update_irq(cpu, timeridx);
  2367. }
  2368. }
  2369. static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2370. {
  2371. gt_timer_reset(env, ri, GTIMER_PHYS);
  2372. }
  2373. static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2374. uint64_t value)
  2375. {
  2376. gt_cval_write(env, ri, GTIMER_PHYS, value);
  2377. }
  2378. static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2379. {
  2380. return gt_tval_read(env, ri, GTIMER_PHYS);
  2381. }
  2382. static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2383. uint64_t value)
  2384. {
  2385. gt_tval_write(env, ri, GTIMER_PHYS, value);
  2386. }
  2387. static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2388. uint64_t value)
  2389. {
  2390. gt_ctl_write(env, ri, GTIMER_PHYS, value);
  2391. }
  2392. static int gt_phys_redir_timeridx(CPUARMState *env)
  2393. {
  2394. switch (arm_mmu_idx(env)) {
  2395. case ARMMMUIdx_E20_0:
  2396. case ARMMMUIdx_E20_2:
  2397. case ARMMMUIdx_E20_2_PAN:
  2398. return GTIMER_HYP;
  2399. default:
  2400. return GTIMER_PHYS;
  2401. }
  2402. }
  2403. static int gt_virt_redir_timeridx(CPUARMState *env)
  2404. {
  2405. switch (arm_mmu_idx(env)) {
  2406. case ARMMMUIdx_E20_0:
  2407. case ARMMMUIdx_E20_2:
  2408. case ARMMMUIdx_E20_2_PAN:
  2409. return GTIMER_HYPVIRT;
  2410. default:
  2411. return GTIMER_VIRT;
  2412. }
  2413. }
  2414. static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
  2415. const ARMCPRegInfo *ri)
  2416. {
  2417. int timeridx = gt_phys_redir_timeridx(env);
  2418. return env->cp15.c14_timer[timeridx].cval;
  2419. }
  2420. static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2421. uint64_t value)
  2422. {
  2423. int timeridx = gt_phys_redir_timeridx(env);
  2424. gt_cval_write(env, ri, timeridx, value);
  2425. }
  2426. static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
  2427. const ARMCPRegInfo *ri)
  2428. {
  2429. int timeridx = gt_phys_redir_timeridx(env);
  2430. return gt_tval_read(env, ri, timeridx);
  2431. }
  2432. static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2433. uint64_t value)
  2434. {
  2435. int timeridx = gt_phys_redir_timeridx(env);
  2436. gt_tval_write(env, ri, timeridx, value);
  2437. }
  2438. static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
  2439. const ARMCPRegInfo *ri)
  2440. {
  2441. int timeridx = gt_phys_redir_timeridx(env);
  2442. return env->cp15.c14_timer[timeridx].ctl;
  2443. }
  2444. static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2445. uint64_t value)
  2446. {
  2447. int timeridx = gt_phys_redir_timeridx(env);
  2448. gt_ctl_write(env, ri, timeridx, value);
  2449. }
  2450. static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2451. {
  2452. gt_timer_reset(env, ri, GTIMER_VIRT);
  2453. }
  2454. static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2455. uint64_t value)
  2456. {
  2457. gt_cval_write(env, ri, GTIMER_VIRT, value);
  2458. }
  2459. static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2460. {
  2461. return gt_tval_read(env, ri, GTIMER_VIRT);
  2462. }
  2463. static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2464. uint64_t value)
  2465. {
  2466. gt_tval_write(env, ri, GTIMER_VIRT, value);
  2467. }
  2468. static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2469. uint64_t value)
  2470. {
  2471. gt_ctl_write(env, ri, GTIMER_VIRT, value);
  2472. }
  2473. static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2474. uint64_t value)
  2475. {
  2476. ARMCPU *cpu = env_archcpu(env);
  2477. uint32_t oldval = env->cp15.cnthctl_el2;
  2478. uint32_t valid_mask =
  2479. R_CNTHCTL_EL0PCTEN_E2H1_MASK |
  2480. R_CNTHCTL_EL0VCTEN_E2H1_MASK |
  2481. R_CNTHCTL_EVNTEN_MASK |
  2482. R_CNTHCTL_EVNTDIR_MASK |
  2483. R_CNTHCTL_EVNTI_MASK |
  2484. R_CNTHCTL_EL0VTEN_MASK |
  2485. R_CNTHCTL_EL0PTEN_MASK |
  2486. R_CNTHCTL_EL1PCTEN_E2H1_MASK |
  2487. R_CNTHCTL_EL1PTEN_MASK;
  2488. if (cpu_isar_feature(aa64_rme, cpu)) {
  2489. valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
  2490. }
  2491. if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
  2492. valid_mask |=
  2493. R_CNTHCTL_EL1TVT_MASK |
  2494. R_CNTHCTL_EL1TVCT_MASK |
  2495. R_CNTHCTL_EL1NVPCT_MASK |
  2496. R_CNTHCTL_EL1NVVCT_MASK |
  2497. R_CNTHCTL_EVNTIS_MASK;
  2498. }
  2499. if (cpu_isar_feature(aa64_ecv, cpu)) {
  2500. valid_mask |= R_CNTHCTL_ECV_MASK;
  2501. }
  2502. /* Clear RES0 bits */
  2503. value &= valid_mask;
  2504. raw_write(env, ri, value);
  2505. if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
  2506. gt_update_irq(cpu, GTIMER_VIRT);
  2507. } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
  2508. gt_update_irq(cpu, GTIMER_PHYS);
  2509. }
  2510. }
  2511. static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2512. uint64_t value)
  2513. {
  2514. ARMCPU *cpu = env_archcpu(env);
  2515. trace_arm_gt_cntvoff_write(value);
  2516. raw_write(env, ri, value);
  2517. gt_recalc_timer(cpu, GTIMER_VIRT);
  2518. }
  2519. static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
  2520. const ARMCPRegInfo *ri)
  2521. {
  2522. int timeridx = gt_virt_redir_timeridx(env);
  2523. return env->cp15.c14_timer[timeridx].cval;
  2524. }
  2525. static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2526. uint64_t value)
  2527. {
  2528. int timeridx = gt_virt_redir_timeridx(env);
  2529. gt_cval_write(env, ri, timeridx, value);
  2530. }
  2531. static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
  2532. const ARMCPRegInfo *ri)
  2533. {
  2534. int timeridx = gt_virt_redir_timeridx(env);
  2535. return gt_tval_read(env, ri, timeridx);
  2536. }
  2537. static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2538. uint64_t value)
  2539. {
  2540. int timeridx = gt_virt_redir_timeridx(env);
  2541. gt_tval_write(env, ri, timeridx, value);
  2542. }
  2543. static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
  2544. const ARMCPRegInfo *ri)
  2545. {
  2546. int timeridx = gt_virt_redir_timeridx(env);
  2547. return env->cp15.c14_timer[timeridx].ctl;
  2548. }
  2549. static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2550. uint64_t value)
  2551. {
  2552. int timeridx = gt_virt_redir_timeridx(env);
  2553. gt_ctl_write(env, ri, timeridx, value);
  2554. }
  2555. static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2556. {
  2557. gt_timer_reset(env, ri, GTIMER_HYP);
  2558. }
  2559. static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2560. uint64_t value)
  2561. {
  2562. gt_cval_write(env, ri, GTIMER_HYP, value);
  2563. }
  2564. static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2565. {
  2566. return gt_tval_read(env, ri, GTIMER_HYP);
  2567. }
  2568. static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2569. uint64_t value)
  2570. {
  2571. gt_tval_write(env, ri, GTIMER_HYP, value);
  2572. }
  2573. static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2574. uint64_t value)
  2575. {
  2576. gt_ctl_write(env, ri, GTIMER_HYP, value);
  2577. }
  2578. static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2579. {
  2580. gt_timer_reset(env, ri, GTIMER_SEC);
  2581. }
  2582. static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2583. uint64_t value)
  2584. {
  2585. gt_cval_write(env, ri, GTIMER_SEC, value);
  2586. }
  2587. static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2588. {
  2589. return gt_tval_read(env, ri, GTIMER_SEC);
  2590. }
  2591. static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2592. uint64_t value)
  2593. {
  2594. gt_tval_write(env, ri, GTIMER_SEC, value);
  2595. }
  2596. static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2597. uint64_t value)
  2598. {
  2599. gt_ctl_write(env, ri, GTIMER_SEC, value);
  2600. }
  2601. static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2602. {
  2603. gt_timer_reset(env, ri, GTIMER_HYPVIRT);
  2604. }
  2605. static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2606. uint64_t value)
  2607. {
  2608. gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
  2609. }
  2610. static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2611. {
  2612. return gt_tval_read(env, ri, GTIMER_HYPVIRT);
  2613. }
  2614. static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2615. uint64_t value)
  2616. {
  2617. gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
  2618. }
  2619. static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2620. uint64_t value)
  2621. {
  2622. gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
  2623. }
  2624. void arm_gt_ptimer_cb(void *opaque)
  2625. {
  2626. ARMCPU *cpu = opaque;
  2627. gt_recalc_timer(cpu, GTIMER_PHYS);
  2628. }
  2629. void arm_gt_vtimer_cb(void *opaque)
  2630. {
  2631. ARMCPU *cpu = opaque;
  2632. gt_recalc_timer(cpu, GTIMER_VIRT);
  2633. }
  2634. void arm_gt_htimer_cb(void *opaque)
  2635. {
  2636. ARMCPU *cpu = opaque;
  2637. gt_recalc_timer(cpu, GTIMER_HYP);
  2638. }
  2639. void arm_gt_stimer_cb(void *opaque)
  2640. {
  2641. ARMCPU *cpu = opaque;
  2642. gt_recalc_timer(cpu, GTIMER_SEC);
  2643. }
  2644. void arm_gt_hvtimer_cb(void *opaque)
  2645. {
  2646. ARMCPU *cpu = opaque;
  2647. gt_recalc_timer(cpu, GTIMER_HYPVIRT);
  2648. }
  2649. static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
  2650. /*
  2651. * Note that CNTFRQ is purely reads-as-written for the benefit
  2652. * of software; writing it doesn't actually change the timer frequency.
  2653. * Our reset value matches the fixed frequency we implement the timer at.
  2654. */
  2655. { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
  2656. .type = ARM_CP_ALIAS,
  2657. .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
  2658. .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
  2659. },
  2660. { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
  2661. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
  2662. .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
  2663. .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
  2664. .resetfn = arm_gt_cntfrq_reset,
  2665. },
  2666. /* overall control: mostly access permissions */
  2667. { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
  2668. .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
  2669. .access = PL1_RW,
  2670. .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
  2671. .resetvalue = 0,
  2672. },
  2673. /* per-timer control */
  2674. { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
  2675. .secure = ARM_CP_SECSTATE_NS,
  2676. .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
  2677. .accessfn = gt_ptimer_access,
  2678. .fieldoffset = offsetoflow32(CPUARMState,
  2679. cp15.c14_timer[GTIMER_PHYS].ctl),
  2680. .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
  2681. .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
  2682. },
  2683. { .name = "CNTP_CTL_S",
  2684. .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
  2685. .secure = ARM_CP_SECSTATE_S,
  2686. .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
  2687. .accessfn = gt_ptimer_access,
  2688. .fieldoffset = offsetoflow32(CPUARMState,
  2689. cp15.c14_timer[GTIMER_SEC].ctl),
  2690. .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
  2691. },
  2692. { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
  2693. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
  2694. .type = ARM_CP_IO, .access = PL0_RW,
  2695. .accessfn = gt_ptimer_access,
  2696. .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
  2697. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
  2698. .resetvalue = 0,
  2699. .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
  2700. .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
  2701. },
  2702. { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
  2703. .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
  2704. .accessfn = gt_vtimer_access,
  2705. .fieldoffset = offsetoflow32(CPUARMState,
  2706. cp15.c14_timer[GTIMER_VIRT].ctl),
  2707. .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
  2708. .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
  2709. },
  2710. { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
  2711. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
  2712. .type = ARM_CP_IO, .access = PL0_RW,
  2713. .accessfn = gt_vtimer_access,
  2714. .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
  2715. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
  2716. .resetvalue = 0,
  2717. .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
  2718. .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
  2719. },
  2720. /* TimerValue views: a 32 bit downcounting view of the underlying state */
  2721. { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
  2722. .secure = ARM_CP_SECSTATE_NS,
  2723. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
  2724. .accessfn = gt_ptimer_access,
  2725. .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
  2726. },
  2727. { .name = "CNTP_TVAL_S",
  2728. .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
  2729. .secure = ARM_CP_SECSTATE_S,
  2730. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
  2731. .accessfn = gt_ptimer_access,
  2732. .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
  2733. },
  2734. { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
  2735. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
  2736. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
  2737. .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
  2738. .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
  2739. },
  2740. { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
  2741. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
  2742. .accessfn = gt_vtimer_access,
  2743. .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
  2744. },
  2745. { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
  2746. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
  2747. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
  2748. .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
  2749. .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
  2750. },
  2751. /* The counter itself */
  2752. { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
  2753. .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
  2754. .accessfn = gt_pct_access,
  2755. .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
  2756. },
  2757. { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
  2758. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
  2759. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2760. .accessfn = gt_pct_access, .readfn = gt_cnt_read,
  2761. },
  2762. { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
  2763. .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
  2764. .accessfn = gt_vct_access,
  2765. .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
  2766. },
  2767. { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
  2768. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
  2769. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2770. .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
  2771. },
  2772. /* Comparison value, indicating when the timer goes off */
  2773. { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
  2774. .secure = ARM_CP_SECSTATE_NS,
  2775. .access = PL0_RW,
  2776. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
  2777. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
  2778. .accessfn = gt_ptimer_access,
  2779. .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
  2780. .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
  2781. },
  2782. { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
  2783. .secure = ARM_CP_SECSTATE_S,
  2784. .access = PL0_RW,
  2785. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
  2786. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
  2787. .accessfn = gt_ptimer_access,
  2788. .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
  2789. },
  2790. { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
  2791. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
  2792. .access = PL0_RW,
  2793. .type = ARM_CP_IO,
  2794. .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
  2795. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
  2796. .resetvalue = 0, .accessfn = gt_ptimer_access,
  2797. .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
  2798. .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
  2799. },
  2800. { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
  2801. .access = PL0_RW,
  2802. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
  2803. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
  2804. .accessfn = gt_vtimer_access,
  2805. .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
  2806. .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
  2807. },
  2808. { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
  2809. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
  2810. .access = PL0_RW,
  2811. .type = ARM_CP_IO,
  2812. .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
  2813. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
  2814. .resetvalue = 0, .accessfn = gt_vtimer_access,
  2815. .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
  2816. .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
  2817. },
  2818. /*
  2819. * Secure timer -- this is actually restricted to only EL3
  2820. * and configurably Secure-EL1 via the accessfn.
  2821. */
  2822. { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
  2823. .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
  2824. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
  2825. .accessfn = gt_stimer_access,
  2826. .readfn = gt_sec_tval_read,
  2827. .writefn = gt_sec_tval_write,
  2828. .resetfn = gt_sec_timer_reset,
  2829. },
  2830. { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
  2831. .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
  2832. .type = ARM_CP_IO, .access = PL1_RW,
  2833. .accessfn = gt_stimer_access,
  2834. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
  2835. .resetvalue = 0,
  2836. .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
  2837. },
  2838. { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
  2839. .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
  2840. .type = ARM_CP_IO, .access = PL1_RW,
  2841. .accessfn = gt_stimer_access,
  2842. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
  2843. .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
  2844. },
  2845. };
  2846. /*
  2847. * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
  2848. * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
  2849. * so our implementations here are identical to the normal registers.
  2850. */
  2851. static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
  2852. { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
  2853. .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
  2854. .accessfn = gt_vct_access,
  2855. .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
  2856. },
  2857. { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
  2858. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
  2859. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2860. .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
  2861. },
  2862. { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
  2863. .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
  2864. .accessfn = gt_pct_access,
  2865. .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
  2866. },
  2867. { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
  2868. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
  2869. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2870. .accessfn = gt_pct_access, .readfn = gt_cnt_read,
  2871. },
  2872. };
  2873. static CPAccessResult gt_cntpoff_access(CPUARMState *env,
  2874. const ARMCPRegInfo *ri,
  2875. bool isread)
  2876. {
  2877. if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
  2878. !(env->cp15.scr_el3 & SCR_ECVEN)) {
  2879. return CP_ACCESS_TRAP_EL3;
  2880. }
  2881. return CP_ACCESS_OK;
  2882. }
  2883. static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2884. uint64_t value)
  2885. {
  2886. ARMCPU *cpu = env_archcpu(env);
  2887. trace_arm_gt_cntpoff_write(value);
  2888. raw_write(env, ri, value);
  2889. gt_recalc_timer(cpu, GTIMER_PHYS);
  2890. }
  2891. static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
  2892. .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
  2893. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
  2894. .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
  2895. .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
  2896. .nv2_redirect_offset = 0x1a8,
  2897. .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
  2898. };
  2899. #else
  2900. /*
  2901. * In user-mode most of the generic timer registers are inaccessible
  2902. * however modern kernels (4.12+) allow access to cntvct_el0
  2903. */
  2904. static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2905. {
  2906. ARMCPU *cpu = env_archcpu(env);
  2907. /*
  2908. * Currently we have no support for QEMUTimer in linux-user so we
  2909. * can't call gt_get_countervalue(env), instead we directly
  2910. * call the lower level functions.
  2911. */
  2912. return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
  2913. }
  2914. static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
  2915. { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
  2916. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
  2917. .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
  2918. .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
  2919. .resetfn = arm_gt_cntfrq_reset,
  2920. },
  2921. { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
  2922. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
  2923. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2924. .readfn = gt_virt_cnt_read,
  2925. },
  2926. };
  2927. /*
  2928. * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
  2929. * is exposed to userspace by Linux.
  2930. */
  2931. static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
  2932. { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
  2933. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
  2934. .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
  2935. .readfn = gt_virt_cnt_read,
  2936. },
  2937. };
  2938. #endif
  2939. static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  2940. {
  2941. if (arm_feature(env, ARM_FEATURE_LPAE)) {
  2942. raw_write(env, ri, value);
  2943. } else if (arm_feature(env, ARM_FEATURE_V7)) {
  2944. raw_write(env, ri, value & 0xfffff6ff);
  2945. } else {
  2946. raw_write(env, ri, value & 0xfffff1ff);
  2947. }
  2948. }
  2949. #ifndef CONFIG_USER_ONLY
  2950. /* get_phys_addr() isn't present for user-mode-only targets */
  2951. static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
  2952. bool isread)
  2953. {
  2954. if (ri->opc2 & 4) {
  2955. /*
  2956. * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
  2957. * Secure EL1 (which can only happen if EL3 is AArch64).
  2958. * They are simply UNDEF if executed from NS EL1.
  2959. * They function normally from EL2 or EL3.
  2960. */
  2961. if (arm_current_el(env) == 1) {
  2962. if (arm_is_secure_below_el3(env)) {
  2963. if (env->cp15.scr_el3 & SCR_EEL2) {
  2964. return CP_ACCESS_TRAP_EL2;
  2965. }
  2966. return CP_ACCESS_TRAP_EL3;
  2967. }
  2968. return CP_ACCESS_UNDEFINED;
  2969. }
  2970. }
  2971. return CP_ACCESS_OK;
  2972. }
  2973. #ifdef CONFIG_TCG
  2974. static int par_el1_shareability(GetPhysAddrResult *res)
  2975. {
  2976. /*
  2977. * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
  2978. * memory -- see pseudocode PAREncodeShareability().
  2979. */
  2980. if (((res->cacheattrs.attrs & 0xf0) == 0) ||
  2981. res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
  2982. return 2;
  2983. }
  2984. return res->cacheattrs.shareability;
  2985. }
  2986. static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
  2987. MMUAccessType access_type, ARMMMUIdx mmu_idx,
  2988. ARMSecuritySpace ss)
  2989. {
  2990. bool ret;
  2991. uint64_t par64;
  2992. bool format64 = false;
  2993. ARMMMUFaultInfo fi = {};
  2994. GetPhysAddrResult res = {};
  2995. /*
  2996. * I_MXTJT: Granule protection checks are not performed on the final
  2997. * address of a successful translation. This is a translation not a
  2998. * memory reference, so "memop = none = 0".
  2999. */
  3000. ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
  3001. mmu_idx, ss, &res, &fi);
  3002. /*
  3003. * ATS operations only do S1 or S1+S2 translations, so we never
  3004. * have to deal with the ARMCacheAttrs format for S2 only.
  3005. */
  3006. assert(!res.cacheattrs.is_s2_format);
  3007. if (ret) {
  3008. /*
  3009. * Some kinds of translation fault must cause exceptions rather
  3010. * than being reported in the PAR.
  3011. */
  3012. int current_el = arm_current_el(env);
  3013. int target_el;
  3014. uint32_t syn, fsr, fsc;
  3015. bool take_exc = false;
  3016. if (fi.s1ptw && current_el == 1
  3017. && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
  3018. /*
  3019. * Synchronous stage 2 fault on an access made as part of the
  3020. * translation table walk for AT S1E0* or AT S1E1* insn
  3021. * executed from NS EL1. If this is a synchronous external abort
  3022. * and SCR_EL3.EA == 1, then we take a synchronous external abort
  3023. * to EL3. Otherwise the fault is taken as an exception to EL2,
  3024. * and HPFAR_EL2 holds the faulting IPA.
  3025. */
  3026. if (fi.type == ARMFault_SyncExternalOnWalk &&
  3027. (env->cp15.scr_el3 & SCR_EA)) {
  3028. target_el = 3;
  3029. } else {
  3030. env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
  3031. if (arm_is_secure_below_el3(env) && fi.s1ns) {
  3032. env->cp15.hpfar_el2 |= HPFAR_NS;
  3033. }
  3034. target_el = 2;
  3035. }
  3036. take_exc = true;
  3037. } else if (fi.type == ARMFault_SyncExternalOnWalk) {
  3038. /*
  3039. * Synchronous external aborts during a translation table walk
  3040. * are taken as Data Abort exceptions.
  3041. */
  3042. if (fi.stage2) {
  3043. if (current_el == 3) {
  3044. target_el = 3;
  3045. } else {
  3046. target_el = 2;
  3047. }
  3048. } else {
  3049. target_el = exception_target_el(env);
  3050. }
  3051. take_exc = true;
  3052. }
  3053. if (take_exc) {
  3054. /* Construct FSR and FSC using same logic as arm_deliver_fault() */
  3055. if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
  3056. arm_s1_regime_using_lpae_format(env, mmu_idx)) {
  3057. fsr = arm_fi_to_lfsc(&fi);
  3058. fsc = extract32(fsr, 0, 6);
  3059. } else {
  3060. fsr = arm_fi_to_sfsc(&fi);
  3061. fsc = 0x3f;
  3062. }
  3063. /*
  3064. * Report exception with ESR indicating a fault due to a
  3065. * translation table walk for a cache maintenance instruction.
  3066. */
  3067. syn = syn_data_abort_no_iss(current_el == target_el, 0,
  3068. fi.ea, 1, fi.s1ptw, 1, fsc);
  3069. env->exception.vaddress = value;
  3070. env->exception.fsr = fsr;
  3071. raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
  3072. }
  3073. }
  3074. if (is_a64(env)) {
  3075. format64 = true;
  3076. } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
  3077. /*
  3078. * ATS1Cxx:
  3079. * * TTBCR.EAE determines whether the result is returned using the
  3080. * 32-bit or the 64-bit PAR format
  3081. * * Instructions executed in Hyp mode always use the 64bit format
  3082. *
  3083. * ATS1S2NSOxx uses the 64bit format if any of the following is true:
  3084. * * The Non-secure TTBCR.EAE bit is set to 1
  3085. * * The implementation includes EL2, and the value of HCR.VM is 1
  3086. *
  3087. * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
  3088. *
  3089. * ATS1Hx always uses the 64bit format.
  3090. */
  3091. format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
  3092. if (arm_feature(env, ARM_FEATURE_EL2)) {
  3093. if (mmu_idx == ARMMMUIdx_E10_0 ||
  3094. mmu_idx == ARMMMUIdx_E10_1 ||
  3095. mmu_idx == ARMMMUIdx_E10_1_PAN) {
  3096. format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
  3097. } else {
  3098. format64 |= arm_current_el(env) == 2;
  3099. }
  3100. }
  3101. }
  3102. if (format64) {
  3103. /* Create a 64-bit PAR */
  3104. par64 = (1 << 11); /* LPAE bit always set */
  3105. if (!ret) {
  3106. par64 |= res.f.phys_addr & ~0xfffULL;
  3107. if (!res.f.attrs.secure) {
  3108. par64 |= (1 << 9); /* NS */
  3109. }
  3110. par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
  3111. par64 |= par_el1_shareability(&res) << 7; /* SH */
  3112. } else {
  3113. uint32_t fsr = arm_fi_to_lfsc(&fi);
  3114. par64 |= 1; /* F */
  3115. par64 |= (fsr & 0x3f) << 1; /* FS */
  3116. if (fi.stage2) {
  3117. par64 |= (1 << 9); /* S */
  3118. }
  3119. if (fi.s1ptw) {
  3120. par64 |= (1 << 8); /* PTW */
  3121. }
  3122. }
  3123. } else {
  3124. /*
  3125. * fsr is a DFSR/IFSR value for the short descriptor
  3126. * translation table format (with WnR always clear).
  3127. * Convert it to a 32-bit PAR.
  3128. */
  3129. if (!ret) {
  3130. /* We do not set any attribute bits in the PAR */
  3131. if (res.f.lg_page_size == 24
  3132. && arm_feature(env, ARM_FEATURE_V7)) {
  3133. par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
  3134. } else {
  3135. par64 = res.f.phys_addr & 0xfffff000;
  3136. }
  3137. if (!res.f.attrs.secure) {
  3138. par64 |= (1 << 9); /* NS */
  3139. }
  3140. } else {
  3141. uint32_t fsr = arm_fi_to_sfsc(&fi);
  3142. par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
  3143. ((fsr & 0xf) << 1) | 1;
  3144. }
  3145. }
  3146. return par64;
  3147. }
  3148. #endif /* CONFIG_TCG */
  3149. static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  3150. {
  3151. #ifdef CONFIG_TCG
  3152. MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
  3153. uint64_t par64;
  3154. ARMMMUIdx mmu_idx;
  3155. int el = arm_current_el(env);
  3156. ARMSecuritySpace ss = arm_security_space(env);
  3157. switch (ri->opc2 & 6) {
  3158. case 0:
  3159. /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
  3160. switch (el) {
  3161. case 3:
  3162. if (ri->crm == 9 && arm_pan_enabled(env)) {
  3163. mmu_idx = ARMMMUIdx_E30_3_PAN;
  3164. } else {
  3165. mmu_idx = ARMMMUIdx_E3;
  3166. }
  3167. break;
  3168. case 2:
  3169. g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
  3170. /* fall through */
  3171. case 1:
  3172. if (ri->crm == 9 && arm_pan_enabled(env)) {
  3173. mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
  3174. } else {
  3175. mmu_idx = ARMMMUIdx_Stage1_E1;
  3176. }
  3177. break;
  3178. default:
  3179. g_assert_not_reached();
  3180. }
  3181. break;
  3182. case 2:
  3183. /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
  3184. switch (el) {
  3185. case 3:
  3186. mmu_idx = ARMMMUIdx_E30_0;
  3187. break;
  3188. case 2:
  3189. g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
  3190. mmu_idx = ARMMMUIdx_Stage1_E0;
  3191. break;
  3192. case 1:
  3193. mmu_idx = ARMMMUIdx_Stage1_E0;
  3194. break;
  3195. default:
  3196. g_assert_not_reached();
  3197. }
  3198. break;
  3199. case 4:
  3200. /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
  3201. mmu_idx = ARMMMUIdx_E10_1;
  3202. ss = ARMSS_NonSecure;
  3203. break;
  3204. case 6:
  3205. /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
  3206. mmu_idx = ARMMMUIdx_E10_0;
  3207. ss = ARMSS_NonSecure;
  3208. break;
  3209. default:
  3210. g_assert_not_reached();
  3211. }
  3212. par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
  3213. A32_BANKED_CURRENT_REG_SET(env, par, par64);
  3214. #else
  3215. /* Handled by hardware accelerator. */
  3216. g_assert_not_reached();
  3217. #endif /* CONFIG_TCG */
  3218. }
  3219. static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3220. uint64_t value)
  3221. {
  3222. #ifdef CONFIG_TCG
  3223. MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
  3224. uint64_t par64;
  3225. /* There is no SecureEL2 for AArch32. */
  3226. par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
  3227. ARMSS_NonSecure);
  3228. A32_BANKED_CURRENT_REG_SET(env, par, par64);
  3229. #else
  3230. /* Handled by hardware accelerator. */
  3231. g_assert_not_reached();
  3232. #endif /* CONFIG_TCG */
  3233. }
  3234. static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
  3235. bool isread)
  3236. {
  3237. /*
  3238. * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
  3239. * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
  3240. * only happen when executing at EL3 because that combination also causes an
  3241. * illegal exception return. We don't need to check FEAT_RME either, because
  3242. * scr_write() ensures that the NSE bit is not set otherwise.
  3243. */
  3244. if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
  3245. return CP_ACCESS_UNDEFINED;
  3246. }
  3247. return CP_ACCESS_OK;
  3248. }
  3249. static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
  3250. bool isread)
  3251. {
  3252. if (arm_current_el(env) == 3 &&
  3253. !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
  3254. return CP_ACCESS_UNDEFINED;
  3255. }
  3256. return at_e012_access(env, ri, isread);
  3257. }
  3258. static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
  3259. bool isread)
  3260. {
  3261. if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
  3262. return CP_ACCESS_TRAP_EL2;
  3263. }
  3264. return at_e012_access(env, ri, isread);
  3265. }
  3266. static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
  3267. uint64_t value)
  3268. {
  3269. #ifdef CONFIG_TCG
  3270. MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
  3271. ARMMMUIdx mmu_idx;
  3272. uint64_t hcr_el2 = arm_hcr_el2_eff(env);
  3273. bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
  3274. bool for_el3 = false;
  3275. ARMSecuritySpace ss;
  3276. switch (ri->opc2 & 6) {
  3277. case 0:
  3278. switch (ri->opc1) {
  3279. case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
  3280. if (ri->crm == 9 && arm_pan_enabled(env)) {
  3281. mmu_idx = regime_e20 ?
  3282. ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
  3283. } else {
  3284. mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
  3285. }
  3286. break;
  3287. case 4: /* AT S1E2R, AT S1E2W */
  3288. mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
  3289. break;
  3290. case 6: /* AT S1E3R, AT S1E3W */
  3291. mmu_idx = ARMMMUIdx_E3;
  3292. for_el3 = true;
  3293. break;
  3294. default:
  3295. g_assert_not_reached();
  3296. }
  3297. break;
  3298. case 2: /* AT S1E0R, AT S1E0W */
  3299. mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
  3300. break;
  3301. case 4: /* AT S12E1R, AT S12E1W */
  3302. mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
  3303. break;
  3304. case 6: /* AT S12E0R, AT S12E0W */
  3305. mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
  3306. break;
  3307. default:
  3308. g_assert_not_reached();
  3309. }
  3310. ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
  3311. env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
  3312. #else
  3313. /* Handled by hardware accelerator. */
  3314. g_assert_not_reached();
  3315. #endif /* CONFIG_TCG */
  3316. }
  3317. #endif
  3318. /* Return basic MPU access permission bits. */
  3319. static uint32_t simple_mpu_ap_bits(uint32_t val)
  3320. {
  3321. uint32_t ret;
  3322. uint32_t mask;
  3323. int i;
  3324. ret = 0;
  3325. mask = 3;
  3326. for (i = 0; i < 16; i += 2) {
  3327. ret |= (val >> i) & mask;
  3328. mask <<= 2;
  3329. }
  3330. return ret;
  3331. }
  3332. /* Pad basic MPU access permission bits to extended format. */
  3333. static uint32_t extended_mpu_ap_bits(uint32_t val)
  3334. {
  3335. uint32_t ret;
  3336. uint32_t mask;
  3337. int i;
  3338. ret = 0;
  3339. mask = 3;
  3340. for (i = 0; i < 16; i += 2) {
  3341. ret |= (val & mask) << i;
  3342. mask <<= 2;
  3343. }
  3344. return ret;
  3345. }
  3346. static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3347. uint64_t value)
  3348. {
  3349. env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
  3350. }
  3351. static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3352. {
  3353. return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
  3354. }
  3355. static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3356. uint64_t value)
  3357. {
  3358. env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
  3359. }
  3360. static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3361. {
  3362. return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
  3363. }
  3364. static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3365. {
  3366. uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
  3367. if (!u32p) {
  3368. return 0;
  3369. }
  3370. u32p += env->pmsav7.rnr[M_REG_NS];
  3371. return *u32p;
  3372. }
  3373. static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3374. uint64_t value)
  3375. {
  3376. ARMCPU *cpu = env_archcpu(env);
  3377. uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
  3378. if (!u32p) {
  3379. return;
  3380. }
  3381. u32p += env->pmsav7.rnr[M_REG_NS];
  3382. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3383. *u32p = value;
  3384. }
  3385. static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3386. uint64_t value)
  3387. {
  3388. ARMCPU *cpu = env_archcpu(env);
  3389. uint32_t nrgs = cpu->pmsav7_dregion;
  3390. if (value >= nrgs) {
  3391. qemu_log_mask(LOG_GUEST_ERROR,
  3392. "PMSAv7 RGNR write >= # supported regions, %" PRIu32
  3393. " > %" PRIu32 "\n", (uint32_t)value, nrgs);
  3394. return;
  3395. }
  3396. raw_write(env, ri, value);
  3397. }
  3398. static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3399. uint64_t value)
  3400. {
  3401. ARMCPU *cpu = env_archcpu(env);
  3402. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3403. env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
  3404. }
  3405. static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3406. {
  3407. return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
  3408. }
  3409. static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3410. uint64_t value)
  3411. {
  3412. ARMCPU *cpu = env_archcpu(env);
  3413. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3414. env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
  3415. }
  3416. static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3417. {
  3418. return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
  3419. }
  3420. static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3421. uint64_t value)
  3422. {
  3423. ARMCPU *cpu = env_archcpu(env);
  3424. /*
  3425. * Ignore writes that would select not implemented region.
  3426. * This is architecturally UNPREDICTABLE.
  3427. */
  3428. if (value >= cpu->pmsav7_dregion) {
  3429. return;
  3430. }
  3431. env->pmsav7.rnr[M_REG_NS] = value;
  3432. }
  3433. static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3434. uint64_t value)
  3435. {
  3436. ARMCPU *cpu = env_archcpu(env);
  3437. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3438. env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
  3439. }
  3440. static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3441. {
  3442. return env->pmsav8.hprbar[env->pmsav8.hprselr];
  3443. }
  3444. static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3445. uint64_t value)
  3446. {
  3447. ARMCPU *cpu = env_archcpu(env);
  3448. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3449. env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
  3450. }
  3451. static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3452. {
  3453. return env->pmsav8.hprlar[env->pmsav8.hprselr];
  3454. }
  3455. static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3456. uint64_t value)
  3457. {
  3458. uint32_t n;
  3459. uint32_t bit;
  3460. ARMCPU *cpu = env_archcpu(env);
  3461. /* Ignore writes to unimplemented regions */
  3462. int rmax = MIN(cpu->pmsav8r_hdregion, 32);
  3463. value &= MAKE_64BIT_MASK(0, rmax);
  3464. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3465. /* Register alias is only valid for first 32 indexes */
  3466. for (n = 0; n < rmax; ++n) {
  3467. bit = extract32(value, n, 1);
  3468. env->pmsav8.hprlar[n] = deposit32(
  3469. env->pmsav8.hprlar[n], 0, 1, bit);
  3470. }
  3471. }
  3472. static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3473. {
  3474. uint32_t n;
  3475. uint32_t result = 0x0;
  3476. ARMCPU *cpu = env_archcpu(env);
  3477. /* Register alias is only valid for first 32 indexes */
  3478. for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
  3479. if (env->pmsav8.hprlar[n] & 0x1) {
  3480. result |= (0x1 << n);
  3481. }
  3482. }
  3483. return result;
  3484. }
  3485. static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3486. uint64_t value)
  3487. {
  3488. ARMCPU *cpu = env_archcpu(env);
  3489. /*
  3490. * Ignore writes that would select not implemented region.
  3491. * This is architecturally UNPREDICTABLE.
  3492. */
  3493. if (value >= cpu->pmsav8r_hdregion) {
  3494. return;
  3495. }
  3496. env->pmsav8.hprselr = value;
  3497. }
  3498. static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3499. uint64_t value)
  3500. {
  3501. ARMCPU *cpu = env_archcpu(env);
  3502. uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
  3503. (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
  3504. tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
  3505. if (ri->opc1 & 4) {
  3506. if (index >= cpu->pmsav8r_hdregion) {
  3507. return;
  3508. }
  3509. if (ri->opc2 & 0x1) {
  3510. env->pmsav8.hprlar[index] = value;
  3511. } else {
  3512. env->pmsav8.hprbar[index] = value;
  3513. }
  3514. } else {
  3515. if (index >= cpu->pmsav7_dregion) {
  3516. return;
  3517. }
  3518. if (ri->opc2 & 0x1) {
  3519. env->pmsav8.rlar[M_REG_NS][index] = value;
  3520. } else {
  3521. env->pmsav8.rbar[M_REG_NS][index] = value;
  3522. }
  3523. }
  3524. }
  3525. static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3526. {
  3527. ARMCPU *cpu = env_archcpu(env);
  3528. uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
  3529. (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
  3530. if (ri->opc1 & 4) {
  3531. if (index >= cpu->pmsav8r_hdregion) {
  3532. return 0x0;
  3533. }
  3534. if (ri->opc2 & 0x1) {
  3535. return env->pmsav8.hprlar[index];
  3536. } else {
  3537. return env->pmsav8.hprbar[index];
  3538. }
  3539. } else {
  3540. if (index >= cpu->pmsav7_dregion) {
  3541. return 0x0;
  3542. }
  3543. if (ri->opc2 & 0x1) {
  3544. return env->pmsav8.rlar[M_REG_NS][index];
  3545. } else {
  3546. return env->pmsav8.rbar[M_REG_NS][index];
  3547. }
  3548. }
  3549. }
  3550. static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
  3551. { .name = "PRBAR",
  3552. .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
  3553. .access = PL1_RW, .type = ARM_CP_NO_RAW,
  3554. .accessfn = access_tvm_trvm,
  3555. .readfn = prbar_read, .writefn = prbar_write },
  3556. { .name = "PRLAR",
  3557. .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
  3558. .access = PL1_RW, .type = ARM_CP_NO_RAW,
  3559. .accessfn = access_tvm_trvm,
  3560. .readfn = prlar_read, .writefn = prlar_write },
  3561. { .name = "PRSELR", .resetvalue = 0,
  3562. .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
  3563. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3564. .writefn = prselr_write,
  3565. .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
  3566. { .name = "HPRBAR", .resetvalue = 0,
  3567. .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
  3568. .access = PL2_RW, .type = ARM_CP_NO_RAW,
  3569. .readfn = hprbar_read, .writefn = hprbar_write },
  3570. { .name = "HPRLAR",
  3571. .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
  3572. .access = PL2_RW, .type = ARM_CP_NO_RAW,
  3573. .readfn = hprlar_read, .writefn = hprlar_write },
  3574. { .name = "HPRSELR", .resetvalue = 0,
  3575. .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
  3576. .access = PL2_RW,
  3577. .writefn = hprselr_write,
  3578. .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
  3579. { .name = "HPRENR",
  3580. .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
  3581. .access = PL2_RW, .type = ARM_CP_NO_RAW,
  3582. .readfn = hprenr_read, .writefn = hprenr_write },
  3583. };
  3584. static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
  3585. /*
  3586. * Reset for all these registers is handled in arm_cpu_reset(),
  3587. * because the PMSAv7 is also used by M-profile CPUs, which do
  3588. * not register cpregs but still need the state to be reset.
  3589. */
  3590. { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
  3591. .access = PL1_RW, .type = ARM_CP_NO_RAW,
  3592. .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
  3593. .readfn = pmsav7_read, .writefn = pmsav7_write,
  3594. .resetfn = arm_cp_reset_ignore },
  3595. { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
  3596. .access = PL1_RW, .type = ARM_CP_NO_RAW,
  3597. .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
  3598. .readfn = pmsav7_read, .writefn = pmsav7_write,
  3599. .resetfn = arm_cp_reset_ignore },
  3600. { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
  3601. .access = PL1_RW, .type = ARM_CP_NO_RAW,
  3602. .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
  3603. .readfn = pmsav7_read, .writefn = pmsav7_write,
  3604. .resetfn = arm_cp_reset_ignore },
  3605. { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
  3606. .access = PL1_RW,
  3607. .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
  3608. .writefn = pmsav7_rgnr_write,
  3609. .resetfn = arm_cp_reset_ignore },
  3610. };
  3611. static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
  3612. { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
  3613. .access = PL1_RW, .type = ARM_CP_ALIAS,
  3614. .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
  3615. .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
  3616. { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
  3617. .access = PL1_RW, .type = ARM_CP_ALIAS,
  3618. .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
  3619. .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
  3620. { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
  3621. .access = PL1_RW,
  3622. .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
  3623. .resetvalue = 0, },
  3624. { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
  3625. .access = PL1_RW,
  3626. .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
  3627. .resetvalue = 0, },
  3628. { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
  3629. .access = PL1_RW,
  3630. .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
  3631. { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
  3632. .access = PL1_RW,
  3633. .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
  3634. /* Protection region base and size registers */
  3635. { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
  3636. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3637. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
  3638. { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
  3639. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3640. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
  3641. { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
  3642. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3643. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
  3644. { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
  3645. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3646. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
  3647. { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
  3648. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3649. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
  3650. { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
  3651. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3652. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
  3653. { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
  3654. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3655. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
  3656. { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
  3657. .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
  3658. .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
  3659. };
  3660. static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3661. uint64_t value)
  3662. {
  3663. ARMCPU *cpu = env_archcpu(env);
  3664. if (!arm_feature(env, ARM_FEATURE_V8)) {
  3665. if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
  3666. /*
  3667. * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
  3668. * using Long-descriptor translation table format
  3669. */
  3670. value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
  3671. } else if (arm_feature(env, ARM_FEATURE_EL3)) {
  3672. /*
  3673. * In an implementation that includes the Security Extensions
  3674. * TTBCR has additional fields PD0 [4] and PD1 [5] for
  3675. * Short-descriptor translation table format.
  3676. */
  3677. value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
  3678. } else {
  3679. value &= TTBCR_N;
  3680. }
  3681. }
  3682. if (arm_feature(env, ARM_FEATURE_LPAE)) {
  3683. /*
  3684. * With LPAE the TTBCR could result in a change of ASID
  3685. * via the TTBCR.A1 bit, so do a TLB flush.
  3686. */
  3687. tlb_flush(CPU(cpu));
  3688. }
  3689. raw_write(env, ri, value);
  3690. }
  3691. static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3692. uint64_t value)
  3693. {
  3694. ARMCPU *cpu = env_archcpu(env);
  3695. /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
  3696. tlb_flush(CPU(cpu));
  3697. raw_write(env, ri, value);
  3698. }
  3699. static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3700. uint64_t value)
  3701. {
  3702. /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
  3703. if (cpreg_field_is_64bit(ri) &&
  3704. extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
  3705. ARMCPU *cpu = env_archcpu(env);
  3706. tlb_flush(CPU(cpu));
  3707. }
  3708. raw_write(env, ri, value);
  3709. }
  3710. static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3711. uint64_t value)
  3712. {
  3713. /*
  3714. * If we are running with E2&0 regime, then an ASID is active.
  3715. * Flush if that might be changing. Note we're not checking
  3716. * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
  3717. * holds the active ASID, only checking the field that might.
  3718. */
  3719. if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
  3720. (arm_hcr_el2_eff(env) & HCR_E2H)) {
  3721. uint16_t mask = ARMMMUIdxBit_E20_2 |
  3722. ARMMMUIdxBit_E20_2_PAN |
  3723. ARMMMUIdxBit_E20_0;
  3724. tlb_flush_by_mmuidx(env_cpu(env), mask);
  3725. }
  3726. raw_write(env, ri, value);
  3727. }
  3728. static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3729. uint64_t value)
  3730. {
  3731. ARMCPU *cpu = env_archcpu(env);
  3732. CPUState *cs = CPU(cpu);
  3733. /*
  3734. * A change in VMID to the stage2 page table (Stage2) invalidates
  3735. * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
  3736. */
  3737. if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
  3738. tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
  3739. }
  3740. raw_write(env, ri, value);
  3741. }
  3742. static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
  3743. { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
  3744. .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
  3745. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
  3746. offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
  3747. { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
  3748. .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
  3749. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
  3750. offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
  3751. { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
  3752. .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
  3753. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
  3754. offsetof(CPUARMState, cp15.dfar_ns) } },
  3755. { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
  3756. .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
  3757. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3758. .fgt = FGT_FAR_EL1,
  3759. .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
  3760. .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
  3761. .resetvalue = 0, },
  3762. };
  3763. static const ARMCPRegInfo vmsa_cp_reginfo[] = {
  3764. { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
  3765. .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
  3766. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3767. .fgt = FGT_ESR_EL1,
  3768. .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
  3769. .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
  3770. { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
  3771. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
  3772. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3773. .fgt = FGT_TTBR0_EL1,
  3774. .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
  3775. .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
  3776. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
  3777. offsetof(CPUARMState, cp15.ttbr0_ns) } },
  3778. { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
  3779. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
  3780. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3781. .fgt = FGT_TTBR1_EL1,
  3782. .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
  3783. .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
  3784. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
  3785. offsetof(CPUARMState, cp15.ttbr1_ns) } },
  3786. { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
  3787. .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
  3788. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3789. .fgt = FGT_TCR_EL1,
  3790. .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
  3791. .writefn = vmsa_tcr_el12_write,
  3792. .raw_writefn = raw_write,
  3793. .resetvalue = 0,
  3794. .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
  3795. { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
  3796. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3797. .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
  3798. .raw_writefn = raw_write,
  3799. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
  3800. offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
  3801. };
  3802. /*
  3803. * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
  3804. * qemu tlbs nor adjusting cached masks.
  3805. */
  3806. static const ARMCPRegInfo ttbcr2_reginfo = {
  3807. .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
  3808. .access = PL1_RW, .accessfn = access_tvm_trvm,
  3809. .type = ARM_CP_ALIAS,
  3810. .bank_fieldoffsets = {
  3811. offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
  3812. offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
  3813. },
  3814. };
  3815. static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3816. uint64_t value)
  3817. {
  3818. env->cp15.c15_ticonfig = value & 0xe7;
  3819. /* The OS_TYPE bit in this register changes the reported CPUID! */
  3820. env->cp15.c0_cpuid = (value & (1 << 5)) ?
  3821. ARM_CPUID_TI915T : ARM_CPUID_TI925T;
  3822. }
  3823. static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3824. uint64_t value)
  3825. {
  3826. env->cp15.c15_threadid = value & 0xffff;
  3827. }
  3828. static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3829. uint64_t value)
  3830. {
  3831. /* Wait-for-interrupt (deprecated) */
  3832. cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
  3833. }
  3834. static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3835. uint64_t value)
  3836. {
  3837. /*
  3838. * On OMAP there are registers indicating the max/min index of dcache lines
  3839. * containing a dirty line; cache flush operations have to reset these.
  3840. */
  3841. env->cp15.c15_i_max = 0x000;
  3842. env->cp15.c15_i_min = 0xff0;
  3843. }
  3844. static const ARMCPRegInfo omap_cp_reginfo[] = {
  3845. { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
  3846. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
  3847. .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
  3848. .resetvalue = 0, },
  3849. { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
  3850. .access = PL1_RW, .type = ARM_CP_NOP },
  3851. { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
  3852. .access = PL1_RW,
  3853. .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
  3854. .writefn = omap_ticonfig_write },
  3855. { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
  3856. .access = PL1_RW,
  3857. .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
  3858. { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
  3859. .access = PL1_RW, .resetvalue = 0xff0,
  3860. .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
  3861. { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
  3862. .access = PL1_RW,
  3863. .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
  3864. .writefn = omap_threadid_write },
  3865. { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
  3866. .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
  3867. .type = ARM_CP_NO_RAW,
  3868. .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
  3869. /*
  3870. * TODO: Peripheral port remap register:
  3871. * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
  3872. * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
  3873. * when MMU is off.
  3874. */
  3875. { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
  3876. .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
  3877. .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
  3878. .writefn = omap_cachemaint_write },
  3879. { .name = "C9", .cp = 15, .crn = 9,
  3880. .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
  3881. .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
  3882. };
  3883. static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
  3884. uint64_t value)
  3885. {
  3886. env->cp15.c15_cpar = value & 0x3fff;
  3887. }
  3888. static const ARMCPRegInfo xscale_cp_reginfo[] = {
  3889. { .name = "XSCALE_CPAR",
  3890. .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
  3891. .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
  3892. .writefn = xscale_cpar_write, },
  3893. { .name = "XSCALE_AUXCR",
  3894. .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
  3895. .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
  3896. .resetvalue = 0, },
  3897. /*
  3898. * XScale specific cache-lockdown: since we have no cache we NOP these
  3899. * and hope the guest does not really rely on cache behaviour.
  3900. */
  3901. { .name = "XSCALE_LOCK_ICACHE_LINE",
  3902. .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
  3903. .access = PL1_W, .type = ARM_CP_NOP },
  3904. { .name = "XSCALE_UNLOCK_ICACHE",
  3905. .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
  3906. .access = PL1_W, .type = ARM_CP_NOP },
  3907. { .name = "XSCALE_DCACHE_LOCK",
  3908. .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
  3909. .access = PL1_RW, .type = ARM_CP_NOP },
  3910. { .name = "XSCALE_UNLOCK_DCACHE",
  3911. .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
  3912. .access = PL1_W, .type = ARM_CP_NOP },
  3913. };
  3914. static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
  3915. /*
  3916. * RAZ/WI the whole crn=15 space, when we don't have a more specific
  3917. * implementation of this implementation-defined space.
  3918. * Ideally this should eventually disappear in favour of actually
  3919. * implementing the correct behaviour for all cores.
  3920. */
  3921. { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
  3922. .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
  3923. .access = PL1_RW,
  3924. .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
  3925. .resetvalue = 0 },
  3926. };
  3927. static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
  3928. /* Cache status: RAZ because we have no cache so it's always clean */
  3929. { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
  3930. .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
  3931. .resetvalue = 0 },
  3932. };
  3933. static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
  3934. /* We never have a block transfer operation in progress */
  3935. { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
  3936. .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
  3937. .resetvalue = 0 },
  3938. /* The cache ops themselves: these all NOP for QEMU */
  3939. { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
  3940. .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3941. { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
  3942. .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3943. { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
  3944. .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3945. { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
  3946. .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3947. { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
  3948. .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3949. { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
  3950. .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
  3951. };
  3952. static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
  3953. /*
  3954. * The cache test-and-clean instructions always return (1 << 30)
  3955. * to indicate that there are no dirty cache lines.
  3956. */
  3957. { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
  3958. .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
  3959. .resetvalue = (1 << 30) },
  3960. { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
  3961. .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
  3962. .resetvalue = (1 << 30) },
  3963. };
  3964. static const ARMCPRegInfo strongarm_cp_reginfo[] = {
  3965. /* Ignore ReadBuffer accesses */
  3966. { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
  3967. .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
  3968. .access = PL1_RW, .resetvalue = 0,
  3969. .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
  3970. };
  3971. static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3972. {
  3973. unsigned int cur_el = arm_current_el(env);
  3974. if (arm_is_el2_enabled(env) && cur_el == 1) {
  3975. return env->cp15.vpidr_el2;
  3976. }
  3977. return raw_read(env, ri);
  3978. }
  3979. static uint64_t mpidr_read_val(CPUARMState *env)
  3980. {
  3981. ARMCPU *cpu = env_archcpu(env);
  3982. uint64_t mpidr = cpu->mp_affinity;
  3983. if (arm_feature(env, ARM_FEATURE_V7MP)) {
  3984. mpidr |= (1U << 31);
  3985. /*
  3986. * Cores which are uniprocessor (non-coherent)
  3987. * but still implement the MP extensions set
  3988. * bit 30. (For instance, Cortex-R5).
  3989. */
  3990. if (cpu->mp_is_up) {
  3991. mpidr |= (1u << 30);
  3992. }
  3993. }
  3994. return mpidr;
  3995. }
  3996. static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  3997. {
  3998. unsigned int cur_el = arm_current_el(env);
  3999. if (arm_is_el2_enabled(env) && cur_el == 1) {
  4000. return env->cp15.vmpidr_el2;
  4001. }
  4002. return mpidr_read_val(env);
  4003. }
  4004. static const ARMCPRegInfo lpae_cp_reginfo[] = {
  4005. /* NOP AMAIR0/1 */
  4006. { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
  4007. .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
  4008. .access = PL1_RW, .accessfn = access_tvm_trvm,
  4009. .fgt = FGT_AMAIR_EL1,
  4010. .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
  4011. .type = ARM_CP_CONST, .resetvalue = 0 },
  4012. /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
  4013. { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
  4014. .access = PL1_RW, .accessfn = access_tvm_trvm,
  4015. .type = ARM_CP_CONST, .resetvalue = 0 },
  4016. { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
  4017. .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
  4018. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
  4019. offsetof(CPUARMState, cp15.par_ns)} },
  4020. { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
  4021. .access = PL1_RW, .accessfn = access_tvm_trvm,
  4022. .type = ARM_CP_64BIT | ARM_CP_ALIAS,
  4023. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
  4024. offsetof(CPUARMState, cp15.ttbr0_ns) },
  4025. .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
  4026. { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
  4027. .access = PL1_RW, .accessfn = access_tvm_trvm,
  4028. .type = ARM_CP_64BIT | ARM_CP_ALIAS,
  4029. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
  4030. offsetof(CPUARMState, cp15.ttbr1_ns) },
  4031. .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
  4032. };
  4033. static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4034. {
  4035. return vfp_get_fpcr(env);
  4036. }
  4037. static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4038. uint64_t value)
  4039. {
  4040. vfp_set_fpcr(env, value);
  4041. }
  4042. static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4043. {
  4044. return vfp_get_fpsr(env);
  4045. }
  4046. static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4047. uint64_t value)
  4048. {
  4049. vfp_set_fpsr(env, value);
  4050. }
  4051. static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
  4052. bool isread)
  4053. {
  4054. if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
  4055. return CP_ACCESS_TRAP_EL1;
  4056. }
  4057. return CP_ACCESS_OK;
  4058. }
  4059. static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4060. uint64_t value)
  4061. {
  4062. env->daif = value & PSTATE_DAIF;
  4063. }
  4064. static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4065. {
  4066. return env->pstate & PSTATE_PAN;
  4067. }
  4068. static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4069. uint64_t value)
  4070. {
  4071. env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
  4072. }
  4073. static const ARMCPRegInfo pan_reginfo = {
  4074. .name = "PAN", .state = ARM_CP_STATE_AA64,
  4075. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
  4076. .type = ARM_CP_NO_RAW, .access = PL1_RW,
  4077. .readfn = aa64_pan_read, .writefn = aa64_pan_write
  4078. };
  4079. static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4080. {
  4081. return env->pstate & PSTATE_UAO;
  4082. }
  4083. static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4084. uint64_t value)
  4085. {
  4086. env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
  4087. }
  4088. static const ARMCPRegInfo uao_reginfo = {
  4089. .name = "UAO", .state = ARM_CP_STATE_AA64,
  4090. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
  4091. .type = ARM_CP_NO_RAW, .access = PL1_RW,
  4092. .readfn = aa64_uao_read, .writefn = aa64_uao_write
  4093. };
  4094. static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4095. {
  4096. return env->pstate & PSTATE_DIT;
  4097. }
  4098. static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4099. uint64_t value)
  4100. {
  4101. env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
  4102. }
  4103. static const ARMCPRegInfo dit_reginfo = {
  4104. .name = "DIT", .state = ARM_CP_STATE_AA64,
  4105. .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
  4106. .type = ARM_CP_NO_RAW, .access = PL0_RW,
  4107. .readfn = aa64_dit_read, .writefn = aa64_dit_write
  4108. };
  4109. static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4110. {
  4111. return env->pstate & PSTATE_SSBS;
  4112. }
  4113. static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4114. uint64_t value)
  4115. {
  4116. env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
  4117. }
  4118. static const ARMCPRegInfo ssbs_reginfo = {
  4119. .name = "SSBS", .state = ARM_CP_STATE_AA64,
  4120. .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
  4121. .type = ARM_CP_NO_RAW, .access = PL0_RW,
  4122. .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
  4123. };
  4124. static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
  4125. const ARMCPRegInfo *ri,
  4126. bool isread)
  4127. {
  4128. /* Cache invalidate/clean to Point of Coherency or Persistence... */
  4129. switch (arm_current_el(env)) {
  4130. case 0:
  4131. /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */
  4132. if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
  4133. return CP_ACCESS_TRAP_EL1;
  4134. }
  4135. /* fall through */
  4136. case 1:
  4137. /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
  4138. if (arm_hcr_el2_eff(env) & HCR_TPCP) {
  4139. return CP_ACCESS_TRAP_EL2;
  4140. }
  4141. break;
  4142. }
  4143. return CP_ACCESS_OK;
  4144. }
  4145. static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
  4146. {
  4147. /* Cache invalidate/clean to Point of Unification... */
  4148. switch (arm_current_el(env)) {
  4149. case 0:
  4150. /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */
  4151. if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
  4152. return CP_ACCESS_TRAP_EL1;
  4153. }
  4154. /* fall through */
  4155. case 1:
  4156. /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
  4157. if (arm_hcr_el2_eff(env) & hcrflags) {
  4158. return CP_ACCESS_TRAP_EL2;
  4159. }
  4160. break;
  4161. }
  4162. return CP_ACCESS_OK;
  4163. }
  4164. static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
  4165. bool isread)
  4166. {
  4167. return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
  4168. }
  4169. static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
  4170. bool isread)
  4171. {
  4172. return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
  4173. }
  4174. static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
  4175. bool isread)
  4176. {
  4177. int cur_el = arm_current_el(env);
  4178. if (cur_el < 2) {
  4179. uint64_t hcr = arm_hcr_el2_eff(env);
  4180. if (cur_el == 0) {
  4181. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  4182. if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
  4183. return CP_ACCESS_TRAP_EL2;
  4184. }
  4185. } else {
  4186. if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
  4187. return CP_ACCESS_TRAP_EL1;
  4188. }
  4189. if (hcr & HCR_TDZ) {
  4190. return CP_ACCESS_TRAP_EL2;
  4191. }
  4192. }
  4193. } else if (hcr & HCR_TDZ) {
  4194. return CP_ACCESS_TRAP_EL2;
  4195. }
  4196. }
  4197. return CP_ACCESS_OK;
  4198. }
  4199. static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4200. {
  4201. ARMCPU *cpu = env_archcpu(env);
  4202. int dzp_bit = 1 << 4;
  4203. /* DZP indicates whether DC ZVA access is allowed */
  4204. if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
  4205. dzp_bit = 0;
  4206. }
  4207. return cpu->dcz_blocksize | dzp_bit;
  4208. }
  4209. static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
  4210. bool isread)
  4211. {
  4212. if (!(env->pstate & PSTATE_SP)) {
  4213. /*
  4214. * Access to SP_EL0 is undefined if it's being used as
  4215. * the stack pointer.
  4216. */
  4217. return CP_ACCESS_UNDEFINED;
  4218. }
  4219. return CP_ACCESS_OK;
  4220. }
  4221. static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4222. {
  4223. return env->pstate & PSTATE_SP;
  4224. }
  4225. static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
  4226. {
  4227. update_spsel(env, val);
  4228. }
  4229. static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4230. uint64_t value)
  4231. {
  4232. ARMCPU *cpu = env_archcpu(env);
  4233. if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
  4234. /* M bit is RAZ/WI for PMSA with no MPU implemented */
  4235. value &= ~SCTLR_M;
  4236. }
  4237. /* ??? Lots of these bits are not implemented. */
  4238. if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
  4239. if (ri->opc1 == 6) { /* SCTLR_EL3 */
  4240. value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
  4241. } else {
  4242. value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
  4243. SCTLR_ATA0 | SCTLR_ATA);
  4244. }
  4245. }
  4246. if (raw_read(env, ri) == value) {
  4247. /*
  4248. * Skip the TLB flush if nothing actually changed; Linux likes
  4249. * to do a lot of pointless SCTLR writes.
  4250. */
  4251. return;
  4252. }
  4253. raw_write(env, ri, value);
  4254. /* This may enable/disable the MMU, so do a TLB flush. */
  4255. tlb_flush(CPU(cpu));
  4256. if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
  4257. /*
  4258. * Normally we would always end the TB on an SCTLR write; see the
  4259. * comment in ARMCPRegInfo sctlr initialization below for why Xscale
  4260. * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
  4261. * of hflags from the translator, so do it here.
  4262. */
  4263. arm_rebuild_hflags(env);
  4264. }
  4265. }
  4266. static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4267. uint64_t value)
  4268. {
  4269. /*
  4270. * Some MDCR_EL3 bits affect whether PMU counters are running:
  4271. * if we are trying to change any of those then we must
  4272. * bracket this update with PMU start/finish calls.
  4273. */
  4274. bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
  4275. if (pmu_op) {
  4276. pmu_op_start(env);
  4277. }
  4278. env->cp15.mdcr_el3 = value;
  4279. if (pmu_op) {
  4280. pmu_op_finish(env);
  4281. }
  4282. }
  4283. static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4284. uint64_t value)
  4285. {
  4286. /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
  4287. mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
  4288. }
  4289. static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4290. uint64_t value)
  4291. {
  4292. /*
  4293. * Some MDCR_EL2 bits affect whether PMU counters are running:
  4294. * if we are trying to change any of those then we must
  4295. * bracket this update with PMU start/finish calls.
  4296. */
  4297. bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
  4298. if (pmu_op) {
  4299. pmu_op_start(env);
  4300. }
  4301. env->cp15.mdcr_el2 = value;
  4302. if (pmu_op) {
  4303. pmu_op_finish(env);
  4304. }
  4305. }
  4306. static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
  4307. bool isread)
  4308. {
  4309. if (arm_current_el(env) == 1) {
  4310. uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
  4311. if (hcr_nv == (HCR_NV | HCR_NV1)) {
  4312. return CP_ACCESS_TRAP_EL2;
  4313. }
  4314. }
  4315. return CP_ACCESS_OK;
  4316. }
  4317. #ifdef CONFIG_USER_ONLY
  4318. /*
  4319. * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
  4320. * code to get around W^X restrictions, where one region is writable and the
  4321. * other is executable.
  4322. *
  4323. * Since the executable region is never written to we cannot detect code
  4324. * changes when running in user mode, and rely on the emulated JIT telling us
  4325. * that the code has changed by executing this instruction.
  4326. */
  4327. static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4328. uint64_t value)
  4329. {
  4330. uint64_t icache_line_mask, start_address, end_address;
  4331. const ARMCPU *cpu;
  4332. cpu = env_archcpu(env);
  4333. icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
  4334. start_address = value & ~icache_line_mask;
  4335. end_address = value | icache_line_mask;
  4336. mmap_lock();
  4337. tb_invalidate_phys_range(start_address, end_address);
  4338. mmap_unlock();
  4339. }
  4340. #endif
  4341. static const ARMCPRegInfo v8_cp_reginfo[] = {
  4342. /*
  4343. * Minimal set of EL0-visible registers. This will need to be expanded
  4344. * significantly for system emulation of AArch64 CPUs.
  4345. */
  4346. { .name = "NZCV", .state = ARM_CP_STATE_AA64,
  4347. .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
  4348. .access = PL0_RW, .type = ARM_CP_NZCV },
  4349. { .name = "DAIF", .state = ARM_CP_STATE_AA64,
  4350. .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
  4351. .type = ARM_CP_NO_RAW,
  4352. .access = PL0_RW, .accessfn = aa64_daif_access,
  4353. .fieldoffset = offsetof(CPUARMState, daif),
  4354. .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
  4355. { .name = "FPCR", .state = ARM_CP_STATE_AA64,
  4356. .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
  4357. .access = PL0_RW, .type = ARM_CP_FPU,
  4358. .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
  4359. { .name = "FPSR", .state = ARM_CP_STATE_AA64,
  4360. .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
  4361. .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
  4362. .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
  4363. { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
  4364. .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
  4365. .access = PL0_R, .type = ARM_CP_NO_RAW,
  4366. .fgt = FGT_DCZID_EL0,
  4367. .readfn = aa64_dczid_read },
  4368. { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
  4369. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
  4370. .access = PL0_W, .type = ARM_CP_DC_ZVA,
  4371. #ifndef CONFIG_USER_ONLY
  4372. /* Avoid overhead of an access check that always passes in user-mode */
  4373. .accessfn = aa64_zva_access,
  4374. .fgt = FGT_DCZVA,
  4375. #endif
  4376. },
  4377. { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
  4378. .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
  4379. .access = PL1_R, .type = ARM_CP_CURRENTEL },
  4380. /*
  4381. * Instruction cache ops. All of these except `IC IVAU` NOP because we
  4382. * don't emulate caches.
  4383. */
  4384. { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
  4385. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
  4386. .access = PL1_W, .type = ARM_CP_NOP,
  4387. .fgt = FGT_ICIALLUIS,
  4388. .accessfn = access_ticab },
  4389. { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
  4390. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
  4391. .access = PL1_W, .type = ARM_CP_NOP,
  4392. .fgt = FGT_ICIALLU,
  4393. .accessfn = access_tocu },
  4394. { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
  4395. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
  4396. .access = PL0_W,
  4397. .fgt = FGT_ICIVAU,
  4398. .accessfn = access_tocu,
  4399. #ifdef CONFIG_USER_ONLY
  4400. .type = ARM_CP_NO_RAW,
  4401. .writefn = ic_ivau_write
  4402. #else
  4403. .type = ARM_CP_NOP
  4404. #endif
  4405. },
  4406. /* Cache ops: all NOPs since we don't emulate caches */
  4407. { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
  4408. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
  4409. .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
  4410. .fgt = FGT_DCIVAC,
  4411. .type = ARM_CP_NOP },
  4412. { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
  4413. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
  4414. .fgt = FGT_DCISW,
  4415. .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
  4416. { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
  4417. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
  4418. .access = PL0_W, .type = ARM_CP_NOP,
  4419. .fgt = FGT_DCCVAC,
  4420. .accessfn = aa64_cacheop_poc_access },
  4421. { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
  4422. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
  4423. .fgt = FGT_DCCSW,
  4424. .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
  4425. { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
  4426. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
  4427. .access = PL0_W, .type = ARM_CP_NOP,
  4428. .fgt = FGT_DCCVAU,
  4429. .accessfn = access_tocu },
  4430. { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
  4431. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
  4432. .access = PL0_W, .type = ARM_CP_NOP,
  4433. .fgt = FGT_DCCIVAC,
  4434. .accessfn = aa64_cacheop_poc_access },
  4435. { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
  4436. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
  4437. .fgt = FGT_DCCISW,
  4438. .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
  4439. #ifndef CONFIG_USER_ONLY
  4440. /* 64 bit address translation operations */
  4441. { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
  4442. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
  4443. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4444. .fgt = FGT_ATS1E1R,
  4445. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  4446. { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
  4447. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
  4448. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4449. .fgt = FGT_ATS1E1W,
  4450. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  4451. { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
  4452. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
  4453. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4454. .fgt = FGT_ATS1E0R,
  4455. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  4456. { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
  4457. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
  4458. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4459. .fgt = FGT_ATS1E0W,
  4460. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  4461. { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
  4462. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
  4463. .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4464. .accessfn = at_e012_access, .writefn = ats_write64 },
  4465. { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
  4466. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
  4467. .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4468. .accessfn = at_e012_access, .writefn = ats_write64 },
  4469. { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
  4470. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
  4471. .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4472. .accessfn = at_e012_access, .writefn = ats_write64 },
  4473. { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
  4474. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
  4475. .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4476. .accessfn = at_e012_access, .writefn = ats_write64 },
  4477. /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
  4478. { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
  4479. .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
  4480. .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4481. .writefn = ats_write64 },
  4482. { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
  4483. .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
  4484. .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  4485. .writefn = ats_write64 },
  4486. { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
  4487. .type = ARM_CP_ALIAS,
  4488. .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
  4489. .access = PL1_RW, .resetvalue = 0,
  4490. .fgt = FGT_PAR_EL1,
  4491. .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
  4492. .writefn = par_write },
  4493. #endif
  4494. /* 32 bit cache operations */
  4495. { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
  4496. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
  4497. { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
  4498. .type = ARM_CP_NOP, .access = PL1_W },
  4499. { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
  4500. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
  4501. { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
  4502. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
  4503. { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
  4504. .type = ARM_CP_NOP, .access = PL1_W },
  4505. { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
  4506. .type = ARM_CP_NOP, .access = PL1_W },
  4507. { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
  4508. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
  4509. { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
  4510. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  4511. { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
  4512. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
  4513. { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
  4514. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  4515. { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
  4516. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
  4517. { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
  4518. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
  4519. { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
  4520. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  4521. /* MMU Domain access control / MPU write buffer control */
  4522. { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
  4523. .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
  4524. .writefn = dacr_write, .raw_writefn = raw_write,
  4525. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
  4526. offsetoflow32(CPUARMState, cp15.dacr_ns) } },
  4527. { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
  4528. .type = ARM_CP_ALIAS,
  4529. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
  4530. .access = PL1_RW, .accessfn = access_nv1,
  4531. .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
  4532. .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
  4533. { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
  4534. .type = ARM_CP_ALIAS,
  4535. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
  4536. .access = PL1_RW, .accessfn = access_nv1,
  4537. .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
  4538. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
  4539. /*
  4540. * We rely on the access checks not allowing the guest to write to the
  4541. * state field when SPSel indicates that it's being used as the stack
  4542. * pointer.
  4543. */
  4544. { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
  4545. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
  4546. .access = PL1_RW, .accessfn = sp_el0_access,
  4547. .type = ARM_CP_ALIAS,
  4548. .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
  4549. { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
  4550. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
  4551. .nv2_redirect_offset = 0x240,
  4552. .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
  4553. .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
  4554. { .name = "SPSel", .state = ARM_CP_STATE_AA64,
  4555. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
  4556. .type = ARM_CP_NO_RAW,
  4557. .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
  4558. { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
  4559. .type = ARM_CP_ALIAS,
  4560. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
  4561. .access = PL2_RW,
  4562. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
  4563. { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
  4564. .type = ARM_CP_ALIAS,
  4565. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
  4566. .access = PL2_RW,
  4567. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
  4568. { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
  4569. .type = ARM_CP_ALIAS,
  4570. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
  4571. .access = PL2_RW,
  4572. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
  4573. { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
  4574. .type = ARM_CP_ALIAS,
  4575. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
  4576. .access = PL2_RW,
  4577. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
  4578. { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
  4579. .type = ARM_CP_IO,
  4580. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
  4581. .resetvalue = 0,
  4582. .access = PL3_RW,
  4583. .writefn = mdcr_el3_write,
  4584. .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
  4585. { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
  4586. .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
  4587. .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
  4588. .writefn = sdcr_write,
  4589. .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
  4590. };
  4591. /* These are present only when EL1 supports AArch32 */
  4592. static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
  4593. { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
  4594. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
  4595. .access = PL2_RW,
  4596. .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
  4597. .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
  4598. { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
  4599. .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
  4600. .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
  4601. .writefn = dacr_write, .raw_writefn = raw_write,
  4602. .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
  4603. { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
  4604. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
  4605. .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
  4606. .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
  4607. };
  4608. static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
  4609. {
  4610. ARMCPU *cpu = env_archcpu(env);
  4611. if (arm_feature(env, ARM_FEATURE_V8)) {
  4612. valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
  4613. } else {
  4614. valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
  4615. }
  4616. if (arm_feature(env, ARM_FEATURE_EL3)) {
  4617. valid_mask &= ~HCR_HCD;
  4618. } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
  4619. /*
  4620. * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
  4621. * However, if we're using the SMC PSCI conduit then QEMU is
  4622. * effectively acting like EL3 firmware and so the guest at
  4623. * EL2 should retain the ability to prevent EL1 from being
  4624. * able to make SMC calls into the ersatz firmware, so in
  4625. * that case HCR.TSC should be read/write.
  4626. */
  4627. valid_mask &= ~HCR_TSC;
  4628. }
  4629. if (arm_feature(env, ARM_FEATURE_AARCH64)) {
  4630. if (cpu_isar_feature(aa64_vh, cpu)) {
  4631. valid_mask |= HCR_E2H;
  4632. }
  4633. if (cpu_isar_feature(aa64_ras, cpu)) {
  4634. valid_mask |= HCR_TERR | HCR_TEA;
  4635. }
  4636. if (cpu_isar_feature(aa64_lor, cpu)) {
  4637. valid_mask |= HCR_TLOR;
  4638. }
  4639. if (cpu_isar_feature(aa64_pauth, cpu)) {
  4640. valid_mask |= HCR_API | HCR_APK;
  4641. }
  4642. if (cpu_isar_feature(aa64_mte, cpu)) {
  4643. valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
  4644. }
  4645. if (cpu_isar_feature(aa64_scxtnum, cpu)) {
  4646. valid_mask |= HCR_ENSCXT;
  4647. }
  4648. if (cpu_isar_feature(aa64_fwb, cpu)) {
  4649. valid_mask |= HCR_FWB;
  4650. }
  4651. if (cpu_isar_feature(aa64_rme, cpu)) {
  4652. valid_mask |= HCR_GPF;
  4653. }
  4654. if (cpu_isar_feature(aa64_nv, cpu)) {
  4655. valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
  4656. }
  4657. if (cpu_isar_feature(aa64_nv2, cpu)) {
  4658. valid_mask |= HCR_NV2;
  4659. }
  4660. }
  4661. if (cpu_isar_feature(any_evt, cpu)) {
  4662. valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
  4663. } else if (cpu_isar_feature(any_half_evt, cpu)) {
  4664. valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
  4665. }
  4666. /* Clear RES0 bits. */
  4667. value &= valid_mask;
  4668. /*
  4669. * These bits change the MMU setup:
  4670. * HCR_VM enables stage 2 translation
  4671. * HCR_PTW forbids certain page-table setups
  4672. * HCR_DC disables stage1 and enables stage2 translation
  4673. * HCR_DCT enables tagging on (disabled) stage1 translation
  4674. * HCR_FWB changes the interpretation of stage2 descriptor bits
  4675. * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
  4676. */
  4677. if ((env->cp15.hcr_el2 ^ value) &
  4678. (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
  4679. tlb_flush(CPU(cpu));
  4680. }
  4681. env->cp15.hcr_el2 = value;
  4682. /*
  4683. * Updates to VI and VF require us to update the status of
  4684. * virtual interrupts, which are the logical OR of these bits
  4685. * and the state of the input lines from the GIC. (This requires
  4686. * that we have the BQL, which is done by marking the
  4687. * reginfo structs as ARM_CP_IO.)
  4688. * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
  4689. * VFNMI, it is never possible for it to be taken immediately
  4690. * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
  4691. * at EL0 or EL1, and HCR can only be written at EL2.
  4692. */
  4693. g_assert(bql_locked());
  4694. arm_cpu_update_virq(cpu);
  4695. arm_cpu_update_vfiq(cpu);
  4696. arm_cpu_update_vserr(cpu);
  4697. if (cpu_isar_feature(aa64_nmi, cpu)) {
  4698. arm_cpu_update_vinmi(cpu);
  4699. arm_cpu_update_vfnmi(cpu);
  4700. }
  4701. }
  4702. static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  4703. {
  4704. do_hcr_write(env, value, 0);
  4705. }
  4706. static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
  4707. uint64_t value)
  4708. {
  4709. /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
  4710. value = deposit64(env->cp15.hcr_el2, 32, 32, value);
  4711. do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
  4712. }
  4713. static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
  4714. uint64_t value)
  4715. {
  4716. /* Handle HCR write, i.e. write to low half of HCR_EL2 */
  4717. value = deposit64(env->cp15.hcr_el2, 0, 32, value);
  4718. do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
  4719. }
  4720. /*
  4721. * Return the effective value of HCR_EL2, at the given security state.
  4722. * Bits that are not included here:
  4723. * RW (read from SCR_EL3.RW as needed)
  4724. */
  4725. uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
  4726. {
  4727. uint64_t ret = env->cp15.hcr_el2;
  4728. assert(space != ARMSS_Root);
  4729. if (!arm_is_el2_enabled_secstate(env, space)) {
  4730. /*
  4731. * "This register has no effect if EL2 is not enabled in the
  4732. * current Security state". This is ARMv8.4-SecEL2 speak for
  4733. * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
  4734. *
  4735. * Prior to that, the language was "In an implementation that
  4736. * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
  4737. * as if this field is 0 for all purposes other than a direct
  4738. * read or write access of HCR_EL2". With lots of enumeration
  4739. * on a per-field basis. In current QEMU, this is condition
  4740. * is arm_is_secure_below_el3.
  4741. *
  4742. * Since the v8.4 language applies to the entire register, and
  4743. * appears to be backward compatible, use that.
  4744. */
  4745. return 0;
  4746. }
  4747. /*
  4748. * For a cpu that supports both aarch64 and aarch32, we can set bits
  4749. * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
  4750. * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
  4751. */
  4752. if (!arm_el_is_aa64(env, 2)) {
  4753. uint64_t aa32_valid;
  4754. /*
  4755. * These bits are up-to-date as of ARMv8.6.
  4756. * For HCR, it's easiest to list just the 2 bits that are invalid.
  4757. * For HCR2, list those that are valid.
  4758. */
  4759. aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
  4760. aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
  4761. HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
  4762. ret &= aa32_valid;
  4763. }
  4764. if (ret & HCR_TGE) {
  4765. /* These bits are up-to-date as of ARMv8.6. */
  4766. if (ret & HCR_E2H) {
  4767. ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
  4768. HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
  4769. HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
  4770. HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
  4771. HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
  4772. HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
  4773. } else {
  4774. ret |= HCR_FMO | HCR_IMO | HCR_AMO;
  4775. }
  4776. ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
  4777. HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
  4778. HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
  4779. HCR_TLOR);
  4780. }
  4781. return ret;
  4782. }
  4783. uint64_t arm_hcr_el2_eff(CPUARMState *env)
  4784. {
  4785. if (arm_feature(env, ARM_FEATURE_M)) {
  4786. return 0;
  4787. }
  4788. return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
  4789. }
  4790. /*
  4791. * Corresponds to ARM pseudocode function ELIsInHost().
  4792. */
  4793. bool el_is_in_host(CPUARMState *env, int el)
  4794. {
  4795. uint64_t mask;
  4796. /*
  4797. * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
  4798. * Perform the simplest bit tests first, and validate EL2 afterward.
  4799. */
  4800. if (el & 1) {
  4801. return false; /* EL1 or EL3 */
  4802. }
  4803. /*
  4804. * Note that hcr_write() checks isar_feature_aa64_vh(),
  4805. * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
  4806. */
  4807. mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
  4808. if ((env->cp15.hcr_el2 & mask) != mask) {
  4809. return false;
  4810. }
  4811. /* TGE and/or E2H set: double check those bits are currently legal. */
  4812. return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
  4813. }
  4814. static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4815. uint64_t value)
  4816. {
  4817. ARMCPU *cpu = env_archcpu(env);
  4818. uint64_t valid_mask = 0;
  4819. /* FEAT_MOPS adds MSCEn and MCE2 */
  4820. if (cpu_isar_feature(aa64_mops, cpu)) {
  4821. valid_mask |= HCRX_MSCEN | HCRX_MCE2;
  4822. }
  4823. /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
  4824. if (cpu_isar_feature(aa64_nmi, cpu)) {
  4825. valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
  4826. }
  4827. /* FEAT_CMOW adds CMOW */
  4828. if (cpu_isar_feature(aa64_cmow, cpu)) {
  4829. valid_mask |= HCRX_CMOW;
  4830. }
  4831. /* FEAT_XS adds FGTnXS, FnXS */
  4832. if (cpu_isar_feature(aa64_xs, cpu)) {
  4833. valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
  4834. }
  4835. /* Clear RES0 bits. */
  4836. env->cp15.hcrx_el2 = value & valid_mask;
  4837. /*
  4838. * Updates to VINMI and VFNMI require us to update the status of
  4839. * virtual NMI, which are the logical OR of these bits
  4840. * and the state of the input lines from the GIC. (This requires
  4841. * that we have the BQL, which is done by marking the
  4842. * reginfo structs as ARM_CP_IO.)
  4843. * Note that if a write to HCRX pends a VINMI or VFNMI it is never
  4844. * possible for it to be taken immediately, because VINMI and
  4845. * VFNMI are masked unless running at EL0 or EL1, and HCRX
  4846. * can only be written at EL2.
  4847. */
  4848. if (cpu_isar_feature(aa64_nmi, cpu)) {
  4849. g_assert(bql_locked());
  4850. arm_cpu_update_vinmi(cpu);
  4851. arm_cpu_update_vfnmi(cpu);
  4852. }
  4853. }
  4854. static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
  4855. bool isread)
  4856. {
  4857. if (arm_current_el(env) == 2
  4858. && arm_feature(env, ARM_FEATURE_EL3)
  4859. && !(env->cp15.scr_el3 & SCR_HXEN)) {
  4860. return CP_ACCESS_TRAP_EL3;
  4861. }
  4862. return CP_ACCESS_OK;
  4863. }
  4864. static const ARMCPRegInfo hcrx_el2_reginfo = {
  4865. .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
  4866. .type = ARM_CP_IO,
  4867. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
  4868. .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
  4869. .nv2_redirect_offset = 0xa0,
  4870. .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
  4871. };
  4872. /* Return the effective value of HCRX_EL2. */
  4873. uint64_t arm_hcrx_el2_eff(CPUARMState *env)
  4874. {
  4875. /*
  4876. * The bits in this register behave as 0 for all purposes other than
  4877. * direct reads of the register if SCR_EL3.HXEn is 0.
  4878. * If EL2 is not enabled in the current security state, then the
  4879. * bit may behave as if 0, or as if 1, depending on the bit.
  4880. * For the moment, we treat the EL2-disabled case as taking
  4881. * priority over the HXEn-disabled case. This is true for the only
  4882. * bit for a feature which we implement where the answer is different
  4883. * for the two cases (MSCEn for FEAT_MOPS).
  4884. * This may need to be revisited for future bits.
  4885. */
  4886. if (!arm_is_el2_enabled(env)) {
  4887. uint64_t hcrx = 0;
  4888. if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
  4889. /* MSCEn behaves as 1 if EL2 is not enabled */
  4890. hcrx |= HCRX_MSCEN;
  4891. }
  4892. return hcrx;
  4893. }
  4894. if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
  4895. return 0;
  4896. }
  4897. return env->cp15.hcrx_el2;
  4898. }
  4899. static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
  4900. uint64_t value)
  4901. {
  4902. /*
  4903. * For A-profile AArch32 EL3, if NSACR.CP10
  4904. * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
  4905. */
  4906. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
  4907. !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
  4908. uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
  4909. value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
  4910. }
  4911. env->cp15.cptr_el[2] = value;
  4912. }
  4913. static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
  4914. {
  4915. /*
  4916. * For A-profile AArch32 EL3, if NSACR.CP10
  4917. * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
  4918. */
  4919. uint64_t value = env->cp15.cptr_el[2];
  4920. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
  4921. !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
  4922. value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
  4923. }
  4924. return value;
  4925. }
  4926. static const ARMCPRegInfo el2_cp_reginfo[] = {
  4927. { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
  4928. .type = ARM_CP_IO,
  4929. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
  4930. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
  4931. .nv2_redirect_offset = 0x78,
  4932. .writefn = hcr_write, .raw_writefn = raw_write },
  4933. { .name = "HCR", .state = ARM_CP_STATE_AA32,
  4934. .type = ARM_CP_ALIAS | ARM_CP_IO,
  4935. .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
  4936. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
  4937. .writefn = hcr_writelow },
  4938. { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
  4939. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
  4940. .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
  4941. { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
  4942. .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
  4943. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
  4944. .access = PL2_RW,
  4945. .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
  4946. { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
  4947. .type = ARM_CP_NV2_REDIRECT,
  4948. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
  4949. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
  4950. { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
  4951. .type = ARM_CP_NV2_REDIRECT,
  4952. .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
  4953. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
  4954. { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
  4955. .type = ARM_CP_ALIAS,
  4956. .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
  4957. .access = PL2_RW,
  4958. .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
  4959. { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
  4960. .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
  4961. .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
  4962. .access = PL2_RW,
  4963. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
  4964. { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
  4965. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
  4966. .access = PL2_RW, .writefn = vbar_write,
  4967. .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
  4968. .resetvalue = 0 },
  4969. { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
  4970. .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
  4971. .access = PL3_RW, .type = ARM_CP_ALIAS,
  4972. .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
  4973. { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
  4974. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
  4975. .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
  4976. .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
  4977. .readfn = cptr_el2_read, .writefn = cptr_el2_write },
  4978. { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
  4979. .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
  4980. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
  4981. .resetvalue = 0 },
  4982. { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
  4983. .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
  4984. .access = PL2_RW, .type = ARM_CP_ALIAS,
  4985. .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
  4986. { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
  4987. .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
  4988. .access = PL2_RW, .type = ARM_CP_CONST,
  4989. .resetvalue = 0 },
  4990. /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
  4991. { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
  4992. .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
  4993. .access = PL2_RW, .type = ARM_CP_CONST,
  4994. .resetvalue = 0 },
  4995. { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
  4996. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
  4997. .access = PL2_RW, .type = ARM_CP_CONST,
  4998. .resetvalue = 0 },
  4999. { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
  5000. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
  5001. .access = PL2_RW, .type = ARM_CP_CONST,
  5002. .resetvalue = 0 },
  5003. { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
  5004. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
  5005. .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
  5006. .raw_writefn = raw_write,
  5007. .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
  5008. { .name = "VTCR", .state = ARM_CP_STATE_AA32,
  5009. .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
  5010. .type = ARM_CP_ALIAS,
  5011. .access = PL2_RW, .accessfn = access_el3_aa32ns,
  5012. .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
  5013. { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
  5014. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
  5015. .access = PL2_RW,
  5016. .nv2_redirect_offset = 0x40,
  5017. /* no .writefn needed as this can't cause an ASID change */
  5018. .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
  5019. { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
  5020. .cp = 15, .opc1 = 6, .crm = 2,
  5021. .type = ARM_CP_64BIT | ARM_CP_ALIAS,
  5022. .access = PL2_RW, .accessfn = access_el3_aa32ns,
  5023. .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
  5024. .writefn = vttbr_write, .raw_writefn = raw_write },
  5025. { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
  5026. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
  5027. .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
  5028. .nv2_redirect_offset = 0x20,
  5029. .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
  5030. { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
  5031. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
  5032. .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
  5033. .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
  5034. { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
  5035. .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
  5036. .access = PL2_RW, .resetvalue = 0,
  5037. .nv2_redirect_offset = 0x90,
  5038. .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
  5039. { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
  5040. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
  5041. .access = PL2_RW, .resetvalue = 0,
  5042. .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
  5043. .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
  5044. { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
  5045. .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
  5046. .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
  5047. #ifndef CONFIG_USER_ONLY
  5048. /*
  5049. * Unlike the other EL2-related AT operations, these must
  5050. * UNDEF from EL3 if EL2 is not implemented, which is why we
  5051. * define them here rather than with the rest of the AT ops.
  5052. */
  5053. { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
  5054. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
  5055. .access = PL2_W, .accessfn = at_s1e2_access,
  5056. .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
  5057. .writefn = ats_write64 },
  5058. { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
  5059. .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
  5060. .access = PL2_W, .accessfn = at_s1e2_access,
  5061. .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
  5062. .writefn = ats_write64 },
  5063. /*
  5064. * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
  5065. * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
  5066. * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
  5067. * to behave as if SCR.NS was 1.
  5068. */
  5069. { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
  5070. .access = PL2_W,
  5071. .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
  5072. { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
  5073. .access = PL2_W,
  5074. .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
  5075. { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
  5076. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
  5077. /*
  5078. * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
  5079. * reset values as IMPDEF. We choose to reset to 3 to comply with
  5080. * both ARMv7 and ARMv8.
  5081. */
  5082. .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
  5083. .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
  5084. .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
  5085. { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
  5086. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
  5087. .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
  5088. .writefn = gt_cntvoff_write,
  5089. .nv2_redirect_offset = 0x60,
  5090. .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
  5091. { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
  5092. .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
  5093. .writefn = gt_cntvoff_write,
  5094. .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
  5095. { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
  5096. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
  5097. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
  5098. .type = ARM_CP_IO, .access = PL2_RW,
  5099. .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
  5100. { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
  5101. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
  5102. .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
  5103. .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
  5104. { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
  5105. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
  5106. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
  5107. .resetfn = gt_hyp_timer_reset,
  5108. .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
  5109. { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
  5110. .type = ARM_CP_IO,
  5111. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
  5112. .access = PL2_RW,
  5113. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
  5114. .resetvalue = 0,
  5115. .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
  5116. #endif
  5117. { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
  5118. .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
  5119. .access = PL2_RW, .accessfn = access_el3_aa32ns,
  5120. .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
  5121. { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
  5122. .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
  5123. .access = PL2_RW,
  5124. .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
  5125. { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
  5126. .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
  5127. .access = PL2_RW,
  5128. .nv2_redirect_offset = 0x80,
  5129. .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
  5130. };
  5131. static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
  5132. { .name = "HCR2", .state = ARM_CP_STATE_AA32,
  5133. .type = ARM_CP_ALIAS | ARM_CP_IO,
  5134. .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
  5135. .access = PL2_RW,
  5136. .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
  5137. .writefn = hcr_writehigh },
  5138. };
  5139. static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
  5140. bool isread)
  5141. {
  5142. if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
  5143. return CP_ACCESS_OK;
  5144. }
  5145. return CP_ACCESS_UNDEFINED;
  5146. }
  5147. static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
  5148. { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
  5149. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
  5150. .access = PL2_RW, .accessfn = sel2_access,
  5151. .nv2_redirect_offset = 0x30,
  5152. .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
  5153. { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
  5154. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
  5155. .access = PL2_RW, .accessfn = sel2_access,
  5156. .nv2_redirect_offset = 0x48,
  5157. .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
  5158. };
  5159. static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
  5160. bool isread)
  5161. {
  5162. /*
  5163. * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
  5164. * At Secure EL1 it traps to EL3 or EL2.
  5165. */
  5166. if (arm_current_el(env) == 3) {
  5167. return CP_ACCESS_OK;
  5168. }
  5169. if (arm_is_secure_below_el3(env)) {
  5170. if (env->cp15.scr_el3 & SCR_EEL2) {
  5171. return CP_ACCESS_TRAP_EL2;
  5172. }
  5173. return CP_ACCESS_TRAP_EL3;
  5174. }
  5175. /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
  5176. if (isread) {
  5177. return CP_ACCESS_OK;
  5178. }
  5179. return CP_ACCESS_UNDEFINED;
  5180. }
  5181. static const ARMCPRegInfo el3_cp_reginfo[] = {
  5182. { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
  5183. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
  5184. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
  5185. .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
  5186. { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
  5187. .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
  5188. .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
  5189. .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
  5190. .writefn = scr_write, .raw_writefn = raw_write },
  5191. { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
  5192. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
  5193. .access = PL3_RW, .resetvalue = 0,
  5194. .fieldoffset = offsetof(CPUARMState, cp15.sder) },
  5195. { .name = "SDER",
  5196. .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
  5197. .access = PL3_RW, .resetvalue = 0,
  5198. .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
  5199. { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
  5200. .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
  5201. .writefn = vbar_write, .resetvalue = 0,
  5202. .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
  5203. { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
  5204. .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
  5205. .access = PL3_RW, .resetvalue = 0,
  5206. .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
  5207. { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
  5208. .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
  5209. .access = PL3_RW,
  5210. /* no .writefn needed as this can't cause an ASID change */
  5211. .resetvalue = 0,
  5212. .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
  5213. { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
  5214. .type = ARM_CP_ALIAS,
  5215. .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
  5216. .access = PL3_RW,
  5217. .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
  5218. { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
  5219. .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
  5220. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
  5221. { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
  5222. .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
  5223. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
  5224. { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
  5225. .type = ARM_CP_ALIAS,
  5226. .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
  5227. .access = PL3_RW,
  5228. .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
  5229. { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
  5230. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
  5231. .access = PL3_RW, .writefn = vbar_write,
  5232. .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
  5233. .resetvalue = 0 },
  5234. { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
  5235. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
  5236. .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
  5237. .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
  5238. { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
  5239. .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
  5240. .access = PL3_RW, .resetvalue = 0,
  5241. .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
  5242. { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
  5243. .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
  5244. .access = PL3_RW, .type = ARM_CP_CONST,
  5245. .resetvalue = 0 },
  5246. { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
  5247. .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
  5248. .access = PL3_RW, .type = ARM_CP_CONST,
  5249. .resetvalue = 0 },
  5250. { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
  5251. .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
  5252. .access = PL3_RW, .type = ARM_CP_CONST,
  5253. .resetvalue = 0 },
  5254. };
  5255. #ifndef CONFIG_USER_ONLY
  5256. static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
  5257. bool isread)
  5258. {
  5259. if (arm_current_el(env) == 1) {
  5260. /* This must be a FEAT_NV access */
  5261. return CP_ACCESS_OK;
  5262. }
  5263. if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
  5264. return CP_ACCESS_UNDEFINED;
  5265. }
  5266. return CP_ACCESS_OK;
  5267. }
  5268. static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
  5269. bool isread)
  5270. {
  5271. if (arm_current_el(env) == 1) {
  5272. /* This must be a FEAT_NV access with NVx == 101 */
  5273. if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
  5274. return CP_ACCESS_TRAP_EL2;
  5275. }
  5276. }
  5277. return e2h_access(env, ri, isread);
  5278. }
  5279. static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
  5280. bool isread)
  5281. {
  5282. if (arm_current_el(env) == 1) {
  5283. /* This must be a FEAT_NV access with NVx == 101 */
  5284. if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
  5285. return CP_ACCESS_TRAP_EL2;
  5286. }
  5287. }
  5288. return e2h_access(env, ri, isread);
  5289. }
  5290. /* Test if system register redirection is to occur in the current state. */
  5291. static bool redirect_for_e2h(CPUARMState *env)
  5292. {
  5293. return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
  5294. }
  5295. static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
  5296. {
  5297. CPReadFn *readfn;
  5298. if (redirect_for_e2h(env)) {
  5299. /* Switch to the saved EL2 version of the register. */
  5300. ri = ri->opaque;
  5301. readfn = ri->readfn;
  5302. } else {
  5303. readfn = ri->orig_readfn;
  5304. }
  5305. if (readfn == NULL) {
  5306. readfn = raw_read;
  5307. }
  5308. return readfn(env, ri);
  5309. }
  5310. static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5311. uint64_t value)
  5312. {
  5313. CPWriteFn *writefn;
  5314. if (redirect_for_e2h(env)) {
  5315. /* Switch to the saved EL2 version of the register. */
  5316. ri = ri->opaque;
  5317. writefn = ri->writefn;
  5318. } else {
  5319. writefn = ri->orig_writefn;
  5320. }
  5321. if (writefn == NULL) {
  5322. writefn = raw_write;
  5323. }
  5324. writefn(env, ri, value);
  5325. }
  5326. static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
  5327. {
  5328. /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
  5329. return ri->orig_readfn(env, ri->opaque);
  5330. }
  5331. static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5332. uint64_t value)
  5333. {
  5334. /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
  5335. return ri->orig_writefn(env, ri->opaque, value);
  5336. }
  5337. static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
  5338. const ARMCPRegInfo *ri,
  5339. bool isread)
  5340. {
  5341. if (arm_current_el(env) == 1) {
  5342. /*
  5343. * This must be a FEAT_NV access (will either trap or redirect
  5344. * to memory). None of the registers with _EL12 aliases want to
  5345. * apply their trap controls for this kind of access, so don't
  5346. * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
  5347. */
  5348. return CP_ACCESS_OK;
  5349. }
  5350. /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
  5351. if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
  5352. return CP_ACCESS_UNDEFINED;
  5353. }
  5354. if (ri->orig_accessfn) {
  5355. return ri->orig_accessfn(env, ri->opaque, isread);
  5356. }
  5357. return CP_ACCESS_OK;
  5358. }
  5359. static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
  5360. {
  5361. struct E2HAlias {
  5362. uint32_t src_key, dst_key, new_key;
  5363. const char *src_name, *dst_name, *new_name;
  5364. bool (*feature)(const ARMISARegisters *id);
  5365. };
  5366. #define K(op0, op1, crn, crm, op2) \
  5367. ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
  5368. static const struct E2HAlias aliases[] = {
  5369. { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
  5370. "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
  5371. { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
  5372. "CPACR", "CPTR_EL2", "CPACR_EL12" },
  5373. { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
  5374. "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
  5375. { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
  5376. "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
  5377. { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
  5378. "TCR_EL1", "TCR_EL2", "TCR_EL12" },
  5379. { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
  5380. "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
  5381. { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
  5382. "ELR_EL1", "ELR_EL2", "ELR_EL12" },
  5383. { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
  5384. "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
  5385. { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
  5386. "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
  5387. { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
  5388. "ESR_EL1", "ESR_EL2", "ESR_EL12" },
  5389. { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
  5390. "FAR_EL1", "FAR_EL2", "FAR_EL12" },
  5391. { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
  5392. "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
  5393. { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
  5394. "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
  5395. { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
  5396. "VBAR", "VBAR_EL2", "VBAR_EL12" },
  5397. { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
  5398. "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
  5399. { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
  5400. "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
  5401. /*
  5402. * Note that redirection of ZCR is mentioned in the description
  5403. * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
  5404. * not in the summary table.
  5405. */
  5406. { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
  5407. "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
  5408. { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
  5409. "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
  5410. { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
  5411. "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
  5412. { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
  5413. "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
  5414. isar_feature_aa64_scxtnum },
  5415. /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
  5416. /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
  5417. };
  5418. #undef K
  5419. size_t i;
  5420. for (i = 0; i < ARRAY_SIZE(aliases); i++) {
  5421. const struct E2HAlias *a = &aliases[i];
  5422. ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
  5423. bool ok;
  5424. if (a->feature && !a->feature(&cpu->isar)) {
  5425. continue;
  5426. }
  5427. src_reg = g_hash_table_lookup(cpu->cp_regs,
  5428. (gpointer)(uintptr_t)a->src_key);
  5429. dst_reg = g_hash_table_lookup(cpu->cp_regs,
  5430. (gpointer)(uintptr_t)a->dst_key);
  5431. g_assert(src_reg != NULL);
  5432. g_assert(dst_reg != NULL);
  5433. /* Cross-compare names to detect typos in the keys. */
  5434. g_assert(strcmp(src_reg->name, a->src_name) == 0);
  5435. g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
  5436. /* None of the core system registers use opaque; we will. */
  5437. g_assert(src_reg->opaque == NULL);
  5438. /* Create alias before redirection so we dup the right data. */
  5439. new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
  5440. new_reg->name = a->new_name;
  5441. new_reg->type |= ARM_CP_ALIAS;
  5442. /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
  5443. new_reg->access &= PL2_RW | PL3_RW;
  5444. /* The new_reg op fields are as per new_key, not the target reg */
  5445. new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
  5446. >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
  5447. new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
  5448. >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
  5449. new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
  5450. >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
  5451. new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
  5452. >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
  5453. new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
  5454. >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
  5455. new_reg->opaque = src_reg;
  5456. new_reg->orig_readfn = src_reg->readfn ?: raw_read;
  5457. new_reg->orig_writefn = src_reg->writefn ?: raw_write;
  5458. new_reg->orig_accessfn = src_reg->accessfn;
  5459. if (!new_reg->raw_readfn) {
  5460. new_reg->raw_readfn = raw_read;
  5461. }
  5462. if (!new_reg->raw_writefn) {
  5463. new_reg->raw_writefn = raw_write;
  5464. }
  5465. new_reg->readfn = el2_e2h_e12_read;
  5466. new_reg->writefn = el2_e2h_e12_write;
  5467. new_reg->accessfn = el2_e2h_e12_access;
  5468. /*
  5469. * If the _EL1 register is redirected to memory by FEAT_NV2,
  5470. * then it shares the offset with the _EL12 register,
  5471. * and which one is redirected depends on HCR_EL2.NV1.
  5472. */
  5473. if (new_reg->nv2_redirect_offset) {
  5474. assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
  5475. new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
  5476. new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
  5477. }
  5478. ok = g_hash_table_insert(cpu->cp_regs,
  5479. (gpointer)(uintptr_t)a->new_key, new_reg);
  5480. g_assert(ok);
  5481. src_reg->opaque = dst_reg;
  5482. src_reg->orig_readfn = src_reg->readfn ?: raw_read;
  5483. src_reg->orig_writefn = src_reg->writefn ?: raw_write;
  5484. if (!src_reg->raw_readfn) {
  5485. src_reg->raw_readfn = raw_read;
  5486. }
  5487. if (!src_reg->raw_writefn) {
  5488. src_reg->raw_writefn = raw_write;
  5489. }
  5490. src_reg->readfn = el2_e2h_read;
  5491. src_reg->writefn = el2_e2h_write;
  5492. }
  5493. }
  5494. #endif
  5495. static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
  5496. bool isread)
  5497. {
  5498. int cur_el = arm_current_el(env);
  5499. if (cur_el < 2) {
  5500. uint64_t hcr = arm_hcr_el2_eff(env);
  5501. if (cur_el == 0) {
  5502. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  5503. if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
  5504. return CP_ACCESS_TRAP_EL2;
  5505. }
  5506. } else {
  5507. if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
  5508. return CP_ACCESS_TRAP_EL1;
  5509. }
  5510. if (hcr & HCR_TID2) {
  5511. return CP_ACCESS_TRAP_EL2;
  5512. }
  5513. }
  5514. } else if (hcr & HCR_TID2) {
  5515. return CP_ACCESS_TRAP_EL2;
  5516. }
  5517. }
  5518. if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
  5519. return CP_ACCESS_TRAP_EL2;
  5520. }
  5521. return CP_ACCESS_OK;
  5522. }
  5523. /*
  5524. * Check for traps to RAS registers, which are controlled
  5525. * by HCR_EL2.TERR and SCR_EL3.TERR.
  5526. */
  5527. static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
  5528. bool isread)
  5529. {
  5530. int el = arm_current_el(env);
  5531. if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
  5532. return CP_ACCESS_TRAP_EL2;
  5533. }
  5534. if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
  5535. return CP_ACCESS_TRAP_EL3;
  5536. }
  5537. return CP_ACCESS_OK;
  5538. }
  5539. static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  5540. {
  5541. int el = arm_current_el(env);
  5542. if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
  5543. return env->cp15.vdisr_el2;
  5544. }
  5545. if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
  5546. return 0; /* RAZ/WI */
  5547. }
  5548. return env->cp15.disr_el1;
  5549. }
  5550. static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
  5551. {
  5552. int el = arm_current_el(env);
  5553. if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
  5554. env->cp15.vdisr_el2 = val;
  5555. return;
  5556. }
  5557. if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
  5558. return; /* RAZ/WI */
  5559. }
  5560. env->cp15.disr_el1 = val;
  5561. }
  5562. /*
  5563. * Minimal RAS implementation with no Error Records.
  5564. * Which means that all of the Error Record registers:
  5565. * ERXADDR_EL1
  5566. * ERXCTLR_EL1
  5567. * ERXFR_EL1
  5568. * ERXMISC0_EL1
  5569. * ERXMISC1_EL1
  5570. * ERXMISC2_EL1
  5571. * ERXMISC3_EL1
  5572. * ERXPFGCDN_EL1 (RASv1p1)
  5573. * ERXPFGCTL_EL1 (RASv1p1)
  5574. * ERXPFGF_EL1 (RASv1p1)
  5575. * ERXSTATUS_EL1
  5576. * and
  5577. * ERRSELR_EL1
  5578. * may generate UNDEFINED, which is the effect we get by not
  5579. * listing them at all.
  5580. *
  5581. * These registers have fine-grained trap bits, but UNDEF-to-EL1
  5582. * is higher priority than FGT-to-EL2 so we do not need to list them
  5583. * in order to check for an FGT.
  5584. */
  5585. static const ARMCPRegInfo minimal_ras_reginfo[] = {
  5586. { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
  5587. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
  5588. .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
  5589. .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
  5590. { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
  5591. .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
  5592. .access = PL1_R, .accessfn = access_terr,
  5593. .fgt = FGT_ERRIDR_EL1,
  5594. .type = ARM_CP_CONST, .resetvalue = 0 },
  5595. { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
  5596. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
  5597. .nv2_redirect_offset = 0x500,
  5598. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
  5599. { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
  5600. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
  5601. .nv2_redirect_offset = 0x508,
  5602. .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
  5603. };
  5604. /*
  5605. * Return the exception level to which exceptions should be taken
  5606. * via SVEAccessTrap. This excludes the check for whether the exception
  5607. * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
  5608. * be found by testing 0 < fp_exception_el < sve_exception_el.
  5609. *
  5610. * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
  5611. * pseudocode does *not* separate out the FP trap checks, but has them
  5612. * all in one function.
  5613. */
  5614. int sve_exception_el(CPUARMState *env, int el)
  5615. {
  5616. #ifndef CONFIG_USER_ONLY
  5617. if (el <= 1 && !el_is_in_host(env, el)) {
  5618. switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
  5619. case 1:
  5620. if (el != 0) {
  5621. break;
  5622. }
  5623. /* fall through */
  5624. case 0:
  5625. case 2:
  5626. return 1;
  5627. }
  5628. }
  5629. if (el <= 2 && arm_is_el2_enabled(env)) {
  5630. /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
  5631. if (env->cp15.hcr_el2 & HCR_E2H) {
  5632. switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
  5633. case 1:
  5634. if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
  5635. break;
  5636. }
  5637. /* fall through */
  5638. case 0:
  5639. case 2:
  5640. return 2;
  5641. }
  5642. } else {
  5643. if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
  5644. return 2;
  5645. }
  5646. }
  5647. }
  5648. /* CPTR_EL3. Since EZ is negative we must check for EL3. */
  5649. if (arm_feature(env, ARM_FEATURE_EL3)
  5650. && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
  5651. return 3;
  5652. }
  5653. #endif
  5654. return 0;
  5655. }
  5656. /*
  5657. * Return the exception level to which exceptions should be taken for SME.
  5658. * C.f. the ARM pseudocode function CheckSMEAccess.
  5659. */
  5660. int sme_exception_el(CPUARMState *env, int el)
  5661. {
  5662. #ifndef CONFIG_USER_ONLY
  5663. if (el <= 1 && !el_is_in_host(env, el)) {
  5664. switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
  5665. case 1:
  5666. if (el != 0) {
  5667. break;
  5668. }
  5669. /* fall through */
  5670. case 0:
  5671. case 2:
  5672. return 1;
  5673. }
  5674. }
  5675. if (el <= 2 && arm_is_el2_enabled(env)) {
  5676. /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
  5677. if (env->cp15.hcr_el2 & HCR_E2H) {
  5678. switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
  5679. case 1:
  5680. if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
  5681. break;
  5682. }
  5683. /* fall through */
  5684. case 0:
  5685. case 2:
  5686. return 2;
  5687. }
  5688. } else {
  5689. if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
  5690. return 2;
  5691. }
  5692. }
  5693. }
  5694. /* CPTR_EL3. Since ESM is negative we must check for EL3. */
  5695. if (arm_feature(env, ARM_FEATURE_EL3)
  5696. && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
  5697. return 3;
  5698. }
  5699. #endif
  5700. return 0;
  5701. }
  5702. /*
  5703. * Given that SVE is enabled, return the vector length for EL.
  5704. */
  5705. uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
  5706. {
  5707. ARMCPU *cpu = env_archcpu(env);
  5708. uint64_t *cr = env->vfp.zcr_el;
  5709. uint32_t map = cpu->sve_vq.map;
  5710. uint32_t len = ARM_MAX_VQ - 1;
  5711. if (sm) {
  5712. cr = env->vfp.smcr_el;
  5713. map = cpu->sme_vq.map;
  5714. }
  5715. if (el <= 1 && !el_is_in_host(env, el)) {
  5716. len = MIN(len, 0xf & (uint32_t)cr[1]);
  5717. }
  5718. if (el <= 2 && arm_is_el2_enabled(env)) {
  5719. len = MIN(len, 0xf & (uint32_t)cr[2]);
  5720. }
  5721. if (arm_feature(env, ARM_FEATURE_EL3)) {
  5722. len = MIN(len, 0xf & (uint32_t)cr[3]);
  5723. }
  5724. map &= MAKE_64BIT_MASK(0, len + 1);
  5725. if (map != 0) {
  5726. return 31 - clz32(map);
  5727. }
  5728. /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
  5729. assert(sm);
  5730. return ctz32(cpu->sme_vq.map);
  5731. }
  5732. uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
  5733. {
  5734. return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
  5735. }
  5736. static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5737. uint64_t value)
  5738. {
  5739. int cur_el = arm_current_el(env);
  5740. int old_len = sve_vqm1_for_el(env, cur_el);
  5741. int new_len;
  5742. /* Bits other than [3:0] are RAZ/WI. */
  5743. QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
  5744. raw_write(env, ri, value & 0xf);
  5745. /*
  5746. * Because we arrived here, we know both FP and SVE are enabled;
  5747. * otherwise we would have trapped access to the ZCR_ELn register.
  5748. */
  5749. new_len = sve_vqm1_for_el(env, cur_el);
  5750. if (new_len < old_len) {
  5751. aarch64_sve_narrow_vq(env, new_len + 1);
  5752. }
  5753. }
  5754. static const ARMCPRegInfo zcr_reginfo[] = {
  5755. { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
  5756. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
  5757. .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
  5758. .access = PL1_RW, .type = ARM_CP_SVE,
  5759. .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
  5760. .writefn = zcr_write, .raw_writefn = raw_write },
  5761. { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
  5762. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
  5763. .access = PL2_RW, .type = ARM_CP_SVE,
  5764. .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
  5765. .writefn = zcr_write, .raw_writefn = raw_write },
  5766. { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
  5767. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
  5768. .access = PL3_RW, .type = ARM_CP_SVE,
  5769. .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
  5770. .writefn = zcr_write, .raw_writefn = raw_write },
  5771. };
  5772. #ifdef TARGET_AARCH64
  5773. static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
  5774. bool isread)
  5775. {
  5776. int el = arm_current_el(env);
  5777. if (el == 0) {
  5778. uint64_t sctlr = arm_sctlr(env, el);
  5779. if (!(sctlr & SCTLR_EnTP2)) {
  5780. return CP_ACCESS_TRAP_EL1;
  5781. }
  5782. }
  5783. /* TODO: FEAT_FGT */
  5784. if (el < 3
  5785. && arm_feature(env, ARM_FEATURE_EL3)
  5786. && !(env->cp15.scr_el3 & SCR_ENTP2)) {
  5787. return CP_ACCESS_TRAP_EL3;
  5788. }
  5789. return CP_ACCESS_OK;
  5790. }
  5791. static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
  5792. bool isread)
  5793. {
  5794. /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
  5795. if (arm_current_el(env) == 2
  5796. && arm_feature(env, ARM_FEATURE_EL3)
  5797. && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
  5798. return CP_ACCESS_TRAP_EL3;
  5799. }
  5800. return CP_ACCESS_OK;
  5801. }
  5802. static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
  5803. bool isread)
  5804. {
  5805. if (arm_current_el(env) < 3
  5806. && arm_feature(env, ARM_FEATURE_EL3)
  5807. && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
  5808. return CP_ACCESS_TRAP_EL3;
  5809. }
  5810. return CP_ACCESS_OK;
  5811. }
  5812. /* ResetSVEState */
  5813. static void arm_reset_sve_state(CPUARMState *env)
  5814. {
  5815. memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
  5816. /* Recall that FFR is stored as pregs[16]. */
  5817. memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
  5818. vfp_set_fpsr(env, 0x0800009f);
  5819. }
  5820. void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
  5821. {
  5822. uint64_t change = (env->svcr ^ new) & mask;
  5823. if (change == 0) {
  5824. return;
  5825. }
  5826. env->svcr ^= change;
  5827. if (change & R_SVCR_SM_MASK) {
  5828. arm_reset_sve_state(env);
  5829. }
  5830. /*
  5831. * ResetSMEState.
  5832. *
  5833. * SetPSTATE_ZA zeros on enable and disable. We can zero this only
  5834. * on enable: while disabled, the storage is inaccessible and the
  5835. * value does not matter. We're not saving the storage in vmstate
  5836. * when disabled either.
  5837. */
  5838. if (change & new & R_SVCR_ZA_MASK) {
  5839. memset(env->zarray, 0, sizeof(env->zarray));
  5840. }
  5841. if (tcg_enabled()) {
  5842. arm_rebuild_hflags(env);
  5843. }
  5844. }
  5845. static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5846. uint64_t value)
  5847. {
  5848. aarch64_set_svcr(env, value, -1);
  5849. }
  5850. static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5851. uint64_t value)
  5852. {
  5853. int cur_el = arm_current_el(env);
  5854. int old_len = sve_vqm1_for_el(env, cur_el);
  5855. int new_len;
  5856. QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
  5857. value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
  5858. raw_write(env, ri, value);
  5859. /*
  5860. * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
  5861. * when SVL is widened (old values kept, or zeros). Choose to keep the
  5862. * current values for simplicity. But for QEMU internals, we must still
  5863. * apply the narrower SVL to the Zregs and Pregs -- see the comment
  5864. * above aarch64_sve_narrow_vq.
  5865. */
  5866. new_len = sve_vqm1_for_el(env, cur_el);
  5867. if (new_len < old_len) {
  5868. aarch64_sve_narrow_vq(env, new_len + 1);
  5869. }
  5870. }
  5871. static const ARMCPRegInfo sme_reginfo[] = {
  5872. { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
  5873. .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
  5874. .access = PL0_RW, .accessfn = access_tpidr2,
  5875. .fgt = FGT_NTPIDR2_EL0,
  5876. .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
  5877. { .name = "SVCR", .state = ARM_CP_STATE_AA64,
  5878. .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
  5879. .access = PL0_RW, .type = ARM_CP_SME,
  5880. .fieldoffset = offsetof(CPUARMState, svcr),
  5881. .writefn = svcr_write, .raw_writefn = raw_write },
  5882. { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
  5883. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
  5884. .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
  5885. .access = PL1_RW, .type = ARM_CP_SME,
  5886. .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
  5887. .writefn = smcr_write, .raw_writefn = raw_write },
  5888. { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
  5889. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
  5890. .access = PL2_RW, .type = ARM_CP_SME,
  5891. .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
  5892. .writefn = smcr_write, .raw_writefn = raw_write },
  5893. { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
  5894. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
  5895. .access = PL3_RW, .type = ARM_CP_SME,
  5896. .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
  5897. .writefn = smcr_write, .raw_writefn = raw_write },
  5898. { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
  5899. .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
  5900. .access = PL1_R, .accessfn = access_aa64_tid1,
  5901. /*
  5902. * IMPLEMENTOR = 0 (software)
  5903. * REVISION = 0 (implementation defined)
  5904. * SMPS = 0 (no streaming execution priority in QEMU)
  5905. * AFFINITY = 0 (streaming sve mode not shared with other PEs)
  5906. */
  5907. .type = ARM_CP_CONST, .resetvalue = 0, },
  5908. /*
  5909. * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
  5910. */
  5911. { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
  5912. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
  5913. .access = PL1_RW, .accessfn = access_smpri,
  5914. .fgt = FGT_NSMPRI_EL1,
  5915. .type = ARM_CP_CONST, .resetvalue = 0 },
  5916. { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
  5917. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
  5918. .nv2_redirect_offset = 0x1f8,
  5919. .access = PL2_RW, .accessfn = access_smprimap,
  5920. .type = ARM_CP_CONST, .resetvalue = 0 },
  5921. };
  5922. static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5923. uint64_t value)
  5924. {
  5925. /* L0GPTSZ is RO; other bits not mentioned are RES0. */
  5926. uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
  5927. R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
  5928. R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
  5929. env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
  5930. }
  5931. static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  5932. {
  5933. env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
  5934. env_archcpu(env)->reset_l0gptsz);
  5935. }
  5936. static const ARMCPRegInfo rme_reginfo[] = {
  5937. { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
  5938. .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
  5939. .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
  5940. .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
  5941. { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
  5942. .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
  5943. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
  5944. { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
  5945. .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
  5946. .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
  5947. { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
  5948. .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
  5949. .access = PL3_W, .type = ARM_CP_NOP },
  5950. };
  5951. static const ARMCPRegInfo rme_mte_reginfo[] = {
  5952. { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
  5953. .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
  5954. .access = PL3_W, .type = ARM_CP_NOP },
  5955. };
  5956. static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
  5957. uint64_t value)
  5958. {
  5959. env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
  5960. }
  5961. static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
  5962. {
  5963. return env->pstate & PSTATE_ALLINT;
  5964. }
  5965. static CPAccessResult aa64_allint_access(CPUARMState *env,
  5966. const ARMCPRegInfo *ri, bool isread)
  5967. {
  5968. if (!isread && arm_current_el(env) == 1 &&
  5969. (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
  5970. return CP_ACCESS_TRAP_EL2;
  5971. }
  5972. return CP_ACCESS_OK;
  5973. }
  5974. static const ARMCPRegInfo nmi_reginfo[] = {
  5975. { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
  5976. .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
  5977. .type = ARM_CP_NO_RAW,
  5978. .access = PL1_RW, .accessfn = aa64_allint_access,
  5979. .fieldoffset = offsetof(CPUARMState, pstate),
  5980. .writefn = aa64_allint_write, .readfn = aa64_allint_read,
  5981. .resetfn = arm_cp_reset_ignore },
  5982. };
  5983. #endif /* TARGET_AARCH64 */
  5984. static void define_pmu_regs(ARMCPU *cpu)
  5985. {
  5986. /*
  5987. * v7 performance monitor control register: same implementor
  5988. * field as main ID register, and we implement four counters in
  5989. * addition to the cycle count register.
  5990. */
  5991. unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
  5992. ARMCPRegInfo pmcr = {
  5993. .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
  5994. .access = PL0_RW,
  5995. .fgt = FGT_PMCR_EL0,
  5996. .type = ARM_CP_IO | ARM_CP_ALIAS,
  5997. .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
  5998. .accessfn = pmreg_access,
  5999. .readfn = pmcr_read, .raw_readfn = raw_read,
  6000. .writefn = pmcr_write, .raw_writefn = raw_write,
  6001. };
  6002. ARMCPRegInfo pmcr64 = {
  6003. .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
  6004. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
  6005. .access = PL0_RW, .accessfn = pmreg_access,
  6006. .fgt = FGT_PMCR_EL0,
  6007. .type = ARM_CP_IO,
  6008. .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
  6009. .resetvalue = cpu->isar.reset_pmcr_el0,
  6010. .readfn = pmcr_read, .raw_readfn = raw_read,
  6011. .writefn = pmcr_write, .raw_writefn = raw_write,
  6012. };
  6013. define_one_arm_cp_reg(cpu, &pmcr);
  6014. define_one_arm_cp_reg(cpu, &pmcr64);
  6015. for (i = 0; i < pmcrn; i++) {
  6016. char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
  6017. char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
  6018. char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
  6019. char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
  6020. ARMCPRegInfo pmev_regs[] = {
  6021. { .name = pmevcntr_name, .cp = 15, .crn = 14,
  6022. .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
  6023. .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
  6024. .fgt = FGT_PMEVCNTRN_EL0,
  6025. .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
  6026. .accessfn = pmreg_access_xevcntr },
  6027. { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
  6028. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
  6029. .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
  6030. .type = ARM_CP_IO,
  6031. .fgt = FGT_PMEVCNTRN_EL0,
  6032. .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
  6033. .raw_readfn = pmevcntr_rawread,
  6034. .raw_writefn = pmevcntr_rawwrite },
  6035. { .name = pmevtyper_name, .cp = 15, .crn = 14,
  6036. .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
  6037. .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
  6038. .fgt = FGT_PMEVTYPERN_EL0,
  6039. .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
  6040. .accessfn = pmreg_access },
  6041. { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
  6042. .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
  6043. .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
  6044. .fgt = FGT_PMEVTYPERN_EL0,
  6045. .type = ARM_CP_IO,
  6046. .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
  6047. .raw_writefn = pmevtyper_rawwrite },
  6048. };
  6049. define_arm_cp_regs(cpu, pmev_regs);
  6050. g_free(pmevcntr_name);
  6051. g_free(pmevcntr_el0_name);
  6052. g_free(pmevtyper_name);
  6053. g_free(pmevtyper_el0_name);
  6054. }
  6055. if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
  6056. ARMCPRegInfo v81_pmu_regs[] = {
  6057. { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
  6058. .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
  6059. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  6060. .fgt = FGT_PMCEIDN_EL0,
  6061. .resetvalue = extract64(cpu->pmceid0, 32, 32) },
  6062. { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
  6063. .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
  6064. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  6065. .fgt = FGT_PMCEIDN_EL0,
  6066. .resetvalue = extract64(cpu->pmceid1, 32, 32) },
  6067. };
  6068. define_arm_cp_regs(cpu, v81_pmu_regs);
  6069. }
  6070. if (cpu_isar_feature(any_pmuv3p4, cpu)) {
  6071. static const ARMCPRegInfo v84_pmmir = {
  6072. .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
  6073. .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
  6074. .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  6075. .fgt = FGT_PMMIR_EL1,
  6076. .resetvalue = 0
  6077. };
  6078. define_one_arm_cp_reg(cpu, &v84_pmmir);
  6079. }
  6080. }
  6081. #ifndef CONFIG_USER_ONLY
  6082. /*
  6083. * We don't know until after realize whether there's a GICv3
  6084. * attached, and that is what registers the gicv3 sysregs.
  6085. * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
  6086. * at runtime.
  6087. */
  6088. static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  6089. {
  6090. ARMCPU *cpu = env_archcpu(env);
  6091. uint64_t pfr1 = cpu->isar.id_pfr1;
  6092. if (env->gicv3state) {
  6093. pfr1 |= 1 << 28;
  6094. }
  6095. return pfr1;
  6096. }
  6097. static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
  6098. {
  6099. ARMCPU *cpu = env_archcpu(env);
  6100. uint64_t pfr0 = cpu->isar.id_aa64pfr0;
  6101. if (env->gicv3state) {
  6102. pfr0 |= 1 << 24;
  6103. }
  6104. return pfr0;
  6105. }
  6106. #endif
  6107. /*
  6108. * Shared logic between LORID and the rest of the LOR* registers.
  6109. * Secure state exclusion has already been dealt with.
  6110. */
  6111. static CPAccessResult access_lor_ns(CPUARMState *env,
  6112. const ARMCPRegInfo *ri, bool isread)
  6113. {
  6114. int el = arm_current_el(env);
  6115. if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
  6116. return CP_ACCESS_TRAP_EL2;
  6117. }
  6118. if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
  6119. return CP_ACCESS_TRAP_EL3;
  6120. }
  6121. return CP_ACCESS_OK;
  6122. }
  6123. static CPAccessResult access_lor_other(CPUARMState *env,
  6124. const ARMCPRegInfo *ri, bool isread)
  6125. {
  6126. if (arm_is_secure_below_el3(env)) {
  6127. /* UNDEF if SCR_EL3.NS == 0 */
  6128. return CP_ACCESS_UNDEFINED;
  6129. }
  6130. return access_lor_ns(env, ri, isread);
  6131. }
  6132. /*
  6133. * A trivial implementation of ARMv8.1-LOR leaves all of these
  6134. * registers fixed at 0, which indicates that there are zero
  6135. * supported Limited Ordering regions.
  6136. */
  6137. static const ARMCPRegInfo lor_reginfo[] = {
  6138. { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
  6139. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
  6140. .access = PL1_RW, .accessfn = access_lor_other,
  6141. .fgt = FGT_LORSA_EL1,
  6142. .type = ARM_CP_CONST, .resetvalue = 0 },
  6143. { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
  6144. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
  6145. .access = PL1_RW, .accessfn = access_lor_other,
  6146. .fgt = FGT_LOREA_EL1,
  6147. .type = ARM_CP_CONST, .resetvalue = 0 },
  6148. { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
  6149. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
  6150. .access = PL1_RW, .accessfn = access_lor_other,
  6151. .fgt = FGT_LORN_EL1,
  6152. .type = ARM_CP_CONST, .resetvalue = 0 },
  6153. { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
  6154. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
  6155. .access = PL1_RW, .accessfn = access_lor_other,
  6156. .fgt = FGT_LORC_EL1,
  6157. .type = ARM_CP_CONST, .resetvalue = 0 },
  6158. { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
  6159. .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
  6160. .access = PL1_R, .accessfn = access_lor_ns,
  6161. .fgt = FGT_LORID_EL1,
  6162. .type = ARM_CP_CONST, .resetvalue = 0 },
  6163. };
  6164. #ifdef TARGET_AARCH64
  6165. static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
  6166. bool isread)
  6167. {
  6168. int el = arm_current_el(env);
  6169. if (el < 2 &&
  6170. arm_is_el2_enabled(env) &&
  6171. !(arm_hcr_el2_eff(env) & HCR_APK)) {
  6172. return CP_ACCESS_TRAP_EL2;
  6173. }
  6174. if (el < 3 &&
  6175. arm_feature(env, ARM_FEATURE_EL3) &&
  6176. !(env->cp15.scr_el3 & SCR_APK)) {
  6177. return CP_ACCESS_TRAP_EL3;
  6178. }
  6179. return CP_ACCESS_OK;
  6180. }
  6181. static const ARMCPRegInfo pauth_reginfo[] = {
  6182. { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
  6183. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
  6184. .access = PL1_RW, .accessfn = access_pauth,
  6185. .fgt = FGT_APDAKEY,
  6186. .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
  6187. { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
  6188. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
  6189. .access = PL1_RW, .accessfn = access_pauth,
  6190. .fgt = FGT_APDAKEY,
  6191. .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
  6192. { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
  6193. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
  6194. .access = PL1_RW, .accessfn = access_pauth,
  6195. .fgt = FGT_APDBKEY,
  6196. .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
  6197. { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
  6198. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
  6199. .access = PL1_RW, .accessfn = access_pauth,
  6200. .fgt = FGT_APDBKEY,
  6201. .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
  6202. { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
  6203. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
  6204. .access = PL1_RW, .accessfn = access_pauth,
  6205. .fgt = FGT_APGAKEY,
  6206. .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
  6207. { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
  6208. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
  6209. .access = PL1_RW, .accessfn = access_pauth,
  6210. .fgt = FGT_APGAKEY,
  6211. .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
  6212. { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
  6213. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
  6214. .access = PL1_RW, .accessfn = access_pauth,
  6215. .fgt = FGT_APIAKEY,
  6216. .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
  6217. { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
  6218. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
  6219. .access = PL1_RW, .accessfn = access_pauth,
  6220. .fgt = FGT_APIAKEY,
  6221. .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
  6222. { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
  6223. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
  6224. .access = PL1_RW, .accessfn = access_pauth,
  6225. .fgt = FGT_APIBKEY,
  6226. .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
  6227. { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
  6228. .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
  6229. .access = PL1_RW, .accessfn = access_pauth,
  6230. .fgt = FGT_APIBKEY,
  6231. .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
  6232. };
  6233. static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
  6234. {
  6235. Error *err = NULL;
  6236. uint64_t ret;
  6237. /* Success sets NZCV = 0000. */
  6238. env->NF = env->CF = env->VF = 0, env->ZF = 1;
  6239. if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
  6240. /*
  6241. * ??? Failed, for unknown reasons in the crypto subsystem.
  6242. * The best we can do is log the reason and return the
  6243. * timed-out indication to the guest. There is no reason
  6244. * we know to expect this failure to be transitory, so the
  6245. * guest may well hang retrying the operation.
  6246. */
  6247. qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
  6248. ri->name, error_get_pretty(err));
  6249. error_free(err);
  6250. env->ZF = 0; /* NZCF = 0100 */
  6251. return 0;
  6252. }
  6253. return ret;
  6254. }
  6255. /* We do not support re-seeding, so the two registers operate the same. */
  6256. static const ARMCPRegInfo rndr_reginfo[] = {
  6257. { .name = "RNDR", .state = ARM_CP_STATE_AA64,
  6258. .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
  6259. .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
  6260. .access = PL0_R, .readfn = rndr_readfn },
  6261. { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
  6262. .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
  6263. .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
  6264. .access = PL0_R, .readfn = rndr_readfn },
  6265. };
  6266. static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
  6267. uint64_t value)
  6268. {
  6269. #ifdef CONFIG_TCG
  6270. ARMCPU *cpu = env_archcpu(env);
  6271. /* CTR_EL0 System register -> DminLine, bits [19:16] */
  6272. uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
  6273. uint64_t vaddr_in = (uint64_t) value;
  6274. uint64_t vaddr = vaddr_in & ~(dline_size - 1);
  6275. void *haddr;
  6276. int mem_idx = arm_env_mmu_index(env);
  6277. /* This won't be crossing page boundaries */
  6278. haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
  6279. if (haddr) {
  6280. #ifndef CONFIG_USER_ONLY
  6281. ram_addr_t offset;
  6282. MemoryRegion *mr;
  6283. /* RCU lock is already being held */
  6284. mr = memory_region_from_host(haddr, &offset);
  6285. if (mr) {
  6286. memory_region_writeback(mr, offset, dline_size);
  6287. }
  6288. #endif /*CONFIG_USER_ONLY*/
  6289. }
  6290. #else
  6291. /* Handled by hardware accelerator. */
  6292. g_assert_not_reached();
  6293. #endif /* CONFIG_TCG */
  6294. }
  6295. static const ARMCPRegInfo dcpop_reg[] = {
  6296. { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
  6297. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
  6298. .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
  6299. .fgt = FGT_DCCVAP,
  6300. .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
  6301. };
  6302. static const ARMCPRegInfo dcpodp_reg[] = {
  6303. { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
  6304. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
  6305. .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
  6306. .fgt = FGT_DCCVADP,
  6307. .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
  6308. };
  6309. static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
  6310. bool isread)
  6311. {
  6312. if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
  6313. return CP_ACCESS_TRAP_EL2;
  6314. }
  6315. return CP_ACCESS_OK;
  6316. }
  6317. static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
  6318. bool isread)
  6319. {
  6320. int el = arm_current_el(env);
  6321. if (el < 2 && arm_is_el2_enabled(env)) {
  6322. uint64_t hcr = arm_hcr_el2_eff(env);
  6323. if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
  6324. return CP_ACCESS_TRAP_EL2;
  6325. }
  6326. }
  6327. if (el < 3 &&
  6328. arm_feature(env, ARM_FEATURE_EL3) &&
  6329. !(env->cp15.scr_el3 & SCR_ATA)) {
  6330. return CP_ACCESS_TRAP_EL3;
  6331. }
  6332. return CP_ACCESS_OK;
  6333. }
  6334. static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
  6335. bool isread)
  6336. {
  6337. CPAccessResult nv1 = access_nv1(env, ri, isread);
  6338. if (nv1 != CP_ACCESS_OK) {
  6339. return nv1;
  6340. }
  6341. return access_mte(env, ri, isread);
  6342. }
  6343. static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
  6344. bool isread)
  6345. {
  6346. /*
  6347. * TFSR_EL2: similar to generic access_mte(), but we need to
  6348. * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
  6349. * if NV2 is enabled then we will redirect this to TFSR_EL1
  6350. * after doing the HCR and SCR ATA traps; otherwise this will
  6351. * be a trap to EL2 and the HCR/SCR traps do not apply.
  6352. */
  6353. int el = arm_current_el(env);
  6354. if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
  6355. return CP_ACCESS_OK;
  6356. }
  6357. if (el < 2 && arm_is_el2_enabled(env)) {
  6358. uint64_t hcr = arm_hcr_el2_eff(env);
  6359. if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
  6360. return CP_ACCESS_TRAP_EL2;
  6361. }
  6362. }
  6363. if (el < 3 &&
  6364. arm_feature(env, ARM_FEATURE_EL3) &&
  6365. !(env->cp15.scr_el3 & SCR_ATA)) {
  6366. return CP_ACCESS_TRAP_EL3;
  6367. }
  6368. return CP_ACCESS_OK;
  6369. }
  6370. static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
  6371. {
  6372. return env->pstate & PSTATE_TCO;
  6373. }
  6374. static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
  6375. {
  6376. env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
  6377. }
  6378. static const ARMCPRegInfo mte_reginfo[] = {
  6379. { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
  6380. .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
  6381. .access = PL1_RW, .accessfn = access_mte,
  6382. .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
  6383. { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
  6384. .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
  6385. .access = PL1_RW, .accessfn = access_tfsr_el1,
  6386. .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
  6387. .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
  6388. { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
  6389. .type = ARM_CP_NV2_REDIRECT,
  6390. .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
  6391. .access = PL2_RW, .accessfn = access_tfsr_el2,
  6392. .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
  6393. { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
  6394. .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
  6395. .access = PL3_RW,
  6396. .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
  6397. { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
  6398. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
  6399. .access = PL1_RW, .accessfn = access_mte,
  6400. .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
  6401. { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
  6402. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
  6403. .access = PL1_RW, .accessfn = access_mte,
  6404. .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
  6405. { .name = "TCO", .state = ARM_CP_STATE_AA64,
  6406. .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
  6407. .type = ARM_CP_NO_RAW,
  6408. .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
  6409. { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
  6410. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
  6411. .type = ARM_CP_NOP, .access = PL1_W,
  6412. .fgt = FGT_DCIVAC,
  6413. .accessfn = aa64_cacheop_poc_access },
  6414. { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
  6415. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
  6416. .fgt = FGT_DCISW,
  6417. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6418. { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
  6419. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
  6420. .type = ARM_CP_NOP, .access = PL1_W,
  6421. .fgt = FGT_DCIVAC,
  6422. .accessfn = aa64_cacheop_poc_access },
  6423. { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
  6424. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
  6425. .fgt = FGT_DCISW,
  6426. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6427. { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
  6428. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
  6429. .fgt = FGT_DCCSW,
  6430. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6431. { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
  6432. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
  6433. .fgt = FGT_DCCSW,
  6434. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6435. { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
  6436. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
  6437. .fgt = FGT_DCCISW,
  6438. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6439. { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
  6440. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
  6441. .fgt = FGT_DCCISW,
  6442. .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
  6443. };
  6444. static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
  6445. { .name = "TCO", .state = ARM_CP_STATE_AA64,
  6446. .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
  6447. .type = ARM_CP_CONST, .access = PL0_RW, },
  6448. };
  6449. static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
  6450. { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
  6451. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
  6452. .type = ARM_CP_NOP, .access = PL0_W,
  6453. .fgt = FGT_DCCVAC,
  6454. .accessfn = aa64_cacheop_poc_access },
  6455. { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
  6456. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
  6457. .type = ARM_CP_NOP, .access = PL0_W,
  6458. .fgt = FGT_DCCVAC,
  6459. .accessfn = aa64_cacheop_poc_access },
  6460. { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
  6461. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
  6462. .type = ARM_CP_NOP, .access = PL0_W,
  6463. .fgt = FGT_DCCVAP,
  6464. .accessfn = aa64_cacheop_poc_access },
  6465. { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
  6466. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
  6467. .type = ARM_CP_NOP, .access = PL0_W,
  6468. .fgt = FGT_DCCVAP,
  6469. .accessfn = aa64_cacheop_poc_access },
  6470. { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
  6471. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
  6472. .type = ARM_CP_NOP, .access = PL0_W,
  6473. .fgt = FGT_DCCVADP,
  6474. .accessfn = aa64_cacheop_poc_access },
  6475. { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
  6476. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
  6477. .type = ARM_CP_NOP, .access = PL0_W,
  6478. .fgt = FGT_DCCVADP,
  6479. .accessfn = aa64_cacheop_poc_access },
  6480. { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
  6481. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
  6482. .type = ARM_CP_NOP, .access = PL0_W,
  6483. .fgt = FGT_DCCIVAC,
  6484. .accessfn = aa64_cacheop_poc_access },
  6485. { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
  6486. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
  6487. .type = ARM_CP_NOP, .access = PL0_W,
  6488. .fgt = FGT_DCCIVAC,
  6489. .accessfn = aa64_cacheop_poc_access },
  6490. { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
  6491. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
  6492. .access = PL0_W, .type = ARM_CP_DC_GVA,
  6493. #ifndef CONFIG_USER_ONLY
  6494. /* Avoid overhead of an access check that always passes in user-mode */
  6495. .accessfn = aa64_zva_access,
  6496. .fgt = FGT_DCZVA,
  6497. #endif
  6498. },
  6499. { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
  6500. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
  6501. .access = PL0_W, .type = ARM_CP_DC_GZVA,
  6502. #ifndef CONFIG_USER_ONLY
  6503. /* Avoid overhead of an access check that always passes in user-mode */
  6504. .accessfn = aa64_zva_access,
  6505. .fgt = FGT_DCZVA,
  6506. #endif
  6507. },
  6508. };
  6509. static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
  6510. bool isread)
  6511. {
  6512. uint64_t hcr = arm_hcr_el2_eff(env);
  6513. int el = arm_current_el(env);
  6514. if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
  6515. if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
  6516. if (hcr & HCR_TGE) {
  6517. return CP_ACCESS_TRAP_EL2;
  6518. }
  6519. return CP_ACCESS_TRAP_EL1;
  6520. }
  6521. } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
  6522. return CP_ACCESS_TRAP_EL2;
  6523. }
  6524. if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
  6525. return CP_ACCESS_TRAP_EL2;
  6526. }
  6527. if (el < 3
  6528. && arm_feature(env, ARM_FEATURE_EL3)
  6529. && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
  6530. return CP_ACCESS_TRAP_EL3;
  6531. }
  6532. return CP_ACCESS_OK;
  6533. }
  6534. static CPAccessResult access_scxtnum_el1(CPUARMState *env,
  6535. const ARMCPRegInfo *ri,
  6536. bool isread)
  6537. {
  6538. CPAccessResult nv1 = access_nv1(env, ri, isread);
  6539. if (nv1 != CP_ACCESS_OK) {
  6540. return nv1;
  6541. }
  6542. return access_scxtnum(env, ri, isread);
  6543. }
  6544. static const ARMCPRegInfo scxtnum_reginfo[] = {
  6545. { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
  6546. .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
  6547. .access = PL0_RW, .accessfn = access_scxtnum,
  6548. .fgt = FGT_SCXTNUM_EL0,
  6549. .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
  6550. { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
  6551. .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
  6552. .access = PL1_RW, .accessfn = access_scxtnum_el1,
  6553. .fgt = FGT_SCXTNUM_EL1,
  6554. .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
  6555. .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
  6556. { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
  6557. .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
  6558. .access = PL2_RW, .accessfn = access_scxtnum,
  6559. .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
  6560. { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
  6561. .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
  6562. .access = PL3_RW,
  6563. .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
  6564. };
  6565. static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
  6566. bool isread)
  6567. {
  6568. if (arm_current_el(env) == 2 &&
  6569. arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
  6570. return CP_ACCESS_TRAP_EL3;
  6571. }
  6572. return CP_ACCESS_OK;
  6573. }
  6574. static const ARMCPRegInfo fgt_reginfo[] = {
  6575. { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
  6576. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
  6577. .nv2_redirect_offset = 0x1b8,
  6578. .access = PL2_RW, .accessfn = access_fgt,
  6579. .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
  6580. { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
  6581. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
  6582. .nv2_redirect_offset = 0x1c0,
  6583. .access = PL2_RW, .accessfn = access_fgt,
  6584. .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
  6585. { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
  6586. .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
  6587. .nv2_redirect_offset = 0x1d0,
  6588. .access = PL2_RW, .accessfn = access_fgt,
  6589. .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
  6590. { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
  6591. .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
  6592. .nv2_redirect_offset = 0x1d8,
  6593. .access = PL2_RW, .accessfn = access_fgt,
  6594. .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
  6595. { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
  6596. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
  6597. .nv2_redirect_offset = 0x1c8,
  6598. .access = PL2_RW, .accessfn = access_fgt,
  6599. .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
  6600. };
  6601. static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  6602. uint64_t value)
  6603. {
  6604. /*
  6605. * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
  6606. * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
  6607. * about the RESS bits at the top -- we choose the "generate an EL2
  6608. * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
  6609. * the ptw.c code detect the resulting invalid address).
  6610. */
  6611. env->cp15.vncr_el2 = value & ~0xfffULL;
  6612. }
  6613. static const ARMCPRegInfo nv2_reginfo[] = {
  6614. { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
  6615. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
  6616. .access = PL2_RW,
  6617. .writefn = vncr_write,
  6618. .nv2_redirect_offset = 0xb0,
  6619. .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
  6620. };
  6621. #endif /* TARGET_AARCH64 */
  6622. static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
  6623. bool isread)
  6624. {
  6625. int el = arm_current_el(env);
  6626. if (el == 0) {
  6627. uint64_t sctlr = arm_sctlr(env, el);
  6628. if (!(sctlr & SCTLR_EnRCTX)) {
  6629. return CP_ACCESS_TRAP_EL1;
  6630. }
  6631. } else if (el == 1) {
  6632. uint64_t hcr = arm_hcr_el2_eff(env);
  6633. if (hcr & HCR_NV) {
  6634. return CP_ACCESS_TRAP_EL2;
  6635. }
  6636. }
  6637. return CP_ACCESS_OK;
  6638. }
  6639. static const ARMCPRegInfo predinv_reginfo[] = {
  6640. { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
  6641. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
  6642. .fgt = FGT_CFPRCTX,
  6643. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6644. { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
  6645. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
  6646. .fgt = FGT_DVPRCTX,
  6647. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6648. { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
  6649. .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
  6650. .fgt = FGT_CPPRCTX,
  6651. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6652. /*
  6653. * Note the AArch32 opcodes have a different OPC1.
  6654. */
  6655. { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
  6656. .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
  6657. .fgt = FGT_CFPRCTX,
  6658. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6659. { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
  6660. .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
  6661. .fgt = FGT_DVPRCTX,
  6662. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6663. { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
  6664. .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
  6665. .fgt = FGT_CPPRCTX,
  6666. .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
  6667. };
  6668. static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
  6669. {
  6670. /* Read the high 32 bits of the current CCSIDR */
  6671. return extract64(ccsidr_read(env, ri), 32, 32);
  6672. }
  6673. static const ARMCPRegInfo ccsidr2_reginfo[] = {
  6674. { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
  6675. .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
  6676. .access = PL1_R,
  6677. .accessfn = access_tid4,
  6678. .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
  6679. };
  6680. static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
  6681. bool isread)
  6682. {
  6683. if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
  6684. return CP_ACCESS_TRAP_EL2;
  6685. }
  6686. return CP_ACCESS_OK;
  6687. }
  6688. static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
  6689. bool isread)
  6690. {
  6691. if (arm_feature(env, ARM_FEATURE_V8)) {
  6692. return access_aa64_tid3(env, ri, isread);
  6693. }
  6694. return CP_ACCESS_OK;
  6695. }
  6696. static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
  6697. bool isread)
  6698. {
  6699. if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
  6700. return CP_ACCESS_TRAP_EL2;
  6701. }
  6702. return CP_ACCESS_OK;
  6703. }
  6704. static CPAccessResult access_joscr_jmcr(CPUARMState *env,
  6705. const ARMCPRegInfo *ri, bool isread)
  6706. {
  6707. /*
  6708. * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
  6709. * in v7A, not in v8A.
  6710. */
  6711. if (!arm_feature(env, ARM_FEATURE_V8) &&
  6712. arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
  6713. (env->cp15.hstr_el2 & HSTR_TJDBX)) {
  6714. return CP_ACCESS_TRAP_EL2;
  6715. }
  6716. return CP_ACCESS_OK;
  6717. }
  6718. static const ARMCPRegInfo jazelle_regs[] = {
  6719. { .name = "JIDR",
  6720. .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
  6721. .access = PL1_R, .accessfn = access_jazelle,
  6722. .type = ARM_CP_CONST, .resetvalue = 0 },
  6723. { .name = "JOSCR",
  6724. .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
  6725. .accessfn = access_joscr_jmcr,
  6726. .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
  6727. { .name = "JMCR",
  6728. .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
  6729. .accessfn = access_joscr_jmcr,
  6730. .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
  6731. };
  6732. static const ARMCPRegInfo contextidr_el2 = {
  6733. .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
  6734. .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
  6735. .access = PL2_RW,
  6736. .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
  6737. };
  6738. static const ARMCPRegInfo vhe_reginfo[] = {
  6739. { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
  6740. .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
  6741. .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
  6742. .raw_writefn = raw_write,
  6743. .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
  6744. #ifndef CONFIG_USER_ONLY
  6745. { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
  6746. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
  6747. .fieldoffset =
  6748. offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
  6749. .type = ARM_CP_IO, .access = PL2_RW,
  6750. .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
  6751. { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
  6752. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
  6753. .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
  6754. .resetfn = gt_hv_timer_reset,
  6755. .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
  6756. { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
  6757. .type = ARM_CP_IO,
  6758. .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
  6759. .access = PL2_RW,
  6760. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
  6761. .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
  6762. { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
  6763. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
  6764. .type = ARM_CP_IO | ARM_CP_ALIAS,
  6765. .access = PL2_RW, .accessfn = access_el1nvpct,
  6766. .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
  6767. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
  6768. .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
  6769. { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
  6770. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
  6771. .type = ARM_CP_IO | ARM_CP_ALIAS,
  6772. .access = PL2_RW, .accessfn = access_el1nvvct,
  6773. .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
  6774. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
  6775. .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
  6776. { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
  6777. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
  6778. .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
  6779. .access = PL2_RW, .accessfn = e2h_access,
  6780. .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
  6781. { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
  6782. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
  6783. .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
  6784. .access = PL2_RW, .accessfn = e2h_access,
  6785. .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
  6786. { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
  6787. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
  6788. .type = ARM_CP_IO | ARM_CP_ALIAS,
  6789. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
  6790. .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
  6791. .access = PL2_RW, .accessfn = access_el1nvpct,
  6792. .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
  6793. { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
  6794. .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
  6795. .type = ARM_CP_IO | ARM_CP_ALIAS,
  6796. .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
  6797. .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
  6798. .access = PL2_RW, .accessfn = access_el1nvvct,
  6799. .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
  6800. #endif
  6801. };
  6802. #ifndef CONFIG_USER_ONLY
  6803. static const ARMCPRegInfo ats1e1_reginfo[] = {
  6804. { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
  6805. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
  6806. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  6807. .fgt = FGT_ATS1E1RP,
  6808. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  6809. { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
  6810. .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
  6811. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  6812. .fgt = FGT_ATS1E1WP,
  6813. .accessfn = at_s1e01_access, .writefn = ats_write64 },
  6814. };
  6815. static const ARMCPRegInfo ats1cp_reginfo[] = {
  6816. { .name = "ATS1CPRP",
  6817. .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
  6818. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  6819. .writefn = ats_write },
  6820. { .name = "ATS1CPWP",
  6821. .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
  6822. .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
  6823. .writefn = ats_write },
  6824. };
  6825. #endif
  6826. /*
  6827. * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
  6828. * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
  6829. * is non-zero, which is never for ARMv7, optionally in ARMv8
  6830. * and mandatorily for ARMv8.2 and up.
  6831. * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
  6832. * implementation is RAZ/WI we can ignore this detail, as we
  6833. * do for ACTLR.
  6834. */
  6835. static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
  6836. { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
  6837. .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
  6838. .access = PL1_RW, .accessfn = access_tacr,
  6839. .type = ARM_CP_CONST, .resetvalue = 0 },
  6840. { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
  6841. .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
  6842. .access = PL2_RW, .type = ARM_CP_CONST,
  6843. .resetvalue = 0 },
  6844. };
  6845. void register_cp_regs_for_features(ARMCPU *cpu)
  6846. {
  6847. /* Register all the coprocessor registers based on feature bits */
  6848. CPUARMState *env = &cpu->env;
  6849. if (arm_feature(env, ARM_FEATURE_M)) {
  6850. /* M profile has no coprocessor registers */
  6851. return;
  6852. }
  6853. define_arm_cp_regs(cpu, cp_reginfo);
  6854. if (!arm_feature(env, ARM_FEATURE_V8)) {
  6855. /*
  6856. * Must go early as it is full of wildcards that may be
  6857. * overridden by later definitions.
  6858. */
  6859. define_arm_cp_regs(cpu, not_v8_cp_reginfo);
  6860. }
  6861. define_tlb_insn_regs(cpu);
  6862. if (arm_feature(env, ARM_FEATURE_V6)) {
  6863. /* The ID registers all have impdef reset values */
  6864. ARMCPRegInfo v6_idregs[] = {
  6865. { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
  6866. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
  6867. .access = PL1_R, .type = ARM_CP_CONST,
  6868. .accessfn = access_aa32_tid3,
  6869. .resetvalue = cpu->isar.id_pfr0 },
  6870. /*
  6871. * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
  6872. * the value of the GIC field until after we define these regs.
  6873. */
  6874. { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
  6875. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
  6876. .access = PL1_R, .type = ARM_CP_NO_RAW,
  6877. .accessfn = access_aa32_tid3,
  6878. #ifdef CONFIG_USER_ONLY
  6879. .type = ARM_CP_CONST,
  6880. .resetvalue = cpu->isar.id_pfr1,
  6881. #else
  6882. .type = ARM_CP_NO_RAW,
  6883. .accessfn = access_aa32_tid3,
  6884. .readfn = id_pfr1_read,
  6885. .writefn = arm_cp_write_ignore
  6886. #endif
  6887. },
  6888. { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
  6889. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
  6890. .access = PL1_R, .type = ARM_CP_CONST,
  6891. .accessfn = access_aa32_tid3,
  6892. .resetvalue = cpu->isar.id_dfr0 },
  6893. { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
  6894. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
  6895. .access = PL1_R, .type = ARM_CP_CONST,
  6896. .accessfn = access_aa32_tid3,
  6897. .resetvalue = cpu->id_afr0 },
  6898. { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
  6899. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
  6900. .access = PL1_R, .type = ARM_CP_CONST,
  6901. .accessfn = access_aa32_tid3,
  6902. .resetvalue = cpu->isar.id_mmfr0 },
  6903. { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
  6904. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
  6905. .access = PL1_R, .type = ARM_CP_CONST,
  6906. .accessfn = access_aa32_tid3,
  6907. .resetvalue = cpu->isar.id_mmfr1 },
  6908. { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
  6909. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
  6910. .access = PL1_R, .type = ARM_CP_CONST,
  6911. .accessfn = access_aa32_tid3,
  6912. .resetvalue = cpu->isar.id_mmfr2 },
  6913. { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
  6914. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
  6915. .access = PL1_R, .type = ARM_CP_CONST,
  6916. .accessfn = access_aa32_tid3,
  6917. .resetvalue = cpu->isar.id_mmfr3 },
  6918. { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
  6919. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
  6920. .access = PL1_R, .type = ARM_CP_CONST,
  6921. .accessfn = access_aa32_tid3,
  6922. .resetvalue = cpu->isar.id_isar0 },
  6923. { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
  6924. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
  6925. .access = PL1_R, .type = ARM_CP_CONST,
  6926. .accessfn = access_aa32_tid3,
  6927. .resetvalue = cpu->isar.id_isar1 },
  6928. { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
  6929. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
  6930. .access = PL1_R, .type = ARM_CP_CONST,
  6931. .accessfn = access_aa32_tid3,
  6932. .resetvalue = cpu->isar.id_isar2 },
  6933. { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
  6934. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
  6935. .access = PL1_R, .type = ARM_CP_CONST,
  6936. .accessfn = access_aa32_tid3,
  6937. .resetvalue = cpu->isar.id_isar3 },
  6938. { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
  6939. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
  6940. .access = PL1_R, .type = ARM_CP_CONST,
  6941. .accessfn = access_aa32_tid3,
  6942. .resetvalue = cpu->isar.id_isar4 },
  6943. { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
  6944. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
  6945. .access = PL1_R, .type = ARM_CP_CONST,
  6946. .accessfn = access_aa32_tid3,
  6947. .resetvalue = cpu->isar.id_isar5 },
  6948. { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
  6949. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
  6950. .access = PL1_R, .type = ARM_CP_CONST,
  6951. .accessfn = access_aa32_tid3,
  6952. .resetvalue = cpu->isar.id_mmfr4 },
  6953. { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
  6954. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
  6955. .access = PL1_R, .type = ARM_CP_CONST,
  6956. .accessfn = access_aa32_tid3,
  6957. .resetvalue = cpu->isar.id_isar6 },
  6958. };
  6959. define_arm_cp_regs(cpu, v6_idregs);
  6960. define_arm_cp_regs(cpu, v6_cp_reginfo);
  6961. } else {
  6962. define_arm_cp_regs(cpu, not_v6_cp_reginfo);
  6963. }
  6964. if (arm_feature(env, ARM_FEATURE_V6K)) {
  6965. define_arm_cp_regs(cpu, v6k_cp_reginfo);
  6966. }
  6967. if (arm_feature(env, ARM_FEATURE_V7VE)) {
  6968. define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
  6969. }
  6970. if (arm_feature(env, ARM_FEATURE_V7)) {
  6971. ARMCPRegInfo clidr = {
  6972. .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
  6973. .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
  6974. .access = PL1_R, .type = ARM_CP_CONST,
  6975. .accessfn = access_tid4,
  6976. .fgt = FGT_CLIDR_EL1,
  6977. .resetvalue = cpu->clidr
  6978. };
  6979. define_one_arm_cp_reg(cpu, &clidr);
  6980. define_arm_cp_regs(cpu, v7_cp_reginfo);
  6981. define_debug_regs(cpu);
  6982. define_pmu_regs(cpu);
  6983. } else {
  6984. define_arm_cp_regs(cpu, not_v7_cp_reginfo);
  6985. }
  6986. if (arm_feature(env, ARM_FEATURE_V8)) {
  6987. /*
  6988. * v8 ID registers, which all have impdef reset values.
  6989. * Note that within the ID register ranges the unused slots
  6990. * must all RAZ, not UNDEF; future architecture versions may
  6991. * define new registers here.
  6992. * ID registers which are AArch64 views of the AArch32 ID registers
  6993. * which already existed in v6 and v7 are handled elsewhere,
  6994. * in v6_idregs[].
  6995. */
  6996. int i;
  6997. ARMCPRegInfo v8_idregs[] = {
  6998. /*
  6999. * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
  7000. * emulation because we don't know the right value for the
  7001. * GIC field until after we define these regs.
  7002. */
  7003. { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
  7004. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
  7005. .access = PL1_R,
  7006. #ifdef CONFIG_USER_ONLY
  7007. .type = ARM_CP_CONST,
  7008. .resetvalue = cpu->isar.id_aa64pfr0
  7009. #else
  7010. .type = ARM_CP_NO_RAW,
  7011. .accessfn = access_aa64_tid3,
  7012. .readfn = id_aa64pfr0_read,
  7013. .writefn = arm_cp_write_ignore
  7014. #endif
  7015. },
  7016. { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
  7017. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
  7018. .access = PL1_R, .type = ARM_CP_CONST,
  7019. .accessfn = access_aa64_tid3,
  7020. .resetvalue = cpu->isar.id_aa64pfr1},
  7021. { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7022. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
  7023. .access = PL1_R, .type = ARM_CP_CONST,
  7024. .accessfn = access_aa64_tid3,
  7025. .resetvalue = 0 },
  7026. { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7027. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
  7028. .access = PL1_R, .type = ARM_CP_CONST,
  7029. .accessfn = access_aa64_tid3,
  7030. .resetvalue = 0 },
  7031. { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
  7032. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
  7033. .access = PL1_R, .type = ARM_CP_CONST,
  7034. .accessfn = access_aa64_tid3,
  7035. .resetvalue = cpu->isar.id_aa64zfr0 },
  7036. { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
  7037. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
  7038. .access = PL1_R, .type = ARM_CP_CONST,
  7039. .accessfn = access_aa64_tid3,
  7040. .resetvalue = cpu->isar.id_aa64smfr0 },
  7041. { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7042. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
  7043. .access = PL1_R, .type = ARM_CP_CONST,
  7044. .accessfn = access_aa64_tid3,
  7045. .resetvalue = 0 },
  7046. { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7047. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
  7048. .access = PL1_R, .type = ARM_CP_CONST,
  7049. .accessfn = access_aa64_tid3,
  7050. .resetvalue = 0 },
  7051. { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
  7052. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
  7053. .access = PL1_R, .type = ARM_CP_CONST,
  7054. .accessfn = access_aa64_tid3,
  7055. .resetvalue = cpu->isar.id_aa64dfr0 },
  7056. { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
  7057. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
  7058. .access = PL1_R, .type = ARM_CP_CONST,
  7059. .accessfn = access_aa64_tid3,
  7060. .resetvalue = cpu->isar.id_aa64dfr1 },
  7061. { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7062. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
  7063. .access = PL1_R, .type = ARM_CP_CONST,
  7064. .accessfn = access_aa64_tid3,
  7065. .resetvalue = 0 },
  7066. { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7067. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
  7068. .access = PL1_R, .type = ARM_CP_CONST,
  7069. .accessfn = access_aa64_tid3,
  7070. .resetvalue = 0 },
  7071. { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
  7072. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
  7073. .access = PL1_R, .type = ARM_CP_CONST,
  7074. .accessfn = access_aa64_tid3,
  7075. .resetvalue = cpu->id_aa64afr0 },
  7076. { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
  7077. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
  7078. .access = PL1_R, .type = ARM_CP_CONST,
  7079. .accessfn = access_aa64_tid3,
  7080. .resetvalue = cpu->id_aa64afr1 },
  7081. { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7082. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
  7083. .access = PL1_R, .type = ARM_CP_CONST,
  7084. .accessfn = access_aa64_tid3,
  7085. .resetvalue = 0 },
  7086. { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7087. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
  7088. .access = PL1_R, .type = ARM_CP_CONST,
  7089. .accessfn = access_aa64_tid3,
  7090. .resetvalue = 0 },
  7091. { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
  7092. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
  7093. .access = PL1_R, .type = ARM_CP_CONST,
  7094. .accessfn = access_aa64_tid3,
  7095. .resetvalue = cpu->isar.id_aa64isar0 },
  7096. { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
  7097. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
  7098. .access = PL1_R, .type = ARM_CP_CONST,
  7099. .accessfn = access_aa64_tid3,
  7100. .resetvalue = cpu->isar.id_aa64isar1 },
  7101. { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
  7102. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
  7103. .access = PL1_R, .type = ARM_CP_CONST,
  7104. .accessfn = access_aa64_tid3,
  7105. .resetvalue = cpu->isar.id_aa64isar2 },
  7106. { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7107. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
  7108. .access = PL1_R, .type = ARM_CP_CONST,
  7109. .accessfn = access_aa64_tid3,
  7110. .resetvalue = 0 },
  7111. { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7112. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
  7113. .access = PL1_R, .type = ARM_CP_CONST,
  7114. .accessfn = access_aa64_tid3,
  7115. .resetvalue = 0 },
  7116. { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7117. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
  7118. .access = PL1_R, .type = ARM_CP_CONST,
  7119. .accessfn = access_aa64_tid3,
  7120. .resetvalue = 0 },
  7121. { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7122. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
  7123. .access = PL1_R, .type = ARM_CP_CONST,
  7124. .accessfn = access_aa64_tid3,
  7125. .resetvalue = 0 },
  7126. { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7127. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
  7128. .access = PL1_R, .type = ARM_CP_CONST,
  7129. .accessfn = access_aa64_tid3,
  7130. .resetvalue = 0 },
  7131. { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
  7132. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
  7133. .access = PL1_R, .type = ARM_CP_CONST,
  7134. .accessfn = access_aa64_tid3,
  7135. .resetvalue = cpu->isar.id_aa64mmfr0 },
  7136. { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
  7137. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
  7138. .access = PL1_R, .type = ARM_CP_CONST,
  7139. .accessfn = access_aa64_tid3,
  7140. .resetvalue = cpu->isar.id_aa64mmfr1 },
  7141. { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
  7142. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
  7143. .access = PL1_R, .type = ARM_CP_CONST,
  7144. .accessfn = access_aa64_tid3,
  7145. .resetvalue = cpu->isar.id_aa64mmfr2 },
  7146. { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
  7147. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
  7148. .access = PL1_R, .type = ARM_CP_CONST,
  7149. .accessfn = access_aa64_tid3,
  7150. .resetvalue = cpu->isar.id_aa64mmfr3 },
  7151. { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7152. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
  7153. .access = PL1_R, .type = ARM_CP_CONST,
  7154. .accessfn = access_aa64_tid3,
  7155. .resetvalue = 0 },
  7156. { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7157. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
  7158. .access = PL1_R, .type = ARM_CP_CONST,
  7159. .accessfn = access_aa64_tid3,
  7160. .resetvalue = 0 },
  7161. { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7162. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
  7163. .access = PL1_R, .type = ARM_CP_CONST,
  7164. .accessfn = access_aa64_tid3,
  7165. .resetvalue = 0 },
  7166. { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
  7167. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
  7168. .access = PL1_R, .type = ARM_CP_CONST,
  7169. .accessfn = access_aa64_tid3,
  7170. .resetvalue = 0 },
  7171. { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
  7172. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
  7173. .access = PL1_R, .type = ARM_CP_CONST,
  7174. .accessfn = access_aa64_tid3,
  7175. .resetvalue = cpu->isar.mvfr0 },
  7176. { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
  7177. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
  7178. .access = PL1_R, .type = ARM_CP_CONST,
  7179. .accessfn = access_aa64_tid3,
  7180. .resetvalue = cpu->isar.mvfr1 },
  7181. { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
  7182. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
  7183. .access = PL1_R, .type = ARM_CP_CONST,
  7184. .accessfn = access_aa64_tid3,
  7185. .resetvalue = cpu->isar.mvfr2 },
  7186. /*
  7187. * "0, c0, c3, {0,1,2}" are the encodings corresponding to
  7188. * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
  7189. * as RAZ, since it is in the "reserved for future ID
  7190. * registers, RAZ" part of the AArch32 encoding space.
  7191. */
  7192. { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
  7193. .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
  7194. .access = PL1_R, .type = ARM_CP_CONST,
  7195. .accessfn = access_aa64_tid3,
  7196. .resetvalue = 0 },
  7197. { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
  7198. .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
  7199. .access = PL1_R, .type = ARM_CP_CONST,
  7200. .accessfn = access_aa64_tid3,
  7201. .resetvalue = 0 },
  7202. { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
  7203. .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
  7204. .access = PL1_R, .type = ARM_CP_CONST,
  7205. .accessfn = access_aa64_tid3,
  7206. .resetvalue = 0 },
  7207. /*
  7208. * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
  7209. * they're also RAZ for AArch64, and in v8 are gradually
  7210. * being filled with AArch64-view-of-AArch32-ID-register
  7211. * for new ID registers.
  7212. */
  7213. { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
  7214. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
  7215. .access = PL1_R, .type = ARM_CP_CONST,
  7216. .accessfn = access_aa64_tid3,
  7217. .resetvalue = 0 },
  7218. { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
  7219. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
  7220. .access = PL1_R, .type = ARM_CP_CONST,
  7221. .accessfn = access_aa64_tid3,
  7222. .resetvalue = cpu->isar.id_pfr2 },
  7223. { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
  7224. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
  7225. .access = PL1_R, .type = ARM_CP_CONST,
  7226. .accessfn = access_aa64_tid3,
  7227. .resetvalue = cpu->isar.id_dfr1 },
  7228. { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
  7229. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
  7230. .access = PL1_R, .type = ARM_CP_CONST,
  7231. .accessfn = access_aa64_tid3,
  7232. .resetvalue = cpu->isar.id_mmfr5 },
  7233. { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
  7234. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
  7235. .access = PL1_R, .type = ARM_CP_CONST,
  7236. .accessfn = access_aa64_tid3,
  7237. .resetvalue = 0 },
  7238. { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
  7239. .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
  7240. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  7241. .fgt = FGT_PMCEIDN_EL0,
  7242. .resetvalue = extract64(cpu->pmceid0, 0, 32) },
  7243. { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
  7244. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
  7245. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  7246. .fgt = FGT_PMCEIDN_EL0,
  7247. .resetvalue = cpu->pmceid0 },
  7248. { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
  7249. .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
  7250. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  7251. .fgt = FGT_PMCEIDN_EL0,
  7252. .resetvalue = extract64(cpu->pmceid1, 0, 32) },
  7253. { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
  7254. .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
  7255. .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
  7256. .fgt = FGT_PMCEIDN_EL0,
  7257. .resetvalue = cpu->pmceid1 },
  7258. };
  7259. #ifdef CONFIG_USER_ONLY
  7260. static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
  7261. { .name = "ID_AA64PFR0_EL1",
  7262. .exported_bits = R_ID_AA64PFR0_FP_MASK |
  7263. R_ID_AA64PFR0_ADVSIMD_MASK |
  7264. R_ID_AA64PFR0_SVE_MASK |
  7265. R_ID_AA64PFR0_DIT_MASK,
  7266. .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
  7267. (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
  7268. { .name = "ID_AA64PFR1_EL1",
  7269. .exported_bits = R_ID_AA64PFR1_BT_MASK |
  7270. R_ID_AA64PFR1_SSBS_MASK |
  7271. R_ID_AA64PFR1_MTE_MASK |
  7272. R_ID_AA64PFR1_SME_MASK },
  7273. { .name = "ID_AA64PFR*_EL1_RESERVED",
  7274. .is_glob = true },
  7275. { .name = "ID_AA64ZFR0_EL1",
  7276. .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
  7277. R_ID_AA64ZFR0_AES_MASK |
  7278. R_ID_AA64ZFR0_BITPERM_MASK |
  7279. R_ID_AA64ZFR0_BFLOAT16_MASK |
  7280. R_ID_AA64ZFR0_B16B16_MASK |
  7281. R_ID_AA64ZFR0_SHA3_MASK |
  7282. R_ID_AA64ZFR0_SM4_MASK |
  7283. R_ID_AA64ZFR0_I8MM_MASK |
  7284. R_ID_AA64ZFR0_F32MM_MASK |
  7285. R_ID_AA64ZFR0_F64MM_MASK },
  7286. { .name = "ID_AA64SMFR0_EL1",
  7287. .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
  7288. R_ID_AA64SMFR0_BI32I32_MASK |
  7289. R_ID_AA64SMFR0_B16F32_MASK |
  7290. R_ID_AA64SMFR0_F16F32_MASK |
  7291. R_ID_AA64SMFR0_I8I32_MASK |
  7292. R_ID_AA64SMFR0_F16F16_MASK |
  7293. R_ID_AA64SMFR0_B16B16_MASK |
  7294. R_ID_AA64SMFR0_I16I32_MASK |
  7295. R_ID_AA64SMFR0_F64F64_MASK |
  7296. R_ID_AA64SMFR0_I16I64_MASK |
  7297. R_ID_AA64SMFR0_SMEVER_MASK |
  7298. R_ID_AA64SMFR0_FA64_MASK },
  7299. { .name = "ID_AA64MMFR0_EL1",
  7300. .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
  7301. .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
  7302. (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
  7303. { .name = "ID_AA64MMFR1_EL1",
  7304. .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
  7305. { .name = "ID_AA64MMFR2_EL1",
  7306. .exported_bits = R_ID_AA64MMFR2_AT_MASK },
  7307. { .name = "ID_AA64MMFR3_EL1",
  7308. .exported_bits = 0 },
  7309. { .name = "ID_AA64MMFR*_EL1_RESERVED",
  7310. .is_glob = true },
  7311. { .name = "ID_AA64DFR0_EL1",
  7312. .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
  7313. { .name = "ID_AA64DFR1_EL1" },
  7314. { .name = "ID_AA64DFR*_EL1_RESERVED",
  7315. .is_glob = true },
  7316. { .name = "ID_AA64AFR*",
  7317. .is_glob = true },
  7318. { .name = "ID_AA64ISAR0_EL1",
  7319. .exported_bits = R_ID_AA64ISAR0_AES_MASK |
  7320. R_ID_AA64ISAR0_SHA1_MASK |
  7321. R_ID_AA64ISAR0_SHA2_MASK |
  7322. R_ID_AA64ISAR0_CRC32_MASK |
  7323. R_ID_AA64ISAR0_ATOMIC_MASK |
  7324. R_ID_AA64ISAR0_RDM_MASK |
  7325. R_ID_AA64ISAR0_SHA3_MASK |
  7326. R_ID_AA64ISAR0_SM3_MASK |
  7327. R_ID_AA64ISAR0_SM4_MASK |
  7328. R_ID_AA64ISAR0_DP_MASK |
  7329. R_ID_AA64ISAR0_FHM_MASK |
  7330. R_ID_AA64ISAR0_TS_MASK |
  7331. R_ID_AA64ISAR0_RNDR_MASK },
  7332. { .name = "ID_AA64ISAR1_EL1",
  7333. .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
  7334. R_ID_AA64ISAR1_APA_MASK |
  7335. R_ID_AA64ISAR1_API_MASK |
  7336. R_ID_AA64ISAR1_JSCVT_MASK |
  7337. R_ID_AA64ISAR1_FCMA_MASK |
  7338. R_ID_AA64ISAR1_LRCPC_MASK |
  7339. R_ID_AA64ISAR1_GPA_MASK |
  7340. R_ID_AA64ISAR1_GPI_MASK |
  7341. R_ID_AA64ISAR1_FRINTTS_MASK |
  7342. R_ID_AA64ISAR1_SB_MASK |
  7343. R_ID_AA64ISAR1_BF16_MASK |
  7344. R_ID_AA64ISAR1_DGH_MASK |
  7345. R_ID_AA64ISAR1_I8MM_MASK },
  7346. { .name = "ID_AA64ISAR2_EL1",
  7347. .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
  7348. R_ID_AA64ISAR2_RPRES_MASK |
  7349. R_ID_AA64ISAR2_GPA3_MASK |
  7350. R_ID_AA64ISAR2_APA3_MASK |
  7351. R_ID_AA64ISAR2_MOPS_MASK |
  7352. R_ID_AA64ISAR2_BC_MASK |
  7353. R_ID_AA64ISAR2_RPRFM_MASK |
  7354. R_ID_AA64ISAR2_CSSC_MASK },
  7355. { .name = "ID_AA64ISAR*_EL1_RESERVED",
  7356. .is_glob = true },
  7357. };
  7358. modify_arm_cp_regs(v8_idregs, v8_user_idregs);
  7359. #endif
  7360. /*
  7361. * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
  7362. * TODO: For RMR, a write with bit 1 set should do something with
  7363. * cpu_reset(). In the meantime, "the bit is strictly a request",
  7364. * so we are in spec just ignoring writes.
  7365. */
  7366. if (!arm_feature(env, ARM_FEATURE_EL3) &&
  7367. !arm_feature(env, ARM_FEATURE_EL2)) {
  7368. ARMCPRegInfo el1_reset_regs[] = {
  7369. { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
  7370. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
  7371. .access = PL1_R,
  7372. .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
  7373. { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
  7374. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
  7375. .access = PL1_RW, .type = ARM_CP_CONST,
  7376. .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
  7377. };
  7378. define_arm_cp_regs(cpu, el1_reset_regs);
  7379. }
  7380. define_arm_cp_regs(cpu, v8_idregs);
  7381. define_arm_cp_regs(cpu, v8_cp_reginfo);
  7382. if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
  7383. define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
  7384. }
  7385. for (i = 4; i < 16; i++) {
  7386. /*
  7387. * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
  7388. * For pre-v8 cores there are RAZ patterns for these in
  7389. * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
  7390. * v8 extends the "must RAZ" part of the ID register space
  7391. * to also cover c0, 0, c{8-15}, {0-7}.
  7392. * These are STATE_AA32 because in the AArch64 sysreg space
  7393. * c4-c7 is where the AArch64 ID registers live (and we've
  7394. * already defined those in v8_idregs[]), and c8-c15 are not
  7395. * "must RAZ" for AArch64.
  7396. */
  7397. g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
  7398. ARMCPRegInfo v8_aa32_raz_idregs = {
  7399. .name = name,
  7400. .state = ARM_CP_STATE_AA32,
  7401. .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
  7402. .access = PL1_R, .type = ARM_CP_CONST,
  7403. .accessfn = access_aa64_tid3,
  7404. .resetvalue = 0 };
  7405. define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
  7406. }
  7407. }
  7408. /*
  7409. * Register the base EL2 cpregs.
  7410. * Pre v8, these registers are implemented only as part of the
  7411. * Virtualization Extensions (EL2 present). Beginning with v8,
  7412. * if EL2 is missing but EL3 is enabled, mostly these become
  7413. * RES0 from EL3, with some specific exceptions.
  7414. */
  7415. if (arm_feature(env, ARM_FEATURE_EL2)
  7416. || (arm_feature(env, ARM_FEATURE_EL3)
  7417. && arm_feature(env, ARM_FEATURE_V8))) {
  7418. uint64_t vmpidr_def = mpidr_read_val(env);
  7419. ARMCPRegInfo vpidr_regs[] = {
  7420. { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
  7421. .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
  7422. .access = PL2_RW, .accessfn = access_el3_aa32ns,
  7423. .resetvalue = cpu->midr,
  7424. .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
  7425. .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
  7426. { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
  7427. .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
  7428. .access = PL2_RW, .resetvalue = cpu->midr,
  7429. .type = ARM_CP_EL3_NO_EL2_C_NZ,
  7430. .nv2_redirect_offset = 0x88,
  7431. .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
  7432. { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
  7433. .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
  7434. .access = PL2_RW, .accessfn = access_el3_aa32ns,
  7435. .resetvalue = vmpidr_def,
  7436. .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
  7437. .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
  7438. { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
  7439. .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
  7440. .access = PL2_RW, .resetvalue = vmpidr_def,
  7441. .type = ARM_CP_EL3_NO_EL2_C_NZ,
  7442. .nv2_redirect_offset = 0x50,
  7443. .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
  7444. };
  7445. /*
  7446. * The only field of MDCR_EL2 that has a defined architectural reset
  7447. * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
  7448. */
  7449. ARMCPRegInfo mdcr_el2 = {
  7450. .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
  7451. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
  7452. .writefn = mdcr_el2_write,
  7453. .access = PL2_RW, .resetvalue = pmu_num_counters(env),
  7454. .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
  7455. };
  7456. define_one_arm_cp_reg(cpu, &mdcr_el2);
  7457. define_arm_cp_regs(cpu, vpidr_regs);
  7458. define_arm_cp_regs(cpu, el2_cp_reginfo);
  7459. if (arm_feature(env, ARM_FEATURE_V8)) {
  7460. define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
  7461. }
  7462. if (cpu_isar_feature(aa64_sel2, cpu)) {
  7463. define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
  7464. }
  7465. /*
  7466. * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
  7467. * See commentary near RMR_EL1.
  7468. */
  7469. if (!arm_feature(env, ARM_FEATURE_EL3)) {
  7470. static const ARMCPRegInfo el2_reset_regs[] = {
  7471. { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
  7472. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
  7473. .access = PL2_R,
  7474. .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
  7475. { .name = "RVBAR", .type = ARM_CP_ALIAS,
  7476. .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
  7477. .access = PL2_R,
  7478. .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
  7479. { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
  7480. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
  7481. .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
  7482. };
  7483. define_arm_cp_regs(cpu, el2_reset_regs);
  7484. }
  7485. }
  7486. /* Register the base EL3 cpregs. */
  7487. if (arm_feature(env, ARM_FEATURE_EL3)) {
  7488. define_arm_cp_regs(cpu, el3_cp_reginfo);
  7489. ARMCPRegInfo el3_regs[] = {
  7490. { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
  7491. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
  7492. .access = PL3_R,
  7493. .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
  7494. { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
  7495. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
  7496. .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
  7497. { .name = "RMR", .state = ARM_CP_STATE_AA32,
  7498. .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
  7499. .access = PL3_RW, .type = ARM_CP_CONST,
  7500. .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
  7501. { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
  7502. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
  7503. .access = PL3_RW,
  7504. .raw_writefn = raw_write, .writefn = sctlr_write,
  7505. .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
  7506. .resetvalue = cpu->reset_sctlr },
  7507. };
  7508. define_arm_cp_regs(cpu, el3_regs);
  7509. }
  7510. /*
  7511. * The behaviour of NSACR is sufficiently various that we don't
  7512. * try to describe it in a single reginfo:
  7513. * if EL3 is 64 bit, then trap to EL3 from S EL1,
  7514. * reads as constant 0xc00 from NS EL1 and NS EL2
  7515. * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
  7516. * if v7 without EL3, register doesn't exist
  7517. * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
  7518. */
  7519. if (arm_feature(env, ARM_FEATURE_EL3)) {
  7520. if (arm_feature(env, ARM_FEATURE_AARCH64)) {
  7521. static const ARMCPRegInfo nsacr = {
  7522. .name = "NSACR", .type = ARM_CP_CONST,
  7523. .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
  7524. .access = PL1_RW, .accessfn = nsacr_access,
  7525. .resetvalue = 0xc00
  7526. };
  7527. define_one_arm_cp_reg(cpu, &nsacr);
  7528. } else {
  7529. static const ARMCPRegInfo nsacr = {
  7530. .name = "NSACR",
  7531. .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
  7532. .access = PL3_RW | PL1_R,
  7533. .resetvalue = 0,
  7534. .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
  7535. };
  7536. define_one_arm_cp_reg(cpu, &nsacr);
  7537. }
  7538. } else {
  7539. if (arm_feature(env, ARM_FEATURE_V8)) {
  7540. static const ARMCPRegInfo nsacr = {
  7541. .name = "NSACR", .type = ARM_CP_CONST,
  7542. .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
  7543. .access = PL1_R,
  7544. .resetvalue = 0xc00
  7545. };
  7546. define_one_arm_cp_reg(cpu, &nsacr);
  7547. }
  7548. }
  7549. if (arm_feature(env, ARM_FEATURE_PMSA)) {
  7550. if (arm_feature(env, ARM_FEATURE_V6)) {
  7551. /* PMSAv6 not implemented */
  7552. assert(arm_feature(env, ARM_FEATURE_V7));
  7553. define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
  7554. define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
  7555. } else {
  7556. define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
  7557. }
  7558. } else {
  7559. define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
  7560. define_arm_cp_regs(cpu, vmsa_cp_reginfo);
  7561. /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
  7562. if (cpu_isar_feature(aa32_hpd, cpu)) {
  7563. define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
  7564. }
  7565. }
  7566. if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
  7567. define_arm_cp_regs(cpu, t2ee_cp_reginfo);
  7568. }
  7569. if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
  7570. define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
  7571. }
  7572. if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
  7573. define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
  7574. }
  7575. #ifndef CONFIG_USER_ONLY
  7576. if (cpu_isar_feature(aa64_ecv, cpu)) {
  7577. define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
  7578. }
  7579. #endif
  7580. if (arm_feature(env, ARM_FEATURE_VAPA)) {
  7581. ARMCPRegInfo vapa_cp_reginfo[] = {
  7582. { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
  7583. .access = PL1_RW, .resetvalue = 0,
  7584. .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
  7585. offsetoflow32(CPUARMState, cp15.par_ns) },
  7586. .writefn = par_write},
  7587. #ifndef CONFIG_USER_ONLY
  7588. /* This underdecoding is safe because the reginfo is NO_RAW. */
  7589. { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
  7590. .access = PL1_W, .accessfn = ats_access,
  7591. .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
  7592. #endif
  7593. };
  7594. /*
  7595. * When LPAE exists this 32-bit PAR register is an alias of the
  7596. * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
  7597. */
  7598. if (arm_feature(env, ARM_FEATURE_LPAE)) {
  7599. vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
  7600. }
  7601. define_arm_cp_regs(cpu, vapa_cp_reginfo);
  7602. }
  7603. if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
  7604. define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
  7605. }
  7606. if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
  7607. define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
  7608. }
  7609. if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
  7610. define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
  7611. }
  7612. if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
  7613. define_arm_cp_regs(cpu, omap_cp_reginfo);
  7614. }
  7615. if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
  7616. define_arm_cp_regs(cpu, strongarm_cp_reginfo);
  7617. }
  7618. if (arm_feature(env, ARM_FEATURE_XSCALE)) {
  7619. define_arm_cp_regs(cpu, xscale_cp_reginfo);
  7620. }
  7621. if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
  7622. define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
  7623. }
  7624. if (arm_feature(env, ARM_FEATURE_LPAE)) {
  7625. define_arm_cp_regs(cpu, lpae_cp_reginfo);
  7626. }
  7627. if (cpu_isar_feature(aa32_jazelle, cpu)) {
  7628. define_arm_cp_regs(cpu, jazelle_regs);
  7629. }
  7630. /*
  7631. * Slightly awkwardly, the OMAP and StrongARM cores need all of
  7632. * cp15 crn=0 to be writes-ignored, whereas for other cores they should
  7633. * be read-only (ie write causes UNDEF exception).
  7634. */
  7635. {
  7636. ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
  7637. /*
  7638. * Pre-v8 MIDR space.
  7639. * Note that the MIDR isn't a simple constant register because
  7640. * of the TI925 behaviour where writes to another register can
  7641. * cause the MIDR value to change.
  7642. *
  7643. * Unimplemented registers in the c15 0 0 0 space default to
  7644. * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
  7645. * and friends override accordingly.
  7646. */
  7647. { .name = "MIDR",
  7648. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
  7649. .access = PL1_R, .resetvalue = cpu->midr,
  7650. .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
  7651. .readfn = midr_read,
  7652. .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
  7653. .type = ARM_CP_OVERRIDE },
  7654. /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
  7655. { .name = "DUMMY",
  7656. .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
  7657. .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  7658. { .name = "DUMMY",
  7659. .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
  7660. .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  7661. { .name = "DUMMY",
  7662. .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
  7663. .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  7664. { .name = "DUMMY",
  7665. .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
  7666. .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  7667. { .name = "DUMMY",
  7668. .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
  7669. .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
  7670. };
  7671. ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
  7672. { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
  7673. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
  7674. .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
  7675. .fgt = FGT_MIDR_EL1,
  7676. .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
  7677. .readfn = midr_read },
  7678. /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
  7679. { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
  7680. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
  7681. .access = PL1_R, .resetvalue = cpu->midr },
  7682. { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
  7683. .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
  7684. .access = PL1_R,
  7685. .accessfn = access_aa64_tid1,
  7686. .fgt = FGT_REVIDR_EL1,
  7687. .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
  7688. };
  7689. ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
  7690. .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
  7691. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
  7692. .access = PL1_R, .resetvalue = cpu->midr
  7693. };
  7694. ARMCPRegInfo id_cp_reginfo[] = {
  7695. /* These are common to v8 and pre-v8 */
  7696. { .name = "CTR",
  7697. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
  7698. .access = PL1_R, .accessfn = ctr_el0_access,
  7699. .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
  7700. { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
  7701. .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
  7702. .access = PL0_R, .accessfn = ctr_el0_access,
  7703. .fgt = FGT_CTR_EL0,
  7704. .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
  7705. /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
  7706. { .name = "TCMTR",
  7707. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
  7708. .access = PL1_R,
  7709. .accessfn = access_aa32_tid1,
  7710. .type = ARM_CP_CONST, .resetvalue = 0 },
  7711. };
  7712. /* TLBTR is specific to VMSA */
  7713. ARMCPRegInfo id_tlbtr_reginfo = {
  7714. .name = "TLBTR",
  7715. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
  7716. .access = PL1_R,
  7717. .accessfn = access_aa32_tid1,
  7718. .type = ARM_CP_CONST, .resetvalue = 0,
  7719. };
  7720. /* MPUIR is specific to PMSA V6+ */
  7721. ARMCPRegInfo id_mpuir_reginfo = {
  7722. .name = "MPUIR",
  7723. .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
  7724. .access = PL1_R, .type = ARM_CP_CONST,
  7725. .resetvalue = cpu->pmsav7_dregion << 8
  7726. };
  7727. /* HMPUIR is specific to PMSA V8 */
  7728. ARMCPRegInfo id_hmpuir_reginfo = {
  7729. .name = "HMPUIR",
  7730. .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
  7731. .access = PL2_R, .type = ARM_CP_CONST,
  7732. .resetvalue = cpu->pmsav8r_hdregion
  7733. };
  7734. static const ARMCPRegInfo crn0_wi_reginfo = {
  7735. .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
  7736. .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
  7737. .type = ARM_CP_NOP | ARM_CP_OVERRIDE
  7738. };
  7739. #ifdef CONFIG_USER_ONLY
  7740. static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
  7741. { .name = "MIDR_EL1",
  7742. .exported_bits = R_MIDR_EL1_REVISION_MASK |
  7743. R_MIDR_EL1_PARTNUM_MASK |
  7744. R_MIDR_EL1_ARCHITECTURE_MASK |
  7745. R_MIDR_EL1_VARIANT_MASK |
  7746. R_MIDR_EL1_IMPLEMENTER_MASK },
  7747. { .name = "REVIDR_EL1" },
  7748. };
  7749. modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
  7750. #endif
  7751. if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
  7752. arm_feature(env, ARM_FEATURE_STRONGARM)) {
  7753. size_t i;
  7754. /*
  7755. * Register the blanket "writes ignored" value first to cover the
  7756. * whole space. Then update the specific ID registers to allow write
  7757. * access, so that they ignore writes rather than causing them to
  7758. * UNDEF.
  7759. */
  7760. define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
  7761. for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
  7762. id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
  7763. }
  7764. for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
  7765. id_cp_reginfo[i].access = PL1_RW;
  7766. }
  7767. id_mpuir_reginfo.access = PL1_RW;
  7768. id_tlbtr_reginfo.access = PL1_RW;
  7769. }
  7770. if (arm_feature(env, ARM_FEATURE_V8)) {
  7771. define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
  7772. if (!arm_feature(env, ARM_FEATURE_PMSA)) {
  7773. define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
  7774. }
  7775. } else {
  7776. define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
  7777. }
  7778. define_arm_cp_regs(cpu, id_cp_reginfo);
  7779. if (!arm_feature(env, ARM_FEATURE_PMSA)) {
  7780. define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
  7781. } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
  7782. arm_feature(env, ARM_FEATURE_V8)) {
  7783. uint32_t i = 0;
  7784. char *tmp_string;
  7785. define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
  7786. define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
  7787. define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
  7788. /* Register alias is only valid for first 32 indexes */
  7789. for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
  7790. uint8_t crm = 0b1000 | extract32(i, 1, 3);
  7791. uint8_t opc1 = extract32(i, 4, 1);
  7792. uint8_t opc2 = extract32(i, 0, 1) << 2;
  7793. tmp_string = g_strdup_printf("PRBAR%u", i);
  7794. ARMCPRegInfo tmp_prbarn_reginfo = {
  7795. .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
  7796. .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
  7797. .access = PL1_RW, .resetvalue = 0,
  7798. .accessfn = access_tvm_trvm,
  7799. .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
  7800. };
  7801. define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
  7802. g_free(tmp_string);
  7803. opc2 = extract32(i, 0, 1) << 2 | 0x1;
  7804. tmp_string = g_strdup_printf("PRLAR%u", i);
  7805. ARMCPRegInfo tmp_prlarn_reginfo = {
  7806. .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
  7807. .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
  7808. .access = PL1_RW, .resetvalue = 0,
  7809. .accessfn = access_tvm_trvm,
  7810. .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
  7811. };
  7812. define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
  7813. g_free(tmp_string);
  7814. }
  7815. /* Register alias is only valid for first 32 indexes */
  7816. for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
  7817. uint8_t crm = 0b1000 | extract32(i, 1, 3);
  7818. uint8_t opc1 = 0b100 | extract32(i, 4, 1);
  7819. uint8_t opc2 = extract32(i, 0, 1) << 2;
  7820. tmp_string = g_strdup_printf("HPRBAR%u", i);
  7821. ARMCPRegInfo tmp_hprbarn_reginfo = {
  7822. .name = tmp_string,
  7823. .type = ARM_CP_NO_RAW,
  7824. .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
  7825. .access = PL2_RW, .resetvalue = 0,
  7826. .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
  7827. };
  7828. define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
  7829. g_free(tmp_string);
  7830. opc2 = extract32(i, 0, 1) << 2 | 0x1;
  7831. tmp_string = g_strdup_printf("HPRLAR%u", i);
  7832. ARMCPRegInfo tmp_hprlarn_reginfo = {
  7833. .name = tmp_string,
  7834. .type = ARM_CP_NO_RAW,
  7835. .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
  7836. .access = PL2_RW, .resetvalue = 0,
  7837. .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
  7838. };
  7839. define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
  7840. g_free(tmp_string);
  7841. }
  7842. } else if (arm_feature(env, ARM_FEATURE_V7)) {
  7843. define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
  7844. }
  7845. }
  7846. if (arm_feature(env, ARM_FEATURE_MPIDR)) {
  7847. ARMCPRegInfo mpidr_cp_reginfo[] = {
  7848. { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
  7849. .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
  7850. .fgt = FGT_MPIDR_EL1,
  7851. .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
  7852. };
  7853. #ifdef CONFIG_USER_ONLY
  7854. static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
  7855. { .name = "MPIDR_EL1",
  7856. .fixed_bits = 0x0000000080000000 },
  7857. };
  7858. modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
  7859. #endif
  7860. define_arm_cp_regs(cpu, mpidr_cp_reginfo);
  7861. }
  7862. if (arm_feature(env, ARM_FEATURE_AUXCR)) {
  7863. ARMCPRegInfo auxcr_reginfo[] = {
  7864. { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
  7865. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
  7866. .access = PL1_RW, .accessfn = access_tacr,
  7867. .nv2_redirect_offset = 0x118,
  7868. .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
  7869. { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
  7870. .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
  7871. .access = PL2_RW, .type = ARM_CP_CONST,
  7872. .resetvalue = 0 },
  7873. { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
  7874. .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
  7875. .access = PL3_RW, .type = ARM_CP_CONST,
  7876. .resetvalue = 0 },
  7877. };
  7878. define_arm_cp_regs(cpu, auxcr_reginfo);
  7879. if (cpu_isar_feature(aa32_ac2, cpu)) {
  7880. define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
  7881. }
  7882. }
  7883. if (arm_feature(env, ARM_FEATURE_CBAR)) {
  7884. /*
  7885. * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
  7886. * There are two flavours:
  7887. * (1) older 32-bit only cores have a simple 32-bit CBAR
  7888. * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
  7889. * 32-bit register visible to AArch32 at a different encoding
  7890. * to the "flavour 1" register and with the bits rearranged to
  7891. * be able to squash a 64-bit address into the 32-bit view.
  7892. * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
  7893. * in future if we support AArch32-only configs of some of the
  7894. * AArch64 cores we might need to add a specific feature flag
  7895. * to indicate cores with "flavour 2" CBAR.
  7896. */
  7897. if (arm_feature(env, ARM_FEATURE_V8)) {
  7898. /* 32 bit view is [31:18] 0...0 [43:32]. */
  7899. uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
  7900. | extract64(cpu->reset_cbar, 32, 12);
  7901. ARMCPRegInfo cbar_reginfo[] = {
  7902. { .name = "CBAR",
  7903. .type = ARM_CP_CONST,
  7904. .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
  7905. .access = PL1_R, .resetvalue = cbar32 },
  7906. { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
  7907. .type = ARM_CP_CONST,
  7908. .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
  7909. .access = PL1_R, .resetvalue = cpu->reset_cbar },
  7910. };
  7911. /* We don't implement a r/w 64 bit CBAR currently */
  7912. assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
  7913. define_arm_cp_regs(cpu, cbar_reginfo);
  7914. } else {
  7915. ARMCPRegInfo cbar = {
  7916. .name = "CBAR",
  7917. .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
  7918. .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
  7919. .fieldoffset = offsetof(CPUARMState,
  7920. cp15.c15_config_base_address)
  7921. };
  7922. if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
  7923. cbar.access = PL1_R;
  7924. cbar.fieldoffset = 0;
  7925. cbar.type = ARM_CP_CONST;
  7926. }
  7927. define_one_arm_cp_reg(cpu, &cbar);
  7928. }
  7929. }
  7930. if (arm_feature(env, ARM_FEATURE_VBAR)) {
  7931. static const ARMCPRegInfo vbar_cp_reginfo[] = {
  7932. { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
  7933. .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
  7934. .access = PL1_RW, .writefn = vbar_write,
  7935. .accessfn = access_nv1,
  7936. .fgt = FGT_VBAR_EL1,
  7937. .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
  7938. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
  7939. offsetof(CPUARMState, cp15.vbar_ns) },
  7940. .resetvalue = 0 },
  7941. };
  7942. define_arm_cp_regs(cpu, vbar_cp_reginfo);
  7943. }
  7944. /* Generic registers whose values depend on the implementation */
  7945. {
  7946. ARMCPRegInfo sctlr = {
  7947. .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
  7948. .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
  7949. .access = PL1_RW, .accessfn = access_tvm_trvm,
  7950. .fgt = FGT_SCTLR_EL1,
  7951. .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
  7952. .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
  7953. offsetof(CPUARMState, cp15.sctlr_ns) },
  7954. .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
  7955. .raw_writefn = raw_write,
  7956. };
  7957. if (arm_feature(env, ARM_FEATURE_XSCALE)) {
  7958. /*
  7959. * Normally we would always end the TB on an SCTLR write, but Linux
  7960. * arch/arm/mach-pxa/sleep.S expects two instructions following
  7961. * an MMU enable to execute from cache. Imitate this behaviour.
  7962. */
  7963. sctlr.type |= ARM_CP_SUPPRESS_TB_END;
  7964. }
  7965. define_one_arm_cp_reg(cpu, &sctlr);
  7966. if (arm_feature(env, ARM_FEATURE_PMSA) &&
  7967. arm_feature(env, ARM_FEATURE_V8)) {
  7968. ARMCPRegInfo vsctlr = {
  7969. .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
  7970. .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
  7971. .access = PL2_RW, .resetvalue = 0x0,
  7972. .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
  7973. };
  7974. define_one_arm_cp_reg(cpu, &vsctlr);
  7975. }
  7976. }
  7977. if (cpu_isar_feature(aa64_lor, cpu)) {
  7978. define_arm_cp_regs(cpu, lor_reginfo);
  7979. }
  7980. if (cpu_isar_feature(aa64_pan, cpu)) {
  7981. define_one_arm_cp_reg(cpu, &pan_reginfo);
  7982. }
  7983. #ifndef CONFIG_USER_ONLY
  7984. if (cpu_isar_feature(aa64_ats1e1, cpu)) {
  7985. define_arm_cp_regs(cpu, ats1e1_reginfo);
  7986. }
  7987. if (cpu_isar_feature(aa32_ats1e1, cpu)) {
  7988. define_arm_cp_regs(cpu, ats1cp_reginfo);
  7989. }
  7990. #endif
  7991. if (cpu_isar_feature(aa64_uao, cpu)) {
  7992. define_one_arm_cp_reg(cpu, &uao_reginfo);
  7993. }
  7994. if (cpu_isar_feature(aa64_dit, cpu)) {
  7995. define_one_arm_cp_reg(cpu, &dit_reginfo);
  7996. }
  7997. if (cpu_isar_feature(aa64_ssbs, cpu)) {
  7998. define_one_arm_cp_reg(cpu, &ssbs_reginfo);
  7999. }
  8000. if (cpu_isar_feature(any_ras, cpu)) {
  8001. define_arm_cp_regs(cpu, minimal_ras_reginfo);
  8002. }
  8003. if (cpu_isar_feature(aa64_vh, cpu) ||
  8004. cpu_isar_feature(aa64_debugv8p2, cpu)) {
  8005. define_one_arm_cp_reg(cpu, &contextidr_el2);
  8006. }
  8007. if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
  8008. define_arm_cp_regs(cpu, vhe_reginfo);
  8009. }
  8010. if (cpu_isar_feature(aa64_sve, cpu)) {
  8011. define_arm_cp_regs(cpu, zcr_reginfo);
  8012. }
  8013. if (cpu_isar_feature(aa64_hcx, cpu)) {
  8014. define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
  8015. }
  8016. #ifdef TARGET_AARCH64
  8017. if (cpu_isar_feature(aa64_sme, cpu)) {
  8018. define_arm_cp_regs(cpu, sme_reginfo);
  8019. }
  8020. if (cpu_isar_feature(aa64_pauth, cpu)) {
  8021. define_arm_cp_regs(cpu, pauth_reginfo);
  8022. }
  8023. if (cpu_isar_feature(aa64_rndr, cpu)) {
  8024. define_arm_cp_regs(cpu, rndr_reginfo);
  8025. }
  8026. /* Data Cache clean instructions up to PoP */
  8027. if (cpu_isar_feature(aa64_dcpop, cpu)) {
  8028. define_one_arm_cp_reg(cpu, dcpop_reg);
  8029. if (cpu_isar_feature(aa64_dcpodp, cpu)) {
  8030. define_one_arm_cp_reg(cpu, dcpodp_reg);
  8031. }
  8032. }
  8033. /*
  8034. * If full MTE is enabled, add all of the system registers.
  8035. * If only "instructions available at EL0" are enabled,
  8036. * then define only a RAZ/WI version of PSTATE.TCO.
  8037. */
  8038. if (cpu_isar_feature(aa64_mte, cpu)) {
  8039. ARMCPRegInfo gmid_reginfo = {
  8040. .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
  8041. .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
  8042. .access = PL1_R, .accessfn = access_aa64_tid5,
  8043. .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
  8044. };
  8045. define_one_arm_cp_reg(cpu, &gmid_reginfo);
  8046. define_arm_cp_regs(cpu, mte_reginfo);
  8047. define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
  8048. } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
  8049. define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
  8050. define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
  8051. }
  8052. if (cpu_isar_feature(aa64_scxtnum, cpu)) {
  8053. define_arm_cp_regs(cpu, scxtnum_reginfo);
  8054. }
  8055. if (cpu_isar_feature(aa64_fgt, cpu)) {
  8056. define_arm_cp_regs(cpu, fgt_reginfo);
  8057. }
  8058. if (cpu_isar_feature(aa64_rme, cpu)) {
  8059. define_arm_cp_regs(cpu, rme_reginfo);
  8060. if (cpu_isar_feature(aa64_mte, cpu)) {
  8061. define_arm_cp_regs(cpu, rme_mte_reginfo);
  8062. }
  8063. }
  8064. if (cpu_isar_feature(aa64_nv2, cpu)) {
  8065. define_arm_cp_regs(cpu, nv2_reginfo);
  8066. }
  8067. if (cpu_isar_feature(aa64_nmi, cpu)) {
  8068. define_arm_cp_regs(cpu, nmi_reginfo);
  8069. }
  8070. #endif
  8071. if (cpu_isar_feature(any_predinv, cpu)) {
  8072. define_arm_cp_regs(cpu, predinv_reginfo);
  8073. }
  8074. if (cpu_isar_feature(any_ccidx, cpu)) {
  8075. define_arm_cp_regs(cpu, ccsidr2_reginfo);
  8076. }
  8077. #ifndef CONFIG_USER_ONLY
  8078. /*
  8079. * Register redirections and aliases must be done last,
  8080. * after the registers from the other extensions have been defined.
  8081. */
  8082. if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
  8083. define_arm_vh_e2h_redirects_aliases(cpu);
  8084. }
  8085. #endif
  8086. }
  8087. /*
  8088. * Private utility function for define_one_arm_cp_reg_with_opaque():
  8089. * add a single reginfo struct to the hash table.
  8090. */
  8091. static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
  8092. void *opaque, CPState state,
  8093. CPSecureState secstate,
  8094. int crm, int opc1, int opc2,
  8095. const char *name)
  8096. {
  8097. CPUARMState *env = &cpu->env;
  8098. uint32_t key;
  8099. ARMCPRegInfo *r2;
  8100. bool is64 = r->type & ARM_CP_64BIT;
  8101. bool ns = secstate & ARM_CP_SECSTATE_NS;
  8102. int cp = r->cp;
  8103. size_t name_len;
  8104. bool make_const;
  8105. switch (state) {
  8106. case ARM_CP_STATE_AA32:
  8107. /* We assume it is a cp15 register if the .cp field is left unset. */
  8108. if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
  8109. cp = 15;
  8110. }
  8111. key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
  8112. break;
  8113. case ARM_CP_STATE_AA64:
  8114. /*
  8115. * To allow abbreviation of ARMCPRegInfo definitions, we treat
  8116. * cp == 0 as equivalent to the value for "standard guest-visible
  8117. * sysreg". STATE_BOTH definitions are also always "standard sysreg"
  8118. * in their AArch64 view (the .cp value may be non-zero for the
  8119. * benefit of the AArch32 view).
  8120. */
  8121. if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
  8122. cp = CP_REG_ARM64_SYSREG_CP;
  8123. }
  8124. key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
  8125. break;
  8126. default:
  8127. g_assert_not_reached();
  8128. }
  8129. /* Overriding of an existing definition must be explicitly requested. */
  8130. if (!(r->type & ARM_CP_OVERRIDE)) {
  8131. const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
  8132. if (oldreg) {
  8133. assert(oldreg->type & ARM_CP_OVERRIDE);
  8134. }
  8135. }
  8136. /*
  8137. * Eliminate registers that are not present because the EL is missing.
  8138. * Doing this here makes it easier to put all registers for a given
  8139. * feature into the same ARMCPRegInfo array and define them all at once.
  8140. */
  8141. make_const = false;
  8142. if (arm_feature(env, ARM_FEATURE_EL3)) {
  8143. /*
  8144. * An EL2 register without EL2 but with EL3 is (usually) RES0.
  8145. * See rule RJFFP in section D1.1.3 of DDI0487H.a.
  8146. */
  8147. int min_el = ctz32(r->access) / 2;
  8148. if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
  8149. if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
  8150. return;
  8151. }
  8152. make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
  8153. }
  8154. } else {
  8155. CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
  8156. ? PL2_RW : PL1_RW);
  8157. if ((r->access & max_el) == 0) {
  8158. return;
  8159. }
  8160. }
  8161. /* Combine cpreg and name into one allocation. */
  8162. name_len = strlen(name) + 1;
  8163. r2 = g_malloc(sizeof(*r2) + name_len);
  8164. *r2 = *r;
  8165. r2->name = memcpy(r2 + 1, name, name_len);
  8166. /*
  8167. * Update fields to match the instantiation, overwiting wildcards
  8168. * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
  8169. */
  8170. r2->cp = cp;
  8171. r2->crm = crm;
  8172. r2->opc1 = opc1;
  8173. r2->opc2 = opc2;
  8174. r2->state = state;
  8175. r2->secure = secstate;
  8176. if (opaque) {
  8177. r2->opaque = opaque;
  8178. }
  8179. if (make_const) {
  8180. /* This should not have been a very special register to begin. */
  8181. int old_special = r2->type & ARM_CP_SPECIAL_MASK;
  8182. assert(old_special == 0 || old_special == ARM_CP_NOP);
  8183. /*
  8184. * Set the special function to CONST, retaining the other flags.
  8185. * This is important for e.g. ARM_CP_SVE so that we still
  8186. * take the SVE trap if CPTR_EL3.EZ == 0.
  8187. */
  8188. r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
  8189. /*
  8190. * Usually, these registers become RES0, but there are a few
  8191. * special cases like VPIDR_EL2 which have a constant non-zero
  8192. * value with writes ignored.
  8193. */
  8194. if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
  8195. r2->resetvalue = 0;
  8196. }
  8197. /*
  8198. * ARM_CP_CONST has precedence, so removing the callbacks and
  8199. * offsets are not strictly necessary, but it is potentially
  8200. * less confusing to debug later.
  8201. */
  8202. r2->readfn = NULL;
  8203. r2->writefn = NULL;
  8204. r2->raw_readfn = NULL;
  8205. r2->raw_writefn = NULL;
  8206. r2->resetfn = NULL;
  8207. r2->fieldoffset = 0;
  8208. r2->bank_fieldoffsets[0] = 0;
  8209. r2->bank_fieldoffsets[1] = 0;
  8210. } else {
  8211. bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
  8212. if (isbanked) {
  8213. /*
  8214. * Register is banked (using both entries in array).
  8215. * Overwriting fieldoffset as the array is only used to define
  8216. * banked registers but later only fieldoffset is used.
  8217. */
  8218. r2->fieldoffset = r->bank_fieldoffsets[ns];
  8219. }
  8220. if (state == ARM_CP_STATE_AA32) {
  8221. if (isbanked) {
  8222. /*
  8223. * If the register is banked then we don't need to migrate or
  8224. * reset the 32-bit instance in certain cases:
  8225. *
  8226. * 1) If the register has both 32-bit and 64-bit instances
  8227. * then we can count on the 64-bit instance taking care
  8228. * of the non-secure bank.
  8229. * 2) If ARMv8 is enabled then we can count on a 64-bit
  8230. * version taking care of the secure bank. This requires
  8231. * that separate 32 and 64-bit definitions are provided.
  8232. */
  8233. if ((r->state == ARM_CP_STATE_BOTH && ns) ||
  8234. (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
  8235. r2->type |= ARM_CP_ALIAS;
  8236. }
  8237. } else if ((secstate != r->secure) && !ns) {
  8238. /*
  8239. * The register is not banked so we only want to allow
  8240. * migration of the non-secure instance.
  8241. */
  8242. r2->type |= ARM_CP_ALIAS;
  8243. }
  8244. if (HOST_BIG_ENDIAN &&
  8245. r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
  8246. r2->fieldoffset += sizeof(uint32_t);
  8247. }
  8248. }
  8249. }
  8250. /*
  8251. * By convention, for wildcarded registers only the first
  8252. * entry is used for migration; the others are marked as
  8253. * ALIAS so we don't try to transfer the register
  8254. * multiple times. Special registers (ie NOP/WFI) are
  8255. * never migratable and not even raw-accessible.
  8256. */
  8257. if (r2->type & ARM_CP_SPECIAL_MASK) {
  8258. r2->type |= ARM_CP_NO_RAW;
  8259. }
  8260. if (((r->crm == CP_ANY) && crm != 0) ||
  8261. ((r->opc1 == CP_ANY) && opc1 != 0) ||
  8262. ((r->opc2 == CP_ANY) && opc2 != 0)) {
  8263. r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
  8264. }
  8265. /*
  8266. * Check that raw accesses are either forbidden or handled. Note that
  8267. * we can't assert this earlier because the setup of fieldoffset for
  8268. * banked registers has to be done first.
  8269. */
  8270. if (!(r2->type & ARM_CP_NO_RAW)) {
  8271. assert(!raw_accessors_invalid(r2));
  8272. }
  8273. g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
  8274. }
  8275. void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
  8276. const ARMCPRegInfo *r, void *opaque)
  8277. {
  8278. /*
  8279. * Define implementations of coprocessor registers.
  8280. * We store these in a hashtable because typically
  8281. * there are less than 150 registers in a space which
  8282. * is 16*16*16*8*8 = 262144 in size.
  8283. * Wildcarding is supported for the crm, opc1 and opc2 fields.
  8284. * If a register is defined twice then the second definition is
  8285. * used, so this can be used to define some generic registers and
  8286. * then override them with implementation specific variations.
  8287. * At least one of the original and the second definition should
  8288. * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
  8289. * against accidental use.
  8290. *
  8291. * The state field defines whether the register is to be
  8292. * visible in the AArch32 or AArch64 execution state. If the
  8293. * state is set to ARM_CP_STATE_BOTH then we synthesise a
  8294. * reginfo structure for the AArch32 view, which sees the lower
  8295. * 32 bits of the 64 bit register.
  8296. *
  8297. * Only registers visible in AArch64 may set r->opc0; opc0 cannot
  8298. * be wildcarded. AArch64 registers are always considered to be 64
  8299. * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
  8300. * the register, if any.
  8301. */
  8302. int crm, opc1, opc2;
  8303. int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
  8304. int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
  8305. int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
  8306. int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
  8307. int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
  8308. int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
  8309. CPState state;
  8310. /* 64 bit registers have only CRm and Opc1 fields */
  8311. assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
  8312. /* op0 only exists in the AArch64 encodings */
  8313. assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
  8314. /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
  8315. assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
  8316. /*
  8317. * This API is only for Arm's system coprocessors (14 and 15) or
  8318. * (M-profile or v7A-and-earlier only) for implementation defined
  8319. * coprocessors in the range 0..7. Our decode assumes this, since
  8320. * 8..13 can be used for other insns including VFP and Neon. See
  8321. * valid_cp() in translate.c. Assert here that we haven't tried
  8322. * to use an invalid coprocessor number.
  8323. */
  8324. switch (r->state) {
  8325. case ARM_CP_STATE_BOTH:
  8326. /* 0 has a special meaning, but otherwise the same rules as AA32. */
  8327. if (r->cp == 0) {
  8328. break;
  8329. }
  8330. /* fall through */
  8331. case ARM_CP_STATE_AA32:
  8332. if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
  8333. !arm_feature(&cpu->env, ARM_FEATURE_M)) {
  8334. assert(r->cp >= 14 && r->cp <= 15);
  8335. } else {
  8336. assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
  8337. }
  8338. break;
  8339. case ARM_CP_STATE_AA64:
  8340. assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
  8341. break;
  8342. default:
  8343. g_assert_not_reached();
  8344. }
  8345. /*
  8346. * The AArch64 pseudocode CheckSystemAccess() specifies that op1
  8347. * encodes a minimum access level for the register. We roll this
  8348. * runtime check into our general permission check code, so check
  8349. * here that the reginfo's specified permissions are strict enough
  8350. * to encompass the generic architectural permission check.
  8351. */
  8352. if (r->state != ARM_CP_STATE_AA32) {
  8353. CPAccessRights mask;
  8354. switch (r->opc1) {
  8355. case 0:
  8356. /* min_EL EL1, but some accessible to EL0 via kernel ABI */
  8357. mask = PL0U_R | PL1_RW;
  8358. break;
  8359. case 1: case 2:
  8360. /* min_EL EL1 */
  8361. mask = PL1_RW;
  8362. break;
  8363. case 3:
  8364. /* min_EL EL0 */
  8365. mask = PL0_RW;
  8366. break;
  8367. case 4:
  8368. case 5:
  8369. /* min_EL EL2 */
  8370. mask = PL2_RW;
  8371. break;
  8372. case 6:
  8373. /* min_EL EL3 */
  8374. mask = PL3_RW;
  8375. break;
  8376. case 7:
  8377. /* min_EL EL1, secure mode only (we don't check the latter) */
  8378. mask = PL1_RW;
  8379. break;
  8380. default:
  8381. /* broken reginfo with out-of-range opc1 */
  8382. g_assert_not_reached();
  8383. }
  8384. /* assert our permissions are not too lax (stricter is fine) */
  8385. assert((r->access & ~mask) == 0);
  8386. }
  8387. /*
  8388. * Check that the register definition has enough info to handle
  8389. * reads and writes if they are permitted.
  8390. */
  8391. if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
  8392. if (r->access & PL3_R) {
  8393. assert((r->fieldoffset ||
  8394. (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
  8395. r->readfn);
  8396. }
  8397. if (r->access & PL3_W) {
  8398. assert((r->fieldoffset ||
  8399. (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
  8400. r->writefn);
  8401. }
  8402. }
  8403. for (crm = crmmin; crm <= crmmax; crm++) {
  8404. for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
  8405. for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
  8406. for (state = ARM_CP_STATE_AA32;
  8407. state <= ARM_CP_STATE_AA64; state++) {
  8408. if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
  8409. continue;
  8410. }
  8411. if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
  8412. cpu_isar_feature(aa64_xs, cpu)) {
  8413. /*
  8414. * This is a TLBI insn which has an NXS variant. The
  8415. * NXS variant is at the same encoding except that
  8416. * crn is +1, and has the same behaviour except for
  8417. * fine-grained trapping. Add the NXS insn here and
  8418. * then fall through to add the normal register.
  8419. * add_cpreg_to_hashtable() copies the cpreg struct
  8420. * and name that it is passed, so it's OK to use
  8421. * a local struct here.
  8422. */
  8423. ARMCPRegInfo nxs_ri = *r;
  8424. g_autofree char *name = g_strdup_printf("%sNXS", r->name);
  8425. assert(state == ARM_CP_STATE_AA64);
  8426. assert(nxs_ri.crn < 0xf);
  8427. nxs_ri.crn++;
  8428. if (nxs_ri.fgt) {
  8429. nxs_ri.fgt |= R_FGT_NXS_MASK;
  8430. }
  8431. add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
  8432. ARM_CP_SECSTATE_NS,
  8433. crm, opc1, opc2, name);
  8434. }
  8435. if (state == ARM_CP_STATE_AA32) {
  8436. /*
  8437. * Under AArch32 CP registers can be common
  8438. * (same for secure and non-secure world) or banked.
  8439. */
  8440. char *name;
  8441. switch (r->secure) {
  8442. case ARM_CP_SECSTATE_S:
  8443. case ARM_CP_SECSTATE_NS:
  8444. add_cpreg_to_hashtable(cpu, r, opaque, state,
  8445. r->secure, crm, opc1, opc2,
  8446. r->name);
  8447. break;
  8448. case ARM_CP_SECSTATE_BOTH:
  8449. name = g_strdup_printf("%s_S", r->name);
  8450. add_cpreg_to_hashtable(cpu, r, opaque, state,
  8451. ARM_CP_SECSTATE_S,
  8452. crm, opc1, opc2, name);
  8453. g_free(name);
  8454. add_cpreg_to_hashtable(cpu, r, opaque, state,
  8455. ARM_CP_SECSTATE_NS,
  8456. crm, opc1, opc2, r->name);
  8457. break;
  8458. default:
  8459. g_assert_not_reached();
  8460. }
  8461. } else {
  8462. /*
  8463. * AArch64 registers get mapped to non-secure instance
  8464. * of AArch32
  8465. */
  8466. add_cpreg_to_hashtable(cpu, r, opaque, state,
  8467. ARM_CP_SECSTATE_NS,
  8468. crm, opc1, opc2, r->name);
  8469. }
  8470. }
  8471. }
  8472. }
  8473. }
  8474. }
  8475. /* Define a whole list of registers */
  8476. void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
  8477. void *opaque, size_t len)
  8478. {
  8479. size_t i;
  8480. for (i = 0; i < len; ++i) {
  8481. define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
  8482. }
  8483. }
  8484. /*
  8485. * Modify ARMCPRegInfo for access from userspace.
  8486. *
  8487. * This is a data driven modification directed by
  8488. * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
  8489. * user-space cannot alter any values and dynamic values pertaining to
  8490. * execution state are hidden from user space view anyway.
  8491. */
  8492. void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
  8493. const ARMCPRegUserSpaceInfo *mods,
  8494. size_t mods_len)
  8495. {
  8496. for (size_t mi = 0; mi < mods_len; ++mi) {
  8497. const ARMCPRegUserSpaceInfo *m = mods + mi;
  8498. GPatternSpec *pat = NULL;
  8499. if (m->is_glob) {
  8500. pat = g_pattern_spec_new(m->name);
  8501. }
  8502. for (size_t ri = 0; ri < regs_len; ++ri) {
  8503. ARMCPRegInfo *r = regs + ri;
  8504. if (pat && g_pattern_match_string(pat, r->name)) {
  8505. r->type = ARM_CP_CONST;
  8506. r->access = PL0U_R;
  8507. r->resetvalue = 0;
  8508. /* continue */
  8509. } else if (strcmp(r->name, m->name) == 0) {
  8510. r->type = ARM_CP_CONST;
  8511. r->access = PL0U_R;
  8512. r->resetvalue &= m->exported_bits;
  8513. r->resetvalue |= m->fixed_bits;
  8514. break;
  8515. }
  8516. }
  8517. if (pat) {
  8518. g_pattern_spec_free(pat);
  8519. }
  8520. }
  8521. }
  8522. const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
  8523. {
  8524. return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
  8525. }
  8526. void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
  8527. uint64_t value)
  8528. {
  8529. /* Helper coprocessor write function for write-ignore registers */
  8530. }
  8531. uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
  8532. {
  8533. /* Helper coprocessor write function for read-as-zero registers */
  8534. return 0;
  8535. }
  8536. void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
  8537. {
  8538. /* Helper coprocessor reset function for do-nothing-on-reset registers */
  8539. }
  8540. static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
  8541. {
  8542. /*
  8543. * Return true if it is not valid for us to switch to
  8544. * this CPU mode (ie all the UNPREDICTABLE cases in
  8545. * the ARM ARM CPSRWriteByInstr pseudocode).
  8546. */
  8547. /* Changes to or from Hyp via MSR and CPS are illegal. */
  8548. if (write_type == CPSRWriteByInstr &&
  8549. ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
  8550. mode == ARM_CPU_MODE_HYP)) {
  8551. return 1;
  8552. }
  8553. switch (mode) {
  8554. case ARM_CPU_MODE_USR:
  8555. return 0;
  8556. case ARM_CPU_MODE_SYS:
  8557. case ARM_CPU_MODE_SVC:
  8558. case ARM_CPU_MODE_ABT:
  8559. case ARM_CPU_MODE_UND:
  8560. case ARM_CPU_MODE_IRQ:
  8561. case ARM_CPU_MODE_FIQ:
  8562. /*
  8563. * Note that we don't implement the IMPDEF NSACR.RFR which in v7
  8564. * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
  8565. */
  8566. /*
  8567. * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
  8568. * and CPS are treated as illegal mode changes.
  8569. */
  8570. if (write_type == CPSRWriteByInstr &&
  8571. (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
  8572. (arm_hcr_el2_eff(env) & HCR_TGE)) {
  8573. return 1;
  8574. }
  8575. return 0;
  8576. case ARM_CPU_MODE_HYP:
  8577. return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
  8578. case ARM_CPU_MODE_MON:
  8579. return arm_current_el(env) < 3;
  8580. default:
  8581. return 1;
  8582. }
  8583. }
  8584. uint32_t cpsr_read(CPUARMState *env)
  8585. {
  8586. int ZF;
  8587. ZF = (env->ZF == 0);
  8588. return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
  8589. (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
  8590. | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
  8591. | ((env->condexec_bits & 0xfc) << 8)
  8592. | (env->GE << 16) | (env->daif & CPSR_AIF);
  8593. }
  8594. void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
  8595. CPSRWriteType write_type)
  8596. {
  8597. uint32_t changed_daif;
  8598. bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
  8599. (mask & (CPSR_M | CPSR_E | CPSR_IL));
  8600. if (mask & CPSR_NZCV) {
  8601. env->ZF = (~val) & CPSR_Z;
  8602. env->NF = val;
  8603. env->CF = (val >> 29) & 1;
  8604. env->VF = (val << 3) & 0x80000000;
  8605. }
  8606. if (mask & CPSR_Q) {
  8607. env->QF = ((val & CPSR_Q) != 0);
  8608. }
  8609. if (mask & CPSR_T) {
  8610. env->thumb = ((val & CPSR_T) != 0);
  8611. }
  8612. if (mask & CPSR_IT_0_1) {
  8613. env->condexec_bits &= ~3;
  8614. env->condexec_bits |= (val >> 25) & 3;
  8615. }
  8616. if (mask & CPSR_IT_2_7) {
  8617. env->condexec_bits &= 3;
  8618. env->condexec_bits |= (val >> 8) & 0xfc;
  8619. }
  8620. if (mask & CPSR_GE) {
  8621. env->GE = (val >> 16) & 0xf;
  8622. }
  8623. /*
  8624. * In a V7 implementation that includes the security extensions but does
  8625. * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
  8626. * whether non-secure software is allowed to change the CPSR_F and CPSR_A
  8627. * bits respectively.
  8628. *
  8629. * In a V8 implementation, it is permitted for privileged software to
  8630. * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
  8631. */
  8632. if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
  8633. arm_feature(env, ARM_FEATURE_EL3) &&
  8634. !arm_feature(env, ARM_FEATURE_EL2) &&
  8635. !arm_is_secure(env)) {
  8636. changed_daif = (env->daif ^ val) & mask;
  8637. if (changed_daif & CPSR_A) {
  8638. /*
  8639. * Check to see if we are allowed to change the masking of async
  8640. * abort exceptions from a non-secure state.
  8641. */
  8642. if (!(env->cp15.scr_el3 & SCR_AW)) {
  8643. qemu_log_mask(LOG_GUEST_ERROR,
  8644. "Ignoring attempt to switch CPSR_A flag from "
  8645. "non-secure world with SCR.AW bit clear\n");
  8646. mask &= ~CPSR_A;
  8647. }
  8648. }
  8649. if (changed_daif & CPSR_F) {
  8650. /*
  8651. * Check to see if we are allowed to change the masking of FIQ
  8652. * exceptions from a non-secure state.
  8653. */
  8654. if (!(env->cp15.scr_el3 & SCR_FW)) {
  8655. qemu_log_mask(LOG_GUEST_ERROR,
  8656. "Ignoring attempt to switch CPSR_F flag from "
  8657. "non-secure world with SCR.FW bit clear\n");
  8658. mask &= ~CPSR_F;
  8659. }
  8660. /*
  8661. * Check whether non-maskable FIQ (NMFI) support is enabled.
  8662. * If this bit is set software is not allowed to mask
  8663. * FIQs, but is allowed to set CPSR_F to 0.
  8664. */
  8665. if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
  8666. (val & CPSR_F)) {
  8667. qemu_log_mask(LOG_GUEST_ERROR,
  8668. "Ignoring attempt to enable CPSR_F flag "
  8669. "(non-maskable FIQ [NMFI] support enabled)\n");
  8670. mask &= ~CPSR_F;
  8671. }
  8672. }
  8673. }
  8674. env->daif &= ~(CPSR_AIF & mask);
  8675. env->daif |= val & CPSR_AIF & mask;
  8676. if (write_type != CPSRWriteRaw &&
  8677. ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
  8678. if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
  8679. /*
  8680. * Note that we can only get here in USR mode if this is a
  8681. * gdb stub write; for this case we follow the architectural
  8682. * behaviour for guest writes in USR mode of ignoring an attempt
  8683. * to switch mode. (Those are caught by translate.c for writes
  8684. * triggered by guest instructions.)
  8685. */
  8686. mask &= ~CPSR_M;
  8687. } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
  8688. /*
  8689. * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
  8690. * v7, and has defined behaviour in v8:
  8691. * + leave CPSR.M untouched
  8692. * + allow changes to the other CPSR fields
  8693. * + set PSTATE.IL
  8694. * For user changes via the GDB stub, we don't set PSTATE.IL,
  8695. * as this would be unnecessarily harsh for a user error.
  8696. */
  8697. mask &= ~CPSR_M;
  8698. if (write_type != CPSRWriteByGDBStub &&
  8699. arm_feature(env, ARM_FEATURE_V8)) {
  8700. mask |= CPSR_IL;
  8701. val |= CPSR_IL;
  8702. }
  8703. qemu_log_mask(LOG_GUEST_ERROR,
  8704. "Illegal AArch32 mode switch attempt from %s to %s\n",
  8705. aarch32_mode_name(env->uncached_cpsr),
  8706. aarch32_mode_name(val));
  8707. } else {
  8708. qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
  8709. write_type == CPSRWriteExceptionReturn ?
  8710. "Exception return from AArch32" :
  8711. "AArch32 mode switch from",
  8712. aarch32_mode_name(env->uncached_cpsr),
  8713. aarch32_mode_name(val), env->regs[15]);
  8714. switch_mode(env, val & CPSR_M);
  8715. }
  8716. }
  8717. mask &= ~CACHED_CPSR_BITS;
  8718. env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
  8719. if (tcg_enabled() && rebuild_hflags) {
  8720. arm_rebuild_hflags(env);
  8721. }
  8722. }
  8723. #ifdef CONFIG_USER_ONLY
  8724. static void switch_mode(CPUARMState *env, int mode)
  8725. {
  8726. ARMCPU *cpu = env_archcpu(env);
  8727. if (mode != ARM_CPU_MODE_USR) {
  8728. cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
  8729. }
  8730. }
  8731. uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
  8732. uint32_t cur_el, bool secure)
  8733. {
  8734. return 1;
  8735. }
  8736. void aarch64_sync_64_to_32(CPUARMState *env)
  8737. {
  8738. g_assert_not_reached();
  8739. }
  8740. #else
  8741. static void switch_mode(CPUARMState *env, int mode)
  8742. {
  8743. int old_mode;
  8744. int i;
  8745. old_mode = env->uncached_cpsr & CPSR_M;
  8746. if (mode == old_mode) {
  8747. return;
  8748. }
  8749. if (old_mode == ARM_CPU_MODE_FIQ) {
  8750. memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
  8751. memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
  8752. } else if (mode == ARM_CPU_MODE_FIQ) {
  8753. memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
  8754. memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
  8755. }
  8756. i = bank_number(old_mode);
  8757. env->banked_r13[i] = env->regs[13];
  8758. env->banked_spsr[i] = env->spsr;
  8759. i = bank_number(mode);
  8760. env->regs[13] = env->banked_r13[i];
  8761. env->spsr = env->banked_spsr[i];
  8762. env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
  8763. env->regs[14] = env->banked_r14[r14_bank_number(mode)];
  8764. }
  8765. /*
  8766. * Physical Interrupt Target EL Lookup Table
  8767. *
  8768. * [ From ARM ARM section G1.13.4 (Table G1-15) ]
  8769. *
  8770. * The below multi-dimensional table is used for looking up the target
  8771. * exception level given numerous condition criteria. Specifically, the
  8772. * target EL is based on SCR and HCR routing controls as well as the
  8773. * currently executing EL and secure state.
  8774. *
  8775. * Dimensions:
  8776. * target_el_table[2][2][2][2][2][4]
  8777. * | | | | | +--- Current EL
  8778. * | | | | +------ Non-secure(0)/Secure(1)
  8779. * | | | +--------- HCR mask override
  8780. * | | +------------ SCR exec state control
  8781. * | +--------------- SCR mask override
  8782. * +------------------ 32-bit(0)/64-bit(1) EL3
  8783. *
  8784. * The table values are as such:
  8785. * 0-3 = EL0-EL3
  8786. * -1 = Cannot occur
  8787. *
  8788. * The ARM ARM target EL table includes entries indicating that an "exception
  8789. * is not taken". The two cases where this is applicable are:
  8790. * 1) An exception is taken from EL3 but the SCR does not have the exception
  8791. * routed to EL3.
  8792. * 2) An exception is taken from EL2 but the HCR does not have the exception
  8793. * routed to EL2.
  8794. * In these two cases, the below table contain a target of EL1. This value is
  8795. * returned as it is expected that the consumer of the table data will check
  8796. * for "target EL >= current EL" to ensure the exception is not taken.
  8797. *
  8798. * SCR HCR
  8799. * 64 EA AMO From
  8800. * BIT IRQ IMO Non-secure Secure
  8801. * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
  8802. */
  8803. static const int8_t target_el_table[2][2][2][2][2][4] = {
  8804. {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
  8805. {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
  8806. {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
  8807. {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
  8808. {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
  8809. {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
  8810. {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
  8811. {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
  8812. {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
  8813. {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
  8814. {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
  8815. {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
  8816. {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
  8817. {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
  8818. {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
  8819. {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
  8820. };
  8821. /*
  8822. * Determine the target EL for physical exceptions
  8823. */
  8824. uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
  8825. uint32_t cur_el, bool secure)
  8826. {
  8827. CPUARMState *env = cpu_env(cs);
  8828. bool rw;
  8829. bool scr;
  8830. bool hcr;
  8831. int target_el;
  8832. /* Is the highest EL AArch64? */
  8833. bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
  8834. uint64_t hcr_el2;
  8835. if (arm_feature(env, ARM_FEATURE_EL3)) {
  8836. rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
  8837. } else {
  8838. /*
  8839. * Either EL2 is the highest EL (and so the EL2 register width
  8840. * is given by is64); or there is no EL2 or EL3, in which case
  8841. * the value of 'rw' does not affect the table lookup anyway.
  8842. */
  8843. rw = is64;
  8844. }
  8845. hcr_el2 = arm_hcr_el2_eff(env);
  8846. switch (excp_idx) {
  8847. case EXCP_IRQ:
  8848. case EXCP_NMI:
  8849. scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
  8850. hcr = hcr_el2 & HCR_IMO;
  8851. break;
  8852. case EXCP_FIQ:
  8853. scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
  8854. hcr = hcr_el2 & HCR_FMO;
  8855. break;
  8856. default:
  8857. scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
  8858. hcr = hcr_el2 & HCR_AMO;
  8859. break;
  8860. };
  8861. /*
  8862. * For these purposes, TGE and AMO/IMO/FMO both force the
  8863. * interrupt to EL2. Fold TGE into the bit extracted above.
  8864. */
  8865. hcr |= (hcr_el2 & HCR_TGE) != 0;
  8866. /* Perform a table-lookup for the target EL given the current state */
  8867. target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
  8868. assert(target_el > 0);
  8869. return target_el;
  8870. }
  8871. void arm_log_exception(CPUState *cs)
  8872. {
  8873. int idx = cs->exception_index;
  8874. if (qemu_loglevel_mask(CPU_LOG_INT)) {
  8875. const char *exc = NULL;
  8876. static const char * const excnames[] = {
  8877. [EXCP_UDEF] = "Undefined Instruction",
  8878. [EXCP_SWI] = "SVC",
  8879. [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
  8880. [EXCP_DATA_ABORT] = "Data Abort",
  8881. [EXCP_IRQ] = "IRQ",
  8882. [EXCP_FIQ] = "FIQ",
  8883. [EXCP_BKPT] = "Breakpoint",
  8884. [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
  8885. [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
  8886. [EXCP_HVC] = "Hypervisor Call",
  8887. [EXCP_HYP_TRAP] = "Hypervisor Trap",
  8888. [EXCP_SMC] = "Secure Monitor Call",
  8889. [EXCP_VIRQ] = "Virtual IRQ",
  8890. [EXCP_VFIQ] = "Virtual FIQ",
  8891. [EXCP_SEMIHOST] = "Semihosting call",
  8892. [EXCP_NOCP] = "v7M NOCP UsageFault",
  8893. [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
  8894. [EXCP_STKOF] = "v8M STKOF UsageFault",
  8895. [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
  8896. [EXCP_LSERR] = "v8M LSERR UsageFault",
  8897. [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
  8898. [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
  8899. [EXCP_VSERR] = "Virtual SERR",
  8900. [EXCP_GPC] = "Granule Protection Check",
  8901. [EXCP_NMI] = "NMI",
  8902. [EXCP_VINMI] = "Virtual IRQ NMI",
  8903. [EXCP_VFNMI] = "Virtual FIQ NMI",
  8904. [EXCP_MON_TRAP] = "Monitor Trap",
  8905. };
  8906. if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
  8907. exc = excnames[idx];
  8908. }
  8909. if (!exc) {
  8910. exc = "unknown";
  8911. }
  8912. qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
  8913. idx, exc, cs->cpu_index);
  8914. }
  8915. }
  8916. /*
  8917. * Function used to synchronize QEMU's AArch64 register set with AArch32
  8918. * register set. This is necessary when switching between AArch32 and AArch64
  8919. * execution state.
  8920. */
  8921. void aarch64_sync_32_to_64(CPUARMState *env)
  8922. {
  8923. int i;
  8924. uint32_t mode = env->uncached_cpsr & CPSR_M;
  8925. /* We can blanket copy R[0:7] to X[0:7] */
  8926. for (i = 0; i < 8; i++) {
  8927. env->xregs[i] = env->regs[i];
  8928. }
  8929. /*
  8930. * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
  8931. * Otherwise, they come from the banked user regs.
  8932. */
  8933. if (mode == ARM_CPU_MODE_FIQ) {
  8934. for (i = 8; i < 13; i++) {
  8935. env->xregs[i] = env->usr_regs[i - 8];
  8936. }
  8937. } else {
  8938. for (i = 8; i < 13; i++) {
  8939. env->xregs[i] = env->regs[i];
  8940. }
  8941. }
  8942. /*
  8943. * Registers x13-x23 are the various mode SP and FP registers. Registers
  8944. * r13 and r14 are only copied if we are in that mode, otherwise we copy
  8945. * from the mode banked register.
  8946. */
  8947. if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
  8948. env->xregs[13] = env->regs[13];
  8949. env->xregs[14] = env->regs[14];
  8950. } else {
  8951. env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
  8952. /* HYP is an exception in that it is copied from r14 */
  8953. if (mode == ARM_CPU_MODE_HYP) {
  8954. env->xregs[14] = env->regs[14];
  8955. } else {
  8956. env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
  8957. }
  8958. }
  8959. if (mode == ARM_CPU_MODE_HYP) {
  8960. env->xregs[15] = env->regs[13];
  8961. } else {
  8962. env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
  8963. }
  8964. if (mode == ARM_CPU_MODE_IRQ) {
  8965. env->xregs[16] = env->regs[14];
  8966. env->xregs[17] = env->regs[13];
  8967. } else {
  8968. env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
  8969. env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
  8970. }
  8971. if (mode == ARM_CPU_MODE_SVC) {
  8972. env->xregs[18] = env->regs[14];
  8973. env->xregs[19] = env->regs[13];
  8974. } else {
  8975. env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
  8976. env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
  8977. }
  8978. if (mode == ARM_CPU_MODE_ABT) {
  8979. env->xregs[20] = env->regs[14];
  8980. env->xregs[21] = env->regs[13];
  8981. } else {
  8982. env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
  8983. env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
  8984. }
  8985. if (mode == ARM_CPU_MODE_UND) {
  8986. env->xregs[22] = env->regs[14];
  8987. env->xregs[23] = env->regs[13];
  8988. } else {
  8989. env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
  8990. env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
  8991. }
  8992. /*
  8993. * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
  8994. * mode, then we can copy from r8-r14. Otherwise, we copy from the
  8995. * FIQ bank for r8-r14.
  8996. */
  8997. if (mode == ARM_CPU_MODE_FIQ) {
  8998. for (i = 24; i < 31; i++) {
  8999. env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
  9000. }
  9001. } else {
  9002. for (i = 24; i < 29; i++) {
  9003. env->xregs[i] = env->fiq_regs[i - 24];
  9004. }
  9005. env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
  9006. env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
  9007. }
  9008. env->pc = env->regs[15];
  9009. }
  9010. /*
  9011. * Function used to synchronize QEMU's AArch32 register set with AArch64
  9012. * register set. This is necessary when switching between AArch32 and AArch64
  9013. * execution state.
  9014. */
  9015. void aarch64_sync_64_to_32(CPUARMState *env)
  9016. {
  9017. int i;
  9018. uint32_t mode = env->uncached_cpsr & CPSR_M;
  9019. /* We can blanket copy X[0:7] to R[0:7] */
  9020. for (i = 0; i < 8; i++) {
  9021. env->regs[i] = env->xregs[i];
  9022. }
  9023. /*
  9024. * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
  9025. * Otherwise, we copy x8-x12 into the banked user regs.
  9026. */
  9027. if (mode == ARM_CPU_MODE_FIQ) {
  9028. for (i = 8; i < 13; i++) {
  9029. env->usr_regs[i - 8] = env->xregs[i];
  9030. }
  9031. } else {
  9032. for (i = 8; i < 13; i++) {
  9033. env->regs[i] = env->xregs[i];
  9034. }
  9035. }
  9036. /*
  9037. * Registers r13 & r14 depend on the current mode.
  9038. * If we are in a given mode, we copy the corresponding x registers to r13
  9039. * and r14. Otherwise, we copy the x register to the banked r13 and r14
  9040. * for the mode.
  9041. */
  9042. if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
  9043. env->regs[13] = env->xregs[13];
  9044. env->regs[14] = env->xregs[14];
  9045. } else {
  9046. env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
  9047. /*
  9048. * HYP is an exception in that it does not have its own banked r14 but
  9049. * shares the USR r14
  9050. */
  9051. if (mode == ARM_CPU_MODE_HYP) {
  9052. env->regs[14] = env->xregs[14];
  9053. } else {
  9054. env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
  9055. }
  9056. }
  9057. if (mode == ARM_CPU_MODE_HYP) {
  9058. env->regs[13] = env->xregs[15];
  9059. } else {
  9060. env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
  9061. }
  9062. if (mode == ARM_CPU_MODE_IRQ) {
  9063. env->regs[14] = env->xregs[16];
  9064. env->regs[13] = env->xregs[17];
  9065. } else {
  9066. env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
  9067. env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
  9068. }
  9069. if (mode == ARM_CPU_MODE_SVC) {
  9070. env->regs[14] = env->xregs[18];
  9071. env->regs[13] = env->xregs[19];
  9072. } else {
  9073. env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
  9074. env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
  9075. }
  9076. if (mode == ARM_CPU_MODE_ABT) {
  9077. env->regs[14] = env->xregs[20];
  9078. env->regs[13] = env->xregs[21];
  9079. } else {
  9080. env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
  9081. env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
  9082. }
  9083. if (mode == ARM_CPU_MODE_UND) {
  9084. env->regs[14] = env->xregs[22];
  9085. env->regs[13] = env->xregs[23];
  9086. } else {
  9087. env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
  9088. env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
  9089. }
  9090. /*
  9091. * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
  9092. * mode, then we can copy to r8-r14. Otherwise, we copy to the
  9093. * FIQ bank for r8-r14.
  9094. */
  9095. if (mode == ARM_CPU_MODE_FIQ) {
  9096. for (i = 24; i < 31; i++) {
  9097. env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
  9098. }
  9099. } else {
  9100. for (i = 24; i < 29; i++) {
  9101. env->fiq_regs[i - 24] = env->xregs[i];
  9102. }
  9103. env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
  9104. env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
  9105. }
  9106. env->regs[15] = env->pc;
  9107. }
  9108. static void take_aarch32_exception(CPUARMState *env, int new_mode,
  9109. uint32_t mask, uint32_t offset,
  9110. uint32_t newpc)
  9111. {
  9112. int new_el;
  9113. /* Change the CPU state so as to actually take the exception. */
  9114. switch_mode(env, new_mode);
  9115. /*
  9116. * For exceptions taken to AArch32 we must clear the SS bit in both
  9117. * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
  9118. */
  9119. env->pstate &= ~PSTATE_SS;
  9120. env->spsr = cpsr_read(env);
  9121. /* Clear IT bits. */
  9122. env->condexec_bits = 0;
  9123. /* Switch to the new mode, and to the correct instruction set. */
  9124. env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
  9125. /* This must be after mode switching. */
  9126. new_el = arm_current_el(env);
  9127. /* Set new mode endianness */
  9128. env->uncached_cpsr &= ~CPSR_E;
  9129. if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
  9130. env->uncached_cpsr |= CPSR_E;
  9131. }
  9132. /* J and IL must always be cleared for exception entry */
  9133. env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
  9134. env->daif |= mask;
  9135. if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
  9136. if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
  9137. env->uncached_cpsr |= CPSR_SSBS;
  9138. } else {
  9139. env->uncached_cpsr &= ~CPSR_SSBS;
  9140. }
  9141. }
  9142. if (new_mode == ARM_CPU_MODE_HYP) {
  9143. env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
  9144. env->elr_el[2] = env->regs[15];
  9145. } else {
  9146. /* CPSR.PAN is normally preserved preserved unless... */
  9147. if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
  9148. switch (new_el) {
  9149. case 3:
  9150. if (!arm_is_secure_below_el3(env)) {
  9151. /* ... the target is EL3, from non-secure state. */
  9152. env->uncached_cpsr &= ~CPSR_PAN;
  9153. break;
  9154. }
  9155. /* ... the target is EL3, from secure state ... */
  9156. /* fall through */
  9157. case 1:
  9158. /* ... the target is EL1 and SCTLR.SPAN is 0. */
  9159. if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
  9160. env->uncached_cpsr |= CPSR_PAN;
  9161. }
  9162. break;
  9163. }
  9164. }
  9165. /*
  9166. * this is a lie, as there was no c1_sys on V4T/V5, but who cares
  9167. * and we should just guard the thumb mode on V4
  9168. */
  9169. if (arm_feature(env, ARM_FEATURE_V4T)) {
  9170. env->thumb =
  9171. (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
  9172. }
  9173. env->regs[14] = env->regs[15] + offset;
  9174. }
  9175. env->regs[15] = newpc;
  9176. if (tcg_enabled()) {
  9177. arm_rebuild_hflags(env);
  9178. }
  9179. }
  9180. static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
  9181. {
  9182. /*
  9183. * Handle exception entry to Hyp mode; this is sufficiently
  9184. * different to entry to other AArch32 modes that we handle it
  9185. * separately here.
  9186. *
  9187. * The vector table entry used is always the 0x14 Hyp mode entry point,
  9188. * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
  9189. * The offset applied to the preferred return address is always zero
  9190. * (see DDI0487C.a section G1.12.3).
  9191. * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
  9192. */
  9193. uint32_t addr, mask;
  9194. ARMCPU *cpu = ARM_CPU(cs);
  9195. CPUARMState *env = &cpu->env;
  9196. switch (cs->exception_index) {
  9197. case EXCP_UDEF:
  9198. addr = 0x04;
  9199. break;
  9200. case EXCP_SWI:
  9201. addr = 0x08;
  9202. break;
  9203. case EXCP_BKPT:
  9204. /* Fall through to prefetch abort. */
  9205. case EXCP_PREFETCH_ABORT:
  9206. env->cp15.ifar_s = env->exception.vaddress;
  9207. qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
  9208. (uint32_t)env->exception.vaddress);
  9209. addr = 0x0c;
  9210. break;
  9211. case EXCP_DATA_ABORT:
  9212. env->cp15.dfar_s = env->exception.vaddress;
  9213. qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
  9214. (uint32_t)env->exception.vaddress);
  9215. addr = 0x10;
  9216. break;
  9217. case EXCP_IRQ:
  9218. addr = 0x18;
  9219. break;
  9220. case EXCP_FIQ:
  9221. addr = 0x1c;
  9222. break;
  9223. case EXCP_HVC:
  9224. addr = 0x08;
  9225. break;
  9226. case EXCP_HYP_TRAP:
  9227. addr = 0x14;
  9228. break;
  9229. default:
  9230. cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
  9231. }
  9232. if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
  9233. if (!arm_feature(env, ARM_FEATURE_V8)) {
  9234. /*
  9235. * QEMU syndrome values are v8-style. v7 has the IL bit
  9236. * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
  9237. * If this is a v7 CPU, squash the IL bit in those cases.
  9238. */
  9239. if (cs->exception_index == EXCP_PREFETCH_ABORT ||
  9240. (cs->exception_index == EXCP_DATA_ABORT &&
  9241. !(env->exception.syndrome & ARM_EL_ISV)) ||
  9242. syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
  9243. env->exception.syndrome &= ~ARM_EL_IL;
  9244. }
  9245. }
  9246. env->cp15.esr_el[2] = env->exception.syndrome;
  9247. }
  9248. if (arm_current_el(env) != 2 && addr < 0x14) {
  9249. addr = 0x14;
  9250. }
  9251. mask = 0;
  9252. if (!(env->cp15.scr_el3 & SCR_EA)) {
  9253. mask |= CPSR_A;
  9254. }
  9255. if (!(env->cp15.scr_el3 & SCR_IRQ)) {
  9256. mask |= CPSR_I;
  9257. }
  9258. if (!(env->cp15.scr_el3 & SCR_FIQ)) {
  9259. mask |= CPSR_F;
  9260. }
  9261. addr += env->cp15.hvbar;
  9262. take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
  9263. }
  9264. static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
  9265. {
  9266. ARMCPU *cpu = ARM_CPU(cs);
  9267. CPUARMState *env = &cpu->env;
  9268. uint32_t addr;
  9269. uint32_t mask;
  9270. int new_mode;
  9271. uint32_t offset;
  9272. uint32_t moe;
  9273. /* If this is a debug exception we must update the DBGDSCR.MOE bits */
  9274. switch (syn_get_ec(env->exception.syndrome)) {
  9275. case EC_BREAKPOINT:
  9276. case EC_BREAKPOINT_SAME_EL:
  9277. moe = 1;
  9278. break;
  9279. case EC_WATCHPOINT:
  9280. case EC_WATCHPOINT_SAME_EL:
  9281. moe = 10;
  9282. break;
  9283. case EC_AA32_BKPT:
  9284. moe = 3;
  9285. break;
  9286. case EC_VECTORCATCH:
  9287. moe = 5;
  9288. break;
  9289. default:
  9290. moe = 0;
  9291. break;
  9292. }
  9293. if (moe) {
  9294. env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
  9295. }
  9296. if (env->exception.target_el == 2) {
  9297. /* Debug exceptions are reported differently on AArch32 */
  9298. switch (syn_get_ec(env->exception.syndrome)) {
  9299. case EC_BREAKPOINT:
  9300. case EC_BREAKPOINT_SAME_EL:
  9301. case EC_AA32_BKPT:
  9302. case EC_VECTORCATCH:
  9303. env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
  9304. 0, 0, 0x22);
  9305. break;
  9306. case EC_WATCHPOINT:
  9307. env->exception.syndrome = syn_set_ec(env->exception.syndrome,
  9308. EC_DATAABORT);
  9309. break;
  9310. case EC_WATCHPOINT_SAME_EL:
  9311. env->exception.syndrome = syn_set_ec(env->exception.syndrome,
  9312. EC_DATAABORT_SAME_EL);
  9313. break;
  9314. }
  9315. arm_cpu_do_interrupt_aarch32_hyp(cs);
  9316. return;
  9317. }
  9318. switch (cs->exception_index) {
  9319. case EXCP_UDEF:
  9320. new_mode = ARM_CPU_MODE_UND;
  9321. addr = 0x04;
  9322. mask = CPSR_I;
  9323. if (env->thumb) {
  9324. offset = 2;
  9325. } else {
  9326. offset = 4;
  9327. }
  9328. break;
  9329. case EXCP_SWI:
  9330. new_mode = ARM_CPU_MODE_SVC;
  9331. addr = 0x08;
  9332. mask = CPSR_I;
  9333. /* The PC already points to the next instruction. */
  9334. offset = 0;
  9335. break;
  9336. case EXCP_BKPT:
  9337. /* Fall through to prefetch abort. */
  9338. case EXCP_PREFETCH_ABORT:
  9339. A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
  9340. A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
  9341. qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
  9342. env->exception.fsr, (uint32_t)env->exception.vaddress);
  9343. new_mode = ARM_CPU_MODE_ABT;
  9344. addr = 0x0c;
  9345. mask = CPSR_A | CPSR_I;
  9346. offset = 4;
  9347. break;
  9348. case EXCP_DATA_ABORT:
  9349. A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
  9350. A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
  9351. qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
  9352. env->exception.fsr,
  9353. (uint32_t)env->exception.vaddress);
  9354. new_mode = ARM_CPU_MODE_ABT;
  9355. addr = 0x10;
  9356. mask = CPSR_A | CPSR_I;
  9357. offset = 8;
  9358. break;
  9359. case EXCP_IRQ:
  9360. new_mode = ARM_CPU_MODE_IRQ;
  9361. addr = 0x18;
  9362. /* Disable IRQ and imprecise data aborts. */
  9363. mask = CPSR_A | CPSR_I;
  9364. offset = 4;
  9365. if (env->cp15.scr_el3 & SCR_IRQ) {
  9366. /* IRQ routed to monitor mode */
  9367. new_mode = ARM_CPU_MODE_MON;
  9368. mask |= CPSR_F;
  9369. }
  9370. break;
  9371. case EXCP_FIQ:
  9372. new_mode = ARM_CPU_MODE_FIQ;
  9373. addr = 0x1c;
  9374. /* Disable FIQ, IRQ and imprecise data aborts. */
  9375. mask = CPSR_A | CPSR_I | CPSR_F;
  9376. if (env->cp15.scr_el3 & SCR_FIQ) {
  9377. /* FIQ routed to monitor mode */
  9378. new_mode = ARM_CPU_MODE_MON;
  9379. }
  9380. offset = 4;
  9381. break;
  9382. case EXCP_VIRQ:
  9383. new_mode = ARM_CPU_MODE_IRQ;
  9384. addr = 0x18;
  9385. /* Disable IRQ and imprecise data aborts. */
  9386. mask = CPSR_A | CPSR_I;
  9387. offset = 4;
  9388. break;
  9389. case EXCP_VFIQ:
  9390. new_mode = ARM_CPU_MODE_FIQ;
  9391. addr = 0x1c;
  9392. /* Disable FIQ, IRQ and imprecise data aborts. */
  9393. mask = CPSR_A | CPSR_I | CPSR_F;
  9394. offset = 4;
  9395. break;
  9396. case EXCP_VSERR:
  9397. {
  9398. /*
  9399. * Note that this is reported as a data abort, but the DFAR
  9400. * has an UNKNOWN value. Construct the SError syndrome from
  9401. * AET and ExT fields.
  9402. */
  9403. ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
  9404. if (extended_addresses_enabled(env)) {
  9405. env->exception.fsr = arm_fi_to_lfsc(&fi);
  9406. } else {
  9407. env->exception.fsr = arm_fi_to_sfsc(&fi);
  9408. }
  9409. env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
  9410. A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
  9411. qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
  9412. env->exception.fsr);
  9413. new_mode = ARM_CPU_MODE_ABT;
  9414. addr = 0x10;
  9415. mask = CPSR_A | CPSR_I;
  9416. offset = 8;
  9417. }
  9418. break;
  9419. case EXCP_SMC:
  9420. new_mode = ARM_CPU_MODE_MON;
  9421. addr = 0x08;
  9422. mask = CPSR_A | CPSR_I | CPSR_F;
  9423. offset = 0;
  9424. break;
  9425. case EXCP_MON_TRAP:
  9426. new_mode = ARM_CPU_MODE_MON;
  9427. addr = 0x04;
  9428. mask = CPSR_A | CPSR_I | CPSR_F;
  9429. if (env->thumb) {
  9430. offset = 2;
  9431. } else {
  9432. offset = 4;
  9433. }
  9434. break;
  9435. default:
  9436. cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
  9437. return; /* Never happens. Keep compiler happy. */
  9438. }
  9439. if (new_mode == ARM_CPU_MODE_MON) {
  9440. addr += env->cp15.mvbar;
  9441. } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
  9442. /* High vectors. When enabled, base address cannot be remapped. */
  9443. addr += 0xffff0000;
  9444. } else {
  9445. /*
  9446. * ARM v7 architectures provide a vector base address register to remap
  9447. * the interrupt vector table.
  9448. * This register is only followed in non-monitor mode, and is banked.
  9449. * Note: only bits 31:5 are valid.
  9450. */
  9451. addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
  9452. }
  9453. if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
  9454. env->cp15.scr_el3 &= ~SCR_NS;
  9455. }
  9456. take_aarch32_exception(env, new_mode, mask, offset, addr);
  9457. }
  9458. static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
  9459. {
  9460. /*
  9461. * Return the register number of the AArch64 view of the AArch32
  9462. * register @aarch32_reg. The CPUARMState CPSR is assumed to still
  9463. * be that of the AArch32 mode the exception came from.
  9464. */
  9465. int mode = env->uncached_cpsr & CPSR_M;
  9466. switch (aarch32_reg) {
  9467. case 0 ... 7:
  9468. return aarch32_reg;
  9469. case 8 ... 12:
  9470. return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
  9471. case 13:
  9472. switch (mode) {
  9473. case ARM_CPU_MODE_USR:
  9474. case ARM_CPU_MODE_SYS:
  9475. return 13;
  9476. case ARM_CPU_MODE_HYP:
  9477. return 15;
  9478. case ARM_CPU_MODE_IRQ:
  9479. return 17;
  9480. case ARM_CPU_MODE_SVC:
  9481. return 19;
  9482. case ARM_CPU_MODE_ABT:
  9483. return 21;
  9484. case ARM_CPU_MODE_UND:
  9485. return 23;
  9486. case ARM_CPU_MODE_FIQ:
  9487. return 29;
  9488. default:
  9489. g_assert_not_reached();
  9490. }
  9491. case 14:
  9492. switch (mode) {
  9493. case ARM_CPU_MODE_USR:
  9494. case ARM_CPU_MODE_SYS:
  9495. case ARM_CPU_MODE_HYP:
  9496. return 14;
  9497. case ARM_CPU_MODE_IRQ:
  9498. return 16;
  9499. case ARM_CPU_MODE_SVC:
  9500. return 18;
  9501. case ARM_CPU_MODE_ABT:
  9502. return 20;
  9503. case ARM_CPU_MODE_UND:
  9504. return 22;
  9505. case ARM_CPU_MODE_FIQ:
  9506. return 30;
  9507. default:
  9508. g_assert_not_reached();
  9509. }
  9510. case 15:
  9511. return 31;
  9512. default:
  9513. g_assert_not_reached();
  9514. }
  9515. }
  9516. static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
  9517. {
  9518. uint32_t ret = cpsr_read(env);
  9519. /* Move DIT to the correct location for SPSR_ELx */
  9520. if (ret & CPSR_DIT) {
  9521. ret &= ~CPSR_DIT;
  9522. ret |= PSTATE_DIT;
  9523. }
  9524. /* Merge PSTATE.SS into SPSR_ELx */
  9525. ret |= env->pstate & PSTATE_SS;
  9526. return ret;
  9527. }
  9528. static bool syndrome_is_sync_extabt(uint32_t syndrome)
  9529. {
  9530. /* Return true if this syndrome value is a synchronous external abort */
  9531. switch (syn_get_ec(syndrome)) {
  9532. case EC_INSNABORT:
  9533. case EC_INSNABORT_SAME_EL:
  9534. case EC_DATAABORT:
  9535. case EC_DATAABORT_SAME_EL:
  9536. /* Look at fault status code for all the synchronous ext abort cases */
  9537. switch (syndrome & 0x3f) {
  9538. case 0x10:
  9539. case 0x13:
  9540. case 0x14:
  9541. case 0x15:
  9542. case 0x16:
  9543. case 0x17:
  9544. return true;
  9545. default:
  9546. return false;
  9547. }
  9548. default:
  9549. return false;
  9550. }
  9551. }
  9552. /* Handle exception entry to a target EL which is using AArch64 */
  9553. static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
  9554. {
  9555. ARMCPU *cpu = ARM_CPU(cs);
  9556. CPUARMState *env = &cpu->env;
  9557. unsigned int new_el = env->exception.target_el;
  9558. target_ulong addr = env->cp15.vbar_el[new_el];
  9559. unsigned int new_mode = aarch64_pstate_mode(new_el, true);
  9560. unsigned int old_mode;
  9561. unsigned int cur_el = arm_current_el(env);
  9562. int rt;
  9563. if (tcg_enabled()) {
  9564. /*
  9565. * Note that new_el can never be 0. If cur_el is 0, then
  9566. * el0_a64 is is_a64(), else el0_a64 is ignored.
  9567. */
  9568. aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
  9569. }
  9570. if (cur_el < new_el) {
  9571. /*
  9572. * Entry vector offset depends on whether the implemented EL
  9573. * immediately lower than the target level is using AArch32 or AArch64
  9574. */
  9575. bool is_aa64;
  9576. uint64_t hcr;
  9577. switch (new_el) {
  9578. case 3:
  9579. is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
  9580. break;
  9581. case 2:
  9582. hcr = arm_hcr_el2_eff(env);
  9583. if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
  9584. is_aa64 = (hcr & HCR_RW) != 0;
  9585. break;
  9586. }
  9587. /* fall through */
  9588. case 1:
  9589. is_aa64 = is_a64(env);
  9590. break;
  9591. default:
  9592. g_assert_not_reached();
  9593. }
  9594. if (is_aa64) {
  9595. addr += 0x400;
  9596. } else {
  9597. addr += 0x600;
  9598. }
  9599. } else if (pstate_read(env) & PSTATE_SP) {
  9600. addr += 0x200;
  9601. }
  9602. switch (cs->exception_index) {
  9603. case EXCP_GPC:
  9604. qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
  9605. env->cp15.mfar_el3);
  9606. /* fall through */
  9607. case EXCP_PREFETCH_ABORT:
  9608. case EXCP_DATA_ABORT:
  9609. /*
  9610. * FEAT_DoubleFault allows synchronous external aborts taken to EL3
  9611. * to be taken to the SError vector entrypoint.
  9612. */
  9613. if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
  9614. syndrome_is_sync_extabt(env->exception.syndrome)) {
  9615. addr += 0x180;
  9616. }
  9617. env->cp15.far_el[new_el] = env->exception.vaddress;
  9618. qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
  9619. env->cp15.far_el[new_el]);
  9620. /* fall through */
  9621. case EXCP_BKPT:
  9622. case EXCP_UDEF:
  9623. case EXCP_SWI:
  9624. case EXCP_HVC:
  9625. case EXCP_HYP_TRAP:
  9626. case EXCP_SMC:
  9627. switch (syn_get_ec(env->exception.syndrome)) {
  9628. case EC_ADVSIMDFPACCESSTRAP:
  9629. /*
  9630. * QEMU internal FP/SIMD syndromes from AArch32 include the
  9631. * TA and coproc fields which are only exposed if the exception
  9632. * is taken to AArch32 Hyp mode. Mask them out to get a valid
  9633. * AArch64 format syndrome.
  9634. */
  9635. env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
  9636. break;
  9637. case EC_CP14RTTRAP:
  9638. case EC_CP15RTTRAP:
  9639. case EC_CP14DTTRAP:
  9640. /*
  9641. * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
  9642. * the raw register field from the insn; when taking this to
  9643. * AArch64 we must convert it to the AArch64 view of the register
  9644. * number. Notice that we read a 4-bit AArch32 register number and
  9645. * write back a 5-bit AArch64 one.
  9646. */
  9647. rt = extract32(env->exception.syndrome, 5, 4);
  9648. rt = aarch64_regnum(env, rt);
  9649. env->exception.syndrome = deposit32(env->exception.syndrome,
  9650. 5, 5, rt);
  9651. break;
  9652. case EC_CP15RRTTRAP:
  9653. case EC_CP14RRTTRAP:
  9654. /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
  9655. rt = extract32(env->exception.syndrome, 5, 4);
  9656. rt = aarch64_regnum(env, rt);
  9657. env->exception.syndrome = deposit32(env->exception.syndrome,
  9658. 5, 5, rt);
  9659. rt = extract32(env->exception.syndrome, 10, 4);
  9660. rt = aarch64_regnum(env, rt);
  9661. env->exception.syndrome = deposit32(env->exception.syndrome,
  9662. 10, 5, rt);
  9663. break;
  9664. }
  9665. env->cp15.esr_el[new_el] = env->exception.syndrome;
  9666. break;
  9667. case EXCP_IRQ:
  9668. case EXCP_VIRQ:
  9669. case EXCP_NMI:
  9670. case EXCP_VINMI:
  9671. addr += 0x80;
  9672. break;
  9673. case EXCP_FIQ:
  9674. case EXCP_VFIQ:
  9675. case EXCP_VFNMI:
  9676. addr += 0x100;
  9677. break;
  9678. case EXCP_VSERR:
  9679. addr += 0x180;
  9680. /* Construct the SError syndrome from IDS and ISS fields. */
  9681. env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
  9682. env->cp15.esr_el[new_el] = env->exception.syndrome;
  9683. break;
  9684. default:
  9685. cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
  9686. }
  9687. if (is_a64(env)) {
  9688. old_mode = pstate_read(env);
  9689. aarch64_save_sp(env, arm_current_el(env));
  9690. env->elr_el[new_el] = env->pc;
  9691. if (cur_el == 1 && new_el == 1) {
  9692. uint64_t hcr = arm_hcr_el2_eff(env);
  9693. if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
  9694. (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
  9695. /*
  9696. * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
  9697. * by setting M[3:2] to 0b10.
  9698. * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
  9699. * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
  9700. */
  9701. old_mode = deposit32(old_mode, 2, 2, 2);
  9702. }
  9703. }
  9704. } else {
  9705. old_mode = cpsr_read_for_spsr_elx(env);
  9706. env->elr_el[new_el] = env->regs[15];
  9707. aarch64_sync_32_to_64(env);
  9708. env->condexec_bits = 0;
  9709. }
  9710. env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
  9711. qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
  9712. qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
  9713. env->elr_el[new_el]);
  9714. if (cpu_isar_feature(aa64_pan, cpu)) {
  9715. /* The value of PSTATE.PAN is normally preserved, except when ... */
  9716. new_mode |= old_mode & PSTATE_PAN;
  9717. switch (new_el) {
  9718. case 2:
  9719. /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
  9720. if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
  9721. != (HCR_E2H | HCR_TGE)) {
  9722. break;
  9723. }
  9724. /* fall through */
  9725. case 1:
  9726. /* ... the target is EL1 ... */
  9727. /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
  9728. if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
  9729. new_mode |= PSTATE_PAN;
  9730. }
  9731. break;
  9732. }
  9733. }
  9734. if (cpu_isar_feature(aa64_mte, cpu)) {
  9735. new_mode |= PSTATE_TCO;
  9736. }
  9737. if (cpu_isar_feature(aa64_ssbs, cpu)) {
  9738. if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
  9739. new_mode |= PSTATE_SSBS;
  9740. } else {
  9741. new_mode &= ~PSTATE_SSBS;
  9742. }
  9743. }
  9744. if (cpu_isar_feature(aa64_nmi, cpu)) {
  9745. if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
  9746. new_mode |= PSTATE_ALLINT;
  9747. } else {
  9748. new_mode &= ~PSTATE_ALLINT;
  9749. }
  9750. }
  9751. pstate_write(env, PSTATE_DAIF | new_mode);
  9752. env->aarch64 = true;
  9753. aarch64_restore_sp(env, new_el);
  9754. if (tcg_enabled()) {
  9755. helper_rebuild_hflags_a64(env, new_el);
  9756. }
  9757. env->pc = addr;
  9758. qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
  9759. new_el, env->pc, pstate_read(env));
  9760. }
  9761. /*
  9762. * Do semihosting call and set the appropriate return value. All the
  9763. * permission and validity checks have been done at translate time.
  9764. *
  9765. * We only see semihosting exceptions in TCG only as they are not
  9766. * trapped to the hypervisor in KVM.
  9767. */
  9768. #ifdef CONFIG_TCG
  9769. static void tcg_handle_semihosting(CPUState *cs)
  9770. {
  9771. ARMCPU *cpu = ARM_CPU(cs);
  9772. CPUARMState *env = &cpu->env;
  9773. if (is_a64(env)) {
  9774. qemu_log_mask(CPU_LOG_INT,
  9775. "...handling as semihosting call 0x%" PRIx64 "\n",
  9776. env->xregs[0]);
  9777. do_common_semihosting(cs);
  9778. env->pc += 4;
  9779. } else {
  9780. qemu_log_mask(CPU_LOG_INT,
  9781. "...handling as semihosting call 0x%x\n",
  9782. env->regs[0]);
  9783. do_common_semihosting(cs);
  9784. env->regs[15] += env->thumb ? 2 : 4;
  9785. }
  9786. }
  9787. #endif
  9788. /*
  9789. * Handle a CPU exception for A and R profile CPUs.
  9790. * Do any appropriate logging, handle PSCI calls, and then hand off
  9791. * to the AArch64-entry or AArch32-entry function depending on the
  9792. * target exception level's register width.
  9793. *
  9794. * Note: this is used for both TCG (as the do_interrupt tcg op),
  9795. * and KVM to re-inject guest debug exceptions, and to
  9796. * inject a Synchronous-External-Abort.
  9797. */
  9798. void arm_cpu_do_interrupt(CPUState *cs)
  9799. {
  9800. ARMCPU *cpu = ARM_CPU(cs);
  9801. CPUARMState *env = &cpu->env;
  9802. unsigned int new_el = env->exception.target_el;
  9803. assert(!arm_feature(env, ARM_FEATURE_M));
  9804. arm_log_exception(cs);
  9805. qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
  9806. new_el);
  9807. if (qemu_loglevel_mask(CPU_LOG_INT)
  9808. && !excp_is_internal(cs->exception_index)) {
  9809. qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
  9810. syn_get_ec(env->exception.syndrome),
  9811. env->exception.syndrome);
  9812. }
  9813. if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
  9814. arm_handle_psci_call(cpu);
  9815. qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
  9816. return;
  9817. }
  9818. /*
  9819. * Semihosting semantics depend on the register width of the code
  9820. * that caused the exception, not the target exception level, so
  9821. * must be handled here.
  9822. */
  9823. #ifdef CONFIG_TCG
  9824. if (cs->exception_index == EXCP_SEMIHOST) {
  9825. tcg_handle_semihosting(cs);
  9826. return;
  9827. }
  9828. #endif
  9829. /*
  9830. * Hooks may change global state so BQL should be held, also the
  9831. * BQL needs to be held for any modification of
  9832. * cs->interrupt_request.
  9833. */
  9834. g_assert(bql_locked());
  9835. arm_call_pre_el_change_hook(cpu);
  9836. assert(!excp_is_internal(cs->exception_index));
  9837. if (arm_el_is_aa64(env, new_el)) {
  9838. arm_cpu_do_interrupt_aarch64(cs);
  9839. } else {
  9840. arm_cpu_do_interrupt_aarch32(cs);
  9841. }
  9842. arm_call_el_change_hook(cpu);
  9843. if (!kvm_enabled()) {
  9844. cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
  9845. }
  9846. }
  9847. #endif /* !CONFIG_USER_ONLY */
  9848. uint64_t arm_sctlr(CPUARMState *env, int el)
  9849. {
  9850. /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
  9851. if (el == 0) {
  9852. ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
  9853. switch (mmu_idx) {
  9854. case ARMMMUIdx_E20_0:
  9855. el = 2;
  9856. break;
  9857. case ARMMMUIdx_E30_0:
  9858. el = 3;
  9859. break;
  9860. default:
  9861. el = 1;
  9862. break;
  9863. }
  9864. }
  9865. return env->cp15.sctlr_el[el];
  9866. }
  9867. int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
  9868. {
  9869. if (regime_has_2_ranges(mmu_idx)) {
  9870. return extract64(tcr, 37, 2);
  9871. } else if (regime_is_stage2(mmu_idx)) {
  9872. return 0; /* VTCR_EL2 */
  9873. } else {
  9874. /* Replicate the single TBI bit so we always have 2 bits. */
  9875. return extract32(tcr, 20, 1) * 3;
  9876. }
  9877. }
  9878. int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
  9879. {
  9880. if (regime_has_2_ranges(mmu_idx)) {
  9881. return extract64(tcr, 51, 2);
  9882. } else if (regime_is_stage2(mmu_idx)) {
  9883. return 0; /* VTCR_EL2 */
  9884. } else {
  9885. /* Replicate the single TBID bit so we always have 2 bits. */
  9886. return extract32(tcr, 29, 1) * 3;
  9887. }
  9888. }
  9889. int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
  9890. {
  9891. if (regime_has_2_ranges(mmu_idx)) {
  9892. return extract64(tcr, 57, 2);
  9893. } else {
  9894. /* Replicate the single TCMA bit so we always have 2 bits. */
  9895. return extract32(tcr, 30, 1) * 3;
  9896. }
  9897. }
  9898. static ARMGranuleSize tg0_to_gran_size(int tg)
  9899. {
  9900. switch (tg) {
  9901. case 0:
  9902. return Gran4K;
  9903. case 1:
  9904. return Gran64K;
  9905. case 2:
  9906. return Gran16K;
  9907. default:
  9908. return GranInvalid;
  9909. }
  9910. }
  9911. static ARMGranuleSize tg1_to_gran_size(int tg)
  9912. {
  9913. switch (tg) {
  9914. case 1:
  9915. return Gran16K;
  9916. case 2:
  9917. return Gran4K;
  9918. case 3:
  9919. return Gran64K;
  9920. default:
  9921. return GranInvalid;
  9922. }
  9923. }
  9924. static inline bool have4k(ARMCPU *cpu, bool stage2)
  9925. {
  9926. return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
  9927. : cpu_isar_feature(aa64_tgran4, cpu);
  9928. }
  9929. static inline bool have16k(ARMCPU *cpu, bool stage2)
  9930. {
  9931. return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
  9932. : cpu_isar_feature(aa64_tgran16, cpu);
  9933. }
  9934. static inline bool have64k(ARMCPU *cpu, bool stage2)
  9935. {
  9936. return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
  9937. : cpu_isar_feature(aa64_tgran64, cpu);
  9938. }
  9939. static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
  9940. bool stage2)
  9941. {
  9942. switch (gran) {
  9943. case Gran4K:
  9944. if (have4k(cpu, stage2)) {
  9945. return gran;
  9946. }
  9947. break;
  9948. case Gran16K:
  9949. if (have16k(cpu, stage2)) {
  9950. return gran;
  9951. }
  9952. break;
  9953. case Gran64K:
  9954. if (have64k(cpu, stage2)) {
  9955. return gran;
  9956. }
  9957. break;
  9958. case GranInvalid:
  9959. break;
  9960. }
  9961. /*
  9962. * If the guest selects a granule size that isn't implemented,
  9963. * the architecture requires that we behave as if it selected one
  9964. * that is (with an IMPDEF choice of which one to pick). We choose
  9965. * to implement the smallest supported granule size.
  9966. */
  9967. if (have4k(cpu, stage2)) {
  9968. return Gran4K;
  9969. }
  9970. if (have16k(cpu, stage2)) {
  9971. return Gran16K;
  9972. }
  9973. assert(have64k(cpu, stage2));
  9974. return Gran64K;
  9975. }
  9976. ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
  9977. ARMMMUIdx mmu_idx, bool data,
  9978. bool el1_is_aa32)
  9979. {
  9980. uint64_t tcr = regime_tcr(env, mmu_idx);
  9981. bool epd, hpd, tsz_oob, ds, ha, hd;
  9982. int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
  9983. ARMGranuleSize gran;
  9984. ARMCPU *cpu = env_archcpu(env);
  9985. bool stage2 = regime_is_stage2(mmu_idx);
  9986. if (!regime_has_2_ranges(mmu_idx)) {
  9987. select = 0;
  9988. tsz = extract32(tcr, 0, 6);
  9989. gran = tg0_to_gran_size(extract32(tcr, 14, 2));
  9990. if (stage2) {
  9991. /* VTCR_EL2 */
  9992. hpd = false;
  9993. } else {
  9994. hpd = extract32(tcr, 24, 1);
  9995. }
  9996. epd = false;
  9997. sh = extract32(tcr, 12, 2);
  9998. ps = extract32(tcr, 16, 3);
  9999. ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
  10000. hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
  10001. ds = extract64(tcr, 32, 1);
  10002. } else {
  10003. bool e0pd;
  10004. /*
  10005. * Bit 55 is always between the two regions, and is canonical for
  10006. * determining if address tagging is enabled.
  10007. */
  10008. select = extract64(va, 55, 1);
  10009. if (!select) {
  10010. tsz = extract32(tcr, 0, 6);
  10011. gran = tg0_to_gran_size(extract32(tcr, 14, 2));
  10012. epd = extract32(tcr, 7, 1);
  10013. sh = extract32(tcr, 12, 2);
  10014. hpd = extract64(tcr, 41, 1);
  10015. e0pd = extract64(tcr, 55, 1);
  10016. } else {
  10017. tsz = extract32(tcr, 16, 6);
  10018. gran = tg1_to_gran_size(extract32(tcr, 30, 2));
  10019. epd = extract32(tcr, 23, 1);
  10020. sh = extract32(tcr, 28, 2);
  10021. hpd = extract64(tcr, 42, 1);
  10022. e0pd = extract64(tcr, 56, 1);
  10023. }
  10024. ps = extract64(tcr, 32, 3);
  10025. ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
  10026. hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
  10027. ds = extract64(tcr, 59, 1);
  10028. if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
  10029. regime_is_user(env, mmu_idx)) {
  10030. epd = true;
  10031. }
  10032. }
  10033. gran = sanitize_gran_size(cpu, gran, stage2);
  10034. if (cpu_isar_feature(aa64_st, cpu)) {
  10035. max_tsz = 48 - (gran == Gran64K);
  10036. } else {
  10037. max_tsz = 39;
  10038. }
  10039. /*
  10040. * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
  10041. * adjust the effective value of DS, as documented.
  10042. */
  10043. min_tsz = 16;
  10044. if (gran == Gran64K) {
  10045. if (cpu_isar_feature(aa64_lva, cpu)) {
  10046. min_tsz = 12;
  10047. }
  10048. ds = false;
  10049. } else if (ds) {
  10050. if (regime_is_stage2(mmu_idx)) {
  10051. if (gran == Gran16K) {
  10052. ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
  10053. } else {
  10054. ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
  10055. }
  10056. } else {
  10057. if (gran == Gran16K) {
  10058. ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
  10059. } else {
  10060. ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
  10061. }
  10062. }
  10063. if (ds) {
  10064. min_tsz = 12;
  10065. }
  10066. }
  10067. if (stage2 && el1_is_aa32) {
  10068. /*
  10069. * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
  10070. * are loosened: a configured IPA of 40 bits is permitted even if
  10071. * the implemented PA is less than that (and so a 40 bit IPA would
  10072. * fault for an AArch64 EL1). See R_DTLMN.
  10073. */
  10074. min_tsz = MIN(min_tsz, 24);
  10075. }
  10076. if (tsz > max_tsz) {
  10077. tsz = max_tsz;
  10078. tsz_oob = true;
  10079. } else if (tsz < min_tsz) {
  10080. tsz = min_tsz;
  10081. tsz_oob = true;
  10082. } else {
  10083. tsz_oob = false;
  10084. }
  10085. /* Present TBI as a composite with TBID. */
  10086. tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
  10087. if (!data) {
  10088. tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
  10089. }
  10090. tbi = (tbi >> select) & 1;
  10091. return (ARMVAParameters) {
  10092. .tsz = tsz,
  10093. .ps = ps,
  10094. .sh = sh,
  10095. .select = select,
  10096. .tbi = tbi,
  10097. .epd = epd,
  10098. .hpd = hpd,
  10099. .tsz_oob = tsz_oob,
  10100. .ds = ds,
  10101. .ha = ha,
  10102. .hd = ha && hd,
  10103. .gran = gran,
  10104. };
  10105. }
  10106. /*
  10107. * Return the exception level to which FP-disabled exceptions should
  10108. * be taken, or 0 if FP is enabled.
  10109. */
  10110. int fp_exception_el(CPUARMState *env, int cur_el)
  10111. {
  10112. #ifndef CONFIG_USER_ONLY
  10113. uint64_t hcr_el2;
  10114. /*
  10115. * CPACR and the CPTR registers don't exist before v6, so FP is
  10116. * always accessible
  10117. */
  10118. if (!arm_feature(env, ARM_FEATURE_V6)) {
  10119. return 0;
  10120. }
  10121. if (arm_feature(env, ARM_FEATURE_M)) {
  10122. /* CPACR can cause a NOCP UsageFault taken to current security state */
  10123. if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
  10124. return 1;
  10125. }
  10126. if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
  10127. if (!extract32(env->v7m.nsacr, 10, 1)) {
  10128. /* FP insns cause a NOCP UsageFault taken to Secure */
  10129. return 3;
  10130. }
  10131. }
  10132. return 0;
  10133. }
  10134. hcr_el2 = arm_hcr_el2_eff(env);
  10135. /*
  10136. * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
  10137. * 0, 2 : trap EL0 and EL1/PL1 accesses
  10138. * 1 : trap only EL0 accesses
  10139. * 3 : trap no accesses
  10140. * This register is ignored if E2H+TGE are both set.
  10141. */
  10142. if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
  10143. int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
  10144. switch (fpen) {
  10145. case 1:
  10146. if (cur_el != 0) {
  10147. break;
  10148. }
  10149. /* fall through */
  10150. case 0:
  10151. case 2:
  10152. /* Trap from Secure PL0 or PL1 to Secure PL1. */
  10153. if (!arm_el_is_aa64(env, 3)
  10154. && (cur_el == 3 || arm_is_secure_below_el3(env))) {
  10155. return 3;
  10156. }
  10157. if (cur_el <= 1) {
  10158. return 1;
  10159. }
  10160. break;
  10161. }
  10162. }
  10163. /*
  10164. * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
  10165. * to control non-secure access to the FPU. It doesn't have any
  10166. * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
  10167. */
  10168. if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
  10169. cur_el <= 2 && !arm_is_secure_below_el3(env))) {
  10170. if (!extract32(env->cp15.nsacr, 10, 1)) {
  10171. /* FP insns act as UNDEF */
  10172. return cur_el == 2 ? 2 : 1;
  10173. }
  10174. }
  10175. /*
  10176. * CPTR_EL2 is present in v7VE or v8, and changes format
  10177. * with HCR_EL2.E2H (regardless of TGE).
  10178. */
  10179. if (cur_el <= 2) {
  10180. if (hcr_el2 & HCR_E2H) {
  10181. switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
  10182. case 1:
  10183. if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
  10184. break;
  10185. }
  10186. /* fall through */
  10187. case 0:
  10188. case 2:
  10189. return 2;
  10190. }
  10191. } else if (arm_is_el2_enabled(env)) {
  10192. if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
  10193. return 2;
  10194. }
  10195. }
  10196. }
  10197. /* CPTR_EL3 : present in v8 */
  10198. if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
  10199. /* Trap all FP ops to EL3 */
  10200. return 3;
  10201. }
  10202. #endif
  10203. return 0;
  10204. }
  10205. /* Return the exception level we're running at if this is our mmu_idx */
  10206. int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
  10207. {
  10208. if (mmu_idx & ARM_MMU_IDX_M) {
  10209. return mmu_idx & ARM_MMU_IDX_M_PRIV;
  10210. }
  10211. switch (mmu_idx) {
  10212. case ARMMMUIdx_E10_0:
  10213. case ARMMMUIdx_E20_0:
  10214. case ARMMMUIdx_E30_0:
  10215. return 0;
  10216. case ARMMMUIdx_E10_1:
  10217. case ARMMMUIdx_E10_1_PAN:
  10218. return 1;
  10219. case ARMMMUIdx_E2:
  10220. case ARMMMUIdx_E20_2:
  10221. case ARMMMUIdx_E20_2_PAN:
  10222. return 2;
  10223. case ARMMMUIdx_E3:
  10224. case ARMMMUIdx_E30_3_PAN:
  10225. return 3;
  10226. default:
  10227. g_assert_not_reached();
  10228. }
  10229. }
  10230. #ifndef CONFIG_TCG
  10231. ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
  10232. {
  10233. g_assert_not_reached();
  10234. }
  10235. #endif
  10236. ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
  10237. {
  10238. ARMMMUIdx idx;
  10239. uint64_t hcr;
  10240. if (arm_feature(env, ARM_FEATURE_M)) {
  10241. return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
  10242. }
  10243. /* See ARM pseudo-function ELIsInHost. */
  10244. switch (el) {
  10245. case 0:
  10246. hcr = arm_hcr_el2_eff(env);
  10247. if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
  10248. idx = ARMMMUIdx_E20_0;
  10249. } else if (arm_is_secure_below_el3(env) &&
  10250. !arm_el_is_aa64(env, 3)) {
  10251. idx = ARMMMUIdx_E30_0;
  10252. } else {
  10253. idx = ARMMMUIdx_E10_0;
  10254. }
  10255. break;
  10256. case 1:
  10257. if (arm_pan_enabled(env)) {
  10258. idx = ARMMMUIdx_E10_1_PAN;
  10259. } else {
  10260. idx = ARMMMUIdx_E10_1;
  10261. }
  10262. break;
  10263. case 2:
  10264. /* Note that TGE does not apply at EL2. */
  10265. if (arm_hcr_el2_eff(env) & HCR_E2H) {
  10266. if (arm_pan_enabled(env)) {
  10267. idx = ARMMMUIdx_E20_2_PAN;
  10268. } else {
  10269. idx = ARMMMUIdx_E20_2;
  10270. }
  10271. } else {
  10272. idx = ARMMMUIdx_E2;
  10273. }
  10274. break;
  10275. case 3:
  10276. if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
  10277. return ARMMMUIdx_E30_3_PAN;
  10278. }
  10279. return ARMMMUIdx_E3;
  10280. default:
  10281. g_assert_not_reached();
  10282. }
  10283. return idx;
  10284. }
  10285. ARMMMUIdx arm_mmu_idx(CPUARMState *env)
  10286. {
  10287. return arm_mmu_idx_el(env, arm_current_el(env));
  10288. }
  10289. static bool mve_no_pred(CPUARMState *env)
  10290. {
  10291. /*
  10292. * Return true if there is definitely no predication of MVE
  10293. * instructions by VPR or LTPSIZE. (Returning false even if there
  10294. * isn't any predication is OK; generated code will just be
  10295. * a little worse.)
  10296. * If the CPU does not implement MVE then this TB flag is always 0.
  10297. *
  10298. * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
  10299. * logic in gen_update_fp_context() needs to be updated to match.
  10300. *
  10301. * We do not include the effect of the ECI bits here -- they are
  10302. * tracked in other TB flags. This simplifies the logic for
  10303. * "when did we emit code that changes the MVE_NO_PRED TB flag
  10304. * and thus need to end the TB?".
  10305. */
  10306. if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
  10307. return false;
  10308. }
  10309. if (env->v7m.vpr) {
  10310. return false;
  10311. }
  10312. if (env->v7m.ltpsize < 4) {
  10313. return false;
  10314. }
  10315. return true;
  10316. }
  10317. void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
  10318. uint64_t *cs_base, uint32_t *pflags)
  10319. {
  10320. CPUARMTBFlags flags;
  10321. assert_hflags_rebuild_correctly(env);
  10322. flags = env->hflags;
  10323. if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
  10324. *pc = env->pc;
  10325. if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
  10326. DP_TBFLAG_A64(flags, BTYPE, env->btype);
  10327. }
  10328. } else {
  10329. *pc = env->regs[15];
  10330. if (arm_feature(env, ARM_FEATURE_M)) {
  10331. if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
  10332. FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
  10333. != env->v7m.secure) {
  10334. DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
  10335. }
  10336. if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
  10337. (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
  10338. (env->v7m.secure &&
  10339. !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
  10340. /*
  10341. * ASPEN is set, but FPCA/SFPA indicate that there is no
  10342. * active FP context; we must create a new FP context before
  10343. * executing any FP insn.
  10344. */
  10345. DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
  10346. }
  10347. bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
  10348. if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
  10349. DP_TBFLAG_M32(flags, LSPACT, 1);
  10350. }
  10351. if (mve_no_pred(env)) {
  10352. DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
  10353. }
  10354. } else {
  10355. /*
  10356. * Note that XSCALE_CPAR shares bits with VECSTRIDE.
  10357. * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
  10358. */
  10359. if (arm_feature(env, ARM_FEATURE_XSCALE)) {
  10360. DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
  10361. } else {
  10362. DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
  10363. DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
  10364. }
  10365. if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
  10366. DP_TBFLAG_A32(flags, VFPEN, 1);
  10367. }
  10368. }
  10369. DP_TBFLAG_AM32(flags, THUMB, env->thumb);
  10370. DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
  10371. }
  10372. /*
  10373. * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
  10374. * states defined in the ARM ARM for software singlestep:
  10375. * SS_ACTIVE PSTATE.SS State
  10376. * 0 x Inactive (the TB flag for SS is always 0)
  10377. * 1 0 Active-pending
  10378. * 1 1 Active-not-pending
  10379. * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
  10380. */
  10381. if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
  10382. DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
  10383. }
  10384. *pflags = flags.flags;
  10385. *cs_base = flags.flags2;
  10386. }
  10387. #ifdef TARGET_AARCH64
  10388. /*
  10389. * The manual says that when SVE is enabled and VQ is widened the
  10390. * implementation is allowed to zero the previously inaccessible
  10391. * portion of the registers. The corollary to that is that when
  10392. * SVE is enabled and VQ is narrowed we are also allowed to zero
  10393. * the now inaccessible portion of the registers.
  10394. *
  10395. * The intent of this is that no predicate bit beyond VQ is ever set.
  10396. * Which means that some operations on predicate registers themselves
  10397. * may operate on full uint64_t or even unrolled across the maximum
  10398. * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
  10399. * may well be cheaper than conditionals to restrict the operation
  10400. * to the relevant portion of a uint16_t[16].
  10401. */
  10402. void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
  10403. {
  10404. int i, j;
  10405. uint64_t pmask;
  10406. assert(vq >= 1 && vq <= ARM_MAX_VQ);
  10407. assert(vq <= env_archcpu(env)->sve_max_vq);
  10408. /* Zap the high bits of the zregs. */
  10409. for (i = 0; i < 32; i++) {
  10410. memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
  10411. }
  10412. /* Zap the high bits of the pregs and ffr. */
  10413. pmask = 0;
  10414. if (vq & 3) {
  10415. pmask = ~(-1ULL << (16 * (vq & 3)));
  10416. }
  10417. for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
  10418. for (i = 0; i < 17; ++i) {
  10419. env->vfp.pregs[i].p[j] &= pmask;
  10420. }
  10421. pmask = 0;
  10422. }
  10423. }
  10424. static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
  10425. {
  10426. int exc_el;
  10427. if (sm) {
  10428. exc_el = sme_exception_el(env, el);
  10429. } else {
  10430. exc_el = sve_exception_el(env, el);
  10431. }
  10432. if (exc_el) {
  10433. return 0; /* disabled */
  10434. }
  10435. return sve_vqm1_for_el_sm(env, el, sm);
  10436. }
  10437. /*
  10438. * Notice a change in SVE vector size when changing EL.
  10439. */
  10440. void aarch64_sve_change_el(CPUARMState *env, int old_el,
  10441. int new_el, bool el0_a64)
  10442. {
  10443. ARMCPU *cpu = env_archcpu(env);
  10444. int old_len, new_len;
  10445. bool old_a64, new_a64, sm;
  10446. /* Nothing to do if no SVE. */
  10447. if (!cpu_isar_feature(aa64_sve, cpu)) {
  10448. return;
  10449. }
  10450. /* Nothing to do if FP is disabled in either EL. */
  10451. if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
  10452. return;
  10453. }
  10454. old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
  10455. new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
  10456. /*
  10457. * Both AArch64.TakeException and AArch64.ExceptionReturn
  10458. * invoke ResetSVEState when taking an exception from, or
  10459. * returning to, AArch32 state when PSTATE.SM is enabled.
  10460. */
  10461. sm = FIELD_EX64(env->svcr, SVCR, SM);
  10462. if (old_a64 != new_a64 && sm) {
  10463. arm_reset_sve_state(env);
  10464. return;
  10465. }
  10466. /*
  10467. * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
  10468. * at ELx, or not available because the EL is in AArch32 state, then
  10469. * for all purposes other than a direct read, the ZCR_ELx.LEN field
  10470. * has an effective value of 0".
  10471. *
  10472. * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
  10473. * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
  10474. * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
  10475. * we already have the correct register contents when encountering the
  10476. * vq0->vq0 transition between EL0->EL1.
  10477. */
  10478. old_len = new_len = 0;
  10479. if (old_a64) {
  10480. old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
  10481. }
  10482. if (new_a64) {
  10483. new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
  10484. }
  10485. /* When changing vector length, clear inaccessible state. */
  10486. if (new_len < old_len) {
  10487. aarch64_sve_narrow_vq(env, new_len + 1);
  10488. }
  10489. }
  10490. #endif
  10491. #ifndef CONFIG_USER_ONLY
  10492. ARMSecuritySpace arm_security_space(CPUARMState *env)
  10493. {
  10494. if (arm_feature(env, ARM_FEATURE_M)) {
  10495. return arm_secure_to_space(env->v7m.secure);
  10496. }
  10497. /*
  10498. * If EL3 is not supported then the secure state is implementation
  10499. * defined, in which case QEMU defaults to non-secure.
  10500. */
  10501. if (!arm_feature(env, ARM_FEATURE_EL3)) {
  10502. return ARMSS_NonSecure;
  10503. }
  10504. /* Check for AArch64 EL3 or AArch32 Mon. */
  10505. if (is_a64(env)) {
  10506. if (extract32(env->pstate, 2, 2) == 3) {
  10507. if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
  10508. return ARMSS_Root;
  10509. } else {
  10510. return ARMSS_Secure;
  10511. }
  10512. }
  10513. } else {
  10514. if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
  10515. return ARMSS_Secure;
  10516. }
  10517. }
  10518. return arm_security_space_below_el3(env);
  10519. }
  10520. ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
  10521. {
  10522. assert(!arm_feature(env, ARM_FEATURE_M));
  10523. /*
  10524. * If EL3 is not supported then the secure state is implementation
  10525. * defined, in which case QEMU defaults to non-secure.
  10526. */
  10527. if (!arm_feature(env, ARM_FEATURE_EL3)) {
  10528. return ARMSS_NonSecure;
  10529. }
  10530. /*
  10531. * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
  10532. * Ignoring NSE when !NS retains consistency without having to
  10533. * modify other predicates.
  10534. */
  10535. if (!(env->cp15.scr_el3 & SCR_NS)) {
  10536. return ARMSS_Secure;
  10537. } else if (env->cp15.scr_el3 & SCR_NSE) {
  10538. return ARMSS_Realm;
  10539. } else {
  10540. return ARMSS_NonSecure;
  10541. }
  10542. }
  10543. #endif /* !CONFIG_USER_ONLY */