xref: /openbmc/qemu/target/arm/helper.c (revision fe1a3ace13a8b53fc20c74fb7e3337f754396e6b)
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 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "exec/page-protection.h"
17 #include "exec/mmap-lock.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/timer.h"
20 #include "qemu/bitops.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/cputlb.h"
23 #include "exec/exec-all.h"
24 #include "exec/translation-block.h"
25 #include "hw/irq.h"
26 #include "system/cpu-timers.h"
27 #include "exec/icount.h"
28 #include "system/kvm.h"
29 #include "system/tcg.h"
30 #include "qapi/error.h"
31 #include "qemu/guest-random.h"
32 #ifdef CONFIG_TCG
33 #include "accel/tcg/probe.h"
34 #include "semihosting/common-semi.h"
35 #endif
36 #include "cpregs.h"
37 #include "target/arm/gtimer.h"
38 
39 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
40 
41 static void switch_mode(CPUARMState *env, int mode);
42 
43 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
44 {
45     assert(ri->fieldoffset);
46     if (cpreg_field_is_64bit(ri)) {
47         return CPREG_FIELD64(env, ri);
48     } else {
49         return CPREG_FIELD32(env, ri);
50     }
51 }
52 
53 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
54 {
55     assert(ri->fieldoffset);
56     if (cpreg_field_is_64bit(ri)) {
57         CPREG_FIELD64(env, ri) = value;
58     } else {
59         CPREG_FIELD32(env, ri) = value;
60     }
61 }
62 
63 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
64 {
65     return (char *)env + ri->fieldoffset;
66 }
67 
68 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
69 {
70     /* Raw read of a coprocessor register (as needed for migration, etc). */
71     if (ri->type & ARM_CP_CONST) {
72         return ri->resetvalue;
73     } else if (ri->raw_readfn) {
74         return ri->raw_readfn(env, ri);
75     } else if (ri->readfn) {
76         return ri->readfn(env, ri);
77     } else {
78         return raw_read(env, ri);
79     }
80 }
81 
82 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
83                              uint64_t v)
84 {
85     /*
86      * Raw write of a coprocessor register (as needed for migration, etc).
87      * Note that constant registers are treated as write-ignored; the
88      * caller should check for success by whether a readback gives the
89      * value written.
90      */
91     if (ri->type & ARM_CP_CONST) {
92         return;
93     } else if (ri->raw_writefn) {
94         ri->raw_writefn(env, ri, v);
95     } else if (ri->writefn) {
96         ri->writefn(env, ri, v);
97     } else {
98         raw_write(env, ri, v);
99     }
100 }
101 
102 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
103 {
104    /*
105     * Return true if the regdef would cause an assertion if you called
106     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
107     * program bug for it not to have the NO_RAW flag).
108     * NB that returning false here doesn't necessarily mean that calling
109     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
110     * read/write access functions which are safe for raw use" from "has
111     * read/write access functions which have side effects but has forgotten
112     * to provide raw access functions".
113     * The tests here line up with the conditions in read/write_raw_cp_reg()
114     * and assertions in raw_read()/raw_write().
115     */
116     if ((ri->type & ARM_CP_CONST) ||
117         ri->fieldoffset ||
118         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
119         return false;
120     }
121     return true;
122 }
123 
124 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
125 {
126     /* Write the coprocessor state from cpu->env to the (index,value) list. */
127     int i;
128     bool ok = true;
129 
130     for (i = 0; i < cpu->cpreg_array_len; i++) {
131         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
132         const ARMCPRegInfo *ri;
133         uint64_t newval;
134 
135         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
136         if (!ri) {
137             ok = false;
138             continue;
139         }
140         if (ri->type & ARM_CP_NO_RAW) {
141             continue;
142         }
143 
144         newval = read_raw_cp_reg(&cpu->env, ri);
145         if (kvm_sync) {
146             /*
147              * Only sync if the previous list->cpustate sync succeeded.
148              * Rather than tracking the success/failure state for every
149              * item in the list, we just recheck "does the raw write we must
150              * have made in write_list_to_cpustate() read back OK" here.
151              */
152             uint64_t oldval = cpu->cpreg_values[i];
153 
154             if (oldval == newval) {
155                 continue;
156             }
157 
158             write_raw_cp_reg(&cpu->env, ri, oldval);
159             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
160                 continue;
161             }
162 
163             write_raw_cp_reg(&cpu->env, ri, newval);
164         }
165         cpu->cpreg_values[i] = newval;
166     }
167     return ok;
168 }
169 
170 bool write_list_to_cpustate(ARMCPU *cpu)
171 {
172     int i;
173     bool ok = true;
174 
175     for (i = 0; i < cpu->cpreg_array_len; i++) {
176         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
177         uint64_t v = cpu->cpreg_values[i];
178         const ARMCPRegInfo *ri;
179 
180         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
181         if (!ri) {
182             ok = false;
183             continue;
184         }
185         if (ri->type & ARM_CP_NO_RAW) {
186             continue;
187         }
188         /*
189          * Write value and confirm it reads back as written
190          * (to catch read-only registers and partially read-only
191          * registers where the incoming migration value doesn't match)
192          */
193         write_raw_cp_reg(&cpu->env, ri, v);
194         if (read_raw_cp_reg(&cpu->env, ri) != v) {
195             ok = false;
196         }
197     }
198     return ok;
199 }
200 
201 static void add_cpreg_to_list(gpointer key, gpointer opaque)
202 {
203     ARMCPU *cpu = opaque;
204     uint32_t regidx = (uintptr_t)key;
205     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
206 
207     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
208         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
209         /* The value array need not be initialized at this point */
210         cpu->cpreg_array_len++;
211     }
212 }
213 
214 static void count_cpreg(gpointer key, gpointer opaque)
215 {
216     ARMCPU *cpu = opaque;
217     const ARMCPRegInfo *ri;
218 
219     ri = g_hash_table_lookup(cpu->cp_regs, key);
220 
221     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
222         cpu->cpreg_array_len++;
223     }
224 }
225 
226 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
227 {
228     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
229     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
230 
231     if (aidx > bidx) {
232         return 1;
233     }
234     if (aidx < bidx) {
235         return -1;
236     }
237     return 0;
238 }
239 
240 void init_cpreg_list(ARMCPU *cpu)
241 {
242     /*
243      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
244      * Note that we require cpreg_tuples[] to be sorted by key ID.
245      */
246     GList *keys;
247     int arraylen;
248 
249     keys = g_hash_table_get_keys(cpu->cp_regs);
250     keys = g_list_sort(keys, cpreg_key_compare);
251 
252     cpu->cpreg_array_len = 0;
253 
254     g_list_foreach(keys, count_cpreg, cpu);
255 
256     arraylen = cpu->cpreg_array_len;
257     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
258     cpu->cpreg_values = g_new(uint64_t, arraylen);
259     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
260     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
261     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
262     cpu->cpreg_array_len = 0;
263 
264     g_list_foreach(keys, add_cpreg_to_list, cpu);
265 
266     assert(cpu->cpreg_array_len == arraylen);
267 
268     g_list_free(keys);
269 }
270 
271 static bool arm_pan_enabled(CPUARMState *env)
272 {
273     if (is_a64(env)) {
274         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
275             return false;
276         }
277         return env->pstate & PSTATE_PAN;
278     } else {
279         return env->uncached_cpsr & CPSR_PAN;
280     }
281 }
282 
283 /*
284  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
285  */
286 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
287                                         const ARMCPRegInfo *ri,
288                                         bool isread)
289 {
290     if (!is_a64(env) && arm_current_el(env) == 3 &&
291         arm_is_secure_below_el3(env)) {
292         return CP_ACCESS_UNDEFINED;
293     }
294     return CP_ACCESS_OK;
295 }
296 
297 /*
298  * Some secure-only AArch32 registers trap to EL3 if used from
299  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
300  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
301  * We assume that the .access field is set to PL1_RW.
302  */
303 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
304                                             const ARMCPRegInfo *ri,
305                                             bool isread)
306 {
307     if (arm_current_el(env) == 3) {
308         return CP_ACCESS_OK;
309     }
310     if (arm_is_secure_below_el3(env)) {
311         if (env->cp15.scr_el3 & SCR_EEL2) {
312             return CP_ACCESS_TRAP_EL2;
313         }
314         return CP_ACCESS_TRAP_EL3;
315     }
316     /* This will be EL1 NS and EL2 NS, which just UNDEF */
317     return CP_ACCESS_UNDEFINED;
318 }
319 
320 /*
321  * Check for traps to performance monitor registers, which are controlled
322  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
323  */
324 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
325                                  bool isread)
326 {
327     int el = arm_current_el(env);
328     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
329 
330     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
331         return CP_ACCESS_TRAP_EL2;
332     }
333     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
334         return CP_ACCESS_TRAP_EL3;
335     }
336     return CP_ACCESS_OK;
337 }
338 
339 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
340 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
341                                bool isread)
342 {
343     if (arm_current_el(env) == 1) {
344         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
345         if (arm_hcr_el2_eff(env) & trap) {
346             return CP_ACCESS_TRAP_EL2;
347         }
348     }
349     return CP_ACCESS_OK;
350 }
351 
352 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
353 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
354                                  bool isread)
355 {
356     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
357         return CP_ACCESS_TRAP_EL2;
358     }
359     return CP_ACCESS_OK;
360 }
361 
362 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
363 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
364                                   bool isread)
365 {
366     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
367         return CP_ACCESS_TRAP_EL2;
368     }
369     return CP_ACCESS_OK;
370 }
371 
372 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
373 {
374     ARMCPU *cpu = env_archcpu(env);
375 
376     raw_write(env, ri, value);
377     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
378 }
379 
380 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
381 {
382     ARMCPU *cpu = env_archcpu(env);
383 
384     if (raw_read(env, ri) != value) {
385         /*
386          * Unlike real hardware the qemu TLB uses virtual addresses,
387          * not modified virtual addresses, so this causes a TLB flush.
388          */
389         tlb_flush(CPU(cpu));
390         raw_write(env, ri, value);
391     }
392 }
393 
394 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
395                              uint64_t value)
396 {
397     ARMCPU *cpu = env_archcpu(env);
398 
399     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
400         && !extended_addresses_enabled(env)) {
401         /*
402          * For VMSA (when not using the LPAE long descriptor page table
403          * format) this register includes the ASID, so do a TLB flush.
404          * For PMSA it is purely a process ID and no action is needed.
405          */
406         tlb_flush(CPU(cpu));
407     }
408     raw_write(env, ri, value);
409 }
410 
411 int alle1_tlbmask(CPUARMState *env)
412 {
413     /*
414      * Note that the 'ALL' scope must invalidate both stage 1 and
415      * stage 2 translations, whereas most other scopes only invalidate
416      * stage 1 translations.
417      *
418      * For AArch32 this is only used for TLBIALLNSNH and VTTBR
419      * writes, so only needs to apply to NS PL1&0, not S PL1&0.
420      */
421     return (ARMMMUIdxBit_E10_1 |
422             ARMMMUIdxBit_E10_1_PAN |
423             ARMMMUIdxBit_E10_0 |
424             ARMMMUIdxBit_Stage2 |
425             ARMMMUIdxBit_Stage2_S);
426 }
427 
428 static const ARMCPRegInfo cp_reginfo[] = {
429     /*
430      * Define the secure and non-secure FCSE identifier CP registers
431      * separately because there is no secure bank in V8 (no _EL3).  This allows
432      * the secure register to be properly reset and migrated. There is also no
433      * v8 EL1 version of the register so the non-secure instance stands alone.
434      */
435     { .name = "FCSEIDR",
436       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
437       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
438       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
439       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
440     { .name = "FCSEIDR_S",
441       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
442       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
443       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
444       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
445     /*
446      * Define the secure and non-secure context identifier CP registers
447      * separately because there is no secure bank in V8 (no _EL3).  This allows
448      * the secure register to be properly reset and migrated.  In the
449      * non-secure case, the 32-bit register will have reset and migration
450      * disabled during registration as it is handled by the 64-bit instance.
451      */
452     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
453       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
454       .access = PL1_RW, .accessfn = access_tvm_trvm,
455       .fgt = FGT_CONTEXTIDR_EL1,
456       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
457       .secure = ARM_CP_SECSTATE_NS,
458       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
459       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
460     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
461       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
462       .access = PL1_RW, .accessfn = access_tvm_trvm,
463       .secure = ARM_CP_SECSTATE_S,
464       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
465       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
466 };
467 
468 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
469     /*
470      * NB: Some of these registers exist in v8 but with more precise
471      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
472      */
473     /* MMU Domain access control / MPU write buffer control */
474     { .name = "DACR",
475       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
476       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
477       .writefn = dacr_write, .raw_writefn = raw_write,
478       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
479                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
480     /*
481      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
482      * For v6 and v5, these mappings are overly broad.
483      */
484     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
485       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
486     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
487       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
488     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
489       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
490     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
491       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
492     /* Cache maintenance ops; some of this space may be overridden later. */
493     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
494       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
495       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
496 };
497 
498 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
499     /*
500      * Not all pre-v6 cores implemented this WFI, so this is slightly
501      * over-broad.
502      */
503     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
504       .access = PL1_W, .type = ARM_CP_WFI },
505 };
506 
507 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
508     /*
509      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
510      * is UNPREDICTABLE; we choose to NOP as most implementations do).
511      */
512     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
513       .access = PL1_W, .type = ARM_CP_WFI },
514     /*
515      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
516      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
517      * OMAPCP will override this space.
518      */
519     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
520       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
521       .resetvalue = 0 },
522     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
523       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
524       .resetvalue = 0 },
525     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
526     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
527       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
528       .resetvalue = 0 },
529     /*
530      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
531      * implementing it as RAZ means the "debug architecture version" bits
532      * will read as a reserved value, which should cause Linux to not try
533      * to use the debug hardware.
534      */
535     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
536       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
537     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
538       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
539     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
540       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
541 };
542 
543 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
544                         uint64_t value)
545 {
546     uint32_t mask = 0;
547 
548     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
549     if (!arm_feature(env, ARM_FEATURE_V8)) {
550         /*
551          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
552          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
553          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
554          */
555         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
556             /* VFP coprocessor: cp10 & cp11 [23:20] */
557             mask |= R_CPACR_ASEDIS_MASK |
558                     R_CPACR_D32DIS_MASK |
559                     R_CPACR_CP11_MASK |
560                     R_CPACR_CP10_MASK;
561 
562             if (!arm_feature(env, ARM_FEATURE_NEON)) {
563                 /* ASEDIS [31] bit is RAO/WI */
564                 value |= R_CPACR_ASEDIS_MASK;
565             }
566 
567             /*
568              * VFPv3 and upwards with NEON implement 32 double precision
569              * registers (D0-D31).
570              */
571             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
572                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
573                 value |= R_CPACR_D32DIS_MASK;
574             }
575         }
576         value &= mask;
577     }
578 
579     /*
580      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
581      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
582      */
583     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
584         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
585         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
586         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
587     }
588 
589     env->cp15.cpacr_el1 = value;
590 }
591 
592 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
593 {
594     /*
595      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
596      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
597      */
598     uint64_t value = env->cp15.cpacr_el1;
599 
600     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
601         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
602         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
603     }
604     return value;
605 }
606 
607 
608 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
609 {
610     /*
611      * Call cpacr_write() so that we reset with the correct RAO bits set
612      * for our CPU features.
613      */
614     cpacr_write(env, ri, 0);
615 }
616 
617 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
618                                    bool isread)
619 {
620     if (arm_feature(env, ARM_FEATURE_V8)) {
621         /* Check if CPACR accesses are to be trapped to EL2 */
622         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
623             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
624             return CP_ACCESS_TRAP_EL2;
625         /* Check if CPACR accesses are to be trapped to EL3 */
626         } else if (arm_current_el(env) < 3 &&
627                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
628             return CP_ACCESS_TRAP_EL3;
629         }
630     }
631 
632     return CP_ACCESS_OK;
633 }
634 
635 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
636                                   bool isread)
637 {
638     /* Check if CPTR accesses are set to trap to EL3 */
639     if (arm_current_el(env) == 2 &&
640         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
641         return CP_ACCESS_TRAP_EL3;
642     }
643 
644     return CP_ACCESS_OK;
645 }
646 
647 static const ARMCPRegInfo v6_cp_reginfo[] = {
648     /* prefetch by MVA in v6, NOP in v7 */
649     { .name = "MVA_prefetch",
650       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
651       .access = PL1_W, .type = ARM_CP_NOP },
652     /*
653      * We need to break the TB after ISB to execute self-modifying code
654      * correctly and also to take any pending interrupts immediately.
655      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
656      */
657     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
658       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
659     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
660       .access = PL0_W, .type = ARM_CP_NOP },
661     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
662       .access = PL0_W, .type = ARM_CP_NOP },
663     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
664       .access = PL1_RW, .accessfn = access_tvm_trvm,
665       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
666                              offsetof(CPUARMState, cp15.ifar_ns) },
667       .resetvalue = 0, },
668     /*
669      * Watchpoint Fault Address Register : should actually only be present
670      * for 1136, 1176, 11MPCore.
671      */
672     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
673       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
674     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
675       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
676       .fgt = FGT_CPACR_EL1,
677       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
678       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
679       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
680 };
681 
682 typedef struct pm_event {
683     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
684     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
685     bool (*supported)(CPUARMState *);
686     /*
687      * Retrieve the current count of the underlying event. The programmed
688      * counters hold a difference from the return value from this function
689      */
690     uint64_t (*get_count)(CPUARMState *);
691     /*
692      * Return how many nanoseconds it will take (at a minimum) for count events
693      * to occur. A negative value indicates the counter will never overflow, or
694      * that the counter has otherwise arranged for the overflow bit to be set
695      * and the PMU interrupt to be raised on overflow.
696      */
697     int64_t (*ns_per_count)(uint64_t);
698 } pm_event;
699 
700 static bool event_always_supported(CPUARMState *env)
701 {
702     return true;
703 }
704 
705 static uint64_t swinc_get_count(CPUARMState *env)
706 {
707     /*
708      * SW_INCR events are written directly to the pmevcntr's by writes to
709      * PMSWINC, so there is no underlying count maintained by the PMU itself
710      */
711     return 0;
712 }
713 
714 static int64_t swinc_ns_per(uint64_t ignored)
715 {
716     return -1;
717 }
718 
719 /*
720  * Return the underlying cycle count for the PMU cycle counters. If we're in
721  * usermode, simply return 0.
722  */
723 static uint64_t cycles_get_count(CPUARMState *env)
724 {
725 #ifndef CONFIG_USER_ONLY
726     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
727                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
728 #else
729     return cpu_get_host_ticks();
730 #endif
731 }
732 
733 #ifndef CONFIG_USER_ONLY
734 static int64_t cycles_ns_per(uint64_t cycles)
735 {
736     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
737 }
738 
739 static bool instructions_supported(CPUARMState *env)
740 {
741     /* Precise instruction counting */
742     return icount_enabled() == ICOUNT_PRECISE;
743 }
744 
745 static uint64_t instructions_get_count(CPUARMState *env)
746 {
747     assert(icount_enabled() == ICOUNT_PRECISE);
748     return (uint64_t)icount_get_raw();
749 }
750 
751 static int64_t instructions_ns_per(uint64_t icount)
752 {
753     assert(icount_enabled() == ICOUNT_PRECISE);
754     return icount_to_ns((int64_t)icount);
755 }
756 #endif
757 
758 static bool pmuv3p1_events_supported(CPUARMState *env)
759 {
760     /* For events which are supported in any v8.1 PMU */
761     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
762 }
763 
764 static bool pmuv3p4_events_supported(CPUARMState *env)
765 {
766     /* For events which are supported in any v8.1 PMU */
767     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
768 }
769 
770 static uint64_t zero_event_get_count(CPUARMState *env)
771 {
772     /* For events which on QEMU never fire, so their count is always zero */
773     return 0;
774 }
775 
776 static int64_t zero_event_ns_per(uint64_t cycles)
777 {
778     /* An event which never fires can never overflow */
779     return -1;
780 }
781 
782 static const pm_event pm_events[] = {
783     { .number = 0x000, /* SW_INCR */
784       .supported = event_always_supported,
785       .get_count = swinc_get_count,
786       .ns_per_count = swinc_ns_per,
787     },
788 #ifndef CONFIG_USER_ONLY
789     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
790       .supported = instructions_supported,
791       .get_count = instructions_get_count,
792       .ns_per_count = instructions_ns_per,
793     },
794     { .number = 0x011, /* CPU_CYCLES, Cycle */
795       .supported = event_always_supported,
796       .get_count = cycles_get_count,
797       .ns_per_count = cycles_ns_per,
798     },
799 #endif
800     { .number = 0x023, /* STALL_FRONTEND */
801       .supported = pmuv3p1_events_supported,
802       .get_count = zero_event_get_count,
803       .ns_per_count = zero_event_ns_per,
804     },
805     { .number = 0x024, /* STALL_BACKEND */
806       .supported = pmuv3p1_events_supported,
807       .get_count = zero_event_get_count,
808       .ns_per_count = zero_event_ns_per,
809     },
810     { .number = 0x03c, /* STALL */
811       .supported = pmuv3p4_events_supported,
812       .get_count = zero_event_get_count,
813       .ns_per_count = zero_event_ns_per,
814     },
815 };
816 
817 /*
818  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
819  * events (i.e. the statistical profiling extension), this implementation
820  * should first be updated to something sparse instead of the current
821  * supported_event_map[] array.
822  */
823 #define MAX_EVENT_ID 0x3c
824 #define UNSUPPORTED_EVENT UINT16_MAX
825 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
826 
827 /*
828  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
829  * of ARM event numbers to indices in our pm_events array.
830  *
831  * Note: Events in the 0x40XX range are not currently supported.
832  */
833 void pmu_init(ARMCPU *cpu)
834 {
835     unsigned int i;
836 
837     /*
838      * Empty supported_event_map and cpu->pmceid[01] before adding supported
839      * events to them
840      */
841     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
842         supported_event_map[i] = UNSUPPORTED_EVENT;
843     }
844     cpu->pmceid0 = 0;
845     cpu->pmceid1 = 0;
846 
847     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
848         const pm_event *cnt = &pm_events[i];
849         assert(cnt->number <= MAX_EVENT_ID);
850         /* We do not currently support events in the 0x40xx range */
851         assert(cnt->number <= 0x3f);
852 
853         if (cnt->supported(&cpu->env)) {
854             supported_event_map[cnt->number] = i;
855             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
856             if (cnt->number & 0x20) {
857                 cpu->pmceid1 |= event_mask;
858             } else {
859                 cpu->pmceid0 |= event_mask;
860             }
861         }
862     }
863 }
864 
865 /*
866  * Check at runtime whether a PMU event is supported for the current machine
867  */
868 static bool event_supported(uint16_t number)
869 {
870     if (number > MAX_EVENT_ID) {
871         return false;
872     }
873     return supported_event_map[number] != UNSUPPORTED_EVENT;
874 }
875 
876 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
877                                    bool isread)
878 {
879     /*
880      * Performance monitor registers user accessibility is controlled
881      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
882      * trapping to EL2 or EL3 for other accesses.
883      */
884     int el = arm_current_el(env);
885     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
886 
887     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
888         return CP_ACCESS_TRAP_EL1;
889     }
890     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
891         return CP_ACCESS_TRAP_EL2;
892     }
893     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
894         return CP_ACCESS_TRAP_EL3;
895     }
896 
897     return CP_ACCESS_OK;
898 }
899 
900 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
901                                            const ARMCPRegInfo *ri,
902                                            bool isread)
903 {
904     /* ER: event counter read trap control */
905     if (arm_feature(env, ARM_FEATURE_V8)
906         && arm_current_el(env) == 0
907         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
908         && isread) {
909         return CP_ACCESS_OK;
910     }
911 
912     return pmreg_access(env, ri, isread);
913 }
914 
915 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
916                                          const ARMCPRegInfo *ri,
917                                          bool isread)
918 {
919     /* SW: software increment write trap control */
920     if (arm_feature(env, ARM_FEATURE_V8)
921         && arm_current_el(env) == 0
922         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
923         && !isread) {
924         return CP_ACCESS_OK;
925     }
926 
927     return pmreg_access(env, ri, isread);
928 }
929 
930 static CPAccessResult pmreg_access_selr(CPUARMState *env,
931                                         const ARMCPRegInfo *ri,
932                                         bool isread)
933 {
934     /* ER: event counter read trap control */
935     if (arm_feature(env, ARM_FEATURE_V8)
936         && arm_current_el(env) == 0
937         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
938         return CP_ACCESS_OK;
939     }
940 
941     return pmreg_access(env, ri, isread);
942 }
943 
944 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
945                                          const ARMCPRegInfo *ri,
946                                          bool isread)
947 {
948     /* CR: cycle counter read trap control */
949     if (arm_feature(env, ARM_FEATURE_V8)
950         && arm_current_el(env) == 0
951         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
952         && isread) {
953         return CP_ACCESS_OK;
954     }
955 
956     return pmreg_access(env, ri, isread);
957 }
958 
959 /*
960  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
961  * We use these to decide whether we need to wrap a write to MDCR_EL2
962  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
963  */
964 #define MDCR_EL2_PMU_ENABLE_BITS \
965     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
966 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
967 
968 /*
969  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
970  * the current EL, security state, and register configuration.
971  */
972 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
973 {
974     uint64_t filter;
975     bool e, p, u, nsk, nsu, nsh, m;
976     bool enabled, prohibited = false, filtered;
977     bool secure = arm_is_secure(env);
978     int el = arm_current_el(env);
979     uint64_t mdcr_el2;
980     uint8_t hpmn;
981 
982     /*
983      * We might be called for M-profile cores where MDCR_EL2 doesn't
984      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
985      * must be before we read that value.
986      */
987     if (!arm_feature(env, ARM_FEATURE_PMU)) {
988         return false;
989     }
990 
991     mdcr_el2 = arm_mdcr_el2_eff(env);
992     hpmn = mdcr_el2 & MDCR_HPMN;
993 
994     if (!arm_feature(env, ARM_FEATURE_EL2) ||
995             (counter < hpmn || counter == 31)) {
996         e = env->cp15.c9_pmcr & PMCRE;
997     } else {
998         e = mdcr_el2 & MDCR_HPME;
999     }
1000     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1001 
1002     /* Is event counting prohibited? */
1003     if (el == 2 && (counter < hpmn || counter == 31)) {
1004         prohibited = mdcr_el2 & MDCR_HPMD;
1005     }
1006     if (secure) {
1007         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1008     }
1009 
1010     if (counter == 31) {
1011         /*
1012          * The cycle counter defaults to running. PMCR.DP says "disable
1013          * the cycle counter when event counting is prohibited".
1014          * Some MDCR bits disable the cycle counter specifically.
1015          */
1016         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1017         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1018             if (secure) {
1019                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1020             }
1021             if (el == 2) {
1022                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1023             }
1024         }
1025     }
1026 
1027     if (counter == 31) {
1028         filter = env->cp15.pmccfiltr_el0;
1029     } else {
1030         filter = env->cp15.c14_pmevtyper[counter];
1031     }
1032 
1033     p   = filter & PMXEVTYPER_P;
1034     u   = filter & PMXEVTYPER_U;
1035     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1036     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1037     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1038     m   = arm_el_is_aa64(env, 1) &&
1039               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1040 
1041     if (el == 0) {
1042         filtered = secure ? u : u != nsu;
1043     } else if (el == 1) {
1044         filtered = secure ? p : p != nsk;
1045     } else if (el == 2) {
1046         filtered = !nsh;
1047     } else { /* EL3 */
1048         filtered = m != p;
1049     }
1050 
1051     if (counter != 31) {
1052         /*
1053          * If not checking PMCCNTR, ensure the counter is setup to an event we
1054          * support
1055          */
1056         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1057         if (!event_supported(event)) {
1058             return false;
1059         }
1060     }
1061 
1062     return enabled && !prohibited && !filtered;
1063 }
1064 
1065 static void pmu_update_irq(CPUARMState *env)
1066 {
1067     ARMCPU *cpu = env_archcpu(env);
1068     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1069             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1070 }
1071 
1072 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1073 {
1074     /*
1075      * Return true if the clock divider is enabled and the cycle counter
1076      * is supposed to tick only once every 64 clock cycles. This is
1077      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1078      * (64-bit) cycle counter PMCR.D has no effect.
1079      */
1080     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1081 }
1082 
1083 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1084 {
1085     /* Return true if the specified event counter is configured to be 64 bit */
1086 
1087     /* This isn't intended to be used with the cycle counter */
1088     assert(counter < 31);
1089 
1090     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1091         return false;
1092     }
1093 
1094     if (arm_feature(env, ARM_FEATURE_EL2)) {
1095         /*
1096          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1097          * current security state, so we don't use arm_mdcr_el2_eff() here.
1098          */
1099         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1100         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1101 
1102         if (counter >= hpmn) {
1103             return hlp;
1104         }
1105     }
1106     return env->cp15.c9_pmcr & PMCRLP;
1107 }
1108 
1109 /*
1110  * Ensure c15_ccnt is the guest-visible count so that operations such as
1111  * enabling/disabling the counter or filtering, modifying the count itself,
1112  * etc. can be done logically. This is essentially a no-op if the counter is
1113  * not enabled at the time of the call.
1114  */
1115 static void pmccntr_op_start(CPUARMState *env)
1116 {
1117     uint64_t cycles = cycles_get_count(env);
1118 
1119     if (pmu_counter_enabled(env, 31)) {
1120         uint64_t eff_cycles = cycles;
1121         if (pmccntr_clockdiv_enabled(env)) {
1122             eff_cycles /= 64;
1123         }
1124 
1125         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1126 
1127         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1128                                  1ull << 63 : 1ull << 31;
1129         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1130             env->cp15.c9_pmovsr |= (1ULL << 31);
1131             pmu_update_irq(env);
1132         }
1133 
1134         env->cp15.c15_ccnt = new_pmccntr;
1135     }
1136     env->cp15.c15_ccnt_delta = cycles;
1137 }
1138 
1139 /*
1140  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1141  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1142  * pmccntr_op_start.
1143  */
1144 static void pmccntr_op_finish(CPUARMState *env)
1145 {
1146     if (pmu_counter_enabled(env, 31)) {
1147 #ifndef CONFIG_USER_ONLY
1148         /* Calculate when the counter will next overflow */
1149         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1150         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1151             remaining_cycles = (uint32_t)remaining_cycles;
1152         }
1153         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1154 
1155         if (overflow_in > 0) {
1156             int64_t overflow_at;
1157 
1158             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1159                                  overflow_in, &overflow_at)) {
1160                 ARMCPU *cpu = env_archcpu(env);
1161                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1162             }
1163         }
1164 #endif
1165 
1166         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1167         if (pmccntr_clockdiv_enabled(env)) {
1168             prev_cycles /= 64;
1169         }
1170         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1171     }
1172 }
1173 
1174 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1175 {
1176 
1177     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1178     uint64_t count = 0;
1179     if (event_supported(event)) {
1180         uint16_t event_idx = supported_event_map[event];
1181         count = pm_events[event_idx].get_count(env);
1182     }
1183 
1184     if (pmu_counter_enabled(env, counter)) {
1185         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1186         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1187             1ULL << 63 : 1ULL << 31;
1188 
1189         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1190             env->cp15.c9_pmovsr |= (1 << counter);
1191             pmu_update_irq(env);
1192         }
1193         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1194     }
1195     env->cp15.c14_pmevcntr_delta[counter] = count;
1196 }
1197 
1198 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1199 {
1200     if (pmu_counter_enabled(env, counter)) {
1201 #ifndef CONFIG_USER_ONLY
1202         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1203         uint16_t event_idx = supported_event_map[event];
1204         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1205         int64_t overflow_in;
1206 
1207         if (!pmevcntr_is_64_bit(env, counter)) {
1208             delta = (uint32_t)delta;
1209         }
1210         overflow_in = pm_events[event_idx].ns_per_count(delta);
1211 
1212         if (overflow_in > 0) {
1213             int64_t overflow_at;
1214 
1215             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1216                                  overflow_in, &overflow_at)) {
1217                 ARMCPU *cpu = env_archcpu(env);
1218                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1219             }
1220         }
1221 #endif
1222 
1223         env->cp15.c14_pmevcntr_delta[counter] -=
1224             env->cp15.c14_pmevcntr[counter];
1225     }
1226 }
1227 
1228 void pmu_op_start(CPUARMState *env)
1229 {
1230     unsigned int i;
1231     pmccntr_op_start(env);
1232     for (i = 0; i < pmu_num_counters(env); i++) {
1233         pmevcntr_op_start(env, i);
1234     }
1235 }
1236 
1237 void pmu_op_finish(CPUARMState *env)
1238 {
1239     unsigned int i;
1240     pmccntr_op_finish(env);
1241     for (i = 0; i < pmu_num_counters(env); i++) {
1242         pmevcntr_op_finish(env, i);
1243     }
1244 }
1245 
1246 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1247 {
1248     pmu_op_start(&cpu->env);
1249 }
1250 
1251 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1252 {
1253     pmu_op_finish(&cpu->env);
1254 }
1255 
1256 void arm_pmu_timer_cb(void *opaque)
1257 {
1258     ARMCPU *cpu = opaque;
1259 
1260     /*
1261      * Update all the counter values based on the current underlying counts,
1262      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1263      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1264      * counter may expire.
1265      */
1266     pmu_op_start(&cpu->env);
1267     pmu_op_finish(&cpu->env);
1268 }
1269 
1270 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1271                        uint64_t value)
1272 {
1273     pmu_op_start(env);
1274 
1275     if (value & PMCRC) {
1276         /* The counter has been reset */
1277         env->cp15.c15_ccnt = 0;
1278     }
1279 
1280     if (value & PMCRP) {
1281         unsigned int i;
1282         for (i = 0; i < pmu_num_counters(env); i++) {
1283             env->cp15.c14_pmevcntr[i] = 0;
1284         }
1285     }
1286 
1287     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1288     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1289 
1290     pmu_op_finish(env);
1291 }
1292 
1293 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1294 {
1295     uint64_t pmcr = env->cp15.c9_pmcr;
1296 
1297     /*
1298      * If EL2 is implemented and enabled for the current security state, reads
1299      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1300      */
1301     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1302         pmcr &= ~PMCRN_MASK;
1303         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1304     }
1305 
1306     return pmcr;
1307 }
1308 
1309 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1310                           uint64_t value)
1311 {
1312     unsigned int i;
1313     uint64_t overflow_mask, new_pmswinc;
1314 
1315     for (i = 0; i < pmu_num_counters(env); i++) {
1316         /* Increment a counter's count iff: */
1317         if ((value & (1 << i)) && /* counter's bit is set */
1318                 /* counter is enabled and not filtered */
1319                 pmu_counter_enabled(env, i) &&
1320                 /* counter is SW_INCR */
1321                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1322             pmevcntr_op_start(env, i);
1323 
1324             /*
1325              * Detect if this write causes an overflow since we can't predict
1326              * PMSWINC overflows like we can for other events
1327              */
1328             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1329 
1330             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1331                 1ULL << 63 : 1ULL << 31;
1332 
1333             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1334                 env->cp15.c9_pmovsr |= (1 << i);
1335                 pmu_update_irq(env);
1336             }
1337 
1338             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1339 
1340             pmevcntr_op_finish(env, i);
1341         }
1342     }
1343 }
1344 
1345 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1346 {
1347     uint64_t ret;
1348     pmccntr_op_start(env);
1349     ret = env->cp15.c15_ccnt;
1350     pmccntr_op_finish(env);
1351     return ret;
1352 }
1353 
1354 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1355                          uint64_t value)
1356 {
1357     /*
1358      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1359      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1360      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1361      * accessed.
1362      */
1363     env->cp15.c9_pmselr = value & 0x1f;
1364 }
1365 
1366 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1367                         uint64_t value)
1368 {
1369     pmccntr_op_start(env);
1370     env->cp15.c15_ccnt = value;
1371     pmccntr_op_finish(env);
1372 }
1373 
1374 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1375                             uint64_t value)
1376 {
1377     uint64_t cur_val = pmccntr_read(env, NULL);
1378 
1379     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1380 }
1381 
1382 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1383                             uint64_t value)
1384 {
1385     pmccntr_op_start(env);
1386     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1387     pmccntr_op_finish(env);
1388 }
1389 
1390 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1391                             uint64_t value)
1392 {
1393     pmccntr_op_start(env);
1394     /* M is not accessible from AArch32 */
1395     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1396         (value & PMCCFILTR);
1397     pmccntr_op_finish(env);
1398 }
1399 
1400 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1401 {
1402     /* M is not visible in AArch32 */
1403     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1404 }
1405 
1406 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1407                             uint64_t value)
1408 {
1409     pmu_op_start(env);
1410     value &= pmu_counter_mask(env);
1411     env->cp15.c9_pmcnten |= value;
1412     pmu_op_finish(env);
1413 }
1414 
1415 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1416                              uint64_t value)
1417 {
1418     pmu_op_start(env);
1419     value &= pmu_counter_mask(env);
1420     env->cp15.c9_pmcnten &= ~value;
1421     pmu_op_finish(env);
1422 }
1423 
1424 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1425                          uint64_t value)
1426 {
1427     value &= pmu_counter_mask(env);
1428     env->cp15.c9_pmovsr &= ~value;
1429     pmu_update_irq(env);
1430 }
1431 
1432 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1433                          uint64_t value)
1434 {
1435     value &= pmu_counter_mask(env);
1436     env->cp15.c9_pmovsr |= value;
1437     pmu_update_irq(env);
1438 }
1439 
1440 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1441                              uint64_t value, const uint8_t counter)
1442 {
1443     if (counter == 31) {
1444         pmccfiltr_write(env, ri, value);
1445     } else if (counter < pmu_num_counters(env)) {
1446         pmevcntr_op_start(env, counter);
1447 
1448         /*
1449          * If this counter's event type is changing, store the current
1450          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1451          * pmevcntr_op_finish has the correct baseline when it converts back to
1452          * a delta.
1453          */
1454         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1455             PMXEVTYPER_EVTCOUNT;
1456         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1457         if (old_event != new_event) {
1458             uint64_t count = 0;
1459             if (event_supported(new_event)) {
1460                 uint16_t event_idx = supported_event_map[new_event];
1461                 count = pm_events[event_idx].get_count(env);
1462             }
1463             env->cp15.c14_pmevcntr_delta[counter] = count;
1464         }
1465 
1466         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1467         pmevcntr_op_finish(env, counter);
1468     }
1469     /*
1470      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1471      * PMSELR value is equal to or greater than the number of implemented
1472      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1473      */
1474 }
1475 
1476 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1477                                const uint8_t counter)
1478 {
1479     if (counter == 31) {
1480         return env->cp15.pmccfiltr_el0;
1481     } else if (counter < pmu_num_counters(env)) {
1482         return env->cp15.c14_pmevtyper[counter];
1483     } else {
1484       /*
1485        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1486        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1487        */
1488         return 0;
1489     }
1490 }
1491 
1492 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1493                               uint64_t value)
1494 {
1495     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1496     pmevtyper_write(env, ri, value, counter);
1497 }
1498 
1499 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1500                                uint64_t value)
1501 {
1502     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1503     env->cp15.c14_pmevtyper[counter] = value;
1504 
1505     /*
1506      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1507      * pmu_op_finish calls when loading saved state for a migration. Because
1508      * we're potentially updating the type of event here, the value written to
1509      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1510      * different counter type. Therefore, we need to set this value to the
1511      * current count for the counter type we're writing so that pmu_op_finish
1512      * has the correct count for its calculation.
1513      */
1514     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1515     if (event_supported(event)) {
1516         uint16_t event_idx = supported_event_map[event];
1517         env->cp15.c14_pmevcntr_delta[counter] =
1518             pm_events[event_idx].get_count(env);
1519     }
1520 }
1521 
1522 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1523 {
1524     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1525     return pmevtyper_read(env, ri, counter);
1526 }
1527 
1528 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1529                              uint64_t value)
1530 {
1531     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1532 }
1533 
1534 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1535 {
1536     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1537 }
1538 
1539 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1540                              uint64_t value, uint8_t counter)
1541 {
1542     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1543         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1544         value &= MAKE_64BIT_MASK(0, 32);
1545     }
1546     if (counter < pmu_num_counters(env)) {
1547         pmevcntr_op_start(env, counter);
1548         env->cp15.c14_pmevcntr[counter] = value;
1549         pmevcntr_op_finish(env, counter);
1550     }
1551     /*
1552      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1553      * are CONSTRAINED UNPREDICTABLE.
1554      */
1555 }
1556 
1557 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1558                               uint8_t counter)
1559 {
1560     if (counter < pmu_num_counters(env)) {
1561         uint64_t ret;
1562         pmevcntr_op_start(env, counter);
1563         ret = env->cp15.c14_pmevcntr[counter];
1564         pmevcntr_op_finish(env, counter);
1565         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1566             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1567             ret &= MAKE_64BIT_MASK(0, 32);
1568         }
1569         return ret;
1570     } else {
1571       /*
1572        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1573        * are CONSTRAINED UNPREDICTABLE.
1574        */
1575         return 0;
1576     }
1577 }
1578 
1579 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1580                              uint64_t value)
1581 {
1582     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1583     pmevcntr_write(env, ri, value, counter);
1584 }
1585 
1586 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1587 {
1588     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1589     return pmevcntr_read(env, ri, counter);
1590 }
1591 
1592 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1593                              uint64_t value)
1594 {
1595     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1596     assert(counter < pmu_num_counters(env));
1597     env->cp15.c14_pmevcntr[counter] = value;
1598     pmevcntr_write(env, ri, value, counter);
1599 }
1600 
1601 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1602 {
1603     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1604     assert(counter < pmu_num_counters(env));
1605     return env->cp15.c14_pmevcntr[counter];
1606 }
1607 
1608 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1609                              uint64_t value)
1610 {
1611     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1612 }
1613 
1614 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1615 {
1616     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1617 }
1618 
1619 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1620                             uint64_t value)
1621 {
1622     if (arm_feature(env, ARM_FEATURE_V8)) {
1623         env->cp15.c9_pmuserenr = value & 0xf;
1624     } else {
1625         env->cp15.c9_pmuserenr = value & 1;
1626     }
1627 }
1628 
1629 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1630                              uint64_t value)
1631 {
1632     /* We have no event counters so only the C bit can be changed */
1633     value &= pmu_counter_mask(env);
1634     env->cp15.c9_pminten |= value;
1635     pmu_update_irq(env);
1636 }
1637 
1638 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1639                              uint64_t value)
1640 {
1641     value &= pmu_counter_mask(env);
1642     env->cp15.c9_pminten &= ~value;
1643     pmu_update_irq(env);
1644 }
1645 
1646 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1647                        uint64_t value)
1648 {
1649     /*
1650      * Note that even though the AArch64 view of this register has bits
1651      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1652      * architectural requirements for bits which are RES0 only in some
1653      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1654      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1655      */
1656     raw_write(env, ri, value & ~0x1FULL);
1657 }
1658 
1659 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1660 {
1661     /* Begin with base v8.0 state.  */
1662     uint64_t valid_mask = 0x3fff;
1663     ARMCPU *cpu = env_archcpu(env);
1664     uint64_t changed;
1665 
1666     /*
1667      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1668      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1669      * Instead, choose the format based on the mode of EL3.
1670      */
1671     if (arm_el_is_aa64(env, 3)) {
1672         value |= SCR_FW | SCR_AW;      /* RES1 */
1673         valid_mask &= ~SCR_NET;        /* RES0 */
1674 
1675         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1676             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1677             value |= SCR_RW;           /* RAO/WI */
1678         }
1679         if (cpu_isar_feature(aa64_ras, cpu)) {
1680             valid_mask |= SCR_TERR;
1681         }
1682         if (cpu_isar_feature(aa64_lor, cpu)) {
1683             valid_mask |= SCR_TLOR;
1684         }
1685         if (cpu_isar_feature(aa64_pauth, cpu)) {
1686             valid_mask |= SCR_API | SCR_APK;
1687         }
1688         if (cpu_isar_feature(aa64_sel2, cpu)) {
1689             valid_mask |= SCR_EEL2;
1690         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1691             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1692             value |= SCR_NS;
1693         }
1694         if (cpu_isar_feature(aa64_mte, cpu)) {
1695             valid_mask |= SCR_ATA;
1696         }
1697         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1698             valid_mask |= SCR_ENSCXT;
1699         }
1700         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1701             valid_mask |= SCR_EASE | SCR_NMEA;
1702         }
1703         if (cpu_isar_feature(aa64_sme, cpu)) {
1704             valid_mask |= SCR_ENTP2;
1705         }
1706         if (cpu_isar_feature(aa64_hcx, cpu)) {
1707             valid_mask |= SCR_HXEN;
1708         }
1709         if (cpu_isar_feature(aa64_fgt, cpu)) {
1710             valid_mask |= SCR_FGTEN;
1711         }
1712         if (cpu_isar_feature(aa64_rme, cpu)) {
1713             valid_mask |= SCR_NSE | SCR_GPF;
1714         }
1715         if (cpu_isar_feature(aa64_ecv, cpu)) {
1716             valid_mask |= SCR_ECVEN;
1717         }
1718     } else {
1719         valid_mask &= ~(SCR_RW | SCR_ST);
1720         if (cpu_isar_feature(aa32_ras, cpu)) {
1721             valid_mask |= SCR_TERR;
1722         }
1723     }
1724 
1725     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1726         valid_mask &= ~SCR_HCE;
1727 
1728         /*
1729          * On ARMv7, SMD (or SCD as it is called in v7) is only
1730          * supported if EL2 exists. The bit is UNK/SBZP when
1731          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1732          * when EL2 is unavailable.
1733          * On ARMv8, this bit is always available.
1734          */
1735         if (arm_feature(env, ARM_FEATURE_V7) &&
1736             !arm_feature(env, ARM_FEATURE_V8)) {
1737             valid_mask &= ~SCR_SMD;
1738         }
1739     }
1740 
1741     /* Clear all-context RES0 bits.  */
1742     value &= valid_mask;
1743     changed = env->cp15.scr_el3 ^ value;
1744     env->cp15.scr_el3 = value;
1745 
1746     /*
1747      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1748      * we must invalidate all TLBs below EL3.
1749      */
1750     if (changed & (SCR_NS | SCR_NSE)) {
1751         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1752                                            ARMMMUIdxBit_E20_0 |
1753                                            ARMMMUIdxBit_E10_1 |
1754                                            ARMMMUIdxBit_E20_2 |
1755                                            ARMMMUIdxBit_E10_1_PAN |
1756                                            ARMMMUIdxBit_E20_2_PAN |
1757                                            ARMMMUIdxBit_E2));
1758     }
1759 }
1760 
1761 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1762 {
1763     /*
1764      * scr_write will set the RES1 bits on an AArch64-only CPU.
1765      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1766      */
1767     scr_write(env, ri, 0);
1768 }
1769 
1770 static CPAccessResult access_tid4(CPUARMState *env,
1771                                   const ARMCPRegInfo *ri,
1772                                   bool isread)
1773 {
1774     if (arm_current_el(env) == 1 &&
1775         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1776         return CP_ACCESS_TRAP_EL2;
1777     }
1778 
1779     return CP_ACCESS_OK;
1780 }
1781 
1782 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1783 {
1784     ARMCPU *cpu = env_archcpu(env);
1785 
1786     /*
1787      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1788      * bank
1789      */
1790     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1791                                         ri->secure & ARM_CP_SECSTATE_S);
1792 
1793     return cpu->ccsidr[index];
1794 }
1795 
1796 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1797                          uint64_t value)
1798 {
1799     raw_write(env, ri, value & 0xf);
1800 }
1801 
1802 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1803 {
1804     CPUState *cs = env_cpu(env);
1805     bool el1 = arm_current_el(env) == 1;
1806     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1807     uint64_t ret = 0;
1808 
1809     if (hcr_el2 & HCR_IMO) {
1810         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1811             ret |= CPSR_I;
1812         }
1813         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1814             ret |= ISR_IS;
1815             ret |= CPSR_I;
1816         }
1817     } else {
1818         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1819             ret |= CPSR_I;
1820         }
1821 
1822         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1823             ret |= ISR_IS;
1824             ret |= CPSR_I;
1825         }
1826     }
1827 
1828     if (hcr_el2 & HCR_FMO) {
1829         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1830             ret |= CPSR_F;
1831         }
1832         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1833             ret |= ISR_FS;
1834             ret |= CPSR_F;
1835         }
1836     } else {
1837         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1838             ret |= CPSR_F;
1839         }
1840     }
1841 
1842     if (hcr_el2 & HCR_AMO) {
1843         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1844             ret |= CPSR_A;
1845         }
1846     }
1847 
1848     return ret;
1849 }
1850 
1851 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1852                                        bool isread)
1853 {
1854     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1855         return CP_ACCESS_TRAP_EL2;
1856     }
1857 
1858     return CP_ACCESS_OK;
1859 }
1860 
1861 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1862                                        bool isread)
1863 {
1864     if (arm_feature(env, ARM_FEATURE_V8)) {
1865         return access_aa64_tid1(env, ri, isread);
1866     }
1867 
1868     return CP_ACCESS_OK;
1869 }
1870 
1871 static const ARMCPRegInfo v7_cp_reginfo[] = {
1872     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1873     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1874       .access = PL1_W, .type = ARM_CP_NOP },
1875     /*
1876      * Performance monitors are implementation defined in v7,
1877      * but with an ARM recommended set of registers, which we
1878      * follow.
1879      *
1880      * Performance registers fall into three categories:
1881      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1882      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1883      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1884      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1885      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1886      */
1887     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1888       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1889       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1890       .writefn = pmcntenset_write,
1891       .accessfn = pmreg_access,
1892       .fgt = FGT_PMCNTEN,
1893       .raw_writefn = raw_write },
1894     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1895       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1896       .access = PL0_RW, .accessfn = pmreg_access,
1897       .fgt = FGT_PMCNTEN,
1898       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1899       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1900     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1901       .access = PL0_RW,
1902       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1903       .accessfn = pmreg_access,
1904       .fgt = FGT_PMCNTEN,
1905       .writefn = pmcntenclr_write,
1906       .type = ARM_CP_ALIAS | ARM_CP_IO },
1907     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1908       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1909       .access = PL0_RW, .accessfn = pmreg_access,
1910       .fgt = FGT_PMCNTEN,
1911       .type = ARM_CP_ALIAS | ARM_CP_IO,
1912       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1913       .writefn = pmcntenclr_write },
1914     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1915       .access = PL0_RW, .type = ARM_CP_IO,
1916       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1917       .accessfn = pmreg_access,
1918       .fgt = FGT_PMOVS,
1919       .writefn = pmovsr_write,
1920       .raw_writefn = raw_write },
1921     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1922       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1923       .access = PL0_RW, .accessfn = pmreg_access,
1924       .fgt = FGT_PMOVS,
1925       .type = ARM_CP_ALIAS | ARM_CP_IO,
1926       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1927       .writefn = pmovsr_write,
1928       .raw_writefn = raw_write },
1929     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1930       .access = PL0_W, .accessfn = pmreg_access_swinc,
1931       .fgt = FGT_PMSWINC_EL0,
1932       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1933       .writefn = pmswinc_write },
1934     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1935       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1936       .access = PL0_W, .accessfn = pmreg_access_swinc,
1937       .fgt = FGT_PMSWINC_EL0,
1938       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1939       .writefn = pmswinc_write },
1940     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1941       .access = PL0_RW, .type = ARM_CP_ALIAS,
1942       .fgt = FGT_PMSELR_EL0,
1943       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1944       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1945       .raw_writefn = raw_write},
1946     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1947       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1948       .access = PL0_RW, .accessfn = pmreg_access_selr,
1949       .fgt = FGT_PMSELR_EL0,
1950       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1951       .writefn = pmselr_write, .raw_writefn = raw_write, },
1952     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1953       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1954       .fgt = FGT_PMCCNTR_EL0,
1955       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1956       .accessfn = pmreg_access_ccntr },
1957     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1958       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1959       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1960       .fgt = FGT_PMCCNTR_EL0,
1961       .type = ARM_CP_IO,
1962       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1963       .readfn = pmccntr_read, .writefn = pmccntr_write,
1964       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1965     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1966       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1967       .access = PL0_RW, .accessfn = pmreg_access,
1968       .fgt = FGT_PMCCFILTR_EL0,
1969       .type = ARM_CP_ALIAS | ARM_CP_IO,
1970       .resetvalue = 0, },
1971     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1972       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1973       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1974       .access = PL0_RW, .accessfn = pmreg_access,
1975       .fgt = FGT_PMCCFILTR_EL0,
1976       .type = ARM_CP_IO,
1977       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1978       .resetvalue = 0, },
1979     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1980       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1981       .accessfn = pmreg_access,
1982       .fgt = FGT_PMEVTYPERN_EL0,
1983       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1984     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1985       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1986       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1987       .accessfn = pmreg_access,
1988       .fgt = FGT_PMEVTYPERN_EL0,
1989       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1990     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1991       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1992       .accessfn = pmreg_access_xevcntr,
1993       .fgt = FGT_PMEVCNTRN_EL0,
1994       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1995     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1996       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1997       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1998       .accessfn = pmreg_access_xevcntr,
1999       .fgt = FGT_PMEVCNTRN_EL0,
2000       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2001     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2002       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2003       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2004       .resetvalue = 0,
2005       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2006     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2007       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2008       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2009       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2010       .resetvalue = 0,
2011       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2012     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2013       .access = PL1_RW, .accessfn = access_tpm,
2014       .fgt = FGT_PMINTEN,
2015       .type = ARM_CP_ALIAS | ARM_CP_IO,
2016       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2017       .resetvalue = 0,
2018       .writefn = pmintenset_write, .raw_writefn = raw_write },
2019     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2020       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2021       .access = PL1_RW, .accessfn = access_tpm,
2022       .fgt = FGT_PMINTEN,
2023       .type = ARM_CP_IO,
2024       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2025       .writefn = pmintenset_write, .raw_writefn = raw_write,
2026       .resetvalue = 0x0 },
2027     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2028       .access = PL1_RW, .accessfn = access_tpm,
2029       .fgt = FGT_PMINTEN,
2030       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2031       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2032       .writefn = pmintenclr_write, },
2033     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2034       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2035       .access = PL1_RW, .accessfn = access_tpm,
2036       .fgt = FGT_PMINTEN,
2037       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2038       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2039       .writefn = pmintenclr_write },
2040     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2041       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2042       .access = PL1_R,
2043       .accessfn = access_tid4,
2044       .fgt = FGT_CCSIDR_EL1,
2045       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2046     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2047       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2048       .access = PL1_RW,
2049       .accessfn = access_tid4,
2050       .fgt = FGT_CSSELR_EL1,
2051       .writefn = csselr_write, .resetvalue = 0,
2052       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2053                              offsetof(CPUARMState, cp15.csselr_ns) } },
2054     /*
2055      * Auxiliary ID register: this actually has an IMPDEF value but for now
2056      * just RAZ for all cores:
2057      */
2058     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2059       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2060       .access = PL1_R, .type = ARM_CP_CONST,
2061       .accessfn = access_aa64_tid1,
2062       .fgt = FGT_AIDR_EL1,
2063       .resetvalue = 0 },
2064     /*
2065      * Auxiliary fault status registers: these also are IMPDEF, and we
2066      * choose to RAZ/WI for all cores.
2067      */
2068     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2069       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2070       .access = PL1_RW, .accessfn = access_tvm_trvm,
2071       .fgt = FGT_AFSR0_EL1,
2072       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2073       .type = ARM_CP_CONST, .resetvalue = 0 },
2074     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2075       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2076       .access = PL1_RW, .accessfn = access_tvm_trvm,
2077       .fgt = FGT_AFSR1_EL1,
2078       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2079       .type = ARM_CP_CONST, .resetvalue = 0 },
2080     /*
2081      * MAIR can just read-as-written because we don't implement caches
2082      * and so don't need to care about memory attributes.
2083      */
2084     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2085       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2086       .access = PL1_RW, .accessfn = access_tvm_trvm,
2087       .fgt = FGT_MAIR_EL1,
2088       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2089       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2090       .resetvalue = 0 },
2091     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2092       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2093       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2094       .resetvalue = 0 },
2095     /*
2096      * For non-long-descriptor page tables these are PRRR and NMRR;
2097      * regardless they still act as reads-as-written for QEMU.
2098      */
2099      /*
2100       * MAIR0/1 are defined separately from their 64-bit counterpart which
2101       * allows them to assign the correct fieldoffset based on the endianness
2102       * handled in the field definitions.
2103       */
2104     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2105       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2106       .access = PL1_RW, .accessfn = access_tvm_trvm,
2107       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2108                              offsetof(CPUARMState, cp15.mair0_ns) },
2109       .resetfn = arm_cp_reset_ignore },
2110     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2111       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2112       .access = PL1_RW, .accessfn = access_tvm_trvm,
2113       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2114                              offsetof(CPUARMState, cp15.mair1_ns) },
2115       .resetfn = arm_cp_reset_ignore },
2116     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2117       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2118       .fgt = FGT_ISR_EL1,
2119       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2120 };
2121 
2122 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2123     /* PMOVSSET is not implemented in v7 before v7ve */
2124     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2125       .access = PL0_RW, .accessfn = pmreg_access,
2126       .fgt = FGT_PMOVS,
2127       .type = ARM_CP_ALIAS | ARM_CP_IO,
2128       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2129       .writefn = pmovsset_write,
2130       .raw_writefn = raw_write },
2131     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2132       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2133       .access = PL0_RW, .accessfn = pmreg_access,
2134       .fgt = FGT_PMOVS,
2135       .type = ARM_CP_ALIAS | ARM_CP_IO,
2136       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2137       .writefn = pmovsset_write,
2138       .raw_writefn = raw_write },
2139 };
2140 
2141 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2142                         uint64_t value)
2143 {
2144     value &= 1;
2145     env->teecr = value;
2146 }
2147 
2148 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2149                                    bool isread)
2150 {
2151     /*
2152      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2153      * at all, so we don't need to check whether we're v8A.
2154      */
2155     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2156         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2157         return CP_ACCESS_TRAP_EL2;
2158     }
2159     return CP_ACCESS_OK;
2160 }
2161 
2162 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2163                                     bool isread)
2164 {
2165     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2166         return CP_ACCESS_TRAP_EL1;
2167     }
2168     return teecr_access(env, ri, isread);
2169 }
2170 
2171 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2172     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2173       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2174       .resetvalue = 0,
2175       .writefn = teecr_write, .accessfn = teecr_access },
2176     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2177       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2178       .accessfn = teehbr_access, .resetvalue = 0 },
2179 };
2180 
2181 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2182     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2183       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2184       .access = PL0_RW,
2185       .fgt = FGT_TPIDR_EL0,
2186       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2187     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2188       .access = PL0_RW,
2189       .fgt = FGT_TPIDR_EL0,
2190       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2191                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2192       .resetfn = arm_cp_reset_ignore },
2193     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2194       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2195       .access = PL0_R | PL1_W,
2196       .fgt = FGT_TPIDRRO_EL0,
2197       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2198       .resetvalue = 0},
2199     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2200       .access = PL0_R | PL1_W,
2201       .fgt = FGT_TPIDRRO_EL0,
2202       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2203                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2204       .resetfn = arm_cp_reset_ignore },
2205     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2206       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2207       .access = PL1_RW,
2208       .fgt = FGT_TPIDR_EL1,
2209       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2210     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2211       .access = PL1_RW,
2212       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2213                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2214       .resetvalue = 0 },
2215 };
2216 
2217 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2218 {
2219     ARMCPU *cpu = env_archcpu(env);
2220 
2221     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2222 }
2223 
2224 #ifndef CONFIG_USER_ONLY
2225 
2226 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2227                                        bool isread)
2228 {
2229     /*
2230      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2231      * Writable only at the highest implemented exception level.
2232      */
2233     int el = arm_current_el(env);
2234     uint64_t hcr;
2235     uint32_t cntkctl;
2236 
2237     switch (el) {
2238     case 0:
2239         hcr = arm_hcr_el2_eff(env);
2240         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2241             cntkctl = env->cp15.cnthctl_el2;
2242         } else {
2243             cntkctl = env->cp15.c14_cntkctl;
2244         }
2245         if (!extract32(cntkctl, 0, 2)) {
2246             return CP_ACCESS_TRAP_EL1;
2247         }
2248         break;
2249     case 1:
2250         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2251             arm_is_secure_below_el3(env)) {
2252             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2253             return CP_ACCESS_UNDEFINED;
2254         }
2255         break;
2256     case 2:
2257     case 3:
2258         break;
2259     }
2260 
2261     if (!isread && el < arm_highest_el(env)) {
2262         return CP_ACCESS_UNDEFINED;
2263     }
2264 
2265     return CP_ACCESS_OK;
2266 }
2267 
2268 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2269                                         bool isread)
2270 {
2271     unsigned int cur_el = arm_current_el(env);
2272     bool has_el2 = arm_is_el2_enabled(env);
2273     uint64_t hcr = arm_hcr_el2_eff(env);
2274 
2275     switch (cur_el) {
2276     case 0:
2277         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2278         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2279             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2280                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2281         }
2282 
2283         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2284         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2285             return CP_ACCESS_TRAP_EL1;
2286         }
2287         /* fall through */
2288     case 1:
2289         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2290         if (has_el2 && timeridx == GTIMER_PHYS &&
2291             (hcr & HCR_E2H
2292              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2293              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2294             return CP_ACCESS_TRAP_EL2;
2295         }
2296         if (has_el2 && timeridx == GTIMER_VIRT) {
2297             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2298                 return CP_ACCESS_TRAP_EL2;
2299             }
2300         }
2301         break;
2302     }
2303     return CP_ACCESS_OK;
2304 }
2305 
2306 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2307                                       bool isread)
2308 {
2309     unsigned int cur_el = arm_current_el(env);
2310     bool has_el2 = arm_is_el2_enabled(env);
2311     uint64_t hcr = arm_hcr_el2_eff(env);
2312 
2313     switch (cur_el) {
2314     case 0:
2315         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2316             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2317             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2318                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2319         }
2320 
2321         /*
2322          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2323          * EL0 if EL0[PV]TEN is zero.
2324          */
2325         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2326             return CP_ACCESS_TRAP_EL1;
2327         }
2328         /* fall through */
2329 
2330     case 1:
2331         if (has_el2 && timeridx == GTIMER_PHYS) {
2332             if (hcr & HCR_E2H) {
2333                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2334                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2335                     return CP_ACCESS_TRAP_EL2;
2336                 }
2337             } else {
2338                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2339                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2340                     return CP_ACCESS_TRAP_EL2;
2341                 }
2342             }
2343         }
2344         if (has_el2 && timeridx == GTIMER_VIRT) {
2345             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2346                 return CP_ACCESS_TRAP_EL2;
2347             }
2348         }
2349         break;
2350     }
2351     return CP_ACCESS_OK;
2352 }
2353 
2354 static CPAccessResult gt_pct_access(CPUARMState *env,
2355                                     const ARMCPRegInfo *ri,
2356                                     bool isread)
2357 {
2358     return gt_counter_access(env, GTIMER_PHYS, isread);
2359 }
2360 
2361 static CPAccessResult gt_vct_access(CPUARMState *env,
2362                                     const ARMCPRegInfo *ri,
2363                                     bool isread)
2364 {
2365     return gt_counter_access(env, GTIMER_VIRT, isread);
2366 }
2367 
2368 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2369                                        bool isread)
2370 {
2371     return gt_timer_access(env, GTIMER_PHYS, isread);
2372 }
2373 
2374 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2375                                        bool isread)
2376 {
2377     return gt_timer_access(env, GTIMER_VIRT, isread);
2378 }
2379 
2380 static CPAccessResult gt_stimer_access(CPUARMState *env,
2381                                        const ARMCPRegInfo *ri,
2382                                        bool isread)
2383 {
2384     /*
2385      * The AArch64 register view of the secure physical timer is
2386      * always accessible from EL3, and configurably accessible from
2387      * Secure EL1.
2388      */
2389     switch (arm_current_el(env)) {
2390     case 1:
2391         if (!arm_is_secure(env)) {
2392             return CP_ACCESS_UNDEFINED;
2393         }
2394         if (arm_is_el2_enabled(env)) {
2395             return CP_ACCESS_UNDEFINED;
2396         }
2397         if (!(env->cp15.scr_el3 & SCR_ST)) {
2398             return CP_ACCESS_TRAP_EL3;
2399         }
2400         return CP_ACCESS_OK;
2401     case 0:
2402     case 2:
2403         return CP_ACCESS_UNDEFINED;
2404     case 3:
2405         return CP_ACCESS_OK;
2406     default:
2407         g_assert_not_reached();
2408     }
2409 }
2410 
2411 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2412                                           const ARMCPRegInfo *ri,
2413                                           bool isread)
2414 {
2415     /*
2416      * The AArch64 register view of the secure EL2 timers are mostly
2417      * accessible from EL3 and EL2 although can also be trapped to EL2
2418      * from EL1 depending on nested virt config.
2419      */
2420     switch (arm_current_el(env)) {
2421     case 0: /* UNDEFINED */
2422         return CP_ACCESS_UNDEFINED;
2423     case 1:
2424         if (!arm_is_secure(env)) {
2425             /* UNDEFINED */
2426             return CP_ACCESS_UNDEFINED;
2427         } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2428             /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2429             return CP_ACCESS_TRAP_EL2;
2430         }
2431         /* UNDEFINED */
2432         return CP_ACCESS_UNDEFINED;
2433     case 2:
2434         if (!arm_is_secure(env)) {
2435             /* UNDEFINED */
2436             return CP_ACCESS_UNDEFINED;
2437         }
2438         return CP_ACCESS_OK;
2439     case 3:
2440         if (env->cp15.scr_el3 & SCR_EEL2) {
2441             return CP_ACCESS_OK;
2442         } else {
2443             return CP_ACCESS_UNDEFINED;
2444         }
2445     default:
2446         g_assert_not_reached();
2447     }
2448 }
2449 
2450 uint64_t gt_get_countervalue(CPUARMState *env)
2451 {
2452     ARMCPU *cpu = env_archcpu(env);
2453 
2454     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2455 }
2456 
2457 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2458 {
2459     CPUARMState *env = &cpu->env;
2460     uint64_t cnthctl = env->cp15.cnthctl_el2;
2461     ARMSecuritySpace ss = arm_security_space(env);
2462     /* ISTATUS && !IMASK */
2463     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2464 
2465     /*
2466      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2467      * It is RES0 in Secure and NonSecure state.
2468      */
2469     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2470         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2471          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2472         irqstate = 0;
2473     }
2474 
2475     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2476     trace_arm_gt_update_irq(timeridx, irqstate);
2477 }
2478 
2479 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2480 {
2481     /*
2482      * Changing security state between Root and Secure/NonSecure, which may
2483      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2484      * mask bits. Update the IRQ state accordingly.
2485      */
2486     gt_update_irq(cpu, GTIMER_VIRT);
2487     gt_update_irq(cpu, GTIMER_PHYS);
2488 }
2489 
2490 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2491 {
2492     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2493         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2494         arm_is_el2_enabled(env) &&
2495         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2496         return env->cp15.cntpoff_el2;
2497     }
2498     return 0;
2499 }
2500 
2501 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2502 {
2503     /*
2504      * Return the timer offset to use for indirect accesses to the timer.
2505      * This is the Offset value as defined in D12.2.4.1 "Operation of the
2506      * CompareValue views of the timers".
2507      *
2508      * The condition here is not always the same as the condition for
2509      * whether to apply an offset register when doing a direct read of
2510      * the counter sysreg; those conditions are described in the
2511      * access pseudocode for each counter register.
2512      */
2513     switch (timeridx) {
2514     case GTIMER_PHYS:
2515         return gt_phys_raw_cnt_offset(env);
2516     case GTIMER_VIRT:
2517         return env->cp15.cntvoff_el2;
2518     case GTIMER_HYP:
2519     case GTIMER_SEC:
2520     case GTIMER_HYPVIRT:
2521     case GTIMER_S_EL2_PHYS:
2522     case GTIMER_S_EL2_VIRT:
2523         return 0;
2524     default:
2525         g_assert_not_reached();
2526     }
2527 }
2528 
2529 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2530 {
2531     /*
2532      * Return the timer offset to use for direct accesses to the
2533      * counter registers CNTPCT and CNTVCT, and for direct accesses
2534      * to the CNT*_TVAL registers.
2535      *
2536      * This isn't exactly the same as the indirect-access offset,
2537      * because here we also care about what EL the register access
2538      * is being made from.
2539      *
2540      * This corresponds to the access pseudocode for the registers.
2541      */
2542     uint64_t hcr;
2543 
2544     switch (timeridx) {
2545     case GTIMER_PHYS:
2546         if (arm_current_el(env) >= 2) {
2547             return 0;
2548         }
2549         return gt_phys_raw_cnt_offset(env);
2550     case GTIMER_VIRT:
2551         switch (arm_current_el(env)) {
2552         case 2:
2553             hcr = arm_hcr_el2_eff(env);
2554             if (hcr & HCR_E2H) {
2555                 return 0;
2556             }
2557             break;
2558         case 0:
2559             hcr = arm_hcr_el2_eff(env);
2560             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2561                 return 0;
2562             }
2563             break;
2564         }
2565         return env->cp15.cntvoff_el2;
2566     case GTIMER_HYP:
2567     case GTIMER_SEC:
2568     case GTIMER_HYPVIRT:
2569     case GTIMER_S_EL2_PHYS:
2570     case GTIMER_S_EL2_VIRT:
2571         return 0;
2572     default:
2573         g_assert_not_reached();
2574     }
2575 }
2576 
2577 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2578 {
2579     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2580 
2581     if (gt->ctl & 1) {
2582         /*
2583          * Timer enabled: calculate and set current ISTATUS, irq, and
2584          * reset timer to when ISTATUS next has to change
2585          */
2586         uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2587         uint64_t count = gt_get_countervalue(&cpu->env);
2588         /* Note that this must be unsigned 64 bit arithmetic: */
2589         int istatus = count - offset >= gt->cval;
2590         uint64_t nexttick;
2591 
2592         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2593 
2594         if (istatus) {
2595             /*
2596              * Next transition is when (count - offset) rolls back over to 0.
2597              * If offset > count then this is when count == offset;
2598              * if offset <= count then this is when count == offset + 2^64
2599              * For the latter case we set nexttick to an "as far in future
2600              * as possible" value and let the code below handle it.
2601              */
2602             if (offset > count) {
2603                 nexttick = offset;
2604             } else {
2605                 nexttick = UINT64_MAX;
2606             }
2607         } else {
2608             /*
2609              * Next transition is when (count - offset) == cval, i.e.
2610              * when count == (cval + offset).
2611              * If that would overflow, then again we set up the next interrupt
2612              * for "as far in the future as possible" for the code below.
2613              */
2614             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2615                 nexttick = UINT64_MAX;
2616             }
2617         }
2618         /*
2619          * Note that the desired next expiry time might be beyond the
2620          * signed-64-bit range of a QEMUTimer -- in this case we just
2621          * set the timer for as far in the future as possible. When the
2622          * timer expires we will reset the timer for any remaining period.
2623          */
2624         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2625             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2626         } else {
2627             timer_mod(cpu->gt_timer[timeridx], nexttick);
2628         }
2629         trace_arm_gt_recalc(timeridx, nexttick);
2630     } else {
2631         /* Timer disabled: ISTATUS and timer output always clear */
2632         gt->ctl &= ~4;
2633         timer_del(cpu->gt_timer[timeridx]);
2634         trace_arm_gt_recalc_disabled(timeridx);
2635     }
2636     gt_update_irq(cpu, timeridx);
2637 }
2638 
2639 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2640                            int timeridx)
2641 {
2642     ARMCPU *cpu = env_archcpu(env);
2643 
2644     timer_del(cpu->gt_timer[timeridx]);
2645 }
2646 
2647 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2648 {
2649     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2650     return gt_get_countervalue(env) - offset;
2651 }
2652 
2653 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2654 {
2655     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2656     return gt_get_countervalue(env) - offset;
2657 }
2658 
2659 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2660                           int timeridx,
2661                           uint64_t value)
2662 {
2663     trace_arm_gt_cval_write(timeridx, value);
2664     env->cp15.c14_timer[timeridx].cval = value;
2665     gt_recalc_timer(env_archcpu(env), timeridx);
2666 }
2667 
2668 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2669 {
2670     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2671                       (gt_get_countervalue(env) - offset));
2672 }
2673 
2674 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2675                              int timeridx)
2676 {
2677     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2678 
2679     return do_tval_read(env, timeridx, offset);
2680 }
2681 
2682 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2683                           uint64_t offset)
2684 {
2685     trace_arm_gt_tval_write(timeridx, value);
2686     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2687                                          sextract64(value, 0, 32);
2688     gt_recalc_timer(env_archcpu(env), timeridx);
2689 }
2690 
2691 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692                           int timeridx,
2693                           uint64_t value)
2694 {
2695     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2696 
2697     do_tval_write(env, timeridx, value, offset);
2698 }
2699 
2700 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2701                          int timeridx,
2702                          uint64_t value)
2703 {
2704     ARMCPU *cpu = env_archcpu(env);
2705     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2706 
2707     trace_arm_gt_ctl_write(timeridx, value);
2708     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2709     if ((oldval ^ value) & 1) {
2710         /* Enable toggled */
2711         gt_recalc_timer(cpu, timeridx);
2712     } else if ((oldval ^ value) & 2) {
2713         /*
2714          * IMASK toggled: don't need to recalculate,
2715          * just set the interrupt line based on ISTATUS
2716          */
2717         trace_arm_gt_imask_toggle(timeridx);
2718         gt_update_irq(cpu, timeridx);
2719     }
2720 }
2721 
2722 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2723 {
2724     gt_timer_reset(env, ri, GTIMER_PHYS);
2725 }
2726 
2727 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2728                                uint64_t value)
2729 {
2730     gt_cval_write(env, ri, GTIMER_PHYS, value);
2731 }
2732 
2733 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2734 {
2735     return gt_tval_read(env, ri, GTIMER_PHYS);
2736 }
2737 
2738 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739                                uint64_t value)
2740 {
2741     gt_tval_write(env, ri, GTIMER_PHYS, value);
2742 }
2743 
2744 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745                               uint64_t value)
2746 {
2747     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2748 }
2749 
2750 static int gt_phys_redir_timeridx(CPUARMState *env)
2751 {
2752     switch (arm_mmu_idx(env)) {
2753     case ARMMMUIdx_E20_0:
2754     case ARMMMUIdx_E20_2:
2755     case ARMMMUIdx_E20_2_PAN:
2756         return GTIMER_HYP;
2757     default:
2758         return GTIMER_PHYS;
2759     }
2760 }
2761 
2762 static int gt_virt_redir_timeridx(CPUARMState *env)
2763 {
2764     switch (arm_mmu_idx(env)) {
2765     case ARMMMUIdx_E20_0:
2766     case ARMMMUIdx_E20_2:
2767     case ARMMMUIdx_E20_2_PAN:
2768         return GTIMER_HYPVIRT;
2769     default:
2770         return GTIMER_VIRT;
2771     }
2772 }
2773 
2774 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2775                                         const ARMCPRegInfo *ri)
2776 {
2777     int timeridx = gt_phys_redir_timeridx(env);
2778     return env->cp15.c14_timer[timeridx].cval;
2779 }
2780 
2781 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782                                      uint64_t value)
2783 {
2784     int timeridx = gt_phys_redir_timeridx(env);
2785     gt_cval_write(env, ri, timeridx, value);
2786 }
2787 
2788 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2789                                         const ARMCPRegInfo *ri)
2790 {
2791     int timeridx = gt_phys_redir_timeridx(env);
2792     return gt_tval_read(env, ri, timeridx);
2793 }
2794 
2795 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796                                      uint64_t value)
2797 {
2798     int timeridx = gt_phys_redir_timeridx(env);
2799     gt_tval_write(env, ri, timeridx, value);
2800 }
2801 
2802 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2803                                        const ARMCPRegInfo *ri)
2804 {
2805     int timeridx = gt_phys_redir_timeridx(env);
2806     return env->cp15.c14_timer[timeridx].ctl;
2807 }
2808 
2809 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810                                     uint64_t value)
2811 {
2812     int timeridx = gt_phys_redir_timeridx(env);
2813     gt_ctl_write(env, ri, timeridx, value);
2814 }
2815 
2816 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2817 {
2818     gt_timer_reset(env, ri, GTIMER_VIRT);
2819 }
2820 
2821 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822                                uint64_t value)
2823 {
2824     gt_cval_write(env, ri, GTIMER_VIRT, value);
2825 }
2826 
2827 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     /*
2830      * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
2831      * we always apply CNTVOFF_EL2. Special case that here rather
2832      * than going into the generic gt_tval_read() and then having
2833      * to re-detect that it's this register.
2834      * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
2835      */
2836     return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
2837 }
2838 
2839 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2840                                uint64_t value)
2841 {
2842     /* Similarly for writes to CNTV_TVAL_EL02 */
2843     do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
2844 }
2845 
2846 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2847                               uint64_t value)
2848 {
2849     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2850 }
2851 
2852 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2853                              uint64_t value)
2854 {
2855     ARMCPU *cpu = env_archcpu(env);
2856     uint32_t oldval = env->cp15.cnthctl_el2;
2857     uint32_t valid_mask =
2858         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2859         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2860         R_CNTHCTL_EVNTEN_MASK |
2861         R_CNTHCTL_EVNTDIR_MASK |
2862         R_CNTHCTL_EVNTI_MASK |
2863         R_CNTHCTL_EL0VTEN_MASK |
2864         R_CNTHCTL_EL0PTEN_MASK |
2865         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2866         R_CNTHCTL_EL1PTEN_MASK;
2867 
2868     if (cpu_isar_feature(aa64_rme, cpu)) {
2869         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2870     }
2871     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2872         valid_mask |=
2873             R_CNTHCTL_EL1TVT_MASK |
2874             R_CNTHCTL_EL1TVCT_MASK |
2875             R_CNTHCTL_EL1NVPCT_MASK |
2876             R_CNTHCTL_EL1NVVCT_MASK |
2877             R_CNTHCTL_EVNTIS_MASK;
2878     }
2879     if (cpu_isar_feature(aa64_ecv, cpu)) {
2880         valid_mask |= R_CNTHCTL_ECV_MASK;
2881     }
2882 
2883     /* Clear RES0 bits */
2884     value &= valid_mask;
2885 
2886     raw_write(env, ri, value);
2887 
2888     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2889         gt_update_irq(cpu, GTIMER_VIRT);
2890     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2891         gt_update_irq(cpu, GTIMER_PHYS);
2892     }
2893 }
2894 
2895 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2896                               uint64_t value)
2897 {
2898     ARMCPU *cpu = env_archcpu(env);
2899 
2900     trace_arm_gt_cntvoff_write(value);
2901     raw_write(env, ri, value);
2902     gt_recalc_timer(cpu, GTIMER_VIRT);
2903 }
2904 
2905 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2906                                         const ARMCPRegInfo *ri)
2907 {
2908     int timeridx = gt_virt_redir_timeridx(env);
2909     return env->cp15.c14_timer[timeridx].cval;
2910 }
2911 
2912 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2913                                      uint64_t value)
2914 {
2915     int timeridx = gt_virt_redir_timeridx(env);
2916     gt_cval_write(env, ri, timeridx, value);
2917 }
2918 
2919 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2920                                         const ARMCPRegInfo *ri)
2921 {
2922     int timeridx = gt_virt_redir_timeridx(env);
2923     return gt_tval_read(env, ri, timeridx);
2924 }
2925 
2926 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927                                      uint64_t value)
2928 {
2929     int timeridx = gt_virt_redir_timeridx(env);
2930     gt_tval_write(env, ri, timeridx, value);
2931 }
2932 
2933 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2934                                        const ARMCPRegInfo *ri)
2935 {
2936     int timeridx = gt_virt_redir_timeridx(env);
2937     return env->cp15.c14_timer[timeridx].ctl;
2938 }
2939 
2940 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941                                     uint64_t value)
2942 {
2943     int timeridx = gt_virt_redir_timeridx(env);
2944     gt_ctl_write(env, ri, timeridx, value);
2945 }
2946 
2947 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2948 {
2949     gt_timer_reset(env, ri, GTIMER_HYP);
2950 }
2951 
2952 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2953                               uint64_t value)
2954 {
2955     gt_cval_write(env, ri, GTIMER_HYP, value);
2956 }
2957 
2958 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2959 {
2960     return gt_tval_read(env, ri, GTIMER_HYP);
2961 }
2962 
2963 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2964                               uint64_t value)
2965 {
2966     gt_tval_write(env, ri, GTIMER_HYP, value);
2967 }
2968 
2969 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2970                               uint64_t value)
2971 {
2972     gt_ctl_write(env, ri, GTIMER_HYP, value);
2973 }
2974 
2975 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2976 {
2977     gt_timer_reset(env, ri, GTIMER_SEC);
2978 }
2979 
2980 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2981                               uint64_t value)
2982 {
2983     gt_cval_write(env, ri, GTIMER_SEC, value);
2984 }
2985 
2986 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2987 {
2988     return gt_tval_read(env, ri, GTIMER_SEC);
2989 }
2990 
2991 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2992                               uint64_t value)
2993 {
2994     gt_tval_write(env, ri, GTIMER_SEC, value);
2995 }
2996 
2997 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2998                               uint64_t value)
2999 {
3000     gt_ctl_write(env, ri, GTIMER_SEC, value);
3001 }
3002 
3003 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3004 {
3005     gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3006 }
3007 
3008 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3009                                    uint64_t value)
3010 {
3011     gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3012 }
3013 
3014 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3015 {
3016     return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3017 }
3018 
3019 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3020                               uint64_t value)
3021 {
3022     gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3023 }
3024 
3025 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3026                               uint64_t value)
3027 {
3028     gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3029 }
3030 
3031 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3032 {
3033     gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3034 }
3035 
3036 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3037                               uint64_t value)
3038 {
3039     gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3040 }
3041 
3042 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3043 {
3044     return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3045 }
3046 
3047 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3048                                    uint64_t value)
3049 {
3050     gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3051 }
3052 
3053 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3054                               uint64_t value)
3055 {
3056     gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3057 }
3058 
3059 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3060 {
3061     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3062 }
3063 
3064 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3065                              uint64_t value)
3066 {
3067     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3068 }
3069 
3070 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3071 {
3072     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3073 }
3074 
3075 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3076                              uint64_t value)
3077 {
3078     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3079 }
3080 
3081 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3082                             uint64_t value)
3083 {
3084     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3085 }
3086 
3087 void arm_gt_ptimer_cb(void *opaque)
3088 {
3089     ARMCPU *cpu = opaque;
3090 
3091     gt_recalc_timer(cpu, GTIMER_PHYS);
3092 }
3093 
3094 void arm_gt_vtimer_cb(void *opaque)
3095 {
3096     ARMCPU *cpu = opaque;
3097 
3098     gt_recalc_timer(cpu, GTIMER_VIRT);
3099 }
3100 
3101 void arm_gt_htimer_cb(void *opaque)
3102 {
3103     ARMCPU *cpu = opaque;
3104 
3105     gt_recalc_timer(cpu, GTIMER_HYP);
3106 }
3107 
3108 void arm_gt_stimer_cb(void *opaque)
3109 {
3110     ARMCPU *cpu = opaque;
3111 
3112     gt_recalc_timer(cpu, GTIMER_SEC);
3113 }
3114 
3115 void arm_gt_sel2timer_cb(void *opaque)
3116 {
3117     ARMCPU *cpu = opaque;
3118 
3119     gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3120 }
3121 
3122 void arm_gt_sel2vtimer_cb(void *opaque)
3123 {
3124     ARMCPU *cpu = opaque;
3125 
3126     gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3127 }
3128 
3129 void arm_gt_hvtimer_cb(void *opaque)
3130 {
3131     ARMCPU *cpu = opaque;
3132 
3133     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3134 }
3135 
3136 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3137     /*
3138      * Note that CNTFRQ is purely reads-as-written for the benefit
3139      * of software; writing it doesn't actually change the timer frequency.
3140      * Our reset value matches the fixed frequency we implement the timer at.
3141      */
3142     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3143       .type = ARM_CP_ALIAS,
3144       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3145       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3146     },
3147     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3148       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3149       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3150       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3151       .resetfn = arm_gt_cntfrq_reset,
3152     },
3153     /* overall control: mostly access permissions */
3154     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3155       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3156       .access = PL1_RW,
3157       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3158       .resetvalue = 0,
3159     },
3160     /* per-timer control */
3161     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3162       .secure = ARM_CP_SECSTATE_NS,
3163       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3164       .accessfn = gt_ptimer_access,
3165       .fieldoffset = offsetoflow32(CPUARMState,
3166                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3167       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3168       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3169     },
3170     { .name = "CNTP_CTL_S",
3171       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3172       .secure = ARM_CP_SECSTATE_S,
3173       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3174       .accessfn = gt_ptimer_access,
3175       .fieldoffset = offsetoflow32(CPUARMState,
3176                                    cp15.c14_timer[GTIMER_SEC].ctl),
3177       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3178     },
3179     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3180       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3181       .type = ARM_CP_IO, .access = PL0_RW,
3182       .accessfn = gt_ptimer_access,
3183       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3184       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3185       .resetvalue = 0,
3186       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3187       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3188     },
3189     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3190       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3191       .accessfn = gt_vtimer_access,
3192       .fieldoffset = offsetoflow32(CPUARMState,
3193                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3194       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3195       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3196     },
3197     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3198       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3199       .type = ARM_CP_IO, .access = PL0_RW,
3200       .accessfn = gt_vtimer_access,
3201       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3202       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3203       .resetvalue = 0,
3204       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3205       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3206     },
3207     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3208     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3209       .secure = ARM_CP_SECSTATE_NS,
3210       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3211       .accessfn = gt_ptimer_access,
3212       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3213     },
3214     { .name = "CNTP_TVAL_S",
3215       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3216       .secure = ARM_CP_SECSTATE_S,
3217       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3218       .accessfn = gt_ptimer_access,
3219       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3220     },
3221     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3222       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3223       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3224       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3225       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3226     },
3227     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3228       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3229       .accessfn = gt_vtimer_access,
3230       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3231     },
3232     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3233       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3234       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3235       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3236       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3237     },
3238     /* The counter itself */
3239     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3240       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3241       .accessfn = gt_pct_access,
3242       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3243     },
3244     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3245       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3246       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3247       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3248     },
3249     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3250       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3251       .accessfn = gt_vct_access,
3252       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3253     },
3254     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3255       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3256       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3257       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3258     },
3259     /* Comparison value, indicating when the timer goes off */
3260     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3261       .secure = ARM_CP_SECSTATE_NS,
3262       .access = PL0_RW,
3263       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3264       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3265       .accessfn = gt_ptimer_access,
3266       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3267       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3268     },
3269     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3270       .secure = ARM_CP_SECSTATE_S,
3271       .access = PL0_RW,
3272       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3273       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3274       .accessfn = gt_ptimer_access,
3275       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3276     },
3277     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3278       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3279       .access = PL0_RW,
3280       .type = ARM_CP_IO,
3281       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3282       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3283       .resetvalue = 0, .accessfn = gt_ptimer_access,
3284       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3285       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3286     },
3287     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3288       .access = PL0_RW,
3289       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3290       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3291       .accessfn = gt_vtimer_access,
3292       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3293       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3294     },
3295     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3296       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3297       .access = PL0_RW,
3298       .type = ARM_CP_IO,
3299       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3300       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3301       .resetvalue = 0, .accessfn = gt_vtimer_access,
3302       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3303       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3304     },
3305     /*
3306      * Secure timer -- this is actually restricted to only EL3
3307      * and configurably Secure-EL1 via the accessfn.
3308      */
3309     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3310       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3311       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3312       .accessfn = gt_stimer_access,
3313       .readfn = gt_sec_tval_read,
3314       .writefn = gt_sec_tval_write,
3315       .resetfn = gt_sec_timer_reset,
3316     },
3317     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3318       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3319       .type = ARM_CP_IO, .access = PL1_RW,
3320       .accessfn = gt_stimer_access,
3321       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3322       .resetvalue = 0,
3323       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3324     },
3325     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3326       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3327       .type = ARM_CP_IO, .access = PL1_RW,
3328       .accessfn = gt_stimer_access,
3329       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3330       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3331     },
3332 };
3333 
3334 /*
3335  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3336  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3337  * so our implementations here are identical to the normal registers.
3338  */
3339 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3340     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3341       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3342       .accessfn = gt_vct_access,
3343       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3344     },
3345     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3346       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3347       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3348       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3349     },
3350     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3351       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3352       .accessfn = gt_pct_access,
3353       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3354     },
3355     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3356       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3357       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3358       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3359     },
3360 };
3361 
3362 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3363                                         const ARMCPRegInfo *ri,
3364                                         bool isread)
3365 {
3366     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3367         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3368         return CP_ACCESS_TRAP_EL3;
3369     }
3370     return CP_ACCESS_OK;
3371 }
3372 
3373 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3374                               uint64_t value)
3375 {
3376     ARMCPU *cpu = env_archcpu(env);
3377 
3378     trace_arm_gt_cntpoff_write(value);
3379     raw_write(env, ri, value);
3380     gt_recalc_timer(cpu, GTIMER_PHYS);
3381 }
3382 
3383 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3384     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3385     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3386     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3387     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3388     .nv2_redirect_offset = 0x1a8,
3389     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3390 };
3391 #else
3392 
3393 /*
3394  * In user-mode most of the generic timer registers are inaccessible
3395  * however modern kernels (4.12+) allow access to cntvct_el0
3396  */
3397 
3398 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3399 {
3400     ARMCPU *cpu = env_archcpu(env);
3401 
3402     /*
3403      * Currently we have no support for QEMUTimer in linux-user so we
3404      * can't call gt_get_countervalue(env), instead we directly
3405      * call the lower level functions.
3406      */
3407     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3408 }
3409 
3410 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3411     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3412       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3413       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3414       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3415       .resetfn = arm_gt_cntfrq_reset,
3416     },
3417     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3418       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3419       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3420       .readfn = gt_virt_cnt_read,
3421     },
3422 };
3423 
3424 /*
3425  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3426  * is exposed to userspace by Linux.
3427  */
3428 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3429     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3430       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3431       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3432       .readfn = gt_virt_cnt_read,
3433     },
3434 };
3435 
3436 #endif
3437 
3438 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3439 {
3440     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3441         raw_write(env, ri, value);
3442     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3443         raw_write(env, ri, value & 0xfffff6ff);
3444     } else {
3445         raw_write(env, ri, value & 0xfffff1ff);
3446     }
3447 }
3448 
3449 #ifndef CONFIG_USER_ONLY
3450 /* get_phys_addr() isn't present for user-mode-only targets */
3451 
3452 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3453                                  bool isread)
3454 {
3455     if (ri->opc2 & 4) {
3456         /*
3457          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3458          * Secure EL1 (which can only happen if EL3 is AArch64).
3459          * They are simply UNDEF if executed from NS EL1.
3460          * They function normally from EL2 or EL3.
3461          */
3462         if (arm_current_el(env) == 1) {
3463             if (arm_is_secure_below_el3(env)) {
3464                 if (env->cp15.scr_el3 & SCR_EEL2) {
3465                     return CP_ACCESS_TRAP_EL2;
3466                 }
3467                 return CP_ACCESS_TRAP_EL3;
3468             }
3469             return CP_ACCESS_UNDEFINED;
3470         }
3471     }
3472     return CP_ACCESS_OK;
3473 }
3474 
3475 #ifdef CONFIG_TCG
3476 static int par_el1_shareability(GetPhysAddrResult *res)
3477 {
3478     /*
3479      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3480      * memory -- see pseudocode PAREncodeShareability().
3481      */
3482     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3483         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3484         return 2;
3485     }
3486     return res->cacheattrs.shareability;
3487 }
3488 
3489 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3490                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3491                              ARMSecuritySpace ss)
3492 {
3493     bool ret;
3494     uint64_t par64;
3495     bool format64 = false;
3496     ARMMMUFaultInfo fi = {};
3497     GetPhysAddrResult res = {};
3498 
3499     /*
3500      * I_MXTJT: Granule protection checks are not performed on the final
3501      * address of a successful translation.  This is a translation not a
3502      * memory reference, so "memop = none = 0".
3503      */
3504     ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3505                                          mmu_idx, ss, &res, &fi);
3506 
3507     /*
3508      * ATS operations only do S1 or S1+S2 translations, so we never
3509      * have to deal with the ARMCacheAttrs format for S2 only.
3510      */
3511     assert(!res.cacheattrs.is_s2_format);
3512 
3513     if (ret) {
3514         /*
3515          * Some kinds of translation fault must cause exceptions rather
3516          * than being reported in the PAR.
3517          */
3518         int current_el = arm_current_el(env);
3519         int target_el;
3520         uint32_t syn, fsr, fsc;
3521         bool take_exc = false;
3522 
3523         if (fi.s1ptw && current_el == 1
3524             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3525             /*
3526              * Synchronous stage 2 fault on an access made as part of the
3527              * translation table walk for AT S1E0* or AT S1E1* insn
3528              * executed from NS EL1. If this is a synchronous external abort
3529              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3530              * to EL3. Otherwise the fault is taken as an exception to EL2,
3531              * and HPFAR_EL2 holds the faulting IPA.
3532              */
3533             if (fi.type == ARMFault_SyncExternalOnWalk &&
3534                 (env->cp15.scr_el3 & SCR_EA)) {
3535                 target_el = 3;
3536             } else {
3537                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3538                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3539                     env->cp15.hpfar_el2 |= HPFAR_NS;
3540                 }
3541                 target_el = 2;
3542             }
3543             take_exc = true;
3544         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3545             /*
3546              * Synchronous external aborts during a translation table walk
3547              * are taken as Data Abort exceptions.
3548              */
3549             if (fi.stage2) {
3550                 if (current_el == 3) {
3551                     target_el = 3;
3552                 } else {
3553                     target_el = 2;
3554                 }
3555             } else {
3556                 target_el = exception_target_el(env);
3557             }
3558             take_exc = true;
3559         }
3560 
3561         if (take_exc) {
3562             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3563             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3564                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3565                 fsr = arm_fi_to_lfsc(&fi);
3566                 fsc = extract32(fsr, 0, 6);
3567             } else {
3568                 fsr = arm_fi_to_sfsc(&fi);
3569                 fsc = 0x3f;
3570             }
3571             /*
3572              * Report exception with ESR indicating a fault due to a
3573              * translation table walk for a cache maintenance instruction.
3574              */
3575             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3576                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3577             env->exception.vaddress = value;
3578             env->exception.fsr = fsr;
3579             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3580         }
3581     }
3582 
3583     if (is_a64(env)) {
3584         format64 = true;
3585     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3586         /*
3587          * ATS1Cxx:
3588          * * TTBCR.EAE determines whether the result is returned using the
3589          *   32-bit or the 64-bit PAR format
3590          * * Instructions executed in Hyp mode always use the 64bit format
3591          *
3592          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3593          * * The Non-secure TTBCR.EAE bit is set to 1
3594          * * The implementation includes EL2, and the value of HCR.VM is 1
3595          *
3596          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3597          *
3598          * ATS1Hx always uses the 64bit format.
3599          */
3600         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3601 
3602         if (arm_feature(env, ARM_FEATURE_EL2)) {
3603             if (mmu_idx == ARMMMUIdx_E10_0 ||
3604                 mmu_idx == ARMMMUIdx_E10_1 ||
3605                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3606                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3607             } else {
3608                 format64 |= arm_current_el(env) == 2;
3609             }
3610         }
3611     }
3612 
3613     if (format64) {
3614         /* Create a 64-bit PAR */
3615         par64 = (1 << 11); /* LPAE bit always set */
3616         if (!ret) {
3617             par64 |= res.f.phys_addr & ~0xfffULL;
3618             if (!res.f.attrs.secure) {
3619                 par64 |= (1 << 9); /* NS */
3620             }
3621             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3622             par64 |= par_el1_shareability(&res) << 7; /* SH */
3623         } else {
3624             uint32_t fsr = arm_fi_to_lfsc(&fi);
3625 
3626             par64 |= 1; /* F */
3627             par64 |= (fsr & 0x3f) << 1; /* FS */
3628             if (fi.stage2) {
3629                 par64 |= (1 << 9); /* S */
3630             }
3631             if (fi.s1ptw) {
3632                 par64 |= (1 << 8); /* PTW */
3633             }
3634         }
3635     } else {
3636         /*
3637          * fsr is a DFSR/IFSR value for the short descriptor
3638          * translation table format (with WnR always clear).
3639          * Convert it to a 32-bit PAR.
3640          */
3641         if (!ret) {
3642             /* We do not set any attribute bits in the PAR */
3643             if (res.f.lg_page_size == 24
3644                 && arm_feature(env, ARM_FEATURE_V7)) {
3645                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3646             } else {
3647                 par64 = res.f.phys_addr & 0xfffff000;
3648             }
3649             if (!res.f.attrs.secure) {
3650                 par64 |= (1 << 9); /* NS */
3651             }
3652         } else {
3653             uint32_t fsr = arm_fi_to_sfsc(&fi);
3654 
3655             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3656                     ((fsr & 0xf) << 1) | 1;
3657         }
3658     }
3659     return par64;
3660 }
3661 #endif /* CONFIG_TCG */
3662 
3663 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3664 {
3665 #ifdef CONFIG_TCG
3666     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3667     uint64_t par64;
3668     ARMMMUIdx mmu_idx;
3669     int el = arm_current_el(env);
3670     ARMSecuritySpace ss = arm_security_space(env);
3671 
3672     switch (ri->opc2 & 6) {
3673     case 0:
3674         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3675         switch (el) {
3676         case 3:
3677             if (ri->crm == 9 && arm_pan_enabled(env)) {
3678                 mmu_idx = ARMMMUIdx_E30_3_PAN;
3679             } else {
3680                 mmu_idx = ARMMMUIdx_E3;
3681             }
3682             break;
3683         case 2:
3684             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3685             /* fall through */
3686         case 1:
3687             if (ri->crm == 9 && arm_pan_enabled(env)) {
3688                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3689             } else {
3690                 mmu_idx = ARMMMUIdx_Stage1_E1;
3691             }
3692             break;
3693         default:
3694             g_assert_not_reached();
3695         }
3696         break;
3697     case 2:
3698         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3699         switch (el) {
3700         case 3:
3701             mmu_idx = ARMMMUIdx_E30_0;
3702             break;
3703         case 2:
3704             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3705             mmu_idx = ARMMMUIdx_Stage1_E0;
3706             break;
3707         case 1:
3708             mmu_idx = ARMMMUIdx_Stage1_E0;
3709             break;
3710         default:
3711             g_assert_not_reached();
3712         }
3713         break;
3714     case 4:
3715         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3716         mmu_idx = ARMMMUIdx_E10_1;
3717         ss = ARMSS_NonSecure;
3718         break;
3719     case 6:
3720         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3721         mmu_idx = ARMMMUIdx_E10_0;
3722         ss = ARMSS_NonSecure;
3723         break;
3724     default:
3725         g_assert_not_reached();
3726     }
3727 
3728     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3729 
3730     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3731 #else
3732     /* Handled by hardware accelerator. */
3733     g_assert_not_reached();
3734 #endif /* CONFIG_TCG */
3735 }
3736 
3737 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3738                         uint64_t value)
3739 {
3740 #ifdef CONFIG_TCG
3741     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3742     uint64_t par64;
3743 
3744     /* There is no SecureEL2 for AArch32. */
3745     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3746                          ARMSS_NonSecure);
3747 
3748     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3749 #else
3750     /* Handled by hardware accelerator. */
3751     g_assert_not_reached();
3752 #endif /* CONFIG_TCG */
3753 }
3754 
3755 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3756                                      bool isread)
3757 {
3758     /*
3759      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3760      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3761      * only happen when executing at EL3 because that combination also causes an
3762      * illegal exception return. We don't need to check FEAT_RME either, because
3763      * scr_write() ensures that the NSE bit is not set otherwise.
3764      */
3765     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3766         return CP_ACCESS_UNDEFINED;
3767     }
3768     return CP_ACCESS_OK;
3769 }
3770 
3771 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3772                                      bool isread)
3773 {
3774     if (arm_current_el(env) == 3 &&
3775         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3776         return CP_ACCESS_UNDEFINED;
3777     }
3778     return at_e012_access(env, ri, isread);
3779 }
3780 
3781 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3782                                       bool isread)
3783 {
3784     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3785         return CP_ACCESS_TRAP_EL2;
3786     }
3787     return at_e012_access(env, ri, isread);
3788 }
3789 
3790 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3791                         uint64_t value)
3792 {
3793 #ifdef CONFIG_TCG
3794     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3795     ARMMMUIdx mmu_idx;
3796     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3797     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3798     bool for_el3 = false;
3799     ARMSecuritySpace ss;
3800 
3801     switch (ri->opc2 & 6) {
3802     case 0:
3803         switch (ri->opc1) {
3804         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3805             if (ri->crm == 9 && arm_pan_enabled(env)) {
3806                 mmu_idx = regime_e20 ?
3807                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3808             } else {
3809                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3810             }
3811             break;
3812         case 4: /* AT S1E2R, AT S1E2W */
3813             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3814             break;
3815         case 6: /* AT S1E3R, AT S1E3W */
3816             mmu_idx = ARMMMUIdx_E3;
3817             for_el3 = true;
3818             break;
3819         default:
3820             g_assert_not_reached();
3821         }
3822         break;
3823     case 2: /* AT S1E0R, AT S1E0W */
3824         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3825         break;
3826     case 4: /* AT S12E1R, AT S12E1W */
3827         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3828         break;
3829     case 6: /* AT S12E0R, AT S12E0W */
3830         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3831         break;
3832     default:
3833         g_assert_not_reached();
3834     }
3835 
3836     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3837     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3838 #else
3839     /* Handled by hardware accelerator. */
3840     g_assert_not_reached();
3841 #endif /* CONFIG_TCG */
3842 }
3843 #endif
3844 
3845 /* Return basic MPU access permission bits.  */
3846 static uint32_t simple_mpu_ap_bits(uint32_t val)
3847 {
3848     uint32_t ret;
3849     uint32_t mask;
3850     int i;
3851     ret = 0;
3852     mask = 3;
3853     for (i = 0; i < 16; i += 2) {
3854         ret |= (val >> i) & mask;
3855         mask <<= 2;
3856     }
3857     return ret;
3858 }
3859 
3860 /* Pad basic MPU access permission bits to extended format.  */
3861 static uint32_t extended_mpu_ap_bits(uint32_t val)
3862 {
3863     uint32_t ret;
3864     uint32_t mask;
3865     int i;
3866     ret = 0;
3867     mask = 3;
3868     for (i = 0; i < 16; i += 2) {
3869         ret |= (val & mask) << i;
3870         mask <<= 2;
3871     }
3872     return ret;
3873 }
3874 
3875 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3876                                  uint64_t value)
3877 {
3878     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3879 }
3880 
3881 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3882 {
3883     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3884 }
3885 
3886 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3887                                  uint64_t value)
3888 {
3889     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3890 }
3891 
3892 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3893 {
3894     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3895 }
3896 
3897 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3898 {
3899     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3900 
3901     if (!u32p) {
3902         return 0;
3903     }
3904 
3905     u32p += env->pmsav7.rnr[M_REG_NS];
3906     return *u32p;
3907 }
3908 
3909 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3910                          uint64_t value)
3911 {
3912     ARMCPU *cpu = env_archcpu(env);
3913     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3914 
3915     if (!u32p) {
3916         return;
3917     }
3918 
3919     u32p += env->pmsav7.rnr[M_REG_NS];
3920     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3921     *u32p = value;
3922 }
3923 
3924 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3925                               uint64_t value)
3926 {
3927     ARMCPU *cpu = env_archcpu(env);
3928     uint32_t nrgs = cpu->pmsav7_dregion;
3929 
3930     if (value >= nrgs) {
3931         qemu_log_mask(LOG_GUEST_ERROR,
3932                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3933                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3934         return;
3935     }
3936 
3937     raw_write(env, ri, value);
3938 }
3939 
3940 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3941                           uint64_t value)
3942 {
3943     ARMCPU *cpu = env_archcpu(env);
3944 
3945     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3946     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3947 }
3948 
3949 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3950 {
3951     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3952 }
3953 
3954 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3955                           uint64_t value)
3956 {
3957     ARMCPU *cpu = env_archcpu(env);
3958 
3959     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3960     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3961 }
3962 
3963 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3964 {
3965     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3966 }
3967 
3968 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3969                            uint64_t value)
3970 {
3971     ARMCPU *cpu = env_archcpu(env);
3972 
3973     /*
3974      * Ignore writes that would select not implemented region.
3975      * This is architecturally UNPREDICTABLE.
3976      */
3977     if (value >= cpu->pmsav7_dregion) {
3978         return;
3979     }
3980 
3981     env->pmsav7.rnr[M_REG_NS] = value;
3982 }
3983 
3984 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3985                           uint64_t value)
3986 {
3987     ARMCPU *cpu = env_archcpu(env);
3988 
3989     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3990     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3991 }
3992 
3993 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3994 {
3995     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3996 }
3997 
3998 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3999                           uint64_t value)
4000 {
4001     ARMCPU *cpu = env_archcpu(env);
4002 
4003     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4004     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4005 }
4006 
4007 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4008 {
4009     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4010 }
4011 
4012 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4013                           uint64_t value)
4014 {
4015     uint32_t n;
4016     uint32_t bit;
4017     ARMCPU *cpu = env_archcpu(env);
4018 
4019     /* Ignore writes to unimplemented regions */
4020     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4021     value &= MAKE_64BIT_MASK(0, rmax);
4022 
4023     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4024 
4025     /* Register alias is only valid for first 32 indexes */
4026     for (n = 0; n < rmax; ++n) {
4027         bit = extract32(value, n, 1);
4028         env->pmsav8.hprlar[n] = deposit32(
4029                     env->pmsav8.hprlar[n], 0, 1, bit);
4030     }
4031 }
4032 
4033 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4034 {
4035     uint32_t n;
4036     uint32_t result = 0x0;
4037     ARMCPU *cpu = env_archcpu(env);
4038 
4039     /* Register alias is only valid for first 32 indexes */
4040     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4041         if (env->pmsav8.hprlar[n] & 0x1) {
4042             result |= (0x1 << n);
4043         }
4044     }
4045     return result;
4046 }
4047 
4048 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4049                            uint64_t value)
4050 {
4051     ARMCPU *cpu = env_archcpu(env);
4052 
4053     /*
4054      * Ignore writes that would select not implemented region.
4055      * This is architecturally UNPREDICTABLE.
4056      */
4057     if (value >= cpu->pmsav8r_hdregion) {
4058         return;
4059     }
4060 
4061     env->pmsav8.hprselr = value;
4062 }
4063 
4064 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4065                           uint64_t value)
4066 {
4067     ARMCPU *cpu = env_archcpu(env);
4068     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4069                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4070 
4071     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4072 
4073     if (ri->opc1 & 4) {
4074         if (index >= cpu->pmsav8r_hdregion) {
4075             return;
4076         }
4077         if (ri->opc2 & 0x1) {
4078             env->pmsav8.hprlar[index] = value;
4079         } else {
4080             env->pmsav8.hprbar[index] = value;
4081         }
4082     } else {
4083         if (index >= cpu->pmsav7_dregion) {
4084             return;
4085         }
4086         if (ri->opc2 & 0x1) {
4087             env->pmsav8.rlar[M_REG_NS][index] = value;
4088         } else {
4089             env->pmsav8.rbar[M_REG_NS][index] = value;
4090         }
4091     }
4092 }
4093 
4094 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4095 {
4096     ARMCPU *cpu = env_archcpu(env);
4097     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4098                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4099 
4100     if (ri->opc1 & 4) {
4101         if (index >= cpu->pmsav8r_hdregion) {
4102             return 0x0;
4103         }
4104         if (ri->opc2 & 0x1) {
4105             return env->pmsav8.hprlar[index];
4106         } else {
4107             return env->pmsav8.hprbar[index];
4108         }
4109     } else {
4110         if (index >= cpu->pmsav7_dregion) {
4111             return 0x0;
4112         }
4113         if (ri->opc2 & 0x1) {
4114             return env->pmsav8.rlar[M_REG_NS][index];
4115         } else {
4116             return env->pmsav8.rbar[M_REG_NS][index];
4117         }
4118     }
4119 }
4120 
4121 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4122     { .name = "PRBAR",
4123       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4124       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4125       .accessfn = access_tvm_trvm,
4126       .readfn = prbar_read, .writefn = prbar_write },
4127     { .name = "PRLAR",
4128       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4129       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4130       .accessfn = access_tvm_trvm,
4131       .readfn = prlar_read, .writefn = prlar_write },
4132     { .name = "PRSELR", .resetvalue = 0,
4133       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4134       .access = PL1_RW, .accessfn = access_tvm_trvm,
4135       .writefn = prselr_write,
4136       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4137     { .name = "HPRBAR", .resetvalue = 0,
4138       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4139       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4140       .readfn = hprbar_read, .writefn = hprbar_write },
4141     { .name = "HPRLAR",
4142       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4143       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4144       .readfn = hprlar_read, .writefn = hprlar_write },
4145     { .name = "HPRSELR", .resetvalue = 0,
4146       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4147       .access = PL2_RW,
4148       .writefn = hprselr_write,
4149       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4150     { .name = "HPRENR",
4151       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4152       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4153       .readfn = hprenr_read, .writefn = hprenr_write },
4154 };
4155 
4156 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4157     /*
4158      * Reset for all these registers is handled in arm_cpu_reset(),
4159      * because the PMSAv7 is also used by M-profile CPUs, which do
4160      * not register cpregs but still need the state to be reset.
4161      */
4162     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4163       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4164       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4165       .readfn = pmsav7_read, .writefn = pmsav7_write,
4166       .resetfn = arm_cp_reset_ignore },
4167     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4168       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4169       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4170       .readfn = pmsav7_read, .writefn = pmsav7_write,
4171       .resetfn = arm_cp_reset_ignore },
4172     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4173       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4174       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4175       .readfn = pmsav7_read, .writefn = pmsav7_write,
4176       .resetfn = arm_cp_reset_ignore },
4177     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4178       .access = PL1_RW,
4179       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4180       .writefn = pmsav7_rgnr_write,
4181       .resetfn = arm_cp_reset_ignore },
4182 };
4183 
4184 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4185     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4186       .access = PL1_RW, .type = ARM_CP_ALIAS,
4187       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4188       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4189     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4190       .access = PL1_RW, .type = ARM_CP_ALIAS,
4191       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4192       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4193     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4194       .access = PL1_RW,
4195       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4196       .resetvalue = 0, },
4197     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4198       .access = PL1_RW,
4199       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4200       .resetvalue = 0, },
4201     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4202       .access = PL1_RW,
4203       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4204     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4205       .access = PL1_RW,
4206       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4207     /* Protection region base and size registers */
4208     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4209       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4210       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4211     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4212       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4213       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4214     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4215       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4216       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4217     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4218       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4219       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4220     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4221       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4222       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4223     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4224       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4225       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4226     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4227       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4228       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4229     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4230       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4231       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4232 };
4233 
4234 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4235                              uint64_t value)
4236 {
4237     ARMCPU *cpu = env_archcpu(env);
4238 
4239     if (!arm_feature(env, ARM_FEATURE_V8)) {
4240         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4241             /*
4242              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4243              * using Long-descriptor translation table format
4244              */
4245             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4246         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4247             /*
4248              * In an implementation that includes the Security Extensions
4249              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4250              * Short-descriptor translation table format.
4251              */
4252             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4253         } else {
4254             value &= TTBCR_N;
4255         }
4256     }
4257 
4258     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4259         /*
4260          * With LPAE the TTBCR could result in a change of ASID
4261          * via the TTBCR.A1 bit, so do a TLB flush.
4262          */
4263         tlb_flush(CPU(cpu));
4264     }
4265     raw_write(env, ri, value);
4266 }
4267 
4268 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4269                                uint64_t value)
4270 {
4271     ARMCPU *cpu = env_archcpu(env);
4272 
4273     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4274     tlb_flush(CPU(cpu));
4275     raw_write(env, ri, value);
4276 }
4277 
4278 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4279                             uint64_t value)
4280 {
4281     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4282     if (cpreg_field_is_64bit(ri) &&
4283         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4284         ARMCPU *cpu = env_archcpu(env);
4285         tlb_flush(CPU(cpu));
4286     }
4287     raw_write(env, ri, value);
4288 }
4289 
4290 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4291                                     uint64_t value)
4292 {
4293     /*
4294      * If we are running with E2&0 regime, then an ASID is active.
4295      * Flush if that might be changing.  Note we're not checking
4296      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4297      * holds the active ASID, only checking the field that might.
4298      */
4299     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4300         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4301         uint16_t mask = ARMMMUIdxBit_E20_2 |
4302                         ARMMMUIdxBit_E20_2_PAN |
4303                         ARMMMUIdxBit_E20_0;
4304         tlb_flush_by_mmuidx(env_cpu(env), mask);
4305     }
4306     raw_write(env, ri, value);
4307 }
4308 
4309 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4310                         uint64_t value)
4311 {
4312     ARMCPU *cpu = env_archcpu(env);
4313     CPUState *cs = CPU(cpu);
4314 
4315     /*
4316      * A change in VMID to the stage2 page table (Stage2) invalidates
4317      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4318      */
4319     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4320         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4321     }
4322     raw_write(env, ri, value);
4323 }
4324 
4325 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4326     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4327       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4328       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4329                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4330     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4331       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4332       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4333                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4334     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4335       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4336       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4337                              offsetof(CPUARMState, cp15.dfar_ns) } },
4338     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4339       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4340       .access = PL1_RW, .accessfn = access_tvm_trvm,
4341       .fgt = FGT_FAR_EL1,
4342       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4343       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4344       .resetvalue = 0, },
4345 };
4346 
4347 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4348     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4349       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4350       .access = PL1_RW, .accessfn = access_tvm_trvm,
4351       .fgt = FGT_ESR_EL1,
4352       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4353       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4354     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4355       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4356       .access = PL1_RW, .accessfn = access_tvm_trvm,
4357       .fgt = FGT_TTBR0_EL1,
4358       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4359       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4360       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4361                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4362     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4363       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4364       .access = PL1_RW, .accessfn = access_tvm_trvm,
4365       .fgt = FGT_TTBR1_EL1,
4366       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4367       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4368       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4369                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4370     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4371       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4372       .access = PL1_RW, .accessfn = access_tvm_trvm,
4373       .fgt = FGT_TCR_EL1,
4374       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4375       .writefn = vmsa_tcr_el12_write,
4376       .raw_writefn = raw_write,
4377       .resetvalue = 0,
4378       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4379     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4380       .access = PL1_RW, .accessfn = access_tvm_trvm,
4381       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4382       .raw_writefn = raw_write,
4383       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4384                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4385 };
4386 
4387 /*
4388  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4389  * qemu tlbs nor adjusting cached masks.
4390  */
4391 static const ARMCPRegInfo ttbcr2_reginfo = {
4392     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4393     .access = PL1_RW, .accessfn = access_tvm_trvm,
4394     .type = ARM_CP_ALIAS,
4395     .bank_fieldoffsets = {
4396         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4397         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4398     },
4399 };
4400 
4401 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4402                                 uint64_t value)
4403 {
4404     env->cp15.c15_ticonfig = value & 0xe7;
4405     /* The OS_TYPE bit in this register changes the reported CPUID! */
4406     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4407         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4408 }
4409 
4410 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4411                                 uint64_t value)
4412 {
4413     env->cp15.c15_threadid = value & 0xffff;
4414 }
4415 
4416 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4417                            uint64_t value)
4418 {
4419     /* Wait-for-interrupt (deprecated) */
4420     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4421 }
4422 
4423 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4424                                   uint64_t value)
4425 {
4426     /*
4427      * On OMAP there are registers indicating the max/min index of dcache lines
4428      * containing a dirty line; cache flush operations have to reset these.
4429      */
4430     env->cp15.c15_i_max = 0x000;
4431     env->cp15.c15_i_min = 0xff0;
4432 }
4433 
4434 static const ARMCPRegInfo omap_cp_reginfo[] = {
4435     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4436       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4437       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4438       .resetvalue = 0, },
4439     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4440       .access = PL1_RW, .type = ARM_CP_NOP },
4441     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4442       .access = PL1_RW,
4443       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4444       .writefn = omap_ticonfig_write },
4445     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4446       .access = PL1_RW,
4447       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4448     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4449       .access = PL1_RW, .resetvalue = 0xff0,
4450       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4451     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4452       .access = PL1_RW,
4453       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4454       .writefn = omap_threadid_write },
4455     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4456       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4457       .type = ARM_CP_NO_RAW,
4458       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4459     /*
4460      * TODO: Peripheral port remap register:
4461      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4462      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4463      * when MMU is off.
4464      */
4465     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4466       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4467       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4468       .writefn = omap_cachemaint_write },
4469     { .name = "C9", .cp = 15, .crn = 9,
4470       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4471       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4472 };
4473 
4474 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4475                               uint64_t value)
4476 {
4477     env->cp15.c15_cpar = value & 0x3fff;
4478 }
4479 
4480 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4481     { .name = "XSCALE_CPAR",
4482       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4483       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4484       .writefn = xscale_cpar_write, },
4485     { .name = "XSCALE_AUXCR",
4486       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4487       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4488       .resetvalue = 0, },
4489     /*
4490      * XScale specific cache-lockdown: since we have no cache we NOP these
4491      * and hope the guest does not really rely on cache behaviour.
4492      */
4493     { .name = "XSCALE_LOCK_ICACHE_LINE",
4494       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4495       .access = PL1_W, .type = ARM_CP_NOP },
4496     { .name = "XSCALE_UNLOCK_ICACHE",
4497       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4498       .access = PL1_W, .type = ARM_CP_NOP },
4499     { .name = "XSCALE_DCACHE_LOCK",
4500       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4501       .access = PL1_RW, .type = ARM_CP_NOP },
4502     { .name = "XSCALE_UNLOCK_DCACHE",
4503       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4504       .access = PL1_W, .type = ARM_CP_NOP },
4505 };
4506 
4507 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4508     /*
4509      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4510      * implementation of this implementation-defined space.
4511      * Ideally this should eventually disappear in favour of actually
4512      * implementing the correct behaviour for all cores.
4513      */
4514     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4515       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4516       .access = PL1_RW,
4517       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4518       .resetvalue = 0 },
4519 };
4520 
4521 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4522     /* Cache status: RAZ because we have no cache so it's always clean */
4523     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4524       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4525       .resetvalue = 0 },
4526 };
4527 
4528 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4529     /* We never have a block transfer operation in progress */
4530     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4531       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4532       .resetvalue = 0 },
4533     /* The cache ops themselves: these all NOP for QEMU */
4534     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4535       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4536     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4537       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4538     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4539       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4540     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4541       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4542     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4543       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4544     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4545       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4546 };
4547 
4548 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4549     /*
4550      * The cache test-and-clean instructions always return (1 << 30)
4551      * to indicate that there are no dirty cache lines.
4552      */
4553     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4554       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4555       .resetvalue = (1 << 30) },
4556     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4557       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4558       .resetvalue = (1 << 30) },
4559 };
4560 
4561 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4562     /* Ignore ReadBuffer accesses */
4563     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4564       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4565       .access = PL1_RW, .resetvalue = 0,
4566       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4567 };
4568 
4569 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4570 {
4571     unsigned int cur_el = arm_current_el(env);
4572 
4573     if (arm_is_el2_enabled(env) && cur_el == 1) {
4574         return env->cp15.vpidr_el2;
4575     }
4576     return raw_read(env, ri);
4577 }
4578 
4579 static uint64_t mpidr_read_val(CPUARMState *env)
4580 {
4581     ARMCPU *cpu = env_archcpu(env);
4582     uint64_t mpidr = cpu->mp_affinity;
4583 
4584     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4585         mpidr |= (1U << 31);
4586         /*
4587          * Cores which are uniprocessor (non-coherent)
4588          * but still implement the MP extensions set
4589          * bit 30. (For instance, Cortex-R5).
4590          */
4591         if (cpu->mp_is_up) {
4592             mpidr |= (1u << 30);
4593         }
4594     }
4595     return mpidr;
4596 }
4597 
4598 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4599 {
4600     unsigned int cur_el = arm_current_el(env);
4601 
4602     if (arm_is_el2_enabled(env) && cur_el == 1) {
4603         return env->cp15.vmpidr_el2;
4604     }
4605     return mpidr_read_val(env);
4606 }
4607 
4608 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4609     /* NOP AMAIR0/1 */
4610     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4611       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4612       .access = PL1_RW, .accessfn = access_tvm_trvm,
4613       .fgt = FGT_AMAIR_EL1,
4614       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4615       .type = ARM_CP_CONST, .resetvalue = 0 },
4616     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4617     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4618       .access = PL1_RW, .accessfn = access_tvm_trvm,
4619       .type = ARM_CP_CONST, .resetvalue = 0 },
4620     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4621       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4622       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4623                              offsetof(CPUARMState, cp15.par_ns)} },
4624     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4625       .access = PL1_RW, .accessfn = access_tvm_trvm,
4626       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4627       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4628                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4629       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4630     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4631       .access = PL1_RW, .accessfn = access_tvm_trvm,
4632       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4633       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4634                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4635       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4636 };
4637 
4638 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4639 {
4640     return vfp_get_fpcr(env);
4641 }
4642 
4643 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4644                             uint64_t value)
4645 {
4646     vfp_set_fpcr(env, value);
4647 }
4648 
4649 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4650 {
4651     return vfp_get_fpsr(env);
4652 }
4653 
4654 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4655                             uint64_t value)
4656 {
4657     vfp_set_fpsr(env, value);
4658 }
4659 
4660 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4661                                        bool isread)
4662 {
4663     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4664         return CP_ACCESS_TRAP_EL1;
4665     }
4666     return CP_ACCESS_OK;
4667 }
4668 
4669 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4670                             uint64_t value)
4671 {
4672     env->daif = value & PSTATE_DAIF;
4673 }
4674 
4675 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4676 {
4677     return env->pstate & PSTATE_PAN;
4678 }
4679 
4680 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4681                            uint64_t value)
4682 {
4683     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4684 }
4685 
4686 static const ARMCPRegInfo pan_reginfo = {
4687     .name = "PAN", .state = ARM_CP_STATE_AA64,
4688     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4689     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4690     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4691 };
4692 
4693 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4694 {
4695     return env->pstate & PSTATE_UAO;
4696 }
4697 
4698 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4699                            uint64_t value)
4700 {
4701     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4702 }
4703 
4704 static const ARMCPRegInfo uao_reginfo = {
4705     .name = "UAO", .state = ARM_CP_STATE_AA64,
4706     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4707     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4708     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4709 };
4710 
4711 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4712 {
4713     return env->pstate & PSTATE_DIT;
4714 }
4715 
4716 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4717                            uint64_t value)
4718 {
4719     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4720 }
4721 
4722 static const ARMCPRegInfo dit_reginfo = {
4723     .name = "DIT", .state = ARM_CP_STATE_AA64,
4724     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4725     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4726     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4727 };
4728 
4729 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4730 {
4731     return env->pstate & PSTATE_SSBS;
4732 }
4733 
4734 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4735                            uint64_t value)
4736 {
4737     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4738 }
4739 
4740 static const ARMCPRegInfo ssbs_reginfo = {
4741     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4742     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4743     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4744     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4745 };
4746 
4747 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4748                                               const ARMCPRegInfo *ri,
4749                                               bool isread)
4750 {
4751     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4752     switch (arm_current_el(env)) {
4753     case 0:
4754         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4755         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4756             return CP_ACCESS_TRAP_EL1;
4757         }
4758         /* fall through */
4759     case 1:
4760         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4761         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4762             return CP_ACCESS_TRAP_EL2;
4763         }
4764         break;
4765     }
4766     return CP_ACCESS_OK;
4767 }
4768 
4769 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4770 {
4771     /* Cache invalidate/clean to Point of Unification... */
4772     switch (arm_current_el(env)) {
4773     case 0:
4774         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4775         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4776             return CP_ACCESS_TRAP_EL1;
4777         }
4778         /* fall through */
4779     case 1:
4780         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4781         if (arm_hcr_el2_eff(env) & hcrflags) {
4782             return CP_ACCESS_TRAP_EL2;
4783         }
4784         break;
4785     }
4786     return CP_ACCESS_OK;
4787 }
4788 
4789 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4790                                    bool isread)
4791 {
4792     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4793 }
4794 
4795 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4796                                   bool isread)
4797 {
4798     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4799 }
4800 
4801 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4802                                       bool isread)
4803 {
4804     int cur_el = arm_current_el(env);
4805 
4806     if (cur_el < 2) {
4807         uint64_t hcr = arm_hcr_el2_eff(env);
4808 
4809         if (cur_el == 0) {
4810             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4811                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4812                     return CP_ACCESS_TRAP_EL2;
4813                 }
4814             } else {
4815                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4816                     return CP_ACCESS_TRAP_EL1;
4817                 }
4818                 if (hcr & HCR_TDZ) {
4819                     return CP_ACCESS_TRAP_EL2;
4820                 }
4821             }
4822         } else if (hcr & HCR_TDZ) {
4823             return CP_ACCESS_TRAP_EL2;
4824         }
4825     }
4826     return CP_ACCESS_OK;
4827 }
4828 
4829 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4830 {
4831     ARMCPU *cpu = env_archcpu(env);
4832     int dzp_bit = 1 << 4;
4833 
4834     /* DZP indicates whether DC ZVA access is allowed */
4835     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4836         dzp_bit = 0;
4837     }
4838     return cpu->dcz_blocksize | dzp_bit;
4839 }
4840 
4841 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4842                                     bool isread)
4843 {
4844     if (!(env->pstate & PSTATE_SP)) {
4845         /*
4846          * Access to SP_EL0 is undefined if it's being used as
4847          * the stack pointer.
4848          */
4849         return CP_ACCESS_UNDEFINED;
4850     }
4851     return CP_ACCESS_OK;
4852 }
4853 
4854 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4855 {
4856     return env->pstate & PSTATE_SP;
4857 }
4858 
4859 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4860 {
4861     update_spsel(env, val);
4862 }
4863 
4864 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4865                         uint64_t value)
4866 {
4867     ARMCPU *cpu = env_archcpu(env);
4868 
4869     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4870         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4871         value &= ~SCTLR_M;
4872     }
4873 
4874     /* ??? Lots of these bits are not implemented.  */
4875 
4876     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4877         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4878             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4879         } else {
4880             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4881                        SCTLR_ATA0 | SCTLR_ATA);
4882         }
4883     }
4884 
4885     if (raw_read(env, ri) == value) {
4886         /*
4887          * Skip the TLB flush if nothing actually changed; Linux likes
4888          * to do a lot of pointless SCTLR writes.
4889          */
4890         return;
4891     }
4892 
4893     raw_write(env, ri, value);
4894 
4895     /* This may enable/disable the MMU, so do a TLB flush.  */
4896     tlb_flush(CPU(cpu));
4897 
4898     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4899         /*
4900          * Normally we would always end the TB on an SCTLR write; see the
4901          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4902          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4903          * of hflags from the translator, so do it here.
4904          */
4905         arm_rebuild_hflags(env);
4906     }
4907 }
4908 
4909 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4910                            uint64_t value)
4911 {
4912     /*
4913      * Some MDCR_EL3 bits affect whether PMU counters are running:
4914      * if we are trying to change any of those then we must
4915      * bracket this update with PMU start/finish calls.
4916      */
4917     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4918 
4919     if (pmu_op) {
4920         pmu_op_start(env);
4921     }
4922     env->cp15.mdcr_el3 = value;
4923     if (pmu_op) {
4924         pmu_op_finish(env);
4925     }
4926 }
4927 
4928 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4929                        uint64_t value)
4930 {
4931     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4932     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4933 }
4934 
4935 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4936                            uint64_t value)
4937 {
4938     /*
4939      * Some MDCR_EL2 bits affect whether PMU counters are running:
4940      * if we are trying to change any of those then we must
4941      * bracket this update with PMU start/finish calls.
4942      */
4943     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4944 
4945     if (pmu_op) {
4946         pmu_op_start(env);
4947     }
4948     env->cp15.mdcr_el2 = value;
4949     if (pmu_op) {
4950         pmu_op_finish(env);
4951     }
4952 }
4953 
4954 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4955                                  bool isread)
4956 {
4957     if (arm_current_el(env) == 1) {
4958         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4959 
4960         if (hcr_nv == (HCR_NV | HCR_NV1)) {
4961             return CP_ACCESS_TRAP_EL2;
4962         }
4963     }
4964     return CP_ACCESS_OK;
4965 }
4966 
4967 #ifdef CONFIG_USER_ONLY
4968 /*
4969  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4970  * code to get around W^X restrictions, where one region is writable and the
4971  * other is executable.
4972  *
4973  * Since the executable region is never written to we cannot detect code
4974  * changes when running in user mode, and rely on the emulated JIT telling us
4975  * that the code has changed by executing this instruction.
4976  */
4977 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4978                           uint64_t value)
4979 {
4980     uint64_t icache_line_mask, start_address, end_address;
4981     const ARMCPU *cpu;
4982 
4983     cpu = env_archcpu(env);
4984 
4985     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4986     start_address = value & ~icache_line_mask;
4987     end_address = value | icache_line_mask;
4988 
4989     mmap_lock();
4990 
4991     tb_invalidate_phys_range(env_cpu(env), start_address, end_address);
4992 
4993     mmap_unlock();
4994 }
4995 #endif
4996 
4997 static const ARMCPRegInfo v8_cp_reginfo[] = {
4998     /*
4999      * Minimal set of EL0-visible registers. This will need to be expanded
5000      * significantly for system emulation of AArch64 CPUs.
5001      */
5002     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5003       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5004       .access = PL0_RW, .type = ARM_CP_NZCV },
5005     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5006       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5007       .type = ARM_CP_NO_RAW,
5008       .access = PL0_RW, .accessfn = aa64_daif_access,
5009       .fieldoffset = offsetof(CPUARMState, daif),
5010       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5011     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5012       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5013       .access = PL0_RW, .type = ARM_CP_FPU,
5014       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5015     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5016       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5017       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5018       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5019     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5020       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5021       .access = PL0_R, .type = ARM_CP_NO_RAW,
5022       .fgt = FGT_DCZID_EL0,
5023       .readfn = aa64_dczid_read },
5024     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5025       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5026       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5027 #ifndef CONFIG_USER_ONLY
5028       /* Avoid overhead of an access check that always passes in user-mode */
5029       .accessfn = aa64_zva_access,
5030       .fgt = FGT_DCZVA,
5031 #endif
5032     },
5033     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5034       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5035       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5036     /*
5037      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5038      * don't emulate caches.
5039      */
5040     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5041       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5042       .access = PL1_W, .type = ARM_CP_NOP,
5043       .fgt = FGT_ICIALLUIS,
5044       .accessfn = access_ticab },
5045     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5046       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5047       .access = PL1_W, .type = ARM_CP_NOP,
5048       .fgt = FGT_ICIALLU,
5049       .accessfn = access_tocu },
5050     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5051       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5052       .access = PL0_W,
5053       .fgt = FGT_ICIVAU,
5054       .accessfn = access_tocu,
5055 #ifdef CONFIG_USER_ONLY
5056       .type = ARM_CP_NO_RAW,
5057       .writefn = ic_ivau_write
5058 #else
5059       .type = ARM_CP_NOP
5060 #endif
5061     },
5062     /* Cache ops: all NOPs since we don't emulate caches */
5063     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5064       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5065       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5066       .fgt = FGT_DCIVAC,
5067       .type = ARM_CP_NOP },
5068     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5069       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5070       .fgt = FGT_DCISW,
5071       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5072     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5073       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5074       .access = PL0_W, .type = ARM_CP_NOP,
5075       .fgt = FGT_DCCVAC,
5076       .accessfn = aa64_cacheop_poc_access },
5077     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5078       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5079       .fgt = FGT_DCCSW,
5080       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5081     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5082       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5083       .access = PL0_W, .type = ARM_CP_NOP,
5084       .fgt = FGT_DCCVAU,
5085       .accessfn = access_tocu },
5086     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5087       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5088       .access = PL0_W, .type = ARM_CP_NOP,
5089       .fgt = FGT_DCCIVAC,
5090       .accessfn = aa64_cacheop_poc_access },
5091     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5092       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5093       .fgt = FGT_DCCISW,
5094       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5095 #ifndef CONFIG_USER_ONLY
5096     /* 64 bit address translation operations */
5097     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5098       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5099       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5100       .fgt = FGT_ATS1E1R,
5101       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5102     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5103       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5104       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5105       .fgt = FGT_ATS1E1W,
5106       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5107     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5108       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5109       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5110       .fgt = FGT_ATS1E0R,
5111       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5112     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5113       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5114       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5115       .fgt = FGT_ATS1E0W,
5116       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5117     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5118       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5119       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5120       .accessfn = at_e012_access, .writefn = ats_write64 },
5121     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5122       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5123       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5124       .accessfn = at_e012_access, .writefn = ats_write64 },
5125     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5126       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5127       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5128       .accessfn = at_e012_access, .writefn = ats_write64 },
5129     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5130       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5131       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5132       .accessfn = at_e012_access, .writefn = ats_write64 },
5133     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5134     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5135       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5136       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5137       .writefn = ats_write64 },
5138     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5139       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5140       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5141       .writefn = ats_write64 },
5142     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5143       .type = ARM_CP_ALIAS,
5144       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5145       .access = PL1_RW, .resetvalue = 0,
5146       .fgt = FGT_PAR_EL1,
5147       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5148       .writefn = par_write },
5149 #endif
5150     /* 32 bit cache operations */
5151     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5152       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5153     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5154       .type = ARM_CP_NOP, .access = PL1_W },
5155     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5156       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5157     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5158       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5159     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5160       .type = ARM_CP_NOP, .access = PL1_W },
5161     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5162       .type = ARM_CP_NOP, .access = PL1_W },
5163     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5164       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5165     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5166       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5167     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5168       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5169     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5170       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5171     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5172       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5173     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5174       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5175     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5176       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5177     /* MMU Domain access control / MPU write buffer control */
5178     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5179       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5180       .writefn = dacr_write, .raw_writefn = raw_write,
5181       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5182                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5183     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5184       .type = ARM_CP_ALIAS,
5185       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5186       .access = PL1_RW, .accessfn = access_nv1,
5187       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5188       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5189     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5190       .type = ARM_CP_ALIAS,
5191       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5192       .access = PL1_RW, .accessfn = access_nv1,
5193       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5194       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5195     /*
5196      * We rely on the access checks not allowing the guest to write to the
5197      * state field when SPSel indicates that it's being used as the stack
5198      * pointer.
5199      */
5200     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5201       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5202       .access = PL1_RW, .accessfn = sp_el0_access,
5203       .type = ARM_CP_ALIAS,
5204       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5205     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5206       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5207       .nv2_redirect_offset = 0x240,
5208       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5209       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5210     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5211       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5212       .type = ARM_CP_NO_RAW,
5213       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5214     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5215       .type = ARM_CP_ALIAS,
5216       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5217       .access = PL2_RW,
5218       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5219     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5220       .type = ARM_CP_ALIAS,
5221       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5222       .access = PL2_RW,
5223       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5224     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5225       .type = ARM_CP_ALIAS,
5226       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5227       .access = PL2_RW,
5228       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5229     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5230       .type = ARM_CP_ALIAS,
5231       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5232       .access = PL2_RW,
5233       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5234     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5235       .type = ARM_CP_IO,
5236       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5237       .resetvalue = 0,
5238       .access = PL3_RW,
5239       .writefn = mdcr_el3_write,
5240       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5241     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5242       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5243       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5244       .writefn = sdcr_write,
5245       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5246 };
5247 
5248 /* These are present only when EL1 supports AArch32 */
5249 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5250     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5251       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5252       .access = PL2_RW,
5253       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5254       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5255     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5256       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5257       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5258       .writefn = dacr_write, .raw_writefn = raw_write,
5259       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5260     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5261       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5262       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5263       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5264 };
5265 
5266 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5267 {
5268     ARMCPU *cpu = env_archcpu(env);
5269 
5270     if (arm_feature(env, ARM_FEATURE_V8)) {
5271         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5272     } else {
5273         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5274     }
5275 
5276     if (arm_feature(env, ARM_FEATURE_EL3)) {
5277         valid_mask &= ~HCR_HCD;
5278     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5279         /*
5280          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5281          * However, if we're using the SMC PSCI conduit then QEMU is
5282          * effectively acting like EL3 firmware and so the guest at
5283          * EL2 should retain the ability to prevent EL1 from being
5284          * able to make SMC calls into the ersatz firmware, so in
5285          * that case HCR.TSC should be read/write.
5286          */
5287         valid_mask &= ~HCR_TSC;
5288     }
5289 
5290     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5291         if (cpu_isar_feature(aa64_vh, cpu)) {
5292             valid_mask |= HCR_E2H;
5293         }
5294         if (cpu_isar_feature(aa64_ras, cpu)) {
5295             valid_mask |= HCR_TERR | HCR_TEA;
5296         }
5297         if (cpu_isar_feature(aa64_lor, cpu)) {
5298             valid_mask |= HCR_TLOR;
5299         }
5300         if (cpu_isar_feature(aa64_pauth, cpu)) {
5301             valid_mask |= HCR_API | HCR_APK;
5302         }
5303         if (cpu_isar_feature(aa64_mte, cpu)) {
5304             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5305         }
5306         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5307             valid_mask |= HCR_ENSCXT;
5308         }
5309         if (cpu_isar_feature(aa64_fwb, cpu)) {
5310             valid_mask |= HCR_FWB;
5311         }
5312         if (cpu_isar_feature(aa64_rme, cpu)) {
5313             valid_mask |= HCR_GPF;
5314         }
5315         if (cpu_isar_feature(aa64_nv, cpu)) {
5316             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5317         }
5318         if (cpu_isar_feature(aa64_nv2, cpu)) {
5319             valid_mask |= HCR_NV2;
5320         }
5321     }
5322 
5323     if (cpu_isar_feature(any_evt, cpu)) {
5324         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5325     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5326         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5327     }
5328 
5329     /* Clear RES0 bits.  */
5330     value &= valid_mask;
5331 
5332     /* RW is RAO/WI if EL1 is AArch64 only */
5333     if (!cpu_isar_feature(aa64_aa32_el1, cpu)) {
5334         value |= HCR_RW;
5335     }
5336 
5337     /*
5338      * These bits change the MMU setup:
5339      * HCR_VM enables stage 2 translation
5340      * HCR_PTW forbids certain page-table setups
5341      * HCR_DC disables stage1 and enables stage2 translation
5342      * HCR_DCT enables tagging on (disabled) stage1 translation
5343      * HCR_FWB changes the interpretation of stage2 descriptor bits
5344      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5345      */
5346     if ((env->cp15.hcr_el2 ^ value) &
5347         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5348         tlb_flush(CPU(cpu));
5349     }
5350     env->cp15.hcr_el2 = value;
5351 
5352     /*
5353      * Updates to VI and VF require us to update the status of
5354      * virtual interrupts, which are the logical OR of these bits
5355      * and the state of the input lines from the GIC. (This requires
5356      * that we have the BQL, which is done by marking the
5357      * reginfo structs as ARM_CP_IO.)
5358      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5359      * VFNMI, it is never possible for it to be taken immediately
5360      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5361      * at EL0 or EL1, and HCR can only be written at EL2.
5362      */
5363     g_assert(bql_locked());
5364     arm_cpu_update_virq(cpu);
5365     arm_cpu_update_vfiq(cpu);
5366     arm_cpu_update_vserr(cpu);
5367     if (cpu_isar_feature(aa64_nmi, cpu)) {
5368         arm_cpu_update_vinmi(cpu);
5369         arm_cpu_update_vfnmi(cpu);
5370     }
5371 }
5372 
5373 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5374 {
5375     do_hcr_write(env, value, 0);
5376 }
5377 
5378 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5379                           uint64_t value)
5380 {
5381     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5382     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5383     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5384 }
5385 
5386 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5387                          uint64_t value)
5388 {
5389     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5390     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5391     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5392 }
5393 
5394 static void hcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
5395 {
5396     /* hcr_write will set the RES1 bits on an AArch64-only CPU */
5397     hcr_write(env, ri, 0);
5398 }
5399 
5400 /*
5401  * Return the effective value of HCR_EL2, at the given security state.
5402  * Bits that are not included here:
5403  * RW       (read from SCR_EL3.RW as needed)
5404  */
5405 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5406 {
5407     uint64_t ret = env->cp15.hcr_el2;
5408 
5409     assert(space != ARMSS_Root);
5410 
5411     if (!arm_is_el2_enabled_secstate(env, space)) {
5412         /*
5413          * "This register has no effect if EL2 is not enabled in the
5414          * current Security state".  This is ARMv8.4-SecEL2 speak for
5415          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5416          *
5417          * Prior to that, the language was "In an implementation that
5418          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5419          * as if this field is 0 for all purposes other than a direct
5420          * read or write access of HCR_EL2".  With lots of enumeration
5421          * on a per-field basis.  In current QEMU, this is condition
5422          * is arm_is_secure_below_el3.
5423          *
5424          * Since the v8.4 language applies to the entire register, and
5425          * appears to be backward compatible, use that.
5426          */
5427         return 0;
5428     }
5429 
5430     /*
5431      * For a cpu that supports both aarch64 and aarch32, we can set bits
5432      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5433      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5434      */
5435     if (!arm_el_is_aa64(env, 2)) {
5436         uint64_t aa32_valid;
5437 
5438         /*
5439          * These bits are up-to-date as of ARMv8.6.
5440          * For HCR, it's easiest to list just the 2 bits that are invalid.
5441          * For HCR2, list those that are valid.
5442          */
5443         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5444         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5445                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5446         ret &= aa32_valid;
5447     }
5448 
5449     if (ret & HCR_TGE) {
5450         /* These bits are up-to-date as of ARMv8.6.  */
5451         if (ret & HCR_E2H) {
5452             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5453                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5454                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5455                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5456                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5457                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5458         } else {
5459             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5460         }
5461         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5462                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5463                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5464                  HCR_TLOR);
5465     }
5466 
5467     return ret;
5468 }
5469 
5470 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5471 {
5472     if (arm_feature(env, ARM_FEATURE_M)) {
5473         return 0;
5474     }
5475     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5476 }
5477 
5478 /*
5479  * Corresponds to ARM pseudocode function ELIsInHost().
5480  */
5481 bool el_is_in_host(CPUARMState *env, int el)
5482 {
5483     uint64_t mask;
5484 
5485     /*
5486      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5487      * Perform the simplest bit tests first, and validate EL2 afterward.
5488      */
5489     if (el & 1) {
5490         return false; /* EL1 or EL3 */
5491     }
5492 
5493     /*
5494      * Note that hcr_write() checks isar_feature_aa64_vh(),
5495      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5496      */
5497     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5498     if ((env->cp15.hcr_el2 & mask) != mask) {
5499         return false;
5500     }
5501 
5502     /* TGE and/or E2H set: double check those bits are currently legal. */
5503     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5504 }
5505 
5506 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5507                        uint64_t value)
5508 {
5509     ARMCPU *cpu = env_archcpu(env);
5510     uint64_t valid_mask = 0;
5511 
5512     /* FEAT_MOPS adds MSCEn and MCE2 */
5513     if (cpu_isar_feature(aa64_mops, cpu)) {
5514         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5515     }
5516 
5517     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5518     if (cpu_isar_feature(aa64_nmi, cpu)) {
5519         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5520     }
5521     /* FEAT_CMOW adds CMOW */
5522     if (cpu_isar_feature(aa64_cmow, cpu)) {
5523         valid_mask |= HCRX_CMOW;
5524     }
5525     /* FEAT_XS adds FGTnXS, FnXS */
5526     if (cpu_isar_feature(aa64_xs, cpu)) {
5527         valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5528     }
5529 
5530     /* Clear RES0 bits.  */
5531     env->cp15.hcrx_el2 = value & valid_mask;
5532 
5533     /*
5534      * Updates to VINMI and VFNMI require us to update the status of
5535      * virtual NMI, which are the logical OR of these bits
5536      * and the state of the input lines from the GIC. (This requires
5537      * that we have the BQL, which is done by marking the
5538      * reginfo structs as ARM_CP_IO.)
5539      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5540      * possible for it to be taken immediately, because VINMI and
5541      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5542      * can only be written at EL2.
5543      */
5544     if (cpu_isar_feature(aa64_nmi, cpu)) {
5545         g_assert(bql_locked());
5546         arm_cpu_update_vinmi(cpu);
5547         arm_cpu_update_vfnmi(cpu);
5548     }
5549 }
5550 
5551 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5552                                   bool isread)
5553 {
5554     if (arm_current_el(env) == 2
5555         && arm_feature(env, ARM_FEATURE_EL3)
5556         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5557         return CP_ACCESS_TRAP_EL3;
5558     }
5559     return CP_ACCESS_OK;
5560 }
5561 
5562 static const ARMCPRegInfo hcrx_el2_reginfo = {
5563     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5564     .type = ARM_CP_IO,
5565     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5566     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5567     .nv2_redirect_offset = 0xa0,
5568     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5569 };
5570 
5571 /* Return the effective value of HCRX_EL2.  */
5572 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5573 {
5574     /*
5575      * The bits in this register behave as 0 for all purposes other than
5576      * direct reads of the register if SCR_EL3.HXEn is 0.
5577      * If EL2 is not enabled in the current security state, then the
5578      * bit may behave as if 0, or as if 1, depending on the bit.
5579      * For the moment, we treat the EL2-disabled case as taking
5580      * priority over the HXEn-disabled case. This is true for the only
5581      * bit for a feature which we implement where the answer is different
5582      * for the two cases (MSCEn for FEAT_MOPS).
5583      * This may need to be revisited for future bits.
5584      */
5585     if (!arm_is_el2_enabled(env)) {
5586         uint64_t hcrx = 0;
5587         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5588             /* MSCEn behaves as 1 if EL2 is not enabled */
5589             hcrx |= HCRX_MSCEN;
5590         }
5591         return hcrx;
5592     }
5593     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5594         return 0;
5595     }
5596     return env->cp15.hcrx_el2;
5597 }
5598 
5599 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5600                            uint64_t value)
5601 {
5602     /*
5603      * For A-profile AArch32 EL3, if NSACR.CP10
5604      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5605      */
5606     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5607         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5608         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5609         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5610     }
5611     env->cp15.cptr_el[2] = value;
5612 }
5613 
5614 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5615 {
5616     /*
5617      * For A-profile AArch32 EL3, if NSACR.CP10
5618      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5619      */
5620     uint64_t value = env->cp15.cptr_el[2];
5621 
5622     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5623         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5624         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5625     }
5626     return value;
5627 }
5628 
5629 static const ARMCPRegInfo el2_cp_reginfo[] = {
5630     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5631       .type = ARM_CP_IO,
5632       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5633       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5634       .nv2_redirect_offset = 0x78,
5635       .resetfn = hcr_reset,
5636       .writefn = hcr_write, .raw_writefn = raw_write },
5637     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5638       .type = ARM_CP_ALIAS | ARM_CP_IO,
5639       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5640       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5641       .writefn = hcr_writelow },
5642     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5643       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5644       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5645     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5646       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5647       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5648       .access = PL2_RW,
5649       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5650     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5651       .type = ARM_CP_NV2_REDIRECT,
5652       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5653       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5654     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5655       .type = ARM_CP_NV2_REDIRECT,
5656       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5657       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5658     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5659       .type = ARM_CP_ALIAS,
5660       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5661       .access = PL2_RW,
5662       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5663     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5664       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5665       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5666       .access = PL2_RW,
5667       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5668     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5669       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5670       .access = PL2_RW, .writefn = vbar_write,
5671       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5672       .resetvalue = 0 },
5673     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5674       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5675       .access = PL3_RW, .type = ARM_CP_ALIAS,
5676       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5677     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5678       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5679       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5680       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5681       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5682     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5683       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5684       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5685       .resetvalue = 0 },
5686     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5687       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5688       .access = PL2_RW, .type = ARM_CP_ALIAS,
5689       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5690     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5691       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5692       .access = PL2_RW, .type = ARM_CP_CONST,
5693       .resetvalue = 0 },
5694     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5695     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5696       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5697       .access = PL2_RW, .type = ARM_CP_CONST,
5698       .resetvalue = 0 },
5699     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5700       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5701       .access = PL2_RW, .type = ARM_CP_CONST,
5702       .resetvalue = 0 },
5703     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5704       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5705       .access = PL2_RW, .type = ARM_CP_CONST,
5706       .resetvalue = 0 },
5707     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5708       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5709       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5710       .raw_writefn = raw_write,
5711       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5712     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5713       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5714       .type = ARM_CP_ALIAS,
5715       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5716       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5717     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5718       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5719       .access = PL2_RW,
5720       .nv2_redirect_offset = 0x40,
5721       /* no .writefn needed as this can't cause an ASID change */
5722       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5723     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5724       .cp = 15, .opc1 = 6, .crm = 2,
5725       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5726       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5727       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5728       .writefn = vttbr_write, .raw_writefn = raw_write },
5729     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5730       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5731       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5732       .nv2_redirect_offset = 0x20,
5733       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5734     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5735       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5736       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5737       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5738     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5739       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5740       .access = PL2_RW, .resetvalue = 0,
5741       .nv2_redirect_offset = 0x90,
5742       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5743     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5744       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5745       .access = PL2_RW, .resetvalue = 0,
5746       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5747       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5748     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5749       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5750       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5751 #ifndef CONFIG_USER_ONLY
5752     /*
5753      * Unlike the other EL2-related AT operations, these must
5754      * UNDEF from EL3 if EL2 is not implemented, which is why we
5755      * define them here rather than with the rest of the AT ops.
5756      */
5757     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5758       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5759       .access = PL2_W, .accessfn = at_s1e2_access,
5760       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5761       .writefn = ats_write64 },
5762     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5763       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5764       .access = PL2_W, .accessfn = at_s1e2_access,
5765       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5766       .writefn = ats_write64 },
5767     /*
5768      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5769      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5770      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5771      * to behave as if SCR.NS was 1.
5772      */
5773     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5774       .access = PL2_W,
5775       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5776     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5777       .access = PL2_W,
5778       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5779     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5780       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5781       /*
5782        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5783        * reset values as IMPDEF. We choose to reset to 3 to comply with
5784        * both ARMv7 and ARMv8.
5785        */
5786       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5787       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5788       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5789     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5790       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5791       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5792       .writefn = gt_cntvoff_write,
5793       .nv2_redirect_offset = 0x60,
5794       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5795     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5796       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5797       .writefn = gt_cntvoff_write,
5798       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5799     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5800       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5801       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5802       .type = ARM_CP_IO, .access = PL2_RW,
5803       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5804     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5805       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5806       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5807       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5808     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5809       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5810       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5811       .resetfn = gt_hyp_timer_reset,
5812       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5813     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5814       .type = ARM_CP_IO,
5815       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5816       .access = PL2_RW,
5817       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5818       .resetvalue = 0,
5819       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5820 #endif
5821     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5822       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5823       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5824       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5825     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5826       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5827       .access = PL2_RW,
5828       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5829     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5830       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5831       .access = PL2_RW,
5832       .nv2_redirect_offset = 0x80,
5833       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5834 };
5835 
5836 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5837     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5838       .type = ARM_CP_ALIAS | ARM_CP_IO,
5839       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5840       .access = PL2_RW,
5841       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5842       .writefn = hcr_writehigh },
5843 };
5844 
5845 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5846                                   bool isread)
5847 {
5848     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5849         return CP_ACCESS_OK;
5850     }
5851     return CP_ACCESS_UNDEFINED;
5852 }
5853 
5854 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5855     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5856       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5857       .access = PL2_RW, .accessfn = sel2_access,
5858       .nv2_redirect_offset = 0x30,
5859       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5860     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5861       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5862       .access = PL2_RW, .accessfn = sel2_access,
5863       .nv2_redirect_offset = 0x48,
5864       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5865 #ifndef CONFIG_USER_ONLY
5866     /* Secure EL2 Physical Timer */
5867     { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5868       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
5869       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5870       .accessfn = gt_sel2timer_access,
5871       .readfn = gt_sec_pel2_tval_read,
5872       .writefn = gt_sec_pel2_tval_write,
5873       .resetfn = gt_sec_pel2_timer_reset,
5874     },
5875     { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5876       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
5877       .type = ARM_CP_IO, .access = PL2_RW,
5878       .accessfn = gt_sel2timer_access,
5879       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
5880       .resetvalue = 0,
5881       .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
5882     },
5883     { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5884       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
5885       .type = ARM_CP_IO, .access = PL2_RW,
5886       .accessfn = gt_sel2timer_access,
5887       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
5888       .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
5889     },
5890     /* Secure EL2 Virtual Timer */
5891     { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5892       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
5893       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5894       .accessfn = gt_sel2timer_access,
5895       .readfn = gt_sec_vel2_tval_read,
5896       .writefn = gt_sec_vel2_tval_write,
5897       .resetfn = gt_sec_vel2_timer_reset,
5898     },
5899     { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5900       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
5901       .type = ARM_CP_IO, .access = PL2_RW,
5902       .accessfn = gt_sel2timer_access,
5903       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
5904       .resetvalue = 0,
5905       .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
5906     },
5907     { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5908       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
5909       .type = ARM_CP_IO, .access = PL2_RW,
5910       .accessfn = gt_sel2timer_access,
5911       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
5912       .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
5913     },
5914 #endif
5915 };
5916 
5917 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5918                                    bool isread)
5919 {
5920     /*
5921      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5922      * At Secure EL1 it traps to EL3 or EL2.
5923      */
5924     if (arm_current_el(env) == 3) {
5925         return CP_ACCESS_OK;
5926     }
5927     if (arm_is_secure_below_el3(env)) {
5928         if (env->cp15.scr_el3 & SCR_EEL2) {
5929             return CP_ACCESS_TRAP_EL2;
5930         }
5931         return CP_ACCESS_TRAP_EL3;
5932     }
5933     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5934     if (isread) {
5935         return CP_ACCESS_OK;
5936     }
5937     return CP_ACCESS_UNDEFINED;
5938 }
5939 
5940 static const ARMCPRegInfo el3_cp_reginfo[] = {
5941     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5942       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5943       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5944       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5945     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5946       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5947       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5948       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5949       .writefn = scr_write, .raw_writefn = raw_write },
5950     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5951       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5952       .access = PL3_RW, .resetvalue = 0,
5953       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5954     { .name = "SDER",
5955       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5956       .access = PL3_RW, .resetvalue = 0,
5957       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5958     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5959       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5960       .writefn = vbar_write, .resetvalue = 0,
5961       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5962     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5963       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5964       .access = PL3_RW, .resetvalue = 0,
5965       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5966     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5967       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5968       .access = PL3_RW,
5969       /* no .writefn needed as this can't cause an ASID change */
5970       .resetvalue = 0,
5971       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5972     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5973       .type = ARM_CP_ALIAS,
5974       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5975       .access = PL3_RW,
5976       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5977     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5978       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5979       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5980     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5981       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5982       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5983     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5984       .type = ARM_CP_ALIAS,
5985       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5986       .access = PL3_RW,
5987       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5988     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5989       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5990       .access = PL3_RW, .writefn = vbar_write,
5991       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5992       .resetvalue = 0 },
5993     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5994       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5995       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5996       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5997     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5998       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5999       .access = PL3_RW, .resetvalue = 0,
6000       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6001     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6002       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6003       .access = PL3_RW, .type = ARM_CP_CONST,
6004       .resetvalue = 0 },
6005     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6006       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6007       .access = PL3_RW, .type = ARM_CP_CONST,
6008       .resetvalue = 0 },
6009     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6010       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6011       .access = PL3_RW, .type = ARM_CP_CONST,
6012       .resetvalue = 0 },
6013 };
6014 
6015 #ifndef CONFIG_USER_ONLY
6016 
6017 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6018                                  bool isread)
6019 {
6020     if (arm_current_el(env) == 1) {
6021         /* This must be a FEAT_NV access */
6022         return CP_ACCESS_OK;
6023     }
6024     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6025         return CP_ACCESS_UNDEFINED;
6026     }
6027     return CP_ACCESS_OK;
6028 }
6029 
6030 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6031                                       bool isread)
6032 {
6033     if (arm_current_el(env) == 1) {
6034         /* This must be a FEAT_NV access with NVx == 101 */
6035         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6036             return CP_ACCESS_TRAP_EL2;
6037         }
6038     }
6039     return e2h_access(env, ri, isread);
6040 }
6041 
6042 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6043                                       bool isread)
6044 {
6045     if (arm_current_el(env) == 1) {
6046         /* This must be a FEAT_NV access with NVx == 101 */
6047         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6048             return CP_ACCESS_TRAP_EL2;
6049         }
6050     }
6051     return e2h_access(env, ri, isread);
6052 }
6053 
6054 /* Test if system register redirection is to occur in the current state.  */
6055 static bool redirect_for_e2h(CPUARMState *env)
6056 {
6057     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6058 }
6059 
6060 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6061 {
6062     CPReadFn *readfn;
6063 
6064     if (redirect_for_e2h(env)) {
6065         /* Switch to the saved EL2 version of the register.  */
6066         ri = ri->opaque;
6067         readfn = ri->readfn;
6068     } else {
6069         readfn = ri->orig_readfn;
6070     }
6071     if (readfn == NULL) {
6072         readfn = raw_read;
6073     }
6074     return readfn(env, ri);
6075 }
6076 
6077 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6078                           uint64_t value)
6079 {
6080     CPWriteFn *writefn;
6081 
6082     if (redirect_for_e2h(env)) {
6083         /* Switch to the saved EL2 version of the register.  */
6084         ri = ri->opaque;
6085         writefn = ri->writefn;
6086     } else {
6087         writefn = ri->orig_writefn;
6088     }
6089     if (writefn == NULL) {
6090         writefn = raw_write;
6091     }
6092     writefn(env, ri, value);
6093 }
6094 
6095 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6096 {
6097     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6098     return ri->orig_readfn(env, ri->opaque);
6099 }
6100 
6101 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6102                               uint64_t value)
6103 {
6104     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6105     return ri->orig_writefn(env, ri->opaque, value);
6106 }
6107 
6108 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6109                                          const ARMCPRegInfo *ri,
6110                                          bool isread)
6111 {
6112     if (arm_current_el(env) == 1) {
6113         /*
6114          * This must be a FEAT_NV access (will either trap or redirect
6115          * to memory). None of the registers with _EL12 aliases want to
6116          * apply their trap controls for this kind of access, so don't
6117          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6118          */
6119         return CP_ACCESS_OK;
6120     }
6121     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6122     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6123         return CP_ACCESS_UNDEFINED;
6124     }
6125     if (ri->orig_accessfn) {
6126         return ri->orig_accessfn(env, ri->opaque, isread);
6127     }
6128     return CP_ACCESS_OK;
6129 }
6130 
6131 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6132 {
6133     struct E2HAlias {
6134         uint32_t src_key, dst_key, new_key;
6135         const char *src_name, *dst_name, *new_name;
6136         bool (*feature)(const ARMISARegisters *id);
6137     };
6138 
6139 #define K(op0, op1, crn, crm, op2) \
6140     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6141 
6142     static const struct E2HAlias aliases[] = {
6143         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6144           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6145         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6146           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6147         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6148           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6149         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6150           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6151         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6152           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6153         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6154           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6155         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6156           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6157         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6158           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6159         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6160           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6161         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6162           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6163         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6164           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6165         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6166           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6167         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6168           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6169         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6170           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6171         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6172           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6173         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6174           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6175 
6176         /*
6177          * Note that redirection of ZCR is mentioned in the description
6178          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6179          * not in the summary table.
6180          */
6181         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6182           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6183         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6184           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6185 
6186         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6187           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6188 
6189         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6190           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6191           isar_feature_aa64_scxtnum },
6192 
6193         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6194         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6195     };
6196 #undef K
6197 
6198     size_t i;
6199 
6200     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6201         const struct E2HAlias *a = &aliases[i];
6202         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6203         bool ok;
6204 
6205         if (a->feature && !a->feature(&cpu->isar)) {
6206             continue;
6207         }
6208 
6209         src_reg = g_hash_table_lookup(cpu->cp_regs,
6210                                       (gpointer)(uintptr_t)a->src_key);
6211         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6212                                       (gpointer)(uintptr_t)a->dst_key);
6213         g_assert(src_reg != NULL);
6214         g_assert(dst_reg != NULL);
6215 
6216         /* Cross-compare names to detect typos in the keys.  */
6217         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6218         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6219 
6220         /* None of the core system registers use opaque; we will.  */
6221         g_assert(src_reg->opaque == NULL);
6222 
6223         /* Create alias before redirection so we dup the right data. */
6224         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6225 
6226         new_reg->name = a->new_name;
6227         new_reg->type |= ARM_CP_ALIAS;
6228         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6229         new_reg->access &= PL2_RW | PL3_RW;
6230         /* The new_reg op fields are as per new_key, not the target reg */
6231         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6232             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6233         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6234             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6235         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6236             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6237         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6238             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6239         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6240             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6241         new_reg->opaque = src_reg;
6242         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6243         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6244         new_reg->orig_accessfn = src_reg->accessfn;
6245         if (!new_reg->raw_readfn) {
6246             new_reg->raw_readfn = raw_read;
6247         }
6248         if (!new_reg->raw_writefn) {
6249             new_reg->raw_writefn = raw_write;
6250         }
6251         new_reg->readfn = el2_e2h_e12_read;
6252         new_reg->writefn = el2_e2h_e12_write;
6253         new_reg->accessfn = el2_e2h_e12_access;
6254 
6255         /*
6256          * If the _EL1 register is redirected to memory by FEAT_NV2,
6257          * then it shares the offset with the _EL12 register,
6258          * and which one is redirected depends on HCR_EL2.NV1.
6259          */
6260         if (new_reg->nv2_redirect_offset) {
6261             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6262             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6263             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6264         }
6265 
6266         ok = g_hash_table_insert(cpu->cp_regs,
6267                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6268         g_assert(ok);
6269 
6270         src_reg->opaque = dst_reg;
6271         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6272         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6273         if (!src_reg->raw_readfn) {
6274             src_reg->raw_readfn = raw_read;
6275         }
6276         if (!src_reg->raw_writefn) {
6277             src_reg->raw_writefn = raw_write;
6278         }
6279         src_reg->readfn = el2_e2h_read;
6280         src_reg->writefn = el2_e2h_write;
6281     }
6282 }
6283 #endif
6284 
6285 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6286                                      bool isread)
6287 {
6288     int cur_el = arm_current_el(env);
6289 
6290     if (cur_el < 2) {
6291         uint64_t hcr = arm_hcr_el2_eff(env);
6292 
6293         if (cur_el == 0) {
6294             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6295                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6296                     return CP_ACCESS_TRAP_EL2;
6297                 }
6298             } else {
6299                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6300                     return CP_ACCESS_TRAP_EL1;
6301                 }
6302                 if (hcr & HCR_TID2) {
6303                     return CP_ACCESS_TRAP_EL2;
6304                 }
6305             }
6306         } else if (hcr & HCR_TID2) {
6307             return CP_ACCESS_TRAP_EL2;
6308         }
6309     }
6310 
6311     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6312         return CP_ACCESS_TRAP_EL2;
6313     }
6314 
6315     return CP_ACCESS_OK;
6316 }
6317 
6318 /*
6319  * Check for traps to RAS registers, which are controlled
6320  * by HCR_EL2.TERR and SCR_EL3.TERR.
6321  */
6322 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6323                                   bool isread)
6324 {
6325     int el = arm_current_el(env);
6326 
6327     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6328         return CP_ACCESS_TRAP_EL2;
6329     }
6330     if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
6331         return CP_ACCESS_TRAP_EL3;
6332     }
6333     return CP_ACCESS_OK;
6334 }
6335 
6336 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6337 {
6338     int el = arm_current_el(env);
6339 
6340     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6341         return env->cp15.vdisr_el2;
6342     }
6343     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6344         return 0; /* RAZ/WI */
6345     }
6346     return env->cp15.disr_el1;
6347 }
6348 
6349 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6350 {
6351     int el = arm_current_el(env);
6352 
6353     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6354         env->cp15.vdisr_el2 = val;
6355         return;
6356     }
6357     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6358         return; /* RAZ/WI */
6359     }
6360     env->cp15.disr_el1 = val;
6361 }
6362 
6363 /*
6364  * Minimal RAS implementation with no Error Records.
6365  * Which means that all of the Error Record registers:
6366  *   ERXADDR_EL1
6367  *   ERXCTLR_EL1
6368  *   ERXFR_EL1
6369  *   ERXMISC0_EL1
6370  *   ERXMISC1_EL1
6371  *   ERXMISC2_EL1
6372  *   ERXMISC3_EL1
6373  *   ERXPFGCDN_EL1  (RASv1p1)
6374  *   ERXPFGCTL_EL1  (RASv1p1)
6375  *   ERXPFGF_EL1    (RASv1p1)
6376  *   ERXSTATUS_EL1
6377  * and
6378  *   ERRSELR_EL1
6379  * may generate UNDEFINED, which is the effect we get by not
6380  * listing them at all.
6381  *
6382  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6383  * is higher priority than FGT-to-EL2 so we do not need to list them
6384  * in order to check for an FGT.
6385  */
6386 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6387     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6388       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6389       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6390       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6391     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6392       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6393       .access = PL1_R, .accessfn = access_terr,
6394       .fgt = FGT_ERRIDR_EL1,
6395       .type = ARM_CP_CONST, .resetvalue = 0 },
6396     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6397       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6398       .nv2_redirect_offset = 0x500,
6399       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6400     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6401       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6402       .nv2_redirect_offset = 0x508,
6403       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6404 };
6405 
6406 /*
6407  * Return the exception level to which exceptions should be taken
6408  * via SVEAccessTrap.  This excludes the check for whether the exception
6409  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6410  * be found by testing 0 < fp_exception_el < sve_exception_el.
6411  *
6412  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6413  * pseudocode does *not* separate out the FP trap checks, but has them
6414  * all in one function.
6415  */
6416 int sve_exception_el(CPUARMState *env, int el)
6417 {
6418 #ifndef CONFIG_USER_ONLY
6419     if (el <= 1 && !el_is_in_host(env, el)) {
6420         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6421         case 1:
6422             if (el != 0) {
6423                 break;
6424             }
6425             /* fall through */
6426         case 0:
6427         case 2:
6428             return 1;
6429         }
6430     }
6431 
6432     if (el <= 2 && arm_is_el2_enabled(env)) {
6433         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6434         if (env->cp15.hcr_el2 & HCR_E2H) {
6435             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6436             case 1:
6437                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6438                     break;
6439                 }
6440                 /* fall through */
6441             case 0:
6442             case 2:
6443                 return 2;
6444             }
6445         } else {
6446             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6447                 return 2;
6448             }
6449         }
6450     }
6451 
6452     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6453     if (arm_feature(env, ARM_FEATURE_EL3)
6454         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6455         return 3;
6456     }
6457 #endif
6458     return 0;
6459 }
6460 
6461 /*
6462  * Return the exception level to which exceptions should be taken for SME.
6463  * C.f. the ARM pseudocode function CheckSMEAccess.
6464  */
6465 int sme_exception_el(CPUARMState *env, int el)
6466 {
6467 #ifndef CONFIG_USER_ONLY
6468     if (el <= 1 && !el_is_in_host(env, el)) {
6469         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6470         case 1:
6471             if (el != 0) {
6472                 break;
6473             }
6474             /* fall through */
6475         case 0:
6476         case 2:
6477             return 1;
6478         }
6479     }
6480 
6481     if (el <= 2 && arm_is_el2_enabled(env)) {
6482         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6483         if (env->cp15.hcr_el2 & HCR_E2H) {
6484             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6485             case 1:
6486                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6487                     break;
6488                 }
6489                 /* fall through */
6490             case 0:
6491             case 2:
6492                 return 2;
6493             }
6494         } else {
6495             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6496                 return 2;
6497             }
6498         }
6499     }
6500 
6501     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6502     if (arm_feature(env, ARM_FEATURE_EL3)
6503         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6504         return 3;
6505     }
6506 #endif
6507     return 0;
6508 }
6509 
6510 /*
6511  * Given that SVE is enabled, return the vector length for EL.
6512  */
6513 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6514 {
6515     ARMCPU *cpu = env_archcpu(env);
6516     uint64_t *cr = env->vfp.zcr_el;
6517     uint32_t map = cpu->sve_vq.map;
6518     uint32_t len = ARM_MAX_VQ - 1;
6519 
6520     if (sm) {
6521         cr = env->vfp.smcr_el;
6522         map = cpu->sme_vq.map;
6523     }
6524 
6525     if (el <= 1 && !el_is_in_host(env, el)) {
6526         len = MIN(len, 0xf & (uint32_t)cr[1]);
6527     }
6528     if (el <= 2 && arm_is_el2_enabled(env)) {
6529         len = MIN(len, 0xf & (uint32_t)cr[2]);
6530     }
6531     if (arm_feature(env, ARM_FEATURE_EL3)) {
6532         len = MIN(len, 0xf & (uint32_t)cr[3]);
6533     }
6534 
6535     map &= MAKE_64BIT_MASK(0, len + 1);
6536     if (map != 0) {
6537         return 31 - clz32(map);
6538     }
6539 
6540     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6541     assert(sm);
6542     return ctz32(cpu->sme_vq.map);
6543 }
6544 
6545 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6546 {
6547     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6548 }
6549 
6550 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6551                       uint64_t value)
6552 {
6553     int cur_el = arm_current_el(env);
6554     int old_len = sve_vqm1_for_el(env, cur_el);
6555     int new_len;
6556 
6557     /* Bits other than [3:0] are RAZ/WI.  */
6558     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6559     raw_write(env, ri, value & 0xf);
6560 
6561     /*
6562      * Because we arrived here, we know both FP and SVE are enabled;
6563      * otherwise we would have trapped access to the ZCR_ELn register.
6564      */
6565     new_len = sve_vqm1_for_el(env, cur_el);
6566     if (new_len < old_len) {
6567 #ifdef TARGET_AARCH64
6568         aarch64_sve_narrow_vq(env, new_len + 1);
6569 #endif
6570     }
6571 }
6572 
6573 static const ARMCPRegInfo zcr_reginfo[] = {
6574     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6575       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6576       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6577       .access = PL1_RW, .type = ARM_CP_SVE,
6578       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6579       .writefn = zcr_write, .raw_writefn = raw_write },
6580     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6581       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6582       .access = PL2_RW, .type = ARM_CP_SVE,
6583       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6584       .writefn = zcr_write, .raw_writefn = raw_write },
6585     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6586       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6587       .access = PL3_RW, .type = ARM_CP_SVE,
6588       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6589       .writefn = zcr_write, .raw_writefn = raw_write },
6590 };
6591 
6592 #ifdef TARGET_AARCH64
6593 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6594                                     bool isread)
6595 {
6596     int el = arm_current_el(env);
6597 
6598     if (el == 0) {
6599         uint64_t sctlr = arm_sctlr(env, el);
6600         if (!(sctlr & SCTLR_EnTP2)) {
6601             return CP_ACCESS_TRAP_EL1;
6602         }
6603     }
6604     /* TODO: FEAT_FGT */
6605     if (el < 3
6606         && arm_feature(env, ARM_FEATURE_EL3)
6607         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6608         return CP_ACCESS_TRAP_EL3;
6609     }
6610     return CP_ACCESS_OK;
6611 }
6612 
6613 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6614                                       bool isread)
6615 {
6616     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6617     if (arm_current_el(env) == 2
6618         && arm_feature(env, ARM_FEATURE_EL3)
6619         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6620         return CP_ACCESS_TRAP_EL3;
6621     }
6622     return CP_ACCESS_OK;
6623 }
6624 
6625 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6626                                    bool isread)
6627 {
6628     if (arm_current_el(env) < 3
6629         && arm_feature(env, ARM_FEATURE_EL3)
6630         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6631         return CP_ACCESS_TRAP_EL3;
6632     }
6633     return CP_ACCESS_OK;
6634 }
6635 
6636 /* ResetSVEState */
6637 static void arm_reset_sve_state(CPUARMState *env)
6638 {
6639     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6640     /* Recall that FFR is stored as pregs[16]. */
6641     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6642     vfp_set_fpsr(env, 0x0800009f);
6643 }
6644 
6645 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6646 {
6647     uint64_t change = (env->svcr ^ new) & mask;
6648 
6649     if (change == 0) {
6650         return;
6651     }
6652     env->svcr ^= change;
6653 
6654     if (change & R_SVCR_SM_MASK) {
6655         arm_reset_sve_state(env);
6656     }
6657 
6658     /*
6659      * ResetSMEState.
6660      *
6661      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6662      * on enable: while disabled, the storage is inaccessible and the
6663      * value does not matter.  We're not saving the storage in vmstate
6664      * when disabled either.
6665      */
6666     if (change & new & R_SVCR_ZA_MASK) {
6667         memset(env->zarray, 0, sizeof(env->zarray));
6668     }
6669 
6670     if (tcg_enabled()) {
6671         arm_rebuild_hflags(env);
6672     }
6673 }
6674 
6675 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6676                        uint64_t value)
6677 {
6678     aarch64_set_svcr(env, value, -1);
6679 }
6680 
6681 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6682                        uint64_t value)
6683 {
6684     int cur_el = arm_current_el(env);
6685     int old_len = sve_vqm1_for_el(env, cur_el);
6686     int new_len;
6687 
6688     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6689     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6690     raw_write(env, ri, value);
6691 
6692     /*
6693      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6694      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6695      * current values for simplicity.  But for QEMU internals, we must still
6696      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6697      * above aarch64_sve_narrow_vq.
6698      */
6699     new_len = sve_vqm1_for_el(env, cur_el);
6700     if (new_len < old_len) {
6701         aarch64_sve_narrow_vq(env, new_len + 1);
6702     }
6703 }
6704 
6705 static const ARMCPRegInfo sme_reginfo[] = {
6706     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6707       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6708       .access = PL0_RW, .accessfn = access_tpidr2,
6709       .fgt = FGT_NTPIDR2_EL0,
6710       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6711     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6712       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6713       .access = PL0_RW, .type = ARM_CP_SME,
6714       .fieldoffset = offsetof(CPUARMState, svcr),
6715       .writefn = svcr_write, .raw_writefn = raw_write },
6716     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6717       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6718       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6719       .access = PL1_RW, .type = ARM_CP_SME,
6720       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6721       .writefn = smcr_write, .raw_writefn = raw_write },
6722     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6723       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6724       .access = PL2_RW, .type = ARM_CP_SME,
6725       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6726       .writefn = smcr_write, .raw_writefn = raw_write },
6727     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6728       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6729       .access = PL3_RW, .type = ARM_CP_SME,
6730       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6731       .writefn = smcr_write, .raw_writefn = raw_write },
6732     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6733       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6734       .access = PL1_R, .accessfn = access_aa64_tid1,
6735       /*
6736        * IMPLEMENTOR = 0 (software)
6737        * REVISION    = 0 (implementation defined)
6738        * SMPS        = 0 (no streaming execution priority in QEMU)
6739        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6740        */
6741       .type = ARM_CP_CONST, .resetvalue = 0, },
6742     /*
6743      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6744      */
6745     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6746       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6747       .access = PL1_RW, .accessfn = access_smpri,
6748       .fgt = FGT_NSMPRI_EL1,
6749       .type = ARM_CP_CONST, .resetvalue = 0 },
6750     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6751       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6752       .nv2_redirect_offset = 0x1f8,
6753       .access = PL2_RW, .accessfn = access_smprimap,
6754       .type = ARM_CP_CONST, .resetvalue = 0 },
6755 };
6756 
6757 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6758                         uint64_t value)
6759 {
6760     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6761     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6762         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6763         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6764 
6765     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6766 }
6767 
6768 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6769 {
6770     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6771                                      env_archcpu(env)->reset_l0gptsz);
6772 }
6773 
6774 static const ARMCPRegInfo rme_reginfo[] = {
6775     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6776       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6777       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6778       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6779     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6780       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6781       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6782     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6784       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6785     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6786       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6787       .access = PL3_W, .type = ARM_CP_NOP },
6788 };
6789 
6790 static const ARMCPRegInfo rme_mte_reginfo[] = {
6791     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6792       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6793       .access = PL3_W, .type = ARM_CP_NOP },
6794 };
6795 
6796 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6797                               uint64_t value)
6798 {
6799     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6800 }
6801 
6802 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6803 {
6804     return env->pstate & PSTATE_ALLINT;
6805 }
6806 
6807 static CPAccessResult aa64_allint_access(CPUARMState *env,
6808                                          const ARMCPRegInfo *ri, bool isread)
6809 {
6810     if (!isread && arm_current_el(env) == 1 &&
6811         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6812         return CP_ACCESS_TRAP_EL2;
6813     }
6814     return CP_ACCESS_OK;
6815 }
6816 
6817 static const ARMCPRegInfo nmi_reginfo[] = {
6818     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6819       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6820       .type = ARM_CP_NO_RAW,
6821       .access = PL1_RW, .accessfn = aa64_allint_access,
6822       .fieldoffset = offsetof(CPUARMState, pstate),
6823       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6824       .resetfn = arm_cp_reset_ignore },
6825 };
6826 #endif /* TARGET_AARCH64 */
6827 
6828 static void define_pmu_regs(ARMCPU *cpu)
6829 {
6830     /*
6831      * v7 performance monitor control register: same implementor
6832      * field as main ID register, and we implement four counters in
6833      * addition to the cycle count register.
6834      */
6835     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6836     ARMCPRegInfo pmcr = {
6837         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6838         .access = PL0_RW,
6839         .fgt = FGT_PMCR_EL0,
6840         .type = ARM_CP_IO | ARM_CP_ALIAS,
6841         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6842         .accessfn = pmreg_access,
6843         .readfn = pmcr_read, .raw_readfn = raw_read,
6844         .writefn = pmcr_write, .raw_writefn = raw_write,
6845     };
6846     ARMCPRegInfo pmcr64 = {
6847         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6848         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6849         .access = PL0_RW, .accessfn = pmreg_access,
6850         .fgt = FGT_PMCR_EL0,
6851         .type = ARM_CP_IO,
6852         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6853         .resetvalue = cpu->isar.reset_pmcr_el0,
6854         .readfn = pmcr_read, .raw_readfn = raw_read,
6855         .writefn = pmcr_write, .raw_writefn = raw_write,
6856     };
6857 
6858     define_one_arm_cp_reg(cpu, &pmcr);
6859     define_one_arm_cp_reg(cpu, &pmcr64);
6860     for (i = 0; i < pmcrn; i++) {
6861         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6862         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6863         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6864         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6865         ARMCPRegInfo pmev_regs[] = {
6866             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6867               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6868               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6869               .fgt = FGT_PMEVCNTRN_EL0,
6870               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6871               .accessfn = pmreg_access_xevcntr },
6872             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6873               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6874               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6875               .type = ARM_CP_IO,
6876               .fgt = FGT_PMEVCNTRN_EL0,
6877               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6878               .raw_readfn = pmevcntr_rawread,
6879               .raw_writefn = pmevcntr_rawwrite },
6880             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6881               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6882               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6883               .fgt = FGT_PMEVTYPERN_EL0,
6884               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6885               .accessfn = pmreg_access },
6886             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6887               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6888               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6889               .fgt = FGT_PMEVTYPERN_EL0,
6890               .type = ARM_CP_IO,
6891               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6892               .raw_writefn = pmevtyper_rawwrite },
6893         };
6894         define_arm_cp_regs(cpu, pmev_regs);
6895         g_free(pmevcntr_name);
6896         g_free(pmevcntr_el0_name);
6897         g_free(pmevtyper_name);
6898         g_free(pmevtyper_el0_name);
6899     }
6900     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6901         ARMCPRegInfo v81_pmu_regs[] = {
6902             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6903               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6904               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6905               .fgt = FGT_PMCEIDN_EL0,
6906               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6907             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6908               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6909               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6910               .fgt = FGT_PMCEIDN_EL0,
6911               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6912         };
6913         define_arm_cp_regs(cpu, v81_pmu_regs);
6914     }
6915     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6916         static const ARMCPRegInfo v84_pmmir = {
6917             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6918             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6919             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6920             .fgt = FGT_PMMIR_EL1,
6921             .resetvalue = 0
6922         };
6923         define_one_arm_cp_reg(cpu, &v84_pmmir);
6924     }
6925 }
6926 
6927 #ifndef CONFIG_USER_ONLY
6928 /*
6929  * We don't know until after realize whether there's a GICv3
6930  * attached, and that is what registers the gicv3 sysregs.
6931  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6932  * at runtime.
6933  */
6934 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6935 {
6936     ARMCPU *cpu = env_archcpu(env);
6937     uint64_t pfr1 = cpu->isar.id_pfr1;
6938 
6939     if (env->gicv3state) {
6940         pfr1 |= 1 << 28;
6941     }
6942     return pfr1;
6943 }
6944 
6945 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6946 {
6947     ARMCPU *cpu = env_archcpu(env);
6948     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6949 
6950     if (env->gicv3state) {
6951         pfr0 |= 1 << 24;
6952     }
6953     return pfr0;
6954 }
6955 #endif
6956 
6957 /*
6958  * Shared logic between LORID and the rest of the LOR* registers.
6959  * Secure state exclusion has already been dealt with.
6960  */
6961 static CPAccessResult access_lor_ns(CPUARMState *env,
6962                                     const ARMCPRegInfo *ri, bool isread)
6963 {
6964     int el = arm_current_el(env);
6965 
6966     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6967         return CP_ACCESS_TRAP_EL2;
6968     }
6969     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6970         return CP_ACCESS_TRAP_EL3;
6971     }
6972     return CP_ACCESS_OK;
6973 }
6974 
6975 static CPAccessResult access_lor_other(CPUARMState *env,
6976                                        const ARMCPRegInfo *ri, bool isread)
6977 {
6978     if (arm_is_secure_below_el3(env)) {
6979         /* UNDEF if SCR_EL3.NS == 0 */
6980         return CP_ACCESS_UNDEFINED;
6981     }
6982     return access_lor_ns(env, ri, isread);
6983 }
6984 
6985 /*
6986  * A trivial implementation of ARMv8.1-LOR leaves all of these
6987  * registers fixed at 0, which indicates that there are zero
6988  * supported Limited Ordering regions.
6989  */
6990 static const ARMCPRegInfo lor_reginfo[] = {
6991     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6992       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6993       .access = PL1_RW, .accessfn = access_lor_other,
6994       .fgt = FGT_LORSA_EL1,
6995       .type = ARM_CP_CONST, .resetvalue = 0 },
6996     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6997       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6998       .access = PL1_RW, .accessfn = access_lor_other,
6999       .fgt = FGT_LOREA_EL1,
7000       .type = ARM_CP_CONST, .resetvalue = 0 },
7001     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7002       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7003       .access = PL1_RW, .accessfn = access_lor_other,
7004       .fgt = FGT_LORN_EL1,
7005       .type = ARM_CP_CONST, .resetvalue = 0 },
7006     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7007       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7008       .access = PL1_RW, .accessfn = access_lor_other,
7009       .fgt = FGT_LORC_EL1,
7010       .type = ARM_CP_CONST, .resetvalue = 0 },
7011     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7012       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7013       .access = PL1_R, .accessfn = access_lor_ns,
7014       .fgt = FGT_LORID_EL1,
7015       .type = ARM_CP_CONST, .resetvalue = 0 },
7016 };
7017 
7018 #ifdef TARGET_AARCH64
7019 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7020                                    bool isread)
7021 {
7022     int el = arm_current_el(env);
7023 
7024     if (el < 2 &&
7025         arm_is_el2_enabled(env) &&
7026         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7027         return CP_ACCESS_TRAP_EL2;
7028     }
7029     if (el < 3 &&
7030         arm_feature(env, ARM_FEATURE_EL3) &&
7031         !(env->cp15.scr_el3 & SCR_APK)) {
7032         return CP_ACCESS_TRAP_EL3;
7033     }
7034     return CP_ACCESS_OK;
7035 }
7036 
7037 static const ARMCPRegInfo pauth_reginfo[] = {
7038     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7040       .access = PL1_RW, .accessfn = access_pauth,
7041       .fgt = FGT_APDAKEY,
7042       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7043     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7044       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7045       .access = PL1_RW, .accessfn = access_pauth,
7046       .fgt = FGT_APDAKEY,
7047       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7048     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7050       .access = PL1_RW, .accessfn = access_pauth,
7051       .fgt = FGT_APDBKEY,
7052       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7053     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7054       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7055       .access = PL1_RW, .accessfn = access_pauth,
7056       .fgt = FGT_APDBKEY,
7057       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7058     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7059       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7060       .access = PL1_RW, .accessfn = access_pauth,
7061       .fgt = FGT_APGAKEY,
7062       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7063     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7064       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7065       .access = PL1_RW, .accessfn = access_pauth,
7066       .fgt = FGT_APGAKEY,
7067       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7068     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7069       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7070       .access = PL1_RW, .accessfn = access_pauth,
7071       .fgt = FGT_APIAKEY,
7072       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7073     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7074       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7075       .access = PL1_RW, .accessfn = access_pauth,
7076       .fgt = FGT_APIAKEY,
7077       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7078     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7079       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7080       .access = PL1_RW, .accessfn = access_pauth,
7081       .fgt = FGT_APIBKEY,
7082       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7083     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7084       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7085       .access = PL1_RW, .accessfn = access_pauth,
7086       .fgt = FGT_APIBKEY,
7087       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7088 };
7089 
7090 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7091 {
7092     Error *err = NULL;
7093     uint64_t ret;
7094 
7095     /* Success sets NZCV = 0000.  */
7096     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7097 
7098     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7099         /*
7100          * ??? Failed, for unknown reasons in the crypto subsystem.
7101          * The best we can do is log the reason and return the
7102          * timed-out indication to the guest.  There is no reason
7103          * we know to expect this failure to be transitory, so the
7104          * guest may well hang retrying the operation.
7105          */
7106         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7107                       ri->name, error_get_pretty(err));
7108         error_free(err);
7109 
7110         env->ZF = 0; /* NZCF = 0100 */
7111         return 0;
7112     }
7113     return ret;
7114 }
7115 
7116 /* We do not support re-seeding, so the two registers operate the same.  */
7117 static const ARMCPRegInfo rndr_reginfo[] = {
7118     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7119       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7120       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7121       .access = PL0_R, .readfn = rndr_readfn },
7122     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7123       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7124       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7125       .access = PL0_R, .readfn = rndr_readfn },
7126 };
7127 
7128 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7129                           uint64_t value)
7130 {
7131 #ifdef CONFIG_TCG
7132     ARMCPU *cpu = env_archcpu(env);
7133     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7134     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7135     uint64_t vaddr_in = (uint64_t) value;
7136     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7137     void *haddr;
7138     int mem_idx = arm_env_mmu_index(env);
7139 
7140     /* This won't be crossing page boundaries */
7141     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7142     if (haddr) {
7143 #ifndef CONFIG_USER_ONLY
7144 
7145         ram_addr_t offset;
7146         MemoryRegion *mr;
7147 
7148         /* RCU lock is already being held */
7149         mr = memory_region_from_host(haddr, &offset);
7150 
7151         if (mr) {
7152             memory_region_writeback(mr, offset, dline_size);
7153         }
7154 #endif /*CONFIG_USER_ONLY*/
7155     }
7156 #else
7157     /* Handled by hardware accelerator. */
7158     g_assert_not_reached();
7159 #endif /* CONFIG_TCG */
7160 }
7161 
7162 static const ARMCPRegInfo dcpop_reg[] = {
7163     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7164       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7165       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7166       .fgt = FGT_DCCVAP,
7167       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7168 };
7169 
7170 static const ARMCPRegInfo dcpodp_reg[] = {
7171     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7172       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7173       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7174       .fgt = FGT_DCCVADP,
7175       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7176 };
7177 
7178 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7179                                        bool isread)
7180 {
7181     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7182         return CP_ACCESS_TRAP_EL2;
7183     }
7184 
7185     return CP_ACCESS_OK;
7186 }
7187 
7188 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7189                                  bool isread)
7190 {
7191     int el = arm_current_el(env);
7192     if (el < 2 && arm_is_el2_enabled(env)) {
7193         uint64_t hcr = arm_hcr_el2_eff(env);
7194         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7195             return CP_ACCESS_TRAP_EL2;
7196         }
7197     }
7198     if (el < 3 &&
7199         arm_feature(env, ARM_FEATURE_EL3) &&
7200         !(env->cp15.scr_el3 & SCR_ATA)) {
7201         return CP_ACCESS_TRAP_EL3;
7202     }
7203     return CP_ACCESS_OK;
7204 }
7205 
7206 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7207                                       bool isread)
7208 {
7209     CPAccessResult nv1 = access_nv1(env, ri, isread);
7210 
7211     if (nv1 != CP_ACCESS_OK) {
7212         return nv1;
7213     }
7214     return access_mte(env, ri, isread);
7215 }
7216 
7217 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7218                                       bool isread)
7219 {
7220     /*
7221      * TFSR_EL2: similar to generic access_mte(), but we need to
7222      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7223      * if NV2 is enabled then we will redirect this to TFSR_EL1
7224      * after doing the HCR and SCR ATA traps; otherwise this will
7225      * be a trap to EL2 and the HCR/SCR traps do not apply.
7226      */
7227     int el = arm_current_el(env);
7228 
7229     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7230         return CP_ACCESS_OK;
7231     }
7232     if (el < 2 && arm_is_el2_enabled(env)) {
7233         uint64_t hcr = arm_hcr_el2_eff(env);
7234         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7235             return CP_ACCESS_TRAP_EL2;
7236         }
7237     }
7238     if (el < 3 &&
7239         arm_feature(env, ARM_FEATURE_EL3) &&
7240         !(env->cp15.scr_el3 & SCR_ATA)) {
7241         return CP_ACCESS_TRAP_EL3;
7242     }
7243     return CP_ACCESS_OK;
7244 }
7245 
7246 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7247 {
7248     return env->pstate & PSTATE_TCO;
7249 }
7250 
7251 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7252 {
7253     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7254 }
7255 
7256 static const ARMCPRegInfo mte_reginfo[] = {
7257     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7258       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7259       .access = PL1_RW, .accessfn = access_mte,
7260       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7261     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7262       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7263       .access = PL1_RW, .accessfn = access_tfsr_el1,
7264       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7265       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7266     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7267       .type = ARM_CP_NV2_REDIRECT,
7268       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7269       .access = PL2_RW, .accessfn = access_tfsr_el2,
7270       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7271     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7272       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7273       .access = PL3_RW,
7274       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7275     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7276       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7277       .access = PL1_RW, .accessfn = access_mte,
7278       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7279     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7280       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7281       .access = PL1_RW, .accessfn = access_mte,
7282       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7283     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7284       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7285       .type = ARM_CP_NO_RAW,
7286       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7287     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7288       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7289       .type = ARM_CP_NOP, .access = PL1_W,
7290       .fgt = FGT_DCIVAC,
7291       .accessfn = aa64_cacheop_poc_access },
7292     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7293       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7294       .fgt = FGT_DCISW,
7295       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7296     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7297       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7298       .type = ARM_CP_NOP, .access = PL1_W,
7299       .fgt = FGT_DCIVAC,
7300       .accessfn = aa64_cacheop_poc_access },
7301     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7302       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7303       .fgt = FGT_DCISW,
7304       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7305     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7306       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7307       .fgt = FGT_DCCSW,
7308       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7309     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7310       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7311       .fgt = FGT_DCCSW,
7312       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7313     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7314       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7315       .fgt = FGT_DCCISW,
7316       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7317     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7318       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7319       .fgt = FGT_DCCISW,
7320       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7321 };
7322 
7323 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7324     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7325       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7326       .type = ARM_CP_CONST, .access = PL0_RW, },
7327 };
7328 
7329 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7330     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7331       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7332       .type = ARM_CP_NOP, .access = PL0_W,
7333       .fgt = FGT_DCCVAC,
7334       .accessfn = aa64_cacheop_poc_access },
7335     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7336       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7337       .type = ARM_CP_NOP, .access = PL0_W,
7338       .fgt = FGT_DCCVAC,
7339       .accessfn = aa64_cacheop_poc_access },
7340     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7341       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7342       .type = ARM_CP_NOP, .access = PL0_W,
7343       .fgt = FGT_DCCVAP,
7344       .accessfn = aa64_cacheop_poc_access },
7345     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7346       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7347       .type = ARM_CP_NOP, .access = PL0_W,
7348       .fgt = FGT_DCCVAP,
7349       .accessfn = aa64_cacheop_poc_access },
7350     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7351       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7352       .type = ARM_CP_NOP, .access = PL0_W,
7353       .fgt = FGT_DCCVADP,
7354       .accessfn = aa64_cacheop_poc_access },
7355     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7356       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7357       .type = ARM_CP_NOP, .access = PL0_W,
7358       .fgt = FGT_DCCVADP,
7359       .accessfn = aa64_cacheop_poc_access },
7360     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7361       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7362       .type = ARM_CP_NOP, .access = PL0_W,
7363       .fgt = FGT_DCCIVAC,
7364       .accessfn = aa64_cacheop_poc_access },
7365     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7366       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7367       .type = ARM_CP_NOP, .access = PL0_W,
7368       .fgt = FGT_DCCIVAC,
7369       .accessfn = aa64_cacheop_poc_access },
7370     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7371       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7372       .access = PL0_W, .type = ARM_CP_DC_GVA,
7373 #ifndef CONFIG_USER_ONLY
7374       /* Avoid overhead of an access check that always passes in user-mode */
7375       .accessfn = aa64_zva_access,
7376       .fgt = FGT_DCZVA,
7377 #endif
7378     },
7379     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7380       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7381       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7382 #ifndef CONFIG_USER_ONLY
7383       /* Avoid overhead of an access check that always passes in user-mode */
7384       .accessfn = aa64_zva_access,
7385       .fgt = FGT_DCZVA,
7386 #endif
7387     },
7388 };
7389 
7390 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7391                                      bool isread)
7392 {
7393     uint64_t hcr = arm_hcr_el2_eff(env);
7394     int el = arm_current_el(env);
7395 
7396     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7397         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7398             if (hcr & HCR_TGE) {
7399                 return CP_ACCESS_TRAP_EL2;
7400             }
7401             return CP_ACCESS_TRAP_EL1;
7402         }
7403     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7404         return CP_ACCESS_TRAP_EL2;
7405     }
7406     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7407         return CP_ACCESS_TRAP_EL2;
7408     }
7409     if (el < 3
7410         && arm_feature(env, ARM_FEATURE_EL3)
7411         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7412         return CP_ACCESS_TRAP_EL3;
7413     }
7414     return CP_ACCESS_OK;
7415 }
7416 
7417 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7418                                          const ARMCPRegInfo *ri,
7419                                          bool isread)
7420 {
7421     CPAccessResult nv1 = access_nv1(env, ri, isread);
7422 
7423     if (nv1 != CP_ACCESS_OK) {
7424         return nv1;
7425     }
7426     return access_scxtnum(env, ri, isread);
7427 }
7428 
7429 static const ARMCPRegInfo scxtnum_reginfo[] = {
7430     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7431       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7432       .access = PL0_RW, .accessfn = access_scxtnum,
7433       .fgt = FGT_SCXTNUM_EL0,
7434       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7435     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7436       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7437       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7438       .fgt = FGT_SCXTNUM_EL1,
7439       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7440       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7441     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7443       .access = PL2_RW, .accessfn = access_scxtnum,
7444       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7445     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7446       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7447       .access = PL3_RW,
7448       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7449 };
7450 
7451 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7452                                  bool isread)
7453 {
7454     if (arm_current_el(env) == 2 &&
7455         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7456         return CP_ACCESS_TRAP_EL3;
7457     }
7458     return CP_ACCESS_OK;
7459 }
7460 
7461 static const ARMCPRegInfo fgt_reginfo[] = {
7462     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7463       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7464       .nv2_redirect_offset = 0x1b8,
7465       .access = PL2_RW, .accessfn = access_fgt,
7466       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7467     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7468       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7469       .nv2_redirect_offset = 0x1c0,
7470       .access = PL2_RW, .accessfn = access_fgt,
7471       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7472     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7473       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7474       .nv2_redirect_offset = 0x1d0,
7475       .access = PL2_RW, .accessfn = access_fgt,
7476       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7477     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7478       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7479       .nv2_redirect_offset = 0x1d8,
7480       .access = PL2_RW, .accessfn = access_fgt,
7481       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7482     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7483       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7484       .nv2_redirect_offset = 0x1c8,
7485       .access = PL2_RW, .accessfn = access_fgt,
7486       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7487 };
7488 
7489 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7490                        uint64_t value)
7491 {
7492     /*
7493      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7494      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7495      * about the RESS bits at the top -- we choose the "generate an EL2
7496      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7497      * the ptw.c code detect the resulting invalid address).
7498      */
7499     env->cp15.vncr_el2 = value & ~0xfffULL;
7500 }
7501 
7502 static const ARMCPRegInfo nv2_reginfo[] = {
7503     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7504       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7505       .access = PL2_RW,
7506       .writefn = vncr_write,
7507       .nv2_redirect_offset = 0xb0,
7508       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7509 };
7510 
7511 #endif /* TARGET_AARCH64 */
7512 
7513 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7514                                      bool isread)
7515 {
7516     int el = arm_current_el(env);
7517 
7518     if (el == 0) {
7519         uint64_t sctlr = arm_sctlr(env, el);
7520         if (!(sctlr & SCTLR_EnRCTX)) {
7521             return CP_ACCESS_TRAP_EL1;
7522         }
7523     } else if (el == 1) {
7524         uint64_t hcr = arm_hcr_el2_eff(env);
7525         if (hcr & HCR_NV) {
7526             return CP_ACCESS_TRAP_EL2;
7527         }
7528     }
7529     return CP_ACCESS_OK;
7530 }
7531 
7532 static const ARMCPRegInfo predinv_reginfo[] = {
7533     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7534       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7535       .fgt = FGT_CFPRCTX,
7536       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7537     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7538       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7539       .fgt = FGT_DVPRCTX,
7540       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7541     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7542       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7543       .fgt = FGT_CPPRCTX,
7544       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7545     /*
7546      * Note the AArch32 opcodes have a different OPC1.
7547      */
7548     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7549       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7550       .fgt = FGT_CFPRCTX,
7551       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7552     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7553       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7554       .fgt = FGT_DVPRCTX,
7555       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7556     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7557       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7558       .fgt = FGT_CPPRCTX,
7559       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7560 };
7561 
7562 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7563 {
7564     /* Read the high 32 bits of the current CCSIDR */
7565     return extract64(ccsidr_read(env, ri), 32, 32);
7566 }
7567 
7568 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7569     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7570       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7571       .access = PL1_R,
7572       .accessfn = access_tid4,
7573       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7574 };
7575 
7576 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7577                                        bool isread)
7578 {
7579     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7580         return CP_ACCESS_TRAP_EL2;
7581     }
7582 
7583     return CP_ACCESS_OK;
7584 }
7585 
7586 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7587                                        bool isread)
7588 {
7589     if (arm_feature(env, ARM_FEATURE_V8)) {
7590         return access_aa64_tid3(env, ri, isread);
7591     }
7592 
7593     return CP_ACCESS_OK;
7594 }
7595 
7596 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7597                                      bool isread)
7598 {
7599     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7600         return CP_ACCESS_TRAP_EL2;
7601     }
7602 
7603     return CP_ACCESS_OK;
7604 }
7605 
7606 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7607                                         const ARMCPRegInfo *ri, bool isread)
7608 {
7609     /*
7610      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7611      * in v7A, not in v8A.
7612      */
7613     if (!arm_feature(env, ARM_FEATURE_V8) &&
7614         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7615         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7616         return CP_ACCESS_TRAP_EL2;
7617     }
7618     return CP_ACCESS_OK;
7619 }
7620 
7621 static const ARMCPRegInfo jazelle_regs[] = {
7622     { .name = "JIDR",
7623       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7624       .access = PL1_R, .accessfn = access_jazelle,
7625       .type = ARM_CP_CONST, .resetvalue = 0 },
7626     { .name = "JOSCR",
7627       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7628       .accessfn = access_joscr_jmcr,
7629       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7630     { .name = "JMCR",
7631       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7632       .accessfn = access_joscr_jmcr,
7633       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7634 };
7635 
7636 static const ARMCPRegInfo contextidr_el2 = {
7637     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7638     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7639     .access = PL2_RW,
7640     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7641 };
7642 
7643 static const ARMCPRegInfo vhe_reginfo[] = {
7644     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7645       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7646       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7647       .raw_writefn = raw_write,
7648       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7649 #ifndef CONFIG_USER_ONLY
7650     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7651       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7652       .fieldoffset =
7653         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7654       .type = ARM_CP_IO, .access = PL2_RW,
7655       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7656     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7657       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7658       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7659       .resetfn = gt_hv_timer_reset,
7660       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7661     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7662       .type = ARM_CP_IO,
7663       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7664       .access = PL2_RW,
7665       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7666       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7667     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7668       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7669       .type = ARM_CP_IO | ARM_CP_ALIAS,
7670       .access = PL2_RW, .accessfn = access_el1nvpct,
7671       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7672       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7673       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7674     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7675       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7676       .type = ARM_CP_IO | ARM_CP_ALIAS,
7677       .access = PL2_RW, .accessfn = access_el1nvvct,
7678       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7679       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7680       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7681     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7683       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7684       .access = PL2_RW, .accessfn = e2h_access,
7685       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7686     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7687       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7688       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7689       .access = PL2_RW, .accessfn = e2h_access,
7690       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7691     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7692       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7693       .type = ARM_CP_IO | ARM_CP_ALIAS,
7694       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7695       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7696       .access = PL2_RW, .accessfn = access_el1nvpct,
7697       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7698     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7699       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7700       .type = ARM_CP_IO | ARM_CP_ALIAS,
7701       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7702       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7703       .access = PL2_RW, .accessfn = access_el1nvvct,
7704       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7705 #endif
7706 };
7707 
7708 #ifndef CONFIG_USER_ONLY
7709 static const ARMCPRegInfo ats1e1_reginfo[] = {
7710     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7712       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7713       .fgt = FGT_ATS1E1RP,
7714       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7715     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7716       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7717       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7718       .fgt = FGT_ATS1E1WP,
7719       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7720 };
7721 
7722 static const ARMCPRegInfo ats1cp_reginfo[] = {
7723     { .name = "ATS1CPRP",
7724       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7725       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7726       .writefn = ats_write },
7727     { .name = "ATS1CPWP",
7728       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7729       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7730       .writefn = ats_write },
7731 };
7732 #endif
7733 
7734 /*
7735  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7736  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7737  * is non-zero, which is never for ARMv7, optionally in ARMv8
7738  * and mandatorily for ARMv8.2 and up.
7739  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7740  * implementation is RAZ/WI we can ignore this detail, as we
7741  * do for ACTLR.
7742  */
7743 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7744     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7745       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7746       .access = PL1_RW, .accessfn = access_tacr,
7747       .type = ARM_CP_CONST, .resetvalue = 0 },
7748     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7749       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7750       .access = PL2_RW, .type = ARM_CP_CONST,
7751       .resetvalue = 0 },
7752 };
7753 
7754 void register_cp_regs_for_features(ARMCPU *cpu)
7755 {
7756     /* Register all the coprocessor registers based on feature bits */
7757     CPUARMState *env = &cpu->env;
7758     if (arm_feature(env, ARM_FEATURE_M)) {
7759         /* M profile has no coprocessor registers */
7760         return;
7761     }
7762 
7763     define_arm_cp_regs(cpu, cp_reginfo);
7764     if (!arm_feature(env, ARM_FEATURE_V8)) {
7765         /*
7766          * Must go early as it is full of wildcards that may be
7767          * overridden by later definitions.
7768          */
7769         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7770     }
7771 
7772     define_tlb_insn_regs(cpu);
7773 
7774     if (arm_feature(env, ARM_FEATURE_V6)) {
7775         /* The ID registers all have impdef reset values */
7776         ARMCPRegInfo v6_idregs[] = {
7777             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7778               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7779               .access = PL1_R, .type = ARM_CP_CONST,
7780               .accessfn = access_aa32_tid3,
7781               .resetvalue = cpu->isar.id_pfr0 },
7782             /*
7783              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7784              * the value of the GIC field until after we define these regs.
7785              */
7786             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7787               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7788               .access = PL1_R, .type = ARM_CP_NO_RAW,
7789               .accessfn = access_aa32_tid3,
7790 #ifdef CONFIG_USER_ONLY
7791               .type = ARM_CP_CONST,
7792               .resetvalue = cpu->isar.id_pfr1,
7793 #else
7794               .type = ARM_CP_NO_RAW,
7795               .accessfn = access_aa32_tid3,
7796               .readfn = id_pfr1_read,
7797               .writefn = arm_cp_write_ignore
7798 #endif
7799             },
7800             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7801               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7802               .access = PL1_R, .type = ARM_CP_CONST,
7803               .accessfn = access_aa32_tid3,
7804               .resetvalue = cpu->isar.id_dfr0 },
7805             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7806               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7807               .access = PL1_R, .type = ARM_CP_CONST,
7808               .accessfn = access_aa32_tid3,
7809               .resetvalue = cpu->id_afr0 },
7810             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7811               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7812               .access = PL1_R, .type = ARM_CP_CONST,
7813               .accessfn = access_aa32_tid3,
7814               .resetvalue = cpu->isar.id_mmfr0 },
7815             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7816               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7817               .access = PL1_R, .type = ARM_CP_CONST,
7818               .accessfn = access_aa32_tid3,
7819               .resetvalue = cpu->isar.id_mmfr1 },
7820             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7821               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7822               .access = PL1_R, .type = ARM_CP_CONST,
7823               .accessfn = access_aa32_tid3,
7824               .resetvalue = cpu->isar.id_mmfr2 },
7825             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7826               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7827               .access = PL1_R, .type = ARM_CP_CONST,
7828               .accessfn = access_aa32_tid3,
7829               .resetvalue = cpu->isar.id_mmfr3 },
7830             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7831               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7832               .access = PL1_R, .type = ARM_CP_CONST,
7833               .accessfn = access_aa32_tid3,
7834               .resetvalue = cpu->isar.id_isar0 },
7835             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7836               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7837               .access = PL1_R, .type = ARM_CP_CONST,
7838               .accessfn = access_aa32_tid3,
7839               .resetvalue = cpu->isar.id_isar1 },
7840             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7841               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7842               .access = PL1_R, .type = ARM_CP_CONST,
7843               .accessfn = access_aa32_tid3,
7844               .resetvalue = cpu->isar.id_isar2 },
7845             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7846               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7847               .access = PL1_R, .type = ARM_CP_CONST,
7848               .accessfn = access_aa32_tid3,
7849               .resetvalue = cpu->isar.id_isar3 },
7850             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7851               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7852               .access = PL1_R, .type = ARM_CP_CONST,
7853               .accessfn = access_aa32_tid3,
7854               .resetvalue = cpu->isar.id_isar4 },
7855             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7856               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7857               .access = PL1_R, .type = ARM_CP_CONST,
7858               .accessfn = access_aa32_tid3,
7859               .resetvalue = cpu->isar.id_isar5 },
7860             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7861               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7862               .access = PL1_R, .type = ARM_CP_CONST,
7863               .accessfn = access_aa32_tid3,
7864               .resetvalue = cpu->isar.id_mmfr4 },
7865             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7866               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7867               .access = PL1_R, .type = ARM_CP_CONST,
7868               .accessfn = access_aa32_tid3,
7869               .resetvalue = cpu->isar.id_isar6 },
7870         };
7871         define_arm_cp_regs(cpu, v6_idregs);
7872         define_arm_cp_regs(cpu, v6_cp_reginfo);
7873     } else {
7874         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7875     }
7876     if (arm_feature(env, ARM_FEATURE_V6K)) {
7877         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7878     }
7879     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7880         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7881     }
7882     if (arm_feature(env, ARM_FEATURE_V7)) {
7883         ARMCPRegInfo clidr = {
7884             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7885             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7886             .access = PL1_R, .type = ARM_CP_CONST,
7887             .accessfn = access_tid4,
7888             .fgt = FGT_CLIDR_EL1,
7889             .resetvalue = cpu->clidr
7890         };
7891         define_one_arm_cp_reg(cpu, &clidr);
7892         define_arm_cp_regs(cpu, v7_cp_reginfo);
7893         define_debug_regs(cpu);
7894         define_pmu_regs(cpu);
7895     } else {
7896         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7897     }
7898     if (arm_feature(env, ARM_FEATURE_V8)) {
7899         /*
7900          * v8 ID registers, which all have impdef reset values.
7901          * Note that within the ID register ranges the unused slots
7902          * must all RAZ, not UNDEF; future architecture versions may
7903          * define new registers here.
7904          * ID registers which are AArch64 views of the AArch32 ID registers
7905          * which already existed in v6 and v7 are handled elsewhere,
7906          * in v6_idregs[].
7907          */
7908         int i;
7909         ARMCPRegInfo v8_idregs[] = {
7910             /*
7911              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7912              * emulation because we don't know the right value for the
7913              * GIC field until after we define these regs.
7914              */
7915             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7916               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7917               .access = PL1_R,
7918 #ifdef CONFIG_USER_ONLY
7919               .type = ARM_CP_CONST,
7920               .resetvalue = cpu->isar.id_aa64pfr0
7921 #else
7922               .type = ARM_CP_NO_RAW,
7923               .accessfn = access_aa64_tid3,
7924               .readfn = id_aa64pfr0_read,
7925               .writefn = arm_cp_write_ignore
7926 #endif
7927             },
7928             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7929               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7930               .access = PL1_R, .type = ARM_CP_CONST,
7931               .accessfn = access_aa64_tid3,
7932               .resetvalue = cpu->isar.id_aa64pfr1},
7933             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7934               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7935               .access = PL1_R, .type = ARM_CP_CONST,
7936               .accessfn = access_aa64_tid3,
7937               .resetvalue = 0 },
7938             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7939               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7940               .access = PL1_R, .type = ARM_CP_CONST,
7941               .accessfn = access_aa64_tid3,
7942               .resetvalue = 0 },
7943             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7944               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7945               .access = PL1_R, .type = ARM_CP_CONST,
7946               .accessfn = access_aa64_tid3,
7947               .resetvalue = cpu->isar.id_aa64zfr0 },
7948             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7949               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7950               .access = PL1_R, .type = ARM_CP_CONST,
7951               .accessfn = access_aa64_tid3,
7952               .resetvalue = cpu->isar.id_aa64smfr0 },
7953             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7954               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7955               .access = PL1_R, .type = ARM_CP_CONST,
7956               .accessfn = access_aa64_tid3,
7957               .resetvalue = 0 },
7958             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7959               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7960               .access = PL1_R, .type = ARM_CP_CONST,
7961               .accessfn = access_aa64_tid3,
7962               .resetvalue = 0 },
7963             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7964               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7965               .access = PL1_R, .type = ARM_CP_CONST,
7966               .accessfn = access_aa64_tid3,
7967               .resetvalue = cpu->isar.id_aa64dfr0 },
7968             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7969               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7970               .access = PL1_R, .type = ARM_CP_CONST,
7971               .accessfn = access_aa64_tid3,
7972               .resetvalue = cpu->isar.id_aa64dfr1 },
7973             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7974               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7975               .access = PL1_R, .type = ARM_CP_CONST,
7976               .accessfn = access_aa64_tid3,
7977               .resetvalue = 0 },
7978             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7979               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7980               .access = PL1_R, .type = ARM_CP_CONST,
7981               .accessfn = access_aa64_tid3,
7982               .resetvalue = 0 },
7983             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7984               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7985               .access = PL1_R, .type = ARM_CP_CONST,
7986               .accessfn = access_aa64_tid3,
7987               .resetvalue = cpu->id_aa64afr0 },
7988             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7989               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7990               .access = PL1_R, .type = ARM_CP_CONST,
7991               .accessfn = access_aa64_tid3,
7992               .resetvalue = cpu->id_aa64afr1 },
7993             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7994               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7995               .access = PL1_R, .type = ARM_CP_CONST,
7996               .accessfn = access_aa64_tid3,
7997               .resetvalue = 0 },
7998             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7999               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8000               .access = PL1_R, .type = ARM_CP_CONST,
8001               .accessfn = access_aa64_tid3,
8002               .resetvalue = 0 },
8003             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8004               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8005               .access = PL1_R, .type = ARM_CP_CONST,
8006               .accessfn = access_aa64_tid3,
8007               .resetvalue = cpu->isar.id_aa64isar0 },
8008             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8009               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8010               .access = PL1_R, .type = ARM_CP_CONST,
8011               .accessfn = access_aa64_tid3,
8012               .resetvalue = cpu->isar.id_aa64isar1 },
8013             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8014               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8015               .access = PL1_R, .type = ARM_CP_CONST,
8016               .accessfn = access_aa64_tid3,
8017               .resetvalue = cpu->isar.id_aa64isar2 },
8018             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8019               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8020               .access = PL1_R, .type = ARM_CP_CONST,
8021               .accessfn = access_aa64_tid3,
8022               .resetvalue = 0 },
8023             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8024               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8025               .access = PL1_R, .type = ARM_CP_CONST,
8026               .accessfn = access_aa64_tid3,
8027               .resetvalue = 0 },
8028             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8029               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8030               .access = PL1_R, .type = ARM_CP_CONST,
8031               .accessfn = access_aa64_tid3,
8032               .resetvalue = 0 },
8033             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8034               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8035               .access = PL1_R, .type = ARM_CP_CONST,
8036               .accessfn = access_aa64_tid3,
8037               .resetvalue = 0 },
8038             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8039               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8040               .access = PL1_R, .type = ARM_CP_CONST,
8041               .accessfn = access_aa64_tid3,
8042               .resetvalue = 0 },
8043             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8044               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8045               .access = PL1_R, .type = ARM_CP_CONST,
8046               .accessfn = access_aa64_tid3,
8047               .resetvalue = cpu->isar.id_aa64mmfr0 },
8048             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8049               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8050               .access = PL1_R, .type = ARM_CP_CONST,
8051               .accessfn = access_aa64_tid3,
8052               .resetvalue = cpu->isar.id_aa64mmfr1 },
8053             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8054               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8055               .access = PL1_R, .type = ARM_CP_CONST,
8056               .accessfn = access_aa64_tid3,
8057               .resetvalue = cpu->isar.id_aa64mmfr2 },
8058             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
8059               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8060               .access = PL1_R, .type = ARM_CP_CONST,
8061               .accessfn = access_aa64_tid3,
8062               .resetvalue = cpu->isar.id_aa64mmfr3 },
8063             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8064               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8065               .access = PL1_R, .type = ARM_CP_CONST,
8066               .accessfn = access_aa64_tid3,
8067               .resetvalue = 0 },
8068             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8069               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8070               .access = PL1_R, .type = ARM_CP_CONST,
8071               .accessfn = access_aa64_tid3,
8072               .resetvalue = 0 },
8073             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8074               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8075               .access = PL1_R, .type = ARM_CP_CONST,
8076               .accessfn = access_aa64_tid3,
8077               .resetvalue = 0 },
8078             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8079               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8080               .access = PL1_R, .type = ARM_CP_CONST,
8081               .accessfn = access_aa64_tid3,
8082               .resetvalue = 0 },
8083             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8084               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8085               .access = PL1_R, .type = ARM_CP_CONST,
8086               .accessfn = access_aa64_tid3,
8087               .resetvalue = cpu->isar.mvfr0 },
8088             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8089               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8090               .access = PL1_R, .type = ARM_CP_CONST,
8091               .accessfn = access_aa64_tid3,
8092               .resetvalue = cpu->isar.mvfr1 },
8093             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8094               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8095               .access = PL1_R, .type = ARM_CP_CONST,
8096               .accessfn = access_aa64_tid3,
8097               .resetvalue = cpu->isar.mvfr2 },
8098             /*
8099              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8100              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8101              * as RAZ, since it is in the "reserved for future ID
8102              * registers, RAZ" part of the AArch32 encoding space.
8103              */
8104             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8105               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8106               .access = PL1_R, .type = ARM_CP_CONST,
8107               .accessfn = access_aa64_tid3,
8108               .resetvalue = 0 },
8109             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8110               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8111               .access = PL1_R, .type = ARM_CP_CONST,
8112               .accessfn = access_aa64_tid3,
8113               .resetvalue = 0 },
8114             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8115               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8116               .access = PL1_R, .type = ARM_CP_CONST,
8117               .accessfn = access_aa64_tid3,
8118               .resetvalue = 0 },
8119             /*
8120              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8121              * they're also RAZ for AArch64, and in v8 are gradually
8122              * being filled with AArch64-view-of-AArch32-ID-register
8123              * for new ID registers.
8124              */
8125             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8126               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8127               .access = PL1_R, .type = ARM_CP_CONST,
8128               .accessfn = access_aa64_tid3,
8129               .resetvalue = 0 },
8130             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8131               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8132               .access = PL1_R, .type = ARM_CP_CONST,
8133               .accessfn = access_aa64_tid3,
8134               .resetvalue = cpu->isar.id_pfr2 },
8135             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8136               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8137               .access = PL1_R, .type = ARM_CP_CONST,
8138               .accessfn = access_aa64_tid3,
8139               .resetvalue = cpu->isar.id_dfr1 },
8140             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8141               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8142               .access = PL1_R, .type = ARM_CP_CONST,
8143               .accessfn = access_aa64_tid3,
8144               .resetvalue = cpu->isar.id_mmfr5 },
8145             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8146               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8147               .access = PL1_R, .type = ARM_CP_CONST,
8148               .accessfn = access_aa64_tid3,
8149               .resetvalue = 0 },
8150             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8151               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8152               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8153               .fgt = FGT_PMCEIDN_EL0,
8154               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8155             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8156               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8157               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8158               .fgt = FGT_PMCEIDN_EL0,
8159               .resetvalue = cpu->pmceid0 },
8160             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8161               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8162               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8163               .fgt = FGT_PMCEIDN_EL0,
8164               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8165             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8166               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8167               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8168               .fgt = FGT_PMCEIDN_EL0,
8169               .resetvalue = cpu->pmceid1 },
8170         };
8171 #ifdef CONFIG_USER_ONLY
8172         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8173             { .name = "ID_AA64PFR0_EL1",
8174               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8175                                R_ID_AA64PFR0_ADVSIMD_MASK |
8176                                R_ID_AA64PFR0_SVE_MASK |
8177                                R_ID_AA64PFR0_DIT_MASK,
8178               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8179                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8180             { .name = "ID_AA64PFR1_EL1",
8181               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8182                                R_ID_AA64PFR1_SSBS_MASK |
8183                                R_ID_AA64PFR1_MTE_MASK |
8184                                R_ID_AA64PFR1_SME_MASK },
8185             { .name = "ID_AA64PFR*_EL1_RESERVED",
8186               .is_glob = true },
8187             { .name = "ID_AA64ZFR0_EL1",
8188               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8189                                R_ID_AA64ZFR0_AES_MASK |
8190                                R_ID_AA64ZFR0_BITPERM_MASK |
8191                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8192                                R_ID_AA64ZFR0_B16B16_MASK |
8193                                R_ID_AA64ZFR0_SHA3_MASK |
8194                                R_ID_AA64ZFR0_SM4_MASK |
8195                                R_ID_AA64ZFR0_I8MM_MASK |
8196                                R_ID_AA64ZFR0_F32MM_MASK |
8197                                R_ID_AA64ZFR0_F64MM_MASK },
8198             { .name = "ID_AA64SMFR0_EL1",
8199               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8200                                R_ID_AA64SMFR0_BI32I32_MASK |
8201                                R_ID_AA64SMFR0_B16F32_MASK |
8202                                R_ID_AA64SMFR0_F16F32_MASK |
8203                                R_ID_AA64SMFR0_I8I32_MASK |
8204                                R_ID_AA64SMFR0_F16F16_MASK |
8205                                R_ID_AA64SMFR0_B16B16_MASK |
8206                                R_ID_AA64SMFR0_I16I32_MASK |
8207                                R_ID_AA64SMFR0_F64F64_MASK |
8208                                R_ID_AA64SMFR0_I16I64_MASK |
8209                                R_ID_AA64SMFR0_SMEVER_MASK |
8210                                R_ID_AA64SMFR0_FA64_MASK },
8211             { .name = "ID_AA64MMFR0_EL1",
8212               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8213               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8214                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8215             { .name = "ID_AA64MMFR1_EL1",
8216               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8217             { .name = "ID_AA64MMFR2_EL1",
8218               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8219             { .name = "ID_AA64MMFR3_EL1",
8220               .exported_bits = 0 },
8221             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8222               .is_glob = true },
8223             { .name = "ID_AA64DFR0_EL1",
8224               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8225             { .name = "ID_AA64DFR1_EL1" },
8226             { .name = "ID_AA64DFR*_EL1_RESERVED",
8227               .is_glob = true },
8228             { .name = "ID_AA64AFR*",
8229               .is_glob = true },
8230             { .name = "ID_AA64ISAR0_EL1",
8231               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8232                                R_ID_AA64ISAR0_SHA1_MASK |
8233                                R_ID_AA64ISAR0_SHA2_MASK |
8234                                R_ID_AA64ISAR0_CRC32_MASK |
8235                                R_ID_AA64ISAR0_ATOMIC_MASK |
8236                                R_ID_AA64ISAR0_RDM_MASK |
8237                                R_ID_AA64ISAR0_SHA3_MASK |
8238                                R_ID_AA64ISAR0_SM3_MASK |
8239                                R_ID_AA64ISAR0_SM4_MASK |
8240                                R_ID_AA64ISAR0_DP_MASK |
8241                                R_ID_AA64ISAR0_FHM_MASK |
8242                                R_ID_AA64ISAR0_TS_MASK |
8243                                R_ID_AA64ISAR0_RNDR_MASK },
8244             { .name = "ID_AA64ISAR1_EL1",
8245               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8246                                R_ID_AA64ISAR1_APA_MASK |
8247                                R_ID_AA64ISAR1_API_MASK |
8248                                R_ID_AA64ISAR1_JSCVT_MASK |
8249                                R_ID_AA64ISAR1_FCMA_MASK |
8250                                R_ID_AA64ISAR1_LRCPC_MASK |
8251                                R_ID_AA64ISAR1_GPA_MASK |
8252                                R_ID_AA64ISAR1_GPI_MASK |
8253                                R_ID_AA64ISAR1_FRINTTS_MASK |
8254                                R_ID_AA64ISAR1_SB_MASK |
8255                                R_ID_AA64ISAR1_BF16_MASK |
8256                                R_ID_AA64ISAR1_DGH_MASK |
8257                                R_ID_AA64ISAR1_I8MM_MASK },
8258             { .name = "ID_AA64ISAR2_EL1",
8259               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8260                                R_ID_AA64ISAR2_RPRES_MASK |
8261                                R_ID_AA64ISAR2_GPA3_MASK |
8262                                R_ID_AA64ISAR2_APA3_MASK |
8263                                R_ID_AA64ISAR2_MOPS_MASK |
8264                                R_ID_AA64ISAR2_BC_MASK |
8265                                R_ID_AA64ISAR2_RPRFM_MASK |
8266                                R_ID_AA64ISAR2_CSSC_MASK },
8267             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8268               .is_glob = true },
8269         };
8270         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8271 #endif
8272         /*
8273          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8274          * TODO: For RMR, a write with bit 1 set should do something with
8275          * cpu_reset(). In the meantime, "the bit is strictly a request",
8276          * so we are in spec just ignoring writes.
8277          */
8278         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8279             !arm_feature(env, ARM_FEATURE_EL2)) {
8280             ARMCPRegInfo el1_reset_regs[] = {
8281                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8282                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8283                   .access = PL1_R,
8284                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8285                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8286                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8287                   .access = PL1_RW, .type = ARM_CP_CONST,
8288                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8289             };
8290             define_arm_cp_regs(cpu, el1_reset_regs);
8291         }
8292         define_arm_cp_regs(cpu, v8_idregs);
8293         define_arm_cp_regs(cpu, v8_cp_reginfo);
8294         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8295             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8296         }
8297 
8298         for (i = 4; i < 16; i++) {
8299             /*
8300              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8301              * For pre-v8 cores there are RAZ patterns for these in
8302              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8303              * v8 extends the "must RAZ" part of the ID register space
8304              * to also cover c0, 0, c{8-15}, {0-7}.
8305              * These are STATE_AA32 because in the AArch64 sysreg space
8306              * c4-c7 is where the AArch64 ID registers live (and we've
8307              * already defined those in v8_idregs[]), and c8-c15 are not
8308              * "must RAZ" for AArch64.
8309              */
8310             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8311             ARMCPRegInfo v8_aa32_raz_idregs = {
8312                 .name = name,
8313                 .state = ARM_CP_STATE_AA32,
8314                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8315                 .access = PL1_R, .type = ARM_CP_CONST,
8316                 .accessfn = access_aa64_tid3,
8317                 .resetvalue = 0 };
8318             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8319         }
8320     }
8321 
8322     /*
8323      * Register the base EL2 cpregs.
8324      * Pre v8, these registers are implemented only as part of the
8325      * Virtualization Extensions (EL2 present).  Beginning with v8,
8326      * if EL2 is missing but EL3 is enabled, mostly these become
8327      * RES0 from EL3, with some specific exceptions.
8328      */
8329     if (arm_feature(env, ARM_FEATURE_EL2)
8330         || (arm_feature(env, ARM_FEATURE_EL3)
8331             && arm_feature(env, ARM_FEATURE_V8))) {
8332         uint64_t vmpidr_def = mpidr_read_val(env);
8333         ARMCPRegInfo vpidr_regs[] = {
8334             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8335               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8336               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8337               .resetvalue = cpu->midr,
8338               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8339               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8340             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8341               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8342               .access = PL2_RW, .resetvalue = cpu->midr,
8343               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8344               .nv2_redirect_offset = 0x88,
8345               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8346             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8347               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8348               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8349               .resetvalue = vmpidr_def,
8350               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8351               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8352             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8353               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8354               .access = PL2_RW, .resetvalue = vmpidr_def,
8355               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8356               .nv2_redirect_offset = 0x50,
8357               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8358         };
8359         /*
8360          * The only field of MDCR_EL2 that has a defined architectural reset
8361          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8362          */
8363         ARMCPRegInfo mdcr_el2 = {
8364             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8365             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8366             .writefn = mdcr_el2_write,
8367             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8368             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8369         };
8370         define_one_arm_cp_reg(cpu, &mdcr_el2);
8371         define_arm_cp_regs(cpu, vpidr_regs);
8372         define_arm_cp_regs(cpu, el2_cp_reginfo);
8373         if (arm_feature(env, ARM_FEATURE_V8)) {
8374             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8375         }
8376         if (cpu_isar_feature(aa64_sel2, cpu)) {
8377             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8378         }
8379         /*
8380          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8381          * See commentary near RMR_EL1.
8382          */
8383         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8384             static const ARMCPRegInfo el2_reset_regs[] = {
8385                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8386                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8387                   .access = PL2_R,
8388                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8389                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8390                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8391                   .access = PL2_R,
8392                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8393                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8394                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8395                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8396             };
8397             define_arm_cp_regs(cpu, el2_reset_regs);
8398         }
8399     }
8400 
8401     /* Register the base EL3 cpregs. */
8402     if (arm_feature(env, ARM_FEATURE_EL3)) {
8403         define_arm_cp_regs(cpu, el3_cp_reginfo);
8404         ARMCPRegInfo el3_regs[] = {
8405             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8406               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8407               .access = PL3_R,
8408               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8409             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8410               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8411               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8412             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8413               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8414               .access = PL3_RW, .type = ARM_CP_CONST,
8415               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8416             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8417               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8418               .access = PL3_RW,
8419               .raw_writefn = raw_write, .writefn = sctlr_write,
8420               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8421               .resetvalue = cpu->reset_sctlr },
8422         };
8423 
8424         define_arm_cp_regs(cpu, el3_regs);
8425     }
8426     /*
8427      * The behaviour of NSACR is sufficiently various that we don't
8428      * try to describe it in a single reginfo:
8429      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8430      *     reads as constant 0xc00 from NS EL1 and NS EL2
8431      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8432      *  if v7 without EL3, register doesn't exist
8433      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8434      */
8435     if (arm_feature(env, ARM_FEATURE_EL3)) {
8436         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8437             static const ARMCPRegInfo nsacr = {
8438                 .name = "NSACR", .type = ARM_CP_CONST,
8439                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8440                 .access = PL1_RW, .accessfn = nsacr_access,
8441                 .resetvalue = 0xc00
8442             };
8443             define_one_arm_cp_reg(cpu, &nsacr);
8444         } else {
8445             static const ARMCPRegInfo nsacr = {
8446                 .name = "NSACR",
8447                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8448                 .access = PL3_RW | PL1_R,
8449                 .resetvalue = 0,
8450                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8451             };
8452             define_one_arm_cp_reg(cpu, &nsacr);
8453         }
8454     } else {
8455         if (arm_feature(env, ARM_FEATURE_V8)) {
8456             static const ARMCPRegInfo nsacr = {
8457                 .name = "NSACR", .type = ARM_CP_CONST,
8458                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8459                 .access = PL1_R,
8460                 .resetvalue = 0xc00
8461             };
8462             define_one_arm_cp_reg(cpu, &nsacr);
8463         }
8464     }
8465 
8466     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8467         if (arm_feature(env, ARM_FEATURE_V6)) {
8468             /* PMSAv6 not implemented */
8469             assert(arm_feature(env, ARM_FEATURE_V7));
8470             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8471             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8472         } else {
8473             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8474         }
8475     } else {
8476         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8477         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8478         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8479         if (cpu_isar_feature(aa32_hpd, cpu)) {
8480             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8481         }
8482     }
8483     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8484         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8485     }
8486     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8487         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8488     }
8489     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8490         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8491     }
8492 #ifndef CONFIG_USER_ONLY
8493     if (cpu_isar_feature(aa64_ecv, cpu)) {
8494         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8495     }
8496 #endif
8497     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8498         ARMCPRegInfo vapa_cp_reginfo[] = {
8499             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8500               .access = PL1_RW, .resetvalue = 0,
8501               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8502                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8503               .writefn = par_write},
8504 #ifndef CONFIG_USER_ONLY
8505             /* This underdecoding is safe because the reginfo is NO_RAW. */
8506             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8507               .access = PL1_W, .accessfn = ats_access,
8508               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8509 #endif
8510         };
8511 
8512         /*
8513          * When LPAE exists this 32-bit PAR register is an alias of the
8514          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8515          */
8516         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8517             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8518         }
8519         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8520     }
8521     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8522         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8523     }
8524     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8525         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8526     }
8527     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8528         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8529     }
8530     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8531         define_arm_cp_regs(cpu, omap_cp_reginfo);
8532     }
8533     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8534         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8535     }
8536     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8537         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8538     }
8539     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8540         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8541     }
8542     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8543         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8544     }
8545     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8546         define_arm_cp_regs(cpu, jazelle_regs);
8547     }
8548     /*
8549      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8550      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8551      * be read-only (ie write causes UNDEF exception).
8552      */
8553     {
8554         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8555             /*
8556              * Pre-v8 MIDR space.
8557              * Note that the MIDR isn't a simple constant register because
8558              * of the TI925 behaviour where writes to another register can
8559              * cause the MIDR value to change.
8560              *
8561              * Unimplemented registers in the c15 0 0 0 space default to
8562              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8563              * and friends override accordingly.
8564              */
8565             { .name = "MIDR",
8566               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8567               .access = PL1_R, .resetvalue = cpu->midr,
8568               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8569               .readfn = midr_read,
8570               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8571               .type = ARM_CP_OVERRIDE },
8572             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8573             { .name = "DUMMY",
8574               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8575               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8576             { .name = "DUMMY",
8577               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8578               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8579             { .name = "DUMMY",
8580               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8581               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8582             { .name = "DUMMY",
8583               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8584               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8585             { .name = "DUMMY",
8586               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8587               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8588         };
8589         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8590             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8591               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8592               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8593               .fgt = FGT_MIDR_EL1,
8594               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8595               .readfn = midr_read },
8596             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8597             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8598               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8599               .access = PL1_R, .resetvalue = cpu->midr },
8600             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8601               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8602               .access = PL1_R,
8603               .accessfn = access_aa64_tid1,
8604               .fgt = FGT_REVIDR_EL1,
8605               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8606         };
8607         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8608             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8609             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8610             .access = PL1_R, .resetvalue = cpu->midr
8611         };
8612         ARMCPRegInfo id_cp_reginfo[] = {
8613             /* These are common to v8 and pre-v8 */
8614             { .name = "CTR",
8615               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8616               .access = PL1_R, .accessfn = ctr_el0_access,
8617               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8618             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8619               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8620               .access = PL0_R, .accessfn = ctr_el0_access,
8621               .fgt = FGT_CTR_EL0,
8622               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8623             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8624             { .name = "TCMTR",
8625               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8626               .access = PL1_R,
8627               .accessfn = access_aa32_tid1,
8628               .type = ARM_CP_CONST, .resetvalue = 0 },
8629         };
8630         /* TLBTR is specific to VMSA */
8631         ARMCPRegInfo id_tlbtr_reginfo = {
8632               .name = "TLBTR",
8633               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8634               .access = PL1_R,
8635               .accessfn = access_aa32_tid1,
8636               .type = ARM_CP_CONST, .resetvalue = 0,
8637         };
8638         /* MPUIR is specific to PMSA V6+ */
8639         ARMCPRegInfo id_mpuir_reginfo = {
8640               .name = "MPUIR",
8641               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8642               .access = PL1_R, .type = ARM_CP_CONST,
8643               .resetvalue = cpu->pmsav7_dregion << 8
8644         };
8645         /* HMPUIR is specific to PMSA V8 */
8646         ARMCPRegInfo id_hmpuir_reginfo = {
8647             .name = "HMPUIR",
8648             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8649             .access = PL2_R, .type = ARM_CP_CONST,
8650             .resetvalue = cpu->pmsav8r_hdregion
8651         };
8652         static const ARMCPRegInfo crn0_wi_reginfo = {
8653             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8654             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8655             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8656         };
8657 #ifdef CONFIG_USER_ONLY
8658         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8659             { .name = "MIDR_EL1",
8660               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8661                                R_MIDR_EL1_PARTNUM_MASK |
8662                                R_MIDR_EL1_ARCHITECTURE_MASK |
8663                                R_MIDR_EL1_VARIANT_MASK |
8664                                R_MIDR_EL1_IMPLEMENTER_MASK },
8665             { .name = "REVIDR_EL1" },
8666         };
8667         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8668 #endif
8669         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8670             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8671             size_t i;
8672             /*
8673              * Register the blanket "writes ignored" value first to cover the
8674              * whole space. Then update the specific ID registers to allow write
8675              * access, so that they ignore writes rather than causing them to
8676              * UNDEF.
8677              */
8678             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8679             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8680                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8681             }
8682             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8683                 id_cp_reginfo[i].access = PL1_RW;
8684             }
8685             id_mpuir_reginfo.access = PL1_RW;
8686             id_tlbtr_reginfo.access = PL1_RW;
8687         }
8688         if (arm_feature(env, ARM_FEATURE_V8)) {
8689             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8690             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8691                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8692             }
8693         } else {
8694             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8695         }
8696         define_arm_cp_regs(cpu, id_cp_reginfo);
8697         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8698             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8699         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8700                    arm_feature(env, ARM_FEATURE_V8)) {
8701             uint32_t i = 0;
8702             char *tmp_string;
8703 
8704             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8705             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8706             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8707 
8708             /* Register alias is only valid for first 32 indexes */
8709             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8710                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8711                 uint8_t opc1 = extract32(i, 4, 1);
8712                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8713 
8714                 tmp_string = g_strdup_printf("PRBAR%u", i);
8715                 ARMCPRegInfo tmp_prbarn_reginfo = {
8716                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8717                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8718                     .access = PL1_RW, .resetvalue = 0,
8719                     .accessfn = access_tvm_trvm,
8720                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8721                 };
8722                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8723                 g_free(tmp_string);
8724 
8725                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8726                 tmp_string = g_strdup_printf("PRLAR%u", i);
8727                 ARMCPRegInfo tmp_prlarn_reginfo = {
8728                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8729                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8730                     .access = PL1_RW, .resetvalue = 0,
8731                     .accessfn = access_tvm_trvm,
8732                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8733                 };
8734                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8735                 g_free(tmp_string);
8736             }
8737 
8738             /* Register alias is only valid for first 32 indexes */
8739             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8740                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8741                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8742                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8743 
8744                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8745                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8746                     .name = tmp_string,
8747                     .type = ARM_CP_NO_RAW,
8748                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8749                     .access = PL2_RW, .resetvalue = 0,
8750                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8751                 };
8752                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8753                 g_free(tmp_string);
8754 
8755                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8756                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8757                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8758                     .name = tmp_string,
8759                     .type = ARM_CP_NO_RAW,
8760                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8761                     .access = PL2_RW, .resetvalue = 0,
8762                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8763                 };
8764                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8765                 g_free(tmp_string);
8766             }
8767         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8768             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8769         }
8770     }
8771 
8772     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8773         ARMCPRegInfo mpidr_cp_reginfo[] = {
8774             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8775               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8776               .fgt = FGT_MPIDR_EL1,
8777               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8778         };
8779 #ifdef CONFIG_USER_ONLY
8780         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8781             { .name = "MPIDR_EL1",
8782               .fixed_bits = 0x0000000080000000 },
8783         };
8784         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8785 #endif
8786         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8787     }
8788 
8789     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8790         ARMCPRegInfo auxcr_reginfo[] = {
8791             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8792               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8793               .access = PL1_RW, .accessfn = access_tacr,
8794               .nv2_redirect_offset = 0x118,
8795               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8796             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8797               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8798               .access = PL2_RW, .type = ARM_CP_CONST,
8799               .resetvalue = 0 },
8800             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8801               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8802               .access = PL3_RW, .type = ARM_CP_CONST,
8803               .resetvalue = 0 },
8804         };
8805         define_arm_cp_regs(cpu, auxcr_reginfo);
8806         if (cpu_isar_feature(aa32_ac2, cpu)) {
8807             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8808         }
8809     }
8810 
8811     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8812         /*
8813          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8814          * There are two flavours:
8815          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8816          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8817          *      32-bit register visible to AArch32 at a different encoding
8818          *      to the "flavour 1" register and with the bits rearranged to
8819          *      be able to squash a 64-bit address into the 32-bit view.
8820          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8821          * in future if we support AArch32-only configs of some of the
8822          * AArch64 cores we might need to add a specific feature flag
8823          * to indicate cores with "flavour 2" CBAR.
8824          */
8825         if (arm_feature(env, ARM_FEATURE_V8)) {
8826             /* 32 bit view is [31:18] 0...0 [43:32]. */
8827             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8828                 | extract64(cpu->reset_cbar, 32, 12);
8829             ARMCPRegInfo cbar_reginfo[] = {
8830                 { .name = "CBAR",
8831                   .type = ARM_CP_CONST,
8832                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8833                   .access = PL1_R, .resetvalue = cbar32 },
8834                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8835                   .type = ARM_CP_CONST,
8836                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8837                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8838             };
8839             /* We don't implement a r/w 64 bit CBAR currently */
8840             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8841             define_arm_cp_regs(cpu, cbar_reginfo);
8842         } else {
8843             ARMCPRegInfo cbar = {
8844                 .name = "CBAR",
8845                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8846                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8847                 .fieldoffset = offsetof(CPUARMState,
8848                                         cp15.c15_config_base_address)
8849             };
8850             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8851                 cbar.access = PL1_R;
8852                 cbar.fieldoffset = 0;
8853                 cbar.type = ARM_CP_CONST;
8854             }
8855             define_one_arm_cp_reg(cpu, &cbar);
8856         }
8857     }
8858 
8859     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8860         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8861             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8862               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8863               .access = PL1_RW, .writefn = vbar_write,
8864               .accessfn = access_nv1,
8865               .fgt = FGT_VBAR_EL1,
8866               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8867               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8868                                      offsetof(CPUARMState, cp15.vbar_ns) },
8869               .resetvalue = 0 },
8870         };
8871         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8872     }
8873 
8874     /* Generic registers whose values depend on the implementation */
8875     {
8876         ARMCPRegInfo sctlr = {
8877             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8878             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8879             .access = PL1_RW, .accessfn = access_tvm_trvm,
8880             .fgt = FGT_SCTLR_EL1,
8881             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8882             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8883                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8884             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8885             .raw_writefn = raw_write,
8886         };
8887         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8888             /*
8889              * Normally we would always end the TB on an SCTLR write, but Linux
8890              * arch/arm/mach-pxa/sleep.S expects two instructions following
8891              * an MMU enable to execute from cache.  Imitate this behaviour.
8892              */
8893             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8894         }
8895         define_one_arm_cp_reg(cpu, &sctlr);
8896 
8897         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8898             arm_feature(env, ARM_FEATURE_V8)) {
8899             ARMCPRegInfo vsctlr = {
8900                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8901                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8902                 .access = PL2_RW, .resetvalue = 0x0,
8903                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8904             };
8905             define_one_arm_cp_reg(cpu, &vsctlr);
8906         }
8907     }
8908 
8909     if (cpu_isar_feature(aa64_lor, cpu)) {
8910         define_arm_cp_regs(cpu, lor_reginfo);
8911     }
8912     if (cpu_isar_feature(aa64_pan, cpu)) {
8913         define_one_arm_cp_reg(cpu, &pan_reginfo);
8914     }
8915 #ifndef CONFIG_USER_ONLY
8916     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8917         define_arm_cp_regs(cpu, ats1e1_reginfo);
8918     }
8919     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8920         define_arm_cp_regs(cpu, ats1cp_reginfo);
8921     }
8922 #endif
8923     if (cpu_isar_feature(aa64_uao, cpu)) {
8924         define_one_arm_cp_reg(cpu, &uao_reginfo);
8925     }
8926 
8927     if (cpu_isar_feature(aa64_dit, cpu)) {
8928         define_one_arm_cp_reg(cpu, &dit_reginfo);
8929     }
8930     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8931         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8932     }
8933     if (cpu_isar_feature(any_ras, cpu)) {
8934         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8935     }
8936 
8937     if (cpu_isar_feature(aa64_vh, cpu) ||
8938         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8939         define_one_arm_cp_reg(cpu, &contextidr_el2);
8940     }
8941     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8942         define_arm_cp_regs(cpu, vhe_reginfo);
8943     }
8944 
8945     if (cpu_isar_feature(aa64_sve, cpu)) {
8946         define_arm_cp_regs(cpu, zcr_reginfo);
8947     }
8948 
8949     if (cpu_isar_feature(aa64_hcx, cpu)) {
8950         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8951     }
8952 
8953 #ifdef TARGET_AARCH64
8954     if (cpu_isar_feature(aa64_sme, cpu)) {
8955         define_arm_cp_regs(cpu, sme_reginfo);
8956     }
8957     if (cpu_isar_feature(aa64_pauth, cpu)) {
8958         define_arm_cp_regs(cpu, pauth_reginfo);
8959     }
8960     if (cpu_isar_feature(aa64_rndr, cpu)) {
8961         define_arm_cp_regs(cpu, rndr_reginfo);
8962     }
8963     /* Data Cache clean instructions up to PoP */
8964     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8965         define_one_arm_cp_reg(cpu, dcpop_reg);
8966 
8967         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8968             define_one_arm_cp_reg(cpu, dcpodp_reg);
8969         }
8970     }
8971 
8972     /*
8973      * If full MTE is enabled, add all of the system registers.
8974      * If only "instructions available at EL0" are enabled,
8975      * then define only a RAZ/WI version of PSTATE.TCO.
8976      */
8977     if (cpu_isar_feature(aa64_mte, cpu)) {
8978         ARMCPRegInfo gmid_reginfo = {
8979             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8980             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8981             .access = PL1_R, .accessfn = access_aa64_tid5,
8982             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8983         };
8984         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8985         define_arm_cp_regs(cpu, mte_reginfo);
8986         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8987     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8988         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8989         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8990     }
8991 
8992     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8993         define_arm_cp_regs(cpu, scxtnum_reginfo);
8994     }
8995 
8996     if (cpu_isar_feature(aa64_fgt, cpu)) {
8997         define_arm_cp_regs(cpu, fgt_reginfo);
8998     }
8999 
9000     if (cpu_isar_feature(aa64_rme, cpu)) {
9001         define_arm_cp_regs(cpu, rme_reginfo);
9002         if (cpu_isar_feature(aa64_mte, cpu)) {
9003             define_arm_cp_regs(cpu, rme_mte_reginfo);
9004         }
9005     }
9006 
9007     if (cpu_isar_feature(aa64_nv2, cpu)) {
9008         define_arm_cp_regs(cpu, nv2_reginfo);
9009     }
9010 
9011     if (cpu_isar_feature(aa64_nmi, cpu)) {
9012         define_arm_cp_regs(cpu, nmi_reginfo);
9013     }
9014 #endif
9015 
9016     if (cpu_isar_feature(any_predinv, cpu)) {
9017         define_arm_cp_regs(cpu, predinv_reginfo);
9018     }
9019 
9020     if (cpu_isar_feature(any_ccidx, cpu)) {
9021         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9022     }
9023 
9024 #ifndef CONFIG_USER_ONLY
9025     /*
9026      * Register redirections and aliases must be done last,
9027      * after the registers from the other extensions have been defined.
9028      */
9029     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9030         define_arm_vh_e2h_redirects_aliases(cpu);
9031     }
9032 #endif
9033 }
9034 
9035 /*
9036  * Private utility function for define_one_arm_cp_reg_with_opaque():
9037  * add a single reginfo struct to the hash table.
9038  */
9039 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9040                                    void *opaque, CPState state,
9041                                    CPSecureState secstate,
9042                                    int crm, int opc1, int opc2,
9043                                    const char *name)
9044 {
9045     CPUARMState *env = &cpu->env;
9046     uint32_t key;
9047     ARMCPRegInfo *r2;
9048     bool is64 = r->type & ARM_CP_64BIT;
9049     bool ns = secstate & ARM_CP_SECSTATE_NS;
9050     int cp = r->cp;
9051     size_t name_len;
9052     bool make_const;
9053 
9054     switch (state) {
9055     case ARM_CP_STATE_AA32:
9056         /* We assume it is a cp15 register if the .cp field is left unset. */
9057         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9058             cp = 15;
9059         }
9060         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9061         break;
9062     case ARM_CP_STATE_AA64:
9063         /*
9064          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9065          * cp == 0 as equivalent to the value for "standard guest-visible
9066          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9067          * in their AArch64 view (the .cp value may be non-zero for the
9068          * benefit of the AArch32 view).
9069          */
9070         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9071             cp = CP_REG_ARM64_SYSREG_CP;
9072         }
9073         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9074         break;
9075     default:
9076         g_assert_not_reached();
9077     }
9078 
9079     /* Overriding of an existing definition must be explicitly requested. */
9080     if (!(r->type & ARM_CP_OVERRIDE)) {
9081         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9082         if (oldreg) {
9083             assert(oldreg->type & ARM_CP_OVERRIDE);
9084         }
9085     }
9086 
9087     /*
9088      * Eliminate registers that are not present because the EL is missing.
9089      * Doing this here makes it easier to put all registers for a given
9090      * feature into the same ARMCPRegInfo array and define them all at once.
9091      */
9092     make_const = false;
9093     if (arm_feature(env, ARM_FEATURE_EL3)) {
9094         /*
9095          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9096          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9097          */
9098         int min_el = ctz32(r->access) / 2;
9099         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9100             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9101                 return;
9102             }
9103             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9104         }
9105     } else {
9106         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9107                                  ? PL2_RW : PL1_RW);
9108         if ((r->access & max_el) == 0) {
9109             return;
9110         }
9111     }
9112 
9113     /* Combine cpreg and name into one allocation. */
9114     name_len = strlen(name) + 1;
9115     r2 = g_malloc(sizeof(*r2) + name_len);
9116     *r2 = *r;
9117     r2->name = memcpy(r2 + 1, name, name_len);
9118 
9119     /*
9120      * Update fields to match the instantiation, overwiting wildcards
9121      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9122      */
9123     r2->cp = cp;
9124     r2->crm = crm;
9125     r2->opc1 = opc1;
9126     r2->opc2 = opc2;
9127     r2->state = state;
9128     r2->secure = secstate;
9129     if (opaque) {
9130         r2->opaque = opaque;
9131     }
9132 
9133     if (make_const) {
9134         /* This should not have been a very special register to begin. */
9135         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9136         assert(old_special == 0 || old_special == ARM_CP_NOP);
9137         /*
9138          * Set the special function to CONST, retaining the other flags.
9139          * This is important for e.g. ARM_CP_SVE so that we still
9140          * take the SVE trap if CPTR_EL3.EZ == 0.
9141          */
9142         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9143         /*
9144          * Usually, these registers become RES0, but there are a few
9145          * special cases like VPIDR_EL2 which have a constant non-zero
9146          * value with writes ignored.
9147          */
9148         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9149             r2->resetvalue = 0;
9150         }
9151         /*
9152          * ARM_CP_CONST has precedence, so removing the callbacks and
9153          * offsets are not strictly necessary, but it is potentially
9154          * less confusing to debug later.
9155          */
9156         r2->readfn = NULL;
9157         r2->writefn = NULL;
9158         r2->raw_readfn = NULL;
9159         r2->raw_writefn = NULL;
9160         r2->resetfn = NULL;
9161         r2->fieldoffset = 0;
9162         r2->bank_fieldoffsets[0] = 0;
9163         r2->bank_fieldoffsets[1] = 0;
9164     } else {
9165         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9166 
9167         if (isbanked) {
9168             /*
9169              * Register is banked (using both entries in array).
9170              * Overwriting fieldoffset as the array is only used to define
9171              * banked registers but later only fieldoffset is used.
9172              */
9173             r2->fieldoffset = r->bank_fieldoffsets[ns];
9174         }
9175         if (state == ARM_CP_STATE_AA32) {
9176             if (isbanked) {
9177                 /*
9178                  * If the register is banked then we don't need to migrate or
9179                  * reset the 32-bit instance in certain cases:
9180                  *
9181                  * 1) If the register has both 32-bit and 64-bit instances
9182                  *    then we can count on the 64-bit instance taking care
9183                  *    of the non-secure bank.
9184                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9185                  *    version taking care of the secure bank.  This requires
9186                  *    that separate 32 and 64-bit definitions are provided.
9187                  */
9188                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9189                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9190                     r2->type |= ARM_CP_ALIAS;
9191                 }
9192             } else if ((secstate != r->secure) && !ns) {
9193                 /*
9194                  * The register is not banked so we only want to allow
9195                  * migration of the non-secure instance.
9196                  */
9197                 r2->type |= ARM_CP_ALIAS;
9198             }
9199 
9200             if (HOST_BIG_ENDIAN &&
9201                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9202                 r2->fieldoffset += sizeof(uint32_t);
9203             }
9204         }
9205     }
9206 
9207     /*
9208      * By convention, for wildcarded registers only the first
9209      * entry is used for migration; the others are marked as
9210      * ALIAS so we don't try to transfer the register
9211      * multiple times. Special registers (ie NOP/WFI) are
9212      * never migratable and not even raw-accessible.
9213      */
9214     if (r2->type & ARM_CP_SPECIAL_MASK) {
9215         r2->type |= ARM_CP_NO_RAW;
9216     }
9217     if (((r->crm == CP_ANY) && crm != 0) ||
9218         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9219         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9220         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9221     }
9222 
9223     /*
9224      * Check that raw accesses are either forbidden or handled. Note that
9225      * we can't assert this earlier because the setup of fieldoffset for
9226      * banked registers has to be done first.
9227      */
9228     if (!(r2->type & ARM_CP_NO_RAW)) {
9229         assert(!raw_accessors_invalid(r2));
9230     }
9231 
9232     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9233 }
9234 
9235 
9236 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9237                                        const ARMCPRegInfo *r, void *opaque)
9238 {
9239     /*
9240      * Define implementations of coprocessor registers.
9241      * We store these in a hashtable because typically
9242      * there are less than 150 registers in a space which
9243      * is 16*16*16*8*8 = 262144 in size.
9244      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9245      * If a register is defined twice then the second definition is
9246      * used, so this can be used to define some generic registers and
9247      * then override them with implementation specific variations.
9248      * At least one of the original and the second definition should
9249      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9250      * against accidental use.
9251      *
9252      * The state field defines whether the register is to be
9253      * visible in the AArch32 or AArch64 execution state. If the
9254      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9255      * reginfo structure for the AArch32 view, which sees the lower
9256      * 32 bits of the 64 bit register.
9257      *
9258      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9259      * be wildcarded. AArch64 registers are always considered to be 64
9260      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9261      * the register, if any.
9262      */
9263     int crm, opc1, opc2;
9264     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9265     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9266     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9267     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9268     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9269     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9270     CPState state;
9271 
9272     /* 64 bit registers have only CRm and Opc1 fields */
9273     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9274     /* op0 only exists in the AArch64 encodings */
9275     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9276     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9277     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9278     /*
9279      * This API is only for Arm's system coprocessors (14 and 15) or
9280      * (M-profile or v7A-and-earlier only) for implementation defined
9281      * coprocessors in the range 0..7.  Our decode assumes this, since
9282      * 8..13 can be used for other insns including VFP and Neon. See
9283      * valid_cp() in translate.c.  Assert here that we haven't tried
9284      * to use an invalid coprocessor number.
9285      */
9286     switch (r->state) {
9287     case ARM_CP_STATE_BOTH:
9288         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9289         if (r->cp == 0) {
9290             break;
9291         }
9292         /* fall through */
9293     case ARM_CP_STATE_AA32:
9294         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9295             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9296             assert(r->cp >= 14 && r->cp <= 15);
9297         } else {
9298             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9299         }
9300         break;
9301     case ARM_CP_STATE_AA64:
9302         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9303         break;
9304     default:
9305         g_assert_not_reached();
9306     }
9307     /*
9308      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9309      * encodes a minimum access level for the register. We roll this
9310      * runtime check into our general permission check code, so check
9311      * here that the reginfo's specified permissions are strict enough
9312      * to encompass the generic architectural permission check.
9313      */
9314     if (r->state != ARM_CP_STATE_AA32) {
9315         CPAccessRights mask;
9316         switch (r->opc1) {
9317         case 0:
9318             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9319             mask = PL0U_R | PL1_RW;
9320             break;
9321         case 1: case 2:
9322             /* min_EL EL1 */
9323             mask = PL1_RW;
9324             break;
9325         case 3:
9326             /* min_EL EL0 */
9327             mask = PL0_RW;
9328             break;
9329         case 4:
9330         case 5:
9331             /* min_EL EL2 */
9332             mask = PL2_RW;
9333             break;
9334         case 6:
9335             /* min_EL EL3 */
9336             mask = PL3_RW;
9337             break;
9338         case 7:
9339             /* min_EL EL1, secure mode only (we don't check the latter) */
9340             mask = PL1_RW;
9341             break;
9342         default:
9343             /* broken reginfo with out-of-range opc1 */
9344             g_assert_not_reached();
9345         }
9346         /* assert our permissions are not too lax (stricter is fine) */
9347         assert((r->access & ~mask) == 0);
9348     }
9349 
9350     /*
9351      * Check that the register definition has enough info to handle
9352      * reads and writes if they are permitted.
9353      */
9354     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9355         if (r->access & PL3_R) {
9356             assert((r->fieldoffset ||
9357                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9358                    r->readfn);
9359         }
9360         if (r->access & PL3_W) {
9361             assert((r->fieldoffset ||
9362                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9363                    r->writefn);
9364         }
9365     }
9366 
9367     for (crm = crmmin; crm <= crmmax; crm++) {
9368         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9369             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9370                 for (state = ARM_CP_STATE_AA32;
9371                      state <= ARM_CP_STATE_AA64; state++) {
9372                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9373                         continue;
9374                     }
9375                     if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
9376                         cpu_isar_feature(aa64_xs, cpu)) {
9377                         /*
9378                          * This is a TLBI insn which has an NXS variant. The
9379                          * NXS variant is at the same encoding except that
9380                          * crn is +1, and has the same behaviour except for
9381                          * fine-grained trapping. Add the NXS insn here and
9382                          * then fall through to add the normal register.
9383                          * add_cpreg_to_hashtable() copies the cpreg struct
9384                          * and name that it is passed, so it's OK to use
9385                          * a local struct here.
9386                          */
9387                         ARMCPRegInfo nxs_ri = *r;
9388                         g_autofree char *name = g_strdup_printf("%sNXS", r->name);
9389 
9390                         assert(state == ARM_CP_STATE_AA64);
9391                         assert(nxs_ri.crn < 0xf);
9392                         nxs_ri.crn++;
9393                         if (nxs_ri.fgt) {
9394                             nxs_ri.fgt |= R_FGT_NXS_MASK;
9395                         }
9396                         add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
9397                                                ARM_CP_SECSTATE_NS,
9398                                                crm, opc1, opc2, name);
9399                     }
9400                     if (state == ARM_CP_STATE_AA32) {
9401                         /*
9402                          * Under AArch32 CP registers can be common
9403                          * (same for secure and non-secure world) or banked.
9404                          */
9405                         char *name;
9406 
9407                         switch (r->secure) {
9408                         case ARM_CP_SECSTATE_S:
9409                         case ARM_CP_SECSTATE_NS:
9410                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9411                                                    r->secure, crm, opc1, opc2,
9412                                                    r->name);
9413                             break;
9414                         case ARM_CP_SECSTATE_BOTH:
9415                             name = g_strdup_printf("%s_S", r->name);
9416                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9417                                                    ARM_CP_SECSTATE_S,
9418                                                    crm, opc1, opc2, name);
9419                             g_free(name);
9420                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9421                                                    ARM_CP_SECSTATE_NS,
9422                                                    crm, opc1, opc2, r->name);
9423                             break;
9424                         default:
9425                             g_assert_not_reached();
9426                         }
9427                     } else {
9428                         /*
9429                          * AArch64 registers get mapped to non-secure instance
9430                          * of AArch32
9431                          */
9432                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9433                                                ARM_CP_SECSTATE_NS,
9434                                                crm, opc1, opc2, r->name);
9435                     }
9436                 }
9437             }
9438         }
9439     }
9440 }
9441 
9442 /* Define a whole list of registers */
9443 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9444                                         void *opaque, size_t len)
9445 {
9446     size_t i;
9447     for (i = 0; i < len; ++i) {
9448         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9449     }
9450 }
9451 
9452 /*
9453  * Modify ARMCPRegInfo for access from userspace.
9454  *
9455  * This is a data driven modification directed by
9456  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9457  * user-space cannot alter any values and dynamic values pertaining to
9458  * execution state are hidden from user space view anyway.
9459  */
9460 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9461                                  const ARMCPRegUserSpaceInfo *mods,
9462                                  size_t mods_len)
9463 {
9464     for (size_t mi = 0; mi < mods_len; ++mi) {
9465         const ARMCPRegUserSpaceInfo *m = mods + mi;
9466         GPatternSpec *pat = NULL;
9467 
9468         if (m->is_glob) {
9469             pat = g_pattern_spec_new(m->name);
9470         }
9471         for (size_t ri = 0; ri < regs_len; ++ri) {
9472             ARMCPRegInfo *r = regs + ri;
9473 
9474             if (pat && g_pattern_match_string(pat, r->name)) {
9475                 r->type = ARM_CP_CONST;
9476                 r->access = PL0U_R;
9477                 r->resetvalue = 0;
9478                 /* continue */
9479             } else if (strcmp(r->name, m->name) == 0) {
9480                 r->type = ARM_CP_CONST;
9481                 r->access = PL0U_R;
9482                 r->resetvalue &= m->exported_bits;
9483                 r->resetvalue |= m->fixed_bits;
9484                 break;
9485             }
9486         }
9487         if (pat) {
9488             g_pattern_spec_free(pat);
9489         }
9490     }
9491 }
9492 
9493 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9494 {
9495     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9496 }
9497 
9498 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9499                          uint64_t value)
9500 {
9501     /* Helper coprocessor write function for write-ignore registers */
9502 }
9503 
9504 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9505 {
9506     /* Helper coprocessor write function for read-as-zero registers */
9507     return 0;
9508 }
9509 
9510 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9511 {
9512     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9513 }
9514 
9515 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9516 {
9517     /*
9518      * Return true if it is not valid for us to switch to
9519      * this CPU mode (ie all the UNPREDICTABLE cases in
9520      * the ARM ARM CPSRWriteByInstr pseudocode).
9521      */
9522 
9523     /* Changes to or from Hyp via MSR and CPS are illegal. */
9524     if (write_type == CPSRWriteByInstr &&
9525         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9526          mode == ARM_CPU_MODE_HYP)) {
9527         return 1;
9528     }
9529 
9530     switch (mode) {
9531     case ARM_CPU_MODE_USR:
9532         return 0;
9533     case ARM_CPU_MODE_SYS:
9534     case ARM_CPU_MODE_SVC:
9535     case ARM_CPU_MODE_ABT:
9536     case ARM_CPU_MODE_UND:
9537     case ARM_CPU_MODE_IRQ:
9538     case ARM_CPU_MODE_FIQ:
9539         /*
9540          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9541          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9542          */
9543         /*
9544          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9545          * and CPS are treated as illegal mode changes.
9546          */
9547         if (write_type == CPSRWriteByInstr &&
9548             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9549             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9550             return 1;
9551         }
9552         return 0;
9553     case ARM_CPU_MODE_HYP:
9554         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9555     case ARM_CPU_MODE_MON:
9556         return arm_current_el(env) < 3;
9557     default:
9558         return 1;
9559     }
9560 }
9561 
9562 uint32_t cpsr_read(CPUARMState *env)
9563 {
9564     int ZF;
9565     ZF = (env->ZF == 0);
9566     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9567         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9568         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9569         | ((env->condexec_bits & 0xfc) << 8)
9570         | (env->GE << 16) | (env->daif & CPSR_AIF);
9571 }
9572 
9573 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9574                 CPSRWriteType write_type)
9575 {
9576     uint32_t changed_daif;
9577     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9578         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9579 
9580     if (mask & CPSR_NZCV) {
9581         env->ZF = (~val) & CPSR_Z;
9582         env->NF = val;
9583         env->CF = (val >> 29) & 1;
9584         env->VF = (val << 3) & 0x80000000;
9585     }
9586     if (mask & CPSR_Q) {
9587         env->QF = ((val & CPSR_Q) != 0);
9588     }
9589     if (mask & CPSR_T) {
9590         env->thumb = ((val & CPSR_T) != 0);
9591     }
9592     if (mask & CPSR_IT_0_1) {
9593         env->condexec_bits &= ~3;
9594         env->condexec_bits |= (val >> 25) & 3;
9595     }
9596     if (mask & CPSR_IT_2_7) {
9597         env->condexec_bits &= 3;
9598         env->condexec_bits |= (val >> 8) & 0xfc;
9599     }
9600     if (mask & CPSR_GE) {
9601         env->GE = (val >> 16) & 0xf;
9602     }
9603 
9604     /*
9605      * In a V7 implementation that includes the security extensions but does
9606      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9607      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9608      * bits respectively.
9609      *
9610      * In a V8 implementation, it is permitted for privileged software to
9611      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9612      */
9613     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9614         arm_feature(env, ARM_FEATURE_EL3) &&
9615         !arm_feature(env, ARM_FEATURE_EL2) &&
9616         !arm_is_secure(env)) {
9617 
9618         changed_daif = (env->daif ^ val) & mask;
9619 
9620         if (changed_daif & CPSR_A) {
9621             /*
9622              * Check to see if we are allowed to change the masking of async
9623              * abort exceptions from a non-secure state.
9624              */
9625             if (!(env->cp15.scr_el3 & SCR_AW)) {
9626                 qemu_log_mask(LOG_GUEST_ERROR,
9627                               "Ignoring attempt to switch CPSR_A flag from "
9628                               "non-secure world with SCR.AW bit clear\n");
9629                 mask &= ~CPSR_A;
9630             }
9631         }
9632 
9633         if (changed_daif & CPSR_F) {
9634             /*
9635              * Check to see if we are allowed to change the masking of FIQ
9636              * exceptions from a non-secure state.
9637              */
9638             if (!(env->cp15.scr_el3 & SCR_FW)) {
9639                 qemu_log_mask(LOG_GUEST_ERROR,
9640                               "Ignoring attempt to switch CPSR_F flag from "
9641                               "non-secure world with SCR.FW bit clear\n");
9642                 mask &= ~CPSR_F;
9643             }
9644 
9645             /*
9646              * Check whether non-maskable FIQ (NMFI) support is enabled.
9647              * If this bit is set software is not allowed to mask
9648              * FIQs, but is allowed to set CPSR_F to 0.
9649              */
9650             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9651                 (val & CPSR_F)) {
9652                 qemu_log_mask(LOG_GUEST_ERROR,
9653                               "Ignoring attempt to enable CPSR_F flag "
9654                               "(non-maskable FIQ [NMFI] support enabled)\n");
9655                 mask &= ~CPSR_F;
9656             }
9657         }
9658     }
9659 
9660     env->daif &= ~(CPSR_AIF & mask);
9661     env->daif |= val & CPSR_AIF & mask;
9662 
9663     if (write_type != CPSRWriteRaw &&
9664         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9665         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9666             /*
9667              * Note that we can only get here in USR mode if this is a
9668              * gdb stub write; for this case we follow the architectural
9669              * behaviour for guest writes in USR mode of ignoring an attempt
9670              * to switch mode. (Those are caught by translate.c for writes
9671              * triggered by guest instructions.)
9672              */
9673             mask &= ~CPSR_M;
9674         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9675             /*
9676              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9677              * v7, and has defined behaviour in v8:
9678              *  + leave CPSR.M untouched
9679              *  + allow changes to the other CPSR fields
9680              *  + set PSTATE.IL
9681              * For user changes via the GDB stub, we don't set PSTATE.IL,
9682              * as this would be unnecessarily harsh for a user error.
9683              */
9684             mask &= ~CPSR_M;
9685             if (write_type != CPSRWriteByGDBStub &&
9686                 arm_feature(env, ARM_FEATURE_V8)) {
9687                 mask |= CPSR_IL;
9688                 val |= CPSR_IL;
9689             }
9690             qemu_log_mask(LOG_GUEST_ERROR,
9691                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9692                           aarch32_mode_name(env->uncached_cpsr),
9693                           aarch32_mode_name(val));
9694         } else {
9695             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9696                           write_type == CPSRWriteExceptionReturn ?
9697                           "Exception return from AArch32" :
9698                           "AArch32 mode switch from",
9699                           aarch32_mode_name(env->uncached_cpsr),
9700                           aarch32_mode_name(val), env->regs[15]);
9701             switch_mode(env, val & CPSR_M);
9702         }
9703     }
9704     mask &= ~CACHED_CPSR_BITS;
9705     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9706     if (tcg_enabled() && rebuild_hflags) {
9707         arm_rebuild_hflags(env);
9708     }
9709 }
9710 
9711 #ifdef CONFIG_USER_ONLY
9712 
9713 static void switch_mode(CPUARMState *env, int mode)
9714 {
9715     ARMCPU *cpu = env_archcpu(env);
9716 
9717     if (mode != ARM_CPU_MODE_USR) {
9718         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9719     }
9720 }
9721 
9722 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9723                                  uint32_t cur_el, bool secure)
9724 {
9725     return 1;
9726 }
9727 
9728 void aarch64_sync_64_to_32(CPUARMState *env)
9729 {
9730     g_assert_not_reached();
9731 }
9732 
9733 #else
9734 
9735 static void switch_mode(CPUARMState *env, int mode)
9736 {
9737     int old_mode;
9738     int i;
9739 
9740     old_mode = env->uncached_cpsr & CPSR_M;
9741     if (mode == old_mode) {
9742         return;
9743     }
9744 
9745     if (old_mode == ARM_CPU_MODE_FIQ) {
9746         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9747         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9748     } else if (mode == ARM_CPU_MODE_FIQ) {
9749         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9750         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9751     }
9752 
9753     i = bank_number(old_mode);
9754     env->banked_r13[i] = env->regs[13];
9755     env->banked_spsr[i] = env->spsr;
9756 
9757     i = bank_number(mode);
9758     env->regs[13] = env->banked_r13[i];
9759     env->spsr = env->banked_spsr[i];
9760 
9761     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9762     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9763 }
9764 
9765 /*
9766  * Physical Interrupt Target EL Lookup Table
9767  *
9768  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9769  *
9770  * The below multi-dimensional table is used for looking up the target
9771  * exception level given numerous condition criteria.  Specifically, the
9772  * target EL is based on SCR and HCR routing controls as well as the
9773  * currently executing EL and secure state.
9774  *
9775  *    Dimensions:
9776  *    target_el_table[2][2][2][2][2][4]
9777  *                    |  |  |  |  |  +--- Current EL
9778  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9779  *                    |  |  |  +--------- HCR mask override
9780  *                    |  |  +------------ SCR exec state control
9781  *                    |  +--------------- SCR mask override
9782  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9783  *
9784  *    The table values are as such:
9785  *    0-3 = EL0-EL3
9786  *     -1 = Cannot occur
9787  *
9788  * The ARM ARM target EL table includes entries indicating that an "exception
9789  * is not taken".  The two cases where this is applicable are:
9790  *    1) An exception is taken from EL3 but the SCR does not have the exception
9791  *    routed to EL3.
9792  *    2) An exception is taken from EL2 but the HCR does not have the exception
9793  *    routed to EL2.
9794  * In these two cases, the below table contain a target of EL1.  This value is
9795  * returned as it is expected that the consumer of the table data will check
9796  * for "target EL >= current EL" to ensure the exception is not taken.
9797  *
9798  *            SCR     HCR
9799  *         64  EA     AMO                 From
9800  *        BIT IRQ     IMO      Non-secure         Secure
9801  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9802  */
9803 static const int8_t target_el_table[2][2][2][2][2][4] = {
9804     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9805        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9806       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9807        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9808      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9809        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9810       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9811        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9812     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9813        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9814       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9815        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9816      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9817        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9818       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9819        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9820 };
9821 
9822 /*
9823  * Determine the target EL for physical exceptions
9824  */
9825 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9826                                  uint32_t cur_el, bool secure)
9827 {
9828     CPUARMState *env = cpu_env(cs);
9829     bool rw;
9830     bool scr;
9831     bool hcr;
9832     int target_el;
9833     /* Is the highest EL AArch64? */
9834     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9835     uint64_t hcr_el2;
9836 
9837     if (arm_feature(env, ARM_FEATURE_EL3)) {
9838         rw = arm_scr_rw_eff(env);
9839     } else {
9840         /*
9841          * Either EL2 is the highest EL (and so the EL2 register width
9842          * is given by is64); or there is no EL2 or EL3, in which case
9843          * the value of 'rw' does not affect the table lookup anyway.
9844          */
9845         rw = is64;
9846     }
9847 
9848     hcr_el2 = arm_hcr_el2_eff(env);
9849     switch (excp_idx) {
9850     case EXCP_IRQ:
9851     case EXCP_NMI:
9852         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9853         hcr = hcr_el2 & HCR_IMO;
9854         break;
9855     case EXCP_FIQ:
9856         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9857         hcr = hcr_el2 & HCR_FMO;
9858         break;
9859     default:
9860         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9861         hcr = hcr_el2 & HCR_AMO;
9862         break;
9863     };
9864 
9865     /*
9866      * For these purposes, TGE and AMO/IMO/FMO both force the
9867      * interrupt to EL2.  Fold TGE into the bit extracted above.
9868      */
9869     hcr |= (hcr_el2 & HCR_TGE) != 0;
9870 
9871     /* Perform a table-lookup for the target EL given the current state */
9872     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9873 
9874     assert(target_el > 0);
9875 
9876     return target_el;
9877 }
9878 
9879 void arm_log_exception(CPUState *cs)
9880 {
9881     int idx = cs->exception_index;
9882 
9883     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9884         const char *exc = NULL;
9885         static const char * const excnames[] = {
9886             [EXCP_UDEF] = "Undefined Instruction",
9887             [EXCP_SWI] = "SVC",
9888             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9889             [EXCP_DATA_ABORT] = "Data Abort",
9890             [EXCP_IRQ] = "IRQ",
9891             [EXCP_FIQ] = "FIQ",
9892             [EXCP_BKPT] = "Breakpoint",
9893             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9894             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9895             [EXCP_HVC] = "Hypervisor Call",
9896             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9897             [EXCP_SMC] = "Secure Monitor Call",
9898             [EXCP_VIRQ] = "Virtual IRQ",
9899             [EXCP_VFIQ] = "Virtual FIQ",
9900             [EXCP_SEMIHOST] = "Semihosting call",
9901             [EXCP_NOCP] = "v7M NOCP UsageFault",
9902             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9903             [EXCP_STKOF] = "v8M STKOF UsageFault",
9904             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9905             [EXCP_LSERR] = "v8M LSERR UsageFault",
9906             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9907             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9908             [EXCP_VSERR] = "Virtual SERR",
9909             [EXCP_GPC] = "Granule Protection Check",
9910             [EXCP_NMI] = "NMI",
9911             [EXCP_VINMI] = "Virtual IRQ NMI",
9912             [EXCP_VFNMI] = "Virtual FIQ NMI",
9913             [EXCP_MON_TRAP] = "Monitor Trap",
9914         };
9915 
9916         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9917             exc = excnames[idx];
9918         }
9919         if (!exc) {
9920             exc = "unknown";
9921         }
9922         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9923                       idx, exc, cs->cpu_index);
9924     }
9925 }
9926 
9927 /*
9928  * Function used to synchronize QEMU's AArch64 register set with AArch32
9929  * register set.  This is necessary when switching between AArch32 and AArch64
9930  * execution state.
9931  */
9932 void aarch64_sync_32_to_64(CPUARMState *env)
9933 {
9934     int i;
9935     uint32_t mode = env->uncached_cpsr & CPSR_M;
9936 
9937     /* We can blanket copy R[0:7] to X[0:7] */
9938     for (i = 0; i < 8; i++) {
9939         env->xregs[i] = env->regs[i];
9940     }
9941 
9942     /*
9943      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9944      * Otherwise, they come from the banked user regs.
9945      */
9946     if (mode == ARM_CPU_MODE_FIQ) {
9947         for (i = 8; i < 13; i++) {
9948             env->xregs[i] = env->usr_regs[i - 8];
9949         }
9950     } else {
9951         for (i = 8; i < 13; i++) {
9952             env->xregs[i] = env->regs[i];
9953         }
9954     }
9955 
9956     /*
9957      * Registers x13-x23 are the various mode SP and FP registers. Registers
9958      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9959      * from the mode banked register.
9960      */
9961     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9962         env->xregs[13] = env->regs[13];
9963         env->xregs[14] = env->regs[14];
9964     } else {
9965         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9966         /* HYP is an exception in that it is copied from r14 */
9967         if (mode == ARM_CPU_MODE_HYP) {
9968             env->xregs[14] = env->regs[14];
9969         } else {
9970             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9971         }
9972     }
9973 
9974     if (mode == ARM_CPU_MODE_HYP) {
9975         env->xregs[15] = env->regs[13];
9976     } else {
9977         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9978     }
9979 
9980     if (mode == ARM_CPU_MODE_IRQ) {
9981         env->xregs[16] = env->regs[14];
9982         env->xregs[17] = env->regs[13];
9983     } else {
9984         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9985         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9986     }
9987 
9988     if (mode == ARM_CPU_MODE_SVC) {
9989         env->xregs[18] = env->regs[14];
9990         env->xregs[19] = env->regs[13];
9991     } else {
9992         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9993         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9994     }
9995 
9996     if (mode == ARM_CPU_MODE_ABT) {
9997         env->xregs[20] = env->regs[14];
9998         env->xregs[21] = env->regs[13];
9999     } else {
10000         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10001         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10002     }
10003 
10004     if (mode == ARM_CPU_MODE_UND) {
10005         env->xregs[22] = env->regs[14];
10006         env->xregs[23] = env->regs[13];
10007     } else {
10008         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10009         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10010     }
10011 
10012     /*
10013      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10014      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10015      * FIQ bank for r8-r14.
10016      */
10017     if (mode == ARM_CPU_MODE_FIQ) {
10018         for (i = 24; i < 31; i++) {
10019             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10020         }
10021     } else {
10022         for (i = 24; i < 29; i++) {
10023             env->xregs[i] = env->fiq_regs[i - 24];
10024         }
10025         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10026         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10027     }
10028 
10029     env->pc = env->regs[15];
10030 }
10031 
10032 /*
10033  * Function used to synchronize QEMU's AArch32 register set with AArch64
10034  * register set.  This is necessary when switching between AArch32 and AArch64
10035  * execution state.
10036  */
10037 void aarch64_sync_64_to_32(CPUARMState *env)
10038 {
10039     int i;
10040     uint32_t mode = env->uncached_cpsr & CPSR_M;
10041 
10042     /* We can blanket copy X[0:7] to R[0:7] */
10043     for (i = 0; i < 8; i++) {
10044         env->regs[i] = env->xregs[i];
10045     }
10046 
10047     /*
10048      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10049      * Otherwise, we copy x8-x12 into the banked user regs.
10050      */
10051     if (mode == ARM_CPU_MODE_FIQ) {
10052         for (i = 8; i < 13; i++) {
10053             env->usr_regs[i - 8] = env->xregs[i];
10054         }
10055     } else {
10056         for (i = 8; i < 13; i++) {
10057             env->regs[i] = env->xregs[i];
10058         }
10059     }
10060 
10061     /*
10062      * Registers r13 & r14 depend on the current mode.
10063      * If we are in a given mode, we copy the corresponding x registers to r13
10064      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10065      * for the mode.
10066      */
10067     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10068         env->regs[13] = env->xregs[13];
10069         env->regs[14] = env->xregs[14];
10070     } else {
10071         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10072 
10073         /*
10074          * HYP is an exception in that it does not have its own banked r14 but
10075          * shares the USR r14
10076          */
10077         if (mode == ARM_CPU_MODE_HYP) {
10078             env->regs[14] = env->xregs[14];
10079         } else {
10080             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10081         }
10082     }
10083 
10084     if (mode == ARM_CPU_MODE_HYP) {
10085         env->regs[13] = env->xregs[15];
10086     } else {
10087         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10088     }
10089 
10090     if (mode == ARM_CPU_MODE_IRQ) {
10091         env->regs[14] = env->xregs[16];
10092         env->regs[13] = env->xregs[17];
10093     } else {
10094         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10095         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10096     }
10097 
10098     if (mode == ARM_CPU_MODE_SVC) {
10099         env->regs[14] = env->xregs[18];
10100         env->regs[13] = env->xregs[19];
10101     } else {
10102         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10103         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10104     }
10105 
10106     if (mode == ARM_CPU_MODE_ABT) {
10107         env->regs[14] = env->xregs[20];
10108         env->regs[13] = env->xregs[21];
10109     } else {
10110         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10111         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10112     }
10113 
10114     if (mode == ARM_CPU_MODE_UND) {
10115         env->regs[14] = env->xregs[22];
10116         env->regs[13] = env->xregs[23];
10117     } else {
10118         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10119         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10120     }
10121 
10122     /*
10123      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10124      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10125      * FIQ bank for r8-r14.
10126      */
10127     if (mode == ARM_CPU_MODE_FIQ) {
10128         for (i = 24; i < 31; i++) {
10129             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10130         }
10131     } else {
10132         for (i = 24; i < 29; i++) {
10133             env->fiq_regs[i - 24] = env->xregs[i];
10134         }
10135         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10136         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10137     }
10138 
10139     env->regs[15] = env->pc;
10140 }
10141 
10142 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10143                                    uint32_t mask, uint32_t offset,
10144                                    uint32_t newpc)
10145 {
10146     int new_el;
10147 
10148     /* Change the CPU state so as to actually take the exception. */
10149     switch_mode(env, new_mode);
10150 
10151     /*
10152      * For exceptions taken to AArch32 we must clear the SS bit in both
10153      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10154      */
10155     env->pstate &= ~PSTATE_SS;
10156     env->spsr = cpsr_read(env);
10157     /* Clear IT bits.  */
10158     env->condexec_bits = 0;
10159     /* Switch to the new mode, and to the correct instruction set.  */
10160     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10161 
10162     /* This must be after mode switching. */
10163     new_el = arm_current_el(env);
10164 
10165     /* Set new mode endianness */
10166     env->uncached_cpsr &= ~CPSR_E;
10167     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10168         env->uncached_cpsr |= CPSR_E;
10169     }
10170     /* J and IL must always be cleared for exception entry */
10171     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10172     env->daif |= mask;
10173 
10174     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10175         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10176             env->uncached_cpsr |= CPSR_SSBS;
10177         } else {
10178             env->uncached_cpsr &= ~CPSR_SSBS;
10179         }
10180     }
10181 
10182     if (new_mode == ARM_CPU_MODE_HYP) {
10183         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10184         env->elr_el[2] = env->regs[15];
10185     } else {
10186         /* CPSR.PAN is normally preserved preserved unless...  */
10187         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10188             switch (new_el) {
10189             case 3:
10190                 if (!arm_is_secure_below_el3(env)) {
10191                     /* ... the target is EL3, from non-secure state.  */
10192                     env->uncached_cpsr &= ~CPSR_PAN;
10193                     break;
10194                 }
10195                 /* ... the target is EL3, from secure state ... */
10196                 /* fall through */
10197             case 1:
10198                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10199                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10200                     env->uncached_cpsr |= CPSR_PAN;
10201                 }
10202                 break;
10203             }
10204         }
10205         /*
10206          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10207          * and we should just guard the thumb mode on V4
10208          */
10209         if (arm_feature(env, ARM_FEATURE_V4T)) {
10210             env->thumb =
10211                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10212         }
10213         env->regs[14] = env->regs[15] + offset;
10214     }
10215     env->regs[15] = newpc;
10216 
10217     if (tcg_enabled()) {
10218         arm_rebuild_hflags(env);
10219     }
10220 }
10221 
10222 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10223 {
10224     /*
10225      * Handle exception entry to Hyp mode; this is sufficiently
10226      * different to entry to other AArch32 modes that we handle it
10227      * separately here.
10228      *
10229      * The vector table entry used is always the 0x14 Hyp mode entry point,
10230      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10231      * The offset applied to the preferred return address is always zero
10232      * (see DDI0487C.a section G1.12.3).
10233      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10234      */
10235     uint32_t addr, mask;
10236     ARMCPU *cpu = ARM_CPU(cs);
10237     CPUARMState *env = &cpu->env;
10238 
10239     switch (cs->exception_index) {
10240     case EXCP_UDEF:
10241         addr = 0x04;
10242         break;
10243     case EXCP_SWI:
10244         addr = 0x08;
10245         break;
10246     case EXCP_BKPT:
10247         /* Fall through to prefetch abort.  */
10248     case EXCP_PREFETCH_ABORT:
10249         env->cp15.ifar_s = env->exception.vaddress;
10250         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10251                       (uint32_t)env->exception.vaddress);
10252         addr = 0x0c;
10253         break;
10254     case EXCP_DATA_ABORT:
10255         env->cp15.dfar_s = env->exception.vaddress;
10256         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10257                       (uint32_t)env->exception.vaddress);
10258         addr = 0x10;
10259         break;
10260     case EXCP_IRQ:
10261         addr = 0x18;
10262         break;
10263     case EXCP_FIQ:
10264         addr = 0x1c;
10265         break;
10266     case EXCP_HVC:
10267         addr = 0x08;
10268         break;
10269     case EXCP_HYP_TRAP:
10270         addr = 0x14;
10271         break;
10272     default:
10273         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10274     }
10275 
10276     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10277         if (!arm_feature(env, ARM_FEATURE_V8)) {
10278             /*
10279              * QEMU syndrome values are v8-style. v7 has the IL bit
10280              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10281              * If this is a v7 CPU, squash the IL bit in those cases.
10282              */
10283             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10284                 (cs->exception_index == EXCP_DATA_ABORT &&
10285                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10286                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10287                 env->exception.syndrome &= ~ARM_EL_IL;
10288             }
10289         }
10290         env->cp15.esr_el[2] = env->exception.syndrome;
10291     }
10292 
10293     if (arm_current_el(env) != 2 && addr < 0x14) {
10294         addr = 0x14;
10295     }
10296 
10297     mask = 0;
10298     if (!(env->cp15.scr_el3 & SCR_EA)) {
10299         mask |= CPSR_A;
10300     }
10301     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10302         mask |= CPSR_I;
10303     }
10304     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10305         mask |= CPSR_F;
10306     }
10307 
10308     addr += env->cp15.hvbar;
10309 
10310     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10311 }
10312 
10313 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10314 {
10315     ARMCPU *cpu = ARM_CPU(cs);
10316     CPUARMState *env = &cpu->env;
10317     uint32_t addr;
10318     uint32_t mask;
10319     int new_mode;
10320     uint32_t offset;
10321     uint32_t moe;
10322 
10323     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10324     switch (syn_get_ec(env->exception.syndrome)) {
10325     case EC_BREAKPOINT:
10326     case EC_BREAKPOINT_SAME_EL:
10327         moe = 1;
10328         break;
10329     case EC_WATCHPOINT:
10330     case EC_WATCHPOINT_SAME_EL:
10331         moe = 10;
10332         break;
10333     case EC_AA32_BKPT:
10334         moe = 3;
10335         break;
10336     case EC_VECTORCATCH:
10337         moe = 5;
10338         break;
10339     default:
10340         moe = 0;
10341         break;
10342     }
10343 
10344     if (moe) {
10345         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10346     }
10347 
10348     if (env->exception.target_el == 2) {
10349         /* Debug exceptions are reported differently on AArch32 */
10350         switch (syn_get_ec(env->exception.syndrome)) {
10351         case EC_BREAKPOINT:
10352         case EC_BREAKPOINT_SAME_EL:
10353         case EC_AA32_BKPT:
10354         case EC_VECTORCATCH:
10355             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10356                                                      0, 0, 0x22);
10357             break;
10358         case EC_WATCHPOINT:
10359             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10360                                                  EC_DATAABORT);
10361             break;
10362         case EC_WATCHPOINT_SAME_EL:
10363             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10364                                                  EC_DATAABORT_SAME_EL);
10365             break;
10366         }
10367         arm_cpu_do_interrupt_aarch32_hyp(cs);
10368         return;
10369     }
10370 
10371     switch (cs->exception_index) {
10372     case EXCP_UDEF:
10373         new_mode = ARM_CPU_MODE_UND;
10374         addr = 0x04;
10375         mask = CPSR_I;
10376         if (env->thumb) {
10377             offset = 2;
10378         } else {
10379             offset = 4;
10380         }
10381         break;
10382     case EXCP_SWI:
10383         new_mode = ARM_CPU_MODE_SVC;
10384         addr = 0x08;
10385         mask = CPSR_I;
10386         /* The PC already points to the next instruction.  */
10387         offset = 0;
10388         break;
10389     case EXCP_BKPT:
10390         /* Fall through to prefetch abort.  */
10391     case EXCP_PREFETCH_ABORT:
10392         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10393         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10394         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10395                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10396         new_mode = ARM_CPU_MODE_ABT;
10397         addr = 0x0c;
10398         mask = CPSR_A | CPSR_I;
10399         offset = 4;
10400         break;
10401     case EXCP_DATA_ABORT:
10402         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10403         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10404         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10405                       env->exception.fsr,
10406                       (uint32_t)env->exception.vaddress);
10407         new_mode = ARM_CPU_MODE_ABT;
10408         addr = 0x10;
10409         mask = CPSR_A | CPSR_I;
10410         offset = 8;
10411         break;
10412     case EXCP_IRQ:
10413         new_mode = ARM_CPU_MODE_IRQ;
10414         addr = 0x18;
10415         /* Disable IRQ and imprecise data aborts.  */
10416         mask = CPSR_A | CPSR_I;
10417         offset = 4;
10418         if (env->cp15.scr_el3 & SCR_IRQ) {
10419             /* IRQ routed to monitor mode */
10420             new_mode = ARM_CPU_MODE_MON;
10421             mask |= CPSR_F;
10422         }
10423         break;
10424     case EXCP_FIQ:
10425         new_mode = ARM_CPU_MODE_FIQ;
10426         addr = 0x1c;
10427         /* Disable FIQ, IRQ and imprecise data aborts.  */
10428         mask = CPSR_A | CPSR_I | CPSR_F;
10429         if (env->cp15.scr_el3 & SCR_FIQ) {
10430             /* FIQ routed to monitor mode */
10431             new_mode = ARM_CPU_MODE_MON;
10432         }
10433         offset = 4;
10434         break;
10435     case EXCP_VIRQ:
10436         new_mode = ARM_CPU_MODE_IRQ;
10437         addr = 0x18;
10438         /* Disable IRQ and imprecise data aborts.  */
10439         mask = CPSR_A | CPSR_I;
10440         offset = 4;
10441         break;
10442     case EXCP_VFIQ:
10443         new_mode = ARM_CPU_MODE_FIQ;
10444         addr = 0x1c;
10445         /* Disable FIQ, IRQ and imprecise data aborts.  */
10446         mask = CPSR_A | CPSR_I | CPSR_F;
10447         offset = 4;
10448         break;
10449     case EXCP_VSERR:
10450         {
10451             /*
10452              * Note that this is reported as a data abort, but the DFAR
10453              * has an UNKNOWN value.  Construct the SError syndrome from
10454              * AET and ExT fields.
10455              */
10456             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10457 
10458             if (extended_addresses_enabled(env)) {
10459                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10460             } else {
10461                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10462             }
10463             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10464             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10465             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10466                           env->exception.fsr);
10467 
10468             new_mode = ARM_CPU_MODE_ABT;
10469             addr = 0x10;
10470             mask = CPSR_A | CPSR_I;
10471             offset = 8;
10472         }
10473         break;
10474     case EXCP_SMC:
10475         new_mode = ARM_CPU_MODE_MON;
10476         addr = 0x08;
10477         mask = CPSR_A | CPSR_I | CPSR_F;
10478         offset = 0;
10479         break;
10480     case EXCP_MON_TRAP:
10481         new_mode = ARM_CPU_MODE_MON;
10482         addr = 0x04;
10483         mask = CPSR_A | CPSR_I | CPSR_F;
10484         if (env->thumb) {
10485             offset = 2;
10486         } else {
10487             offset = 4;
10488         }
10489         break;
10490     default:
10491         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10492         return; /* Never happens.  Keep compiler happy.  */
10493     }
10494 
10495     if (new_mode == ARM_CPU_MODE_MON) {
10496         addr += env->cp15.mvbar;
10497     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10498         /* High vectors. When enabled, base address cannot be remapped. */
10499         addr += 0xffff0000;
10500     } else {
10501         /*
10502          * ARM v7 architectures provide a vector base address register to remap
10503          * the interrupt vector table.
10504          * This register is only followed in non-monitor mode, and is banked.
10505          * Note: only bits 31:5 are valid.
10506          */
10507         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10508     }
10509 
10510     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10511         env->cp15.scr_el3 &= ~SCR_NS;
10512     }
10513 
10514     take_aarch32_exception(env, new_mode, mask, offset, addr);
10515 }
10516 
10517 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10518 {
10519     /*
10520      * Return the register number of the AArch64 view of the AArch32
10521      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10522      * be that of the AArch32 mode the exception came from.
10523      */
10524     int mode = env->uncached_cpsr & CPSR_M;
10525 
10526     switch (aarch32_reg) {
10527     case 0 ... 7:
10528         return aarch32_reg;
10529     case 8 ... 12:
10530         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10531     case 13:
10532         switch (mode) {
10533         case ARM_CPU_MODE_USR:
10534         case ARM_CPU_MODE_SYS:
10535             return 13;
10536         case ARM_CPU_MODE_HYP:
10537             return 15;
10538         case ARM_CPU_MODE_IRQ:
10539             return 17;
10540         case ARM_CPU_MODE_SVC:
10541             return 19;
10542         case ARM_CPU_MODE_ABT:
10543             return 21;
10544         case ARM_CPU_MODE_UND:
10545             return 23;
10546         case ARM_CPU_MODE_FIQ:
10547             return 29;
10548         default:
10549             g_assert_not_reached();
10550         }
10551     case 14:
10552         switch (mode) {
10553         case ARM_CPU_MODE_USR:
10554         case ARM_CPU_MODE_SYS:
10555         case ARM_CPU_MODE_HYP:
10556             return 14;
10557         case ARM_CPU_MODE_IRQ:
10558             return 16;
10559         case ARM_CPU_MODE_SVC:
10560             return 18;
10561         case ARM_CPU_MODE_ABT:
10562             return 20;
10563         case ARM_CPU_MODE_UND:
10564             return 22;
10565         case ARM_CPU_MODE_FIQ:
10566             return 30;
10567         default:
10568             g_assert_not_reached();
10569         }
10570     case 15:
10571         return 31;
10572     default:
10573         g_assert_not_reached();
10574     }
10575 }
10576 
10577 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10578 {
10579     uint32_t ret = cpsr_read(env);
10580 
10581     /* Move DIT to the correct location for SPSR_ELx */
10582     if (ret & CPSR_DIT) {
10583         ret &= ~CPSR_DIT;
10584         ret |= PSTATE_DIT;
10585     }
10586     /* Merge PSTATE.SS into SPSR_ELx */
10587     ret |= env->pstate & PSTATE_SS;
10588 
10589     return ret;
10590 }
10591 
10592 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10593 {
10594     /* Return true if this syndrome value is a synchronous external abort */
10595     switch (syn_get_ec(syndrome)) {
10596     case EC_INSNABORT:
10597     case EC_INSNABORT_SAME_EL:
10598     case EC_DATAABORT:
10599     case EC_DATAABORT_SAME_EL:
10600         /* Look at fault status code for all the synchronous ext abort cases */
10601         switch (syndrome & 0x3f) {
10602         case 0x10:
10603         case 0x13:
10604         case 0x14:
10605         case 0x15:
10606         case 0x16:
10607         case 0x17:
10608             return true;
10609         default:
10610             return false;
10611         }
10612     default:
10613         return false;
10614     }
10615 }
10616 
10617 /* Handle exception entry to a target EL which is using AArch64 */
10618 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10619 {
10620     ARMCPU *cpu = ARM_CPU(cs);
10621     CPUARMState *env = &cpu->env;
10622     unsigned int new_el = env->exception.target_el;
10623     target_ulong addr = env->cp15.vbar_el[new_el];
10624     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10625     unsigned int old_mode;
10626     unsigned int cur_el = arm_current_el(env);
10627     int rt;
10628 
10629     if (tcg_enabled()) {
10630         /*
10631          * Note that new_el can never be 0.  If cur_el is 0, then
10632          * el0_a64 is is_a64(), else el0_a64 is ignored.
10633          */
10634 #ifdef TARGET_AARCH64
10635         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10636 #endif
10637     }
10638 
10639     if (cur_el < new_el) {
10640         /*
10641          * Entry vector offset depends on whether the implemented EL
10642          * immediately lower than the target level is using AArch32 or AArch64
10643          */
10644         bool is_aa64;
10645         uint64_t hcr;
10646 
10647         switch (new_el) {
10648         case 3:
10649             is_aa64 = arm_scr_rw_eff(env);
10650             break;
10651         case 2:
10652             hcr = arm_hcr_el2_eff(env);
10653             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10654                 is_aa64 = (hcr & HCR_RW) != 0;
10655                 break;
10656             }
10657             /* fall through */
10658         case 1:
10659             is_aa64 = is_a64(env);
10660             break;
10661         default:
10662             g_assert_not_reached();
10663         }
10664 
10665         if (is_aa64) {
10666             addr += 0x400;
10667         } else {
10668             addr += 0x600;
10669         }
10670     } else if (pstate_read(env) & PSTATE_SP) {
10671         addr += 0x200;
10672     }
10673 
10674     switch (cs->exception_index) {
10675     case EXCP_GPC:
10676         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10677                       env->cp15.mfar_el3);
10678         /* fall through */
10679     case EXCP_PREFETCH_ABORT:
10680     case EXCP_DATA_ABORT:
10681         /*
10682          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10683          * to be taken to the SError vector entrypoint.
10684          */
10685         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10686             syndrome_is_sync_extabt(env->exception.syndrome)) {
10687             addr += 0x180;
10688         }
10689         env->cp15.far_el[new_el] = env->exception.vaddress;
10690         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10691                       env->cp15.far_el[new_el]);
10692         /* fall through */
10693     case EXCP_BKPT:
10694     case EXCP_UDEF:
10695     case EXCP_SWI:
10696     case EXCP_HVC:
10697     case EXCP_HYP_TRAP:
10698     case EXCP_SMC:
10699         switch (syn_get_ec(env->exception.syndrome)) {
10700         case EC_ADVSIMDFPACCESSTRAP:
10701             /*
10702              * QEMU internal FP/SIMD syndromes from AArch32 include the
10703              * TA and coproc fields which are only exposed if the exception
10704              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10705              * AArch64 format syndrome.
10706              */
10707             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10708             break;
10709         case EC_CP14RTTRAP:
10710         case EC_CP15RTTRAP:
10711         case EC_CP14DTTRAP:
10712             /*
10713              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10714              * the raw register field from the insn; when taking this to
10715              * AArch64 we must convert it to the AArch64 view of the register
10716              * number. Notice that we read a 4-bit AArch32 register number and
10717              * write back a 5-bit AArch64 one.
10718              */
10719             rt = extract32(env->exception.syndrome, 5, 4);
10720             rt = aarch64_regnum(env, rt);
10721             env->exception.syndrome = deposit32(env->exception.syndrome,
10722                                                 5, 5, rt);
10723             break;
10724         case EC_CP15RRTTRAP:
10725         case EC_CP14RRTTRAP:
10726             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10727             rt = extract32(env->exception.syndrome, 5, 4);
10728             rt = aarch64_regnum(env, rt);
10729             env->exception.syndrome = deposit32(env->exception.syndrome,
10730                                                 5, 5, rt);
10731             rt = extract32(env->exception.syndrome, 10, 4);
10732             rt = aarch64_regnum(env, rt);
10733             env->exception.syndrome = deposit32(env->exception.syndrome,
10734                                                 10, 5, rt);
10735             break;
10736         }
10737         env->cp15.esr_el[new_el] = env->exception.syndrome;
10738         break;
10739     case EXCP_IRQ:
10740     case EXCP_VIRQ:
10741     case EXCP_NMI:
10742     case EXCP_VINMI:
10743         addr += 0x80;
10744         break;
10745     case EXCP_FIQ:
10746     case EXCP_VFIQ:
10747     case EXCP_VFNMI:
10748         addr += 0x100;
10749         break;
10750     case EXCP_VSERR:
10751         addr += 0x180;
10752         /* Construct the SError syndrome from IDS and ISS fields. */
10753         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10754         env->cp15.esr_el[new_el] = env->exception.syndrome;
10755         break;
10756     default:
10757         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10758     }
10759 
10760     if (is_a64(env)) {
10761         old_mode = pstate_read(env);
10762         aarch64_save_sp(env, arm_current_el(env));
10763         env->elr_el[new_el] = env->pc;
10764 
10765         if (cur_el == 1 && new_el == 1) {
10766             uint64_t hcr = arm_hcr_el2_eff(env);
10767             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10768                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10769                 /*
10770                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10771                  * by setting M[3:2] to 0b10.
10772                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10773                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10774                  */
10775                 old_mode = deposit32(old_mode, 2, 2, 2);
10776             }
10777         }
10778     } else {
10779         old_mode = cpsr_read_for_spsr_elx(env);
10780         env->elr_el[new_el] = env->regs[15];
10781 
10782         aarch64_sync_32_to_64(env);
10783 
10784         env->condexec_bits = 0;
10785     }
10786     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10787 
10788     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10789     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10790                   env->elr_el[new_el]);
10791 
10792     if (cpu_isar_feature(aa64_pan, cpu)) {
10793         /* The value of PSTATE.PAN is normally preserved, except when ... */
10794         new_mode |= old_mode & PSTATE_PAN;
10795         switch (new_el) {
10796         case 2:
10797             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10798             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10799                 != (HCR_E2H | HCR_TGE)) {
10800                 break;
10801             }
10802             /* fall through */
10803         case 1:
10804             /* ... the target is EL1 ... */
10805             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10806             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10807                 new_mode |= PSTATE_PAN;
10808             }
10809             break;
10810         }
10811     }
10812     if (cpu_isar_feature(aa64_mte, cpu)) {
10813         new_mode |= PSTATE_TCO;
10814     }
10815 
10816     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10817         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10818             new_mode |= PSTATE_SSBS;
10819         } else {
10820             new_mode &= ~PSTATE_SSBS;
10821         }
10822     }
10823 
10824     if (cpu_isar_feature(aa64_nmi, cpu)) {
10825         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10826             new_mode |= PSTATE_ALLINT;
10827         } else {
10828             new_mode &= ~PSTATE_ALLINT;
10829         }
10830     }
10831 
10832     pstate_write(env, PSTATE_DAIF | new_mode);
10833     env->aarch64 = true;
10834     aarch64_restore_sp(env, new_el);
10835 
10836     if (tcg_enabled()) {
10837         helper_rebuild_hflags_a64(env, new_el);
10838     }
10839 
10840     env->pc = addr;
10841 
10842     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10843                   new_el, env->pc, pstate_read(env));
10844 }
10845 
10846 /*
10847  * Do semihosting call and set the appropriate return value. All the
10848  * permission and validity checks have been done at translate time.
10849  *
10850  * We only see semihosting exceptions in TCG only as they are not
10851  * trapped to the hypervisor in KVM.
10852  */
10853 #ifdef CONFIG_TCG
10854 static void tcg_handle_semihosting(CPUState *cs)
10855 {
10856     ARMCPU *cpu = ARM_CPU(cs);
10857     CPUARMState *env = &cpu->env;
10858 
10859     if (is_a64(env)) {
10860         qemu_log_mask(CPU_LOG_INT,
10861                       "...handling as semihosting call 0x%" PRIx64 "\n",
10862                       env->xregs[0]);
10863         do_common_semihosting(cs);
10864         env->pc += 4;
10865     } else {
10866         qemu_log_mask(CPU_LOG_INT,
10867                       "...handling as semihosting call 0x%x\n",
10868                       env->regs[0]);
10869         do_common_semihosting(cs);
10870         env->regs[15] += env->thumb ? 2 : 4;
10871     }
10872 }
10873 #endif
10874 
10875 /*
10876  * Handle a CPU exception for A and R profile CPUs.
10877  * Do any appropriate logging, handle PSCI calls, and then hand off
10878  * to the AArch64-entry or AArch32-entry function depending on the
10879  * target exception level's register width.
10880  *
10881  * Note: this is used for both TCG (as the do_interrupt tcg op),
10882  *       and KVM to re-inject guest debug exceptions, and to
10883  *       inject a Synchronous-External-Abort.
10884  */
10885 void arm_cpu_do_interrupt(CPUState *cs)
10886 {
10887     ARMCPU *cpu = ARM_CPU(cs);
10888     CPUARMState *env = &cpu->env;
10889     unsigned int new_el = env->exception.target_el;
10890 
10891     assert(!arm_feature(env, ARM_FEATURE_M));
10892 
10893     arm_log_exception(cs);
10894     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10895                   new_el);
10896     if (qemu_loglevel_mask(CPU_LOG_INT)
10897         && !excp_is_internal(cs->exception_index)) {
10898         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10899                       syn_get_ec(env->exception.syndrome),
10900                       env->exception.syndrome);
10901     }
10902 
10903     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10904         arm_handle_psci_call(cpu);
10905         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10906         return;
10907     }
10908 
10909     /*
10910      * Semihosting semantics depend on the register width of the code
10911      * that caused the exception, not the target exception level, so
10912      * must be handled here.
10913      */
10914 #ifdef CONFIG_TCG
10915     if (cs->exception_index == EXCP_SEMIHOST) {
10916         tcg_handle_semihosting(cs);
10917         return;
10918     }
10919 #endif
10920 
10921     /*
10922      * Hooks may change global state so BQL should be held, also the
10923      * BQL needs to be held for any modification of
10924      * cs->interrupt_request.
10925      */
10926     g_assert(bql_locked());
10927 
10928     arm_call_pre_el_change_hook(cpu);
10929 
10930     assert(!excp_is_internal(cs->exception_index));
10931     if (arm_el_is_aa64(env, new_el)) {
10932         arm_cpu_do_interrupt_aarch64(cs);
10933     } else {
10934         arm_cpu_do_interrupt_aarch32(cs);
10935     }
10936 
10937     arm_call_el_change_hook(cpu);
10938 
10939     if (!kvm_enabled()) {
10940         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10941     }
10942 }
10943 #endif /* !CONFIG_USER_ONLY */
10944 
10945 uint64_t arm_sctlr(CPUARMState *env, int el)
10946 {
10947     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10948     if (el == 0) {
10949         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10950         switch (mmu_idx) {
10951         case ARMMMUIdx_E20_0:
10952             el = 2;
10953             break;
10954         case ARMMMUIdx_E30_0:
10955             el = 3;
10956             break;
10957         default:
10958             el = 1;
10959             break;
10960         }
10961     }
10962     return env->cp15.sctlr_el[el];
10963 }
10964 
10965 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10966 {
10967     if (regime_has_2_ranges(mmu_idx)) {
10968         return extract64(tcr, 37, 2);
10969     } else if (regime_is_stage2(mmu_idx)) {
10970         return 0; /* VTCR_EL2 */
10971     } else {
10972         /* Replicate the single TBI bit so we always have 2 bits.  */
10973         return extract32(tcr, 20, 1) * 3;
10974     }
10975 }
10976 
10977 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10978 {
10979     if (regime_has_2_ranges(mmu_idx)) {
10980         return extract64(tcr, 51, 2);
10981     } else if (regime_is_stage2(mmu_idx)) {
10982         return 0; /* VTCR_EL2 */
10983     } else {
10984         /* Replicate the single TBID bit so we always have 2 bits.  */
10985         return extract32(tcr, 29, 1) * 3;
10986     }
10987 }
10988 
10989 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10990 {
10991     if (regime_has_2_ranges(mmu_idx)) {
10992         return extract64(tcr, 57, 2);
10993     } else {
10994         /* Replicate the single TCMA bit so we always have 2 bits.  */
10995         return extract32(tcr, 30, 1) * 3;
10996     }
10997 }
10998 
10999 static ARMGranuleSize tg0_to_gran_size(int tg)
11000 {
11001     switch (tg) {
11002     case 0:
11003         return Gran4K;
11004     case 1:
11005         return Gran64K;
11006     case 2:
11007         return Gran16K;
11008     default:
11009         return GranInvalid;
11010     }
11011 }
11012 
11013 static ARMGranuleSize tg1_to_gran_size(int tg)
11014 {
11015     switch (tg) {
11016     case 1:
11017         return Gran16K;
11018     case 2:
11019         return Gran4K;
11020     case 3:
11021         return Gran64K;
11022     default:
11023         return GranInvalid;
11024     }
11025 }
11026 
11027 static inline bool have4k(ARMCPU *cpu, bool stage2)
11028 {
11029     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11030         : cpu_isar_feature(aa64_tgran4, cpu);
11031 }
11032 
11033 static inline bool have16k(ARMCPU *cpu, bool stage2)
11034 {
11035     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11036         : cpu_isar_feature(aa64_tgran16, cpu);
11037 }
11038 
11039 static inline bool have64k(ARMCPU *cpu, bool stage2)
11040 {
11041     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11042         : cpu_isar_feature(aa64_tgran64, cpu);
11043 }
11044 
11045 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11046                                          bool stage2)
11047 {
11048     switch (gran) {
11049     case Gran4K:
11050         if (have4k(cpu, stage2)) {
11051             return gran;
11052         }
11053         break;
11054     case Gran16K:
11055         if (have16k(cpu, stage2)) {
11056             return gran;
11057         }
11058         break;
11059     case Gran64K:
11060         if (have64k(cpu, stage2)) {
11061             return gran;
11062         }
11063         break;
11064     case GranInvalid:
11065         break;
11066     }
11067     /*
11068      * If the guest selects a granule size that isn't implemented,
11069      * the architecture requires that we behave as if it selected one
11070      * that is (with an IMPDEF choice of which one to pick). We choose
11071      * to implement the smallest supported granule size.
11072      */
11073     if (have4k(cpu, stage2)) {
11074         return Gran4K;
11075     }
11076     if (have16k(cpu, stage2)) {
11077         return Gran16K;
11078     }
11079     assert(have64k(cpu, stage2));
11080     return Gran64K;
11081 }
11082 
11083 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11084                                    ARMMMUIdx mmu_idx, bool data,
11085                                    bool el1_is_aa32)
11086 {
11087     uint64_t tcr = regime_tcr(env, mmu_idx);
11088     bool epd, hpd, tsz_oob, ds, ha, hd;
11089     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11090     ARMGranuleSize gran;
11091     ARMCPU *cpu = env_archcpu(env);
11092     bool stage2 = regime_is_stage2(mmu_idx);
11093 
11094     if (!regime_has_2_ranges(mmu_idx)) {
11095         select = 0;
11096         tsz = extract32(tcr, 0, 6);
11097         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11098         if (stage2) {
11099             /* VTCR_EL2 */
11100             hpd = false;
11101         } else {
11102             hpd = extract32(tcr, 24, 1);
11103         }
11104         epd = false;
11105         sh = extract32(tcr, 12, 2);
11106         ps = extract32(tcr, 16, 3);
11107         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11108         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11109         ds = extract64(tcr, 32, 1);
11110     } else {
11111         bool e0pd;
11112 
11113         /*
11114          * Bit 55 is always between the two regions, and is canonical for
11115          * determining if address tagging is enabled.
11116          */
11117         select = extract64(va, 55, 1);
11118         if (!select) {
11119             tsz = extract32(tcr, 0, 6);
11120             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11121             epd = extract32(tcr, 7, 1);
11122             sh = extract32(tcr, 12, 2);
11123             hpd = extract64(tcr, 41, 1);
11124             e0pd = extract64(tcr, 55, 1);
11125         } else {
11126             tsz = extract32(tcr, 16, 6);
11127             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11128             epd = extract32(tcr, 23, 1);
11129             sh = extract32(tcr, 28, 2);
11130             hpd = extract64(tcr, 42, 1);
11131             e0pd = extract64(tcr, 56, 1);
11132         }
11133         ps = extract64(tcr, 32, 3);
11134         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11135         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11136         ds = extract64(tcr, 59, 1);
11137 
11138         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11139             regime_is_user(env, mmu_idx)) {
11140             epd = true;
11141         }
11142     }
11143 
11144     gran = sanitize_gran_size(cpu, gran, stage2);
11145 
11146     if (cpu_isar_feature(aa64_st, cpu)) {
11147         max_tsz = 48 - (gran == Gran64K);
11148     } else {
11149         max_tsz = 39;
11150     }
11151 
11152     /*
11153      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11154      * adjust the effective value of DS, as documented.
11155      */
11156     min_tsz = 16;
11157     if (gran == Gran64K) {
11158         if (cpu_isar_feature(aa64_lva, cpu)) {
11159             min_tsz = 12;
11160         }
11161         ds = false;
11162     } else if (ds) {
11163         if (regime_is_stage2(mmu_idx)) {
11164             if (gran == Gran16K) {
11165                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11166             } else {
11167                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11168             }
11169         } else {
11170             if (gran == Gran16K) {
11171                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11172             } else {
11173                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11174             }
11175         }
11176         if (ds) {
11177             min_tsz = 12;
11178         }
11179     }
11180 
11181     if (stage2 && el1_is_aa32) {
11182         /*
11183          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11184          * are loosened: a configured IPA of 40 bits is permitted even if
11185          * the implemented PA is less than that (and so a 40 bit IPA would
11186          * fault for an AArch64 EL1). See R_DTLMN.
11187          */
11188         min_tsz = MIN(min_tsz, 24);
11189     }
11190 
11191     if (tsz > max_tsz) {
11192         tsz = max_tsz;
11193         tsz_oob = true;
11194     } else if (tsz < min_tsz) {
11195         tsz = min_tsz;
11196         tsz_oob = true;
11197     } else {
11198         tsz_oob = false;
11199     }
11200 
11201     /* Present TBI as a composite with TBID.  */
11202     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11203     if (!data) {
11204         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11205     }
11206     tbi = (tbi >> select) & 1;
11207 
11208     return (ARMVAParameters) {
11209         .tsz = tsz,
11210         .ps = ps,
11211         .sh = sh,
11212         .select = select,
11213         .tbi = tbi,
11214         .epd = epd,
11215         .hpd = hpd,
11216         .tsz_oob = tsz_oob,
11217         .ds = ds,
11218         .ha = ha,
11219         .hd = ha && hd,
11220         .gran = gran,
11221     };
11222 }
11223 
11224 
11225 /*
11226  * Return the exception level to which FP-disabled exceptions should
11227  * be taken, or 0 if FP is enabled.
11228  */
11229 int fp_exception_el(CPUARMState *env, int cur_el)
11230 {
11231 #ifndef CONFIG_USER_ONLY
11232     uint64_t hcr_el2;
11233 
11234     /*
11235      * CPACR and the CPTR registers don't exist before v6, so FP is
11236      * always accessible
11237      */
11238     if (!arm_feature(env, ARM_FEATURE_V6)) {
11239         return 0;
11240     }
11241 
11242     if (arm_feature(env, ARM_FEATURE_M)) {
11243         /* CPACR can cause a NOCP UsageFault taken to current security state */
11244         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11245             return 1;
11246         }
11247 
11248         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11249             if (!extract32(env->v7m.nsacr, 10, 1)) {
11250                 /* FP insns cause a NOCP UsageFault taken to Secure */
11251                 return 3;
11252             }
11253         }
11254 
11255         return 0;
11256     }
11257 
11258     hcr_el2 = arm_hcr_el2_eff(env);
11259 
11260     /*
11261      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11262      * 0, 2 : trap EL0 and EL1/PL1 accesses
11263      * 1    : trap only EL0 accesses
11264      * 3    : trap no accesses
11265      * This register is ignored if E2H+TGE are both set.
11266      */
11267     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11268         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11269 
11270         switch (fpen) {
11271         case 1:
11272             if (cur_el != 0) {
11273                 break;
11274             }
11275             /* fall through */
11276         case 0:
11277         case 2:
11278             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11279             if (!arm_el_is_aa64(env, 3)
11280                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11281                 return 3;
11282             }
11283             if (cur_el <= 1) {
11284                 return 1;
11285             }
11286             break;
11287         }
11288     }
11289 
11290     /*
11291      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11292      * to control non-secure access to the FPU. It doesn't have any
11293      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11294      */
11295     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11296          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11297         if (!extract32(env->cp15.nsacr, 10, 1)) {
11298             /* FP insns act as UNDEF */
11299             return cur_el == 2 ? 2 : 1;
11300         }
11301     }
11302 
11303     /*
11304      * CPTR_EL2 is present in v7VE or v8, and changes format
11305      * with HCR_EL2.E2H (regardless of TGE).
11306      */
11307     if (cur_el <= 2) {
11308         if (hcr_el2 & HCR_E2H) {
11309             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11310             case 1:
11311                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11312                     break;
11313                 }
11314                 /* fall through */
11315             case 0:
11316             case 2:
11317                 return 2;
11318             }
11319         } else if (arm_is_el2_enabled(env)) {
11320             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11321                 return 2;
11322             }
11323         }
11324     }
11325 
11326     /* CPTR_EL3 : present in v8 */
11327     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11328         /* Trap all FP ops to EL3 */
11329         return 3;
11330     }
11331 #endif
11332     return 0;
11333 }
11334 
11335 /* Return the exception level we're running at if this is our mmu_idx */
11336 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11337 {
11338     if (mmu_idx & ARM_MMU_IDX_M) {
11339         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11340     }
11341 
11342     switch (mmu_idx) {
11343     case ARMMMUIdx_E10_0:
11344     case ARMMMUIdx_E20_0:
11345     case ARMMMUIdx_E30_0:
11346         return 0;
11347     case ARMMMUIdx_E10_1:
11348     case ARMMMUIdx_E10_1_PAN:
11349         return 1;
11350     case ARMMMUIdx_E2:
11351     case ARMMMUIdx_E20_2:
11352     case ARMMMUIdx_E20_2_PAN:
11353         return 2;
11354     case ARMMMUIdx_E3:
11355     case ARMMMUIdx_E30_3_PAN:
11356         return 3;
11357     default:
11358         g_assert_not_reached();
11359     }
11360 }
11361 
11362 #ifndef CONFIG_TCG
11363 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11364 {
11365     g_assert_not_reached();
11366 }
11367 #endif
11368 
11369 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11370 {
11371     ARMMMUIdx idx;
11372     uint64_t hcr;
11373 
11374     if (arm_feature(env, ARM_FEATURE_M)) {
11375         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11376     }
11377 
11378     /* See ARM pseudo-function ELIsInHost.  */
11379     switch (el) {
11380     case 0:
11381         hcr = arm_hcr_el2_eff(env);
11382         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11383             idx = ARMMMUIdx_E20_0;
11384         } else if (arm_is_secure_below_el3(env) &&
11385                    !arm_el_is_aa64(env, 3)) {
11386             idx = ARMMMUIdx_E30_0;
11387         } else {
11388             idx = ARMMMUIdx_E10_0;
11389         }
11390         break;
11391     case 1:
11392         if (arm_pan_enabled(env)) {
11393             idx = ARMMMUIdx_E10_1_PAN;
11394         } else {
11395             idx = ARMMMUIdx_E10_1;
11396         }
11397         break;
11398     case 2:
11399         /* Note that TGE does not apply at EL2.  */
11400         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11401             if (arm_pan_enabled(env)) {
11402                 idx = ARMMMUIdx_E20_2_PAN;
11403             } else {
11404                 idx = ARMMMUIdx_E20_2;
11405             }
11406         } else {
11407             idx = ARMMMUIdx_E2;
11408         }
11409         break;
11410     case 3:
11411         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11412             return ARMMMUIdx_E30_3_PAN;
11413         }
11414         return ARMMMUIdx_E3;
11415     default:
11416         g_assert_not_reached();
11417     }
11418 
11419     return idx;
11420 }
11421 
11422 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11423 {
11424     return arm_mmu_idx_el(env, arm_current_el(env));
11425 }
11426 
11427 static bool mve_no_pred(CPUARMState *env)
11428 {
11429     /*
11430      * Return true if there is definitely no predication of MVE
11431      * instructions by VPR or LTPSIZE. (Returning false even if there
11432      * isn't any predication is OK; generated code will just be
11433      * a little worse.)
11434      * If the CPU does not implement MVE then this TB flag is always 0.
11435      *
11436      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11437      * logic in gen_update_fp_context() needs to be updated to match.
11438      *
11439      * We do not include the effect of the ECI bits here -- they are
11440      * tracked in other TB flags. This simplifies the logic for
11441      * "when did we emit code that changes the MVE_NO_PRED TB flag
11442      * and thus need to end the TB?".
11443      */
11444     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11445         return false;
11446     }
11447     if (env->v7m.vpr) {
11448         return false;
11449     }
11450     if (env->v7m.ltpsize < 4) {
11451         return false;
11452     }
11453     return true;
11454 }
11455 
11456 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11457                           uint64_t *cs_base, uint32_t *pflags)
11458 {
11459     CPUARMTBFlags flags;
11460 
11461     assert_hflags_rebuild_correctly(env);
11462     flags = env->hflags;
11463 
11464     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11465         *pc = env->pc;
11466         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11467             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11468         }
11469     } else {
11470         *pc = env->regs[15];
11471 
11472         if (arm_feature(env, ARM_FEATURE_M)) {
11473             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11474                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11475                 != env->v7m.secure) {
11476                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11477             }
11478 
11479             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11480                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11481                  (env->v7m.secure &&
11482                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11483                 /*
11484                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11485                  * active FP context; we must create a new FP context before
11486                  * executing any FP insn.
11487                  */
11488                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11489             }
11490 
11491             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11492             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11493                 DP_TBFLAG_M32(flags, LSPACT, 1);
11494             }
11495 
11496             if (mve_no_pred(env)) {
11497                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11498             }
11499         } else {
11500             /*
11501              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11502              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11503              */
11504             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11505                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11506             } else {
11507                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11508                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11509             }
11510             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11511                 DP_TBFLAG_A32(flags, VFPEN, 1);
11512             }
11513         }
11514 
11515         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11516         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11517     }
11518 
11519     /*
11520      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11521      * states defined in the ARM ARM for software singlestep:
11522      *  SS_ACTIVE   PSTATE.SS   State
11523      *     0            x       Inactive (the TB flag for SS is always 0)
11524      *     1            0       Active-pending
11525      *     1            1       Active-not-pending
11526      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11527      */
11528     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11529         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11530     }
11531 
11532     *pflags = flags.flags;
11533     *cs_base = flags.flags2;
11534 }
11535 
11536 #ifdef TARGET_AARCH64
11537 /*
11538  * The manual says that when SVE is enabled and VQ is widened the
11539  * implementation is allowed to zero the previously inaccessible
11540  * portion of the registers.  The corollary to that is that when
11541  * SVE is enabled and VQ is narrowed we are also allowed to zero
11542  * the now inaccessible portion of the registers.
11543  *
11544  * The intent of this is that no predicate bit beyond VQ is ever set.
11545  * Which means that some operations on predicate registers themselves
11546  * may operate on full uint64_t or even unrolled across the maximum
11547  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11548  * may well be cheaper than conditionals to restrict the operation
11549  * to the relevant portion of a uint16_t[16].
11550  */
11551 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11552 {
11553     int i, j;
11554     uint64_t pmask;
11555 
11556     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11557     assert(vq <= env_archcpu(env)->sve_max_vq);
11558 
11559     /* Zap the high bits of the zregs.  */
11560     for (i = 0; i < 32; i++) {
11561         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11562     }
11563 
11564     /* Zap the high bits of the pregs and ffr.  */
11565     pmask = 0;
11566     if (vq & 3) {
11567         pmask = ~(-1ULL << (16 * (vq & 3)));
11568     }
11569     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11570         for (i = 0; i < 17; ++i) {
11571             env->vfp.pregs[i].p[j] &= pmask;
11572         }
11573         pmask = 0;
11574     }
11575 }
11576 
11577 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11578 {
11579     int exc_el;
11580 
11581     if (sm) {
11582         exc_el = sme_exception_el(env, el);
11583     } else {
11584         exc_el = sve_exception_el(env, el);
11585     }
11586     if (exc_el) {
11587         return 0; /* disabled */
11588     }
11589     return sve_vqm1_for_el_sm(env, el, sm);
11590 }
11591 
11592 /*
11593  * Notice a change in SVE vector size when changing EL.
11594  */
11595 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11596                            int new_el, bool el0_a64)
11597 {
11598     ARMCPU *cpu = env_archcpu(env);
11599     int old_len, new_len;
11600     bool old_a64, new_a64, sm;
11601 
11602     /* Nothing to do if no SVE.  */
11603     if (!cpu_isar_feature(aa64_sve, cpu)) {
11604         return;
11605     }
11606 
11607     /* Nothing to do if FP is disabled in either EL.  */
11608     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11609         return;
11610     }
11611 
11612     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11613     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11614 
11615     /*
11616      * Both AArch64.TakeException and AArch64.ExceptionReturn
11617      * invoke ResetSVEState when taking an exception from, or
11618      * returning to, AArch32 state when PSTATE.SM is enabled.
11619      */
11620     sm = FIELD_EX64(env->svcr, SVCR, SM);
11621     if (old_a64 != new_a64 && sm) {
11622         arm_reset_sve_state(env);
11623         return;
11624     }
11625 
11626     /*
11627      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11628      * at ELx, or not available because the EL is in AArch32 state, then
11629      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11630      * has an effective value of 0".
11631      *
11632      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11633      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11634      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11635      * we already have the correct register contents when encountering the
11636      * vq0->vq0 transition between EL0->EL1.
11637      */
11638     old_len = new_len = 0;
11639     if (old_a64) {
11640         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11641     }
11642     if (new_a64) {
11643         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11644     }
11645 
11646     /* When changing vector length, clear inaccessible state.  */
11647     if (new_len < old_len) {
11648 #ifdef TARGET_AARCH64
11649         aarch64_sve_narrow_vq(env, new_len + 1);
11650 #endif
11651     }
11652 }
11653 #endif
11654 
11655 #ifndef CONFIG_USER_ONLY
11656 ARMSecuritySpace arm_security_space(CPUARMState *env)
11657 {
11658     if (arm_feature(env, ARM_FEATURE_M)) {
11659         return arm_secure_to_space(env->v7m.secure);
11660     }
11661 
11662     /*
11663      * If EL3 is not supported then the secure state is implementation
11664      * defined, in which case QEMU defaults to non-secure.
11665      */
11666     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11667         return ARMSS_NonSecure;
11668     }
11669 
11670     /* Check for AArch64 EL3 or AArch32 Mon. */
11671     if (is_a64(env)) {
11672         if (extract32(env->pstate, 2, 2) == 3) {
11673             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11674                 return ARMSS_Root;
11675             } else {
11676                 return ARMSS_Secure;
11677             }
11678         }
11679     } else {
11680         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11681             return ARMSS_Secure;
11682         }
11683     }
11684 
11685     return arm_security_space_below_el3(env);
11686 }
11687 
11688 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11689 {
11690     assert(!arm_feature(env, ARM_FEATURE_M));
11691 
11692     /*
11693      * If EL3 is not supported then the secure state is implementation
11694      * defined, in which case QEMU defaults to non-secure.
11695      */
11696     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11697         return ARMSS_NonSecure;
11698     }
11699 
11700     /*
11701      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11702      * Ignoring NSE when !NS retains consistency without having to
11703      * modify other predicates.
11704      */
11705     if (!(env->cp15.scr_el3 & SCR_NS)) {
11706         return ARMSS_Secure;
11707     } else if (env->cp15.scr_el3 & SCR_NSE) {
11708         return ARMSS_Realm;
11709     } else {
11710         return ARMSS_NonSecure;
11711     }
11712 }
11713 #endif /* !CONFIG_USER_ONLY */
11714