xref: /qemu/target/arm/helper.c (revision e923f5e1b853ac80e96b416143300af9d189af8e)
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 "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* for crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 #include "target/arm/gtimer.h"
34 
35 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
36 
37 static void switch_mode(CPUARMState *env, int mode);
38 
39 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
40 {
41     assert(ri->fieldoffset);
42     if (cpreg_field_is_64bit(ri)) {
43         return CPREG_FIELD64(env, ri);
44     } else {
45         return CPREG_FIELD32(env, ri);
46     }
47 }
48 
49 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
50 {
51     assert(ri->fieldoffset);
52     if (cpreg_field_is_64bit(ri)) {
53         CPREG_FIELD64(env, ri) = value;
54     } else {
55         CPREG_FIELD32(env, ri) = value;
56     }
57 }
58 
59 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
60 {
61     return (char *)env + ri->fieldoffset;
62 }
63 
64 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66     /* Raw read of a coprocessor register (as needed for migration, etc). */
67     if (ri->type & ARM_CP_CONST) {
68         return ri->resetvalue;
69     } else if (ri->raw_readfn) {
70         return ri->raw_readfn(env, ri);
71     } else if (ri->readfn) {
72         return ri->readfn(env, ri);
73     } else {
74         return raw_read(env, ri);
75     }
76 }
77 
78 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
79                              uint64_t v)
80 {
81     /*
82      * Raw write of a coprocessor register (as needed for migration, etc).
83      * Note that constant registers are treated as write-ignored; the
84      * caller should check for success by whether a readback gives the
85      * value written.
86      */
87     if (ri->type & ARM_CP_CONST) {
88         return;
89     } else if (ri->raw_writefn) {
90         ri->raw_writefn(env, ri, v);
91     } else if (ri->writefn) {
92         ri->writefn(env, ri, v);
93     } else {
94         raw_write(env, ri, v);
95     }
96 }
97 
98 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
99 {
100    /*
101     * Return true if the regdef would cause an assertion if you called
102     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
103     * program bug for it not to have the NO_RAW flag).
104     * NB that returning false here doesn't necessarily mean that calling
105     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
106     * read/write access functions which are safe for raw use" from "has
107     * read/write access functions which have side effects but has forgotten
108     * to provide raw access functions".
109     * The tests here line up with the conditions in read/write_raw_cp_reg()
110     * and assertions in raw_read()/raw_write().
111     */
112     if ((ri->type & ARM_CP_CONST) ||
113         ri->fieldoffset ||
114         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
115         return false;
116     }
117     return true;
118 }
119 
120 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
121 {
122     /* Write the coprocessor state from cpu->env to the (index,value) list. */
123     int i;
124     bool ok = true;
125 
126     for (i = 0; i < cpu->cpreg_array_len; i++) {
127         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
128         const ARMCPRegInfo *ri;
129         uint64_t newval;
130 
131         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
132         if (!ri) {
133             ok = false;
134             continue;
135         }
136         if (ri->type & ARM_CP_NO_RAW) {
137             continue;
138         }
139 
140         newval = read_raw_cp_reg(&cpu->env, ri);
141         if (kvm_sync) {
142             /*
143              * Only sync if the previous list->cpustate sync succeeded.
144              * Rather than tracking the success/failure state for every
145              * item in the list, we just recheck "does the raw write we must
146              * have made in write_list_to_cpustate() read back OK" here.
147              */
148             uint64_t oldval = cpu->cpreg_values[i];
149 
150             if (oldval == newval) {
151                 continue;
152             }
153 
154             write_raw_cp_reg(&cpu->env, ri, oldval);
155             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
156                 continue;
157             }
158 
159             write_raw_cp_reg(&cpu->env, ri, newval);
160         }
161         cpu->cpreg_values[i] = newval;
162     }
163     return ok;
164 }
165 
166 bool write_list_to_cpustate(ARMCPU *cpu)
167 {
168     int i;
169     bool ok = true;
170 
171     for (i = 0; i < cpu->cpreg_array_len; i++) {
172         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
173         uint64_t v = cpu->cpreg_values[i];
174         const ARMCPRegInfo *ri;
175 
176         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
177         if (!ri) {
178             ok = false;
179             continue;
180         }
181         if (ri->type & ARM_CP_NO_RAW) {
182             continue;
183         }
184         /*
185          * Write value and confirm it reads back as written
186          * (to catch read-only registers and partially read-only
187          * registers where the incoming migration value doesn't match)
188          */
189         write_raw_cp_reg(&cpu->env, ri, v);
190         if (read_raw_cp_reg(&cpu->env, ri) != v) {
191             ok = false;
192         }
193     }
194     return ok;
195 }
196 
197 static void add_cpreg_to_list(gpointer key, gpointer opaque)
198 {
199     ARMCPU *cpu = opaque;
200     uint32_t regidx = (uintptr_t)key;
201     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
202 
203     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
204         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
205         /* The value array need not be initialized at this point */
206         cpu->cpreg_array_len++;
207     }
208 }
209 
210 static void count_cpreg(gpointer key, gpointer opaque)
211 {
212     ARMCPU *cpu = opaque;
213     const ARMCPRegInfo *ri;
214 
215     ri = g_hash_table_lookup(cpu->cp_regs, key);
216 
217     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
218         cpu->cpreg_array_len++;
219     }
220 }
221 
222 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
223 {
224     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
225     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
226 
227     if (aidx > bidx) {
228         return 1;
229     }
230     if (aidx < bidx) {
231         return -1;
232     }
233     return 0;
234 }
235 
236 void init_cpreg_list(ARMCPU *cpu)
237 {
238     /*
239      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
240      * Note that we require cpreg_tuples[] to be sorted by key ID.
241      */
242     GList *keys;
243     int arraylen;
244 
245     keys = g_hash_table_get_keys(cpu->cp_regs);
246     keys = g_list_sort(keys, cpreg_key_compare);
247 
248     cpu->cpreg_array_len = 0;
249 
250     g_list_foreach(keys, count_cpreg, cpu);
251 
252     arraylen = cpu->cpreg_array_len;
253     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
254     cpu->cpreg_values = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
258     cpu->cpreg_array_len = 0;
259 
260     g_list_foreach(keys, add_cpreg_to_list, cpu);
261 
262     assert(cpu->cpreg_array_len == arraylen);
263 
264     g_list_free(keys);
265 }
266 
267 static bool arm_pan_enabled(CPUARMState *env)
268 {
269     if (is_a64(env)) {
270         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
271             return false;
272         }
273         return env->pstate & PSTATE_PAN;
274     } else {
275         return env->uncached_cpsr & CPSR_PAN;
276     }
277 }
278 
279 /*
280  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
281  */
282 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
283                                         const ARMCPRegInfo *ri,
284                                         bool isread)
285 {
286     if (!is_a64(env) && arm_current_el(env) == 3 &&
287         arm_is_secure_below_el3(env)) {
288         return CP_ACCESS_TRAP_UNCATEGORIZED;
289     }
290     return CP_ACCESS_OK;
291 }
292 
293 /*
294  * Some secure-only AArch32 registers trap to EL3 if used from
295  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
296  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
297  * We assume that the .access field is set to PL1_RW.
298  */
299 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
300                                             const ARMCPRegInfo *ri,
301                                             bool isread)
302 {
303     if (arm_current_el(env) == 3) {
304         return CP_ACCESS_OK;
305     }
306     if (arm_is_secure_below_el3(env)) {
307         if (env->cp15.scr_el3 & SCR_EEL2) {
308             return CP_ACCESS_TRAP_EL2;
309         }
310         return CP_ACCESS_TRAP_EL3;
311     }
312     /* This will be EL1 NS and EL2 NS, which just UNDEF */
313     return CP_ACCESS_TRAP_UNCATEGORIZED;
314 }
315 
316 /*
317  * Check for traps to performance monitor registers, which are controlled
318  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
319  */
320 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
321                                  bool isread)
322 {
323     int el = arm_current_el(env);
324     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
325 
326     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
327         return CP_ACCESS_TRAP_EL2;
328     }
329     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
330         return CP_ACCESS_TRAP_EL3;
331     }
332     return CP_ACCESS_OK;
333 }
334 
335 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
336 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
337                                bool isread)
338 {
339     if (arm_current_el(env) == 1) {
340         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
341         if (arm_hcr_el2_eff(env) & trap) {
342             return CP_ACCESS_TRAP_EL2;
343         }
344     }
345     return CP_ACCESS_OK;
346 }
347 
348 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
349 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
350                                  bool isread)
351 {
352     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
353         return CP_ACCESS_TRAP_EL2;
354     }
355     return CP_ACCESS_OK;
356 }
357 
358 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
359 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
360                                   bool isread)
361 {
362     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
363         return CP_ACCESS_TRAP_EL2;
364     }
365     return CP_ACCESS_OK;
366 }
367 
368 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
369 {
370     ARMCPU *cpu = env_archcpu(env);
371 
372     raw_write(env, ri, value);
373     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
374 }
375 
376 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
377 {
378     ARMCPU *cpu = env_archcpu(env);
379 
380     if (raw_read(env, ri) != value) {
381         /*
382          * Unlike real hardware the qemu TLB uses virtual addresses,
383          * not modified virtual addresses, so this causes a TLB flush.
384          */
385         tlb_flush(CPU(cpu));
386         raw_write(env, ri, value);
387     }
388 }
389 
390 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
391                              uint64_t value)
392 {
393     ARMCPU *cpu = env_archcpu(env);
394 
395     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
396         && !extended_addresses_enabled(env)) {
397         /*
398          * For VMSA (when not using the LPAE long descriptor page table
399          * format) this register includes the ASID, so do a TLB flush.
400          * For PMSA it is purely a process ID and no action is needed.
401          */
402         tlb_flush(CPU(cpu));
403     }
404     raw_write(env, ri, value);
405 }
406 
407 int alle1_tlbmask(CPUARMState *env)
408 {
409     /*
410      * Note that the 'ALL' scope must invalidate both stage 1 and
411      * stage 2 translations, whereas most other scopes only invalidate
412      * stage 1 translations.
413      *
414      * For AArch32 this is only used for TLBIALLNSNH and VTTBR
415      * writes, so only needs to apply to NS PL1&0, not S PL1&0.
416      */
417     return (ARMMMUIdxBit_E10_1 |
418             ARMMMUIdxBit_E10_1_PAN |
419             ARMMMUIdxBit_E10_0 |
420             ARMMMUIdxBit_Stage2 |
421             ARMMMUIdxBit_Stage2_S);
422 }
423 
424 static const ARMCPRegInfo cp_reginfo[] = {
425     /*
426      * Define the secure and non-secure FCSE identifier CP registers
427      * separately because there is no secure bank in V8 (no _EL3).  This allows
428      * the secure register to be properly reset and migrated. There is also no
429      * v8 EL1 version of the register so the non-secure instance stands alone.
430      */
431     { .name = "FCSEIDR",
432       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
433       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
434       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
435       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
436     { .name = "FCSEIDR_S",
437       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
438       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
439       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
440       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
441     /*
442      * Define the secure and non-secure context identifier CP registers
443      * separately because there is no secure bank in V8 (no _EL3).  This allows
444      * the secure register to be properly reset and migrated.  In the
445      * non-secure case, the 32-bit register will have reset and migration
446      * disabled during registration as it is handled by the 64-bit instance.
447      */
448     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
449       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
450       .access = PL1_RW, .accessfn = access_tvm_trvm,
451       .fgt = FGT_CONTEXTIDR_EL1,
452       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
453       .secure = ARM_CP_SECSTATE_NS,
454       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
455       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
456     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
457       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
458       .access = PL1_RW, .accessfn = access_tvm_trvm,
459       .secure = ARM_CP_SECSTATE_S,
460       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
461       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
462 };
463 
464 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
465     /*
466      * NB: Some of these registers exist in v8 but with more precise
467      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
468      */
469     /* MMU Domain access control / MPU write buffer control */
470     { .name = "DACR",
471       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
472       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
473       .writefn = dacr_write, .raw_writefn = raw_write,
474       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
475                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
476     /*
477      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
478      * For v6 and v5, these mappings are overly broad.
479      */
480     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
481       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
482     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
483       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
484     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
485       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
486     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
487       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
488     /* Cache maintenance ops; some of this space may be overridden later. */
489     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
490       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
491       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
492 };
493 
494 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
495     /*
496      * Not all pre-v6 cores implemented this WFI, so this is slightly
497      * over-broad.
498      */
499     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
500       .access = PL1_W, .type = ARM_CP_WFI },
501 };
502 
503 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
504     /*
505      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
506      * is UNPREDICTABLE; we choose to NOP as most implementations do).
507      */
508     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
509       .access = PL1_W, .type = ARM_CP_WFI },
510     /*
511      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
512      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
513      * OMAPCP will override this space.
514      */
515     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
516       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
517       .resetvalue = 0 },
518     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
519       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
520       .resetvalue = 0 },
521     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
522     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
523       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
524       .resetvalue = 0 },
525     /*
526      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
527      * implementing it as RAZ means the "debug architecture version" bits
528      * will read as a reserved value, which should cause Linux to not try
529      * to use the debug hardware.
530      */
531     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
532       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
533     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
534       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
535     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
536       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
537 };
538 
539 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                         uint64_t value)
541 {
542     uint32_t mask = 0;
543 
544     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
545     if (!arm_feature(env, ARM_FEATURE_V8)) {
546         /*
547          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
548          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
549          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
550          */
551         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
552             /* VFP coprocessor: cp10 & cp11 [23:20] */
553             mask |= R_CPACR_ASEDIS_MASK |
554                     R_CPACR_D32DIS_MASK |
555                     R_CPACR_CP11_MASK |
556                     R_CPACR_CP10_MASK;
557 
558             if (!arm_feature(env, ARM_FEATURE_NEON)) {
559                 /* ASEDIS [31] bit is RAO/WI */
560                 value |= R_CPACR_ASEDIS_MASK;
561             }
562 
563             /*
564              * VFPv3 and upwards with NEON implement 32 double precision
565              * registers (D0-D31).
566              */
567             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
568                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
569                 value |= R_CPACR_D32DIS_MASK;
570             }
571         }
572         value &= mask;
573     }
574 
575     /*
576      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
577      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
578      */
579     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
580         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
581         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
582         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
583     }
584 
585     env->cp15.cpacr_el1 = value;
586 }
587 
588 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
589 {
590     /*
591      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
592      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
593      */
594     uint64_t value = env->cp15.cpacr_el1;
595 
596     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
597         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
598         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
599     }
600     return value;
601 }
602 
603 
604 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
605 {
606     /*
607      * Call cpacr_write() so that we reset with the correct RAO bits set
608      * for our CPU features.
609      */
610     cpacr_write(env, ri, 0);
611 }
612 
613 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
614                                    bool isread)
615 {
616     if (arm_feature(env, ARM_FEATURE_V8)) {
617         /* Check if CPACR accesses are to be trapped to EL2 */
618         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
619             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
620             return CP_ACCESS_TRAP_EL2;
621         /* Check if CPACR accesses are to be trapped to EL3 */
622         } else if (arm_current_el(env) < 3 &&
623                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
624             return CP_ACCESS_TRAP_EL3;
625         }
626     }
627 
628     return CP_ACCESS_OK;
629 }
630 
631 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
632                                   bool isread)
633 {
634     /* Check if CPTR accesses are set to trap to EL3 */
635     if (arm_current_el(env) == 2 &&
636         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
637         return CP_ACCESS_TRAP_EL3;
638     }
639 
640     return CP_ACCESS_OK;
641 }
642 
643 static const ARMCPRegInfo v6_cp_reginfo[] = {
644     /* prefetch by MVA in v6, NOP in v7 */
645     { .name = "MVA_prefetch",
646       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
647       .access = PL1_W, .type = ARM_CP_NOP },
648     /*
649      * We need to break the TB after ISB to execute self-modifying code
650      * correctly and also to take any pending interrupts immediately.
651      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
652      */
653     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
654       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
655     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
656       .access = PL0_W, .type = ARM_CP_NOP },
657     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
658       .access = PL0_W, .type = ARM_CP_NOP },
659     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
660       .access = PL1_RW, .accessfn = access_tvm_trvm,
661       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
662                              offsetof(CPUARMState, cp15.ifar_ns) },
663       .resetvalue = 0, },
664     /*
665      * Watchpoint Fault Address Register : should actually only be present
666      * for 1136, 1176, 11MPCore.
667      */
668     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
669       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
670     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
671       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
672       .fgt = FGT_CPACR_EL1,
673       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
674       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
675       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
676 };
677 
678 typedef struct pm_event {
679     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
680     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
681     bool (*supported)(CPUARMState *);
682     /*
683      * Retrieve the current count of the underlying event. The programmed
684      * counters hold a difference from the return value from this function
685      */
686     uint64_t (*get_count)(CPUARMState *);
687     /*
688      * Return how many nanoseconds it will take (at a minimum) for count events
689      * to occur. A negative value indicates the counter will never overflow, or
690      * that the counter has otherwise arranged for the overflow bit to be set
691      * and the PMU interrupt to be raised on overflow.
692      */
693     int64_t (*ns_per_count)(uint64_t);
694 } pm_event;
695 
696 static bool event_always_supported(CPUARMState *env)
697 {
698     return true;
699 }
700 
701 static uint64_t swinc_get_count(CPUARMState *env)
702 {
703     /*
704      * SW_INCR events are written directly to the pmevcntr's by writes to
705      * PMSWINC, so there is no underlying count maintained by the PMU itself
706      */
707     return 0;
708 }
709 
710 static int64_t swinc_ns_per(uint64_t ignored)
711 {
712     return -1;
713 }
714 
715 /*
716  * Return the underlying cycle count for the PMU cycle counters. If we're in
717  * usermode, simply return 0.
718  */
719 static uint64_t cycles_get_count(CPUARMState *env)
720 {
721 #ifndef CONFIG_USER_ONLY
722     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
723                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
724 #else
725     return cpu_get_host_ticks();
726 #endif
727 }
728 
729 #ifndef CONFIG_USER_ONLY
730 static int64_t cycles_ns_per(uint64_t cycles)
731 {
732     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
733 }
734 
735 static bool instructions_supported(CPUARMState *env)
736 {
737     /* Precise instruction counting */
738     return icount_enabled() == ICOUNT_PRECISE;
739 }
740 
741 static uint64_t instructions_get_count(CPUARMState *env)
742 {
743     assert(icount_enabled() == ICOUNT_PRECISE);
744     return (uint64_t)icount_get_raw();
745 }
746 
747 static int64_t instructions_ns_per(uint64_t icount)
748 {
749     assert(icount_enabled() == ICOUNT_PRECISE);
750     return icount_to_ns((int64_t)icount);
751 }
752 #endif
753 
754 static bool pmuv3p1_events_supported(CPUARMState *env)
755 {
756     /* For events which are supported in any v8.1 PMU */
757     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
758 }
759 
760 static bool pmuv3p4_events_supported(CPUARMState *env)
761 {
762     /* For events which are supported in any v8.1 PMU */
763     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
764 }
765 
766 static uint64_t zero_event_get_count(CPUARMState *env)
767 {
768     /* For events which on QEMU never fire, so their count is always zero */
769     return 0;
770 }
771 
772 static int64_t zero_event_ns_per(uint64_t cycles)
773 {
774     /* An event which never fires can never overflow */
775     return -1;
776 }
777 
778 static const pm_event pm_events[] = {
779     { .number = 0x000, /* SW_INCR */
780       .supported = event_always_supported,
781       .get_count = swinc_get_count,
782       .ns_per_count = swinc_ns_per,
783     },
784 #ifndef CONFIG_USER_ONLY
785     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
786       .supported = instructions_supported,
787       .get_count = instructions_get_count,
788       .ns_per_count = instructions_ns_per,
789     },
790     { .number = 0x011, /* CPU_CYCLES, Cycle */
791       .supported = event_always_supported,
792       .get_count = cycles_get_count,
793       .ns_per_count = cycles_ns_per,
794     },
795 #endif
796     { .number = 0x023, /* STALL_FRONTEND */
797       .supported = pmuv3p1_events_supported,
798       .get_count = zero_event_get_count,
799       .ns_per_count = zero_event_ns_per,
800     },
801     { .number = 0x024, /* STALL_BACKEND */
802       .supported = pmuv3p1_events_supported,
803       .get_count = zero_event_get_count,
804       .ns_per_count = zero_event_ns_per,
805     },
806     { .number = 0x03c, /* STALL */
807       .supported = pmuv3p4_events_supported,
808       .get_count = zero_event_get_count,
809       .ns_per_count = zero_event_ns_per,
810     },
811 };
812 
813 /*
814  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
815  * events (i.e. the statistical profiling extension), this implementation
816  * should first be updated to something sparse instead of the current
817  * supported_event_map[] array.
818  */
819 #define MAX_EVENT_ID 0x3c
820 #define UNSUPPORTED_EVENT UINT16_MAX
821 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
822 
823 /*
824  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
825  * of ARM event numbers to indices in our pm_events array.
826  *
827  * Note: Events in the 0x40XX range are not currently supported.
828  */
829 void pmu_init(ARMCPU *cpu)
830 {
831     unsigned int i;
832 
833     /*
834      * Empty supported_event_map and cpu->pmceid[01] before adding supported
835      * events to them
836      */
837     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
838         supported_event_map[i] = UNSUPPORTED_EVENT;
839     }
840     cpu->pmceid0 = 0;
841     cpu->pmceid1 = 0;
842 
843     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
844         const pm_event *cnt = &pm_events[i];
845         assert(cnt->number <= MAX_EVENT_ID);
846         /* We do not currently support events in the 0x40xx range */
847         assert(cnt->number <= 0x3f);
848 
849         if (cnt->supported(&cpu->env)) {
850             supported_event_map[cnt->number] = i;
851             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
852             if (cnt->number & 0x20) {
853                 cpu->pmceid1 |= event_mask;
854             } else {
855                 cpu->pmceid0 |= event_mask;
856             }
857         }
858     }
859 }
860 
861 /*
862  * Check at runtime whether a PMU event is supported for the current machine
863  */
864 static bool event_supported(uint16_t number)
865 {
866     if (number > MAX_EVENT_ID) {
867         return false;
868     }
869     return supported_event_map[number] != UNSUPPORTED_EVENT;
870 }
871 
872 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
873                                    bool isread)
874 {
875     /*
876      * Performance monitor registers user accessibility is controlled
877      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
878      * trapping to EL2 or EL3 for other accesses.
879      */
880     int el = arm_current_el(env);
881     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
882 
883     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
884         return CP_ACCESS_TRAP;
885     }
886     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
887         return CP_ACCESS_TRAP_EL2;
888     }
889     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
890         return CP_ACCESS_TRAP_EL3;
891     }
892 
893     return CP_ACCESS_OK;
894 }
895 
896 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
897                                            const ARMCPRegInfo *ri,
898                                            bool isread)
899 {
900     /* ER: event counter read trap control */
901     if (arm_feature(env, ARM_FEATURE_V8)
902         && arm_current_el(env) == 0
903         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
904         && isread) {
905         return CP_ACCESS_OK;
906     }
907 
908     return pmreg_access(env, ri, isread);
909 }
910 
911 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
912                                          const ARMCPRegInfo *ri,
913                                          bool isread)
914 {
915     /* SW: software increment write trap control */
916     if (arm_feature(env, ARM_FEATURE_V8)
917         && arm_current_el(env) == 0
918         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
919         && !isread) {
920         return CP_ACCESS_OK;
921     }
922 
923     return pmreg_access(env, ri, isread);
924 }
925 
926 static CPAccessResult pmreg_access_selr(CPUARMState *env,
927                                         const ARMCPRegInfo *ri,
928                                         bool isread)
929 {
930     /* ER: event counter read trap control */
931     if (arm_feature(env, ARM_FEATURE_V8)
932         && arm_current_el(env) == 0
933         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
934         return CP_ACCESS_OK;
935     }
936 
937     return pmreg_access(env, ri, isread);
938 }
939 
940 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
941                                          const ARMCPRegInfo *ri,
942                                          bool isread)
943 {
944     /* CR: cycle counter read trap control */
945     if (arm_feature(env, ARM_FEATURE_V8)
946         && arm_current_el(env) == 0
947         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
948         && isread) {
949         return CP_ACCESS_OK;
950     }
951 
952     return pmreg_access(env, ri, isread);
953 }
954 
955 /*
956  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
957  * We use these to decide whether we need to wrap a write to MDCR_EL2
958  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
959  */
960 #define MDCR_EL2_PMU_ENABLE_BITS \
961     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
962 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
963 
964 /*
965  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
966  * the current EL, security state, and register configuration.
967  */
968 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
969 {
970     uint64_t filter;
971     bool e, p, u, nsk, nsu, nsh, m;
972     bool enabled, prohibited = false, filtered;
973     bool secure = arm_is_secure(env);
974     int el = arm_current_el(env);
975     uint64_t mdcr_el2;
976     uint8_t hpmn;
977 
978     /*
979      * We might be called for M-profile cores where MDCR_EL2 doesn't
980      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
981      * must be before we read that value.
982      */
983     if (!arm_feature(env, ARM_FEATURE_PMU)) {
984         return false;
985     }
986 
987     mdcr_el2 = arm_mdcr_el2_eff(env);
988     hpmn = mdcr_el2 & MDCR_HPMN;
989 
990     if (!arm_feature(env, ARM_FEATURE_EL2) ||
991             (counter < hpmn || counter == 31)) {
992         e = env->cp15.c9_pmcr & PMCRE;
993     } else {
994         e = mdcr_el2 & MDCR_HPME;
995     }
996     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
997 
998     /* Is event counting prohibited? */
999     if (el == 2 && (counter < hpmn || counter == 31)) {
1000         prohibited = mdcr_el2 & MDCR_HPMD;
1001     }
1002     if (secure) {
1003         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1004     }
1005 
1006     if (counter == 31) {
1007         /*
1008          * The cycle counter defaults to running. PMCR.DP says "disable
1009          * the cycle counter when event counting is prohibited".
1010          * Some MDCR bits disable the cycle counter specifically.
1011          */
1012         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1013         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1014             if (secure) {
1015                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1016             }
1017             if (el == 2) {
1018                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1019             }
1020         }
1021     }
1022 
1023     if (counter == 31) {
1024         filter = env->cp15.pmccfiltr_el0;
1025     } else {
1026         filter = env->cp15.c14_pmevtyper[counter];
1027     }
1028 
1029     p   = filter & PMXEVTYPER_P;
1030     u   = filter & PMXEVTYPER_U;
1031     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1032     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1033     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1034     m   = arm_el_is_aa64(env, 1) &&
1035               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1036 
1037     if (el == 0) {
1038         filtered = secure ? u : u != nsu;
1039     } else if (el == 1) {
1040         filtered = secure ? p : p != nsk;
1041     } else if (el == 2) {
1042         filtered = !nsh;
1043     } else { /* EL3 */
1044         filtered = m != p;
1045     }
1046 
1047     if (counter != 31) {
1048         /*
1049          * If not checking PMCCNTR, ensure the counter is setup to an event we
1050          * support
1051          */
1052         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1053         if (!event_supported(event)) {
1054             return false;
1055         }
1056     }
1057 
1058     return enabled && !prohibited && !filtered;
1059 }
1060 
1061 static void pmu_update_irq(CPUARMState *env)
1062 {
1063     ARMCPU *cpu = env_archcpu(env);
1064     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1065             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1066 }
1067 
1068 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1069 {
1070     /*
1071      * Return true if the clock divider is enabled and the cycle counter
1072      * is supposed to tick only once every 64 clock cycles. This is
1073      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1074      * (64-bit) cycle counter PMCR.D has no effect.
1075      */
1076     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1077 }
1078 
1079 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1080 {
1081     /* Return true if the specified event counter is configured to be 64 bit */
1082 
1083     /* This isn't intended to be used with the cycle counter */
1084     assert(counter < 31);
1085 
1086     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1087         return false;
1088     }
1089 
1090     if (arm_feature(env, ARM_FEATURE_EL2)) {
1091         /*
1092          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1093          * current security state, so we don't use arm_mdcr_el2_eff() here.
1094          */
1095         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1096         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1097 
1098         if (counter >= hpmn) {
1099             return hlp;
1100         }
1101     }
1102     return env->cp15.c9_pmcr & PMCRLP;
1103 }
1104 
1105 /*
1106  * Ensure c15_ccnt is the guest-visible count so that operations such as
1107  * enabling/disabling the counter or filtering, modifying the count itself,
1108  * etc. can be done logically. This is essentially a no-op if the counter is
1109  * not enabled at the time of the call.
1110  */
1111 static void pmccntr_op_start(CPUARMState *env)
1112 {
1113     uint64_t cycles = cycles_get_count(env);
1114 
1115     if (pmu_counter_enabled(env, 31)) {
1116         uint64_t eff_cycles = cycles;
1117         if (pmccntr_clockdiv_enabled(env)) {
1118             eff_cycles /= 64;
1119         }
1120 
1121         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1122 
1123         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1124                                  1ull << 63 : 1ull << 31;
1125         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1126             env->cp15.c9_pmovsr |= (1ULL << 31);
1127             pmu_update_irq(env);
1128         }
1129 
1130         env->cp15.c15_ccnt = new_pmccntr;
1131     }
1132     env->cp15.c15_ccnt_delta = cycles;
1133 }
1134 
1135 /*
1136  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1137  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1138  * pmccntr_op_start.
1139  */
1140 static void pmccntr_op_finish(CPUARMState *env)
1141 {
1142     if (pmu_counter_enabled(env, 31)) {
1143 #ifndef CONFIG_USER_ONLY
1144         /* Calculate when the counter will next overflow */
1145         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1146         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1147             remaining_cycles = (uint32_t)remaining_cycles;
1148         }
1149         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1150 
1151         if (overflow_in > 0) {
1152             int64_t overflow_at;
1153 
1154             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1155                                  overflow_in, &overflow_at)) {
1156                 ARMCPU *cpu = env_archcpu(env);
1157                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1158             }
1159         }
1160 #endif
1161 
1162         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1163         if (pmccntr_clockdiv_enabled(env)) {
1164             prev_cycles /= 64;
1165         }
1166         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1167     }
1168 }
1169 
1170 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1171 {
1172 
1173     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1174     uint64_t count = 0;
1175     if (event_supported(event)) {
1176         uint16_t event_idx = supported_event_map[event];
1177         count = pm_events[event_idx].get_count(env);
1178     }
1179 
1180     if (pmu_counter_enabled(env, counter)) {
1181         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1182         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1183             1ULL << 63 : 1ULL << 31;
1184 
1185         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1186             env->cp15.c9_pmovsr |= (1 << counter);
1187             pmu_update_irq(env);
1188         }
1189         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1190     }
1191     env->cp15.c14_pmevcntr_delta[counter] = count;
1192 }
1193 
1194 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1195 {
1196     if (pmu_counter_enabled(env, counter)) {
1197 #ifndef CONFIG_USER_ONLY
1198         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1199         uint16_t event_idx = supported_event_map[event];
1200         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1201         int64_t overflow_in;
1202 
1203         if (!pmevcntr_is_64_bit(env, counter)) {
1204             delta = (uint32_t)delta;
1205         }
1206         overflow_in = pm_events[event_idx].ns_per_count(delta);
1207 
1208         if (overflow_in > 0) {
1209             int64_t overflow_at;
1210 
1211             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1212                                  overflow_in, &overflow_at)) {
1213                 ARMCPU *cpu = env_archcpu(env);
1214                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1215             }
1216         }
1217 #endif
1218 
1219         env->cp15.c14_pmevcntr_delta[counter] -=
1220             env->cp15.c14_pmevcntr[counter];
1221     }
1222 }
1223 
1224 void pmu_op_start(CPUARMState *env)
1225 {
1226     unsigned int i;
1227     pmccntr_op_start(env);
1228     for (i = 0; i < pmu_num_counters(env); i++) {
1229         pmevcntr_op_start(env, i);
1230     }
1231 }
1232 
1233 void pmu_op_finish(CPUARMState *env)
1234 {
1235     unsigned int i;
1236     pmccntr_op_finish(env);
1237     for (i = 0; i < pmu_num_counters(env); i++) {
1238         pmevcntr_op_finish(env, i);
1239     }
1240 }
1241 
1242 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1243 {
1244     pmu_op_start(&cpu->env);
1245 }
1246 
1247 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1248 {
1249     pmu_op_finish(&cpu->env);
1250 }
1251 
1252 void arm_pmu_timer_cb(void *opaque)
1253 {
1254     ARMCPU *cpu = opaque;
1255 
1256     /*
1257      * Update all the counter values based on the current underlying counts,
1258      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1259      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1260      * counter may expire.
1261      */
1262     pmu_op_start(&cpu->env);
1263     pmu_op_finish(&cpu->env);
1264 }
1265 
1266 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1267                        uint64_t value)
1268 {
1269     pmu_op_start(env);
1270 
1271     if (value & PMCRC) {
1272         /* The counter has been reset */
1273         env->cp15.c15_ccnt = 0;
1274     }
1275 
1276     if (value & PMCRP) {
1277         unsigned int i;
1278         for (i = 0; i < pmu_num_counters(env); i++) {
1279             env->cp15.c14_pmevcntr[i] = 0;
1280         }
1281     }
1282 
1283     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1284     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1285 
1286     pmu_op_finish(env);
1287 }
1288 
1289 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1290 {
1291     uint64_t pmcr = env->cp15.c9_pmcr;
1292 
1293     /*
1294      * If EL2 is implemented and enabled for the current security state, reads
1295      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1296      */
1297     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1298         pmcr &= ~PMCRN_MASK;
1299         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1300     }
1301 
1302     return pmcr;
1303 }
1304 
1305 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1306                           uint64_t value)
1307 {
1308     unsigned int i;
1309     uint64_t overflow_mask, new_pmswinc;
1310 
1311     for (i = 0; i < pmu_num_counters(env); i++) {
1312         /* Increment a counter's count iff: */
1313         if ((value & (1 << i)) && /* counter's bit is set */
1314                 /* counter is enabled and not filtered */
1315                 pmu_counter_enabled(env, i) &&
1316                 /* counter is SW_INCR */
1317                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1318             pmevcntr_op_start(env, i);
1319 
1320             /*
1321              * Detect if this write causes an overflow since we can't predict
1322              * PMSWINC overflows like we can for other events
1323              */
1324             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1325 
1326             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1327                 1ULL << 63 : 1ULL << 31;
1328 
1329             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1330                 env->cp15.c9_pmovsr |= (1 << i);
1331                 pmu_update_irq(env);
1332             }
1333 
1334             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1335 
1336             pmevcntr_op_finish(env, i);
1337         }
1338     }
1339 }
1340 
1341 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1342 {
1343     uint64_t ret;
1344     pmccntr_op_start(env);
1345     ret = env->cp15.c15_ccnt;
1346     pmccntr_op_finish(env);
1347     return ret;
1348 }
1349 
1350 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1351                          uint64_t value)
1352 {
1353     /*
1354      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1355      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1356      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1357      * accessed.
1358      */
1359     env->cp15.c9_pmselr = value & 0x1f;
1360 }
1361 
1362 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1363                         uint64_t value)
1364 {
1365     pmccntr_op_start(env);
1366     env->cp15.c15_ccnt = value;
1367     pmccntr_op_finish(env);
1368 }
1369 
1370 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1371                             uint64_t value)
1372 {
1373     uint64_t cur_val = pmccntr_read(env, NULL);
1374 
1375     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1376 }
1377 
1378 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1379                             uint64_t value)
1380 {
1381     pmccntr_op_start(env);
1382     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1383     pmccntr_op_finish(env);
1384 }
1385 
1386 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1387                             uint64_t value)
1388 {
1389     pmccntr_op_start(env);
1390     /* M is not accessible from AArch32 */
1391     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1392         (value & PMCCFILTR);
1393     pmccntr_op_finish(env);
1394 }
1395 
1396 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1397 {
1398     /* M is not visible in AArch32 */
1399     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1400 }
1401 
1402 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1403                             uint64_t value)
1404 {
1405     pmu_op_start(env);
1406     value &= pmu_counter_mask(env);
1407     env->cp15.c9_pmcnten |= value;
1408     pmu_op_finish(env);
1409 }
1410 
1411 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1412                              uint64_t value)
1413 {
1414     pmu_op_start(env);
1415     value &= pmu_counter_mask(env);
1416     env->cp15.c9_pmcnten &= ~value;
1417     pmu_op_finish(env);
1418 }
1419 
1420 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1421                          uint64_t value)
1422 {
1423     value &= pmu_counter_mask(env);
1424     env->cp15.c9_pmovsr &= ~value;
1425     pmu_update_irq(env);
1426 }
1427 
1428 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1429                          uint64_t value)
1430 {
1431     value &= pmu_counter_mask(env);
1432     env->cp15.c9_pmovsr |= value;
1433     pmu_update_irq(env);
1434 }
1435 
1436 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1437                              uint64_t value, const uint8_t counter)
1438 {
1439     if (counter == 31) {
1440         pmccfiltr_write(env, ri, value);
1441     } else if (counter < pmu_num_counters(env)) {
1442         pmevcntr_op_start(env, counter);
1443 
1444         /*
1445          * If this counter's event type is changing, store the current
1446          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1447          * pmevcntr_op_finish has the correct baseline when it converts back to
1448          * a delta.
1449          */
1450         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1451             PMXEVTYPER_EVTCOUNT;
1452         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1453         if (old_event != new_event) {
1454             uint64_t count = 0;
1455             if (event_supported(new_event)) {
1456                 uint16_t event_idx = supported_event_map[new_event];
1457                 count = pm_events[event_idx].get_count(env);
1458             }
1459             env->cp15.c14_pmevcntr_delta[counter] = count;
1460         }
1461 
1462         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1463         pmevcntr_op_finish(env, counter);
1464     }
1465     /*
1466      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1467      * PMSELR value is equal to or greater than the number of implemented
1468      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1469      */
1470 }
1471 
1472 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1473                                const uint8_t counter)
1474 {
1475     if (counter == 31) {
1476         return env->cp15.pmccfiltr_el0;
1477     } else if (counter < pmu_num_counters(env)) {
1478         return env->cp15.c14_pmevtyper[counter];
1479     } else {
1480       /*
1481        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1482        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1483        */
1484         return 0;
1485     }
1486 }
1487 
1488 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1489                               uint64_t value)
1490 {
1491     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1492     pmevtyper_write(env, ri, value, counter);
1493 }
1494 
1495 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1496                                uint64_t value)
1497 {
1498     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1499     env->cp15.c14_pmevtyper[counter] = value;
1500 
1501     /*
1502      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1503      * pmu_op_finish calls when loading saved state for a migration. Because
1504      * we're potentially updating the type of event here, the value written to
1505      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1506      * different counter type. Therefore, we need to set this value to the
1507      * current count for the counter type we're writing so that pmu_op_finish
1508      * has the correct count for its calculation.
1509      */
1510     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1511     if (event_supported(event)) {
1512         uint16_t event_idx = supported_event_map[event];
1513         env->cp15.c14_pmevcntr_delta[counter] =
1514             pm_events[event_idx].get_count(env);
1515     }
1516 }
1517 
1518 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1519 {
1520     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1521     return pmevtyper_read(env, ri, counter);
1522 }
1523 
1524 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1525                              uint64_t value)
1526 {
1527     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1528 }
1529 
1530 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1531 {
1532     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1533 }
1534 
1535 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536                              uint64_t value, uint8_t counter)
1537 {
1538     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1539         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1540         value &= MAKE_64BIT_MASK(0, 32);
1541     }
1542     if (counter < pmu_num_counters(env)) {
1543         pmevcntr_op_start(env, counter);
1544         env->cp15.c14_pmevcntr[counter] = value;
1545         pmevcntr_op_finish(env, counter);
1546     }
1547     /*
1548      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1549      * are CONSTRAINED UNPREDICTABLE.
1550      */
1551 }
1552 
1553 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1554                               uint8_t counter)
1555 {
1556     if (counter < pmu_num_counters(env)) {
1557         uint64_t ret;
1558         pmevcntr_op_start(env, counter);
1559         ret = env->cp15.c14_pmevcntr[counter];
1560         pmevcntr_op_finish(env, counter);
1561         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1562             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1563             ret &= MAKE_64BIT_MASK(0, 32);
1564         }
1565         return ret;
1566     } else {
1567       /*
1568        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1569        * are CONSTRAINED UNPREDICTABLE.
1570        */
1571         return 0;
1572     }
1573 }
1574 
1575 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1576                              uint64_t value)
1577 {
1578     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1579     pmevcntr_write(env, ri, value, counter);
1580 }
1581 
1582 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1583 {
1584     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1585     return pmevcntr_read(env, ri, counter);
1586 }
1587 
1588 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1589                              uint64_t value)
1590 {
1591     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1592     assert(counter < pmu_num_counters(env));
1593     env->cp15.c14_pmevcntr[counter] = value;
1594     pmevcntr_write(env, ri, value, counter);
1595 }
1596 
1597 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1598 {
1599     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1600     assert(counter < pmu_num_counters(env));
1601     return env->cp15.c14_pmevcntr[counter];
1602 }
1603 
1604 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1605                              uint64_t value)
1606 {
1607     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1608 }
1609 
1610 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1611 {
1612     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1613 }
1614 
1615 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1616                             uint64_t value)
1617 {
1618     if (arm_feature(env, ARM_FEATURE_V8)) {
1619         env->cp15.c9_pmuserenr = value & 0xf;
1620     } else {
1621         env->cp15.c9_pmuserenr = value & 1;
1622     }
1623 }
1624 
1625 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626                              uint64_t value)
1627 {
1628     /* We have no event counters so only the C bit can be changed */
1629     value &= pmu_counter_mask(env);
1630     env->cp15.c9_pminten |= value;
1631     pmu_update_irq(env);
1632 }
1633 
1634 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1635                              uint64_t value)
1636 {
1637     value &= pmu_counter_mask(env);
1638     env->cp15.c9_pminten &= ~value;
1639     pmu_update_irq(env);
1640 }
1641 
1642 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1643                        uint64_t value)
1644 {
1645     /*
1646      * Note that even though the AArch64 view of this register has bits
1647      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1648      * architectural requirements for bits which are RES0 only in some
1649      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1650      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1651      */
1652     raw_write(env, ri, value & ~0x1FULL);
1653 }
1654 
1655 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1656 {
1657     /* Begin with base v8.0 state.  */
1658     uint64_t valid_mask = 0x3fff;
1659     ARMCPU *cpu = env_archcpu(env);
1660     uint64_t changed;
1661 
1662     /*
1663      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1664      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1665      * Instead, choose the format based on the mode of EL3.
1666      */
1667     if (arm_el_is_aa64(env, 3)) {
1668         value |= SCR_FW | SCR_AW;      /* RES1 */
1669         valid_mask &= ~SCR_NET;        /* RES0 */
1670 
1671         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1672             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1673             value |= SCR_RW;           /* RAO/WI */
1674         }
1675         if (cpu_isar_feature(aa64_ras, cpu)) {
1676             valid_mask |= SCR_TERR;
1677         }
1678         if (cpu_isar_feature(aa64_lor, cpu)) {
1679             valid_mask |= SCR_TLOR;
1680         }
1681         if (cpu_isar_feature(aa64_pauth, cpu)) {
1682             valid_mask |= SCR_API | SCR_APK;
1683         }
1684         if (cpu_isar_feature(aa64_sel2, cpu)) {
1685             valid_mask |= SCR_EEL2;
1686         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1687             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1688             value |= SCR_NS;
1689         }
1690         if (cpu_isar_feature(aa64_mte, cpu)) {
1691             valid_mask |= SCR_ATA;
1692         }
1693         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1694             valid_mask |= SCR_ENSCXT;
1695         }
1696         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1697             valid_mask |= SCR_EASE | SCR_NMEA;
1698         }
1699         if (cpu_isar_feature(aa64_sme, cpu)) {
1700             valid_mask |= SCR_ENTP2;
1701         }
1702         if (cpu_isar_feature(aa64_hcx, cpu)) {
1703             valid_mask |= SCR_HXEN;
1704         }
1705         if (cpu_isar_feature(aa64_fgt, cpu)) {
1706             valid_mask |= SCR_FGTEN;
1707         }
1708         if (cpu_isar_feature(aa64_rme, cpu)) {
1709             valid_mask |= SCR_NSE | SCR_GPF;
1710         }
1711         if (cpu_isar_feature(aa64_ecv, cpu)) {
1712             valid_mask |= SCR_ECVEN;
1713         }
1714     } else {
1715         valid_mask &= ~(SCR_RW | SCR_ST);
1716         if (cpu_isar_feature(aa32_ras, cpu)) {
1717             valid_mask |= SCR_TERR;
1718         }
1719     }
1720 
1721     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1722         valid_mask &= ~SCR_HCE;
1723 
1724         /*
1725          * On ARMv7, SMD (or SCD as it is called in v7) is only
1726          * supported if EL2 exists. The bit is UNK/SBZP when
1727          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1728          * when EL2 is unavailable.
1729          * On ARMv8, this bit is always available.
1730          */
1731         if (arm_feature(env, ARM_FEATURE_V7) &&
1732             !arm_feature(env, ARM_FEATURE_V8)) {
1733             valid_mask &= ~SCR_SMD;
1734         }
1735     }
1736 
1737     /* Clear all-context RES0 bits.  */
1738     value &= valid_mask;
1739     changed = env->cp15.scr_el3 ^ value;
1740     env->cp15.scr_el3 = value;
1741 
1742     /*
1743      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1744      * we must invalidate all TLBs below EL3.
1745      */
1746     if (changed & (SCR_NS | SCR_NSE)) {
1747         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1748                                            ARMMMUIdxBit_E20_0 |
1749                                            ARMMMUIdxBit_E10_1 |
1750                                            ARMMMUIdxBit_E20_2 |
1751                                            ARMMMUIdxBit_E10_1_PAN |
1752                                            ARMMMUIdxBit_E20_2_PAN |
1753                                            ARMMMUIdxBit_E2));
1754     }
1755 }
1756 
1757 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1758 {
1759     /*
1760      * scr_write will set the RES1 bits on an AArch64-only CPU.
1761      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1762      */
1763     scr_write(env, ri, 0);
1764 }
1765 
1766 static CPAccessResult access_tid4(CPUARMState *env,
1767                                   const ARMCPRegInfo *ri,
1768                                   bool isread)
1769 {
1770     if (arm_current_el(env) == 1 &&
1771         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1772         return CP_ACCESS_TRAP_EL2;
1773     }
1774 
1775     return CP_ACCESS_OK;
1776 }
1777 
1778 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1779 {
1780     ARMCPU *cpu = env_archcpu(env);
1781 
1782     /*
1783      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1784      * bank
1785      */
1786     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1787                                         ri->secure & ARM_CP_SECSTATE_S);
1788 
1789     return cpu->ccsidr[index];
1790 }
1791 
1792 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1793                          uint64_t value)
1794 {
1795     raw_write(env, ri, value & 0xf);
1796 }
1797 
1798 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1799 {
1800     CPUState *cs = env_cpu(env);
1801     bool el1 = arm_current_el(env) == 1;
1802     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1803     uint64_t ret = 0;
1804 
1805     if (hcr_el2 & HCR_IMO) {
1806         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1807             ret |= CPSR_I;
1808         }
1809         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1810             ret |= ISR_IS;
1811             ret |= CPSR_I;
1812         }
1813     } else {
1814         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1815             ret |= CPSR_I;
1816         }
1817 
1818         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1819             ret |= ISR_IS;
1820             ret |= CPSR_I;
1821         }
1822     }
1823 
1824     if (hcr_el2 & HCR_FMO) {
1825         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1826             ret |= CPSR_F;
1827         }
1828         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1829             ret |= ISR_FS;
1830             ret |= CPSR_F;
1831         }
1832     } else {
1833         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1834             ret |= CPSR_F;
1835         }
1836     }
1837 
1838     if (hcr_el2 & HCR_AMO) {
1839         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1840             ret |= CPSR_A;
1841         }
1842     }
1843 
1844     return ret;
1845 }
1846 
1847 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1848                                        bool isread)
1849 {
1850     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1851         return CP_ACCESS_TRAP_EL2;
1852     }
1853 
1854     return CP_ACCESS_OK;
1855 }
1856 
1857 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1858                                        bool isread)
1859 {
1860     if (arm_feature(env, ARM_FEATURE_V8)) {
1861         return access_aa64_tid1(env, ri, isread);
1862     }
1863 
1864     return CP_ACCESS_OK;
1865 }
1866 
1867 static const ARMCPRegInfo v7_cp_reginfo[] = {
1868     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1869     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1870       .access = PL1_W, .type = ARM_CP_NOP },
1871     /*
1872      * Performance monitors are implementation defined in v7,
1873      * but with an ARM recommended set of registers, which we
1874      * follow.
1875      *
1876      * Performance registers fall into three categories:
1877      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1878      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1879      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1880      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1881      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1882      */
1883     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1884       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1885       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1886       .writefn = pmcntenset_write,
1887       .accessfn = pmreg_access,
1888       .fgt = FGT_PMCNTEN,
1889       .raw_writefn = raw_write },
1890     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1891       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1892       .access = PL0_RW, .accessfn = pmreg_access,
1893       .fgt = FGT_PMCNTEN,
1894       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1895       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1896     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1897       .access = PL0_RW,
1898       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1899       .accessfn = pmreg_access,
1900       .fgt = FGT_PMCNTEN,
1901       .writefn = pmcntenclr_write,
1902       .type = ARM_CP_ALIAS | ARM_CP_IO },
1903     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1904       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1905       .access = PL0_RW, .accessfn = pmreg_access,
1906       .fgt = FGT_PMCNTEN,
1907       .type = ARM_CP_ALIAS | ARM_CP_IO,
1908       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1909       .writefn = pmcntenclr_write },
1910     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1911       .access = PL0_RW, .type = ARM_CP_IO,
1912       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1913       .accessfn = pmreg_access,
1914       .fgt = FGT_PMOVS,
1915       .writefn = pmovsr_write,
1916       .raw_writefn = raw_write },
1917     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1918       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1919       .access = PL0_RW, .accessfn = pmreg_access,
1920       .fgt = FGT_PMOVS,
1921       .type = ARM_CP_ALIAS | ARM_CP_IO,
1922       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1923       .writefn = pmovsr_write,
1924       .raw_writefn = raw_write },
1925     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1926       .access = PL0_W, .accessfn = pmreg_access_swinc,
1927       .fgt = FGT_PMSWINC_EL0,
1928       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1929       .writefn = pmswinc_write },
1930     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1931       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1932       .access = PL0_W, .accessfn = pmreg_access_swinc,
1933       .fgt = FGT_PMSWINC_EL0,
1934       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1935       .writefn = pmswinc_write },
1936     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1937       .access = PL0_RW, .type = ARM_CP_ALIAS,
1938       .fgt = FGT_PMSELR_EL0,
1939       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1940       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1941       .raw_writefn = raw_write},
1942     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1943       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1944       .access = PL0_RW, .accessfn = pmreg_access_selr,
1945       .fgt = FGT_PMSELR_EL0,
1946       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1947       .writefn = pmselr_write, .raw_writefn = raw_write, },
1948     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1949       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1950       .fgt = FGT_PMCCNTR_EL0,
1951       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1952       .accessfn = pmreg_access_ccntr },
1953     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1954       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1955       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1956       .fgt = FGT_PMCCNTR_EL0,
1957       .type = ARM_CP_IO,
1958       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1959       .readfn = pmccntr_read, .writefn = pmccntr_write,
1960       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1961     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1962       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1963       .access = PL0_RW, .accessfn = pmreg_access,
1964       .fgt = FGT_PMCCFILTR_EL0,
1965       .type = ARM_CP_ALIAS | ARM_CP_IO,
1966       .resetvalue = 0, },
1967     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1968       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1969       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1970       .access = PL0_RW, .accessfn = pmreg_access,
1971       .fgt = FGT_PMCCFILTR_EL0,
1972       .type = ARM_CP_IO,
1973       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1974       .resetvalue = 0, },
1975     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1976       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1977       .accessfn = pmreg_access,
1978       .fgt = FGT_PMEVTYPERN_EL0,
1979       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1980     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1981       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1982       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1983       .accessfn = pmreg_access,
1984       .fgt = FGT_PMEVTYPERN_EL0,
1985       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1986     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1987       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1988       .accessfn = pmreg_access_xevcntr,
1989       .fgt = FGT_PMEVCNTRN_EL0,
1990       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1991     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1992       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1993       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1994       .accessfn = pmreg_access_xevcntr,
1995       .fgt = FGT_PMEVCNTRN_EL0,
1996       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1997     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1998       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1999       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2000       .resetvalue = 0,
2001       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2002     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2003       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2004       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2005       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2006       .resetvalue = 0,
2007       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2008     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2009       .access = PL1_RW, .accessfn = access_tpm,
2010       .fgt = FGT_PMINTEN,
2011       .type = ARM_CP_ALIAS | ARM_CP_IO,
2012       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2013       .resetvalue = 0,
2014       .writefn = pmintenset_write, .raw_writefn = raw_write },
2015     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2016       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2017       .access = PL1_RW, .accessfn = access_tpm,
2018       .fgt = FGT_PMINTEN,
2019       .type = ARM_CP_IO,
2020       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2021       .writefn = pmintenset_write, .raw_writefn = raw_write,
2022       .resetvalue = 0x0 },
2023     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2024       .access = PL1_RW, .accessfn = access_tpm,
2025       .fgt = FGT_PMINTEN,
2026       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2027       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2028       .writefn = pmintenclr_write, },
2029     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2030       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2031       .access = PL1_RW, .accessfn = access_tpm,
2032       .fgt = FGT_PMINTEN,
2033       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2034       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2035       .writefn = pmintenclr_write },
2036     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2037       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2038       .access = PL1_R,
2039       .accessfn = access_tid4,
2040       .fgt = FGT_CCSIDR_EL1,
2041       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2042     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2043       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2044       .access = PL1_RW,
2045       .accessfn = access_tid4,
2046       .fgt = FGT_CSSELR_EL1,
2047       .writefn = csselr_write, .resetvalue = 0,
2048       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2049                              offsetof(CPUARMState, cp15.csselr_ns) } },
2050     /*
2051      * Auxiliary ID register: this actually has an IMPDEF value but for now
2052      * just RAZ for all cores:
2053      */
2054     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2055       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2056       .access = PL1_R, .type = ARM_CP_CONST,
2057       .accessfn = access_aa64_tid1,
2058       .fgt = FGT_AIDR_EL1,
2059       .resetvalue = 0 },
2060     /*
2061      * Auxiliary fault status registers: these also are IMPDEF, and we
2062      * choose to RAZ/WI for all cores.
2063      */
2064     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2065       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2066       .access = PL1_RW, .accessfn = access_tvm_trvm,
2067       .fgt = FGT_AFSR0_EL1,
2068       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2069       .type = ARM_CP_CONST, .resetvalue = 0 },
2070     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2071       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2072       .access = PL1_RW, .accessfn = access_tvm_trvm,
2073       .fgt = FGT_AFSR1_EL1,
2074       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2075       .type = ARM_CP_CONST, .resetvalue = 0 },
2076     /*
2077      * MAIR can just read-as-written because we don't implement caches
2078      * and so don't need to care about memory attributes.
2079      */
2080     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2081       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2082       .access = PL1_RW, .accessfn = access_tvm_trvm,
2083       .fgt = FGT_MAIR_EL1,
2084       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2085       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2086       .resetvalue = 0 },
2087     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2088       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2089       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2090       .resetvalue = 0 },
2091     /*
2092      * For non-long-descriptor page tables these are PRRR and NMRR;
2093      * regardless they still act as reads-as-written for QEMU.
2094      */
2095      /*
2096       * MAIR0/1 are defined separately from their 64-bit counterpart which
2097       * allows them to assign the correct fieldoffset based on the endianness
2098       * handled in the field definitions.
2099       */
2100     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2101       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2102       .access = PL1_RW, .accessfn = access_tvm_trvm,
2103       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2104                              offsetof(CPUARMState, cp15.mair0_ns) },
2105       .resetfn = arm_cp_reset_ignore },
2106     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2107       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2108       .access = PL1_RW, .accessfn = access_tvm_trvm,
2109       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2110                              offsetof(CPUARMState, cp15.mair1_ns) },
2111       .resetfn = arm_cp_reset_ignore },
2112     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2113       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2114       .fgt = FGT_ISR_EL1,
2115       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2116 };
2117 
2118 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2119     /* PMOVSSET is not implemented in v7 before v7ve */
2120     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2121       .access = PL0_RW, .accessfn = pmreg_access,
2122       .fgt = FGT_PMOVS,
2123       .type = ARM_CP_ALIAS | ARM_CP_IO,
2124       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2125       .writefn = pmovsset_write,
2126       .raw_writefn = raw_write },
2127     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2128       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2129       .access = PL0_RW, .accessfn = pmreg_access,
2130       .fgt = FGT_PMOVS,
2131       .type = ARM_CP_ALIAS | ARM_CP_IO,
2132       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2133       .writefn = pmovsset_write,
2134       .raw_writefn = raw_write },
2135 };
2136 
2137 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2138                         uint64_t value)
2139 {
2140     value &= 1;
2141     env->teecr = value;
2142 }
2143 
2144 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2145                                    bool isread)
2146 {
2147     /*
2148      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2149      * at all, so we don't need to check whether we're v8A.
2150      */
2151     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2152         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2153         return CP_ACCESS_TRAP_EL2;
2154     }
2155     return CP_ACCESS_OK;
2156 }
2157 
2158 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2159                                     bool isread)
2160 {
2161     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2162         return CP_ACCESS_TRAP;
2163     }
2164     return teecr_access(env, ri, isread);
2165 }
2166 
2167 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2168     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2169       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2170       .resetvalue = 0,
2171       .writefn = teecr_write, .accessfn = teecr_access },
2172     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2173       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2174       .accessfn = teehbr_access, .resetvalue = 0 },
2175 };
2176 
2177 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2178     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2179       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2180       .access = PL0_RW,
2181       .fgt = FGT_TPIDR_EL0,
2182       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2183     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2184       .access = PL0_RW,
2185       .fgt = FGT_TPIDR_EL0,
2186       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2187                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2188       .resetfn = arm_cp_reset_ignore },
2189     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2190       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2191       .access = PL0_R | PL1_W,
2192       .fgt = FGT_TPIDRRO_EL0,
2193       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2194       .resetvalue = 0},
2195     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2196       .access = PL0_R | PL1_W,
2197       .fgt = FGT_TPIDRRO_EL0,
2198       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2199                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2200       .resetfn = arm_cp_reset_ignore },
2201     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2202       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2203       .access = PL1_RW,
2204       .fgt = FGT_TPIDR_EL1,
2205       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2206     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2207       .access = PL1_RW,
2208       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2209                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2210       .resetvalue = 0 },
2211 };
2212 
2213 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2214 {
2215     ARMCPU *cpu = env_archcpu(env);
2216 
2217     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2218 }
2219 
2220 #ifndef CONFIG_USER_ONLY
2221 
2222 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2223                                        bool isread)
2224 {
2225     /*
2226      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2227      * Writable only at the highest implemented exception level.
2228      */
2229     int el = arm_current_el(env);
2230     uint64_t hcr;
2231     uint32_t cntkctl;
2232 
2233     switch (el) {
2234     case 0:
2235         hcr = arm_hcr_el2_eff(env);
2236         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2237             cntkctl = env->cp15.cnthctl_el2;
2238         } else {
2239             cntkctl = env->cp15.c14_cntkctl;
2240         }
2241         if (!extract32(cntkctl, 0, 2)) {
2242             return CP_ACCESS_TRAP;
2243         }
2244         break;
2245     case 1:
2246         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2247             arm_is_secure_below_el3(env)) {
2248             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2249             return CP_ACCESS_TRAP_UNCATEGORIZED;
2250         }
2251         break;
2252     case 2:
2253     case 3:
2254         break;
2255     }
2256 
2257     if (!isread && el < arm_highest_el(env)) {
2258         return CP_ACCESS_TRAP_UNCATEGORIZED;
2259     }
2260 
2261     return CP_ACCESS_OK;
2262 }
2263 
2264 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2265                                         bool isread)
2266 {
2267     unsigned int cur_el = arm_current_el(env);
2268     bool has_el2 = arm_is_el2_enabled(env);
2269     uint64_t hcr = arm_hcr_el2_eff(env);
2270 
2271     switch (cur_el) {
2272     case 0:
2273         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2274         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2275             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2276                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2277         }
2278 
2279         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2280         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2281             return CP_ACCESS_TRAP;
2282         }
2283         /* fall through */
2284     case 1:
2285         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2286         if (has_el2 && timeridx == GTIMER_PHYS &&
2287             (hcr & HCR_E2H
2288              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2289              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2290             return CP_ACCESS_TRAP_EL2;
2291         }
2292         if (has_el2 && timeridx == GTIMER_VIRT) {
2293             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2294                 return CP_ACCESS_TRAP_EL2;
2295             }
2296         }
2297         break;
2298     }
2299     return CP_ACCESS_OK;
2300 }
2301 
2302 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2303                                       bool isread)
2304 {
2305     unsigned int cur_el = arm_current_el(env);
2306     bool has_el2 = arm_is_el2_enabled(env);
2307     uint64_t hcr = arm_hcr_el2_eff(env);
2308 
2309     switch (cur_el) {
2310     case 0:
2311         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2312             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2313             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2314                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2315         }
2316 
2317         /*
2318          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2319          * EL0 if EL0[PV]TEN is zero.
2320          */
2321         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2322             return CP_ACCESS_TRAP;
2323         }
2324         /* fall through */
2325 
2326     case 1:
2327         if (has_el2 && timeridx == GTIMER_PHYS) {
2328             if (hcr & HCR_E2H) {
2329                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2330                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2331                     return CP_ACCESS_TRAP_EL2;
2332                 }
2333             } else {
2334                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2335                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2336                     return CP_ACCESS_TRAP_EL2;
2337                 }
2338             }
2339         }
2340         if (has_el2 && timeridx == GTIMER_VIRT) {
2341             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2342                 return CP_ACCESS_TRAP_EL2;
2343             }
2344         }
2345         break;
2346     }
2347     return CP_ACCESS_OK;
2348 }
2349 
2350 static CPAccessResult gt_pct_access(CPUARMState *env,
2351                                     const ARMCPRegInfo *ri,
2352                                     bool isread)
2353 {
2354     return gt_counter_access(env, GTIMER_PHYS, isread);
2355 }
2356 
2357 static CPAccessResult gt_vct_access(CPUARMState *env,
2358                                     const ARMCPRegInfo *ri,
2359                                     bool isread)
2360 {
2361     return gt_counter_access(env, GTIMER_VIRT, isread);
2362 }
2363 
2364 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2365                                        bool isread)
2366 {
2367     return gt_timer_access(env, GTIMER_PHYS, isread);
2368 }
2369 
2370 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2371                                        bool isread)
2372 {
2373     return gt_timer_access(env, GTIMER_VIRT, isread);
2374 }
2375 
2376 static CPAccessResult gt_stimer_access(CPUARMState *env,
2377                                        const ARMCPRegInfo *ri,
2378                                        bool isread)
2379 {
2380     /*
2381      * The AArch64 register view of the secure physical timer is
2382      * always accessible from EL3, and configurably accessible from
2383      * Secure EL1.
2384      */
2385     switch (arm_current_el(env)) {
2386     case 1:
2387         if (!arm_is_secure(env)) {
2388             return CP_ACCESS_TRAP;
2389         }
2390         if (!(env->cp15.scr_el3 & SCR_ST)) {
2391             return CP_ACCESS_TRAP_EL3;
2392         }
2393         return CP_ACCESS_OK;
2394     case 0:
2395     case 2:
2396         return CP_ACCESS_TRAP;
2397     case 3:
2398         return CP_ACCESS_OK;
2399     default:
2400         g_assert_not_reached();
2401     }
2402 }
2403 
2404 uint64_t gt_get_countervalue(CPUARMState *env)
2405 {
2406     ARMCPU *cpu = env_archcpu(env);
2407 
2408     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2409 }
2410 
2411 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2412 {
2413     CPUARMState *env = &cpu->env;
2414     uint64_t cnthctl = env->cp15.cnthctl_el2;
2415     ARMSecuritySpace ss = arm_security_space(env);
2416     /* ISTATUS && !IMASK */
2417     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2418 
2419     /*
2420      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2421      * It is RES0 in Secure and NonSecure state.
2422      */
2423     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2424         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2425          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2426         irqstate = 0;
2427     }
2428 
2429     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2430     trace_arm_gt_update_irq(timeridx, irqstate);
2431 }
2432 
2433 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2434 {
2435     /*
2436      * Changing security state between Root and Secure/NonSecure, which may
2437      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2438      * mask bits. Update the IRQ state accordingly.
2439      */
2440     gt_update_irq(cpu, GTIMER_VIRT);
2441     gt_update_irq(cpu, GTIMER_PHYS);
2442 }
2443 
2444 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2445 {
2446     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2447         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2448         arm_is_el2_enabled(env) &&
2449         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2450         return env->cp15.cntpoff_el2;
2451     }
2452     return 0;
2453 }
2454 
2455 static uint64_t gt_phys_cnt_offset(CPUARMState *env)
2456 {
2457     if (arm_current_el(env) >= 2) {
2458         return 0;
2459     }
2460     return gt_phys_raw_cnt_offset(env);
2461 }
2462 
2463 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2464 {
2465     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2466 
2467     if (gt->ctl & 1) {
2468         /*
2469          * Timer enabled: calculate and set current ISTATUS, irq, and
2470          * reset timer to when ISTATUS next has to change
2471          */
2472         uint64_t offset = timeridx == GTIMER_VIRT ?
2473             cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
2474         uint64_t count = gt_get_countervalue(&cpu->env);
2475         /* Note that this must be unsigned 64 bit arithmetic: */
2476         int istatus = count - offset >= gt->cval;
2477         uint64_t nexttick;
2478 
2479         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2480 
2481         if (istatus) {
2482             /*
2483              * Next transition is when (count - offset) rolls back over to 0.
2484              * If offset > count then this is when count == offset;
2485              * if offset <= count then this is when count == offset + 2^64
2486              * For the latter case we set nexttick to an "as far in future
2487              * as possible" value and let the code below handle it.
2488              */
2489             if (offset > count) {
2490                 nexttick = offset;
2491             } else {
2492                 nexttick = UINT64_MAX;
2493             }
2494         } else {
2495             /*
2496              * Next transition is when (count - offset) == cval, i.e.
2497              * when count == (cval + offset).
2498              * If that would overflow, then again we set up the next interrupt
2499              * for "as far in the future as possible" for the code below.
2500              */
2501             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2502                 nexttick = UINT64_MAX;
2503             }
2504         }
2505         /*
2506          * Note that the desired next expiry time might be beyond the
2507          * signed-64-bit range of a QEMUTimer -- in this case we just
2508          * set the timer for as far in the future as possible. When the
2509          * timer expires we will reset the timer for any remaining period.
2510          */
2511         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2512             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2513         } else {
2514             timer_mod(cpu->gt_timer[timeridx], nexttick);
2515         }
2516         trace_arm_gt_recalc(timeridx, nexttick);
2517     } else {
2518         /* Timer disabled: ISTATUS and timer output always clear */
2519         gt->ctl &= ~4;
2520         timer_del(cpu->gt_timer[timeridx]);
2521         trace_arm_gt_recalc_disabled(timeridx);
2522     }
2523     gt_update_irq(cpu, timeridx);
2524 }
2525 
2526 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2527                            int timeridx)
2528 {
2529     ARMCPU *cpu = env_archcpu(env);
2530 
2531     timer_del(cpu->gt_timer[timeridx]);
2532 }
2533 
2534 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2535 {
2536     return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
2537 }
2538 
2539 uint64_t gt_virt_cnt_offset(CPUARMState *env)
2540 {
2541     uint64_t hcr;
2542 
2543     switch (arm_current_el(env)) {
2544     case 2:
2545         hcr = arm_hcr_el2_eff(env);
2546         if (hcr & HCR_E2H) {
2547             return 0;
2548         }
2549         break;
2550     case 0:
2551         hcr = arm_hcr_el2_eff(env);
2552         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2553             return 0;
2554         }
2555         break;
2556     }
2557 
2558     return env->cp15.cntvoff_el2;
2559 }
2560 
2561 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2562 {
2563     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2564 }
2565 
2566 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2567                           int timeridx,
2568                           uint64_t value)
2569 {
2570     trace_arm_gt_cval_write(timeridx, value);
2571     env->cp15.c14_timer[timeridx].cval = value;
2572     gt_recalc_timer(env_archcpu(env), timeridx);
2573 }
2574 
2575 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2576                              int timeridx)
2577 {
2578     uint64_t offset = 0;
2579 
2580     switch (timeridx) {
2581     case GTIMER_VIRT:
2582     case GTIMER_HYPVIRT:
2583         offset = gt_virt_cnt_offset(env);
2584         break;
2585     case GTIMER_PHYS:
2586         offset = gt_phys_cnt_offset(env);
2587         break;
2588     }
2589 
2590     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2591                       (gt_get_countervalue(env) - offset));
2592 }
2593 
2594 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2595                           int timeridx,
2596                           uint64_t value)
2597 {
2598     uint64_t offset = 0;
2599 
2600     switch (timeridx) {
2601     case GTIMER_VIRT:
2602     case GTIMER_HYPVIRT:
2603         offset = gt_virt_cnt_offset(env);
2604         break;
2605     case GTIMER_PHYS:
2606         offset = gt_phys_cnt_offset(env);
2607         break;
2608     }
2609 
2610     trace_arm_gt_tval_write(timeridx, value);
2611     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2612                                          sextract64(value, 0, 32);
2613     gt_recalc_timer(env_archcpu(env), timeridx);
2614 }
2615 
2616 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2617                          int timeridx,
2618                          uint64_t value)
2619 {
2620     ARMCPU *cpu = env_archcpu(env);
2621     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2622 
2623     trace_arm_gt_ctl_write(timeridx, value);
2624     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2625     if ((oldval ^ value) & 1) {
2626         /* Enable toggled */
2627         gt_recalc_timer(cpu, timeridx);
2628     } else if ((oldval ^ value) & 2) {
2629         /*
2630          * IMASK toggled: don't need to recalculate,
2631          * just set the interrupt line based on ISTATUS
2632          */
2633         trace_arm_gt_imask_toggle(timeridx);
2634         gt_update_irq(cpu, timeridx);
2635     }
2636 }
2637 
2638 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2639 {
2640     gt_timer_reset(env, ri, GTIMER_PHYS);
2641 }
2642 
2643 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2644                                uint64_t value)
2645 {
2646     gt_cval_write(env, ri, GTIMER_PHYS, value);
2647 }
2648 
2649 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650 {
2651     return gt_tval_read(env, ri, GTIMER_PHYS);
2652 }
2653 
2654 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655                                uint64_t value)
2656 {
2657     gt_tval_write(env, ri, GTIMER_PHYS, value);
2658 }
2659 
2660 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2661                               uint64_t value)
2662 {
2663     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2664 }
2665 
2666 static int gt_phys_redir_timeridx(CPUARMState *env)
2667 {
2668     switch (arm_mmu_idx(env)) {
2669     case ARMMMUIdx_E20_0:
2670     case ARMMMUIdx_E20_2:
2671     case ARMMMUIdx_E20_2_PAN:
2672         return GTIMER_HYP;
2673     default:
2674         return GTIMER_PHYS;
2675     }
2676 }
2677 
2678 static int gt_virt_redir_timeridx(CPUARMState *env)
2679 {
2680     switch (arm_mmu_idx(env)) {
2681     case ARMMMUIdx_E20_0:
2682     case ARMMMUIdx_E20_2:
2683     case ARMMMUIdx_E20_2_PAN:
2684         return GTIMER_HYPVIRT;
2685     default:
2686         return GTIMER_VIRT;
2687     }
2688 }
2689 
2690 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2691                                         const ARMCPRegInfo *ri)
2692 {
2693     int timeridx = gt_phys_redir_timeridx(env);
2694     return env->cp15.c14_timer[timeridx].cval;
2695 }
2696 
2697 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2698                                      uint64_t value)
2699 {
2700     int timeridx = gt_phys_redir_timeridx(env);
2701     gt_cval_write(env, ri, timeridx, value);
2702 }
2703 
2704 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2705                                         const ARMCPRegInfo *ri)
2706 {
2707     int timeridx = gt_phys_redir_timeridx(env);
2708     return gt_tval_read(env, ri, timeridx);
2709 }
2710 
2711 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2712                                      uint64_t value)
2713 {
2714     int timeridx = gt_phys_redir_timeridx(env);
2715     gt_tval_write(env, ri, timeridx, value);
2716 }
2717 
2718 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2719                                        const ARMCPRegInfo *ri)
2720 {
2721     int timeridx = gt_phys_redir_timeridx(env);
2722     return env->cp15.c14_timer[timeridx].ctl;
2723 }
2724 
2725 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2726                                     uint64_t value)
2727 {
2728     int timeridx = gt_phys_redir_timeridx(env);
2729     gt_ctl_write(env, ri, timeridx, value);
2730 }
2731 
2732 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2733 {
2734     gt_timer_reset(env, ri, GTIMER_VIRT);
2735 }
2736 
2737 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2738                                uint64_t value)
2739 {
2740     gt_cval_write(env, ri, GTIMER_VIRT, value);
2741 }
2742 
2743 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2744 {
2745     return gt_tval_read(env, ri, GTIMER_VIRT);
2746 }
2747 
2748 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2749                                uint64_t value)
2750 {
2751     gt_tval_write(env, ri, GTIMER_VIRT, value);
2752 }
2753 
2754 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2755                               uint64_t value)
2756 {
2757     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2758 }
2759 
2760 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2761                              uint64_t value)
2762 {
2763     ARMCPU *cpu = env_archcpu(env);
2764     uint32_t oldval = env->cp15.cnthctl_el2;
2765     uint32_t valid_mask =
2766         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2767         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2768         R_CNTHCTL_EVNTEN_MASK |
2769         R_CNTHCTL_EVNTDIR_MASK |
2770         R_CNTHCTL_EVNTI_MASK |
2771         R_CNTHCTL_EL0VTEN_MASK |
2772         R_CNTHCTL_EL0PTEN_MASK |
2773         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2774         R_CNTHCTL_EL1PTEN_MASK;
2775 
2776     if (cpu_isar_feature(aa64_rme, cpu)) {
2777         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2778     }
2779     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2780         valid_mask |=
2781             R_CNTHCTL_EL1TVT_MASK |
2782             R_CNTHCTL_EL1TVCT_MASK |
2783             R_CNTHCTL_EL1NVPCT_MASK |
2784             R_CNTHCTL_EL1NVVCT_MASK |
2785             R_CNTHCTL_EVNTIS_MASK;
2786     }
2787     if (cpu_isar_feature(aa64_ecv, cpu)) {
2788         valid_mask |= R_CNTHCTL_ECV_MASK;
2789     }
2790 
2791     /* Clear RES0 bits */
2792     value &= valid_mask;
2793 
2794     raw_write(env, ri, value);
2795 
2796     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2797         gt_update_irq(cpu, GTIMER_VIRT);
2798     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2799         gt_update_irq(cpu, GTIMER_PHYS);
2800     }
2801 }
2802 
2803 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2804                               uint64_t value)
2805 {
2806     ARMCPU *cpu = env_archcpu(env);
2807 
2808     trace_arm_gt_cntvoff_write(value);
2809     raw_write(env, ri, value);
2810     gt_recalc_timer(cpu, GTIMER_VIRT);
2811 }
2812 
2813 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2814                                         const ARMCPRegInfo *ri)
2815 {
2816     int timeridx = gt_virt_redir_timeridx(env);
2817     return env->cp15.c14_timer[timeridx].cval;
2818 }
2819 
2820 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2821                                      uint64_t value)
2822 {
2823     int timeridx = gt_virt_redir_timeridx(env);
2824     gt_cval_write(env, ri, timeridx, value);
2825 }
2826 
2827 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2828                                         const ARMCPRegInfo *ri)
2829 {
2830     int timeridx = gt_virt_redir_timeridx(env);
2831     return gt_tval_read(env, ri, timeridx);
2832 }
2833 
2834 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2835                                      uint64_t value)
2836 {
2837     int timeridx = gt_virt_redir_timeridx(env);
2838     gt_tval_write(env, ri, timeridx, value);
2839 }
2840 
2841 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2842                                        const ARMCPRegInfo *ri)
2843 {
2844     int timeridx = gt_virt_redir_timeridx(env);
2845     return env->cp15.c14_timer[timeridx].ctl;
2846 }
2847 
2848 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2849                                     uint64_t value)
2850 {
2851     int timeridx = gt_virt_redir_timeridx(env);
2852     gt_ctl_write(env, ri, timeridx, value);
2853 }
2854 
2855 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2856 {
2857     gt_timer_reset(env, ri, GTIMER_HYP);
2858 }
2859 
2860 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861                               uint64_t value)
2862 {
2863     gt_cval_write(env, ri, GTIMER_HYP, value);
2864 }
2865 
2866 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2867 {
2868     return gt_tval_read(env, ri, GTIMER_HYP);
2869 }
2870 
2871 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2872                               uint64_t value)
2873 {
2874     gt_tval_write(env, ri, GTIMER_HYP, value);
2875 }
2876 
2877 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2878                               uint64_t value)
2879 {
2880     gt_ctl_write(env, ri, GTIMER_HYP, value);
2881 }
2882 
2883 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2884 {
2885     gt_timer_reset(env, ri, GTIMER_SEC);
2886 }
2887 
2888 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2889                               uint64_t value)
2890 {
2891     gt_cval_write(env, ri, GTIMER_SEC, value);
2892 }
2893 
2894 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2895 {
2896     return gt_tval_read(env, ri, GTIMER_SEC);
2897 }
2898 
2899 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2900                               uint64_t value)
2901 {
2902     gt_tval_write(env, ri, GTIMER_SEC, value);
2903 }
2904 
2905 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2906                               uint64_t value)
2907 {
2908     gt_ctl_write(env, ri, GTIMER_SEC, value);
2909 }
2910 
2911 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2912 {
2913     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2914 }
2915 
2916 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2917                              uint64_t value)
2918 {
2919     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2920 }
2921 
2922 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2923 {
2924     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2925 }
2926 
2927 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2928                              uint64_t value)
2929 {
2930     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2931 }
2932 
2933 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2934                             uint64_t value)
2935 {
2936     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2937 }
2938 
2939 void arm_gt_ptimer_cb(void *opaque)
2940 {
2941     ARMCPU *cpu = opaque;
2942 
2943     gt_recalc_timer(cpu, GTIMER_PHYS);
2944 }
2945 
2946 void arm_gt_vtimer_cb(void *opaque)
2947 {
2948     ARMCPU *cpu = opaque;
2949 
2950     gt_recalc_timer(cpu, GTIMER_VIRT);
2951 }
2952 
2953 void arm_gt_htimer_cb(void *opaque)
2954 {
2955     ARMCPU *cpu = opaque;
2956 
2957     gt_recalc_timer(cpu, GTIMER_HYP);
2958 }
2959 
2960 void arm_gt_stimer_cb(void *opaque)
2961 {
2962     ARMCPU *cpu = opaque;
2963 
2964     gt_recalc_timer(cpu, GTIMER_SEC);
2965 }
2966 
2967 void arm_gt_hvtimer_cb(void *opaque)
2968 {
2969     ARMCPU *cpu = opaque;
2970 
2971     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2972 }
2973 
2974 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2975     /*
2976      * Note that CNTFRQ is purely reads-as-written for the benefit
2977      * of software; writing it doesn't actually change the timer frequency.
2978      * Our reset value matches the fixed frequency we implement the timer at.
2979      */
2980     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2981       .type = ARM_CP_ALIAS,
2982       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2983       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2984     },
2985     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2986       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2987       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2988       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2989       .resetfn = arm_gt_cntfrq_reset,
2990     },
2991     /* overall control: mostly access permissions */
2992     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2993       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2994       .access = PL1_RW,
2995       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2996       .resetvalue = 0,
2997     },
2998     /* per-timer control */
2999     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3000       .secure = ARM_CP_SECSTATE_NS,
3001       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3002       .accessfn = gt_ptimer_access,
3003       .fieldoffset = offsetoflow32(CPUARMState,
3004                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3005       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3006       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3007     },
3008     { .name = "CNTP_CTL_S",
3009       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3010       .secure = ARM_CP_SECSTATE_S,
3011       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3012       .accessfn = gt_ptimer_access,
3013       .fieldoffset = offsetoflow32(CPUARMState,
3014                                    cp15.c14_timer[GTIMER_SEC].ctl),
3015       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3016     },
3017     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3018       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3019       .type = ARM_CP_IO, .access = PL0_RW,
3020       .accessfn = gt_ptimer_access,
3021       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3022       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3023       .resetvalue = 0,
3024       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3025       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3026     },
3027     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3028       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3029       .accessfn = gt_vtimer_access,
3030       .fieldoffset = offsetoflow32(CPUARMState,
3031                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3032       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3033       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3034     },
3035     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3036       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3037       .type = ARM_CP_IO, .access = PL0_RW,
3038       .accessfn = gt_vtimer_access,
3039       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3040       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3041       .resetvalue = 0,
3042       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3043       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3044     },
3045     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3046     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3047       .secure = ARM_CP_SECSTATE_NS,
3048       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3049       .accessfn = gt_ptimer_access,
3050       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3051     },
3052     { .name = "CNTP_TVAL_S",
3053       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3054       .secure = ARM_CP_SECSTATE_S,
3055       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3056       .accessfn = gt_ptimer_access,
3057       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3058     },
3059     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3060       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3061       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3062       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3063       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3064     },
3065     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3066       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3067       .accessfn = gt_vtimer_access,
3068       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3069     },
3070     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3071       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3072       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3073       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3074       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3075     },
3076     /* The counter itself */
3077     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3078       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3079       .accessfn = gt_pct_access,
3080       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3081     },
3082     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3083       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3084       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3085       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3086     },
3087     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3088       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3089       .accessfn = gt_vct_access,
3090       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3091     },
3092     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3093       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3094       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3095       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3096     },
3097     /* Comparison value, indicating when the timer goes off */
3098     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3099       .secure = ARM_CP_SECSTATE_NS,
3100       .access = PL0_RW,
3101       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3102       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3103       .accessfn = gt_ptimer_access,
3104       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3105       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3106     },
3107     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3108       .secure = ARM_CP_SECSTATE_S,
3109       .access = PL0_RW,
3110       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3111       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3112       .accessfn = gt_ptimer_access,
3113       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3114     },
3115     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3116       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3117       .access = PL0_RW,
3118       .type = ARM_CP_IO,
3119       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3120       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3121       .resetvalue = 0, .accessfn = gt_ptimer_access,
3122       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3123       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3124     },
3125     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3126       .access = PL0_RW,
3127       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3128       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3129       .accessfn = gt_vtimer_access,
3130       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3131       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3132     },
3133     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3134       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3135       .access = PL0_RW,
3136       .type = ARM_CP_IO,
3137       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3138       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3139       .resetvalue = 0, .accessfn = gt_vtimer_access,
3140       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3141       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3142     },
3143     /*
3144      * Secure timer -- this is actually restricted to only EL3
3145      * and configurably Secure-EL1 via the accessfn.
3146      */
3147     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3148       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3149       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3150       .accessfn = gt_stimer_access,
3151       .readfn = gt_sec_tval_read,
3152       .writefn = gt_sec_tval_write,
3153       .resetfn = gt_sec_timer_reset,
3154     },
3155     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3156       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3157       .type = ARM_CP_IO, .access = PL1_RW,
3158       .accessfn = gt_stimer_access,
3159       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3160       .resetvalue = 0,
3161       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3162     },
3163     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3164       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3165       .type = ARM_CP_IO, .access = PL1_RW,
3166       .accessfn = gt_stimer_access,
3167       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3168       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3169     },
3170 };
3171 
3172 /*
3173  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3174  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3175  * so our implementations here are identical to the normal registers.
3176  */
3177 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3178     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3179       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3180       .accessfn = gt_vct_access,
3181       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3182     },
3183     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3184       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3185       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3186       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3187     },
3188     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3189       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3190       .accessfn = gt_pct_access,
3191       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3192     },
3193     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3194       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3195       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3196       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3197     },
3198 };
3199 
3200 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3201                                         const ARMCPRegInfo *ri,
3202                                         bool isread)
3203 {
3204     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3205         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3206         return CP_ACCESS_TRAP_EL3;
3207     }
3208     return CP_ACCESS_OK;
3209 }
3210 
3211 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3212                               uint64_t value)
3213 {
3214     ARMCPU *cpu = env_archcpu(env);
3215 
3216     trace_arm_gt_cntpoff_write(value);
3217     raw_write(env, ri, value);
3218     gt_recalc_timer(cpu, GTIMER_PHYS);
3219 }
3220 
3221 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3222     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3223     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3224     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3225     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3226     .nv2_redirect_offset = 0x1a8,
3227     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3228 };
3229 #else
3230 
3231 /*
3232  * In user-mode most of the generic timer registers are inaccessible
3233  * however modern kernels (4.12+) allow access to cntvct_el0
3234  */
3235 
3236 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3237 {
3238     ARMCPU *cpu = env_archcpu(env);
3239 
3240     /*
3241      * Currently we have no support for QEMUTimer in linux-user so we
3242      * can't call gt_get_countervalue(env), instead we directly
3243      * call the lower level functions.
3244      */
3245     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3246 }
3247 
3248 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3249     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3250       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3251       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3252       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3253       .resetfn = arm_gt_cntfrq_reset,
3254     },
3255     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3256       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3257       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3258       .readfn = gt_virt_cnt_read,
3259     },
3260 };
3261 
3262 /*
3263  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3264  * is exposed to userspace by Linux.
3265  */
3266 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3267     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3268       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3269       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3270       .readfn = gt_virt_cnt_read,
3271     },
3272 };
3273 
3274 #endif
3275 
3276 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3277 {
3278     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3279         raw_write(env, ri, value);
3280     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3281         raw_write(env, ri, value & 0xfffff6ff);
3282     } else {
3283         raw_write(env, ri, value & 0xfffff1ff);
3284     }
3285 }
3286 
3287 #ifndef CONFIG_USER_ONLY
3288 /* get_phys_addr() isn't present for user-mode-only targets */
3289 
3290 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3291                                  bool isread)
3292 {
3293     if (ri->opc2 & 4) {
3294         /*
3295          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3296          * Secure EL1 (which can only happen if EL3 is AArch64).
3297          * They are simply UNDEF if executed from NS EL1.
3298          * They function normally from EL2 or EL3.
3299          */
3300         if (arm_current_el(env) == 1) {
3301             if (arm_is_secure_below_el3(env)) {
3302                 if (env->cp15.scr_el3 & SCR_EEL2) {
3303                     return CP_ACCESS_TRAP_EL2;
3304                 }
3305                 return CP_ACCESS_TRAP_EL3;
3306             }
3307             return CP_ACCESS_TRAP_UNCATEGORIZED;
3308         }
3309     }
3310     return CP_ACCESS_OK;
3311 }
3312 
3313 #ifdef CONFIG_TCG
3314 static int par_el1_shareability(GetPhysAddrResult *res)
3315 {
3316     /*
3317      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3318      * memory -- see pseudocode PAREncodeShareability().
3319      */
3320     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3321         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3322         return 2;
3323     }
3324     return res->cacheattrs.shareability;
3325 }
3326 
3327 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3328                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3329                              ARMSecuritySpace ss)
3330 {
3331     bool ret;
3332     uint64_t par64;
3333     bool format64 = false;
3334     ARMMMUFaultInfo fi = {};
3335     GetPhysAddrResult res = {};
3336 
3337     /*
3338      * I_MXTJT: Granule protection checks are not performed on the final
3339      * address of a successful translation.  This is a translation not a
3340      * memory reference, so "memop = none = 0".
3341      */
3342     ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3343                                          mmu_idx, ss, &res, &fi);
3344 
3345     /*
3346      * ATS operations only do S1 or S1+S2 translations, so we never
3347      * have to deal with the ARMCacheAttrs format for S2 only.
3348      */
3349     assert(!res.cacheattrs.is_s2_format);
3350 
3351     if (ret) {
3352         /*
3353          * Some kinds of translation fault must cause exceptions rather
3354          * than being reported in the PAR.
3355          */
3356         int current_el = arm_current_el(env);
3357         int target_el;
3358         uint32_t syn, fsr, fsc;
3359         bool take_exc = false;
3360 
3361         if (fi.s1ptw && current_el == 1
3362             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3363             /*
3364              * Synchronous stage 2 fault on an access made as part of the
3365              * translation table walk for AT S1E0* or AT S1E1* insn
3366              * executed from NS EL1. If this is a synchronous external abort
3367              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3368              * to EL3. Otherwise the fault is taken as an exception to EL2,
3369              * and HPFAR_EL2 holds the faulting IPA.
3370              */
3371             if (fi.type == ARMFault_SyncExternalOnWalk &&
3372                 (env->cp15.scr_el3 & SCR_EA)) {
3373                 target_el = 3;
3374             } else {
3375                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3376                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3377                     env->cp15.hpfar_el2 |= HPFAR_NS;
3378                 }
3379                 target_el = 2;
3380             }
3381             take_exc = true;
3382         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3383             /*
3384              * Synchronous external aborts during a translation table walk
3385              * are taken as Data Abort exceptions.
3386              */
3387             if (fi.stage2) {
3388                 if (current_el == 3) {
3389                     target_el = 3;
3390                 } else {
3391                     target_el = 2;
3392                 }
3393             } else {
3394                 target_el = exception_target_el(env);
3395             }
3396             take_exc = true;
3397         }
3398 
3399         if (take_exc) {
3400             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3401             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3402                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3403                 fsr = arm_fi_to_lfsc(&fi);
3404                 fsc = extract32(fsr, 0, 6);
3405             } else {
3406                 fsr = arm_fi_to_sfsc(&fi);
3407                 fsc = 0x3f;
3408             }
3409             /*
3410              * Report exception with ESR indicating a fault due to a
3411              * translation table walk for a cache maintenance instruction.
3412              */
3413             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3414                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3415             env->exception.vaddress = value;
3416             env->exception.fsr = fsr;
3417             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3418         }
3419     }
3420 
3421     if (is_a64(env)) {
3422         format64 = true;
3423     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3424         /*
3425          * ATS1Cxx:
3426          * * TTBCR.EAE determines whether the result is returned using the
3427          *   32-bit or the 64-bit PAR format
3428          * * Instructions executed in Hyp mode always use the 64bit format
3429          *
3430          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3431          * * The Non-secure TTBCR.EAE bit is set to 1
3432          * * The implementation includes EL2, and the value of HCR.VM is 1
3433          *
3434          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3435          *
3436          * ATS1Hx always uses the 64bit format.
3437          */
3438         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3439 
3440         if (arm_feature(env, ARM_FEATURE_EL2)) {
3441             if (mmu_idx == ARMMMUIdx_E10_0 ||
3442                 mmu_idx == ARMMMUIdx_E10_1 ||
3443                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3444                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3445             } else {
3446                 format64 |= arm_current_el(env) == 2;
3447             }
3448         }
3449     }
3450 
3451     if (format64) {
3452         /* Create a 64-bit PAR */
3453         par64 = (1 << 11); /* LPAE bit always set */
3454         if (!ret) {
3455             par64 |= res.f.phys_addr & ~0xfffULL;
3456             if (!res.f.attrs.secure) {
3457                 par64 |= (1 << 9); /* NS */
3458             }
3459             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3460             par64 |= par_el1_shareability(&res) << 7; /* SH */
3461         } else {
3462             uint32_t fsr = arm_fi_to_lfsc(&fi);
3463 
3464             par64 |= 1; /* F */
3465             par64 |= (fsr & 0x3f) << 1; /* FS */
3466             if (fi.stage2) {
3467                 par64 |= (1 << 9); /* S */
3468             }
3469             if (fi.s1ptw) {
3470                 par64 |= (1 << 8); /* PTW */
3471             }
3472         }
3473     } else {
3474         /*
3475          * fsr is a DFSR/IFSR value for the short descriptor
3476          * translation table format (with WnR always clear).
3477          * Convert it to a 32-bit PAR.
3478          */
3479         if (!ret) {
3480             /* We do not set any attribute bits in the PAR */
3481             if (res.f.lg_page_size == 24
3482                 && arm_feature(env, ARM_FEATURE_V7)) {
3483                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3484             } else {
3485                 par64 = res.f.phys_addr & 0xfffff000;
3486             }
3487             if (!res.f.attrs.secure) {
3488                 par64 |= (1 << 9); /* NS */
3489             }
3490         } else {
3491             uint32_t fsr = arm_fi_to_sfsc(&fi);
3492 
3493             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3494                     ((fsr & 0xf) << 1) | 1;
3495         }
3496     }
3497     return par64;
3498 }
3499 #endif /* CONFIG_TCG */
3500 
3501 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3502 {
3503 #ifdef CONFIG_TCG
3504     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3505     uint64_t par64;
3506     ARMMMUIdx mmu_idx;
3507     int el = arm_current_el(env);
3508     ARMSecuritySpace ss = arm_security_space(env);
3509 
3510     switch (ri->opc2 & 6) {
3511     case 0:
3512         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3513         switch (el) {
3514         case 3:
3515             if (ri->crm == 9 && arm_pan_enabled(env)) {
3516                 mmu_idx = ARMMMUIdx_E30_3_PAN;
3517             } else {
3518                 mmu_idx = ARMMMUIdx_E3;
3519             }
3520             break;
3521         case 2:
3522             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3523             /* fall through */
3524         case 1:
3525             if (ri->crm == 9 && arm_pan_enabled(env)) {
3526                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3527             } else {
3528                 mmu_idx = ARMMMUIdx_Stage1_E1;
3529             }
3530             break;
3531         default:
3532             g_assert_not_reached();
3533         }
3534         break;
3535     case 2:
3536         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3537         switch (el) {
3538         case 3:
3539             mmu_idx = ARMMMUIdx_E30_0;
3540             break;
3541         case 2:
3542             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3543             mmu_idx = ARMMMUIdx_Stage1_E0;
3544             break;
3545         case 1:
3546             mmu_idx = ARMMMUIdx_Stage1_E0;
3547             break;
3548         default:
3549             g_assert_not_reached();
3550         }
3551         break;
3552     case 4:
3553         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3554         mmu_idx = ARMMMUIdx_E10_1;
3555         ss = ARMSS_NonSecure;
3556         break;
3557     case 6:
3558         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3559         mmu_idx = ARMMMUIdx_E10_0;
3560         ss = ARMSS_NonSecure;
3561         break;
3562     default:
3563         g_assert_not_reached();
3564     }
3565 
3566     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3567 
3568     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3569 #else
3570     /* Handled by hardware accelerator. */
3571     g_assert_not_reached();
3572 #endif /* CONFIG_TCG */
3573 }
3574 
3575 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3576                         uint64_t value)
3577 {
3578 #ifdef CONFIG_TCG
3579     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3580     uint64_t par64;
3581 
3582     /* There is no SecureEL2 for AArch32. */
3583     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3584                          ARMSS_NonSecure);
3585 
3586     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3587 #else
3588     /* Handled by hardware accelerator. */
3589     g_assert_not_reached();
3590 #endif /* CONFIG_TCG */
3591 }
3592 
3593 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3594                                      bool isread)
3595 {
3596     /*
3597      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3598      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3599      * only happen when executing at EL3 because that combination also causes an
3600      * illegal exception return. We don't need to check FEAT_RME either, because
3601      * scr_write() ensures that the NSE bit is not set otherwise.
3602      */
3603     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3604         return CP_ACCESS_TRAP;
3605     }
3606     return CP_ACCESS_OK;
3607 }
3608 
3609 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3610                                      bool isread)
3611 {
3612     if (arm_current_el(env) == 3 &&
3613         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3614         return CP_ACCESS_TRAP;
3615     }
3616     return at_e012_access(env, ri, isread);
3617 }
3618 
3619 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3620                                       bool isread)
3621 {
3622     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3623         return CP_ACCESS_TRAP_EL2;
3624     }
3625     return at_e012_access(env, ri, isread);
3626 }
3627 
3628 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3629                         uint64_t value)
3630 {
3631 #ifdef CONFIG_TCG
3632     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3633     ARMMMUIdx mmu_idx;
3634     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3635     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3636     bool for_el3 = false;
3637     ARMSecuritySpace ss;
3638 
3639     switch (ri->opc2 & 6) {
3640     case 0:
3641         switch (ri->opc1) {
3642         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3643             if (ri->crm == 9 && arm_pan_enabled(env)) {
3644                 mmu_idx = regime_e20 ?
3645                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3646             } else {
3647                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3648             }
3649             break;
3650         case 4: /* AT S1E2R, AT S1E2W */
3651             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3652             break;
3653         case 6: /* AT S1E3R, AT S1E3W */
3654             mmu_idx = ARMMMUIdx_E3;
3655             for_el3 = true;
3656             break;
3657         default:
3658             g_assert_not_reached();
3659         }
3660         break;
3661     case 2: /* AT S1E0R, AT S1E0W */
3662         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3663         break;
3664     case 4: /* AT S12E1R, AT S12E1W */
3665         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3666         break;
3667     case 6: /* AT S12E0R, AT S12E0W */
3668         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3669         break;
3670     default:
3671         g_assert_not_reached();
3672     }
3673 
3674     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3675     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3676 #else
3677     /* Handled by hardware accelerator. */
3678     g_assert_not_reached();
3679 #endif /* CONFIG_TCG */
3680 }
3681 #endif
3682 
3683 /* Return basic MPU access permission bits.  */
3684 static uint32_t simple_mpu_ap_bits(uint32_t val)
3685 {
3686     uint32_t ret;
3687     uint32_t mask;
3688     int i;
3689     ret = 0;
3690     mask = 3;
3691     for (i = 0; i < 16; i += 2) {
3692         ret |= (val >> i) & mask;
3693         mask <<= 2;
3694     }
3695     return ret;
3696 }
3697 
3698 /* Pad basic MPU access permission bits to extended format.  */
3699 static uint32_t extended_mpu_ap_bits(uint32_t val)
3700 {
3701     uint32_t ret;
3702     uint32_t mask;
3703     int i;
3704     ret = 0;
3705     mask = 3;
3706     for (i = 0; i < 16; i += 2) {
3707         ret |= (val & mask) << i;
3708         mask <<= 2;
3709     }
3710     return ret;
3711 }
3712 
3713 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3714                                  uint64_t value)
3715 {
3716     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3717 }
3718 
3719 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3720 {
3721     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3722 }
3723 
3724 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3725                                  uint64_t value)
3726 {
3727     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3728 }
3729 
3730 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3731 {
3732     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3733 }
3734 
3735 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3736 {
3737     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3738 
3739     if (!u32p) {
3740         return 0;
3741     }
3742 
3743     u32p += env->pmsav7.rnr[M_REG_NS];
3744     return *u32p;
3745 }
3746 
3747 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3748                          uint64_t value)
3749 {
3750     ARMCPU *cpu = env_archcpu(env);
3751     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3752 
3753     if (!u32p) {
3754         return;
3755     }
3756 
3757     u32p += env->pmsav7.rnr[M_REG_NS];
3758     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3759     *u32p = value;
3760 }
3761 
3762 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3763                               uint64_t value)
3764 {
3765     ARMCPU *cpu = env_archcpu(env);
3766     uint32_t nrgs = cpu->pmsav7_dregion;
3767 
3768     if (value >= nrgs) {
3769         qemu_log_mask(LOG_GUEST_ERROR,
3770                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3771                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3772         return;
3773     }
3774 
3775     raw_write(env, ri, value);
3776 }
3777 
3778 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3779                           uint64_t value)
3780 {
3781     ARMCPU *cpu = env_archcpu(env);
3782 
3783     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3784     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3785 }
3786 
3787 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3788 {
3789     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3790 }
3791 
3792 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3793                           uint64_t value)
3794 {
3795     ARMCPU *cpu = env_archcpu(env);
3796 
3797     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3798     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3799 }
3800 
3801 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3802 {
3803     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3804 }
3805 
3806 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3807                            uint64_t value)
3808 {
3809     ARMCPU *cpu = env_archcpu(env);
3810 
3811     /*
3812      * Ignore writes that would select not implemented region.
3813      * This is architecturally UNPREDICTABLE.
3814      */
3815     if (value >= cpu->pmsav7_dregion) {
3816         return;
3817     }
3818 
3819     env->pmsav7.rnr[M_REG_NS] = value;
3820 }
3821 
3822 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3823                           uint64_t value)
3824 {
3825     ARMCPU *cpu = env_archcpu(env);
3826 
3827     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3828     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3829 }
3830 
3831 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3832 {
3833     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3834 }
3835 
3836 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3837                           uint64_t value)
3838 {
3839     ARMCPU *cpu = env_archcpu(env);
3840 
3841     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3842     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3843 }
3844 
3845 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3846 {
3847     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3848 }
3849 
3850 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3851                           uint64_t value)
3852 {
3853     uint32_t n;
3854     uint32_t bit;
3855     ARMCPU *cpu = env_archcpu(env);
3856 
3857     /* Ignore writes to unimplemented regions */
3858     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3859     value &= MAKE_64BIT_MASK(0, rmax);
3860 
3861     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3862 
3863     /* Register alias is only valid for first 32 indexes */
3864     for (n = 0; n < rmax; ++n) {
3865         bit = extract32(value, n, 1);
3866         env->pmsav8.hprlar[n] = deposit32(
3867                     env->pmsav8.hprlar[n], 0, 1, bit);
3868     }
3869 }
3870 
3871 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3872 {
3873     uint32_t n;
3874     uint32_t result = 0x0;
3875     ARMCPU *cpu = env_archcpu(env);
3876 
3877     /* Register alias is only valid for first 32 indexes */
3878     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3879         if (env->pmsav8.hprlar[n] & 0x1) {
3880             result |= (0x1 << n);
3881         }
3882     }
3883     return result;
3884 }
3885 
3886 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3887                            uint64_t value)
3888 {
3889     ARMCPU *cpu = env_archcpu(env);
3890 
3891     /*
3892      * Ignore writes that would select not implemented region.
3893      * This is architecturally UNPREDICTABLE.
3894      */
3895     if (value >= cpu->pmsav8r_hdregion) {
3896         return;
3897     }
3898 
3899     env->pmsav8.hprselr = value;
3900 }
3901 
3902 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3903                           uint64_t value)
3904 {
3905     ARMCPU *cpu = env_archcpu(env);
3906     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3907                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3908 
3909     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3910 
3911     if (ri->opc1 & 4) {
3912         if (index >= cpu->pmsav8r_hdregion) {
3913             return;
3914         }
3915         if (ri->opc2 & 0x1) {
3916             env->pmsav8.hprlar[index] = value;
3917         } else {
3918             env->pmsav8.hprbar[index] = value;
3919         }
3920     } else {
3921         if (index >= cpu->pmsav7_dregion) {
3922             return;
3923         }
3924         if (ri->opc2 & 0x1) {
3925             env->pmsav8.rlar[M_REG_NS][index] = value;
3926         } else {
3927             env->pmsav8.rbar[M_REG_NS][index] = value;
3928         }
3929     }
3930 }
3931 
3932 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3933 {
3934     ARMCPU *cpu = env_archcpu(env);
3935     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3936                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3937 
3938     if (ri->opc1 & 4) {
3939         if (index >= cpu->pmsav8r_hdregion) {
3940             return 0x0;
3941         }
3942         if (ri->opc2 & 0x1) {
3943             return env->pmsav8.hprlar[index];
3944         } else {
3945             return env->pmsav8.hprbar[index];
3946         }
3947     } else {
3948         if (index >= cpu->pmsav7_dregion) {
3949             return 0x0;
3950         }
3951         if (ri->opc2 & 0x1) {
3952             return env->pmsav8.rlar[M_REG_NS][index];
3953         } else {
3954             return env->pmsav8.rbar[M_REG_NS][index];
3955         }
3956     }
3957 }
3958 
3959 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3960     { .name = "PRBAR",
3961       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3962       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3963       .accessfn = access_tvm_trvm,
3964       .readfn = prbar_read, .writefn = prbar_write },
3965     { .name = "PRLAR",
3966       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3967       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3968       .accessfn = access_tvm_trvm,
3969       .readfn = prlar_read, .writefn = prlar_write },
3970     { .name = "PRSELR", .resetvalue = 0,
3971       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3972       .access = PL1_RW, .accessfn = access_tvm_trvm,
3973       .writefn = prselr_write,
3974       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3975     { .name = "HPRBAR", .resetvalue = 0,
3976       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3977       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3978       .readfn = hprbar_read, .writefn = hprbar_write },
3979     { .name = "HPRLAR",
3980       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3981       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3982       .readfn = hprlar_read, .writefn = hprlar_write },
3983     { .name = "HPRSELR", .resetvalue = 0,
3984       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3985       .access = PL2_RW,
3986       .writefn = hprselr_write,
3987       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3988     { .name = "HPRENR",
3989       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3990       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3991       .readfn = hprenr_read, .writefn = hprenr_write },
3992 };
3993 
3994 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3995     /*
3996      * Reset for all these registers is handled in arm_cpu_reset(),
3997      * because the PMSAv7 is also used by M-profile CPUs, which do
3998      * not register cpregs but still need the state to be reset.
3999      */
4000     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4001       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4002       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4003       .readfn = pmsav7_read, .writefn = pmsav7_write,
4004       .resetfn = arm_cp_reset_ignore },
4005     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4006       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4007       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4008       .readfn = pmsav7_read, .writefn = pmsav7_write,
4009       .resetfn = arm_cp_reset_ignore },
4010     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4011       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4012       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4013       .readfn = pmsav7_read, .writefn = pmsav7_write,
4014       .resetfn = arm_cp_reset_ignore },
4015     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4016       .access = PL1_RW,
4017       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4018       .writefn = pmsav7_rgnr_write,
4019       .resetfn = arm_cp_reset_ignore },
4020 };
4021 
4022 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4023     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4024       .access = PL1_RW, .type = ARM_CP_ALIAS,
4025       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4026       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4027     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4028       .access = PL1_RW, .type = ARM_CP_ALIAS,
4029       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4030       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4031     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4032       .access = PL1_RW,
4033       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4034       .resetvalue = 0, },
4035     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4036       .access = PL1_RW,
4037       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4038       .resetvalue = 0, },
4039     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4040       .access = PL1_RW,
4041       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4042     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4043       .access = PL1_RW,
4044       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4045     /* Protection region base and size registers */
4046     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4047       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4048       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4049     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4050       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4051       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4052     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4053       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4054       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4055     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4056       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4057       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4058     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4059       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4060       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4061     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4062       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4063       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4064     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4065       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4066       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4067     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4068       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4069       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4070 };
4071 
4072 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4073                              uint64_t value)
4074 {
4075     ARMCPU *cpu = env_archcpu(env);
4076 
4077     if (!arm_feature(env, ARM_FEATURE_V8)) {
4078         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4079             /*
4080              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4081              * using Long-descriptor translation table format
4082              */
4083             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4084         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4085             /*
4086              * In an implementation that includes the Security Extensions
4087              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4088              * Short-descriptor translation table format.
4089              */
4090             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4091         } else {
4092             value &= TTBCR_N;
4093         }
4094     }
4095 
4096     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4097         /*
4098          * With LPAE the TTBCR could result in a change of ASID
4099          * via the TTBCR.A1 bit, so do a TLB flush.
4100          */
4101         tlb_flush(CPU(cpu));
4102     }
4103     raw_write(env, ri, value);
4104 }
4105 
4106 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4107                                uint64_t value)
4108 {
4109     ARMCPU *cpu = env_archcpu(env);
4110 
4111     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4112     tlb_flush(CPU(cpu));
4113     raw_write(env, ri, value);
4114 }
4115 
4116 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4117                             uint64_t value)
4118 {
4119     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4120     if (cpreg_field_is_64bit(ri) &&
4121         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4122         ARMCPU *cpu = env_archcpu(env);
4123         tlb_flush(CPU(cpu));
4124     }
4125     raw_write(env, ri, value);
4126 }
4127 
4128 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129                                     uint64_t value)
4130 {
4131     /*
4132      * If we are running with E2&0 regime, then an ASID is active.
4133      * Flush if that might be changing.  Note we're not checking
4134      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4135      * holds the active ASID, only checking the field that might.
4136      */
4137     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4138         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4139         uint16_t mask = ARMMMUIdxBit_E20_2 |
4140                         ARMMMUIdxBit_E20_2_PAN |
4141                         ARMMMUIdxBit_E20_0;
4142         tlb_flush_by_mmuidx(env_cpu(env), mask);
4143     }
4144     raw_write(env, ri, value);
4145 }
4146 
4147 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4148                         uint64_t value)
4149 {
4150     ARMCPU *cpu = env_archcpu(env);
4151     CPUState *cs = CPU(cpu);
4152 
4153     /*
4154      * A change in VMID to the stage2 page table (Stage2) invalidates
4155      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4156      */
4157     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4158         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4159     }
4160     raw_write(env, ri, value);
4161 }
4162 
4163 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4164     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4165       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4166       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4167                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4168     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4169       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4170       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4171                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4172     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4173       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4174       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4175                              offsetof(CPUARMState, cp15.dfar_ns) } },
4176     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4177       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4178       .access = PL1_RW, .accessfn = access_tvm_trvm,
4179       .fgt = FGT_FAR_EL1,
4180       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4181       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4182       .resetvalue = 0, },
4183 };
4184 
4185 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4186     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4187       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4188       .access = PL1_RW, .accessfn = access_tvm_trvm,
4189       .fgt = FGT_ESR_EL1,
4190       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4191       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4192     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4193       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4194       .access = PL1_RW, .accessfn = access_tvm_trvm,
4195       .fgt = FGT_TTBR0_EL1,
4196       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4197       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4198       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4199                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4200     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4201       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4202       .access = PL1_RW, .accessfn = access_tvm_trvm,
4203       .fgt = FGT_TTBR1_EL1,
4204       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4205       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4206       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4207                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4208     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4209       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4210       .access = PL1_RW, .accessfn = access_tvm_trvm,
4211       .fgt = FGT_TCR_EL1,
4212       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4213       .writefn = vmsa_tcr_el12_write,
4214       .raw_writefn = raw_write,
4215       .resetvalue = 0,
4216       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4217     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4218       .access = PL1_RW, .accessfn = access_tvm_trvm,
4219       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4220       .raw_writefn = raw_write,
4221       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4222                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4223 };
4224 
4225 /*
4226  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4227  * qemu tlbs nor adjusting cached masks.
4228  */
4229 static const ARMCPRegInfo ttbcr2_reginfo = {
4230     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4231     .access = PL1_RW, .accessfn = access_tvm_trvm,
4232     .type = ARM_CP_ALIAS,
4233     .bank_fieldoffsets = {
4234         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4235         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4236     },
4237 };
4238 
4239 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4240                                 uint64_t value)
4241 {
4242     env->cp15.c15_ticonfig = value & 0xe7;
4243     /* The OS_TYPE bit in this register changes the reported CPUID! */
4244     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4245         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4246 }
4247 
4248 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4249                                 uint64_t value)
4250 {
4251     env->cp15.c15_threadid = value & 0xffff;
4252 }
4253 
4254 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4255                            uint64_t value)
4256 {
4257     /* Wait-for-interrupt (deprecated) */
4258     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4259 }
4260 
4261 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4262                                   uint64_t value)
4263 {
4264     /*
4265      * On OMAP there are registers indicating the max/min index of dcache lines
4266      * containing a dirty line; cache flush operations have to reset these.
4267      */
4268     env->cp15.c15_i_max = 0x000;
4269     env->cp15.c15_i_min = 0xff0;
4270 }
4271 
4272 static const ARMCPRegInfo omap_cp_reginfo[] = {
4273     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4274       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4275       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4276       .resetvalue = 0, },
4277     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4278       .access = PL1_RW, .type = ARM_CP_NOP },
4279     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4280       .access = PL1_RW,
4281       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4282       .writefn = omap_ticonfig_write },
4283     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4284       .access = PL1_RW,
4285       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4286     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4287       .access = PL1_RW, .resetvalue = 0xff0,
4288       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4289     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4290       .access = PL1_RW,
4291       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4292       .writefn = omap_threadid_write },
4293     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4294       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4295       .type = ARM_CP_NO_RAW,
4296       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4297     /*
4298      * TODO: Peripheral port remap register:
4299      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4300      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4301      * when MMU is off.
4302      */
4303     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4304       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4305       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4306       .writefn = omap_cachemaint_write },
4307     { .name = "C9", .cp = 15, .crn = 9,
4308       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4309       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4310 };
4311 
4312 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4313                               uint64_t value)
4314 {
4315     env->cp15.c15_cpar = value & 0x3fff;
4316 }
4317 
4318 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4319     { .name = "XSCALE_CPAR",
4320       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4321       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4322       .writefn = xscale_cpar_write, },
4323     { .name = "XSCALE_AUXCR",
4324       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4325       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4326       .resetvalue = 0, },
4327     /*
4328      * XScale specific cache-lockdown: since we have no cache we NOP these
4329      * and hope the guest does not really rely on cache behaviour.
4330      */
4331     { .name = "XSCALE_LOCK_ICACHE_LINE",
4332       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4333       .access = PL1_W, .type = ARM_CP_NOP },
4334     { .name = "XSCALE_UNLOCK_ICACHE",
4335       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4336       .access = PL1_W, .type = ARM_CP_NOP },
4337     { .name = "XSCALE_DCACHE_LOCK",
4338       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4339       .access = PL1_RW, .type = ARM_CP_NOP },
4340     { .name = "XSCALE_UNLOCK_DCACHE",
4341       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4342       .access = PL1_W, .type = ARM_CP_NOP },
4343 };
4344 
4345 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4346     /*
4347      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4348      * implementation of this implementation-defined space.
4349      * Ideally this should eventually disappear in favour of actually
4350      * implementing the correct behaviour for all cores.
4351      */
4352     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4353       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4354       .access = PL1_RW,
4355       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4356       .resetvalue = 0 },
4357 };
4358 
4359 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4360     /* Cache status: RAZ because we have no cache so it's always clean */
4361     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4362       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4363       .resetvalue = 0 },
4364 };
4365 
4366 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4367     /* We never have a block transfer operation in progress */
4368     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4369       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4370       .resetvalue = 0 },
4371     /* The cache ops themselves: these all NOP for QEMU */
4372     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4373       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4374     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4375       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4376     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4377       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4378     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4379       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4380     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4381       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4382     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4383       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4384 };
4385 
4386 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4387     /*
4388      * The cache test-and-clean instructions always return (1 << 30)
4389      * to indicate that there are no dirty cache lines.
4390      */
4391     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4392       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4393       .resetvalue = (1 << 30) },
4394     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4395       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4396       .resetvalue = (1 << 30) },
4397 };
4398 
4399 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4400     /* Ignore ReadBuffer accesses */
4401     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4402       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4403       .access = PL1_RW, .resetvalue = 0,
4404       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4405 };
4406 
4407 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4408 {
4409     unsigned int cur_el = arm_current_el(env);
4410 
4411     if (arm_is_el2_enabled(env) && cur_el == 1) {
4412         return env->cp15.vpidr_el2;
4413     }
4414     return raw_read(env, ri);
4415 }
4416 
4417 static uint64_t mpidr_read_val(CPUARMState *env)
4418 {
4419     ARMCPU *cpu = env_archcpu(env);
4420     uint64_t mpidr = cpu->mp_affinity;
4421 
4422     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4423         mpidr |= (1U << 31);
4424         /*
4425          * Cores which are uniprocessor (non-coherent)
4426          * but still implement the MP extensions set
4427          * bit 30. (For instance, Cortex-R5).
4428          */
4429         if (cpu->mp_is_up) {
4430             mpidr |= (1u << 30);
4431         }
4432     }
4433     return mpidr;
4434 }
4435 
4436 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4437 {
4438     unsigned int cur_el = arm_current_el(env);
4439 
4440     if (arm_is_el2_enabled(env) && cur_el == 1) {
4441         return env->cp15.vmpidr_el2;
4442     }
4443     return mpidr_read_val(env);
4444 }
4445 
4446 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4447     /* NOP AMAIR0/1 */
4448     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4449       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4450       .access = PL1_RW, .accessfn = access_tvm_trvm,
4451       .fgt = FGT_AMAIR_EL1,
4452       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4453       .type = ARM_CP_CONST, .resetvalue = 0 },
4454     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4455     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4456       .access = PL1_RW, .accessfn = access_tvm_trvm,
4457       .type = ARM_CP_CONST, .resetvalue = 0 },
4458     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4459       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4460       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4461                              offsetof(CPUARMState, cp15.par_ns)} },
4462     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4463       .access = PL1_RW, .accessfn = access_tvm_trvm,
4464       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4465       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4466                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4467       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4468     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4469       .access = PL1_RW, .accessfn = access_tvm_trvm,
4470       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4471       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4472                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4473       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4474 };
4475 
4476 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4477 {
4478     return vfp_get_fpcr(env);
4479 }
4480 
4481 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4482                             uint64_t value)
4483 {
4484     vfp_set_fpcr(env, value);
4485 }
4486 
4487 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4488 {
4489     return vfp_get_fpsr(env);
4490 }
4491 
4492 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4493                             uint64_t value)
4494 {
4495     vfp_set_fpsr(env, value);
4496 }
4497 
4498 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4499                                        bool isread)
4500 {
4501     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4502         return CP_ACCESS_TRAP;
4503     }
4504     return CP_ACCESS_OK;
4505 }
4506 
4507 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4508                             uint64_t value)
4509 {
4510     env->daif = value & PSTATE_DAIF;
4511 }
4512 
4513 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4514 {
4515     return env->pstate & PSTATE_PAN;
4516 }
4517 
4518 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4519                            uint64_t value)
4520 {
4521     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4522 }
4523 
4524 static const ARMCPRegInfo pan_reginfo = {
4525     .name = "PAN", .state = ARM_CP_STATE_AA64,
4526     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4527     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4528     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4529 };
4530 
4531 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4532 {
4533     return env->pstate & PSTATE_UAO;
4534 }
4535 
4536 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4537                            uint64_t value)
4538 {
4539     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4540 }
4541 
4542 static const ARMCPRegInfo uao_reginfo = {
4543     .name = "UAO", .state = ARM_CP_STATE_AA64,
4544     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4545     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4546     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4547 };
4548 
4549 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4550 {
4551     return env->pstate & PSTATE_DIT;
4552 }
4553 
4554 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4555                            uint64_t value)
4556 {
4557     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4558 }
4559 
4560 static const ARMCPRegInfo dit_reginfo = {
4561     .name = "DIT", .state = ARM_CP_STATE_AA64,
4562     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4563     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4564     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4565 };
4566 
4567 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4568 {
4569     return env->pstate & PSTATE_SSBS;
4570 }
4571 
4572 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4573                            uint64_t value)
4574 {
4575     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4576 }
4577 
4578 static const ARMCPRegInfo ssbs_reginfo = {
4579     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4580     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4581     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4582     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4583 };
4584 
4585 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4586                                               const ARMCPRegInfo *ri,
4587                                               bool isread)
4588 {
4589     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4590     switch (arm_current_el(env)) {
4591     case 0:
4592         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4593         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4594             return CP_ACCESS_TRAP;
4595         }
4596         /* fall through */
4597     case 1:
4598         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4599         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4600             return CP_ACCESS_TRAP_EL2;
4601         }
4602         break;
4603     }
4604     return CP_ACCESS_OK;
4605 }
4606 
4607 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4608 {
4609     /* Cache invalidate/clean to Point of Unification... */
4610     switch (arm_current_el(env)) {
4611     case 0:
4612         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4613         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4614             return CP_ACCESS_TRAP;
4615         }
4616         /* fall through */
4617     case 1:
4618         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4619         if (arm_hcr_el2_eff(env) & hcrflags) {
4620             return CP_ACCESS_TRAP_EL2;
4621         }
4622         break;
4623     }
4624     return CP_ACCESS_OK;
4625 }
4626 
4627 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4628                                    bool isread)
4629 {
4630     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4631 }
4632 
4633 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4634                                   bool isread)
4635 {
4636     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4637 }
4638 
4639 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4640                                       bool isread)
4641 {
4642     int cur_el = arm_current_el(env);
4643 
4644     if (cur_el < 2) {
4645         uint64_t hcr = arm_hcr_el2_eff(env);
4646 
4647         if (cur_el == 0) {
4648             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4649                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4650                     return CP_ACCESS_TRAP_EL2;
4651                 }
4652             } else {
4653                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4654                     return CP_ACCESS_TRAP;
4655                 }
4656                 if (hcr & HCR_TDZ) {
4657                     return CP_ACCESS_TRAP_EL2;
4658                 }
4659             }
4660         } else if (hcr & HCR_TDZ) {
4661             return CP_ACCESS_TRAP_EL2;
4662         }
4663     }
4664     return CP_ACCESS_OK;
4665 }
4666 
4667 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4668 {
4669     ARMCPU *cpu = env_archcpu(env);
4670     int dzp_bit = 1 << 4;
4671 
4672     /* DZP indicates whether DC ZVA access is allowed */
4673     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4674         dzp_bit = 0;
4675     }
4676     return cpu->dcz_blocksize | dzp_bit;
4677 }
4678 
4679 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4680                                     bool isread)
4681 {
4682     if (!(env->pstate & PSTATE_SP)) {
4683         /*
4684          * Access to SP_EL0 is undefined if it's being used as
4685          * the stack pointer.
4686          */
4687         return CP_ACCESS_TRAP_UNCATEGORIZED;
4688     }
4689     return CP_ACCESS_OK;
4690 }
4691 
4692 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4693 {
4694     return env->pstate & PSTATE_SP;
4695 }
4696 
4697 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4698 {
4699     update_spsel(env, val);
4700 }
4701 
4702 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4703                         uint64_t value)
4704 {
4705     ARMCPU *cpu = env_archcpu(env);
4706 
4707     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4708         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4709         value &= ~SCTLR_M;
4710     }
4711 
4712     /* ??? Lots of these bits are not implemented.  */
4713 
4714     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4715         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4716             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4717         } else {
4718             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4719                        SCTLR_ATA0 | SCTLR_ATA);
4720         }
4721     }
4722 
4723     if (raw_read(env, ri) == value) {
4724         /*
4725          * Skip the TLB flush if nothing actually changed; Linux likes
4726          * to do a lot of pointless SCTLR writes.
4727          */
4728         return;
4729     }
4730 
4731     raw_write(env, ri, value);
4732 
4733     /* This may enable/disable the MMU, so do a TLB flush.  */
4734     tlb_flush(CPU(cpu));
4735 
4736     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4737         /*
4738          * Normally we would always end the TB on an SCTLR write; see the
4739          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4740          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4741          * of hflags from the translator, so do it here.
4742          */
4743         arm_rebuild_hflags(env);
4744     }
4745 }
4746 
4747 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4748                            uint64_t value)
4749 {
4750     /*
4751      * Some MDCR_EL3 bits affect whether PMU counters are running:
4752      * if we are trying to change any of those then we must
4753      * bracket this update with PMU start/finish calls.
4754      */
4755     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4756 
4757     if (pmu_op) {
4758         pmu_op_start(env);
4759     }
4760     env->cp15.mdcr_el3 = value;
4761     if (pmu_op) {
4762         pmu_op_finish(env);
4763     }
4764 }
4765 
4766 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4767                        uint64_t value)
4768 {
4769     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4770     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4771 }
4772 
4773 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4774                            uint64_t value)
4775 {
4776     /*
4777      * Some MDCR_EL2 bits affect whether PMU counters are running:
4778      * if we are trying to change any of those then we must
4779      * bracket this update with PMU start/finish calls.
4780      */
4781     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4782 
4783     if (pmu_op) {
4784         pmu_op_start(env);
4785     }
4786     env->cp15.mdcr_el2 = value;
4787     if (pmu_op) {
4788         pmu_op_finish(env);
4789     }
4790 }
4791 
4792 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4793                                  bool isread)
4794 {
4795     if (arm_current_el(env) == 1) {
4796         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4797 
4798         if (hcr_nv == (HCR_NV | HCR_NV1)) {
4799             return CP_ACCESS_TRAP_EL2;
4800         }
4801     }
4802     return CP_ACCESS_OK;
4803 }
4804 
4805 #ifdef CONFIG_USER_ONLY
4806 /*
4807  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4808  * code to get around W^X restrictions, where one region is writable and the
4809  * other is executable.
4810  *
4811  * Since the executable region is never written to we cannot detect code
4812  * changes when running in user mode, and rely on the emulated JIT telling us
4813  * that the code has changed by executing this instruction.
4814  */
4815 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4816                           uint64_t value)
4817 {
4818     uint64_t icache_line_mask, start_address, end_address;
4819     const ARMCPU *cpu;
4820 
4821     cpu = env_archcpu(env);
4822 
4823     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4824     start_address = value & ~icache_line_mask;
4825     end_address = value | icache_line_mask;
4826 
4827     mmap_lock();
4828 
4829     tb_invalidate_phys_range(start_address, end_address);
4830 
4831     mmap_unlock();
4832 }
4833 #endif
4834 
4835 static const ARMCPRegInfo v8_cp_reginfo[] = {
4836     /*
4837      * Minimal set of EL0-visible registers. This will need to be expanded
4838      * significantly for system emulation of AArch64 CPUs.
4839      */
4840     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4841       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4842       .access = PL0_RW, .type = ARM_CP_NZCV },
4843     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4844       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4845       .type = ARM_CP_NO_RAW,
4846       .access = PL0_RW, .accessfn = aa64_daif_access,
4847       .fieldoffset = offsetof(CPUARMState, daif),
4848       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4849     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4850       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4851       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4852       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4853     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4854       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4855       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4856       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4857     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4858       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4859       .access = PL0_R, .type = ARM_CP_NO_RAW,
4860       .fgt = FGT_DCZID_EL0,
4861       .readfn = aa64_dczid_read },
4862     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4863       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4864       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4865 #ifndef CONFIG_USER_ONLY
4866       /* Avoid overhead of an access check that always passes in user-mode */
4867       .accessfn = aa64_zva_access,
4868       .fgt = FGT_DCZVA,
4869 #endif
4870     },
4871     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4872       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4873       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4874     /*
4875      * Instruction cache ops. All of these except `IC IVAU` NOP because we
4876      * don't emulate caches.
4877      */
4878     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4879       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4880       .access = PL1_W, .type = ARM_CP_NOP,
4881       .fgt = FGT_ICIALLUIS,
4882       .accessfn = access_ticab },
4883     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4884       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4885       .access = PL1_W, .type = ARM_CP_NOP,
4886       .fgt = FGT_ICIALLU,
4887       .accessfn = access_tocu },
4888     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4889       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4890       .access = PL0_W,
4891       .fgt = FGT_ICIVAU,
4892       .accessfn = access_tocu,
4893 #ifdef CONFIG_USER_ONLY
4894       .type = ARM_CP_NO_RAW,
4895       .writefn = ic_ivau_write
4896 #else
4897       .type = ARM_CP_NOP
4898 #endif
4899     },
4900     /* Cache ops: all NOPs since we don't emulate caches */
4901     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4902       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4903       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4904       .fgt = FGT_DCIVAC,
4905       .type = ARM_CP_NOP },
4906     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4907       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4908       .fgt = FGT_DCISW,
4909       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4910     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4911       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4912       .access = PL0_W, .type = ARM_CP_NOP,
4913       .fgt = FGT_DCCVAC,
4914       .accessfn = aa64_cacheop_poc_access },
4915     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4916       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4917       .fgt = FGT_DCCSW,
4918       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4919     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4920       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4921       .access = PL0_W, .type = ARM_CP_NOP,
4922       .fgt = FGT_DCCVAU,
4923       .accessfn = access_tocu },
4924     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4925       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4926       .access = PL0_W, .type = ARM_CP_NOP,
4927       .fgt = FGT_DCCIVAC,
4928       .accessfn = aa64_cacheop_poc_access },
4929     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4930       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4931       .fgt = FGT_DCCISW,
4932       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4933 #ifndef CONFIG_USER_ONLY
4934     /* 64 bit address translation operations */
4935     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4936       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4937       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4938       .fgt = FGT_ATS1E1R,
4939       .accessfn = at_s1e01_access, .writefn = ats_write64 },
4940     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4941       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4942       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4943       .fgt = FGT_ATS1E1W,
4944       .accessfn = at_s1e01_access, .writefn = ats_write64 },
4945     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4946       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4947       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4948       .fgt = FGT_ATS1E0R,
4949       .accessfn = at_s1e01_access, .writefn = ats_write64 },
4950     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4952       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4953       .fgt = FGT_ATS1E0W,
4954       .accessfn = at_s1e01_access, .writefn = ats_write64 },
4955     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4956       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4957       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4958       .accessfn = at_e012_access, .writefn = ats_write64 },
4959     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4960       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4961       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4962       .accessfn = at_e012_access, .writefn = ats_write64 },
4963     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4964       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4965       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4966       .accessfn = at_e012_access, .writefn = ats_write64 },
4967     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4968       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4969       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4970       .accessfn = at_e012_access, .writefn = ats_write64 },
4971     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4972     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4973       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4974       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4975       .writefn = ats_write64 },
4976     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4977       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4978       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4979       .writefn = ats_write64 },
4980     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4981       .type = ARM_CP_ALIAS,
4982       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4983       .access = PL1_RW, .resetvalue = 0,
4984       .fgt = FGT_PAR_EL1,
4985       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4986       .writefn = par_write },
4987 #endif
4988     /* 32 bit cache operations */
4989     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4990       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
4991     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4992       .type = ARM_CP_NOP, .access = PL1_W },
4993     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4994       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
4995     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4996       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
4997     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4998       .type = ARM_CP_NOP, .access = PL1_W },
4999     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5000       .type = ARM_CP_NOP, .access = PL1_W },
5001     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5002       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5003     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5004       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5005     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5006       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5007     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5008       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5009     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5010       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5011     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5012       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5013     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5014       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5015     /* MMU Domain access control / MPU write buffer control */
5016     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5017       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5018       .writefn = dacr_write, .raw_writefn = raw_write,
5019       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5020                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5021     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5022       .type = ARM_CP_ALIAS,
5023       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5024       .access = PL1_RW, .accessfn = access_nv1,
5025       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5026       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5027     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5028       .type = ARM_CP_ALIAS,
5029       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5030       .access = PL1_RW, .accessfn = access_nv1,
5031       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5032       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5033     /*
5034      * We rely on the access checks not allowing the guest to write to the
5035      * state field when SPSel indicates that it's being used as the stack
5036      * pointer.
5037      */
5038     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5039       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5040       .access = PL1_RW, .accessfn = sp_el0_access,
5041       .type = ARM_CP_ALIAS,
5042       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5043     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5044       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5045       .nv2_redirect_offset = 0x240,
5046       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5047       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5048     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5049       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5050       .type = ARM_CP_NO_RAW,
5051       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5052     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5053       .type = ARM_CP_ALIAS,
5054       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5055       .access = PL2_RW,
5056       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5057     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5058       .type = ARM_CP_ALIAS,
5059       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5060       .access = PL2_RW,
5061       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5062     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5063       .type = ARM_CP_ALIAS,
5064       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5065       .access = PL2_RW,
5066       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5067     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5068       .type = ARM_CP_ALIAS,
5069       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5070       .access = PL2_RW,
5071       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5072     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5073       .type = ARM_CP_IO,
5074       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5075       .resetvalue = 0,
5076       .access = PL3_RW,
5077       .writefn = mdcr_el3_write,
5078       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5079     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5080       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5081       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5082       .writefn = sdcr_write,
5083       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5084 };
5085 
5086 /* These are present only when EL1 supports AArch32 */
5087 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5088     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5089       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5090       .access = PL2_RW,
5091       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5092       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5093     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5094       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5095       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5096       .writefn = dacr_write, .raw_writefn = raw_write,
5097       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5098     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5099       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5100       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5101       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5102 };
5103 
5104 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5105 {
5106     ARMCPU *cpu = env_archcpu(env);
5107 
5108     if (arm_feature(env, ARM_FEATURE_V8)) {
5109         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5110     } else {
5111         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5112     }
5113 
5114     if (arm_feature(env, ARM_FEATURE_EL3)) {
5115         valid_mask &= ~HCR_HCD;
5116     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5117         /*
5118          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5119          * However, if we're using the SMC PSCI conduit then QEMU is
5120          * effectively acting like EL3 firmware and so the guest at
5121          * EL2 should retain the ability to prevent EL1 from being
5122          * able to make SMC calls into the ersatz firmware, so in
5123          * that case HCR.TSC should be read/write.
5124          */
5125         valid_mask &= ~HCR_TSC;
5126     }
5127 
5128     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5129         if (cpu_isar_feature(aa64_vh, cpu)) {
5130             valid_mask |= HCR_E2H;
5131         }
5132         if (cpu_isar_feature(aa64_ras, cpu)) {
5133             valid_mask |= HCR_TERR | HCR_TEA;
5134         }
5135         if (cpu_isar_feature(aa64_lor, cpu)) {
5136             valid_mask |= HCR_TLOR;
5137         }
5138         if (cpu_isar_feature(aa64_pauth, cpu)) {
5139             valid_mask |= HCR_API | HCR_APK;
5140         }
5141         if (cpu_isar_feature(aa64_mte, cpu)) {
5142             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5143         }
5144         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5145             valid_mask |= HCR_ENSCXT;
5146         }
5147         if (cpu_isar_feature(aa64_fwb, cpu)) {
5148             valid_mask |= HCR_FWB;
5149         }
5150         if (cpu_isar_feature(aa64_rme, cpu)) {
5151             valid_mask |= HCR_GPF;
5152         }
5153         if (cpu_isar_feature(aa64_nv, cpu)) {
5154             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5155         }
5156         if (cpu_isar_feature(aa64_nv2, cpu)) {
5157             valid_mask |= HCR_NV2;
5158         }
5159     }
5160 
5161     if (cpu_isar_feature(any_evt, cpu)) {
5162         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5163     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5164         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5165     }
5166 
5167     /* Clear RES0 bits.  */
5168     value &= valid_mask;
5169 
5170     /*
5171      * These bits change the MMU setup:
5172      * HCR_VM enables stage 2 translation
5173      * HCR_PTW forbids certain page-table setups
5174      * HCR_DC disables stage1 and enables stage2 translation
5175      * HCR_DCT enables tagging on (disabled) stage1 translation
5176      * HCR_FWB changes the interpretation of stage2 descriptor bits
5177      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5178      */
5179     if ((env->cp15.hcr_el2 ^ value) &
5180         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5181         tlb_flush(CPU(cpu));
5182     }
5183     env->cp15.hcr_el2 = value;
5184 
5185     /*
5186      * Updates to VI and VF require us to update the status of
5187      * virtual interrupts, which are the logical OR of these bits
5188      * and the state of the input lines from the GIC. (This requires
5189      * that we have the BQL, which is done by marking the
5190      * reginfo structs as ARM_CP_IO.)
5191      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5192      * VFNMI, it is never possible for it to be taken immediately
5193      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5194      * at EL0 or EL1, and HCR can only be written at EL2.
5195      */
5196     g_assert(bql_locked());
5197     arm_cpu_update_virq(cpu);
5198     arm_cpu_update_vfiq(cpu);
5199     arm_cpu_update_vserr(cpu);
5200     if (cpu_isar_feature(aa64_nmi, cpu)) {
5201         arm_cpu_update_vinmi(cpu);
5202         arm_cpu_update_vfnmi(cpu);
5203     }
5204 }
5205 
5206 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5207 {
5208     do_hcr_write(env, value, 0);
5209 }
5210 
5211 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5212                           uint64_t value)
5213 {
5214     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5215     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5216     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5217 }
5218 
5219 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5220                          uint64_t value)
5221 {
5222     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5223     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5224     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5225 }
5226 
5227 /*
5228  * Return the effective value of HCR_EL2, at the given security state.
5229  * Bits that are not included here:
5230  * RW       (read from SCR_EL3.RW as needed)
5231  */
5232 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5233 {
5234     uint64_t ret = env->cp15.hcr_el2;
5235 
5236     assert(space != ARMSS_Root);
5237 
5238     if (!arm_is_el2_enabled_secstate(env, space)) {
5239         /*
5240          * "This register has no effect if EL2 is not enabled in the
5241          * current Security state".  This is ARMv8.4-SecEL2 speak for
5242          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5243          *
5244          * Prior to that, the language was "In an implementation that
5245          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5246          * as if this field is 0 for all purposes other than a direct
5247          * read or write access of HCR_EL2".  With lots of enumeration
5248          * on a per-field basis.  In current QEMU, this is condition
5249          * is arm_is_secure_below_el3.
5250          *
5251          * Since the v8.4 language applies to the entire register, and
5252          * appears to be backward compatible, use that.
5253          */
5254         return 0;
5255     }
5256 
5257     /*
5258      * For a cpu that supports both aarch64 and aarch32, we can set bits
5259      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5260      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5261      */
5262     if (!arm_el_is_aa64(env, 2)) {
5263         uint64_t aa32_valid;
5264 
5265         /*
5266          * These bits are up-to-date as of ARMv8.6.
5267          * For HCR, it's easiest to list just the 2 bits that are invalid.
5268          * For HCR2, list those that are valid.
5269          */
5270         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5271         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5272                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5273         ret &= aa32_valid;
5274     }
5275 
5276     if (ret & HCR_TGE) {
5277         /* These bits are up-to-date as of ARMv8.6.  */
5278         if (ret & HCR_E2H) {
5279             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5280                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5281                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5282                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5283                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5284                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5285         } else {
5286             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5287         }
5288         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5289                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5290                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5291                  HCR_TLOR);
5292     }
5293 
5294     return ret;
5295 }
5296 
5297 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5298 {
5299     if (arm_feature(env, ARM_FEATURE_M)) {
5300         return 0;
5301     }
5302     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5303 }
5304 
5305 /*
5306  * Corresponds to ARM pseudocode function ELIsInHost().
5307  */
5308 bool el_is_in_host(CPUARMState *env, int el)
5309 {
5310     uint64_t mask;
5311 
5312     /*
5313      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5314      * Perform the simplest bit tests first, and validate EL2 afterward.
5315      */
5316     if (el & 1) {
5317         return false; /* EL1 or EL3 */
5318     }
5319 
5320     /*
5321      * Note that hcr_write() checks isar_feature_aa64_vh(),
5322      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5323      */
5324     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5325     if ((env->cp15.hcr_el2 & mask) != mask) {
5326         return false;
5327     }
5328 
5329     /* TGE and/or E2H set: double check those bits are currently legal. */
5330     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5331 }
5332 
5333 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5334                        uint64_t value)
5335 {
5336     ARMCPU *cpu = env_archcpu(env);
5337     uint64_t valid_mask = 0;
5338 
5339     /* FEAT_MOPS adds MSCEn and MCE2 */
5340     if (cpu_isar_feature(aa64_mops, cpu)) {
5341         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5342     }
5343 
5344     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5345     if (cpu_isar_feature(aa64_nmi, cpu)) {
5346         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5347     }
5348     /* FEAT_CMOW adds CMOW */
5349 
5350     if (cpu_isar_feature(aa64_cmow, cpu)) {
5351         valid_mask |= HCRX_CMOW;
5352     }
5353 
5354     /* Clear RES0 bits.  */
5355     env->cp15.hcrx_el2 = value & valid_mask;
5356 
5357     /*
5358      * Updates to VINMI and VFNMI require us to update the status of
5359      * virtual NMI, which are the logical OR of these bits
5360      * and the state of the input lines from the GIC. (This requires
5361      * that we have the BQL, which is done by marking the
5362      * reginfo structs as ARM_CP_IO.)
5363      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5364      * possible for it to be taken immediately, because VINMI and
5365      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5366      * can only be written at EL2.
5367      */
5368     if (cpu_isar_feature(aa64_nmi, cpu)) {
5369         g_assert(bql_locked());
5370         arm_cpu_update_vinmi(cpu);
5371         arm_cpu_update_vfnmi(cpu);
5372     }
5373 }
5374 
5375 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5376                                   bool isread)
5377 {
5378     if (arm_current_el(env) == 2
5379         && arm_feature(env, ARM_FEATURE_EL3)
5380         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5381         return CP_ACCESS_TRAP_EL3;
5382     }
5383     return CP_ACCESS_OK;
5384 }
5385 
5386 static const ARMCPRegInfo hcrx_el2_reginfo = {
5387     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5388     .type = ARM_CP_IO,
5389     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5390     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5391     .nv2_redirect_offset = 0xa0,
5392     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5393 };
5394 
5395 /* Return the effective value of HCRX_EL2.  */
5396 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5397 {
5398     /*
5399      * The bits in this register behave as 0 for all purposes other than
5400      * direct reads of the register if SCR_EL3.HXEn is 0.
5401      * If EL2 is not enabled in the current security state, then the
5402      * bit may behave as if 0, or as if 1, depending on the bit.
5403      * For the moment, we treat the EL2-disabled case as taking
5404      * priority over the HXEn-disabled case. This is true for the only
5405      * bit for a feature which we implement where the answer is different
5406      * for the two cases (MSCEn for FEAT_MOPS).
5407      * This may need to be revisited for future bits.
5408      */
5409     if (!arm_is_el2_enabled(env)) {
5410         uint64_t hcrx = 0;
5411         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5412             /* MSCEn behaves as 1 if EL2 is not enabled */
5413             hcrx |= HCRX_MSCEN;
5414         }
5415         return hcrx;
5416     }
5417     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5418         return 0;
5419     }
5420     return env->cp15.hcrx_el2;
5421 }
5422 
5423 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5424                            uint64_t value)
5425 {
5426     /*
5427      * For A-profile AArch32 EL3, if NSACR.CP10
5428      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5429      */
5430     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5431         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5432         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5433         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5434     }
5435     env->cp15.cptr_el[2] = value;
5436 }
5437 
5438 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5439 {
5440     /*
5441      * For A-profile AArch32 EL3, if NSACR.CP10
5442      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5443      */
5444     uint64_t value = env->cp15.cptr_el[2];
5445 
5446     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5447         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5448         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5449     }
5450     return value;
5451 }
5452 
5453 static const ARMCPRegInfo el2_cp_reginfo[] = {
5454     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5455       .type = ARM_CP_IO,
5456       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5457       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5458       .nv2_redirect_offset = 0x78,
5459       .writefn = hcr_write, .raw_writefn = raw_write },
5460     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5461       .type = ARM_CP_ALIAS | ARM_CP_IO,
5462       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5463       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5464       .writefn = hcr_writelow },
5465     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5466       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5467       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5468     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5469       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5470       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5471       .access = PL2_RW,
5472       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5473     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5474       .type = ARM_CP_NV2_REDIRECT,
5475       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5476       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5477     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5478       .type = ARM_CP_NV2_REDIRECT,
5479       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5480       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5481     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5482       .type = ARM_CP_ALIAS,
5483       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5484       .access = PL2_RW,
5485       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5486     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5487       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5488       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5489       .access = PL2_RW,
5490       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5491     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5492       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5493       .access = PL2_RW, .writefn = vbar_write,
5494       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5495       .resetvalue = 0 },
5496     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5497       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5498       .access = PL3_RW, .type = ARM_CP_ALIAS,
5499       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5500     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5501       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5502       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5503       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5504       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5505     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5506       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5507       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5508       .resetvalue = 0 },
5509     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5510       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5511       .access = PL2_RW, .type = ARM_CP_ALIAS,
5512       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5513     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5514       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5515       .access = PL2_RW, .type = ARM_CP_CONST,
5516       .resetvalue = 0 },
5517     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5518     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5519       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5520       .access = PL2_RW, .type = ARM_CP_CONST,
5521       .resetvalue = 0 },
5522     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5523       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5524       .access = PL2_RW, .type = ARM_CP_CONST,
5525       .resetvalue = 0 },
5526     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5527       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5528       .access = PL2_RW, .type = ARM_CP_CONST,
5529       .resetvalue = 0 },
5530     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5531       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5532       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5533       .raw_writefn = raw_write,
5534       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5535     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5536       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5537       .type = ARM_CP_ALIAS,
5538       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5539       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5540     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5541       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5542       .access = PL2_RW,
5543       .nv2_redirect_offset = 0x40,
5544       /* no .writefn needed as this can't cause an ASID change */
5545       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5546     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5547       .cp = 15, .opc1 = 6, .crm = 2,
5548       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5549       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5550       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5551       .writefn = vttbr_write, .raw_writefn = raw_write },
5552     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5553       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5554       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5555       .nv2_redirect_offset = 0x20,
5556       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5557     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5558       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5559       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5560       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5561     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5562       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5563       .access = PL2_RW, .resetvalue = 0,
5564       .nv2_redirect_offset = 0x90,
5565       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5566     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5567       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5568       .access = PL2_RW, .resetvalue = 0,
5569       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5570       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5571     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5572       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5573       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5574 #ifndef CONFIG_USER_ONLY
5575     /*
5576      * Unlike the other EL2-related AT operations, these must
5577      * UNDEF from EL3 if EL2 is not implemented, which is why we
5578      * define them here rather than with the rest of the AT ops.
5579      */
5580     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5581       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5582       .access = PL2_W, .accessfn = at_s1e2_access,
5583       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5584       .writefn = ats_write64 },
5585     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5586       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5587       .access = PL2_W, .accessfn = at_s1e2_access,
5588       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5589       .writefn = ats_write64 },
5590     /*
5591      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5592      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5593      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5594      * to behave as if SCR.NS was 1.
5595      */
5596     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5597       .access = PL2_W,
5598       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5599     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5600       .access = PL2_W,
5601       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5602     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5603       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5604       /*
5605        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5606        * reset values as IMPDEF. We choose to reset to 3 to comply with
5607        * both ARMv7 and ARMv8.
5608        */
5609       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5610       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5611       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5612     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5613       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5614       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5615       .writefn = gt_cntvoff_write,
5616       .nv2_redirect_offset = 0x60,
5617       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5618     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5619       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5620       .writefn = gt_cntvoff_write,
5621       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5622     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5623       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5624       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5625       .type = ARM_CP_IO, .access = PL2_RW,
5626       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5627     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5628       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5629       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5630       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5631     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5632       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5633       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5634       .resetfn = gt_hyp_timer_reset,
5635       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5636     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5637       .type = ARM_CP_IO,
5638       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5639       .access = PL2_RW,
5640       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5641       .resetvalue = 0,
5642       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5643 #endif
5644     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5645       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5646       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5647       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5648     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5649       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5650       .access = PL2_RW,
5651       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5652     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5653       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5654       .access = PL2_RW,
5655       .nv2_redirect_offset = 0x80,
5656       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5657 };
5658 
5659 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5660     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5661       .type = ARM_CP_ALIAS | ARM_CP_IO,
5662       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5663       .access = PL2_RW,
5664       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5665       .writefn = hcr_writehigh },
5666 };
5667 
5668 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5669                                   bool isread)
5670 {
5671     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5672         return CP_ACCESS_OK;
5673     }
5674     return CP_ACCESS_TRAP_UNCATEGORIZED;
5675 }
5676 
5677 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5678     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5679       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5680       .access = PL2_RW, .accessfn = sel2_access,
5681       .nv2_redirect_offset = 0x30,
5682       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5683     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5684       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5685       .access = PL2_RW, .accessfn = sel2_access,
5686       .nv2_redirect_offset = 0x48,
5687       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5688 };
5689 
5690 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5691                                    bool isread)
5692 {
5693     /*
5694      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5695      * At Secure EL1 it traps to EL3 or EL2.
5696      */
5697     if (arm_current_el(env) == 3) {
5698         return CP_ACCESS_OK;
5699     }
5700     if (arm_is_secure_below_el3(env)) {
5701         if (env->cp15.scr_el3 & SCR_EEL2) {
5702             return CP_ACCESS_TRAP_EL2;
5703         }
5704         return CP_ACCESS_TRAP_EL3;
5705     }
5706     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5707     if (isread) {
5708         return CP_ACCESS_OK;
5709     }
5710     return CP_ACCESS_TRAP_UNCATEGORIZED;
5711 }
5712 
5713 static const ARMCPRegInfo el3_cp_reginfo[] = {
5714     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5715       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5716       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5717       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5718     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5719       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5720       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5721       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5722       .writefn = scr_write, .raw_writefn = raw_write },
5723     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5724       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5725       .access = PL3_RW, .resetvalue = 0,
5726       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5727     { .name = "SDER",
5728       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5729       .access = PL3_RW, .resetvalue = 0,
5730       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5731     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5732       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5733       .writefn = vbar_write, .resetvalue = 0,
5734       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5735     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5736       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5737       .access = PL3_RW, .resetvalue = 0,
5738       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5739     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5740       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5741       .access = PL3_RW,
5742       /* no .writefn needed as this can't cause an ASID change */
5743       .resetvalue = 0,
5744       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5745     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5746       .type = ARM_CP_ALIAS,
5747       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5748       .access = PL3_RW,
5749       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5750     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5751       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5752       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5753     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5754       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5755       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5756     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5757       .type = ARM_CP_ALIAS,
5758       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5759       .access = PL3_RW,
5760       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5761     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5762       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5763       .access = PL3_RW, .writefn = vbar_write,
5764       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5765       .resetvalue = 0 },
5766     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5767       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5768       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5769       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5770     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5771       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5772       .access = PL3_RW, .resetvalue = 0,
5773       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5774     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5775       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5776       .access = PL3_RW, .type = ARM_CP_CONST,
5777       .resetvalue = 0 },
5778     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5779       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5780       .access = PL3_RW, .type = ARM_CP_CONST,
5781       .resetvalue = 0 },
5782     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5783       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5784       .access = PL3_RW, .type = ARM_CP_CONST,
5785       .resetvalue = 0 },
5786 };
5787 
5788 #ifndef CONFIG_USER_ONLY
5789 
5790 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
5791                                  bool isread)
5792 {
5793     if (arm_current_el(env) == 1) {
5794         /* This must be a FEAT_NV access */
5795         return CP_ACCESS_OK;
5796     }
5797     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
5798         return CP_ACCESS_TRAP_UNCATEGORIZED;
5799     }
5800     return CP_ACCESS_OK;
5801 }
5802 
5803 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
5804                                       bool isread)
5805 {
5806     if (arm_current_el(env) == 1) {
5807         /* This must be a FEAT_NV access with NVx == 101 */
5808         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
5809             return CP_ACCESS_TRAP_EL2;
5810         }
5811     }
5812     return e2h_access(env, ri, isread);
5813 }
5814 
5815 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
5816                                       bool isread)
5817 {
5818     if (arm_current_el(env) == 1) {
5819         /* This must be a FEAT_NV access with NVx == 101 */
5820         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
5821             return CP_ACCESS_TRAP_EL2;
5822         }
5823     }
5824     return e2h_access(env, ri, isread);
5825 }
5826 
5827 /* Test if system register redirection is to occur in the current state.  */
5828 static bool redirect_for_e2h(CPUARMState *env)
5829 {
5830     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5831 }
5832 
5833 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5834 {
5835     CPReadFn *readfn;
5836 
5837     if (redirect_for_e2h(env)) {
5838         /* Switch to the saved EL2 version of the register.  */
5839         ri = ri->opaque;
5840         readfn = ri->readfn;
5841     } else {
5842         readfn = ri->orig_readfn;
5843     }
5844     if (readfn == NULL) {
5845         readfn = raw_read;
5846     }
5847     return readfn(env, ri);
5848 }
5849 
5850 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5851                           uint64_t value)
5852 {
5853     CPWriteFn *writefn;
5854 
5855     if (redirect_for_e2h(env)) {
5856         /* Switch to the saved EL2 version of the register.  */
5857         ri = ri->opaque;
5858         writefn = ri->writefn;
5859     } else {
5860         writefn = ri->orig_writefn;
5861     }
5862     if (writefn == NULL) {
5863         writefn = raw_write;
5864     }
5865     writefn(env, ri, value);
5866 }
5867 
5868 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
5869 {
5870     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
5871     return ri->orig_readfn(env, ri->opaque);
5872 }
5873 
5874 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
5875                               uint64_t value)
5876 {
5877     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
5878     return ri->orig_writefn(env, ri->opaque, value);
5879 }
5880 
5881 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
5882                                          const ARMCPRegInfo *ri,
5883                                          bool isread)
5884 {
5885     if (arm_current_el(env) == 1) {
5886         /*
5887          * This must be a FEAT_NV access (will either trap or redirect
5888          * to memory). None of the registers with _EL12 aliases want to
5889          * apply their trap controls for this kind of access, so don't
5890          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
5891          */
5892         return CP_ACCESS_OK;
5893     }
5894     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
5895     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
5896         return CP_ACCESS_TRAP_UNCATEGORIZED;
5897     }
5898     if (ri->orig_accessfn) {
5899         return ri->orig_accessfn(env, ri->opaque, isread);
5900     }
5901     return CP_ACCESS_OK;
5902 }
5903 
5904 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5905 {
5906     struct E2HAlias {
5907         uint32_t src_key, dst_key, new_key;
5908         const char *src_name, *dst_name, *new_name;
5909         bool (*feature)(const ARMISARegisters *id);
5910     };
5911 
5912 #define K(op0, op1, crn, crm, op2) \
5913     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5914 
5915     static const struct E2HAlias aliases[] = {
5916         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5917           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5918         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5919           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5920         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5921           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5922         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5923           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5924         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5925           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5926         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5927           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5928         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5929           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5930         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5931           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5932         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5933           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5934         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5935           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5936         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5937           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5938         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5939           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5940         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5941           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5942         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5943           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5944         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5945           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5946         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5947           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5948 
5949         /*
5950          * Note that redirection of ZCR is mentioned in the description
5951          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5952          * not in the summary table.
5953          */
5954         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5955           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5956         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5957           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5958 
5959         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5960           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5961 
5962         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5963           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5964           isar_feature_aa64_scxtnum },
5965 
5966         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5967         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5968     };
5969 #undef K
5970 
5971     size_t i;
5972 
5973     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5974         const struct E2HAlias *a = &aliases[i];
5975         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5976         bool ok;
5977 
5978         if (a->feature && !a->feature(&cpu->isar)) {
5979             continue;
5980         }
5981 
5982         src_reg = g_hash_table_lookup(cpu->cp_regs,
5983                                       (gpointer)(uintptr_t)a->src_key);
5984         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5985                                       (gpointer)(uintptr_t)a->dst_key);
5986         g_assert(src_reg != NULL);
5987         g_assert(dst_reg != NULL);
5988 
5989         /* Cross-compare names to detect typos in the keys.  */
5990         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5991         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5992 
5993         /* None of the core system registers use opaque; we will.  */
5994         g_assert(src_reg->opaque == NULL);
5995 
5996         /* Create alias before redirection so we dup the right data. */
5997         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5998 
5999         new_reg->name = a->new_name;
6000         new_reg->type |= ARM_CP_ALIAS;
6001         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6002         new_reg->access &= PL2_RW | PL3_RW;
6003         /* The new_reg op fields are as per new_key, not the target reg */
6004         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6005             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6006         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6007             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6008         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6009             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6010         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6011             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6012         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6013             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6014         new_reg->opaque = src_reg;
6015         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6016         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6017         new_reg->orig_accessfn = src_reg->accessfn;
6018         if (!new_reg->raw_readfn) {
6019             new_reg->raw_readfn = raw_read;
6020         }
6021         if (!new_reg->raw_writefn) {
6022             new_reg->raw_writefn = raw_write;
6023         }
6024         new_reg->readfn = el2_e2h_e12_read;
6025         new_reg->writefn = el2_e2h_e12_write;
6026         new_reg->accessfn = el2_e2h_e12_access;
6027 
6028         /*
6029          * If the _EL1 register is redirected to memory by FEAT_NV2,
6030          * then it shares the offset with the _EL12 register,
6031          * and which one is redirected depends on HCR_EL2.NV1.
6032          */
6033         if (new_reg->nv2_redirect_offset) {
6034             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6035             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6036             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6037         }
6038 
6039         ok = g_hash_table_insert(cpu->cp_regs,
6040                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6041         g_assert(ok);
6042 
6043         src_reg->opaque = dst_reg;
6044         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6045         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6046         if (!src_reg->raw_readfn) {
6047             src_reg->raw_readfn = raw_read;
6048         }
6049         if (!src_reg->raw_writefn) {
6050             src_reg->raw_writefn = raw_write;
6051         }
6052         src_reg->readfn = el2_e2h_read;
6053         src_reg->writefn = el2_e2h_write;
6054     }
6055 }
6056 #endif
6057 
6058 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6059                                      bool isread)
6060 {
6061     int cur_el = arm_current_el(env);
6062 
6063     if (cur_el < 2) {
6064         uint64_t hcr = arm_hcr_el2_eff(env);
6065 
6066         if (cur_el == 0) {
6067             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6068                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6069                     return CP_ACCESS_TRAP_EL2;
6070                 }
6071             } else {
6072                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6073                     return CP_ACCESS_TRAP;
6074                 }
6075                 if (hcr & HCR_TID2) {
6076                     return CP_ACCESS_TRAP_EL2;
6077                 }
6078             }
6079         } else if (hcr & HCR_TID2) {
6080             return CP_ACCESS_TRAP_EL2;
6081         }
6082     }
6083 
6084     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6085         return CP_ACCESS_TRAP_EL2;
6086     }
6087 
6088     return CP_ACCESS_OK;
6089 }
6090 
6091 /*
6092  * Check for traps to RAS registers, which are controlled
6093  * by HCR_EL2.TERR and SCR_EL3.TERR.
6094  */
6095 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6096                                   bool isread)
6097 {
6098     int el = arm_current_el(env);
6099 
6100     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6101         return CP_ACCESS_TRAP_EL2;
6102     }
6103     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6104         return CP_ACCESS_TRAP_EL3;
6105     }
6106     return CP_ACCESS_OK;
6107 }
6108 
6109 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6110 {
6111     int el = arm_current_el(env);
6112 
6113     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6114         return env->cp15.vdisr_el2;
6115     }
6116     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6117         return 0; /* RAZ/WI */
6118     }
6119     return env->cp15.disr_el1;
6120 }
6121 
6122 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6123 {
6124     int el = arm_current_el(env);
6125 
6126     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6127         env->cp15.vdisr_el2 = val;
6128         return;
6129     }
6130     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6131         return; /* RAZ/WI */
6132     }
6133     env->cp15.disr_el1 = val;
6134 }
6135 
6136 /*
6137  * Minimal RAS implementation with no Error Records.
6138  * Which means that all of the Error Record registers:
6139  *   ERXADDR_EL1
6140  *   ERXCTLR_EL1
6141  *   ERXFR_EL1
6142  *   ERXMISC0_EL1
6143  *   ERXMISC1_EL1
6144  *   ERXMISC2_EL1
6145  *   ERXMISC3_EL1
6146  *   ERXPFGCDN_EL1  (RASv1p1)
6147  *   ERXPFGCTL_EL1  (RASv1p1)
6148  *   ERXPFGF_EL1    (RASv1p1)
6149  *   ERXSTATUS_EL1
6150  * and
6151  *   ERRSELR_EL1
6152  * may generate UNDEFINED, which is the effect we get by not
6153  * listing them at all.
6154  *
6155  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6156  * is higher priority than FGT-to-EL2 so we do not need to list them
6157  * in order to check for an FGT.
6158  */
6159 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6160     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6161       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6162       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6163       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6164     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6165       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6166       .access = PL1_R, .accessfn = access_terr,
6167       .fgt = FGT_ERRIDR_EL1,
6168       .type = ARM_CP_CONST, .resetvalue = 0 },
6169     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6170       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6171       .nv2_redirect_offset = 0x500,
6172       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6173     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6174       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6175       .nv2_redirect_offset = 0x508,
6176       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6177 };
6178 
6179 /*
6180  * Return the exception level to which exceptions should be taken
6181  * via SVEAccessTrap.  This excludes the check for whether the exception
6182  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6183  * be found by testing 0 < fp_exception_el < sve_exception_el.
6184  *
6185  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6186  * pseudocode does *not* separate out the FP trap checks, but has them
6187  * all in one function.
6188  */
6189 int sve_exception_el(CPUARMState *env, int el)
6190 {
6191 #ifndef CONFIG_USER_ONLY
6192     if (el <= 1 && !el_is_in_host(env, el)) {
6193         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6194         case 1:
6195             if (el != 0) {
6196                 break;
6197             }
6198             /* fall through */
6199         case 0:
6200         case 2:
6201             return 1;
6202         }
6203     }
6204 
6205     if (el <= 2 && arm_is_el2_enabled(env)) {
6206         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6207         if (env->cp15.hcr_el2 & HCR_E2H) {
6208             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6209             case 1:
6210                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6211                     break;
6212                 }
6213                 /* fall through */
6214             case 0:
6215             case 2:
6216                 return 2;
6217             }
6218         } else {
6219             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6220                 return 2;
6221             }
6222         }
6223     }
6224 
6225     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6226     if (arm_feature(env, ARM_FEATURE_EL3)
6227         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6228         return 3;
6229     }
6230 #endif
6231     return 0;
6232 }
6233 
6234 /*
6235  * Return the exception level to which exceptions should be taken for SME.
6236  * C.f. the ARM pseudocode function CheckSMEAccess.
6237  */
6238 int sme_exception_el(CPUARMState *env, int el)
6239 {
6240 #ifndef CONFIG_USER_ONLY
6241     if (el <= 1 && !el_is_in_host(env, el)) {
6242         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6243         case 1:
6244             if (el != 0) {
6245                 break;
6246             }
6247             /* fall through */
6248         case 0:
6249         case 2:
6250             return 1;
6251         }
6252     }
6253 
6254     if (el <= 2 && arm_is_el2_enabled(env)) {
6255         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6256         if (env->cp15.hcr_el2 & HCR_E2H) {
6257             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6258             case 1:
6259                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6260                     break;
6261                 }
6262                 /* fall through */
6263             case 0:
6264             case 2:
6265                 return 2;
6266             }
6267         } else {
6268             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6269                 return 2;
6270             }
6271         }
6272     }
6273 
6274     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6275     if (arm_feature(env, ARM_FEATURE_EL3)
6276         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6277         return 3;
6278     }
6279 #endif
6280     return 0;
6281 }
6282 
6283 /*
6284  * Given that SVE is enabled, return the vector length for EL.
6285  */
6286 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6287 {
6288     ARMCPU *cpu = env_archcpu(env);
6289     uint64_t *cr = env->vfp.zcr_el;
6290     uint32_t map = cpu->sve_vq.map;
6291     uint32_t len = ARM_MAX_VQ - 1;
6292 
6293     if (sm) {
6294         cr = env->vfp.smcr_el;
6295         map = cpu->sme_vq.map;
6296     }
6297 
6298     if (el <= 1 && !el_is_in_host(env, el)) {
6299         len = MIN(len, 0xf & (uint32_t)cr[1]);
6300     }
6301     if (el <= 2 && arm_is_el2_enabled(env)) {
6302         len = MIN(len, 0xf & (uint32_t)cr[2]);
6303     }
6304     if (arm_feature(env, ARM_FEATURE_EL3)) {
6305         len = MIN(len, 0xf & (uint32_t)cr[3]);
6306     }
6307 
6308     map &= MAKE_64BIT_MASK(0, len + 1);
6309     if (map != 0) {
6310         return 31 - clz32(map);
6311     }
6312 
6313     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6314     assert(sm);
6315     return ctz32(cpu->sme_vq.map);
6316 }
6317 
6318 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6319 {
6320     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6321 }
6322 
6323 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6324                       uint64_t value)
6325 {
6326     int cur_el = arm_current_el(env);
6327     int old_len = sve_vqm1_for_el(env, cur_el);
6328     int new_len;
6329 
6330     /* Bits other than [3:0] are RAZ/WI.  */
6331     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6332     raw_write(env, ri, value & 0xf);
6333 
6334     /*
6335      * Because we arrived here, we know both FP and SVE are enabled;
6336      * otherwise we would have trapped access to the ZCR_ELn register.
6337      */
6338     new_len = sve_vqm1_for_el(env, cur_el);
6339     if (new_len < old_len) {
6340         aarch64_sve_narrow_vq(env, new_len + 1);
6341     }
6342 }
6343 
6344 static const ARMCPRegInfo zcr_reginfo[] = {
6345     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6346       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6347       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6348       .access = PL1_RW, .type = ARM_CP_SVE,
6349       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6350       .writefn = zcr_write, .raw_writefn = raw_write },
6351     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6352       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6353       .access = PL2_RW, .type = ARM_CP_SVE,
6354       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6355       .writefn = zcr_write, .raw_writefn = raw_write },
6356     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6357       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6358       .access = PL3_RW, .type = ARM_CP_SVE,
6359       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6360       .writefn = zcr_write, .raw_writefn = raw_write },
6361 };
6362 
6363 #ifdef TARGET_AARCH64
6364 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6365                                     bool isread)
6366 {
6367     int el = arm_current_el(env);
6368 
6369     if (el == 0) {
6370         uint64_t sctlr = arm_sctlr(env, el);
6371         if (!(sctlr & SCTLR_EnTP2)) {
6372             return CP_ACCESS_TRAP;
6373         }
6374     }
6375     /* TODO: FEAT_FGT */
6376     if (el < 3
6377         && arm_feature(env, ARM_FEATURE_EL3)
6378         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6379         return CP_ACCESS_TRAP_EL3;
6380     }
6381     return CP_ACCESS_OK;
6382 }
6383 
6384 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6385                                       bool isread)
6386 {
6387     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6388     if (arm_current_el(env) == 2
6389         && arm_feature(env, ARM_FEATURE_EL3)
6390         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6391         return CP_ACCESS_TRAP_EL3;
6392     }
6393     return CP_ACCESS_OK;
6394 }
6395 
6396 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6397                                    bool isread)
6398 {
6399     if (arm_current_el(env) < 3
6400         && arm_feature(env, ARM_FEATURE_EL3)
6401         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6402         return CP_ACCESS_TRAP_EL3;
6403     }
6404     return CP_ACCESS_OK;
6405 }
6406 
6407 /* ResetSVEState */
6408 static void arm_reset_sve_state(CPUARMState *env)
6409 {
6410     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6411     /* Recall that FFR is stored as pregs[16]. */
6412     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6413     vfp_set_fpcr(env, 0x0800009f);
6414 }
6415 
6416 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6417 {
6418     uint64_t change = (env->svcr ^ new) & mask;
6419 
6420     if (change == 0) {
6421         return;
6422     }
6423     env->svcr ^= change;
6424 
6425     if (change & R_SVCR_SM_MASK) {
6426         arm_reset_sve_state(env);
6427     }
6428 
6429     /*
6430      * ResetSMEState.
6431      *
6432      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6433      * on enable: while disabled, the storage is inaccessible and the
6434      * value does not matter.  We're not saving the storage in vmstate
6435      * when disabled either.
6436      */
6437     if (change & new & R_SVCR_ZA_MASK) {
6438         memset(env->zarray, 0, sizeof(env->zarray));
6439     }
6440 
6441     if (tcg_enabled()) {
6442         arm_rebuild_hflags(env);
6443     }
6444 }
6445 
6446 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6447                        uint64_t value)
6448 {
6449     aarch64_set_svcr(env, value, -1);
6450 }
6451 
6452 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6453                        uint64_t value)
6454 {
6455     int cur_el = arm_current_el(env);
6456     int old_len = sve_vqm1_for_el(env, cur_el);
6457     int new_len;
6458 
6459     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6460     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6461     raw_write(env, ri, value);
6462 
6463     /*
6464      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6465      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6466      * current values for simplicity.  But for QEMU internals, we must still
6467      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6468      * above aarch64_sve_narrow_vq.
6469      */
6470     new_len = sve_vqm1_for_el(env, cur_el);
6471     if (new_len < old_len) {
6472         aarch64_sve_narrow_vq(env, new_len + 1);
6473     }
6474 }
6475 
6476 static const ARMCPRegInfo sme_reginfo[] = {
6477     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6478       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6479       .access = PL0_RW, .accessfn = access_tpidr2,
6480       .fgt = FGT_NTPIDR2_EL0,
6481       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6482     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6483       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6484       .access = PL0_RW, .type = ARM_CP_SME,
6485       .fieldoffset = offsetof(CPUARMState, svcr),
6486       .writefn = svcr_write, .raw_writefn = raw_write },
6487     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6488       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6489       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6490       .access = PL1_RW, .type = ARM_CP_SME,
6491       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6492       .writefn = smcr_write, .raw_writefn = raw_write },
6493     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6494       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6495       .access = PL2_RW, .type = ARM_CP_SME,
6496       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6497       .writefn = smcr_write, .raw_writefn = raw_write },
6498     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6499       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6500       .access = PL3_RW, .type = ARM_CP_SME,
6501       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6502       .writefn = smcr_write, .raw_writefn = raw_write },
6503     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6504       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6505       .access = PL1_R, .accessfn = access_aa64_tid1,
6506       /*
6507        * IMPLEMENTOR = 0 (software)
6508        * REVISION    = 0 (implementation defined)
6509        * SMPS        = 0 (no streaming execution priority in QEMU)
6510        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6511        */
6512       .type = ARM_CP_CONST, .resetvalue = 0, },
6513     /*
6514      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6515      */
6516     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6517       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6518       .access = PL1_RW, .accessfn = access_smpri,
6519       .fgt = FGT_NSMPRI_EL1,
6520       .type = ARM_CP_CONST, .resetvalue = 0 },
6521     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6522       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6523       .nv2_redirect_offset = 0x1f8,
6524       .access = PL2_RW, .accessfn = access_smprimap,
6525       .type = ARM_CP_CONST, .resetvalue = 0 },
6526 };
6527 
6528 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6529                         uint64_t value)
6530 {
6531     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6532     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6533         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6534         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6535 
6536     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6537 }
6538 
6539 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6540 {
6541     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6542                                      env_archcpu(env)->reset_l0gptsz);
6543 }
6544 
6545 static const ARMCPRegInfo rme_reginfo[] = {
6546     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6547       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6548       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6549       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6550     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6551       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6552       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6553     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6554       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6555       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6556     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6557       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6558       .access = PL3_W, .type = ARM_CP_NOP },
6559 };
6560 
6561 static const ARMCPRegInfo rme_mte_reginfo[] = {
6562     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6563       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6564       .access = PL3_W, .type = ARM_CP_NOP },
6565 };
6566 
6567 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6568                               uint64_t value)
6569 {
6570     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6571 }
6572 
6573 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6574 {
6575     return env->pstate & PSTATE_ALLINT;
6576 }
6577 
6578 static CPAccessResult aa64_allint_access(CPUARMState *env,
6579                                          const ARMCPRegInfo *ri, bool isread)
6580 {
6581     if (!isread && arm_current_el(env) == 1 &&
6582         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6583         return CP_ACCESS_TRAP_EL2;
6584     }
6585     return CP_ACCESS_OK;
6586 }
6587 
6588 static const ARMCPRegInfo nmi_reginfo[] = {
6589     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6590       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6591       .type = ARM_CP_NO_RAW,
6592       .access = PL1_RW, .accessfn = aa64_allint_access,
6593       .fieldoffset = offsetof(CPUARMState, pstate),
6594       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6595       .resetfn = arm_cp_reset_ignore },
6596 };
6597 #endif /* TARGET_AARCH64 */
6598 
6599 static void define_pmu_regs(ARMCPU *cpu)
6600 {
6601     /*
6602      * v7 performance monitor control register: same implementor
6603      * field as main ID register, and we implement four counters in
6604      * addition to the cycle count register.
6605      */
6606     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6607     ARMCPRegInfo pmcr = {
6608         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6609         .access = PL0_RW,
6610         .fgt = FGT_PMCR_EL0,
6611         .type = ARM_CP_IO | ARM_CP_ALIAS,
6612         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6613         .accessfn = pmreg_access,
6614         .readfn = pmcr_read, .raw_readfn = raw_read,
6615         .writefn = pmcr_write, .raw_writefn = raw_write,
6616     };
6617     ARMCPRegInfo pmcr64 = {
6618         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6619         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6620         .access = PL0_RW, .accessfn = pmreg_access,
6621         .fgt = FGT_PMCR_EL0,
6622         .type = ARM_CP_IO,
6623         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6624         .resetvalue = cpu->isar.reset_pmcr_el0,
6625         .readfn = pmcr_read, .raw_readfn = raw_read,
6626         .writefn = pmcr_write, .raw_writefn = raw_write,
6627     };
6628 
6629     define_one_arm_cp_reg(cpu, &pmcr);
6630     define_one_arm_cp_reg(cpu, &pmcr64);
6631     for (i = 0; i < pmcrn; i++) {
6632         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6633         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6634         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6635         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6636         ARMCPRegInfo pmev_regs[] = {
6637             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6638               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6639               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6640               .fgt = FGT_PMEVCNTRN_EL0,
6641               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6642               .accessfn = pmreg_access_xevcntr },
6643             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6644               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6645               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6646               .type = ARM_CP_IO,
6647               .fgt = FGT_PMEVCNTRN_EL0,
6648               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6649               .raw_readfn = pmevcntr_rawread,
6650               .raw_writefn = pmevcntr_rawwrite },
6651             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6652               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6653               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6654               .fgt = FGT_PMEVTYPERN_EL0,
6655               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6656               .accessfn = pmreg_access },
6657             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6658               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6659               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6660               .fgt = FGT_PMEVTYPERN_EL0,
6661               .type = ARM_CP_IO,
6662               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6663               .raw_writefn = pmevtyper_rawwrite },
6664         };
6665         define_arm_cp_regs(cpu, pmev_regs);
6666         g_free(pmevcntr_name);
6667         g_free(pmevcntr_el0_name);
6668         g_free(pmevtyper_name);
6669         g_free(pmevtyper_el0_name);
6670     }
6671     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6672         ARMCPRegInfo v81_pmu_regs[] = {
6673             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6674               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6675               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6676               .fgt = FGT_PMCEIDN_EL0,
6677               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6678             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6679               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6680               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6681               .fgt = FGT_PMCEIDN_EL0,
6682               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6683         };
6684         define_arm_cp_regs(cpu, v81_pmu_regs);
6685     }
6686     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6687         static const ARMCPRegInfo v84_pmmir = {
6688             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6689             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6690             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6691             .fgt = FGT_PMMIR_EL1,
6692             .resetvalue = 0
6693         };
6694         define_one_arm_cp_reg(cpu, &v84_pmmir);
6695     }
6696 }
6697 
6698 #ifndef CONFIG_USER_ONLY
6699 /*
6700  * We don't know until after realize whether there's a GICv3
6701  * attached, and that is what registers the gicv3 sysregs.
6702  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6703  * at runtime.
6704  */
6705 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6706 {
6707     ARMCPU *cpu = env_archcpu(env);
6708     uint64_t pfr1 = cpu->isar.id_pfr1;
6709 
6710     if (env->gicv3state) {
6711         pfr1 |= 1 << 28;
6712     }
6713     return pfr1;
6714 }
6715 
6716 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6717 {
6718     ARMCPU *cpu = env_archcpu(env);
6719     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6720 
6721     if (env->gicv3state) {
6722         pfr0 |= 1 << 24;
6723     }
6724     return pfr0;
6725 }
6726 #endif
6727 
6728 /*
6729  * Shared logic between LORID and the rest of the LOR* registers.
6730  * Secure state exclusion has already been dealt with.
6731  */
6732 static CPAccessResult access_lor_ns(CPUARMState *env,
6733                                     const ARMCPRegInfo *ri, bool isread)
6734 {
6735     int el = arm_current_el(env);
6736 
6737     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6738         return CP_ACCESS_TRAP_EL2;
6739     }
6740     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6741         return CP_ACCESS_TRAP_EL3;
6742     }
6743     return CP_ACCESS_OK;
6744 }
6745 
6746 static CPAccessResult access_lor_other(CPUARMState *env,
6747                                        const ARMCPRegInfo *ri, bool isread)
6748 {
6749     if (arm_is_secure_below_el3(env)) {
6750         /* Access denied in secure mode.  */
6751         return CP_ACCESS_TRAP;
6752     }
6753     return access_lor_ns(env, ri, isread);
6754 }
6755 
6756 /*
6757  * A trivial implementation of ARMv8.1-LOR leaves all of these
6758  * registers fixed at 0, which indicates that there are zero
6759  * supported Limited Ordering regions.
6760  */
6761 static const ARMCPRegInfo lor_reginfo[] = {
6762     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6763       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6764       .access = PL1_RW, .accessfn = access_lor_other,
6765       .fgt = FGT_LORSA_EL1,
6766       .type = ARM_CP_CONST, .resetvalue = 0 },
6767     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6768       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6769       .access = PL1_RW, .accessfn = access_lor_other,
6770       .fgt = FGT_LOREA_EL1,
6771       .type = ARM_CP_CONST, .resetvalue = 0 },
6772     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6773       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6774       .access = PL1_RW, .accessfn = access_lor_other,
6775       .fgt = FGT_LORN_EL1,
6776       .type = ARM_CP_CONST, .resetvalue = 0 },
6777     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6778       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6779       .access = PL1_RW, .accessfn = access_lor_other,
6780       .fgt = FGT_LORC_EL1,
6781       .type = ARM_CP_CONST, .resetvalue = 0 },
6782     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6784       .access = PL1_R, .accessfn = access_lor_ns,
6785       .fgt = FGT_LORID_EL1,
6786       .type = ARM_CP_CONST, .resetvalue = 0 },
6787 };
6788 
6789 #ifdef TARGET_AARCH64
6790 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6791                                    bool isread)
6792 {
6793     int el = arm_current_el(env);
6794 
6795     if (el < 2 &&
6796         arm_is_el2_enabled(env) &&
6797         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6798         return CP_ACCESS_TRAP_EL2;
6799     }
6800     if (el < 3 &&
6801         arm_feature(env, ARM_FEATURE_EL3) &&
6802         !(env->cp15.scr_el3 & SCR_APK)) {
6803         return CP_ACCESS_TRAP_EL3;
6804     }
6805     return CP_ACCESS_OK;
6806 }
6807 
6808 static const ARMCPRegInfo pauth_reginfo[] = {
6809     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6810       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6811       .access = PL1_RW, .accessfn = access_pauth,
6812       .fgt = FGT_APDAKEY,
6813       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6814     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6815       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6816       .access = PL1_RW, .accessfn = access_pauth,
6817       .fgt = FGT_APDAKEY,
6818       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6819     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6820       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6821       .access = PL1_RW, .accessfn = access_pauth,
6822       .fgt = FGT_APDBKEY,
6823       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6824     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6825       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6826       .access = PL1_RW, .accessfn = access_pauth,
6827       .fgt = FGT_APDBKEY,
6828       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6829     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6830       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6831       .access = PL1_RW, .accessfn = access_pauth,
6832       .fgt = FGT_APGAKEY,
6833       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6834     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6835       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6836       .access = PL1_RW, .accessfn = access_pauth,
6837       .fgt = FGT_APGAKEY,
6838       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6839     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6840       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6841       .access = PL1_RW, .accessfn = access_pauth,
6842       .fgt = FGT_APIAKEY,
6843       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6844     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6845       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6846       .access = PL1_RW, .accessfn = access_pauth,
6847       .fgt = FGT_APIAKEY,
6848       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6849     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6850       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6851       .access = PL1_RW, .accessfn = access_pauth,
6852       .fgt = FGT_APIBKEY,
6853       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6854     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6855       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6856       .access = PL1_RW, .accessfn = access_pauth,
6857       .fgt = FGT_APIBKEY,
6858       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6859 };
6860 
6861 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6862 {
6863     Error *err = NULL;
6864     uint64_t ret;
6865 
6866     /* Success sets NZCV = 0000.  */
6867     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6868 
6869     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6870         /*
6871          * ??? Failed, for unknown reasons in the crypto subsystem.
6872          * The best we can do is log the reason and return the
6873          * timed-out indication to the guest.  There is no reason
6874          * we know to expect this failure to be transitory, so the
6875          * guest may well hang retrying the operation.
6876          */
6877         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6878                       ri->name, error_get_pretty(err));
6879         error_free(err);
6880 
6881         env->ZF = 0; /* NZCF = 0100 */
6882         return 0;
6883     }
6884     return ret;
6885 }
6886 
6887 /* We do not support re-seeding, so the two registers operate the same.  */
6888 static const ARMCPRegInfo rndr_reginfo[] = {
6889     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6890       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6891       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6892       .access = PL0_R, .readfn = rndr_readfn },
6893     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6894       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6895       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6896       .access = PL0_R, .readfn = rndr_readfn },
6897 };
6898 
6899 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6900                           uint64_t value)
6901 {
6902 #ifdef CONFIG_TCG
6903     ARMCPU *cpu = env_archcpu(env);
6904     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6905     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6906     uint64_t vaddr_in = (uint64_t) value;
6907     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6908     void *haddr;
6909     int mem_idx = arm_env_mmu_index(env);
6910 
6911     /* This won't be crossing page boundaries */
6912     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6913     if (haddr) {
6914 #ifndef CONFIG_USER_ONLY
6915 
6916         ram_addr_t offset;
6917         MemoryRegion *mr;
6918 
6919         /* RCU lock is already being held */
6920         mr = memory_region_from_host(haddr, &offset);
6921 
6922         if (mr) {
6923             memory_region_writeback(mr, offset, dline_size);
6924         }
6925 #endif /*CONFIG_USER_ONLY*/
6926     }
6927 #else
6928     /* Handled by hardware accelerator. */
6929     g_assert_not_reached();
6930 #endif /* CONFIG_TCG */
6931 }
6932 
6933 static const ARMCPRegInfo dcpop_reg[] = {
6934     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6935       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6936       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6937       .fgt = FGT_DCCVAP,
6938       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6939 };
6940 
6941 static const ARMCPRegInfo dcpodp_reg[] = {
6942     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6943       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6944       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6945       .fgt = FGT_DCCVADP,
6946       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6947 };
6948 
6949 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6950                                        bool isread)
6951 {
6952     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6953         return CP_ACCESS_TRAP_EL2;
6954     }
6955 
6956     return CP_ACCESS_OK;
6957 }
6958 
6959 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6960                                  bool isread)
6961 {
6962     int el = arm_current_el(env);
6963     if (el < 2 && arm_is_el2_enabled(env)) {
6964         uint64_t hcr = arm_hcr_el2_eff(env);
6965         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6966             return CP_ACCESS_TRAP_EL2;
6967         }
6968     }
6969     if (el < 3 &&
6970         arm_feature(env, ARM_FEATURE_EL3) &&
6971         !(env->cp15.scr_el3 & SCR_ATA)) {
6972         return CP_ACCESS_TRAP_EL3;
6973     }
6974     return CP_ACCESS_OK;
6975 }
6976 
6977 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
6978                                       bool isread)
6979 {
6980     CPAccessResult nv1 = access_nv1(env, ri, isread);
6981 
6982     if (nv1 != CP_ACCESS_OK) {
6983         return nv1;
6984     }
6985     return access_mte(env, ri, isread);
6986 }
6987 
6988 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
6989                                       bool isread)
6990 {
6991     /*
6992      * TFSR_EL2: similar to generic access_mte(), but we need to
6993      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
6994      * if NV2 is enabled then we will redirect this to TFSR_EL1
6995      * after doing the HCR and SCR ATA traps; otherwise this will
6996      * be a trap to EL2 and the HCR/SCR traps do not apply.
6997      */
6998     int el = arm_current_el(env);
6999 
7000     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7001         return CP_ACCESS_OK;
7002     }
7003     if (el < 2 && arm_is_el2_enabled(env)) {
7004         uint64_t hcr = arm_hcr_el2_eff(env);
7005         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7006             return CP_ACCESS_TRAP_EL2;
7007         }
7008     }
7009     if (el < 3 &&
7010         arm_feature(env, ARM_FEATURE_EL3) &&
7011         !(env->cp15.scr_el3 & SCR_ATA)) {
7012         return CP_ACCESS_TRAP_EL3;
7013     }
7014     return CP_ACCESS_OK;
7015 }
7016 
7017 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7018 {
7019     return env->pstate & PSTATE_TCO;
7020 }
7021 
7022 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7023 {
7024     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7025 }
7026 
7027 static const ARMCPRegInfo mte_reginfo[] = {
7028     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7029       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7030       .access = PL1_RW, .accessfn = access_mte,
7031       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7032     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7033       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7034       .access = PL1_RW, .accessfn = access_tfsr_el1,
7035       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7036       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7037     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7038       .type = ARM_CP_NV2_REDIRECT,
7039       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7040       .access = PL2_RW, .accessfn = access_tfsr_el2,
7041       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7042     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7043       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7044       .access = PL3_RW,
7045       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7046     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7048       .access = PL1_RW, .accessfn = access_mte,
7049       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7050     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7052       .access = PL1_RW, .accessfn = access_mte,
7053       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7054     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7055       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7056       .type = ARM_CP_NO_RAW,
7057       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7058     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7059       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7060       .type = ARM_CP_NOP, .access = PL1_W,
7061       .fgt = FGT_DCIVAC,
7062       .accessfn = aa64_cacheop_poc_access },
7063     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7064       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7065       .fgt = FGT_DCISW,
7066       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7067     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7068       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7069       .type = ARM_CP_NOP, .access = PL1_W,
7070       .fgt = FGT_DCIVAC,
7071       .accessfn = aa64_cacheop_poc_access },
7072     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7073       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7074       .fgt = FGT_DCISW,
7075       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7076     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7078       .fgt = FGT_DCCSW,
7079       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7080     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7081       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7082       .fgt = FGT_DCCSW,
7083       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7084     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7085       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7086       .fgt = FGT_DCCISW,
7087       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7088     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7089       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7090       .fgt = FGT_DCCISW,
7091       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7092 };
7093 
7094 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7095     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7096       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7097       .type = ARM_CP_CONST, .access = PL0_RW, },
7098 };
7099 
7100 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7101     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7102       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7103       .type = ARM_CP_NOP, .access = PL0_W,
7104       .fgt = FGT_DCCVAC,
7105       .accessfn = aa64_cacheop_poc_access },
7106     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7107       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7108       .type = ARM_CP_NOP, .access = PL0_W,
7109       .fgt = FGT_DCCVAC,
7110       .accessfn = aa64_cacheop_poc_access },
7111     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7113       .type = ARM_CP_NOP, .access = PL0_W,
7114       .fgt = FGT_DCCVAP,
7115       .accessfn = aa64_cacheop_poc_access },
7116     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7117       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7118       .type = ARM_CP_NOP, .access = PL0_W,
7119       .fgt = FGT_DCCVAP,
7120       .accessfn = aa64_cacheop_poc_access },
7121     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7122       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7123       .type = ARM_CP_NOP, .access = PL0_W,
7124       .fgt = FGT_DCCVADP,
7125       .accessfn = aa64_cacheop_poc_access },
7126     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7127       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7128       .type = ARM_CP_NOP, .access = PL0_W,
7129       .fgt = FGT_DCCVADP,
7130       .accessfn = aa64_cacheop_poc_access },
7131     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7133       .type = ARM_CP_NOP, .access = PL0_W,
7134       .fgt = FGT_DCCIVAC,
7135       .accessfn = aa64_cacheop_poc_access },
7136     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7137       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7138       .type = ARM_CP_NOP, .access = PL0_W,
7139       .fgt = FGT_DCCIVAC,
7140       .accessfn = aa64_cacheop_poc_access },
7141     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7142       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7143       .access = PL0_W, .type = ARM_CP_DC_GVA,
7144 #ifndef CONFIG_USER_ONLY
7145       /* Avoid overhead of an access check that always passes in user-mode */
7146       .accessfn = aa64_zva_access,
7147       .fgt = FGT_DCZVA,
7148 #endif
7149     },
7150     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7151       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7152       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7153 #ifndef CONFIG_USER_ONLY
7154       /* Avoid overhead of an access check that always passes in user-mode */
7155       .accessfn = aa64_zva_access,
7156       .fgt = FGT_DCZVA,
7157 #endif
7158     },
7159 };
7160 
7161 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7162                                      bool isread)
7163 {
7164     uint64_t hcr = arm_hcr_el2_eff(env);
7165     int el = arm_current_el(env);
7166 
7167     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7168         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7169             if (hcr & HCR_TGE) {
7170                 return CP_ACCESS_TRAP_EL2;
7171             }
7172             return CP_ACCESS_TRAP;
7173         }
7174     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7175         return CP_ACCESS_TRAP_EL2;
7176     }
7177     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7178         return CP_ACCESS_TRAP_EL2;
7179     }
7180     if (el < 3
7181         && arm_feature(env, ARM_FEATURE_EL3)
7182         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7183         return CP_ACCESS_TRAP_EL3;
7184     }
7185     return CP_ACCESS_OK;
7186 }
7187 
7188 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7189                                          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_scxtnum(env, ri, isread);
7198 }
7199 
7200 static const ARMCPRegInfo scxtnum_reginfo[] = {
7201     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7202       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7203       .access = PL0_RW, .accessfn = access_scxtnum,
7204       .fgt = FGT_SCXTNUM_EL0,
7205       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7206     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7207       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7208       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7209       .fgt = FGT_SCXTNUM_EL1,
7210       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7211       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7212     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7213       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7214       .access = PL2_RW, .accessfn = access_scxtnum,
7215       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7216     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7217       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7218       .access = PL3_RW,
7219       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7220 };
7221 
7222 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7223                                  bool isread)
7224 {
7225     if (arm_current_el(env) == 2 &&
7226         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7227         return CP_ACCESS_TRAP_EL3;
7228     }
7229     return CP_ACCESS_OK;
7230 }
7231 
7232 static const ARMCPRegInfo fgt_reginfo[] = {
7233     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7234       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7235       .nv2_redirect_offset = 0x1b8,
7236       .access = PL2_RW, .accessfn = access_fgt,
7237       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7238     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7239       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7240       .nv2_redirect_offset = 0x1c0,
7241       .access = PL2_RW, .accessfn = access_fgt,
7242       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7243     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7244       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7245       .nv2_redirect_offset = 0x1d0,
7246       .access = PL2_RW, .accessfn = access_fgt,
7247       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7248     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7249       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7250       .nv2_redirect_offset = 0x1d8,
7251       .access = PL2_RW, .accessfn = access_fgt,
7252       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7253     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7254       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7255       .nv2_redirect_offset = 0x1c8,
7256       .access = PL2_RW, .accessfn = access_fgt,
7257       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7258 };
7259 
7260 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7261                        uint64_t value)
7262 {
7263     /*
7264      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7265      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7266      * about the RESS bits at the top -- we choose the "generate an EL2
7267      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7268      * the ptw.c code detect the resulting invalid address).
7269      */
7270     env->cp15.vncr_el2 = value & ~0xfffULL;
7271 }
7272 
7273 static const ARMCPRegInfo nv2_reginfo[] = {
7274     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7275       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7276       .access = PL2_RW,
7277       .writefn = vncr_write,
7278       .nv2_redirect_offset = 0xb0,
7279       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7280 };
7281 
7282 #endif /* TARGET_AARCH64 */
7283 
7284 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7285                                      bool isread)
7286 {
7287     int el = arm_current_el(env);
7288 
7289     if (el == 0) {
7290         uint64_t sctlr = arm_sctlr(env, el);
7291         if (!(sctlr & SCTLR_EnRCTX)) {
7292             return CP_ACCESS_TRAP;
7293         }
7294     } else if (el == 1) {
7295         uint64_t hcr = arm_hcr_el2_eff(env);
7296         if (hcr & HCR_NV) {
7297             return CP_ACCESS_TRAP_EL2;
7298         }
7299     }
7300     return CP_ACCESS_OK;
7301 }
7302 
7303 static const ARMCPRegInfo predinv_reginfo[] = {
7304     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7305       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7306       .fgt = FGT_CFPRCTX,
7307       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7308     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7309       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7310       .fgt = FGT_DVPRCTX,
7311       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7312     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7313       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7314       .fgt = FGT_CPPRCTX,
7315       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7316     /*
7317      * Note the AArch32 opcodes have a different OPC1.
7318      */
7319     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7320       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7321       .fgt = FGT_CFPRCTX,
7322       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7323     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7324       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7325       .fgt = FGT_DVPRCTX,
7326       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7327     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7328       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7329       .fgt = FGT_CPPRCTX,
7330       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7331 };
7332 
7333 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7334 {
7335     /* Read the high 32 bits of the current CCSIDR */
7336     return extract64(ccsidr_read(env, ri), 32, 32);
7337 }
7338 
7339 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7340     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7341       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7342       .access = PL1_R,
7343       .accessfn = access_tid4,
7344       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7345 };
7346 
7347 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7348                                        bool isread)
7349 {
7350     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7351         return CP_ACCESS_TRAP_EL2;
7352     }
7353 
7354     return CP_ACCESS_OK;
7355 }
7356 
7357 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7358                                        bool isread)
7359 {
7360     if (arm_feature(env, ARM_FEATURE_V8)) {
7361         return access_aa64_tid3(env, ri, isread);
7362     }
7363 
7364     return CP_ACCESS_OK;
7365 }
7366 
7367 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7368                                      bool isread)
7369 {
7370     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7371         return CP_ACCESS_TRAP_EL2;
7372     }
7373 
7374     return CP_ACCESS_OK;
7375 }
7376 
7377 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7378                                         const ARMCPRegInfo *ri, bool isread)
7379 {
7380     /*
7381      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7382      * in v7A, not in v8A.
7383      */
7384     if (!arm_feature(env, ARM_FEATURE_V8) &&
7385         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7386         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7387         return CP_ACCESS_TRAP_EL2;
7388     }
7389     return CP_ACCESS_OK;
7390 }
7391 
7392 static const ARMCPRegInfo jazelle_regs[] = {
7393     { .name = "JIDR",
7394       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7395       .access = PL1_R, .accessfn = access_jazelle,
7396       .type = ARM_CP_CONST, .resetvalue = 0 },
7397     { .name = "JOSCR",
7398       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7399       .accessfn = access_joscr_jmcr,
7400       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7401     { .name = "JMCR",
7402       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7403       .accessfn = access_joscr_jmcr,
7404       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7405 };
7406 
7407 static const ARMCPRegInfo contextidr_el2 = {
7408     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7409     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7410     .access = PL2_RW,
7411     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7412 };
7413 
7414 static const ARMCPRegInfo vhe_reginfo[] = {
7415     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7416       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7417       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7418       .raw_writefn = raw_write,
7419       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7420 #ifndef CONFIG_USER_ONLY
7421     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7422       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7423       .fieldoffset =
7424         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7425       .type = ARM_CP_IO, .access = PL2_RW,
7426       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7427     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7428       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7429       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7430       .resetfn = gt_hv_timer_reset,
7431       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7432     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7433       .type = ARM_CP_IO,
7434       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7435       .access = PL2_RW,
7436       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7437       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7438     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7439       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7440       .type = ARM_CP_IO | ARM_CP_ALIAS,
7441       .access = PL2_RW, .accessfn = access_el1nvpct,
7442       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7443       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7444       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7445     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7446       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7447       .type = ARM_CP_IO | ARM_CP_ALIAS,
7448       .access = PL2_RW, .accessfn = access_el1nvvct,
7449       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7450       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7451       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7452     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7453       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7454       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7455       .access = PL2_RW, .accessfn = e2h_access,
7456       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7457     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7458       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7459       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7460       .access = PL2_RW, .accessfn = e2h_access,
7461       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7462     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7463       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7464       .type = ARM_CP_IO | ARM_CP_ALIAS,
7465       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7466       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7467       .access = PL2_RW, .accessfn = access_el1nvpct,
7468       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7469     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7470       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7471       .type = ARM_CP_IO | ARM_CP_ALIAS,
7472       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7473       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7474       .access = PL2_RW, .accessfn = access_el1nvvct,
7475       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7476 #endif
7477 };
7478 
7479 #ifndef CONFIG_USER_ONLY
7480 static const ARMCPRegInfo ats1e1_reginfo[] = {
7481     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7482       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7483       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7484       .fgt = FGT_ATS1E1RP,
7485       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7486     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7487       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7488       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7489       .fgt = FGT_ATS1E1WP,
7490       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7491 };
7492 
7493 static const ARMCPRegInfo ats1cp_reginfo[] = {
7494     { .name = "ATS1CPRP",
7495       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7496       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7497       .writefn = ats_write },
7498     { .name = "ATS1CPWP",
7499       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7500       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7501       .writefn = ats_write },
7502 };
7503 #endif
7504 
7505 /*
7506  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7507  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7508  * is non-zero, which is never for ARMv7, optionally in ARMv8
7509  * and mandatorily for ARMv8.2 and up.
7510  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7511  * implementation is RAZ/WI we can ignore this detail, as we
7512  * do for ACTLR.
7513  */
7514 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7515     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7516       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7517       .access = PL1_RW, .accessfn = access_tacr,
7518       .type = ARM_CP_CONST, .resetvalue = 0 },
7519     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7520       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7521       .access = PL2_RW, .type = ARM_CP_CONST,
7522       .resetvalue = 0 },
7523 };
7524 
7525 void register_cp_regs_for_features(ARMCPU *cpu)
7526 {
7527     /* Register all the coprocessor registers based on feature bits */
7528     CPUARMState *env = &cpu->env;
7529     if (arm_feature(env, ARM_FEATURE_M)) {
7530         /* M profile has no coprocessor registers */
7531         return;
7532     }
7533 
7534     define_arm_cp_regs(cpu, cp_reginfo);
7535     if (!arm_feature(env, ARM_FEATURE_V8)) {
7536         /*
7537          * Must go early as it is full of wildcards that may be
7538          * overridden by later definitions.
7539          */
7540         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7541     }
7542 
7543     define_tlb_insn_regs(cpu);
7544 
7545     if (arm_feature(env, ARM_FEATURE_V6)) {
7546         /* The ID registers all have impdef reset values */
7547         ARMCPRegInfo v6_idregs[] = {
7548             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7549               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7550               .access = PL1_R, .type = ARM_CP_CONST,
7551               .accessfn = access_aa32_tid3,
7552               .resetvalue = cpu->isar.id_pfr0 },
7553             /*
7554              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7555              * the value of the GIC field until after we define these regs.
7556              */
7557             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7558               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7559               .access = PL1_R, .type = ARM_CP_NO_RAW,
7560               .accessfn = access_aa32_tid3,
7561 #ifdef CONFIG_USER_ONLY
7562               .type = ARM_CP_CONST,
7563               .resetvalue = cpu->isar.id_pfr1,
7564 #else
7565               .type = ARM_CP_NO_RAW,
7566               .accessfn = access_aa32_tid3,
7567               .readfn = id_pfr1_read,
7568               .writefn = arm_cp_write_ignore
7569 #endif
7570             },
7571             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7572               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7573               .access = PL1_R, .type = ARM_CP_CONST,
7574               .accessfn = access_aa32_tid3,
7575               .resetvalue = cpu->isar.id_dfr0 },
7576             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7577               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7578               .access = PL1_R, .type = ARM_CP_CONST,
7579               .accessfn = access_aa32_tid3,
7580               .resetvalue = cpu->id_afr0 },
7581             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7582               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7583               .access = PL1_R, .type = ARM_CP_CONST,
7584               .accessfn = access_aa32_tid3,
7585               .resetvalue = cpu->isar.id_mmfr0 },
7586             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7587               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7588               .access = PL1_R, .type = ARM_CP_CONST,
7589               .accessfn = access_aa32_tid3,
7590               .resetvalue = cpu->isar.id_mmfr1 },
7591             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7592               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7593               .access = PL1_R, .type = ARM_CP_CONST,
7594               .accessfn = access_aa32_tid3,
7595               .resetvalue = cpu->isar.id_mmfr2 },
7596             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7597               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7598               .access = PL1_R, .type = ARM_CP_CONST,
7599               .accessfn = access_aa32_tid3,
7600               .resetvalue = cpu->isar.id_mmfr3 },
7601             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7602               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7603               .access = PL1_R, .type = ARM_CP_CONST,
7604               .accessfn = access_aa32_tid3,
7605               .resetvalue = cpu->isar.id_isar0 },
7606             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7607               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7608               .access = PL1_R, .type = ARM_CP_CONST,
7609               .accessfn = access_aa32_tid3,
7610               .resetvalue = cpu->isar.id_isar1 },
7611             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7612               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7613               .access = PL1_R, .type = ARM_CP_CONST,
7614               .accessfn = access_aa32_tid3,
7615               .resetvalue = cpu->isar.id_isar2 },
7616             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7617               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7618               .access = PL1_R, .type = ARM_CP_CONST,
7619               .accessfn = access_aa32_tid3,
7620               .resetvalue = cpu->isar.id_isar3 },
7621             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7622               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7623               .access = PL1_R, .type = ARM_CP_CONST,
7624               .accessfn = access_aa32_tid3,
7625               .resetvalue = cpu->isar.id_isar4 },
7626             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7627               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7628               .access = PL1_R, .type = ARM_CP_CONST,
7629               .accessfn = access_aa32_tid3,
7630               .resetvalue = cpu->isar.id_isar5 },
7631             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7632               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7633               .access = PL1_R, .type = ARM_CP_CONST,
7634               .accessfn = access_aa32_tid3,
7635               .resetvalue = cpu->isar.id_mmfr4 },
7636             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7637               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7638               .access = PL1_R, .type = ARM_CP_CONST,
7639               .accessfn = access_aa32_tid3,
7640               .resetvalue = cpu->isar.id_isar6 },
7641         };
7642         define_arm_cp_regs(cpu, v6_idregs);
7643         define_arm_cp_regs(cpu, v6_cp_reginfo);
7644     } else {
7645         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7646     }
7647     if (arm_feature(env, ARM_FEATURE_V6K)) {
7648         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7649     }
7650     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7651         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7652     }
7653     if (arm_feature(env, ARM_FEATURE_V7)) {
7654         ARMCPRegInfo clidr = {
7655             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7656             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7657             .access = PL1_R, .type = ARM_CP_CONST,
7658             .accessfn = access_tid4,
7659             .fgt = FGT_CLIDR_EL1,
7660             .resetvalue = cpu->clidr
7661         };
7662         define_one_arm_cp_reg(cpu, &clidr);
7663         define_arm_cp_regs(cpu, v7_cp_reginfo);
7664         define_debug_regs(cpu);
7665         define_pmu_regs(cpu);
7666     } else {
7667         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7668     }
7669     if (arm_feature(env, ARM_FEATURE_V8)) {
7670         /*
7671          * v8 ID registers, which all have impdef reset values.
7672          * Note that within the ID register ranges the unused slots
7673          * must all RAZ, not UNDEF; future architecture versions may
7674          * define new registers here.
7675          * ID registers which are AArch64 views of the AArch32 ID registers
7676          * which already existed in v6 and v7 are handled elsewhere,
7677          * in v6_idregs[].
7678          */
7679         int i;
7680         ARMCPRegInfo v8_idregs[] = {
7681             /*
7682              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7683              * emulation because we don't know the right value for the
7684              * GIC field until after we define these regs.
7685              */
7686             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7687               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7688               .access = PL1_R,
7689 #ifdef CONFIG_USER_ONLY
7690               .type = ARM_CP_CONST,
7691               .resetvalue = cpu->isar.id_aa64pfr0
7692 #else
7693               .type = ARM_CP_NO_RAW,
7694               .accessfn = access_aa64_tid3,
7695               .readfn = id_aa64pfr0_read,
7696               .writefn = arm_cp_write_ignore
7697 #endif
7698             },
7699             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7700               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7701               .access = PL1_R, .type = ARM_CP_CONST,
7702               .accessfn = access_aa64_tid3,
7703               .resetvalue = cpu->isar.id_aa64pfr1},
7704             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7705               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7706               .access = PL1_R, .type = ARM_CP_CONST,
7707               .accessfn = access_aa64_tid3,
7708               .resetvalue = 0 },
7709             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7710               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7711               .access = PL1_R, .type = ARM_CP_CONST,
7712               .accessfn = access_aa64_tid3,
7713               .resetvalue = 0 },
7714             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7715               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7716               .access = PL1_R, .type = ARM_CP_CONST,
7717               .accessfn = access_aa64_tid3,
7718               .resetvalue = cpu->isar.id_aa64zfr0 },
7719             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7720               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7721               .access = PL1_R, .type = ARM_CP_CONST,
7722               .accessfn = access_aa64_tid3,
7723               .resetvalue = cpu->isar.id_aa64smfr0 },
7724             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7725               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7726               .access = PL1_R, .type = ARM_CP_CONST,
7727               .accessfn = access_aa64_tid3,
7728               .resetvalue = 0 },
7729             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7730               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7731               .access = PL1_R, .type = ARM_CP_CONST,
7732               .accessfn = access_aa64_tid3,
7733               .resetvalue = 0 },
7734             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7735               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7736               .access = PL1_R, .type = ARM_CP_CONST,
7737               .accessfn = access_aa64_tid3,
7738               .resetvalue = cpu->isar.id_aa64dfr0 },
7739             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7740               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7741               .access = PL1_R, .type = ARM_CP_CONST,
7742               .accessfn = access_aa64_tid3,
7743               .resetvalue = cpu->isar.id_aa64dfr1 },
7744             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7745               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7746               .access = PL1_R, .type = ARM_CP_CONST,
7747               .accessfn = access_aa64_tid3,
7748               .resetvalue = 0 },
7749             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7750               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7751               .access = PL1_R, .type = ARM_CP_CONST,
7752               .accessfn = access_aa64_tid3,
7753               .resetvalue = 0 },
7754             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7755               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7756               .access = PL1_R, .type = ARM_CP_CONST,
7757               .accessfn = access_aa64_tid3,
7758               .resetvalue = cpu->id_aa64afr0 },
7759             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7760               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7761               .access = PL1_R, .type = ARM_CP_CONST,
7762               .accessfn = access_aa64_tid3,
7763               .resetvalue = cpu->id_aa64afr1 },
7764             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7765               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7766               .access = PL1_R, .type = ARM_CP_CONST,
7767               .accessfn = access_aa64_tid3,
7768               .resetvalue = 0 },
7769             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7770               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7771               .access = PL1_R, .type = ARM_CP_CONST,
7772               .accessfn = access_aa64_tid3,
7773               .resetvalue = 0 },
7774             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7775               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7776               .access = PL1_R, .type = ARM_CP_CONST,
7777               .accessfn = access_aa64_tid3,
7778               .resetvalue = cpu->isar.id_aa64isar0 },
7779             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7780               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7781               .access = PL1_R, .type = ARM_CP_CONST,
7782               .accessfn = access_aa64_tid3,
7783               .resetvalue = cpu->isar.id_aa64isar1 },
7784             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
7785               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7786               .access = PL1_R, .type = ARM_CP_CONST,
7787               .accessfn = access_aa64_tid3,
7788               .resetvalue = cpu->isar.id_aa64isar2 },
7789             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7790               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7791               .access = PL1_R, .type = ARM_CP_CONST,
7792               .accessfn = access_aa64_tid3,
7793               .resetvalue = 0 },
7794             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7795               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7796               .access = PL1_R, .type = ARM_CP_CONST,
7797               .accessfn = access_aa64_tid3,
7798               .resetvalue = 0 },
7799             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7800               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7801               .access = PL1_R, .type = ARM_CP_CONST,
7802               .accessfn = access_aa64_tid3,
7803               .resetvalue = 0 },
7804             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7805               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7806               .access = PL1_R, .type = ARM_CP_CONST,
7807               .accessfn = access_aa64_tid3,
7808               .resetvalue = 0 },
7809             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7810               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7811               .access = PL1_R, .type = ARM_CP_CONST,
7812               .accessfn = access_aa64_tid3,
7813               .resetvalue = 0 },
7814             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7815               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7816               .access = PL1_R, .type = ARM_CP_CONST,
7817               .accessfn = access_aa64_tid3,
7818               .resetvalue = cpu->isar.id_aa64mmfr0 },
7819             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7820               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7821               .access = PL1_R, .type = ARM_CP_CONST,
7822               .accessfn = access_aa64_tid3,
7823               .resetvalue = cpu->isar.id_aa64mmfr1 },
7824             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7825               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7826               .access = PL1_R, .type = ARM_CP_CONST,
7827               .accessfn = access_aa64_tid3,
7828               .resetvalue = cpu->isar.id_aa64mmfr2 },
7829             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
7830               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7831               .access = PL1_R, .type = ARM_CP_CONST,
7832               .accessfn = access_aa64_tid3,
7833               .resetvalue = cpu->isar.id_aa64mmfr3 },
7834             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7835               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7836               .access = PL1_R, .type = ARM_CP_CONST,
7837               .accessfn = access_aa64_tid3,
7838               .resetvalue = 0 },
7839             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7840               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7841               .access = PL1_R, .type = ARM_CP_CONST,
7842               .accessfn = access_aa64_tid3,
7843               .resetvalue = 0 },
7844             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7845               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7846               .access = PL1_R, .type = ARM_CP_CONST,
7847               .accessfn = access_aa64_tid3,
7848               .resetvalue = 0 },
7849             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7850               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7851               .access = PL1_R, .type = ARM_CP_CONST,
7852               .accessfn = access_aa64_tid3,
7853               .resetvalue = 0 },
7854             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7855               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7856               .access = PL1_R, .type = ARM_CP_CONST,
7857               .accessfn = access_aa64_tid3,
7858               .resetvalue = cpu->isar.mvfr0 },
7859             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7860               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7861               .access = PL1_R, .type = ARM_CP_CONST,
7862               .accessfn = access_aa64_tid3,
7863               .resetvalue = cpu->isar.mvfr1 },
7864             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7865               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7866               .access = PL1_R, .type = ARM_CP_CONST,
7867               .accessfn = access_aa64_tid3,
7868               .resetvalue = cpu->isar.mvfr2 },
7869             /*
7870              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7871              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7872              * as RAZ, since it is in the "reserved for future ID
7873              * registers, RAZ" part of the AArch32 encoding space.
7874              */
7875             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7876               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7877               .access = PL1_R, .type = ARM_CP_CONST,
7878               .accessfn = access_aa64_tid3,
7879               .resetvalue = 0 },
7880             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7881               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7882               .access = PL1_R, .type = ARM_CP_CONST,
7883               .accessfn = access_aa64_tid3,
7884               .resetvalue = 0 },
7885             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7886               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7887               .access = PL1_R, .type = ARM_CP_CONST,
7888               .accessfn = access_aa64_tid3,
7889               .resetvalue = 0 },
7890             /*
7891              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7892              * they're also RAZ for AArch64, and in v8 are gradually
7893              * being filled with AArch64-view-of-AArch32-ID-register
7894              * for new ID registers.
7895              */
7896             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7897               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7898               .access = PL1_R, .type = ARM_CP_CONST,
7899               .accessfn = access_aa64_tid3,
7900               .resetvalue = 0 },
7901             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7902               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7903               .access = PL1_R, .type = ARM_CP_CONST,
7904               .accessfn = access_aa64_tid3,
7905               .resetvalue = cpu->isar.id_pfr2 },
7906             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7907               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7908               .access = PL1_R, .type = ARM_CP_CONST,
7909               .accessfn = access_aa64_tid3,
7910               .resetvalue = cpu->isar.id_dfr1 },
7911             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7912               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7913               .access = PL1_R, .type = ARM_CP_CONST,
7914               .accessfn = access_aa64_tid3,
7915               .resetvalue = cpu->isar.id_mmfr5 },
7916             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7917               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7918               .access = PL1_R, .type = ARM_CP_CONST,
7919               .accessfn = access_aa64_tid3,
7920               .resetvalue = 0 },
7921             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7922               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7923               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7924               .fgt = FGT_PMCEIDN_EL0,
7925               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7926             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7927               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7928               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7929               .fgt = FGT_PMCEIDN_EL0,
7930               .resetvalue = cpu->pmceid0 },
7931             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7932               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7933               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7934               .fgt = FGT_PMCEIDN_EL0,
7935               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7936             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7937               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7938               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7939               .fgt = FGT_PMCEIDN_EL0,
7940               .resetvalue = cpu->pmceid1 },
7941         };
7942 #ifdef CONFIG_USER_ONLY
7943         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7944             { .name = "ID_AA64PFR0_EL1",
7945               .exported_bits = R_ID_AA64PFR0_FP_MASK |
7946                                R_ID_AA64PFR0_ADVSIMD_MASK |
7947                                R_ID_AA64PFR0_SVE_MASK |
7948                                R_ID_AA64PFR0_DIT_MASK,
7949               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
7950                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
7951             { .name = "ID_AA64PFR1_EL1",
7952               .exported_bits = R_ID_AA64PFR1_BT_MASK |
7953                                R_ID_AA64PFR1_SSBS_MASK |
7954                                R_ID_AA64PFR1_MTE_MASK |
7955                                R_ID_AA64PFR1_SME_MASK },
7956             { .name = "ID_AA64PFR*_EL1_RESERVED",
7957               .is_glob = true },
7958             { .name = "ID_AA64ZFR0_EL1",
7959               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
7960                                R_ID_AA64ZFR0_AES_MASK |
7961                                R_ID_AA64ZFR0_BITPERM_MASK |
7962                                R_ID_AA64ZFR0_BFLOAT16_MASK |
7963                                R_ID_AA64ZFR0_B16B16_MASK |
7964                                R_ID_AA64ZFR0_SHA3_MASK |
7965                                R_ID_AA64ZFR0_SM4_MASK |
7966                                R_ID_AA64ZFR0_I8MM_MASK |
7967                                R_ID_AA64ZFR0_F32MM_MASK |
7968                                R_ID_AA64ZFR0_F64MM_MASK },
7969             { .name = "ID_AA64SMFR0_EL1",
7970               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
7971                                R_ID_AA64SMFR0_BI32I32_MASK |
7972                                R_ID_AA64SMFR0_B16F32_MASK |
7973                                R_ID_AA64SMFR0_F16F32_MASK |
7974                                R_ID_AA64SMFR0_I8I32_MASK |
7975                                R_ID_AA64SMFR0_F16F16_MASK |
7976                                R_ID_AA64SMFR0_B16B16_MASK |
7977                                R_ID_AA64SMFR0_I16I32_MASK |
7978                                R_ID_AA64SMFR0_F64F64_MASK |
7979                                R_ID_AA64SMFR0_I16I64_MASK |
7980                                R_ID_AA64SMFR0_SMEVER_MASK |
7981                                R_ID_AA64SMFR0_FA64_MASK },
7982             { .name = "ID_AA64MMFR0_EL1",
7983               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
7984               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
7985                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
7986             { .name = "ID_AA64MMFR1_EL1",
7987               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
7988             { .name = "ID_AA64MMFR2_EL1",
7989               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
7990             { .name = "ID_AA64MMFR3_EL1",
7991               .exported_bits = 0 },
7992             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7993               .is_glob = true },
7994             { .name = "ID_AA64DFR0_EL1",
7995               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
7996             { .name = "ID_AA64DFR1_EL1" },
7997             { .name = "ID_AA64DFR*_EL1_RESERVED",
7998               .is_glob = true },
7999             { .name = "ID_AA64AFR*",
8000               .is_glob = true },
8001             { .name = "ID_AA64ISAR0_EL1",
8002               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8003                                R_ID_AA64ISAR0_SHA1_MASK |
8004                                R_ID_AA64ISAR0_SHA2_MASK |
8005                                R_ID_AA64ISAR0_CRC32_MASK |
8006                                R_ID_AA64ISAR0_ATOMIC_MASK |
8007                                R_ID_AA64ISAR0_RDM_MASK |
8008                                R_ID_AA64ISAR0_SHA3_MASK |
8009                                R_ID_AA64ISAR0_SM3_MASK |
8010                                R_ID_AA64ISAR0_SM4_MASK |
8011                                R_ID_AA64ISAR0_DP_MASK |
8012                                R_ID_AA64ISAR0_FHM_MASK |
8013                                R_ID_AA64ISAR0_TS_MASK |
8014                                R_ID_AA64ISAR0_RNDR_MASK },
8015             { .name = "ID_AA64ISAR1_EL1",
8016               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8017                                R_ID_AA64ISAR1_APA_MASK |
8018                                R_ID_AA64ISAR1_API_MASK |
8019                                R_ID_AA64ISAR1_JSCVT_MASK |
8020                                R_ID_AA64ISAR1_FCMA_MASK |
8021                                R_ID_AA64ISAR1_LRCPC_MASK |
8022                                R_ID_AA64ISAR1_GPA_MASK |
8023                                R_ID_AA64ISAR1_GPI_MASK |
8024                                R_ID_AA64ISAR1_FRINTTS_MASK |
8025                                R_ID_AA64ISAR1_SB_MASK |
8026                                R_ID_AA64ISAR1_BF16_MASK |
8027                                R_ID_AA64ISAR1_DGH_MASK |
8028                                R_ID_AA64ISAR1_I8MM_MASK },
8029             { .name = "ID_AA64ISAR2_EL1",
8030               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8031                                R_ID_AA64ISAR2_RPRES_MASK |
8032                                R_ID_AA64ISAR2_GPA3_MASK |
8033                                R_ID_AA64ISAR2_APA3_MASK |
8034                                R_ID_AA64ISAR2_MOPS_MASK |
8035                                R_ID_AA64ISAR2_BC_MASK |
8036                                R_ID_AA64ISAR2_RPRFM_MASK |
8037                                R_ID_AA64ISAR2_CSSC_MASK },
8038             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8039               .is_glob = true },
8040         };
8041         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8042 #endif
8043         /*
8044          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8045          * TODO: For RMR, a write with bit 1 set should do something with
8046          * cpu_reset(). In the meantime, "the bit is strictly a request",
8047          * so we are in spec just ignoring writes.
8048          */
8049         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8050             !arm_feature(env, ARM_FEATURE_EL2)) {
8051             ARMCPRegInfo el1_reset_regs[] = {
8052                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8053                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8054                   .access = PL1_R,
8055                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8056                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8057                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8058                   .access = PL1_RW, .type = ARM_CP_CONST,
8059                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8060             };
8061             define_arm_cp_regs(cpu, el1_reset_regs);
8062         }
8063         define_arm_cp_regs(cpu, v8_idregs);
8064         define_arm_cp_regs(cpu, v8_cp_reginfo);
8065         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8066             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8067         }
8068 
8069         for (i = 4; i < 16; i++) {
8070             /*
8071              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8072              * For pre-v8 cores there are RAZ patterns for these in
8073              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8074              * v8 extends the "must RAZ" part of the ID register space
8075              * to also cover c0, 0, c{8-15}, {0-7}.
8076              * These are STATE_AA32 because in the AArch64 sysreg space
8077              * c4-c7 is where the AArch64 ID registers live (and we've
8078              * already defined those in v8_idregs[]), and c8-c15 are not
8079              * "must RAZ" for AArch64.
8080              */
8081             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8082             ARMCPRegInfo v8_aa32_raz_idregs = {
8083                 .name = name,
8084                 .state = ARM_CP_STATE_AA32,
8085                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8086                 .access = PL1_R, .type = ARM_CP_CONST,
8087                 .accessfn = access_aa64_tid3,
8088                 .resetvalue = 0 };
8089             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8090         }
8091     }
8092 
8093     /*
8094      * Register the base EL2 cpregs.
8095      * Pre v8, these registers are implemented only as part of the
8096      * Virtualization Extensions (EL2 present).  Beginning with v8,
8097      * if EL2 is missing but EL3 is enabled, mostly these become
8098      * RES0 from EL3, with some specific exceptions.
8099      */
8100     if (arm_feature(env, ARM_FEATURE_EL2)
8101         || (arm_feature(env, ARM_FEATURE_EL3)
8102             && arm_feature(env, ARM_FEATURE_V8))) {
8103         uint64_t vmpidr_def = mpidr_read_val(env);
8104         ARMCPRegInfo vpidr_regs[] = {
8105             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8106               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8107               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8108               .resetvalue = cpu->midr,
8109               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8110               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8111             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8112               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8113               .access = PL2_RW, .resetvalue = cpu->midr,
8114               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8115               .nv2_redirect_offset = 0x88,
8116               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8117             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8118               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8119               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8120               .resetvalue = vmpidr_def,
8121               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8122               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8123             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8124               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8125               .access = PL2_RW, .resetvalue = vmpidr_def,
8126               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8127               .nv2_redirect_offset = 0x50,
8128               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8129         };
8130         /*
8131          * The only field of MDCR_EL2 that has a defined architectural reset
8132          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8133          */
8134         ARMCPRegInfo mdcr_el2 = {
8135             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8136             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8137             .writefn = mdcr_el2_write,
8138             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8139             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8140         };
8141         define_one_arm_cp_reg(cpu, &mdcr_el2);
8142         define_arm_cp_regs(cpu, vpidr_regs);
8143         define_arm_cp_regs(cpu, el2_cp_reginfo);
8144         if (arm_feature(env, ARM_FEATURE_V8)) {
8145             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8146         }
8147         if (cpu_isar_feature(aa64_sel2, cpu)) {
8148             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8149         }
8150         /*
8151          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8152          * See commentary near RMR_EL1.
8153          */
8154         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8155             static const ARMCPRegInfo el2_reset_regs[] = {
8156                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8157                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8158                   .access = PL2_R,
8159                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8160                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8161                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8162                   .access = PL2_R,
8163                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8164                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8165                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8166                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8167             };
8168             define_arm_cp_regs(cpu, el2_reset_regs);
8169         }
8170     }
8171 
8172     /* Register the base EL3 cpregs. */
8173     if (arm_feature(env, ARM_FEATURE_EL3)) {
8174         define_arm_cp_regs(cpu, el3_cp_reginfo);
8175         ARMCPRegInfo el3_regs[] = {
8176             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8177               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8178               .access = PL3_R,
8179               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8180             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8181               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8182               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8183             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8184               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8185               .access = PL3_RW, .type = ARM_CP_CONST,
8186               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8187             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8188               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8189               .access = PL3_RW,
8190               .raw_writefn = raw_write, .writefn = sctlr_write,
8191               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8192               .resetvalue = cpu->reset_sctlr },
8193         };
8194 
8195         define_arm_cp_regs(cpu, el3_regs);
8196     }
8197     /*
8198      * The behaviour of NSACR is sufficiently various that we don't
8199      * try to describe it in a single reginfo:
8200      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8201      *     reads as constant 0xc00 from NS EL1 and NS EL2
8202      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8203      *  if v7 without EL3, register doesn't exist
8204      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8205      */
8206     if (arm_feature(env, ARM_FEATURE_EL3)) {
8207         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8208             static const ARMCPRegInfo nsacr = {
8209                 .name = "NSACR", .type = ARM_CP_CONST,
8210                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8211                 .access = PL1_RW, .accessfn = nsacr_access,
8212                 .resetvalue = 0xc00
8213             };
8214             define_one_arm_cp_reg(cpu, &nsacr);
8215         } else {
8216             static const ARMCPRegInfo nsacr = {
8217                 .name = "NSACR",
8218                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8219                 .access = PL3_RW | PL1_R,
8220                 .resetvalue = 0,
8221                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8222             };
8223             define_one_arm_cp_reg(cpu, &nsacr);
8224         }
8225     } else {
8226         if (arm_feature(env, ARM_FEATURE_V8)) {
8227             static const ARMCPRegInfo nsacr = {
8228                 .name = "NSACR", .type = ARM_CP_CONST,
8229                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8230                 .access = PL1_R,
8231                 .resetvalue = 0xc00
8232             };
8233             define_one_arm_cp_reg(cpu, &nsacr);
8234         }
8235     }
8236 
8237     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8238         if (arm_feature(env, ARM_FEATURE_V6)) {
8239             /* PMSAv6 not implemented */
8240             assert(arm_feature(env, ARM_FEATURE_V7));
8241             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8242             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8243         } else {
8244             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8245         }
8246     } else {
8247         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8248         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8249         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8250         if (cpu_isar_feature(aa32_hpd, cpu)) {
8251             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8252         }
8253     }
8254     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8255         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8256     }
8257     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8258         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8259     }
8260     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8261         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8262     }
8263 #ifndef CONFIG_USER_ONLY
8264     if (cpu_isar_feature(aa64_ecv, cpu)) {
8265         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8266     }
8267 #endif
8268     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8269         ARMCPRegInfo vapa_cp_reginfo[] = {
8270             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8271               .access = PL1_RW, .resetvalue = 0,
8272               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8273                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8274               .writefn = par_write},
8275 #ifndef CONFIG_USER_ONLY
8276             /* This underdecoding is safe because the reginfo is NO_RAW. */
8277             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8278               .access = PL1_W, .accessfn = ats_access,
8279               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8280 #endif
8281         };
8282 
8283         /*
8284          * When LPAE exists this 32-bit PAR register is an alias of the
8285          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8286          */
8287         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8288             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8289         }
8290         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8291     }
8292     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8293         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8294     }
8295     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8296         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8297     }
8298     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8299         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8300     }
8301     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8302         define_arm_cp_regs(cpu, omap_cp_reginfo);
8303     }
8304     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8305         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8306     }
8307     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8308         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8309     }
8310     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8311         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8312     }
8313     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8314         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8315     }
8316     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8317         define_arm_cp_regs(cpu, jazelle_regs);
8318     }
8319     /*
8320      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8321      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8322      * be read-only (ie write causes UNDEF exception).
8323      */
8324     {
8325         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8326             /*
8327              * Pre-v8 MIDR space.
8328              * Note that the MIDR isn't a simple constant register because
8329              * of the TI925 behaviour where writes to another register can
8330              * cause the MIDR value to change.
8331              *
8332              * Unimplemented registers in the c15 0 0 0 space default to
8333              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8334              * and friends override accordingly.
8335              */
8336             { .name = "MIDR",
8337               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8338               .access = PL1_R, .resetvalue = cpu->midr,
8339               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8340               .readfn = midr_read,
8341               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8342               .type = ARM_CP_OVERRIDE },
8343             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8344             { .name = "DUMMY",
8345               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8346               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8347             { .name = "DUMMY",
8348               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8349               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8350             { .name = "DUMMY",
8351               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8352               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8353             { .name = "DUMMY",
8354               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8355               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8356             { .name = "DUMMY",
8357               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8358               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8359         };
8360         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8361             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8362               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8363               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8364               .fgt = FGT_MIDR_EL1,
8365               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8366               .readfn = midr_read },
8367             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8368             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8369               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8370               .access = PL1_R, .resetvalue = cpu->midr },
8371             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8372               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8373               .access = PL1_R,
8374               .accessfn = access_aa64_tid1,
8375               .fgt = FGT_REVIDR_EL1,
8376               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8377         };
8378         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8379             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8380             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8381             .access = PL1_R, .resetvalue = cpu->midr
8382         };
8383         ARMCPRegInfo id_cp_reginfo[] = {
8384             /* These are common to v8 and pre-v8 */
8385             { .name = "CTR",
8386               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8387               .access = PL1_R, .accessfn = ctr_el0_access,
8388               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8389             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8390               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8391               .access = PL0_R, .accessfn = ctr_el0_access,
8392               .fgt = FGT_CTR_EL0,
8393               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8394             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8395             { .name = "TCMTR",
8396               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8397               .access = PL1_R,
8398               .accessfn = access_aa32_tid1,
8399               .type = ARM_CP_CONST, .resetvalue = 0 },
8400         };
8401         /* TLBTR is specific to VMSA */
8402         ARMCPRegInfo id_tlbtr_reginfo = {
8403               .name = "TLBTR",
8404               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8405               .access = PL1_R,
8406               .accessfn = access_aa32_tid1,
8407               .type = ARM_CP_CONST, .resetvalue = 0,
8408         };
8409         /* MPUIR is specific to PMSA V6+ */
8410         ARMCPRegInfo id_mpuir_reginfo = {
8411               .name = "MPUIR",
8412               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8413               .access = PL1_R, .type = ARM_CP_CONST,
8414               .resetvalue = cpu->pmsav7_dregion << 8
8415         };
8416         /* HMPUIR is specific to PMSA V8 */
8417         ARMCPRegInfo id_hmpuir_reginfo = {
8418             .name = "HMPUIR",
8419             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8420             .access = PL2_R, .type = ARM_CP_CONST,
8421             .resetvalue = cpu->pmsav8r_hdregion
8422         };
8423         static const ARMCPRegInfo crn0_wi_reginfo = {
8424             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8425             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8426             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8427         };
8428 #ifdef CONFIG_USER_ONLY
8429         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8430             { .name = "MIDR_EL1",
8431               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8432                                R_MIDR_EL1_PARTNUM_MASK |
8433                                R_MIDR_EL1_ARCHITECTURE_MASK |
8434                                R_MIDR_EL1_VARIANT_MASK |
8435                                R_MIDR_EL1_IMPLEMENTER_MASK },
8436             { .name = "REVIDR_EL1" },
8437         };
8438         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8439 #endif
8440         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8441             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8442             size_t i;
8443             /*
8444              * Register the blanket "writes ignored" value first to cover the
8445              * whole space. Then update the specific ID registers to allow write
8446              * access, so that they ignore writes rather than causing them to
8447              * UNDEF.
8448              */
8449             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8450             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8451                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8452             }
8453             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8454                 id_cp_reginfo[i].access = PL1_RW;
8455             }
8456             id_mpuir_reginfo.access = PL1_RW;
8457             id_tlbtr_reginfo.access = PL1_RW;
8458         }
8459         if (arm_feature(env, ARM_FEATURE_V8)) {
8460             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8461             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8462                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8463             }
8464         } else {
8465             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8466         }
8467         define_arm_cp_regs(cpu, id_cp_reginfo);
8468         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8469             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8470         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8471                    arm_feature(env, ARM_FEATURE_V8)) {
8472             uint32_t i = 0;
8473             char *tmp_string;
8474 
8475             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8476             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8477             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8478 
8479             /* Register alias is only valid for first 32 indexes */
8480             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8481                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8482                 uint8_t opc1 = extract32(i, 4, 1);
8483                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8484 
8485                 tmp_string = g_strdup_printf("PRBAR%u", i);
8486                 ARMCPRegInfo tmp_prbarn_reginfo = {
8487                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8488                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8489                     .access = PL1_RW, .resetvalue = 0,
8490                     .accessfn = access_tvm_trvm,
8491                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8492                 };
8493                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8494                 g_free(tmp_string);
8495 
8496                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8497                 tmp_string = g_strdup_printf("PRLAR%u", i);
8498                 ARMCPRegInfo tmp_prlarn_reginfo = {
8499                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8500                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8501                     .access = PL1_RW, .resetvalue = 0,
8502                     .accessfn = access_tvm_trvm,
8503                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8504                 };
8505                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8506                 g_free(tmp_string);
8507             }
8508 
8509             /* Register alias is only valid for first 32 indexes */
8510             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8511                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8512                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8513                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8514 
8515                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8516                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8517                     .name = tmp_string,
8518                     .type = ARM_CP_NO_RAW,
8519                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8520                     .access = PL2_RW, .resetvalue = 0,
8521                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8522                 };
8523                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8524                 g_free(tmp_string);
8525 
8526                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8527                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8528                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8529                     .name = tmp_string,
8530                     .type = ARM_CP_NO_RAW,
8531                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8532                     .access = PL2_RW, .resetvalue = 0,
8533                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8534                 };
8535                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8536                 g_free(tmp_string);
8537             }
8538         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8539             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8540         }
8541     }
8542 
8543     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8544         ARMCPRegInfo mpidr_cp_reginfo[] = {
8545             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8546               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8547               .fgt = FGT_MPIDR_EL1,
8548               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8549         };
8550 #ifdef CONFIG_USER_ONLY
8551         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8552             { .name = "MPIDR_EL1",
8553               .fixed_bits = 0x0000000080000000 },
8554         };
8555         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8556 #endif
8557         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8558     }
8559 
8560     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8561         ARMCPRegInfo auxcr_reginfo[] = {
8562             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8563               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8564               .access = PL1_RW, .accessfn = access_tacr,
8565               .nv2_redirect_offset = 0x118,
8566               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8567             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8568               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8569               .access = PL2_RW, .type = ARM_CP_CONST,
8570               .resetvalue = 0 },
8571             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8572               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8573               .access = PL3_RW, .type = ARM_CP_CONST,
8574               .resetvalue = 0 },
8575         };
8576         define_arm_cp_regs(cpu, auxcr_reginfo);
8577         if (cpu_isar_feature(aa32_ac2, cpu)) {
8578             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8579         }
8580     }
8581 
8582     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8583         /*
8584          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8585          * There are two flavours:
8586          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8587          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8588          *      32-bit register visible to AArch32 at a different encoding
8589          *      to the "flavour 1" register and with the bits rearranged to
8590          *      be able to squash a 64-bit address into the 32-bit view.
8591          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8592          * in future if we support AArch32-only configs of some of the
8593          * AArch64 cores we might need to add a specific feature flag
8594          * to indicate cores with "flavour 2" CBAR.
8595          */
8596         if (arm_feature(env, ARM_FEATURE_V8)) {
8597             /* 32 bit view is [31:18] 0...0 [43:32]. */
8598             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8599                 | extract64(cpu->reset_cbar, 32, 12);
8600             ARMCPRegInfo cbar_reginfo[] = {
8601                 { .name = "CBAR",
8602                   .type = ARM_CP_CONST,
8603                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8604                   .access = PL1_R, .resetvalue = cbar32 },
8605                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8606                   .type = ARM_CP_CONST,
8607                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8608                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8609             };
8610             /* We don't implement a r/w 64 bit CBAR currently */
8611             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8612             define_arm_cp_regs(cpu, cbar_reginfo);
8613         } else {
8614             ARMCPRegInfo cbar = {
8615                 .name = "CBAR",
8616                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8617                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8618                 .fieldoffset = offsetof(CPUARMState,
8619                                         cp15.c15_config_base_address)
8620             };
8621             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8622                 cbar.access = PL1_R;
8623                 cbar.fieldoffset = 0;
8624                 cbar.type = ARM_CP_CONST;
8625             }
8626             define_one_arm_cp_reg(cpu, &cbar);
8627         }
8628     }
8629 
8630     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8631         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8632             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8633               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8634               .access = PL1_RW, .writefn = vbar_write,
8635               .accessfn = access_nv1,
8636               .fgt = FGT_VBAR_EL1,
8637               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8638               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8639                                      offsetof(CPUARMState, cp15.vbar_ns) },
8640               .resetvalue = 0 },
8641         };
8642         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8643     }
8644 
8645     /* Generic registers whose values depend on the implementation */
8646     {
8647         ARMCPRegInfo sctlr = {
8648             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8649             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8650             .access = PL1_RW, .accessfn = access_tvm_trvm,
8651             .fgt = FGT_SCTLR_EL1,
8652             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8653             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8654                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8655             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8656             .raw_writefn = raw_write,
8657         };
8658         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8659             /*
8660              * Normally we would always end the TB on an SCTLR write, but Linux
8661              * arch/arm/mach-pxa/sleep.S expects two instructions following
8662              * an MMU enable to execute from cache.  Imitate this behaviour.
8663              */
8664             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8665         }
8666         define_one_arm_cp_reg(cpu, &sctlr);
8667 
8668         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8669             arm_feature(env, ARM_FEATURE_V8)) {
8670             ARMCPRegInfo vsctlr = {
8671                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8672                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8673                 .access = PL2_RW, .resetvalue = 0x0,
8674                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8675             };
8676             define_one_arm_cp_reg(cpu, &vsctlr);
8677         }
8678     }
8679 
8680     if (cpu_isar_feature(aa64_lor, cpu)) {
8681         define_arm_cp_regs(cpu, lor_reginfo);
8682     }
8683     if (cpu_isar_feature(aa64_pan, cpu)) {
8684         define_one_arm_cp_reg(cpu, &pan_reginfo);
8685     }
8686 #ifndef CONFIG_USER_ONLY
8687     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8688         define_arm_cp_regs(cpu, ats1e1_reginfo);
8689     }
8690     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8691         define_arm_cp_regs(cpu, ats1cp_reginfo);
8692     }
8693 #endif
8694     if (cpu_isar_feature(aa64_uao, cpu)) {
8695         define_one_arm_cp_reg(cpu, &uao_reginfo);
8696     }
8697 
8698     if (cpu_isar_feature(aa64_dit, cpu)) {
8699         define_one_arm_cp_reg(cpu, &dit_reginfo);
8700     }
8701     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8702         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8703     }
8704     if (cpu_isar_feature(any_ras, cpu)) {
8705         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8706     }
8707 
8708     if (cpu_isar_feature(aa64_vh, cpu) ||
8709         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8710         define_one_arm_cp_reg(cpu, &contextidr_el2);
8711     }
8712     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8713         define_arm_cp_regs(cpu, vhe_reginfo);
8714     }
8715 
8716     if (cpu_isar_feature(aa64_sve, cpu)) {
8717         define_arm_cp_regs(cpu, zcr_reginfo);
8718     }
8719 
8720     if (cpu_isar_feature(aa64_hcx, cpu)) {
8721         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8722     }
8723 
8724 #ifdef TARGET_AARCH64
8725     if (cpu_isar_feature(aa64_sme, cpu)) {
8726         define_arm_cp_regs(cpu, sme_reginfo);
8727     }
8728     if (cpu_isar_feature(aa64_pauth, cpu)) {
8729         define_arm_cp_regs(cpu, pauth_reginfo);
8730     }
8731     if (cpu_isar_feature(aa64_rndr, cpu)) {
8732         define_arm_cp_regs(cpu, rndr_reginfo);
8733     }
8734     /* Data Cache clean instructions up to PoP */
8735     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8736         define_one_arm_cp_reg(cpu, dcpop_reg);
8737 
8738         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8739             define_one_arm_cp_reg(cpu, dcpodp_reg);
8740         }
8741     }
8742 
8743     /*
8744      * If full MTE is enabled, add all of the system registers.
8745      * If only "instructions available at EL0" are enabled,
8746      * then define only a RAZ/WI version of PSTATE.TCO.
8747      */
8748     if (cpu_isar_feature(aa64_mte, cpu)) {
8749         ARMCPRegInfo gmid_reginfo = {
8750             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8751             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8752             .access = PL1_R, .accessfn = access_aa64_tid5,
8753             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8754         };
8755         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8756         define_arm_cp_regs(cpu, mte_reginfo);
8757         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8758     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8759         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8760         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8761     }
8762 
8763     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8764         define_arm_cp_regs(cpu, scxtnum_reginfo);
8765     }
8766 
8767     if (cpu_isar_feature(aa64_fgt, cpu)) {
8768         define_arm_cp_regs(cpu, fgt_reginfo);
8769     }
8770 
8771     if (cpu_isar_feature(aa64_rme, cpu)) {
8772         define_arm_cp_regs(cpu, rme_reginfo);
8773         if (cpu_isar_feature(aa64_mte, cpu)) {
8774             define_arm_cp_regs(cpu, rme_mte_reginfo);
8775         }
8776     }
8777 
8778     if (cpu_isar_feature(aa64_nv2, cpu)) {
8779         define_arm_cp_regs(cpu, nv2_reginfo);
8780     }
8781 
8782     if (cpu_isar_feature(aa64_nmi, cpu)) {
8783         define_arm_cp_regs(cpu, nmi_reginfo);
8784     }
8785 #endif
8786 
8787     if (cpu_isar_feature(any_predinv, cpu)) {
8788         define_arm_cp_regs(cpu, predinv_reginfo);
8789     }
8790 
8791     if (cpu_isar_feature(any_ccidx, cpu)) {
8792         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8793     }
8794 
8795 #ifndef CONFIG_USER_ONLY
8796     /*
8797      * Register redirections and aliases must be done last,
8798      * after the registers from the other extensions have been defined.
8799      */
8800     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8801         define_arm_vh_e2h_redirects_aliases(cpu);
8802     }
8803 #endif
8804 }
8805 
8806 /*
8807  * Private utility function for define_one_arm_cp_reg_with_opaque():
8808  * add a single reginfo struct to the hash table.
8809  */
8810 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8811                                    void *opaque, CPState state,
8812                                    CPSecureState secstate,
8813                                    int crm, int opc1, int opc2,
8814                                    const char *name)
8815 {
8816     CPUARMState *env = &cpu->env;
8817     uint32_t key;
8818     ARMCPRegInfo *r2;
8819     bool is64 = r->type & ARM_CP_64BIT;
8820     bool ns = secstate & ARM_CP_SECSTATE_NS;
8821     int cp = r->cp;
8822     size_t name_len;
8823     bool make_const;
8824 
8825     switch (state) {
8826     case ARM_CP_STATE_AA32:
8827         /* We assume it is a cp15 register if the .cp field is left unset. */
8828         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8829             cp = 15;
8830         }
8831         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8832         break;
8833     case ARM_CP_STATE_AA64:
8834         /*
8835          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8836          * cp == 0 as equivalent to the value for "standard guest-visible
8837          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8838          * in their AArch64 view (the .cp value may be non-zero for the
8839          * benefit of the AArch32 view).
8840          */
8841         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8842             cp = CP_REG_ARM64_SYSREG_CP;
8843         }
8844         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8845         break;
8846     default:
8847         g_assert_not_reached();
8848     }
8849 
8850     /* Overriding of an existing definition must be explicitly requested. */
8851     if (!(r->type & ARM_CP_OVERRIDE)) {
8852         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8853         if (oldreg) {
8854             assert(oldreg->type & ARM_CP_OVERRIDE);
8855         }
8856     }
8857 
8858     /*
8859      * Eliminate registers that are not present because the EL is missing.
8860      * Doing this here makes it easier to put all registers for a given
8861      * feature into the same ARMCPRegInfo array and define them all at once.
8862      */
8863     make_const = false;
8864     if (arm_feature(env, ARM_FEATURE_EL3)) {
8865         /*
8866          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8867          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8868          */
8869         int min_el = ctz32(r->access) / 2;
8870         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8871             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8872                 return;
8873             }
8874             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8875         }
8876     } else {
8877         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8878                                  ? PL2_RW : PL1_RW);
8879         if ((r->access & max_el) == 0) {
8880             return;
8881         }
8882     }
8883 
8884     /* Combine cpreg and name into one allocation. */
8885     name_len = strlen(name) + 1;
8886     r2 = g_malloc(sizeof(*r2) + name_len);
8887     *r2 = *r;
8888     r2->name = memcpy(r2 + 1, name, name_len);
8889 
8890     /*
8891      * Update fields to match the instantiation, overwiting wildcards
8892      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8893      */
8894     r2->cp = cp;
8895     r2->crm = crm;
8896     r2->opc1 = opc1;
8897     r2->opc2 = opc2;
8898     r2->state = state;
8899     r2->secure = secstate;
8900     if (opaque) {
8901         r2->opaque = opaque;
8902     }
8903 
8904     if (make_const) {
8905         /* This should not have been a very special register to begin. */
8906         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8907         assert(old_special == 0 || old_special == ARM_CP_NOP);
8908         /*
8909          * Set the special function to CONST, retaining the other flags.
8910          * This is important for e.g. ARM_CP_SVE so that we still
8911          * take the SVE trap if CPTR_EL3.EZ == 0.
8912          */
8913         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8914         /*
8915          * Usually, these registers become RES0, but there are a few
8916          * special cases like VPIDR_EL2 which have a constant non-zero
8917          * value with writes ignored.
8918          */
8919         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8920             r2->resetvalue = 0;
8921         }
8922         /*
8923          * ARM_CP_CONST has precedence, so removing the callbacks and
8924          * offsets are not strictly necessary, but it is potentially
8925          * less confusing to debug later.
8926          */
8927         r2->readfn = NULL;
8928         r2->writefn = NULL;
8929         r2->raw_readfn = NULL;
8930         r2->raw_writefn = NULL;
8931         r2->resetfn = NULL;
8932         r2->fieldoffset = 0;
8933         r2->bank_fieldoffsets[0] = 0;
8934         r2->bank_fieldoffsets[1] = 0;
8935     } else {
8936         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8937 
8938         if (isbanked) {
8939             /*
8940              * Register is banked (using both entries in array).
8941              * Overwriting fieldoffset as the array is only used to define
8942              * banked registers but later only fieldoffset is used.
8943              */
8944             r2->fieldoffset = r->bank_fieldoffsets[ns];
8945         }
8946         if (state == ARM_CP_STATE_AA32) {
8947             if (isbanked) {
8948                 /*
8949                  * If the register is banked then we don't need to migrate or
8950                  * reset the 32-bit instance in certain cases:
8951                  *
8952                  * 1) If the register has both 32-bit and 64-bit instances
8953                  *    then we can count on the 64-bit instance taking care
8954                  *    of the non-secure bank.
8955                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8956                  *    version taking care of the secure bank.  This requires
8957                  *    that separate 32 and 64-bit definitions are provided.
8958                  */
8959                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8960                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8961                     r2->type |= ARM_CP_ALIAS;
8962                 }
8963             } else if ((secstate != r->secure) && !ns) {
8964                 /*
8965                  * The register is not banked so we only want to allow
8966                  * migration of the non-secure instance.
8967                  */
8968                 r2->type |= ARM_CP_ALIAS;
8969             }
8970 
8971             if (HOST_BIG_ENDIAN &&
8972                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8973                 r2->fieldoffset += sizeof(uint32_t);
8974             }
8975         }
8976     }
8977 
8978     /*
8979      * By convention, for wildcarded registers only the first
8980      * entry is used for migration; the others are marked as
8981      * ALIAS so we don't try to transfer the register
8982      * multiple times. Special registers (ie NOP/WFI) are
8983      * never migratable and not even raw-accessible.
8984      */
8985     if (r2->type & ARM_CP_SPECIAL_MASK) {
8986         r2->type |= ARM_CP_NO_RAW;
8987     }
8988     if (((r->crm == CP_ANY) && crm != 0) ||
8989         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8990         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8991         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8992     }
8993 
8994     /*
8995      * Check that raw accesses are either forbidden or handled. Note that
8996      * we can't assert this earlier because the setup of fieldoffset for
8997      * banked registers has to be done first.
8998      */
8999     if (!(r2->type & ARM_CP_NO_RAW)) {
9000         assert(!raw_accessors_invalid(r2));
9001     }
9002 
9003     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9004 }
9005 
9006 
9007 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9008                                        const ARMCPRegInfo *r, void *opaque)
9009 {
9010     /*
9011      * Define implementations of coprocessor registers.
9012      * We store these in a hashtable because typically
9013      * there are less than 150 registers in a space which
9014      * is 16*16*16*8*8 = 262144 in size.
9015      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9016      * If a register is defined twice then the second definition is
9017      * used, so this can be used to define some generic registers and
9018      * then override them with implementation specific variations.
9019      * At least one of the original and the second definition should
9020      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9021      * against accidental use.
9022      *
9023      * The state field defines whether the register is to be
9024      * visible in the AArch32 or AArch64 execution state. If the
9025      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9026      * reginfo structure for the AArch32 view, which sees the lower
9027      * 32 bits of the 64 bit register.
9028      *
9029      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9030      * be wildcarded. AArch64 registers are always considered to be 64
9031      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9032      * the register, if any.
9033      */
9034     int crm, opc1, opc2;
9035     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9036     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9037     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9038     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9039     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9040     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9041     CPState state;
9042 
9043     /* 64 bit registers have only CRm and Opc1 fields */
9044     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9045     /* op0 only exists in the AArch64 encodings */
9046     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9047     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9048     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9049     /*
9050      * This API is only for Arm's system coprocessors (14 and 15) or
9051      * (M-profile or v7A-and-earlier only) for implementation defined
9052      * coprocessors in the range 0..7.  Our decode assumes this, since
9053      * 8..13 can be used for other insns including VFP and Neon. See
9054      * valid_cp() in translate.c.  Assert here that we haven't tried
9055      * to use an invalid coprocessor number.
9056      */
9057     switch (r->state) {
9058     case ARM_CP_STATE_BOTH:
9059         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9060         if (r->cp == 0) {
9061             break;
9062         }
9063         /* fall through */
9064     case ARM_CP_STATE_AA32:
9065         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9066             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9067             assert(r->cp >= 14 && r->cp <= 15);
9068         } else {
9069             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9070         }
9071         break;
9072     case ARM_CP_STATE_AA64:
9073         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9074         break;
9075     default:
9076         g_assert_not_reached();
9077     }
9078     /*
9079      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9080      * encodes a minimum access level for the register. We roll this
9081      * runtime check into our general permission check code, so check
9082      * here that the reginfo's specified permissions are strict enough
9083      * to encompass the generic architectural permission check.
9084      */
9085     if (r->state != ARM_CP_STATE_AA32) {
9086         CPAccessRights mask;
9087         switch (r->opc1) {
9088         case 0:
9089             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9090             mask = PL0U_R | PL1_RW;
9091             break;
9092         case 1: case 2:
9093             /* min_EL EL1 */
9094             mask = PL1_RW;
9095             break;
9096         case 3:
9097             /* min_EL EL0 */
9098             mask = PL0_RW;
9099             break;
9100         case 4:
9101         case 5:
9102             /* min_EL EL2 */
9103             mask = PL2_RW;
9104             break;
9105         case 6:
9106             /* min_EL EL3 */
9107             mask = PL3_RW;
9108             break;
9109         case 7:
9110             /* min_EL EL1, secure mode only (we don't check the latter) */
9111             mask = PL1_RW;
9112             break;
9113         default:
9114             /* broken reginfo with out-of-range opc1 */
9115             g_assert_not_reached();
9116         }
9117         /* assert our permissions are not too lax (stricter is fine) */
9118         assert((r->access & ~mask) == 0);
9119     }
9120 
9121     /*
9122      * Check that the register definition has enough info to handle
9123      * reads and writes if they are permitted.
9124      */
9125     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9126         if (r->access & PL3_R) {
9127             assert((r->fieldoffset ||
9128                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9129                    r->readfn);
9130         }
9131         if (r->access & PL3_W) {
9132             assert((r->fieldoffset ||
9133                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9134                    r->writefn);
9135         }
9136     }
9137 
9138     for (crm = crmmin; crm <= crmmax; crm++) {
9139         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9140             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9141                 for (state = ARM_CP_STATE_AA32;
9142                      state <= ARM_CP_STATE_AA64; state++) {
9143                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9144                         continue;
9145                     }
9146                     if (state == ARM_CP_STATE_AA32) {
9147                         /*
9148                          * Under AArch32 CP registers can be common
9149                          * (same for secure and non-secure world) or banked.
9150                          */
9151                         char *name;
9152 
9153                         switch (r->secure) {
9154                         case ARM_CP_SECSTATE_S:
9155                         case ARM_CP_SECSTATE_NS:
9156                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9157                                                    r->secure, crm, opc1, opc2,
9158                                                    r->name);
9159                             break;
9160                         case ARM_CP_SECSTATE_BOTH:
9161                             name = g_strdup_printf("%s_S", r->name);
9162                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9163                                                    ARM_CP_SECSTATE_S,
9164                                                    crm, opc1, opc2, name);
9165                             g_free(name);
9166                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9167                                                    ARM_CP_SECSTATE_NS,
9168                                                    crm, opc1, opc2, r->name);
9169                             break;
9170                         default:
9171                             g_assert_not_reached();
9172                         }
9173                     } else {
9174                         /*
9175                          * AArch64 registers get mapped to non-secure instance
9176                          * of AArch32
9177                          */
9178                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9179                                                ARM_CP_SECSTATE_NS,
9180                                                crm, opc1, opc2, r->name);
9181                     }
9182                 }
9183             }
9184         }
9185     }
9186 }
9187 
9188 /* Define a whole list of registers */
9189 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9190                                         void *opaque, size_t len)
9191 {
9192     size_t i;
9193     for (i = 0; i < len; ++i) {
9194         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9195     }
9196 }
9197 
9198 /*
9199  * Modify ARMCPRegInfo for access from userspace.
9200  *
9201  * This is a data driven modification directed by
9202  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9203  * user-space cannot alter any values and dynamic values pertaining to
9204  * execution state are hidden from user space view anyway.
9205  */
9206 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9207                                  const ARMCPRegUserSpaceInfo *mods,
9208                                  size_t mods_len)
9209 {
9210     for (size_t mi = 0; mi < mods_len; ++mi) {
9211         const ARMCPRegUserSpaceInfo *m = mods + mi;
9212         GPatternSpec *pat = NULL;
9213 
9214         if (m->is_glob) {
9215             pat = g_pattern_spec_new(m->name);
9216         }
9217         for (size_t ri = 0; ri < regs_len; ++ri) {
9218             ARMCPRegInfo *r = regs + ri;
9219 
9220             if (pat && g_pattern_match_string(pat, r->name)) {
9221                 r->type = ARM_CP_CONST;
9222                 r->access = PL0U_R;
9223                 r->resetvalue = 0;
9224                 /* continue */
9225             } else if (strcmp(r->name, m->name) == 0) {
9226                 r->type = ARM_CP_CONST;
9227                 r->access = PL0U_R;
9228                 r->resetvalue &= m->exported_bits;
9229                 r->resetvalue |= m->fixed_bits;
9230                 break;
9231             }
9232         }
9233         if (pat) {
9234             g_pattern_spec_free(pat);
9235         }
9236     }
9237 }
9238 
9239 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9240 {
9241     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9242 }
9243 
9244 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9245                          uint64_t value)
9246 {
9247     /* Helper coprocessor write function for write-ignore registers */
9248 }
9249 
9250 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9251 {
9252     /* Helper coprocessor write function for read-as-zero registers */
9253     return 0;
9254 }
9255 
9256 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9257 {
9258     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9259 }
9260 
9261 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9262 {
9263     /*
9264      * Return true if it is not valid for us to switch to
9265      * this CPU mode (ie all the UNPREDICTABLE cases in
9266      * the ARM ARM CPSRWriteByInstr pseudocode).
9267      */
9268 
9269     /* Changes to or from Hyp via MSR and CPS are illegal. */
9270     if (write_type == CPSRWriteByInstr &&
9271         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9272          mode == ARM_CPU_MODE_HYP)) {
9273         return 1;
9274     }
9275 
9276     switch (mode) {
9277     case ARM_CPU_MODE_USR:
9278         return 0;
9279     case ARM_CPU_MODE_SYS:
9280     case ARM_CPU_MODE_SVC:
9281     case ARM_CPU_MODE_ABT:
9282     case ARM_CPU_MODE_UND:
9283     case ARM_CPU_MODE_IRQ:
9284     case ARM_CPU_MODE_FIQ:
9285         /*
9286          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9287          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9288          */
9289         /*
9290          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9291          * and CPS are treated as illegal mode changes.
9292          */
9293         if (write_type == CPSRWriteByInstr &&
9294             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9295             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9296             return 1;
9297         }
9298         return 0;
9299     case ARM_CPU_MODE_HYP:
9300         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9301     case ARM_CPU_MODE_MON:
9302         return arm_current_el(env) < 3;
9303     default:
9304         return 1;
9305     }
9306 }
9307 
9308 uint32_t cpsr_read(CPUARMState *env)
9309 {
9310     int ZF;
9311     ZF = (env->ZF == 0);
9312     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9313         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9314         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9315         | ((env->condexec_bits & 0xfc) << 8)
9316         | (env->GE << 16) | (env->daif & CPSR_AIF);
9317 }
9318 
9319 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9320                 CPSRWriteType write_type)
9321 {
9322     uint32_t changed_daif;
9323     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9324         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9325 
9326     if (mask & CPSR_NZCV) {
9327         env->ZF = (~val) & CPSR_Z;
9328         env->NF = val;
9329         env->CF = (val >> 29) & 1;
9330         env->VF = (val << 3) & 0x80000000;
9331     }
9332     if (mask & CPSR_Q) {
9333         env->QF = ((val & CPSR_Q) != 0);
9334     }
9335     if (mask & CPSR_T) {
9336         env->thumb = ((val & CPSR_T) != 0);
9337     }
9338     if (mask & CPSR_IT_0_1) {
9339         env->condexec_bits &= ~3;
9340         env->condexec_bits |= (val >> 25) & 3;
9341     }
9342     if (mask & CPSR_IT_2_7) {
9343         env->condexec_bits &= 3;
9344         env->condexec_bits |= (val >> 8) & 0xfc;
9345     }
9346     if (mask & CPSR_GE) {
9347         env->GE = (val >> 16) & 0xf;
9348     }
9349 
9350     /*
9351      * In a V7 implementation that includes the security extensions but does
9352      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9353      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9354      * bits respectively.
9355      *
9356      * In a V8 implementation, it is permitted for privileged software to
9357      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9358      */
9359     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9360         arm_feature(env, ARM_FEATURE_EL3) &&
9361         !arm_feature(env, ARM_FEATURE_EL2) &&
9362         !arm_is_secure(env)) {
9363 
9364         changed_daif = (env->daif ^ val) & mask;
9365 
9366         if (changed_daif & CPSR_A) {
9367             /*
9368              * Check to see if we are allowed to change the masking of async
9369              * abort exceptions from a non-secure state.
9370              */
9371             if (!(env->cp15.scr_el3 & SCR_AW)) {
9372                 qemu_log_mask(LOG_GUEST_ERROR,
9373                               "Ignoring attempt to switch CPSR_A flag from "
9374                               "non-secure world with SCR.AW bit clear\n");
9375                 mask &= ~CPSR_A;
9376             }
9377         }
9378 
9379         if (changed_daif & CPSR_F) {
9380             /*
9381              * Check to see if we are allowed to change the masking of FIQ
9382              * exceptions from a non-secure state.
9383              */
9384             if (!(env->cp15.scr_el3 & SCR_FW)) {
9385                 qemu_log_mask(LOG_GUEST_ERROR,
9386                               "Ignoring attempt to switch CPSR_F flag from "
9387                               "non-secure world with SCR.FW bit clear\n");
9388                 mask &= ~CPSR_F;
9389             }
9390 
9391             /*
9392              * Check whether non-maskable FIQ (NMFI) support is enabled.
9393              * If this bit is set software is not allowed to mask
9394              * FIQs, but is allowed to set CPSR_F to 0.
9395              */
9396             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9397                 (val & CPSR_F)) {
9398                 qemu_log_mask(LOG_GUEST_ERROR,
9399                               "Ignoring attempt to enable CPSR_F flag "
9400                               "(non-maskable FIQ [NMFI] support enabled)\n");
9401                 mask &= ~CPSR_F;
9402             }
9403         }
9404     }
9405 
9406     env->daif &= ~(CPSR_AIF & mask);
9407     env->daif |= val & CPSR_AIF & mask;
9408 
9409     if (write_type != CPSRWriteRaw &&
9410         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9411         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9412             /*
9413              * Note that we can only get here in USR mode if this is a
9414              * gdb stub write; for this case we follow the architectural
9415              * behaviour for guest writes in USR mode of ignoring an attempt
9416              * to switch mode. (Those are caught by translate.c for writes
9417              * triggered by guest instructions.)
9418              */
9419             mask &= ~CPSR_M;
9420         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9421             /*
9422              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9423              * v7, and has defined behaviour in v8:
9424              *  + leave CPSR.M untouched
9425              *  + allow changes to the other CPSR fields
9426              *  + set PSTATE.IL
9427              * For user changes via the GDB stub, we don't set PSTATE.IL,
9428              * as this would be unnecessarily harsh for a user error.
9429              */
9430             mask &= ~CPSR_M;
9431             if (write_type != CPSRWriteByGDBStub &&
9432                 arm_feature(env, ARM_FEATURE_V8)) {
9433                 mask |= CPSR_IL;
9434                 val |= CPSR_IL;
9435             }
9436             qemu_log_mask(LOG_GUEST_ERROR,
9437                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9438                           aarch32_mode_name(env->uncached_cpsr),
9439                           aarch32_mode_name(val));
9440         } else {
9441             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9442                           write_type == CPSRWriteExceptionReturn ?
9443                           "Exception return from AArch32" :
9444                           "AArch32 mode switch from",
9445                           aarch32_mode_name(env->uncached_cpsr),
9446                           aarch32_mode_name(val), env->regs[15]);
9447             switch_mode(env, val & CPSR_M);
9448         }
9449     }
9450     mask &= ~CACHED_CPSR_BITS;
9451     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9452     if (tcg_enabled() && rebuild_hflags) {
9453         arm_rebuild_hflags(env);
9454     }
9455 }
9456 
9457 #ifdef CONFIG_USER_ONLY
9458 
9459 static void switch_mode(CPUARMState *env, int mode)
9460 {
9461     ARMCPU *cpu = env_archcpu(env);
9462 
9463     if (mode != ARM_CPU_MODE_USR) {
9464         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9465     }
9466 }
9467 
9468 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9469                                  uint32_t cur_el, bool secure)
9470 {
9471     return 1;
9472 }
9473 
9474 void aarch64_sync_64_to_32(CPUARMState *env)
9475 {
9476     g_assert_not_reached();
9477 }
9478 
9479 #else
9480 
9481 static void switch_mode(CPUARMState *env, int mode)
9482 {
9483     int old_mode;
9484     int i;
9485 
9486     old_mode = env->uncached_cpsr & CPSR_M;
9487     if (mode == old_mode) {
9488         return;
9489     }
9490 
9491     if (old_mode == ARM_CPU_MODE_FIQ) {
9492         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9493         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9494     } else if (mode == ARM_CPU_MODE_FIQ) {
9495         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9496         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9497     }
9498 
9499     i = bank_number(old_mode);
9500     env->banked_r13[i] = env->regs[13];
9501     env->banked_spsr[i] = env->spsr;
9502 
9503     i = bank_number(mode);
9504     env->regs[13] = env->banked_r13[i];
9505     env->spsr = env->banked_spsr[i];
9506 
9507     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9508     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9509 }
9510 
9511 /*
9512  * Physical Interrupt Target EL Lookup Table
9513  *
9514  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9515  *
9516  * The below multi-dimensional table is used for looking up the target
9517  * exception level given numerous condition criteria.  Specifically, the
9518  * target EL is based on SCR and HCR routing controls as well as the
9519  * currently executing EL and secure state.
9520  *
9521  *    Dimensions:
9522  *    target_el_table[2][2][2][2][2][4]
9523  *                    |  |  |  |  |  +--- Current EL
9524  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9525  *                    |  |  |  +--------- HCR mask override
9526  *                    |  |  +------------ SCR exec state control
9527  *                    |  +--------------- SCR mask override
9528  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9529  *
9530  *    The table values are as such:
9531  *    0-3 = EL0-EL3
9532  *     -1 = Cannot occur
9533  *
9534  * The ARM ARM target EL table includes entries indicating that an "exception
9535  * is not taken".  The two cases where this is applicable are:
9536  *    1) An exception is taken from EL3 but the SCR does not have the exception
9537  *    routed to EL3.
9538  *    2) An exception is taken from EL2 but the HCR does not have the exception
9539  *    routed to EL2.
9540  * In these two cases, the below table contain a target of EL1.  This value is
9541  * returned as it is expected that the consumer of the table data will check
9542  * for "target EL >= current EL" to ensure the exception is not taken.
9543  *
9544  *            SCR     HCR
9545  *         64  EA     AMO                 From
9546  *        BIT IRQ     IMO      Non-secure         Secure
9547  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9548  */
9549 static const int8_t target_el_table[2][2][2][2][2][4] = {
9550     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9551        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9552       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9553        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9554      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9555        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9556       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9557        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9558     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9559        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9560       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9561        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9562      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9563        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9564       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9565        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9566 };
9567 
9568 /*
9569  * Determine the target EL for physical exceptions
9570  */
9571 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9572                                  uint32_t cur_el, bool secure)
9573 {
9574     CPUARMState *env = cpu_env(cs);
9575     bool rw;
9576     bool scr;
9577     bool hcr;
9578     int target_el;
9579     /* Is the highest EL AArch64? */
9580     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9581     uint64_t hcr_el2;
9582 
9583     if (arm_feature(env, ARM_FEATURE_EL3)) {
9584         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9585     } else {
9586         /*
9587          * Either EL2 is the highest EL (and so the EL2 register width
9588          * is given by is64); or there is no EL2 or EL3, in which case
9589          * the value of 'rw' does not affect the table lookup anyway.
9590          */
9591         rw = is64;
9592     }
9593 
9594     hcr_el2 = arm_hcr_el2_eff(env);
9595     switch (excp_idx) {
9596     case EXCP_IRQ:
9597     case EXCP_NMI:
9598         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9599         hcr = hcr_el2 & HCR_IMO;
9600         break;
9601     case EXCP_FIQ:
9602         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9603         hcr = hcr_el2 & HCR_FMO;
9604         break;
9605     default:
9606         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9607         hcr = hcr_el2 & HCR_AMO;
9608         break;
9609     };
9610 
9611     /*
9612      * For these purposes, TGE and AMO/IMO/FMO both force the
9613      * interrupt to EL2.  Fold TGE into the bit extracted above.
9614      */
9615     hcr |= (hcr_el2 & HCR_TGE) != 0;
9616 
9617     /* Perform a table-lookup for the target EL given the current state */
9618     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9619 
9620     assert(target_el > 0);
9621 
9622     return target_el;
9623 }
9624 
9625 void arm_log_exception(CPUState *cs)
9626 {
9627     int idx = cs->exception_index;
9628 
9629     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9630         const char *exc = NULL;
9631         static const char * const excnames[] = {
9632             [EXCP_UDEF] = "Undefined Instruction",
9633             [EXCP_SWI] = "SVC",
9634             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9635             [EXCP_DATA_ABORT] = "Data Abort",
9636             [EXCP_IRQ] = "IRQ",
9637             [EXCP_FIQ] = "FIQ",
9638             [EXCP_BKPT] = "Breakpoint",
9639             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9640             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9641             [EXCP_HVC] = "Hypervisor Call",
9642             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9643             [EXCP_SMC] = "Secure Monitor Call",
9644             [EXCP_VIRQ] = "Virtual IRQ",
9645             [EXCP_VFIQ] = "Virtual FIQ",
9646             [EXCP_SEMIHOST] = "Semihosting call",
9647             [EXCP_NOCP] = "v7M NOCP UsageFault",
9648             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9649             [EXCP_STKOF] = "v8M STKOF UsageFault",
9650             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9651             [EXCP_LSERR] = "v8M LSERR UsageFault",
9652             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9653             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9654             [EXCP_VSERR] = "Virtual SERR",
9655             [EXCP_GPC] = "Granule Protection Check",
9656             [EXCP_NMI] = "NMI",
9657             [EXCP_VINMI] = "Virtual IRQ NMI",
9658             [EXCP_VFNMI] = "Virtual FIQ NMI",
9659         };
9660 
9661         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9662             exc = excnames[idx];
9663         }
9664         if (!exc) {
9665             exc = "unknown";
9666         }
9667         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9668                       idx, exc, cs->cpu_index);
9669     }
9670 }
9671 
9672 /*
9673  * Function used to synchronize QEMU's AArch64 register set with AArch32
9674  * register set.  This is necessary when switching between AArch32 and AArch64
9675  * execution state.
9676  */
9677 void aarch64_sync_32_to_64(CPUARMState *env)
9678 {
9679     int i;
9680     uint32_t mode = env->uncached_cpsr & CPSR_M;
9681 
9682     /* We can blanket copy R[0:7] to X[0:7] */
9683     for (i = 0; i < 8; i++) {
9684         env->xregs[i] = env->regs[i];
9685     }
9686 
9687     /*
9688      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9689      * Otherwise, they come from the banked user regs.
9690      */
9691     if (mode == ARM_CPU_MODE_FIQ) {
9692         for (i = 8; i < 13; i++) {
9693             env->xregs[i] = env->usr_regs[i - 8];
9694         }
9695     } else {
9696         for (i = 8; i < 13; i++) {
9697             env->xregs[i] = env->regs[i];
9698         }
9699     }
9700 
9701     /*
9702      * Registers x13-x23 are the various mode SP and FP registers. Registers
9703      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9704      * from the mode banked register.
9705      */
9706     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9707         env->xregs[13] = env->regs[13];
9708         env->xregs[14] = env->regs[14];
9709     } else {
9710         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9711         /* HYP is an exception in that it is copied from r14 */
9712         if (mode == ARM_CPU_MODE_HYP) {
9713             env->xregs[14] = env->regs[14];
9714         } else {
9715             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9716         }
9717     }
9718 
9719     if (mode == ARM_CPU_MODE_HYP) {
9720         env->xregs[15] = env->regs[13];
9721     } else {
9722         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9723     }
9724 
9725     if (mode == ARM_CPU_MODE_IRQ) {
9726         env->xregs[16] = env->regs[14];
9727         env->xregs[17] = env->regs[13];
9728     } else {
9729         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9730         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9731     }
9732 
9733     if (mode == ARM_CPU_MODE_SVC) {
9734         env->xregs[18] = env->regs[14];
9735         env->xregs[19] = env->regs[13];
9736     } else {
9737         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9738         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9739     }
9740 
9741     if (mode == ARM_CPU_MODE_ABT) {
9742         env->xregs[20] = env->regs[14];
9743         env->xregs[21] = env->regs[13];
9744     } else {
9745         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9746         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9747     }
9748 
9749     if (mode == ARM_CPU_MODE_UND) {
9750         env->xregs[22] = env->regs[14];
9751         env->xregs[23] = env->regs[13];
9752     } else {
9753         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9754         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9755     }
9756 
9757     /*
9758      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9759      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9760      * FIQ bank for r8-r14.
9761      */
9762     if (mode == ARM_CPU_MODE_FIQ) {
9763         for (i = 24; i < 31; i++) {
9764             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9765         }
9766     } else {
9767         for (i = 24; i < 29; i++) {
9768             env->xregs[i] = env->fiq_regs[i - 24];
9769         }
9770         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9771         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9772     }
9773 
9774     env->pc = env->regs[15];
9775 }
9776 
9777 /*
9778  * Function used to synchronize QEMU's AArch32 register set with AArch64
9779  * register set.  This is necessary when switching between AArch32 and AArch64
9780  * execution state.
9781  */
9782 void aarch64_sync_64_to_32(CPUARMState *env)
9783 {
9784     int i;
9785     uint32_t mode = env->uncached_cpsr & CPSR_M;
9786 
9787     /* We can blanket copy X[0:7] to R[0:7] */
9788     for (i = 0; i < 8; i++) {
9789         env->regs[i] = env->xregs[i];
9790     }
9791 
9792     /*
9793      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9794      * Otherwise, we copy x8-x12 into the banked user regs.
9795      */
9796     if (mode == ARM_CPU_MODE_FIQ) {
9797         for (i = 8; i < 13; i++) {
9798             env->usr_regs[i - 8] = env->xregs[i];
9799         }
9800     } else {
9801         for (i = 8; i < 13; i++) {
9802             env->regs[i] = env->xregs[i];
9803         }
9804     }
9805 
9806     /*
9807      * Registers r13 & r14 depend on the current mode.
9808      * If we are in a given mode, we copy the corresponding x registers to r13
9809      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9810      * for the mode.
9811      */
9812     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9813         env->regs[13] = env->xregs[13];
9814         env->regs[14] = env->xregs[14];
9815     } else {
9816         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9817 
9818         /*
9819          * HYP is an exception in that it does not have its own banked r14 but
9820          * shares the USR r14
9821          */
9822         if (mode == ARM_CPU_MODE_HYP) {
9823             env->regs[14] = env->xregs[14];
9824         } else {
9825             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9826         }
9827     }
9828 
9829     if (mode == ARM_CPU_MODE_HYP) {
9830         env->regs[13] = env->xregs[15];
9831     } else {
9832         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9833     }
9834 
9835     if (mode == ARM_CPU_MODE_IRQ) {
9836         env->regs[14] = env->xregs[16];
9837         env->regs[13] = env->xregs[17];
9838     } else {
9839         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9840         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9841     }
9842 
9843     if (mode == ARM_CPU_MODE_SVC) {
9844         env->regs[14] = env->xregs[18];
9845         env->regs[13] = env->xregs[19];
9846     } else {
9847         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9848         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9849     }
9850 
9851     if (mode == ARM_CPU_MODE_ABT) {
9852         env->regs[14] = env->xregs[20];
9853         env->regs[13] = env->xregs[21];
9854     } else {
9855         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9856         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9857     }
9858 
9859     if (mode == ARM_CPU_MODE_UND) {
9860         env->regs[14] = env->xregs[22];
9861         env->regs[13] = env->xregs[23];
9862     } else {
9863         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9864         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9865     }
9866 
9867     /*
9868      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9869      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9870      * FIQ bank for r8-r14.
9871      */
9872     if (mode == ARM_CPU_MODE_FIQ) {
9873         for (i = 24; i < 31; i++) {
9874             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9875         }
9876     } else {
9877         for (i = 24; i < 29; i++) {
9878             env->fiq_regs[i - 24] = env->xregs[i];
9879         }
9880         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9881         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9882     }
9883 
9884     env->regs[15] = env->pc;
9885 }
9886 
9887 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9888                                    uint32_t mask, uint32_t offset,
9889                                    uint32_t newpc)
9890 {
9891     int new_el;
9892 
9893     /* Change the CPU state so as to actually take the exception. */
9894     switch_mode(env, new_mode);
9895 
9896     /*
9897      * For exceptions taken to AArch32 we must clear the SS bit in both
9898      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9899      */
9900     env->pstate &= ~PSTATE_SS;
9901     env->spsr = cpsr_read(env);
9902     /* Clear IT bits.  */
9903     env->condexec_bits = 0;
9904     /* Switch to the new mode, and to the correct instruction set.  */
9905     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9906 
9907     /* This must be after mode switching. */
9908     new_el = arm_current_el(env);
9909 
9910     /* Set new mode endianness */
9911     env->uncached_cpsr &= ~CPSR_E;
9912     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9913         env->uncached_cpsr |= CPSR_E;
9914     }
9915     /* J and IL must always be cleared for exception entry */
9916     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9917     env->daif |= mask;
9918 
9919     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9920         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9921             env->uncached_cpsr |= CPSR_SSBS;
9922         } else {
9923             env->uncached_cpsr &= ~CPSR_SSBS;
9924         }
9925     }
9926 
9927     if (new_mode == ARM_CPU_MODE_HYP) {
9928         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9929         env->elr_el[2] = env->regs[15];
9930     } else {
9931         /* CPSR.PAN is normally preserved preserved unless...  */
9932         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9933             switch (new_el) {
9934             case 3:
9935                 if (!arm_is_secure_below_el3(env)) {
9936                     /* ... the target is EL3, from non-secure state.  */
9937                     env->uncached_cpsr &= ~CPSR_PAN;
9938                     break;
9939                 }
9940                 /* ... the target is EL3, from secure state ... */
9941                 /* fall through */
9942             case 1:
9943                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9944                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9945                     env->uncached_cpsr |= CPSR_PAN;
9946                 }
9947                 break;
9948             }
9949         }
9950         /*
9951          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9952          * and we should just guard the thumb mode on V4
9953          */
9954         if (arm_feature(env, ARM_FEATURE_V4T)) {
9955             env->thumb =
9956                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9957         }
9958         env->regs[14] = env->regs[15] + offset;
9959     }
9960     env->regs[15] = newpc;
9961 
9962     if (tcg_enabled()) {
9963         arm_rebuild_hflags(env);
9964     }
9965 }
9966 
9967 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9968 {
9969     /*
9970      * Handle exception entry to Hyp mode; this is sufficiently
9971      * different to entry to other AArch32 modes that we handle it
9972      * separately here.
9973      *
9974      * The vector table entry used is always the 0x14 Hyp mode entry point,
9975      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9976      * The offset applied to the preferred return address is always zero
9977      * (see DDI0487C.a section G1.12.3).
9978      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9979      */
9980     uint32_t addr, mask;
9981     ARMCPU *cpu = ARM_CPU(cs);
9982     CPUARMState *env = &cpu->env;
9983 
9984     switch (cs->exception_index) {
9985     case EXCP_UDEF:
9986         addr = 0x04;
9987         break;
9988     case EXCP_SWI:
9989         addr = 0x08;
9990         break;
9991     case EXCP_BKPT:
9992         /* Fall through to prefetch abort.  */
9993     case EXCP_PREFETCH_ABORT:
9994         env->cp15.ifar_s = env->exception.vaddress;
9995         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9996                       (uint32_t)env->exception.vaddress);
9997         addr = 0x0c;
9998         break;
9999     case EXCP_DATA_ABORT:
10000         env->cp15.dfar_s = env->exception.vaddress;
10001         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10002                       (uint32_t)env->exception.vaddress);
10003         addr = 0x10;
10004         break;
10005     case EXCP_IRQ:
10006         addr = 0x18;
10007         break;
10008     case EXCP_FIQ:
10009         addr = 0x1c;
10010         break;
10011     case EXCP_HVC:
10012         addr = 0x08;
10013         break;
10014     case EXCP_HYP_TRAP:
10015         addr = 0x14;
10016         break;
10017     default:
10018         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10019     }
10020 
10021     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10022         if (!arm_feature(env, ARM_FEATURE_V8)) {
10023             /*
10024              * QEMU syndrome values are v8-style. v7 has the IL bit
10025              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10026              * If this is a v7 CPU, squash the IL bit in those cases.
10027              */
10028             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10029                 (cs->exception_index == EXCP_DATA_ABORT &&
10030                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10031                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10032                 env->exception.syndrome &= ~ARM_EL_IL;
10033             }
10034         }
10035         env->cp15.esr_el[2] = env->exception.syndrome;
10036     }
10037 
10038     if (arm_current_el(env) != 2 && addr < 0x14) {
10039         addr = 0x14;
10040     }
10041 
10042     mask = 0;
10043     if (!(env->cp15.scr_el3 & SCR_EA)) {
10044         mask |= CPSR_A;
10045     }
10046     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10047         mask |= CPSR_I;
10048     }
10049     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10050         mask |= CPSR_F;
10051     }
10052 
10053     addr += env->cp15.hvbar;
10054 
10055     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10056 }
10057 
10058 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10059 {
10060     ARMCPU *cpu = ARM_CPU(cs);
10061     CPUARMState *env = &cpu->env;
10062     uint32_t addr;
10063     uint32_t mask;
10064     int new_mode;
10065     uint32_t offset;
10066     uint32_t moe;
10067 
10068     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10069     switch (syn_get_ec(env->exception.syndrome)) {
10070     case EC_BREAKPOINT:
10071     case EC_BREAKPOINT_SAME_EL:
10072         moe = 1;
10073         break;
10074     case EC_WATCHPOINT:
10075     case EC_WATCHPOINT_SAME_EL:
10076         moe = 10;
10077         break;
10078     case EC_AA32_BKPT:
10079         moe = 3;
10080         break;
10081     case EC_VECTORCATCH:
10082         moe = 5;
10083         break;
10084     default:
10085         moe = 0;
10086         break;
10087     }
10088 
10089     if (moe) {
10090         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10091     }
10092 
10093     if (env->exception.target_el == 2) {
10094         /* Debug exceptions are reported differently on AArch32 */
10095         switch (syn_get_ec(env->exception.syndrome)) {
10096         case EC_BREAKPOINT:
10097         case EC_BREAKPOINT_SAME_EL:
10098         case EC_AA32_BKPT:
10099         case EC_VECTORCATCH:
10100             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10101                                                      0, 0, 0x22);
10102             break;
10103         case EC_WATCHPOINT:
10104             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10105                                                  EC_DATAABORT);
10106             break;
10107         case EC_WATCHPOINT_SAME_EL:
10108             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10109                                                  EC_DATAABORT_SAME_EL);
10110             break;
10111         }
10112         arm_cpu_do_interrupt_aarch32_hyp(cs);
10113         return;
10114     }
10115 
10116     switch (cs->exception_index) {
10117     case EXCP_UDEF:
10118         new_mode = ARM_CPU_MODE_UND;
10119         addr = 0x04;
10120         mask = CPSR_I;
10121         if (env->thumb) {
10122             offset = 2;
10123         } else {
10124             offset = 4;
10125         }
10126         break;
10127     case EXCP_SWI:
10128         new_mode = ARM_CPU_MODE_SVC;
10129         addr = 0x08;
10130         mask = CPSR_I;
10131         /* The PC already points to the next instruction.  */
10132         offset = 0;
10133         break;
10134     case EXCP_BKPT:
10135         /* Fall through to prefetch abort.  */
10136     case EXCP_PREFETCH_ABORT:
10137         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10138         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10139         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10140                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10141         new_mode = ARM_CPU_MODE_ABT;
10142         addr = 0x0c;
10143         mask = CPSR_A | CPSR_I;
10144         offset = 4;
10145         break;
10146     case EXCP_DATA_ABORT:
10147         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10148         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10149         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10150                       env->exception.fsr,
10151                       (uint32_t)env->exception.vaddress);
10152         new_mode = ARM_CPU_MODE_ABT;
10153         addr = 0x10;
10154         mask = CPSR_A | CPSR_I;
10155         offset = 8;
10156         break;
10157     case EXCP_IRQ:
10158         new_mode = ARM_CPU_MODE_IRQ;
10159         addr = 0x18;
10160         /* Disable IRQ and imprecise data aborts.  */
10161         mask = CPSR_A | CPSR_I;
10162         offset = 4;
10163         if (env->cp15.scr_el3 & SCR_IRQ) {
10164             /* IRQ routed to monitor mode */
10165             new_mode = ARM_CPU_MODE_MON;
10166             mask |= CPSR_F;
10167         }
10168         break;
10169     case EXCP_FIQ:
10170         new_mode = ARM_CPU_MODE_FIQ;
10171         addr = 0x1c;
10172         /* Disable FIQ, IRQ and imprecise data aborts.  */
10173         mask = CPSR_A | CPSR_I | CPSR_F;
10174         if (env->cp15.scr_el3 & SCR_FIQ) {
10175             /* FIQ routed to monitor mode */
10176             new_mode = ARM_CPU_MODE_MON;
10177         }
10178         offset = 4;
10179         break;
10180     case EXCP_VIRQ:
10181         new_mode = ARM_CPU_MODE_IRQ;
10182         addr = 0x18;
10183         /* Disable IRQ and imprecise data aborts.  */
10184         mask = CPSR_A | CPSR_I;
10185         offset = 4;
10186         break;
10187     case EXCP_VFIQ:
10188         new_mode = ARM_CPU_MODE_FIQ;
10189         addr = 0x1c;
10190         /* Disable FIQ, IRQ and imprecise data aborts.  */
10191         mask = CPSR_A | CPSR_I | CPSR_F;
10192         offset = 4;
10193         break;
10194     case EXCP_VSERR:
10195         {
10196             /*
10197              * Note that this is reported as a data abort, but the DFAR
10198              * has an UNKNOWN value.  Construct the SError syndrome from
10199              * AET and ExT fields.
10200              */
10201             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10202 
10203             if (extended_addresses_enabled(env)) {
10204                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10205             } else {
10206                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10207             }
10208             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10209             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10210             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10211                           env->exception.fsr);
10212 
10213             new_mode = ARM_CPU_MODE_ABT;
10214             addr = 0x10;
10215             mask = CPSR_A | CPSR_I;
10216             offset = 8;
10217         }
10218         break;
10219     case EXCP_SMC:
10220         new_mode = ARM_CPU_MODE_MON;
10221         addr = 0x08;
10222         mask = CPSR_A | CPSR_I | CPSR_F;
10223         offset = 0;
10224         break;
10225     default:
10226         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10227         return; /* Never happens.  Keep compiler happy.  */
10228     }
10229 
10230     if (new_mode == ARM_CPU_MODE_MON) {
10231         addr += env->cp15.mvbar;
10232     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10233         /* High vectors. When enabled, base address cannot be remapped. */
10234         addr += 0xffff0000;
10235     } else {
10236         /*
10237          * ARM v7 architectures provide a vector base address register to remap
10238          * the interrupt vector table.
10239          * This register is only followed in non-monitor mode, and is banked.
10240          * Note: only bits 31:5 are valid.
10241          */
10242         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10243     }
10244 
10245     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10246         env->cp15.scr_el3 &= ~SCR_NS;
10247     }
10248 
10249     take_aarch32_exception(env, new_mode, mask, offset, addr);
10250 }
10251 
10252 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10253 {
10254     /*
10255      * Return the register number of the AArch64 view of the AArch32
10256      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10257      * be that of the AArch32 mode the exception came from.
10258      */
10259     int mode = env->uncached_cpsr & CPSR_M;
10260 
10261     switch (aarch32_reg) {
10262     case 0 ... 7:
10263         return aarch32_reg;
10264     case 8 ... 12:
10265         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10266     case 13:
10267         switch (mode) {
10268         case ARM_CPU_MODE_USR:
10269         case ARM_CPU_MODE_SYS:
10270             return 13;
10271         case ARM_CPU_MODE_HYP:
10272             return 15;
10273         case ARM_CPU_MODE_IRQ:
10274             return 17;
10275         case ARM_CPU_MODE_SVC:
10276             return 19;
10277         case ARM_CPU_MODE_ABT:
10278             return 21;
10279         case ARM_CPU_MODE_UND:
10280             return 23;
10281         case ARM_CPU_MODE_FIQ:
10282             return 29;
10283         default:
10284             g_assert_not_reached();
10285         }
10286     case 14:
10287         switch (mode) {
10288         case ARM_CPU_MODE_USR:
10289         case ARM_CPU_MODE_SYS:
10290         case ARM_CPU_MODE_HYP:
10291             return 14;
10292         case ARM_CPU_MODE_IRQ:
10293             return 16;
10294         case ARM_CPU_MODE_SVC:
10295             return 18;
10296         case ARM_CPU_MODE_ABT:
10297             return 20;
10298         case ARM_CPU_MODE_UND:
10299             return 22;
10300         case ARM_CPU_MODE_FIQ:
10301             return 30;
10302         default:
10303             g_assert_not_reached();
10304         }
10305     case 15:
10306         return 31;
10307     default:
10308         g_assert_not_reached();
10309     }
10310 }
10311 
10312 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10313 {
10314     uint32_t ret = cpsr_read(env);
10315 
10316     /* Move DIT to the correct location for SPSR_ELx */
10317     if (ret & CPSR_DIT) {
10318         ret &= ~CPSR_DIT;
10319         ret |= PSTATE_DIT;
10320     }
10321     /* Merge PSTATE.SS into SPSR_ELx */
10322     ret |= env->pstate & PSTATE_SS;
10323 
10324     return ret;
10325 }
10326 
10327 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10328 {
10329     /* Return true if this syndrome value is a synchronous external abort */
10330     switch (syn_get_ec(syndrome)) {
10331     case EC_INSNABORT:
10332     case EC_INSNABORT_SAME_EL:
10333     case EC_DATAABORT:
10334     case EC_DATAABORT_SAME_EL:
10335         /* Look at fault status code for all the synchronous ext abort cases */
10336         switch (syndrome & 0x3f) {
10337         case 0x10:
10338         case 0x13:
10339         case 0x14:
10340         case 0x15:
10341         case 0x16:
10342         case 0x17:
10343             return true;
10344         default:
10345             return false;
10346         }
10347     default:
10348         return false;
10349     }
10350 }
10351 
10352 /* Handle exception entry to a target EL which is using AArch64 */
10353 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10354 {
10355     ARMCPU *cpu = ARM_CPU(cs);
10356     CPUARMState *env = &cpu->env;
10357     unsigned int new_el = env->exception.target_el;
10358     target_ulong addr = env->cp15.vbar_el[new_el];
10359     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10360     unsigned int old_mode;
10361     unsigned int cur_el = arm_current_el(env);
10362     int rt;
10363 
10364     if (tcg_enabled()) {
10365         /*
10366          * Note that new_el can never be 0.  If cur_el is 0, then
10367          * el0_a64 is is_a64(), else el0_a64 is ignored.
10368          */
10369         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10370     }
10371 
10372     if (cur_el < new_el) {
10373         /*
10374          * Entry vector offset depends on whether the implemented EL
10375          * immediately lower than the target level is using AArch32 or AArch64
10376          */
10377         bool is_aa64;
10378         uint64_t hcr;
10379 
10380         switch (new_el) {
10381         case 3:
10382             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10383             break;
10384         case 2:
10385             hcr = arm_hcr_el2_eff(env);
10386             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10387                 is_aa64 = (hcr & HCR_RW) != 0;
10388                 break;
10389             }
10390             /* fall through */
10391         case 1:
10392             is_aa64 = is_a64(env);
10393             break;
10394         default:
10395             g_assert_not_reached();
10396         }
10397 
10398         if (is_aa64) {
10399             addr += 0x400;
10400         } else {
10401             addr += 0x600;
10402         }
10403     } else if (pstate_read(env) & PSTATE_SP) {
10404         addr += 0x200;
10405     }
10406 
10407     switch (cs->exception_index) {
10408     case EXCP_GPC:
10409         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10410                       env->cp15.mfar_el3);
10411         /* fall through */
10412     case EXCP_PREFETCH_ABORT:
10413     case EXCP_DATA_ABORT:
10414         /*
10415          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10416          * to be taken to the SError vector entrypoint.
10417          */
10418         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10419             syndrome_is_sync_extabt(env->exception.syndrome)) {
10420             addr += 0x180;
10421         }
10422         env->cp15.far_el[new_el] = env->exception.vaddress;
10423         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10424                       env->cp15.far_el[new_el]);
10425         /* fall through */
10426     case EXCP_BKPT:
10427     case EXCP_UDEF:
10428     case EXCP_SWI:
10429     case EXCP_HVC:
10430     case EXCP_HYP_TRAP:
10431     case EXCP_SMC:
10432         switch (syn_get_ec(env->exception.syndrome)) {
10433         case EC_ADVSIMDFPACCESSTRAP:
10434             /*
10435              * QEMU internal FP/SIMD syndromes from AArch32 include the
10436              * TA and coproc fields which are only exposed if the exception
10437              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10438              * AArch64 format syndrome.
10439              */
10440             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10441             break;
10442         case EC_CP14RTTRAP:
10443         case EC_CP15RTTRAP:
10444         case EC_CP14DTTRAP:
10445             /*
10446              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10447              * the raw register field from the insn; when taking this to
10448              * AArch64 we must convert it to the AArch64 view of the register
10449              * number. Notice that we read a 4-bit AArch32 register number and
10450              * write back a 5-bit AArch64 one.
10451              */
10452             rt = extract32(env->exception.syndrome, 5, 4);
10453             rt = aarch64_regnum(env, rt);
10454             env->exception.syndrome = deposit32(env->exception.syndrome,
10455                                                 5, 5, rt);
10456             break;
10457         case EC_CP15RRTTRAP:
10458         case EC_CP14RRTTRAP:
10459             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10460             rt = extract32(env->exception.syndrome, 5, 4);
10461             rt = aarch64_regnum(env, rt);
10462             env->exception.syndrome = deposit32(env->exception.syndrome,
10463                                                 5, 5, rt);
10464             rt = extract32(env->exception.syndrome, 10, 4);
10465             rt = aarch64_regnum(env, rt);
10466             env->exception.syndrome = deposit32(env->exception.syndrome,
10467                                                 10, 5, rt);
10468             break;
10469         }
10470         env->cp15.esr_el[new_el] = env->exception.syndrome;
10471         break;
10472     case EXCP_IRQ:
10473     case EXCP_VIRQ:
10474     case EXCP_NMI:
10475     case EXCP_VINMI:
10476         addr += 0x80;
10477         break;
10478     case EXCP_FIQ:
10479     case EXCP_VFIQ:
10480     case EXCP_VFNMI:
10481         addr += 0x100;
10482         break;
10483     case EXCP_VSERR:
10484         addr += 0x180;
10485         /* Construct the SError syndrome from IDS and ISS fields. */
10486         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10487         env->cp15.esr_el[new_el] = env->exception.syndrome;
10488         break;
10489     default:
10490         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10491     }
10492 
10493     if (is_a64(env)) {
10494         old_mode = pstate_read(env);
10495         aarch64_save_sp(env, arm_current_el(env));
10496         env->elr_el[new_el] = env->pc;
10497 
10498         if (cur_el == 1 && new_el == 1) {
10499             uint64_t hcr = arm_hcr_el2_eff(env);
10500             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10501                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10502                 /*
10503                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10504                  * by setting M[3:2] to 0b10.
10505                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10506                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10507                  */
10508                 old_mode = deposit32(old_mode, 2, 2, 2);
10509             }
10510         }
10511     } else {
10512         old_mode = cpsr_read_for_spsr_elx(env);
10513         env->elr_el[new_el] = env->regs[15];
10514 
10515         aarch64_sync_32_to_64(env);
10516 
10517         env->condexec_bits = 0;
10518     }
10519     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10520 
10521     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10522     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10523                   env->elr_el[new_el]);
10524 
10525     if (cpu_isar_feature(aa64_pan, cpu)) {
10526         /* The value of PSTATE.PAN is normally preserved, except when ... */
10527         new_mode |= old_mode & PSTATE_PAN;
10528         switch (new_el) {
10529         case 2:
10530             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10531             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10532                 != (HCR_E2H | HCR_TGE)) {
10533                 break;
10534             }
10535             /* fall through */
10536         case 1:
10537             /* ... the target is EL1 ... */
10538             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10539             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10540                 new_mode |= PSTATE_PAN;
10541             }
10542             break;
10543         }
10544     }
10545     if (cpu_isar_feature(aa64_mte, cpu)) {
10546         new_mode |= PSTATE_TCO;
10547     }
10548 
10549     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10550         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10551             new_mode |= PSTATE_SSBS;
10552         } else {
10553             new_mode &= ~PSTATE_SSBS;
10554         }
10555     }
10556 
10557     if (cpu_isar_feature(aa64_nmi, cpu)) {
10558         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10559             new_mode |= PSTATE_ALLINT;
10560         } else {
10561             new_mode &= ~PSTATE_ALLINT;
10562         }
10563     }
10564 
10565     pstate_write(env, PSTATE_DAIF | new_mode);
10566     env->aarch64 = true;
10567     aarch64_restore_sp(env, new_el);
10568 
10569     if (tcg_enabled()) {
10570         helper_rebuild_hflags_a64(env, new_el);
10571     }
10572 
10573     env->pc = addr;
10574 
10575     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10576                   new_el, env->pc, pstate_read(env));
10577 }
10578 
10579 /*
10580  * Do semihosting call and set the appropriate return value. All the
10581  * permission and validity checks have been done at translate time.
10582  *
10583  * We only see semihosting exceptions in TCG only as they are not
10584  * trapped to the hypervisor in KVM.
10585  */
10586 #ifdef CONFIG_TCG
10587 static void tcg_handle_semihosting(CPUState *cs)
10588 {
10589     ARMCPU *cpu = ARM_CPU(cs);
10590     CPUARMState *env = &cpu->env;
10591 
10592     if (is_a64(env)) {
10593         qemu_log_mask(CPU_LOG_INT,
10594                       "...handling as semihosting call 0x%" PRIx64 "\n",
10595                       env->xregs[0]);
10596         do_common_semihosting(cs);
10597         env->pc += 4;
10598     } else {
10599         qemu_log_mask(CPU_LOG_INT,
10600                       "...handling as semihosting call 0x%x\n",
10601                       env->regs[0]);
10602         do_common_semihosting(cs);
10603         env->regs[15] += env->thumb ? 2 : 4;
10604     }
10605 }
10606 #endif
10607 
10608 /*
10609  * Handle a CPU exception for A and R profile CPUs.
10610  * Do any appropriate logging, handle PSCI calls, and then hand off
10611  * to the AArch64-entry or AArch32-entry function depending on the
10612  * target exception level's register width.
10613  *
10614  * Note: this is used for both TCG (as the do_interrupt tcg op),
10615  *       and KVM to re-inject guest debug exceptions, and to
10616  *       inject a Synchronous-External-Abort.
10617  */
10618 void arm_cpu_do_interrupt(CPUState *cs)
10619 {
10620     ARMCPU *cpu = ARM_CPU(cs);
10621     CPUARMState *env = &cpu->env;
10622     unsigned int new_el = env->exception.target_el;
10623 
10624     assert(!arm_feature(env, ARM_FEATURE_M));
10625 
10626     arm_log_exception(cs);
10627     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10628                   new_el);
10629     if (qemu_loglevel_mask(CPU_LOG_INT)
10630         && !excp_is_internal(cs->exception_index)) {
10631         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10632                       syn_get_ec(env->exception.syndrome),
10633                       env->exception.syndrome);
10634     }
10635 
10636     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10637         arm_handle_psci_call(cpu);
10638         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10639         return;
10640     }
10641 
10642     /*
10643      * Semihosting semantics depend on the register width of the code
10644      * that caused the exception, not the target exception level, so
10645      * must be handled here.
10646      */
10647 #ifdef CONFIG_TCG
10648     if (cs->exception_index == EXCP_SEMIHOST) {
10649         tcg_handle_semihosting(cs);
10650         return;
10651     }
10652 #endif
10653 
10654     /*
10655      * Hooks may change global state so BQL should be held, also the
10656      * BQL needs to be held for any modification of
10657      * cs->interrupt_request.
10658      */
10659     g_assert(bql_locked());
10660 
10661     arm_call_pre_el_change_hook(cpu);
10662 
10663     assert(!excp_is_internal(cs->exception_index));
10664     if (arm_el_is_aa64(env, new_el)) {
10665         arm_cpu_do_interrupt_aarch64(cs);
10666     } else {
10667         arm_cpu_do_interrupt_aarch32(cs);
10668     }
10669 
10670     arm_call_el_change_hook(cpu);
10671 
10672     if (!kvm_enabled()) {
10673         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10674     }
10675 }
10676 #endif /* !CONFIG_USER_ONLY */
10677 
10678 uint64_t arm_sctlr(CPUARMState *env, int el)
10679 {
10680     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10681     if (el == 0) {
10682         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10683         switch (mmu_idx) {
10684         case ARMMMUIdx_E20_0:
10685             el = 2;
10686             break;
10687         case ARMMMUIdx_E30_0:
10688             el = 3;
10689             break;
10690         default:
10691             el = 1;
10692             break;
10693         }
10694     }
10695     return env->cp15.sctlr_el[el];
10696 }
10697 
10698 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10699 {
10700     if (regime_has_2_ranges(mmu_idx)) {
10701         return extract64(tcr, 37, 2);
10702     } else if (regime_is_stage2(mmu_idx)) {
10703         return 0; /* VTCR_EL2 */
10704     } else {
10705         /* Replicate the single TBI bit so we always have 2 bits.  */
10706         return extract32(tcr, 20, 1) * 3;
10707     }
10708 }
10709 
10710 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10711 {
10712     if (regime_has_2_ranges(mmu_idx)) {
10713         return extract64(tcr, 51, 2);
10714     } else if (regime_is_stage2(mmu_idx)) {
10715         return 0; /* VTCR_EL2 */
10716     } else {
10717         /* Replicate the single TBID bit so we always have 2 bits.  */
10718         return extract32(tcr, 29, 1) * 3;
10719     }
10720 }
10721 
10722 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10723 {
10724     if (regime_has_2_ranges(mmu_idx)) {
10725         return extract64(tcr, 57, 2);
10726     } else {
10727         /* Replicate the single TCMA bit so we always have 2 bits.  */
10728         return extract32(tcr, 30, 1) * 3;
10729     }
10730 }
10731 
10732 static ARMGranuleSize tg0_to_gran_size(int tg)
10733 {
10734     switch (tg) {
10735     case 0:
10736         return Gran4K;
10737     case 1:
10738         return Gran64K;
10739     case 2:
10740         return Gran16K;
10741     default:
10742         return GranInvalid;
10743     }
10744 }
10745 
10746 static ARMGranuleSize tg1_to_gran_size(int tg)
10747 {
10748     switch (tg) {
10749     case 1:
10750         return Gran16K;
10751     case 2:
10752         return Gran4K;
10753     case 3:
10754         return Gran64K;
10755     default:
10756         return GranInvalid;
10757     }
10758 }
10759 
10760 static inline bool have4k(ARMCPU *cpu, bool stage2)
10761 {
10762     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10763         : cpu_isar_feature(aa64_tgran4, cpu);
10764 }
10765 
10766 static inline bool have16k(ARMCPU *cpu, bool stage2)
10767 {
10768     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10769         : cpu_isar_feature(aa64_tgran16, cpu);
10770 }
10771 
10772 static inline bool have64k(ARMCPU *cpu, bool stage2)
10773 {
10774     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10775         : cpu_isar_feature(aa64_tgran64, cpu);
10776 }
10777 
10778 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10779                                          bool stage2)
10780 {
10781     switch (gran) {
10782     case Gran4K:
10783         if (have4k(cpu, stage2)) {
10784             return gran;
10785         }
10786         break;
10787     case Gran16K:
10788         if (have16k(cpu, stage2)) {
10789             return gran;
10790         }
10791         break;
10792     case Gran64K:
10793         if (have64k(cpu, stage2)) {
10794             return gran;
10795         }
10796         break;
10797     case GranInvalid:
10798         break;
10799     }
10800     /*
10801      * If the guest selects a granule size that isn't implemented,
10802      * the architecture requires that we behave as if it selected one
10803      * that is (with an IMPDEF choice of which one to pick). We choose
10804      * to implement the smallest supported granule size.
10805      */
10806     if (have4k(cpu, stage2)) {
10807         return Gran4K;
10808     }
10809     if (have16k(cpu, stage2)) {
10810         return Gran16K;
10811     }
10812     assert(have64k(cpu, stage2));
10813     return Gran64K;
10814 }
10815 
10816 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10817                                    ARMMMUIdx mmu_idx, bool data,
10818                                    bool el1_is_aa32)
10819 {
10820     uint64_t tcr = regime_tcr(env, mmu_idx);
10821     bool epd, hpd, tsz_oob, ds, ha, hd;
10822     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10823     ARMGranuleSize gran;
10824     ARMCPU *cpu = env_archcpu(env);
10825     bool stage2 = regime_is_stage2(mmu_idx);
10826 
10827     if (!regime_has_2_ranges(mmu_idx)) {
10828         select = 0;
10829         tsz = extract32(tcr, 0, 6);
10830         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10831         if (stage2) {
10832             /* VTCR_EL2 */
10833             hpd = false;
10834         } else {
10835             hpd = extract32(tcr, 24, 1);
10836         }
10837         epd = false;
10838         sh = extract32(tcr, 12, 2);
10839         ps = extract32(tcr, 16, 3);
10840         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
10841         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10842         ds = extract64(tcr, 32, 1);
10843     } else {
10844         bool e0pd;
10845 
10846         /*
10847          * Bit 55 is always between the two regions, and is canonical for
10848          * determining if address tagging is enabled.
10849          */
10850         select = extract64(va, 55, 1);
10851         if (!select) {
10852             tsz = extract32(tcr, 0, 6);
10853             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10854             epd = extract32(tcr, 7, 1);
10855             sh = extract32(tcr, 12, 2);
10856             hpd = extract64(tcr, 41, 1);
10857             e0pd = extract64(tcr, 55, 1);
10858         } else {
10859             tsz = extract32(tcr, 16, 6);
10860             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10861             epd = extract32(tcr, 23, 1);
10862             sh = extract32(tcr, 28, 2);
10863             hpd = extract64(tcr, 42, 1);
10864             e0pd = extract64(tcr, 56, 1);
10865         }
10866         ps = extract64(tcr, 32, 3);
10867         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
10868         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10869         ds = extract64(tcr, 59, 1);
10870 
10871         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
10872             regime_is_user(env, mmu_idx)) {
10873             epd = true;
10874         }
10875     }
10876 
10877     gran = sanitize_gran_size(cpu, gran, stage2);
10878 
10879     if (cpu_isar_feature(aa64_st, cpu)) {
10880         max_tsz = 48 - (gran == Gran64K);
10881     } else {
10882         max_tsz = 39;
10883     }
10884 
10885     /*
10886      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10887      * adjust the effective value of DS, as documented.
10888      */
10889     min_tsz = 16;
10890     if (gran == Gran64K) {
10891         if (cpu_isar_feature(aa64_lva, cpu)) {
10892             min_tsz = 12;
10893         }
10894         ds = false;
10895     } else if (ds) {
10896         if (regime_is_stage2(mmu_idx)) {
10897             if (gran == Gran16K) {
10898                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10899             } else {
10900                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10901             }
10902         } else {
10903             if (gran == Gran16K) {
10904                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10905             } else {
10906                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10907             }
10908         }
10909         if (ds) {
10910             min_tsz = 12;
10911         }
10912     }
10913 
10914     if (stage2 && el1_is_aa32) {
10915         /*
10916          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
10917          * are loosened: a configured IPA of 40 bits is permitted even if
10918          * the implemented PA is less than that (and so a 40 bit IPA would
10919          * fault for an AArch64 EL1). See R_DTLMN.
10920          */
10921         min_tsz = MIN(min_tsz, 24);
10922     }
10923 
10924     if (tsz > max_tsz) {
10925         tsz = max_tsz;
10926         tsz_oob = true;
10927     } else if (tsz < min_tsz) {
10928         tsz = min_tsz;
10929         tsz_oob = true;
10930     } else {
10931         tsz_oob = false;
10932     }
10933 
10934     /* Present TBI as a composite with TBID.  */
10935     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10936     if (!data) {
10937         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10938     }
10939     tbi = (tbi >> select) & 1;
10940 
10941     return (ARMVAParameters) {
10942         .tsz = tsz,
10943         .ps = ps,
10944         .sh = sh,
10945         .select = select,
10946         .tbi = tbi,
10947         .epd = epd,
10948         .hpd = hpd,
10949         .tsz_oob = tsz_oob,
10950         .ds = ds,
10951         .ha = ha,
10952         .hd = ha && hd,
10953         .gran = gran,
10954     };
10955 }
10956 
10957 /*
10958  * Note that signed overflow is undefined in C.  The following routines are
10959  * careful to use unsigned types where modulo arithmetic is required.
10960  * Failure to do so _will_ break on newer gcc.
10961  */
10962 
10963 /* Signed saturating arithmetic.  */
10964 
10965 /* Perform 16-bit signed saturating addition.  */
10966 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10967 {
10968     uint16_t res;
10969 
10970     res = a + b;
10971     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10972         if (a & 0x8000) {
10973             res = 0x8000;
10974         } else {
10975             res = 0x7fff;
10976         }
10977     }
10978     return res;
10979 }
10980 
10981 /* Perform 8-bit signed saturating addition.  */
10982 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10983 {
10984     uint8_t res;
10985 
10986     res = a + b;
10987     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10988         if (a & 0x80) {
10989             res = 0x80;
10990         } else {
10991             res = 0x7f;
10992         }
10993     }
10994     return res;
10995 }
10996 
10997 /* Perform 16-bit signed saturating subtraction.  */
10998 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10999 {
11000     uint16_t res;
11001 
11002     res = a - b;
11003     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11004         if (a & 0x8000) {
11005             res = 0x8000;
11006         } else {
11007             res = 0x7fff;
11008         }
11009     }
11010     return res;
11011 }
11012 
11013 /* Perform 8-bit signed saturating subtraction.  */
11014 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11015 {
11016     uint8_t res;
11017 
11018     res = a - b;
11019     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11020         if (a & 0x80) {
11021             res = 0x80;
11022         } else {
11023             res = 0x7f;
11024         }
11025     }
11026     return res;
11027 }
11028 
11029 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11030 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11031 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11032 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11033 #define PFX q
11034 
11035 #include "op_addsub.h"
11036 
11037 /* Unsigned saturating arithmetic.  */
11038 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11039 {
11040     uint16_t res;
11041     res = a + b;
11042     if (res < a) {
11043         res = 0xffff;
11044     }
11045     return res;
11046 }
11047 
11048 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11049 {
11050     if (a > b) {
11051         return a - b;
11052     } else {
11053         return 0;
11054     }
11055 }
11056 
11057 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11058 {
11059     uint8_t res;
11060     res = a + b;
11061     if (res < a) {
11062         res = 0xff;
11063     }
11064     return res;
11065 }
11066 
11067 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11068 {
11069     if (a > b) {
11070         return a - b;
11071     } else {
11072         return 0;
11073     }
11074 }
11075 
11076 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11077 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11078 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11079 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11080 #define PFX uq
11081 
11082 #include "op_addsub.h"
11083 
11084 /* Signed modulo arithmetic.  */
11085 #define SARITH16(a, b, n, op) do { \
11086     int32_t sum; \
11087     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11088     RESULT(sum, n, 16); \
11089     if (sum >= 0) \
11090         ge |= 3 << (n * 2); \
11091     } while (0)
11092 
11093 #define SARITH8(a, b, n, op) do { \
11094     int32_t sum; \
11095     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11096     RESULT(sum, n, 8); \
11097     if (sum >= 0) \
11098         ge |= 1 << n; \
11099     } while (0)
11100 
11101 
11102 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11103 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11104 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11105 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11106 #define PFX s
11107 #define ARITH_GE
11108 
11109 #include "op_addsub.h"
11110 
11111 /* Unsigned modulo arithmetic.  */
11112 #define ADD16(a, b, n) do { \
11113     uint32_t sum; \
11114     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11115     RESULT(sum, n, 16); \
11116     if ((sum >> 16) == 1) \
11117         ge |= 3 << (n * 2); \
11118     } while (0)
11119 
11120 #define ADD8(a, b, n) do { \
11121     uint32_t sum; \
11122     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11123     RESULT(sum, n, 8); \
11124     if ((sum >> 8) == 1) \
11125         ge |= 1 << n; \
11126     } while (0)
11127 
11128 #define SUB16(a, b, n) do { \
11129     uint32_t sum; \
11130     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11131     RESULT(sum, n, 16); \
11132     if ((sum >> 16) == 0) \
11133         ge |= 3 << (n * 2); \
11134     } while (0)
11135 
11136 #define SUB8(a, b, n) do { \
11137     uint32_t sum; \
11138     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11139     RESULT(sum, n, 8); \
11140     if ((sum >> 8) == 0) \
11141         ge |= 1 << n; \
11142     } while (0)
11143 
11144 #define PFX u
11145 #define ARITH_GE
11146 
11147 #include "op_addsub.h"
11148 
11149 /* Halved signed arithmetic.  */
11150 #define ADD16(a, b, n) \
11151   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11152 #define SUB16(a, b, n) \
11153   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11154 #define ADD8(a, b, n) \
11155   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11156 #define SUB8(a, b, n) \
11157   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11158 #define PFX sh
11159 
11160 #include "op_addsub.h"
11161 
11162 /* Halved unsigned arithmetic.  */
11163 #define ADD16(a, b, n) \
11164   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11165 #define SUB16(a, b, n) \
11166   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11167 #define ADD8(a, b, n) \
11168   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11169 #define SUB8(a, b, n) \
11170   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11171 #define PFX uh
11172 
11173 #include "op_addsub.h"
11174 
11175 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11176 {
11177     if (a > b) {
11178         return a - b;
11179     } else {
11180         return b - a;
11181     }
11182 }
11183 
11184 /* Unsigned sum of absolute byte differences.  */
11185 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11186 {
11187     uint32_t sum;
11188     sum = do_usad(a, b);
11189     sum += do_usad(a >> 8, b >> 8);
11190     sum += do_usad(a >> 16, b >> 16);
11191     sum += do_usad(a >> 24, b >> 24);
11192     return sum;
11193 }
11194 
11195 /* For ARMv6 SEL instruction.  */
11196 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11197 {
11198     uint32_t mask;
11199 
11200     mask = 0;
11201     if (flags & 1) {
11202         mask |= 0xff;
11203     }
11204     if (flags & 2) {
11205         mask |= 0xff00;
11206     }
11207     if (flags & 4) {
11208         mask |= 0xff0000;
11209     }
11210     if (flags & 8) {
11211         mask |= 0xff000000;
11212     }
11213     return (a & mask) | (b & ~mask);
11214 }
11215 
11216 /*
11217  * CRC helpers.
11218  * The upper bytes of val (above the number specified by 'bytes') must have
11219  * been zeroed out by the caller.
11220  */
11221 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11222 {
11223     uint8_t buf[4];
11224 
11225     stl_le_p(buf, val);
11226 
11227     /* zlib crc32 converts the accumulator and output to one's complement.  */
11228     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11229 }
11230 
11231 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11232 {
11233     uint8_t buf[4];
11234 
11235     stl_le_p(buf, val);
11236 
11237     /* Linux crc32c converts the output to one's complement.  */
11238     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11239 }
11240 
11241 /*
11242  * Return the exception level to which FP-disabled exceptions should
11243  * be taken, or 0 if FP is enabled.
11244  */
11245 int fp_exception_el(CPUARMState *env, int cur_el)
11246 {
11247 #ifndef CONFIG_USER_ONLY
11248     uint64_t hcr_el2;
11249 
11250     /*
11251      * CPACR and the CPTR registers don't exist before v6, so FP is
11252      * always accessible
11253      */
11254     if (!arm_feature(env, ARM_FEATURE_V6)) {
11255         return 0;
11256     }
11257 
11258     if (arm_feature(env, ARM_FEATURE_M)) {
11259         /* CPACR can cause a NOCP UsageFault taken to current security state */
11260         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11261             return 1;
11262         }
11263 
11264         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11265             if (!extract32(env->v7m.nsacr, 10, 1)) {
11266                 /* FP insns cause a NOCP UsageFault taken to Secure */
11267                 return 3;
11268             }
11269         }
11270 
11271         return 0;
11272     }
11273 
11274     hcr_el2 = arm_hcr_el2_eff(env);
11275 
11276     /*
11277      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11278      * 0, 2 : trap EL0 and EL1/PL1 accesses
11279      * 1    : trap only EL0 accesses
11280      * 3    : trap no accesses
11281      * This register is ignored if E2H+TGE are both set.
11282      */
11283     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11284         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11285 
11286         switch (fpen) {
11287         case 1:
11288             if (cur_el != 0) {
11289                 break;
11290             }
11291             /* fall through */
11292         case 0:
11293         case 2:
11294             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11295             if (!arm_el_is_aa64(env, 3)
11296                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11297                 return 3;
11298             }
11299             if (cur_el <= 1) {
11300                 return 1;
11301             }
11302             break;
11303         }
11304     }
11305 
11306     /*
11307      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11308      * to control non-secure access to the FPU. It doesn't have any
11309      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11310      */
11311     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11312          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11313         if (!extract32(env->cp15.nsacr, 10, 1)) {
11314             /* FP insns act as UNDEF */
11315             return cur_el == 2 ? 2 : 1;
11316         }
11317     }
11318 
11319     /*
11320      * CPTR_EL2 is present in v7VE or v8, and changes format
11321      * with HCR_EL2.E2H (regardless of TGE).
11322      */
11323     if (cur_el <= 2) {
11324         if (hcr_el2 & HCR_E2H) {
11325             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11326             case 1:
11327                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11328                     break;
11329                 }
11330                 /* fall through */
11331             case 0:
11332             case 2:
11333                 return 2;
11334             }
11335         } else if (arm_is_el2_enabled(env)) {
11336             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11337                 return 2;
11338             }
11339         }
11340     }
11341 
11342     /* CPTR_EL3 : present in v8 */
11343     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11344         /* Trap all FP ops to EL3 */
11345         return 3;
11346     }
11347 #endif
11348     return 0;
11349 }
11350 
11351 /* Return the exception level we're running at if this is our mmu_idx */
11352 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11353 {
11354     if (mmu_idx & ARM_MMU_IDX_M) {
11355         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11356     }
11357 
11358     switch (mmu_idx) {
11359     case ARMMMUIdx_E10_0:
11360     case ARMMMUIdx_E20_0:
11361     case ARMMMUIdx_E30_0:
11362         return 0;
11363     case ARMMMUIdx_E10_1:
11364     case ARMMMUIdx_E10_1_PAN:
11365         return 1;
11366     case ARMMMUIdx_E2:
11367     case ARMMMUIdx_E20_2:
11368     case ARMMMUIdx_E20_2_PAN:
11369         return 2;
11370     case ARMMMUIdx_E3:
11371     case ARMMMUIdx_E30_3_PAN:
11372         return 3;
11373     default:
11374         g_assert_not_reached();
11375     }
11376 }
11377 
11378 #ifndef CONFIG_TCG
11379 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11380 {
11381     g_assert_not_reached();
11382 }
11383 #endif
11384 
11385 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11386 {
11387     ARMMMUIdx idx;
11388     uint64_t hcr;
11389 
11390     if (arm_feature(env, ARM_FEATURE_M)) {
11391         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11392     }
11393 
11394     /* See ARM pseudo-function ELIsInHost.  */
11395     switch (el) {
11396     case 0:
11397         hcr = arm_hcr_el2_eff(env);
11398         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11399             idx = ARMMMUIdx_E20_0;
11400         } else if (arm_is_secure_below_el3(env) &&
11401                    !arm_el_is_aa64(env, 3)) {
11402             idx = ARMMMUIdx_E30_0;
11403         } else {
11404             idx = ARMMMUIdx_E10_0;
11405         }
11406         break;
11407     case 1:
11408         if (arm_pan_enabled(env)) {
11409             idx = ARMMMUIdx_E10_1_PAN;
11410         } else {
11411             idx = ARMMMUIdx_E10_1;
11412         }
11413         break;
11414     case 2:
11415         /* Note that TGE does not apply at EL2.  */
11416         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11417             if (arm_pan_enabled(env)) {
11418                 idx = ARMMMUIdx_E20_2_PAN;
11419             } else {
11420                 idx = ARMMMUIdx_E20_2;
11421             }
11422         } else {
11423             idx = ARMMMUIdx_E2;
11424         }
11425         break;
11426     case 3:
11427         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11428             return ARMMMUIdx_E30_3_PAN;
11429         }
11430         return ARMMMUIdx_E3;
11431     default:
11432         g_assert_not_reached();
11433     }
11434 
11435     return idx;
11436 }
11437 
11438 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11439 {
11440     return arm_mmu_idx_el(env, arm_current_el(env));
11441 }
11442 
11443 static bool mve_no_pred(CPUARMState *env)
11444 {
11445     /*
11446      * Return true if there is definitely no predication of MVE
11447      * instructions by VPR or LTPSIZE. (Returning false even if there
11448      * isn't any predication is OK; generated code will just be
11449      * a little worse.)
11450      * If the CPU does not implement MVE then this TB flag is always 0.
11451      *
11452      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11453      * logic in gen_update_fp_context() needs to be updated to match.
11454      *
11455      * We do not include the effect of the ECI bits here -- they are
11456      * tracked in other TB flags. This simplifies the logic for
11457      * "when did we emit code that changes the MVE_NO_PRED TB flag
11458      * and thus need to end the TB?".
11459      */
11460     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11461         return false;
11462     }
11463     if (env->v7m.vpr) {
11464         return false;
11465     }
11466     if (env->v7m.ltpsize < 4) {
11467         return false;
11468     }
11469     return true;
11470 }
11471 
11472 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11473                           uint64_t *cs_base, uint32_t *pflags)
11474 {
11475     CPUARMTBFlags flags;
11476 
11477     assert_hflags_rebuild_correctly(env);
11478     flags = env->hflags;
11479 
11480     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11481         *pc = env->pc;
11482         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11483             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11484         }
11485     } else {
11486         *pc = env->regs[15];
11487 
11488         if (arm_feature(env, ARM_FEATURE_M)) {
11489             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11490                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11491                 != env->v7m.secure) {
11492                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11493             }
11494 
11495             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11496                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11497                  (env->v7m.secure &&
11498                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11499                 /*
11500                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11501                  * active FP context; we must create a new FP context before
11502                  * executing any FP insn.
11503                  */
11504                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11505             }
11506 
11507             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11508             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11509                 DP_TBFLAG_M32(flags, LSPACT, 1);
11510             }
11511 
11512             if (mve_no_pred(env)) {
11513                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11514             }
11515         } else {
11516             /*
11517              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11518              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11519              */
11520             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11521                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11522             } else {
11523                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11524                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11525             }
11526             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11527                 DP_TBFLAG_A32(flags, VFPEN, 1);
11528             }
11529         }
11530 
11531         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11532         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11533     }
11534 
11535     /*
11536      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11537      * states defined in the ARM ARM for software singlestep:
11538      *  SS_ACTIVE   PSTATE.SS   State
11539      *     0            x       Inactive (the TB flag for SS is always 0)
11540      *     1            0       Active-pending
11541      *     1            1       Active-not-pending
11542      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11543      */
11544     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11545         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11546     }
11547 
11548     *pflags = flags.flags;
11549     *cs_base = flags.flags2;
11550 }
11551 
11552 #ifdef TARGET_AARCH64
11553 /*
11554  * The manual says that when SVE is enabled and VQ is widened the
11555  * implementation is allowed to zero the previously inaccessible
11556  * portion of the registers.  The corollary to that is that when
11557  * SVE is enabled and VQ is narrowed we are also allowed to zero
11558  * the now inaccessible portion of the registers.
11559  *
11560  * The intent of this is that no predicate bit beyond VQ is ever set.
11561  * Which means that some operations on predicate registers themselves
11562  * may operate on full uint64_t or even unrolled across the maximum
11563  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11564  * may well be cheaper than conditionals to restrict the operation
11565  * to the relevant portion of a uint16_t[16].
11566  */
11567 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11568 {
11569     int i, j;
11570     uint64_t pmask;
11571 
11572     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11573     assert(vq <= env_archcpu(env)->sve_max_vq);
11574 
11575     /* Zap the high bits of the zregs.  */
11576     for (i = 0; i < 32; i++) {
11577         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11578     }
11579 
11580     /* Zap the high bits of the pregs and ffr.  */
11581     pmask = 0;
11582     if (vq & 3) {
11583         pmask = ~(-1ULL << (16 * (vq & 3)));
11584     }
11585     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11586         for (i = 0; i < 17; ++i) {
11587             env->vfp.pregs[i].p[j] &= pmask;
11588         }
11589         pmask = 0;
11590     }
11591 }
11592 
11593 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11594 {
11595     int exc_el;
11596 
11597     if (sm) {
11598         exc_el = sme_exception_el(env, el);
11599     } else {
11600         exc_el = sve_exception_el(env, el);
11601     }
11602     if (exc_el) {
11603         return 0; /* disabled */
11604     }
11605     return sve_vqm1_for_el_sm(env, el, sm);
11606 }
11607 
11608 /*
11609  * Notice a change in SVE vector size when changing EL.
11610  */
11611 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11612                            int new_el, bool el0_a64)
11613 {
11614     ARMCPU *cpu = env_archcpu(env);
11615     int old_len, new_len;
11616     bool old_a64, new_a64, sm;
11617 
11618     /* Nothing to do if no SVE.  */
11619     if (!cpu_isar_feature(aa64_sve, cpu)) {
11620         return;
11621     }
11622 
11623     /* Nothing to do if FP is disabled in either EL.  */
11624     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11625         return;
11626     }
11627 
11628     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11629     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11630 
11631     /*
11632      * Both AArch64.TakeException and AArch64.ExceptionReturn
11633      * invoke ResetSVEState when taking an exception from, or
11634      * returning to, AArch32 state when PSTATE.SM is enabled.
11635      */
11636     sm = FIELD_EX64(env->svcr, SVCR, SM);
11637     if (old_a64 != new_a64 && sm) {
11638         arm_reset_sve_state(env);
11639         return;
11640     }
11641 
11642     /*
11643      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11644      * at ELx, or not available because the EL is in AArch32 state, then
11645      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11646      * has an effective value of 0".
11647      *
11648      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11649      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11650      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11651      * we already have the correct register contents when encountering the
11652      * vq0->vq0 transition between EL0->EL1.
11653      */
11654     old_len = new_len = 0;
11655     if (old_a64) {
11656         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11657     }
11658     if (new_a64) {
11659         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11660     }
11661 
11662     /* When changing vector length, clear inaccessible state.  */
11663     if (new_len < old_len) {
11664         aarch64_sve_narrow_vq(env, new_len + 1);
11665     }
11666 }
11667 #endif
11668 
11669 #ifndef CONFIG_USER_ONLY
11670 ARMSecuritySpace arm_security_space(CPUARMState *env)
11671 {
11672     if (arm_feature(env, ARM_FEATURE_M)) {
11673         return arm_secure_to_space(env->v7m.secure);
11674     }
11675 
11676     /*
11677      * If EL3 is not supported then the secure state is implementation
11678      * defined, in which case QEMU defaults to non-secure.
11679      */
11680     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11681         return ARMSS_NonSecure;
11682     }
11683 
11684     /* Check for AArch64 EL3 or AArch32 Mon. */
11685     if (is_a64(env)) {
11686         if (extract32(env->pstate, 2, 2) == 3) {
11687             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11688                 return ARMSS_Root;
11689             } else {
11690                 return ARMSS_Secure;
11691             }
11692         }
11693     } else {
11694         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11695             return ARMSS_Secure;
11696         }
11697     }
11698 
11699     return arm_security_space_below_el3(env);
11700 }
11701 
11702 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11703 {
11704     assert(!arm_feature(env, ARM_FEATURE_M));
11705 
11706     /*
11707      * If EL3 is not supported then the secure state is implementation
11708      * defined, in which case QEMU defaults to non-secure.
11709      */
11710     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11711         return ARMSS_NonSecure;
11712     }
11713 
11714     /*
11715      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11716      * Ignoring NSE when !NS retains consistency without having to
11717      * modify other predicates.
11718      */
11719     if (!(env->cp15.scr_el3 & SCR_NS)) {
11720         return ARMSS_Secure;
11721     } else if (env->cp15.scr_el3 & SCR_NSE) {
11722         return ARMSS_Realm;
11723     } else {
11724         return ARMSS_NonSecure;
11725     }
11726 }
11727 #endif /* !CONFIG_USER_ONLY */
11728