xref: /qemu/target/arm/helper.c (revision 6c1ae457a17a9462fb89ef1f30ad7da5266bfea6)
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 "system/kvm.h"
28 #include "system/tcg.h"
29 #include "qapi/error.h"
30 #include "qemu/guest-random.h"
31 #ifdef CONFIG_TCG
32 #include "semihosting/common-semi.h"
33 #endif
34 #include "cpregs.h"
35 #include "target/arm/gtimer.h"
36 
37 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
38 
39 static void switch_mode(CPUARMState *env, int mode);
40 
41 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
42 {
43     assert(ri->fieldoffset);
44     if (cpreg_field_is_64bit(ri)) {
45         return CPREG_FIELD64(env, ri);
46     } else {
47         return CPREG_FIELD32(env, ri);
48     }
49 }
50 
51 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
52 {
53     assert(ri->fieldoffset);
54     if (cpreg_field_is_64bit(ri)) {
55         CPREG_FIELD64(env, ri) = value;
56     } else {
57         CPREG_FIELD32(env, ri) = value;
58     }
59 }
60 
61 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
62 {
63     return (char *)env + ri->fieldoffset;
64 }
65 
66 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
67 {
68     /* Raw read of a coprocessor register (as needed for migration, etc). */
69     if (ri->type & ARM_CP_CONST) {
70         return ri->resetvalue;
71     } else if (ri->raw_readfn) {
72         return ri->raw_readfn(env, ri);
73     } else if (ri->readfn) {
74         return ri->readfn(env, ri);
75     } else {
76         return raw_read(env, ri);
77     }
78 }
79 
80 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
81                              uint64_t v)
82 {
83     /*
84      * Raw write of a coprocessor register (as needed for migration, etc).
85      * Note that constant registers are treated as write-ignored; the
86      * caller should check for success by whether a readback gives the
87      * value written.
88      */
89     if (ri->type & ARM_CP_CONST) {
90         return;
91     } else if (ri->raw_writefn) {
92         ri->raw_writefn(env, ri, v);
93     } else if (ri->writefn) {
94         ri->writefn(env, ri, v);
95     } else {
96         raw_write(env, ri, v);
97     }
98 }
99 
100 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
101 {
102    /*
103     * Return true if the regdef would cause an assertion if you called
104     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
105     * program bug for it not to have the NO_RAW flag).
106     * NB that returning false here doesn't necessarily mean that calling
107     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
108     * read/write access functions which are safe for raw use" from "has
109     * read/write access functions which have side effects but has forgotten
110     * to provide raw access functions".
111     * The tests here line up with the conditions in read/write_raw_cp_reg()
112     * and assertions in raw_read()/raw_write().
113     */
114     if ((ri->type & ARM_CP_CONST) ||
115         ri->fieldoffset ||
116         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
117         return false;
118     }
119     return true;
120 }
121 
122 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
123 {
124     /* Write the coprocessor state from cpu->env to the (index,value) list. */
125     int i;
126     bool ok = true;
127 
128     for (i = 0; i < cpu->cpreg_array_len; i++) {
129         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
130         const ARMCPRegInfo *ri;
131         uint64_t newval;
132 
133         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
134         if (!ri) {
135             ok = false;
136             continue;
137         }
138         if (ri->type & ARM_CP_NO_RAW) {
139             continue;
140         }
141 
142         newval = read_raw_cp_reg(&cpu->env, ri);
143         if (kvm_sync) {
144             /*
145              * Only sync if the previous list->cpustate sync succeeded.
146              * Rather than tracking the success/failure state for every
147              * item in the list, we just recheck "does the raw write we must
148              * have made in write_list_to_cpustate() read back OK" here.
149              */
150             uint64_t oldval = cpu->cpreg_values[i];
151 
152             if (oldval == newval) {
153                 continue;
154             }
155 
156             write_raw_cp_reg(&cpu->env, ri, oldval);
157             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
158                 continue;
159             }
160 
161             write_raw_cp_reg(&cpu->env, ri, newval);
162         }
163         cpu->cpreg_values[i] = newval;
164     }
165     return ok;
166 }
167 
168 bool write_list_to_cpustate(ARMCPU *cpu)
169 {
170     int i;
171     bool ok = true;
172 
173     for (i = 0; i < cpu->cpreg_array_len; i++) {
174         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
175         uint64_t v = cpu->cpreg_values[i];
176         const ARMCPRegInfo *ri;
177 
178         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
179         if (!ri) {
180             ok = false;
181             continue;
182         }
183         if (ri->type & ARM_CP_NO_RAW) {
184             continue;
185         }
186         /*
187          * Write value and confirm it reads back as written
188          * (to catch read-only registers and partially read-only
189          * registers where the incoming migration value doesn't match)
190          */
191         write_raw_cp_reg(&cpu->env, ri, v);
192         if (read_raw_cp_reg(&cpu->env, ri) != v) {
193             ok = false;
194         }
195     }
196     return ok;
197 }
198 
199 static void add_cpreg_to_list(gpointer key, gpointer opaque)
200 {
201     ARMCPU *cpu = opaque;
202     uint32_t regidx = (uintptr_t)key;
203     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
204 
205     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
206         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207         /* The value array need not be initialized at this point */
208         cpu->cpreg_array_len++;
209     }
210 }
211 
212 static void count_cpreg(gpointer key, gpointer opaque)
213 {
214     ARMCPU *cpu = opaque;
215     const ARMCPRegInfo *ri;
216 
217     ri = g_hash_table_lookup(cpu->cp_regs, key);
218 
219     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
220         cpu->cpreg_array_len++;
221     }
222 }
223 
224 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
225 {
226     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
228 
229     if (aidx > bidx) {
230         return 1;
231     }
232     if (aidx < bidx) {
233         return -1;
234     }
235     return 0;
236 }
237 
238 void init_cpreg_list(ARMCPU *cpu)
239 {
240     /*
241      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
242      * Note that we require cpreg_tuples[] to be sorted by key ID.
243      */
244     GList *keys;
245     int arraylen;
246 
247     keys = g_hash_table_get_keys(cpu->cp_regs);
248     keys = g_list_sort(keys, cpreg_key_compare);
249 
250     cpu->cpreg_array_len = 0;
251 
252     g_list_foreach(keys, count_cpreg, cpu);
253 
254     arraylen = cpu->cpreg_array_len;
255     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
256     cpu->cpreg_values = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
259     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
260     cpu->cpreg_array_len = 0;
261 
262     g_list_foreach(keys, add_cpreg_to_list, cpu);
263 
264     assert(cpu->cpreg_array_len == arraylen);
265 
266     g_list_free(keys);
267 }
268 
269 static bool arm_pan_enabled(CPUARMState *env)
270 {
271     if (is_a64(env)) {
272         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
273             return false;
274         }
275         return env->pstate & PSTATE_PAN;
276     } else {
277         return env->uncached_cpsr & CPSR_PAN;
278     }
279 }
280 
281 /*
282  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
283  */
284 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
285                                         const ARMCPRegInfo *ri,
286                                         bool isread)
287 {
288     if (!is_a64(env) && arm_current_el(env) == 3 &&
289         arm_is_secure_below_el3(env)) {
290         return CP_ACCESS_UNDEFINED;
291     }
292     return CP_ACCESS_OK;
293 }
294 
295 /*
296  * Some secure-only AArch32 registers trap to EL3 if used from
297  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
298  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
299  * We assume that the .access field is set to PL1_RW.
300  */
301 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
302                                             const ARMCPRegInfo *ri,
303                                             bool isread)
304 {
305     if (arm_current_el(env) == 3) {
306         return CP_ACCESS_OK;
307     }
308     if (arm_is_secure_below_el3(env)) {
309         if (env->cp15.scr_el3 & SCR_EEL2) {
310             return CP_ACCESS_TRAP_EL2;
311         }
312         return CP_ACCESS_TRAP_EL3;
313     }
314     /* This will be EL1 NS and EL2 NS, which just UNDEF */
315     return CP_ACCESS_UNDEFINED;
316 }
317 
318 /*
319  * Check for traps to performance monitor registers, which are controlled
320  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
321  */
322 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
323                                  bool isread)
324 {
325     int el = arm_current_el(env);
326     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
327 
328     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
329         return CP_ACCESS_TRAP_EL2;
330     }
331     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
332         return CP_ACCESS_TRAP_EL3;
333     }
334     return CP_ACCESS_OK;
335 }
336 
337 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
338 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
339                                bool isread)
340 {
341     if (arm_current_el(env) == 1) {
342         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
343         if (arm_hcr_el2_eff(env) & trap) {
344             return CP_ACCESS_TRAP_EL2;
345         }
346     }
347     return CP_ACCESS_OK;
348 }
349 
350 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
351 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
352                                  bool isread)
353 {
354     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
355         return CP_ACCESS_TRAP_EL2;
356     }
357     return CP_ACCESS_OK;
358 }
359 
360 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
361 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
362                                   bool isread)
363 {
364     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
365         return CP_ACCESS_TRAP_EL2;
366     }
367     return CP_ACCESS_OK;
368 }
369 
370 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
371 {
372     ARMCPU *cpu = env_archcpu(env);
373 
374     raw_write(env, ri, value);
375     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
376 }
377 
378 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
379 {
380     ARMCPU *cpu = env_archcpu(env);
381 
382     if (raw_read(env, ri) != value) {
383         /*
384          * Unlike real hardware the qemu TLB uses virtual addresses,
385          * not modified virtual addresses, so this causes a TLB flush.
386          */
387         tlb_flush(CPU(cpu));
388         raw_write(env, ri, value);
389     }
390 }
391 
392 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
393                              uint64_t value)
394 {
395     ARMCPU *cpu = env_archcpu(env);
396 
397     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
398         && !extended_addresses_enabled(env)) {
399         /*
400          * For VMSA (when not using the LPAE long descriptor page table
401          * format) this register includes the ASID, so do a TLB flush.
402          * For PMSA it is purely a process ID and no action is needed.
403          */
404         tlb_flush(CPU(cpu));
405     }
406     raw_write(env, ri, value);
407 }
408 
409 int alle1_tlbmask(CPUARMState *env)
410 {
411     /*
412      * Note that the 'ALL' scope must invalidate both stage 1 and
413      * stage 2 translations, whereas most other scopes only invalidate
414      * stage 1 translations.
415      *
416      * For AArch32 this is only used for TLBIALLNSNH and VTTBR
417      * writes, so only needs to apply to NS PL1&0, not S PL1&0.
418      */
419     return (ARMMMUIdxBit_E10_1 |
420             ARMMMUIdxBit_E10_1_PAN |
421             ARMMMUIdxBit_E10_0 |
422             ARMMMUIdxBit_Stage2 |
423             ARMMMUIdxBit_Stage2_S);
424 }
425 
426 static const ARMCPRegInfo cp_reginfo[] = {
427     /*
428      * Define the secure and non-secure FCSE identifier CP registers
429      * separately because there is no secure bank in V8 (no _EL3).  This allows
430      * the secure register to be properly reset and migrated. There is also no
431      * v8 EL1 version of the register so the non-secure instance stands alone.
432      */
433     { .name = "FCSEIDR",
434       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
435       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
436       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
437       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
438     { .name = "FCSEIDR_S",
439       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
440       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
441       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
442       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
443     /*
444      * Define the secure and non-secure context identifier CP registers
445      * separately because there is no secure bank in V8 (no _EL3).  This allows
446      * the secure register to be properly reset and migrated.  In the
447      * non-secure case, the 32-bit register will have reset and migration
448      * disabled during registration as it is handled by the 64-bit instance.
449      */
450     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
451       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
452       .access = PL1_RW, .accessfn = access_tvm_trvm,
453       .fgt = FGT_CONTEXTIDR_EL1,
454       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
455       .secure = ARM_CP_SECSTATE_NS,
456       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
457       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
458     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
459       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
460       .access = PL1_RW, .accessfn = access_tvm_trvm,
461       .secure = ARM_CP_SECSTATE_S,
462       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
463       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
464 };
465 
466 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
467     /*
468      * NB: Some of these registers exist in v8 but with more precise
469      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
470      */
471     /* MMU Domain access control / MPU write buffer control */
472     { .name = "DACR",
473       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
474       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
475       .writefn = dacr_write, .raw_writefn = raw_write,
476       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
477                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
478     /*
479      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
480      * For v6 and v5, these mappings are overly broad.
481      */
482     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
483       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
484     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
485       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
486     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
487       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
488     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
489       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
490     /* Cache maintenance ops; some of this space may be overridden later. */
491     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
492       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
493       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
494 };
495 
496 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
497     /*
498      * Not all pre-v6 cores implemented this WFI, so this is slightly
499      * over-broad.
500      */
501     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
502       .access = PL1_W, .type = ARM_CP_WFI },
503 };
504 
505 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
506     /*
507      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
508      * is UNPREDICTABLE; we choose to NOP as most implementations do).
509      */
510     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
511       .access = PL1_W, .type = ARM_CP_WFI },
512     /*
513      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
514      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
515      * OMAPCP will override this space.
516      */
517     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
518       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
519       .resetvalue = 0 },
520     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
521       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
522       .resetvalue = 0 },
523     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
524     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
525       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
526       .resetvalue = 0 },
527     /*
528      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
529      * implementing it as RAZ means the "debug architecture version" bits
530      * will read as a reserved value, which should cause Linux to not try
531      * to use the debug hardware.
532      */
533     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
534       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
535     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
536       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
537     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
538       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
539 };
540 
541 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
542                         uint64_t value)
543 {
544     uint32_t mask = 0;
545 
546     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
547     if (!arm_feature(env, ARM_FEATURE_V8)) {
548         /*
549          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
550          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
551          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
552          */
553         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
554             /* VFP coprocessor: cp10 & cp11 [23:20] */
555             mask |= R_CPACR_ASEDIS_MASK |
556                     R_CPACR_D32DIS_MASK |
557                     R_CPACR_CP11_MASK |
558                     R_CPACR_CP10_MASK;
559 
560             if (!arm_feature(env, ARM_FEATURE_NEON)) {
561                 /* ASEDIS [31] bit is RAO/WI */
562                 value |= R_CPACR_ASEDIS_MASK;
563             }
564 
565             /*
566              * VFPv3 and upwards with NEON implement 32 double precision
567              * registers (D0-D31).
568              */
569             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
570                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
571                 value |= R_CPACR_D32DIS_MASK;
572             }
573         }
574         value &= mask;
575     }
576 
577     /*
578      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
579      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
580      */
581     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
582         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
583         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
584         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
585     }
586 
587     env->cp15.cpacr_el1 = value;
588 }
589 
590 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
591 {
592     /*
593      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
594      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
595      */
596     uint64_t value = env->cp15.cpacr_el1;
597 
598     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
599         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
600         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
601     }
602     return value;
603 }
604 
605 
606 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
607 {
608     /*
609      * Call cpacr_write() so that we reset with the correct RAO bits set
610      * for our CPU features.
611      */
612     cpacr_write(env, ri, 0);
613 }
614 
615 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
616                                    bool isread)
617 {
618     if (arm_feature(env, ARM_FEATURE_V8)) {
619         /* Check if CPACR accesses are to be trapped to EL2 */
620         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
621             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
622             return CP_ACCESS_TRAP_EL2;
623         /* Check if CPACR accesses are to be trapped to EL3 */
624         } else if (arm_current_el(env) < 3 &&
625                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
626             return CP_ACCESS_TRAP_EL3;
627         }
628     }
629 
630     return CP_ACCESS_OK;
631 }
632 
633 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
634                                   bool isread)
635 {
636     /* Check if CPTR accesses are set to trap to EL3 */
637     if (arm_current_el(env) == 2 &&
638         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
639         return CP_ACCESS_TRAP_EL3;
640     }
641 
642     return CP_ACCESS_OK;
643 }
644 
645 static const ARMCPRegInfo v6_cp_reginfo[] = {
646     /* prefetch by MVA in v6, NOP in v7 */
647     { .name = "MVA_prefetch",
648       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
649       .access = PL1_W, .type = ARM_CP_NOP },
650     /*
651      * We need to break the TB after ISB to execute self-modifying code
652      * correctly and also to take any pending interrupts immediately.
653      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
654      */
655     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
656       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
657     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
658       .access = PL0_W, .type = ARM_CP_NOP },
659     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
660       .access = PL0_W, .type = ARM_CP_NOP },
661     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
662       .access = PL1_RW, .accessfn = access_tvm_trvm,
663       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
664                              offsetof(CPUARMState, cp15.ifar_ns) },
665       .resetvalue = 0, },
666     /*
667      * Watchpoint Fault Address Register : should actually only be present
668      * for 1136, 1176, 11MPCore.
669      */
670     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
671       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
672     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
673       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
674       .fgt = FGT_CPACR_EL1,
675       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
676       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
677       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
678 };
679 
680 typedef struct pm_event {
681     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
682     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
683     bool (*supported)(CPUARMState *);
684     /*
685      * Retrieve the current count of the underlying event. The programmed
686      * counters hold a difference from the return value from this function
687      */
688     uint64_t (*get_count)(CPUARMState *);
689     /*
690      * Return how many nanoseconds it will take (at a minimum) for count events
691      * to occur. A negative value indicates the counter will never overflow, or
692      * that the counter has otherwise arranged for the overflow bit to be set
693      * and the PMU interrupt to be raised on overflow.
694      */
695     int64_t (*ns_per_count)(uint64_t);
696 } pm_event;
697 
698 static bool event_always_supported(CPUARMState *env)
699 {
700     return true;
701 }
702 
703 static uint64_t swinc_get_count(CPUARMState *env)
704 {
705     /*
706      * SW_INCR events are written directly to the pmevcntr's by writes to
707      * PMSWINC, so there is no underlying count maintained by the PMU itself
708      */
709     return 0;
710 }
711 
712 static int64_t swinc_ns_per(uint64_t ignored)
713 {
714     return -1;
715 }
716 
717 /*
718  * Return the underlying cycle count for the PMU cycle counters. If we're in
719  * usermode, simply return 0.
720  */
721 static uint64_t cycles_get_count(CPUARMState *env)
722 {
723 #ifndef CONFIG_USER_ONLY
724     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
725                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
726 #else
727     return cpu_get_host_ticks();
728 #endif
729 }
730 
731 #ifndef CONFIG_USER_ONLY
732 static int64_t cycles_ns_per(uint64_t cycles)
733 {
734     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
735 }
736 
737 static bool instructions_supported(CPUARMState *env)
738 {
739     /* Precise instruction counting */
740     return icount_enabled() == ICOUNT_PRECISE;
741 }
742 
743 static uint64_t instructions_get_count(CPUARMState *env)
744 {
745     assert(icount_enabled() == ICOUNT_PRECISE);
746     return (uint64_t)icount_get_raw();
747 }
748 
749 static int64_t instructions_ns_per(uint64_t icount)
750 {
751     assert(icount_enabled() == ICOUNT_PRECISE);
752     return icount_to_ns((int64_t)icount);
753 }
754 #endif
755 
756 static bool pmuv3p1_events_supported(CPUARMState *env)
757 {
758     /* For events which are supported in any v8.1 PMU */
759     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
760 }
761 
762 static bool pmuv3p4_events_supported(CPUARMState *env)
763 {
764     /* For events which are supported in any v8.1 PMU */
765     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
766 }
767 
768 static uint64_t zero_event_get_count(CPUARMState *env)
769 {
770     /* For events which on QEMU never fire, so their count is always zero */
771     return 0;
772 }
773 
774 static int64_t zero_event_ns_per(uint64_t cycles)
775 {
776     /* An event which never fires can never overflow */
777     return -1;
778 }
779 
780 static const pm_event pm_events[] = {
781     { .number = 0x000, /* SW_INCR */
782       .supported = event_always_supported,
783       .get_count = swinc_get_count,
784       .ns_per_count = swinc_ns_per,
785     },
786 #ifndef CONFIG_USER_ONLY
787     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
788       .supported = instructions_supported,
789       .get_count = instructions_get_count,
790       .ns_per_count = instructions_ns_per,
791     },
792     { .number = 0x011, /* CPU_CYCLES, Cycle */
793       .supported = event_always_supported,
794       .get_count = cycles_get_count,
795       .ns_per_count = cycles_ns_per,
796     },
797 #endif
798     { .number = 0x023, /* STALL_FRONTEND */
799       .supported = pmuv3p1_events_supported,
800       .get_count = zero_event_get_count,
801       .ns_per_count = zero_event_ns_per,
802     },
803     { .number = 0x024, /* STALL_BACKEND */
804       .supported = pmuv3p1_events_supported,
805       .get_count = zero_event_get_count,
806       .ns_per_count = zero_event_ns_per,
807     },
808     { .number = 0x03c, /* STALL */
809       .supported = pmuv3p4_events_supported,
810       .get_count = zero_event_get_count,
811       .ns_per_count = zero_event_ns_per,
812     },
813 };
814 
815 /*
816  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
817  * events (i.e. the statistical profiling extension), this implementation
818  * should first be updated to something sparse instead of the current
819  * supported_event_map[] array.
820  */
821 #define MAX_EVENT_ID 0x3c
822 #define UNSUPPORTED_EVENT UINT16_MAX
823 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
824 
825 /*
826  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
827  * of ARM event numbers to indices in our pm_events array.
828  *
829  * Note: Events in the 0x40XX range are not currently supported.
830  */
831 void pmu_init(ARMCPU *cpu)
832 {
833     unsigned int i;
834 
835     /*
836      * Empty supported_event_map and cpu->pmceid[01] before adding supported
837      * events to them
838      */
839     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
840         supported_event_map[i] = UNSUPPORTED_EVENT;
841     }
842     cpu->pmceid0 = 0;
843     cpu->pmceid1 = 0;
844 
845     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
846         const pm_event *cnt = &pm_events[i];
847         assert(cnt->number <= MAX_EVENT_ID);
848         /* We do not currently support events in the 0x40xx range */
849         assert(cnt->number <= 0x3f);
850 
851         if (cnt->supported(&cpu->env)) {
852             supported_event_map[cnt->number] = i;
853             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
854             if (cnt->number & 0x20) {
855                 cpu->pmceid1 |= event_mask;
856             } else {
857                 cpu->pmceid0 |= event_mask;
858             }
859         }
860     }
861 }
862 
863 /*
864  * Check at runtime whether a PMU event is supported for the current machine
865  */
866 static bool event_supported(uint16_t number)
867 {
868     if (number > MAX_EVENT_ID) {
869         return false;
870     }
871     return supported_event_map[number] != UNSUPPORTED_EVENT;
872 }
873 
874 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
875                                    bool isread)
876 {
877     /*
878      * Performance monitor registers user accessibility is controlled
879      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
880      * trapping to EL2 or EL3 for other accesses.
881      */
882     int el = arm_current_el(env);
883     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
884 
885     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
886         return CP_ACCESS_TRAP_EL1;
887     }
888     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
889         return CP_ACCESS_TRAP_EL2;
890     }
891     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
892         return CP_ACCESS_TRAP_EL3;
893     }
894 
895     return CP_ACCESS_OK;
896 }
897 
898 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
899                                            const ARMCPRegInfo *ri,
900                                            bool isread)
901 {
902     /* ER: event counter read trap control */
903     if (arm_feature(env, ARM_FEATURE_V8)
904         && arm_current_el(env) == 0
905         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
906         && isread) {
907         return CP_ACCESS_OK;
908     }
909 
910     return pmreg_access(env, ri, isread);
911 }
912 
913 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
914                                          const ARMCPRegInfo *ri,
915                                          bool isread)
916 {
917     /* SW: software increment write trap control */
918     if (arm_feature(env, ARM_FEATURE_V8)
919         && arm_current_el(env) == 0
920         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
921         && !isread) {
922         return CP_ACCESS_OK;
923     }
924 
925     return pmreg_access(env, ri, isread);
926 }
927 
928 static CPAccessResult pmreg_access_selr(CPUARMState *env,
929                                         const ARMCPRegInfo *ri,
930                                         bool isread)
931 {
932     /* ER: event counter read trap control */
933     if (arm_feature(env, ARM_FEATURE_V8)
934         && arm_current_el(env) == 0
935         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
936         return CP_ACCESS_OK;
937     }
938 
939     return pmreg_access(env, ri, isread);
940 }
941 
942 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
943                                          const ARMCPRegInfo *ri,
944                                          bool isread)
945 {
946     /* CR: cycle counter read trap control */
947     if (arm_feature(env, ARM_FEATURE_V8)
948         && arm_current_el(env) == 0
949         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
950         && isread) {
951         return CP_ACCESS_OK;
952     }
953 
954     return pmreg_access(env, ri, isread);
955 }
956 
957 /*
958  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
959  * We use these to decide whether we need to wrap a write to MDCR_EL2
960  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
961  */
962 #define MDCR_EL2_PMU_ENABLE_BITS \
963     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
964 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
965 
966 /*
967  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
968  * the current EL, security state, and register configuration.
969  */
970 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
971 {
972     uint64_t filter;
973     bool e, p, u, nsk, nsu, nsh, m;
974     bool enabled, prohibited = false, filtered;
975     bool secure = arm_is_secure(env);
976     int el = arm_current_el(env);
977     uint64_t mdcr_el2;
978     uint8_t hpmn;
979 
980     /*
981      * We might be called for M-profile cores where MDCR_EL2 doesn't
982      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
983      * must be before we read that value.
984      */
985     if (!arm_feature(env, ARM_FEATURE_PMU)) {
986         return false;
987     }
988 
989     mdcr_el2 = arm_mdcr_el2_eff(env);
990     hpmn = mdcr_el2 & MDCR_HPMN;
991 
992     if (!arm_feature(env, ARM_FEATURE_EL2) ||
993             (counter < hpmn || counter == 31)) {
994         e = env->cp15.c9_pmcr & PMCRE;
995     } else {
996         e = mdcr_el2 & MDCR_HPME;
997     }
998     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
999 
1000     /* Is event counting prohibited? */
1001     if (el == 2 && (counter < hpmn || counter == 31)) {
1002         prohibited = mdcr_el2 & MDCR_HPMD;
1003     }
1004     if (secure) {
1005         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1006     }
1007 
1008     if (counter == 31) {
1009         /*
1010          * The cycle counter defaults to running. PMCR.DP says "disable
1011          * the cycle counter when event counting is prohibited".
1012          * Some MDCR bits disable the cycle counter specifically.
1013          */
1014         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1015         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1016             if (secure) {
1017                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1018             }
1019             if (el == 2) {
1020                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1021             }
1022         }
1023     }
1024 
1025     if (counter == 31) {
1026         filter = env->cp15.pmccfiltr_el0;
1027     } else {
1028         filter = env->cp15.c14_pmevtyper[counter];
1029     }
1030 
1031     p   = filter & PMXEVTYPER_P;
1032     u   = filter & PMXEVTYPER_U;
1033     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1034     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1035     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1036     m   = arm_el_is_aa64(env, 1) &&
1037               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1038 
1039     if (el == 0) {
1040         filtered = secure ? u : u != nsu;
1041     } else if (el == 1) {
1042         filtered = secure ? p : p != nsk;
1043     } else if (el == 2) {
1044         filtered = !nsh;
1045     } else { /* EL3 */
1046         filtered = m != p;
1047     }
1048 
1049     if (counter != 31) {
1050         /*
1051          * If not checking PMCCNTR, ensure the counter is setup to an event we
1052          * support
1053          */
1054         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1055         if (!event_supported(event)) {
1056             return false;
1057         }
1058     }
1059 
1060     return enabled && !prohibited && !filtered;
1061 }
1062 
1063 static void pmu_update_irq(CPUARMState *env)
1064 {
1065     ARMCPU *cpu = env_archcpu(env);
1066     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1067             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1068 }
1069 
1070 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1071 {
1072     /*
1073      * Return true if the clock divider is enabled and the cycle counter
1074      * is supposed to tick only once every 64 clock cycles. This is
1075      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1076      * (64-bit) cycle counter PMCR.D has no effect.
1077      */
1078     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1079 }
1080 
1081 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1082 {
1083     /* Return true if the specified event counter is configured to be 64 bit */
1084 
1085     /* This isn't intended to be used with the cycle counter */
1086     assert(counter < 31);
1087 
1088     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1089         return false;
1090     }
1091 
1092     if (arm_feature(env, ARM_FEATURE_EL2)) {
1093         /*
1094          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1095          * current security state, so we don't use arm_mdcr_el2_eff() here.
1096          */
1097         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1098         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1099 
1100         if (counter >= hpmn) {
1101             return hlp;
1102         }
1103     }
1104     return env->cp15.c9_pmcr & PMCRLP;
1105 }
1106 
1107 /*
1108  * Ensure c15_ccnt is the guest-visible count so that operations such as
1109  * enabling/disabling the counter or filtering, modifying the count itself,
1110  * etc. can be done logically. This is essentially a no-op if the counter is
1111  * not enabled at the time of the call.
1112  */
1113 static void pmccntr_op_start(CPUARMState *env)
1114 {
1115     uint64_t cycles = cycles_get_count(env);
1116 
1117     if (pmu_counter_enabled(env, 31)) {
1118         uint64_t eff_cycles = cycles;
1119         if (pmccntr_clockdiv_enabled(env)) {
1120             eff_cycles /= 64;
1121         }
1122 
1123         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1124 
1125         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1126                                  1ull << 63 : 1ull << 31;
1127         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1128             env->cp15.c9_pmovsr |= (1ULL << 31);
1129             pmu_update_irq(env);
1130         }
1131 
1132         env->cp15.c15_ccnt = new_pmccntr;
1133     }
1134     env->cp15.c15_ccnt_delta = cycles;
1135 }
1136 
1137 /*
1138  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1139  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1140  * pmccntr_op_start.
1141  */
1142 static void pmccntr_op_finish(CPUARMState *env)
1143 {
1144     if (pmu_counter_enabled(env, 31)) {
1145 #ifndef CONFIG_USER_ONLY
1146         /* Calculate when the counter will next overflow */
1147         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1148         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1149             remaining_cycles = (uint32_t)remaining_cycles;
1150         }
1151         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1152 
1153         if (overflow_in > 0) {
1154             int64_t overflow_at;
1155 
1156             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1157                                  overflow_in, &overflow_at)) {
1158                 ARMCPU *cpu = env_archcpu(env);
1159                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1160             }
1161         }
1162 #endif
1163 
1164         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1165         if (pmccntr_clockdiv_enabled(env)) {
1166             prev_cycles /= 64;
1167         }
1168         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1169     }
1170 }
1171 
1172 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1173 {
1174 
1175     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1176     uint64_t count = 0;
1177     if (event_supported(event)) {
1178         uint16_t event_idx = supported_event_map[event];
1179         count = pm_events[event_idx].get_count(env);
1180     }
1181 
1182     if (pmu_counter_enabled(env, counter)) {
1183         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1184         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1185             1ULL << 63 : 1ULL << 31;
1186 
1187         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1188             env->cp15.c9_pmovsr |= (1 << counter);
1189             pmu_update_irq(env);
1190         }
1191         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1192     }
1193     env->cp15.c14_pmevcntr_delta[counter] = count;
1194 }
1195 
1196 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1197 {
1198     if (pmu_counter_enabled(env, counter)) {
1199 #ifndef CONFIG_USER_ONLY
1200         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1201         uint16_t event_idx = supported_event_map[event];
1202         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1203         int64_t overflow_in;
1204 
1205         if (!pmevcntr_is_64_bit(env, counter)) {
1206             delta = (uint32_t)delta;
1207         }
1208         overflow_in = pm_events[event_idx].ns_per_count(delta);
1209 
1210         if (overflow_in > 0) {
1211             int64_t overflow_at;
1212 
1213             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1214                                  overflow_in, &overflow_at)) {
1215                 ARMCPU *cpu = env_archcpu(env);
1216                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1217             }
1218         }
1219 #endif
1220 
1221         env->cp15.c14_pmevcntr_delta[counter] -=
1222             env->cp15.c14_pmevcntr[counter];
1223     }
1224 }
1225 
1226 void pmu_op_start(CPUARMState *env)
1227 {
1228     unsigned int i;
1229     pmccntr_op_start(env);
1230     for (i = 0; i < pmu_num_counters(env); i++) {
1231         pmevcntr_op_start(env, i);
1232     }
1233 }
1234 
1235 void pmu_op_finish(CPUARMState *env)
1236 {
1237     unsigned int i;
1238     pmccntr_op_finish(env);
1239     for (i = 0; i < pmu_num_counters(env); i++) {
1240         pmevcntr_op_finish(env, i);
1241     }
1242 }
1243 
1244 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1245 {
1246     pmu_op_start(&cpu->env);
1247 }
1248 
1249 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1250 {
1251     pmu_op_finish(&cpu->env);
1252 }
1253 
1254 void arm_pmu_timer_cb(void *opaque)
1255 {
1256     ARMCPU *cpu = opaque;
1257 
1258     /*
1259      * Update all the counter values based on the current underlying counts,
1260      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1261      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1262      * counter may expire.
1263      */
1264     pmu_op_start(&cpu->env);
1265     pmu_op_finish(&cpu->env);
1266 }
1267 
1268 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1269                        uint64_t value)
1270 {
1271     pmu_op_start(env);
1272 
1273     if (value & PMCRC) {
1274         /* The counter has been reset */
1275         env->cp15.c15_ccnt = 0;
1276     }
1277 
1278     if (value & PMCRP) {
1279         unsigned int i;
1280         for (i = 0; i < pmu_num_counters(env); i++) {
1281             env->cp15.c14_pmevcntr[i] = 0;
1282         }
1283     }
1284 
1285     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1286     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1287 
1288     pmu_op_finish(env);
1289 }
1290 
1291 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1292 {
1293     uint64_t pmcr = env->cp15.c9_pmcr;
1294 
1295     /*
1296      * If EL2 is implemented and enabled for the current security state, reads
1297      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1298      */
1299     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1300         pmcr &= ~PMCRN_MASK;
1301         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1302     }
1303 
1304     return pmcr;
1305 }
1306 
1307 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1308                           uint64_t value)
1309 {
1310     unsigned int i;
1311     uint64_t overflow_mask, new_pmswinc;
1312 
1313     for (i = 0; i < pmu_num_counters(env); i++) {
1314         /* Increment a counter's count iff: */
1315         if ((value & (1 << i)) && /* counter's bit is set */
1316                 /* counter is enabled and not filtered */
1317                 pmu_counter_enabled(env, i) &&
1318                 /* counter is SW_INCR */
1319                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1320             pmevcntr_op_start(env, i);
1321 
1322             /*
1323              * Detect if this write causes an overflow since we can't predict
1324              * PMSWINC overflows like we can for other events
1325              */
1326             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1327 
1328             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1329                 1ULL << 63 : 1ULL << 31;
1330 
1331             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1332                 env->cp15.c9_pmovsr |= (1 << i);
1333                 pmu_update_irq(env);
1334             }
1335 
1336             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1337 
1338             pmevcntr_op_finish(env, i);
1339         }
1340     }
1341 }
1342 
1343 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1344 {
1345     uint64_t ret;
1346     pmccntr_op_start(env);
1347     ret = env->cp15.c15_ccnt;
1348     pmccntr_op_finish(env);
1349     return ret;
1350 }
1351 
1352 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1353                          uint64_t value)
1354 {
1355     /*
1356      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1357      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1358      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1359      * accessed.
1360      */
1361     env->cp15.c9_pmselr = value & 0x1f;
1362 }
1363 
1364 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1365                         uint64_t value)
1366 {
1367     pmccntr_op_start(env);
1368     env->cp15.c15_ccnt = value;
1369     pmccntr_op_finish(env);
1370 }
1371 
1372 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1373                             uint64_t value)
1374 {
1375     uint64_t cur_val = pmccntr_read(env, NULL);
1376 
1377     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1378 }
1379 
1380 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1381                             uint64_t value)
1382 {
1383     pmccntr_op_start(env);
1384     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1385     pmccntr_op_finish(env);
1386 }
1387 
1388 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1389                             uint64_t value)
1390 {
1391     pmccntr_op_start(env);
1392     /* M is not accessible from AArch32 */
1393     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1394         (value & PMCCFILTR);
1395     pmccntr_op_finish(env);
1396 }
1397 
1398 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1399 {
1400     /* M is not visible in AArch32 */
1401     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1402 }
1403 
1404 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405                             uint64_t value)
1406 {
1407     pmu_op_start(env);
1408     value &= pmu_counter_mask(env);
1409     env->cp15.c9_pmcnten |= value;
1410     pmu_op_finish(env);
1411 }
1412 
1413 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1414                              uint64_t value)
1415 {
1416     pmu_op_start(env);
1417     value &= pmu_counter_mask(env);
1418     env->cp15.c9_pmcnten &= ~value;
1419     pmu_op_finish(env);
1420 }
1421 
1422 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1423                          uint64_t value)
1424 {
1425     value &= pmu_counter_mask(env);
1426     env->cp15.c9_pmovsr &= ~value;
1427     pmu_update_irq(env);
1428 }
1429 
1430 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1431                          uint64_t value)
1432 {
1433     value &= pmu_counter_mask(env);
1434     env->cp15.c9_pmovsr |= value;
1435     pmu_update_irq(env);
1436 }
1437 
1438 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1439                              uint64_t value, const uint8_t counter)
1440 {
1441     if (counter == 31) {
1442         pmccfiltr_write(env, ri, value);
1443     } else if (counter < pmu_num_counters(env)) {
1444         pmevcntr_op_start(env, counter);
1445 
1446         /*
1447          * If this counter's event type is changing, store the current
1448          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1449          * pmevcntr_op_finish has the correct baseline when it converts back to
1450          * a delta.
1451          */
1452         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1453             PMXEVTYPER_EVTCOUNT;
1454         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1455         if (old_event != new_event) {
1456             uint64_t count = 0;
1457             if (event_supported(new_event)) {
1458                 uint16_t event_idx = supported_event_map[new_event];
1459                 count = pm_events[event_idx].get_count(env);
1460             }
1461             env->cp15.c14_pmevcntr_delta[counter] = count;
1462         }
1463 
1464         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1465         pmevcntr_op_finish(env, counter);
1466     }
1467     /*
1468      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1469      * PMSELR value is equal to or greater than the number of implemented
1470      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1471      */
1472 }
1473 
1474 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1475                                const uint8_t counter)
1476 {
1477     if (counter == 31) {
1478         return env->cp15.pmccfiltr_el0;
1479     } else if (counter < pmu_num_counters(env)) {
1480         return env->cp15.c14_pmevtyper[counter];
1481     } else {
1482       /*
1483        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1484        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1485        */
1486         return 0;
1487     }
1488 }
1489 
1490 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1491                               uint64_t value)
1492 {
1493     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1494     pmevtyper_write(env, ri, value, counter);
1495 }
1496 
1497 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1498                                uint64_t value)
1499 {
1500     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1501     env->cp15.c14_pmevtyper[counter] = value;
1502 
1503     /*
1504      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1505      * pmu_op_finish calls when loading saved state for a migration. Because
1506      * we're potentially updating the type of event here, the value written to
1507      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1508      * different counter type. Therefore, we need to set this value to the
1509      * current count for the counter type we're writing so that pmu_op_finish
1510      * has the correct count for its calculation.
1511      */
1512     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1513     if (event_supported(event)) {
1514         uint16_t event_idx = supported_event_map[event];
1515         env->cp15.c14_pmevcntr_delta[counter] =
1516             pm_events[event_idx].get_count(env);
1517     }
1518 }
1519 
1520 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1521 {
1522     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1523     return pmevtyper_read(env, ri, counter);
1524 }
1525 
1526 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1527                              uint64_t value)
1528 {
1529     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1530 }
1531 
1532 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1533 {
1534     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1535 }
1536 
1537 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1538                              uint64_t value, uint8_t counter)
1539 {
1540     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1541         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1542         value &= MAKE_64BIT_MASK(0, 32);
1543     }
1544     if (counter < pmu_num_counters(env)) {
1545         pmevcntr_op_start(env, counter);
1546         env->cp15.c14_pmevcntr[counter] = value;
1547         pmevcntr_op_finish(env, counter);
1548     }
1549     /*
1550      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1551      * are CONSTRAINED UNPREDICTABLE.
1552      */
1553 }
1554 
1555 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1556                               uint8_t counter)
1557 {
1558     if (counter < pmu_num_counters(env)) {
1559         uint64_t ret;
1560         pmevcntr_op_start(env, counter);
1561         ret = env->cp15.c14_pmevcntr[counter];
1562         pmevcntr_op_finish(env, counter);
1563         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1564             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1565             ret &= MAKE_64BIT_MASK(0, 32);
1566         }
1567         return ret;
1568     } else {
1569       /*
1570        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1571        * are CONSTRAINED UNPREDICTABLE.
1572        */
1573         return 0;
1574     }
1575 }
1576 
1577 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1578                              uint64_t value)
1579 {
1580     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1581     pmevcntr_write(env, ri, value, counter);
1582 }
1583 
1584 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1585 {
1586     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1587     return pmevcntr_read(env, ri, counter);
1588 }
1589 
1590 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1591                              uint64_t value)
1592 {
1593     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1594     assert(counter < pmu_num_counters(env));
1595     env->cp15.c14_pmevcntr[counter] = value;
1596     pmevcntr_write(env, ri, value, counter);
1597 }
1598 
1599 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1600 {
1601     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1602     assert(counter < pmu_num_counters(env));
1603     return env->cp15.c14_pmevcntr[counter];
1604 }
1605 
1606 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1607                              uint64_t value)
1608 {
1609     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1610 }
1611 
1612 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1613 {
1614     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1615 }
1616 
1617 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618                             uint64_t value)
1619 {
1620     if (arm_feature(env, ARM_FEATURE_V8)) {
1621         env->cp15.c9_pmuserenr = value & 0xf;
1622     } else {
1623         env->cp15.c9_pmuserenr = value & 1;
1624     }
1625 }
1626 
1627 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1628                              uint64_t value)
1629 {
1630     /* We have no event counters so only the C bit can be changed */
1631     value &= pmu_counter_mask(env);
1632     env->cp15.c9_pminten |= value;
1633     pmu_update_irq(env);
1634 }
1635 
1636 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1637                              uint64_t value)
1638 {
1639     value &= pmu_counter_mask(env);
1640     env->cp15.c9_pminten &= ~value;
1641     pmu_update_irq(env);
1642 }
1643 
1644 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1645                        uint64_t value)
1646 {
1647     /*
1648      * Note that even though the AArch64 view of this register has bits
1649      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1650      * architectural requirements for bits which are RES0 only in some
1651      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1652      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1653      */
1654     raw_write(env, ri, value & ~0x1FULL);
1655 }
1656 
1657 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1658 {
1659     /* Begin with base v8.0 state.  */
1660     uint64_t valid_mask = 0x3fff;
1661     ARMCPU *cpu = env_archcpu(env);
1662     uint64_t changed;
1663 
1664     /*
1665      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1666      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1667      * Instead, choose the format based on the mode of EL3.
1668      */
1669     if (arm_el_is_aa64(env, 3)) {
1670         value |= SCR_FW | SCR_AW;      /* RES1 */
1671         valid_mask &= ~SCR_NET;        /* RES0 */
1672 
1673         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1674             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1675             value |= SCR_RW;           /* RAO/WI */
1676         }
1677         if (cpu_isar_feature(aa64_ras, cpu)) {
1678             valid_mask |= SCR_TERR;
1679         }
1680         if (cpu_isar_feature(aa64_lor, cpu)) {
1681             valid_mask |= SCR_TLOR;
1682         }
1683         if (cpu_isar_feature(aa64_pauth, cpu)) {
1684             valid_mask |= SCR_API | SCR_APK;
1685         }
1686         if (cpu_isar_feature(aa64_sel2, cpu)) {
1687             valid_mask |= SCR_EEL2;
1688         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1689             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1690             value |= SCR_NS;
1691         }
1692         if (cpu_isar_feature(aa64_mte, cpu)) {
1693             valid_mask |= SCR_ATA;
1694         }
1695         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1696             valid_mask |= SCR_ENSCXT;
1697         }
1698         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1699             valid_mask |= SCR_EASE | SCR_NMEA;
1700         }
1701         if (cpu_isar_feature(aa64_sme, cpu)) {
1702             valid_mask |= SCR_ENTP2;
1703         }
1704         if (cpu_isar_feature(aa64_hcx, cpu)) {
1705             valid_mask |= SCR_HXEN;
1706         }
1707         if (cpu_isar_feature(aa64_fgt, cpu)) {
1708             valid_mask |= SCR_FGTEN;
1709         }
1710         if (cpu_isar_feature(aa64_rme, cpu)) {
1711             valid_mask |= SCR_NSE | SCR_GPF;
1712         }
1713         if (cpu_isar_feature(aa64_ecv, cpu)) {
1714             valid_mask |= SCR_ECVEN;
1715         }
1716     } else {
1717         valid_mask &= ~(SCR_RW | SCR_ST);
1718         if (cpu_isar_feature(aa32_ras, cpu)) {
1719             valid_mask |= SCR_TERR;
1720         }
1721     }
1722 
1723     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1724         valid_mask &= ~SCR_HCE;
1725 
1726         /*
1727          * On ARMv7, SMD (or SCD as it is called in v7) is only
1728          * supported if EL2 exists. The bit is UNK/SBZP when
1729          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1730          * when EL2 is unavailable.
1731          * On ARMv8, this bit is always available.
1732          */
1733         if (arm_feature(env, ARM_FEATURE_V7) &&
1734             !arm_feature(env, ARM_FEATURE_V8)) {
1735             valid_mask &= ~SCR_SMD;
1736         }
1737     }
1738 
1739     /* Clear all-context RES0 bits.  */
1740     value &= valid_mask;
1741     changed = env->cp15.scr_el3 ^ value;
1742     env->cp15.scr_el3 = value;
1743 
1744     /*
1745      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1746      * we must invalidate all TLBs below EL3.
1747      */
1748     if (changed & (SCR_NS | SCR_NSE)) {
1749         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1750                                            ARMMMUIdxBit_E20_0 |
1751                                            ARMMMUIdxBit_E10_1 |
1752                                            ARMMMUIdxBit_E20_2 |
1753                                            ARMMMUIdxBit_E10_1_PAN |
1754                                            ARMMMUIdxBit_E20_2_PAN |
1755                                            ARMMMUIdxBit_E2));
1756     }
1757 }
1758 
1759 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1760 {
1761     /*
1762      * scr_write will set the RES1 bits on an AArch64-only CPU.
1763      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1764      */
1765     scr_write(env, ri, 0);
1766 }
1767 
1768 static CPAccessResult access_tid4(CPUARMState *env,
1769                                   const ARMCPRegInfo *ri,
1770                                   bool isread)
1771 {
1772     if (arm_current_el(env) == 1 &&
1773         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1774         return CP_ACCESS_TRAP_EL2;
1775     }
1776 
1777     return CP_ACCESS_OK;
1778 }
1779 
1780 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1781 {
1782     ARMCPU *cpu = env_archcpu(env);
1783 
1784     /*
1785      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1786      * bank
1787      */
1788     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1789                                         ri->secure & ARM_CP_SECSTATE_S);
1790 
1791     return cpu->ccsidr[index];
1792 }
1793 
1794 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795                          uint64_t value)
1796 {
1797     raw_write(env, ri, value & 0xf);
1798 }
1799 
1800 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1801 {
1802     CPUState *cs = env_cpu(env);
1803     bool el1 = arm_current_el(env) == 1;
1804     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1805     uint64_t ret = 0;
1806 
1807     if (hcr_el2 & HCR_IMO) {
1808         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1809             ret |= CPSR_I;
1810         }
1811         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1812             ret |= ISR_IS;
1813             ret |= CPSR_I;
1814         }
1815     } else {
1816         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1817             ret |= CPSR_I;
1818         }
1819 
1820         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1821             ret |= ISR_IS;
1822             ret |= CPSR_I;
1823         }
1824     }
1825 
1826     if (hcr_el2 & HCR_FMO) {
1827         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1828             ret |= CPSR_F;
1829         }
1830         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1831             ret |= ISR_FS;
1832             ret |= CPSR_F;
1833         }
1834     } else {
1835         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1836             ret |= CPSR_F;
1837         }
1838     }
1839 
1840     if (hcr_el2 & HCR_AMO) {
1841         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1842             ret |= CPSR_A;
1843         }
1844     }
1845 
1846     return ret;
1847 }
1848 
1849 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1850                                        bool isread)
1851 {
1852     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1853         return CP_ACCESS_TRAP_EL2;
1854     }
1855 
1856     return CP_ACCESS_OK;
1857 }
1858 
1859 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1860                                        bool isread)
1861 {
1862     if (arm_feature(env, ARM_FEATURE_V8)) {
1863         return access_aa64_tid1(env, ri, isread);
1864     }
1865 
1866     return CP_ACCESS_OK;
1867 }
1868 
1869 static const ARMCPRegInfo v7_cp_reginfo[] = {
1870     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1871     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1872       .access = PL1_W, .type = ARM_CP_NOP },
1873     /*
1874      * Performance monitors are implementation defined in v7,
1875      * but with an ARM recommended set of registers, which we
1876      * follow.
1877      *
1878      * Performance registers fall into three categories:
1879      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1880      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1881      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1882      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1883      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1884      */
1885     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1886       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1887       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1888       .writefn = pmcntenset_write,
1889       .accessfn = pmreg_access,
1890       .fgt = FGT_PMCNTEN,
1891       .raw_writefn = raw_write },
1892     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1893       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1894       .access = PL0_RW, .accessfn = pmreg_access,
1895       .fgt = FGT_PMCNTEN,
1896       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1897       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1898     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1899       .access = PL0_RW,
1900       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1901       .accessfn = pmreg_access,
1902       .fgt = FGT_PMCNTEN,
1903       .writefn = pmcntenclr_write,
1904       .type = ARM_CP_ALIAS | ARM_CP_IO },
1905     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1906       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1907       .access = PL0_RW, .accessfn = pmreg_access,
1908       .fgt = FGT_PMCNTEN,
1909       .type = ARM_CP_ALIAS | ARM_CP_IO,
1910       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1911       .writefn = pmcntenclr_write },
1912     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1913       .access = PL0_RW, .type = ARM_CP_IO,
1914       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1915       .accessfn = pmreg_access,
1916       .fgt = FGT_PMOVS,
1917       .writefn = pmovsr_write,
1918       .raw_writefn = raw_write },
1919     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1920       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1921       .access = PL0_RW, .accessfn = pmreg_access,
1922       .fgt = FGT_PMOVS,
1923       .type = ARM_CP_ALIAS | ARM_CP_IO,
1924       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1925       .writefn = pmovsr_write,
1926       .raw_writefn = raw_write },
1927     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1928       .access = PL0_W, .accessfn = pmreg_access_swinc,
1929       .fgt = FGT_PMSWINC_EL0,
1930       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1931       .writefn = pmswinc_write },
1932     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1933       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1934       .access = PL0_W, .accessfn = pmreg_access_swinc,
1935       .fgt = FGT_PMSWINC_EL0,
1936       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1937       .writefn = pmswinc_write },
1938     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1939       .access = PL0_RW, .type = ARM_CP_ALIAS,
1940       .fgt = FGT_PMSELR_EL0,
1941       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1942       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1943       .raw_writefn = raw_write},
1944     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1945       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1946       .access = PL0_RW, .accessfn = pmreg_access_selr,
1947       .fgt = FGT_PMSELR_EL0,
1948       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1949       .writefn = pmselr_write, .raw_writefn = raw_write, },
1950     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1951       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1952       .fgt = FGT_PMCCNTR_EL0,
1953       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1954       .accessfn = pmreg_access_ccntr },
1955     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1956       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1957       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1958       .fgt = FGT_PMCCNTR_EL0,
1959       .type = ARM_CP_IO,
1960       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1961       .readfn = pmccntr_read, .writefn = pmccntr_write,
1962       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1963     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1964       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1965       .access = PL0_RW, .accessfn = pmreg_access,
1966       .fgt = FGT_PMCCFILTR_EL0,
1967       .type = ARM_CP_ALIAS | ARM_CP_IO,
1968       .resetvalue = 0, },
1969     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1970       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1971       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1972       .access = PL0_RW, .accessfn = pmreg_access,
1973       .fgt = FGT_PMCCFILTR_EL0,
1974       .type = ARM_CP_IO,
1975       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1976       .resetvalue = 0, },
1977     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1978       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1979       .accessfn = pmreg_access,
1980       .fgt = FGT_PMEVTYPERN_EL0,
1981       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1982     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1983       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1984       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1985       .accessfn = pmreg_access,
1986       .fgt = FGT_PMEVTYPERN_EL0,
1987       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1988     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1989       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1990       .accessfn = pmreg_access_xevcntr,
1991       .fgt = FGT_PMEVCNTRN_EL0,
1992       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1993     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1994       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1995       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1996       .accessfn = pmreg_access_xevcntr,
1997       .fgt = FGT_PMEVCNTRN_EL0,
1998       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1999     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2000       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2001       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2002       .resetvalue = 0,
2003       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2004     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2005       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2006       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2007       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2008       .resetvalue = 0,
2009       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2010     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2011       .access = PL1_RW, .accessfn = access_tpm,
2012       .fgt = FGT_PMINTEN,
2013       .type = ARM_CP_ALIAS | ARM_CP_IO,
2014       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2015       .resetvalue = 0,
2016       .writefn = pmintenset_write, .raw_writefn = raw_write },
2017     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2018       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2019       .access = PL1_RW, .accessfn = access_tpm,
2020       .fgt = FGT_PMINTEN,
2021       .type = ARM_CP_IO,
2022       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2023       .writefn = pmintenset_write, .raw_writefn = raw_write,
2024       .resetvalue = 0x0 },
2025     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2026       .access = PL1_RW, .accessfn = access_tpm,
2027       .fgt = FGT_PMINTEN,
2028       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2029       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2030       .writefn = pmintenclr_write, },
2031     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2032       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2033       .access = PL1_RW, .accessfn = access_tpm,
2034       .fgt = FGT_PMINTEN,
2035       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2036       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2037       .writefn = pmintenclr_write },
2038     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2039       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2040       .access = PL1_R,
2041       .accessfn = access_tid4,
2042       .fgt = FGT_CCSIDR_EL1,
2043       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2044     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2045       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2046       .access = PL1_RW,
2047       .accessfn = access_tid4,
2048       .fgt = FGT_CSSELR_EL1,
2049       .writefn = csselr_write, .resetvalue = 0,
2050       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2051                              offsetof(CPUARMState, cp15.csselr_ns) } },
2052     /*
2053      * Auxiliary ID register: this actually has an IMPDEF value but for now
2054      * just RAZ for all cores:
2055      */
2056     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2057       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2058       .access = PL1_R, .type = ARM_CP_CONST,
2059       .accessfn = access_aa64_tid1,
2060       .fgt = FGT_AIDR_EL1,
2061       .resetvalue = 0 },
2062     /*
2063      * Auxiliary fault status registers: these also are IMPDEF, and we
2064      * choose to RAZ/WI for all cores.
2065      */
2066     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2067       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2068       .access = PL1_RW, .accessfn = access_tvm_trvm,
2069       .fgt = FGT_AFSR0_EL1,
2070       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2071       .type = ARM_CP_CONST, .resetvalue = 0 },
2072     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2073       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2074       .access = PL1_RW, .accessfn = access_tvm_trvm,
2075       .fgt = FGT_AFSR1_EL1,
2076       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2077       .type = ARM_CP_CONST, .resetvalue = 0 },
2078     /*
2079      * MAIR can just read-as-written because we don't implement caches
2080      * and so don't need to care about memory attributes.
2081      */
2082     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2083       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2084       .access = PL1_RW, .accessfn = access_tvm_trvm,
2085       .fgt = FGT_MAIR_EL1,
2086       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2087       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2088       .resetvalue = 0 },
2089     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2090       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2091       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2092       .resetvalue = 0 },
2093     /*
2094      * For non-long-descriptor page tables these are PRRR and NMRR;
2095      * regardless they still act as reads-as-written for QEMU.
2096      */
2097      /*
2098       * MAIR0/1 are defined separately from their 64-bit counterpart which
2099       * allows them to assign the correct fieldoffset based on the endianness
2100       * handled in the field definitions.
2101       */
2102     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2103       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2104       .access = PL1_RW, .accessfn = access_tvm_trvm,
2105       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2106                              offsetof(CPUARMState, cp15.mair0_ns) },
2107       .resetfn = arm_cp_reset_ignore },
2108     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2109       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2110       .access = PL1_RW, .accessfn = access_tvm_trvm,
2111       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2112                              offsetof(CPUARMState, cp15.mair1_ns) },
2113       .resetfn = arm_cp_reset_ignore },
2114     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2115       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2116       .fgt = FGT_ISR_EL1,
2117       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2118 };
2119 
2120 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2121     /* PMOVSSET is not implemented in v7 before v7ve */
2122     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2123       .access = PL0_RW, .accessfn = pmreg_access,
2124       .fgt = FGT_PMOVS,
2125       .type = ARM_CP_ALIAS | ARM_CP_IO,
2126       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2127       .writefn = pmovsset_write,
2128       .raw_writefn = raw_write },
2129     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2130       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2131       .access = PL0_RW, .accessfn = pmreg_access,
2132       .fgt = FGT_PMOVS,
2133       .type = ARM_CP_ALIAS | ARM_CP_IO,
2134       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2135       .writefn = pmovsset_write,
2136       .raw_writefn = raw_write },
2137 };
2138 
2139 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2140                         uint64_t value)
2141 {
2142     value &= 1;
2143     env->teecr = value;
2144 }
2145 
2146 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2147                                    bool isread)
2148 {
2149     /*
2150      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2151      * at all, so we don't need to check whether we're v8A.
2152      */
2153     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2154         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2155         return CP_ACCESS_TRAP_EL2;
2156     }
2157     return CP_ACCESS_OK;
2158 }
2159 
2160 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2161                                     bool isread)
2162 {
2163     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2164         return CP_ACCESS_TRAP_EL1;
2165     }
2166     return teecr_access(env, ri, isread);
2167 }
2168 
2169 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2170     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2171       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2172       .resetvalue = 0,
2173       .writefn = teecr_write, .accessfn = teecr_access },
2174     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2175       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2176       .accessfn = teehbr_access, .resetvalue = 0 },
2177 };
2178 
2179 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2180     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2181       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2182       .access = PL0_RW,
2183       .fgt = FGT_TPIDR_EL0,
2184       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2185     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2186       .access = PL0_RW,
2187       .fgt = FGT_TPIDR_EL0,
2188       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2189                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2190       .resetfn = arm_cp_reset_ignore },
2191     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2192       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2193       .access = PL0_R | PL1_W,
2194       .fgt = FGT_TPIDRRO_EL0,
2195       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2196       .resetvalue = 0},
2197     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2198       .access = PL0_R | PL1_W,
2199       .fgt = FGT_TPIDRRO_EL0,
2200       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2201                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2202       .resetfn = arm_cp_reset_ignore },
2203     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2204       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2205       .access = PL1_RW,
2206       .fgt = FGT_TPIDR_EL1,
2207       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2208     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2209       .access = PL1_RW,
2210       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2211                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2212       .resetvalue = 0 },
2213 };
2214 
2215 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2216 {
2217     ARMCPU *cpu = env_archcpu(env);
2218 
2219     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2220 }
2221 
2222 #ifndef CONFIG_USER_ONLY
2223 
2224 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2225                                        bool isread)
2226 {
2227     /*
2228      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2229      * Writable only at the highest implemented exception level.
2230      */
2231     int el = arm_current_el(env);
2232     uint64_t hcr;
2233     uint32_t cntkctl;
2234 
2235     switch (el) {
2236     case 0:
2237         hcr = arm_hcr_el2_eff(env);
2238         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2239             cntkctl = env->cp15.cnthctl_el2;
2240         } else {
2241             cntkctl = env->cp15.c14_cntkctl;
2242         }
2243         if (!extract32(cntkctl, 0, 2)) {
2244             return CP_ACCESS_TRAP_EL1;
2245         }
2246         break;
2247     case 1:
2248         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2249             arm_is_secure_below_el3(env)) {
2250             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2251             return CP_ACCESS_UNDEFINED;
2252         }
2253         break;
2254     case 2:
2255     case 3:
2256         break;
2257     }
2258 
2259     if (!isread && el < arm_highest_el(env)) {
2260         return CP_ACCESS_UNDEFINED;
2261     }
2262 
2263     return CP_ACCESS_OK;
2264 }
2265 
2266 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2267                                         bool isread)
2268 {
2269     unsigned int cur_el = arm_current_el(env);
2270     bool has_el2 = arm_is_el2_enabled(env);
2271     uint64_t hcr = arm_hcr_el2_eff(env);
2272 
2273     switch (cur_el) {
2274     case 0:
2275         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2276         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2277             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2278                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2279         }
2280 
2281         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2282         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2283             return CP_ACCESS_TRAP_EL1;
2284         }
2285         /* fall through */
2286     case 1:
2287         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2288         if (has_el2 && timeridx == GTIMER_PHYS &&
2289             (hcr & HCR_E2H
2290              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2291              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2292             return CP_ACCESS_TRAP_EL2;
2293         }
2294         if (has_el2 && timeridx == GTIMER_VIRT) {
2295             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2296                 return CP_ACCESS_TRAP_EL2;
2297             }
2298         }
2299         break;
2300     }
2301     return CP_ACCESS_OK;
2302 }
2303 
2304 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2305                                       bool isread)
2306 {
2307     unsigned int cur_el = arm_current_el(env);
2308     bool has_el2 = arm_is_el2_enabled(env);
2309     uint64_t hcr = arm_hcr_el2_eff(env);
2310 
2311     switch (cur_el) {
2312     case 0:
2313         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2314             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2315             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2316                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2317         }
2318 
2319         /*
2320          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2321          * EL0 if EL0[PV]TEN is zero.
2322          */
2323         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2324             return CP_ACCESS_TRAP_EL1;
2325         }
2326         /* fall through */
2327 
2328     case 1:
2329         if (has_el2 && timeridx == GTIMER_PHYS) {
2330             if (hcr & HCR_E2H) {
2331                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2332                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2333                     return CP_ACCESS_TRAP_EL2;
2334                 }
2335             } else {
2336                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2337                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2338                     return CP_ACCESS_TRAP_EL2;
2339                 }
2340             }
2341         }
2342         if (has_el2 && timeridx == GTIMER_VIRT) {
2343             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2344                 return CP_ACCESS_TRAP_EL2;
2345             }
2346         }
2347         break;
2348     }
2349     return CP_ACCESS_OK;
2350 }
2351 
2352 static CPAccessResult gt_pct_access(CPUARMState *env,
2353                                     const ARMCPRegInfo *ri,
2354                                     bool isread)
2355 {
2356     return gt_counter_access(env, GTIMER_PHYS, isread);
2357 }
2358 
2359 static CPAccessResult gt_vct_access(CPUARMState *env,
2360                                     const ARMCPRegInfo *ri,
2361                                     bool isread)
2362 {
2363     return gt_counter_access(env, GTIMER_VIRT, isread);
2364 }
2365 
2366 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2367                                        bool isread)
2368 {
2369     return gt_timer_access(env, GTIMER_PHYS, isread);
2370 }
2371 
2372 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2373                                        bool isread)
2374 {
2375     return gt_timer_access(env, GTIMER_VIRT, isread);
2376 }
2377 
2378 static CPAccessResult gt_stimer_access(CPUARMState *env,
2379                                        const ARMCPRegInfo *ri,
2380                                        bool isread)
2381 {
2382     /*
2383      * The AArch64 register view of the secure physical timer is
2384      * always accessible from EL3, and configurably accessible from
2385      * Secure EL1.
2386      */
2387     switch (arm_current_el(env)) {
2388     case 1:
2389         if (!arm_is_secure(env)) {
2390             return CP_ACCESS_UNDEFINED;
2391         }
2392         if (arm_is_el2_enabled(env)) {
2393             return CP_ACCESS_UNDEFINED;
2394         }
2395         if (!(env->cp15.scr_el3 & SCR_ST)) {
2396             return CP_ACCESS_TRAP_EL3;
2397         }
2398         return CP_ACCESS_OK;
2399     case 0:
2400     case 2:
2401         return CP_ACCESS_UNDEFINED;
2402     case 3:
2403         return CP_ACCESS_OK;
2404     default:
2405         g_assert_not_reached();
2406     }
2407 }
2408 
2409 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2410                                           const ARMCPRegInfo *ri,
2411                                           bool isread)
2412 {
2413     /*
2414      * The AArch64 register view of the secure EL2 timers are mostly
2415      * accessible from EL3 and EL2 although can also be trapped to EL2
2416      * from EL1 depending on nested virt config.
2417      */
2418     switch (arm_current_el(env)) {
2419     case 0: /* UNDEFINED */
2420         return CP_ACCESS_UNDEFINED;
2421     case 1:
2422         if (!arm_is_secure(env)) {
2423             /* UNDEFINED */
2424             return CP_ACCESS_UNDEFINED;
2425         } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2426             /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2427             return CP_ACCESS_TRAP_EL2;
2428         }
2429         /* UNDEFINED */
2430         return CP_ACCESS_UNDEFINED;
2431     case 2:
2432         if (!arm_is_secure(env)) {
2433             /* UNDEFINED */
2434             return CP_ACCESS_UNDEFINED;
2435         }
2436         return CP_ACCESS_OK;
2437     case 3:
2438         if (env->cp15.scr_el3 & SCR_EEL2) {
2439             return CP_ACCESS_OK;
2440         } else {
2441             return CP_ACCESS_UNDEFINED;
2442         }
2443     default:
2444         g_assert_not_reached();
2445     }
2446 }
2447 
2448 uint64_t gt_get_countervalue(CPUARMState *env)
2449 {
2450     ARMCPU *cpu = env_archcpu(env);
2451 
2452     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2453 }
2454 
2455 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2456 {
2457     CPUARMState *env = &cpu->env;
2458     uint64_t cnthctl = env->cp15.cnthctl_el2;
2459     ARMSecuritySpace ss = arm_security_space(env);
2460     /* ISTATUS && !IMASK */
2461     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2462 
2463     /*
2464      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2465      * It is RES0 in Secure and NonSecure state.
2466      */
2467     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2468         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2469          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2470         irqstate = 0;
2471     }
2472 
2473     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2474     trace_arm_gt_update_irq(timeridx, irqstate);
2475 }
2476 
2477 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2478 {
2479     /*
2480      * Changing security state between Root and Secure/NonSecure, which may
2481      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2482      * mask bits. Update the IRQ state accordingly.
2483      */
2484     gt_update_irq(cpu, GTIMER_VIRT);
2485     gt_update_irq(cpu, GTIMER_PHYS);
2486 }
2487 
2488 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2489 {
2490     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2491         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2492         arm_is_el2_enabled(env) &&
2493         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2494         return env->cp15.cntpoff_el2;
2495     }
2496     return 0;
2497 }
2498 
2499 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2500 {
2501     /*
2502      * Return the timer offset to use for indirect accesses to the timer.
2503      * This is the Offset value as defined in D12.2.4.1 "Operation of the
2504      * CompareValue views of the timers".
2505      *
2506      * The condition here is not always the same as the condition for
2507      * whether to apply an offset register when doing a direct read of
2508      * the counter sysreg; those conditions are described in the
2509      * access pseudocode for each counter register.
2510      */
2511     switch (timeridx) {
2512     case GTIMER_PHYS:
2513         return gt_phys_raw_cnt_offset(env);
2514     case GTIMER_VIRT:
2515         return env->cp15.cntvoff_el2;
2516     case GTIMER_HYP:
2517     case GTIMER_SEC:
2518     case GTIMER_HYPVIRT:
2519     case GTIMER_S_EL2_PHYS:
2520     case GTIMER_S_EL2_VIRT:
2521         return 0;
2522     default:
2523         g_assert_not_reached();
2524     }
2525 }
2526 
2527 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2528 {
2529     /*
2530      * Return the timer offset to use for direct accesses to the
2531      * counter registers CNTPCT and CNTVCT, and for direct accesses
2532      * to the CNT*_TVAL registers.
2533      *
2534      * This isn't exactly the same as the indirect-access offset,
2535      * because here we also care about what EL the register access
2536      * is being made from.
2537      *
2538      * This corresponds to the access pseudocode for the registers.
2539      */
2540     uint64_t hcr;
2541 
2542     switch (timeridx) {
2543     case GTIMER_PHYS:
2544         if (arm_current_el(env) >= 2) {
2545             return 0;
2546         }
2547         return gt_phys_raw_cnt_offset(env);
2548     case GTIMER_VIRT:
2549         switch (arm_current_el(env)) {
2550         case 2:
2551             hcr = arm_hcr_el2_eff(env);
2552             if (hcr & HCR_E2H) {
2553                 return 0;
2554             }
2555             break;
2556         case 0:
2557             hcr = arm_hcr_el2_eff(env);
2558             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2559                 return 0;
2560             }
2561             break;
2562         }
2563         return env->cp15.cntvoff_el2;
2564     case GTIMER_HYP:
2565     case GTIMER_SEC:
2566     case GTIMER_HYPVIRT:
2567     case GTIMER_S_EL2_PHYS:
2568     case GTIMER_S_EL2_VIRT:
2569         return 0;
2570     default:
2571         g_assert_not_reached();
2572     }
2573 }
2574 
2575 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2576 {
2577     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2578 
2579     if (gt->ctl & 1) {
2580         /*
2581          * Timer enabled: calculate and set current ISTATUS, irq, and
2582          * reset timer to when ISTATUS next has to change
2583          */
2584         uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2585         uint64_t count = gt_get_countervalue(&cpu->env);
2586         /* Note that this must be unsigned 64 bit arithmetic: */
2587         int istatus = count - offset >= gt->cval;
2588         uint64_t nexttick;
2589 
2590         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2591 
2592         if (istatus) {
2593             /*
2594              * Next transition is when (count - offset) rolls back over to 0.
2595              * If offset > count then this is when count == offset;
2596              * if offset <= count then this is when count == offset + 2^64
2597              * For the latter case we set nexttick to an "as far in future
2598              * as possible" value and let the code below handle it.
2599              */
2600             if (offset > count) {
2601                 nexttick = offset;
2602             } else {
2603                 nexttick = UINT64_MAX;
2604             }
2605         } else {
2606             /*
2607              * Next transition is when (count - offset) == cval, i.e.
2608              * when count == (cval + offset).
2609              * If that would overflow, then again we set up the next interrupt
2610              * for "as far in the future as possible" for the code below.
2611              */
2612             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2613                 nexttick = UINT64_MAX;
2614             }
2615         }
2616         /*
2617          * Note that the desired next expiry time might be beyond the
2618          * signed-64-bit range of a QEMUTimer -- in this case we just
2619          * set the timer for as far in the future as possible. When the
2620          * timer expires we will reset the timer for any remaining period.
2621          */
2622         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2623             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2624         } else {
2625             timer_mod(cpu->gt_timer[timeridx], nexttick);
2626         }
2627         trace_arm_gt_recalc(timeridx, nexttick);
2628     } else {
2629         /* Timer disabled: ISTATUS and timer output always clear */
2630         gt->ctl &= ~4;
2631         timer_del(cpu->gt_timer[timeridx]);
2632         trace_arm_gt_recalc_disabled(timeridx);
2633     }
2634     gt_update_irq(cpu, timeridx);
2635 }
2636 
2637 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2638                            int timeridx)
2639 {
2640     ARMCPU *cpu = env_archcpu(env);
2641 
2642     timer_del(cpu->gt_timer[timeridx]);
2643 }
2644 
2645 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2646 {
2647     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2648     return gt_get_countervalue(env) - offset;
2649 }
2650 
2651 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2652 {
2653     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2654     return gt_get_countervalue(env) - offset;
2655 }
2656 
2657 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2658                           int timeridx,
2659                           uint64_t value)
2660 {
2661     trace_arm_gt_cval_write(timeridx, value);
2662     env->cp15.c14_timer[timeridx].cval = value;
2663     gt_recalc_timer(env_archcpu(env), timeridx);
2664 }
2665 
2666 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2667 {
2668     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2669                       (gt_get_countervalue(env) - offset));
2670 }
2671 
2672 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2673                              int timeridx)
2674 {
2675     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2676 
2677     return do_tval_read(env, timeridx, offset);
2678 }
2679 
2680 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2681                           uint64_t offset)
2682 {
2683     trace_arm_gt_tval_write(timeridx, value);
2684     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2685                                          sextract64(value, 0, 32);
2686     gt_recalc_timer(env_archcpu(env), timeridx);
2687 }
2688 
2689 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690                           int timeridx,
2691                           uint64_t value)
2692 {
2693     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2694 
2695     do_tval_write(env, timeridx, value, offset);
2696 }
2697 
2698 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2699                          int timeridx,
2700                          uint64_t value)
2701 {
2702     ARMCPU *cpu = env_archcpu(env);
2703     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2704 
2705     trace_arm_gt_ctl_write(timeridx, value);
2706     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2707     if ((oldval ^ value) & 1) {
2708         /* Enable toggled */
2709         gt_recalc_timer(cpu, timeridx);
2710     } else if ((oldval ^ value) & 2) {
2711         /*
2712          * IMASK toggled: don't need to recalculate,
2713          * just set the interrupt line based on ISTATUS
2714          */
2715         trace_arm_gt_imask_toggle(timeridx);
2716         gt_update_irq(cpu, timeridx);
2717     }
2718 }
2719 
2720 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2721 {
2722     gt_timer_reset(env, ri, GTIMER_PHYS);
2723 }
2724 
2725 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2726                                uint64_t value)
2727 {
2728     gt_cval_write(env, ri, GTIMER_PHYS, value);
2729 }
2730 
2731 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2732 {
2733     return gt_tval_read(env, ri, GTIMER_PHYS);
2734 }
2735 
2736 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2737                                uint64_t value)
2738 {
2739     gt_tval_write(env, ri, GTIMER_PHYS, value);
2740 }
2741 
2742 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743                               uint64_t value)
2744 {
2745     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2746 }
2747 
2748 static int gt_phys_redir_timeridx(CPUARMState *env)
2749 {
2750     switch (arm_mmu_idx(env)) {
2751     case ARMMMUIdx_E20_0:
2752     case ARMMMUIdx_E20_2:
2753     case ARMMMUIdx_E20_2_PAN:
2754         return GTIMER_HYP;
2755     default:
2756         return GTIMER_PHYS;
2757     }
2758 }
2759 
2760 static int gt_virt_redir_timeridx(CPUARMState *env)
2761 {
2762     switch (arm_mmu_idx(env)) {
2763     case ARMMMUIdx_E20_0:
2764     case ARMMMUIdx_E20_2:
2765     case ARMMMUIdx_E20_2_PAN:
2766         return GTIMER_HYPVIRT;
2767     default:
2768         return GTIMER_VIRT;
2769     }
2770 }
2771 
2772 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2773                                         const ARMCPRegInfo *ri)
2774 {
2775     int timeridx = gt_phys_redir_timeridx(env);
2776     return env->cp15.c14_timer[timeridx].cval;
2777 }
2778 
2779 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2780                                      uint64_t value)
2781 {
2782     int timeridx = gt_phys_redir_timeridx(env);
2783     gt_cval_write(env, ri, timeridx, value);
2784 }
2785 
2786 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2787                                         const ARMCPRegInfo *ri)
2788 {
2789     int timeridx = gt_phys_redir_timeridx(env);
2790     return gt_tval_read(env, ri, timeridx);
2791 }
2792 
2793 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2794                                      uint64_t value)
2795 {
2796     int timeridx = gt_phys_redir_timeridx(env);
2797     gt_tval_write(env, ri, timeridx, value);
2798 }
2799 
2800 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2801                                        const ARMCPRegInfo *ri)
2802 {
2803     int timeridx = gt_phys_redir_timeridx(env);
2804     return env->cp15.c14_timer[timeridx].ctl;
2805 }
2806 
2807 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2808                                     uint64_t value)
2809 {
2810     int timeridx = gt_phys_redir_timeridx(env);
2811     gt_ctl_write(env, ri, timeridx, value);
2812 }
2813 
2814 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2815 {
2816     gt_timer_reset(env, ri, GTIMER_VIRT);
2817 }
2818 
2819 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2820                                uint64_t value)
2821 {
2822     gt_cval_write(env, ri, GTIMER_VIRT, value);
2823 }
2824 
2825 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2826 {
2827     /*
2828      * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
2829      * we always apply CNTVOFF_EL2. Special case that here rather
2830      * than going into the generic gt_tval_read() and then having
2831      * to re-detect that it's this register.
2832      * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
2833      */
2834     return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
2835 }
2836 
2837 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                                uint64_t value)
2839 {
2840     /* Similarly for writes to CNTV_TVAL_EL02 */
2841     do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
2842 }
2843 
2844 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2845                               uint64_t value)
2846 {
2847     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2848 }
2849 
2850 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851                              uint64_t value)
2852 {
2853     ARMCPU *cpu = env_archcpu(env);
2854     uint32_t oldval = env->cp15.cnthctl_el2;
2855     uint32_t valid_mask =
2856         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2857         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2858         R_CNTHCTL_EVNTEN_MASK |
2859         R_CNTHCTL_EVNTDIR_MASK |
2860         R_CNTHCTL_EVNTI_MASK |
2861         R_CNTHCTL_EL0VTEN_MASK |
2862         R_CNTHCTL_EL0PTEN_MASK |
2863         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2864         R_CNTHCTL_EL1PTEN_MASK;
2865 
2866     if (cpu_isar_feature(aa64_rme, cpu)) {
2867         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2868     }
2869     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2870         valid_mask |=
2871             R_CNTHCTL_EL1TVT_MASK |
2872             R_CNTHCTL_EL1TVCT_MASK |
2873             R_CNTHCTL_EL1NVPCT_MASK |
2874             R_CNTHCTL_EL1NVVCT_MASK |
2875             R_CNTHCTL_EVNTIS_MASK;
2876     }
2877     if (cpu_isar_feature(aa64_ecv, cpu)) {
2878         valid_mask |= R_CNTHCTL_ECV_MASK;
2879     }
2880 
2881     /* Clear RES0 bits */
2882     value &= valid_mask;
2883 
2884     raw_write(env, ri, value);
2885 
2886     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2887         gt_update_irq(cpu, GTIMER_VIRT);
2888     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2889         gt_update_irq(cpu, GTIMER_PHYS);
2890     }
2891 }
2892 
2893 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2894                               uint64_t value)
2895 {
2896     ARMCPU *cpu = env_archcpu(env);
2897 
2898     trace_arm_gt_cntvoff_write(value);
2899     raw_write(env, ri, value);
2900     gt_recalc_timer(cpu, GTIMER_VIRT);
2901 }
2902 
2903 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2904                                         const ARMCPRegInfo *ri)
2905 {
2906     int timeridx = gt_virt_redir_timeridx(env);
2907     return env->cp15.c14_timer[timeridx].cval;
2908 }
2909 
2910 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2911                                      uint64_t value)
2912 {
2913     int timeridx = gt_virt_redir_timeridx(env);
2914     gt_cval_write(env, ri, timeridx, value);
2915 }
2916 
2917 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2918                                         const ARMCPRegInfo *ri)
2919 {
2920     int timeridx = gt_virt_redir_timeridx(env);
2921     return gt_tval_read(env, ri, timeridx);
2922 }
2923 
2924 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2925                                      uint64_t value)
2926 {
2927     int timeridx = gt_virt_redir_timeridx(env);
2928     gt_tval_write(env, ri, timeridx, value);
2929 }
2930 
2931 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2932                                        const ARMCPRegInfo *ri)
2933 {
2934     int timeridx = gt_virt_redir_timeridx(env);
2935     return env->cp15.c14_timer[timeridx].ctl;
2936 }
2937 
2938 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2939                                     uint64_t value)
2940 {
2941     int timeridx = gt_virt_redir_timeridx(env);
2942     gt_ctl_write(env, ri, timeridx, value);
2943 }
2944 
2945 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2946 {
2947     gt_timer_reset(env, ri, GTIMER_HYP);
2948 }
2949 
2950 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2951                               uint64_t value)
2952 {
2953     gt_cval_write(env, ri, GTIMER_HYP, value);
2954 }
2955 
2956 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2957 {
2958     return gt_tval_read(env, ri, GTIMER_HYP);
2959 }
2960 
2961 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2962                               uint64_t value)
2963 {
2964     gt_tval_write(env, ri, GTIMER_HYP, value);
2965 }
2966 
2967 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2968                               uint64_t value)
2969 {
2970     gt_ctl_write(env, ri, GTIMER_HYP, value);
2971 }
2972 
2973 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2974 {
2975     gt_timer_reset(env, ri, GTIMER_SEC);
2976 }
2977 
2978 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2979                               uint64_t value)
2980 {
2981     gt_cval_write(env, ri, GTIMER_SEC, value);
2982 }
2983 
2984 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2985 {
2986     return gt_tval_read(env, ri, GTIMER_SEC);
2987 }
2988 
2989 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2990                               uint64_t value)
2991 {
2992     gt_tval_write(env, ri, GTIMER_SEC, value);
2993 }
2994 
2995 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2996                               uint64_t value)
2997 {
2998     gt_ctl_write(env, ri, GTIMER_SEC, value);
2999 }
3000 
3001 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3002 {
3003     gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3004 }
3005 
3006 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3007                                    uint64_t value)
3008 {
3009     gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3010 }
3011 
3012 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3013 {
3014     return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3015 }
3016 
3017 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3018                               uint64_t value)
3019 {
3020     gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3021 }
3022 
3023 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3024                               uint64_t value)
3025 {
3026     gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3027 }
3028 
3029 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3030 {
3031     gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3032 }
3033 
3034 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3035                               uint64_t value)
3036 {
3037     gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3038 }
3039 
3040 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3041 {
3042     return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3043 }
3044 
3045 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3046                                    uint64_t value)
3047 {
3048     gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3049 }
3050 
3051 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3052                               uint64_t value)
3053 {
3054     gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3055 }
3056 
3057 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3058 {
3059     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3060 }
3061 
3062 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3063                              uint64_t value)
3064 {
3065     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3066 }
3067 
3068 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3069 {
3070     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3071 }
3072 
3073 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3074                              uint64_t value)
3075 {
3076     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3077 }
3078 
3079 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3080                             uint64_t value)
3081 {
3082     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3083 }
3084 
3085 void arm_gt_ptimer_cb(void *opaque)
3086 {
3087     ARMCPU *cpu = opaque;
3088 
3089     gt_recalc_timer(cpu, GTIMER_PHYS);
3090 }
3091 
3092 void arm_gt_vtimer_cb(void *opaque)
3093 {
3094     ARMCPU *cpu = opaque;
3095 
3096     gt_recalc_timer(cpu, GTIMER_VIRT);
3097 }
3098 
3099 void arm_gt_htimer_cb(void *opaque)
3100 {
3101     ARMCPU *cpu = opaque;
3102 
3103     gt_recalc_timer(cpu, GTIMER_HYP);
3104 }
3105 
3106 void arm_gt_stimer_cb(void *opaque)
3107 {
3108     ARMCPU *cpu = opaque;
3109 
3110     gt_recalc_timer(cpu, GTIMER_SEC);
3111 }
3112 
3113 void arm_gt_sel2timer_cb(void *opaque)
3114 {
3115     ARMCPU *cpu = opaque;
3116 
3117     gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3118 }
3119 
3120 void arm_gt_sel2vtimer_cb(void *opaque)
3121 {
3122     ARMCPU *cpu = opaque;
3123 
3124     gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3125 }
3126 
3127 void arm_gt_hvtimer_cb(void *opaque)
3128 {
3129     ARMCPU *cpu = opaque;
3130 
3131     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3132 }
3133 
3134 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3135     /*
3136      * Note that CNTFRQ is purely reads-as-written for the benefit
3137      * of software; writing it doesn't actually change the timer frequency.
3138      * Our reset value matches the fixed frequency we implement the timer at.
3139      */
3140     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3141       .type = ARM_CP_ALIAS,
3142       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3143       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3144     },
3145     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3146       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3147       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3148       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3149       .resetfn = arm_gt_cntfrq_reset,
3150     },
3151     /* overall control: mostly access permissions */
3152     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3153       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3154       .access = PL1_RW,
3155       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3156       .resetvalue = 0,
3157     },
3158     /* per-timer control */
3159     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3160       .secure = ARM_CP_SECSTATE_NS,
3161       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3162       .accessfn = gt_ptimer_access,
3163       .fieldoffset = offsetoflow32(CPUARMState,
3164                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3165       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3166       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3167     },
3168     { .name = "CNTP_CTL_S",
3169       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3170       .secure = ARM_CP_SECSTATE_S,
3171       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3172       .accessfn = gt_ptimer_access,
3173       .fieldoffset = offsetoflow32(CPUARMState,
3174                                    cp15.c14_timer[GTIMER_SEC].ctl),
3175       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3176     },
3177     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3178       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3179       .type = ARM_CP_IO, .access = PL0_RW,
3180       .accessfn = gt_ptimer_access,
3181       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3182       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3183       .resetvalue = 0,
3184       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3185       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3186     },
3187     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3188       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3189       .accessfn = gt_vtimer_access,
3190       .fieldoffset = offsetoflow32(CPUARMState,
3191                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3192       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3193       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3194     },
3195     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3196       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3197       .type = ARM_CP_IO, .access = PL0_RW,
3198       .accessfn = gt_vtimer_access,
3199       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3200       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3201       .resetvalue = 0,
3202       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3203       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3204     },
3205     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3206     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3207       .secure = ARM_CP_SECSTATE_NS,
3208       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3209       .accessfn = gt_ptimer_access,
3210       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3211     },
3212     { .name = "CNTP_TVAL_S",
3213       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3214       .secure = ARM_CP_SECSTATE_S,
3215       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3216       .accessfn = gt_ptimer_access,
3217       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3218     },
3219     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3220       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3221       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3222       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3223       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3224     },
3225     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3226       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3227       .accessfn = gt_vtimer_access,
3228       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3229     },
3230     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3231       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3232       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3233       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3234       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3235     },
3236     /* The counter itself */
3237     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3238       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3239       .accessfn = gt_pct_access,
3240       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3241     },
3242     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3243       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3244       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3245       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3246     },
3247     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3248       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3249       .accessfn = gt_vct_access,
3250       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3251     },
3252     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3253       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3254       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3255       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3256     },
3257     /* Comparison value, indicating when the timer goes off */
3258     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3259       .secure = ARM_CP_SECSTATE_NS,
3260       .access = PL0_RW,
3261       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3262       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3263       .accessfn = gt_ptimer_access,
3264       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3265       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3266     },
3267     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3268       .secure = ARM_CP_SECSTATE_S,
3269       .access = PL0_RW,
3270       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3271       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3272       .accessfn = gt_ptimer_access,
3273       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3274     },
3275     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3276       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3277       .access = PL0_RW,
3278       .type = ARM_CP_IO,
3279       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3280       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3281       .resetvalue = 0, .accessfn = gt_ptimer_access,
3282       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3283       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3284     },
3285     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3286       .access = PL0_RW,
3287       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3288       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3289       .accessfn = gt_vtimer_access,
3290       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3291       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3292     },
3293     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3294       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3295       .access = PL0_RW,
3296       .type = ARM_CP_IO,
3297       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3298       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3299       .resetvalue = 0, .accessfn = gt_vtimer_access,
3300       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3301       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3302     },
3303     /*
3304      * Secure timer -- this is actually restricted to only EL3
3305      * and configurably Secure-EL1 via the accessfn.
3306      */
3307     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3308       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3309       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3310       .accessfn = gt_stimer_access,
3311       .readfn = gt_sec_tval_read,
3312       .writefn = gt_sec_tval_write,
3313       .resetfn = gt_sec_timer_reset,
3314     },
3315     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3316       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3317       .type = ARM_CP_IO, .access = PL1_RW,
3318       .accessfn = gt_stimer_access,
3319       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3320       .resetvalue = 0,
3321       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3322     },
3323     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3324       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3325       .type = ARM_CP_IO, .access = PL1_RW,
3326       .accessfn = gt_stimer_access,
3327       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3328       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3329     },
3330 };
3331 
3332 /*
3333  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3334  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3335  * so our implementations here are identical to the normal registers.
3336  */
3337 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3338     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3339       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3340       .accessfn = gt_vct_access,
3341       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3342     },
3343     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3344       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3345       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3346       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3347     },
3348     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3349       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3350       .accessfn = gt_pct_access,
3351       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3352     },
3353     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3354       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3355       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3356       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3357     },
3358 };
3359 
3360 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3361                                         const ARMCPRegInfo *ri,
3362                                         bool isread)
3363 {
3364     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3365         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3366         return CP_ACCESS_TRAP_EL3;
3367     }
3368     return CP_ACCESS_OK;
3369 }
3370 
3371 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3372                               uint64_t value)
3373 {
3374     ARMCPU *cpu = env_archcpu(env);
3375 
3376     trace_arm_gt_cntpoff_write(value);
3377     raw_write(env, ri, value);
3378     gt_recalc_timer(cpu, GTIMER_PHYS);
3379 }
3380 
3381 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3382     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3383     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3384     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3385     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3386     .nv2_redirect_offset = 0x1a8,
3387     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3388 };
3389 #else
3390 
3391 /*
3392  * In user-mode most of the generic timer registers are inaccessible
3393  * however modern kernels (4.12+) allow access to cntvct_el0
3394  */
3395 
3396 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3397 {
3398     ARMCPU *cpu = env_archcpu(env);
3399 
3400     /*
3401      * Currently we have no support for QEMUTimer in linux-user so we
3402      * can't call gt_get_countervalue(env), instead we directly
3403      * call the lower level functions.
3404      */
3405     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3406 }
3407 
3408 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3409     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3410       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3411       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3412       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3413       .resetfn = arm_gt_cntfrq_reset,
3414     },
3415     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3416       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3417       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3418       .readfn = gt_virt_cnt_read,
3419     },
3420 };
3421 
3422 /*
3423  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3424  * is exposed to userspace by Linux.
3425  */
3426 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3427     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3428       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3429       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3430       .readfn = gt_virt_cnt_read,
3431     },
3432 };
3433 
3434 #endif
3435 
3436 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3437 {
3438     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3439         raw_write(env, ri, value);
3440     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3441         raw_write(env, ri, value & 0xfffff6ff);
3442     } else {
3443         raw_write(env, ri, value & 0xfffff1ff);
3444     }
3445 }
3446 
3447 #ifndef CONFIG_USER_ONLY
3448 /* get_phys_addr() isn't present for user-mode-only targets */
3449 
3450 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3451                                  bool isread)
3452 {
3453     if (ri->opc2 & 4) {
3454         /*
3455          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3456          * Secure EL1 (which can only happen if EL3 is AArch64).
3457          * They are simply UNDEF if executed from NS EL1.
3458          * They function normally from EL2 or EL3.
3459          */
3460         if (arm_current_el(env) == 1) {
3461             if (arm_is_secure_below_el3(env)) {
3462                 if (env->cp15.scr_el3 & SCR_EEL2) {
3463                     return CP_ACCESS_TRAP_EL2;
3464                 }
3465                 return CP_ACCESS_TRAP_EL3;
3466             }
3467             return CP_ACCESS_UNDEFINED;
3468         }
3469     }
3470     return CP_ACCESS_OK;
3471 }
3472 
3473 #ifdef CONFIG_TCG
3474 static int par_el1_shareability(GetPhysAddrResult *res)
3475 {
3476     /*
3477      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3478      * memory -- see pseudocode PAREncodeShareability().
3479      */
3480     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3481         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3482         return 2;
3483     }
3484     return res->cacheattrs.shareability;
3485 }
3486 
3487 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3488                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3489                              ARMSecuritySpace ss)
3490 {
3491     bool ret;
3492     uint64_t par64;
3493     bool format64 = false;
3494     ARMMMUFaultInfo fi = {};
3495     GetPhysAddrResult res = {};
3496 
3497     /*
3498      * I_MXTJT: Granule protection checks are not performed on the final
3499      * address of a successful translation.  This is a translation not a
3500      * memory reference, so "memop = none = 0".
3501      */
3502     ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3503                                          mmu_idx, ss, &res, &fi);
3504 
3505     /*
3506      * ATS operations only do S1 or S1+S2 translations, so we never
3507      * have to deal with the ARMCacheAttrs format for S2 only.
3508      */
3509     assert(!res.cacheattrs.is_s2_format);
3510 
3511     if (ret) {
3512         /*
3513          * Some kinds of translation fault must cause exceptions rather
3514          * than being reported in the PAR.
3515          */
3516         int current_el = arm_current_el(env);
3517         int target_el;
3518         uint32_t syn, fsr, fsc;
3519         bool take_exc = false;
3520 
3521         if (fi.s1ptw && current_el == 1
3522             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3523             /*
3524              * Synchronous stage 2 fault on an access made as part of the
3525              * translation table walk for AT S1E0* or AT S1E1* insn
3526              * executed from NS EL1. If this is a synchronous external abort
3527              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3528              * to EL3. Otherwise the fault is taken as an exception to EL2,
3529              * and HPFAR_EL2 holds the faulting IPA.
3530              */
3531             if (fi.type == ARMFault_SyncExternalOnWalk &&
3532                 (env->cp15.scr_el3 & SCR_EA)) {
3533                 target_el = 3;
3534             } else {
3535                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3536                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3537                     env->cp15.hpfar_el2 |= HPFAR_NS;
3538                 }
3539                 target_el = 2;
3540             }
3541             take_exc = true;
3542         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3543             /*
3544              * Synchronous external aborts during a translation table walk
3545              * are taken as Data Abort exceptions.
3546              */
3547             if (fi.stage2) {
3548                 if (current_el == 3) {
3549                     target_el = 3;
3550                 } else {
3551                     target_el = 2;
3552                 }
3553             } else {
3554                 target_el = exception_target_el(env);
3555             }
3556             take_exc = true;
3557         }
3558 
3559         if (take_exc) {
3560             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3561             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3562                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3563                 fsr = arm_fi_to_lfsc(&fi);
3564                 fsc = extract32(fsr, 0, 6);
3565             } else {
3566                 fsr = arm_fi_to_sfsc(&fi);
3567                 fsc = 0x3f;
3568             }
3569             /*
3570              * Report exception with ESR indicating a fault due to a
3571              * translation table walk for a cache maintenance instruction.
3572              */
3573             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3574                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3575             env->exception.vaddress = value;
3576             env->exception.fsr = fsr;
3577             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3578         }
3579     }
3580 
3581     if (is_a64(env)) {
3582         format64 = true;
3583     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3584         /*
3585          * ATS1Cxx:
3586          * * TTBCR.EAE determines whether the result is returned using the
3587          *   32-bit or the 64-bit PAR format
3588          * * Instructions executed in Hyp mode always use the 64bit format
3589          *
3590          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3591          * * The Non-secure TTBCR.EAE bit is set to 1
3592          * * The implementation includes EL2, and the value of HCR.VM is 1
3593          *
3594          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3595          *
3596          * ATS1Hx always uses the 64bit format.
3597          */
3598         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3599 
3600         if (arm_feature(env, ARM_FEATURE_EL2)) {
3601             if (mmu_idx == ARMMMUIdx_E10_0 ||
3602                 mmu_idx == ARMMMUIdx_E10_1 ||
3603                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3604                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3605             } else {
3606                 format64 |= arm_current_el(env) == 2;
3607             }
3608         }
3609     }
3610 
3611     if (format64) {
3612         /* Create a 64-bit PAR */
3613         par64 = (1 << 11); /* LPAE bit always set */
3614         if (!ret) {
3615             par64 |= res.f.phys_addr & ~0xfffULL;
3616             if (!res.f.attrs.secure) {
3617                 par64 |= (1 << 9); /* NS */
3618             }
3619             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3620             par64 |= par_el1_shareability(&res) << 7; /* SH */
3621         } else {
3622             uint32_t fsr = arm_fi_to_lfsc(&fi);
3623 
3624             par64 |= 1; /* F */
3625             par64 |= (fsr & 0x3f) << 1; /* FS */
3626             if (fi.stage2) {
3627                 par64 |= (1 << 9); /* S */
3628             }
3629             if (fi.s1ptw) {
3630                 par64 |= (1 << 8); /* PTW */
3631             }
3632         }
3633     } else {
3634         /*
3635          * fsr is a DFSR/IFSR value for the short descriptor
3636          * translation table format (with WnR always clear).
3637          * Convert it to a 32-bit PAR.
3638          */
3639         if (!ret) {
3640             /* We do not set any attribute bits in the PAR */
3641             if (res.f.lg_page_size == 24
3642                 && arm_feature(env, ARM_FEATURE_V7)) {
3643                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3644             } else {
3645                 par64 = res.f.phys_addr & 0xfffff000;
3646             }
3647             if (!res.f.attrs.secure) {
3648                 par64 |= (1 << 9); /* NS */
3649             }
3650         } else {
3651             uint32_t fsr = arm_fi_to_sfsc(&fi);
3652 
3653             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3654                     ((fsr & 0xf) << 1) | 1;
3655         }
3656     }
3657     return par64;
3658 }
3659 #endif /* CONFIG_TCG */
3660 
3661 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3662 {
3663 #ifdef CONFIG_TCG
3664     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3665     uint64_t par64;
3666     ARMMMUIdx mmu_idx;
3667     int el = arm_current_el(env);
3668     ARMSecuritySpace ss = arm_security_space(env);
3669 
3670     switch (ri->opc2 & 6) {
3671     case 0:
3672         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3673         switch (el) {
3674         case 3:
3675             if (ri->crm == 9 && arm_pan_enabled(env)) {
3676                 mmu_idx = ARMMMUIdx_E30_3_PAN;
3677             } else {
3678                 mmu_idx = ARMMMUIdx_E3;
3679             }
3680             break;
3681         case 2:
3682             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3683             /* fall through */
3684         case 1:
3685             if (ri->crm == 9 && arm_pan_enabled(env)) {
3686                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3687             } else {
3688                 mmu_idx = ARMMMUIdx_Stage1_E1;
3689             }
3690             break;
3691         default:
3692             g_assert_not_reached();
3693         }
3694         break;
3695     case 2:
3696         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3697         switch (el) {
3698         case 3:
3699             mmu_idx = ARMMMUIdx_E30_0;
3700             break;
3701         case 2:
3702             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3703             mmu_idx = ARMMMUIdx_Stage1_E0;
3704             break;
3705         case 1:
3706             mmu_idx = ARMMMUIdx_Stage1_E0;
3707             break;
3708         default:
3709             g_assert_not_reached();
3710         }
3711         break;
3712     case 4:
3713         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3714         mmu_idx = ARMMMUIdx_E10_1;
3715         ss = ARMSS_NonSecure;
3716         break;
3717     case 6:
3718         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3719         mmu_idx = ARMMMUIdx_E10_0;
3720         ss = ARMSS_NonSecure;
3721         break;
3722     default:
3723         g_assert_not_reached();
3724     }
3725 
3726     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3727 
3728     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3729 #else
3730     /* Handled by hardware accelerator. */
3731     g_assert_not_reached();
3732 #endif /* CONFIG_TCG */
3733 }
3734 
3735 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3736                         uint64_t value)
3737 {
3738 #ifdef CONFIG_TCG
3739     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3740     uint64_t par64;
3741 
3742     /* There is no SecureEL2 for AArch32. */
3743     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3744                          ARMSS_NonSecure);
3745 
3746     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3747 #else
3748     /* Handled by hardware accelerator. */
3749     g_assert_not_reached();
3750 #endif /* CONFIG_TCG */
3751 }
3752 
3753 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3754                                      bool isread)
3755 {
3756     /*
3757      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3758      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3759      * only happen when executing at EL3 because that combination also causes an
3760      * illegal exception return. We don't need to check FEAT_RME either, because
3761      * scr_write() ensures that the NSE bit is not set otherwise.
3762      */
3763     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3764         return CP_ACCESS_UNDEFINED;
3765     }
3766     return CP_ACCESS_OK;
3767 }
3768 
3769 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3770                                      bool isread)
3771 {
3772     if (arm_current_el(env) == 3 &&
3773         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3774         return CP_ACCESS_UNDEFINED;
3775     }
3776     return at_e012_access(env, ri, isread);
3777 }
3778 
3779 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3780                                       bool isread)
3781 {
3782     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3783         return CP_ACCESS_TRAP_EL2;
3784     }
3785     return at_e012_access(env, ri, isread);
3786 }
3787 
3788 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3789                         uint64_t value)
3790 {
3791 #ifdef CONFIG_TCG
3792     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3793     ARMMMUIdx mmu_idx;
3794     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3795     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3796     bool for_el3 = false;
3797     ARMSecuritySpace ss;
3798 
3799     switch (ri->opc2 & 6) {
3800     case 0:
3801         switch (ri->opc1) {
3802         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3803             if (ri->crm == 9 && arm_pan_enabled(env)) {
3804                 mmu_idx = regime_e20 ?
3805                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3806             } else {
3807                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3808             }
3809             break;
3810         case 4: /* AT S1E2R, AT S1E2W */
3811             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3812             break;
3813         case 6: /* AT S1E3R, AT S1E3W */
3814             mmu_idx = ARMMMUIdx_E3;
3815             for_el3 = true;
3816             break;
3817         default:
3818             g_assert_not_reached();
3819         }
3820         break;
3821     case 2: /* AT S1E0R, AT S1E0W */
3822         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3823         break;
3824     case 4: /* AT S12E1R, AT S12E1W */
3825         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3826         break;
3827     case 6: /* AT S12E0R, AT S12E0W */
3828         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3829         break;
3830     default:
3831         g_assert_not_reached();
3832     }
3833 
3834     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3835     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3836 #else
3837     /* Handled by hardware accelerator. */
3838     g_assert_not_reached();
3839 #endif /* CONFIG_TCG */
3840 }
3841 #endif
3842 
3843 /* Return basic MPU access permission bits.  */
3844 static uint32_t simple_mpu_ap_bits(uint32_t val)
3845 {
3846     uint32_t ret;
3847     uint32_t mask;
3848     int i;
3849     ret = 0;
3850     mask = 3;
3851     for (i = 0; i < 16; i += 2) {
3852         ret |= (val >> i) & mask;
3853         mask <<= 2;
3854     }
3855     return ret;
3856 }
3857 
3858 /* Pad basic MPU access permission bits to extended format.  */
3859 static uint32_t extended_mpu_ap_bits(uint32_t val)
3860 {
3861     uint32_t ret;
3862     uint32_t mask;
3863     int i;
3864     ret = 0;
3865     mask = 3;
3866     for (i = 0; i < 16; i += 2) {
3867         ret |= (val & mask) << i;
3868         mask <<= 2;
3869     }
3870     return ret;
3871 }
3872 
3873 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3874                                  uint64_t value)
3875 {
3876     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3877 }
3878 
3879 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3880 {
3881     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3882 }
3883 
3884 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3885                                  uint64_t value)
3886 {
3887     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3888 }
3889 
3890 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3891 {
3892     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3893 }
3894 
3895 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3896 {
3897     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3898 
3899     if (!u32p) {
3900         return 0;
3901     }
3902 
3903     u32p += env->pmsav7.rnr[M_REG_NS];
3904     return *u32p;
3905 }
3906 
3907 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3908                          uint64_t value)
3909 {
3910     ARMCPU *cpu = env_archcpu(env);
3911     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3912 
3913     if (!u32p) {
3914         return;
3915     }
3916 
3917     u32p += env->pmsav7.rnr[M_REG_NS];
3918     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3919     *u32p = value;
3920 }
3921 
3922 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3923                               uint64_t value)
3924 {
3925     ARMCPU *cpu = env_archcpu(env);
3926     uint32_t nrgs = cpu->pmsav7_dregion;
3927 
3928     if (value >= nrgs) {
3929         qemu_log_mask(LOG_GUEST_ERROR,
3930                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3931                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3932         return;
3933     }
3934 
3935     raw_write(env, ri, value);
3936 }
3937 
3938 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3939                           uint64_t value)
3940 {
3941     ARMCPU *cpu = env_archcpu(env);
3942 
3943     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3944     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3945 }
3946 
3947 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3948 {
3949     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3950 }
3951 
3952 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3953                           uint64_t value)
3954 {
3955     ARMCPU *cpu = env_archcpu(env);
3956 
3957     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3958     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3959 }
3960 
3961 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3962 {
3963     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3964 }
3965 
3966 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3967                            uint64_t value)
3968 {
3969     ARMCPU *cpu = env_archcpu(env);
3970 
3971     /*
3972      * Ignore writes that would select not implemented region.
3973      * This is architecturally UNPREDICTABLE.
3974      */
3975     if (value >= cpu->pmsav7_dregion) {
3976         return;
3977     }
3978 
3979     env->pmsav7.rnr[M_REG_NS] = value;
3980 }
3981 
3982 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3983                           uint64_t value)
3984 {
3985     ARMCPU *cpu = env_archcpu(env);
3986 
3987     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3988     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3989 }
3990 
3991 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3992 {
3993     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3994 }
3995 
3996 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3997                           uint64_t value)
3998 {
3999     ARMCPU *cpu = env_archcpu(env);
4000 
4001     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4002     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4003 }
4004 
4005 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4006 {
4007     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4008 }
4009 
4010 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4011                           uint64_t value)
4012 {
4013     uint32_t n;
4014     uint32_t bit;
4015     ARMCPU *cpu = env_archcpu(env);
4016 
4017     /* Ignore writes to unimplemented regions */
4018     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4019     value &= MAKE_64BIT_MASK(0, rmax);
4020 
4021     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4022 
4023     /* Register alias is only valid for first 32 indexes */
4024     for (n = 0; n < rmax; ++n) {
4025         bit = extract32(value, n, 1);
4026         env->pmsav8.hprlar[n] = deposit32(
4027                     env->pmsav8.hprlar[n], 0, 1, bit);
4028     }
4029 }
4030 
4031 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4032 {
4033     uint32_t n;
4034     uint32_t result = 0x0;
4035     ARMCPU *cpu = env_archcpu(env);
4036 
4037     /* Register alias is only valid for first 32 indexes */
4038     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4039         if (env->pmsav8.hprlar[n] & 0x1) {
4040             result |= (0x1 << n);
4041         }
4042     }
4043     return result;
4044 }
4045 
4046 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4047                            uint64_t value)
4048 {
4049     ARMCPU *cpu = env_archcpu(env);
4050 
4051     /*
4052      * Ignore writes that would select not implemented region.
4053      * This is architecturally UNPREDICTABLE.
4054      */
4055     if (value >= cpu->pmsav8r_hdregion) {
4056         return;
4057     }
4058 
4059     env->pmsav8.hprselr = value;
4060 }
4061 
4062 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4063                           uint64_t value)
4064 {
4065     ARMCPU *cpu = env_archcpu(env);
4066     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4067                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4068 
4069     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4070 
4071     if (ri->opc1 & 4) {
4072         if (index >= cpu->pmsav8r_hdregion) {
4073             return;
4074         }
4075         if (ri->opc2 & 0x1) {
4076             env->pmsav8.hprlar[index] = value;
4077         } else {
4078             env->pmsav8.hprbar[index] = value;
4079         }
4080     } else {
4081         if (index >= cpu->pmsav7_dregion) {
4082             return;
4083         }
4084         if (ri->opc2 & 0x1) {
4085             env->pmsav8.rlar[M_REG_NS][index] = value;
4086         } else {
4087             env->pmsav8.rbar[M_REG_NS][index] = value;
4088         }
4089     }
4090 }
4091 
4092 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4093 {
4094     ARMCPU *cpu = env_archcpu(env);
4095     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4096                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4097 
4098     if (ri->opc1 & 4) {
4099         if (index >= cpu->pmsav8r_hdregion) {
4100             return 0x0;
4101         }
4102         if (ri->opc2 & 0x1) {
4103             return env->pmsav8.hprlar[index];
4104         } else {
4105             return env->pmsav8.hprbar[index];
4106         }
4107     } else {
4108         if (index >= cpu->pmsav7_dregion) {
4109             return 0x0;
4110         }
4111         if (ri->opc2 & 0x1) {
4112             return env->pmsav8.rlar[M_REG_NS][index];
4113         } else {
4114             return env->pmsav8.rbar[M_REG_NS][index];
4115         }
4116     }
4117 }
4118 
4119 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4120     { .name = "PRBAR",
4121       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4122       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4123       .accessfn = access_tvm_trvm,
4124       .readfn = prbar_read, .writefn = prbar_write },
4125     { .name = "PRLAR",
4126       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4127       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4128       .accessfn = access_tvm_trvm,
4129       .readfn = prlar_read, .writefn = prlar_write },
4130     { .name = "PRSELR", .resetvalue = 0,
4131       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4132       .access = PL1_RW, .accessfn = access_tvm_trvm,
4133       .writefn = prselr_write,
4134       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4135     { .name = "HPRBAR", .resetvalue = 0,
4136       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4137       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4138       .readfn = hprbar_read, .writefn = hprbar_write },
4139     { .name = "HPRLAR",
4140       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4141       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4142       .readfn = hprlar_read, .writefn = hprlar_write },
4143     { .name = "HPRSELR", .resetvalue = 0,
4144       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4145       .access = PL2_RW,
4146       .writefn = hprselr_write,
4147       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4148     { .name = "HPRENR",
4149       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4150       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4151       .readfn = hprenr_read, .writefn = hprenr_write },
4152 };
4153 
4154 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4155     /*
4156      * Reset for all these registers is handled in arm_cpu_reset(),
4157      * because the PMSAv7 is also used by M-profile CPUs, which do
4158      * not register cpregs but still need the state to be reset.
4159      */
4160     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4161       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4162       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4163       .readfn = pmsav7_read, .writefn = pmsav7_write,
4164       .resetfn = arm_cp_reset_ignore },
4165     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4166       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4167       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4168       .readfn = pmsav7_read, .writefn = pmsav7_write,
4169       .resetfn = arm_cp_reset_ignore },
4170     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4171       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4172       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4173       .readfn = pmsav7_read, .writefn = pmsav7_write,
4174       .resetfn = arm_cp_reset_ignore },
4175     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4176       .access = PL1_RW,
4177       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4178       .writefn = pmsav7_rgnr_write,
4179       .resetfn = arm_cp_reset_ignore },
4180 };
4181 
4182 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4183     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4184       .access = PL1_RW, .type = ARM_CP_ALIAS,
4185       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4186       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4187     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4188       .access = PL1_RW, .type = ARM_CP_ALIAS,
4189       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4190       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4191     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4192       .access = PL1_RW,
4193       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4194       .resetvalue = 0, },
4195     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4196       .access = PL1_RW,
4197       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4198       .resetvalue = 0, },
4199     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4200       .access = PL1_RW,
4201       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4202     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4203       .access = PL1_RW,
4204       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4205     /* Protection region base and size registers */
4206     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4207       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4208       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4209     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4210       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4211       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4212     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4213       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4214       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4215     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4216       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4217       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4218     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4219       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4220       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4221     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4222       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4223       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4224     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4225       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4226       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4227     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4228       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4229       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4230 };
4231 
4232 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4233                              uint64_t value)
4234 {
4235     ARMCPU *cpu = env_archcpu(env);
4236 
4237     if (!arm_feature(env, ARM_FEATURE_V8)) {
4238         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4239             /*
4240              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4241              * using Long-descriptor translation table format
4242              */
4243             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4244         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4245             /*
4246              * In an implementation that includes the Security Extensions
4247              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4248              * Short-descriptor translation table format.
4249              */
4250             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4251         } else {
4252             value &= TTBCR_N;
4253         }
4254     }
4255 
4256     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4257         /*
4258          * With LPAE the TTBCR could result in a change of ASID
4259          * via the TTBCR.A1 bit, so do a TLB flush.
4260          */
4261         tlb_flush(CPU(cpu));
4262     }
4263     raw_write(env, ri, value);
4264 }
4265 
4266 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4267                                uint64_t value)
4268 {
4269     ARMCPU *cpu = env_archcpu(env);
4270 
4271     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4272     tlb_flush(CPU(cpu));
4273     raw_write(env, ri, value);
4274 }
4275 
4276 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4277                             uint64_t value)
4278 {
4279     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4280     if (cpreg_field_is_64bit(ri) &&
4281         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4282         ARMCPU *cpu = env_archcpu(env);
4283         tlb_flush(CPU(cpu));
4284     }
4285     raw_write(env, ri, value);
4286 }
4287 
4288 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4289                                     uint64_t value)
4290 {
4291     /*
4292      * If we are running with E2&0 regime, then an ASID is active.
4293      * Flush if that might be changing.  Note we're not checking
4294      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4295      * holds the active ASID, only checking the field that might.
4296      */
4297     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4298         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4299         uint16_t mask = ARMMMUIdxBit_E20_2 |
4300                         ARMMMUIdxBit_E20_2_PAN |
4301                         ARMMMUIdxBit_E20_0;
4302         tlb_flush_by_mmuidx(env_cpu(env), mask);
4303     }
4304     raw_write(env, ri, value);
4305 }
4306 
4307 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4308                         uint64_t value)
4309 {
4310     ARMCPU *cpu = env_archcpu(env);
4311     CPUState *cs = CPU(cpu);
4312 
4313     /*
4314      * A change in VMID to the stage2 page table (Stage2) invalidates
4315      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4316      */
4317     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4318         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4319     }
4320     raw_write(env, ri, value);
4321 }
4322 
4323 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4324     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4325       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4326       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4327                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4328     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4329       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4330       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4331                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4332     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4333       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4334       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4335                              offsetof(CPUARMState, cp15.dfar_ns) } },
4336     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4337       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4338       .access = PL1_RW, .accessfn = access_tvm_trvm,
4339       .fgt = FGT_FAR_EL1,
4340       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4341       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4342       .resetvalue = 0, },
4343 };
4344 
4345 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4346     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4347       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4348       .access = PL1_RW, .accessfn = access_tvm_trvm,
4349       .fgt = FGT_ESR_EL1,
4350       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4351       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4352     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4353       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4354       .access = PL1_RW, .accessfn = access_tvm_trvm,
4355       .fgt = FGT_TTBR0_EL1,
4356       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4357       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4358       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4359                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4360     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4361       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4362       .access = PL1_RW, .accessfn = access_tvm_trvm,
4363       .fgt = FGT_TTBR1_EL1,
4364       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4365       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4366       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4367                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4368     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4369       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4370       .access = PL1_RW, .accessfn = access_tvm_trvm,
4371       .fgt = FGT_TCR_EL1,
4372       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4373       .writefn = vmsa_tcr_el12_write,
4374       .raw_writefn = raw_write,
4375       .resetvalue = 0,
4376       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4377     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4378       .access = PL1_RW, .accessfn = access_tvm_trvm,
4379       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4380       .raw_writefn = raw_write,
4381       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4382                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4383 };
4384 
4385 /*
4386  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4387  * qemu tlbs nor adjusting cached masks.
4388  */
4389 static const ARMCPRegInfo ttbcr2_reginfo = {
4390     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4391     .access = PL1_RW, .accessfn = access_tvm_trvm,
4392     .type = ARM_CP_ALIAS,
4393     .bank_fieldoffsets = {
4394         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4395         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4396     },
4397 };
4398 
4399 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4400                                 uint64_t value)
4401 {
4402     env->cp15.c15_ticonfig = value & 0xe7;
4403     /* The OS_TYPE bit in this register changes the reported CPUID! */
4404     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4405         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4406 }
4407 
4408 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4409                                 uint64_t value)
4410 {
4411     env->cp15.c15_threadid = value & 0xffff;
4412 }
4413 
4414 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4415                            uint64_t value)
4416 {
4417     /* Wait-for-interrupt (deprecated) */
4418     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4419 }
4420 
4421 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4422                                   uint64_t value)
4423 {
4424     /*
4425      * On OMAP there are registers indicating the max/min index of dcache lines
4426      * containing a dirty line; cache flush operations have to reset these.
4427      */
4428     env->cp15.c15_i_max = 0x000;
4429     env->cp15.c15_i_min = 0xff0;
4430 }
4431 
4432 static const ARMCPRegInfo omap_cp_reginfo[] = {
4433     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4434       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4435       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4436       .resetvalue = 0, },
4437     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4438       .access = PL1_RW, .type = ARM_CP_NOP },
4439     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4440       .access = PL1_RW,
4441       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4442       .writefn = omap_ticonfig_write },
4443     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4444       .access = PL1_RW,
4445       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4446     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4447       .access = PL1_RW, .resetvalue = 0xff0,
4448       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4449     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4450       .access = PL1_RW,
4451       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4452       .writefn = omap_threadid_write },
4453     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4454       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4455       .type = ARM_CP_NO_RAW,
4456       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4457     /*
4458      * TODO: Peripheral port remap register:
4459      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4460      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4461      * when MMU is off.
4462      */
4463     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4464       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4465       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4466       .writefn = omap_cachemaint_write },
4467     { .name = "C9", .cp = 15, .crn = 9,
4468       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4469       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4470 };
4471 
4472 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4473                               uint64_t value)
4474 {
4475     env->cp15.c15_cpar = value & 0x3fff;
4476 }
4477 
4478 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4479     { .name = "XSCALE_CPAR",
4480       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4481       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4482       .writefn = xscale_cpar_write, },
4483     { .name = "XSCALE_AUXCR",
4484       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4485       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4486       .resetvalue = 0, },
4487     /*
4488      * XScale specific cache-lockdown: since we have no cache we NOP these
4489      * and hope the guest does not really rely on cache behaviour.
4490      */
4491     { .name = "XSCALE_LOCK_ICACHE_LINE",
4492       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4493       .access = PL1_W, .type = ARM_CP_NOP },
4494     { .name = "XSCALE_UNLOCK_ICACHE",
4495       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4496       .access = PL1_W, .type = ARM_CP_NOP },
4497     { .name = "XSCALE_DCACHE_LOCK",
4498       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4499       .access = PL1_RW, .type = ARM_CP_NOP },
4500     { .name = "XSCALE_UNLOCK_DCACHE",
4501       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4502       .access = PL1_W, .type = ARM_CP_NOP },
4503 };
4504 
4505 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4506     /*
4507      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4508      * implementation of this implementation-defined space.
4509      * Ideally this should eventually disappear in favour of actually
4510      * implementing the correct behaviour for all cores.
4511      */
4512     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4513       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4514       .access = PL1_RW,
4515       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4516       .resetvalue = 0 },
4517 };
4518 
4519 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4520     /* Cache status: RAZ because we have no cache so it's always clean */
4521     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4522       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4523       .resetvalue = 0 },
4524 };
4525 
4526 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4527     /* We never have a block transfer operation in progress */
4528     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4529       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4530       .resetvalue = 0 },
4531     /* The cache ops themselves: these all NOP for QEMU */
4532     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4533       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4534     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4535       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4536     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4537       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4538     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4539       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4540     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4541       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4542     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4543       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4544 };
4545 
4546 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4547     /*
4548      * The cache test-and-clean instructions always return (1 << 30)
4549      * to indicate that there are no dirty cache lines.
4550      */
4551     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4552       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4553       .resetvalue = (1 << 30) },
4554     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4555       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4556       .resetvalue = (1 << 30) },
4557 };
4558 
4559 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4560     /* Ignore ReadBuffer accesses */
4561     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4562       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4563       .access = PL1_RW, .resetvalue = 0,
4564       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4565 };
4566 
4567 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4568 {
4569     unsigned int cur_el = arm_current_el(env);
4570 
4571     if (arm_is_el2_enabled(env) && cur_el == 1) {
4572         return env->cp15.vpidr_el2;
4573     }
4574     return raw_read(env, ri);
4575 }
4576 
4577 static uint64_t mpidr_read_val(CPUARMState *env)
4578 {
4579     ARMCPU *cpu = env_archcpu(env);
4580     uint64_t mpidr = cpu->mp_affinity;
4581 
4582     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4583         mpidr |= (1U << 31);
4584         /*
4585          * Cores which are uniprocessor (non-coherent)
4586          * but still implement the MP extensions set
4587          * bit 30. (For instance, Cortex-R5).
4588          */
4589         if (cpu->mp_is_up) {
4590             mpidr |= (1u << 30);
4591         }
4592     }
4593     return mpidr;
4594 }
4595 
4596 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4597 {
4598     unsigned int cur_el = arm_current_el(env);
4599 
4600     if (arm_is_el2_enabled(env) && cur_el == 1) {
4601         return env->cp15.vmpidr_el2;
4602     }
4603     return mpidr_read_val(env);
4604 }
4605 
4606 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4607     /* NOP AMAIR0/1 */
4608     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4609       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4610       .access = PL1_RW, .accessfn = access_tvm_trvm,
4611       .fgt = FGT_AMAIR_EL1,
4612       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4613       .type = ARM_CP_CONST, .resetvalue = 0 },
4614     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4615     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4616       .access = PL1_RW, .accessfn = access_tvm_trvm,
4617       .type = ARM_CP_CONST, .resetvalue = 0 },
4618     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4619       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4620       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4621                              offsetof(CPUARMState, cp15.par_ns)} },
4622     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4623       .access = PL1_RW, .accessfn = access_tvm_trvm,
4624       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4625       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4626                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4627       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4628     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4629       .access = PL1_RW, .accessfn = access_tvm_trvm,
4630       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4631       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4632                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4633       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4634 };
4635 
4636 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4637 {
4638     return vfp_get_fpcr(env);
4639 }
4640 
4641 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4642                             uint64_t value)
4643 {
4644     vfp_set_fpcr(env, value);
4645 }
4646 
4647 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4648 {
4649     return vfp_get_fpsr(env);
4650 }
4651 
4652 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4653                             uint64_t value)
4654 {
4655     vfp_set_fpsr(env, value);
4656 }
4657 
4658 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4659                                        bool isread)
4660 {
4661     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4662         return CP_ACCESS_TRAP_EL1;
4663     }
4664     return CP_ACCESS_OK;
4665 }
4666 
4667 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4668                             uint64_t value)
4669 {
4670     env->daif = value & PSTATE_DAIF;
4671 }
4672 
4673 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4674 {
4675     return env->pstate & PSTATE_PAN;
4676 }
4677 
4678 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4679                            uint64_t value)
4680 {
4681     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4682 }
4683 
4684 static const ARMCPRegInfo pan_reginfo = {
4685     .name = "PAN", .state = ARM_CP_STATE_AA64,
4686     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4687     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4688     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4689 };
4690 
4691 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4692 {
4693     return env->pstate & PSTATE_UAO;
4694 }
4695 
4696 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4697                            uint64_t value)
4698 {
4699     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4700 }
4701 
4702 static const ARMCPRegInfo uao_reginfo = {
4703     .name = "UAO", .state = ARM_CP_STATE_AA64,
4704     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4705     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4706     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4707 };
4708 
4709 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4710 {
4711     return env->pstate & PSTATE_DIT;
4712 }
4713 
4714 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4715                            uint64_t value)
4716 {
4717     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4718 }
4719 
4720 static const ARMCPRegInfo dit_reginfo = {
4721     .name = "DIT", .state = ARM_CP_STATE_AA64,
4722     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4723     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4724     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4725 };
4726 
4727 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4728 {
4729     return env->pstate & PSTATE_SSBS;
4730 }
4731 
4732 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4733                            uint64_t value)
4734 {
4735     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4736 }
4737 
4738 static const ARMCPRegInfo ssbs_reginfo = {
4739     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4740     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4741     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4742     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4743 };
4744 
4745 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4746                                               const ARMCPRegInfo *ri,
4747                                               bool isread)
4748 {
4749     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4750     switch (arm_current_el(env)) {
4751     case 0:
4752         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4753         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4754             return CP_ACCESS_TRAP_EL1;
4755         }
4756         /* fall through */
4757     case 1:
4758         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4759         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4760             return CP_ACCESS_TRAP_EL2;
4761         }
4762         break;
4763     }
4764     return CP_ACCESS_OK;
4765 }
4766 
4767 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4768 {
4769     /* Cache invalidate/clean to Point of Unification... */
4770     switch (arm_current_el(env)) {
4771     case 0:
4772         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4773         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4774             return CP_ACCESS_TRAP_EL1;
4775         }
4776         /* fall through */
4777     case 1:
4778         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4779         if (arm_hcr_el2_eff(env) & hcrflags) {
4780             return CP_ACCESS_TRAP_EL2;
4781         }
4782         break;
4783     }
4784     return CP_ACCESS_OK;
4785 }
4786 
4787 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4788                                    bool isread)
4789 {
4790     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4791 }
4792 
4793 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4794                                   bool isread)
4795 {
4796     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4797 }
4798 
4799 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4800                                       bool isread)
4801 {
4802     int cur_el = arm_current_el(env);
4803 
4804     if (cur_el < 2) {
4805         uint64_t hcr = arm_hcr_el2_eff(env);
4806 
4807         if (cur_el == 0) {
4808             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4809                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4810                     return CP_ACCESS_TRAP_EL2;
4811                 }
4812             } else {
4813                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4814                     return CP_ACCESS_TRAP_EL1;
4815                 }
4816                 if (hcr & HCR_TDZ) {
4817                     return CP_ACCESS_TRAP_EL2;
4818                 }
4819             }
4820         } else if (hcr & HCR_TDZ) {
4821             return CP_ACCESS_TRAP_EL2;
4822         }
4823     }
4824     return CP_ACCESS_OK;
4825 }
4826 
4827 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4828 {
4829     ARMCPU *cpu = env_archcpu(env);
4830     int dzp_bit = 1 << 4;
4831 
4832     /* DZP indicates whether DC ZVA access is allowed */
4833     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4834         dzp_bit = 0;
4835     }
4836     return cpu->dcz_blocksize | dzp_bit;
4837 }
4838 
4839 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4840                                     bool isread)
4841 {
4842     if (!(env->pstate & PSTATE_SP)) {
4843         /*
4844          * Access to SP_EL0 is undefined if it's being used as
4845          * the stack pointer.
4846          */
4847         return CP_ACCESS_UNDEFINED;
4848     }
4849     return CP_ACCESS_OK;
4850 }
4851 
4852 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4853 {
4854     return env->pstate & PSTATE_SP;
4855 }
4856 
4857 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4858 {
4859     update_spsel(env, val);
4860 }
4861 
4862 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4863                         uint64_t value)
4864 {
4865     ARMCPU *cpu = env_archcpu(env);
4866 
4867     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4868         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4869         value &= ~SCTLR_M;
4870     }
4871 
4872     /* ??? Lots of these bits are not implemented.  */
4873 
4874     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4875         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4876             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4877         } else {
4878             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4879                        SCTLR_ATA0 | SCTLR_ATA);
4880         }
4881     }
4882 
4883     if (raw_read(env, ri) == value) {
4884         /*
4885          * Skip the TLB flush if nothing actually changed; Linux likes
4886          * to do a lot of pointless SCTLR writes.
4887          */
4888         return;
4889     }
4890 
4891     raw_write(env, ri, value);
4892 
4893     /* This may enable/disable the MMU, so do a TLB flush.  */
4894     tlb_flush(CPU(cpu));
4895 
4896     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4897         /*
4898          * Normally we would always end the TB on an SCTLR write; see the
4899          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4900          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4901          * of hflags from the translator, so do it here.
4902          */
4903         arm_rebuild_hflags(env);
4904     }
4905 }
4906 
4907 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4908                            uint64_t value)
4909 {
4910     /*
4911      * Some MDCR_EL3 bits affect whether PMU counters are running:
4912      * if we are trying to change any of those then we must
4913      * bracket this update with PMU start/finish calls.
4914      */
4915     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4916 
4917     if (pmu_op) {
4918         pmu_op_start(env);
4919     }
4920     env->cp15.mdcr_el3 = value;
4921     if (pmu_op) {
4922         pmu_op_finish(env);
4923     }
4924 }
4925 
4926 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4927                        uint64_t value)
4928 {
4929     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4930     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4931 }
4932 
4933 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4934                            uint64_t value)
4935 {
4936     /*
4937      * Some MDCR_EL2 bits affect whether PMU counters are running:
4938      * if we are trying to change any of those then we must
4939      * bracket this update with PMU start/finish calls.
4940      */
4941     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4942 
4943     if (pmu_op) {
4944         pmu_op_start(env);
4945     }
4946     env->cp15.mdcr_el2 = value;
4947     if (pmu_op) {
4948         pmu_op_finish(env);
4949     }
4950 }
4951 
4952 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4953                                  bool isread)
4954 {
4955     if (arm_current_el(env) == 1) {
4956         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4957 
4958         if (hcr_nv == (HCR_NV | HCR_NV1)) {
4959             return CP_ACCESS_TRAP_EL2;
4960         }
4961     }
4962     return CP_ACCESS_OK;
4963 }
4964 
4965 #ifdef CONFIG_USER_ONLY
4966 /*
4967  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4968  * code to get around W^X restrictions, where one region is writable and the
4969  * other is executable.
4970  *
4971  * Since the executable region is never written to we cannot detect code
4972  * changes when running in user mode, and rely on the emulated JIT telling us
4973  * that the code has changed by executing this instruction.
4974  */
4975 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4976                           uint64_t value)
4977 {
4978     uint64_t icache_line_mask, start_address, end_address;
4979     const ARMCPU *cpu;
4980 
4981     cpu = env_archcpu(env);
4982 
4983     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4984     start_address = value & ~icache_line_mask;
4985     end_address = value | icache_line_mask;
4986 
4987     mmap_lock();
4988 
4989     tb_invalidate_phys_range(start_address, end_address);
4990 
4991     mmap_unlock();
4992 }
4993 #endif
4994 
4995 static const ARMCPRegInfo v8_cp_reginfo[] = {
4996     /*
4997      * Minimal set of EL0-visible registers. This will need to be expanded
4998      * significantly for system emulation of AArch64 CPUs.
4999      */
5000     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5001       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5002       .access = PL0_RW, .type = ARM_CP_NZCV },
5003     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5004       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5005       .type = ARM_CP_NO_RAW,
5006       .access = PL0_RW, .accessfn = aa64_daif_access,
5007       .fieldoffset = offsetof(CPUARMState, daif),
5008       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5009     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5010       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5011       .access = PL0_RW, .type = ARM_CP_FPU,
5012       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5013     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5014       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5015       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5016       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5017     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5018       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5019       .access = PL0_R, .type = ARM_CP_NO_RAW,
5020       .fgt = FGT_DCZID_EL0,
5021       .readfn = aa64_dczid_read },
5022     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5023       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5024       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5025 #ifndef CONFIG_USER_ONLY
5026       /* Avoid overhead of an access check that always passes in user-mode */
5027       .accessfn = aa64_zva_access,
5028       .fgt = FGT_DCZVA,
5029 #endif
5030     },
5031     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5032       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5033       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5034     /*
5035      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5036      * don't emulate caches.
5037      */
5038     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5039       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5040       .access = PL1_W, .type = ARM_CP_NOP,
5041       .fgt = FGT_ICIALLUIS,
5042       .accessfn = access_ticab },
5043     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5044       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5045       .access = PL1_W, .type = ARM_CP_NOP,
5046       .fgt = FGT_ICIALLU,
5047       .accessfn = access_tocu },
5048     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5049       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5050       .access = PL0_W,
5051       .fgt = FGT_ICIVAU,
5052       .accessfn = access_tocu,
5053 #ifdef CONFIG_USER_ONLY
5054       .type = ARM_CP_NO_RAW,
5055       .writefn = ic_ivau_write
5056 #else
5057       .type = ARM_CP_NOP
5058 #endif
5059     },
5060     /* Cache ops: all NOPs since we don't emulate caches */
5061     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5062       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5063       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5064       .fgt = FGT_DCIVAC,
5065       .type = ARM_CP_NOP },
5066     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5067       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5068       .fgt = FGT_DCISW,
5069       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5070     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5071       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5072       .access = PL0_W, .type = ARM_CP_NOP,
5073       .fgt = FGT_DCCVAC,
5074       .accessfn = aa64_cacheop_poc_access },
5075     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5076       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5077       .fgt = FGT_DCCSW,
5078       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5079     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5080       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5081       .access = PL0_W, .type = ARM_CP_NOP,
5082       .fgt = FGT_DCCVAU,
5083       .accessfn = access_tocu },
5084     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5085       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5086       .access = PL0_W, .type = ARM_CP_NOP,
5087       .fgt = FGT_DCCIVAC,
5088       .accessfn = aa64_cacheop_poc_access },
5089     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5090       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5091       .fgt = FGT_DCCISW,
5092       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5093 #ifndef CONFIG_USER_ONLY
5094     /* 64 bit address translation operations */
5095     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5096       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5097       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5098       .fgt = FGT_ATS1E1R,
5099       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5100     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5101       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5102       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5103       .fgt = FGT_ATS1E1W,
5104       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5105     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5106       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5107       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5108       .fgt = FGT_ATS1E0R,
5109       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5110     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5111       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5112       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5113       .fgt = FGT_ATS1E0W,
5114       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5115     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5116       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5117       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5118       .accessfn = at_e012_access, .writefn = ats_write64 },
5119     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5120       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5121       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5122       .accessfn = at_e012_access, .writefn = ats_write64 },
5123     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5124       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5125       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5126       .accessfn = at_e012_access, .writefn = ats_write64 },
5127     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5128       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5129       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5130       .accessfn = at_e012_access, .writefn = ats_write64 },
5131     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5132     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5133       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5134       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5135       .writefn = ats_write64 },
5136     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5137       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5138       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5139       .writefn = ats_write64 },
5140     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5141       .type = ARM_CP_ALIAS,
5142       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5143       .access = PL1_RW, .resetvalue = 0,
5144       .fgt = FGT_PAR_EL1,
5145       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5146       .writefn = par_write },
5147 #endif
5148     /* 32 bit cache operations */
5149     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5150       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5151     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5152       .type = ARM_CP_NOP, .access = PL1_W },
5153     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5154       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5155     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5156       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5157     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5158       .type = ARM_CP_NOP, .access = PL1_W },
5159     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5160       .type = ARM_CP_NOP, .access = PL1_W },
5161     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5162       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5163     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5164       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5165     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5166       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5167     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5168       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5169     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5170       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5171     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5172       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5173     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5174       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5175     /* MMU Domain access control / MPU write buffer control */
5176     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5177       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5178       .writefn = dacr_write, .raw_writefn = raw_write,
5179       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5180                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5181     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5182       .type = ARM_CP_ALIAS,
5183       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5184       .access = PL1_RW, .accessfn = access_nv1,
5185       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5186       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5187     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5188       .type = ARM_CP_ALIAS,
5189       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5190       .access = PL1_RW, .accessfn = access_nv1,
5191       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5192       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5193     /*
5194      * We rely on the access checks not allowing the guest to write to the
5195      * state field when SPSel indicates that it's being used as the stack
5196      * pointer.
5197      */
5198     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5199       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5200       .access = PL1_RW, .accessfn = sp_el0_access,
5201       .type = ARM_CP_ALIAS,
5202       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5203     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5204       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5205       .nv2_redirect_offset = 0x240,
5206       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5207       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5208     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5209       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5210       .type = ARM_CP_NO_RAW,
5211       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5212     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5213       .type = ARM_CP_ALIAS,
5214       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5215       .access = PL2_RW,
5216       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5217     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5218       .type = ARM_CP_ALIAS,
5219       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5220       .access = PL2_RW,
5221       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5222     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5223       .type = ARM_CP_ALIAS,
5224       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5225       .access = PL2_RW,
5226       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5227     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5228       .type = ARM_CP_ALIAS,
5229       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5230       .access = PL2_RW,
5231       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5232     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5233       .type = ARM_CP_IO,
5234       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5235       .resetvalue = 0,
5236       .access = PL3_RW,
5237       .writefn = mdcr_el3_write,
5238       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5239     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5240       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5241       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5242       .writefn = sdcr_write,
5243       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5244 };
5245 
5246 /* These are present only when EL1 supports AArch32 */
5247 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5248     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5249       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5250       .access = PL2_RW,
5251       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5252       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5253     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5254       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5255       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5256       .writefn = dacr_write, .raw_writefn = raw_write,
5257       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5258     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5259       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5260       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5261       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5262 };
5263 
5264 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5265 {
5266     ARMCPU *cpu = env_archcpu(env);
5267 
5268     if (arm_feature(env, ARM_FEATURE_V8)) {
5269         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5270     } else {
5271         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5272     }
5273 
5274     if (arm_feature(env, ARM_FEATURE_EL3)) {
5275         valid_mask &= ~HCR_HCD;
5276     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5277         /*
5278          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5279          * However, if we're using the SMC PSCI conduit then QEMU is
5280          * effectively acting like EL3 firmware and so the guest at
5281          * EL2 should retain the ability to prevent EL1 from being
5282          * able to make SMC calls into the ersatz firmware, so in
5283          * that case HCR.TSC should be read/write.
5284          */
5285         valid_mask &= ~HCR_TSC;
5286     }
5287 
5288     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5289         if (cpu_isar_feature(aa64_vh, cpu)) {
5290             valid_mask |= HCR_E2H;
5291         }
5292         if (cpu_isar_feature(aa64_ras, cpu)) {
5293             valid_mask |= HCR_TERR | HCR_TEA;
5294         }
5295         if (cpu_isar_feature(aa64_lor, cpu)) {
5296             valid_mask |= HCR_TLOR;
5297         }
5298         if (cpu_isar_feature(aa64_pauth, cpu)) {
5299             valid_mask |= HCR_API | HCR_APK;
5300         }
5301         if (cpu_isar_feature(aa64_mte, cpu)) {
5302             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5303         }
5304         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5305             valid_mask |= HCR_ENSCXT;
5306         }
5307         if (cpu_isar_feature(aa64_fwb, cpu)) {
5308             valid_mask |= HCR_FWB;
5309         }
5310         if (cpu_isar_feature(aa64_rme, cpu)) {
5311             valid_mask |= HCR_GPF;
5312         }
5313         if (cpu_isar_feature(aa64_nv, cpu)) {
5314             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5315         }
5316         if (cpu_isar_feature(aa64_nv2, cpu)) {
5317             valid_mask |= HCR_NV2;
5318         }
5319     }
5320 
5321     if (cpu_isar_feature(any_evt, cpu)) {
5322         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5323     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5324         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5325     }
5326 
5327     /* Clear RES0 bits.  */
5328     value &= valid_mask;
5329 
5330     /* RW is RAO/WI if EL1 is AArch64 only */
5331     if (!cpu_isar_feature(aa64_aa32_el1, cpu)) {
5332         value |= HCR_RW;
5333     }
5334 
5335     /*
5336      * These bits change the MMU setup:
5337      * HCR_VM enables stage 2 translation
5338      * HCR_PTW forbids certain page-table setups
5339      * HCR_DC disables stage1 and enables stage2 translation
5340      * HCR_DCT enables tagging on (disabled) stage1 translation
5341      * HCR_FWB changes the interpretation of stage2 descriptor bits
5342      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5343      */
5344     if ((env->cp15.hcr_el2 ^ value) &
5345         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5346         tlb_flush(CPU(cpu));
5347     }
5348     env->cp15.hcr_el2 = value;
5349 
5350     /*
5351      * Updates to VI and VF require us to update the status of
5352      * virtual interrupts, which are the logical OR of these bits
5353      * and the state of the input lines from the GIC. (This requires
5354      * that we have the BQL, which is done by marking the
5355      * reginfo structs as ARM_CP_IO.)
5356      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5357      * VFNMI, it is never possible for it to be taken immediately
5358      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5359      * at EL0 or EL1, and HCR can only be written at EL2.
5360      */
5361     g_assert(bql_locked());
5362     arm_cpu_update_virq(cpu);
5363     arm_cpu_update_vfiq(cpu);
5364     arm_cpu_update_vserr(cpu);
5365     if (cpu_isar_feature(aa64_nmi, cpu)) {
5366         arm_cpu_update_vinmi(cpu);
5367         arm_cpu_update_vfnmi(cpu);
5368     }
5369 }
5370 
5371 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5372 {
5373     do_hcr_write(env, value, 0);
5374 }
5375 
5376 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5377                           uint64_t value)
5378 {
5379     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5380     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5381     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5382 }
5383 
5384 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5385                          uint64_t value)
5386 {
5387     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5388     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5389     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5390 }
5391 
5392 static void hcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
5393 {
5394     /* hcr_write will set the RES1 bits on an AArch64-only CPU */
5395     hcr_write(env, ri, 0);
5396 }
5397 
5398 /*
5399  * Return the effective value of HCR_EL2, at the given security state.
5400  * Bits that are not included here:
5401  * RW       (read from SCR_EL3.RW as needed)
5402  */
5403 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5404 {
5405     uint64_t ret = env->cp15.hcr_el2;
5406 
5407     assert(space != ARMSS_Root);
5408 
5409     if (!arm_is_el2_enabled_secstate(env, space)) {
5410         /*
5411          * "This register has no effect if EL2 is not enabled in the
5412          * current Security state".  This is ARMv8.4-SecEL2 speak for
5413          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5414          *
5415          * Prior to that, the language was "In an implementation that
5416          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5417          * as if this field is 0 for all purposes other than a direct
5418          * read or write access of HCR_EL2".  With lots of enumeration
5419          * on a per-field basis.  In current QEMU, this is condition
5420          * is arm_is_secure_below_el3.
5421          *
5422          * Since the v8.4 language applies to the entire register, and
5423          * appears to be backward compatible, use that.
5424          */
5425         return 0;
5426     }
5427 
5428     /*
5429      * For a cpu that supports both aarch64 and aarch32, we can set bits
5430      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5431      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5432      */
5433     if (!arm_el_is_aa64(env, 2)) {
5434         uint64_t aa32_valid;
5435 
5436         /*
5437          * These bits are up-to-date as of ARMv8.6.
5438          * For HCR, it's easiest to list just the 2 bits that are invalid.
5439          * For HCR2, list those that are valid.
5440          */
5441         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5442         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5443                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5444         ret &= aa32_valid;
5445     }
5446 
5447     if (ret & HCR_TGE) {
5448         /* These bits are up-to-date as of ARMv8.6.  */
5449         if (ret & HCR_E2H) {
5450             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5451                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5452                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5453                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5454                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5455                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5456         } else {
5457             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5458         }
5459         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5460                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5461                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5462                  HCR_TLOR);
5463     }
5464 
5465     return ret;
5466 }
5467 
5468 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5469 {
5470     if (arm_feature(env, ARM_FEATURE_M)) {
5471         return 0;
5472     }
5473     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5474 }
5475 
5476 /*
5477  * Corresponds to ARM pseudocode function ELIsInHost().
5478  */
5479 bool el_is_in_host(CPUARMState *env, int el)
5480 {
5481     uint64_t mask;
5482 
5483     /*
5484      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5485      * Perform the simplest bit tests first, and validate EL2 afterward.
5486      */
5487     if (el & 1) {
5488         return false; /* EL1 or EL3 */
5489     }
5490 
5491     /*
5492      * Note that hcr_write() checks isar_feature_aa64_vh(),
5493      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5494      */
5495     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5496     if ((env->cp15.hcr_el2 & mask) != mask) {
5497         return false;
5498     }
5499 
5500     /* TGE and/or E2H set: double check those bits are currently legal. */
5501     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5502 }
5503 
5504 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5505                        uint64_t value)
5506 {
5507     ARMCPU *cpu = env_archcpu(env);
5508     uint64_t valid_mask = 0;
5509 
5510     /* FEAT_MOPS adds MSCEn and MCE2 */
5511     if (cpu_isar_feature(aa64_mops, cpu)) {
5512         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5513     }
5514 
5515     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5516     if (cpu_isar_feature(aa64_nmi, cpu)) {
5517         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5518     }
5519     /* FEAT_CMOW adds CMOW */
5520     if (cpu_isar_feature(aa64_cmow, cpu)) {
5521         valid_mask |= HCRX_CMOW;
5522     }
5523     /* FEAT_XS adds FGTnXS, FnXS */
5524     if (cpu_isar_feature(aa64_xs, cpu)) {
5525         valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5526     }
5527 
5528     /* Clear RES0 bits.  */
5529     env->cp15.hcrx_el2 = value & valid_mask;
5530 
5531     /*
5532      * Updates to VINMI and VFNMI require us to update the status of
5533      * virtual NMI, which are the logical OR of these bits
5534      * and the state of the input lines from the GIC. (This requires
5535      * that we have the BQL, which is done by marking the
5536      * reginfo structs as ARM_CP_IO.)
5537      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5538      * possible for it to be taken immediately, because VINMI and
5539      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5540      * can only be written at EL2.
5541      */
5542     if (cpu_isar_feature(aa64_nmi, cpu)) {
5543         g_assert(bql_locked());
5544         arm_cpu_update_vinmi(cpu);
5545         arm_cpu_update_vfnmi(cpu);
5546     }
5547 }
5548 
5549 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5550                                   bool isread)
5551 {
5552     if (arm_current_el(env) == 2
5553         && arm_feature(env, ARM_FEATURE_EL3)
5554         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5555         return CP_ACCESS_TRAP_EL3;
5556     }
5557     return CP_ACCESS_OK;
5558 }
5559 
5560 static const ARMCPRegInfo hcrx_el2_reginfo = {
5561     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5562     .type = ARM_CP_IO,
5563     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5564     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5565     .nv2_redirect_offset = 0xa0,
5566     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5567 };
5568 
5569 /* Return the effective value of HCRX_EL2.  */
5570 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5571 {
5572     /*
5573      * The bits in this register behave as 0 for all purposes other than
5574      * direct reads of the register if SCR_EL3.HXEn is 0.
5575      * If EL2 is not enabled in the current security state, then the
5576      * bit may behave as if 0, or as if 1, depending on the bit.
5577      * For the moment, we treat the EL2-disabled case as taking
5578      * priority over the HXEn-disabled case. This is true for the only
5579      * bit for a feature which we implement where the answer is different
5580      * for the two cases (MSCEn for FEAT_MOPS).
5581      * This may need to be revisited for future bits.
5582      */
5583     if (!arm_is_el2_enabled(env)) {
5584         uint64_t hcrx = 0;
5585         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5586             /* MSCEn behaves as 1 if EL2 is not enabled */
5587             hcrx |= HCRX_MSCEN;
5588         }
5589         return hcrx;
5590     }
5591     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5592         return 0;
5593     }
5594     return env->cp15.hcrx_el2;
5595 }
5596 
5597 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5598                            uint64_t value)
5599 {
5600     /*
5601      * For A-profile AArch32 EL3, if NSACR.CP10
5602      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5603      */
5604     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5605         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5606         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5607         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5608     }
5609     env->cp15.cptr_el[2] = value;
5610 }
5611 
5612 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5613 {
5614     /*
5615      * For A-profile AArch32 EL3, if NSACR.CP10
5616      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5617      */
5618     uint64_t value = env->cp15.cptr_el[2];
5619 
5620     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5621         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5622         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5623     }
5624     return value;
5625 }
5626 
5627 static const ARMCPRegInfo el2_cp_reginfo[] = {
5628     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5629       .type = ARM_CP_IO,
5630       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5631       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5632       .nv2_redirect_offset = 0x78,
5633       .resetfn = hcr_reset,
5634       .writefn = hcr_write, .raw_writefn = raw_write },
5635     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5636       .type = ARM_CP_ALIAS | ARM_CP_IO,
5637       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5638       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5639       .writefn = hcr_writelow },
5640     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5641       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5642       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5643     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5644       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5645       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5646       .access = PL2_RW,
5647       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5648     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5649       .type = ARM_CP_NV2_REDIRECT,
5650       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5651       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5652     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5653       .type = ARM_CP_NV2_REDIRECT,
5654       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5655       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5656     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5657       .type = ARM_CP_ALIAS,
5658       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5659       .access = PL2_RW,
5660       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5661     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5662       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5663       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5664       .access = PL2_RW,
5665       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5666     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5667       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5668       .access = PL2_RW, .writefn = vbar_write,
5669       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5670       .resetvalue = 0 },
5671     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5672       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5673       .access = PL3_RW, .type = ARM_CP_ALIAS,
5674       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5675     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5676       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5677       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5678       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5679       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5680     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5681       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5682       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5683       .resetvalue = 0 },
5684     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5685       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5686       .access = PL2_RW, .type = ARM_CP_ALIAS,
5687       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5688     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5689       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5690       .access = PL2_RW, .type = ARM_CP_CONST,
5691       .resetvalue = 0 },
5692     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5693     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5694       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5695       .access = PL2_RW, .type = ARM_CP_CONST,
5696       .resetvalue = 0 },
5697     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5698       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5699       .access = PL2_RW, .type = ARM_CP_CONST,
5700       .resetvalue = 0 },
5701     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5702       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5703       .access = PL2_RW, .type = ARM_CP_CONST,
5704       .resetvalue = 0 },
5705     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5706       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5707       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5708       .raw_writefn = raw_write,
5709       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5710     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5711       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5712       .type = ARM_CP_ALIAS,
5713       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5714       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5715     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5716       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5717       .access = PL2_RW,
5718       .nv2_redirect_offset = 0x40,
5719       /* no .writefn needed as this can't cause an ASID change */
5720       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5721     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5722       .cp = 15, .opc1 = 6, .crm = 2,
5723       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5724       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5725       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5726       .writefn = vttbr_write, .raw_writefn = raw_write },
5727     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5728       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5729       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5730       .nv2_redirect_offset = 0x20,
5731       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5732     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5733       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5734       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5735       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5736     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5737       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5738       .access = PL2_RW, .resetvalue = 0,
5739       .nv2_redirect_offset = 0x90,
5740       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5741     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5742       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5743       .access = PL2_RW, .resetvalue = 0,
5744       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5745       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5746     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5747       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5748       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5749 #ifndef CONFIG_USER_ONLY
5750     /*
5751      * Unlike the other EL2-related AT operations, these must
5752      * UNDEF from EL3 if EL2 is not implemented, which is why we
5753      * define them here rather than with the rest of the AT ops.
5754      */
5755     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5756       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5757       .access = PL2_W, .accessfn = at_s1e2_access,
5758       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5759       .writefn = ats_write64 },
5760     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5761       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5762       .access = PL2_W, .accessfn = at_s1e2_access,
5763       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5764       .writefn = ats_write64 },
5765     /*
5766      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5767      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5768      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5769      * to behave as if SCR.NS was 1.
5770      */
5771     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5772       .access = PL2_W,
5773       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5774     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5775       .access = PL2_W,
5776       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5777     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5778       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5779       /*
5780        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5781        * reset values as IMPDEF. We choose to reset to 3 to comply with
5782        * both ARMv7 and ARMv8.
5783        */
5784       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5785       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5786       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5787     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5788       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5789       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5790       .writefn = gt_cntvoff_write,
5791       .nv2_redirect_offset = 0x60,
5792       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5793     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5794       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5795       .writefn = gt_cntvoff_write,
5796       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5797     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5798       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5799       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5800       .type = ARM_CP_IO, .access = PL2_RW,
5801       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5802     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5803       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5804       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5805       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5806     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5807       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5808       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5809       .resetfn = gt_hyp_timer_reset,
5810       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5811     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5812       .type = ARM_CP_IO,
5813       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5814       .access = PL2_RW,
5815       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5816       .resetvalue = 0,
5817       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5818 #endif
5819     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5820       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5821       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5822       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5823     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5824       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5825       .access = PL2_RW,
5826       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5827     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5828       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5829       .access = PL2_RW,
5830       .nv2_redirect_offset = 0x80,
5831       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5832 };
5833 
5834 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5835     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5836       .type = ARM_CP_ALIAS | ARM_CP_IO,
5837       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5838       .access = PL2_RW,
5839       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5840       .writefn = hcr_writehigh },
5841 };
5842 
5843 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5844                                   bool isread)
5845 {
5846     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5847         return CP_ACCESS_OK;
5848     }
5849     return CP_ACCESS_UNDEFINED;
5850 }
5851 
5852 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5853     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5854       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5855       .access = PL2_RW, .accessfn = sel2_access,
5856       .nv2_redirect_offset = 0x30,
5857       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5858     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5859       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5860       .access = PL2_RW, .accessfn = sel2_access,
5861       .nv2_redirect_offset = 0x48,
5862       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5863 #ifndef CONFIG_USER_ONLY
5864     /* Secure EL2 Physical Timer */
5865     { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5866       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
5867       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5868       .accessfn = gt_sel2timer_access,
5869       .readfn = gt_sec_pel2_tval_read,
5870       .writefn = gt_sec_pel2_tval_write,
5871       .resetfn = gt_sec_pel2_timer_reset,
5872     },
5873     { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5874       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
5875       .type = ARM_CP_IO, .access = PL2_RW,
5876       .accessfn = gt_sel2timer_access,
5877       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
5878       .resetvalue = 0,
5879       .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
5880     },
5881     { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5882       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
5883       .type = ARM_CP_IO, .access = PL2_RW,
5884       .accessfn = gt_sel2timer_access,
5885       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
5886       .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
5887     },
5888     /* Secure EL2 Virtual Timer */
5889     { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5890       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
5891       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5892       .accessfn = gt_sel2timer_access,
5893       .readfn = gt_sec_vel2_tval_read,
5894       .writefn = gt_sec_vel2_tval_write,
5895       .resetfn = gt_sec_vel2_timer_reset,
5896     },
5897     { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5898       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
5899       .type = ARM_CP_IO, .access = PL2_RW,
5900       .accessfn = gt_sel2timer_access,
5901       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
5902       .resetvalue = 0,
5903       .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
5904     },
5905     { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5906       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
5907       .type = ARM_CP_IO, .access = PL2_RW,
5908       .accessfn = gt_sel2timer_access,
5909       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
5910       .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
5911     },
5912 #endif
5913 };
5914 
5915 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5916                                    bool isread)
5917 {
5918     /*
5919      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5920      * At Secure EL1 it traps to EL3 or EL2.
5921      */
5922     if (arm_current_el(env) == 3) {
5923         return CP_ACCESS_OK;
5924     }
5925     if (arm_is_secure_below_el3(env)) {
5926         if (env->cp15.scr_el3 & SCR_EEL2) {
5927             return CP_ACCESS_TRAP_EL2;
5928         }
5929         return CP_ACCESS_TRAP_EL3;
5930     }
5931     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5932     if (isread) {
5933         return CP_ACCESS_OK;
5934     }
5935     return CP_ACCESS_UNDEFINED;
5936 }
5937 
5938 static const ARMCPRegInfo el3_cp_reginfo[] = {
5939     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5940       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5941       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5942       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5943     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5944       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5945       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5946       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5947       .writefn = scr_write, .raw_writefn = raw_write },
5948     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5949       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5950       .access = PL3_RW, .resetvalue = 0,
5951       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5952     { .name = "SDER",
5953       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5954       .access = PL3_RW, .resetvalue = 0,
5955       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5956     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5957       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5958       .writefn = vbar_write, .resetvalue = 0,
5959       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5960     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5961       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5962       .access = PL3_RW, .resetvalue = 0,
5963       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5964     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5965       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5966       .access = PL3_RW,
5967       /* no .writefn needed as this can't cause an ASID change */
5968       .resetvalue = 0,
5969       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5970     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5971       .type = ARM_CP_ALIAS,
5972       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5973       .access = PL3_RW,
5974       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5975     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5976       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5977       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5978     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5979       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5980       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5981     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5982       .type = ARM_CP_ALIAS,
5983       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5984       .access = PL3_RW,
5985       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5986     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5987       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5988       .access = PL3_RW, .writefn = vbar_write,
5989       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5990       .resetvalue = 0 },
5991     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5992       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5993       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5994       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5995     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5996       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5997       .access = PL3_RW, .resetvalue = 0,
5998       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5999     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6000       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6001       .access = PL3_RW, .type = ARM_CP_CONST,
6002       .resetvalue = 0 },
6003     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6004       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6005       .access = PL3_RW, .type = ARM_CP_CONST,
6006       .resetvalue = 0 },
6007     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6008       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6009       .access = PL3_RW, .type = ARM_CP_CONST,
6010       .resetvalue = 0 },
6011 };
6012 
6013 #ifndef CONFIG_USER_ONLY
6014 
6015 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6016                                  bool isread)
6017 {
6018     if (arm_current_el(env) == 1) {
6019         /* This must be a FEAT_NV access */
6020         return CP_ACCESS_OK;
6021     }
6022     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6023         return CP_ACCESS_UNDEFINED;
6024     }
6025     return CP_ACCESS_OK;
6026 }
6027 
6028 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6029                                       bool isread)
6030 {
6031     if (arm_current_el(env) == 1) {
6032         /* This must be a FEAT_NV access with NVx == 101 */
6033         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6034             return CP_ACCESS_TRAP_EL2;
6035         }
6036     }
6037     return e2h_access(env, ri, isread);
6038 }
6039 
6040 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6041                                       bool isread)
6042 {
6043     if (arm_current_el(env) == 1) {
6044         /* This must be a FEAT_NV access with NVx == 101 */
6045         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6046             return CP_ACCESS_TRAP_EL2;
6047         }
6048     }
6049     return e2h_access(env, ri, isread);
6050 }
6051 
6052 /* Test if system register redirection is to occur in the current state.  */
6053 static bool redirect_for_e2h(CPUARMState *env)
6054 {
6055     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6056 }
6057 
6058 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6059 {
6060     CPReadFn *readfn;
6061 
6062     if (redirect_for_e2h(env)) {
6063         /* Switch to the saved EL2 version of the register.  */
6064         ri = ri->opaque;
6065         readfn = ri->readfn;
6066     } else {
6067         readfn = ri->orig_readfn;
6068     }
6069     if (readfn == NULL) {
6070         readfn = raw_read;
6071     }
6072     return readfn(env, ri);
6073 }
6074 
6075 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6076                           uint64_t value)
6077 {
6078     CPWriteFn *writefn;
6079 
6080     if (redirect_for_e2h(env)) {
6081         /* Switch to the saved EL2 version of the register.  */
6082         ri = ri->opaque;
6083         writefn = ri->writefn;
6084     } else {
6085         writefn = ri->orig_writefn;
6086     }
6087     if (writefn == NULL) {
6088         writefn = raw_write;
6089     }
6090     writefn(env, ri, value);
6091 }
6092 
6093 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6094 {
6095     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6096     return ri->orig_readfn(env, ri->opaque);
6097 }
6098 
6099 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6100                               uint64_t value)
6101 {
6102     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6103     return ri->orig_writefn(env, ri->opaque, value);
6104 }
6105 
6106 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6107                                          const ARMCPRegInfo *ri,
6108                                          bool isread)
6109 {
6110     if (arm_current_el(env) == 1) {
6111         /*
6112          * This must be a FEAT_NV access (will either trap or redirect
6113          * to memory). None of the registers with _EL12 aliases want to
6114          * apply their trap controls for this kind of access, so don't
6115          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6116          */
6117         return CP_ACCESS_OK;
6118     }
6119     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6120     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6121         return CP_ACCESS_UNDEFINED;
6122     }
6123     if (ri->orig_accessfn) {
6124         return ri->orig_accessfn(env, ri->opaque, isread);
6125     }
6126     return CP_ACCESS_OK;
6127 }
6128 
6129 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6130 {
6131     struct E2HAlias {
6132         uint32_t src_key, dst_key, new_key;
6133         const char *src_name, *dst_name, *new_name;
6134         bool (*feature)(const ARMISARegisters *id);
6135     };
6136 
6137 #define K(op0, op1, crn, crm, op2) \
6138     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6139 
6140     static const struct E2HAlias aliases[] = {
6141         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6142           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6143         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6144           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6145         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6146           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6147         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6148           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6149         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6150           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6151         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6152           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6153         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6154           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6155         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6156           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6157         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6158           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6159         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6160           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6161         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6162           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6163         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6164           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6165         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6166           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6167         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6168           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6169         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6170           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6171         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6172           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6173 
6174         /*
6175          * Note that redirection of ZCR is mentioned in the description
6176          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6177          * not in the summary table.
6178          */
6179         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6180           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6181         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6182           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6183 
6184         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6185           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6186 
6187         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6188           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6189           isar_feature_aa64_scxtnum },
6190 
6191         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6192         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6193     };
6194 #undef K
6195 
6196     size_t i;
6197 
6198     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6199         const struct E2HAlias *a = &aliases[i];
6200         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6201         bool ok;
6202 
6203         if (a->feature && !a->feature(&cpu->isar)) {
6204             continue;
6205         }
6206 
6207         src_reg = g_hash_table_lookup(cpu->cp_regs,
6208                                       (gpointer)(uintptr_t)a->src_key);
6209         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6210                                       (gpointer)(uintptr_t)a->dst_key);
6211         g_assert(src_reg != NULL);
6212         g_assert(dst_reg != NULL);
6213 
6214         /* Cross-compare names to detect typos in the keys.  */
6215         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6216         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6217 
6218         /* None of the core system registers use opaque; we will.  */
6219         g_assert(src_reg->opaque == NULL);
6220 
6221         /* Create alias before redirection so we dup the right data. */
6222         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6223 
6224         new_reg->name = a->new_name;
6225         new_reg->type |= ARM_CP_ALIAS;
6226         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6227         new_reg->access &= PL2_RW | PL3_RW;
6228         /* The new_reg op fields are as per new_key, not the target reg */
6229         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6230             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6231         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6232             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6233         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6234             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6235         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6236             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6237         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6238             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6239         new_reg->opaque = src_reg;
6240         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6241         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6242         new_reg->orig_accessfn = src_reg->accessfn;
6243         if (!new_reg->raw_readfn) {
6244             new_reg->raw_readfn = raw_read;
6245         }
6246         if (!new_reg->raw_writefn) {
6247             new_reg->raw_writefn = raw_write;
6248         }
6249         new_reg->readfn = el2_e2h_e12_read;
6250         new_reg->writefn = el2_e2h_e12_write;
6251         new_reg->accessfn = el2_e2h_e12_access;
6252 
6253         /*
6254          * If the _EL1 register is redirected to memory by FEAT_NV2,
6255          * then it shares the offset with the _EL12 register,
6256          * and which one is redirected depends on HCR_EL2.NV1.
6257          */
6258         if (new_reg->nv2_redirect_offset) {
6259             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6260             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6261             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6262         }
6263 
6264         ok = g_hash_table_insert(cpu->cp_regs,
6265                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6266         g_assert(ok);
6267 
6268         src_reg->opaque = dst_reg;
6269         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6270         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6271         if (!src_reg->raw_readfn) {
6272             src_reg->raw_readfn = raw_read;
6273         }
6274         if (!src_reg->raw_writefn) {
6275             src_reg->raw_writefn = raw_write;
6276         }
6277         src_reg->readfn = el2_e2h_read;
6278         src_reg->writefn = el2_e2h_write;
6279     }
6280 }
6281 #endif
6282 
6283 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6284                                      bool isread)
6285 {
6286     int cur_el = arm_current_el(env);
6287 
6288     if (cur_el < 2) {
6289         uint64_t hcr = arm_hcr_el2_eff(env);
6290 
6291         if (cur_el == 0) {
6292             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6293                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6294                     return CP_ACCESS_TRAP_EL2;
6295                 }
6296             } else {
6297                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6298                     return CP_ACCESS_TRAP_EL1;
6299                 }
6300                 if (hcr & HCR_TID2) {
6301                     return CP_ACCESS_TRAP_EL2;
6302                 }
6303             }
6304         } else if (hcr & HCR_TID2) {
6305             return CP_ACCESS_TRAP_EL2;
6306         }
6307     }
6308 
6309     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6310         return CP_ACCESS_TRAP_EL2;
6311     }
6312 
6313     return CP_ACCESS_OK;
6314 }
6315 
6316 /*
6317  * Check for traps to RAS registers, which are controlled
6318  * by HCR_EL2.TERR and SCR_EL3.TERR.
6319  */
6320 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6321                                   bool isread)
6322 {
6323     int el = arm_current_el(env);
6324 
6325     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6326         return CP_ACCESS_TRAP_EL2;
6327     }
6328     if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
6329         return CP_ACCESS_TRAP_EL3;
6330     }
6331     return CP_ACCESS_OK;
6332 }
6333 
6334 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6335 {
6336     int el = arm_current_el(env);
6337 
6338     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6339         return env->cp15.vdisr_el2;
6340     }
6341     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6342         return 0; /* RAZ/WI */
6343     }
6344     return env->cp15.disr_el1;
6345 }
6346 
6347 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6348 {
6349     int el = arm_current_el(env);
6350 
6351     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6352         env->cp15.vdisr_el2 = val;
6353         return;
6354     }
6355     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6356         return; /* RAZ/WI */
6357     }
6358     env->cp15.disr_el1 = val;
6359 }
6360 
6361 /*
6362  * Minimal RAS implementation with no Error Records.
6363  * Which means that all of the Error Record registers:
6364  *   ERXADDR_EL1
6365  *   ERXCTLR_EL1
6366  *   ERXFR_EL1
6367  *   ERXMISC0_EL1
6368  *   ERXMISC1_EL1
6369  *   ERXMISC2_EL1
6370  *   ERXMISC3_EL1
6371  *   ERXPFGCDN_EL1  (RASv1p1)
6372  *   ERXPFGCTL_EL1  (RASv1p1)
6373  *   ERXPFGF_EL1    (RASv1p1)
6374  *   ERXSTATUS_EL1
6375  * and
6376  *   ERRSELR_EL1
6377  * may generate UNDEFINED, which is the effect we get by not
6378  * listing them at all.
6379  *
6380  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6381  * is higher priority than FGT-to-EL2 so we do not need to list them
6382  * in order to check for an FGT.
6383  */
6384 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6385     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6386       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6387       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6388       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6389     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6390       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6391       .access = PL1_R, .accessfn = access_terr,
6392       .fgt = FGT_ERRIDR_EL1,
6393       .type = ARM_CP_CONST, .resetvalue = 0 },
6394     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6395       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6396       .nv2_redirect_offset = 0x500,
6397       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6398     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6399       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6400       .nv2_redirect_offset = 0x508,
6401       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6402 };
6403 
6404 /*
6405  * Return the exception level to which exceptions should be taken
6406  * via SVEAccessTrap.  This excludes the check for whether the exception
6407  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6408  * be found by testing 0 < fp_exception_el < sve_exception_el.
6409  *
6410  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6411  * pseudocode does *not* separate out the FP trap checks, but has them
6412  * all in one function.
6413  */
6414 int sve_exception_el(CPUARMState *env, int el)
6415 {
6416 #ifndef CONFIG_USER_ONLY
6417     if (el <= 1 && !el_is_in_host(env, el)) {
6418         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6419         case 1:
6420             if (el != 0) {
6421                 break;
6422             }
6423             /* fall through */
6424         case 0:
6425         case 2:
6426             return 1;
6427         }
6428     }
6429 
6430     if (el <= 2 && arm_is_el2_enabled(env)) {
6431         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6432         if (env->cp15.hcr_el2 & HCR_E2H) {
6433             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6434             case 1:
6435                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6436                     break;
6437                 }
6438                 /* fall through */
6439             case 0:
6440             case 2:
6441                 return 2;
6442             }
6443         } else {
6444             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6445                 return 2;
6446             }
6447         }
6448     }
6449 
6450     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6451     if (arm_feature(env, ARM_FEATURE_EL3)
6452         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6453         return 3;
6454     }
6455 #endif
6456     return 0;
6457 }
6458 
6459 /*
6460  * Return the exception level to which exceptions should be taken for SME.
6461  * C.f. the ARM pseudocode function CheckSMEAccess.
6462  */
6463 int sme_exception_el(CPUARMState *env, int el)
6464 {
6465 #ifndef CONFIG_USER_ONLY
6466     if (el <= 1 && !el_is_in_host(env, el)) {
6467         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6468         case 1:
6469             if (el != 0) {
6470                 break;
6471             }
6472             /* fall through */
6473         case 0:
6474         case 2:
6475             return 1;
6476         }
6477     }
6478 
6479     if (el <= 2 && arm_is_el2_enabled(env)) {
6480         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6481         if (env->cp15.hcr_el2 & HCR_E2H) {
6482             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6483             case 1:
6484                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6485                     break;
6486                 }
6487                 /* fall through */
6488             case 0:
6489             case 2:
6490                 return 2;
6491             }
6492         } else {
6493             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6494                 return 2;
6495             }
6496         }
6497     }
6498 
6499     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6500     if (arm_feature(env, ARM_FEATURE_EL3)
6501         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6502         return 3;
6503     }
6504 #endif
6505     return 0;
6506 }
6507 
6508 /*
6509  * Given that SVE is enabled, return the vector length for EL.
6510  */
6511 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6512 {
6513     ARMCPU *cpu = env_archcpu(env);
6514     uint64_t *cr = env->vfp.zcr_el;
6515     uint32_t map = cpu->sve_vq.map;
6516     uint32_t len = ARM_MAX_VQ - 1;
6517 
6518     if (sm) {
6519         cr = env->vfp.smcr_el;
6520         map = cpu->sme_vq.map;
6521     }
6522 
6523     if (el <= 1 && !el_is_in_host(env, el)) {
6524         len = MIN(len, 0xf & (uint32_t)cr[1]);
6525     }
6526     if (el <= 2 && arm_is_el2_enabled(env)) {
6527         len = MIN(len, 0xf & (uint32_t)cr[2]);
6528     }
6529     if (arm_feature(env, ARM_FEATURE_EL3)) {
6530         len = MIN(len, 0xf & (uint32_t)cr[3]);
6531     }
6532 
6533     map &= MAKE_64BIT_MASK(0, len + 1);
6534     if (map != 0) {
6535         return 31 - clz32(map);
6536     }
6537 
6538     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6539     assert(sm);
6540     return ctz32(cpu->sme_vq.map);
6541 }
6542 
6543 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6544 {
6545     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6546 }
6547 
6548 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6549                       uint64_t value)
6550 {
6551     int cur_el = arm_current_el(env);
6552     int old_len = sve_vqm1_for_el(env, cur_el);
6553     int new_len;
6554 
6555     /* Bits other than [3:0] are RAZ/WI.  */
6556     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6557     raw_write(env, ri, value & 0xf);
6558 
6559     /*
6560      * Because we arrived here, we know both FP and SVE are enabled;
6561      * otherwise we would have trapped access to the ZCR_ELn register.
6562      */
6563     new_len = sve_vqm1_for_el(env, cur_el);
6564     if (new_len < old_len) {
6565         aarch64_sve_narrow_vq(env, new_len + 1);
6566     }
6567 }
6568 
6569 static const ARMCPRegInfo zcr_reginfo[] = {
6570     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6571       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6572       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6573       .access = PL1_RW, .type = ARM_CP_SVE,
6574       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6575       .writefn = zcr_write, .raw_writefn = raw_write },
6576     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6577       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6578       .access = PL2_RW, .type = ARM_CP_SVE,
6579       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6580       .writefn = zcr_write, .raw_writefn = raw_write },
6581     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6582       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6583       .access = PL3_RW, .type = ARM_CP_SVE,
6584       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6585       .writefn = zcr_write, .raw_writefn = raw_write },
6586 };
6587 
6588 #ifdef TARGET_AARCH64
6589 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6590                                     bool isread)
6591 {
6592     int el = arm_current_el(env);
6593 
6594     if (el == 0) {
6595         uint64_t sctlr = arm_sctlr(env, el);
6596         if (!(sctlr & SCTLR_EnTP2)) {
6597             return CP_ACCESS_TRAP_EL1;
6598         }
6599     }
6600     /* TODO: FEAT_FGT */
6601     if (el < 3
6602         && arm_feature(env, ARM_FEATURE_EL3)
6603         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6604         return CP_ACCESS_TRAP_EL3;
6605     }
6606     return CP_ACCESS_OK;
6607 }
6608 
6609 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6610                                       bool isread)
6611 {
6612     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6613     if (arm_current_el(env) == 2
6614         && arm_feature(env, ARM_FEATURE_EL3)
6615         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6616         return CP_ACCESS_TRAP_EL3;
6617     }
6618     return CP_ACCESS_OK;
6619 }
6620 
6621 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6622                                    bool isread)
6623 {
6624     if (arm_current_el(env) < 3
6625         && arm_feature(env, ARM_FEATURE_EL3)
6626         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6627         return CP_ACCESS_TRAP_EL3;
6628     }
6629     return CP_ACCESS_OK;
6630 }
6631 
6632 /* ResetSVEState */
6633 static void arm_reset_sve_state(CPUARMState *env)
6634 {
6635     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6636     /* Recall that FFR is stored as pregs[16]. */
6637     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6638     vfp_set_fpsr(env, 0x0800009f);
6639 }
6640 
6641 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6642 {
6643     uint64_t change = (env->svcr ^ new) & mask;
6644 
6645     if (change == 0) {
6646         return;
6647     }
6648     env->svcr ^= change;
6649 
6650     if (change & R_SVCR_SM_MASK) {
6651         arm_reset_sve_state(env);
6652     }
6653 
6654     /*
6655      * ResetSMEState.
6656      *
6657      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6658      * on enable: while disabled, the storage is inaccessible and the
6659      * value does not matter.  We're not saving the storage in vmstate
6660      * when disabled either.
6661      */
6662     if (change & new & R_SVCR_ZA_MASK) {
6663         memset(env->zarray, 0, sizeof(env->zarray));
6664     }
6665 
6666     if (tcg_enabled()) {
6667         arm_rebuild_hflags(env);
6668     }
6669 }
6670 
6671 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6672                        uint64_t value)
6673 {
6674     aarch64_set_svcr(env, value, -1);
6675 }
6676 
6677 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6678                        uint64_t value)
6679 {
6680     int cur_el = arm_current_el(env);
6681     int old_len = sve_vqm1_for_el(env, cur_el);
6682     int new_len;
6683 
6684     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6685     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6686     raw_write(env, ri, value);
6687 
6688     /*
6689      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6690      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6691      * current values for simplicity.  But for QEMU internals, we must still
6692      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6693      * above aarch64_sve_narrow_vq.
6694      */
6695     new_len = sve_vqm1_for_el(env, cur_el);
6696     if (new_len < old_len) {
6697         aarch64_sve_narrow_vq(env, new_len + 1);
6698     }
6699 }
6700 
6701 static const ARMCPRegInfo sme_reginfo[] = {
6702     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6703       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6704       .access = PL0_RW, .accessfn = access_tpidr2,
6705       .fgt = FGT_NTPIDR2_EL0,
6706       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6707     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6708       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6709       .access = PL0_RW, .type = ARM_CP_SME,
6710       .fieldoffset = offsetof(CPUARMState, svcr),
6711       .writefn = svcr_write, .raw_writefn = raw_write },
6712     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6713       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6714       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6715       .access = PL1_RW, .type = ARM_CP_SME,
6716       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6717       .writefn = smcr_write, .raw_writefn = raw_write },
6718     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6719       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6720       .access = PL2_RW, .type = ARM_CP_SME,
6721       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6722       .writefn = smcr_write, .raw_writefn = raw_write },
6723     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6724       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6725       .access = PL3_RW, .type = ARM_CP_SME,
6726       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6727       .writefn = smcr_write, .raw_writefn = raw_write },
6728     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6729       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6730       .access = PL1_R, .accessfn = access_aa64_tid1,
6731       /*
6732        * IMPLEMENTOR = 0 (software)
6733        * REVISION    = 0 (implementation defined)
6734        * SMPS        = 0 (no streaming execution priority in QEMU)
6735        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6736        */
6737       .type = ARM_CP_CONST, .resetvalue = 0, },
6738     /*
6739      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6740      */
6741     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6742       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6743       .access = PL1_RW, .accessfn = access_smpri,
6744       .fgt = FGT_NSMPRI_EL1,
6745       .type = ARM_CP_CONST, .resetvalue = 0 },
6746     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6747       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6748       .nv2_redirect_offset = 0x1f8,
6749       .access = PL2_RW, .accessfn = access_smprimap,
6750       .type = ARM_CP_CONST, .resetvalue = 0 },
6751 };
6752 
6753 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6754                         uint64_t value)
6755 {
6756     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6757     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6758         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6759         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6760 
6761     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6762 }
6763 
6764 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6765 {
6766     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6767                                      env_archcpu(env)->reset_l0gptsz);
6768 }
6769 
6770 static const ARMCPRegInfo rme_reginfo[] = {
6771     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6772       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6773       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6774       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6775     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6776       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6777       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6778     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6779       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6780       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6781     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6782       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6783       .access = PL3_W, .type = ARM_CP_NOP },
6784 };
6785 
6786 static const ARMCPRegInfo rme_mte_reginfo[] = {
6787     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6788       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6789       .access = PL3_W, .type = ARM_CP_NOP },
6790 };
6791 
6792 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6793                               uint64_t value)
6794 {
6795     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6796 }
6797 
6798 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6799 {
6800     return env->pstate & PSTATE_ALLINT;
6801 }
6802 
6803 static CPAccessResult aa64_allint_access(CPUARMState *env,
6804                                          const ARMCPRegInfo *ri, bool isread)
6805 {
6806     if (!isread && arm_current_el(env) == 1 &&
6807         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6808         return CP_ACCESS_TRAP_EL2;
6809     }
6810     return CP_ACCESS_OK;
6811 }
6812 
6813 static const ARMCPRegInfo nmi_reginfo[] = {
6814     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6815       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6816       .type = ARM_CP_NO_RAW,
6817       .access = PL1_RW, .accessfn = aa64_allint_access,
6818       .fieldoffset = offsetof(CPUARMState, pstate),
6819       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6820       .resetfn = arm_cp_reset_ignore },
6821 };
6822 #endif /* TARGET_AARCH64 */
6823 
6824 static void define_pmu_regs(ARMCPU *cpu)
6825 {
6826     /*
6827      * v7 performance monitor control register: same implementor
6828      * field as main ID register, and we implement four counters in
6829      * addition to the cycle count register.
6830      */
6831     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6832     ARMCPRegInfo pmcr = {
6833         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6834         .access = PL0_RW,
6835         .fgt = FGT_PMCR_EL0,
6836         .type = ARM_CP_IO | ARM_CP_ALIAS,
6837         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6838         .accessfn = pmreg_access,
6839         .readfn = pmcr_read, .raw_readfn = raw_read,
6840         .writefn = pmcr_write, .raw_writefn = raw_write,
6841     };
6842     ARMCPRegInfo pmcr64 = {
6843         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6844         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6845         .access = PL0_RW, .accessfn = pmreg_access,
6846         .fgt = FGT_PMCR_EL0,
6847         .type = ARM_CP_IO,
6848         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6849         .resetvalue = cpu->isar.reset_pmcr_el0,
6850         .readfn = pmcr_read, .raw_readfn = raw_read,
6851         .writefn = pmcr_write, .raw_writefn = raw_write,
6852     };
6853 
6854     define_one_arm_cp_reg(cpu, &pmcr);
6855     define_one_arm_cp_reg(cpu, &pmcr64);
6856     for (i = 0; i < pmcrn; i++) {
6857         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6858         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6859         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6860         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6861         ARMCPRegInfo pmev_regs[] = {
6862             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6863               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6864               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6865               .fgt = FGT_PMEVCNTRN_EL0,
6866               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6867               .accessfn = pmreg_access_xevcntr },
6868             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6869               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6870               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6871               .type = ARM_CP_IO,
6872               .fgt = FGT_PMEVCNTRN_EL0,
6873               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6874               .raw_readfn = pmevcntr_rawread,
6875               .raw_writefn = pmevcntr_rawwrite },
6876             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6877               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6878               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6879               .fgt = FGT_PMEVTYPERN_EL0,
6880               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6881               .accessfn = pmreg_access },
6882             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6883               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6884               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6885               .fgt = FGT_PMEVTYPERN_EL0,
6886               .type = ARM_CP_IO,
6887               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6888               .raw_writefn = pmevtyper_rawwrite },
6889         };
6890         define_arm_cp_regs(cpu, pmev_regs);
6891         g_free(pmevcntr_name);
6892         g_free(pmevcntr_el0_name);
6893         g_free(pmevtyper_name);
6894         g_free(pmevtyper_el0_name);
6895     }
6896     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6897         ARMCPRegInfo v81_pmu_regs[] = {
6898             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6899               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6900               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6901               .fgt = FGT_PMCEIDN_EL0,
6902               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6903             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6904               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6905               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6906               .fgt = FGT_PMCEIDN_EL0,
6907               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6908         };
6909         define_arm_cp_regs(cpu, v81_pmu_regs);
6910     }
6911     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6912         static const ARMCPRegInfo v84_pmmir = {
6913             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6914             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6915             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6916             .fgt = FGT_PMMIR_EL1,
6917             .resetvalue = 0
6918         };
6919         define_one_arm_cp_reg(cpu, &v84_pmmir);
6920     }
6921 }
6922 
6923 #ifndef CONFIG_USER_ONLY
6924 /*
6925  * We don't know until after realize whether there's a GICv3
6926  * attached, and that is what registers the gicv3 sysregs.
6927  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6928  * at runtime.
6929  */
6930 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6931 {
6932     ARMCPU *cpu = env_archcpu(env);
6933     uint64_t pfr1 = cpu->isar.id_pfr1;
6934 
6935     if (env->gicv3state) {
6936         pfr1 |= 1 << 28;
6937     }
6938     return pfr1;
6939 }
6940 
6941 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6942 {
6943     ARMCPU *cpu = env_archcpu(env);
6944     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6945 
6946     if (env->gicv3state) {
6947         pfr0 |= 1 << 24;
6948     }
6949     return pfr0;
6950 }
6951 #endif
6952 
6953 /*
6954  * Shared logic between LORID and the rest of the LOR* registers.
6955  * Secure state exclusion has already been dealt with.
6956  */
6957 static CPAccessResult access_lor_ns(CPUARMState *env,
6958                                     const ARMCPRegInfo *ri, bool isread)
6959 {
6960     int el = arm_current_el(env);
6961 
6962     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6963         return CP_ACCESS_TRAP_EL2;
6964     }
6965     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6966         return CP_ACCESS_TRAP_EL3;
6967     }
6968     return CP_ACCESS_OK;
6969 }
6970 
6971 static CPAccessResult access_lor_other(CPUARMState *env,
6972                                        const ARMCPRegInfo *ri, bool isread)
6973 {
6974     if (arm_is_secure_below_el3(env)) {
6975         /* UNDEF if SCR_EL3.NS == 0 */
6976         return CP_ACCESS_UNDEFINED;
6977     }
6978     return access_lor_ns(env, ri, isread);
6979 }
6980 
6981 /*
6982  * A trivial implementation of ARMv8.1-LOR leaves all of these
6983  * registers fixed at 0, which indicates that there are zero
6984  * supported Limited Ordering regions.
6985  */
6986 static const ARMCPRegInfo lor_reginfo[] = {
6987     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6988       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6989       .access = PL1_RW, .accessfn = access_lor_other,
6990       .fgt = FGT_LORSA_EL1,
6991       .type = ARM_CP_CONST, .resetvalue = 0 },
6992     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6993       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6994       .access = PL1_RW, .accessfn = access_lor_other,
6995       .fgt = FGT_LOREA_EL1,
6996       .type = ARM_CP_CONST, .resetvalue = 0 },
6997     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6998       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6999       .access = PL1_RW, .accessfn = access_lor_other,
7000       .fgt = FGT_LORN_EL1,
7001       .type = ARM_CP_CONST, .resetvalue = 0 },
7002     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7003       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7004       .access = PL1_RW, .accessfn = access_lor_other,
7005       .fgt = FGT_LORC_EL1,
7006       .type = ARM_CP_CONST, .resetvalue = 0 },
7007     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7008       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7009       .access = PL1_R, .accessfn = access_lor_ns,
7010       .fgt = FGT_LORID_EL1,
7011       .type = ARM_CP_CONST, .resetvalue = 0 },
7012 };
7013 
7014 #ifdef TARGET_AARCH64
7015 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7016                                    bool isread)
7017 {
7018     int el = arm_current_el(env);
7019 
7020     if (el < 2 &&
7021         arm_is_el2_enabled(env) &&
7022         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7023         return CP_ACCESS_TRAP_EL2;
7024     }
7025     if (el < 3 &&
7026         arm_feature(env, ARM_FEATURE_EL3) &&
7027         !(env->cp15.scr_el3 & SCR_APK)) {
7028         return CP_ACCESS_TRAP_EL3;
7029     }
7030     return CP_ACCESS_OK;
7031 }
7032 
7033 static const ARMCPRegInfo pauth_reginfo[] = {
7034     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7035       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7036       .access = PL1_RW, .accessfn = access_pauth,
7037       .fgt = FGT_APDAKEY,
7038       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7039     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7040       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7041       .access = PL1_RW, .accessfn = access_pauth,
7042       .fgt = FGT_APDAKEY,
7043       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7044     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7046       .access = PL1_RW, .accessfn = access_pauth,
7047       .fgt = FGT_APDBKEY,
7048       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7049     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7050       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7051       .access = PL1_RW, .accessfn = access_pauth,
7052       .fgt = FGT_APDBKEY,
7053       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7054     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7055       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7056       .access = PL1_RW, .accessfn = access_pauth,
7057       .fgt = FGT_APGAKEY,
7058       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7059     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7060       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7061       .access = PL1_RW, .accessfn = access_pauth,
7062       .fgt = FGT_APGAKEY,
7063       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7064     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7065       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7066       .access = PL1_RW, .accessfn = access_pauth,
7067       .fgt = FGT_APIAKEY,
7068       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7069     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7070       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7071       .access = PL1_RW, .accessfn = access_pauth,
7072       .fgt = FGT_APIAKEY,
7073       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7074     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7075       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7076       .access = PL1_RW, .accessfn = access_pauth,
7077       .fgt = FGT_APIBKEY,
7078       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7079     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7081       .access = PL1_RW, .accessfn = access_pauth,
7082       .fgt = FGT_APIBKEY,
7083       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7084 };
7085 
7086 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7087 {
7088     Error *err = NULL;
7089     uint64_t ret;
7090 
7091     /* Success sets NZCV = 0000.  */
7092     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7093 
7094     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7095         /*
7096          * ??? Failed, for unknown reasons in the crypto subsystem.
7097          * The best we can do is log the reason and return the
7098          * timed-out indication to the guest.  There is no reason
7099          * we know to expect this failure to be transitory, so the
7100          * guest may well hang retrying the operation.
7101          */
7102         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7103                       ri->name, error_get_pretty(err));
7104         error_free(err);
7105 
7106         env->ZF = 0; /* NZCF = 0100 */
7107         return 0;
7108     }
7109     return ret;
7110 }
7111 
7112 /* We do not support re-seeding, so the two registers operate the same.  */
7113 static const ARMCPRegInfo rndr_reginfo[] = {
7114     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7115       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7116       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7117       .access = PL0_R, .readfn = rndr_readfn },
7118     { .name = "RNDRRS", .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 = 1,
7121       .access = PL0_R, .readfn = rndr_readfn },
7122 };
7123 
7124 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7125                           uint64_t value)
7126 {
7127 #ifdef CONFIG_TCG
7128     ARMCPU *cpu = env_archcpu(env);
7129     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7130     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7131     uint64_t vaddr_in = (uint64_t) value;
7132     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7133     void *haddr;
7134     int mem_idx = arm_env_mmu_index(env);
7135 
7136     /* This won't be crossing page boundaries */
7137     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7138     if (haddr) {
7139 #ifndef CONFIG_USER_ONLY
7140 
7141         ram_addr_t offset;
7142         MemoryRegion *mr;
7143 
7144         /* RCU lock is already being held */
7145         mr = memory_region_from_host(haddr, &offset);
7146 
7147         if (mr) {
7148             memory_region_writeback(mr, offset, dline_size);
7149         }
7150 #endif /*CONFIG_USER_ONLY*/
7151     }
7152 #else
7153     /* Handled by hardware accelerator. */
7154     g_assert_not_reached();
7155 #endif /* CONFIG_TCG */
7156 }
7157 
7158 static const ARMCPRegInfo dcpop_reg[] = {
7159     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7160       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7161       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7162       .fgt = FGT_DCCVAP,
7163       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7164 };
7165 
7166 static const ARMCPRegInfo dcpodp_reg[] = {
7167     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7168       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7169       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7170       .fgt = FGT_DCCVADP,
7171       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7172 };
7173 
7174 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7175                                        bool isread)
7176 {
7177     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7178         return CP_ACCESS_TRAP_EL2;
7179     }
7180 
7181     return CP_ACCESS_OK;
7182 }
7183 
7184 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7185                                  bool isread)
7186 {
7187     int el = arm_current_el(env);
7188     if (el < 2 && arm_is_el2_enabled(env)) {
7189         uint64_t hcr = arm_hcr_el2_eff(env);
7190         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7191             return CP_ACCESS_TRAP_EL2;
7192         }
7193     }
7194     if (el < 3 &&
7195         arm_feature(env, ARM_FEATURE_EL3) &&
7196         !(env->cp15.scr_el3 & SCR_ATA)) {
7197         return CP_ACCESS_TRAP_EL3;
7198     }
7199     return CP_ACCESS_OK;
7200 }
7201 
7202 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7203                                       bool isread)
7204 {
7205     CPAccessResult nv1 = access_nv1(env, ri, isread);
7206 
7207     if (nv1 != CP_ACCESS_OK) {
7208         return nv1;
7209     }
7210     return access_mte(env, ri, isread);
7211 }
7212 
7213 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7214                                       bool isread)
7215 {
7216     /*
7217      * TFSR_EL2: similar to generic access_mte(), but we need to
7218      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7219      * if NV2 is enabled then we will redirect this to TFSR_EL1
7220      * after doing the HCR and SCR ATA traps; otherwise this will
7221      * be a trap to EL2 and the HCR/SCR traps do not apply.
7222      */
7223     int el = arm_current_el(env);
7224 
7225     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7226         return CP_ACCESS_OK;
7227     }
7228     if (el < 2 && arm_is_el2_enabled(env)) {
7229         uint64_t hcr = arm_hcr_el2_eff(env);
7230         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7231             return CP_ACCESS_TRAP_EL2;
7232         }
7233     }
7234     if (el < 3 &&
7235         arm_feature(env, ARM_FEATURE_EL3) &&
7236         !(env->cp15.scr_el3 & SCR_ATA)) {
7237         return CP_ACCESS_TRAP_EL3;
7238     }
7239     return CP_ACCESS_OK;
7240 }
7241 
7242 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7243 {
7244     return env->pstate & PSTATE_TCO;
7245 }
7246 
7247 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7248 {
7249     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7250 }
7251 
7252 static const ARMCPRegInfo mte_reginfo[] = {
7253     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7254       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7255       .access = PL1_RW, .accessfn = access_mte,
7256       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7257     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7258       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7259       .access = PL1_RW, .accessfn = access_tfsr_el1,
7260       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7261       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7262     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7263       .type = ARM_CP_NV2_REDIRECT,
7264       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7265       .access = PL2_RW, .accessfn = access_tfsr_el2,
7266       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7267     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7268       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7269       .access = PL3_RW,
7270       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7271     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7272       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7273       .access = PL1_RW, .accessfn = access_mte,
7274       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7275     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7276       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7277       .access = PL1_RW, .accessfn = access_mte,
7278       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7279     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7280       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7281       .type = ARM_CP_NO_RAW,
7282       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7283     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7284       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7285       .type = ARM_CP_NOP, .access = PL1_W,
7286       .fgt = FGT_DCIVAC,
7287       .accessfn = aa64_cacheop_poc_access },
7288     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7290       .fgt = FGT_DCISW,
7291       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7292     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7293       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7294       .type = ARM_CP_NOP, .access = PL1_W,
7295       .fgt = FGT_DCIVAC,
7296       .accessfn = aa64_cacheop_poc_access },
7297     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7298       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7299       .fgt = FGT_DCISW,
7300       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7301     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7302       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7303       .fgt = FGT_DCCSW,
7304       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7305     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7306       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7307       .fgt = FGT_DCCSW,
7308       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7309     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7310       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7311       .fgt = FGT_DCCISW,
7312       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7313     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7314       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7315       .fgt = FGT_DCCISW,
7316       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7317 };
7318 
7319 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7320     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7321       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7322       .type = ARM_CP_CONST, .access = PL0_RW, },
7323 };
7324 
7325 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7326     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7327       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7328       .type = ARM_CP_NOP, .access = PL0_W,
7329       .fgt = FGT_DCCVAC,
7330       .accessfn = aa64_cacheop_poc_access },
7331     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7332       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7333       .type = ARM_CP_NOP, .access = PL0_W,
7334       .fgt = FGT_DCCVAC,
7335       .accessfn = aa64_cacheop_poc_access },
7336     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7337       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7338       .type = ARM_CP_NOP, .access = PL0_W,
7339       .fgt = FGT_DCCVAP,
7340       .accessfn = aa64_cacheop_poc_access },
7341     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7342       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7343       .type = ARM_CP_NOP, .access = PL0_W,
7344       .fgt = FGT_DCCVAP,
7345       .accessfn = aa64_cacheop_poc_access },
7346     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7347       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7348       .type = ARM_CP_NOP, .access = PL0_W,
7349       .fgt = FGT_DCCVADP,
7350       .accessfn = aa64_cacheop_poc_access },
7351     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7352       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7353       .type = ARM_CP_NOP, .access = PL0_W,
7354       .fgt = FGT_DCCVADP,
7355       .accessfn = aa64_cacheop_poc_access },
7356     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7357       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7358       .type = ARM_CP_NOP, .access = PL0_W,
7359       .fgt = FGT_DCCIVAC,
7360       .accessfn = aa64_cacheop_poc_access },
7361     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7362       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7363       .type = ARM_CP_NOP, .access = PL0_W,
7364       .fgt = FGT_DCCIVAC,
7365       .accessfn = aa64_cacheop_poc_access },
7366     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7367       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7368       .access = PL0_W, .type = ARM_CP_DC_GVA,
7369 #ifndef CONFIG_USER_ONLY
7370       /* Avoid overhead of an access check that always passes in user-mode */
7371       .accessfn = aa64_zva_access,
7372       .fgt = FGT_DCZVA,
7373 #endif
7374     },
7375     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7376       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7377       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7378 #ifndef CONFIG_USER_ONLY
7379       /* Avoid overhead of an access check that always passes in user-mode */
7380       .accessfn = aa64_zva_access,
7381       .fgt = FGT_DCZVA,
7382 #endif
7383     },
7384 };
7385 
7386 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7387                                      bool isread)
7388 {
7389     uint64_t hcr = arm_hcr_el2_eff(env);
7390     int el = arm_current_el(env);
7391 
7392     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7393         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7394             if (hcr & HCR_TGE) {
7395                 return CP_ACCESS_TRAP_EL2;
7396             }
7397             return CP_ACCESS_TRAP_EL1;
7398         }
7399     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7400         return CP_ACCESS_TRAP_EL2;
7401     }
7402     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7403         return CP_ACCESS_TRAP_EL2;
7404     }
7405     if (el < 3
7406         && arm_feature(env, ARM_FEATURE_EL3)
7407         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7408         return CP_ACCESS_TRAP_EL3;
7409     }
7410     return CP_ACCESS_OK;
7411 }
7412 
7413 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7414                                          const ARMCPRegInfo *ri,
7415                                          bool isread)
7416 {
7417     CPAccessResult nv1 = access_nv1(env, ri, isread);
7418 
7419     if (nv1 != CP_ACCESS_OK) {
7420         return nv1;
7421     }
7422     return access_scxtnum(env, ri, isread);
7423 }
7424 
7425 static const ARMCPRegInfo scxtnum_reginfo[] = {
7426     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7427       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7428       .access = PL0_RW, .accessfn = access_scxtnum,
7429       .fgt = FGT_SCXTNUM_EL0,
7430       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7431     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7432       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7433       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7434       .fgt = FGT_SCXTNUM_EL1,
7435       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7436       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7437     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7438       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7439       .access = PL2_RW, .accessfn = access_scxtnum,
7440       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7441     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7443       .access = PL3_RW,
7444       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7445 };
7446 
7447 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7448                                  bool isread)
7449 {
7450     if (arm_current_el(env) == 2 &&
7451         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7452         return CP_ACCESS_TRAP_EL3;
7453     }
7454     return CP_ACCESS_OK;
7455 }
7456 
7457 static const ARMCPRegInfo fgt_reginfo[] = {
7458     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7459       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7460       .nv2_redirect_offset = 0x1b8,
7461       .access = PL2_RW, .accessfn = access_fgt,
7462       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7463     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7464       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7465       .nv2_redirect_offset = 0x1c0,
7466       .access = PL2_RW, .accessfn = access_fgt,
7467       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7468     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7469       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7470       .nv2_redirect_offset = 0x1d0,
7471       .access = PL2_RW, .accessfn = access_fgt,
7472       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7473     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7474       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7475       .nv2_redirect_offset = 0x1d8,
7476       .access = PL2_RW, .accessfn = access_fgt,
7477       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7478     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7479       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7480       .nv2_redirect_offset = 0x1c8,
7481       .access = PL2_RW, .accessfn = access_fgt,
7482       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7483 };
7484 
7485 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7486                        uint64_t value)
7487 {
7488     /*
7489      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7490      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7491      * about the RESS bits at the top -- we choose the "generate an EL2
7492      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7493      * the ptw.c code detect the resulting invalid address).
7494      */
7495     env->cp15.vncr_el2 = value & ~0xfffULL;
7496 }
7497 
7498 static const ARMCPRegInfo nv2_reginfo[] = {
7499     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7500       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7501       .access = PL2_RW,
7502       .writefn = vncr_write,
7503       .nv2_redirect_offset = 0xb0,
7504       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7505 };
7506 
7507 #endif /* TARGET_AARCH64 */
7508 
7509 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7510                                      bool isread)
7511 {
7512     int el = arm_current_el(env);
7513 
7514     if (el == 0) {
7515         uint64_t sctlr = arm_sctlr(env, el);
7516         if (!(sctlr & SCTLR_EnRCTX)) {
7517             return CP_ACCESS_TRAP_EL1;
7518         }
7519     } else if (el == 1) {
7520         uint64_t hcr = arm_hcr_el2_eff(env);
7521         if (hcr & HCR_NV) {
7522             return CP_ACCESS_TRAP_EL2;
7523         }
7524     }
7525     return CP_ACCESS_OK;
7526 }
7527 
7528 static const ARMCPRegInfo predinv_reginfo[] = {
7529     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7530       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7531       .fgt = FGT_CFPRCTX,
7532       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7533     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7534       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7535       .fgt = FGT_DVPRCTX,
7536       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7537     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7538       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7539       .fgt = FGT_CPPRCTX,
7540       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7541     /*
7542      * Note the AArch32 opcodes have a different OPC1.
7543      */
7544     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7545       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7546       .fgt = FGT_CFPRCTX,
7547       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7548     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7549       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7550       .fgt = FGT_DVPRCTX,
7551       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7552     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7553       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7554       .fgt = FGT_CPPRCTX,
7555       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7556 };
7557 
7558 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7559 {
7560     /* Read the high 32 bits of the current CCSIDR */
7561     return extract64(ccsidr_read(env, ri), 32, 32);
7562 }
7563 
7564 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7565     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7566       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7567       .access = PL1_R,
7568       .accessfn = access_tid4,
7569       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7570 };
7571 
7572 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7573                                        bool isread)
7574 {
7575     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7576         return CP_ACCESS_TRAP_EL2;
7577     }
7578 
7579     return CP_ACCESS_OK;
7580 }
7581 
7582 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7583                                        bool isread)
7584 {
7585     if (arm_feature(env, ARM_FEATURE_V8)) {
7586         return access_aa64_tid3(env, ri, isread);
7587     }
7588 
7589     return CP_ACCESS_OK;
7590 }
7591 
7592 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7593                                      bool isread)
7594 {
7595     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7596         return CP_ACCESS_TRAP_EL2;
7597     }
7598 
7599     return CP_ACCESS_OK;
7600 }
7601 
7602 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7603                                         const ARMCPRegInfo *ri, bool isread)
7604 {
7605     /*
7606      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7607      * in v7A, not in v8A.
7608      */
7609     if (!arm_feature(env, ARM_FEATURE_V8) &&
7610         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7611         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7612         return CP_ACCESS_TRAP_EL2;
7613     }
7614     return CP_ACCESS_OK;
7615 }
7616 
7617 static const ARMCPRegInfo jazelle_regs[] = {
7618     { .name = "JIDR",
7619       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7620       .access = PL1_R, .accessfn = access_jazelle,
7621       .type = ARM_CP_CONST, .resetvalue = 0 },
7622     { .name = "JOSCR",
7623       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7624       .accessfn = access_joscr_jmcr,
7625       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7626     { .name = "JMCR",
7627       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7628       .accessfn = access_joscr_jmcr,
7629       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7630 };
7631 
7632 static const ARMCPRegInfo contextidr_el2 = {
7633     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7634     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7635     .access = PL2_RW,
7636     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7637 };
7638 
7639 static const ARMCPRegInfo vhe_reginfo[] = {
7640     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7641       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7642       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7643       .raw_writefn = raw_write,
7644       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7645 #ifndef CONFIG_USER_ONLY
7646     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7647       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7648       .fieldoffset =
7649         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7650       .type = ARM_CP_IO, .access = PL2_RW,
7651       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7652     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7653       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7654       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7655       .resetfn = gt_hv_timer_reset,
7656       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7657     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7658       .type = ARM_CP_IO,
7659       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7660       .access = PL2_RW,
7661       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7662       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7663     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7664       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7665       .type = ARM_CP_IO | ARM_CP_ALIAS,
7666       .access = PL2_RW, .accessfn = access_el1nvpct,
7667       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7668       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7669       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7670     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7671       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7672       .type = ARM_CP_IO | ARM_CP_ALIAS,
7673       .access = PL2_RW, .accessfn = access_el1nvvct,
7674       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7675       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7676       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7677     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7678       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7679       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7680       .access = PL2_RW, .accessfn = e2h_access,
7681       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7682     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7683       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7684       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7685       .access = PL2_RW, .accessfn = e2h_access,
7686       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7687     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7688       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7689       .type = ARM_CP_IO | ARM_CP_ALIAS,
7690       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7691       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7692       .access = PL2_RW, .accessfn = access_el1nvpct,
7693       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7694     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7695       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7696       .type = ARM_CP_IO | ARM_CP_ALIAS,
7697       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7698       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7699       .access = PL2_RW, .accessfn = access_el1nvvct,
7700       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7701 #endif
7702 };
7703 
7704 #ifndef CONFIG_USER_ONLY
7705 static const ARMCPRegInfo ats1e1_reginfo[] = {
7706     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7707       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7708       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7709       .fgt = FGT_ATS1E1RP,
7710       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7711     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7712       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7713       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7714       .fgt = FGT_ATS1E1WP,
7715       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7716 };
7717 
7718 static const ARMCPRegInfo ats1cp_reginfo[] = {
7719     { .name = "ATS1CPRP",
7720       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7721       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7722       .writefn = ats_write },
7723     { .name = "ATS1CPWP",
7724       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7725       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7726       .writefn = ats_write },
7727 };
7728 #endif
7729 
7730 /*
7731  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7732  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7733  * is non-zero, which is never for ARMv7, optionally in ARMv8
7734  * and mandatorily for ARMv8.2 and up.
7735  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7736  * implementation is RAZ/WI we can ignore this detail, as we
7737  * do for ACTLR.
7738  */
7739 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7740     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7741       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7742       .access = PL1_RW, .accessfn = access_tacr,
7743       .type = ARM_CP_CONST, .resetvalue = 0 },
7744     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7745       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7746       .access = PL2_RW, .type = ARM_CP_CONST,
7747       .resetvalue = 0 },
7748 };
7749 
7750 void register_cp_regs_for_features(ARMCPU *cpu)
7751 {
7752     /* Register all the coprocessor registers based on feature bits */
7753     CPUARMState *env = &cpu->env;
7754     if (arm_feature(env, ARM_FEATURE_M)) {
7755         /* M profile has no coprocessor registers */
7756         return;
7757     }
7758 
7759     define_arm_cp_regs(cpu, cp_reginfo);
7760     if (!arm_feature(env, ARM_FEATURE_V8)) {
7761         /*
7762          * Must go early as it is full of wildcards that may be
7763          * overridden by later definitions.
7764          */
7765         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7766     }
7767 
7768     define_tlb_insn_regs(cpu);
7769 
7770     if (arm_feature(env, ARM_FEATURE_V6)) {
7771         /* The ID registers all have impdef reset values */
7772         ARMCPRegInfo v6_idregs[] = {
7773             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7774               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7775               .access = PL1_R, .type = ARM_CP_CONST,
7776               .accessfn = access_aa32_tid3,
7777               .resetvalue = cpu->isar.id_pfr0 },
7778             /*
7779              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7780              * the value of the GIC field until after we define these regs.
7781              */
7782             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7783               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7784               .access = PL1_R, .type = ARM_CP_NO_RAW,
7785               .accessfn = access_aa32_tid3,
7786 #ifdef CONFIG_USER_ONLY
7787               .type = ARM_CP_CONST,
7788               .resetvalue = cpu->isar.id_pfr1,
7789 #else
7790               .type = ARM_CP_NO_RAW,
7791               .accessfn = access_aa32_tid3,
7792               .readfn = id_pfr1_read,
7793               .writefn = arm_cp_write_ignore
7794 #endif
7795             },
7796             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7797               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7798               .access = PL1_R, .type = ARM_CP_CONST,
7799               .accessfn = access_aa32_tid3,
7800               .resetvalue = cpu->isar.id_dfr0 },
7801             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7802               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7803               .access = PL1_R, .type = ARM_CP_CONST,
7804               .accessfn = access_aa32_tid3,
7805               .resetvalue = cpu->id_afr0 },
7806             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7807               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7808               .access = PL1_R, .type = ARM_CP_CONST,
7809               .accessfn = access_aa32_tid3,
7810               .resetvalue = cpu->isar.id_mmfr0 },
7811             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7812               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7813               .access = PL1_R, .type = ARM_CP_CONST,
7814               .accessfn = access_aa32_tid3,
7815               .resetvalue = cpu->isar.id_mmfr1 },
7816             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7817               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7818               .access = PL1_R, .type = ARM_CP_CONST,
7819               .accessfn = access_aa32_tid3,
7820               .resetvalue = cpu->isar.id_mmfr2 },
7821             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7822               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7823               .access = PL1_R, .type = ARM_CP_CONST,
7824               .accessfn = access_aa32_tid3,
7825               .resetvalue = cpu->isar.id_mmfr3 },
7826             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7827               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7828               .access = PL1_R, .type = ARM_CP_CONST,
7829               .accessfn = access_aa32_tid3,
7830               .resetvalue = cpu->isar.id_isar0 },
7831             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7832               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7833               .access = PL1_R, .type = ARM_CP_CONST,
7834               .accessfn = access_aa32_tid3,
7835               .resetvalue = cpu->isar.id_isar1 },
7836             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7837               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7838               .access = PL1_R, .type = ARM_CP_CONST,
7839               .accessfn = access_aa32_tid3,
7840               .resetvalue = cpu->isar.id_isar2 },
7841             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7842               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7843               .access = PL1_R, .type = ARM_CP_CONST,
7844               .accessfn = access_aa32_tid3,
7845               .resetvalue = cpu->isar.id_isar3 },
7846             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7847               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7848               .access = PL1_R, .type = ARM_CP_CONST,
7849               .accessfn = access_aa32_tid3,
7850               .resetvalue = cpu->isar.id_isar4 },
7851             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7852               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7853               .access = PL1_R, .type = ARM_CP_CONST,
7854               .accessfn = access_aa32_tid3,
7855               .resetvalue = cpu->isar.id_isar5 },
7856             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7857               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7858               .access = PL1_R, .type = ARM_CP_CONST,
7859               .accessfn = access_aa32_tid3,
7860               .resetvalue = cpu->isar.id_mmfr4 },
7861             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7862               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7863               .access = PL1_R, .type = ARM_CP_CONST,
7864               .accessfn = access_aa32_tid3,
7865               .resetvalue = cpu->isar.id_isar6 },
7866         };
7867         define_arm_cp_regs(cpu, v6_idregs);
7868         define_arm_cp_regs(cpu, v6_cp_reginfo);
7869     } else {
7870         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7871     }
7872     if (arm_feature(env, ARM_FEATURE_V6K)) {
7873         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7874     }
7875     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7876         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7877     }
7878     if (arm_feature(env, ARM_FEATURE_V7)) {
7879         ARMCPRegInfo clidr = {
7880             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7881             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7882             .access = PL1_R, .type = ARM_CP_CONST,
7883             .accessfn = access_tid4,
7884             .fgt = FGT_CLIDR_EL1,
7885             .resetvalue = cpu->clidr
7886         };
7887         define_one_arm_cp_reg(cpu, &clidr);
7888         define_arm_cp_regs(cpu, v7_cp_reginfo);
7889         define_debug_regs(cpu);
7890         define_pmu_regs(cpu);
7891     } else {
7892         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7893     }
7894     if (arm_feature(env, ARM_FEATURE_V8)) {
7895         /*
7896          * v8 ID registers, which all have impdef reset values.
7897          * Note that within the ID register ranges the unused slots
7898          * must all RAZ, not UNDEF; future architecture versions may
7899          * define new registers here.
7900          * ID registers which are AArch64 views of the AArch32 ID registers
7901          * which already existed in v6 and v7 are handled elsewhere,
7902          * in v6_idregs[].
7903          */
7904         int i;
7905         ARMCPRegInfo v8_idregs[] = {
7906             /*
7907              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7908              * emulation because we don't know the right value for the
7909              * GIC field until after we define these regs.
7910              */
7911             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7912               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7913               .access = PL1_R,
7914 #ifdef CONFIG_USER_ONLY
7915               .type = ARM_CP_CONST,
7916               .resetvalue = cpu->isar.id_aa64pfr0
7917 #else
7918               .type = ARM_CP_NO_RAW,
7919               .accessfn = access_aa64_tid3,
7920               .readfn = id_aa64pfr0_read,
7921               .writefn = arm_cp_write_ignore
7922 #endif
7923             },
7924             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7925               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7926               .access = PL1_R, .type = ARM_CP_CONST,
7927               .accessfn = access_aa64_tid3,
7928               .resetvalue = cpu->isar.id_aa64pfr1},
7929             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7930               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7931               .access = PL1_R, .type = ARM_CP_CONST,
7932               .accessfn = access_aa64_tid3,
7933               .resetvalue = 0 },
7934             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7935               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7936               .access = PL1_R, .type = ARM_CP_CONST,
7937               .accessfn = access_aa64_tid3,
7938               .resetvalue = 0 },
7939             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7940               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7941               .access = PL1_R, .type = ARM_CP_CONST,
7942               .accessfn = access_aa64_tid3,
7943               .resetvalue = cpu->isar.id_aa64zfr0 },
7944             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7945               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7946               .access = PL1_R, .type = ARM_CP_CONST,
7947               .accessfn = access_aa64_tid3,
7948               .resetvalue = cpu->isar.id_aa64smfr0 },
7949             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7950               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7951               .access = PL1_R, .type = ARM_CP_CONST,
7952               .accessfn = access_aa64_tid3,
7953               .resetvalue = 0 },
7954             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7955               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7956               .access = PL1_R, .type = ARM_CP_CONST,
7957               .accessfn = access_aa64_tid3,
7958               .resetvalue = 0 },
7959             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7960               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7961               .access = PL1_R, .type = ARM_CP_CONST,
7962               .accessfn = access_aa64_tid3,
7963               .resetvalue = cpu->isar.id_aa64dfr0 },
7964             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7965               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7966               .access = PL1_R, .type = ARM_CP_CONST,
7967               .accessfn = access_aa64_tid3,
7968               .resetvalue = cpu->isar.id_aa64dfr1 },
7969             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7970               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7971               .access = PL1_R, .type = ARM_CP_CONST,
7972               .accessfn = access_aa64_tid3,
7973               .resetvalue = 0 },
7974             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7975               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7976               .access = PL1_R, .type = ARM_CP_CONST,
7977               .accessfn = access_aa64_tid3,
7978               .resetvalue = 0 },
7979             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7980               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7981               .access = PL1_R, .type = ARM_CP_CONST,
7982               .accessfn = access_aa64_tid3,
7983               .resetvalue = cpu->id_aa64afr0 },
7984             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7985               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7986               .access = PL1_R, .type = ARM_CP_CONST,
7987               .accessfn = access_aa64_tid3,
7988               .resetvalue = cpu->id_aa64afr1 },
7989             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7990               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7991               .access = PL1_R, .type = ARM_CP_CONST,
7992               .accessfn = access_aa64_tid3,
7993               .resetvalue = 0 },
7994             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7995               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7996               .access = PL1_R, .type = ARM_CP_CONST,
7997               .accessfn = access_aa64_tid3,
7998               .resetvalue = 0 },
7999             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8000               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8001               .access = PL1_R, .type = ARM_CP_CONST,
8002               .accessfn = access_aa64_tid3,
8003               .resetvalue = cpu->isar.id_aa64isar0 },
8004             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8005               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8006               .access = PL1_R, .type = ARM_CP_CONST,
8007               .accessfn = access_aa64_tid3,
8008               .resetvalue = cpu->isar.id_aa64isar1 },
8009             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8010               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8011               .access = PL1_R, .type = ARM_CP_CONST,
8012               .accessfn = access_aa64_tid3,
8013               .resetvalue = cpu->isar.id_aa64isar2 },
8014             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8015               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8016               .access = PL1_R, .type = ARM_CP_CONST,
8017               .accessfn = access_aa64_tid3,
8018               .resetvalue = 0 },
8019             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8020               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8021               .access = PL1_R, .type = ARM_CP_CONST,
8022               .accessfn = access_aa64_tid3,
8023               .resetvalue = 0 },
8024             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8025               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8026               .access = PL1_R, .type = ARM_CP_CONST,
8027               .accessfn = access_aa64_tid3,
8028               .resetvalue = 0 },
8029             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8030               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8031               .access = PL1_R, .type = ARM_CP_CONST,
8032               .accessfn = access_aa64_tid3,
8033               .resetvalue = 0 },
8034             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8035               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8036               .access = PL1_R, .type = ARM_CP_CONST,
8037               .accessfn = access_aa64_tid3,
8038               .resetvalue = 0 },
8039             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8040               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8041               .access = PL1_R, .type = ARM_CP_CONST,
8042               .accessfn = access_aa64_tid3,
8043               .resetvalue = cpu->isar.id_aa64mmfr0 },
8044             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8045               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8046               .access = PL1_R, .type = ARM_CP_CONST,
8047               .accessfn = access_aa64_tid3,
8048               .resetvalue = cpu->isar.id_aa64mmfr1 },
8049             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8050               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8051               .access = PL1_R, .type = ARM_CP_CONST,
8052               .accessfn = access_aa64_tid3,
8053               .resetvalue = cpu->isar.id_aa64mmfr2 },
8054             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
8055               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8056               .access = PL1_R, .type = ARM_CP_CONST,
8057               .accessfn = access_aa64_tid3,
8058               .resetvalue = cpu->isar.id_aa64mmfr3 },
8059             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8060               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8061               .access = PL1_R, .type = ARM_CP_CONST,
8062               .accessfn = access_aa64_tid3,
8063               .resetvalue = 0 },
8064             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8065               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8066               .access = PL1_R, .type = ARM_CP_CONST,
8067               .accessfn = access_aa64_tid3,
8068               .resetvalue = 0 },
8069             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8070               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8071               .access = PL1_R, .type = ARM_CP_CONST,
8072               .accessfn = access_aa64_tid3,
8073               .resetvalue = 0 },
8074             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8075               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8076               .access = PL1_R, .type = ARM_CP_CONST,
8077               .accessfn = access_aa64_tid3,
8078               .resetvalue = 0 },
8079             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8080               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8081               .access = PL1_R, .type = ARM_CP_CONST,
8082               .accessfn = access_aa64_tid3,
8083               .resetvalue = cpu->isar.mvfr0 },
8084             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8085               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8086               .access = PL1_R, .type = ARM_CP_CONST,
8087               .accessfn = access_aa64_tid3,
8088               .resetvalue = cpu->isar.mvfr1 },
8089             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8090               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8091               .access = PL1_R, .type = ARM_CP_CONST,
8092               .accessfn = access_aa64_tid3,
8093               .resetvalue = cpu->isar.mvfr2 },
8094             /*
8095              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8096              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8097              * as RAZ, since it is in the "reserved for future ID
8098              * registers, RAZ" part of the AArch32 encoding space.
8099              */
8100             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8101               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8102               .access = PL1_R, .type = ARM_CP_CONST,
8103               .accessfn = access_aa64_tid3,
8104               .resetvalue = 0 },
8105             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8106               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8107               .access = PL1_R, .type = ARM_CP_CONST,
8108               .accessfn = access_aa64_tid3,
8109               .resetvalue = 0 },
8110             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8111               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8112               .access = PL1_R, .type = ARM_CP_CONST,
8113               .accessfn = access_aa64_tid3,
8114               .resetvalue = 0 },
8115             /*
8116              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8117              * they're also RAZ for AArch64, and in v8 are gradually
8118              * being filled with AArch64-view-of-AArch32-ID-register
8119              * for new ID registers.
8120              */
8121             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8122               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8123               .access = PL1_R, .type = ARM_CP_CONST,
8124               .accessfn = access_aa64_tid3,
8125               .resetvalue = 0 },
8126             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8127               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8128               .access = PL1_R, .type = ARM_CP_CONST,
8129               .accessfn = access_aa64_tid3,
8130               .resetvalue = cpu->isar.id_pfr2 },
8131             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8132               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8133               .access = PL1_R, .type = ARM_CP_CONST,
8134               .accessfn = access_aa64_tid3,
8135               .resetvalue = cpu->isar.id_dfr1 },
8136             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8137               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8138               .access = PL1_R, .type = ARM_CP_CONST,
8139               .accessfn = access_aa64_tid3,
8140               .resetvalue = cpu->isar.id_mmfr5 },
8141             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8142               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8143               .access = PL1_R, .type = ARM_CP_CONST,
8144               .accessfn = access_aa64_tid3,
8145               .resetvalue = 0 },
8146             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8147               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8148               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8149               .fgt = FGT_PMCEIDN_EL0,
8150               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8151             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8152               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8153               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8154               .fgt = FGT_PMCEIDN_EL0,
8155               .resetvalue = cpu->pmceid0 },
8156             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8157               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8158               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8159               .fgt = FGT_PMCEIDN_EL0,
8160               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8161             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8162               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8163               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8164               .fgt = FGT_PMCEIDN_EL0,
8165               .resetvalue = cpu->pmceid1 },
8166         };
8167 #ifdef CONFIG_USER_ONLY
8168         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8169             { .name = "ID_AA64PFR0_EL1",
8170               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8171                                R_ID_AA64PFR0_ADVSIMD_MASK |
8172                                R_ID_AA64PFR0_SVE_MASK |
8173                                R_ID_AA64PFR0_DIT_MASK,
8174               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8175                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8176             { .name = "ID_AA64PFR1_EL1",
8177               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8178                                R_ID_AA64PFR1_SSBS_MASK |
8179                                R_ID_AA64PFR1_MTE_MASK |
8180                                R_ID_AA64PFR1_SME_MASK },
8181             { .name = "ID_AA64PFR*_EL1_RESERVED",
8182               .is_glob = true },
8183             { .name = "ID_AA64ZFR0_EL1",
8184               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8185                                R_ID_AA64ZFR0_AES_MASK |
8186                                R_ID_AA64ZFR0_BITPERM_MASK |
8187                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8188                                R_ID_AA64ZFR0_B16B16_MASK |
8189                                R_ID_AA64ZFR0_SHA3_MASK |
8190                                R_ID_AA64ZFR0_SM4_MASK |
8191                                R_ID_AA64ZFR0_I8MM_MASK |
8192                                R_ID_AA64ZFR0_F32MM_MASK |
8193                                R_ID_AA64ZFR0_F64MM_MASK },
8194             { .name = "ID_AA64SMFR0_EL1",
8195               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8196                                R_ID_AA64SMFR0_BI32I32_MASK |
8197                                R_ID_AA64SMFR0_B16F32_MASK |
8198                                R_ID_AA64SMFR0_F16F32_MASK |
8199                                R_ID_AA64SMFR0_I8I32_MASK |
8200                                R_ID_AA64SMFR0_F16F16_MASK |
8201                                R_ID_AA64SMFR0_B16B16_MASK |
8202                                R_ID_AA64SMFR0_I16I32_MASK |
8203                                R_ID_AA64SMFR0_F64F64_MASK |
8204                                R_ID_AA64SMFR0_I16I64_MASK |
8205                                R_ID_AA64SMFR0_SMEVER_MASK |
8206                                R_ID_AA64SMFR0_FA64_MASK },
8207             { .name = "ID_AA64MMFR0_EL1",
8208               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8209               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8210                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8211             { .name = "ID_AA64MMFR1_EL1",
8212               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8213             { .name = "ID_AA64MMFR2_EL1",
8214               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8215             { .name = "ID_AA64MMFR3_EL1",
8216               .exported_bits = 0 },
8217             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8218               .is_glob = true },
8219             { .name = "ID_AA64DFR0_EL1",
8220               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8221             { .name = "ID_AA64DFR1_EL1" },
8222             { .name = "ID_AA64DFR*_EL1_RESERVED",
8223               .is_glob = true },
8224             { .name = "ID_AA64AFR*",
8225               .is_glob = true },
8226             { .name = "ID_AA64ISAR0_EL1",
8227               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8228                                R_ID_AA64ISAR0_SHA1_MASK |
8229                                R_ID_AA64ISAR0_SHA2_MASK |
8230                                R_ID_AA64ISAR0_CRC32_MASK |
8231                                R_ID_AA64ISAR0_ATOMIC_MASK |
8232                                R_ID_AA64ISAR0_RDM_MASK |
8233                                R_ID_AA64ISAR0_SHA3_MASK |
8234                                R_ID_AA64ISAR0_SM3_MASK |
8235                                R_ID_AA64ISAR0_SM4_MASK |
8236                                R_ID_AA64ISAR0_DP_MASK |
8237                                R_ID_AA64ISAR0_FHM_MASK |
8238                                R_ID_AA64ISAR0_TS_MASK |
8239                                R_ID_AA64ISAR0_RNDR_MASK },
8240             { .name = "ID_AA64ISAR1_EL1",
8241               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8242                                R_ID_AA64ISAR1_APA_MASK |
8243                                R_ID_AA64ISAR1_API_MASK |
8244                                R_ID_AA64ISAR1_JSCVT_MASK |
8245                                R_ID_AA64ISAR1_FCMA_MASK |
8246                                R_ID_AA64ISAR1_LRCPC_MASK |
8247                                R_ID_AA64ISAR1_GPA_MASK |
8248                                R_ID_AA64ISAR1_GPI_MASK |
8249                                R_ID_AA64ISAR1_FRINTTS_MASK |
8250                                R_ID_AA64ISAR1_SB_MASK |
8251                                R_ID_AA64ISAR1_BF16_MASK |
8252                                R_ID_AA64ISAR1_DGH_MASK |
8253                                R_ID_AA64ISAR1_I8MM_MASK },
8254             { .name = "ID_AA64ISAR2_EL1",
8255               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8256                                R_ID_AA64ISAR2_RPRES_MASK |
8257                                R_ID_AA64ISAR2_GPA3_MASK |
8258                                R_ID_AA64ISAR2_APA3_MASK |
8259                                R_ID_AA64ISAR2_MOPS_MASK |
8260                                R_ID_AA64ISAR2_BC_MASK |
8261                                R_ID_AA64ISAR2_RPRFM_MASK |
8262                                R_ID_AA64ISAR2_CSSC_MASK },
8263             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8264               .is_glob = true },
8265         };
8266         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8267 #endif
8268         /*
8269          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8270          * TODO: For RMR, a write with bit 1 set should do something with
8271          * cpu_reset(). In the meantime, "the bit is strictly a request",
8272          * so we are in spec just ignoring writes.
8273          */
8274         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8275             !arm_feature(env, ARM_FEATURE_EL2)) {
8276             ARMCPRegInfo el1_reset_regs[] = {
8277                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8278                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8279                   .access = PL1_R,
8280                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8281                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8282                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8283                   .access = PL1_RW, .type = ARM_CP_CONST,
8284                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8285             };
8286             define_arm_cp_regs(cpu, el1_reset_regs);
8287         }
8288         define_arm_cp_regs(cpu, v8_idregs);
8289         define_arm_cp_regs(cpu, v8_cp_reginfo);
8290         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8291             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8292         }
8293 
8294         for (i = 4; i < 16; i++) {
8295             /*
8296              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8297              * For pre-v8 cores there are RAZ patterns for these in
8298              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8299              * v8 extends the "must RAZ" part of the ID register space
8300              * to also cover c0, 0, c{8-15}, {0-7}.
8301              * These are STATE_AA32 because in the AArch64 sysreg space
8302              * c4-c7 is where the AArch64 ID registers live (and we've
8303              * already defined those in v8_idregs[]), and c8-c15 are not
8304              * "must RAZ" for AArch64.
8305              */
8306             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8307             ARMCPRegInfo v8_aa32_raz_idregs = {
8308                 .name = name,
8309                 .state = ARM_CP_STATE_AA32,
8310                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8311                 .access = PL1_R, .type = ARM_CP_CONST,
8312                 .accessfn = access_aa64_tid3,
8313                 .resetvalue = 0 };
8314             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8315         }
8316     }
8317 
8318     /*
8319      * Register the base EL2 cpregs.
8320      * Pre v8, these registers are implemented only as part of the
8321      * Virtualization Extensions (EL2 present).  Beginning with v8,
8322      * if EL2 is missing but EL3 is enabled, mostly these become
8323      * RES0 from EL3, with some specific exceptions.
8324      */
8325     if (arm_feature(env, ARM_FEATURE_EL2)
8326         || (arm_feature(env, ARM_FEATURE_EL3)
8327             && arm_feature(env, ARM_FEATURE_V8))) {
8328         uint64_t vmpidr_def = mpidr_read_val(env);
8329         ARMCPRegInfo vpidr_regs[] = {
8330             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8331               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8332               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8333               .resetvalue = cpu->midr,
8334               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8335               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8336             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8337               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8338               .access = PL2_RW, .resetvalue = cpu->midr,
8339               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8340               .nv2_redirect_offset = 0x88,
8341               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8342             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8343               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8344               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8345               .resetvalue = vmpidr_def,
8346               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8347               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8348             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8349               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8350               .access = PL2_RW, .resetvalue = vmpidr_def,
8351               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8352               .nv2_redirect_offset = 0x50,
8353               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8354         };
8355         /*
8356          * The only field of MDCR_EL2 that has a defined architectural reset
8357          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8358          */
8359         ARMCPRegInfo mdcr_el2 = {
8360             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8361             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8362             .writefn = mdcr_el2_write,
8363             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8364             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8365         };
8366         define_one_arm_cp_reg(cpu, &mdcr_el2);
8367         define_arm_cp_regs(cpu, vpidr_regs);
8368         define_arm_cp_regs(cpu, el2_cp_reginfo);
8369         if (arm_feature(env, ARM_FEATURE_V8)) {
8370             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8371         }
8372         if (cpu_isar_feature(aa64_sel2, cpu)) {
8373             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8374         }
8375         /*
8376          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8377          * See commentary near RMR_EL1.
8378          */
8379         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8380             static const ARMCPRegInfo el2_reset_regs[] = {
8381                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8382                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8383                   .access = PL2_R,
8384                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8385                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8386                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8387                   .access = PL2_R,
8388                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8389                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8390                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8391                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8392             };
8393             define_arm_cp_regs(cpu, el2_reset_regs);
8394         }
8395     }
8396 
8397     /* Register the base EL3 cpregs. */
8398     if (arm_feature(env, ARM_FEATURE_EL3)) {
8399         define_arm_cp_regs(cpu, el3_cp_reginfo);
8400         ARMCPRegInfo el3_regs[] = {
8401             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8402               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8403               .access = PL3_R,
8404               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8405             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8406               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8407               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8408             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8409               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8410               .access = PL3_RW, .type = ARM_CP_CONST,
8411               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8412             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8413               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8414               .access = PL3_RW,
8415               .raw_writefn = raw_write, .writefn = sctlr_write,
8416               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8417               .resetvalue = cpu->reset_sctlr },
8418         };
8419 
8420         define_arm_cp_regs(cpu, el3_regs);
8421     }
8422     /*
8423      * The behaviour of NSACR is sufficiently various that we don't
8424      * try to describe it in a single reginfo:
8425      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8426      *     reads as constant 0xc00 from NS EL1 and NS EL2
8427      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8428      *  if v7 without EL3, register doesn't exist
8429      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8430      */
8431     if (arm_feature(env, ARM_FEATURE_EL3)) {
8432         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8433             static const ARMCPRegInfo nsacr = {
8434                 .name = "NSACR", .type = ARM_CP_CONST,
8435                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8436                 .access = PL1_RW, .accessfn = nsacr_access,
8437                 .resetvalue = 0xc00
8438             };
8439             define_one_arm_cp_reg(cpu, &nsacr);
8440         } else {
8441             static const ARMCPRegInfo nsacr = {
8442                 .name = "NSACR",
8443                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8444                 .access = PL3_RW | PL1_R,
8445                 .resetvalue = 0,
8446                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8447             };
8448             define_one_arm_cp_reg(cpu, &nsacr);
8449         }
8450     } else {
8451         if (arm_feature(env, ARM_FEATURE_V8)) {
8452             static const ARMCPRegInfo nsacr = {
8453                 .name = "NSACR", .type = ARM_CP_CONST,
8454                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8455                 .access = PL1_R,
8456                 .resetvalue = 0xc00
8457             };
8458             define_one_arm_cp_reg(cpu, &nsacr);
8459         }
8460     }
8461 
8462     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8463         if (arm_feature(env, ARM_FEATURE_V6)) {
8464             /* PMSAv6 not implemented */
8465             assert(arm_feature(env, ARM_FEATURE_V7));
8466             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8467             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8468         } else {
8469             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8470         }
8471     } else {
8472         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8473         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8474         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8475         if (cpu_isar_feature(aa32_hpd, cpu)) {
8476             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8477         }
8478     }
8479     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8480         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8481     }
8482     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8483         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8484     }
8485     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8486         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8487     }
8488 #ifndef CONFIG_USER_ONLY
8489     if (cpu_isar_feature(aa64_ecv, cpu)) {
8490         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8491     }
8492 #endif
8493     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8494         ARMCPRegInfo vapa_cp_reginfo[] = {
8495             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8496               .access = PL1_RW, .resetvalue = 0,
8497               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8498                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8499               .writefn = par_write},
8500 #ifndef CONFIG_USER_ONLY
8501             /* This underdecoding is safe because the reginfo is NO_RAW. */
8502             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8503               .access = PL1_W, .accessfn = ats_access,
8504               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8505 #endif
8506         };
8507 
8508         /*
8509          * When LPAE exists this 32-bit PAR register is an alias of the
8510          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8511          */
8512         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8513             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8514         }
8515         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8516     }
8517     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8518         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8519     }
8520     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8521         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8522     }
8523     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8524         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8525     }
8526     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8527         define_arm_cp_regs(cpu, omap_cp_reginfo);
8528     }
8529     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8530         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8531     }
8532     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8533         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8534     }
8535     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8536         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8537     }
8538     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8539         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8540     }
8541     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8542         define_arm_cp_regs(cpu, jazelle_regs);
8543     }
8544     /*
8545      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8546      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8547      * be read-only (ie write causes UNDEF exception).
8548      */
8549     {
8550         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8551             /*
8552              * Pre-v8 MIDR space.
8553              * Note that the MIDR isn't a simple constant register because
8554              * of the TI925 behaviour where writes to another register can
8555              * cause the MIDR value to change.
8556              *
8557              * Unimplemented registers in the c15 0 0 0 space default to
8558              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8559              * and friends override accordingly.
8560              */
8561             { .name = "MIDR",
8562               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8563               .access = PL1_R, .resetvalue = cpu->midr,
8564               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8565               .readfn = midr_read,
8566               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8567               .type = ARM_CP_OVERRIDE },
8568             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8569             { .name = "DUMMY",
8570               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8571               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8572             { .name = "DUMMY",
8573               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8574               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8575             { .name = "DUMMY",
8576               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8577               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8578             { .name = "DUMMY",
8579               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8580               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8581             { .name = "DUMMY",
8582               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8583               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8584         };
8585         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8586             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8587               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8588               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8589               .fgt = FGT_MIDR_EL1,
8590               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8591               .readfn = midr_read },
8592             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8593             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8594               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8595               .access = PL1_R, .resetvalue = cpu->midr },
8596             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8597               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8598               .access = PL1_R,
8599               .accessfn = access_aa64_tid1,
8600               .fgt = FGT_REVIDR_EL1,
8601               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8602         };
8603         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8604             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8605             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8606             .access = PL1_R, .resetvalue = cpu->midr
8607         };
8608         ARMCPRegInfo id_cp_reginfo[] = {
8609             /* These are common to v8 and pre-v8 */
8610             { .name = "CTR",
8611               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8612               .access = PL1_R, .accessfn = ctr_el0_access,
8613               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8614             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8615               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8616               .access = PL0_R, .accessfn = ctr_el0_access,
8617               .fgt = FGT_CTR_EL0,
8618               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8619             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8620             { .name = "TCMTR",
8621               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8622               .access = PL1_R,
8623               .accessfn = access_aa32_tid1,
8624               .type = ARM_CP_CONST, .resetvalue = 0 },
8625         };
8626         /* TLBTR is specific to VMSA */
8627         ARMCPRegInfo id_tlbtr_reginfo = {
8628               .name = "TLBTR",
8629               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8630               .access = PL1_R,
8631               .accessfn = access_aa32_tid1,
8632               .type = ARM_CP_CONST, .resetvalue = 0,
8633         };
8634         /* MPUIR is specific to PMSA V6+ */
8635         ARMCPRegInfo id_mpuir_reginfo = {
8636               .name = "MPUIR",
8637               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8638               .access = PL1_R, .type = ARM_CP_CONST,
8639               .resetvalue = cpu->pmsav7_dregion << 8
8640         };
8641         /* HMPUIR is specific to PMSA V8 */
8642         ARMCPRegInfo id_hmpuir_reginfo = {
8643             .name = "HMPUIR",
8644             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8645             .access = PL2_R, .type = ARM_CP_CONST,
8646             .resetvalue = cpu->pmsav8r_hdregion
8647         };
8648         static const ARMCPRegInfo crn0_wi_reginfo = {
8649             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8650             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8651             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8652         };
8653 #ifdef CONFIG_USER_ONLY
8654         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8655             { .name = "MIDR_EL1",
8656               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8657                                R_MIDR_EL1_PARTNUM_MASK |
8658                                R_MIDR_EL1_ARCHITECTURE_MASK |
8659                                R_MIDR_EL1_VARIANT_MASK |
8660                                R_MIDR_EL1_IMPLEMENTER_MASK },
8661             { .name = "REVIDR_EL1" },
8662         };
8663         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8664 #endif
8665         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8666             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8667             size_t i;
8668             /*
8669              * Register the blanket "writes ignored" value first to cover the
8670              * whole space. Then update the specific ID registers to allow write
8671              * access, so that they ignore writes rather than causing them to
8672              * UNDEF.
8673              */
8674             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8675             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8676                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8677             }
8678             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8679                 id_cp_reginfo[i].access = PL1_RW;
8680             }
8681             id_mpuir_reginfo.access = PL1_RW;
8682             id_tlbtr_reginfo.access = PL1_RW;
8683         }
8684         if (arm_feature(env, ARM_FEATURE_V8)) {
8685             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8686             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8687                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8688             }
8689         } else {
8690             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8691         }
8692         define_arm_cp_regs(cpu, id_cp_reginfo);
8693         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8694             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8695         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8696                    arm_feature(env, ARM_FEATURE_V8)) {
8697             uint32_t i = 0;
8698             char *tmp_string;
8699 
8700             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8701             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8702             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8703 
8704             /* Register alias is only valid for first 32 indexes */
8705             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8706                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8707                 uint8_t opc1 = extract32(i, 4, 1);
8708                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8709 
8710                 tmp_string = g_strdup_printf("PRBAR%u", i);
8711                 ARMCPRegInfo tmp_prbarn_reginfo = {
8712                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8713                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8714                     .access = PL1_RW, .resetvalue = 0,
8715                     .accessfn = access_tvm_trvm,
8716                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8717                 };
8718                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8719                 g_free(tmp_string);
8720 
8721                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8722                 tmp_string = g_strdup_printf("PRLAR%u", i);
8723                 ARMCPRegInfo tmp_prlarn_reginfo = {
8724                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8725                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8726                     .access = PL1_RW, .resetvalue = 0,
8727                     .accessfn = access_tvm_trvm,
8728                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8729                 };
8730                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8731                 g_free(tmp_string);
8732             }
8733 
8734             /* Register alias is only valid for first 32 indexes */
8735             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8736                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8737                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8738                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8739 
8740                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8741                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8742                     .name = tmp_string,
8743                     .type = ARM_CP_NO_RAW,
8744                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8745                     .access = PL2_RW, .resetvalue = 0,
8746                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8747                 };
8748                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8749                 g_free(tmp_string);
8750 
8751                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8752                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8753                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8754                     .name = tmp_string,
8755                     .type = ARM_CP_NO_RAW,
8756                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8757                     .access = PL2_RW, .resetvalue = 0,
8758                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8759                 };
8760                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8761                 g_free(tmp_string);
8762             }
8763         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8764             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8765         }
8766     }
8767 
8768     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8769         ARMCPRegInfo mpidr_cp_reginfo[] = {
8770             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8771               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8772               .fgt = FGT_MPIDR_EL1,
8773               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8774         };
8775 #ifdef CONFIG_USER_ONLY
8776         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8777             { .name = "MPIDR_EL1",
8778               .fixed_bits = 0x0000000080000000 },
8779         };
8780         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8781 #endif
8782         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8783     }
8784 
8785     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8786         ARMCPRegInfo auxcr_reginfo[] = {
8787             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8788               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8789               .access = PL1_RW, .accessfn = access_tacr,
8790               .nv2_redirect_offset = 0x118,
8791               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8792             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8793               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8794               .access = PL2_RW, .type = ARM_CP_CONST,
8795               .resetvalue = 0 },
8796             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8797               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8798               .access = PL3_RW, .type = ARM_CP_CONST,
8799               .resetvalue = 0 },
8800         };
8801         define_arm_cp_regs(cpu, auxcr_reginfo);
8802         if (cpu_isar_feature(aa32_ac2, cpu)) {
8803             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8804         }
8805     }
8806 
8807     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8808         /*
8809          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8810          * There are two flavours:
8811          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8812          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8813          *      32-bit register visible to AArch32 at a different encoding
8814          *      to the "flavour 1" register and with the bits rearranged to
8815          *      be able to squash a 64-bit address into the 32-bit view.
8816          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8817          * in future if we support AArch32-only configs of some of the
8818          * AArch64 cores we might need to add a specific feature flag
8819          * to indicate cores with "flavour 2" CBAR.
8820          */
8821         if (arm_feature(env, ARM_FEATURE_V8)) {
8822             /* 32 bit view is [31:18] 0...0 [43:32]. */
8823             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8824                 | extract64(cpu->reset_cbar, 32, 12);
8825             ARMCPRegInfo cbar_reginfo[] = {
8826                 { .name = "CBAR",
8827                   .type = ARM_CP_CONST,
8828                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8829                   .access = PL1_R, .resetvalue = cbar32 },
8830                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8831                   .type = ARM_CP_CONST,
8832                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8833                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8834             };
8835             /* We don't implement a r/w 64 bit CBAR currently */
8836             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8837             define_arm_cp_regs(cpu, cbar_reginfo);
8838         } else {
8839             ARMCPRegInfo cbar = {
8840                 .name = "CBAR",
8841                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8842                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8843                 .fieldoffset = offsetof(CPUARMState,
8844                                         cp15.c15_config_base_address)
8845             };
8846             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8847                 cbar.access = PL1_R;
8848                 cbar.fieldoffset = 0;
8849                 cbar.type = ARM_CP_CONST;
8850             }
8851             define_one_arm_cp_reg(cpu, &cbar);
8852         }
8853     }
8854 
8855     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8856         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8857             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8858               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8859               .access = PL1_RW, .writefn = vbar_write,
8860               .accessfn = access_nv1,
8861               .fgt = FGT_VBAR_EL1,
8862               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8863               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8864                                      offsetof(CPUARMState, cp15.vbar_ns) },
8865               .resetvalue = 0 },
8866         };
8867         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8868     }
8869 
8870     /* Generic registers whose values depend on the implementation */
8871     {
8872         ARMCPRegInfo sctlr = {
8873             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8874             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8875             .access = PL1_RW, .accessfn = access_tvm_trvm,
8876             .fgt = FGT_SCTLR_EL1,
8877             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8878             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8879                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8880             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8881             .raw_writefn = raw_write,
8882         };
8883         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8884             /*
8885              * Normally we would always end the TB on an SCTLR write, but Linux
8886              * arch/arm/mach-pxa/sleep.S expects two instructions following
8887              * an MMU enable to execute from cache.  Imitate this behaviour.
8888              */
8889             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8890         }
8891         define_one_arm_cp_reg(cpu, &sctlr);
8892 
8893         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8894             arm_feature(env, ARM_FEATURE_V8)) {
8895             ARMCPRegInfo vsctlr = {
8896                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8897                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8898                 .access = PL2_RW, .resetvalue = 0x0,
8899                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8900             };
8901             define_one_arm_cp_reg(cpu, &vsctlr);
8902         }
8903     }
8904 
8905     if (cpu_isar_feature(aa64_lor, cpu)) {
8906         define_arm_cp_regs(cpu, lor_reginfo);
8907     }
8908     if (cpu_isar_feature(aa64_pan, cpu)) {
8909         define_one_arm_cp_reg(cpu, &pan_reginfo);
8910     }
8911 #ifndef CONFIG_USER_ONLY
8912     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8913         define_arm_cp_regs(cpu, ats1e1_reginfo);
8914     }
8915     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8916         define_arm_cp_regs(cpu, ats1cp_reginfo);
8917     }
8918 #endif
8919     if (cpu_isar_feature(aa64_uao, cpu)) {
8920         define_one_arm_cp_reg(cpu, &uao_reginfo);
8921     }
8922 
8923     if (cpu_isar_feature(aa64_dit, cpu)) {
8924         define_one_arm_cp_reg(cpu, &dit_reginfo);
8925     }
8926     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8927         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8928     }
8929     if (cpu_isar_feature(any_ras, cpu)) {
8930         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8931     }
8932 
8933     if (cpu_isar_feature(aa64_vh, cpu) ||
8934         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8935         define_one_arm_cp_reg(cpu, &contextidr_el2);
8936     }
8937     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8938         define_arm_cp_regs(cpu, vhe_reginfo);
8939     }
8940 
8941     if (cpu_isar_feature(aa64_sve, cpu)) {
8942         define_arm_cp_regs(cpu, zcr_reginfo);
8943     }
8944 
8945     if (cpu_isar_feature(aa64_hcx, cpu)) {
8946         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8947     }
8948 
8949 #ifdef TARGET_AARCH64
8950     if (cpu_isar_feature(aa64_sme, cpu)) {
8951         define_arm_cp_regs(cpu, sme_reginfo);
8952     }
8953     if (cpu_isar_feature(aa64_pauth, cpu)) {
8954         define_arm_cp_regs(cpu, pauth_reginfo);
8955     }
8956     if (cpu_isar_feature(aa64_rndr, cpu)) {
8957         define_arm_cp_regs(cpu, rndr_reginfo);
8958     }
8959     /* Data Cache clean instructions up to PoP */
8960     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8961         define_one_arm_cp_reg(cpu, dcpop_reg);
8962 
8963         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8964             define_one_arm_cp_reg(cpu, dcpodp_reg);
8965         }
8966     }
8967 
8968     /*
8969      * If full MTE is enabled, add all of the system registers.
8970      * If only "instructions available at EL0" are enabled,
8971      * then define only a RAZ/WI version of PSTATE.TCO.
8972      */
8973     if (cpu_isar_feature(aa64_mte, cpu)) {
8974         ARMCPRegInfo gmid_reginfo = {
8975             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8976             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8977             .access = PL1_R, .accessfn = access_aa64_tid5,
8978             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8979         };
8980         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8981         define_arm_cp_regs(cpu, mte_reginfo);
8982         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8983     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8984         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8985         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8986     }
8987 
8988     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8989         define_arm_cp_regs(cpu, scxtnum_reginfo);
8990     }
8991 
8992     if (cpu_isar_feature(aa64_fgt, cpu)) {
8993         define_arm_cp_regs(cpu, fgt_reginfo);
8994     }
8995 
8996     if (cpu_isar_feature(aa64_rme, cpu)) {
8997         define_arm_cp_regs(cpu, rme_reginfo);
8998         if (cpu_isar_feature(aa64_mte, cpu)) {
8999             define_arm_cp_regs(cpu, rme_mte_reginfo);
9000         }
9001     }
9002 
9003     if (cpu_isar_feature(aa64_nv2, cpu)) {
9004         define_arm_cp_regs(cpu, nv2_reginfo);
9005     }
9006 
9007     if (cpu_isar_feature(aa64_nmi, cpu)) {
9008         define_arm_cp_regs(cpu, nmi_reginfo);
9009     }
9010 #endif
9011 
9012     if (cpu_isar_feature(any_predinv, cpu)) {
9013         define_arm_cp_regs(cpu, predinv_reginfo);
9014     }
9015 
9016     if (cpu_isar_feature(any_ccidx, cpu)) {
9017         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9018     }
9019 
9020 #ifndef CONFIG_USER_ONLY
9021     /*
9022      * Register redirections and aliases must be done last,
9023      * after the registers from the other extensions have been defined.
9024      */
9025     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9026         define_arm_vh_e2h_redirects_aliases(cpu);
9027     }
9028 #endif
9029 }
9030 
9031 /*
9032  * Private utility function for define_one_arm_cp_reg_with_opaque():
9033  * add a single reginfo struct to the hash table.
9034  */
9035 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9036                                    void *opaque, CPState state,
9037                                    CPSecureState secstate,
9038                                    int crm, int opc1, int opc2,
9039                                    const char *name)
9040 {
9041     CPUARMState *env = &cpu->env;
9042     uint32_t key;
9043     ARMCPRegInfo *r2;
9044     bool is64 = r->type & ARM_CP_64BIT;
9045     bool ns = secstate & ARM_CP_SECSTATE_NS;
9046     int cp = r->cp;
9047     size_t name_len;
9048     bool make_const;
9049 
9050     switch (state) {
9051     case ARM_CP_STATE_AA32:
9052         /* We assume it is a cp15 register if the .cp field is left unset. */
9053         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9054             cp = 15;
9055         }
9056         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9057         break;
9058     case ARM_CP_STATE_AA64:
9059         /*
9060          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9061          * cp == 0 as equivalent to the value for "standard guest-visible
9062          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9063          * in their AArch64 view (the .cp value may be non-zero for the
9064          * benefit of the AArch32 view).
9065          */
9066         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9067             cp = CP_REG_ARM64_SYSREG_CP;
9068         }
9069         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9070         break;
9071     default:
9072         g_assert_not_reached();
9073     }
9074 
9075     /* Overriding of an existing definition must be explicitly requested. */
9076     if (!(r->type & ARM_CP_OVERRIDE)) {
9077         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9078         if (oldreg) {
9079             assert(oldreg->type & ARM_CP_OVERRIDE);
9080         }
9081     }
9082 
9083     /*
9084      * Eliminate registers that are not present because the EL is missing.
9085      * Doing this here makes it easier to put all registers for a given
9086      * feature into the same ARMCPRegInfo array and define them all at once.
9087      */
9088     make_const = false;
9089     if (arm_feature(env, ARM_FEATURE_EL3)) {
9090         /*
9091          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9092          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9093          */
9094         int min_el = ctz32(r->access) / 2;
9095         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9096             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9097                 return;
9098             }
9099             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9100         }
9101     } else {
9102         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9103                                  ? PL2_RW : PL1_RW);
9104         if ((r->access & max_el) == 0) {
9105             return;
9106         }
9107     }
9108 
9109     /* Combine cpreg and name into one allocation. */
9110     name_len = strlen(name) + 1;
9111     r2 = g_malloc(sizeof(*r2) + name_len);
9112     *r2 = *r;
9113     r2->name = memcpy(r2 + 1, name, name_len);
9114 
9115     /*
9116      * Update fields to match the instantiation, overwiting wildcards
9117      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9118      */
9119     r2->cp = cp;
9120     r2->crm = crm;
9121     r2->opc1 = opc1;
9122     r2->opc2 = opc2;
9123     r2->state = state;
9124     r2->secure = secstate;
9125     if (opaque) {
9126         r2->opaque = opaque;
9127     }
9128 
9129     if (make_const) {
9130         /* This should not have been a very special register to begin. */
9131         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9132         assert(old_special == 0 || old_special == ARM_CP_NOP);
9133         /*
9134          * Set the special function to CONST, retaining the other flags.
9135          * This is important for e.g. ARM_CP_SVE so that we still
9136          * take the SVE trap if CPTR_EL3.EZ == 0.
9137          */
9138         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9139         /*
9140          * Usually, these registers become RES0, but there are a few
9141          * special cases like VPIDR_EL2 which have a constant non-zero
9142          * value with writes ignored.
9143          */
9144         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9145             r2->resetvalue = 0;
9146         }
9147         /*
9148          * ARM_CP_CONST has precedence, so removing the callbacks and
9149          * offsets are not strictly necessary, but it is potentially
9150          * less confusing to debug later.
9151          */
9152         r2->readfn = NULL;
9153         r2->writefn = NULL;
9154         r2->raw_readfn = NULL;
9155         r2->raw_writefn = NULL;
9156         r2->resetfn = NULL;
9157         r2->fieldoffset = 0;
9158         r2->bank_fieldoffsets[0] = 0;
9159         r2->bank_fieldoffsets[1] = 0;
9160     } else {
9161         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9162 
9163         if (isbanked) {
9164             /*
9165              * Register is banked (using both entries in array).
9166              * Overwriting fieldoffset as the array is only used to define
9167              * banked registers but later only fieldoffset is used.
9168              */
9169             r2->fieldoffset = r->bank_fieldoffsets[ns];
9170         }
9171         if (state == ARM_CP_STATE_AA32) {
9172             if (isbanked) {
9173                 /*
9174                  * If the register is banked then we don't need to migrate or
9175                  * reset the 32-bit instance in certain cases:
9176                  *
9177                  * 1) If the register has both 32-bit and 64-bit instances
9178                  *    then we can count on the 64-bit instance taking care
9179                  *    of the non-secure bank.
9180                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9181                  *    version taking care of the secure bank.  This requires
9182                  *    that separate 32 and 64-bit definitions are provided.
9183                  */
9184                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9185                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9186                     r2->type |= ARM_CP_ALIAS;
9187                 }
9188             } else if ((secstate != r->secure) && !ns) {
9189                 /*
9190                  * The register is not banked so we only want to allow
9191                  * migration of the non-secure instance.
9192                  */
9193                 r2->type |= ARM_CP_ALIAS;
9194             }
9195 
9196             if (HOST_BIG_ENDIAN &&
9197                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9198                 r2->fieldoffset += sizeof(uint32_t);
9199             }
9200         }
9201     }
9202 
9203     /*
9204      * By convention, for wildcarded registers only the first
9205      * entry is used for migration; the others are marked as
9206      * ALIAS so we don't try to transfer the register
9207      * multiple times. Special registers (ie NOP/WFI) are
9208      * never migratable and not even raw-accessible.
9209      */
9210     if (r2->type & ARM_CP_SPECIAL_MASK) {
9211         r2->type |= ARM_CP_NO_RAW;
9212     }
9213     if (((r->crm == CP_ANY) && crm != 0) ||
9214         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9215         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9216         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9217     }
9218 
9219     /*
9220      * Check that raw accesses are either forbidden or handled. Note that
9221      * we can't assert this earlier because the setup of fieldoffset for
9222      * banked registers has to be done first.
9223      */
9224     if (!(r2->type & ARM_CP_NO_RAW)) {
9225         assert(!raw_accessors_invalid(r2));
9226     }
9227 
9228     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9229 }
9230 
9231 
9232 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9233                                        const ARMCPRegInfo *r, void *opaque)
9234 {
9235     /*
9236      * Define implementations of coprocessor registers.
9237      * We store these in a hashtable because typically
9238      * there are less than 150 registers in a space which
9239      * is 16*16*16*8*8 = 262144 in size.
9240      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9241      * If a register is defined twice then the second definition is
9242      * used, so this can be used to define some generic registers and
9243      * then override them with implementation specific variations.
9244      * At least one of the original and the second definition should
9245      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9246      * against accidental use.
9247      *
9248      * The state field defines whether the register is to be
9249      * visible in the AArch32 or AArch64 execution state. If the
9250      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9251      * reginfo structure for the AArch32 view, which sees the lower
9252      * 32 bits of the 64 bit register.
9253      *
9254      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9255      * be wildcarded. AArch64 registers are always considered to be 64
9256      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9257      * the register, if any.
9258      */
9259     int crm, opc1, opc2;
9260     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9261     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9262     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9263     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9264     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9265     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9266     CPState state;
9267 
9268     /* 64 bit registers have only CRm and Opc1 fields */
9269     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9270     /* op0 only exists in the AArch64 encodings */
9271     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9272     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9273     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9274     /*
9275      * This API is only for Arm's system coprocessors (14 and 15) or
9276      * (M-profile or v7A-and-earlier only) for implementation defined
9277      * coprocessors in the range 0..7.  Our decode assumes this, since
9278      * 8..13 can be used for other insns including VFP and Neon. See
9279      * valid_cp() in translate.c.  Assert here that we haven't tried
9280      * to use an invalid coprocessor number.
9281      */
9282     switch (r->state) {
9283     case ARM_CP_STATE_BOTH:
9284         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9285         if (r->cp == 0) {
9286             break;
9287         }
9288         /* fall through */
9289     case ARM_CP_STATE_AA32:
9290         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9291             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9292             assert(r->cp >= 14 && r->cp <= 15);
9293         } else {
9294             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9295         }
9296         break;
9297     case ARM_CP_STATE_AA64:
9298         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9299         break;
9300     default:
9301         g_assert_not_reached();
9302     }
9303     /*
9304      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9305      * encodes a minimum access level for the register. We roll this
9306      * runtime check into our general permission check code, so check
9307      * here that the reginfo's specified permissions are strict enough
9308      * to encompass the generic architectural permission check.
9309      */
9310     if (r->state != ARM_CP_STATE_AA32) {
9311         CPAccessRights mask;
9312         switch (r->opc1) {
9313         case 0:
9314             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9315             mask = PL0U_R | PL1_RW;
9316             break;
9317         case 1: case 2:
9318             /* min_EL EL1 */
9319             mask = PL1_RW;
9320             break;
9321         case 3:
9322             /* min_EL EL0 */
9323             mask = PL0_RW;
9324             break;
9325         case 4:
9326         case 5:
9327             /* min_EL EL2 */
9328             mask = PL2_RW;
9329             break;
9330         case 6:
9331             /* min_EL EL3 */
9332             mask = PL3_RW;
9333             break;
9334         case 7:
9335             /* min_EL EL1, secure mode only (we don't check the latter) */
9336             mask = PL1_RW;
9337             break;
9338         default:
9339             /* broken reginfo with out-of-range opc1 */
9340             g_assert_not_reached();
9341         }
9342         /* assert our permissions are not too lax (stricter is fine) */
9343         assert((r->access & ~mask) == 0);
9344     }
9345 
9346     /*
9347      * Check that the register definition has enough info to handle
9348      * reads and writes if they are permitted.
9349      */
9350     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9351         if (r->access & PL3_R) {
9352             assert((r->fieldoffset ||
9353                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9354                    r->readfn);
9355         }
9356         if (r->access & PL3_W) {
9357             assert((r->fieldoffset ||
9358                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9359                    r->writefn);
9360         }
9361     }
9362 
9363     for (crm = crmmin; crm <= crmmax; crm++) {
9364         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9365             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9366                 for (state = ARM_CP_STATE_AA32;
9367                      state <= ARM_CP_STATE_AA64; state++) {
9368                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9369                         continue;
9370                     }
9371                     if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
9372                         cpu_isar_feature(aa64_xs, cpu)) {
9373                         /*
9374                          * This is a TLBI insn which has an NXS variant. The
9375                          * NXS variant is at the same encoding except that
9376                          * crn is +1, and has the same behaviour except for
9377                          * fine-grained trapping. Add the NXS insn here and
9378                          * then fall through to add the normal register.
9379                          * add_cpreg_to_hashtable() copies the cpreg struct
9380                          * and name that it is passed, so it's OK to use
9381                          * a local struct here.
9382                          */
9383                         ARMCPRegInfo nxs_ri = *r;
9384                         g_autofree char *name = g_strdup_printf("%sNXS", r->name);
9385 
9386                         assert(state == ARM_CP_STATE_AA64);
9387                         assert(nxs_ri.crn < 0xf);
9388                         nxs_ri.crn++;
9389                         if (nxs_ri.fgt) {
9390                             nxs_ri.fgt |= R_FGT_NXS_MASK;
9391                         }
9392                         add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
9393                                                ARM_CP_SECSTATE_NS,
9394                                                crm, opc1, opc2, name);
9395                     }
9396                     if (state == ARM_CP_STATE_AA32) {
9397                         /*
9398                          * Under AArch32 CP registers can be common
9399                          * (same for secure and non-secure world) or banked.
9400                          */
9401                         char *name;
9402 
9403                         switch (r->secure) {
9404                         case ARM_CP_SECSTATE_S:
9405                         case ARM_CP_SECSTATE_NS:
9406                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9407                                                    r->secure, crm, opc1, opc2,
9408                                                    r->name);
9409                             break;
9410                         case ARM_CP_SECSTATE_BOTH:
9411                             name = g_strdup_printf("%s_S", r->name);
9412                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9413                                                    ARM_CP_SECSTATE_S,
9414                                                    crm, opc1, opc2, name);
9415                             g_free(name);
9416                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9417                                                    ARM_CP_SECSTATE_NS,
9418                                                    crm, opc1, opc2, r->name);
9419                             break;
9420                         default:
9421                             g_assert_not_reached();
9422                         }
9423                     } else {
9424                         /*
9425                          * AArch64 registers get mapped to non-secure instance
9426                          * of AArch32
9427                          */
9428                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9429                                                ARM_CP_SECSTATE_NS,
9430                                                crm, opc1, opc2, r->name);
9431                     }
9432                 }
9433             }
9434         }
9435     }
9436 }
9437 
9438 /* Define a whole list of registers */
9439 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9440                                         void *opaque, size_t len)
9441 {
9442     size_t i;
9443     for (i = 0; i < len; ++i) {
9444         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9445     }
9446 }
9447 
9448 /*
9449  * Modify ARMCPRegInfo for access from userspace.
9450  *
9451  * This is a data driven modification directed by
9452  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9453  * user-space cannot alter any values and dynamic values pertaining to
9454  * execution state are hidden from user space view anyway.
9455  */
9456 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9457                                  const ARMCPRegUserSpaceInfo *mods,
9458                                  size_t mods_len)
9459 {
9460     for (size_t mi = 0; mi < mods_len; ++mi) {
9461         const ARMCPRegUserSpaceInfo *m = mods + mi;
9462         GPatternSpec *pat = NULL;
9463 
9464         if (m->is_glob) {
9465             pat = g_pattern_spec_new(m->name);
9466         }
9467         for (size_t ri = 0; ri < regs_len; ++ri) {
9468             ARMCPRegInfo *r = regs + ri;
9469 
9470             if (pat && g_pattern_match_string(pat, r->name)) {
9471                 r->type = ARM_CP_CONST;
9472                 r->access = PL0U_R;
9473                 r->resetvalue = 0;
9474                 /* continue */
9475             } else if (strcmp(r->name, m->name) == 0) {
9476                 r->type = ARM_CP_CONST;
9477                 r->access = PL0U_R;
9478                 r->resetvalue &= m->exported_bits;
9479                 r->resetvalue |= m->fixed_bits;
9480                 break;
9481             }
9482         }
9483         if (pat) {
9484             g_pattern_spec_free(pat);
9485         }
9486     }
9487 }
9488 
9489 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9490 {
9491     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9492 }
9493 
9494 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9495                          uint64_t value)
9496 {
9497     /* Helper coprocessor write function for write-ignore registers */
9498 }
9499 
9500 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9501 {
9502     /* Helper coprocessor write function for read-as-zero registers */
9503     return 0;
9504 }
9505 
9506 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9507 {
9508     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9509 }
9510 
9511 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9512 {
9513     /*
9514      * Return true if it is not valid for us to switch to
9515      * this CPU mode (ie all the UNPREDICTABLE cases in
9516      * the ARM ARM CPSRWriteByInstr pseudocode).
9517      */
9518 
9519     /* Changes to or from Hyp via MSR and CPS are illegal. */
9520     if (write_type == CPSRWriteByInstr &&
9521         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9522          mode == ARM_CPU_MODE_HYP)) {
9523         return 1;
9524     }
9525 
9526     switch (mode) {
9527     case ARM_CPU_MODE_USR:
9528         return 0;
9529     case ARM_CPU_MODE_SYS:
9530     case ARM_CPU_MODE_SVC:
9531     case ARM_CPU_MODE_ABT:
9532     case ARM_CPU_MODE_UND:
9533     case ARM_CPU_MODE_IRQ:
9534     case ARM_CPU_MODE_FIQ:
9535         /*
9536          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9537          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9538          */
9539         /*
9540          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9541          * and CPS are treated as illegal mode changes.
9542          */
9543         if (write_type == CPSRWriteByInstr &&
9544             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9545             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9546             return 1;
9547         }
9548         return 0;
9549     case ARM_CPU_MODE_HYP:
9550         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9551     case ARM_CPU_MODE_MON:
9552         return arm_current_el(env) < 3;
9553     default:
9554         return 1;
9555     }
9556 }
9557 
9558 uint32_t cpsr_read(CPUARMState *env)
9559 {
9560     int ZF;
9561     ZF = (env->ZF == 0);
9562     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9563         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9564         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9565         | ((env->condexec_bits & 0xfc) << 8)
9566         | (env->GE << 16) | (env->daif & CPSR_AIF);
9567 }
9568 
9569 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9570                 CPSRWriteType write_type)
9571 {
9572     uint32_t changed_daif;
9573     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9574         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9575 
9576     if (mask & CPSR_NZCV) {
9577         env->ZF = (~val) & CPSR_Z;
9578         env->NF = val;
9579         env->CF = (val >> 29) & 1;
9580         env->VF = (val << 3) & 0x80000000;
9581     }
9582     if (mask & CPSR_Q) {
9583         env->QF = ((val & CPSR_Q) != 0);
9584     }
9585     if (mask & CPSR_T) {
9586         env->thumb = ((val & CPSR_T) != 0);
9587     }
9588     if (mask & CPSR_IT_0_1) {
9589         env->condexec_bits &= ~3;
9590         env->condexec_bits |= (val >> 25) & 3;
9591     }
9592     if (mask & CPSR_IT_2_7) {
9593         env->condexec_bits &= 3;
9594         env->condexec_bits |= (val >> 8) & 0xfc;
9595     }
9596     if (mask & CPSR_GE) {
9597         env->GE = (val >> 16) & 0xf;
9598     }
9599 
9600     /*
9601      * In a V7 implementation that includes the security extensions but does
9602      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9603      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9604      * bits respectively.
9605      *
9606      * In a V8 implementation, it is permitted for privileged software to
9607      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9608      */
9609     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9610         arm_feature(env, ARM_FEATURE_EL3) &&
9611         !arm_feature(env, ARM_FEATURE_EL2) &&
9612         !arm_is_secure(env)) {
9613 
9614         changed_daif = (env->daif ^ val) & mask;
9615 
9616         if (changed_daif & CPSR_A) {
9617             /*
9618              * Check to see if we are allowed to change the masking of async
9619              * abort exceptions from a non-secure state.
9620              */
9621             if (!(env->cp15.scr_el3 & SCR_AW)) {
9622                 qemu_log_mask(LOG_GUEST_ERROR,
9623                               "Ignoring attempt to switch CPSR_A flag from "
9624                               "non-secure world with SCR.AW bit clear\n");
9625                 mask &= ~CPSR_A;
9626             }
9627         }
9628 
9629         if (changed_daif & CPSR_F) {
9630             /*
9631              * Check to see if we are allowed to change the masking of FIQ
9632              * exceptions from a non-secure state.
9633              */
9634             if (!(env->cp15.scr_el3 & SCR_FW)) {
9635                 qemu_log_mask(LOG_GUEST_ERROR,
9636                               "Ignoring attempt to switch CPSR_F flag from "
9637                               "non-secure world with SCR.FW bit clear\n");
9638                 mask &= ~CPSR_F;
9639             }
9640 
9641             /*
9642              * Check whether non-maskable FIQ (NMFI) support is enabled.
9643              * If this bit is set software is not allowed to mask
9644              * FIQs, but is allowed to set CPSR_F to 0.
9645              */
9646             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9647                 (val & CPSR_F)) {
9648                 qemu_log_mask(LOG_GUEST_ERROR,
9649                               "Ignoring attempt to enable CPSR_F flag "
9650                               "(non-maskable FIQ [NMFI] support enabled)\n");
9651                 mask &= ~CPSR_F;
9652             }
9653         }
9654     }
9655 
9656     env->daif &= ~(CPSR_AIF & mask);
9657     env->daif |= val & CPSR_AIF & mask;
9658 
9659     if (write_type != CPSRWriteRaw &&
9660         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9661         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9662             /*
9663              * Note that we can only get here in USR mode if this is a
9664              * gdb stub write; for this case we follow the architectural
9665              * behaviour for guest writes in USR mode of ignoring an attempt
9666              * to switch mode. (Those are caught by translate.c for writes
9667              * triggered by guest instructions.)
9668              */
9669             mask &= ~CPSR_M;
9670         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9671             /*
9672              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9673              * v7, and has defined behaviour in v8:
9674              *  + leave CPSR.M untouched
9675              *  + allow changes to the other CPSR fields
9676              *  + set PSTATE.IL
9677              * For user changes via the GDB stub, we don't set PSTATE.IL,
9678              * as this would be unnecessarily harsh for a user error.
9679              */
9680             mask &= ~CPSR_M;
9681             if (write_type != CPSRWriteByGDBStub &&
9682                 arm_feature(env, ARM_FEATURE_V8)) {
9683                 mask |= CPSR_IL;
9684                 val |= CPSR_IL;
9685             }
9686             qemu_log_mask(LOG_GUEST_ERROR,
9687                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9688                           aarch32_mode_name(env->uncached_cpsr),
9689                           aarch32_mode_name(val));
9690         } else {
9691             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9692                           write_type == CPSRWriteExceptionReturn ?
9693                           "Exception return from AArch32" :
9694                           "AArch32 mode switch from",
9695                           aarch32_mode_name(env->uncached_cpsr),
9696                           aarch32_mode_name(val), env->regs[15]);
9697             switch_mode(env, val & CPSR_M);
9698         }
9699     }
9700     mask &= ~CACHED_CPSR_BITS;
9701     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9702     if (tcg_enabled() && rebuild_hflags) {
9703         arm_rebuild_hflags(env);
9704     }
9705 }
9706 
9707 #ifdef CONFIG_USER_ONLY
9708 
9709 static void switch_mode(CPUARMState *env, int mode)
9710 {
9711     ARMCPU *cpu = env_archcpu(env);
9712 
9713     if (mode != ARM_CPU_MODE_USR) {
9714         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9715     }
9716 }
9717 
9718 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9719                                  uint32_t cur_el, bool secure)
9720 {
9721     return 1;
9722 }
9723 
9724 void aarch64_sync_64_to_32(CPUARMState *env)
9725 {
9726     g_assert_not_reached();
9727 }
9728 
9729 #else
9730 
9731 static void switch_mode(CPUARMState *env, int mode)
9732 {
9733     int old_mode;
9734     int i;
9735 
9736     old_mode = env->uncached_cpsr & CPSR_M;
9737     if (mode == old_mode) {
9738         return;
9739     }
9740 
9741     if (old_mode == ARM_CPU_MODE_FIQ) {
9742         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9743         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9744     } else if (mode == ARM_CPU_MODE_FIQ) {
9745         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9746         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9747     }
9748 
9749     i = bank_number(old_mode);
9750     env->banked_r13[i] = env->regs[13];
9751     env->banked_spsr[i] = env->spsr;
9752 
9753     i = bank_number(mode);
9754     env->regs[13] = env->banked_r13[i];
9755     env->spsr = env->banked_spsr[i];
9756 
9757     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9758     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9759 }
9760 
9761 /*
9762  * Physical Interrupt Target EL Lookup Table
9763  *
9764  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9765  *
9766  * The below multi-dimensional table is used for looking up the target
9767  * exception level given numerous condition criteria.  Specifically, the
9768  * target EL is based on SCR and HCR routing controls as well as the
9769  * currently executing EL and secure state.
9770  *
9771  *    Dimensions:
9772  *    target_el_table[2][2][2][2][2][4]
9773  *                    |  |  |  |  |  +--- Current EL
9774  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9775  *                    |  |  |  +--------- HCR mask override
9776  *                    |  |  +------------ SCR exec state control
9777  *                    |  +--------------- SCR mask override
9778  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9779  *
9780  *    The table values are as such:
9781  *    0-3 = EL0-EL3
9782  *     -1 = Cannot occur
9783  *
9784  * The ARM ARM target EL table includes entries indicating that an "exception
9785  * is not taken".  The two cases where this is applicable are:
9786  *    1) An exception is taken from EL3 but the SCR does not have the exception
9787  *    routed to EL3.
9788  *    2) An exception is taken from EL2 but the HCR does not have the exception
9789  *    routed to EL2.
9790  * In these two cases, the below table contain a target of EL1.  This value is
9791  * returned as it is expected that the consumer of the table data will check
9792  * for "target EL >= current EL" to ensure the exception is not taken.
9793  *
9794  *            SCR     HCR
9795  *         64  EA     AMO                 From
9796  *        BIT IRQ     IMO      Non-secure         Secure
9797  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9798  */
9799 static const int8_t target_el_table[2][2][2][2][2][4] = {
9800     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9801        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9802       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9803        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9804      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9805        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9806       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9807        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9808     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9809        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9810       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9811        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9812      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9813        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9814       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9815        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9816 };
9817 
9818 /*
9819  * Determine the target EL for physical exceptions
9820  */
9821 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9822                                  uint32_t cur_el, bool secure)
9823 {
9824     CPUARMState *env = cpu_env(cs);
9825     bool rw;
9826     bool scr;
9827     bool hcr;
9828     int target_el;
9829     /* Is the highest EL AArch64? */
9830     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9831     uint64_t hcr_el2;
9832 
9833     if (arm_feature(env, ARM_FEATURE_EL3)) {
9834         rw = arm_scr_rw_eff(env);
9835     } else {
9836         /*
9837          * Either EL2 is the highest EL (and so the EL2 register width
9838          * is given by is64); or there is no EL2 or EL3, in which case
9839          * the value of 'rw' does not affect the table lookup anyway.
9840          */
9841         rw = is64;
9842     }
9843 
9844     hcr_el2 = arm_hcr_el2_eff(env);
9845     switch (excp_idx) {
9846     case EXCP_IRQ:
9847     case EXCP_NMI:
9848         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9849         hcr = hcr_el2 & HCR_IMO;
9850         break;
9851     case EXCP_FIQ:
9852         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9853         hcr = hcr_el2 & HCR_FMO;
9854         break;
9855     default:
9856         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9857         hcr = hcr_el2 & HCR_AMO;
9858         break;
9859     };
9860 
9861     /*
9862      * For these purposes, TGE and AMO/IMO/FMO both force the
9863      * interrupt to EL2.  Fold TGE into the bit extracted above.
9864      */
9865     hcr |= (hcr_el2 & HCR_TGE) != 0;
9866 
9867     /* Perform a table-lookup for the target EL given the current state */
9868     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9869 
9870     assert(target_el > 0);
9871 
9872     return target_el;
9873 }
9874 
9875 void arm_log_exception(CPUState *cs)
9876 {
9877     int idx = cs->exception_index;
9878 
9879     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9880         const char *exc = NULL;
9881         static const char * const excnames[] = {
9882             [EXCP_UDEF] = "Undefined Instruction",
9883             [EXCP_SWI] = "SVC",
9884             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9885             [EXCP_DATA_ABORT] = "Data Abort",
9886             [EXCP_IRQ] = "IRQ",
9887             [EXCP_FIQ] = "FIQ",
9888             [EXCP_BKPT] = "Breakpoint",
9889             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9890             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9891             [EXCP_HVC] = "Hypervisor Call",
9892             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9893             [EXCP_SMC] = "Secure Monitor Call",
9894             [EXCP_VIRQ] = "Virtual IRQ",
9895             [EXCP_VFIQ] = "Virtual FIQ",
9896             [EXCP_SEMIHOST] = "Semihosting call",
9897             [EXCP_NOCP] = "v7M NOCP UsageFault",
9898             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9899             [EXCP_STKOF] = "v8M STKOF UsageFault",
9900             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9901             [EXCP_LSERR] = "v8M LSERR UsageFault",
9902             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9903             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9904             [EXCP_VSERR] = "Virtual SERR",
9905             [EXCP_GPC] = "Granule Protection Check",
9906             [EXCP_NMI] = "NMI",
9907             [EXCP_VINMI] = "Virtual IRQ NMI",
9908             [EXCP_VFNMI] = "Virtual FIQ NMI",
9909             [EXCP_MON_TRAP] = "Monitor Trap",
9910         };
9911 
9912         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9913             exc = excnames[idx];
9914         }
9915         if (!exc) {
9916             exc = "unknown";
9917         }
9918         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9919                       idx, exc, cs->cpu_index);
9920     }
9921 }
9922 
9923 /*
9924  * Function used to synchronize QEMU's AArch64 register set with AArch32
9925  * register set.  This is necessary when switching between AArch32 and AArch64
9926  * execution state.
9927  */
9928 void aarch64_sync_32_to_64(CPUARMState *env)
9929 {
9930     int i;
9931     uint32_t mode = env->uncached_cpsr & CPSR_M;
9932 
9933     /* We can blanket copy R[0:7] to X[0:7] */
9934     for (i = 0; i < 8; i++) {
9935         env->xregs[i] = env->regs[i];
9936     }
9937 
9938     /*
9939      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9940      * Otherwise, they come from the banked user regs.
9941      */
9942     if (mode == ARM_CPU_MODE_FIQ) {
9943         for (i = 8; i < 13; i++) {
9944             env->xregs[i] = env->usr_regs[i - 8];
9945         }
9946     } else {
9947         for (i = 8; i < 13; i++) {
9948             env->xregs[i] = env->regs[i];
9949         }
9950     }
9951 
9952     /*
9953      * Registers x13-x23 are the various mode SP and FP registers. Registers
9954      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9955      * from the mode banked register.
9956      */
9957     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9958         env->xregs[13] = env->regs[13];
9959         env->xregs[14] = env->regs[14];
9960     } else {
9961         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9962         /* HYP is an exception in that it is copied from r14 */
9963         if (mode == ARM_CPU_MODE_HYP) {
9964             env->xregs[14] = env->regs[14];
9965         } else {
9966             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9967         }
9968     }
9969 
9970     if (mode == ARM_CPU_MODE_HYP) {
9971         env->xregs[15] = env->regs[13];
9972     } else {
9973         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9974     }
9975 
9976     if (mode == ARM_CPU_MODE_IRQ) {
9977         env->xregs[16] = env->regs[14];
9978         env->xregs[17] = env->regs[13];
9979     } else {
9980         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9981         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9982     }
9983 
9984     if (mode == ARM_CPU_MODE_SVC) {
9985         env->xregs[18] = env->regs[14];
9986         env->xregs[19] = env->regs[13];
9987     } else {
9988         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9989         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9990     }
9991 
9992     if (mode == ARM_CPU_MODE_ABT) {
9993         env->xregs[20] = env->regs[14];
9994         env->xregs[21] = env->regs[13];
9995     } else {
9996         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9997         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9998     }
9999 
10000     if (mode == ARM_CPU_MODE_UND) {
10001         env->xregs[22] = env->regs[14];
10002         env->xregs[23] = env->regs[13];
10003     } else {
10004         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10005         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10006     }
10007 
10008     /*
10009      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10010      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10011      * FIQ bank for r8-r14.
10012      */
10013     if (mode == ARM_CPU_MODE_FIQ) {
10014         for (i = 24; i < 31; i++) {
10015             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10016         }
10017     } else {
10018         for (i = 24; i < 29; i++) {
10019             env->xregs[i] = env->fiq_regs[i - 24];
10020         }
10021         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10022         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10023     }
10024 
10025     env->pc = env->regs[15];
10026 }
10027 
10028 /*
10029  * Function used to synchronize QEMU's AArch32 register set with AArch64
10030  * register set.  This is necessary when switching between AArch32 and AArch64
10031  * execution state.
10032  */
10033 void aarch64_sync_64_to_32(CPUARMState *env)
10034 {
10035     int i;
10036     uint32_t mode = env->uncached_cpsr & CPSR_M;
10037 
10038     /* We can blanket copy X[0:7] to R[0:7] */
10039     for (i = 0; i < 8; i++) {
10040         env->regs[i] = env->xregs[i];
10041     }
10042 
10043     /*
10044      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10045      * Otherwise, we copy x8-x12 into the banked user regs.
10046      */
10047     if (mode == ARM_CPU_MODE_FIQ) {
10048         for (i = 8; i < 13; i++) {
10049             env->usr_regs[i - 8] = env->xregs[i];
10050         }
10051     } else {
10052         for (i = 8; i < 13; i++) {
10053             env->regs[i] = env->xregs[i];
10054         }
10055     }
10056 
10057     /*
10058      * Registers r13 & r14 depend on the current mode.
10059      * If we are in a given mode, we copy the corresponding x registers to r13
10060      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10061      * for the mode.
10062      */
10063     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10064         env->regs[13] = env->xregs[13];
10065         env->regs[14] = env->xregs[14];
10066     } else {
10067         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10068 
10069         /*
10070          * HYP is an exception in that it does not have its own banked r14 but
10071          * shares the USR r14
10072          */
10073         if (mode == ARM_CPU_MODE_HYP) {
10074             env->regs[14] = env->xregs[14];
10075         } else {
10076             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10077         }
10078     }
10079 
10080     if (mode == ARM_CPU_MODE_HYP) {
10081         env->regs[13] = env->xregs[15];
10082     } else {
10083         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10084     }
10085 
10086     if (mode == ARM_CPU_MODE_IRQ) {
10087         env->regs[14] = env->xregs[16];
10088         env->regs[13] = env->xregs[17];
10089     } else {
10090         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10091         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10092     }
10093 
10094     if (mode == ARM_CPU_MODE_SVC) {
10095         env->regs[14] = env->xregs[18];
10096         env->regs[13] = env->xregs[19];
10097     } else {
10098         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10099         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10100     }
10101 
10102     if (mode == ARM_CPU_MODE_ABT) {
10103         env->regs[14] = env->xregs[20];
10104         env->regs[13] = env->xregs[21];
10105     } else {
10106         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10107         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10108     }
10109 
10110     if (mode == ARM_CPU_MODE_UND) {
10111         env->regs[14] = env->xregs[22];
10112         env->regs[13] = env->xregs[23];
10113     } else {
10114         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10115         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10116     }
10117 
10118     /*
10119      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10120      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10121      * FIQ bank for r8-r14.
10122      */
10123     if (mode == ARM_CPU_MODE_FIQ) {
10124         for (i = 24; i < 31; i++) {
10125             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10126         }
10127     } else {
10128         for (i = 24; i < 29; i++) {
10129             env->fiq_regs[i - 24] = env->xregs[i];
10130         }
10131         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10132         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10133     }
10134 
10135     env->regs[15] = env->pc;
10136 }
10137 
10138 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10139                                    uint32_t mask, uint32_t offset,
10140                                    uint32_t newpc)
10141 {
10142     int new_el;
10143 
10144     /* Change the CPU state so as to actually take the exception. */
10145     switch_mode(env, new_mode);
10146 
10147     /*
10148      * For exceptions taken to AArch32 we must clear the SS bit in both
10149      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10150      */
10151     env->pstate &= ~PSTATE_SS;
10152     env->spsr = cpsr_read(env);
10153     /* Clear IT bits.  */
10154     env->condexec_bits = 0;
10155     /* Switch to the new mode, and to the correct instruction set.  */
10156     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10157 
10158     /* This must be after mode switching. */
10159     new_el = arm_current_el(env);
10160 
10161     /* Set new mode endianness */
10162     env->uncached_cpsr &= ~CPSR_E;
10163     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10164         env->uncached_cpsr |= CPSR_E;
10165     }
10166     /* J and IL must always be cleared for exception entry */
10167     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10168     env->daif |= mask;
10169 
10170     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10171         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10172             env->uncached_cpsr |= CPSR_SSBS;
10173         } else {
10174             env->uncached_cpsr &= ~CPSR_SSBS;
10175         }
10176     }
10177 
10178     if (new_mode == ARM_CPU_MODE_HYP) {
10179         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10180         env->elr_el[2] = env->regs[15];
10181     } else {
10182         /* CPSR.PAN is normally preserved preserved unless...  */
10183         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10184             switch (new_el) {
10185             case 3:
10186                 if (!arm_is_secure_below_el3(env)) {
10187                     /* ... the target is EL3, from non-secure state.  */
10188                     env->uncached_cpsr &= ~CPSR_PAN;
10189                     break;
10190                 }
10191                 /* ... the target is EL3, from secure state ... */
10192                 /* fall through */
10193             case 1:
10194                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10195                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10196                     env->uncached_cpsr |= CPSR_PAN;
10197                 }
10198                 break;
10199             }
10200         }
10201         /*
10202          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10203          * and we should just guard the thumb mode on V4
10204          */
10205         if (arm_feature(env, ARM_FEATURE_V4T)) {
10206             env->thumb =
10207                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10208         }
10209         env->regs[14] = env->regs[15] + offset;
10210     }
10211     env->regs[15] = newpc;
10212 
10213     if (tcg_enabled()) {
10214         arm_rebuild_hflags(env);
10215     }
10216 }
10217 
10218 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10219 {
10220     /*
10221      * Handle exception entry to Hyp mode; this is sufficiently
10222      * different to entry to other AArch32 modes that we handle it
10223      * separately here.
10224      *
10225      * The vector table entry used is always the 0x14 Hyp mode entry point,
10226      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10227      * The offset applied to the preferred return address is always zero
10228      * (see DDI0487C.a section G1.12.3).
10229      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10230      */
10231     uint32_t addr, mask;
10232     ARMCPU *cpu = ARM_CPU(cs);
10233     CPUARMState *env = &cpu->env;
10234 
10235     switch (cs->exception_index) {
10236     case EXCP_UDEF:
10237         addr = 0x04;
10238         break;
10239     case EXCP_SWI:
10240         addr = 0x08;
10241         break;
10242     case EXCP_BKPT:
10243         /* Fall through to prefetch abort.  */
10244     case EXCP_PREFETCH_ABORT:
10245         env->cp15.ifar_s = env->exception.vaddress;
10246         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10247                       (uint32_t)env->exception.vaddress);
10248         addr = 0x0c;
10249         break;
10250     case EXCP_DATA_ABORT:
10251         env->cp15.dfar_s = env->exception.vaddress;
10252         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10253                       (uint32_t)env->exception.vaddress);
10254         addr = 0x10;
10255         break;
10256     case EXCP_IRQ:
10257         addr = 0x18;
10258         break;
10259     case EXCP_FIQ:
10260         addr = 0x1c;
10261         break;
10262     case EXCP_HVC:
10263         addr = 0x08;
10264         break;
10265     case EXCP_HYP_TRAP:
10266         addr = 0x14;
10267         break;
10268     default:
10269         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10270     }
10271 
10272     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10273         if (!arm_feature(env, ARM_FEATURE_V8)) {
10274             /*
10275              * QEMU syndrome values are v8-style. v7 has the IL bit
10276              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10277              * If this is a v7 CPU, squash the IL bit in those cases.
10278              */
10279             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10280                 (cs->exception_index == EXCP_DATA_ABORT &&
10281                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10282                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10283                 env->exception.syndrome &= ~ARM_EL_IL;
10284             }
10285         }
10286         env->cp15.esr_el[2] = env->exception.syndrome;
10287     }
10288 
10289     if (arm_current_el(env) != 2 && addr < 0x14) {
10290         addr = 0x14;
10291     }
10292 
10293     mask = 0;
10294     if (!(env->cp15.scr_el3 & SCR_EA)) {
10295         mask |= CPSR_A;
10296     }
10297     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10298         mask |= CPSR_I;
10299     }
10300     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10301         mask |= CPSR_F;
10302     }
10303 
10304     addr += env->cp15.hvbar;
10305 
10306     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10307 }
10308 
10309 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10310 {
10311     ARMCPU *cpu = ARM_CPU(cs);
10312     CPUARMState *env = &cpu->env;
10313     uint32_t addr;
10314     uint32_t mask;
10315     int new_mode;
10316     uint32_t offset;
10317     uint32_t moe;
10318 
10319     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10320     switch (syn_get_ec(env->exception.syndrome)) {
10321     case EC_BREAKPOINT:
10322     case EC_BREAKPOINT_SAME_EL:
10323         moe = 1;
10324         break;
10325     case EC_WATCHPOINT:
10326     case EC_WATCHPOINT_SAME_EL:
10327         moe = 10;
10328         break;
10329     case EC_AA32_BKPT:
10330         moe = 3;
10331         break;
10332     case EC_VECTORCATCH:
10333         moe = 5;
10334         break;
10335     default:
10336         moe = 0;
10337         break;
10338     }
10339 
10340     if (moe) {
10341         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10342     }
10343 
10344     if (env->exception.target_el == 2) {
10345         /* Debug exceptions are reported differently on AArch32 */
10346         switch (syn_get_ec(env->exception.syndrome)) {
10347         case EC_BREAKPOINT:
10348         case EC_BREAKPOINT_SAME_EL:
10349         case EC_AA32_BKPT:
10350         case EC_VECTORCATCH:
10351             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10352                                                      0, 0, 0x22);
10353             break;
10354         case EC_WATCHPOINT:
10355             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10356                                                  EC_DATAABORT);
10357             break;
10358         case EC_WATCHPOINT_SAME_EL:
10359             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10360                                                  EC_DATAABORT_SAME_EL);
10361             break;
10362         }
10363         arm_cpu_do_interrupt_aarch32_hyp(cs);
10364         return;
10365     }
10366 
10367     switch (cs->exception_index) {
10368     case EXCP_UDEF:
10369         new_mode = ARM_CPU_MODE_UND;
10370         addr = 0x04;
10371         mask = CPSR_I;
10372         if (env->thumb) {
10373             offset = 2;
10374         } else {
10375             offset = 4;
10376         }
10377         break;
10378     case EXCP_SWI:
10379         new_mode = ARM_CPU_MODE_SVC;
10380         addr = 0x08;
10381         mask = CPSR_I;
10382         /* The PC already points to the next instruction.  */
10383         offset = 0;
10384         break;
10385     case EXCP_BKPT:
10386         /* Fall through to prefetch abort.  */
10387     case EXCP_PREFETCH_ABORT:
10388         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10389         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10390         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10391                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10392         new_mode = ARM_CPU_MODE_ABT;
10393         addr = 0x0c;
10394         mask = CPSR_A | CPSR_I;
10395         offset = 4;
10396         break;
10397     case EXCP_DATA_ABORT:
10398         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10399         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10400         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10401                       env->exception.fsr,
10402                       (uint32_t)env->exception.vaddress);
10403         new_mode = ARM_CPU_MODE_ABT;
10404         addr = 0x10;
10405         mask = CPSR_A | CPSR_I;
10406         offset = 8;
10407         break;
10408     case EXCP_IRQ:
10409         new_mode = ARM_CPU_MODE_IRQ;
10410         addr = 0x18;
10411         /* Disable IRQ and imprecise data aborts.  */
10412         mask = CPSR_A | CPSR_I;
10413         offset = 4;
10414         if (env->cp15.scr_el3 & SCR_IRQ) {
10415             /* IRQ routed to monitor mode */
10416             new_mode = ARM_CPU_MODE_MON;
10417             mask |= CPSR_F;
10418         }
10419         break;
10420     case EXCP_FIQ:
10421         new_mode = ARM_CPU_MODE_FIQ;
10422         addr = 0x1c;
10423         /* Disable FIQ, IRQ and imprecise data aborts.  */
10424         mask = CPSR_A | CPSR_I | CPSR_F;
10425         if (env->cp15.scr_el3 & SCR_FIQ) {
10426             /* FIQ routed to monitor mode */
10427             new_mode = ARM_CPU_MODE_MON;
10428         }
10429         offset = 4;
10430         break;
10431     case EXCP_VIRQ:
10432         new_mode = ARM_CPU_MODE_IRQ;
10433         addr = 0x18;
10434         /* Disable IRQ and imprecise data aborts.  */
10435         mask = CPSR_A | CPSR_I;
10436         offset = 4;
10437         break;
10438     case EXCP_VFIQ:
10439         new_mode = ARM_CPU_MODE_FIQ;
10440         addr = 0x1c;
10441         /* Disable FIQ, IRQ and imprecise data aborts.  */
10442         mask = CPSR_A | CPSR_I | CPSR_F;
10443         offset = 4;
10444         break;
10445     case EXCP_VSERR:
10446         {
10447             /*
10448              * Note that this is reported as a data abort, but the DFAR
10449              * has an UNKNOWN value.  Construct the SError syndrome from
10450              * AET and ExT fields.
10451              */
10452             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10453 
10454             if (extended_addresses_enabled(env)) {
10455                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10456             } else {
10457                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10458             }
10459             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10460             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10461             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10462                           env->exception.fsr);
10463 
10464             new_mode = ARM_CPU_MODE_ABT;
10465             addr = 0x10;
10466             mask = CPSR_A | CPSR_I;
10467             offset = 8;
10468         }
10469         break;
10470     case EXCP_SMC:
10471         new_mode = ARM_CPU_MODE_MON;
10472         addr = 0x08;
10473         mask = CPSR_A | CPSR_I | CPSR_F;
10474         offset = 0;
10475         break;
10476     case EXCP_MON_TRAP:
10477         new_mode = ARM_CPU_MODE_MON;
10478         addr = 0x04;
10479         mask = CPSR_A | CPSR_I | CPSR_F;
10480         if (env->thumb) {
10481             offset = 2;
10482         } else {
10483             offset = 4;
10484         }
10485         break;
10486     default:
10487         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10488         return; /* Never happens.  Keep compiler happy.  */
10489     }
10490 
10491     if (new_mode == ARM_CPU_MODE_MON) {
10492         addr += env->cp15.mvbar;
10493     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10494         /* High vectors. When enabled, base address cannot be remapped. */
10495         addr += 0xffff0000;
10496     } else {
10497         /*
10498          * ARM v7 architectures provide a vector base address register to remap
10499          * the interrupt vector table.
10500          * This register is only followed in non-monitor mode, and is banked.
10501          * Note: only bits 31:5 are valid.
10502          */
10503         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10504     }
10505 
10506     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10507         env->cp15.scr_el3 &= ~SCR_NS;
10508     }
10509 
10510     take_aarch32_exception(env, new_mode, mask, offset, addr);
10511 }
10512 
10513 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10514 {
10515     /*
10516      * Return the register number of the AArch64 view of the AArch32
10517      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10518      * be that of the AArch32 mode the exception came from.
10519      */
10520     int mode = env->uncached_cpsr & CPSR_M;
10521 
10522     switch (aarch32_reg) {
10523     case 0 ... 7:
10524         return aarch32_reg;
10525     case 8 ... 12:
10526         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10527     case 13:
10528         switch (mode) {
10529         case ARM_CPU_MODE_USR:
10530         case ARM_CPU_MODE_SYS:
10531             return 13;
10532         case ARM_CPU_MODE_HYP:
10533             return 15;
10534         case ARM_CPU_MODE_IRQ:
10535             return 17;
10536         case ARM_CPU_MODE_SVC:
10537             return 19;
10538         case ARM_CPU_MODE_ABT:
10539             return 21;
10540         case ARM_CPU_MODE_UND:
10541             return 23;
10542         case ARM_CPU_MODE_FIQ:
10543             return 29;
10544         default:
10545             g_assert_not_reached();
10546         }
10547     case 14:
10548         switch (mode) {
10549         case ARM_CPU_MODE_USR:
10550         case ARM_CPU_MODE_SYS:
10551         case ARM_CPU_MODE_HYP:
10552             return 14;
10553         case ARM_CPU_MODE_IRQ:
10554             return 16;
10555         case ARM_CPU_MODE_SVC:
10556             return 18;
10557         case ARM_CPU_MODE_ABT:
10558             return 20;
10559         case ARM_CPU_MODE_UND:
10560             return 22;
10561         case ARM_CPU_MODE_FIQ:
10562             return 30;
10563         default:
10564             g_assert_not_reached();
10565         }
10566     case 15:
10567         return 31;
10568     default:
10569         g_assert_not_reached();
10570     }
10571 }
10572 
10573 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10574 {
10575     uint32_t ret = cpsr_read(env);
10576 
10577     /* Move DIT to the correct location for SPSR_ELx */
10578     if (ret & CPSR_DIT) {
10579         ret &= ~CPSR_DIT;
10580         ret |= PSTATE_DIT;
10581     }
10582     /* Merge PSTATE.SS into SPSR_ELx */
10583     ret |= env->pstate & PSTATE_SS;
10584 
10585     return ret;
10586 }
10587 
10588 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10589 {
10590     /* Return true if this syndrome value is a synchronous external abort */
10591     switch (syn_get_ec(syndrome)) {
10592     case EC_INSNABORT:
10593     case EC_INSNABORT_SAME_EL:
10594     case EC_DATAABORT:
10595     case EC_DATAABORT_SAME_EL:
10596         /* Look at fault status code for all the synchronous ext abort cases */
10597         switch (syndrome & 0x3f) {
10598         case 0x10:
10599         case 0x13:
10600         case 0x14:
10601         case 0x15:
10602         case 0x16:
10603         case 0x17:
10604             return true;
10605         default:
10606             return false;
10607         }
10608     default:
10609         return false;
10610     }
10611 }
10612 
10613 /* Handle exception entry to a target EL which is using AArch64 */
10614 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10615 {
10616     ARMCPU *cpu = ARM_CPU(cs);
10617     CPUARMState *env = &cpu->env;
10618     unsigned int new_el = env->exception.target_el;
10619     target_ulong addr = env->cp15.vbar_el[new_el];
10620     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10621     unsigned int old_mode;
10622     unsigned int cur_el = arm_current_el(env);
10623     int rt;
10624 
10625     if (tcg_enabled()) {
10626         /*
10627          * Note that new_el can never be 0.  If cur_el is 0, then
10628          * el0_a64 is is_a64(), else el0_a64 is ignored.
10629          */
10630         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10631     }
10632 
10633     if (cur_el < new_el) {
10634         /*
10635          * Entry vector offset depends on whether the implemented EL
10636          * immediately lower than the target level is using AArch32 or AArch64
10637          */
10638         bool is_aa64;
10639         uint64_t hcr;
10640 
10641         switch (new_el) {
10642         case 3:
10643             is_aa64 = arm_scr_rw_eff(env);
10644             break;
10645         case 2:
10646             hcr = arm_hcr_el2_eff(env);
10647             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10648                 is_aa64 = (hcr & HCR_RW) != 0;
10649                 break;
10650             }
10651             /* fall through */
10652         case 1:
10653             is_aa64 = is_a64(env);
10654             break;
10655         default:
10656             g_assert_not_reached();
10657         }
10658 
10659         if (is_aa64) {
10660             addr += 0x400;
10661         } else {
10662             addr += 0x600;
10663         }
10664     } else if (pstate_read(env) & PSTATE_SP) {
10665         addr += 0x200;
10666     }
10667 
10668     switch (cs->exception_index) {
10669     case EXCP_GPC:
10670         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10671                       env->cp15.mfar_el3);
10672         /* fall through */
10673     case EXCP_PREFETCH_ABORT:
10674     case EXCP_DATA_ABORT:
10675         /*
10676          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10677          * to be taken to the SError vector entrypoint.
10678          */
10679         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10680             syndrome_is_sync_extabt(env->exception.syndrome)) {
10681             addr += 0x180;
10682         }
10683         env->cp15.far_el[new_el] = env->exception.vaddress;
10684         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10685                       env->cp15.far_el[new_el]);
10686         /* fall through */
10687     case EXCP_BKPT:
10688     case EXCP_UDEF:
10689     case EXCP_SWI:
10690     case EXCP_HVC:
10691     case EXCP_HYP_TRAP:
10692     case EXCP_SMC:
10693         switch (syn_get_ec(env->exception.syndrome)) {
10694         case EC_ADVSIMDFPACCESSTRAP:
10695             /*
10696              * QEMU internal FP/SIMD syndromes from AArch32 include the
10697              * TA and coproc fields which are only exposed if the exception
10698              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10699              * AArch64 format syndrome.
10700              */
10701             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10702             break;
10703         case EC_CP14RTTRAP:
10704         case EC_CP15RTTRAP:
10705         case EC_CP14DTTRAP:
10706             /*
10707              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10708              * the raw register field from the insn; when taking this to
10709              * AArch64 we must convert it to the AArch64 view of the register
10710              * number. Notice that we read a 4-bit AArch32 register number and
10711              * write back a 5-bit AArch64 one.
10712              */
10713             rt = extract32(env->exception.syndrome, 5, 4);
10714             rt = aarch64_regnum(env, rt);
10715             env->exception.syndrome = deposit32(env->exception.syndrome,
10716                                                 5, 5, rt);
10717             break;
10718         case EC_CP15RRTTRAP:
10719         case EC_CP14RRTTRAP:
10720             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10721             rt = extract32(env->exception.syndrome, 5, 4);
10722             rt = aarch64_regnum(env, rt);
10723             env->exception.syndrome = deposit32(env->exception.syndrome,
10724                                                 5, 5, rt);
10725             rt = extract32(env->exception.syndrome, 10, 4);
10726             rt = aarch64_regnum(env, rt);
10727             env->exception.syndrome = deposit32(env->exception.syndrome,
10728                                                 10, 5, rt);
10729             break;
10730         }
10731         env->cp15.esr_el[new_el] = env->exception.syndrome;
10732         break;
10733     case EXCP_IRQ:
10734     case EXCP_VIRQ:
10735     case EXCP_NMI:
10736     case EXCP_VINMI:
10737         addr += 0x80;
10738         break;
10739     case EXCP_FIQ:
10740     case EXCP_VFIQ:
10741     case EXCP_VFNMI:
10742         addr += 0x100;
10743         break;
10744     case EXCP_VSERR:
10745         addr += 0x180;
10746         /* Construct the SError syndrome from IDS and ISS fields. */
10747         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10748         env->cp15.esr_el[new_el] = env->exception.syndrome;
10749         break;
10750     default:
10751         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10752     }
10753 
10754     if (is_a64(env)) {
10755         old_mode = pstate_read(env);
10756         aarch64_save_sp(env, arm_current_el(env));
10757         env->elr_el[new_el] = env->pc;
10758 
10759         if (cur_el == 1 && new_el == 1) {
10760             uint64_t hcr = arm_hcr_el2_eff(env);
10761             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10762                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10763                 /*
10764                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10765                  * by setting M[3:2] to 0b10.
10766                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10767                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10768                  */
10769                 old_mode = deposit32(old_mode, 2, 2, 2);
10770             }
10771         }
10772     } else {
10773         old_mode = cpsr_read_for_spsr_elx(env);
10774         env->elr_el[new_el] = env->regs[15];
10775 
10776         aarch64_sync_32_to_64(env);
10777 
10778         env->condexec_bits = 0;
10779     }
10780     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10781 
10782     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10783     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10784                   env->elr_el[new_el]);
10785 
10786     if (cpu_isar_feature(aa64_pan, cpu)) {
10787         /* The value of PSTATE.PAN is normally preserved, except when ... */
10788         new_mode |= old_mode & PSTATE_PAN;
10789         switch (new_el) {
10790         case 2:
10791             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10792             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10793                 != (HCR_E2H | HCR_TGE)) {
10794                 break;
10795             }
10796             /* fall through */
10797         case 1:
10798             /* ... the target is EL1 ... */
10799             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10800             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10801                 new_mode |= PSTATE_PAN;
10802             }
10803             break;
10804         }
10805     }
10806     if (cpu_isar_feature(aa64_mte, cpu)) {
10807         new_mode |= PSTATE_TCO;
10808     }
10809 
10810     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10811         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10812             new_mode |= PSTATE_SSBS;
10813         } else {
10814             new_mode &= ~PSTATE_SSBS;
10815         }
10816     }
10817 
10818     if (cpu_isar_feature(aa64_nmi, cpu)) {
10819         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10820             new_mode |= PSTATE_ALLINT;
10821         } else {
10822             new_mode &= ~PSTATE_ALLINT;
10823         }
10824     }
10825 
10826     pstate_write(env, PSTATE_DAIF | new_mode);
10827     env->aarch64 = true;
10828     aarch64_restore_sp(env, new_el);
10829 
10830     if (tcg_enabled()) {
10831         helper_rebuild_hflags_a64(env, new_el);
10832     }
10833 
10834     env->pc = addr;
10835 
10836     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10837                   new_el, env->pc, pstate_read(env));
10838 }
10839 
10840 /*
10841  * Do semihosting call and set the appropriate return value. All the
10842  * permission and validity checks have been done at translate time.
10843  *
10844  * We only see semihosting exceptions in TCG only as they are not
10845  * trapped to the hypervisor in KVM.
10846  */
10847 #ifdef CONFIG_TCG
10848 static void tcg_handle_semihosting(CPUState *cs)
10849 {
10850     ARMCPU *cpu = ARM_CPU(cs);
10851     CPUARMState *env = &cpu->env;
10852 
10853     if (is_a64(env)) {
10854         qemu_log_mask(CPU_LOG_INT,
10855                       "...handling as semihosting call 0x%" PRIx64 "\n",
10856                       env->xregs[0]);
10857         do_common_semihosting(cs);
10858         env->pc += 4;
10859     } else {
10860         qemu_log_mask(CPU_LOG_INT,
10861                       "...handling as semihosting call 0x%x\n",
10862                       env->regs[0]);
10863         do_common_semihosting(cs);
10864         env->regs[15] += env->thumb ? 2 : 4;
10865     }
10866 }
10867 #endif
10868 
10869 /*
10870  * Handle a CPU exception for A and R profile CPUs.
10871  * Do any appropriate logging, handle PSCI calls, and then hand off
10872  * to the AArch64-entry or AArch32-entry function depending on the
10873  * target exception level's register width.
10874  *
10875  * Note: this is used for both TCG (as the do_interrupt tcg op),
10876  *       and KVM to re-inject guest debug exceptions, and to
10877  *       inject a Synchronous-External-Abort.
10878  */
10879 void arm_cpu_do_interrupt(CPUState *cs)
10880 {
10881     ARMCPU *cpu = ARM_CPU(cs);
10882     CPUARMState *env = &cpu->env;
10883     unsigned int new_el = env->exception.target_el;
10884 
10885     assert(!arm_feature(env, ARM_FEATURE_M));
10886 
10887     arm_log_exception(cs);
10888     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10889                   new_el);
10890     if (qemu_loglevel_mask(CPU_LOG_INT)
10891         && !excp_is_internal(cs->exception_index)) {
10892         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10893                       syn_get_ec(env->exception.syndrome),
10894                       env->exception.syndrome);
10895     }
10896 
10897     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10898         arm_handle_psci_call(cpu);
10899         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10900         return;
10901     }
10902 
10903     /*
10904      * Semihosting semantics depend on the register width of the code
10905      * that caused the exception, not the target exception level, so
10906      * must be handled here.
10907      */
10908 #ifdef CONFIG_TCG
10909     if (cs->exception_index == EXCP_SEMIHOST) {
10910         tcg_handle_semihosting(cs);
10911         return;
10912     }
10913 #endif
10914 
10915     /*
10916      * Hooks may change global state so BQL should be held, also the
10917      * BQL needs to be held for any modification of
10918      * cs->interrupt_request.
10919      */
10920     g_assert(bql_locked());
10921 
10922     arm_call_pre_el_change_hook(cpu);
10923 
10924     assert(!excp_is_internal(cs->exception_index));
10925     if (arm_el_is_aa64(env, new_el)) {
10926         arm_cpu_do_interrupt_aarch64(cs);
10927     } else {
10928         arm_cpu_do_interrupt_aarch32(cs);
10929     }
10930 
10931     arm_call_el_change_hook(cpu);
10932 
10933     if (!kvm_enabled()) {
10934         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10935     }
10936 }
10937 #endif /* !CONFIG_USER_ONLY */
10938 
10939 uint64_t arm_sctlr(CPUARMState *env, int el)
10940 {
10941     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10942     if (el == 0) {
10943         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10944         switch (mmu_idx) {
10945         case ARMMMUIdx_E20_0:
10946             el = 2;
10947             break;
10948         case ARMMMUIdx_E30_0:
10949             el = 3;
10950             break;
10951         default:
10952             el = 1;
10953             break;
10954         }
10955     }
10956     return env->cp15.sctlr_el[el];
10957 }
10958 
10959 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10960 {
10961     if (regime_has_2_ranges(mmu_idx)) {
10962         return extract64(tcr, 37, 2);
10963     } else if (regime_is_stage2(mmu_idx)) {
10964         return 0; /* VTCR_EL2 */
10965     } else {
10966         /* Replicate the single TBI bit so we always have 2 bits.  */
10967         return extract32(tcr, 20, 1) * 3;
10968     }
10969 }
10970 
10971 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10972 {
10973     if (regime_has_2_ranges(mmu_idx)) {
10974         return extract64(tcr, 51, 2);
10975     } else if (regime_is_stage2(mmu_idx)) {
10976         return 0; /* VTCR_EL2 */
10977     } else {
10978         /* Replicate the single TBID bit so we always have 2 bits.  */
10979         return extract32(tcr, 29, 1) * 3;
10980     }
10981 }
10982 
10983 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10984 {
10985     if (regime_has_2_ranges(mmu_idx)) {
10986         return extract64(tcr, 57, 2);
10987     } else {
10988         /* Replicate the single TCMA bit so we always have 2 bits.  */
10989         return extract32(tcr, 30, 1) * 3;
10990     }
10991 }
10992 
10993 static ARMGranuleSize tg0_to_gran_size(int tg)
10994 {
10995     switch (tg) {
10996     case 0:
10997         return Gran4K;
10998     case 1:
10999         return Gran64K;
11000     case 2:
11001         return Gran16K;
11002     default:
11003         return GranInvalid;
11004     }
11005 }
11006 
11007 static ARMGranuleSize tg1_to_gran_size(int tg)
11008 {
11009     switch (tg) {
11010     case 1:
11011         return Gran16K;
11012     case 2:
11013         return Gran4K;
11014     case 3:
11015         return Gran64K;
11016     default:
11017         return GranInvalid;
11018     }
11019 }
11020 
11021 static inline bool have4k(ARMCPU *cpu, bool stage2)
11022 {
11023     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11024         : cpu_isar_feature(aa64_tgran4, cpu);
11025 }
11026 
11027 static inline bool have16k(ARMCPU *cpu, bool stage2)
11028 {
11029     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11030         : cpu_isar_feature(aa64_tgran16, cpu);
11031 }
11032 
11033 static inline bool have64k(ARMCPU *cpu, bool stage2)
11034 {
11035     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11036         : cpu_isar_feature(aa64_tgran64, cpu);
11037 }
11038 
11039 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11040                                          bool stage2)
11041 {
11042     switch (gran) {
11043     case Gran4K:
11044         if (have4k(cpu, stage2)) {
11045             return gran;
11046         }
11047         break;
11048     case Gran16K:
11049         if (have16k(cpu, stage2)) {
11050             return gran;
11051         }
11052         break;
11053     case Gran64K:
11054         if (have64k(cpu, stage2)) {
11055             return gran;
11056         }
11057         break;
11058     case GranInvalid:
11059         break;
11060     }
11061     /*
11062      * If the guest selects a granule size that isn't implemented,
11063      * the architecture requires that we behave as if it selected one
11064      * that is (with an IMPDEF choice of which one to pick). We choose
11065      * to implement the smallest supported granule size.
11066      */
11067     if (have4k(cpu, stage2)) {
11068         return Gran4K;
11069     }
11070     if (have16k(cpu, stage2)) {
11071         return Gran16K;
11072     }
11073     assert(have64k(cpu, stage2));
11074     return Gran64K;
11075 }
11076 
11077 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11078                                    ARMMMUIdx mmu_idx, bool data,
11079                                    bool el1_is_aa32)
11080 {
11081     uint64_t tcr = regime_tcr(env, mmu_idx);
11082     bool epd, hpd, tsz_oob, ds, ha, hd;
11083     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11084     ARMGranuleSize gran;
11085     ARMCPU *cpu = env_archcpu(env);
11086     bool stage2 = regime_is_stage2(mmu_idx);
11087 
11088     if (!regime_has_2_ranges(mmu_idx)) {
11089         select = 0;
11090         tsz = extract32(tcr, 0, 6);
11091         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11092         if (stage2) {
11093             /* VTCR_EL2 */
11094             hpd = false;
11095         } else {
11096             hpd = extract32(tcr, 24, 1);
11097         }
11098         epd = false;
11099         sh = extract32(tcr, 12, 2);
11100         ps = extract32(tcr, 16, 3);
11101         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11102         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11103         ds = extract64(tcr, 32, 1);
11104     } else {
11105         bool e0pd;
11106 
11107         /*
11108          * Bit 55 is always between the two regions, and is canonical for
11109          * determining if address tagging is enabled.
11110          */
11111         select = extract64(va, 55, 1);
11112         if (!select) {
11113             tsz = extract32(tcr, 0, 6);
11114             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11115             epd = extract32(tcr, 7, 1);
11116             sh = extract32(tcr, 12, 2);
11117             hpd = extract64(tcr, 41, 1);
11118             e0pd = extract64(tcr, 55, 1);
11119         } else {
11120             tsz = extract32(tcr, 16, 6);
11121             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11122             epd = extract32(tcr, 23, 1);
11123             sh = extract32(tcr, 28, 2);
11124             hpd = extract64(tcr, 42, 1);
11125             e0pd = extract64(tcr, 56, 1);
11126         }
11127         ps = extract64(tcr, 32, 3);
11128         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11129         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11130         ds = extract64(tcr, 59, 1);
11131 
11132         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11133             regime_is_user(env, mmu_idx)) {
11134             epd = true;
11135         }
11136     }
11137 
11138     gran = sanitize_gran_size(cpu, gran, stage2);
11139 
11140     if (cpu_isar_feature(aa64_st, cpu)) {
11141         max_tsz = 48 - (gran == Gran64K);
11142     } else {
11143         max_tsz = 39;
11144     }
11145 
11146     /*
11147      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11148      * adjust the effective value of DS, as documented.
11149      */
11150     min_tsz = 16;
11151     if (gran == Gran64K) {
11152         if (cpu_isar_feature(aa64_lva, cpu)) {
11153             min_tsz = 12;
11154         }
11155         ds = false;
11156     } else if (ds) {
11157         if (regime_is_stage2(mmu_idx)) {
11158             if (gran == Gran16K) {
11159                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11160             } else {
11161                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11162             }
11163         } else {
11164             if (gran == Gran16K) {
11165                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11166             } else {
11167                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11168             }
11169         }
11170         if (ds) {
11171             min_tsz = 12;
11172         }
11173     }
11174 
11175     if (stage2 && el1_is_aa32) {
11176         /*
11177          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11178          * are loosened: a configured IPA of 40 bits is permitted even if
11179          * the implemented PA is less than that (and so a 40 bit IPA would
11180          * fault for an AArch64 EL1). See R_DTLMN.
11181          */
11182         min_tsz = MIN(min_tsz, 24);
11183     }
11184 
11185     if (tsz > max_tsz) {
11186         tsz = max_tsz;
11187         tsz_oob = true;
11188     } else if (tsz < min_tsz) {
11189         tsz = min_tsz;
11190         tsz_oob = true;
11191     } else {
11192         tsz_oob = false;
11193     }
11194 
11195     /* Present TBI as a composite with TBID.  */
11196     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11197     if (!data) {
11198         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11199     }
11200     tbi = (tbi >> select) & 1;
11201 
11202     return (ARMVAParameters) {
11203         .tsz = tsz,
11204         .ps = ps,
11205         .sh = sh,
11206         .select = select,
11207         .tbi = tbi,
11208         .epd = epd,
11209         .hpd = hpd,
11210         .tsz_oob = tsz_oob,
11211         .ds = ds,
11212         .ha = ha,
11213         .hd = ha && hd,
11214         .gran = gran,
11215     };
11216 }
11217 
11218 
11219 /*
11220  * Return the exception level to which FP-disabled exceptions should
11221  * be taken, or 0 if FP is enabled.
11222  */
11223 int fp_exception_el(CPUARMState *env, int cur_el)
11224 {
11225 #ifndef CONFIG_USER_ONLY
11226     uint64_t hcr_el2;
11227 
11228     /*
11229      * CPACR and the CPTR registers don't exist before v6, so FP is
11230      * always accessible
11231      */
11232     if (!arm_feature(env, ARM_FEATURE_V6)) {
11233         return 0;
11234     }
11235 
11236     if (arm_feature(env, ARM_FEATURE_M)) {
11237         /* CPACR can cause a NOCP UsageFault taken to current security state */
11238         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11239             return 1;
11240         }
11241 
11242         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11243             if (!extract32(env->v7m.nsacr, 10, 1)) {
11244                 /* FP insns cause a NOCP UsageFault taken to Secure */
11245                 return 3;
11246             }
11247         }
11248 
11249         return 0;
11250     }
11251 
11252     hcr_el2 = arm_hcr_el2_eff(env);
11253 
11254     /*
11255      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11256      * 0, 2 : trap EL0 and EL1/PL1 accesses
11257      * 1    : trap only EL0 accesses
11258      * 3    : trap no accesses
11259      * This register is ignored if E2H+TGE are both set.
11260      */
11261     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11262         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11263 
11264         switch (fpen) {
11265         case 1:
11266             if (cur_el != 0) {
11267                 break;
11268             }
11269             /* fall through */
11270         case 0:
11271         case 2:
11272             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11273             if (!arm_el_is_aa64(env, 3)
11274                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11275                 return 3;
11276             }
11277             if (cur_el <= 1) {
11278                 return 1;
11279             }
11280             break;
11281         }
11282     }
11283 
11284     /*
11285      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11286      * to control non-secure access to the FPU. It doesn't have any
11287      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11288      */
11289     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11290          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11291         if (!extract32(env->cp15.nsacr, 10, 1)) {
11292             /* FP insns act as UNDEF */
11293             return cur_el == 2 ? 2 : 1;
11294         }
11295     }
11296 
11297     /*
11298      * CPTR_EL2 is present in v7VE or v8, and changes format
11299      * with HCR_EL2.E2H (regardless of TGE).
11300      */
11301     if (cur_el <= 2) {
11302         if (hcr_el2 & HCR_E2H) {
11303             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11304             case 1:
11305                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11306                     break;
11307                 }
11308                 /* fall through */
11309             case 0:
11310             case 2:
11311                 return 2;
11312             }
11313         } else if (arm_is_el2_enabled(env)) {
11314             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11315                 return 2;
11316             }
11317         }
11318     }
11319 
11320     /* CPTR_EL3 : present in v8 */
11321     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11322         /* Trap all FP ops to EL3 */
11323         return 3;
11324     }
11325 #endif
11326     return 0;
11327 }
11328 
11329 /* Return the exception level we're running at if this is our mmu_idx */
11330 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11331 {
11332     if (mmu_idx & ARM_MMU_IDX_M) {
11333         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11334     }
11335 
11336     switch (mmu_idx) {
11337     case ARMMMUIdx_E10_0:
11338     case ARMMMUIdx_E20_0:
11339     case ARMMMUIdx_E30_0:
11340         return 0;
11341     case ARMMMUIdx_E10_1:
11342     case ARMMMUIdx_E10_1_PAN:
11343         return 1;
11344     case ARMMMUIdx_E2:
11345     case ARMMMUIdx_E20_2:
11346     case ARMMMUIdx_E20_2_PAN:
11347         return 2;
11348     case ARMMMUIdx_E3:
11349     case ARMMMUIdx_E30_3_PAN:
11350         return 3;
11351     default:
11352         g_assert_not_reached();
11353     }
11354 }
11355 
11356 #ifndef CONFIG_TCG
11357 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11358 {
11359     g_assert_not_reached();
11360 }
11361 #endif
11362 
11363 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11364 {
11365     ARMMMUIdx idx;
11366     uint64_t hcr;
11367 
11368     if (arm_feature(env, ARM_FEATURE_M)) {
11369         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11370     }
11371 
11372     /* See ARM pseudo-function ELIsInHost.  */
11373     switch (el) {
11374     case 0:
11375         hcr = arm_hcr_el2_eff(env);
11376         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11377             idx = ARMMMUIdx_E20_0;
11378         } else if (arm_is_secure_below_el3(env) &&
11379                    !arm_el_is_aa64(env, 3)) {
11380             idx = ARMMMUIdx_E30_0;
11381         } else {
11382             idx = ARMMMUIdx_E10_0;
11383         }
11384         break;
11385     case 1:
11386         if (arm_pan_enabled(env)) {
11387             idx = ARMMMUIdx_E10_1_PAN;
11388         } else {
11389             idx = ARMMMUIdx_E10_1;
11390         }
11391         break;
11392     case 2:
11393         /* Note that TGE does not apply at EL2.  */
11394         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11395             if (arm_pan_enabled(env)) {
11396                 idx = ARMMMUIdx_E20_2_PAN;
11397             } else {
11398                 idx = ARMMMUIdx_E20_2;
11399             }
11400         } else {
11401             idx = ARMMMUIdx_E2;
11402         }
11403         break;
11404     case 3:
11405         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11406             return ARMMMUIdx_E30_3_PAN;
11407         }
11408         return ARMMMUIdx_E3;
11409     default:
11410         g_assert_not_reached();
11411     }
11412 
11413     return idx;
11414 }
11415 
11416 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11417 {
11418     return arm_mmu_idx_el(env, arm_current_el(env));
11419 }
11420 
11421 static bool mve_no_pred(CPUARMState *env)
11422 {
11423     /*
11424      * Return true if there is definitely no predication of MVE
11425      * instructions by VPR or LTPSIZE. (Returning false even if there
11426      * isn't any predication is OK; generated code will just be
11427      * a little worse.)
11428      * If the CPU does not implement MVE then this TB flag is always 0.
11429      *
11430      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11431      * logic in gen_update_fp_context() needs to be updated to match.
11432      *
11433      * We do not include the effect of the ECI bits here -- they are
11434      * tracked in other TB flags. This simplifies the logic for
11435      * "when did we emit code that changes the MVE_NO_PRED TB flag
11436      * and thus need to end the TB?".
11437      */
11438     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11439         return false;
11440     }
11441     if (env->v7m.vpr) {
11442         return false;
11443     }
11444     if (env->v7m.ltpsize < 4) {
11445         return false;
11446     }
11447     return true;
11448 }
11449 
11450 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11451                           uint64_t *cs_base, uint32_t *pflags)
11452 {
11453     CPUARMTBFlags flags;
11454 
11455     assert_hflags_rebuild_correctly(env);
11456     flags = env->hflags;
11457 
11458     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11459         *pc = env->pc;
11460         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11461             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11462         }
11463     } else {
11464         *pc = env->regs[15];
11465 
11466         if (arm_feature(env, ARM_FEATURE_M)) {
11467             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11468                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11469                 != env->v7m.secure) {
11470                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11471             }
11472 
11473             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11474                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11475                  (env->v7m.secure &&
11476                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11477                 /*
11478                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11479                  * active FP context; we must create a new FP context before
11480                  * executing any FP insn.
11481                  */
11482                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11483             }
11484 
11485             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11486             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11487                 DP_TBFLAG_M32(flags, LSPACT, 1);
11488             }
11489 
11490             if (mve_no_pred(env)) {
11491                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11492             }
11493         } else {
11494             /*
11495              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11496              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11497              */
11498             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11499                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11500             } else {
11501                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11502                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11503             }
11504             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11505                 DP_TBFLAG_A32(flags, VFPEN, 1);
11506             }
11507         }
11508 
11509         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11510         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11511     }
11512 
11513     /*
11514      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11515      * states defined in the ARM ARM for software singlestep:
11516      *  SS_ACTIVE   PSTATE.SS   State
11517      *     0            x       Inactive (the TB flag for SS is always 0)
11518      *     1            0       Active-pending
11519      *     1            1       Active-not-pending
11520      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11521      */
11522     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11523         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11524     }
11525 
11526     *pflags = flags.flags;
11527     *cs_base = flags.flags2;
11528 }
11529 
11530 #ifdef TARGET_AARCH64
11531 /*
11532  * The manual says that when SVE is enabled and VQ is widened the
11533  * implementation is allowed to zero the previously inaccessible
11534  * portion of the registers.  The corollary to that is that when
11535  * SVE is enabled and VQ is narrowed we are also allowed to zero
11536  * the now inaccessible portion of the registers.
11537  *
11538  * The intent of this is that no predicate bit beyond VQ is ever set.
11539  * Which means that some operations on predicate registers themselves
11540  * may operate on full uint64_t or even unrolled across the maximum
11541  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11542  * may well be cheaper than conditionals to restrict the operation
11543  * to the relevant portion of a uint16_t[16].
11544  */
11545 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11546 {
11547     int i, j;
11548     uint64_t pmask;
11549 
11550     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11551     assert(vq <= env_archcpu(env)->sve_max_vq);
11552 
11553     /* Zap the high bits of the zregs.  */
11554     for (i = 0; i < 32; i++) {
11555         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11556     }
11557 
11558     /* Zap the high bits of the pregs and ffr.  */
11559     pmask = 0;
11560     if (vq & 3) {
11561         pmask = ~(-1ULL << (16 * (vq & 3)));
11562     }
11563     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11564         for (i = 0; i < 17; ++i) {
11565             env->vfp.pregs[i].p[j] &= pmask;
11566         }
11567         pmask = 0;
11568     }
11569 }
11570 
11571 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11572 {
11573     int exc_el;
11574 
11575     if (sm) {
11576         exc_el = sme_exception_el(env, el);
11577     } else {
11578         exc_el = sve_exception_el(env, el);
11579     }
11580     if (exc_el) {
11581         return 0; /* disabled */
11582     }
11583     return sve_vqm1_for_el_sm(env, el, sm);
11584 }
11585 
11586 /*
11587  * Notice a change in SVE vector size when changing EL.
11588  */
11589 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11590                            int new_el, bool el0_a64)
11591 {
11592     ARMCPU *cpu = env_archcpu(env);
11593     int old_len, new_len;
11594     bool old_a64, new_a64, sm;
11595 
11596     /* Nothing to do if no SVE.  */
11597     if (!cpu_isar_feature(aa64_sve, cpu)) {
11598         return;
11599     }
11600 
11601     /* Nothing to do if FP is disabled in either EL.  */
11602     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11603         return;
11604     }
11605 
11606     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11607     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11608 
11609     /*
11610      * Both AArch64.TakeException and AArch64.ExceptionReturn
11611      * invoke ResetSVEState when taking an exception from, or
11612      * returning to, AArch32 state when PSTATE.SM is enabled.
11613      */
11614     sm = FIELD_EX64(env->svcr, SVCR, SM);
11615     if (old_a64 != new_a64 && sm) {
11616         arm_reset_sve_state(env);
11617         return;
11618     }
11619 
11620     /*
11621      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11622      * at ELx, or not available because the EL is in AArch32 state, then
11623      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11624      * has an effective value of 0".
11625      *
11626      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11627      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11628      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11629      * we already have the correct register contents when encountering the
11630      * vq0->vq0 transition between EL0->EL1.
11631      */
11632     old_len = new_len = 0;
11633     if (old_a64) {
11634         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11635     }
11636     if (new_a64) {
11637         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11638     }
11639 
11640     /* When changing vector length, clear inaccessible state.  */
11641     if (new_len < old_len) {
11642         aarch64_sve_narrow_vq(env, new_len + 1);
11643     }
11644 }
11645 #endif
11646 
11647 #ifndef CONFIG_USER_ONLY
11648 ARMSecuritySpace arm_security_space(CPUARMState *env)
11649 {
11650     if (arm_feature(env, ARM_FEATURE_M)) {
11651         return arm_secure_to_space(env->v7m.secure);
11652     }
11653 
11654     /*
11655      * If EL3 is not supported then the secure state is implementation
11656      * defined, in which case QEMU defaults to non-secure.
11657      */
11658     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11659         return ARMSS_NonSecure;
11660     }
11661 
11662     /* Check for AArch64 EL3 or AArch32 Mon. */
11663     if (is_a64(env)) {
11664         if (extract32(env->pstate, 2, 2) == 3) {
11665             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11666                 return ARMSS_Root;
11667             } else {
11668                 return ARMSS_Secure;
11669             }
11670         }
11671     } else {
11672         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11673             return ARMSS_Secure;
11674         }
11675     }
11676 
11677     return arm_security_space_below_el3(env);
11678 }
11679 
11680 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11681 {
11682     assert(!arm_feature(env, ARM_FEATURE_M));
11683 
11684     /*
11685      * If EL3 is not supported then the secure state is implementation
11686      * defined, in which case QEMU defaults to non-secure.
11687      */
11688     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11689         return ARMSS_NonSecure;
11690     }
11691 
11692     /*
11693      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11694      * Ignoring NSE when !NS retains consistency without having to
11695      * modify other predicates.
11696      */
11697     if (!(env->cp15.scr_el3 & SCR_NS)) {
11698         return ARMSS_Secure;
11699     } else if (env->cp15.scr_el3 & SCR_NSE) {
11700         return ARMSS_Realm;
11701     } else {
11702         return ARMSS_NonSecure;
11703     }
11704 }
11705 #endif /* !CONFIG_USER_ONLY */
11706