xref: /qemu/target/arm/helper.c (revision 003d35ad6c612d13ebf0a78f828b0c3ee4f44e3d)
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 "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/cputlb.h"
22 #include "exec/exec-all.h"
23 #include "exec/translation-block.h"
24 #include "hw/irq.h"
25 #include "system/cpu-timers.h"
26 #include "system/kvm.h"
27 #include "system/tcg.h"
28 #include "qapi/error.h"
29 #include "qemu/guest-random.h"
30 #ifdef CONFIG_TCG
31 #include "semihosting/common-semi.h"
32 #endif
33 #include "cpregs.h"
34 #include "target/arm/gtimer.h"
35 
36 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
37 
38 static void switch_mode(CPUARMState *env, int mode);
39 
40 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
41 {
42     assert(ri->fieldoffset);
43     if (cpreg_field_is_64bit(ri)) {
44         return CPREG_FIELD64(env, ri);
45     } else {
46         return CPREG_FIELD32(env, ri);
47     }
48 }
49 
50 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
51 {
52     assert(ri->fieldoffset);
53     if (cpreg_field_is_64bit(ri)) {
54         CPREG_FIELD64(env, ri) = value;
55     } else {
56         CPREG_FIELD32(env, ri) = value;
57     }
58 }
59 
60 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
61 {
62     return (char *)env + ri->fieldoffset;
63 }
64 
65 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
66 {
67     /* Raw read of a coprocessor register (as needed for migration, etc). */
68     if (ri->type & ARM_CP_CONST) {
69         return ri->resetvalue;
70     } else if (ri->raw_readfn) {
71         return ri->raw_readfn(env, ri);
72     } else if (ri->readfn) {
73         return ri->readfn(env, ri);
74     } else {
75         return raw_read(env, ri);
76     }
77 }
78 
79 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
80                              uint64_t v)
81 {
82     /*
83      * Raw write of a coprocessor register (as needed for migration, etc).
84      * Note that constant registers are treated as write-ignored; the
85      * caller should check for success by whether a readback gives the
86      * value written.
87      */
88     if (ri->type & ARM_CP_CONST) {
89         return;
90     } else if (ri->raw_writefn) {
91         ri->raw_writefn(env, ri, v);
92     } else if (ri->writefn) {
93         ri->writefn(env, ri, v);
94     } else {
95         raw_write(env, ri, v);
96     }
97 }
98 
99 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
100 {
101    /*
102     * Return true if the regdef would cause an assertion if you called
103     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
104     * program bug for it not to have the NO_RAW flag).
105     * NB that returning false here doesn't necessarily mean that calling
106     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
107     * read/write access functions which are safe for raw use" from "has
108     * read/write access functions which have side effects but has forgotten
109     * to provide raw access functions".
110     * The tests here line up with the conditions in read/write_raw_cp_reg()
111     * and assertions in raw_read()/raw_write().
112     */
113     if ((ri->type & ARM_CP_CONST) ||
114         ri->fieldoffset ||
115         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
116         return false;
117     }
118     return true;
119 }
120 
121 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
122 {
123     /* Write the coprocessor state from cpu->env to the (index,value) list. */
124     int i;
125     bool ok = true;
126 
127     for (i = 0; i < cpu->cpreg_array_len; i++) {
128         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
129         const ARMCPRegInfo *ri;
130         uint64_t newval;
131 
132         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
133         if (!ri) {
134             ok = false;
135             continue;
136         }
137         if (ri->type & ARM_CP_NO_RAW) {
138             continue;
139         }
140 
141         newval = read_raw_cp_reg(&cpu->env, ri);
142         if (kvm_sync) {
143             /*
144              * Only sync if the previous list->cpustate sync succeeded.
145              * Rather than tracking the success/failure state for every
146              * item in the list, we just recheck "does the raw write we must
147              * have made in write_list_to_cpustate() read back OK" here.
148              */
149             uint64_t oldval = cpu->cpreg_values[i];
150 
151             if (oldval == newval) {
152                 continue;
153             }
154 
155             write_raw_cp_reg(&cpu->env, ri, oldval);
156             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
157                 continue;
158             }
159 
160             write_raw_cp_reg(&cpu->env, ri, newval);
161         }
162         cpu->cpreg_values[i] = newval;
163     }
164     return ok;
165 }
166 
167 bool write_list_to_cpustate(ARMCPU *cpu)
168 {
169     int i;
170     bool ok = true;
171 
172     for (i = 0; i < cpu->cpreg_array_len; i++) {
173         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
174         uint64_t v = cpu->cpreg_values[i];
175         const ARMCPRegInfo *ri;
176 
177         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
178         if (!ri) {
179             ok = false;
180             continue;
181         }
182         if (ri->type & ARM_CP_NO_RAW) {
183             continue;
184         }
185         /*
186          * Write value and confirm it reads back as written
187          * (to catch read-only registers and partially read-only
188          * registers where the incoming migration value doesn't match)
189          */
190         write_raw_cp_reg(&cpu->env, ri, v);
191         if (read_raw_cp_reg(&cpu->env, ri) != v) {
192             ok = false;
193         }
194     }
195     return ok;
196 }
197 
198 static void add_cpreg_to_list(gpointer key, gpointer opaque)
199 {
200     ARMCPU *cpu = opaque;
201     uint32_t regidx = (uintptr_t)key;
202     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
203 
204     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
205         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
206         /* The value array need not be initialized at this point */
207         cpu->cpreg_array_len++;
208     }
209 }
210 
211 static void count_cpreg(gpointer key, gpointer opaque)
212 {
213     ARMCPU *cpu = opaque;
214     const ARMCPRegInfo *ri;
215 
216     ri = g_hash_table_lookup(cpu->cp_regs, key);
217 
218     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
219         cpu->cpreg_array_len++;
220     }
221 }
222 
223 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
224 {
225     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
226     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
227 
228     if (aidx > bidx) {
229         return 1;
230     }
231     if (aidx < bidx) {
232         return -1;
233     }
234     return 0;
235 }
236 
237 void init_cpreg_list(ARMCPU *cpu)
238 {
239     /*
240      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
241      * Note that we require cpreg_tuples[] to be sorted by key ID.
242      */
243     GList *keys;
244     int arraylen;
245 
246     keys = g_hash_table_get_keys(cpu->cp_regs);
247     keys = g_list_sort(keys, cpreg_key_compare);
248 
249     cpu->cpreg_array_len = 0;
250 
251     g_list_foreach(keys, count_cpreg, cpu);
252 
253     arraylen = cpu->cpreg_array_len;
254     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259     cpu->cpreg_array_len = 0;
260 
261     g_list_foreach(keys, add_cpreg_to_list, cpu);
262 
263     assert(cpu->cpreg_array_len == arraylen);
264 
265     g_list_free(keys);
266 }
267 
268 static bool arm_pan_enabled(CPUARMState *env)
269 {
270     if (is_a64(env)) {
271         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
272             return false;
273         }
274         return env->pstate & PSTATE_PAN;
275     } else {
276         return env->uncached_cpsr & CPSR_PAN;
277     }
278 }
279 
280 /*
281  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
282  */
283 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
284                                         const ARMCPRegInfo *ri,
285                                         bool isread)
286 {
287     if (!is_a64(env) && arm_current_el(env) == 3 &&
288         arm_is_secure_below_el3(env)) {
289         return CP_ACCESS_UNDEFINED;
290     }
291     return CP_ACCESS_OK;
292 }
293 
294 /*
295  * Some secure-only AArch32 registers trap to EL3 if used from
296  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
297  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
298  * We assume that the .access field is set to PL1_RW.
299  */
300 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
301                                             const ARMCPRegInfo *ri,
302                                             bool isread)
303 {
304     if (arm_current_el(env) == 3) {
305         return CP_ACCESS_OK;
306     }
307     if (arm_is_secure_below_el3(env)) {
308         if (env->cp15.scr_el3 & SCR_EEL2) {
309             return CP_ACCESS_TRAP_EL2;
310         }
311         return CP_ACCESS_TRAP_EL3;
312     }
313     /* This will be EL1 NS and EL2 NS, which just UNDEF */
314     return CP_ACCESS_UNDEFINED;
315 }
316 
317 /*
318  * Check for traps to performance monitor registers, which are controlled
319  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
320  */
321 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
322                                  bool isread)
323 {
324     int el = arm_current_el(env);
325     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
326 
327     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
328         return CP_ACCESS_TRAP_EL2;
329     }
330     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
331         return CP_ACCESS_TRAP_EL3;
332     }
333     return CP_ACCESS_OK;
334 }
335 
336 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
337 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
338                                bool isread)
339 {
340     if (arm_current_el(env) == 1) {
341         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
342         if (arm_hcr_el2_eff(env) & trap) {
343             return CP_ACCESS_TRAP_EL2;
344         }
345     }
346     return CP_ACCESS_OK;
347 }
348 
349 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
350 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
351                                  bool isread)
352 {
353     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
354         return CP_ACCESS_TRAP_EL2;
355     }
356     return CP_ACCESS_OK;
357 }
358 
359 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
360 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
361                                   bool isread)
362 {
363     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
364         return CP_ACCESS_TRAP_EL2;
365     }
366     return CP_ACCESS_OK;
367 }
368 
369 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
370 {
371     ARMCPU *cpu = env_archcpu(env);
372 
373     raw_write(env, ri, value);
374     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
375 }
376 
377 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
378 {
379     ARMCPU *cpu = env_archcpu(env);
380 
381     if (raw_read(env, ri) != value) {
382         /*
383          * Unlike real hardware the qemu TLB uses virtual addresses,
384          * not modified virtual addresses, so this causes a TLB flush.
385          */
386         tlb_flush(CPU(cpu));
387         raw_write(env, ri, value);
388     }
389 }
390 
391 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
392                              uint64_t value)
393 {
394     ARMCPU *cpu = env_archcpu(env);
395 
396     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
397         && !extended_addresses_enabled(env)) {
398         /*
399          * For VMSA (when not using the LPAE long descriptor page table
400          * format) this register includes the ASID, so do a TLB flush.
401          * For PMSA it is purely a process ID and no action is needed.
402          */
403         tlb_flush(CPU(cpu));
404     }
405     raw_write(env, ri, value);
406 }
407 
408 int alle1_tlbmask(CPUARMState *env)
409 {
410     /*
411      * Note that the 'ALL' scope must invalidate both stage 1 and
412      * stage 2 translations, whereas most other scopes only invalidate
413      * stage 1 translations.
414      *
415      * For AArch32 this is only used for TLBIALLNSNH and VTTBR
416      * writes, so only needs to apply to NS PL1&0, not S PL1&0.
417      */
418     return (ARMMMUIdxBit_E10_1 |
419             ARMMMUIdxBit_E10_1_PAN |
420             ARMMMUIdxBit_E10_0 |
421             ARMMMUIdxBit_Stage2 |
422             ARMMMUIdxBit_Stage2_S);
423 }
424 
425 static const ARMCPRegInfo cp_reginfo[] = {
426     /*
427      * Define the secure and non-secure FCSE identifier CP registers
428      * separately because there is no secure bank in V8 (no _EL3).  This allows
429      * the secure register to be properly reset and migrated. There is also no
430      * v8 EL1 version of the register so the non-secure instance stands alone.
431      */
432     { .name = "FCSEIDR",
433       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
434       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
435       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
436       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
437     { .name = "FCSEIDR_S",
438       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
439       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
440       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
441       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
442     /*
443      * Define the secure and non-secure context identifier CP registers
444      * separately because there is no secure bank in V8 (no _EL3).  This allows
445      * the secure register to be properly reset and migrated.  In the
446      * non-secure case, the 32-bit register will have reset and migration
447      * disabled during registration as it is handled by the 64-bit instance.
448      */
449     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
450       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
451       .access = PL1_RW, .accessfn = access_tvm_trvm,
452       .fgt = FGT_CONTEXTIDR_EL1,
453       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
454       .secure = ARM_CP_SECSTATE_NS,
455       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
456       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
457     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
458       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
459       .access = PL1_RW, .accessfn = access_tvm_trvm,
460       .secure = ARM_CP_SECSTATE_S,
461       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
462       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
463 };
464 
465 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
466     /*
467      * NB: Some of these registers exist in v8 but with more precise
468      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
469      */
470     /* MMU Domain access control / MPU write buffer control */
471     { .name = "DACR",
472       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
473       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
474       .writefn = dacr_write, .raw_writefn = raw_write,
475       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
476                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
477     /*
478      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
479      * For v6 and v5, these mappings are overly broad.
480      */
481     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
482       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
483     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
484       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
485     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
486       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
487     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
488       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
489     /* Cache maintenance ops; some of this space may be overridden later. */
490     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
491       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
492       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
493 };
494 
495 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
496     /*
497      * Not all pre-v6 cores implemented this WFI, so this is slightly
498      * over-broad.
499      */
500     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
501       .access = PL1_W, .type = ARM_CP_WFI },
502 };
503 
504 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
505     /*
506      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
507      * is UNPREDICTABLE; we choose to NOP as most implementations do).
508      */
509     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
510       .access = PL1_W, .type = ARM_CP_WFI },
511     /*
512      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
513      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
514      * OMAPCP will override this space.
515      */
516     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
517       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
518       .resetvalue = 0 },
519     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
520       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
521       .resetvalue = 0 },
522     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
523     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
524       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
525       .resetvalue = 0 },
526     /*
527      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
528      * implementing it as RAZ means the "debug architecture version" bits
529      * will read as a reserved value, which should cause Linux to not try
530      * to use the debug hardware.
531      */
532     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
533       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
534     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
535       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
536     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
537       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
538 };
539 
540 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
541                         uint64_t value)
542 {
543     uint32_t mask = 0;
544 
545     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
546     if (!arm_feature(env, ARM_FEATURE_V8)) {
547         /*
548          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
549          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
550          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
551          */
552         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
553             /* VFP coprocessor: cp10 & cp11 [23:20] */
554             mask |= R_CPACR_ASEDIS_MASK |
555                     R_CPACR_D32DIS_MASK |
556                     R_CPACR_CP11_MASK |
557                     R_CPACR_CP10_MASK;
558 
559             if (!arm_feature(env, ARM_FEATURE_NEON)) {
560                 /* ASEDIS [31] bit is RAO/WI */
561                 value |= R_CPACR_ASEDIS_MASK;
562             }
563 
564             /*
565              * VFPv3 and upwards with NEON implement 32 double precision
566              * registers (D0-D31).
567              */
568             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
569                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
570                 value |= R_CPACR_D32DIS_MASK;
571             }
572         }
573         value &= mask;
574     }
575 
576     /*
577      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
578      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
579      */
580     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
581         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
582         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
583         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
584     }
585 
586     env->cp15.cpacr_el1 = value;
587 }
588 
589 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
590 {
591     /*
592      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
593      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
594      */
595     uint64_t value = env->cp15.cpacr_el1;
596 
597     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
598         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
599         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
600     }
601     return value;
602 }
603 
604 
605 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
606 {
607     /*
608      * Call cpacr_write() so that we reset with the correct RAO bits set
609      * for our CPU features.
610      */
611     cpacr_write(env, ri, 0);
612 }
613 
614 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
615                                    bool isread)
616 {
617     if (arm_feature(env, ARM_FEATURE_V8)) {
618         /* Check if CPACR accesses are to be trapped to EL2 */
619         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
620             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
621             return CP_ACCESS_TRAP_EL2;
622         /* Check if CPACR accesses are to be trapped to EL3 */
623         } else if (arm_current_el(env) < 3 &&
624                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
625             return CP_ACCESS_TRAP_EL3;
626         }
627     }
628 
629     return CP_ACCESS_OK;
630 }
631 
632 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
633                                   bool isread)
634 {
635     /* Check if CPTR accesses are set to trap to EL3 */
636     if (arm_current_el(env) == 2 &&
637         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
638         return CP_ACCESS_TRAP_EL3;
639     }
640 
641     return CP_ACCESS_OK;
642 }
643 
644 static const ARMCPRegInfo v6_cp_reginfo[] = {
645     /* prefetch by MVA in v6, NOP in v7 */
646     { .name = "MVA_prefetch",
647       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
648       .access = PL1_W, .type = ARM_CP_NOP },
649     /*
650      * We need to break the TB after ISB to execute self-modifying code
651      * correctly and also to take any pending interrupts immediately.
652      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
653      */
654     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
655       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
656     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
657       .access = PL0_W, .type = ARM_CP_NOP },
658     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
659       .access = PL0_W, .type = ARM_CP_NOP },
660     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
661       .access = PL1_RW, .accessfn = access_tvm_trvm,
662       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
663                              offsetof(CPUARMState, cp15.ifar_ns) },
664       .resetvalue = 0, },
665     /*
666      * Watchpoint Fault Address Register : should actually only be present
667      * for 1136, 1176, 11MPCore.
668      */
669     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
670       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
671     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
672       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
673       .fgt = FGT_CPACR_EL1,
674       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
675       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
676       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
677 };
678 
679 typedef struct pm_event {
680     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
681     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
682     bool (*supported)(CPUARMState *);
683     /*
684      * Retrieve the current count of the underlying event. The programmed
685      * counters hold a difference from the return value from this function
686      */
687     uint64_t (*get_count)(CPUARMState *);
688     /*
689      * Return how many nanoseconds it will take (at a minimum) for count events
690      * to occur. A negative value indicates the counter will never overflow, or
691      * that the counter has otherwise arranged for the overflow bit to be set
692      * and the PMU interrupt to be raised on overflow.
693      */
694     int64_t (*ns_per_count)(uint64_t);
695 } pm_event;
696 
697 static bool event_always_supported(CPUARMState *env)
698 {
699     return true;
700 }
701 
702 static uint64_t swinc_get_count(CPUARMState *env)
703 {
704     /*
705      * SW_INCR events are written directly to the pmevcntr's by writes to
706      * PMSWINC, so there is no underlying count maintained by the PMU itself
707      */
708     return 0;
709 }
710 
711 static int64_t swinc_ns_per(uint64_t ignored)
712 {
713     return -1;
714 }
715 
716 /*
717  * Return the underlying cycle count for the PMU cycle counters. If we're in
718  * usermode, simply return 0.
719  */
720 static uint64_t cycles_get_count(CPUARMState *env)
721 {
722 #ifndef CONFIG_USER_ONLY
723     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
724                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
725 #else
726     return cpu_get_host_ticks();
727 #endif
728 }
729 
730 #ifndef CONFIG_USER_ONLY
731 static int64_t cycles_ns_per(uint64_t cycles)
732 {
733     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
734 }
735 
736 static bool instructions_supported(CPUARMState *env)
737 {
738     /* Precise instruction counting */
739     return icount_enabled() == ICOUNT_PRECISE;
740 }
741 
742 static uint64_t instructions_get_count(CPUARMState *env)
743 {
744     assert(icount_enabled() == ICOUNT_PRECISE);
745     return (uint64_t)icount_get_raw();
746 }
747 
748 static int64_t instructions_ns_per(uint64_t icount)
749 {
750     assert(icount_enabled() == ICOUNT_PRECISE);
751     return icount_to_ns((int64_t)icount);
752 }
753 #endif
754 
755 static bool pmuv3p1_events_supported(CPUARMState *env)
756 {
757     /* For events which are supported in any v8.1 PMU */
758     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
759 }
760 
761 static bool pmuv3p4_events_supported(CPUARMState *env)
762 {
763     /* For events which are supported in any v8.1 PMU */
764     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
765 }
766 
767 static uint64_t zero_event_get_count(CPUARMState *env)
768 {
769     /* For events which on QEMU never fire, so their count is always zero */
770     return 0;
771 }
772 
773 static int64_t zero_event_ns_per(uint64_t cycles)
774 {
775     /* An event which never fires can never overflow */
776     return -1;
777 }
778 
779 static const pm_event pm_events[] = {
780     { .number = 0x000, /* SW_INCR */
781       .supported = event_always_supported,
782       .get_count = swinc_get_count,
783       .ns_per_count = swinc_ns_per,
784     },
785 #ifndef CONFIG_USER_ONLY
786     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
787       .supported = instructions_supported,
788       .get_count = instructions_get_count,
789       .ns_per_count = instructions_ns_per,
790     },
791     { .number = 0x011, /* CPU_CYCLES, Cycle */
792       .supported = event_always_supported,
793       .get_count = cycles_get_count,
794       .ns_per_count = cycles_ns_per,
795     },
796 #endif
797     { .number = 0x023, /* STALL_FRONTEND */
798       .supported = pmuv3p1_events_supported,
799       .get_count = zero_event_get_count,
800       .ns_per_count = zero_event_ns_per,
801     },
802     { .number = 0x024, /* STALL_BACKEND */
803       .supported = pmuv3p1_events_supported,
804       .get_count = zero_event_get_count,
805       .ns_per_count = zero_event_ns_per,
806     },
807     { .number = 0x03c, /* STALL */
808       .supported = pmuv3p4_events_supported,
809       .get_count = zero_event_get_count,
810       .ns_per_count = zero_event_ns_per,
811     },
812 };
813 
814 /*
815  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
816  * events (i.e. the statistical profiling extension), this implementation
817  * should first be updated to something sparse instead of the current
818  * supported_event_map[] array.
819  */
820 #define MAX_EVENT_ID 0x3c
821 #define UNSUPPORTED_EVENT UINT16_MAX
822 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
823 
824 /*
825  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
826  * of ARM event numbers to indices in our pm_events array.
827  *
828  * Note: Events in the 0x40XX range are not currently supported.
829  */
830 void pmu_init(ARMCPU *cpu)
831 {
832     unsigned int i;
833 
834     /*
835      * Empty supported_event_map and cpu->pmceid[01] before adding supported
836      * events to them
837      */
838     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
839         supported_event_map[i] = UNSUPPORTED_EVENT;
840     }
841     cpu->pmceid0 = 0;
842     cpu->pmceid1 = 0;
843 
844     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
845         const pm_event *cnt = &pm_events[i];
846         assert(cnt->number <= MAX_EVENT_ID);
847         /* We do not currently support events in the 0x40xx range */
848         assert(cnt->number <= 0x3f);
849 
850         if (cnt->supported(&cpu->env)) {
851             supported_event_map[cnt->number] = i;
852             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
853             if (cnt->number & 0x20) {
854                 cpu->pmceid1 |= event_mask;
855             } else {
856                 cpu->pmceid0 |= event_mask;
857             }
858         }
859     }
860 }
861 
862 /*
863  * Check at runtime whether a PMU event is supported for the current machine
864  */
865 static bool event_supported(uint16_t number)
866 {
867     if (number > MAX_EVENT_ID) {
868         return false;
869     }
870     return supported_event_map[number] != UNSUPPORTED_EVENT;
871 }
872 
873 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
874                                    bool isread)
875 {
876     /*
877      * Performance monitor registers user accessibility is controlled
878      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
879      * trapping to EL2 or EL3 for other accesses.
880      */
881     int el = arm_current_el(env);
882     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
883 
884     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
885         return CP_ACCESS_TRAP_EL1;
886     }
887     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
888         return CP_ACCESS_TRAP_EL2;
889     }
890     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
891         return CP_ACCESS_TRAP_EL3;
892     }
893 
894     return CP_ACCESS_OK;
895 }
896 
897 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
898                                            const ARMCPRegInfo *ri,
899                                            bool isread)
900 {
901     /* ER: event counter read trap control */
902     if (arm_feature(env, ARM_FEATURE_V8)
903         && arm_current_el(env) == 0
904         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
905         && isread) {
906         return CP_ACCESS_OK;
907     }
908 
909     return pmreg_access(env, ri, isread);
910 }
911 
912 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
913                                          const ARMCPRegInfo *ri,
914                                          bool isread)
915 {
916     /* SW: software increment write trap control */
917     if (arm_feature(env, ARM_FEATURE_V8)
918         && arm_current_el(env) == 0
919         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
920         && !isread) {
921         return CP_ACCESS_OK;
922     }
923 
924     return pmreg_access(env, ri, isread);
925 }
926 
927 static CPAccessResult pmreg_access_selr(CPUARMState *env,
928                                         const ARMCPRegInfo *ri,
929                                         bool isread)
930 {
931     /* ER: event counter read trap control */
932     if (arm_feature(env, ARM_FEATURE_V8)
933         && arm_current_el(env) == 0
934         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
935         return CP_ACCESS_OK;
936     }
937 
938     return pmreg_access(env, ri, isread);
939 }
940 
941 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
942                                          const ARMCPRegInfo *ri,
943                                          bool isread)
944 {
945     /* CR: cycle counter read trap control */
946     if (arm_feature(env, ARM_FEATURE_V8)
947         && arm_current_el(env) == 0
948         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
949         && isread) {
950         return CP_ACCESS_OK;
951     }
952 
953     return pmreg_access(env, ri, isread);
954 }
955 
956 /*
957  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
958  * We use these to decide whether we need to wrap a write to MDCR_EL2
959  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
960  */
961 #define MDCR_EL2_PMU_ENABLE_BITS \
962     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
963 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
964 
965 /*
966  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
967  * the current EL, security state, and register configuration.
968  */
969 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
970 {
971     uint64_t filter;
972     bool e, p, u, nsk, nsu, nsh, m;
973     bool enabled, prohibited = false, filtered;
974     bool secure = arm_is_secure(env);
975     int el = arm_current_el(env);
976     uint64_t mdcr_el2;
977     uint8_t hpmn;
978 
979     /*
980      * We might be called for M-profile cores where MDCR_EL2 doesn't
981      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
982      * must be before we read that value.
983      */
984     if (!arm_feature(env, ARM_FEATURE_PMU)) {
985         return false;
986     }
987 
988     mdcr_el2 = arm_mdcr_el2_eff(env);
989     hpmn = mdcr_el2 & MDCR_HPMN;
990 
991     if (!arm_feature(env, ARM_FEATURE_EL2) ||
992             (counter < hpmn || counter == 31)) {
993         e = env->cp15.c9_pmcr & PMCRE;
994     } else {
995         e = mdcr_el2 & MDCR_HPME;
996     }
997     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
998 
999     /* Is event counting prohibited? */
1000     if (el == 2 && (counter < hpmn || counter == 31)) {
1001         prohibited = mdcr_el2 & MDCR_HPMD;
1002     }
1003     if (secure) {
1004         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1005     }
1006 
1007     if (counter == 31) {
1008         /*
1009          * The cycle counter defaults to running. PMCR.DP says "disable
1010          * the cycle counter when event counting is prohibited".
1011          * Some MDCR bits disable the cycle counter specifically.
1012          */
1013         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1014         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1015             if (secure) {
1016                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1017             }
1018             if (el == 2) {
1019                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1020             }
1021         }
1022     }
1023 
1024     if (counter == 31) {
1025         filter = env->cp15.pmccfiltr_el0;
1026     } else {
1027         filter = env->cp15.c14_pmevtyper[counter];
1028     }
1029 
1030     p   = filter & PMXEVTYPER_P;
1031     u   = filter & PMXEVTYPER_U;
1032     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1033     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1034     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1035     m   = arm_el_is_aa64(env, 1) &&
1036               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1037 
1038     if (el == 0) {
1039         filtered = secure ? u : u != nsu;
1040     } else if (el == 1) {
1041         filtered = secure ? p : p != nsk;
1042     } else if (el == 2) {
1043         filtered = !nsh;
1044     } else { /* EL3 */
1045         filtered = m != p;
1046     }
1047 
1048     if (counter != 31) {
1049         /*
1050          * If not checking PMCCNTR, ensure the counter is setup to an event we
1051          * support
1052          */
1053         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1054         if (!event_supported(event)) {
1055             return false;
1056         }
1057     }
1058 
1059     return enabled && !prohibited && !filtered;
1060 }
1061 
1062 static void pmu_update_irq(CPUARMState *env)
1063 {
1064     ARMCPU *cpu = env_archcpu(env);
1065     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1066             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1067 }
1068 
1069 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1070 {
1071     /*
1072      * Return true if the clock divider is enabled and the cycle counter
1073      * is supposed to tick only once every 64 clock cycles. This is
1074      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1075      * (64-bit) cycle counter PMCR.D has no effect.
1076      */
1077     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1078 }
1079 
1080 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1081 {
1082     /* Return true if the specified event counter is configured to be 64 bit */
1083 
1084     /* This isn't intended to be used with the cycle counter */
1085     assert(counter < 31);
1086 
1087     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1088         return false;
1089     }
1090 
1091     if (arm_feature(env, ARM_FEATURE_EL2)) {
1092         /*
1093          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1094          * current security state, so we don't use arm_mdcr_el2_eff() here.
1095          */
1096         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1097         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1098 
1099         if (counter >= hpmn) {
1100             return hlp;
1101         }
1102     }
1103     return env->cp15.c9_pmcr & PMCRLP;
1104 }
1105 
1106 /*
1107  * Ensure c15_ccnt is the guest-visible count so that operations such as
1108  * enabling/disabling the counter or filtering, modifying the count itself,
1109  * etc. can be done logically. This is essentially a no-op if the counter is
1110  * not enabled at the time of the call.
1111  */
1112 static void pmccntr_op_start(CPUARMState *env)
1113 {
1114     uint64_t cycles = cycles_get_count(env);
1115 
1116     if (pmu_counter_enabled(env, 31)) {
1117         uint64_t eff_cycles = cycles;
1118         if (pmccntr_clockdiv_enabled(env)) {
1119             eff_cycles /= 64;
1120         }
1121 
1122         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1123 
1124         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1125                                  1ull << 63 : 1ull << 31;
1126         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1127             env->cp15.c9_pmovsr |= (1ULL << 31);
1128             pmu_update_irq(env);
1129         }
1130 
1131         env->cp15.c15_ccnt = new_pmccntr;
1132     }
1133     env->cp15.c15_ccnt_delta = cycles;
1134 }
1135 
1136 /*
1137  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1138  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1139  * pmccntr_op_start.
1140  */
1141 static void pmccntr_op_finish(CPUARMState *env)
1142 {
1143     if (pmu_counter_enabled(env, 31)) {
1144 #ifndef CONFIG_USER_ONLY
1145         /* Calculate when the counter will next overflow */
1146         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1147         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1148             remaining_cycles = (uint32_t)remaining_cycles;
1149         }
1150         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1151 
1152         if (overflow_in > 0) {
1153             int64_t overflow_at;
1154 
1155             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1156                                  overflow_in, &overflow_at)) {
1157                 ARMCPU *cpu = env_archcpu(env);
1158                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1159             }
1160         }
1161 #endif
1162 
1163         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1164         if (pmccntr_clockdiv_enabled(env)) {
1165             prev_cycles /= 64;
1166         }
1167         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1168     }
1169 }
1170 
1171 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1172 {
1173 
1174     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1175     uint64_t count = 0;
1176     if (event_supported(event)) {
1177         uint16_t event_idx = supported_event_map[event];
1178         count = pm_events[event_idx].get_count(env);
1179     }
1180 
1181     if (pmu_counter_enabled(env, counter)) {
1182         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1183         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1184             1ULL << 63 : 1ULL << 31;
1185 
1186         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1187             env->cp15.c9_pmovsr |= (1 << counter);
1188             pmu_update_irq(env);
1189         }
1190         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1191     }
1192     env->cp15.c14_pmevcntr_delta[counter] = count;
1193 }
1194 
1195 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1196 {
1197     if (pmu_counter_enabled(env, counter)) {
1198 #ifndef CONFIG_USER_ONLY
1199         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1200         uint16_t event_idx = supported_event_map[event];
1201         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1202         int64_t overflow_in;
1203 
1204         if (!pmevcntr_is_64_bit(env, counter)) {
1205             delta = (uint32_t)delta;
1206         }
1207         overflow_in = pm_events[event_idx].ns_per_count(delta);
1208 
1209         if (overflow_in > 0) {
1210             int64_t overflow_at;
1211 
1212             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1213                                  overflow_in, &overflow_at)) {
1214                 ARMCPU *cpu = env_archcpu(env);
1215                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1216             }
1217         }
1218 #endif
1219 
1220         env->cp15.c14_pmevcntr_delta[counter] -=
1221             env->cp15.c14_pmevcntr[counter];
1222     }
1223 }
1224 
1225 void pmu_op_start(CPUARMState *env)
1226 {
1227     unsigned int i;
1228     pmccntr_op_start(env);
1229     for (i = 0; i < pmu_num_counters(env); i++) {
1230         pmevcntr_op_start(env, i);
1231     }
1232 }
1233 
1234 void pmu_op_finish(CPUARMState *env)
1235 {
1236     unsigned int i;
1237     pmccntr_op_finish(env);
1238     for (i = 0; i < pmu_num_counters(env); i++) {
1239         pmevcntr_op_finish(env, i);
1240     }
1241 }
1242 
1243 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1244 {
1245     pmu_op_start(&cpu->env);
1246 }
1247 
1248 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1249 {
1250     pmu_op_finish(&cpu->env);
1251 }
1252 
1253 void arm_pmu_timer_cb(void *opaque)
1254 {
1255     ARMCPU *cpu = opaque;
1256 
1257     /*
1258      * Update all the counter values based on the current underlying counts,
1259      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1260      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1261      * counter may expire.
1262      */
1263     pmu_op_start(&cpu->env);
1264     pmu_op_finish(&cpu->env);
1265 }
1266 
1267 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1268                        uint64_t value)
1269 {
1270     pmu_op_start(env);
1271 
1272     if (value & PMCRC) {
1273         /* The counter has been reset */
1274         env->cp15.c15_ccnt = 0;
1275     }
1276 
1277     if (value & PMCRP) {
1278         unsigned int i;
1279         for (i = 0; i < pmu_num_counters(env); i++) {
1280             env->cp15.c14_pmevcntr[i] = 0;
1281         }
1282     }
1283 
1284     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1285     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1286 
1287     pmu_op_finish(env);
1288 }
1289 
1290 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1291 {
1292     uint64_t pmcr = env->cp15.c9_pmcr;
1293 
1294     /*
1295      * If EL2 is implemented and enabled for the current security state, reads
1296      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1297      */
1298     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1299         pmcr &= ~PMCRN_MASK;
1300         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1301     }
1302 
1303     return pmcr;
1304 }
1305 
1306 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1307                           uint64_t value)
1308 {
1309     unsigned int i;
1310     uint64_t overflow_mask, new_pmswinc;
1311 
1312     for (i = 0; i < pmu_num_counters(env); i++) {
1313         /* Increment a counter's count iff: */
1314         if ((value & (1 << i)) && /* counter's bit is set */
1315                 /* counter is enabled and not filtered */
1316                 pmu_counter_enabled(env, i) &&
1317                 /* counter is SW_INCR */
1318                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1319             pmevcntr_op_start(env, i);
1320 
1321             /*
1322              * Detect if this write causes an overflow since we can't predict
1323              * PMSWINC overflows like we can for other events
1324              */
1325             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1326 
1327             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1328                 1ULL << 63 : 1ULL << 31;
1329 
1330             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1331                 env->cp15.c9_pmovsr |= (1 << i);
1332                 pmu_update_irq(env);
1333             }
1334 
1335             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1336 
1337             pmevcntr_op_finish(env, i);
1338         }
1339     }
1340 }
1341 
1342 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1343 {
1344     uint64_t ret;
1345     pmccntr_op_start(env);
1346     ret = env->cp15.c15_ccnt;
1347     pmccntr_op_finish(env);
1348     return ret;
1349 }
1350 
1351 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1352                          uint64_t value)
1353 {
1354     /*
1355      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1356      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1357      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1358      * accessed.
1359      */
1360     env->cp15.c9_pmselr = value & 0x1f;
1361 }
1362 
1363 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1364                         uint64_t value)
1365 {
1366     pmccntr_op_start(env);
1367     env->cp15.c15_ccnt = value;
1368     pmccntr_op_finish(env);
1369 }
1370 
1371 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1372                             uint64_t value)
1373 {
1374     uint64_t cur_val = pmccntr_read(env, NULL);
1375 
1376     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1377 }
1378 
1379 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1380                             uint64_t value)
1381 {
1382     pmccntr_op_start(env);
1383     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1384     pmccntr_op_finish(env);
1385 }
1386 
1387 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1388                             uint64_t value)
1389 {
1390     pmccntr_op_start(env);
1391     /* M is not accessible from AArch32 */
1392     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1393         (value & PMCCFILTR);
1394     pmccntr_op_finish(env);
1395 }
1396 
1397 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1398 {
1399     /* M is not visible in AArch32 */
1400     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1401 }
1402 
1403 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1404                             uint64_t value)
1405 {
1406     pmu_op_start(env);
1407     value &= pmu_counter_mask(env);
1408     env->cp15.c9_pmcnten |= value;
1409     pmu_op_finish(env);
1410 }
1411 
1412 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1413                              uint64_t value)
1414 {
1415     pmu_op_start(env);
1416     value &= pmu_counter_mask(env);
1417     env->cp15.c9_pmcnten &= ~value;
1418     pmu_op_finish(env);
1419 }
1420 
1421 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1422                          uint64_t value)
1423 {
1424     value &= pmu_counter_mask(env);
1425     env->cp15.c9_pmovsr &= ~value;
1426     pmu_update_irq(env);
1427 }
1428 
1429 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1430                          uint64_t value)
1431 {
1432     value &= pmu_counter_mask(env);
1433     env->cp15.c9_pmovsr |= value;
1434     pmu_update_irq(env);
1435 }
1436 
1437 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1438                              uint64_t value, const uint8_t counter)
1439 {
1440     if (counter == 31) {
1441         pmccfiltr_write(env, ri, value);
1442     } else if (counter < pmu_num_counters(env)) {
1443         pmevcntr_op_start(env, counter);
1444 
1445         /*
1446          * If this counter's event type is changing, store the current
1447          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1448          * pmevcntr_op_finish has the correct baseline when it converts back to
1449          * a delta.
1450          */
1451         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1452             PMXEVTYPER_EVTCOUNT;
1453         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1454         if (old_event != new_event) {
1455             uint64_t count = 0;
1456             if (event_supported(new_event)) {
1457                 uint16_t event_idx = supported_event_map[new_event];
1458                 count = pm_events[event_idx].get_count(env);
1459             }
1460             env->cp15.c14_pmevcntr_delta[counter] = count;
1461         }
1462 
1463         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1464         pmevcntr_op_finish(env, counter);
1465     }
1466     /*
1467      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1468      * PMSELR value is equal to or greater than the number of implemented
1469      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1470      */
1471 }
1472 
1473 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1474                                const uint8_t counter)
1475 {
1476     if (counter == 31) {
1477         return env->cp15.pmccfiltr_el0;
1478     } else if (counter < pmu_num_counters(env)) {
1479         return env->cp15.c14_pmevtyper[counter];
1480     } else {
1481       /*
1482        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1483        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1484        */
1485         return 0;
1486     }
1487 }
1488 
1489 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1490                               uint64_t value)
1491 {
1492     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1493     pmevtyper_write(env, ri, value, counter);
1494 }
1495 
1496 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1497                                uint64_t value)
1498 {
1499     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1500     env->cp15.c14_pmevtyper[counter] = value;
1501 
1502     /*
1503      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1504      * pmu_op_finish calls when loading saved state for a migration. Because
1505      * we're potentially updating the type of event here, the value written to
1506      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1507      * different counter type. Therefore, we need to set this value to the
1508      * current count for the counter type we're writing so that pmu_op_finish
1509      * has the correct count for its calculation.
1510      */
1511     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1512     if (event_supported(event)) {
1513         uint16_t event_idx = supported_event_map[event];
1514         env->cp15.c14_pmevcntr_delta[counter] =
1515             pm_events[event_idx].get_count(env);
1516     }
1517 }
1518 
1519 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1520 {
1521     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1522     return pmevtyper_read(env, ri, counter);
1523 }
1524 
1525 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1526                              uint64_t value)
1527 {
1528     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1529 }
1530 
1531 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1532 {
1533     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1534 }
1535 
1536 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1537                              uint64_t value, uint8_t counter)
1538 {
1539     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1540         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1541         value &= MAKE_64BIT_MASK(0, 32);
1542     }
1543     if (counter < pmu_num_counters(env)) {
1544         pmevcntr_op_start(env, counter);
1545         env->cp15.c14_pmevcntr[counter] = value;
1546         pmevcntr_op_finish(env, counter);
1547     }
1548     /*
1549      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1550      * are CONSTRAINED UNPREDICTABLE.
1551      */
1552 }
1553 
1554 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1555                               uint8_t counter)
1556 {
1557     if (counter < pmu_num_counters(env)) {
1558         uint64_t ret;
1559         pmevcntr_op_start(env, counter);
1560         ret = env->cp15.c14_pmevcntr[counter];
1561         pmevcntr_op_finish(env, counter);
1562         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1563             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1564             ret &= MAKE_64BIT_MASK(0, 32);
1565         }
1566         return ret;
1567     } else {
1568       /*
1569        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1570        * are CONSTRAINED UNPREDICTABLE.
1571        */
1572         return 0;
1573     }
1574 }
1575 
1576 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1577                              uint64_t value)
1578 {
1579     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1580     pmevcntr_write(env, ri, value, counter);
1581 }
1582 
1583 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1584 {
1585     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1586     return pmevcntr_read(env, ri, counter);
1587 }
1588 
1589 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1590                              uint64_t value)
1591 {
1592     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1593     assert(counter < pmu_num_counters(env));
1594     env->cp15.c14_pmevcntr[counter] = value;
1595     pmevcntr_write(env, ri, value, counter);
1596 }
1597 
1598 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1599 {
1600     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1601     assert(counter < pmu_num_counters(env));
1602     return env->cp15.c14_pmevcntr[counter];
1603 }
1604 
1605 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1606                              uint64_t value)
1607 {
1608     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1609 }
1610 
1611 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1612 {
1613     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1614 }
1615 
1616 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1617                             uint64_t value)
1618 {
1619     if (arm_feature(env, ARM_FEATURE_V8)) {
1620         env->cp15.c9_pmuserenr = value & 0xf;
1621     } else {
1622         env->cp15.c9_pmuserenr = value & 1;
1623     }
1624 }
1625 
1626 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627                              uint64_t value)
1628 {
1629     /* We have no event counters so only the C bit can be changed */
1630     value &= pmu_counter_mask(env);
1631     env->cp15.c9_pminten |= value;
1632     pmu_update_irq(env);
1633 }
1634 
1635 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                              uint64_t value)
1637 {
1638     value &= pmu_counter_mask(env);
1639     env->cp15.c9_pminten &= ~value;
1640     pmu_update_irq(env);
1641 }
1642 
1643 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1644                        uint64_t value)
1645 {
1646     /*
1647      * Note that even though the AArch64 view of this register has bits
1648      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1649      * architectural requirements for bits which are RES0 only in some
1650      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1651      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1652      */
1653     raw_write(env, ri, value & ~0x1FULL);
1654 }
1655 
1656 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1657 {
1658     /* Begin with base v8.0 state.  */
1659     uint64_t valid_mask = 0x3fff;
1660     ARMCPU *cpu = env_archcpu(env);
1661     uint64_t changed;
1662 
1663     /*
1664      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1665      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1666      * Instead, choose the format based on the mode of EL3.
1667      */
1668     if (arm_el_is_aa64(env, 3)) {
1669         value |= SCR_FW | SCR_AW;      /* RES1 */
1670         valid_mask &= ~SCR_NET;        /* RES0 */
1671 
1672         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1673             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1674             value |= SCR_RW;           /* RAO/WI */
1675         }
1676         if (cpu_isar_feature(aa64_ras, cpu)) {
1677             valid_mask |= SCR_TERR;
1678         }
1679         if (cpu_isar_feature(aa64_lor, cpu)) {
1680             valid_mask |= SCR_TLOR;
1681         }
1682         if (cpu_isar_feature(aa64_pauth, cpu)) {
1683             valid_mask |= SCR_API | SCR_APK;
1684         }
1685         if (cpu_isar_feature(aa64_sel2, cpu)) {
1686             valid_mask |= SCR_EEL2;
1687         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1688             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1689             value |= SCR_NS;
1690         }
1691         if (cpu_isar_feature(aa64_mte, cpu)) {
1692             valid_mask |= SCR_ATA;
1693         }
1694         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1695             valid_mask |= SCR_ENSCXT;
1696         }
1697         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1698             valid_mask |= SCR_EASE | SCR_NMEA;
1699         }
1700         if (cpu_isar_feature(aa64_sme, cpu)) {
1701             valid_mask |= SCR_ENTP2;
1702         }
1703         if (cpu_isar_feature(aa64_hcx, cpu)) {
1704             valid_mask |= SCR_HXEN;
1705         }
1706         if (cpu_isar_feature(aa64_fgt, cpu)) {
1707             valid_mask |= SCR_FGTEN;
1708         }
1709         if (cpu_isar_feature(aa64_rme, cpu)) {
1710             valid_mask |= SCR_NSE | SCR_GPF;
1711         }
1712         if (cpu_isar_feature(aa64_ecv, cpu)) {
1713             valid_mask |= SCR_ECVEN;
1714         }
1715     } else {
1716         valid_mask &= ~(SCR_RW | SCR_ST);
1717         if (cpu_isar_feature(aa32_ras, cpu)) {
1718             valid_mask |= SCR_TERR;
1719         }
1720     }
1721 
1722     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1723         valid_mask &= ~SCR_HCE;
1724 
1725         /*
1726          * On ARMv7, SMD (or SCD as it is called in v7) is only
1727          * supported if EL2 exists. The bit is UNK/SBZP when
1728          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1729          * when EL2 is unavailable.
1730          * On ARMv8, this bit is always available.
1731          */
1732         if (arm_feature(env, ARM_FEATURE_V7) &&
1733             !arm_feature(env, ARM_FEATURE_V8)) {
1734             valid_mask &= ~SCR_SMD;
1735         }
1736     }
1737 
1738     /* Clear all-context RES0 bits.  */
1739     value &= valid_mask;
1740     changed = env->cp15.scr_el3 ^ value;
1741     env->cp15.scr_el3 = value;
1742 
1743     /*
1744      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1745      * we must invalidate all TLBs below EL3.
1746      */
1747     if (changed & (SCR_NS | SCR_NSE)) {
1748         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1749                                            ARMMMUIdxBit_E20_0 |
1750                                            ARMMMUIdxBit_E10_1 |
1751                                            ARMMMUIdxBit_E20_2 |
1752                                            ARMMMUIdxBit_E10_1_PAN |
1753                                            ARMMMUIdxBit_E20_2_PAN |
1754                                            ARMMMUIdxBit_E2));
1755     }
1756 }
1757 
1758 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1759 {
1760     /*
1761      * scr_write will set the RES1 bits on an AArch64-only CPU.
1762      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1763      */
1764     scr_write(env, ri, 0);
1765 }
1766 
1767 static CPAccessResult access_tid4(CPUARMState *env,
1768                                   const ARMCPRegInfo *ri,
1769                                   bool isread)
1770 {
1771     if (arm_current_el(env) == 1 &&
1772         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1773         return CP_ACCESS_TRAP_EL2;
1774     }
1775 
1776     return CP_ACCESS_OK;
1777 }
1778 
1779 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1780 {
1781     ARMCPU *cpu = env_archcpu(env);
1782 
1783     /*
1784      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1785      * bank
1786      */
1787     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1788                                         ri->secure & ARM_CP_SECSTATE_S);
1789 
1790     return cpu->ccsidr[index];
1791 }
1792 
1793 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794                          uint64_t value)
1795 {
1796     raw_write(env, ri, value & 0xf);
1797 }
1798 
1799 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1800 {
1801     CPUState *cs = env_cpu(env);
1802     bool el1 = arm_current_el(env) == 1;
1803     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1804     uint64_t ret = 0;
1805 
1806     if (hcr_el2 & HCR_IMO) {
1807         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1808             ret |= CPSR_I;
1809         }
1810         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1811             ret |= ISR_IS;
1812             ret |= CPSR_I;
1813         }
1814     } else {
1815         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1816             ret |= CPSR_I;
1817         }
1818 
1819         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1820             ret |= ISR_IS;
1821             ret |= CPSR_I;
1822         }
1823     }
1824 
1825     if (hcr_el2 & HCR_FMO) {
1826         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1827             ret |= CPSR_F;
1828         }
1829         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1830             ret |= ISR_FS;
1831             ret |= CPSR_F;
1832         }
1833     } else {
1834         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1835             ret |= CPSR_F;
1836         }
1837     }
1838 
1839     if (hcr_el2 & HCR_AMO) {
1840         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1841             ret |= CPSR_A;
1842         }
1843     }
1844 
1845     return ret;
1846 }
1847 
1848 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1849                                        bool isread)
1850 {
1851     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1852         return CP_ACCESS_TRAP_EL2;
1853     }
1854 
1855     return CP_ACCESS_OK;
1856 }
1857 
1858 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1859                                        bool isread)
1860 {
1861     if (arm_feature(env, ARM_FEATURE_V8)) {
1862         return access_aa64_tid1(env, ri, isread);
1863     }
1864 
1865     return CP_ACCESS_OK;
1866 }
1867 
1868 static const ARMCPRegInfo v7_cp_reginfo[] = {
1869     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1870     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1871       .access = PL1_W, .type = ARM_CP_NOP },
1872     /*
1873      * Performance monitors are implementation defined in v7,
1874      * but with an ARM recommended set of registers, which we
1875      * follow.
1876      *
1877      * Performance registers fall into three categories:
1878      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1879      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1880      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1881      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1882      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1883      */
1884     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1885       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1886       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1887       .writefn = pmcntenset_write,
1888       .accessfn = pmreg_access,
1889       .fgt = FGT_PMCNTEN,
1890       .raw_writefn = raw_write },
1891     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1892       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1893       .access = PL0_RW, .accessfn = pmreg_access,
1894       .fgt = FGT_PMCNTEN,
1895       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1896       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1897     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1898       .access = PL0_RW,
1899       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1900       .accessfn = pmreg_access,
1901       .fgt = FGT_PMCNTEN,
1902       .writefn = pmcntenclr_write,
1903       .type = ARM_CP_ALIAS | ARM_CP_IO },
1904     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1905       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1906       .access = PL0_RW, .accessfn = pmreg_access,
1907       .fgt = FGT_PMCNTEN,
1908       .type = ARM_CP_ALIAS | ARM_CP_IO,
1909       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1910       .writefn = pmcntenclr_write },
1911     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1912       .access = PL0_RW, .type = ARM_CP_IO,
1913       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1914       .accessfn = pmreg_access,
1915       .fgt = FGT_PMOVS,
1916       .writefn = pmovsr_write,
1917       .raw_writefn = raw_write },
1918     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1919       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1920       .access = PL0_RW, .accessfn = pmreg_access,
1921       .fgt = FGT_PMOVS,
1922       .type = ARM_CP_ALIAS | ARM_CP_IO,
1923       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1924       .writefn = pmovsr_write,
1925       .raw_writefn = raw_write },
1926     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1927       .access = PL0_W, .accessfn = pmreg_access_swinc,
1928       .fgt = FGT_PMSWINC_EL0,
1929       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1930       .writefn = pmswinc_write },
1931     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1932       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1933       .access = PL0_W, .accessfn = pmreg_access_swinc,
1934       .fgt = FGT_PMSWINC_EL0,
1935       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1936       .writefn = pmswinc_write },
1937     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1938       .access = PL0_RW, .type = ARM_CP_ALIAS,
1939       .fgt = FGT_PMSELR_EL0,
1940       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1941       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1942       .raw_writefn = raw_write},
1943     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1944       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1945       .access = PL0_RW, .accessfn = pmreg_access_selr,
1946       .fgt = FGT_PMSELR_EL0,
1947       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1948       .writefn = pmselr_write, .raw_writefn = raw_write, },
1949     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1950       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1951       .fgt = FGT_PMCCNTR_EL0,
1952       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1953       .accessfn = pmreg_access_ccntr },
1954     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1955       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1956       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1957       .fgt = FGT_PMCCNTR_EL0,
1958       .type = ARM_CP_IO,
1959       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1960       .readfn = pmccntr_read, .writefn = pmccntr_write,
1961       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1962     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1963       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1964       .access = PL0_RW, .accessfn = pmreg_access,
1965       .fgt = FGT_PMCCFILTR_EL0,
1966       .type = ARM_CP_ALIAS | ARM_CP_IO,
1967       .resetvalue = 0, },
1968     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1969       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1970       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1971       .access = PL0_RW, .accessfn = pmreg_access,
1972       .fgt = FGT_PMCCFILTR_EL0,
1973       .type = ARM_CP_IO,
1974       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1975       .resetvalue = 0, },
1976     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1977       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1978       .accessfn = pmreg_access,
1979       .fgt = FGT_PMEVTYPERN_EL0,
1980       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1981     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1982       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1983       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1984       .accessfn = pmreg_access,
1985       .fgt = FGT_PMEVTYPERN_EL0,
1986       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1987     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1988       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1989       .accessfn = pmreg_access_xevcntr,
1990       .fgt = FGT_PMEVCNTRN_EL0,
1991       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1992     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1993       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1994       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1995       .accessfn = pmreg_access_xevcntr,
1996       .fgt = FGT_PMEVCNTRN_EL0,
1997       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1998     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1999       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2000       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2001       .resetvalue = 0,
2002       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2003     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2004       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2005       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2006       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2007       .resetvalue = 0,
2008       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2009     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2010       .access = PL1_RW, .accessfn = access_tpm,
2011       .fgt = FGT_PMINTEN,
2012       .type = ARM_CP_ALIAS | ARM_CP_IO,
2013       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2014       .resetvalue = 0,
2015       .writefn = pmintenset_write, .raw_writefn = raw_write },
2016     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2017       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2018       .access = PL1_RW, .accessfn = access_tpm,
2019       .fgt = FGT_PMINTEN,
2020       .type = ARM_CP_IO,
2021       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2022       .writefn = pmintenset_write, .raw_writefn = raw_write,
2023       .resetvalue = 0x0 },
2024     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2025       .access = PL1_RW, .accessfn = access_tpm,
2026       .fgt = FGT_PMINTEN,
2027       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2028       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2029       .writefn = pmintenclr_write, },
2030     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2031       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2032       .access = PL1_RW, .accessfn = access_tpm,
2033       .fgt = FGT_PMINTEN,
2034       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2035       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2036       .writefn = pmintenclr_write },
2037     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2038       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2039       .access = PL1_R,
2040       .accessfn = access_tid4,
2041       .fgt = FGT_CCSIDR_EL1,
2042       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2043     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2044       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2045       .access = PL1_RW,
2046       .accessfn = access_tid4,
2047       .fgt = FGT_CSSELR_EL1,
2048       .writefn = csselr_write, .resetvalue = 0,
2049       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2050                              offsetof(CPUARMState, cp15.csselr_ns) } },
2051     /*
2052      * Auxiliary ID register: this actually has an IMPDEF value but for now
2053      * just RAZ for all cores:
2054      */
2055     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2056       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2057       .access = PL1_R, .type = ARM_CP_CONST,
2058       .accessfn = access_aa64_tid1,
2059       .fgt = FGT_AIDR_EL1,
2060       .resetvalue = 0 },
2061     /*
2062      * Auxiliary fault status registers: these also are IMPDEF, and we
2063      * choose to RAZ/WI for all cores.
2064      */
2065     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2066       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2067       .access = PL1_RW, .accessfn = access_tvm_trvm,
2068       .fgt = FGT_AFSR0_EL1,
2069       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2070       .type = ARM_CP_CONST, .resetvalue = 0 },
2071     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2072       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2073       .access = PL1_RW, .accessfn = access_tvm_trvm,
2074       .fgt = FGT_AFSR1_EL1,
2075       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2076       .type = ARM_CP_CONST, .resetvalue = 0 },
2077     /*
2078      * MAIR can just read-as-written because we don't implement caches
2079      * and so don't need to care about memory attributes.
2080      */
2081     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2082       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2083       .access = PL1_RW, .accessfn = access_tvm_trvm,
2084       .fgt = FGT_MAIR_EL1,
2085       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2086       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2087       .resetvalue = 0 },
2088     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2089       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2090       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2091       .resetvalue = 0 },
2092     /*
2093      * For non-long-descriptor page tables these are PRRR and NMRR;
2094      * regardless they still act as reads-as-written for QEMU.
2095      */
2096      /*
2097       * MAIR0/1 are defined separately from their 64-bit counterpart which
2098       * allows them to assign the correct fieldoffset based on the endianness
2099       * handled in the field definitions.
2100       */
2101     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2102       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2103       .access = PL1_RW, .accessfn = access_tvm_trvm,
2104       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2105                              offsetof(CPUARMState, cp15.mair0_ns) },
2106       .resetfn = arm_cp_reset_ignore },
2107     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2108       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2109       .access = PL1_RW, .accessfn = access_tvm_trvm,
2110       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2111                              offsetof(CPUARMState, cp15.mair1_ns) },
2112       .resetfn = arm_cp_reset_ignore },
2113     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2114       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2115       .fgt = FGT_ISR_EL1,
2116       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2117 };
2118 
2119 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2120     /* PMOVSSET is not implemented in v7 before v7ve */
2121     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2122       .access = PL0_RW, .accessfn = pmreg_access,
2123       .fgt = FGT_PMOVS,
2124       .type = ARM_CP_ALIAS | ARM_CP_IO,
2125       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2126       .writefn = pmovsset_write,
2127       .raw_writefn = raw_write },
2128     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2129       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2130       .access = PL0_RW, .accessfn = pmreg_access,
2131       .fgt = FGT_PMOVS,
2132       .type = ARM_CP_ALIAS | ARM_CP_IO,
2133       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2134       .writefn = pmovsset_write,
2135       .raw_writefn = raw_write },
2136 };
2137 
2138 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2139                         uint64_t value)
2140 {
2141     value &= 1;
2142     env->teecr = value;
2143 }
2144 
2145 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2146                                    bool isread)
2147 {
2148     /*
2149      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2150      * at all, so we don't need to check whether we're v8A.
2151      */
2152     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2153         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2154         return CP_ACCESS_TRAP_EL2;
2155     }
2156     return CP_ACCESS_OK;
2157 }
2158 
2159 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2160                                     bool isread)
2161 {
2162     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2163         return CP_ACCESS_TRAP_EL1;
2164     }
2165     return teecr_access(env, ri, isread);
2166 }
2167 
2168 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2169     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2170       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2171       .resetvalue = 0,
2172       .writefn = teecr_write, .accessfn = teecr_access },
2173     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2174       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2175       .accessfn = teehbr_access, .resetvalue = 0 },
2176 };
2177 
2178 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2179     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2180       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2181       .access = PL0_RW,
2182       .fgt = FGT_TPIDR_EL0,
2183       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2184     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2185       .access = PL0_RW,
2186       .fgt = FGT_TPIDR_EL0,
2187       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2188                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2189       .resetfn = arm_cp_reset_ignore },
2190     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2191       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2192       .access = PL0_R | PL1_W,
2193       .fgt = FGT_TPIDRRO_EL0,
2194       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2195       .resetvalue = 0},
2196     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2197       .access = PL0_R | PL1_W,
2198       .fgt = FGT_TPIDRRO_EL0,
2199       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2200                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2201       .resetfn = arm_cp_reset_ignore },
2202     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2203       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2204       .access = PL1_RW,
2205       .fgt = FGT_TPIDR_EL1,
2206       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2207     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2208       .access = PL1_RW,
2209       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2210                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2211       .resetvalue = 0 },
2212 };
2213 
2214 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2215 {
2216     ARMCPU *cpu = env_archcpu(env);
2217 
2218     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2219 }
2220 
2221 #ifndef CONFIG_USER_ONLY
2222 
2223 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2224                                        bool isread)
2225 {
2226     /*
2227      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2228      * Writable only at the highest implemented exception level.
2229      */
2230     int el = arm_current_el(env);
2231     uint64_t hcr;
2232     uint32_t cntkctl;
2233 
2234     switch (el) {
2235     case 0:
2236         hcr = arm_hcr_el2_eff(env);
2237         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2238             cntkctl = env->cp15.cnthctl_el2;
2239         } else {
2240             cntkctl = env->cp15.c14_cntkctl;
2241         }
2242         if (!extract32(cntkctl, 0, 2)) {
2243             return CP_ACCESS_TRAP_EL1;
2244         }
2245         break;
2246     case 1:
2247         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2248             arm_is_secure_below_el3(env)) {
2249             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2250             return CP_ACCESS_UNDEFINED;
2251         }
2252         break;
2253     case 2:
2254     case 3:
2255         break;
2256     }
2257 
2258     if (!isread && el < arm_highest_el(env)) {
2259         return CP_ACCESS_UNDEFINED;
2260     }
2261 
2262     return CP_ACCESS_OK;
2263 }
2264 
2265 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2266                                         bool isread)
2267 {
2268     unsigned int cur_el = arm_current_el(env);
2269     bool has_el2 = arm_is_el2_enabled(env);
2270     uint64_t hcr = arm_hcr_el2_eff(env);
2271 
2272     switch (cur_el) {
2273     case 0:
2274         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2275         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2276             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2277                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2278         }
2279 
2280         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2281         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2282             return CP_ACCESS_TRAP_EL1;
2283         }
2284         /* fall through */
2285     case 1:
2286         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2287         if (has_el2 && timeridx == GTIMER_PHYS &&
2288             (hcr & HCR_E2H
2289              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2290              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2291             return CP_ACCESS_TRAP_EL2;
2292         }
2293         if (has_el2 && timeridx == GTIMER_VIRT) {
2294             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2295                 return CP_ACCESS_TRAP_EL2;
2296             }
2297         }
2298         break;
2299     }
2300     return CP_ACCESS_OK;
2301 }
2302 
2303 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2304                                       bool isread)
2305 {
2306     unsigned int cur_el = arm_current_el(env);
2307     bool has_el2 = arm_is_el2_enabled(env);
2308     uint64_t hcr = arm_hcr_el2_eff(env);
2309 
2310     switch (cur_el) {
2311     case 0:
2312         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2313             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2314             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2315                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2316         }
2317 
2318         /*
2319          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2320          * EL0 if EL0[PV]TEN is zero.
2321          */
2322         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2323             return CP_ACCESS_TRAP_EL1;
2324         }
2325         /* fall through */
2326 
2327     case 1:
2328         if (has_el2 && timeridx == GTIMER_PHYS) {
2329             if (hcr & HCR_E2H) {
2330                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2331                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2332                     return CP_ACCESS_TRAP_EL2;
2333                 }
2334             } else {
2335                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2336                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2337                     return CP_ACCESS_TRAP_EL2;
2338                 }
2339             }
2340         }
2341         if (has_el2 && timeridx == GTIMER_VIRT) {
2342             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2343                 return CP_ACCESS_TRAP_EL2;
2344             }
2345         }
2346         break;
2347     }
2348     return CP_ACCESS_OK;
2349 }
2350 
2351 static CPAccessResult gt_pct_access(CPUARMState *env,
2352                                     const ARMCPRegInfo *ri,
2353                                     bool isread)
2354 {
2355     return gt_counter_access(env, GTIMER_PHYS, isread);
2356 }
2357 
2358 static CPAccessResult gt_vct_access(CPUARMState *env,
2359                                     const ARMCPRegInfo *ri,
2360                                     bool isread)
2361 {
2362     return gt_counter_access(env, GTIMER_VIRT, isread);
2363 }
2364 
2365 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2366                                        bool isread)
2367 {
2368     return gt_timer_access(env, GTIMER_PHYS, isread);
2369 }
2370 
2371 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2372                                        bool isread)
2373 {
2374     return gt_timer_access(env, GTIMER_VIRT, isread);
2375 }
2376 
2377 static CPAccessResult gt_stimer_access(CPUARMState *env,
2378                                        const ARMCPRegInfo *ri,
2379                                        bool isread)
2380 {
2381     /*
2382      * The AArch64 register view of the secure physical timer is
2383      * always accessible from EL3, and configurably accessible from
2384      * Secure EL1.
2385      */
2386     switch (arm_current_el(env)) {
2387     case 1:
2388         if (!arm_is_secure(env)) {
2389             return CP_ACCESS_UNDEFINED;
2390         }
2391         if (arm_is_el2_enabled(env)) {
2392             return CP_ACCESS_UNDEFINED;
2393         }
2394         if (!(env->cp15.scr_el3 & SCR_ST)) {
2395             return CP_ACCESS_TRAP_EL3;
2396         }
2397         return CP_ACCESS_OK;
2398     case 0:
2399     case 2:
2400         return CP_ACCESS_UNDEFINED;
2401     case 3:
2402         return CP_ACCESS_OK;
2403     default:
2404         g_assert_not_reached();
2405     }
2406 }
2407 
2408 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2409                                           const ARMCPRegInfo *ri,
2410                                           bool isread)
2411 {
2412     /*
2413      * The AArch64 register view of the secure EL2 timers are mostly
2414      * accessible from EL3 and EL2 although can also be trapped to EL2
2415      * from EL1 depending on nested virt config.
2416      */
2417     switch (arm_current_el(env)) {
2418     case 0: /* UNDEFINED */
2419         return CP_ACCESS_UNDEFINED;
2420     case 1:
2421         if (!arm_is_secure(env)) {
2422             /* UNDEFINED */
2423             return CP_ACCESS_UNDEFINED;
2424         } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2425             /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2426             return CP_ACCESS_TRAP_EL2;
2427         }
2428         /* UNDEFINED */
2429         return CP_ACCESS_UNDEFINED;
2430     case 2:
2431         if (!arm_is_secure(env)) {
2432             /* UNDEFINED */
2433             return CP_ACCESS_UNDEFINED;
2434         }
2435         return CP_ACCESS_OK;
2436     case 3:
2437         if (env->cp15.scr_el3 & SCR_EEL2) {
2438             return CP_ACCESS_OK;
2439         } else {
2440             return CP_ACCESS_UNDEFINED;
2441         }
2442     default:
2443         g_assert_not_reached();
2444     }
2445 }
2446 
2447 uint64_t gt_get_countervalue(CPUARMState *env)
2448 {
2449     ARMCPU *cpu = env_archcpu(env);
2450 
2451     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2452 }
2453 
2454 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2455 {
2456     CPUARMState *env = &cpu->env;
2457     uint64_t cnthctl = env->cp15.cnthctl_el2;
2458     ARMSecuritySpace ss = arm_security_space(env);
2459     /* ISTATUS && !IMASK */
2460     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2461 
2462     /*
2463      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2464      * It is RES0 in Secure and NonSecure state.
2465      */
2466     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2467         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2468          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2469         irqstate = 0;
2470     }
2471 
2472     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2473     trace_arm_gt_update_irq(timeridx, irqstate);
2474 }
2475 
2476 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2477 {
2478     /*
2479      * Changing security state between Root and Secure/NonSecure, which may
2480      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2481      * mask bits. Update the IRQ state accordingly.
2482      */
2483     gt_update_irq(cpu, GTIMER_VIRT);
2484     gt_update_irq(cpu, GTIMER_PHYS);
2485 }
2486 
2487 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2488 {
2489     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2490         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2491         arm_is_el2_enabled(env) &&
2492         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2493         return env->cp15.cntpoff_el2;
2494     }
2495     return 0;
2496 }
2497 
2498 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2499 {
2500     /*
2501      * Return the timer offset to use for indirect accesses to the timer.
2502      * This is the Offset value as defined in D12.2.4.1 "Operation of the
2503      * CompareValue views of the timers".
2504      *
2505      * The condition here is not always the same as the condition for
2506      * whether to apply an offset register when doing a direct read of
2507      * the counter sysreg; those conditions are described in the
2508      * access pseudocode for each counter register.
2509      */
2510     switch (timeridx) {
2511     case GTIMER_PHYS:
2512         return gt_phys_raw_cnt_offset(env);
2513     case GTIMER_VIRT:
2514         return env->cp15.cntvoff_el2;
2515     case GTIMER_HYP:
2516     case GTIMER_SEC:
2517     case GTIMER_HYPVIRT:
2518     case GTIMER_S_EL2_PHYS:
2519     case GTIMER_S_EL2_VIRT:
2520         return 0;
2521     default:
2522         g_assert_not_reached();
2523     }
2524 }
2525 
2526 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2527 {
2528     /*
2529      * Return the timer offset to use for direct accesses to the
2530      * counter registers CNTPCT and CNTVCT, and for direct accesses
2531      * to the CNT*_TVAL registers.
2532      *
2533      * This isn't exactly the same as the indirect-access offset,
2534      * because here we also care about what EL the register access
2535      * is being made from.
2536      *
2537      * This corresponds to the access pseudocode for the registers.
2538      */
2539     uint64_t hcr;
2540 
2541     switch (timeridx) {
2542     case GTIMER_PHYS:
2543         if (arm_current_el(env) >= 2) {
2544             return 0;
2545         }
2546         return gt_phys_raw_cnt_offset(env);
2547     case GTIMER_VIRT:
2548         switch (arm_current_el(env)) {
2549         case 2:
2550             hcr = arm_hcr_el2_eff(env);
2551             if (hcr & HCR_E2H) {
2552                 return 0;
2553             }
2554             break;
2555         case 0:
2556             hcr = arm_hcr_el2_eff(env);
2557             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2558                 return 0;
2559             }
2560             break;
2561         }
2562         return env->cp15.cntvoff_el2;
2563     case GTIMER_HYP:
2564     case GTIMER_SEC:
2565     case GTIMER_HYPVIRT:
2566     case GTIMER_S_EL2_PHYS:
2567     case GTIMER_S_EL2_VIRT:
2568         return 0;
2569     default:
2570         g_assert_not_reached();
2571     }
2572 }
2573 
2574 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2575 {
2576     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2577 
2578     if (gt->ctl & 1) {
2579         /*
2580          * Timer enabled: calculate and set current ISTATUS, irq, and
2581          * reset timer to when ISTATUS next has to change
2582          */
2583         uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2584         uint64_t count = gt_get_countervalue(&cpu->env);
2585         /* Note that this must be unsigned 64 bit arithmetic: */
2586         int istatus = count - offset >= gt->cval;
2587         uint64_t nexttick;
2588 
2589         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2590 
2591         if (istatus) {
2592             /*
2593              * Next transition is when (count - offset) rolls back over to 0.
2594              * If offset > count then this is when count == offset;
2595              * if offset <= count then this is when count == offset + 2^64
2596              * For the latter case we set nexttick to an "as far in future
2597              * as possible" value and let the code below handle it.
2598              */
2599             if (offset > count) {
2600                 nexttick = offset;
2601             } else {
2602                 nexttick = UINT64_MAX;
2603             }
2604         } else {
2605             /*
2606              * Next transition is when (count - offset) == cval, i.e.
2607              * when count == (cval + offset).
2608              * If that would overflow, then again we set up the next interrupt
2609              * for "as far in the future as possible" for the code below.
2610              */
2611             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2612                 nexttick = UINT64_MAX;
2613             }
2614         }
2615         /*
2616          * Note that the desired next expiry time might be beyond the
2617          * signed-64-bit range of a QEMUTimer -- in this case we just
2618          * set the timer for as far in the future as possible. When the
2619          * timer expires we will reset the timer for any remaining period.
2620          */
2621         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2622             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2623         } else {
2624             timer_mod(cpu->gt_timer[timeridx], nexttick);
2625         }
2626         trace_arm_gt_recalc(timeridx, nexttick);
2627     } else {
2628         /* Timer disabled: ISTATUS and timer output always clear */
2629         gt->ctl &= ~4;
2630         timer_del(cpu->gt_timer[timeridx]);
2631         trace_arm_gt_recalc_disabled(timeridx);
2632     }
2633     gt_update_irq(cpu, timeridx);
2634 }
2635 
2636 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2637                            int timeridx)
2638 {
2639     ARMCPU *cpu = env_archcpu(env);
2640 
2641     timer_del(cpu->gt_timer[timeridx]);
2642 }
2643 
2644 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2645 {
2646     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2647     return gt_get_countervalue(env) - offset;
2648 }
2649 
2650 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2651 {
2652     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2653     return gt_get_countervalue(env) - offset;
2654 }
2655 
2656 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2657                           int timeridx,
2658                           uint64_t value)
2659 {
2660     trace_arm_gt_cval_write(timeridx, value);
2661     env->cp15.c14_timer[timeridx].cval = value;
2662     gt_recalc_timer(env_archcpu(env), timeridx);
2663 }
2664 
2665 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2666 {
2667     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2668                       (gt_get_countervalue(env) - offset));
2669 }
2670 
2671 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2672                              int timeridx)
2673 {
2674     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2675 
2676     return do_tval_read(env, timeridx, offset);
2677 }
2678 
2679 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2680                           uint64_t offset)
2681 {
2682     trace_arm_gt_tval_write(timeridx, value);
2683     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2684                                          sextract64(value, 0, 32);
2685     gt_recalc_timer(env_archcpu(env), timeridx);
2686 }
2687 
2688 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689                           int timeridx,
2690                           uint64_t value)
2691 {
2692     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2693 
2694     do_tval_write(env, timeridx, value, offset);
2695 }
2696 
2697 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2698                          int timeridx,
2699                          uint64_t value)
2700 {
2701     ARMCPU *cpu = env_archcpu(env);
2702     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2703 
2704     trace_arm_gt_ctl_write(timeridx, value);
2705     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2706     if ((oldval ^ value) & 1) {
2707         /* Enable toggled */
2708         gt_recalc_timer(cpu, timeridx);
2709     } else if ((oldval ^ value) & 2) {
2710         /*
2711          * IMASK toggled: don't need to recalculate,
2712          * just set the interrupt line based on ISTATUS
2713          */
2714         trace_arm_gt_imask_toggle(timeridx);
2715         gt_update_irq(cpu, timeridx);
2716     }
2717 }
2718 
2719 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2720 {
2721     gt_timer_reset(env, ri, GTIMER_PHYS);
2722 }
2723 
2724 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725                                uint64_t value)
2726 {
2727     gt_cval_write(env, ri, GTIMER_PHYS, value);
2728 }
2729 
2730 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2731 {
2732     return gt_tval_read(env, ri, GTIMER_PHYS);
2733 }
2734 
2735 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2736                                uint64_t value)
2737 {
2738     gt_tval_write(env, ri, GTIMER_PHYS, value);
2739 }
2740 
2741 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742                               uint64_t value)
2743 {
2744     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2745 }
2746 
2747 static int gt_phys_redir_timeridx(CPUARMState *env)
2748 {
2749     switch (arm_mmu_idx(env)) {
2750     case ARMMMUIdx_E20_0:
2751     case ARMMMUIdx_E20_2:
2752     case ARMMMUIdx_E20_2_PAN:
2753         return GTIMER_HYP;
2754     default:
2755         return GTIMER_PHYS;
2756     }
2757 }
2758 
2759 static int gt_virt_redir_timeridx(CPUARMState *env)
2760 {
2761     switch (arm_mmu_idx(env)) {
2762     case ARMMMUIdx_E20_0:
2763     case ARMMMUIdx_E20_2:
2764     case ARMMMUIdx_E20_2_PAN:
2765         return GTIMER_HYPVIRT;
2766     default:
2767         return GTIMER_VIRT;
2768     }
2769 }
2770 
2771 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2772                                         const ARMCPRegInfo *ri)
2773 {
2774     int timeridx = gt_phys_redir_timeridx(env);
2775     return env->cp15.c14_timer[timeridx].cval;
2776 }
2777 
2778 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779                                      uint64_t value)
2780 {
2781     int timeridx = gt_phys_redir_timeridx(env);
2782     gt_cval_write(env, ri, timeridx, value);
2783 }
2784 
2785 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2786                                         const ARMCPRegInfo *ri)
2787 {
2788     int timeridx = gt_phys_redir_timeridx(env);
2789     return gt_tval_read(env, ri, timeridx);
2790 }
2791 
2792 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793                                      uint64_t value)
2794 {
2795     int timeridx = gt_phys_redir_timeridx(env);
2796     gt_tval_write(env, ri, timeridx, value);
2797 }
2798 
2799 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2800                                        const ARMCPRegInfo *ri)
2801 {
2802     int timeridx = gt_phys_redir_timeridx(env);
2803     return env->cp15.c14_timer[timeridx].ctl;
2804 }
2805 
2806 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2807                                     uint64_t value)
2808 {
2809     int timeridx = gt_phys_redir_timeridx(env);
2810     gt_ctl_write(env, ri, timeridx, value);
2811 }
2812 
2813 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2814 {
2815     gt_timer_reset(env, ri, GTIMER_VIRT);
2816 }
2817 
2818 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2819                                uint64_t value)
2820 {
2821     gt_cval_write(env, ri, GTIMER_VIRT, value);
2822 }
2823 
2824 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2825 {
2826     /*
2827      * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
2828      * we always apply CNTVOFF_EL2. Special case that here rather
2829      * than going into the generic gt_tval_read() and then having
2830      * to re-detect that it's this register.
2831      * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
2832      */
2833     return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
2834 }
2835 
2836 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2837                                uint64_t value)
2838 {
2839     /* Similarly for writes to CNTV_TVAL_EL02 */
2840     do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
2841 }
2842 
2843 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844                               uint64_t value)
2845 {
2846     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2847 }
2848 
2849 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850                              uint64_t value)
2851 {
2852     ARMCPU *cpu = env_archcpu(env);
2853     uint32_t oldval = env->cp15.cnthctl_el2;
2854     uint32_t valid_mask =
2855         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2856         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2857         R_CNTHCTL_EVNTEN_MASK |
2858         R_CNTHCTL_EVNTDIR_MASK |
2859         R_CNTHCTL_EVNTI_MASK |
2860         R_CNTHCTL_EL0VTEN_MASK |
2861         R_CNTHCTL_EL0PTEN_MASK |
2862         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2863         R_CNTHCTL_EL1PTEN_MASK;
2864 
2865     if (cpu_isar_feature(aa64_rme, cpu)) {
2866         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2867     }
2868     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2869         valid_mask |=
2870             R_CNTHCTL_EL1TVT_MASK |
2871             R_CNTHCTL_EL1TVCT_MASK |
2872             R_CNTHCTL_EL1NVPCT_MASK |
2873             R_CNTHCTL_EL1NVVCT_MASK |
2874             R_CNTHCTL_EVNTIS_MASK;
2875     }
2876     if (cpu_isar_feature(aa64_ecv, cpu)) {
2877         valid_mask |= R_CNTHCTL_ECV_MASK;
2878     }
2879 
2880     /* Clear RES0 bits */
2881     value &= valid_mask;
2882 
2883     raw_write(env, ri, value);
2884 
2885     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2886         gt_update_irq(cpu, GTIMER_VIRT);
2887     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2888         gt_update_irq(cpu, GTIMER_PHYS);
2889     }
2890 }
2891 
2892 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2893                               uint64_t value)
2894 {
2895     ARMCPU *cpu = env_archcpu(env);
2896 
2897     trace_arm_gt_cntvoff_write(value);
2898     raw_write(env, ri, value);
2899     gt_recalc_timer(cpu, GTIMER_VIRT);
2900 }
2901 
2902 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2903                                         const ARMCPRegInfo *ri)
2904 {
2905     int timeridx = gt_virt_redir_timeridx(env);
2906     return env->cp15.c14_timer[timeridx].cval;
2907 }
2908 
2909 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2910                                      uint64_t value)
2911 {
2912     int timeridx = gt_virt_redir_timeridx(env);
2913     gt_cval_write(env, ri, timeridx, value);
2914 }
2915 
2916 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2917                                         const ARMCPRegInfo *ri)
2918 {
2919     int timeridx = gt_virt_redir_timeridx(env);
2920     return gt_tval_read(env, ri, timeridx);
2921 }
2922 
2923 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2924                                      uint64_t value)
2925 {
2926     int timeridx = gt_virt_redir_timeridx(env);
2927     gt_tval_write(env, ri, timeridx, value);
2928 }
2929 
2930 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2931                                        const ARMCPRegInfo *ri)
2932 {
2933     int timeridx = gt_virt_redir_timeridx(env);
2934     return env->cp15.c14_timer[timeridx].ctl;
2935 }
2936 
2937 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938                                     uint64_t value)
2939 {
2940     int timeridx = gt_virt_redir_timeridx(env);
2941     gt_ctl_write(env, ri, timeridx, value);
2942 }
2943 
2944 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2945 {
2946     gt_timer_reset(env, ri, GTIMER_HYP);
2947 }
2948 
2949 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2950                               uint64_t value)
2951 {
2952     gt_cval_write(env, ri, GTIMER_HYP, value);
2953 }
2954 
2955 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2956 {
2957     return gt_tval_read(env, ri, GTIMER_HYP);
2958 }
2959 
2960 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961                               uint64_t value)
2962 {
2963     gt_tval_write(env, ri, GTIMER_HYP, value);
2964 }
2965 
2966 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2967                               uint64_t value)
2968 {
2969     gt_ctl_write(env, ri, GTIMER_HYP, value);
2970 }
2971 
2972 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2973 {
2974     gt_timer_reset(env, ri, GTIMER_SEC);
2975 }
2976 
2977 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978                               uint64_t value)
2979 {
2980     gt_cval_write(env, ri, GTIMER_SEC, value);
2981 }
2982 
2983 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2984 {
2985     return gt_tval_read(env, ri, GTIMER_SEC);
2986 }
2987 
2988 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2989                               uint64_t value)
2990 {
2991     gt_tval_write(env, ri, GTIMER_SEC, value);
2992 }
2993 
2994 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2995                               uint64_t value)
2996 {
2997     gt_ctl_write(env, ri, GTIMER_SEC, value);
2998 }
2999 
3000 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3001 {
3002     gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3003 }
3004 
3005 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3006                                    uint64_t value)
3007 {
3008     gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3009 }
3010 
3011 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3012 {
3013     return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3014 }
3015 
3016 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3017                               uint64_t value)
3018 {
3019     gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3020 }
3021 
3022 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3023                               uint64_t value)
3024 {
3025     gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3026 }
3027 
3028 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3029 {
3030     gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3031 }
3032 
3033 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3034                               uint64_t value)
3035 {
3036     gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3037 }
3038 
3039 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3040 {
3041     return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3042 }
3043 
3044 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3045                                    uint64_t value)
3046 {
3047     gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3048 }
3049 
3050 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3051                               uint64_t value)
3052 {
3053     gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3054 }
3055 
3056 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3057 {
3058     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3059 }
3060 
3061 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3062                              uint64_t value)
3063 {
3064     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3065 }
3066 
3067 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3068 {
3069     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3070 }
3071 
3072 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3073                              uint64_t value)
3074 {
3075     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3076 }
3077 
3078 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3079                             uint64_t value)
3080 {
3081     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3082 }
3083 
3084 void arm_gt_ptimer_cb(void *opaque)
3085 {
3086     ARMCPU *cpu = opaque;
3087 
3088     gt_recalc_timer(cpu, GTIMER_PHYS);
3089 }
3090 
3091 void arm_gt_vtimer_cb(void *opaque)
3092 {
3093     ARMCPU *cpu = opaque;
3094 
3095     gt_recalc_timer(cpu, GTIMER_VIRT);
3096 }
3097 
3098 void arm_gt_htimer_cb(void *opaque)
3099 {
3100     ARMCPU *cpu = opaque;
3101 
3102     gt_recalc_timer(cpu, GTIMER_HYP);
3103 }
3104 
3105 void arm_gt_stimer_cb(void *opaque)
3106 {
3107     ARMCPU *cpu = opaque;
3108 
3109     gt_recalc_timer(cpu, GTIMER_SEC);
3110 }
3111 
3112 void arm_gt_sel2timer_cb(void *opaque)
3113 {
3114     ARMCPU *cpu = opaque;
3115 
3116     gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3117 }
3118 
3119 void arm_gt_sel2vtimer_cb(void *opaque)
3120 {
3121     ARMCPU *cpu = opaque;
3122 
3123     gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3124 }
3125 
3126 void arm_gt_hvtimer_cb(void *opaque)
3127 {
3128     ARMCPU *cpu = opaque;
3129 
3130     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3131 }
3132 
3133 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3134     /*
3135      * Note that CNTFRQ is purely reads-as-written for the benefit
3136      * of software; writing it doesn't actually change the timer frequency.
3137      * Our reset value matches the fixed frequency we implement the timer at.
3138      */
3139     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3140       .type = ARM_CP_ALIAS,
3141       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3142       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3143     },
3144     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3145       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3146       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3147       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3148       .resetfn = arm_gt_cntfrq_reset,
3149     },
3150     /* overall control: mostly access permissions */
3151     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3152       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3153       .access = PL1_RW,
3154       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3155       .resetvalue = 0,
3156     },
3157     /* per-timer control */
3158     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3159       .secure = ARM_CP_SECSTATE_NS,
3160       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3161       .accessfn = gt_ptimer_access,
3162       .fieldoffset = offsetoflow32(CPUARMState,
3163                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3164       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3165       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3166     },
3167     { .name = "CNTP_CTL_S",
3168       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3169       .secure = ARM_CP_SECSTATE_S,
3170       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3171       .accessfn = gt_ptimer_access,
3172       .fieldoffset = offsetoflow32(CPUARMState,
3173                                    cp15.c14_timer[GTIMER_SEC].ctl),
3174       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3175     },
3176     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3177       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3178       .type = ARM_CP_IO, .access = PL0_RW,
3179       .accessfn = gt_ptimer_access,
3180       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3181       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3182       .resetvalue = 0,
3183       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3184       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3185     },
3186     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3187       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3188       .accessfn = gt_vtimer_access,
3189       .fieldoffset = offsetoflow32(CPUARMState,
3190                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3191       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3192       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3193     },
3194     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3195       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3196       .type = ARM_CP_IO, .access = PL0_RW,
3197       .accessfn = gt_vtimer_access,
3198       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3199       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3200       .resetvalue = 0,
3201       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3202       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3203     },
3204     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3205     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3206       .secure = ARM_CP_SECSTATE_NS,
3207       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3208       .accessfn = gt_ptimer_access,
3209       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3210     },
3211     { .name = "CNTP_TVAL_S",
3212       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3213       .secure = ARM_CP_SECSTATE_S,
3214       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3215       .accessfn = gt_ptimer_access,
3216       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3217     },
3218     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3219       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3220       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3221       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3222       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3223     },
3224     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3225       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3226       .accessfn = gt_vtimer_access,
3227       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3228     },
3229     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3230       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3231       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3232       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3233       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3234     },
3235     /* The counter itself */
3236     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3237       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3238       .accessfn = gt_pct_access,
3239       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3240     },
3241     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3242       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3243       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3244       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3245     },
3246     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3247       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3248       .accessfn = gt_vct_access,
3249       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3250     },
3251     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3252       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3253       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3254       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3255     },
3256     /* Comparison value, indicating when the timer goes off */
3257     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3258       .secure = ARM_CP_SECSTATE_NS,
3259       .access = PL0_RW,
3260       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3261       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3262       .accessfn = gt_ptimer_access,
3263       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3264       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3265     },
3266     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3267       .secure = ARM_CP_SECSTATE_S,
3268       .access = PL0_RW,
3269       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3270       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3271       .accessfn = gt_ptimer_access,
3272       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3273     },
3274     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3275       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3276       .access = PL0_RW,
3277       .type = ARM_CP_IO,
3278       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3279       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3280       .resetvalue = 0, .accessfn = gt_ptimer_access,
3281       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3282       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3283     },
3284     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3285       .access = PL0_RW,
3286       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3287       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3288       .accessfn = gt_vtimer_access,
3289       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3290       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3291     },
3292     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3293       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3294       .access = PL0_RW,
3295       .type = ARM_CP_IO,
3296       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3297       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3298       .resetvalue = 0, .accessfn = gt_vtimer_access,
3299       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3300       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3301     },
3302     /*
3303      * Secure timer -- this is actually restricted to only EL3
3304      * and configurably Secure-EL1 via the accessfn.
3305      */
3306     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3307       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3308       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3309       .accessfn = gt_stimer_access,
3310       .readfn = gt_sec_tval_read,
3311       .writefn = gt_sec_tval_write,
3312       .resetfn = gt_sec_timer_reset,
3313     },
3314     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3315       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3316       .type = ARM_CP_IO, .access = PL1_RW,
3317       .accessfn = gt_stimer_access,
3318       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3319       .resetvalue = 0,
3320       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3321     },
3322     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3323       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3324       .type = ARM_CP_IO, .access = PL1_RW,
3325       .accessfn = gt_stimer_access,
3326       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3327       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3328     },
3329 };
3330 
3331 /*
3332  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3333  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3334  * so our implementations here are identical to the normal registers.
3335  */
3336 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3337     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3338       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3339       .accessfn = gt_vct_access,
3340       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3341     },
3342     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3343       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3344       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3345       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3346     },
3347     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3348       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3349       .accessfn = gt_pct_access,
3350       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3351     },
3352     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3353       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3354       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3355       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3356     },
3357 };
3358 
3359 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3360                                         const ARMCPRegInfo *ri,
3361                                         bool isread)
3362 {
3363     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3364         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3365         return CP_ACCESS_TRAP_EL3;
3366     }
3367     return CP_ACCESS_OK;
3368 }
3369 
3370 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3371                               uint64_t value)
3372 {
3373     ARMCPU *cpu = env_archcpu(env);
3374 
3375     trace_arm_gt_cntpoff_write(value);
3376     raw_write(env, ri, value);
3377     gt_recalc_timer(cpu, GTIMER_PHYS);
3378 }
3379 
3380 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3381     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3382     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3383     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3384     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3385     .nv2_redirect_offset = 0x1a8,
3386     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3387 };
3388 #else
3389 
3390 /*
3391  * In user-mode most of the generic timer registers are inaccessible
3392  * however modern kernels (4.12+) allow access to cntvct_el0
3393  */
3394 
3395 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3396 {
3397     ARMCPU *cpu = env_archcpu(env);
3398 
3399     /*
3400      * Currently we have no support for QEMUTimer in linux-user so we
3401      * can't call gt_get_countervalue(env), instead we directly
3402      * call the lower level functions.
3403      */
3404     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3405 }
3406 
3407 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3408     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3409       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3410       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3411       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3412       .resetfn = arm_gt_cntfrq_reset,
3413     },
3414     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3415       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3416       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3417       .readfn = gt_virt_cnt_read,
3418     },
3419 };
3420 
3421 /*
3422  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3423  * is exposed to userspace by Linux.
3424  */
3425 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3426     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3427       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3428       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3429       .readfn = gt_virt_cnt_read,
3430     },
3431 };
3432 
3433 #endif
3434 
3435 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3436 {
3437     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3438         raw_write(env, ri, value);
3439     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3440         raw_write(env, ri, value & 0xfffff6ff);
3441     } else {
3442         raw_write(env, ri, value & 0xfffff1ff);
3443     }
3444 }
3445 
3446 #ifndef CONFIG_USER_ONLY
3447 /* get_phys_addr() isn't present for user-mode-only targets */
3448 
3449 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3450                                  bool isread)
3451 {
3452     if (ri->opc2 & 4) {
3453         /*
3454          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3455          * Secure EL1 (which can only happen if EL3 is AArch64).
3456          * They are simply UNDEF if executed from NS EL1.
3457          * They function normally from EL2 or EL3.
3458          */
3459         if (arm_current_el(env) == 1) {
3460             if (arm_is_secure_below_el3(env)) {
3461                 if (env->cp15.scr_el3 & SCR_EEL2) {
3462                     return CP_ACCESS_TRAP_EL2;
3463                 }
3464                 return CP_ACCESS_TRAP_EL3;
3465             }
3466             return CP_ACCESS_UNDEFINED;
3467         }
3468     }
3469     return CP_ACCESS_OK;
3470 }
3471 
3472 #ifdef CONFIG_TCG
3473 static int par_el1_shareability(GetPhysAddrResult *res)
3474 {
3475     /*
3476      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3477      * memory -- see pseudocode PAREncodeShareability().
3478      */
3479     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3480         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3481         return 2;
3482     }
3483     return res->cacheattrs.shareability;
3484 }
3485 
3486 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3487                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3488                              ARMSecuritySpace ss)
3489 {
3490     bool ret;
3491     uint64_t par64;
3492     bool format64 = false;
3493     ARMMMUFaultInfo fi = {};
3494     GetPhysAddrResult res = {};
3495 
3496     /*
3497      * I_MXTJT: Granule protection checks are not performed on the final
3498      * address of a successful translation.  This is a translation not a
3499      * memory reference, so "memop = none = 0".
3500      */
3501     ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3502                                          mmu_idx, ss, &res, &fi);
3503 
3504     /*
3505      * ATS operations only do S1 or S1+S2 translations, so we never
3506      * have to deal with the ARMCacheAttrs format for S2 only.
3507      */
3508     assert(!res.cacheattrs.is_s2_format);
3509 
3510     if (ret) {
3511         /*
3512          * Some kinds of translation fault must cause exceptions rather
3513          * than being reported in the PAR.
3514          */
3515         int current_el = arm_current_el(env);
3516         int target_el;
3517         uint32_t syn, fsr, fsc;
3518         bool take_exc = false;
3519 
3520         if (fi.s1ptw && current_el == 1
3521             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3522             /*
3523              * Synchronous stage 2 fault on an access made as part of the
3524              * translation table walk for AT S1E0* or AT S1E1* insn
3525              * executed from NS EL1. If this is a synchronous external abort
3526              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3527              * to EL3. Otherwise the fault is taken as an exception to EL2,
3528              * and HPFAR_EL2 holds the faulting IPA.
3529              */
3530             if (fi.type == ARMFault_SyncExternalOnWalk &&
3531                 (env->cp15.scr_el3 & SCR_EA)) {
3532                 target_el = 3;
3533             } else {
3534                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3535                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3536                     env->cp15.hpfar_el2 |= HPFAR_NS;
3537                 }
3538                 target_el = 2;
3539             }
3540             take_exc = true;
3541         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3542             /*
3543              * Synchronous external aborts during a translation table walk
3544              * are taken as Data Abort exceptions.
3545              */
3546             if (fi.stage2) {
3547                 if (current_el == 3) {
3548                     target_el = 3;
3549                 } else {
3550                     target_el = 2;
3551                 }
3552             } else {
3553                 target_el = exception_target_el(env);
3554             }
3555             take_exc = true;
3556         }
3557 
3558         if (take_exc) {
3559             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3560             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3561                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3562                 fsr = arm_fi_to_lfsc(&fi);
3563                 fsc = extract32(fsr, 0, 6);
3564             } else {
3565                 fsr = arm_fi_to_sfsc(&fi);
3566                 fsc = 0x3f;
3567             }
3568             /*
3569              * Report exception with ESR indicating a fault due to a
3570              * translation table walk for a cache maintenance instruction.
3571              */
3572             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3573                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3574             env->exception.vaddress = value;
3575             env->exception.fsr = fsr;
3576             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3577         }
3578     }
3579 
3580     if (is_a64(env)) {
3581         format64 = true;
3582     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3583         /*
3584          * ATS1Cxx:
3585          * * TTBCR.EAE determines whether the result is returned using the
3586          *   32-bit or the 64-bit PAR format
3587          * * Instructions executed in Hyp mode always use the 64bit format
3588          *
3589          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3590          * * The Non-secure TTBCR.EAE bit is set to 1
3591          * * The implementation includes EL2, and the value of HCR.VM is 1
3592          *
3593          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3594          *
3595          * ATS1Hx always uses the 64bit format.
3596          */
3597         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3598 
3599         if (arm_feature(env, ARM_FEATURE_EL2)) {
3600             if (mmu_idx == ARMMMUIdx_E10_0 ||
3601                 mmu_idx == ARMMMUIdx_E10_1 ||
3602                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3603                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3604             } else {
3605                 format64 |= arm_current_el(env) == 2;
3606             }
3607         }
3608     }
3609 
3610     if (format64) {
3611         /* Create a 64-bit PAR */
3612         par64 = (1 << 11); /* LPAE bit always set */
3613         if (!ret) {
3614             par64 |= res.f.phys_addr & ~0xfffULL;
3615             if (!res.f.attrs.secure) {
3616                 par64 |= (1 << 9); /* NS */
3617             }
3618             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3619             par64 |= par_el1_shareability(&res) << 7; /* SH */
3620         } else {
3621             uint32_t fsr = arm_fi_to_lfsc(&fi);
3622 
3623             par64 |= 1; /* F */
3624             par64 |= (fsr & 0x3f) << 1; /* FS */
3625             if (fi.stage2) {
3626                 par64 |= (1 << 9); /* S */
3627             }
3628             if (fi.s1ptw) {
3629                 par64 |= (1 << 8); /* PTW */
3630             }
3631         }
3632     } else {
3633         /*
3634          * fsr is a DFSR/IFSR value for the short descriptor
3635          * translation table format (with WnR always clear).
3636          * Convert it to a 32-bit PAR.
3637          */
3638         if (!ret) {
3639             /* We do not set any attribute bits in the PAR */
3640             if (res.f.lg_page_size == 24
3641                 && arm_feature(env, ARM_FEATURE_V7)) {
3642                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3643             } else {
3644                 par64 = res.f.phys_addr & 0xfffff000;
3645             }
3646             if (!res.f.attrs.secure) {
3647                 par64 |= (1 << 9); /* NS */
3648             }
3649         } else {
3650             uint32_t fsr = arm_fi_to_sfsc(&fi);
3651 
3652             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3653                     ((fsr & 0xf) << 1) | 1;
3654         }
3655     }
3656     return par64;
3657 }
3658 #endif /* CONFIG_TCG */
3659 
3660 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3661 {
3662 #ifdef CONFIG_TCG
3663     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3664     uint64_t par64;
3665     ARMMMUIdx mmu_idx;
3666     int el = arm_current_el(env);
3667     ARMSecuritySpace ss = arm_security_space(env);
3668 
3669     switch (ri->opc2 & 6) {
3670     case 0:
3671         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3672         switch (el) {
3673         case 3:
3674             if (ri->crm == 9 && arm_pan_enabled(env)) {
3675                 mmu_idx = ARMMMUIdx_E30_3_PAN;
3676             } else {
3677                 mmu_idx = ARMMMUIdx_E3;
3678             }
3679             break;
3680         case 2:
3681             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3682             /* fall through */
3683         case 1:
3684             if (ri->crm == 9 && arm_pan_enabled(env)) {
3685                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3686             } else {
3687                 mmu_idx = ARMMMUIdx_Stage1_E1;
3688             }
3689             break;
3690         default:
3691             g_assert_not_reached();
3692         }
3693         break;
3694     case 2:
3695         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3696         switch (el) {
3697         case 3:
3698             mmu_idx = ARMMMUIdx_E30_0;
3699             break;
3700         case 2:
3701             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3702             mmu_idx = ARMMMUIdx_Stage1_E0;
3703             break;
3704         case 1:
3705             mmu_idx = ARMMMUIdx_Stage1_E0;
3706             break;
3707         default:
3708             g_assert_not_reached();
3709         }
3710         break;
3711     case 4:
3712         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3713         mmu_idx = ARMMMUIdx_E10_1;
3714         ss = ARMSS_NonSecure;
3715         break;
3716     case 6:
3717         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3718         mmu_idx = ARMMMUIdx_E10_0;
3719         ss = ARMSS_NonSecure;
3720         break;
3721     default:
3722         g_assert_not_reached();
3723     }
3724 
3725     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3726 
3727     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3728 #else
3729     /* Handled by hardware accelerator. */
3730     g_assert_not_reached();
3731 #endif /* CONFIG_TCG */
3732 }
3733 
3734 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3735                         uint64_t value)
3736 {
3737 #ifdef CONFIG_TCG
3738     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3739     uint64_t par64;
3740 
3741     /* There is no SecureEL2 for AArch32. */
3742     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3743                          ARMSS_NonSecure);
3744 
3745     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3746 #else
3747     /* Handled by hardware accelerator. */
3748     g_assert_not_reached();
3749 #endif /* CONFIG_TCG */
3750 }
3751 
3752 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3753                                      bool isread)
3754 {
3755     /*
3756      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3757      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3758      * only happen when executing at EL3 because that combination also causes an
3759      * illegal exception return. We don't need to check FEAT_RME either, because
3760      * scr_write() ensures that the NSE bit is not set otherwise.
3761      */
3762     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3763         return CP_ACCESS_UNDEFINED;
3764     }
3765     return CP_ACCESS_OK;
3766 }
3767 
3768 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3769                                      bool isread)
3770 {
3771     if (arm_current_el(env) == 3 &&
3772         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3773         return CP_ACCESS_UNDEFINED;
3774     }
3775     return at_e012_access(env, ri, isread);
3776 }
3777 
3778 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3779                                       bool isread)
3780 {
3781     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3782         return CP_ACCESS_TRAP_EL2;
3783     }
3784     return at_e012_access(env, ri, isread);
3785 }
3786 
3787 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3788                         uint64_t value)
3789 {
3790 #ifdef CONFIG_TCG
3791     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3792     ARMMMUIdx mmu_idx;
3793     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3794     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3795     bool for_el3 = false;
3796     ARMSecuritySpace ss;
3797 
3798     switch (ri->opc2 & 6) {
3799     case 0:
3800         switch (ri->opc1) {
3801         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3802             if (ri->crm == 9 && arm_pan_enabled(env)) {
3803                 mmu_idx = regime_e20 ?
3804                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3805             } else {
3806                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3807             }
3808             break;
3809         case 4: /* AT S1E2R, AT S1E2W */
3810             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3811             break;
3812         case 6: /* AT S1E3R, AT S1E3W */
3813             mmu_idx = ARMMMUIdx_E3;
3814             for_el3 = true;
3815             break;
3816         default:
3817             g_assert_not_reached();
3818         }
3819         break;
3820     case 2: /* AT S1E0R, AT S1E0W */
3821         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3822         break;
3823     case 4: /* AT S12E1R, AT S12E1W */
3824         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3825         break;
3826     case 6: /* AT S12E0R, AT S12E0W */
3827         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3828         break;
3829     default:
3830         g_assert_not_reached();
3831     }
3832 
3833     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3834     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3835 #else
3836     /* Handled by hardware accelerator. */
3837     g_assert_not_reached();
3838 #endif /* CONFIG_TCG */
3839 }
3840 #endif
3841 
3842 /* Return basic MPU access permission bits.  */
3843 static uint32_t simple_mpu_ap_bits(uint32_t val)
3844 {
3845     uint32_t ret;
3846     uint32_t mask;
3847     int i;
3848     ret = 0;
3849     mask = 3;
3850     for (i = 0; i < 16; i += 2) {
3851         ret |= (val >> i) & mask;
3852         mask <<= 2;
3853     }
3854     return ret;
3855 }
3856 
3857 /* Pad basic MPU access permission bits to extended format.  */
3858 static uint32_t extended_mpu_ap_bits(uint32_t val)
3859 {
3860     uint32_t ret;
3861     uint32_t mask;
3862     int i;
3863     ret = 0;
3864     mask = 3;
3865     for (i = 0; i < 16; i += 2) {
3866         ret |= (val & mask) << i;
3867         mask <<= 2;
3868     }
3869     return ret;
3870 }
3871 
3872 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3873                                  uint64_t value)
3874 {
3875     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3876 }
3877 
3878 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3879 {
3880     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3881 }
3882 
3883 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3884                                  uint64_t value)
3885 {
3886     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3887 }
3888 
3889 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3890 {
3891     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3892 }
3893 
3894 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3895 {
3896     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3897 
3898     if (!u32p) {
3899         return 0;
3900     }
3901 
3902     u32p += env->pmsav7.rnr[M_REG_NS];
3903     return *u32p;
3904 }
3905 
3906 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3907                          uint64_t value)
3908 {
3909     ARMCPU *cpu = env_archcpu(env);
3910     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3911 
3912     if (!u32p) {
3913         return;
3914     }
3915 
3916     u32p += env->pmsav7.rnr[M_REG_NS];
3917     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3918     *u32p = value;
3919 }
3920 
3921 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3922                               uint64_t value)
3923 {
3924     ARMCPU *cpu = env_archcpu(env);
3925     uint32_t nrgs = cpu->pmsav7_dregion;
3926 
3927     if (value >= nrgs) {
3928         qemu_log_mask(LOG_GUEST_ERROR,
3929                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3930                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3931         return;
3932     }
3933 
3934     raw_write(env, ri, value);
3935 }
3936 
3937 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3938                           uint64_t value)
3939 {
3940     ARMCPU *cpu = env_archcpu(env);
3941 
3942     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3943     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3944 }
3945 
3946 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3947 {
3948     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3949 }
3950 
3951 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3952                           uint64_t value)
3953 {
3954     ARMCPU *cpu = env_archcpu(env);
3955 
3956     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3957     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3958 }
3959 
3960 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3961 {
3962     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3963 }
3964 
3965 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3966                            uint64_t value)
3967 {
3968     ARMCPU *cpu = env_archcpu(env);
3969 
3970     /*
3971      * Ignore writes that would select not implemented region.
3972      * This is architecturally UNPREDICTABLE.
3973      */
3974     if (value >= cpu->pmsav7_dregion) {
3975         return;
3976     }
3977 
3978     env->pmsav7.rnr[M_REG_NS] = value;
3979 }
3980 
3981 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3982                           uint64_t value)
3983 {
3984     ARMCPU *cpu = env_archcpu(env);
3985 
3986     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3987     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3988 }
3989 
3990 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3991 {
3992     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3993 }
3994 
3995 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3996                           uint64_t value)
3997 {
3998     ARMCPU *cpu = env_archcpu(env);
3999 
4000     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4001     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4002 }
4003 
4004 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4005 {
4006     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4007 }
4008 
4009 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4010                           uint64_t value)
4011 {
4012     uint32_t n;
4013     uint32_t bit;
4014     ARMCPU *cpu = env_archcpu(env);
4015 
4016     /* Ignore writes to unimplemented regions */
4017     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4018     value &= MAKE_64BIT_MASK(0, rmax);
4019 
4020     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4021 
4022     /* Register alias is only valid for first 32 indexes */
4023     for (n = 0; n < rmax; ++n) {
4024         bit = extract32(value, n, 1);
4025         env->pmsav8.hprlar[n] = deposit32(
4026                     env->pmsav8.hprlar[n], 0, 1, bit);
4027     }
4028 }
4029 
4030 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4031 {
4032     uint32_t n;
4033     uint32_t result = 0x0;
4034     ARMCPU *cpu = env_archcpu(env);
4035 
4036     /* Register alias is only valid for first 32 indexes */
4037     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4038         if (env->pmsav8.hprlar[n] & 0x1) {
4039             result |= (0x1 << n);
4040         }
4041     }
4042     return result;
4043 }
4044 
4045 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4046                            uint64_t value)
4047 {
4048     ARMCPU *cpu = env_archcpu(env);
4049 
4050     /*
4051      * Ignore writes that would select not implemented region.
4052      * This is architecturally UNPREDICTABLE.
4053      */
4054     if (value >= cpu->pmsav8r_hdregion) {
4055         return;
4056     }
4057 
4058     env->pmsav8.hprselr = value;
4059 }
4060 
4061 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4062                           uint64_t value)
4063 {
4064     ARMCPU *cpu = env_archcpu(env);
4065     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4066                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4067 
4068     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4069 
4070     if (ri->opc1 & 4) {
4071         if (index >= cpu->pmsav8r_hdregion) {
4072             return;
4073         }
4074         if (ri->opc2 & 0x1) {
4075             env->pmsav8.hprlar[index] = value;
4076         } else {
4077             env->pmsav8.hprbar[index] = value;
4078         }
4079     } else {
4080         if (index >= cpu->pmsav7_dregion) {
4081             return;
4082         }
4083         if (ri->opc2 & 0x1) {
4084             env->pmsav8.rlar[M_REG_NS][index] = value;
4085         } else {
4086             env->pmsav8.rbar[M_REG_NS][index] = value;
4087         }
4088     }
4089 }
4090 
4091 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4092 {
4093     ARMCPU *cpu = env_archcpu(env);
4094     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4095                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4096 
4097     if (ri->opc1 & 4) {
4098         if (index >= cpu->pmsav8r_hdregion) {
4099             return 0x0;
4100         }
4101         if (ri->opc2 & 0x1) {
4102             return env->pmsav8.hprlar[index];
4103         } else {
4104             return env->pmsav8.hprbar[index];
4105         }
4106     } else {
4107         if (index >= cpu->pmsav7_dregion) {
4108             return 0x0;
4109         }
4110         if (ri->opc2 & 0x1) {
4111             return env->pmsav8.rlar[M_REG_NS][index];
4112         } else {
4113             return env->pmsav8.rbar[M_REG_NS][index];
4114         }
4115     }
4116 }
4117 
4118 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4119     { .name = "PRBAR",
4120       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4121       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4122       .accessfn = access_tvm_trvm,
4123       .readfn = prbar_read, .writefn = prbar_write },
4124     { .name = "PRLAR",
4125       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4126       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4127       .accessfn = access_tvm_trvm,
4128       .readfn = prlar_read, .writefn = prlar_write },
4129     { .name = "PRSELR", .resetvalue = 0,
4130       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4131       .access = PL1_RW, .accessfn = access_tvm_trvm,
4132       .writefn = prselr_write,
4133       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4134     { .name = "HPRBAR", .resetvalue = 0,
4135       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4136       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4137       .readfn = hprbar_read, .writefn = hprbar_write },
4138     { .name = "HPRLAR",
4139       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4140       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4141       .readfn = hprlar_read, .writefn = hprlar_write },
4142     { .name = "HPRSELR", .resetvalue = 0,
4143       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4144       .access = PL2_RW,
4145       .writefn = hprselr_write,
4146       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4147     { .name = "HPRENR",
4148       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4149       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4150       .readfn = hprenr_read, .writefn = hprenr_write },
4151 };
4152 
4153 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4154     /*
4155      * Reset for all these registers is handled in arm_cpu_reset(),
4156      * because the PMSAv7 is also used by M-profile CPUs, which do
4157      * not register cpregs but still need the state to be reset.
4158      */
4159     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4160       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4161       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4162       .readfn = pmsav7_read, .writefn = pmsav7_write,
4163       .resetfn = arm_cp_reset_ignore },
4164     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4165       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4166       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4167       .readfn = pmsav7_read, .writefn = pmsav7_write,
4168       .resetfn = arm_cp_reset_ignore },
4169     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4170       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4171       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4172       .readfn = pmsav7_read, .writefn = pmsav7_write,
4173       .resetfn = arm_cp_reset_ignore },
4174     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4175       .access = PL1_RW,
4176       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4177       .writefn = pmsav7_rgnr_write,
4178       .resetfn = arm_cp_reset_ignore },
4179 };
4180 
4181 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4182     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4183       .access = PL1_RW, .type = ARM_CP_ALIAS,
4184       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4185       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4186     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4187       .access = PL1_RW, .type = ARM_CP_ALIAS,
4188       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4189       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4190     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4191       .access = PL1_RW,
4192       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4193       .resetvalue = 0, },
4194     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4195       .access = PL1_RW,
4196       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4197       .resetvalue = 0, },
4198     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4199       .access = PL1_RW,
4200       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4201     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4202       .access = PL1_RW,
4203       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4204     /* Protection region base and size registers */
4205     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4206       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4207       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4208     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4209       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4210       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4211     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4212       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4213       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4214     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4215       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4216       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4217     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4218       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4219       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4220     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4221       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4222       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4223     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4224       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4225       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4226     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4227       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4228       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4229 };
4230 
4231 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4232                              uint64_t value)
4233 {
4234     ARMCPU *cpu = env_archcpu(env);
4235 
4236     if (!arm_feature(env, ARM_FEATURE_V8)) {
4237         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4238             /*
4239              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4240              * using Long-descriptor translation table format
4241              */
4242             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4243         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4244             /*
4245              * In an implementation that includes the Security Extensions
4246              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4247              * Short-descriptor translation table format.
4248              */
4249             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4250         } else {
4251             value &= TTBCR_N;
4252         }
4253     }
4254 
4255     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4256         /*
4257          * With LPAE the TTBCR could result in a change of ASID
4258          * via the TTBCR.A1 bit, so do a TLB flush.
4259          */
4260         tlb_flush(CPU(cpu));
4261     }
4262     raw_write(env, ri, value);
4263 }
4264 
4265 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4266                                uint64_t value)
4267 {
4268     ARMCPU *cpu = env_archcpu(env);
4269 
4270     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4271     tlb_flush(CPU(cpu));
4272     raw_write(env, ri, value);
4273 }
4274 
4275 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4276                             uint64_t value)
4277 {
4278     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4279     if (cpreg_field_is_64bit(ri) &&
4280         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4281         ARMCPU *cpu = env_archcpu(env);
4282         tlb_flush(CPU(cpu));
4283     }
4284     raw_write(env, ri, value);
4285 }
4286 
4287 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4288                                     uint64_t value)
4289 {
4290     /*
4291      * If we are running with E2&0 regime, then an ASID is active.
4292      * Flush if that might be changing.  Note we're not checking
4293      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4294      * holds the active ASID, only checking the field that might.
4295      */
4296     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4297         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4298         uint16_t mask = ARMMMUIdxBit_E20_2 |
4299                         ARMMMUIdxBit_E20_2_PAN |
4300                         ARMMMUIdxBit_E20_0;
4301         tlb_flush_by_mmuidx(env_cpu(env), mask);
4302     }
4303     raw_write(env, ri, value);
4304 }
4305 
4306 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4307                         uint64_t value)
4308 {
4309     ARMCPU *cpu = env_archcpu(env);
4310     CPUState *cs = CPU(cpu);
4311 
4312     /*
4313      * A change in VMID to the stage2 page table (Stage2) invalidates
4314      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4315      */
4316     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4317         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4318     }
4319     raw_write(env, ri, value);
4320 }
4321 
4322 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4323     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4324       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4325       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4326                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4327     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4328       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4329       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4330                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4331     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4332       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4333       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4334                              offsetof(CPUARMState, cp15.dfar_ns) } },
4335     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4336       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4337       .access = PL1_RW, .accessfn = access_tvm_trvm,
4338       .fgt = FGT_FAR_EL1,
4339       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4340       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4341       .resetvalue = 0, },
4342 };
4343 
4344 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4345     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4346       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4347       .access = PL1_RW, .accessfn = access_tvm_trvm,
4348       .fgt = FGT_ESR_EL1,
4349       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4350       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4351     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4352       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4353       .access = PL1_RW, .accessfn = access_tvm_trvm,
4354       .fgt = FGT_TTBR0_EL1,
4355       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4356       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4357       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4358                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4359     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4360       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4361       .access = PL1_RW, .accessfn = access_tvm_trvm,
4362       .fgt = FGT_TTBR1_EL1,
4363       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4364       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4365       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4366                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4367     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4368       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4369       .access = PL1_RW, .accessfn = access_tvm_trvm,
4370       .fgt = FGT_TCR_EL1,
4371       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4372       .writefn = vmsa_tcr_el12_write,
4373       .raw_writefn = raw_write,
4374       .resetvalue = 0,
4375       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4376     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4377       .access = PL1_RW, .accessfn = access_tvm_trvm,
4378       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4379       .raw_writefn = raw_write,
4380       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4381                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4382 };
4383 
4384 /*
4385  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4386  * qemu tlbs nor adjusting cached masks.
4387  */
4388 static const ARMCPRegInfo ttbcr2_reginfo = {
4389     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4390     .access = PL1_RW, .accessfn = access_tvm_trvm,
4391     .type = ARM_CP_ALIAS,
4392     .bank_fieldoffsets = {
4393         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4394         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4395     },
4396 };
4397 
4398 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4399                                 uint64_t value)
4400 {
4401     env->cp15.c15_ticonfig = value & 0xe7;
4402     /* The OS_TYPE bit in this register changes the reported CPUID! */
4403     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4404         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4405 }
4406 
4407 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4408                                 uint64_t value)
4409 {
4410     env->cp15.c15_threadid = value & 0xffff;
4411 }
4412 
4413 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414                            uint64_t value)
4415 {
4416     /* Wait-for-interrupt (deprecated) */
4417     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4418 }
4419 
4420 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421                                   uint64_t value)
4422 {
4423     /*
4424      * On OMAP there are registers indicating the max/min index of dcache lines
4425      * containing a dirty line; cache flush operations have to reset these.
4426      */
4427     env->cp15.c15_i_max = 0x000;
4428     env->cp15.c15_i_min = 0xff0;
4429 }
4430 
4431 static const ARMCPRegInfo omap_cp_reginfo[] = {
4432     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4433       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4434       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4435       .resetvalue = 0, },
4436     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4437       .access = PL1_RW, .type = ARM_CP_NOP },
4438     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4439       .access = PL1_RW,
4440       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4441       .writefn = omap_ticonfig_write },
4442     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4443       .access = PL1_RW,
4444       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4445     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4446       .access = PL1_RW, .resetvalue = 0xff0,
4447       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4448     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4449       .access = PL1_RW,
4450       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4451       .writefn = omap_threadid_write },
4452     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4453       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4454       .type = ARM_CP_NO_RAW,
4455       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4456     /*
4457      * TODO: Peripheral port remap register:
4458      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4459      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4460      * when MMU is off.
4461      */
4462     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4463       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4464       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4465       .writefn = omap_cachemaint_write },
4466     { .name = "C9", .cp = 15, .crn = 9,
4467       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4468       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4469 };
4470 
4471 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4472                               uint64_t value)
4473 {
4474     env->cp15.c15_cpar = value & 0x3fff;
4475 }
4476 
4477 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4478     { .name = "XSCALE_CPAR",
4479       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4480       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4481       .writefn = xscale_cpar_write, },
4482     { .name = "XSCALE_AUXCR",
4483       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4484       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4485       .resetvalue = 0, },
4486     /*
4487      * XScale specific cache-lockdown: since we have no cache we NOP these
4488      * and hope the guest does not really rely on cache behaviour.
4489      */
4490     { .name = "XSCALE_LOCK_ICACHE_LINE",
4491       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4492       .access = PL1_W, .type = ARM_CP_NOP },
4493     { .name = "XSCALE_UNLOCK_ICACHE",
4494       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4495       .access = PL1_W, .type = ARM_CP_NOP },
4496     { .name = "XSCALE_DCACHE_LOCK",
4497       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4498       .access = PL1_RW, .type = ARM_CP_NOP },
4499     { .name = "XSCALE_UNLOCK_DCACHE",
4500       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4501       .access = PL1_W, .type = ARM_CP_NOP },
4502 };
4503 
4504 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4505     /*
4506      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4507      * implementation of this implementation-defined space.
4508      * Ideally this should eventually disappear in favour of actually
4509      * implementing the correct behaviour for all cores.
4510      */
4511     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4512       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4513       .access = PL1_RW,
4514       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4515       .resetvalue = 0 },
4516 };
4517 
4518 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4519     /* Cache status: RAZ because we have no cache so it's always clean */
4520     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4521       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4522       .resetvalue = 0 },
4523 };
4524 
4525 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4526     /* We never have a block transfer operation in progress */
4527     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4528       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4529       .resetvalue = 0 },
4530     /* The cache ops themselves: these all NOP for QEMU */
4531     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4532       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4533     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4534       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4535     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4536       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4537     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4538       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4539     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4540       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4541     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4542       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4543 };
4544 
4545 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4546     /*
4547      * The cache test-and-clean instructions always return (1 << 30)
4548      * to indicate that there are no dirty cache lines.
4549      */
4550     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4551       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4552       .resetvalue = (1 << 30) },
4553     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4554       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4555       .resetvalue = (1 << 30) },
4556 };
4557 
4558 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4559     /* Ignore ReadBuffer accesses */
4560     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4561       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4562       .access = PL1_RW, .resetvalue = 0,
4563       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4564 };
4565 
4566 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4567 {
4568     unsigned int cur_el = arm_current_el(env);
4569 
4570     if (arm_is_el2_enabled(env) && cur_el == 1) {
4571         return env->cp15.vpidr_el2;
4572     }
4573     return raw_read(env, ri);
4574 }
4575 
4576 static uint64_t mpidr_read_val(CPUARMState *env)
4577 {
4578     ARMCPU *cpu = env_archcpu(env);
4579     uint64_t mpidr = cpu->mp_affinity;
4580 
4581     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4582         mpidr |= (1U << 31);
4583         /*
4584          * Cores which are uniprocessor (non-coherent)
4585          * but still implement the MP extensions set
4586          * bit 30. (For instance, Cortex-R5).
4587          */
4588         if (cpu->mp_is_up) {
4589             mpidr |= (1u << 30);
4590         }
4591     }
4592     return mpidr;
4593 }
4594 
4595 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4596 {
4597     unsigned int cur_el = arm_current_el(env);
4598 
4599     if (arm_is_el2_enabled(env) && cur_el == 1) {
4600         return env->cp15.vmpidr_el2;
4601     }
4602     return mpidr_read_val(env);
4603 }
4604 
4605 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4606     /* NOP AMAIR0/1 */
4607     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4608       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4609       .access = PL1_RW, .accessfn = access_tvm_trvm,
4610       .fgt = FGT_AMAIR_EL1,
4611       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4612       .type = ARM_CP_CONST, .resetvalue = 0 },
4613     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4614     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4615       .access = PL1_RW, .accessfn = access_tvm_trvm,
4616       .type = ARM_CP_CONST, .resetvalue = 0 },
4617     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4618       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4619       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4620                              offsetof(CPUARMState, cp15.par_ns)} },
4621     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4622       .access = PL1_RW, .accessfn = access_tvm_trvm,
4623       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4624       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4625                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4626       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4627     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4628       .access = PL1_RW, .accessfn = access_tvm_trvm,
4629       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4630       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4631                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4632       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4633 };
4634 
4635 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4636 {
4637     return vfp_get_fpcr(env);
4638 }
4639 
4640 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4641                             uint64_t value)
4642 {
4643     vfp_set_fpcr(env, value);
4644 }
4645 
4646 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4647 {
4648     return vfp_get_fpsr(env);
4649 }
4650 
4651 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4652                             uint64_t value)
4653 {
4654     vfp_set_fpsr(env, value);
4655 }
4656 
4657 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4658                                        bool isread)
4659 {
4660     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4661         return CP_ACCESS_TRAP_EL1;
4662     }
4663     return CP_ACCESS_OK;
4664 }
4665 
4666 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4667                             uint64_t value)
4668 {
4669     env->daif = value & PSTATE_DAIF;
4670 }
4671 
4672 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4673 {
4674     return env->pstate & PSTATE_PAN;
4675 }
4676 
4677 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4678                            uint64_t value)
4679 {
4680     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4681 }
4682 
4683 static const ARMCPRegInfo pan_reginfo = {
4684     .name = "PAN", .state = ARM_CP_STATE_AA64,
4685     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4686     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4687     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4688 };
4689 
4690 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4691 {
4692     return env->pstate & PSTATE_UAO;
4693 }
4694 
4695 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4696                            uint64_t value)
4697 {
4698     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4699 }
4700 
4701 static const ARMCPRegInfo uao_reginfo = {
4702     .name = "UAO", .state = ARM_CP_STATE_AA64,
4703     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4704     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4705     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4706 };
4707 
4708 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4709 {
4710     return env->pstate & PSTATE_DIT;
4711 }
4712 
4713 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4714                            uint64_t value)
4715 {
4716     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4717 }
4718 
4719 static const ARMCPRegInfo dit_reginfo = {
4720     .name = "DIT", .state = ARM_CP_STATE_AA64,
4721     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4722     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4723     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4724 };
4725 
4726 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4727 {
4728     return env->pstate & PSTATE_SSBS;
4729 }
4730 
4731 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4732                            uint64_t value)
4733 {
4734     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4735 }
4736 
4737 static const ARMCPRegInfo ssbs_reginfo = {
4738     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4739     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4740     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4741     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4742 };
4743 
4744 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4745                                               const ARMCPRegInfo *ri,
4746                                               bool isread)
4747 {
4748     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4749     switch (arm_current_el(env)) {
4750     case 0:
4751         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4752         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4753             return CP_ACCESS_TRAP_EL1;
4754         }
4755         /* fall through */
4756     case 1:
4757         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4758         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4759             return CP_ACCESS_TRAP_EL2;
4760         }
4761         break;
4762     }
4763     return CP_ACCESS_OK;
4764 }
4765 
4766 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4767 {
4768     /* Cache invalidate/clean to Point of Unification... */
4769     switch (arm_current_el(env)) {
4770     case 0:
4771         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4772         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4773             return CP_ACCESS_TRAP_EL1;
4774         }
4775         /* fall through */
4776     case 1:
4777         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4778         if (arm_hcr_el2_eff(env) & hcrflags) {
4779             return CP_ACCESS_TRAP_EL2;
4780         }
4781         break;
4782     }
4783     return CP_ACCESS_OK;
4784 }
4785 
4786 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4787                                    bool isread)
4788 {
4789     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4790 }
4791 
4792 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4793                                   bool isread)
4794 {
4795     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4796 }
4797 
4798 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4799                                       bool isread)
4800 {
4801     int cur_el = arm_current_el(env);
4802 
4803     if (cur_el < 2) {
4804         uint64_t hcr = arm_hcr_el2_eff(env);
4805 
4806         if (cur_el == 0) {
4807             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4808                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4809                     return CP_ACCESS_TRAP_EL2;
4810                 }
4811             } else {
4812                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4813                     return CP_ACCESS_TRAP_EL1;
4814                 }
4815                 if (hcr & HCR_TDZ) {
4816                     return CP_ACCESS_TRAP_EL2;
4817                 }
4818             }
4819         } else if (hcr & HCR_TDZ) {
4820             return CP_ACCESS_TRAP_EL2;
4821         }
4822     }
4823     return CP_ACCESS_OK;
4824 }
4825 
4826 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4827 {
4828     ARMCPU *cpu = env_archcpu(env);
4829     int dzp_bit = 1 << 4;
4830 
4831     /* DZP indicates whether DC ZVA access is allowed */
4832     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4833         dzp_bit = 0;
4834     }
4835     return cpu->dcz_blocksize | dzp_bit;
4836 }
4837 
4838 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4839                                     bool isread)
4840 {
4841     if (!(env->pstate & PSTATE_SP)) {
4842         /*
4843          * Access to SP_EL0 is undefined if it's being used as
4844          * the stack pointer.
4845          */
4846         return CP_ACCESS_UNDEFINED;
4847     }
4848     return CP_ACCESS_OK;
4849 }
4850 
4851 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4852 {
4853     return env->pstate & PSTATE_SP;
4854 }
4855 
4856 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4857 {
4858     update_spsel(env, val);
4859 }
4860 
4861 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4862                         uint64_t value)
4863 {
4864     ARMCPU *cpu = env_archcpu(env);
4865 
4866     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4867         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4868         value &= ~SCTLR_M;
4869     }
4870 
4871     /* ??? Lots of these bits are not implemented.  */
4872 
4873     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4874         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4875             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4876         } else {
4877             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4878                        SCTLR_ATA0 | SCTLR_ATA);
4879         }
4880     }
4881 
4882     if (raw_read(env, ri) == value) {
4883         /*
4884          * Skip the TLB flush if nothing actually changed; Linux likes
4885          * to do a lot of pointless SCTLR writes.
4886          */
4887         return;
4888     }
4889 
4890     raw_write(env, ri, value);
4891 
4892     /* This may enable/disable the MMU, so do a TLB flush.  */
4893     tlb_flush(CPU(cpu));
4894 
4895     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4896         /*
4897          * Normally we would always end the TB on an SCTLR write; see the
4898          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4899          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4900          * of hflags from the translator, so do it here.
4901          */
4902         arm_rebuild_hflags(env);
4903     }
4904 }
4905 
4906 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4907                            uint64_t value)
4908 {
4909     /*
4910      * Some MDCR_EL3 bits affect whether PMU counters are running:
4911      * if we are trying to change any of those then we must
4912      * bracket this update with PMU start/finish calls.
4913      */
4914     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4915 
4916     if (pmu_op) {
4917         pmu_op_start(env);
4918     }
4919     env->cp15.mdcr_el3 = value;
4920     if (pmu_op) {
4921         pmu_op_finish(env);
4922     }
4923 }
4924 
4925 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4926                        uint64_t value)
4927 {
4928     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4929     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4930 }
4931 
4932 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4933                            uint64_t value)
4934 {
4935     /*
4936      * Some MDCR_EL2 bits affect whether PMU counters are running:
4937      * if we are trying to change any of those then we must
4938      * bracket this update with PMU start/finish calls.
4939      */
4940     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4941 
4942     if (pmu_op) {
4943         pmu_op_start(env);
4944     }
4945     env->cp15.mdcr_el2 = value;
4946     if (pmu_op) {
4947         pmu_op_finish(env);
4948     }
4949 }
4950 
4951 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4952                                  bool isread)
4953 {
4954     if (arm_current_el(env) == 1) {
4955         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4956 
4957         if (hcr_nv == (HCR_NV | HCR_NV1)) {
4958             return CP_ACCESS_TRAP_EL2;
4959         }
4960     }
4961     return CP_ACCESS_OK;
4962 }
4963 
4964 #ifdef CONFIG_USER_ONLY
4965 /*
4966  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4967  * code to get around W^X restrictions, where one region is writable and the
4968  * other is executable.
4969  *
4970  * Since the executable region is never written to we cannot detect code
4971  * changes when running in user mode, and rely on the emulated JIT telling us
4972  * that the code has changed by executing this instruction.
4973  */
4974 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4975                           uint64_t value)
4976 {
4977     uint64_t icache_line_mask, start_address, end_address;
4978     const ARMCPU *cpu;
4979 
4980     cpu = env_archcpu(env);
4981 
4982     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4983     start_address = value & ~icache_line_mask;
4984     end_address = value | icache_line_mask;
4985 
4986     mmap_lock();
4987 
4988     tb_invalidate_phys_range(start_address, end_address);
4989 
4990     mmap_unlock();
4991 }
4992 #endif
4993 
4994 static const ARMCPRegInfo v8_cp_reginfo[] = {
4995     /*
4996      * Minimal set of EL0-visible registers. This will need to be expanded
4997      * significantly for system emulation of AArch64 CPUs.
4998      */
4999     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5000       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5001       .access = PL0_RW, .type = ARM_CP_NZCV },
5002     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5003       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5004       .type = ARM_CP_NO_RAW,
5005       .access = PL0_RW, .accessfn = aa64_daif_access,
5006       .fieldoffset = offsetof(CPUARMState, daif),
5007       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5008     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5009       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5010       .access = PL0_RW, .type = ARM_CP_FPU,
5011       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5012     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5013       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5014       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5015       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5016     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5017       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5018       .access = PL0_R, .type = ARM_CP_NO_RAW,
5019       .fgt = FGT_DCZID_EL0,
5020       .readfn = aa64_dczid_read },
5021     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5022       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5023       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5024 #ifndef CONFIG_USER_ONLY
5025       /* Avoid overhead of an access check that always passes in user-mode */
5026       .accessfn = aa64_zva_access,
5027       .fgt = FGT_DCZVA,
5028 #endif
5029     },
5030     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5031       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5032       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5033     /*
5034      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5035      * don't emulate caches.
5036      */
5037     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5038       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5039       .access = PL1_W, .type = ARM_CP_NOP,
5040       .fgt = FGT_ICIALLUIS,
5041       .accessfn = access_ticab },
5042     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5043       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5044       .access = PL1_W, .type = ARM_CP_NOP,
5045       .fgt = FGT_ICIALLU,
5046       .accessfn = access_tocu },
5047     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5048       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5049       .access = PL0_W,
5050       .fgt = FGT_ICIVAU,
5051       .accessfn = access_tocu,
5052 #ifdef CONFIG_USER_ONLY
5053       .type = ARM_CP_NO_RAW,
5054       .writefn = ic_ivau_write
5055 #else
5056       .type = ARM_CP_NOP
5057 #endif
5058     },
5059     /* Cache ops: all NOPs since we don't emulate caches */
5060     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5061       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5062       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5063       .fgt = FGT_DCIVAC,
5064       .type = ARM_CP_NOP },
5065     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5066       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5067       .fgt = FGT_DCISW,
5068       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5069     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5070       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5071       .access = PL0_W, .type = ARM_CP_NOP,
5072       .fgt = FGT_DCCVAC,
5073       .accessfn = aa64_cacheop_poc_access },
5074     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5075       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5076       .fgt = FGT_DCCSW,
5077       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5078     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5079       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5080       .access = PL0_W, .type = ARM_CP_NOP,
5081       .fgt = FGT_DCCVAU,
5082       .accessfn = access_tocu },
5083     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5084       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5085       .access = PL0_W, .type = ARM_CP_NOP,
5086       .fgt = FGT_DCCIVAC,
5087       .accessfn = aa64_cacheop_poc_access },
5088     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5089       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5090       .fgt = FGT_DCCISW,
5091       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5092 #ifndef CONFIG_USER_ONLY
5093     /* 64 bit address translation operations */
5094     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5095       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5096       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5097       .fgt = FGT_ATS1E1R,
5098       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5099     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5100       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5101       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5102       .fgt = FGT_ATS1E1W,
5103       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5104     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5105       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5106       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5107       .fgt = FGT_ATS1E0R,
5108       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5109     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5110       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5111       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5112       .fgt = FGT_ATS1E0W,
5113       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5114     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5115       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5116       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5117       .accessfn = at_e012_access, .writefn = ats_write64 },
5118     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5119       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5120       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5121       .accessfn = at_e012_access, .writefn = ats_write64 },
5122     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5123       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5124       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5125       .accessfn = at_e012_access, .writefn = ats_write64 },
5126     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5127       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5128       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5129       .accessfn = at_e012_access, .writefn = ats_write64 },
5130     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5131     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5132       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5133       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5134       .writefn = ats_write64 },
5135     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5136       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5137       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5138       .writefn = ats_write64 },
5139     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5140       .type = ARM_CP_ALIAS,
5141       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5142       .access = PL1_RW, .resetvalue = 0,
5143       .fgt = FGT_PAR_EL1,
5144       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5145       .writefn = par_write },
5146 #endif
5147     /* 32 bit cache operations */
5148     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5149       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5150     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5151       .type = ARM_CP_NOP, .access = PL1_W },
5152     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5153       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5154     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5155       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5156     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5157       .type = ARM_CP_NOP, .access = PL1_W },
5158     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5159       .type = ARM_CP_NOP, .access = PL1_W },
5160     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5161       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5162     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5163       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5164     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5165       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5166     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5167       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5168     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5169       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5170     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5171       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5172     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5173       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5174     /* MMU Domain access control / MPU write buffer control */
5175     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5176       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5177       .writefn = dacr_write, .raw_writefn = raw_write,
5178       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5179                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5180     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5181       .type = ARM_CP_ALIAS,
5182       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5183       .access = PL1_RW, .accessfn = access_nv1,
5184       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5185       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5186     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5187       .type = ARM_CP_ALIAS,
5188       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5189       .access = PL1_RW, .accessfn = access_nv1,
5190       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5191       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5192     /*
5193      * We rely on the access checks not allowing the guest to write to the
5194      * state field when SPSel indicates that it's being used as the stack
5195      * pointer.
5196      */
5197     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5198       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5199       .access = PL1_RW, .accessfn = sp_el0_access,
5200       .type = ARM_CP_ALIAS,
5201       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5202     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5203       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5204       .nv2_redirect_offset = 0x240,
5205       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5206       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5207     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5208       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5209       .type = ARM_CP_NO_RAW,
5210       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5211     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5212       .type = ARM_CP_ALIAS,
5213       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5214       .access = PL2_RW,
5215       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5216     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5217       .type = ARM_CP_ALIAS,
5218       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5219       .access = PL2_RW,
5220       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5221     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5222       .type = ARM_CP_ALIAS,
5223       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5224       .access = PL2_RW,
5225       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5226     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5227       .type = ARM_CP_ALIAS,
5228       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5229       .access = PL2_RW,
5230       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5231     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5232       .type = ARM_CP_IO,
5233       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5234       .resetvalue = 0,
5235       .access = PL3_RW,
5236       .writefn = mdcr_el3_write,
5237       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5238     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5239       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5240       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5241       .writefn = sdcr_write,
5242       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5243 };
5244 
5245 /* These are present only when EL1 supports AArch32 */
5246 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5247     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5248       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5249       .access = PL2_RW,
5250       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5251       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5252     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5253       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5254       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5255       .writefn = dacr_write, .raw_writefn = raw_write,
5256       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5257     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5258       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5259       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5260       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5261 };
5262 
5263 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5264 {
5265     ARMCPU *cpu = env_archcpu(env);
5266 
5267     if (arm_feature(env, ARM_FEATURE_V8)) {
5268         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5269     } else {
5270         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5271     }
5272 
5273     if (arm_feature(env, ARM_FEATURE_EL3)) {
5274         valid_mask &= ~HCR_HCD;
5275     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5276         /*
5277          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5278          * However, if we're using the SMC PSCI conduit then QEMU is
5279          * effectively acting like EL3 firmware and so the guest at
5280          * EL2 should retain the ability to prevent EL1 from being
5281          * able to make SMC calls into the ersatz firmware, so in
5282          * that case HCR.TSC should be read/write.
5283          */
5284         valid_mask &= ~HCR_TSC;
5285     }
5286 
5287     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5288         if (cpu_isar_feature(aa64_vh, cpu)) {
5289             valid_mask |= HCR_E2H;
5290         }
5291         if (cpu_isar_feature(aa64_ras, cpu)) {
5292             valid_mask |= HCR_TERR | HCR_TEA;
5293         }
5294         if (cpu_isar_feature(aa64_lor, cpu)) {
5295             valid_mask |= HCR_TLOR;
5296         }
5297         if (cpu_isar_feature(aa64_pauth, cpu)) {
5298             valid_mask |= HCR_API | HCR_APK;
5299         }
5300         if (cpu_isar_feature(aa64_mte, cpu)) {
5301             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5302         }
5303         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5304             valid_mask |= HCR_ENSCXT;
5305         }
5306         if (cpu_isar_feature(aa64_fwb, cpu)) {
5307             valid_mask |= HCR_FWB;
5308         }
5309         if (cpu_isar_feature(aa64_rme, cpu)) {
5310             valid_mask |= HCR_GPF;
5311         }
5312         if (cpu_isar_feature(aa64_nv, cpu)) {
5313             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5314         }
5315         if (cpu_isar_feature(aa64_nv2, cpu)) {
5316             valid_mask |= HCR_NV2;
5317         }
5318     }
5319 
5320     if (cpu_isar_feature(any_evt, cpu)) {
5321         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5322     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5323         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5324     }
5325 
5326     /* Clear RES0 bits.  */
5327     value &= valid_mask;
5328 
5329     /*
5330      * These bits change the MMU setup:
5331      * HCR_VM enables stage 2 translation
5332      * HCR_PTW forbids certain page-table setups
5333      * HCR_DC disables stage1 and enables stage2 translation
5334      * HCR_DCT enables tagging on (disabled) stage1 translation
5335      * HCR_FWB changes the interpretation of stage2 descriptor bits
5336      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5337      */
5338     if ((env->cp15.hcr_el2 ^ value) &
5339         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5340         tlb_flush(CPU(cpu));
5341     }
5342     env->cp15.hcr_el2 = value;
5343 
5344     /*
5345      * Updates to VI and VF require us to update the status of
5346      * virtual interrupts, which are the logical OR of these bits
5347      * and the state of the input lines from the GIC. (This requires
5348      * that we have the BQL, which is done by marking the
5349      * reginfo structs as ARM_CP_IO.)
5350      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5351      * VFNMI, it is never possible for it to be taken immediately
5352      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5353      * at EL0 or EL1, and HCR can only be written at EL2.
5354      */
5355     g_assert(bql_locked());
5356     arm_cpu_update_virq(cpu);
5357     arm_cpu_update_vfiq(cpu);
5358     arm_cpu_update_vserr(cpu);
5359     if (cpu_isar_feature(aa64_nmi, cpu)) {
5360         arm_cpu_update_vinmi(cpu);
5361         arm_cpu_update_vfnmi(cpu);
5362     }
5363 }
5364 
5365 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5366 {
5367     do_hcr_write(env, value, 0);
5368 }
5369 
5370 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5371                           uint64_t value)
5372 {
5373     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5374     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5375     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5376 }
5377 
5378 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5379                          uint64_t value)
5380 {
5381     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5382     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5383     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5384 }
5385 
5386 /*
5387  * Return the effective value of HCR_EL2, at the given security state.
5388  * Bits that are not included here:
5389  * RW       (read from SCR_EL3.RW as needed)
5390  */
5391 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5392 {
5393     uint64_t ret = env->cp15.hcr_el2;
5394 
5395     assert(space != ARMSS_Root);
5396 
5397     if (!arm_is_el2_enabled_secstate(env, space)) {
5398         /*
5399          * "This register has no effect if EL2 is not enabled in the
5400          * current Security state".  This is ARMv8.4-SecEL2 speak for
5401          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5402          *
5403          * Prior to that, the language was "In an implementation that
5404          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5405          * as if this field is 0 for all purposes other than a direct
5406          * read or write access of HCR_EL2".  With lots of enumeration
5407          * on a per-field basis.  In current QEMU, this is condition
5408          * is arm_is_secure_below_el3.
5409          *
5410          * Since the v8.4 language applies to the entire register, and
5411          * appears to be backward compatible, use that.
5412          */
5413         return 0;
5414     }
5415 
5416     /*
5417      * For a cpu that supports both aarch64 and aarch32, we can set bits
5418      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5419      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5420      */
5421     if (!arm_el_is_aa64(env, 2)) {
5422         uint64_t aa32_valid;
5423 
5424         /*
5425          * These bits are up-to-date as of ARMv8.6.
5426          * For HCR, it's easiest to list just the 2 bits that are invalid.
5427          * For HCR2, list those that are valid.
5428          */
5429         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5430         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5431                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5432         ret &= aa32_valid;
5433     }
5434 
5435     if (ret & HCR_TGE) {
5436         /* These bits are up-to-date as of ARMv8.6.  */
5437         if (ret & HCR_E2H) {
5438             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5439                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5440                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5441                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5442                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5443                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5444         } else {
5445             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5446         }
5447         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5448                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5449                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5450                  HCR_TLOR);
5451     }
5452 
5453     return ret;
5454 }
5455 
5456 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5457 {
5458     if (arm_feature(env, ARM_FEATURE_M)) {
5459         return 0;
5460     }
5461     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5462 }
5463 
5464 /*
5465  * Corresponds to ARM pseudocode function ELIsInHost().
5466  */
5467 bool el_is_in_host(CPUARMState *env, int el)
5468 {
5469     uint64_t mask;
5470 
5471     /*
5472      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5473      * Perform the simplest bit tests first, and validate EL2 afterward.
5474      */
5475     if (el & 1) {
5476         return false; /* EL1 or EL3 */
5477     }
5478 
5479     /*
5480      * Note that hcr_write() checks isar_feature_aa64_vh(),
5481      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5482      */
5483     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5484     if ((env->cp15.hcr_el2 & mask) != mask) {
5485         return false;
5486     }
5487 
5488     /* TGE and/or E2H set: double check those bits are currently legal. */
5489     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5490 }
5491 
5492 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5493                        uint64_t value)
5494 {
5495     ARMCPU *cpu = env_archcpu(env);
5496     uint64_t valid_mask = 0;
5497 
5498     /* FEAT_MOPS adds MSCEn and MCE2 */
5499     if (cpu_isar_feature(aa64_mops, cpu)) {
5500         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5501     }
5502 
5503     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5504     if (cpu_isar_feature(aa64_nmi, cpu)) {
5505         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5506     }
5507     /* FEAT_CMOW adds CMOW */
5508     if (cpu_isar_feature(aa64_cmow, cpu)) {
5509         valid_mask |= HCRX_CMOW;
5510     }
5511     /* FEAT_XS adds FGTnXS, FnXS */
5512     if (cpu_isar_feature(aa64_xs, cpu)) {
5513         valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5514     }
5515 
5516     /* Clear RES0 bits.  */
5517     env->cp15.hcrx_el2 = value & valid_mask;
5518 
5519     /*
5520      * Updates to VINMI and VFNMI require us to update the status of
5521      * virtual NMI, which are the logical OR of these bits
5522      * and the state of the input lines from the GIC. (This requires
5523      * that we have the BQL, which is done by marking the
5524      * reginfo structs as ARM_CP_IO.)
5525      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5526      * possible for it to be taken immediately, because VINMI and
5527      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5528      * can only be written at EL2.
5529      */
5530     if (cpu_isar_feature(aa64_nmi, cpu)) {
5531         g_assert(bql_locked());
5532         arm_cpu_update_vinmi(cpu);
5533         arm_cpu_update_vfnmi(cpu);
5534     }
5535 }
5536 
5537 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5538                                   bool isread)
5539 {
5540     if (arm_current_el(env) == 2
5541         && arm_feature(env, ARM_FEATURE_EL3)
5542         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5543         return CP_ACCESS_TRAP_EL3;
5544     }
5545     return CP_ACCESS_OK;
5546 }
5547 
5548 static const ARMCPRegInfo hcrx_el2_reginfo = {
5549     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5550     .type = ARM_CP_IO,
5551     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5552     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5553     .nv2_redirect_offset = 0xa0,
5554     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5555 };
5556 
5557 /* Return the effective value of HCRX_EL2.  */
5558 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5559 {
5560     /*
5561      * The bits in this register behave as 0 for all purposes other than
5562      * direct reads of the register if SCR_EL3.HXEn is 0.
5563      * If EL2 is not enabled in the current security state, then the
5564      * bit may behave as if 0, or as if 1, depending on the bit.
5565      * For the moment, we treat the EL2-disabled case as taking
5566      * priority over the HXEn-disabled case. This is true for the only
5567      * bit for a feature which we implement where the answer is different
5568      * for the two cases (MSCEn for FEAT_MOPS).
5569      * This may need to be revisited for future bits.
5570      */
5571     if (!arm_is_el2_enabled(env)) {
5572         uint64_t hcrx = 0;
5573         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5574             /* MSCEn behaves as 1 if EL2 is not enabled */
5575             hcrx |= HCRX_MSCEN;
5576         }
5577         return hcrx;
5578     }
5579     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5580         return 0;
5581     }
5582     return env->cp15.hcrx_el2;
5583 }
5584 
5585 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5586                            uint64_t value)
5587 {
5588     /*
5589      * For A-profile AArch32 EL3, if NSACR.CP10
5590      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5591      */
5592     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5593         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5594         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5595         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5596     }
5597     env->cp15.cptr_el[2] = value;
5598 }
5599 
5600 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5601 {
5602     /*
5603      * For A-profile AArch32 EL3, if NSACR.CP10
5604      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5605      */
5606     uint64_t value = env->cp15.cptr_el[2];
5607 
5608     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5609         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5610         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5611     }
5612     return value;
5613 }
5614 
5615 static const ARMCPRegInfo el2_cp_reginfo[] = {
5616     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5617       .type = ARM_CP_IO,
5618       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5619       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5620       .nv2_redirect_offset = 0x78,
5621       .writefn = hcr_write, .raw_writefn = raw_write },
5622     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5623       .type = ARM_CP_ALIAS | ARM_CP_IO,
5624       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5625       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5626       .writefn = hcr_writelow },
5627     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5628       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5629       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5630     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5631       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5632       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5633       .access = PL2_RW,
5634       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5635     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5636       .type = ARM_CP_NV2_REDIRECT,
5637       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5638       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5639     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5640       .type = ARM_CP_NV2_REDIRECT,
5641       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5642       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5643     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5644       .type = ARM_CP_ALIAS,
5645       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5646       .access = PL2_RW,
5647       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5648     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5649       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5650       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5651       .access = PL2_RW,
5652       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5653     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5654       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5655       .access = PL2_RW, .writefn = vbar_write,
5656       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5657       .resetvalue = 0 },
5658     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5659       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5660       .access = PL3_RW, .type = ARM_CP_ALIAS,
5661       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5662     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5663       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5664       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5665       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5666       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5667     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5668       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5669       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5670       .resetvalue = 0 },
5671     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5672       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5673       .access = PL2_RW, .type = ARM_CP_ALIAS,
5674       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5675     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5676       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5677       .access = PL2_RW, .type = ARM_CP_CONST,
5678       .resetvalue = 0 },
5679     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5680     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5681       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5682       .access = PL2_RW, .type = ARM_CP_CONST,
5683       .resetvalue = 0 },
5684     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5685       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5686       .access = PL2_RW, .type = ARM_CP_CONST,
5687       .resetvalue = 0 },
5688     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5689       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5690       .access = PL2_RW, .type = ARM_CP_CONST,
5691       .resetvalue = 0 },
5692     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5693       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5694       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5695       .raw_writefn = raw_write,
5696       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5697     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5698       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5699       .type = ARM_CP_ALIAS,
5700       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5701       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5702     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5703       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5704       .access = PL2_RW,
5705       .nv2_redirect_offset = 0x40,
5706       /* no .writefn needed as this can't cause an ASID change */
5707       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5708     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5709       .cp = 15, .opc1 = 6, .crm = 2,
5710       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5711       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5712       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5713       .writefn = vttbr_write, .raw_writefn = raw_write },
5714     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5715       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5716       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5717       .nv2_redirect_offset = 0x20,
5718       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5719     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5720       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5721       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5722       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5723     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5724       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5725       .access = PL2_RW, .resetvalue = 0,
5726       .nv2_redirect_offset = 0x90,
5727       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5728     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5729       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5730       .access = PL2_RW, .resetvalue = 0,
5731       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5732       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5733     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5734       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5735       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5736 #ifndef CONFIG_USER_ONLY
5737     /*
5738      * Unlike the other EL2-related AT operations, these must
5739      * UNDEF from EL3 if EL2 is not implemented, which is why we
5740      * define them here rather than with the rest of the AT ops.
5741      */
5742     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5743       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5744       .access = PL2_W, .accessfn = at_s1e2_access,
5745       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5746       .writefn = ats_write64 },
5747     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5748       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5749       .access = PL2_W, .accessfn = at_s1e2_access,
5750       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5751       .writefn = ats_write64 },
5752     /*
5753      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5754      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5755      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5756      * to behave as if SCR.NS was 1.
5757      */
5758     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5759       .access = PL2_W,
5760       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5761     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5762       .access = PL2_W,
5763       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5764     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5765       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5766       /*
5767        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5768        * reset values as IMPDEF. We choose to reset to 3 to comply with
5769        * both ARMv7 and ARMv8.
5770        */
5771       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5772       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5773       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5774     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5775       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5776       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5777       .writefn = gt_cntvoff_write,
5778       .nv2_redirect_offset = 0x60,
5779       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5780     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5781       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5782       .writefn = gt_cntvoff_write,
5783       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5784     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5785       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5786       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5787       .type = ARM_CP_IO, .access = PL2_RW,
5788       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5789     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5790       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5791       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5792       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5793     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5794       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5795       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5796       .resetfn = gt_hyp_timer_reset,
5797       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5798     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5799       .type = ARM_CP_IO,
5800       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5801       .access = PL2_RW,
5802       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5803       .resetvalue = 0,
5804       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5805 #endif
5806     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5807       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5808       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5809       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5810     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5811       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5812       .access = PL2_RW,
5813       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5814     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5815       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5816       .access = PL2_RW,
5817       .nv2_redirect_offset = 0x80,
5818       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5819 };
5820 
5821 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5822     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5823       .type = ARM_CP_ALIAS | ARM_CP_IO,
5824       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5825       .access = PL2_RW,
5826       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5827       .writefn = hcr_writehigh },
5828 };
5829 
5830 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5831                                   bool isread)
5832 {
5833     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5834         return CP_ACCESS_OK;
5835     }
5836     return CP_ACCESS_UNDEFINED;
5837 }
5838 
5839 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5840     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5841       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5842       .access = PL2_RW, .accessfn = sel2_access,
5843       .nv2_redirect_offset = 0x30,
5844       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5845     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5846       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5847       .access = PL2_RW, .accessfn = sel2_access,
5848       .nv2_redirect_offset = 0x48,
5849       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5850 #ifndef CONFIG_USER_ONLY
5851     /* Secure EL2 Physical Timer */
5852     { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5853       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
5854       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5855       .accessfn = gt_sel2timer_access,
5856       .readfn = gt_sec_pel2_tval_read,
5857       .writefn = gt_sec_pel2_tval_write,
5858       .resetfn = gt_sec_pel2_timer_reset,
5859     },
5860     { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5861       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
5862       .type = ARM_CP_IO, .access = PL2_RW,
5863       .accessfn = gt_sel2timer_access,
5864       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
5865       .resetvalue = 0,
5866       .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
5867     },
5868     { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5869       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
5870       .type = ARM_CP_IO, .access = PL2_RW,
5871       .accessfn = gt_sel2timer_access,
5872       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
5873       .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
5874     },
5875     /* Secure EL2 Virtual Timer */
5876     { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5877       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
5878       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5879       .accessfn = gt_sel2timer_access,
5880       .readfn = gt_sec_vel2_tval_read,
5881       .writefn = gt_sec_vel2_tval_write,
5882       .resetfn = gt_sec_vel2_timer_reset,
5883     },
5884     { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5885       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
5886       .type = ARM_CP_IO, .access = PL2_RW,
5887       .accessfn = gt_sel2timer_access,
5888       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
5889       .resetvalue = 0,
5890       .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
5891     },
5892     { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5893       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
5894       .type = ARM_CP_IO, .access = PL2_RW,
5895       .accessfn = gt_sel2timer_access,
5896       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
5897       .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
5898     },
5899 #endif
5900 };
5901 
5902 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5903                                    bool isread)
5904 {
5905     /*
5906      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5907      * At Secure EL1 it traps to EL3 or EL2.
5908      */
5909     if (arm_current_el(env) == 3) {
5910         return CP_ACCESS_OK;
5911     }
5912     if (arm_is_secure_below_el3(env)) {
5913         if (env->cp15.scr_el3 & SCR_EEL2) {
5914             return CP_ACCESS_TRAP_EL2;
5915         }
5916         return CP_ACCESS_TRAP_EL3;
5917     }
5918     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5919     if (isread) {
5920         return CP_ACCESS_OK;
5921     }
5922     return CP_ACCESS_UNDEFINED;
5923 }
5924 
5925 static const ARMCPRegInfo el3_cp_reginfo[] = {
5926     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5927       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5928       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5929       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5930     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5931       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5932       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5933       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5934       .writefn = scr_write, .raw_writefn = raw_write },
5935     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5936       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5937       .access = PL3_RW, .resetvalue = 0,
5938       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5939     { .name = "SDER",
5940       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5941       .access = PL3_RW, .resetvalue = 0,
5942       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5943     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5944       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5945       .writefn = vbar_write, .resetvalue = 0,
5946       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5947     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5948       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5949       .access = PL3_RW, .resetvalue = 0,
5950       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5951     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5952       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5953       .access = PL3_RW,
5954       /* no .writefn needed as this can't cause an ASID change */
5955       .resetvalue = 0,
5956       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5957     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5958       .type = ARM_CP_ALIAS,
5959       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5960       .access = PL3_RW,
5961       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5962     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5963       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5964       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5965     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5966       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5967       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5968     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5969       .type = ARM_CP_ALIAS,
5970       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5971       .access = PL3_RW,
5972       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5973     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5974       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5975       .access = PL3_RW, .writefn = vbar_write,
5976       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5977       .resetvalue = 0 },
5978     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5979       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5980       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5981       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5982     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5983       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5984       .access = PL3_RW, .resetvalue = 0,
5985       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5986     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5987       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5988       .access = PL3_RW, .type = ARM_CP_CONST,
5989       .resetvalue = 0 },
5990     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5991       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5992       .access = PL3_RW, .type = ARM_CP_CONST,
5993       .resetvalue = 0 },
5994     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5995       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5996       .access = PL3_RW, .type = ARM_CP_CONST,
5997       .resetvalue = 0 },
5998 };
5999 
6000 #ifndef CONFIG_USER_ONLY
6001 
6002 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6003                                  bool isread)
6004 {
6005     if (arm_current_el(env) == 1) {
6006         /* This must be a FEAT_NV access */
6007         return CP_ACCESS_OK;
6008     }
6009     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6010         return CP_ACCESS_UNDEFINED;
6011     }
6012     return CP_ACCESS_OK;
6013 }
6014 
6015 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6016                                       bool isread)
6017 {
6018     if (arm_current_el(env) == 1) {
6019         /* This must be a FEAT_NV access with NVx == 101 */
6020         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6021             return CP_ACCESS_TRAP_EL2;
6022         }
6023     }
6024     return e2h_access(env, ri, isread);
6025 }
6026 
6027 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6028                                       bool isread)
6029 {
6030     if (arm_current_el(env) == 1) {
6031         /* This must be a FEAT_NV access with NVx == 101 */
6032         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6033             return CP_ACCESS_TRAP_EL2;
6034         }
6035     }
6036     return e2h_access(env, ri, isread);
6037 }
6038 
6039 /* Test if system register redirection is to occur in the current state.  */
6040 static bool redirect_for_e2h(CPUARMState *env)
6041 {
6042     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6043 }
6044 
6045 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6046 {
6047     CPReadFn *readfn;
6048 
6049     if (redirect_for_e2h(env)) {
6050         /* Switch to the saved EL2 version of the register.  */
6051         ri = ri->opaque;
6052         readfn = ri->readfn;
6053     } else {
6054         readfn = ri->orig_readfn;
6055     }
6056     if (readfn == NULL) {
6057         readfn = raw_read;
6058     }
6059     return readfn(env, ri);
6060 }
6061 
6062 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6063                           uint64_t value)
6064 {
6065     CPWriteFn *writefn;
6066 
6067     if (redirect_for_e2h(env)) {
6068         /* Switch to the saved EL2 version of the register.  */
6069         ri = ri->opaque;
6070         writefn = ri->writefn;
6071     } else {
6072         writefn = ri->orig_writefn;
6073     }
6074     if (writefn == NULL) {
6075         writefn = raw_write;
6076     }
6077     writefn(env, ri, value);
6078 }
6079 
6080 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6081 {
6082     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6083     return ri->orig_readfn(env, ri->opaque);
6084 }
6085 
6086 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6087                               uint64_t value)
6088 {
6089     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6090     return ri->orig_writefn(env, ri->opaque, value);
6091 }
6092 
6093 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6094                                          const ARMCPRegInfo *ri,
6095                                          bool isread)
6096 {
6097     if (arm_current_el(env) == 1) {
6098         /*
6099          * This must be a FEAT_NV access (will either trap or redirect
6100          * to memory). None of the registers with _EL12 aliases want to
6101          * apply their trap controls for this kind of access, so don't
6102          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6103          */
6104         return CP_ACCESS_OK;
6105     }
6106     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6107     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6108         return CP_ACCESS_UNDEFINED;
6109     }
6110     if (ri->orig_accessfn) {
6111         return ri->orig_accessfn(env, ri->opaque, isread);
6112     }
6113     return CP_ACCESS_OK;
6114 }
6115 
6116 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6117 {
6118     struct E2HAlias {
6119         uint32_t src_key, dst_key, new_key;
6120         const char *src_name, *dst_name, *new_name;
6121         bool (*feature)(const ARMISARegisters *id);
6122     };
6123 
6124 #define K(op0, op1, crn, crm, op2) \
6125     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6126 
6127     static const struct E2HAlias aliases[] = {
6128         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6129           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6130         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6131           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6132         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6133           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6134         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6135           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6136         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6137           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6138         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6139           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6140         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6141           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6142         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6143           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6144         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6145           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6146         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6147           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6148         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6149           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6150         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6151           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6152         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6153           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6154         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6155           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6156         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6157           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6158         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6159           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6160 
6161         /*
6162          * Note that redirection of ZCR is mentioned in the description
6163          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6164          * not in the summary table.
6165          */
6166         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6167           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6168         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6169           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6170 
6171         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6172           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6173 
6174         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6175           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6176           isar_feature_aa64_scxtnum },
6177 
6178         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6179         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6180     };
6181 #undef K
6182 
6183     size_t i;
6184 
6185     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6186         const struct E2HAlias *a = &aliases[i];
6187         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6188         bool ok;
6189 
6190         if (a->feature && !a->feature(&cpu->isar)) {
6191             continue;
6192         }
6193 
6194         src_reg = g_hash_table_lookup(cpu->cp_regs,
6195                                       (gpointer)(uintptr_t)a->src_key);
6196         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6197                                       (gpointer)(uintptr_t)a->dst_key);
6198         g_assert(src_reg != NULL);
6199         g_assert(dst_reg != NULL);
6200 
6201         /* Cross-compare names to detect typos in the keys.  */
6202         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6203         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6204 
6205         /* None of the core system registers use opaque; we will.  */
6206         g_assert(src_reg->opaque == NULL);
6207 
6208         /* Create alias before redirection so we dup the right data. */
6209         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6210 
6211         new_reg->name = a->new_name;
6212         new_reg->type |= ARM_CP_ALIAS;
6213         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6214         new_reg->access &= PL2_RW | PL3_RW;
6215         /* The new_reg op fields are as per new_key, not the target reg */
6216         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6217             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6218         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6219             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6220         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6221             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6222         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6223             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6224         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6225             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6226         new_reg->opaque = src_reg;
6227         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6228         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6229         new_reg->orig_accessfn = src_reg->accessfn;
6230         if (!new_reg->raw_readfn) {
6231             new_reg->raw_readfn = raw_read;
6232         }
6233         if (!new_reg->raw_writefn) {
6234             new_reg->raw_writefn = raw_write;
6235         }
6236         new_reg->readfn = el2_e2h_e12_read;
6237         new_reg->writefn = el2_e2h_e12_write;
6238         new_reg->accessfn = el2_e2h_e12_access;
6239 
6240         /*
6241          * If the _EL1 register is redirected to memory by FEAT_NV2,
6242          * then it shares the offset with the _EL12 register,
6243          * and which one is redirected depends on HCR_EL2.NV1.
6244          */
6245         if (new_reg->nv2_redirect_offset) {
6246             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6247             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6248             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6249         }
6250 
6251         ok = g_hash_table_insert(cpu->cp_regs,
6252                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6253         g_assert(ok);
6254 
6255         src_reg->opaque = dst_reg;
6256         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6257         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6258         if (!src_reg->raw_readfn) {
6259             src_reg->raw_readfn = raw_read;
6260         }
6261         if (!src_reg->raw_writefn) {
6262             src_reg->raw_writefn = raw_write;
6263         }
6264         src_reg->readfn = el2_e2h_read;
6265         src_reg->writefn = el2_e2h_write;
6266     }
6267 }
6268 #endif
6269 
6270 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6271                                      bool isread)
6272 {
6273     int cur_el = arm_current_el(env);
6274 
6275     if (cur_el < 2) {
6276         uint64_t hcr = arm_hcr_el2_eff(env);
6277 
6278         if (cur_el == 0) {
6279             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6280                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6281                     return CP_ACCESS_TRAP_EL2;
6282                 }
6283             } else {
6284                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6285                     return CP_ACCESS_TRAP_EL1;
6286                 }
6287                 if (hcr & HCR_TID2) {
6288                     return CP_ACCESS_TRAP_EL2;
6289                 }
6290             }
6291         } else if (hcr & HCR_TID2) {
6292             return CP_ACCESS_TRAP_EL2;
6293         }
6294     }
6295 
6296     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6297         return CP_ACCESS_TRAP_EL2;
6298     }
6299 
6300     return CP_ACCESS_OK;
6301 }
6302 
6303 /*
6304  * Check for traps to RAS registers, which are controlled
6305  * by HCR_EL2.TERR and SCR_EL3.TERR.
6306  */
6307 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6308                                   bool isread)
6309 {
6310     int el = arm_current_el(env);
6311 
6312     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6313         return CP_ACCESS_TRAP_EL2;
6314     }
6315     if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
6316         return CP_ACCESS_TRAP_EL3;
6317     }
6318     return CP_ACCESS_OK;
6319 }
6320 
6321 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6322 {
6323     int el = arm_current_el(env);
6324 
6325     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6326         return env->cp15.vdisr_el2;
6327     }
6328     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6329         return 0; /* RAZ/WI */
6330     }
6331     return env->cp15.disr_el1;
6332 }
6333 
6334 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6335 {
6336     int el = arm_current_el(env);
6337 
6338     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6339         env->cp15.vdisr_el2 = val;
6340         return;
6341     }
6342     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6343         return; /* RAZ/WI */
6344     }
6345     env->cp15.disr_el1 = val;
6346 }
6347 
6348 /*
6349  * Minimal RAS implementation with no Error Records.
6350  * Which means that all of the Error Record registers:
6351  *   ERXADDR_EL1
6352  *   ERXCTLR_EL1
6353  *   ERXFR_EL1
6354  *   ERXMISC0_EL1
6355  *   ERXMISC1_EL1
6356  *   ERXMISC2_EL1
6357  *   ERXMISC3_EL1
6358  *   ERXPFGCDN_EL1  (RASv1p1)
6359  *   ERXPFGCTL_EL1  (RASv1p1)
6360  *   ERXPFGF_EL1    (RASv1p1)
6361  *   ERXSTATUS_EL1
6362  * and
6363  *   ERRSELR_EL1
6364  * may generate UNDEFINED, which is the effect we get by not
6365  * listing them at all.
6366  *
6367  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6368  * is higher priority than FGT-to-EL2 so we do not need to list them
6369  * in order to check for an FGT.
6370  */
6371 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6372     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6373       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6374       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6375       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6376     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6377       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6378       .access = PL1_R, .accessfn = access_terr,
6379       .fgt = FGT_ERRIDR_EL1,
6380       .type = ARM_CP_CONST, .resetvalue = 0 },
6381     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6382       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6383       .nv2_redirect_offset = 0x500,
6384       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6385     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6386       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6387       .nv2_redirect_offset = 0x508,
6388       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6389 };
6390 
6391 /*
6392  * Return the exception level to which exceptions should be taken
6393  * via SVEAccessTrap.  This excludes the check for whether the exception
6394  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6395  * be found by testing 0 < fp_exception_el < sve_exception_el.
6396  *
6397  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6398  * pseudocode does *not* separate out the FP trap checks, but has them
6399  * all in one function.
6400  */
6401 int sve_exception_el(CPUARMState *env, int el)
6402 {
6403 #ifndef CONFIG_USER_ONLY
6404     if (el <= 1 && !el_is_in_host(env, el)) {
6405         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6406         case 1:
6407             if (el != 0) {
6408                 break;
6409             }
6410             /* fall through */
6411         case 0:
6412         case 2:
6413             return 1;
6414         }
6415     }
6416 
6417     if (el <= 2 && arm_is_el2_enabled(env)) {
6418         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6419         if (env->cp15.hcr_el2 & HCR_E2H) {
6420             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6421             case 1:
6422                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6423                     break;
6424                 }
6425                 /* fall through */
6426             case 0:
6427             case 2:
6428                 return 2;
6429             }
6430         } else {
6431             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6432                 return 2;
6433             }
6434         }
6435     }
6436 
6437     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6438     if (arm_feature(env, ARM_FEATURE_EL3)
6439         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6440         return 3;
6441     }
6442 #endif
6443     return 0;
6444 }
6445 
6446 /*
6447  * Return the exception level to which exceptions should be taken for SME.
6448  * C.f. the ARM pseudocode function CheckSMEAccess.
6449  */
6450 int sme_exception_el(CPUARMState *env, int el)
6451 {
6452 #ifndef CONFIG_USER_ONLY
6453     if (el <= 1 && !el_is_in_host(env, el)) {
6454         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6455         case 1:
6456             if (el != 0) {
6457                 break;
6458             }
6459             /* fall through */
6460         case 0:
6461         case 2:
6462             return 1;
6463         }
6464     }
6465 
6466     if (el <= 2 && arm_is_el2_enabled(env)) {
6467         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6468         if (env->cp15.hcr_el2 & HCR_E2H) {
6469             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6470             case 1:
6471                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6472                     break;
6473                 }
6474                 /* fall through */
6475             case 0:
6476             case 2:
6477                 return 2;
6478             }
6479         } else {
6480             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6481                 return 2;
6482             }
6483         }
6484     }
6485 
6486     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6487     if (arm_feature(env, ARM_FEATURE_EL3)
6488         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6489         return 3;
6490     }
6491 #endif
6492     return 0;
6493 }
6494 
6495 /*
6496  * Given that SVE is enabled, return the vector length for EL.
6497  */
6498 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6499 {
6500     ARMCPU *cpu = env_archcpu(env);
6501     uint64_t *cr = env->vfp.zcr_el;
6502     uint32_t map = cpu->sve_vq.map;
6503     uint32_t len = ARM_MAX_VQ - 1;
6504 
6505     if (sm) {
6506         cr = env->vfp.smcr_el;
6507         map = cpu->sme_vq.map;
6508     }
6509 
6510     if (el <= 1 && !el_is_in_host(env, el)) {
6511         len = MIN(len, 0xf & (uint32_t)cr[1]);
6512     }
6513     if (el <= 2 && arm_is_el2_enabled(env)) {
6514         len = MIN(len, 0xf & (uint32_t)cr[2]);
6515     }
6516     if (arm_feature(env, ARM_FEATURE_EL3)) {
6517         len = MIN(len, 0xf & (uint32_t)cr[3]);
6518     }
6519 
6520     map &= MAKE_64BIT_MASK(0, len + 1);
6521     if (map != 0) {
6522         return 31 - clz32(map);
6523     }
6524 
6525     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6526     assert(sm);
6527     return ctz32(cpu->sme_vq.map);
6528 }
6529 
6530 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6531 {
6532     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6533 }
6534 
6535 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6536                       uint64_t value)
6537 {
6538     int cur_el = arm_current_el(env);
6539     int old_len = sve_vqm1_for_el(env, cur_el);
6540     int new_len;
6541 
6542     /* Bits other than [3:0] are RAZ/WI.  */
6543     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6544     raw_write(env, ri, value & 0xf);
6545 
6546     /*
6547      * Because we arrived here, we know both FP and SVE are enabled;
6548      * otherwise we would have trapped access to the ZCR_ELn register.
6549      */
6550     new_len = sve_vqm1_for_el(env, cur_el);
6551     if (new_len < old_len) {
6552         aarch64_sve_narrow_vq(env, new_len + 1);
6553     }
6554 }
6555 
6556 static const ARMCPRegInfo zcr_reginfo[] = {
6557     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6558       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6559       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6560       .access = PL1_RW, .type = ARM_CP_SVE,
6561       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6562       .writefn = zcr_write, .raw_writefn = raw_write },
6563     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6564       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6565       .access = PL2_RW, .type = ARM_CP_SVE,
6566       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6567       .writefn = zcr_write, .raw_writefn = raw_write },
6568     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6569       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6570       .access = PL3_RW, .type = ARM_CP_SVE,
6571       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6572       .writefn = zcr_write, .raw_writefn = raw_write },
6573 };
6574 
6575 #ifdef TARGET_AARCH64
6576 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6577                                     bool isread)
6578 {
6579     int el = arm_current_el(env);
6580 
6581     if (el == 0) {
6582         uint64_t sctlr = arm_sctlr(env, el);
6583         if (!(sctlr & SCTLR_EnTP2)) {
6584             return CP_ACCESS_TRAP_EL1;
6585         }
6586     }
6587     /* TODO: FEAT_FGT */
6588     if (el < 3
6589         && arm_feature(env, ARM_FEATURE_EL3)
6590         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6591         return CP_ACCESS_TRAP_EL3;
6592     }
6593     return CP_ACCESS_OK;
6594 }
6595 
6596 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6597                                       bool isread)
6598 {
6599     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6600     if (arm_current_el(env) == 2
6601         && arm_feature(env, ARM_FEATURE_EL3)
6602         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6603         return CP_ACCESS_TRAP_EL3;
6604     }
6605     return CP_ACCESS_OK;
6606 }
6607 
6608 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6609                                    bool isread)
6610 {
6611     if (arm_current_el(env) < 3
6612         && arm_feature(env, ARM_FEATURE_EL3)
6613         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6614         return CP_ACCESS_TRAP_EL3;
6615     }
6616     return CP_ACCESS_OK;
6617 }
6618 
6619 /* ResetSVEState */
6620 static void arm_reset_sve_state(CPUARMState *env)
6621 {
6622     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6623     /* Recall that FFR is stored as pregs[16]. */
6624     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6625     vfp_set_fpsr(env, 0x0800009f);
6626 }
6627 
6628 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6629 {
6630     uint64_t change = (env->svcr ^ new) & mask;
6631 
6632     if (change == 0) {
6633         return;
6634     }
6635     env->svcr ^= change;
6636 
6637     if (change & R_SVCR_SM_MASK) {
6638         arm_reset_sve_state(env);
6639     }
6640 
6641     /*
6642      * ResetSMEState.
6643      *
6644      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6645      * on enable: while disabled, the storage is inaccessible and the
6646      * value does not matter.  We're not saving the storage in vmstate
6647      * when disabled either.
6648      */
6649     if (change & new & R_SVCR_ZA_MASK) {
6650         memset(env->zarray, 0, sizeof(env->zarray));
6651     }
6652 
6653     if (tcg_enabled()) {
6654         arm_rebuild_hflags(env);
6655     }
6656 }
6657 
6658 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6659                        uint64_t value)
6660 {
6661     aarch64_set_svcr(env, value, -1);
6662 }
6663 
6664 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6665                        uint64_t value)
6666 {
6667     int cur_el = arm_current_el(env);
6668     int old_len = sve_vqm1_for_el(env, cur_el);
6669     int new_len;
6670 
6671     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6672     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6673     raw_write(env, ri, value);
6674 
6675     /*
6676      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6677      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6678      * current values for simplicity.  But for QEMU internals, we must still
6679      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6680      * above aarch64_sve_narrow_vq.
6681      */
6682     new_len = sve_vqm1_for_el(env, cur_el);
6683     if (new_len < old_len) {
6684         aarch64_sve_narrow_vq(env, new_len + 1);
6685     }
6686 }
6687 
6688 static const ARMCPRegInfo sme_reginfo[] = {
6689     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6690       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6691       .access = PL0_RW, .accessfn = access_tpidr2,
6692       .fgt = FGT_NTPIDR2_EL0,
6693       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6694     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6695       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6696       .access = PL0_RW, .type = ARM_CP_SME,
6697       .fieldoffset = offsetof(CPUARMState, svcr),
6698       .writefn = svcr_write, .raw_writefn = raw_write },
6699     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6700       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6701       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6702       .access = PL1_RW, .type = ARM_CP_SME,
6703       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6704       .writefn = smcr_write, .raw_writefn = raw_write },
6705     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6706       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6707       .access = PL2_RW, .type = ARM_CP_SME,
6708       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6709       .writefn = smcr_write, .raw_writefn = raw_write },
6710     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6711       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6712       .access = PL3_RW, .type = ARM_CP_SME,
6713       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6714       .writefn = smcr_write, .raw_writefn = raw_write },
6715     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6716       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6717       .access = PL1_R, .accessfn = access_aa64_tid1,
6718       /*
6719        * IMPLEMENTOR = 0 (software)
6720        * REVISION    = 0 (implementation defined)
6721        * SMPS        = 0 (no streaming execution priority in QEMU)
6722        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6723        */
6724       .type = ARM_CP_CONST, .resetvalue = 0, },
6725     /*
6726      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6727      */
6728     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6729       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6730       .access = PL1_RW, .accessfn = access_smpri,
6731       .fgt = FGT_NSMPRI_EL1,
6732       .type = ARM_CP_CONST, .resetvalue = 0 },
6733     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6734       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6735       .nv2_redirect_offset = 0x1f8,
6736       .access = PL2_RW, .accessfn = access_smprimap,
6737       .type = ARM_CP_CONST, .resetvalue = 0 },
6738 };
6739 
6740 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6741                         uint64_t value)
6742 {
6743     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6744     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6745         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6746         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6747 
6748     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6749 }
6750 
6751 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6752 {
6753     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6754                                      env_archcpu(env)->reset_l0gptsz);
6755 }
6756 
6757 static const ARMCPRegInfo rme_reginfo[] = {
6758     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6759       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6760       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6761       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6762     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6763       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6764       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6765     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6766       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6767       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6768     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6769       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6770       .access = PL3_W, .type = ARM_CP_NOP },
6771 };
6772 
6773 static const ARMCPRegInfo rme_mte_reginfo[] = {
6774     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6775       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6776       .access = PL3_W, .type = ARM_CP_NOP },
6777 };
6778 
6779 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6780                               uint64_t value)
6781 {
6782     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6783 }
6784 
6785 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6786 {
6787     return env->pstate & PSTATE_ALLINT;
6788 }
6789 
6790 static CPAccessResult aa64_allint_access(CPUARMState *env,
6791                                          const ARMCPRegInfo *ri, bool isread)
6792 {
6793     if (!isread && arm_current_el(env) == 1 &&
6794         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6795         return CP_ACCESS_TRAP_EL2;
6796     }
6797     return CP_ACCESS_OK;
6798 }
6799 
6800 static const ARMCPRegInfo nmi_reginfo[] = {
6801     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6802       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6803       .type = ARM_CP_NO_RAW,
6804       .access = PL1_RW, .accessfn = aa64_allint_access,
6805       .fieldoffset = offsetof(CPUARMState, pstate),
6806       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6807       .resetfn = arm_cp_reset_ignore },
6808 };
6809 #endif /* TARGET_AARCH64 */
6810 
6811 static void define_pmu_regs(ARMCPU *cpu)
6812 {
6813     /*
6814      * v7 performance monitor control register: same implementor
6815      * field as main ID register, and we implement four counters in
6816      * addition to the cycle count register.
6817      */
6818     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6819     ARMCPRegInfo pmcr = {
6820         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6821         .access = PL0_RW,
6822         .fgt = FGT_PMCR_EL0,
6823         .type = ARM_CP_IO | ARM_CP_ALIAS,
6824         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6825         .accessfn = pmreg_access,
6826         .readfn = pmcr_read, .raw_readfn = raw_read,
6827         .writefn = pmcr_write, .raw_writefn = raw_write,
6828     };
6829     ARMCPRegInfo pmcr64 = {
6830         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6831         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6832         .access = PL0_RW, .accessfn = pmreg_access,
6833         .fgt = FGT_PMCR_EL0,
6834         .type = ARM_CP_IO,
6835         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6836         .resetvalue = cpu->isar.reset_pmcr_el0,
6837         .readfn = pmcr_read, .raw_readfn = raw_read,
6838         .writefn = pmcr_write, .raw_writefn = raw_write,
6839     };
6840 
6841     define_one_arm_cp_reg(cpu, &pmcr);
6842     define_one_arm_cp_reg(cpu, &pmcr64);
6843     for (i = 0; i < pmcrn; i++) {
6844         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6845         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6846         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6847         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6848         ARMCPRegInfo pmev_regs[] = {
6849             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6850               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6851               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6852               .fgt = FGT_PMEVCNTRN_EL0,
6853               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6854               .accessfn = pmreg_access_xevcntr },
6855             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6856               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6857               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6858               .type = ARM_CP_IO,
6859               .fgt = FGT_PMEVCNTRN_EL0,
6860               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6861               .raw_readfn = pmevcntr_rawread,
6862               .raw_writefn = pmevcntr_rawwrite },
6863             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6864               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6865               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6866               .fgt = FGT_PMEVTYPERN_EL0,
6867               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6868               .accessfn = pmreg_access },
6869             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6870               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6871               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6872               .fgt = FGT_PMEVTYPERN_EL0,
6873               .type = ARM_CP_IO,
6874               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6875               .raw_writefn = pmevtyper_rawwrite },
6876         };
6877         define_arm_cp_regs(cpu, pmev_regs);
6878         g_free(pmevcntr_name);
6879         g_free(pmevcntr_el0_name);
6880         g_free(pmevtyper_name);
6881         g_free(pmevtyper_el0_name);
6882     }
6883     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6884         ARMCPRegInfo v81_pmu_regs[] = {
6885             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6886               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6887               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6888               .fgt = FGT_PMCEIDN_EL0,
6889               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6890             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6891               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6892               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6893               .fgt = FGT_PMCEIDN_EL0,
6894               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6895         };
6896         define_arm_cp_regs(cpu, v81_pmu_regs);
6897     }
6898     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6899         static const ARMCPRegInfo v84_pmmir = {
6900             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6901             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6902             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6903             .fgt = FGT_PMMIR_EL1,
6904             .resetvalue = 0
6905         };
6906         define_one_arm_cp_reg(cpu, &v84_pmmir);
6907     }
6908 }
6909 
6910 #ifndef CONFIG_USER_ONLY
6911 /*
6912  * We don't know until after realize whether there's a GICv3
6913  * attached, and that is what registers the gicv3 sysregs.
6914  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6915  * at runtime.
6916  */
6917 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6918 {
6919     ARMCPU *cpu = env_archcpu(env);
6920     uint64_t pfr1 = cpu->isar.id_pfr1;
6921 
6922     if (env->gicv3state) {
6923         pfr1 |= 1 << 28;
6924     }
6925     return pfr1;
6926 }
6927 
6928 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6929 {
6930     ARMCPU *cpu = env_archcpu(env);
6931     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6932 
6933     if (env->gicv3state) {
6934         pfr0 |= 1 << 24;
6935     }
6936     return pfr0;
6937 }
6938 #endif
6939 
6940 /*
6941  * Shared logic between LORID and the rest of the LOR* registers.
6942  * Secure state exclusion has already been dealt with.
6943  */
6944 static CPAccessResult access_lor_ns(CPUARMState *env,
6945                                     const ARMCPRegInfo *ri, bool isread)
6946 {
6947     int el = arm_current_el(env);
6948 
6949     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6950         return CP_ACCESS_TRAP_EL2;
6951     }
6952     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6953         return CP_ACCESS_TRAP_EL3;
6954     }
6955     return CP_ACCESS_OK;
6956 }
6957 
6958 static CPAccessResult access_lor_other(CPUARMState *env,
6959                                        const ARMCPRegInfo *ri, bool isread)
6960 {
6961     if (arm_is_secure_below_el3(env)) {
6962         /* UNDEF if SCR_EL3.NS == 0 */
6963         return CP_ACCESS_UNDEFINED;
6964     }
6965     return access_lor_ns(env, ri, isread);
6966 }
6967 
6968 /*
6969  * A trivial implementation of ARMv8.1-LOR leaves all of these
6970  * registers fixed at 0, which indicates that there are zero
6971  * supported Limited Ordering regions.
6972  */
6973 static const ARMCPRegInfo lor_reginfo[] = {
6974     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6975       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6976       .access = PL1_RW, .accessfn = access_lor_other,
6977       .fgt = FGT_LORSA_EL1,
6978       .type = ARM_CP_CONST, .resetvalue = 0 },
6979     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6980       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6981       .access = PL1_RW, .accessfn = access_lor_other,
6982       .fgt = FGT_LOREA_EL1,
6983       .type = ARM_CP_CONST, .resetvalue = 0 },
6984     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6985       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6986       .access = PL1_RW, .accessfn = access_lor_other,
6987       .fgt = FGT_LORN_EL1,
6988       .type = ARM_CP_CONST, .resetvalue = 0 },
6989     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6990       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6991       .access = PL1_RW, .accessfn = access_lor_other,
6992       .fgt = FGT_LORC_EL1,
6993       .type = ARM_CP_CONST, .resetvalue = 0 },
6994     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6995       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6996       .access = PL1_R, .accessfn = access_lor_ns,
6997       .fgt = FGT_LORID_EL1,
6998       .type = ARM_CP_CONST, .resetvalue = 0 },
6999 };
7000 
7001 #ifdef TARGET_AARCH64
7002 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7003                                    bool isread)
7004 {
7005     int el = arm_current_el(env);
7006 
7007     if (el < 2 &&
7008         arm_is_el2_enabled(env) &&
7009         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7010         return CP_ACCESS_TRAP_EL2;
7011     }
7012     if (el < 3 &&
7013         arm_feature(env, ARM_FEATURE_EL3) &&
7014         !(env->cp15.scr_el3 & SCR_APK)) {
7015         return CP_ACCESS_TRAP_EL3;
7016     }
7017     return CP_ACCESS_OK;
7018 }
7019 
7020 static const ARMCPRegInfo pauth_reginfo[] = {
7021     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7022       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7023       .access = PL1_RW, .accessfn = access_pauth,
7024       .fgt = FGT_APDAKEY,
7025       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7026     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7027       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7028       .access = PL1_RW, .accessfn = access_pauth,
7029       .fgt = FGT_APDAKEY,
7030       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7031     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7032       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7033       .access = PL1_RW, .accessfn = access_pauth,
7034       .fgt = FGT_APDBKEY,
7035       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7036     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7038       .access = PL1_RW, .accessfn = access_pauth,
7039       .fgt = FGT_APDBKEY,
7040       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7041     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7042       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7043       .access = PL1_RW, .accessfn = access_pauth,
7044       .fgt = FGT_APGAKEY,
7045       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7046     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7048       .access = PL1_RW, .accessfn = access_pauth,
7049       .fgt = FGT_APGAKEY,
7050       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7051     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7052       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7053       .access = PL1_RW, .accessfn = access_pauth,
7054       .fgt = FGT_APIAKEY,
7055       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7056     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7058       .access = PL1_RW, .accessfn = access_pauth,
7059       .fgt = FGT_APIAKEY,
7060       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7061     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7062       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7063       .access = PL1_RW, .accessfn = access_pauth,
7064       .fgt = FGT_APIBKEY,
7065       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7066     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7068       .access = PL1_RW, .accessfn = access_pauth,
7069       .fgt = FGT_APIBKEY,
7070       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7071 };
7072 
7073 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7074 {
7075     Error *err = NULL;
7076     uint64_t ret;
7077 
7078     /* Success sets NZCV = 0000.  */
7079     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7080 
7081     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7082         /*
7083          * ??? Failed, for unknown reasons in the crypto subsystem.
7084          * The best we can do is log the reason and return the
7085          * timed-out indication to the guest.  There is no reason
7086          * we know to expect this failure to be transitory, so the
7087          * guest may well hang retrying the operation.
7088          */
7089         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7090                       ri->name, error_get_pretty(err));
7091         error_free(err);
7092 
7093         env->ZF = 0; /* NZCF = 0100 */
7094         return 0;
7095     }
7096     return ret;
7097 }
7098 
7099 /* We do not support re-seeding, so the two registers operate the same.  */
7100 static const ARMCPRegInfo rndr_reginfo[] = {
7101     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7102       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7103       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7104       .access = PL0_R, .readfn = rndr_readfn },
7105     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7106       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7107       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7108       .access = PL0_R, .readfn = rndr_readfn },
7109 };
7110 
7111 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7112                           uint64_t value)
7113 {
7114 #ifdef CONFIG_TCG
7115     ARMCPU *cpu = env_archcpu(env);
7116     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7117     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7118     uint64_t vaddr_in = (uint64_t) value;
7119     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7120     void *haddr;
7121     int mem_idx = arm_env_mmu_index(env);
7122 
7123     /* This won't be crossing page boundaries */
7124     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7125     if (haddr) {
7126 #ifndef CONFIG_USER_ONLY
7127 
7128         ram_addr_t offset;
7129         MemoryRegion *mr;
7130 
7131         /* RCU lock is already being held */
7132         mr = memory_region_from_host(haddr, &offset);
7133 
7134         if (mr) {
7135             memory_region_writeback(mr, offset, dline_size);
7136         }
7137 #endif /*CONFIG_USER_ONLY*/
7138     }
7139 #else
7140     /* Handled by hardware accelerator. */
7141     g_assert_not_reached();
7142 #endif /* CONFIG_TCG */
7143 }
7144 
7145 static const ARMCPRegInfo dcpop_reg[] = {
7146     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7147       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7148       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7149       .fgt = FGT_DCCVAP,
7150       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7151 };
7152 
7153 static const ARMCPRegInfo dcpodp_reg[] = {
7154     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7155       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7156       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7157       .fgt = FGT_DCCVADP,
7158       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7159 };
7160 
7161 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7162                                        bool isread)
7163 {
7164     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7165         return CP_ACCESS_TRAP_EL2;
7166     }
7167 
7168     return CP_ACCESS_OK;
7169 }
7170 
7171 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7172                                  bool isread)
7173 {
7174     int el = arm_current_el(env);
7175     if (el < 2 && arm_is_el2_enabled(env)) {
7176         uint64_t hcr = arm_hcr_el2_eff(env);
7177         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7178             return CP_ACCESS_TRAP_EL2;
7179         }
7180     }
7181     if (el < 3 &&
7182         arm_feature(env, ARM_FEATURE_EL3) &&
7183         !(env->cp15.scr_el3 & SCR_ATA)) {
7184         return CP_ACCESS_TRAP_EL3;
7185     }
7186     return CP_ACCESS_OK;
7187 }
7188 
7189 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7190                                       bool isread)
7191 {
7192     CPAccessResult nv1 = access_nv1(env, ri, isread);
7193 
7194     if (nv1 != CP_ACCESS_OK) {
7195         return nv1;
7196     }
7197     return access_mte(env, ri, isread);
7198 }
7199 
7200 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7201                                       bool isread)
7202 {
7203     /*
7204      * TFSR_EL2: similar to generic access_mte(), but we need to
7205      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7206      * if NV2 is enabled then we will redirect this to TFSR_EL1
7207      * after doing the HCR and SCR ATA traps; otherwise this will
7208      * be a trap to EL2 and the HCR/SCR traps do not apply.
7209      */
7210     int el = arm_current_el(env);
7211 
7212     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7213         return CP_ACCESS_OK;
7214     }
7215     if (el < 2 && arm_is_el2_enabled(env)) {
7216         uint64_t hcr = arm_hcr_el2_eff(env);
7217         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7218             return CP_ACCESS_TRAP_EL2;
7219         }
7220     }
7221     if (el < 3 &&
7222         arm_feature(env, ARM_FEATURE_EL3) &&
7223         !(env->cp15.scr_el3 & SCR_ATA)) {
7224         return CP_ACCESS_TRAP_EL3;
7225     }
7226     return CP_ACCESS_OK;
7227 }
7228 
7229 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7230 {
7231     return env->pstate & PSTATE_TCO;
7232 }
7233 
7234 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7235 {
7236     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7237 }
7238 
7239 static const ARMCPRegInfo mte_reginfo[] = {
7240     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7241       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7242       .access = PL1_RW, .accessfn = access_mte,
7243       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7244     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7245       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7246       .access = PL1_RW, .accessfn = access_tfsr_el1,
7247       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7248       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7249     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7250       .type = ARM_CP_NV2_REDIRECT,
7251       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7252       .access = PL2_RW, .accessfn = access_tfsr_el2,
7253       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7254     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7255       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7256       .access = PL3_RW,
7257       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7258     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7259       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7260       .access = PL1_RW, .accessfn = access_mte,
7261       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7262     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7263       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7264       .access = PL1_RW, .accessfn = access_mte,
7265       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7266     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7267       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7268       .type = ARM_CP_NO_RAW,
7269       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7270     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7271       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7272       .type = ARM_CP_NOP, .access = PL1_W,
7273       .fgt = FGT_DCIVAC,
7274       .accessfn = aa64_cacheop_poc_access },
7275     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7276       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7277       .fgt = FGT_DCISW,
7278       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7279     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7280       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7281       .type = ARM_CP_NOP, .access = PL1_W,
7282       .fgt = FGT_DCIVAC,
7283       .accessfn = aa64_cacheop_poc_access },
7284     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7285       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7286       .fgt = FGT_DCISW,
7287       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7288     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7290       .fgt = FGT_DCCSW,
7291       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7292     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7293       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7294       .fgt = FGT_DCCSW,
7295       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7296     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7297       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7298       .fgt = FGT_DCCISW,
7299       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7300     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7301       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7302       .fgt = FGT_DCCISW,
7303       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7304 };
7305 
7306 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7307     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7308       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7309       .type = ARM_CP_CONST, .access = PL0_RW, },
7310 };
7311 
7312 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7313     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7314       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7315       .type = ARM_CP_NOP, .access = PL0_W,
7316       .fgt = FGT_DCCVAC,
7317       .accessfn = aa64_cacheop_poc_access },
7318     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7319       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7320       .type = ARM_CP_NOP, .access = PL0_W,
7321       .fgt = FGT_DCCVAC,
7322       .accessfn = aa64_cacheop_poc_access },
7323     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7324       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7325       .type = ARM_CP_NOP, .access = PL0_W,
7326       .fgt = FGT_DCCVAP,
7327       .accessfn = aa64_cacheop_poc_access },
7328     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7329       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7330       .type = ARM_CP_NOP, .access = PL0_W,
7331       .fgt = FGT_DCCVAP,
7332       .accessfn = aa64_cacheop_poc_access },
7333     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7334       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7335       .type = ARM_CP_NOP, .access = PL0_W,
7336       .fgt = FGT_DCCVADP,
7337       .accessfn = aa64_cacheop_poc_access },
7338     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7339       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7340       .type = ARM_CP_NOP, .access = PL0_W,
7341       .fgt = FGT_DCCVADP,
7342       .accessfn = aa64_cacheop_poc_access },
7343     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7344       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7345       .type = ARM_CP_NOP, .access = PL0_W,
7346       .fgt = FGT_DCCIVAC,
7347       .accessfn = aa64_cacheop_poc_access },
7348     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7349       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7350       .type = ARM_CP_NOP, .access = PL0_W,
7351       .fgt = FGT_DCCIVAC,
7352       .accessfn = aa64_cacheop_poc_access },
7353     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7354       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7355       .access = PL0_W, .type = ARM_CP_DC_GVA,
7356 #ifndef CONFIG_USER_ONLY
7357       /* Avoid overhead of an access check that always passes in user-mode */
7358       .accessfn = aa64_zva_access,
7359       .fgt = FGT_DCZVA,
7360 #endif
7361     },
7362     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7363       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7364       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7365 #ifndef CONFIG_USER_ONLY
7366       /* Avoid overhead of an access check that always passes in user-mode */
7367       .accessfn = aa64_zva_access,
7368       .fgt = FGT_DCZVA,
7369 #endif
7370     },
7371 };
7372 
7373 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7374                                      bool isread)
7375 {
7376     uint64_t hcr = arm_hcr_el2_eff(env);
7377     int el = arm_current_el(env);
7378 
7379     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7380         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7381             if (hcr & HCR_TGE) {
7382                 return CP_ACCESS_TRAP_EL2;
7383             }
7384             return CP_ACCESS_TRAP_EL1;
7385         }
7386     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7387         return CP_ACCESS_TRAP_EL2;
7388     }
7389     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7390         return CP_ACCESS_TRAP_EL2;
7391     }
7392     if (el < 3
7393         && arm_feature(env, ARM_FEATURE_EL3)
7394         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7395         return CP_ACCESS_TRAP_EL3;
7396     }
7397     return CP_ACCESS_OK;
7398 }
7399 
7400 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7401                                          const ARMCPRegInfo *ri,
7402                                          bool isread)
7403 {
7404     CPAccessResult nv1 = access_nv1(env, ri, isread);
7405 
7406     if (nv1 != CP_ACCESS_OK) {
7407         return nv1;
7408     }
7409     return access_scxtnum(env, ri, isread);
7410 }
7411 
7412 static const ARMCPRegInfo scxtnum_reginfo[] = {
7413     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7414       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7415       .access = PL0_RW, .accessfn = access_scxtnum,
7416       .fgt = FGT_SCXTNUM_EL0,
7417       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7418     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7419       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7420       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7421       .fgt = FGT_SCXTNUM_EL1,
7422       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7423       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7424     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7425       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7426       .access = PL2_RW, .accessfn = access_scxtnum,
7427       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7428     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7429       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7430       .access = PL3_RW,
7431       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7432 };
7433 
7434 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7435                                  bool isread)
7436 {
7437     if (arm_current_el(env) == 2 &&
7438         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7439         return CP_ACCESS_TRAP_EL3;
7440     }
7441     return CP_ACCESS_OK;
7442 }
7443 
7444 static const ARMCPRegInfo fgt_reginfo[] = {
7445     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7446       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7447       .nv2_redirect_offset = 0x1b8,
7448       .access = PL2_RW, .accessfn = access_fgt,
7449       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7450     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7451       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7452       .nv2_redirect_offset = 0x1c0,
7453       .access = PL2_RW, .accessfn = access_fgt,
7454       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7455     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7456       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7457       .nv2_redirect_offset = 0x1d0,
7458       .access = PL2_RW, .accessfn = access_fgt,
7459       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7460     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7461       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7462       .nv2_redirect_offset = 0x1d8,
7463       .access = PL2_RW, .accessfn = access_fgt,
7464       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7465     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7466       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7467       .nv2_redirect_offset = 0x1c8,
7468       .access = PL2_RW, .accessfn = access_fgt,
7469       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7470 };
7471 
7472 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7473                        uint64_t value)
7474 {
7475     /*
7476      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7477      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7478      * about the RESS bits at the top -- we choose the "generate an EL2
7479      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7480      * the ptw.c code detect the resulting invalid address).
7481      */
7482     env->cp15.vncr_el2 = value & ~0xfffULL;
7483 }
7484 
7485 static const ARMCPRegInfo nv2_reginfo[] = {
7486     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7487       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7488       .access = PL2_RW,
7489       .writefn = vncr_write,
7490       .nv2_redirect_offset = 0xb0,
7491       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7492 };
7493 
7494 #endif /* TARGET_AARCH64 */
7495 
7496 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7497                                      bool isread)
7498 {
7499     int el = arm_current_el(env);
7500 
7501     if (el == 0) {
7502         uint64_t sctlr = arm_sctlr(env, el);
7503         if (!(sctlr & SCTLR_EnRCTX)) {
7504             return CP_ACCESS_TRAP_EL1;
7505         }
7506     } else if (el == 1) {
7507         uint64_t hcr = arm_hcr_el2_eff(env);
7508         if (hcr & HCR_NV) {
7509             return CP_ACCESS_TRAP_EL2;
7510         }
7511     }
7512     return CP_ACCESS_OK;
7513 }
7514 
7515 static const ARMCPRegInfo predinv_reginfo[] = {
7516     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7517       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7518       .fgt = FGT_CFPRCTX,
7519       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7520     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7521       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7522       .fgt = FGT_DVPRCTX,
7523       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7524     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7525       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7526       .fgt = FGT_CPPRCTX,
7527       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7528     /*
7529      * Note the AArch32 opcodes have a different OPC1.
7530      */
7531     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7532       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7533       .fgt = FGT_CFPRCTX,
7534       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7535     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7536       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7537       .fgt = FGT_DVPRCTX,
7538       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7539     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7540       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7541       .fgt = FGT_CPPRCTX,
7542       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7543 };
7544 
7545 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7546 {
7547     /* Read the high 32 bits of the current CCSIDR */
7548     return extract64(ccsidr_read(env, ri), 32, 32);
7549 }
7550 
7551 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7552     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7553       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7554       .access = PL1_R,
7555       .accessfn = access_tid4,
7556       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7557 };
7558 
7559 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7560                                        bool isread)
7561 {
7562     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7563         return CP_ACCESS_TRAP_EL2;
7564     }
7565 
7566     return CP_ACCESS_OK;
7567 }
7568 
7569 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7570                                        bool isread)
7571 {
7572     if (arm_feature(env, ARM_FEATURE_V8)) {
7573         return access_aa64_tid3(env, ri, isread);
7574     }
7575 
7576     return CP_ACCESS_OK;
7577 }
7578 
7579 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7580                                      bool isread)
7581 {
7582     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7583         return CP_ACCESS_TRAP_EL2;
7584     }
7585 
7586     return CP_ACCESS_OK;
7587 }
7588 
7589 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7590                                         const ARMCPRegInfo *ri, bool isread)
7591 {
7592     /*
7593      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7594      * in v7A, not in v8A.
7595      */
7596     if (!arm_feature(env, ARM_FEATURE_V8) &&
7597         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7598         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7599         return CP_ACCESS_TRAP_EL2;
7600     }
7601     return CP_ACCESS_OK;
7602 }
7603 
7604 static const ARMCPRegInfo jazelle_regs[] = {
7605     { .name = "JIDR",
7606       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7607       .access = PL1_R, .accessfn = access_jazelle,
7608       .type = ARM_CP_CONST, .resetvalue = 0 },
7609     { .name = "JOSCR",
7610       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7611       .accessfn = access_joscr_jmcr,
7612       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7613     { .name = "JMCR",
7614       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7615       .accessfn = access_joscr_jmcr,
7616       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7617 };
7618 
7619 static const ARMCPRegInfo contextidr_el2 = {
7620     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7621     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7622     .access = PL2_RW,
7623     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7624 };
7625 
7626 static const ARMCPRegInfo vhe_reginfo[] = {
7627     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7628       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7629       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7630       .raw_writefn = raw_write,
7631       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7632 #ifndef CONFIG_USER_ONLY
7633     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7634       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7635       .fieldoffset =
7636         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7637       .type = ARM_CP_IO, .access = PL2_RW,
7638       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7639     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7640       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7641       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7642       .resetfn = gt_hv_timer_reset,
7643       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7644     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7645       .type = ARM_CP_IO,
7646       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7647       .access = PL2_RW,
7648       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7649       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7650     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7651       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7652       .type = ARM_CP_IO | ARM_CP_ALIAS,
7653       .access = PL2_RW, .accessfn = access_el1nvpct,
7654       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7655       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7656       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7657     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7658       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7659       .type = ARM_CP_IO | ARM_CP_ALIAS,
7660       .access = PL2_RW, .accessfn = access_el1nvvct,
7661       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7662       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7663       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7664     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7665       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7666       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7667       .access = PL2_RW, .accessfn = e2h_access,
7668       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7669     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7670       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7671       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7672       .access = PL2_RW, .accessfn = e2h_access,
7673       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7674     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7675       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7676       .type = ARM_CP_IO | ARM_CP_ALIAS,
7677       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7678       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7679       .access = PL2_RW, .accessfn = access_el1nvpct,
7680       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7681     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7683       .type = ARM_CP_IO | ARM_CP_ALIAS,
7684       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7685       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7686       .access = PL2_RW, .accessfn = access_el1nvvct,
7687       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7688 #endif
7689 };
7690 
7691 #ifndef CONFIG_USER_ONLY
7692 static const ARMCPRegInfo ats1e1_reginfo[] = {
7693     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7694       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7695       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7696       .fgt = FGT_ATS1E1RP,
7697       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7698     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7699       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7700       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7701       .fgt = FGT_ATS1E1WP,
7702       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7703 };
7704 
7705 static const ARMCPRegInfo ats1cp_reginfo[] = {
7706     { .name = "ATS1CPRP",
7707       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7708       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7709       .writefn = ats_write },
7710     { .name = "ATS1CPWP",
7711       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7712       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7713       .writefn = ats_write },
7714 };
7715 #endif
7716 
7717 /*
7718  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7719  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7720  * is non-zero, which is never for ARMv7, optionally in ARMv8
7721  * and mandatorily for ARMv8.2 and up.
7722  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7723  * implementation is RAZ/WI we can ignore this detail, as we
7724  * do for ACTLR.
7725  */
7726 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7727     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7728       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7729       .access = PL1_RW, .accessfn = access_tacr,
7730       .type = ARM_CP_CONST, .resetvalue = 0 },
7731     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7732       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7733       .access = PL2_RW, .type = ARM_CP_CONST,
7734       .resetvalue = 0 },
7735 };
7736 
7737 void register_cp_regs_for_features(ARMCPU *cpu)
7738 {
7739     /* Register all the coprocessor registers based on feature bits */
7740     CPUARMState *env = &cpu->env;
7741     if (arm_feature(env, ARM_FEATURE_M)) {
7742         /* M profile has no coprocessor registers */
7743         return;
7744     }
7745 
7746     define_arm_cp_regs(cpu, cp_reginfo);
7747     if (!arm_feature(env, ARM_FEATURE_V8)) {
7748         /*
7749          * Must go early as it is full of wildcards that may be
7750          * overridden by later definitions.
7751          */
7752         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7753     }
7754 
7755     define_tlb_insn_regs(cpu);
7756 
7757     if (arm_feature(env, ARM_FEATURE_V6)) {
7758         /* The ID registers all have impdef reset values */
7759         ARMCPRegInfo v6_idregs[] = {
7760             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7761               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7762               .access = PL1_R, .type = ARM_CP_CONST,
7763               .accessfn = access_aa32_tid3,
7764               .resetvalue = cpu->isar.id_pfr0 },
7765             /*
7766              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7767              * the value of the GIC field until after we define these regs.
7768              */
7769             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7770               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7771               .access = PL1_R, .type = ARM_CP_NO_RAW,
7772               .accessfn = access_aa32_tid3,
7773 #ifdef CONFIG_USER_ONLY
7774               .type = ARM_CP_CONST,
7775               .resetvalue = cpu->isar.id_pfr1,
7776 #else
7777               .type = ARM_CP_NO_RAW,
7778               .accessfn = access_aa32_tid3,
7779               .readfn = id_pfr1_read,
7780               .writefn = arm_cp_write_ignore
7781 #endif
7782             },
7783             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7785               .access = PL1_R, .type = ARM_CP_CONST,
7786               .accessfn = access_aa32_tid3,
7787               .resetvalue = cpu->isar.id_dfr0 },
7788             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7790               .access = PL1_R, .type = ARM_CP_CONST,
7791               .accessfn = access_aa32_tid3,
7792               .resetvalue = cpu->id_afr0 },
7793             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7794               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7795               .access = PL1_R, .type = ARM_CP_CONST,
7796               .accessfn = access_aa32_tid3,
7797               .resetvalue = cpu->isar.id_mmfr0 },
7798             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7799               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7800               .access = PL1_R, .type = ARM_CP_CONST,
7801               .accessfn = access_aa32_tid3,
7802               .resetvalue = cpu->isar.id_mmfr1 },
7803             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7804               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7805               .access = PL1_R, .type = ARM_CP_CONST,
7806               .accessfn = access_aa32_tid3,
7807               .resetvalue = cpu->isar.id_mmfr2 },
7808             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7809               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7810               .access = PL1_R, .type = ARM_CP_CONST,
7811               .accessfn = access_aa32_tid3,
7812               .resetvalue = cpu->isar.id_mmfr3 },
7813             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7814               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7815               .access = PL1_R, .type = ARM_CP_CONST,
7816               .accessfn = access_aa32_tid3,
7817               .resetvalue = cpu->isar.id_isar0 },
7818             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7819               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7820               .access = PL1_R, .type = ARM_CP_CONST,
7821               .accessfn = access_aa32_tid3,
7822               .resetvalue = cpu->isar.id_isar1 },
7823             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7824               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7825               .access = PL1_R, .type = ARM_CP_CONST,
7826               .accessfn = access_aa32_tid3,
7827               .resetvalue = cpu->isar.id_isar2 },
7828             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7829               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7830               .access = PL1_R, .type = ARM_CP_CONST,
7831               .accessfn = access_aa32_tid3,
7832               .resetvalue = cpu->isar.id_isar3 },
7833             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7834               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7835               .access = PL1_R, .type = ARM_CP_CONST,
7836               .accessfn = access_aa32_tid3,
7837               .resetvalue = cpu->isar.id_isar4 },
7838             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7839               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7840               .access = PL1_R, .type = ARM_CP_CONST,
7841               .accessfn = access_aa32_tid3,
7842               .resetvalue = cpu->isar.id_isar5 },
7843             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7844               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7845               .access = PL1_R, .type = ARM_CP_CONST,
7846               .accessfn = access_aa32_tid3,
7847               .resetvalue = cpu->isar.id_mmfr4 },
7848             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7849               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7850               .access = PL1_R, .type = ARM_CP_CONST,
7851               .accessfn = access_aa32_tid3,
7852               .resetvalue = cpu->isar.id_isar6 },
7853         };
7854         define_arm_cp_regs(cpu, v6_idregs);
7855         define_arm_cp_regs(cpu, v6_cp_reginfo);
7856     } else {
7857         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7858     }
7859     if (arm_feature(env, ARM_FEATURE_V6K)) {
7860         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7861     }
7862     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7863         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7864     }
7865     if (arm_feature(env, ARM_FEATURE_V7)) {
7866         ARMCPRegInfo clidr = {
7867             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7868             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7869             .access = PL1_R, .type = ARM_CP_CONST,
7870             .accessfn = access_tid4,
7871             .fgt = FGT_CLIDR_EL1,
7872             .resetvalue = cpu->clidr
7873         };
7874         define_one_arm_cp_reg(cpu, &clidr);
7875         define_arm_cp_regs(cpu, v7_cp_reginfo);
7876         define_debug_regs(cpu);
7877         define_pmu_regs(cpu);
7878     } else {
7879         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7880     }
7881     if (arm_feature(env, ARM_FEATURE_V8)) {
7882         /*
7883          * v8 ID registers, which all have impdef reset values.
7884          * Note that within the ID register ranges the unused slots
7885          * must all RAZ, not UNDEF; future architecture versions may
7886          * define new registers here.
7887          * ID registers which are AArch64 views of the AArch32 ID registers
7888          * which already existed in v6 and v7 are handled elsewhere,
7889          * in v6_idregs[].
7890          */
7891         int i;
7892         ARMCPRegInfo v8_idregs[] = {
7893             /*
7894              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7895              * emulation because we don't know the right value for the
7896              * GIC field until after we define these regs.
7897              */
7898             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7899               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7900               .access = PL1_R,
7901 #ifdef CONFIG_USER_ONLY
7902               .type = ARM_CP_CONST,
7903               .resetvalue = cpu->isar.id_aa64pfr0
7904 #else
7905               .type = ARM_CP_NO_RAW,
7906               .accessfn = access_aa64_tid3,
7907               .readfn = id_aa64pfr0_read,
7908               .writefn = arm_cp_write_ignore
7909 #endif
7910             },
7911             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7912               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7913               .access = PL1_R, .type = ARM_CP_CONST,
7914               .accessfn = access_aa64_tid3,
7915               .resetvalue = cpu->isar.id_aa64pfr1},
7916             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7917               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7918               .access = PL1_R, .type = ARM_CP_CONST,
7919               .accessfn = access_aa64_tid3,
7920               .resetvalue = 0 },
7921             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7922               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7923               .access = PL1_R, .type = ARM_CP_CONST,
7924               .accessfn = access_aa64_tid3,
7925               .resetvalue = 0 },
7926             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7927               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7928               .access = PL1_R, .type = ARM_CP_CONST,
7929               .accessfn = access_aa64_tid3,
7930               .resetvalue = cpu->isar.id_aa64zfr0 },
7931             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7932               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7933               .access = PL1_R, .type = ARM_CP_CONST,
7934               .accessfn = access_aa64_tid3,
7935               .resetvalue = cpu->isar.id_aa64smfr0 },
7936             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7937               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7938               .access = PL1_R, .type = ARM_CP_CONST,
7939               .accessfn = access_aa64_tid3,
7940               .resetvalue = 0 },
7941             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7942               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7943               .access = PL1_R, .type = ARM_CP_CONST,
7944               .accessfn = access_aa64_tid3,
7945               .resetvalue = 0 },
7946             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7947               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7948               .access = PL1_R, .type = ARM_CP_CONST,
7949               .accessfn = access_aa64_tid3,
7950               .resetvalue = cpu->isar.id_aa64dfr0 },
7951             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7952               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7953               .access = PL1_R, .type = ARM_CP_CONST,
7954               .accessfn = access_aa64_tid3,
7955               .resetvalue = cpu->isar.id_aa64dfr1 },
7956             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7957               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7958               .access = PL1_R, .type = ARM_CP_CONST,
7959               .accessfn = access_aa64_tid3,
7960               .resetvalue = 0 },
7961             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7962               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7963               .access = PL1_R, .type = ARM_CP_CONST,
7964               .accessfn = access_aa64_tid3,
7965               .resetvalue = 0 },
7966             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7967               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7968               .access = PL1_R, .type = ARM_CP_CONST,
7969               .accessfn = access_aa64_tid3,
7970               .resetvalue = cpu->id_aa64afr0 },
7971             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7972               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7973               .access = PL1_R, .type = ARM_CP_CONST,
7974               .accessfn = access_aa64_tid3,
7975               .resetvalue = cpu->id_aa64afr1 },
7976             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7977               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7978               .access = PL1_R, .type = ARM_CP_CONST,
7979               .accessfn = access_aa64_tid3,
7980               .resetvalue = 0 },
7981             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7982               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7983               .access = PL1_R, .type = ARM_CP_CONST,
7984               .accessfn = access_aa64_tid3,
7985               .resetvalue = 0 },
7986             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7987               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7988               .access = PL1_R, .type = ARM_CP_CONST,
7989               .accessfn = access_aa64_tid3,
7990               .resetvalue = cpu->isar.id_aa64isar0 },
7991             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7992               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7993               .access = PL1_R, .type = ARM_CP_CONST,
7994               .accessfn = access_aa64_tid3,
7995               .resetvalue = cpu->isar.id_aa64isar1 },
7996             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
7997               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7998               .access = PL1_R, .type = ARM_CP_CONST,
7999               .accessfn = access_aa64_tid3,
8000               .resetvalue = cpu->isar.id_aa64isar2 },
8001             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8002               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8003               .access = PL1_R, .type = ARM_CP_CONST,
8004               .accessfn = access_aa64_tid3,
8005               .resetvalue = 0 },
8006             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8007               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8008               .access = PL1_R, .type = ARM_CP_CONST,
8009               .accessfn = access_aa64_tid3,
8010               .resetvalue = 0 },
8011             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8012               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8013               .access = PL1_R, .type = ARM_CP_CONST,
8014               .accessfn = access_aa64_tid3,
8015               .resetvalue = 0 },
8016             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8017               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8018               .access = PL1_R, .type = ARM_CP_CONST,
8019               .accessfn = access_aa64_tid3,
8020               .resetvalue = 0 },
8021             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8022               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8023               .access = PL1_R, .type = ARM_CP_CONST,
8024               .accessfn = access_aa64_tid3,
8025               .resetvalue = 0 },
8026             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8027               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8028               .access = PL1_R, .type = ARM_CP_CONST,
8029               .accessfn = access_aa64_tid3,
8030               .resetvalue = cpu->isar.id_aa64mmfr0 },
8031             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8032               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8033               .access = PL1_R, .type = ARM_CP_CONST,
8034               .accessfn = access_aa64_tid3,
8035               .resetvalue = cpu->isar.id_aa64mmfr1 },
8036             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8037               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8038               .access = PL1_R, .type = ARM_CP_CONST,
8039               .accessfn = access_aa64_tid3,
8040               .resetvalue = cpu->isar.id_aa64mmfr2 },
8041             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
8042               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8043               .access = PL1_R, .type = ARM_CP_CONST,
8044               .accessfn = access_aa64_tid3,
8045               .resetvalue = cpu->isar.id_aa64mmfr3 },
8046             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8047               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8048               .access = PL1_R, .type = ARM_CP_CONST,
8049               .accessfn = access_aa64_tid3,
8050               .resetvalue = 0 },
8051             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8052               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8053               .access = PL1_R, .type = ARM_CP_CONST,
8054               .accessfn = access_aa64_tid3,
8055               .resetvalue = 0 },
8056             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8057               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8058               .access = PL1_R, .type = ARM_CP_CONST,
8059               .accessfn = access_aa64_tid3,
8060               .resetvalue = 0 },
8061             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8062               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8063               .access = PL1_R, .type = ARM_CP_CONST,
8064               .accessfn = access_aa64_tid3,
8065               .resetvalue = 0 },
8066             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8067               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8068               .access = PL1_R, .type = ARM_CP_CONST,
8069               .accessfn = access_aa64_tid3,
8070               .resetvalue = cpu->isar.mvfr0 },
8071             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8072               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8073               .access = PL1_R, .type = ARM_CP_CONST,
8074               .accessfn = access_aa64_tid3,
8075               .resetvalue = cpu->isar.mvfr1 },
8076             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8077               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8078               .access = PL1_R, .type = ARM_CP_CONST,
8079               .accessfn = access_aa64_tid3,
8080               .resetvalue = cpu->isar.mvfr2 },
8081             /*
8082              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8083              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8084              * as RAZ, since it is in the "reserved for future ID
8085              * registers, RAZ" part of the AArch32 encoding space.
8086              */
8087             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8088               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8089               .access = PL1_R, .type = ARM_CP_CONST,
8090               .accessfn = access_aa64_tid3,
8091               .resetvalue = 0 },
8092             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8093               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8094               .access = PL1_R, .type = ARM_CP_CONST,
8095               .accessfn = access_aa64_tid3,
8096               .resetvalue = 0 },
8097             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8098               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8099               .access = PL1_R, .type = ARM_CP_CONST,
8100               .accessfn = access_aa64_tid3,
8101               .resetvalue = 0 },
8102             /*
8103              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8104              * they're also RAZ for AArch64, and in v8 are gradually
8105              * being filled with AArch64-view-of-AArch32-ID-register
8106              * for new ID registers.
8107              */
8108             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8109               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8110               .access = PL1_R, .type = ARM_CP_CONST,
8111               .accessfn = access_aa64_tid3,
8112               .resetvalue = 0 },
8113             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8114               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8115               .access = PL1_R, .type = ARM_CP_CONST,
8116               .accessfn = access_aa64_tid3,
8117               .resetvalue = cpu->isar.id_pfr2 },
8118             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8119               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8120               .access = PL1_R, .type = ARM_CP_CONST,
8121               .accessfn = access_aa64_tid3,
8122               .resetvalue = cpu->isar.id_dfr1 },
8123             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8124               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8125               .access = PL1_R, .type = ARM_CP_CONST,
8126               .accessfn = access_aa64_tid3,
8127               .resetvalue = cpu->isar.id_mmfr5 },
8128             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8129               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8130               .access = PL1_R, .type = ARM_CP_CONST,
8131               .accessfn = access_aa64_tid3,
8132               .resetvalue = 0 },
8133             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8134               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8135               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8136               .fgt = FGT_PMCEIDN_EL0,
8137               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8138             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8139               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8140               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8141               .fgt = FGT_PMCEIDN_EL0,
8142               .resetvalue = cpu->pmceid0 },
8143             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8144               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8145               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8146               .fgt = FGT_PMCEIDN_EL0,
8147               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8148             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8149               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8150               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8151               .fgt = FGT_PMCEIDN_EL0,
8152               .resetvalue = cpu->pmceid1 },
8153         };
8154 #ifdef CONFIG_USER_ONLY
8155         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8156             { .name = "ID_AA64PFR0_EL1",
8157               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8158                                R_ID_AA64PFR0_ADVSIMD_MASK |
8159                                R_ID_AA64PFR0_SVE_MASK |
8160                                R_ID_AA64PFR0_DIT_MASK,
8161               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8162                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8163             { .name = "ID_AA64PFR1_EL1",
8164               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8165                                R_ID_AA64PFR1_SSBS_MASK |
8166                                R_ID_AA64PFR1_MTE_MASK |
8167                                R_ID_AA64PFR1_SME_MASK },
8168             { .name = "ID_AA64PFR*_EL1_RESERVED",
8169               .is_glob = true },
8170             { .name = "ID_AA64ZFR0_EL1",
8171               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8172                                R_ID_AA64ZFR0_AES_MASK |
8173                                R_ID_AA64ZFR0_BITPERM_MASK |
8174                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8175                                R_ID_AA64ZFR0_B16B16_MASK |
8176                                R_ID_AA64ZFR0_SHA3_MASK |
8177                                R_ID_AA64ZFR0_SM4_MASK |
8178                                R_ID_AA64ZFR0_I8MM_MASK |
8179                                R_ID_AA64ZFR0_F32MM_MASK |
8180                                R_ID_AA64ZFR0_F64MM_MASK },
8181             { .name = "ID_AA64SMFR0_EL1",
8182               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8183                                R_ID_AA64SMFR0_BI32I32_MASK |
8184                                R_ID_AA64SMFR0_B16F32_MASK |
8185                                R_ID_AA64SMFR0_F16F32_MASK |
8186                                R_ID_AA64SMFR0_I8I32_MASK |
8187                                R_ID_AA64SMFR0_F16F16_MASK |
8188                                R_ID_AA64SMFR0_B16B16_MASK |
8189                                R_ID_AA64SMFR0_I16I32_MASK |
8190                                R_ID_AA64SMFR0_F64F64_MASK |
8191                                R_ID_AA64SMFR0_I16I64_MASK |
8192                                R_ID_AA64SMFR0_SMEVER_MASK |
8193                                R_ID_AA64SMFR0_FA64_MASK },
8194             { .name = "ID_AA64MMFR0_EL1",
8195               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8196               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8197                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8198             { .name = "ID_AA64MMFR1_EL1",
8199               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8200             { .name = "ID_AA64MMFR2_EL1",
8201               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8202             { .name = "ID_AA64MMFR3_EL1",
8203               .exported_bits = 0 },
8204             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8205               .is_glob = true },
8206             { .name = "ID_AA64DFR0_EL1",
8207               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8208             { .name = "ID_AA64DFR1_EL1" },
8209             { .name = "ID_AA64DFR*_EL1_RESERVED",
8210               .is_glob = true },
8211             { .name = "ID_AA64AFR*",
8212               .is_glob = true },
8213             { .name = "ID_AA64ISAR0_EL1",
8214               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8215                                R_ID_AA64ISAR0_SHA1_MASK |
8216                                R_ID_AA64ISAR0_SHA2_MASK |
8217                                R_ID_AA64ISAR0_CRC32_MASK |
8218                                R_ID_AA64ISAR0_ATOMIC_MASK |
8219                                R_ID_AA64ISAR0_RDM_MASK |
8220                                R_ID_AA64ISAR0_SHA3_MASK |
8221                                R_ID_AA64ISAR0_SM3_MASK |
8222                                R_ID_AA64ISAR0_SM4_MASK |
8223                                R_ID_AA64ISAR0_DP_MASK |
8224                                R_ID_AA64ISAR0_FHM_MASK |
8225                                R_ID_AA64ISAR0_TS_MASK |
8226                                R_ID_AA64ISAR0_RNDR_MASK },
8227             { .name = "ID_AA64ISAR1_EL1",
8228               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8229                                R_ID_AA64ISAR1_APA_MASK |
8230                                R_ID_AA64ISAR1_API_MASK |
8231                                R_ID_AA64ISAR1_JSCVT_MASK |
8232                                R_ID_AA64ISAR1_FCMA_MASK |
8233                                R_ID_AA64ISAR1_LRCPC_MASK |
8234                                R_ID_AA64ISAR1_GPA_MASK |
8235                                R_ID_AA64ISAR1_GPI_MASK |
8236                                R_ID_AA64ISAR1_FRINTTS_MASK |
8237                                R_ID_AA64ISAR1_SB_MASK |
8238                                R_ID_AA64ISAR1_BF16_MASK |
8239                                R_ID_AA64ISAR1_DGH_MASK |
8240                                R_ID_AA64ISAR1_I8MM_MASK },
8241             { .name = "ID_AA64ISAR2_EL1",
8242               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8243                                R_ID_AA64ISAR2_RPRES_MASK |
8244                                R_ID_AA64ISAR2_GPA3_MASK |
8245                                R_ID_AA64ISAR2_APA3_MASK |
8246                                R_ID_AA64ISAR2_MOPS_MASK |
8247                                R_ID_AA64ISAR2_BC_MASK |
8248                                R_ID_AA64ISAR2_RPRFM_MASK |
8249                                R_ID_AA64ISAR2_CSSC_MASK },
8250             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8251               .is_glob = true },
8252         };
8253         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8254 #endif
8255         /*
8256          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8257          * TODO: For RMR, a write with bit 1 set should do something with
8258          * cpu_reset(). In the meantime, "the bit is strictly a request",
8259          * so we are in spec just ignoring writes.
8260          */
8261         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8262             !arm_feature(env, ARM_FEATURE_EL2)) {
8263             ARMCPRegInfo el1_reset_regs[] = {
8264                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8265                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8266                   .access = PL1_R,
8267                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8268                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8269                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8270                   .access = PL1_RW, .type = ARM_CP_CONST,
8271                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8272             };
8273             define_arm_cp_regs(cpu, el1_reset_regs);
8274         }
8275         define_arm_cp_regs(cpu, v8_idregs);
8276         define_arm_cp_regs(cpu, v8_cp_reginfo);
8277         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8278             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8279         }
8280 
8281         for (i = 4; i < 16; i++) {
8282             /*
8283              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8284              * For pre-v8 cores there are RAZ patterns for these in
8285              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8286              * v8 extends the "must RAZ" part of the ID register space
8287              * to also cover c0, 0, c{8-15}, {0-7}.
8288              * These are STATE_AA32 because in the AArch64 sysreg space
8289              * c4-c7 is where the AArch64 ID registers live (and we've
8290              * already defined those in v8_idregs[]), and c8-c15 are not
8291              * "must RAZ" for AArch64.
8292              */
8293             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8294             ARMCPRegInfo v8_aa32_raz_idregs = {
8295                 .name = name,
8296                 .state = ARM_CP_STATE_AA32,
8297                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8298                 .access = PL1_R, .type = ARM_CP_CONST,
8299                 .accessfn = access_aa64_tid3,
8300                 .resetvalue = 0 };
8301             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8302         }
8303     }
8304 
8305     /*
8306      * Register the base EL2 cpregs.
8307      * Pre v8, these registers are implemented only as part of the
8308      * Virtualization Extensions (EL2 present).  Beginning with v8,
8309      * if EL2 is missing but EL3 is enabled, mostly these become
8310      * RES0 from EL3, with some specific exceptions.
8311      */
8312     if (arm_feature(env, ARM_FEATURE_EL2)
8313         || (arm_feature(env, ARM_FEATURE_EL3)
8314             && arm_feature(env, ARM_FEATURE_V8))) {
8315         uint64_t vmpidr_def = mpidr_read_val(env);
8316         ARMCPRegInfo vpidr_regs[] = {
8317             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8318               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8319               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8320               .resetvalue = cpu->midr,
8321               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8322               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8323             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8324               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8325               .access = PL2_RW, .resetvalue = cpu->midr,
8326               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8327               .nv2_redirect_offset = 0x88,
8328               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8329             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8330               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8331               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8332               .resetvalue = vmpidr_def,
8333               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8334               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8335             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8336               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8337               .access = PL2_RW, .resetvalue = vmpidr_def,
8338               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8339               .nv2_redirect_offset = 0x50,
8340               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8341         };
8342         /*
8343          * The only field of MDCR_EL2 that has a defined architectural reset
8344          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8345          */
8346         ARMCPRegInfo mdcr_el2 = {
8347             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8348             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8349             .writefn = mdcr_el2_write,
8350             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8351             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8352         };
8353         define_one_arm_cp_reg(cpu, &mdcr_el2);
8354         define_arm_cp_regs(cpu, vpidr_regs);
8355         define_arm_cp_regs(cpu, el2_cp_reginfo);
8356         if (arm_feature(env, ARM_FEATURE_V8)) {
8357             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8358         }
8359         if (cpu_isar_feature(aa64_sel2, cpu)) {
8360             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8361         }
8362         /*
8363          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8364          * See commentary near RMR_EL1.
8365          */
8366         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8367             static const ARMCPRegInfo el2_reset_regs[] = {
8368                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8369                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8370                   .access = PL2_R,
8371                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8372                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8373                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8374                   .access = PL2_R,
8375                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8376                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8377                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8378                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8379             };
8380             define_arm_cp_regs(cpu, el2_reset_regs);
8381         }
8382     }
8383 
8384     /* Register the base EL3 cpregs. */
8385     if (arm_feature(env, ARM_FEATURE_EL3)) {
8386         define_arm_cp_regs(cpu, el3_cp_reginfo);
8387         ARMCPRegInfo el3_regs[] = {
8388             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8389               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8390               .access = PL3_R,
8391               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8392             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8393               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8394               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8395             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8396               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8397               .access = PL3_RW, .type = ARM_CP_CONST,
8398               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8399             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8400               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8401               .access = PL3_RW,
8402               .raw_writefn = raw_write, .writefn = sctlr_write,
8403               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8404               .resetvalue = cpu->reset_sctlr },
8405         };
8406 
8407         define_arm_cp_regs(cpu, el3_regs);
8408     }
8409     /*
8410      * The behaviour of NSACR is sufficiently various that we don't
8411      * try to describe it in a single reginfo:
8412      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8413      *     reads as constant 0xc00 from NS EL1 and NS EL2
8414      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8415      *  if v7 without EL3, register doesn't exist
8416      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8417      */
8418     if (arm_feature(env, ARM_FEATURE_EL3)) {
8419         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8420             static const ARMCPRegInfo nsacr = {
8421                 .name = "NSACR", .type = ARM_CP_CONST,
8422                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8423                 .access = PL1_RW, .accessfn = nsacr_access,
8424                 .resetvalue = 0xc00
8425             };
8426             define_one_arm_cp_reg(cpu, &nsacr);
8427         } else {
8428             static const ARMCPRegInfo nsacr = {
8429                 .name = "NSACR",
8430                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8431                 .access = PL3_RW | PL1_R,
8432                 .resetvalue = 0,
8433                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8434             };
8435             define_one_arm_cp_reg(cpu, &nsacr);
8436         }
8437     } else {
8438         if (arm_feature(env, ARM_FEATURE_V8)) {
8439             static const ARMCPRegInfo nsacr = {
8440                 .name = "NSACR", .type = ARM_CP_CONST,
8441                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8442                 .access = PL1_R,
8443                 .resetvalue = 0xc00
8444             };
8445             define_one_arm_cp_reg(cpu, &nsacr);
8446         }
8447     }
8448 
8449     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8450         if (arm_feature(env, ARM_FEATURE_V6)) {
8451             /* PMSAv6 not implemented */
8452             assert(arm_feature(env, ARM_FEATURE_V7));
8453             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8454             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8455         } else {
8456             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8457         }
8458     } else {
8459         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8460         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8461         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8462         if (cpu_isar_feature(aa32_hpd, cpu)) {
8463             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8464         }
8465     }
8466     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8467         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8468     }
8469     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8470         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8471     }
8472     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8473         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8474     }
8475 #ifndef CONFIG_USER_ONLY
8476     if (cpu_isar_feature(aa64_ecv, cpu)) {
8477         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8478     }
8479 #endif
8480     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8481         ARMCPRegInfo vapa_cp_reginfo[] = {
8482             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8483               .access = PL1_RW, .resetvalue = 0,
8484               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8485                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8486               .writefn = par_write},
8487 #ifndef CONFIG_USER_ONLY
8488             /* This underdecoding is safe because the reginfo is NO_RAW. */
8489             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8490               .access = PL1_W, .accessfn = ats_access,
8491               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8492 #endif
8493         };
8494 
8495         /*
8496          * When LPAE exists this 32-bit PAR register is an alias of the
8497          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8498          */
8499         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8500             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8501         }
8502         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8503     }
8504     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8505         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8506     }
8507     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8508         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8509     }
8510     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8511         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8512     }
8513     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8514         define_arm_cp_regs(cpu, omap_cp_reginfo);
8515     }
8516     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8517         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8518     }
8519     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8520         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8521     }
8522     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8523         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8524     }
8525     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8526         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8527     }
8528     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8529         define_arm_cp_regs(cpu, jazelle_regs);
8530     }
8531     /*
8532      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8533      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8534      * be read-only (ie write causes UNDEF exception).
8535      */
8536     {
8537         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8538             /*
8539              * Pre-v8 MIDR space.
8540              * Note that the MIDR isn't a simple constant register because
8541              * of the TI925 behaviour where writes to another register can
8542              * cause the MIDR value to change.
8543              *
8544              * Unimplemented registers in the c15 0 0 0 space default to
8545              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8546              * and friends override accordingly.
8547              */
8548             { .name = "MIDR",
8549               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8550               .access = PL1_R, .resetvalue = cpu->midr,
8551               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8552               .readfn = midr_read,
8553               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8554               .type = ARM_CP_OVERRIDE },
8555             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8556             { .name = "DUMMY",
8557               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8558               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8559             { .name = "DUMMY",
8560               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8561               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8562             { .name = "DUMMY",
8563               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8564               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8565             { .name = "DUMMY",
8566               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8567               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8568             { .name = "DUMMY",
8569               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8570               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8571         };
8572         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8573             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8574               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8575               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8576               .fgt = FGT_MIDR_EL1,
8577               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8578               .readfn = midr_read },
8579             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8580             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8581               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8582               .access = PL1_R, .resetvalue = cpu->midr },
8583             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8584               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8585               .access = PL1_R,
8586               .accessfn = access_aa64_tid1,
8587               .fgt = FGT_REVIDR_EL1,
8588               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8589         };
8590         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8591             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8592             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8593             .access = PL1_R, .resetvalue = cpu->midr
8594         };
8595         ARMCPRegInfo id_cp_reginfo[] = {
8596             /* These are common to v8 and pre-v8 */
8597             { .name = "CTR",
8598               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8599               .access = PL1_R, .accessfn = ctr_el0_access,
8600               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8601             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8602               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8603               .access = PL0_R, .accessfn = ctr_el0_access,
8604               .fgt = FGT_CTR_EL0,
8605               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8606             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8607             { .name = "TCMTR",
8608               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8609               .access = PL1_R,
8610               .accessfn = access_aa32_tid1,
8611               .type = ARM_CP_CONST, .resetvalue = 0 },
8612         };
8613         /* TLBTR is specific to VMSA */
8614         ARMCPRegInfo id_tlbtr_reginfo = {
8615               .name = "TLBTR",
8616               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8617               .access = PL1_R,
8618               .accessfn = access_aa32_tid1,
8619               .type = ARM_CP_CONST, .resetvalue = 0,
8620         };
8621         /* MPUIR is specific to PMSA V6+ */
8622         ARMCPRegInfo id_mpuir_reginfo = {
8623               .name = "MPUIR",
8624               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8625               .access = PL1_R, .type = ARM_CP_CONST,
8626               .resetvalue = cpu->pmsav7_dregion << 8
8627         };
8628         /* HMPUIR is specific to PMSA V8 */
8629         ARMCPRegInfo id_hmpuir_reginfo = {
8630             .name = "HMPUIR",
8631             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8632             .access = PL2_R, .type = ARM_CP_CONST,
8633             .resetvalue = cpu->pmsav8r_hdregion
8634         };
8635         static const ARMCPRegInfo crn0_wi_reginfo = {
8636             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8637             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8638             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8639         };
8640 #ifdef CONFIG_USER_ONLY
8641         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8642             { .name = "MIDR_EL1",
8643               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8644                                R_MIDR_EL1_PARTNUM_MASK |
8645                                R_MIDR_EL1_ARCHITECTURE_MASK |
8646                                R_MIDR_EL1_VARIANT_MASK |
8647                                R_MIDR_EL1_IMPLEMENTER_MASK },
8648             { .name = "REVIDR_EL1" },
8649         };
8650         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8651 #endif
8652         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8653             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8654             size_t i;
8655             /*
8656              * Register the blanket "writes ignored" value first to cover the
8657              * whole space. Then update the specific ID registers to allow write
8658              * access, so that they ignore writes rather than causing them to
8659              * UNDEF.
8660              */
8661             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8662             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8663                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8664             }
8665             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8666                 id_cp_reginfo[i].access = PL1_RW;
8667             }
8668             id_mpuir_reginfo.access = PL1_RW;
8669             id_tlbtr_reginfo.access = PL1_RW;
8670         }
8671         if (arm_feature(env, ARM_FEATURE_V8)) {
8672             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8673             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8674                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8675             }
8676         } else {
8677             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8678         }
8679         define_arm_cp_regs(cpu, id_cp_reginfo);
8680         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8681             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8682         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8683                    arm_feature(env, ARM_FEATURE_V8)) {
8684             uint32_t i = 0;
8685             char *tmp_string;
8686 
8687             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8688             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8689             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8690 
8691             /* Register alias is only valid for first 32 indexes */
8692             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8693                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8694                 uint8_t opc1 = extract32(i, 4, 1);
8695                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8696 
8697                 tmp_string = g_strdup_printf("PRBAR%u", i);
8698                 ARMCPRegInfo tmp_prbarn_reginfo = {
8699                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8700                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8701                     .access = PL1_RW, .resetvalue = 0,
8702                     .accessfn = access_tvm_trvm,
8703                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8704                 };
8705                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8706                 g_free(tmp_string);
8707 
8708                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8709                 tmp_string = g_strdup_printf("PRLAR%u", i);
8710                 ARMCPRegInfo tmp_prlarn_reginfo = {
8711                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8712                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8713                     .access = PL1_RW, .resetvalue = 0,
8714                     .accessfn = access_tvm_trvm,
8715                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8716                 };
8717                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8718                 g_free(tmp_string);
8719             }
8720 
8721             /* Register alias is only valid for first 32 indexes */
8722             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8723                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8724                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8725                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8726 
8727                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8728                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8729                     .name = tmp_string,
8730                     .type = ARM_CP_NO_RAW,
8731                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8732                     .access = PL2_RW, .resetvalue = 0,
8733                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8734                 };
8735                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8736                 g_free(tmp_string);
8737 
8738                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8739                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8740                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8741                     .name = tmp_string,
8742                     .type = ARM_CP_NO_RAW,
8743                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8744                     .access = PL2_RW, .resetvalue = 0,
8745                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8746                 };
8747                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8748                 g_free(tmp_string);
8749             }
8750         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8751             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8752         }
8753     }
8754 
8755     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8756         ARMCPRegInfo mpidr_cp_reginfo[] = {
8757             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8758               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8759               .fgt = FGT_MPIDR_EL1,
8760               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8761         };
8762 #ifdef CONFIG_USER_ONLY
8763         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8764             { .name = "MPIDR_EL1",
8765               .fixed_bits = 0x0000000080000000 },
8766         };
8767         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8768 #endif
8769         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8770     }
8771 
8772     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8773         ARMCPRegInfo auxcr_reginfo[] = {
8774             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8775               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8776               .access = PL1_RW, .accessfn = access_tacr,
8777               .nv2_redirect_offset = 0x118,
8778               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8779             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8780               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8781               .access = PL2_RW, .type = ARM_CP_CONST,
8782               .resetvalue = 0 },
8783             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8784               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8785               .access = PL3_RW, .type = ARM_CP_CONST,
8786               .resetvalue = 0 },
8787         };
8788         define_arm_cp_regs(cpu, auxcr_reginfo);
8789         if (cpu_isar_feature(aa32_ac2, cpu)) {
8790             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8791         }
8792     }
8793 
8794     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8795         /*
8796          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8797          * There are two flavours:
8798          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8799          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8800          *      32-bit register visible to AArch32 at a different encoding
8801          *      to the "flavour 1" register and with the bits rearranged to
8802          *      be able to squash a 64-bit address into the 32-bit view.
8803          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8804          * in future if we support AArch32-only configs of some of the
8805          * AArch64 cores we might need to add a specific feature flag
8806          * to indicate cores with "flavour 2" CBAR.
8807          */
8808         if (arm_feature(env, ARM_FEATURE_V8)) {
8809             /* 32 bit view is [31:18] 0...0 [43:32]. */
8810             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8811                 | extract64(cpu->reset_cbar, 32, 12);
8812             ARMCPRegInfo cbar_reginfo[] = {
8813                 { .name = "CBAR",
8814                   .type = ARM_CP_CONST,
8815                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8816                   .access = PL1_R, .resetvalue = cbar32 },
8817                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8818                   .type = ARM_CP_CONST,
8819                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8820                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8821             };
8822             /* We don't implement a r/w 64 bit CBAR currently */
8823             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8824             define_arm_cp_regs(cpu, cbar_reginfo);
8825         } else {
8826             ARMCPRegInfo cbar = {
8827                 .name = "CBAR",
8828                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8829                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8830                 .fieldoffset = offsetof(CPUARMState,
8831                                         cp15.c15_config_base_address)
8832             };
8833             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8834                 cbar.access = PL1_R;
8835                 cbar.fieldoffset = 0;
8836                 cbar.type = ARM_CP_CONST;
8837             }
8838             define_one_arm_cp_reg(cpu, &cbar);
8839         }
8840     }
8841 
8842     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8843         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8844             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8845               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8846               .access = PL1_RW, .writefn = vbar_write,
8847               .accessfn = access_nv1,
8848               .fgt = FGT_VBAR_EL1,
8849               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8850               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8851                                      offsetof(CPUARMState, cp15.vbar_ns) },
8852               .resetvalue = 0 },
8853         };
8854         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8855     }
8856 
8857     /* Generic registers whose values depend on the implementation */
8858     {
8859         ARMCPRegInfo sctlr = {
8860             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8861             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8862             .access = PL1_RW, .accessfn = access_tvm_trvm,
8863             .fgt = FGT_SCTLR_EL1,
8864             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8865             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8866                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8867             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8868             .raw_writefn = raw_write,
8869         };
8870         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8871             /*
8872              * Normally we would always end the TB on an SCTLR write, but Linux
8873              * arch/arm/mach-pxa/sleep.S expects two instructions following
8874              * an MMU enable to execute from cache.  Imitate this behaviour.
8875              */
8876             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8877         }
8878         define_one_arm_cp_reg(cpu, &sctlr);
8879 
8880         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8881             arm_feature(env, ARM_FEATURE_V8)) {
8882             ARMCPRegInfo vsctlr = {
8883                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8884                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8885                 .access = PL2_RW, .resetvalue = 0x0,
8886                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8887             };
8888             define_one_arm_cp_reg(cpu, &vsctlr);
8889         }
8890     }
8891 
8892     if (cpu_isar_feature(aa64_lor, cpu)) {
8893         define_arm_cp_regs(cpu, lor_reginfo);
8894     }
8895     if (cpu_isar_feature(aa64_pan, cpu)) {
8896         define_one_arm_cp_reg(cpu, &pan_reginfo);
8897     }
8898 #ifndef CONFIG_USER_ONLY
8899     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8900         define_arm_cp_regs(cpu, ats1e1_reginfo);
8901     }
8902     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8903         define_arm_cp_regs(cpu, ats1cp_reginfo);
8904     }
8905 #endif
8906     if (cpu_isar_feature(aa64_uao, cpu)) {
8907         define_one_arm_cp_reg(cpu, &uao_reginfo);
8908     }
8909 
8910     if (cpu_isar_feature(aa64_dit, cpu)) {
8911         define_one_arm_cp_reg(cpu, &dit_reginfo);
8912     }
8913     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8914         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8915     }
8916     if (cpu_isar_feature(any_ras, cpu)) {
8917         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8918     }
8919 
8920     if (cpu_isar_feature(aa64_vh, cpu) ||
8921         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8922         define_one_arm_cp_reg(cpu, &contextidr_el2);
8923     }
8924     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8925         define_arm_cp_regs(cpu, vhe_reginfo);
8926     }
8927 
8928     if (cpu_isar_feature(aa64_sve, cpu)) {
8929         define_arm_cp_regs(cpu, zcr_reginfo);
8930     }
8931 
8932     if (cpu_isar_feature(aa64_hcx, cpu)) {
8933         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8934     }
8935 
8936 #ifdef TARGET_AARCH64
8937     if (cpu_isar_feature(aa64_sme, cpu)) {
8938         define_arm_cp_regs(cpu, sme_reginfo);
8939     }
8940     if (cpu_isar_feature(aa64_pauth, cpu)) {
8941         define_arm_cp_regs(cpu, pauth_reginfo);
8942     }
8943     if (cpu_isar_feature(aa64_rndr, cpu)) {
8944         define_arm_cp_regs(cpu, rndr_reginfo);
8945     }
8946     /* Data Cache clean instructions up to PoP */
8947     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8948         define_one_arm_cp_reg(cpu, dcpop_reg);
8949 
8950         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8951             define_one_arm_cp_reg(cpu, dcpodp_reg);
8952         }
8953     }
8954 
8955     /*
8956      * If full MTE is enabled, add all of the system registers.
8957      * If only "instructions available at EL0" are enabled,
8958      * then define only a RAZ/WI version of PSTATE.TCO.
8959      */
8960     if (cpu_isar_feature(aa64_mte, cpu)) {
8961         ARMCPRegInfo gmid_reginfo = {
8962             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8963             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8964             .access = PL1_R, .accessfn = access_aa64_tid5,
8965             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8966         };
8967         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8968         define_arm_cp_regs(cpu, mte_reginfo);
8969         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8970     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8971         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8972         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8973     }
8974 
8975     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8976         define_arm_cp_regs(cpu, scxtnum_reginfo);
8977     }
8978 
8979     if (cpu_isar_feature(aa64_fgt, cpu)) {
8980         define_arm_cp_regs(cpu, fgt_reginfo);
8981     }
8982 
8983     if (cpu_isar_feature(aa64_rme, cpu)) {
8984         define_arm_cp_regs(cpu, rme_reginfo);
8985         if (cpu_isar_feature(aa64_mte, cpu)) {
8986             define_arm_cp_regs(cpu, rme_mte_reginfo);
8987         }
8988     }
8989 
8990     if (cpu_isar_feature(aa64_nv2, cpu)) {
8991         define_arm_cp_regs(cpu, nv2_reginfo);
8992     }
8993 
8994     if (cpu_isar_feature(aa64_nmi, cpu)) {
8995         define_arm_cp_regs(cpu, nmi_reginfo);
8996     }
8997 #endif
8998 
8999     if (cpu_isar_feature(any_predinv, cpu)) {
9000         define_arm_cp_regs(cpu, predinv_reginfo);
9001     }
9002 
9003     if (cpu_isar_feature(any_ccidx, cpu)) {
9004         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9005     }
9006 
9007 #ifndef CONFIG_USER_ONLY
9008     /*
9009      * Register redirections and aliases must be done last,
9010      * after the registers from the other extensions have been defined.
9011      */
9012     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9013         define_arm_vh_e2h_redirects_aliases(cpu);
9014     }
9015 #endif
9016 }
9017 
9018 /*
9019  * Private utility function for define_one_arm_cp_reg_with_opaque():
9020  * add a single reginfo struct to the hash table.
9021  */
9022 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9023                                    void *opaque, CPState state,
9024                                    CPSecureState secstate,
9025                                    int crm, int opc1, int opc2,
9026                                    const char *name)
9027 {
9028     CPUARMState *env = &cpu->env;
9029     uint32_t key;
9030     ARMCPRegInfo *r2;
9031     bool is64 = r->type & ARM_CP_64BIT;
9032     bool ns = secstate & ARM_CP_SECSTATE_NS;
9033     int cp = r->cp;
9034     size_t name_len;
9035     bool make_const;
9036 
9037     switch (state) {
9038     case ARM_CP_STATE_AA32:
9039         /* We assume it is a cp15 register if the .cp field is left unset. */
9040         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9041             cp = 15;
9042         }
9043         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9044         break;
9045     case ARM_CP_STATE_AA64:
9046         /*
9047          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9048          * cp == 0 as equivalent to the value for "standard guest-visible
9049          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9050          * in their AArch64 view (the .cp value may be non-zero for the
9051          * benefit of the AArch32 view).
9052          */
9053         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9054             cp = CP_REG_ARM64_SYSREG_CP;
9055         }
9056         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9057         break;
9058     default:
9059         g_assert_not_reached();
9060     }
9061 
9062     /* Overriding of an existing definition must be explicitly requested. */
9063     if (!(r->type & ARM_CP_OVERRIDE)) {
9064         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9065         if (oldreg) {
9066             assert(oldreg->type & ARM_CP_OVERRIDE);
9067         }
9068     }
9069 
9070     /*
9071      * Eliminate registers that are not present because the EL is missing.
9072      * Doing this here makes it easier to put all registers for a given
9073      * feature into the same ARMCPRegInfo array and define them all at once.
9074      */
9075     make_const = false;
9076     if (arm_feature(env, ARM_FEATURE_EL3)) {
9077         /*
9078          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9079          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9080          */
9081         int min_el = ctz32(r->access) / 2;
9082         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9083             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9084                 return;
9085             }
9086             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9087         }
9088     } else {
9089         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9090                                  ? PL2_RW : PL1_RW);
9091         if ((r->access & max_el) == 0) {
9092             return;
9093         }
9094     }
9095 
9096     /* Combine cpreg and name into one allocation. */
9097     name_len = strlen(name) + 1;
9098     r2 = g_malloc(sizeof(*r2) + name_len);
9099     *r2 = *r;
9100     r2->name = memcpy(r2 + 1, name, name_len);
9101 
9102     /*
9103      * Update fields to match the instantiation, overwiting wildcards
9104      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9105      */
9106     r2->cp = cp;
9107     r2->crm = crm;
9108     r2->opc1 = opc1;
9109     r2->opc2 = opc2;
9110     r2->state = state;
9111     r2->secure = secstate;
9112     if (opaque) {
9113         r2->opaque = opaque;
9114     }
9115 
9116     if (make_const) {
9117         /* This should not have been a very special register to begin. */
9118         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9119         assert(old_special == 0 || old_special == ARM_CP_NOP);
9120         /*
9121          * Set the special function to CONST, retaining the other flags.
9122          * This is important for e.g. ARM_CP_SVE so that we still
9123          * take the SVE trap if CPTR_EL3.EZ == 0.
9124          */
9125         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9126         /*
9127          * Usually, these registers become RES0, but there are a few
9128          * special cases like VPIDR_EL2 which have a constant non-zero
9129          * value with writes ignored.
9130          */
9131         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9132             r2->resetvalue = 0;
9133         }
9134         /*
9135          * ARM_CP_CONST has precedence, so removing the callbacks and
9136          * offsets are not strictly necessary, but it is potentially
9137          * less confusing to debug later.
9138          */
9139         r2->readfn = NULL;
9140         r2->writefn = NULL;
9141         r2->raw_readfn = NULL;
9142         r2->raw_writefn = NULL;
9143         r2->resetfn = NULL;
9144         r2->fieldoffset = 0;
9145         r2->bank_fieldoffsets[0] = 0;
9146         r2->bank_fieldoffsets[1] = 0;
9147     } else {
9148         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9149 
9150         if (isbanked) {
9151             /*
9152              * Register is banked (using both entries in array).
9153              * Overwriting fieldoffset as the array is only used to define
9154              * banked registers but later only fieldoffset is used.
9155              */
9156             r2->fieldoffset = r->bank_fieldoffsets[ns];
9157         }
9158         if (state == ARM_CP_STATE_AA32) {
9159             if (isbanked) {
9160                 /*
9161                  * If the register is banked then we don't need to migrate or
9162                  * reset the 32-bit instance in certain cases:
9163                  *
9164                  * 1) If the register has both 32-bit and 64-bit instances
9165                  *    then we can count on the 64-bit instance taking care
9166                  *    of the non-secure bank.
9167                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9168                  *    version taking care of the secure bank.  This requires
9169                  *    that separate 32 and 64-bit definitions are provided.
9170                  */
9171                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9172                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9173                     r2->type |= ARM_CP_ALIAS;
9174                 }
9175             } else if ((secstate != r->secure) && !ns) {
9176                 /*
9177                  * The register is not banked so we only want to allow
9178                  * migration of the non-secure instance.
9179                  */
9180                 r2->type |= ARM_CP_ALIAS;
9181             }
9182 
9183             if (HOST_BIG_ENDIAN &&
9184                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9185                 r2->fieldoffset += sizeof(uint32_t);
9186             }
9187         }
9188     }
9189 
9190     /*
9191      * By convention, for wildcarded registers only the first
9192      * entry is used for migration; the others are marked as
9193      * ALIAS so we don't try to transfer the register
9194      * multiple times. Special registers (ie NOP/WFI) are
9195      * never migratable and not even raw-accessible.
9196      */
9197     if (r2->type & ARM_CP_SPECIAL_MASK) {
9198         r2->type |= ARM_CP_NO_RAW;
9199     }
9200     if (((r->crm == CP_ANY) && crm != 0) ||
9201         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9202         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9203         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9204     }
9205 
9206     /*
9207      * Check that raw accesses are either forbidden or handled. Note that
9208      * we can't assert this earlier because the setup of fieldoffset for
9209      * banked registers has to be done first.
9210      */
9211     if (!(r2->type & ARM_CP_NO_RAW)) {
9212         assert(!raw_accessors_invalid(r2));
9213     }
9214 
9215     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9216 }
9217 
9218 
9219 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9220                                        const ARMCPRegInfo *r, void *opaque)
9221 {
9222     /*
9223      * Define implementations of coprocessor registers.
9224      * We store these in a hashtable because typically
9225      * there are less than 150 registers in a space which
9226      * is 16*16*16*8*8 = 262144 in size.
9227      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9228      * If a register is defined twice then the second definition is
9229      * used, so this can be used to define some generic registers and
9230      * then override them with implementation specific variations.
9231      * At least one of the original and the second definition should
9232      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9233      * against accidental use.
9234      *
9235      * The state field defines whether the register is to be
9236      * visible in the AArch32 or AArch64 execution state. If the
9237      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9238      * reginfo structure for the AArch32 view, which sees the lower
9239      * 32 bits of the 64 bit register.
9240      *
9241      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9242      * be wildcarded. AArch64 registers are always considered to be 64
9243      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9244      * the register, if any.
9245      */
9246     int crm, opc1, opc2;
9247     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9248     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9249     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9250     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9251     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9252     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9253     CPState state;
9254 
9255     /* 64 bit registers have only CRm and Opc1 fields */
9256     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9257     /* op0 only exists in the AArch64 encodings */
9258     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9259     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9260     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9261     /*
9262      * This API is only for Arm's system coprocessors (14 and 15) or
9263      * (M-profile or v7A-and-earlier only) for implementation defined
9264      * coprocessors in the range 0..7.  Our decode assumes this, since
9265      * 8..13 can be used for other insns including VFP and Neon. See
9266      * valid_cp() in translate.c.  Assert here that we haven't tried
9267      * to use an invalid coprocessor number.
9268      */
9269     switch (r->state) {
9270     case ARM_CP_STATE_BOTH:
9271         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9272         if (r->cp == 0) {
9273             break;
9274         }
9275         /* fall through */
9276     case ARM_CP_STATE_AA32:
9277         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9278             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9279             assert(r->cp >= 14 && r->cp <= 15);
9280         } else {
9281             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9282         }
9283         break;
9284     case ARM_CP_STATE_AA64:
9285         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9286         break;
9287     default:
9288         g_assert_not_reached();
9289     }
9290     /*
9291      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9292      * encodes a minimum access level for the register. We roll this
9293      * runtime check into our general permission check code, so check
9294      * here that the reginfo's specified permissions are strict enough
9295      * to encompass the generic architectural permission check.
9296      */
9297     if (r->state != ARM_CP_STATE_AA32) {
9298         CPAccessRights mask;
9299         switch (r->opc1) {
9300         case 0:
9301             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9302             mask = PL0U_R | PL1_RW;
9303             break;
9304         case 1: case 2:
9305             /* min_EL EL1 */
9306             mask = PL1_RW;
9307             break;
9308         case 3:
9309             /* min_EL EL0 */
9310             mask = PL0_RW;
9311             break;
9312         case 4:
9313         case 5:
9314             /* min_EL EL2 */
9315             mask = PL2_RW;
9316             break;
9317         case 6:
9318             /* min_EL EL3 */
9319             mask = PL3_RW;
9320             break;
9321         case 7:
9322             /* min_EL EL1, secure mode only (we don't check the latter) */
9323             mask = PL1_RW;
9324             break;
9325         default:
9326             /* broken reginfo with out-of-range opc1 */
9327             g_assert_not_reached();
9328         }
9329         /* assert our permissions are not too lax (stricter is fine) */
9330         assert((r->access & ~mask) == 0);
9331     }
9332 
9333     /*
9334      * Check that the register definition has enough info to handle
9335      * reads and writes if they are permitted.
9336      */
9337     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9338         if (r->access & PL3_R) {
9339             assert((r->fieldoffset ||
9340                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9341                    r->readfn);
9342         }
9343         if (r->access & PL3_W) {
9344             assert((r->fieldoffset ||
9345                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9346                    r->writefn);
9347         }
9348     }
9349 
9350     for (crm = crmmin; crm <= crmmax; crm++) {
9351         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9352             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9353                 for (state = ARM_CP_STATE_AA32;
9354                      state <= ARM_CP_STATE_AA64; state++) {
9355                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9356                         continue;
9357                     }
9358                     if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
9359                         cpu_isar_feature(aa64_xs, cpu)) {
9360                         /*
9361                          * This is a TLBI insn which has an NXS variant. The
9362                          * NXS variant is at the same encoding except that
9363                          * crn is +1, and has the same behaviour except for
9364                          * fine-grained trapping. Add the NXS insn here and
9365                          * then fall through to add the normal register.
9366                          * add_cpreg_to_hashtable() copies the cpreg struct
9367                          * and name that it is passed, so it's OK to use
9368                          * a local struct here.
9369                          */
9370                         ARMCPRegInfo nxs_ri = *r;
9371                         g_autofree char *name = g_strdup_printf("%sNXS", r->name);
9372 
9373                         assert(state == ARM_CP_STATE_AA64);
9374                         assert(nxs_ri.crn < 0xf);
9375                         nxs_ri.crn++;
9376                         if (nxs_ri.fgt) {
9377                             nxs_ri.fgt |= R_FGT_NXS_MASK;
9378                         }
9379                         add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
9380                                                ARM_CP_SECSTATE_NS,
9381                                                crm, opc1, opc2, name);
9382                     }
9383                     if (state == ARM_CP_STATE_AA32) {
9384                         /*
9385                          * Under AArch32 CP registers can be common
9386                          * (same for secure and non-secure world) or banked.
9387                          */
9388                         char *name;
9389 
9390                         switch (r->secure) {
9391                         case ARM_CP_SECSTATE_S:
9392                         case ARM_CP_SECSTATE_NS:
9393                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9394                                                    r->secure, crm, opc1, opc2,
9395                                                    r->name);
9396                             break;
9397                         case ARM_CP_SECSTATE_BOTH:
9398                             name = g_strdup_printf("%s_S", r->name);
9399                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9400                                                    ARM_CP_SECSTATE_S,
9401                                                    crm, opc1, opc2, name);
9402                             g_free(name);
9403                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9404                                                    ARM_CP_SECSTATE_NS,
9405                                                    crm, opc1, opc2, r->name);
9406                             break;
9407                         default:
9408                             g_assert_not_reached();
9409                         }
9410                     } else {
9411                         /*
9412                          * AArch64 registers get mapped to non-secure instance
9413                          * of AArch32
9414                          */
9415                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9416                                                ARM_CP_SECSTATE_NS,
9417                                                crm, opc1, opc2, r->name);
9418                     }
9419                 }
9420             }
9421         }
9422     }
9423 }
9424 
9425 /* Define a whole list of registers */
9426 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9427                                         void *opaque, size_t len)
9428 {
9429     size_t i;
9430     for (i = 0; i < len; ++i) {
9431         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9432     }
9433 }
9434 
9435 /*
9436  * Modify ARMCPRegInfo for access from userspace.
9437  *
9438  * This is a data driven modification directed by
9439  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9440  * user-space cannot alter any values and dynamic values pertaining to
9441  * execution state are hidden from user space view anyway.
9442  */
9443 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9444                                  const ARMCPRegUserSpaceInfo *mods,
9445                                  size_t mods_len)
9446 {
9447     for (size_t mi = 0; mi < mods_len; ++mi) {
9448         const ARMCPRegUserSpaceInfo *m = mods + mi;
9449         GPatternSpec *pat = NULL;
9450 
9451         if (m->is_glob) {
9452             pat = g_pattern_spec_new(m->name);
9453         }
9454         for (size_t ri = 0; ri < regs_len; ++ri) {
9455             ARMCPRegInfo *r = regs + ri;
9456 
9457             if (pat && g_pattern_match_string(pat, r->name)) {
9458                 r->type = ARM_CP_CONST;
9459                 r->access = PL0U_R;
9460                 r->resetvalue = 0;
9461                 /* continue */
9462             } else if (strcmp(r->name, m->name) == 0) {
9463                 r->type = ARM_CP_CONST;
9464                 r->access = PL0U_R;
9465                 r->resetvalue &= m->exported_bits;
9466                 r->resetvalue |= m->fixed_bits;
9467                 break;
9468             }
9469         }
9470         if (pat) {
9471             g_pattern_spec_free(pat);
9472         }
9473     }
9474 }
9475 
9476 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9477 {
9478     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9479 }
9480 
9481 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9482                          uint64_t value)
9483 {
9484     /* Helper coprocessor write function for write-ignore registers */
9485 }
9486 
9487 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9488 {
9489     /* Helper coprocessor write function for read-as-zero registers */
9490     return 0;
9491 }
9492 
9493 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9494 {
9495     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9496 }
9497 
9498 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9499 {
9500     /*
9501      * Return true if it is not valid for us to switch to
9502      * this CPU mode (ie all the UNPREDICTABLE cases in
9503      * the ARM ARM CPSRWriteByInstr pseudocode).
9504      */
9505 
9506     /* Changes to or from Hyp via MSR and CPS are illegal. */
9507     if (write_type == CPSRWriteByInstr &&
9508         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9509          mode == ARM_CPU_MODE_HYP)) {
9510         return 1;
9511     }
9512 
9513     switch (mode) {
9514     case ARM_CPU_MODE_USR:
9515         return 0;
9516     case ARM_CPU_MODE_SYS:
9517     case ARM_CPU_MODE_SVC:
9518     case ARM_CPU_MODE_ABT:
9519     case ARM_CPU_MODE_UND:
9520     case ARM_CPU_MODE_IRQ:
9521     case ARM_CPU_MODE_FIQ:
9522         /*
9523          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9524          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9525          */
9526         /*
9527          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9528          * and CPS are treated as illegal mode changes.
9529          */
9530         if (write_type == CPSRWriteByInstr &&
9531             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9532             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9533             return 1;
9534         }
9535         return 0;
9536     case ARM_CPU_MODE_HYP:
9537         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9538     case ARM_CPU_MODE_MON:
9539         return arm_current_el(env) < 3;
9540     default:
9541         return 1;
9542     }
9543 }
9544 
9545 uint32_t cpsr_read(CPUARMState *env)
9546 {
9547     int ZF;
9548     ZF = (env->ZF == 0);
9549     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9550         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9551         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9552         | ((env->condexec_bits & 0xfc) << 8)
9553         | (env->GE << 16) | (env->daif & CPSR_AIF);
9554 }
9555 
9556 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9557                 CPSRWriteType write_type)
9558 {
9559     uint32_t changed_daif;
9560     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9561         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9562 
9563     if (mask & CPSR_NZCV) {
9564         env->ZF = (~val) & CPSR_Z;
9565         env->NF = val;
9566         env->CF = (val >> 29) & 1;
9567         env->VF = (val << 3) & 0x80000000;
9568     }
9569     if (mask & CPSR_Q) {
9570         env->QF = ((val & CPSR_Q) != 0);
9571     }
9572     if (mask & CPSR_T) {
9573         env->thumb = ((val & CPSR_T) != 0);
9574     }
9575     if (mask & CPSR_IT_0_1) {
9576         env->condexec_bits &= ~3;
9577         env->condexec_bits |= (val >> 25) & 3;
9578     }
9579     if (mask & CPSR_IT_2_7) {
9580         env->condexec_bits &= 3;
9581         env->condexec_bits |= (val >> 8) & 0xfc;
9582     }
9583     if (mask & CPSR_GE) {
9584         env->GE = (val >> 16) & 0xf;
9585     }
9586 
9587     /*
9588      * In a V7 implementation that includes the security extensions but does
9589      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9590      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9591      * bits respectively.
9592      *
9593      * In a V8 implementation, it is permitted for privileged software to
9594      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9595      */
9596     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9597         arm_feature(env, ARM_FEATURE_EL3) &&
9598         !arm_feature(env, ARM_FEATURE_EL2) &&
9599         !arm_is_secure(env)) {
9600 
9601         changed_daif = (env->daif ^ val) & mask;
9602 
9603         if (changed_daif & CPSR_A) {
9604             /*
9605              * Check to see if we are allowed to change the masking of async
9606              * abort exceptions from a non-secure state.
9607              */
9608             if (!(env->cp15.scr_el3 & SCR_AW)) {
9609                 qemu_log_mask(LOG_GUEST_ERROR,
9610                               "Ignoring attempt to switch CPSR_A flag from "
9611                               "non-secure world with SCR.AW bit clear\n");
9612                 mask &= ~CPSR_A;
9613             }
9614         }
9615 
9616         if (changed_daif & CPSR_F) {
9617             /*
9618              * Check to see if we are allowed to change the masking of FIQ
9619              * exceptions from a non-secure state.
9620              */
9621             if (!(env->cp15.scr_el3 & SCR_FW)) {
9622                 qemu_log_mask(LOG_GUEST_ERROR,
9623                               "Ignoring attempt to switch CPSR_F flag from "
9624                               "non-secure world with SCR.FW bit clear\n");
9625                 mask &= ~CPSR_F;
9626             }
9627 
9628             /*
9629              * Check whether non-maskable FIQ (NMFI) support is enabled.
9630              * If this bit is set software is not allowed to mask
9631              * FIQs, but is allowed to set CPSR_F to 0.
9632              */
9633             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9634                 (val & CPSR_F)) {
9635                 qemu_log_mask(LOG_GUEST_ERROR,
9636                               "Ignoring attempt to enable CPSR_F flag "
9637                               "(non-maskable FIQ [NMFI] support enabled)\n");
9638                 mask &= ~CPSR_F;
9639             }
9640         }
9641     }
9642 
9643     env->daif &= ~(CPSR_AIF & mask);
9644     env->daif |= val & CPSR_AIF & mask;
9645 
9646     if (write_type != CPSRWriteRaw &&
9647         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9648         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9649             /*
9650              * Note that we can only get here in USR mode if this is a
9651              * gdb stub write; for this case we follow the architectural
9652              * behaviour for guest writes in USR mode of ignoring an attempt
9653              * to switch mode. (Those are caught by translate.c for writes
9654              * triggered by guest instructions.)
9655              */
9656             mask &= ~CPSR_M;
9657         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9658             /*
9659              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9660              * v7, and has defined behaviour in v8:
9661              *  + leave CPSR.M untouched
9662              *  + allow changes to the other CPSR fields
9663              *  + set PSTATE.IL
9664              * For user changes via the GDB stub, we don't set PSTATE.IL,
9665              * as this would be unnecessarily harsh for a user error.
9666              */
9667             mask &= ~CPSR_M;
9668             if (write_type != CPSRWriteByGDBStub &&
9669                 arm_feature(env, ARM_FEATURE_V8)) {
9670                 mask |= CPSR_IL;
9671                 val |= CPSR_IL;
9672             }
9673             qemu_log_mask(LOG_GUEST_ERROR,
9674                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9675                           aarch32_mode_name(env->uncached_cpsr),
9676                           aarch32_mode_name(val));
9677         } else {
9678             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9679                           write_type == CPSRWriteExceptionReturn ?
9680                           "Exception return from AArch32" :
9681                           "AArch32 mode switch from",
9682                           aarch32_mode_name(env->uncached_cpsr),
9683                           aarch32_mode_name(val), env->regs[15]);
9684             switch_mode(env, val & CPSR_M);
9685         }
9686     }
9687     mask &= ~CACHED_CPSR_BITS;
9688     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9689     if (tcg_enabled() && rebuild_hflags) {
9690         arm_rebuild_hflags(env);
9691     }
9692 }
9693 
9694 #ifdef CONFIG_USER_ONLY
9695 
9696 static void switch_mode(CPUARMState *env, int mode)
9697 {
9698     ARMCPU *cpu = env_archcpu(env);
9699 
9700     if (mode != ARM_CPU_MODE_USR) {
9701         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9702     }
9703 }
9704 
9705 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9706                                  uint32_t cur_el, bool secure)
9707 {
9708     return 1;
9709 }
9710 
9711 void aarch64_sync_64_to_32(CPUARMState *env)
9712 {
9713     g_assert_not_reached();
9714 }
9715 
9716 #else
9717 
9718 static void switch_mode(CPUARMState *env, int mode)
9719 {
9720     int old_mode;
9721     int i;
9722 
9723     old_mode = env->uncached_cpsr & CPSR_M;
9724     if (mode == old_mode) {
9725         return;
9726     }
9727 
9728     if (old_mode == ARM_CPU_MODE_FIQ) {
9729         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9730         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9731     } else if (mode == ARM_CPU_MODE_FIQ) {
9732         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9733         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9734     }
9735 
9736     i = bank_number(old_mode);
9737     env->banked_r13[i] = env->regs[13];
9738     env->banked_spsr[i] = env->spsr;
9739 
9740     i = bank_number(mode);
9741     env->regs[13] = env->banked_r13[i];
9742     env->spsr = env->banked_spsr[i];
9743 
9744     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9745     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9746 }
9747 
9748 /*
9749  * Physical Interrupt Target EL Lookup Table
9750  *
9751  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9752  *
9753  * The below multi-dimensional table is used for looking up the target
9754  * exception level given numerous condition criteria.  Specifically, the
9755  * target EL is based on SCR and HCR routing controls as well as the
9756  * currently executing EL and secure state.
9757  *
9758  *    Dimensions:
9759  *    target_el_table[2][2][2][2][2][4]
9760  *                    |  |  |  |  |  +--- Current EL
9761  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9762  *                    |  |  |  +--------- HCR mask override
9763  *                    |  |  +------------ SCR exec state control
9764  *                    |  +--------------- SCR mask override
9765  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9766  *
9767  *    The table values are as such:
9768  *    0-3 = EL0-EL3
9769  *     -1 = Cannot occur
9770  *
9771  * The ARM ARM target EL table includes entries indicating that an "exception
9772  * is not taken".  The two cases where this is applicable are:
9773  *    1) An exception is taken from EL3 but the SCR does not have the exception
9774  *    routed to EL3.
9775  *    2) An exception is taken from EL2 but the HCR does not have the exception
9776  *    routed to EL2.
9777  * In these two cases, the below table contain a target of EL1.  This value is
9778  * returned as it is expected that the consumer of the table data will check
9779  * for "target EL >= current EL" to ensure the exception is not taken.
9780  *
9781  *            SCR     HCR
9782  *         64  EA     AMO                 From
9783  *        BIT IRQ     IMO      Non-secure         Secure
9784  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9785  */
9786 static const int8_t target_el_table[2][2][2][2][2][4] = {
9787     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9788        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9789       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9790        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9791      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9792        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9793       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9794        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9795     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9796        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9797       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9798        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9799      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9800        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9801       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9802        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9803 };
9804 
9805 /*
9806  * Determine the target EL for physical exceptions
9807  */
9808 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9809                                  uint32_t cur_el, bool secure)
9810 {
9811     CPUARMState *env = cpu_env(cs);
9812     bool rw;
9813     bool scr;
9814     bool hcr;
9815     int target_el;
9816     /* Is the highest EL AArch64? */
9817     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9818     uint64_t hcr_el2;
9819 
9820     if (arm_feature(env, ARM_FEATURE_EL3)) {
9821         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9822     } else {
9823         /*
9824          * Either EL2 is the highest EL (and so the EL2 register width
9825          * is given by is64); or there is no EL2 or EL3, in which case
9826          * the value of 'rw' does not affect the table lookup anyway.
9827          */
9828         rw = is64;
9829     }
9830 
9831     hcr_el2 = arm_hcr_el2_eff(env);
9832     switch (excp_idx) {
9833     case EXCP_IRQ:
9834     case EXCP_NMI:
9835         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9836         hcr = hcr_el2 & HCR_IMO;
9837         break;
9838     case EXCP_FIQ:
9839         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9840         hcr = hcr_el2 & HCR_FMO;
9841         break;
9842     default:
9843         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9844         hcr = hcr_el2 & HCR_AMO;
9845         break;
9846     };
9847 
9848     /*
9849      * For these purposes, TGE and AMO/IMO/FMO both force the
9850      * interrupt to EL2.  Fold TGE into the bit extracted above.
9851      */
9852     hcr |= (hcr_el2 & HCR_TGE) != 0;
9853 
9854     /* Perform a table-lookup for the target EL given the current state */
9855     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9856 
9857     assert(target_el > 0);
9858 
9859     return target_el;
9860 }
9861 
9862 void arm_log_exception(CPUState *cs)
9863 {
9864     int idx = cs->exception_index;
9865 
9866     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9867         const char *exc = NULL;
9868         static const char * const excnames[] = {
9869             [EXCP_UDEF] = "Undefined Instruction",
9870             [EXCP_SWI] = "SVC",
9871             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9872             [EXCP_DATA_ABORT] = "Data Abort",
9873             [EXCP_IRQ] = "IRQ",
9874             [EXCP_FIQ] = "FIQ",
9875             [EXCP_BKPT] = "Breakpoint",
9876             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9877             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9878             [EXCP_HVC] = "Hypervisor Call",
9879             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9880             [EXCP_SMC] = "Secure Monitor Call",
9881             [EXCP_VIRQ] = "Virtual IRQ",
9882             [EXCP_VFIQ] = "Virtual FIQ",
9883             [EXCP_SEMIHOST] = "Semihosting call",
9884             [EXCP_NOCP] = "v7M NOCP UsageFault",
9885             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9886             [EXCP_STKOF] = "v8M STKOF UsageFault",
9887             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9888             [EXCP_LSERR] = "v8M LSERR UsageFault",
9889             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9890             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9891             [EXCP_VSERR] = "Virtual SERR",
9892             [EXCP_GPC] = "Granule Protection Check",
9893             [EXCP_NMI] = "NMI",
9894             [EXCP_VINMI] = "Virtual IRQ NMI",
9895             [EXCP_VFNMI] = "Virtual FIQ NMI",
9896             [EXCP_MON_TRAP] = "Monitor Trap",
9897         };
9898 
9899         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9900             exc = excnames[idx];
9901         }
9902         if (!exc) {
9903             exc = "unknown";
9904         }
9905         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9906                       idx, exc, cs->cpu_index);
9907     }
9908 }
9909 
9910 /*
9911  * Function used to synchronize QEMU's AArch64 register set with AArch32
9912  * register set.  This is necessary when switching between AArch32 and AArch64
9913  * execution state.
9914  */
9915 void aarch64_sync_32_to_64(CPUARMState *env)
9916 {
9917     int i;
9918     uint32_t mode = env->uncached_cpsr & CPSR_M;
9919 
9920     /* We can blanket copy R[0:7] to X[0:7] */
9921     for (i = 0; i < 8; i++) {
9922         env->xregs[i] = env->regs[i];
9923     }
9924 
9925     /*
9926      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9927      * Otherwise, they come from the banked user regs.
9928      */
9929     if (mode == ARM_CPU_MODE_FIQ) {
9930         for (i = 8; i < 13; i++) {
9931             env->xregs[i] = env->usr_regs[i - 8];
9932         }
9933     } else {
9934         for (i = 8; i < 13; i++) {
9935             env->xregs[i] = env->regs[i];
9936         }
9937     }
9938 
9939     /*
9940      * Registers x13-x23 are the various mode SP and FP registers. Registers
9941      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9942      * from the mode banked register.
9943      */
9944     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9945         env->xregs[13] = env->regs[13];
9946         env->xregs[14] = env->regs[14];
9947     } else {
9948         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9949         /* HYP is an exception in that it is copied from r14 */
9950         if (mode == ARM_CPU_MODE_HYP) {
9951             env->xregs[14] = env->regs[14];
9952         } else {
9953             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9954         }
9955     }
9956 
9957     if (mode == ARM_CPU_MODE_HYP) {
9958         env->xregs[15] = env->regs[13];
9959     } else {
9960         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9961     }
9962 
9963     if (mode == ARM_CPU_MODE_IRQ) {
9964         env->xregs[16] = env->regs[14];
9965         env->xregs[17] = env->regs[13];
9966     } else {
9967         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9968         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9969     }
9970 
9971     if (mode == ARM_CPU_MODE_SVC) {
9972         env->xregs[18] = env->regs[14];
9973         env->xregs[19] = env->regs[13];
9974     } else {
9975         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9976         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9977     }
9978 
9979     if (mode == ARM_CPU_MODE_ABT) {
9980         env->xregs[20] = env->regs[14];
9981         env->xregs[21] = env->regs[13];
9982     } else {
9983         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9984         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9985     }
9986 
9987     if (mode == ARM_CPU_MODE_UND) {
9988         env->xregs[22] = env->regs[14];
9989         env->xregs[23] = env->regs[13];
9990     } else {
9991         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9992         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9993     }
9994 
9995     /*
9996      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9997      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9998      * FIQ bank for r8-r14.
9999      */
10000     if (mode == ARM_CPU_MODE_FIQ) {
10001         for (i = 24; i < 31; i++) {
10002             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10003         }
10004     } else {
10005         for (i = 24; i < 29; i++) {
10006             env->xregs[i] = env->fiq_regs[i - 24];
10007         }
10008         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10009         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10010     }
10011 
10012     env->pc = env->regs[15];
10013 }
10014 
10015 /*
10016  * Function used to synchronize QEMU's AArch32 register set with AArch64
10017  * register set.  This is necessary when switching between AArch32 and AArch64
10018  * execution state.
10019  */
10020 void aarch64_sync_64_to_32(CPUARMState *env)
10021 {
10022     int i;
10023     uint32_t mode = env->uncached_cpsr & CPSR_M;
10024 
10025     /* We can blanket copy X[0:7] to R[0:7] */
10026     for (i = 0; i < 8; i++) {
10027         env->regs[i] = env->xregs[i];
10028     }
10029 
10030     /*
10031      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10032      * Otherwise, we copy x8-x12 into the banked user regs.
10033      */
10034     if (mode == ARM_CPU_MODE_FIQ) {
10035         for (i = 8; i < 13; i++) {
10036             env->usr_regs[i - 8] = env->xregs[i];
10037         }
10038     } else {
10039         for (i = 8; i < 13; i++) {
10040             env->regs[i] = env->xregs[i];
10041         }
10042     }
10043 
10044     /*
10045      * Registers r13 & r14 depend on the current mode.
10046      * If we are in a given mode, we copy the corresponding x registers to r13
10047      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10048      * for the mode.
10049      */
10050     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10051         env->regs[13] = env->xregs[13];
10052         env->regs[14] = env->xregs[14];
10053     } else {
10054         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10055 
10056         /*
10057          * HYP is an exception in that it does not have its own banked r14 but
10058          * shares the USR r14
10059          */
10060         if (mode == ARM_CPU_MODE_HYP) {
10061             env->regs[14] = env->xregs[14];
10062         } else {
10063             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10064         }
10065     }
10066 
10067     if (mode == ARM_CPU_MODE_HYP) {
10068         env->regs[13] = env->xregs[15];
10069     } else {
10070         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10071     }
10072 
10073     if (mode == ARM_CPU_MODE_IRQ) {
10074         env->regs[14] = env->xregs[16];
10075         env->regs[13] = env->xregs[17];
10076     } else {
10077         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10078         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10079     }
10080 
10081     if (mode == ARM_CPU_MODE_SVC) {
10082         env->regs[14] = env->xregs[18];
10083         env->regs[13] = env->xregs[19];
10084     } else {
10085         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10086         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10087     }
10088 
10089     if (mode == ARM_CPU_MODE_ABT) {
10090         env->regs[14] = env->xregs[20];
10091         env->regs[13] = env->xregs[21];
10092     } else {
10093         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10094         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10095     }
10096 
10097     if (mode == ARM_CPU_MODE_UND) {
10098         env->regs[14] = env->xregs[22];
10099         env->regs[13] = env->xregs[23];
10100     } else {
10101         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10102         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10103     }
10104 
10105     /*
10106      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10107      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10108      * FIQ bank for r8-r14.
10109      */
10110     if (mode == ARM_CPU_MODE_FIQ) {
10111         for (i = 24; i < 31; i++) {
10112             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10113         }
10114     } else {
10115         for (i = 24; i < 29; i++) {
10116             env->fiq_regs[i - 24] = env->xregs[i];
10117         }
10118         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10119         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10120     }
10121 
10122     env->regs[15] = env->pc;
10123 }
10124 
10125 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10126                                    uint32_t mask, uint32_t offset,
10127                                    uint32_t newpc)
10128 {
10129     int new_el;
10130 
10131     /* Change the CPU state so as to actually take the exception. */
10132     switch_mode(env, new_mode);
10133 
10134     /*
10135      * For exceptions taken to AArch32 we must clear the SS bit in both
10136      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10137      */
10138     env->pstate &= ~PSTATE_SS;
10139     env->spsr = cpsr_read(env);
10140     /* Clear IT bits.  */
10141     env->condexec_bits = 0;
10142     /* Switch to the new mode, and to the correct instruction set.  */
10143     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10144 
10145     /* This must be after mode switching. */
10146     new_el = arm_current_el(env);
10147 
10148     /* Set new mode endianness */
10149     env->uncached_cpsr &= ~CPSR_E;
10150     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10151         env->uncached_cpsr |= CPSR_E;
10152     }
10153     /* J and IL must always be cleared for exception entry */
10154     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10155     env->daif |= mask;
10156 
10157     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10158         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10159             env->uncached_cpsr |= CPSR_SSBS;
10160         } else {
10161             env->uncached_cpsr &= ~CPSR_SSBS;
10162         }
10163     }
10164 
10165     if (new_mode == ARM_CPU_MODE_HYP) {
10166         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10167         env->elr_el[2] = env->regs[15];
10168     } else {
10169         /* CPSR.PAN is normally preserved preserved unless...  */
10170         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10171             switch (new_el) {
10172             case 3:
10173                 if (!arm_is_secure_below_el3(env)) {
10174                     /* ... the target is EL3, from non-secure state.  */
10175                     env->uncached_cpsr &= ~CPSR_PAN;
10176                     break;
10177                 }
10178                 /* ... the target is EL3, from secure state ... */
10179                 /* fall through */
10180             case 1:
10181                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10182                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10183                     env->uncached_cpsr |= CPSR_PAN;
10184                 }
10185                 break;
10186             }
10187         }
10188         /*
10189          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10190          * and we should just guard the thumb mode on V4
10191          */
10192         if (arm_feature(env, ARM_FEATURE_V4T)) {
10193             env->thumb =
10194                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10195         }
10196         env->regs[14] = env->regs[15] + offset;
10197     }
10198     env->regs[15] = newpc;
10199 
10200     if (tcg_enabled()) {
10201         arm_rebuild_hflags(env);
10202     }
10203 }
10204 
10205 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10206 {
10207     /*
10208      * Handle exception entry to Hyp mode; this is sufficiently
10209      * different to entry to other AArch32 modes that we handle it
10210      * separately here.
10211      *
10212      * The vector table entry used is always the 0x14 Hyp mode entry point,
10213      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10214      * The offset applied to the preferred return address is always zero
10215      * (see DDI0487C.a section G1.12.3).
10216      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10217      */
10218     uint32_t addr, mask;
10219     ARMCPU *cpu = ARM_CPU(cs);
10220     CPUARMState *env = &cpu->env;
10221 
10222     switch (cs->exception_index) {
10223     case EXCP_UDEF:
10224         addr = 0x04;
10225         break;
10226     case EXCP_SWI:
10227         addr = 0x08;
10228         break;
10229     case EXCP_BKPT:
10230         /* Fall through to prefetch abort.  */
10231     case EXCP_PREFETCH_ABORT:
10232         env->cp15.ifar_s = env->exception.vaddress;
10233         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10234                       (uint32_t)env->exception.vaddress);
10235         addr = 0x0c;
10236         break;
10237     case EXCP_DATA_ABORT:
10238         env->cp15.dfar_s = env->exception.vaddress;
10239         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10240                       (uint32_t)env->exception.vaddress);
10241         addr = 0x10;
10242         break;
10243     case EXCP_IRQ:
10244         addr = 0x18;
10245         break;
10246     case EXCP_FIQ:
10247         addr = 0x1c;
10248         break;
10249     case EXCP_HVC:
10250         addr = 0x08;
10251         break;
10252     case EXCP_HYP_TRAP:
10253         addr = 0x14;
10254         break;
10255     default:
10256         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10257     }
10258 
10259     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10260         if (!arm_feature(env, ARM_FEATURE_V8)) {
10261             /*
10262              * QEMU syndrome values are v8-style. v7 has the IL bit
10263              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10264              * If this is a v7 CPU, squash the IL bit in those cases.
10265              */
10266             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10267                 (cs->exception_index == EXCP_DATA_ABORT &&
10268                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10269                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10270                 env->exception.syndrome &= ~ARM_EL_IL;
10271             }
10272         }
10273         env->cp15.esr_el[2] = env->exception.syndrome;
10274     }
10275 
10276     if (arm_current_el(env) != 2 && addr < 0x14) {
10277         addr = 0x14;
10278     }
10279 
10280     mask = 0;
10281     if (!(env->cp15.scr_el3 & SCR_EA)) {
10282         mask |= CPSR_A;
10283     }
10284     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10285         mask |= CPSR_I;
10286     }
10287     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10288         mask |= CPSR_F;
10289     }
10290 
10291     addr += env->cp15.hvbar;
10292 
10293     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10294 }
10295 
10296 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10297 {
10298     ARMCPU *cpu = ARM_CPU(cs);
10299     CPUARMState *env = &cpu->env;
10300     uint32_t addr;
10301     uint32_t mask;
10302     int new_mode;
10303     uint32_t offset;
10304     uint32_t moe;
10305 
10306     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10307     switch (syn_get_ec(env->exception.syndrome)) {
10308     case EC_BREAKPOINT:
10309     case EC_BREAKPOINT_SAME_EL:
10310         moe = 1;
10311         break;
10312     case EC_WATCHPOINT:
10313     case EC_WATCHPOINT_SAME_EL:
10314         moe = 10;
10315         break;
10316     case EC_AA32_BKPT:
10317         moe = 3;
10318         break;
10319     case EC_VECTORCATCH:
10320         moe = 5;
10321         break;
10322     default:
10323         moe = 0;
10324         break;
10325     }
10326 
10327     if (moe) {
10328         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10329     }
10330 
10331     if (env->exception.target_el == 2) {
10332         /* Debug exceptions are reported differently on AArch32 */
10333         switch (syn_get_ec(env->exception.syndrome)) {
10334         case EC_BREAKPOINT:
10335         case EC_BREAKPOINT_SAME_EL:
10336         case EC_AA32_BKPT:
10337         case EC_VECTORCATCH:
10338             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10339                                                      0, 0, 0x22);
10340             break;
10341         case EC_WATCHPOINT:
10342             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10343                                                  EC_DATAABORT);
10344             break;
10345         case EC_WATCHPOINT_SAME_EL:
10346             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10347                                                  EC_DATAABORT_SAME_EL);
10348             break;
10349         }
10350         arm_cpu_do_interrupt_aarch32_hyp(cs);
10351         return;
10352     }
10353 
10354     switch (cs->exception_index) {
10355     case EXCP_UDEF:
10356         new_mode = ARM_CPU_MODE_UND;
10357         addr = 0x04;
10358         mask = CPSR_I;
10359         if (env->thumb) {
10360             offset = 2;
10361         } else {
10362             offset = 4;
10363         }
10364         break;
10365     case EXCP_SWI:
10366         new_mode = ARM_CPU_MODE_SVC;
10367         addr = 0x08;
10368         mask = CPSR_I;
10369         /* The PC already points to the next instruction.  */
10370         offset = 0;
10371         break;
10372     case EXCP_BKPT:
10373         /* Fall through to prefetch abort.  */
10374     case EXCP_PREFETCH_ABORT:
10375         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10376         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10377         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10378                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10379         new_mode = ARM_CPU_MODE_ABT;
10380         addr = 0x0c;
10381         mask = CPSR_A | CPSR_I;
10382         offset = 4;
10383         break;
10384     case EXCP_DATA_ABORT:
10385         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10386         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10387         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10388                       env->exception.fsr,
10389                       (uint32_t)env->exception.vaddress);
10390         new_mode = ARM_CPU_MODE_ABT;
10391         addr = 0x10;
10392         mask = CPSR_A | CPSR_I;
10393         offset = 8;
10394         break;
10395     case EXCP_IRQ:
10396         new_mode = ARM_CPU_MODE_IRQ;
10397         addr = 0x18;
10398         /* Disable IRQ and imprecise data aborts.  */
10399         mask = CPSR_A | CPSR_I;
10400         offset = 4;
10401         if (env->cp15.scr_el3 & SCR_IRQ) {
10402             /* IRQ routed to monitor mode */
10403             new_mode = ARM_CPU_MODE_MON;
10404             mask |= CPSR_F;
10405         }
10406         break;
10407     case EXCP_FIQ:
10408         new_mode = ARM_CPU_MODE_FIQ;
10409         addr = 0x1c;
10410         /* Disable FIQ, IRQ and imprecise data aborts.  */
10411         mask = CPSR_A | CPSR_I | CPSR_F;
10412         if (env->cp15.scr_el3 & SCR_FIQ) {
10413             /* FIQ routed to monitor mode */
10414             new_mode = ARM_CPU_MODE_MON;
10415         }
10416         offset = 4;
10417         break;
10418     case EXCP_VIRQ:
10419         new_mode = ARM_CPU_MODE_IRQ;
10420         addr = 0x18;
10421         /* Disable IRQ and imprecise data aborts.  */
10422         mask = CPSR_A | CPSR_I;
10423         offset = 4;
10424         break;
10425     case EXCP_VFIQ:
10426         new_mode = ARM_CPU_MODE_FIQ;
10427         addr = 0x1c;
10428         /* Disable FIQ, IRQ and imprecise data aborts.  */
10429         mask = CPSR_A | CPSR_I | CPSR_F;
10430         offset = 4;
10431         break;
10432     case EXCP_VSERR:
10433         {
10434             /*
10435              * Note that this is reported as a data abort, but the DFAR
10436              * has an UNKNOWN value.  Construct the SError syndrome from
10437              * AET and ExT fields.
10438              */
10439             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10440 
10441             if (extended_addresses_enabled(env)) {
10442                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10443             } else {
10444                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10445             }
10446             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10447             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10448             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10449                           env->exception.fsr);
10450 
10451             new_mode = ARM_CPU_MODE_ABT;
10452             addr = 0x10;
10453             mask = CPSR_A | CPSR_I;
10454             offset = 8;
10455         }
10456         break;
10457     case EXCP_SMC:
10458         new_mode = ARM_CPU_MODE_MON;
10459         addr = 0x08;
10460         mask = CPSR_A | CPSR_I | CPSR_F;
10461         offset = 0;
10462         break;
10463     case EXCP_MON_TRAP:
10464         new_mode = ARM_CPU_MODE_MON;
10465         addr = 0x04;
10466         mask = CPSR_A | CPSR_I | CPSR_F;
10467         if (env->thumb) {
10468             offset = 2;
10469         } else {
10470             offset = 4;
10471         }
10472         break;
10473     default:
10474         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10475         return; /* Never happens.  Keep compiler happy.  */
10476     }
10477 
10478     if (new_mode == ARM_CPU_MODE_MON) {
10479         addr += env->cp15.mvbar;
10480     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10481         /* High vectors. When enabled, base address cannot be remapped. */
10482         addr += 0xffff0000;
10483     } else {
10484         /*
10485          * ARM v7 architectures provide a vector base address register to remap
10486          * the interrupt vector table.
10487          * This register is only followed in non-monitor mode, and is banked.
10488          * Note: only bits 31:5 are valid.
10489          */
10490         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10491     }
10492 
10493     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10494         env->cp15.scr_el3 &= ~SCR_NS;
10495     }
10496 
10497     take_aarch32_exception(env, new_mode, mask, offset, addr);
10498 }
10499 
10500 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10501 {
10502     /*
10503      * Return the register number of the AArch64 view of the AArch32
10504      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10505      * be that of the AArch32 mode the exception came from.
10506      */
10507     int mode = env->uncached_cpsr & CPSR_M;
10508 
10509     switch (aarch32_reg) {
10510     case 0 ... 7:
10511         return aarch32_reg;
10512     case 8 ... 12:
10513         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10514     case 13:
10515         switch (mode) {
10516         case ARM_CPU_MODE_USR:
10517         case ARM_CPU_MODE_SYS:
10518             return 13;
10519         case ARM_CPU_MODE_HYP:
10520             return 15;
10521         case ARM_CPU_MODE_IRQ:
10522             return 17;
10523         case ARM_CPU_MODE_SVC:
10524             return 19;
10525         case ARM_CPU_MODE_ABT:
10526             return 21;
10527         case ARM_CPU_MODE_UND:
10528             return 23;
10529         case ARM_CPU_MODE_FIQ:
10530             return 29;
10531         default:
10532             g_assert_not_reached();
10533         }
10534     case 14:
10535         switch (mode) {
10536         case ARM_CPU_MODE_USR:
10537         case ARM_CPU_MODE_SYS:
10538         case ARM_CPU_MODE_HYP:
10539             return 14;
10540         case ARM_CPU_MODE_IRQ:
10541             return 16;
10542         case ARM_CPU_MODE_SVC:
10543             return 18;
10544         case ARM_CPU_MODE_ABT:
10545             return 20;
10546         case ARM_CPU_MODE_UND:
10547             return 22;
10548         case ARM_CPU_MODE_FIQ:
10549             return 30;
10550         default:
10551             g_assert_not_reached();
10552         }
10553     case 15:
10554         return 31;
10555     default:
10556         g_assert_not_reached();
10557     }
10558 }
10559 
10560 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10561 {
10562     uint32_t ret = cpsr_read(env);
10563 
10564     /* Move DIT to the correct location for SPSR_ELx */
10565     if (ret & CPSR_DIT) {
10566         ret &= ~CPSR_DIT;
10567         ret |= PSTATE_DIT;
10568     }
10569     /* Merge PSTATE.SS into SPSR_ELx */
10570     ret |= env->pstate & PSTATE_SS;
10571 
10572     return ret;
10573 }
10574 
10575 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10576 {
10577     /* Return true if this syndrome value is a synchronous external abort */
10578     switch (syn_get_ec(syndrome)) {
10579     case EC_INSNABORT:
10580     case EC_INSNABORT_SAME_EL:
10581     case EC_DATAABORT:
10582     case EC_DATAABORT_SAME_EL:
10583         /* Look at fault status code for all the synchronous ext abort cases */
10584         switch (syndrome & 0x3f) {
10585         case 0x10:
10586         case 0x13:
10587         case 0x14:
10588         case 0x15:
10589         case 0x16:
10590         case 0x17:
10591             return true;
10592         default:
10593             return false;
10594         }
10595     default:
10596         return false;
10597     }
10598 }
10599 
10600 /* Handle exception entry to a target EL which is using AArch64 */
10601 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10602 {
10603     ARMCPU *cpu = ARM_CPU(cs);
10604     CPUARMState *env = &cpu->env;
10605     unsigned int new_el = env->exception.target_el;
10606     target_ulong addr = env->cp15.vbar_el[new_el];
10607     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10608     unsigned int old_mode;
10609     unsigned int cur_el = arm_current_el(env);
10610     int rt;
10611 
10612     if (tcg_enabled()) {
10613         /*
10614          * Note that new_el can never be 0.  If cur_el is 0, then
10615          * el0_a64 is is_a64(), else el0_a64 is ignored.
10616          */
10617         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10618     }
10619 
10620     if (cur_el < new_el) {
10621         /*
10622          * Entry vector offset depends on whether the implemented EL
10623          * immediately lower than the target level is using AArch32 or AArch64
10624          */
10625         bool is_aa64;
10626         uint64_t hcr;
10627 
10628         switch (new_el) {
10629         case 3:
10630             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10631             break;
10632         case 2:
10633             hcr = arm_hcr_el2_eff(env);
10634             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10635                 is_aa64 = (hcr & HCR_RW) != 0;
10636                 break;
10637             }
10638             /* fall through */
10639         case 1:
10640             is_aa64 = is_a64(env);
10641             break;
10642         default:
10643             g_assert_not_reached();
10644         }
10645 
10646         if (is_aa64) {
10647             addr += 0x400;
10648         } else {
10649             addr += 0x600;
10650         }
10651     } else if (pstate_read(env) & PSTATE_SP) {
10652         addr += 0x200;
10653     }
10654 
10655     switch (cs->exception_index) {
10656     case EXCP_GPC:
10657         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10658                       env->cp15.mfar_el3);
10659         /* fall through */
10660     case EXCP_PREFETCH_ABORT:
10661     case EXCP_DATA_ABORT:
10662         /*
10663          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10664          * to be taken to the SError vector entrypoint.
10665          */
10666         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10667             syndrome_is_sync_extabt(env->exception.syndrome)) {
10668             addr += 0x180;
10669         }
10670         env->cp15.far_el[new_el] = env->exception.vaddress;
10671         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10672                       env->cp15.far_el[new_el]);
10673         /* fall through */
10674     case EXCP_BKPT:
10675     case EXCP_UDEF:
10676     case EXCP_SWI:
10677     case EXCP_HVC:
10678     case EXCP_HYP_TRAP:
10679     case EXCP_SMC:
10680         switch (syn_get_ec(env->exception.syndrome)) {
10681         case EC_ADVSIMDFPACCESSTRAP:
10682             /*
10683              * QEMU internal FP/SIMD syndromes from AArch32 include the
10684              * TA and coproc fields which are only exposed if the exception
10685              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10686              * AArch64 format syndrome.
10687              */
10688             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10689             break;
10690         case EC_CP14RTTRAP:
10691         case EC_CP15RTTRAP:
10692         case EC_CP14DTTRAP:
10693             /*
10694              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10695              * the raw register field from the insn; when taking this to
10696              * AArch64 we must convert it to the AArch64 view of the register
10697              * number. Notice that we read a 4-bit AArch32 register number and
10698              * write back a 5-bit AArch64 one.
10699              */
10700             rt = extract32(env->exception.syndrome, 5, 4);
10701             rt = aarch64_regnum(env, rt);
10702             env->exception.syndrome = deposit32(env->exception.syndrome,
10703                                                 5, 5, rt);
10704             break;
10705         case EC_CP15RRTTRAP:
10706         case EC_CP14RRTTRAP:
10707             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10708             rt = extract32(env->exception.syndrome, 5, 4);
10709             rt = aarch64_regnum(env, rt);
10710             env->exception.syndrome = deposit32(env->exception.syndrome,
10711                                                 5, 5, rt);
10712             rt = extract32(env->exception.syndrome, 10, 4);
10713             rt = aarch64_regnum(env, rt);
10714             env->exception.syndrome = deposit32(env->exception.syndrome,
10715                                                 10, 5, rt);
10716             break;
10717         }
10718         env->cp15.esr_el[new_el] = env->exception.syndrome;
10719         break;
10720     case EXCP_IRQ:
10721     case EXCP_VIRQ:
10722     case EXCP_NMI:
10723     case EXCP_VINMI:
10724         addr += 0x80;
10725         break;
10726     case EXCP_FIQ:
10727     case EXCP_VFIQ:
10728     case EXCP_VFNMI:
10729         addr += 0x100;
10730         break;
10731     case EXCP_VSERR:
10732         addr += 0x180;
10733         /* Construct the SError syndrome from IDS and ISS fields. */
10734         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10735         env->cp15.esr_el[new_el] = env->exception.syndrome;
10736         break;
10737     default:
10738         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10739     }
10740 
10741     if (is_a64(env)) {
10742         old_mode = pstate_read(env);
10743         aarch64_save_sp(env, arm_current_el(env));
10744         env->elr_el[new_el] = env->pc;
10745 
10746         if (cur_el == 1 && new_el == 1) {
10747             uint64_t hcr = arm_hcr_el2_eff(env);
10748             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10749                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10750                 /*
10751                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10752                  * by setting M[3:2] to 0b10.
10753                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10754                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10755                  */
10756                 old_mode = deposit32(old_mode, 2, 2, 2);
10757             }
10758         }
10759     } else {
10760         old_mode = cpsr_read_for_spsr_elx(env);
10761         env->elr_el[new_el] = env->regs[15];
10762 
10763         aarch64_sync_32_to_64(env);
10764 
10765         env->condexec_bits = 0;
10766     }
10767     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10768 
10769     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10770     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10771                   env->elr_el[new_el]);
10772 
10773     if (cpu_isar_feature(aa64_pan, cpu)) {
10774         /* The value of PSTATE.PAN is normally preserved, except when ... */
10775         new_mode |= old_mode & PSTATE_PAN;
10776         switch (new_el) {
10777         case 2:
10778             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10779             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10780                 != (HCR_E2H | HCR_TGE)) {
10781                 break;
10782             }
10783             /* fall through */
10784         case 1:
10785             /* ... the target is EL1 ... */
10786             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10787             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10788                 new_mode |= PSTATE_PAN;
10789             }
10790             break;
10791         }
10792     }
10793     if (cpu_isar_feature(aa64_mte, cpu)) {
10794         new_mode |= PSTATE_TCO;
10795     }
10796 
10797     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10798         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10799             new_mode |= PSTATE_SSBS;
10800         } else {
10801             new_mode &= ~PSTATE_SSBS;
10802         }
10803     }
10804 
10805     if (cpu_isar_feature(aa64_nmi, cpu)) {
10806         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10807             new_mode |= PSTATE_ALLINT;
10808         } else {
10809             new_mode &= ~PSTATE_ALLINT;
10810         }
10811     }
10812 
10813     pstate_write(env, PSTATE_DAIF | new_mode);
10814     env->aarch64 = true;
10815     aarch64_restore_sp(env, new_el);
10816 
10817     if (tcg_enabled()) {
10818         helper_rebuild_hflags_a64(env, new_el);
10819     }
10820 
10821     env->pc = addr;
10822 
10823     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10824                   new_el, env->pc, pstate_read(env));
10825 }
10826 
10827 /*
10828  * Do semihosting call and set the appropriate return value. All the
10829  * permission and validity checks have been done at translate time.
10830  *
10831  * We only see semihosting exceptions in TCG only as they are not
10832  * trapped to the hypervisor in KVM.
10833  */
10834 #ifdef CONFIG_TCG
10835 static void tcg_handle_semihosting(CPUState *cs)
10836 {
10837     ARMCPU *cpu = ARM_CPU(cs);
10838     CPUARMState *env = &cpu->env;
10839 
10840     if (is_a64(env)) {
10841         qemu_log_mask(CPU_LOG_INT,
10842                       "...handling as semihosting call 0x%" PRIx64 "\n",
10843                       env->xregs[0]);
10844         do_common_semihosting(cs);
10845         env->pc += 4;
10846     } else {
10847         qemu_log_mask(CPU_LOG_INT,
10848                       "...handling as semihosting call 0x%x\n",
10849                       env->regs[0]);
10850         do_common_semihosting(cs);
10851         env->regs[15] += env->thumb ? 2 : 4;
10852     }
10853 }
10854 #endif
10855 
10856 /*
10857  * Handle a CPU exception for A and R profile CPUs.
10858  * Do any appropriate logging, handle PSCI calls, and then hand off
10859  * to the AArch64-entry or AArch32-entry function depending on the
10860  * target exception level's register width.
10861  *
10862  * Note: this is used for both TCG (as the do_interrupt tcg op),
10863  *       and KVM to re-inject guest debug exceptions, and to
10864  *       inject a Synchronous-External-Abort.
10865  */
10866 void arm_cpu_do_interrupt(CPUState *cs)
10867 {
10868     ARMCPU *cpu = ARM_CPU(cs);
10869     CPUARMState *env = &cpu->env;
10870     unsigned int new_el = env->exception.target_el;
10871 
10872     assert(!arm_feature(env, ARM_FEATURE_M));
10873 
10874     arm_log_exception(cs);
10875     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10876                   new_el);
10877     if (qemu_loglevel_mask(CPU_LOG_INT)
10878         && !excp_is_internal(cs->exception_index)) {
10879         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10880                       syn_get_ec(env->exception.syndrome),
10881                       env->exception.syndrome);
10882     }
10883 
10884     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10885         arm_handle_psci_call(cpu);
10886         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10887         return;
10888     }
10889 
10890     /*
10891      * Semihosting semantics depend on the register width of the code
10892      * that caused the exception, not the target exception level, so
10893      * must be handled here.
10894      */
10895 #ifdef CONFIG_TCG
10896     if (cs->exception_index == EXCP_SEMIHOST) {
10897         tcg_handle_semihosting(cs);
10898         return;
10899     }
10900 #endif
10901 
10902     /*
10903      * Hooks may change global state so BQL should be held, also the
10904      * BQL needs to be held for any modification of
10905      * cs->interrupt_request.
10906      */
10907     g_assert(bql_locked());
10908 
10909     arm_call_pre_el_change_hook(cpu);
10910 
10911     assert(!excp_is_internal(cs->exception_index));
10912     if (arm_el_is_aa64(env, new_el)) {
10913         arm_cpu_do_interrupt_aarch64(cs);
10914     } else {
10915         arm_cpu_do_interrupt_aarch32(cs);
10916     }
10917 
10918     arm_call_el_change_hook(cpu);
10919 
10920     if (!kvm_enabled()) {
10921         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10922     }
10923 }
10924 #endif /* !CONFIG_USER_ONLY */
10925 
10926 uint64_t arm_sctlr(CPUARMState *env, int el)
10927 {
10928     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10929     if (el == 0) {
10930         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10931         switch (mmu_idx) {
10932         case ARMMMUIdx_E20_0:
10933             el = 2;
10934             break;
10935         case ARMMMUIdx_E30_0:
10936             el = 3;
10937             break;
10938         default:
10939             el = 1;
10940             break;
10941         }
10942     }
10943     return env->cp15.sctlr_el[el];
10944 }
10945 
10946 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10947 {
10948     if (regime_has_2_ranges(mmu_idx)) {
10949         return extract64(tcr, 37, 2);
10950     } else if (regime_is_stage2(mmu_idx)) {
10951         return 0; /* VTCR_EL2 */
10952     } else {
10953         /* Replicate the single TBI bit so we always have 2 bits.  */
10954         return extract32(tcr, 20, 1) * 3;
10955     }
10956 }
10957 
10958 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10959 {
10960     if (regime_has_2_ranges(mmu_idx)) {
10961         return extract64(tcr, 51, 2);
10962     } else if (regime_is_stage2(mmu_idx)) {
10963         return 0; /* VTCR_EL2 */
10964     } else {
10965         /* Replicate the single TBID bit so we always have 2 bits.  */
10966         return extract32(tcr, 29, 1) * 3;
10967     }
10968 }
10969 
10970 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10971 {
10972     if (regime_has_2_ranges(mmu_idx)) {
10973         return extract64(tcr, 57, 2);
10974     } else {
10975         /* Replicate the single TCMA bit so we always have 2 bits.  */
10976         return extract32(tcr, 30, 1) * 3;
10977     }
10978 }
10979 
10980 static ARMGranuleSize tg0_to_gran_size(int tg)
10981 {
10982     switch (tg) {
10983     case 0:
10984         return Gran4K;
10985     case 1:
10986         return Gran64K;
10987     case 2:
10988         return Gran16K;
10989     default:
10990         return GranInvalid;
10991     }
10992 }
10993 
10994 static ARMGranuleSize tg1_to_gran_size(int tg)
10995 {
10996     switch (tg) {
10997     case 1:
10998         return Gran16K;
10999     case 2:
11000         return Gran4K;
11001     case 3:
11002         return Gran64K;
11003     default:
11004         return GranInvalid;
11005     }
11006 }
11007 
11008 static inline bool have4k(ARMCPU *cpu, bool stage2)
11009 {
11010     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11011         : cpu_isar_feature(aa64_tgran4, cpu);
11012 }
11013 
11014 static inline bool have16k(ARMCPU *cpu, bool stage2)
11015 {
11016     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11017         : cpu_isar_feature(aa64_tgran16, cpu);
11018 }
11019 
11020 static inline bool have64k(ARMCPU *cpu, bool stage2)
11021 {
11022     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11023         : cpu_isar_feature(aa64_tgran64, cpu);
11024 }
11025 
11026 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11027                                          bool stage2)
11028 {
11029     switch (gran) {
11030     case Gran4K:
11031         if (have4k(cpu, stage2)) {
11032             return gran;
11033         }
11034         break;
11035     case Gran16K:
11036         if (have16k(cpu, stage2)) {
11037             return gran;
11038         }
11039         break;
11040     case Gran64K:
11041         if (have64k(cpu, stage2)) {
11042             return gran;
11043         }
11044         break;
11045     case GranInvalid:
11046         break;
11047     }
11048     /*
11049      * If the guest selects a granule size that isn't implemented,
11050      * the architecture requires that we behave as if it selected one
11051      * that is (with an IMPDEF choice of which one to pick). We choose
11052      * to implement the smallest supported granule size.
11053      */
11054     if (have4k(cpu, stage2)) {
11055         return Gran4K;
11056     }
11057     if (have16k(cpu, stage2)) {
11058         return Gran16K;
11059     }
11060     assert(have64k(cpu, stage2));
11061     return Gran64K;
11062 }
11063 
11064 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11065                                    ARMMMUIdx mmu_idx, bool data,
11066                                    bool el1_is_aa32)
11067 {
11068     uint64_t tcr = regime_tcr(env, mmu_idx);
11069     bool epd, hpd, tsz_oob, ds, ha, hd;
11070     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11071     ARMGranuleSize gran;
11072     ARMCPU *cpu = env_archcpu(env);
11073     bool stage2 = regime_is_stage2(mmu_idx);
11074 
11075     if (!regime_has_2_ranges(mmu_idx)) {
11076         select = 0;
11077         tsz = extract32(tcr, 0, 6);
11078         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11079         if (stage2) {
11080             /* VTCR_EL2 */
11081             hpd = false;
11082         } else {
11083             hpd = extract32(tcr, 24, 1);
11084         }
11085         epd = false;
11086         sh = extract32(tcr, 12, 2);
11087         ps = extract32(tcr, 16, 3);
11088         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11089         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11090         ds = extract64(tcr, 32, 1);
11091     } else {
11092         bool e0pd;
11093 
11094         /*
11095          * Bit 55 is always between the two regions, and is canonical for
11096          * determining if address tagging is enabled.
11097          */
11098         select = extract64(va, 55, 1);
11099         if (!select) {
11100             tsz = extract32(tcr, 0, 6);
11101             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11102             epd = extract32(tcr, 7, 1);
11103             sh = extract32(tcr, 12, 2);
11104             hpd = extract64(tcr, 41, 1);
11105             e0pd = extract64(tcr, 55, 1);
11106         } else {
11107             tsz = extract32(tcr, 16, 6);
11108             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11109             epd = extract32(tcr, 23, 1);
11110             sh = extract32(tcr, 28, 2);
11111             hpd = extract64(tcr, 42, 1);
11112             e0pd = extract64(tcr, 56, 1);
11113         }
11114         ps = extract64(tcr, 32, 3);
11115         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11116         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11117         ds = extract64(tcr, 59, 1);
11118 
11119         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11120             regime_is_user(env, mmu_idx)) {
11121             epd = true;
11122         }
11123     }
11124 
11125     gran = sanitize_gran_size(cpu, gran, stage2);
11126 
11127     if (cpu_isar_feature(aa64_st, cpu)) {
11128         max_tsz = 48 - (gran == Gran64K);
11129     } else {
11130         max_tsz = 39;
11131     }
11132 
11133     /*
11134      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11135      * adjust the effective value of DS, as documented.
11136      */
11137     min_tsz = 16;
11138     if (gran == Gran64K) {
11139         if (cpu_isar_feature(aa64_lva, cpu)) {
11140             min_tsz = 12;
11141         }
11142         ds = false;
11143     } else if (ds) {
11144         if (regime_is_stage2(mmu_idx)) {
11145             if (gran == Gran16K) {
11146                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11147             } else {
11148                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11149             }
11150         } else {
11151             if (gran == Gran16K) {
11152                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11153             } else {
11154                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11155             }
11156         }
11157         if (ds) {
11158             min_tsz = 12;
11159         }
11160     }
11161 
11162     if (stage2 && el1_is_aa32) {
11163         /*
11164          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11165          * are loosened: a configured IPA of 40 bits is permitted even if
11166          * the implemented PA is less than that (and so a 40 bit IPA would
11167          * fault for an AArch64 EL1). See R_DTLMN.
11168          */
11169         min_tsz = MIN(min_tsz, 24);
11170     }
11171 
11172     if (tsz > max_tsz) {
11173         tsz = max_tsz;
11174         tsz_oob = true;
11175     } else if (tsz < min_tsz) {
11176         tsz = min_tsz;
11177         tsz_oob = true;
11178     } else {
11179         tsz_oob = false;
11180     }
11181 
11182     /* Present TBI as a composite with TBID.  */
11183     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11184     if (!data) {
11185         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11186     }
11187     tbi = (tbi >> select) & 1;
11188 
11189     return (ARMVAParameters) {
11190         .tsz = tsz,
11191         .ps = ps,
11192         .sh = sh,
11193         .select = select,
11194         .tbi = tbi,
11195         .epd = epd,
11196         .hpd = hpd,
11197         .tsz_oob = tsz_oob,
11198         .ds = ds,
11199         .ha = ha,
11200         .hd = ha && hd,
11201         .gran = gran,
11202     };
11203 }
11204 
11205 
11206 /*
11207  * Return the exception level to which FP-disabled exceptions should
11208  * be taken, or 0 if FP is enabled.
11209  */
11210 int fp_exception_el(CPUARMState *env, int cur_el)
11211 {
11212 #ifndef CONFIG_USER_ONLY
11213     uint64_t hcr_el2;
11214 
11215     /*
11216      * CPACR and the CPTR registers don't exist before v6, so FP is
11217      * always accessible
11218      */
11219     if (!arm_feature(env, ARM_FEATURE_V6)) {
11220         return 0;
11221     }
11222 
11223     if (arm_feature(env, ARM_FEATURE_M)) {
11224         /* CPACR can cause a NOCP UsageFault taken to current security state */
11225         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11226             return 1;
11227         }
11228 
11229         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11230             if (!extract32(env->v7m.nsacr, 10, 1)) {
11231                 /* FP insns cause a NOCP UsageFault taken to Secure */
11232                 return 3;
11233             }
11234         }
11235 
11236         return 0;
11237     }
11238 
11239     hcr_el2 = arm_hcr_el2_eff(env);
11240 
11241     /*
11242      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11243      * 0, 2 : trap EL0 and EL1/PL1 accesses
11244      * 1    : trap only EL0 accesses
11245      * 3    : trap no accesses
11246      * This register is ignored if E2H+TGE are both set.
11247      */
11248     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11249         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11250 
11251         switch (fpen) {
11252         case 1:
11253             if (cur_el != 0) {
11254                 break;
11255             }
11256             /* fall through */
11257         case 0:
11258         case 2:
11259             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11260             if (!arm_el_is_aa64(env, 3)
11261                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11262                 return 3;
11263             }
11264             if (cur_el <= 1) {
11265                 return 1;
11266             }
11267             break;
11268         }
11269     }
11270 
11271     /*
11272      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11273      * to control non-secure access to the FPU. It doesn't have any
11274      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11275      */
11276     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11277          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11278         if (!extract32(env->cp15.nsacr, 10, 1)) {
11279             /* FP insns act as UNDEF */
11280             return cur_el == 2 ? 2 : 1;
11281         }
11282     }
11283 
11284     /*
11285      * CPTR_EL2 is present in v7VE or v8, and changes format
11286      * with HCR_EL2.E2H (regardless of TGE).
11287      */
11288     if (cur_el <= 2) {
11289         if (hcr_el2 & HCR_E2H) {
11290             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11291             case 1:
11292                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11293                     break;
11294                 }
11295                 /* fall through */
11296             case 0:
11297             case 2:
11298                 return 2;
11299             }
11300         } else if (arm_is_el2_enabled(env)) {
11301             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11302                 return 2;
11303             }
11304         }
11305     }
11306 
11307     /* CPTR_EL3 : present in v8 */
11308     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11309         /* Trap all FP ops to EL3 */
11310         return 3;
11311     }
11312 #endif
11313     return 0;
11314 }
11315 
11316 /* Return the exception level we're running at if this is our mmu_idx */
11317 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11318 {
11319     if (mmu_idx & ARM_MMU_IDX_M) {
11320         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11321     }
11322 
11323     switch (mmu_idx) {
11324     case ARMMMUIdx_E10_0:
11325     case ARMMMUIdx_E20_0:
11326     case ARMMMUIdx_E30_0:
11327         return 0;
11328     case ARMMMUIdx_E10_1:
11329     case ARMMMUIdx_E10_1_PAN:
11330         return 1;
11331     case ARMMMUIdx_E2:
11332     case ARMMMUIdx_E20_2:
11333     case ARMMMUIdx_E20_2_PAN:
11334         return 2;
11335     case ARMMMUIdx_E3:
11336     case ARMMMUIdx_E30_3_PAN:
11337         return 3;
11338     default:
11339         g_assert_not_reached();
11340     }
11341 }
11342 
11343 #ifndef CONFIG_TCG
11344 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11345 {
11346     g_assert_not_reached();
11347 }
11348 #endif
11349 
11350 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11351 {
11352     ARMMMUIdx idx;
11353     uint64_t hcr;
11354 
11355     if (arm_feature(env, ARM_FEATURE_M)) {
11356         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11357     }
11358 
11359     /* See ARM pseudo-function ELIsInHost.  */
11360     switch (el) {
11361     case 0:
11362         hcr = arm_hcr_el2_eff(env);
11363         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11364             idx = ARMMMUIdx_E20_0;
11365         } else if (arm_is_secure_below_el3(env) &&
11366                    !arm_el_is_aa64(env, 3)) {
11367             idx = ARMMMUIdx_E30_0;
11368         } else {
11369             idx = ARMMMUIdx_E10_0;
11370         }
11371         break;
11372     case 1:
11373         if (arm_pan_enabled(env)) {
11374             idx = ARMMMUIdx_E10_1_PAN;
11375         } else {
11376             idx = ARMMMUIdx_E10_1;
11377         }
11378         break;
11379     case 2:
11380         /* Note that TGE does not apply at EL2.  */
11381         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11382             if (arm_pan_enabled(env)) {
11383                 idx = ARMMMUIdx_E20_2_PAN;
11384             } else {
11385                 idx = ARMMMUIdx_E20_2;
11386             }
11387         } else {
11388             idx = ARMMMUIdx_E2;
11389         }
11390         break;
11391     case 3:
11392         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11393             return ARMMMUIdx_E30_3_PAN;
11394         }
11395         return ARMMMUIdx_E3;
11396     default:
11397         g_assert_not_reached();
11398     }
11399 
11400     return idx;
11401 }
11402 
11403 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11404 {
11405     return arm_mmu_idx_el(env, arm_current_el(env));
11406 }
11407 
11408 static bool mve_no_pred(CPUARMState *env)
11409 {
11410     /*
11411      * Return true if there is definitely no predication of MVE
11412      * instructions by VPR or LTPSIZE. (Returning false even if there
11413      * isn't any predication is OK; generated code will just be
11414      * a little worse.)
11415      * If the CPU does not implement MVE then this TB flag is always 0.
11416      *
11417      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11418      * logic in gen_update_fp_context() needs to be updated to match.
11419      *
11420      * We do not include the effect of the ECI bits here -- they are
11421      * tracked in other TB flags. This simplifies the logic for
11422      * "when did we emit code that changes the MVE_NO_PRED TB flag
11423      * and thus need to end the TB?".
11424      */
11425     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11426         return false;
11427     }
11428     if (env->v7m.vpr) {
11429         return false;
11430     }
11431     if (env->v7m.ltpsize < 4) {
11432         return false;
11433     }
11434     return true;
11435 }
11436 
11437 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11438                           uint64_t *cs_base, uint32_t *pflags)
11439 {
11440     CPUARMTBFlags flags;
11441 
11442     assert_hflags_rebuild_correctly(env);
11443     flags = env->hflags;
11444 
11445     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11446         *pc = env->pc;
11447         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11448             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11449         }
11450     } else {
11451         *pc = env->regs[15];
11452 
11453         if (arm_feature(env, ARM_FEATURE_M)) {
11454             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11455                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11456                 != env->v7m.secure) {
11457                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11458             }
11459 
11460             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11461                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11462                  (env->v7m.secure &&
11463                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11464                 /*
11465                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11466                  * active FP context; we must create a new FP context before
11467                  * executing any FP insn.
11468                  */
11469                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11470             }
11471 
11472             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11473             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11474                 DP_TBFLAG_M32(flags, LSPACT, 1);
11475             }
11476 
11477             if (mve_no_pred(env)) {
11478                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11479             }
11480         } else {
11481             /*
11482              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11483              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11484              */
11485             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11486                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11487             } else {
11488                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11489                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11490             }
11491             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11492                 DP_TBFLAG_A32(flags, VFPEN, 1);
11493             }
11494         }
11495 
11496         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11497         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11498     }
11499 
11500     /*
11501      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11502      * states defined in the ARM ARM for software singlestep:
11503      *  SS_ACTIVE   PSTATE.SS   State
11504      *     0            x       Inactive (the TB flag for SS is always 0)
11505      *     1            0       Active-pending
11506      *     1            1       Active-not-pending
11507      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11508      */
11509     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11510         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11511     }
11512 
11513     *pflags = flags.flags;
11514     *cs_base = flags.flags2;
11515 }
11516 
11517 #ifdef TARGET_AARCH64
11518 /*
11519  * The manual says that when SVE is enabled and VQ is widened the
11520  * implementation is allowed to zero the previously inaccessible
11521  * portion of the registers.  The corollary to that is that when
11522  * SVE is enabled and VQ is narrowed we are also allowed to zero
11523  * the now inaccessible portion of the registers.
11524  *
11525  * The intent of this is that no predicate bit beyond VQ is ever set.
11526  * Which means that some operations on predicate registers themselves
11527  * may operate on full uint64_t or even unrolled across the maximum
11528  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11529  * may well be cheaper than conditionals to restrict the operation
11530  * to the relevant portion of a uint16_t[16].
11531  */
11532 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11533 {
11534     int i, j;
11535     uint64_t pmask;
11536 
11537     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11538     assert(vq <= env_archcpu(env)->sve_max_vq);
11539 
11540     /* Zap the high bits of the zregs.  */
11541     for (i = 0; i < 32; i++) {
11542         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11543     }
11544 
11545     /* Zap the high bits of the pregs and ffr.  */
11546     pmask = 0;
11547     if (vq & 3) {
11548         pmask = ~(-1ULL << (16 * (vq & 3)));
11549     }
11550     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11551         for (i = 0; i < 17; ++i) {
11552             env->vfp.pregs[i].p[j] &= pmask;
11553         }
11554         pmask = 0;
11555     }
11556 }
11557 
11558 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11559 {
11560     int exc_el;
11561 
11562     if (sm) {
11563         exc_el = sme_exception_el(env, el);
11564     } else {
11565         exc_el = sve_exception_el(env, el);
11566     }
11567     if (exc_el) {
11568         return 0; /* disabled */
11569     }
11570     return sve_vqm1_for_el_sm(env, el, sm);
11571 }
11572 
11573 /*
11574  * Notice a change in SVE vector size when changing EL.
11575  */
11576 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11577                            int new_el, bool el0_a64)
11578 {
11579     ARMCPU *cpu = env_archcpu(env);
11580     int old_len, new_len;
11581     bool old_a64, new_a64, sm;
11582 
11583     /* Nothing to do if no SVE.  */
11584     if (!cpu_isar_feature(aa64_sve, cpu)) {
11585         return;
11586     }
11587 
11588     /* Nothing to do if FP is disabled in either EL.  */
11589     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11590         return;
11591     }
11592 
11593     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11594     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11595 
11596     /*
11597      * Both AArch64.TakeException and AArch64.ExceptionReturn
11598      * invoke ResetSVEState when taking an exception from, or
11599      * returning to, AArch32 state when PSTATE.SM is enabled.
11600      */
11601     sm = FIELD_EX64(env->svcr, SVCR, SM);
11602     if (old_a64 != new_a64 && sm) {
11603         arm_reset_sve_state(env);
11604         return;
11605     }
11606 
11607     /*
11608      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11609      * at ELx, or not available because the EL is in AArch32 state, then
11610      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11611      * has an effective value of 0".
11612      *
11613      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11614      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11615      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11616      * we already have the correct register contents when encountering the
11617      * vq0->vq0 transition between EL0->EL1.
11618      */
11619     old_len = new_len = 0;
11620     if (old_a64) {
11621         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11622     }
11623     if (new_a64) {
11624         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11625     }
11626 
11627     /* When changing vector length, clear inaccessible state.  */
11628     if (new_len < old_len) {
11629         aarch64_sve_narrow_vq(env, new_len + 1);
11630     }
11631 }
11632 #endif
11633 
11634 #ifndef CONFIG_USER_ONLY
11635 ARMSecuritySpace arm_security_space(CPUARMState *env)
11636 {
11637     if (arm_feature(env, ARM_FEATURE_M)) {
11638         return arm_secure_to_space(env->v7m.secure);
11639     }
11640 
11641     /*
11642      * If EL3 is not supported then the secure state is implementation
11643      * defined, in which case QEMU defaults to non-secure.
11644      */
11645     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11646         return ARMSS_NonSecure;
11647     }
11648 
11649     /* Check for AArch64 EL3 or AArch32 Mon. */
11650     if (is_a64(env)) {
11651         if (extract32(env->pstate, 2, 2) == 3) {
11652             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11653                 return ARMSS_Root;
11654             } else {
11655                 return ARMSS_Secure;
11656             }
11657         }
11658     } else {
11659         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11660             return ARMSS_Secure;
11661         }
11662     }
11663 
11664     return arm_security_space_below_el3(env);
11665 }
11666 
11667 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11668 {
11669     assert(!arm_feature(env, ARM_FEATURE_M));
11670 
11671     /*
11672      * If EL3 is not supported then the secure state is implementation
11673      * defined, in which case QEMU defaults to non-secure.
11674      */
11675     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11676         return ARMSS_NonSecure;
11677     }
11678 
11679     /*
11680      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11681      * Ignoring NSE when !NS retains consistency without having to
11682      * modify other predicates.
11683      */
11684     if (!(env->cp15.scr_el3 & SCR_NS)) {
11685         return ARMSS_Secure;
11686     } else if (env->cp15.scr_el3 & SCR_NSE) {
11687         return ARMSS_Realm;
11688     } else {
11689         return ARMSS_NonSecure;
11690     }
11691 }
11692 #endif /* !CONFIG_USER_ONLY */
11693