xref: /qemu/target/arm/helper.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "exec/page-protection.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include "exec/translation-block.h"
23 #include "hw/irq.h"
24 #include "system/cpu-timers.h"
25 #include "system/kvm.h"
26 #include "system/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 #include "target/arm/gtimer.h"
34 
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,
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     if (cpu_isar_feature(aa64_cmow, cpu)) {
5350         valid_mask |= HCRX_CMOW;
5351     }
5352     /* FEAT_XS adds FGTnXS, FnXS */
5353     if (cpu_isar_feature(aa64_xs, cpu)) {
5354         valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5355     }
5356 
5357     /* Clear RES0 bits.  */
5358     env->cp15.hcrx_el2 = value & valid_mask;
5359 
5360     /*
5361      * Updates to VINMI and VFNMI require us to update the status of
5362      * virtual NMI, which are the logical OR of these bits
5363      * and the state of the input lines from the GIC. (This requires
5364      * that we have the BQL, which is done by marking the
5365      * reginfo structs as ARM_CP_IO.)
5366      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5367      * possible for it to be taken immediately, because VINMI and
5368      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5369      * can only be written at EL2.
5370      */
5371     if (cpu_isar_feature(aa64_nmi, cpu)) {
5372         g_assert(bql_locked());
5373         arm_cpu_update_vinmi(cpu);
5374         arm_cpu_update_vfnmi(cpu);
5375     }
5376 }
5377 
5378 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5379                                   bool isread)
5380 {
5381     if (arm_current_el(env) == 2
5382         && arm_feature(env, ARM_FEATURE_EL3)
5383         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5384         return CP_ACCESS_TRAP_EL3;
5385     }
5386     return CP_ACCESS_OK;
5387 }
5388 
5389 static const ARMCPRegInfo hcrx_el2_reginfo = {
5390     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5391     .type = ARM_CP_IO,
5392     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5393     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5394     .nv2_redirect_offset = 0xa0,
5395     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5396 };
5397 
5398 /* Return the effective value of HCRX_EL2.  */
5399 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5400 {
5401     /*
5402      * The bits in this register behave as 0 for all purposes other than
5403      * direct reads of the register if SCR_EL3.HXEn is 0.
5404      * If EL2 is not enabled in the current security state, then the
5405      * bit may behave as if 0, or as if 1, depending on the bit.
5406      * For the moment, we treat the EL2-disabled case as taking
5407      * priority over the HXEn-disabled case. This is true for the only
5408      * bit for a feature which we implement where the answer is different
5409      * for the two cases (MSCEn for FEAT_MOPS).
5410      * This may need to be revisited for future bits.
5411      */
5412     if (!arm_is_el2_enabled(env)) {
5413         uint64_t hcrx = 0;
5414         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5415             /* MSCEn behaves as 1 if EL2 is not enabled */
5416             hcrx |= HCRX_MSCEN;
5417         }
5418         return hcrx;
5419     }
5420     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5421         return 0;
5422     }
5423     return env->cp15.hcrx_el2;
5424 }
5425 
5426 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5427                            uint64_t value)
5428 {
5429     /*
5430      * For A-profile AArch32 EL3, if NSACR.CP10
5431      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5432      */
5433     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5434         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5435         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5436         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5437     }
5438     env->cp15.cptr_el[2] = value;
5439 }
5440 
5441 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5442 {
5443     /*
5444      * For A-profile AArch32 EL3, if NSACR.CP10
5445      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5446      */
5447     uint64_t value = env->cp15.cptr_el[2];
5448 
5449     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5450         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5451         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5452     }
5453     return value;
5454 }
5455 
5456 static const ARMCPRegInfo el2_cp_reginfo[] = {
5457     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5458       .type = ARM_CP_IO,
5459       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5460       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5461       .nv2_redirect_offset = 0x78,
5462       .writefn = hcr_write, .raw_writefn = raw_write },
5463     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5464       .type = ARM_CP_ALIAS | ARM_CP_IO,
5465       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5466       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5467       .writefn = hcr_writelow },
5468     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5469       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5470       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5471     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5472       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5473       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5474       .access = PL2_RW,
5475       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5476     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5477       .type = ARM_CP_NV2_REDIRECT,
5478       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5479       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5480     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5481       .type = ARM_CP_NV2_REDIRECT,
5482       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5483       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5484     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5485       .type = ARM_CP_ALIAS,
5486       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5487       .access = PL2_RW,
5488       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5489     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5490       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5491       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5492       .access = PL2_RW,
5493       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5494     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5495       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5496       .access = PL2_RW, .writefn = vbar_write,
5497       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5498       .resetvalue = 0 },
5499     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5500       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5501       .access = PL3_RW, .type = ARM_CP_ALIAS,
5502       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5503     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5504       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5505       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5506       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5507       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5508     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5509       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5510       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5511       .resetvalue = 0 },
5512     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5513       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5514       .access = PL2_RW, .type = ARM_CP_ALIAS,
5515       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5516     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5517       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5518       .access = PL2_RW, .type = ARM_CP_CONST,
5519       .resetvalue = 0 },
5520     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5521     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5522       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5523       .access = PL2_RW, .type = ARM_CP_CONST,
5524       .resetvalue = 0 },
5525     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5526       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5527       .access = PL2_RW, .type = ARM_CP_CONST,
5528       .resetvalue = 0 },
5529     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5530       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5531       .access = PL2_RW, .type = ARM_CP_CONST,
5532       .resetvalue = 0 },
5533     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5534       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5535       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5536       .raw_writefn = raw_write,
5537       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5538     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5539       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5540       .type = ARM_CP_ALIAS,
5541       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5542       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5543     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5544       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5545       .access = PL2_RW,
5546       .nv2_redirect_offset = 0x40,
5547       /* no .writefn needed as this can't cause an ASID change */
5548       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5549     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5550       .cp = 15, .opc1 = 6, .crm = 2,
5551       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5552       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5553       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5554       .writefn = vttbr_write, .raw_writefn = raw_write },
5555     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5556       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5557       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5558       .nv2_redirect_offset = 0x20,
5559       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5560     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5561       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5562       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5563       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5564     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5565       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5566       .access = PL2_RW, .resetvalue = 0,
5567       .nv2_redirect_offset = 0x90,
5568       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5569     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5570       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5571       .access = PL2_RW, .resetvalue = 0,
5572       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5573       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5574     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5575       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5576       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5577 #ifndef CONFIG_USER_ONLY
5578     /*
5579      * Unlike the other EL2-related AT operations, these must
5580      * UNDEF from EL3 if EL2 is not implemented, which is why we
5581      * define them here rather than with the rest of the AT ops.
5582      */
5583     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5584       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5585       .access = PL2_W, .accessfn = at_s1e2_access,
5586       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5587       .writefn = ats_write64 },
5588     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5589       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5590       .access = PL2_W, .accessfn = at_s1e2_access,
5591       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5592       .writefn = ats_write64 },
5593     /*
5594      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5595      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5596      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5597      * to behave as if SCR.NS was 1.
5598      */
5599     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5600       .access = PL2_W,
5601       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5602     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5603       .access = PL2_W,
5604       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5605     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5606       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5607       /*
5608        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5609        * reset values as IMPDEF. We choose to reset to 3 to comply with
5610        * both ARMv7 and ARMv8.
5611        */
5612       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5613       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5614       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5615     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5616       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5617       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5618       .writefn = gt_cntvoff_write,
5619       .nv2_redirect_offset = 0x60,
5620       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5621     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5622       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5623       .writefn = gt_cntvoff_write,
5624       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5625     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5626       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5627       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5628       .type = ARM_CP_IO, .access = PL2_RW,
5629       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5630     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5631       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5632       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5633       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5634     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5635       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5636       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5637       .resetfn = gt_hyp_timer_reset,
5638       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5639     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5640       .type = ARM_CP_IO,
5641       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5642       .access = PL2_RW,
5643       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5644       .resetvalue = 0,
5645       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5646 #endif
5647     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5648       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5649       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5650       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5651     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5652       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5653       .access = PL2_RW,
5654       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5655     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5656       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5657       .access = PL2_RW,
5658       .nv2_redirect_offset = 0x80,
5659       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5660 };
5661 
5662 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5663     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5664       .type = ARM_CP_ALIAS | ARM_CP_IO,
5665       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5666       .access = PL2_RW,
5667       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5668       .writefn = hcr_writehigh },
5669 };
5670 
5671 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5672                                   bool isread)
5673 {
5674     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5675         return CP_ACCESS_OK;
5676     }
5677     return CP_ACCESS_TRAP_UNCATEGORIZED;
5678 }
5679 
5680 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5681     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5682       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5683       .access = PL2_RW, .accessfn = sel2_access,
5684       .nv2_redirect_offset = 0x30,
5685       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5686     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5687       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5688       .access = PL2_RW, .accessfn = sel2_access,
5689       .nv2_redirect_offset = 0x48,
5690       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5691 };
5692 
5693 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5694                                    bool isread)
5695 {
5696     /*
5697      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5698      * At Secure EL1 it traps to EL3 or EL2.
5699      */
5700     if (arm_current_el(env) == 3) {
5701         return CP_ACCESS_OK;
5702     }
5703     if (arm_is_secure_below_el3(env)) {
5704         if (env->cp15.scr_el3 & SCR_EEL2) {
5705             return CP_ACCESS_TRAP_EL2;
5706         }
5707         return CP_ACCESS_TRAP_EL3;
5708     }
5709     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5710     if (isread) {
5711         return CP_ACCESS_OK;
5712     }
5713     return CP_ACCESS_TRAP_UNCATEGORIZED;
5714 }
5715 
5716 static const ARMCPRegInfo el3_cp_reginfo[] = {
5717     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5718       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5719       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5720       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5721     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5722       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5723       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5724       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5725       .writefn = scr_write, .raw_writefn = raw_write },
5726     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5727       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5728       .access = PL3_RW, .resetvalue = 0,
5729       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5730     { .name = "SDER",
5731       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5732       .access = PL3_RW, .resetvalue = 0,
5733       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5734     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5735       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5736       .writefn = vbar_write, .resetvalue = 0,
5737       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5738     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5739       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5740       .access = PL3_RW, .resetvalue = 0,
5741       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5742     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5743       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5744       .access = PL3_RW,
5745       /* no .writefn needed as this can't cause an ASID change */
5746       .resetvalue = 0,
5747       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5748     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5749       .type = ARM_CP_ALIAS,
5750       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5751       .access = PL3_RW,
5752       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5753     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5754       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5755       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5756     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5757       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5758       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5759     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5760       .type = ARM_CP_ALIAS,
5761       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5762       .access = PL3_RW,
5763       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5764     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5765       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5766       .access = PL3_RW, .writefn = vbar_write,
5767       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5768       .resetvalue = 0 },
5769     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5770       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5771       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5772       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5773     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5774       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5775       .access = PL3_RW, .resetvalue = 0,
5776       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5777     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5778       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5779       .access = PL3_RW, .type = ARM_CP_CONST,
5780       .resetvalue = 0 },
5781     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5782       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5783       .access = PL3_RW, .type = ARM_CP_CONST,
5784       .resetvalue = 0 },
5785     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5786       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5787       .access = PL3_RW, .type = ARM_CP_CONST,
5788       .resetvalue = 0 },
5789 };
5790 
5791 #ifndef CONFIG_USER_ONLY
5792 
5793 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
5794                                  bool isread)
5795 {
5796     if (arm_current_el(env) == 1) {
5797         /* This must be a FEAT_NV access */
5798         return CP_ACCESS_OK;
5799     }
5800     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
5801         return CP_ACCESS_TRAP_UNCATEGORIZED;
5802     }
5803     return CP_ACCESS_OK;
5804 }
5805 
5806 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
5807                                       bool isread)
5808 {
5809     if (arm_current_el(env) == 1) {
5810         /* This must be a FEAT_NV access with NVx == 101 */
5811         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
5812             return CP_ACCESS_TRAP_EL2;
5813         }
5814     }
5815     return e2h_access(env, ri, isread);
5816 }
5817 
5818 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
5819                                       bool isread)
5820 {
5821     if (arm_current_el(env) == 1) {
5822         /* This must be a FEAT_NV access with NVx == 101 */
5823         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
5824             return CP_ACCESS_TRAP_EL2;
5825         }
5826     }
5827     return e2h_access(env, ri, isread);
5828 }
5829 
5830 /* Test if system register redirection is to occur in the current state.  */
5831 static bool redirect_for_e2h(CPUARMState *env)
5832 {
5833     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5834 }
5835 
5836 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5837 {
5838     CPReadFn *readfn;
5839 
5840     if (redirect_for_e2h(env)) {
5841         /* Switch to the saved EL2 version of the register.  */
5842         ri = ri->opaque;
5843         readfn = ri->readfn;
5844     } else {
5845         readfn = ri->orig_readfn;
5846     }
5847     if (readfn == NULL) {
5848         readfn = raw_read;
5849     }
5850     return readfn(env, ri);
5851 }
5852 
5853 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5854                           uint64_t value)
5855 {
5856     CPWriteFn *writefn;
5857 
5858     if (redirect_for_e2h(env)) {
5859         /* Switch to the saved EL2 version of the register.  */
5860         ri = ri->opaque;
5861         writefn = ri->writefn;
5862     } else {
5863         writefn = ri->orig_writefn;
5864     }
5865     if (writefn == NULL) {
5866         writefn = raw_write;
5867     }
5868     writefn(env, ri, value);
5869 }
5870 
5871 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
5872 {
5873     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
5874     return ri->orig_readfn(env, ri->opaque);
5875 }
5876 
5877 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
5878                               uint64_t value)
5879 {
5880     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
5881     return ri->orig_writefn(env, ri->opaque, value);
5882 }
5883 
5884 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
5885                                          const ARMCPRegInfo *ri,
5886                                          bool isread)
5887 {
5888     if (arm_current_el(env) == 1) {
5889         /*
5890          * This must be a FEAT_NV access (will either trap or redirect
5891          * to memory). None of the registers with _EL12 aliases want to
5892          * apply their trap controls for this kind of access, so don't
5893          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
5894          */
5895         return CP_ACCESS_OK;
5896     }
5897     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
5898     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
5899         return CP_ACCESS_TRAP_UNCATEGORIZED;
5900     }
5901     if (ri->orig_accessfn) {
5902         return ri->orig_accessfn(env, ri->opaque, isread);
5903     }
5904     return CP_ACCESS_OK;
5905 }
5906 
5907 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5908 {
5909     struct E2HAlias {
5910         uint32_t src_key, dst_key, new_key;
5911         const char *src_name, *dst_name, *new_name;
5912         bool (*feature)(const ARMISARegisters *id);
5913     };
5914 
5915 #define K(op0, op1, crn, crm, op2) \
5916     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5917 
5918     static const struct E2HAlias aliases[] = {
5919         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5920           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5921         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5922           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5923         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5924           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5925         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5926           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5927         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5928           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5929         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5930           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5931         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5932           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5933         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5934           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5935         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5936           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5937         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5938           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5939         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5940           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5941         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5942           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5943         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5944           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5945         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5946           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5947         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5948           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5949         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5950           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5951 
5952         /*
5953          * Note that redirection of ZCR is mentioned in the description
5954          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5955          * not in the summary table.
5956          */
5957         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5958           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5959         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5960           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5961 
5962         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5963           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5964 
5965         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5966           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5967           isar_feature_aa64_scxtnum },
5968 
5969         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5970         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5971     };
5972 #undef K
5973 
5974     size_t i;
5975 
5976     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5977         const struct E2HAlias *a = &aliases[i];
5978         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5979         bool ok;
5980 
5981         if (a->feature && !a->feature(&cpu->isar)) {
5982             continue;
5983         }
5984 
5985         src_reg = g_hash_table_lookup(cpu->cp_regs,
5986                                       (gpointer)(uintptr_t)a->src_key);
5987         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5988                                       (gpointer)(uintptr_t)a->dst_key);
5989         g_assert(src_reg != NULL);
5990         g_assert(dst_reg != NULL);
5991 
5992         /* Cross-compare names to detect typos in the keys.  */
5993         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5994         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5995 
5996         /* None of the core system registers use opaque; we will.  */
5997         g_assert(src_reg->opaque == NULL);
5998 
5999         /* Create alias before redirection so we dup the right data. */
6000         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6001 
6002         new_reg->name = a->new_name;
6003         new_reg->type |= ARM_CP_ALIAS;
6004         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6005         new_reg->access &= PL2_RW | PL3_RW;
6006         /* The new_reg op fields are as per new_key, not the target reg */
6007         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6008             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6009         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6010             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6011         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6012             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6013         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6014             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6015         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6016             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6017         new_reg->opaque = src_reg;
6018         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6019         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6020         new_reg->orig_accessfn = src_reg->accessfn;
6021         if (!new_reg->raw_readfn) {
6022             new_reg->raw_readfn = raw_read;
6023         }
6024         if (!new_reg->raw_writefn) {
6025             new_reg->raw_writefn = raw_write;
6026         }
6027         new_reg->readfn = el2_e2h_e12_read;
6028         new_reg->writefn = el2_e2h_e12_write;
6029         new_reg->accessfn = el2_e2h_e12_access;
6030 
6031         /*
6032          * If the _EL1 register is redirected to memory by FEAT_NV2,
6033          * then it shares the offset with the _EL12 register,
6034          * and which one is redirected depends on HCR_EL2.NV1.
6035          */
6036         if (new_reg->nv2_redirect_offset) {
6037             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6038             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6039             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6040         }
6041 
6042         ok = g_hash_table_insert(cpu->cp_regs,
6043                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6044         g_assert(ok);
6045 
6046         src_reg->opaque = dst_reg;
6047         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6048         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6049         if (!src_reg->raw_readfn) {
6050             src_reg->raw_readfn = raw_read;
6051         }
6052         if (!src_reg->raw_writefn) {
6053             src_reg->raw_writefn = raw_write;
6054         }
6055         src_reg->readfn = el2_e2h_read;
6056         src_reg->writefn = el2_e2h_write;
6057     }
6058 }
6059 #endif
6060 
6061 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6062                                      bool isread)
6063 {
6064     int cur_el = arm_current_el(env);
6065 
6066     if (cur_el < 2) {
6067         uint64_t hcr = arm_hcr_el2_eff(env);
6068 
6069         if (cur_el == 0) {
6070             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6071                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6072                     return CP_ACCESS_TRAP_EL2;
6073                 }
6074             } else {
6075                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6076                     return CP_ACCESS_TRAP;
6077                 }
6078                 if (hcr & HCR_TID2) {
6079                     return CP_ACCESS_TRAP_EL2;
6080                 }
6081             }
6082         } else if (hcr & HCR_TID2) {
6083             return CP_ACCESS_TRAP_EL2;
6084         }
6085     }
6086 
6087     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6088         return CP_ACCESS_TRAP_EL2;
6089     }
6090 
6091     return CP_ACCESS_OK;
6092 }
6093 
6094 /*
6095  * Check for traps to RAS registers, which are controlled
6096  * by HCR_EL2.TERR and SCR_EL3.TERR.
6097  */
6098 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6099                                   bool isread)
6100 {
6101     int el = arm_current_el(env);
6102 
6103     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6104         return CP_ACCESS_TRAP_EL2;
6105     }
6106     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6107         return CP_ACCESS_TRAP_EL3;
6108     }
6109     return CP_ACCESS_OK;
6110 }
6111 
6112 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6113 {
6114     int el = arm_current_el(env);
6115 
6116     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6117         return env->cp15.vdisr_el2;
6118     }
6119     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6120         return 0; /* RAZ/WI */
6121     }
6122     return env->cp15.disr_el1;
6123 }
6124 
6125 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6126 {
6127     int el = arm_current_el(env);
6128 
6129     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6130         env->cp15.vdisr_el2 = val;
6131         return;
6132     }
6133     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6134         return; /* RAZ/WI */
6135     }
6136     env->cp15.disr_el1 = val;
6137 }
6138 
6139 /*
6140  * Minimal RAS implementation with no Error Records.
6141  * Which means that all of the Error Record registers:
6142  *   ERXADDR_EL1
6143  *   ERXCTLR_EL1
6144  *   ERXFR_EL1
6145  *   ERXMISC0_EL1
6146  *   ERXMISC1_EL1
6147  *   ERXMISC2_EL1
6148  *   ERXMISC3_EL1
6149  *   ERXPFGCDN_EL1  (RASv1p1)
6150  *   ERXPFGCTL_EL1  (RASv1p1)
6151  *   ERXPFGF_EL1    (RASv1p1)
6152  *   ERXSTATUS_EL1
6153  * and
6154  *   ERRSELR_EL1
6155  * may generate UNDEFINED, which is the effect we get by not
6156  * listing them at all.
6157  *
6158  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6159  * is higher priority than FGT-to-EL2 so we do not need to list them
6160  * in order to check for an FGT.
6161  */
6162 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6163     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6164       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6165       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6166       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6167     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6168       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6169       .access = PL1_R, .accessfn = access_terr,
6170       .fgt = FGT_ERRIDR_EL1,
6171       .type = ARM_CP_CONST, .resetvalue = 0 },
6172     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6173       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6174       .nv2_redirect_offset = 0x500,
6175       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6176     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6177       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6178       .nv2_redirect_offset = 0x508,
6179       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6180 };
6181 
6182 /*
6183  * Return the exception level to which exceptions should be taken
6184  * via SVEAccessTrap.  This excludes the check for whether the exception
6185  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6186  * be found by testing 0 < fp_exception_el < sve_exception_el.
6187  *
6188  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6189  * pseudocode does *not* separate out the FP trap checks, but has them
6190  * all in one function.
6191  */
6192 int sve_exception_el(CPUARMState *env, int el)
6193 {
6194 #ifndef CONFIG_USER_ONLY
6195     if (el <= 1 && !el_is_in_host(env, el)) {
6196         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6197         case 1:
6198             if (el != 0) {
6199                 break;
6200             }
6201             /* fall through */
6202         case 0:
6203         case 2:
6204             return 1;
6205         }
6206     }
6207 
6208     if (el <= 2 && arm_is_el2_enabled(env)) {
6209         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6210         if (env->cp15.hcr_el2 & HCR_E2H) {
6211             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6212             case 1:
6213                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6214                     break;
6215                 }
6216                 /* fall through */
6217             case 0:
6218             case 2:
6219                 return 2;
6220             }
6221         } else {
6222             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6223                 return 2;
6224             }
6225         }
6226     }
6227 
6228     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6229     if (arm_feature(env, ARM_FEATURE_EL3)
6230         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6231         return 3;
6232     }
6233 #endif
6234     return 0;
6235 }
6236 
6237 /*
6238  * Return the exception level to which exceptions should be taken for SME.
6239  * C.f. the ARM pseudocode function CheckSMEAccess.
6240  */
6241 int sme_exception_el(CPUARMState *env, int el)
6242 {
6243 #ifndef CONFIG_USER_ONLY
6244     if (el <= 1 && !el_is_in_host(env, el)) {
6245         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6246         case 1:
6247             if (el != 0) {
6248                 break;
6249             }
6250             /* fall through */
6251         case 0:
6252         case 2:
6253             return 1;
6254         }
6255     }
6256 
6257     if (el <= 2 && arm_is_el2_enabled(env)) {
6258         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6259         if (env->cp15.hcr_el2 & HCR_E2H) {
6260             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6261             case 1:
6262                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6263                     break;
6264                 }
6265                 /* fall through */
6266             case 0:
6267             case 2:
6268                 return 2;
6269             }
6270         } else {
6271             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6272                 return 2;
6273             }
6274         }
6275     }
6276 
6277     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6278     if (arm_feature(env, ARM_FEATURE_EL3)
6279         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6280         return 3;
6281     }
6282 #endif
6283     return 0;
6284 }
6285 
6286 /*
6287  * Given that SVE is enabled, return the vector length for EL.
6288  */
6289 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6290 {
6291     ARMCPU *cpu = env_archcpu(env);
6292     uint64_t *cr = env->vfp.zcr_el;
6293     uint32_t map = cpu->sve_vq.map;
6294     uint32_t len = ARM_MAX_VQ - 1;
6295 
6296     if (sm) {
6297         cr = env->vfp.smcr_el;
6298         map = cpu->sme_vq.map;
6299     }
6300 
6301     if (el <= 1 && !el_is_in_host(env, el)) {
6302         len = MIN(len, 0xf & (uint32_t)cr[1]);
6303     }
6304     if (el <= 2 && arm_is_el2_enabled(env)) {
6305         len = MIN(len, 0xf & (uint32_t)cr[2]);
6306     }
6307     if (arm_feature(env, ARM_FEATURE_EL3)) {
6308         len = MIN(len, 0xf & (uint32_t)cr[3]);
6309     }
6310 
6311     map &= MAKE_64BIT_MASK(0, len + 1);
6312     if (map != 0) {
6313         return 31 - clz32(map);
6314     }
6315 
6316     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6317     assert(sm);
6318     return ctz32(cpu->sme_vq.map);
6319 }
6320 
6321 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6322 {
6323     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6324 }
6325 
6326 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6327                       uint64_t value)
6328 {
6329     int cur_el = arm_current_el(env);
6330     int old_len = sve_vqm1_for_el(env, cur_el);
6331     int new_len;
6332 
6333     /* Bits other than [3:0] are RAZ/WI.  */
6334     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6335     raw_write(env, ri, value & 0xf);
6336 
6337     /*
6338      * Because we arrived here, we know both FP and SVE are enabled;
6339      * otherwise we would have trapped access to the ZCR_ELn register.
6340      */
6341     new_len = sve_vqm1_for_el(env, cur_el);
6342     if (new_len < old_len) {
6343         aarch64_sve_narrow_vq(env, new_len + 1);
6344     }
6345 }
6346 
6347 static const ARMCPRegInfo zcr_reginfo[] = {
6348     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6349       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6350       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6351       .access = PL1_RW, .type = ARM_CP_SVE,
6352       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6353       .writefn = zcr_write, .raw_writefn = raw_write },
6354     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6355       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6356       .access = PL2_RW, .type = ARM_CP_SVE,
6357       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6358       .writefn = zcr_write, .raw_writefn = raw_write },
6359     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6360       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6361       .access = PL3_RW, .type = ARM_CP_SVE,
6362       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6363       .writefn = zcr_write, .raw_writefn = raw_write },
6364 };
6365 
6366 #ifdef TARGET_AARCH64
6367 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6368                                     bool isread)
6369 {
6370     int el = arm_current_el(env);
6371 
6372     if (el == 0) {
6373         uint64_t sctlr = arm_sctlr(env, el);
6374         if (!(sctlr & SCTLR_EnTP2)) {
6375             return CP_ACCESS_TRAP;
6376         }
6377     }
6378     /* TODO: FEAT_FGT */
6379     if (el < 3
6380         && arm_feature(env, ARM_FEATURE_EL3)
6381         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6382         return CP_ACCESS_TRAP_EL3;
6383     }
6384     return CP_ACCESS_OK;
6385 }
6386 
6387 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6388                                       bool isread)
6389 {
6390     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6391     if (arm_current_el(env) == 2
6392         && arm_feature(env, ARM_FEATURE_EL3)
6393         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6394         return CP_ACCESS_TRAP_EL3;
6395     }
6396     return CP_ACCESS_OK;
6397 }
6398 
6399 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6400                                    bool isread)
6401 {
6402     if (arm_current_el(env) < 3
6403         && arm_feature(env, ARM_FEATURE_EL3)
6404         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6405         return CP_ACCESS_TRAP_EL3;
6406     }
6407     return CP_ACCESS_OK;
6408 }
6409 
6410 /* ResetSVEState */
6411 static void arm_reset_sve_state(CPUARMState *env)
6412 {
6413     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6414     /* Recall that FFR is stored as pregs[16]. */
6415     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6416     vfp_set_fpsr(env, 0x0800009f);
6417 }
6418 
6419 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6420 {
6421     uint64_t change = (env->svcr ^ new) & mask;
6422 
6423     if (change == 0) {
6424         return;
6425     }
6426     env->svcr ^= change;
6427 
6428     if (change & R_SVCR_SM_MASK) {
6429         arm_reset_sve_state(env);
6430     }
6431 
6432     /*
6433      * ResetSMEState.
6434      *
6435      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6436      * on enable: while disabled, the storage is inaccessible and the
6437      * value does not matter.  We're not saving the storage in vmstate
6438      * when disabled either.
6439      */
6440     if (change & new & R_SVCR_ZA_MASK) {
6441         memset(env->zarray, 0, sizeof(env->zarray));
6442     }
6443 
6444     if (tcg_enabled()) {
6445         arm_rebuild_hflags(env);
6446     }
6447 }
6448 
6449 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6450                        uint64_t value)
6451 {
6452     aarch64_set_svcr(env, value, -1);
6453 }
6454 
6455 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6456                        uint64_t value)
6457 {
6458     int cur_el = arm_current_el(env);
6459     int old_len = sve_vqm1_for_el(env, cur_el);
6460     int new_len;
6461 
6462     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6463     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6464     raw_write(env, ri, value);
6465 
6466     /*
6467      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6468      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6469      * current values for simplicity.  But for QEMU internals, we must still
6470      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6471      * above aarch64_sve_narrow_vq.
6472      */
6473     new_len = sve_vqm1_for_el(env, cur_el);
6474     if (new_len < old_len) {
6475         aarch64_sve_narrow_vq(env, new_len + 1);
6476     }
6477 }
6478 
6479 static const ARMCPRegInfo sme_reginfo[] = {
6480     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6481       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6482       .access = PL0_RW, .accessfn = access_tpidr2,
6483       .fgt = FGT_NTPIDR2_EL0,
6484       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6485     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6486       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6487       .access = PL0_RW, .type = ARM_CP_SME,
6488       .fieldoffset = offsetof(CPUARMState, svcr),
6489       .writefn = svcr_write, .raw_writefn = raw_write },
6490     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6491       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6492       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6493       .access = PL1_RW, .type = ARM_CP_SME,
6494       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6495       .writefn = smcr_write, .raw_writefn = raw_write },
6496     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6497       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6498       .access = PL2_RW, .type = ARM_CP_SME,
6499       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6500       .writefn = smcr_write, .raw_writefn = raw_write },
6501     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6502       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6503       .access = PL3_RW, .type = ARM_CP_SME,
6504       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6505       .writefn = smcr_write, .raw_writefn = raw_write },
6506     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6507       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6508       .access = PL1_R, .accessfn = access_aa64_tid1,
6509       /*
6510        * IMPLEMENTOR = 0 (software)
6511        * REVISION    = 0 (implementation defined)
6512        * SMPS        = 0 (no streaming execution priority in QEMU)
6513        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6514        */
6515       .type = ARM_CP_CONST, .resetvalue = 0, },
6516     /*
6517      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6518      */
6519     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6520       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6521       .access = PL1_RW, .accessfn = access_smpri,
6522       .fgt = FGT_NSMPRI_EL1,
6523       .type = ARM_CP_CONST, .resetvalue = 0 },
6524     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6525       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6526       .nv2_redirect_offset = 0x1f8,
6527       .access = PL2_RW, .accessfn = access_smprimap,
6528       .type = ARM_CP_CONST, .resetvalue = 0 },
6529 };
6530 
6531 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6532                         uint64_t value)
6533 {
6534     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6535     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6536         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6537         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6538 
6539     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6540 }
6541 
6542 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6543 {
6544     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6545                                      env_archcpu(env)->reset_l0gptsz);
6546 }
6547 
6548 static const ARMCPRegInfo rme_reginfo[] = {
6549     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6550       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6551       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6552       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6553     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6554       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6555       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6556     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6557       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6558       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6559     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6560       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6561       .access = PL3_W, .type = ARM_CP_NOP },
6562 };
6563 
6564 static const ARMCPRegInfo rme_mte_reginfo[] = {
6565     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6566       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6567       .access = PL3_W, .type = ARM_CP_NOP },
6568 };
6569 
6570 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6571                               uint64_t value)
6572 {
6573     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6574 }
6575 
6576 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6577 {
6578     return env->pstate & PSTATE_ALLINT;
6579 }
6580 
6581 static CPAccessResult aa64_allint_access(CPUARMState *env,
6582                                          const ARMCPRegInfo *ri, bool isread)
6583 {
6584     if (!isread && arm_current_el(env) == 1 &&
6585         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6586         return CP_ACCESS_TRAP_EL2;
6587     }
6588     return CP_ACCESS_OK;
6589 }
6590 
6591 static const ARMCPRegInfo nmi_reginfo[] = {
6592     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6593       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6594       .type = ARM_CP_NO_RAW,
6595       .access = PL1_RW, .accessfn = aa64_allint_access,
6596       .fieldoffset = offsetof(CPUARMState, pstate),
6597       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6598       .resetfn = arm_cp_reset_ignore },
6599 };
6600 #endif /* TARGET_AARCH64 */
6601 
6602 static void define_pmu_regs(ARMCPU *cpu)
6603 {
6604     /*
6605      * v7 performance monitor control register: same implementor
6606      * field as main ID register, and we implement four counters in
6607      * addition to the cycle count register.
6608      */
6609     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6610     ARMCPRegInfo pmcr = {
6611         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6612         .access = PL0_RW,
6613         .fgt = FGT_PMCR_EL0,
6614         .type = ARM_CP_IO | ARM_CP_ALIAS,
6615         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6616         .accessfn = pmreg_access,
6617         .readfn = pmcr_read, .raw_readfn = raw_read,
6618         .writefn = pmcr_write, .raw_writefn = raw_write,
6619     };
6620     ARMCPRegInfo pmcr64 = {
6621         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6622         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6623         .access = PL0_RW, .accessfn = pmreg_access,
6624         .fgt = FGT_PMCR_EL0,
6625         .type = ARM_CP_IO,
6626         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6627         .resetvalue = cpu->isar.reset_pmcr_el0,
6628         .readfn = pmcr_read, .raw_readfn = raw_read,
6629         .writefn = pmcr_write, .raw_writefn = raw_write,
6630     };
6631 
6632     define_one_arm_cp_reg(cpu, &pmcr);
6633     define_one_arm_cp_reg(cpu, &pmcr64);
6634     for (i = 0; i < pmcrn; i++) {
6635         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6636         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6637         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6638         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6639         ARMCPRegInfo pmev_regs[] = {
6640             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6641               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6642               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6643               .fgt = FGT_PMEVCNTRN_EL0,
6644               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6645               .accessfn = pmreg_access_xevcntr },
6646             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6647               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6648               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6649               .type = ARM_CP_IO,
6650               .fgt = FGT_PMEVCNTRN_EL0,
6651               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6652               .raw_readfn = pmevcntr_rawread,
6653               .raw_writefn = pmevcntr_rawwrite },
6654             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6655               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6656               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6657               .fgt = FGT_PMEVTYPERN_EL0,
6658               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6659               .accessfn = pmreg_access },
6660             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6661               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6662               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6663               .fgt = FGT_PMEVTYPERN_EL0,
6664               .type = ARM_CP_IO,
6665               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6666               .raw_writefn = pmevtyper_rawwrite },
6667         };
6668         define_arm_cp_regs(cpu, pmev_regs);
6669         g_free(pmevcntr_name);
6670         g_free(pmevcntr_el0_name);
6671         g_free(pmevtyper_name);
6672         g_free(pmevtyper_el0_name);
6673     }
6674     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6675         ARMCPRegInfo v81_pmu_regs[] = {
6676             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6677               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6678               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6679               .fgt = FGT_PMCEIDN_EL0,
6680               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6681             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6682               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6683               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6684               .fgt = FGT_PMCEIDN_EL0,
6685               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6686         };
6687         define_arm_cp_regs(cpu, v81_pmu_regs);
6688     }
6689     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6690         static const ARMCPRegInfo v84_pmmir = {
6691             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6692             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6693             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6694             .fgt = FGT_PMMIR_EL1,
6695             .resetvalue = 0
6696         };
6697         define_one_arm_cp_reg(cpu, &v84_pmmir);
6698     }
6699 }
6700 
6701 #ifndef CONFIG_USER_ONLY
6702 /*
6703  * We don't know until after realize whether there's a GICv3
6704  * attached, and that is what registers the gicv3 sysregs.
6705  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6706  * at runtime.
6707  */
6708 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6709 {
6710     ARMCPU *cpu = env_archcpu(env);
6711     uint64_t pfr1 = cpu->isar.id_pfr1;
6712 
6713     if (env->gicv3state) {
6714         pfr1 |= 1 << 28;
6715     }
6716     return pfr1;
6717 }
6718 
6719 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6720 {
6721     ARMCPU *cpu = env_archcpu(env);
6722     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6723 
6724     if (env->gicv3state) {
6725         pfr0 |= 1 << 24;
6726     }
6727     return pfr0;
6728 }
6729 #endif
6730 
6731 /*
6732  * Shared logic between LORID and the rest of the LOR* registers.
6733  * Secure state exclusion has already been dealt with.
6734  */
6735 static CPAccessResult access_lor_ns(CPUARMState *env,
6736                                     const ARMCPRegInfo *ri, bool isread)
6737 {
6738     int el = arm_current_el(env);
6739 
6740     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6741         return CP_ACCESS_TRAP_EL2;
6742     }
6743     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6744         return CP_ACCESS_TRAP_EL3;
6745     }
6746     return CP_ACCESS_OK;
6747 }
6748 
6749 static CPAccessResult access_lor_other(CPUARMState *env,
6750                                        const ARMCPRegInfo *ri, bool isread)
6751 {
6752     if (arm_is_secure_below_el3(env)) {
6753         /* Access denied in secure mode.  */
6754         return CP_ACCESS_TRAP;
6755     }
6756     return access_lor_ns(env, ri, isread);
6757 }
6758 
6759 /*
6760  * A trivial implementation of ARMv8.1-LOR leaves all of these
6761  * registers fixed at 0, which indicates that there are zero
6762  * supported Limited Ordering regions.
6763  */
6764 static const ARMCPRegInfo lor_reginfo[] = {
6765     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6766       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6767       .access = PL1_RW, .accessfn = access_lor_other,
6768       .fgt = FGT_LORSA_EL1,
6769       .type = ARM_CP_CONST, .resetvalue = 0 },
6770     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6771       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6772       .access = PL1_RW, .accessfn = access_lor_other,
6773       .fgt = FGT_LOREA_EL1,
6774       .type = ARM_CP_CONST, .resetvalue = 0 },
6775     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6776       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6777       .access = PL1_RW, .accessfn = access_lor_other,
6778       .fgt = FGT_LORN_EL1,
6779       .type = ARM_CP_CONST, .resetvalue = 0 },
6780     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6781       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6782       .access = PL1_RW, .accessfn = access_lor_other,
6783       .fgt = FGT_LORC_EL1,
6784       .type = ARM_CP_CONST, .resetvalue = 0 },
6785     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6786       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6787       .access = PL1_R, .accessfn = access_lor_ns,
6788       .fgt = FGT_LORID_EL1,
6789       .type = ARM_CP_CONST, .resetvalue = 0 },
6790 };
6791 
6792 #ifdef TARGET_AARCH64
6793 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6794                                    bool isread)
6795 {
6796     int el = arm_current_el(env);
6797 
6798     if (el < 2 &&
6799         arm_is_el2_enabled(env) &&
6800         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6801         return CP_ACCESS_TRAP_EL2;
6802     }
6803     if (el < 3 &&
6804         arm_feature(env, ARM_FEATURE_EL3) &&
6805         !(env->cp15.scr_el3 & SCR_APK)) {
6806         return CP_ACCESS_TRAP_EL3;
6807     }
6808     return CP_ACCESS_OK;
6809 }
6810 
6811 static const ARMCPRegInfo pauth_reginfo[] = {
6812     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6813       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6814       .access = PL1_RW, .accessfn = access_pauth,
6815       .fgt = FGT_APDAKEY,
6816       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6817     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6818       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6819       .access = PL1_RW, .accessfn = access_pauth,
6820       .fgt = FGT_APDAKEY,
6821       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6822     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6824       .access = PL1_RW, .accessfn = access_pauth,
6825       .fgt = FGT_APDBKEY,
6826       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6827     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6828       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6829       .access = PL1_RW, .accessfn = access_pauth,
6830       .fgt = FGT_APDBKEY,
6831       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6832     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6833       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6834       .access = PL1_RW, .accessfn = access_pauth,
6835       .fgt = FGT_APGAKEY,
6836       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6837     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6838       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6839       .access = PL1_RW, .accessfn = access_pauth,
6840       .fgt = FGT_APGAKEY,
6841       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6842     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6843       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6844       .access = PL1_RW, .accessfn = access_pauth,
6845       .fgt = FGT_APIAKEY,
6846       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6847     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6848       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6849       .access = PL1_RW, .accessfn = access_pauth,
6850       .fgt = FGT_APIAKEY,
6851       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6852     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6853       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6854       .access = PL1_RW, .accessfn = access_pauth,
6855       .fgt = FGT_APIBKEY,
6856       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6857     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6858       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6859       .access = PL1_RW, .accessfn = access_pauth,
6860       .fgt = FGT_APIBKEY,
6861       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6862 };
6863 
6864 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6865 {
6866     Error *err = NULL;
6867     uint64_t ret;
6868 
6869     /* Success sets NZCV = 0000.  */
6870     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6871 
6872     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6873         /*
6874          * ??? Failed, for unknown reasons in the crypto subsystem.
6875          * The best we can do is log the reason and return the
6876          * timed-out indication to the guest.  There is no reason
6877          * we know to expect this failure to be transitory, so the
6878          * guest may well hang retrying the operation.
6879          */
6880         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6881                       ri->name, error_get_pretty(err));
6882         error_free(err);
6883 
6884         env->ZF = 0; /* NZCF = 0100 */
6885         return 0;
6886     }
6887     return ret;
6888 }
6889 
6890 /* We do not support re-seeding, so the two registers operate the same.  */
6891 static const ARMCPRegInfo rndr_reginfo[] = {
6892     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6893       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6894       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6895       .access = PL0_R, .readfn = rndr_readfn },
6896     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6897       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6898       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6899       .access = PL0_R, .readfn = rndr_readfn },
6900 };
6901 
6902 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6903                           uint64_t value)
6904 {
6905 #ifdef CONFIG_TCG
6906     ARMCPU *cpu = env_archcpu(env);
6907     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6908     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6909     uint64_t vaddr_in = (uint64_t) value;
6910     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6911     void *haddr;
6912     int mem_idx = arm_env_mmu_index(env);
6913 
6914     /* This won't be crossing page boundaries */
6915     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6916     if (haddr) {
6917 #ifndef CONFIG_USER_ONLY
6918 
6919         ram_addr_t offset;
6920         MemoryRegion *mr;
6921 
6922         /* RCU lock is already being held */
6923         mr = memory_region_from_host(haddr, &offset);
6924 
6925         if (mr) {
6926             memory_region_writeback(mr, offset, dline_size);
6927         }
6928 #endif /*CONFIG_USER_ONLY*/
6929     }
6930 #else
6931     /* Handled by hardware accelerator. */
6932     g_assert_not_reached();
6933 #endif /* CONFIG_TCG */
6934 }
6935 
6936 static const ARMCPRegInfo dcpop_reg[] = {
6937     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6938       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6939       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6940       .fgt = FGT_DCCVAP,
6941       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6942 };
6943 
6944 static const ARMCPRegInfo dcpodp_reg[] = {
6945     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6946       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6947       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6948       .fgt = FGT_DCCVADP,
6949       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6950 };
6951 
6952 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6953                                        bool isread)
6954 {
6955     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6956         return CP_ACCESS_TRAP_EL2;
6957     }
6958 
6959     return CP_ACCESS_OK;
6960 }
6961 
6962 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6963                                  bool isread)
6964 {
6965     int el = arm_current_el(env);
6966     if (el < 2 && arm_is_el2_enabled(env)) {
6967         uint64_t hcr = arm_hcr_el2_eff(env);
6968         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6969             return CP_ACCESS_TRAP_EL2;
6970         }
6971     }
6972     if (el < 3 &&
6973         arm_feature(env, ARM_FEATURE_EL3) &&
6974         !(env->cp15.scr_el3 & SCR_ATA)) {
6975         return CP_ACCESS_TRAP_EL3;
6976     }
6977     return CP_ACCESS_OK;
6978 }
6979 
6980 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
6981                                       bool isread)
6982 {
6983     CPAccessResult nv1 = access_nv1(env, ri, isread);
6984 
6985     if (nv1 != CP_ACCESS_OK) {
6986         return nv1;
6987     }
6988     return access_mte(env, ri, isread);
6989 }
6990 
6991 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
6992                                       bool isread)
6993 {
6994     /*
6995      * TFSR_EL2: similar to generic access_mte(), but we need to
6996      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
6997      * if NV2 is enabled then we will redirect this to TFSR_EL1
6998      * after doing the HCR and SCR ATA traps; otherwise this will
6999      * be a trap to EL2 and the HCR/SCR traps do not apply.
7000      */
7001     int el = arm_current_el(env);
7002 
7003     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7004         return CP_ACCESS_OK;
7005     }
7006     if (el < 2 && arm_is_el2_enabled(env)) {
7007         uint64_t hcr = arm_hcr_el2_eff(env);
7008         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7009             return CP_ACCESS_TRAP_EL2;
7010         }
7011     }
7012     if (el < 3 &&
7013         arm_feature(env, ARM_FEATURE_EL3) &&
7014         !(env->cp15.scr_el3 & SCR_ATA)) {
7015         return CP_ACCESS_TRAP_EL3;
7016     }
7017     return CP_ACCESS_OK;
7018 }
7019 
7020 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7021 {
7022     return env->pstate & PSTATE_TCO;
7023 }
7024 
7025 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7026 {
7027     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7028 }
7029 
7030 static const ARMCPRegInfo mte_reginfo[] = {
7031     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7032       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7033       .access = PL1_RW, .accessfn = access_mte,
7034       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7035     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7036       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7037       .access = PL1_RW, .accessfn = access_tfsr_el1,
7038       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7039       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7040     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7041       .type = ARM_CP_NV2_REDIRECT,
7042       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7043       .access = PL2_RW, .accessfn = access_tfsr_el2,
7044       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7045     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7046       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7047       .access = PL3_RW,
7048       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7049     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7050       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7051       .access = PL1_RW, .accessfn = access_mte,
7052       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7053     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7054       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7055       .access = PL1_RW, .accessfn = access_mte,
7056       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7057     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7058       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7059       .type = ARM_CP_NO_RAW,
7060       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7061     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7062       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7063       .type = ARM_CP_NOP, .access = PL1_W,
7064       .fgt = FGT_DCIVAC,
7065       .accessfn = aa64_cacheop_poc_access },
7066     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7068       .fgt = FGT_DCISW,
7069       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7070     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7071       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7072       .type = ARM_CP_NOP, .access = PL1_W,
7073       .fgt = FGT_DCIVAC,
7074       .accessfn = aa64_cacheop_poc_access },
7075     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7076       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7077       .fgt = FGT_DCISW,
7078       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7079     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7081       .fgt = FGT_DCCSW,
7082       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7083     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7084       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7085       .fgt = FGT_DCCSW,
7086       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7087     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7088       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7089       .fgt = FGT_DCCISW,
7090       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7091     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7092       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7093       .fgt = FGT_DCCISW,
7094       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7095 };
7096 
7097 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7098     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7099       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7100       .type = ARM_CP_CONST, .access = PL0_RW, },
7101 };
7102 
7103 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7104     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7105       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7106       .type = ARM_CP_NOP, .access = PL0_W,
7107       .fgt = FGT_DCCVAC,
7108       .accessfn = aa64_cacheop_poc_access },
7109     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7110       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7111       .type = ARM_CP_NOP, .access = PL0_W,
7112       .fgt = FGT_DCCVAC,
7113       .accessfn = aa64_cacheop_poc_access },
7114     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7115       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7116       .type = ARM_CP_NOP, .access = PL0_W,
7117       .fgt = FGT_DCCVAP,
7118       .accessfn = aa64_cacheop_poc_access },
7119     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7121       .type = ARM_CP_NOP, .access = PL0_W,
7122       .fgt = FGT_DCCVAP,
7123       .accessfn = aa64_cacheop_poc_access },
7124     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7125       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7126       .type = ARM_CP_NOP, .access = PL0_W,
7127       .fgt = FGT_DCCVADP,
7128       .accessfn = aa64_cacheop_poc_access },
7129     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7130       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7131       .type = ARM_CP_NOP, .access = PL0_W,
7132       .fgt = FGT_DCCVADP,
7133       .accessfn = aa64_cacheop_poc_access },
7134     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7135       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7136       .type = ARM_CP_NOP, .access = PL0_W,
7137       .fgt = FGT_DCCIVAC,
7138       .accessfn = aa64_cacheop_poc_access },
7139     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7141       .type = ARM_CP_NOP, .access = PL0_W,
7142       .fgt = FGT_DCCIVAC,
7143       .accessfn = aa64_cacheop_poc_access },
7144     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7145       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7146       .access = PL0_W, .type = ARM_CP_DC_GVA,
7147 #ifndef CONFIG_USER_ONLY
7148       /* Avoid overhead of an access check that always passes in user-mode */
7149       .accessfn = aa64_zva_access,
7150       .fgt = FGT_DCZVA,
7151 #endif
7152     },
7153     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7154       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7155       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7156 #ifndef CONFIG_USER_ONLY
7157       /* Avoid overhead of an access check that always passes in user-mode */
7158       .accessfn = aa64_zva_access,
7159       .fgt = FGT_DCZVA,
7160 #endif
7161     },
7162 };
7163 
7164 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7165                                      bool isread)
7166 {
7167     uint64_t hcr = arm_hcr_el2_eff(env);
7168     int el = arm_current_el(env);
7169 
7170     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7171         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7172             if (hcr & HCR_TGE) {
7173                 return CP_ACCESS_TRAP_EL2;
7174             }
7175             return CP_ACCESS_TRAP;
7176         }
7177     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7178         return CP_ACCESS_TRAP_EL2;
7179     }
7180     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7181         return CP_ACCESS_TRAP_EL2;
7182     }
7183     if (el < 3
7184         && arm_feature(env, ARM_FEATURE_EL3)
7185         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7186         return CP_ACCESS_TRAP_EL3;
7187     }
7188     return CP_ACCESS_OK;
7189 }
7190 
7191 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7192                                          const ARMCPRegInfo *ri,
7193                                          bool isread)
7194 {
7195     CPAccessResult nv1 = access_nv1(env, ri, isread);
7196 
7197     if (nv1 != CP_ACCESS_OK) {
7198         return nv1;
7199     }
7200     return access_scxtnum(env, ri, isread);
7201 }
7202 
7203 static const ARMCPRegInfo scxtnum_reginfo[] = {
7204     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7205       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7206       .access = PL0_RW, .accessfn = access_scxtnum,
7207       .fgt = FGT_SCXTNUM_EL0,
7208       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7209     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7210       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7211       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7212       .fgt = FGT_SCXTNUM_EL1,
7213       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7214       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7215     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7216       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7217       .access = PL2_RW, .accessfn = access_scxtnum,
7218       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7219     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7220       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7221       .access = PL3_RW,
7222       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7223 };
7224 
7225 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7226                                  bool isread)
7227 {
7228     if (arm_current_el(env) == 2 &&
7229         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7230         return CP_ACCESS_TRAP_EL3;
7231     }
7232     return CP_ACCESS_OK;
7233 }
7234 
7235 static const ARMCPRegInfo fgt_reginfo[] = {
7236     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7237       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7238       .nv2_redirect_offset = 0x1b8,
7239       .access = PL2_RW, .accessfn = access_fgt,
7240       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7241     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7242       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7243       .nv2_redirect_offset = 0x1c0,
7244       .access = PL2_RW, .accessfn = access_fgt,
7245       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7246     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7247       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7248       .nv2_redirect_offset = 0x1d0,
7249       .access = PL2_RW, .accessfn = access_fgt,
7250       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7251     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7252       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7253       .nv2_redirect_offset = 0x1d8,
7254       .access = PL2_RW, .accessfn = access_fgt,
7255       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7256     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7257       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7258       .nv2_redirect_offset = 0x1c8,
7259       .access = PL2_RW, .accessfn = access_fgt,
7260       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7261 };
7262 
7263 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7264                        uint64_t value)
7265 {
7266     /*
7267      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7268      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7269      * about the RESS bits at the top -- we choose the "generate an EL2
7270      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7271      * the ptw.c code detect the resulting invalid address).
7272      */
7273     env->cp15.vncr_el2 = value & ~0xfffULL;
7274 }
7275 
7276 static const ARMCPRegInfo nv2_reginfo[] = {
7277     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7278       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7279       .access = PL2_RW,
7280       .writefn = vncr_write,
7281       .nv2_redirect_offset = 0xb0,
7282       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7283 };
7284 
7285 #endif /* TARGET_AARCH64 */
7286 
7287 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7288                                      bool isread)
7289 {
7290     int el = arm_current_el(env);
7291 
7292     if (el == 0) {
7293         uint64_t sctlr = arm_sctlr(env, el);
7294         if (!(sctlr & SCTLR_EnRCTX)) {
7295             return CP_ACCESS_TRAP;
7296         }
7297     } else if (el == 1) {
7298         uint64_t hcr = arm_hcr_el2_eff(env);
7299         if (hcr & HCR_NV) {
7300             return CP_ACCESS_TRAP_EL2;
7301         }
7302     }
7303     return CP_ACCESS_OK;
7304 }
7305 
7306 static const ARMCPRegInfo predinv_reginfo[] = {
7307     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7308       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7309       .fgt = FGT_CFPRCTX,
7310       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7311     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7312       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7313       .fgt = FGT_DVPRCTX,
7314       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7315     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7316       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7317       .fgt = FGT_CPPRCTX,
7318       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7319     /*
7320      * Note the AArch32 opcodes have a different OPC1.
7321      */
7322     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7323       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7324       .fgt = FGT_CFPRCTX,
7325       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7326     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7327       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7328       .fgt = FGT_DVPRCTX,
7329       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7330     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7331       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7332       .fgt = FGT_CPPRCTX,
7333       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7334 };
7335 
7336 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7337 {
7338     /* Read the high 32 bits of the current CCSIDR */
7339     return extract64(ccsidr_read(env, ri), 32, 32);
7340 }
7341 
7342 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7343     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7344       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7345       .access = PL1_R,
7346       .accessfn = access_tid4,
7347       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7348 };
7349 
7350 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7351                                        bool isread)
7352 {
7353     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7354         return CP_ACCESS_TRAP_EL2;
7355     }
7356 
7357     return CP_ACCESS_OK;
7358 }
7359 
7360 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7361                                        bool isread)
7362 {
7363     if (arm_feature(env, ARM_FEATURE_V8)) {
7364         return access_aa64_tid3(env, ri, isread);
7365     }
7366 
7367     return CP_ACCESS_OK;
7368 }
7369 
7370 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7371                                      bool isread)
7372 {
7373     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7374         return CP_ACCESS_TRAP_EL2;
7375     }
7376 
7377     return CP_ACCESS_OK;
7378 }
7379 
7380 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7381                                         const ARMCPRegInfo *ri, bool isread)
7382 {
7383     /*
7384      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7385      * in v7A, not in v8A.
7386      */
7387     if (!arm_feature(env, ARM_FEATURE_V8) &&
7388         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7389         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7390         return CP_ACCESS_TRAP_EL2;
7391     }
7392     return CP_ACCESS_OK;
7393 }
7394 
7395 static const ARMCPRegInfo jazelle_regs[] = {
7396     { .name = "JIDR",
7397       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7398       .access = PL1_R, .accessfn = access_jazelle,
7399       .type = ARM_CP_CONST, .resetvalue = 0 },
7400     { .name = "JOSCR",
7401       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7402       .accessfn = access_joscr_jmcr,
7403       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7404     { .name = "JMCR",
7405       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7406       .accessfn = access_joscr_jmcr,
7407       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7408 };
7409 
7410 static const ARMCPRegInfo contextidr_el2 = {
7411     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7412     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7413     .access = PL2_RW,
7414     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7415 };
7416 
7417 static const ARMCPRegInfo vhe_reginfo[] = {
7418     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7419       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7420       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7421       .raw_writefn = raw_write,
7422       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7423 #ifndef CONFIG_USER_ONLY
7424     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7425       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7426       .fieldoffset =
7427         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7428       .type = ARM_CP_IO, .access = PL2_RW,
7429       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7430     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7431       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7432       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7433       .resetfn = gt_hv_timer_reset,
7434       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7435     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7436       .type = ARM_CP_IO,
7437       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7438       .access = PL2_RW,
7439       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7440       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7441     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7443       .type = ARM_CP_IO | ARM_CP_ALIAS,
7444       .access = PL2_RW, .accessfn = access_el1nvpct,
7445       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7446       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7447       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7448     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7449       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7450       .type = ARM_CP_IO | ARM_CP_ALIAS,
7451       .access = PL2_RW, .accessfn = access_el1nvvct,
7452       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7453       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7454       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7455     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7456       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7457       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7458       .access = PL2_RW, .accessfn = e2h_access,
7459       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7460     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7461       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7462       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7463       .access = PL2_RW, .accessfn = e2h_access,
7464       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7465     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7466       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7467       .type = ARM_CP_IO | ARM_CP_ALIAS,
7468       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7469       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7470       .access = PL2_RW, .accessfn = access_el1nvpct,
7471       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7472     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7473       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7474       .type = ARM_CP_IO | ARM_CP_ALIAS,
7475       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7476       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7477       .access = PL2_RW, .accessfn = access_el1nvvct,
7478       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7479 #endif
7480 };
7481 
7482 #ifndef CONFIG_USER_ONLY
7483 static const ARMCPRegInfo ats1e1_reginfo[] = {
7484     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7485       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7486       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7487       .fgt = FGT_ATS1E1RP,
7488       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7489     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7490       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7491       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7492       .fgt = FGT_ATS1E1WP,
7493       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7494 };
7495 
7496 static const ARMCPRegInfo ats1cp_reginfo[] = {
7497     { .name = "ATS1CPRP",
7498       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7499       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7500       .writefn = ats_write },
7501     { .name = "ATS1CPWP",
7502       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7503       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7504       .writefn = ats_write },
7505 };
7506 #endif
7507 
7508 /*
7509  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7510  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7511  * is non-zero, which is never for ARMv7, optionally in ARMv8
7512  * and mandatorily for ARMv8.2 and up.
7513  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7514  * implementation is RAZ/WI we can ignore this detail, as we
7515  * do for ACTLR.
7516  */
7517 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7518     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7519       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7520       .access = PL1_RW, .accessfn = access_tacr,
7521       .type = ARM_CP_CONST, .resetvalue = 0 },
7522     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7523       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7524       .access = PL2_RW, .type = ARM_CP_CONST,
7525       .resetvalue = 0 },
7526 };
7527 
7528 void register_cp_regs_for_features(ARMCPU *cpu)
7529 {
7530     /* Register all the coprocessor registers based on feature bits */
7531     CPUARMState *env = &cpu->env;
7532     if (arm_feature(env, ARM_FEATURE_M)) {
7533         /* M profile has no coprocessor registers */
7534         return;
7535     }
7536 
7537     define_arm_cp_regs(cpu, cp_reginfo);
7538     if (!arm_feature(env, ARM_FEATURE_V8)) {
7539         /*
7540          * Must go early as it is full of wildcards that may be
7541          * overridden by later definitions.
7542          */
7543         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7544     }
7545 
7546     define_tlb_insn_regs(cpu);
7547 
7548     if (arm_feature(env, ARM_FEATURE_V6)) {
7549         /* The ID registers all have impdef reset values */
7550         ARMCPRegInfo v6_idregs[] = {
7551             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7552               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7553               .access = PL1_R, .type = ARM_CP_CONST,
7554               .accessfn = access_aa32_tid3,
7555               .resetvalue = cpu->isar.id_pfr0 },
7556             /*
7557              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7558              * the value of the GIC field until after we define these regs.
7559              */
7560             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7561               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7562               .access = PL1_R, .type = ARM_CP_NO_RAW,
7563               .accessfn = access_aa32_tid3,
7564 #ifdef CONFIG_USER_ONLY
7565               .type = ARM_CP_CONST,
7566               .resetvalue = cpu->isar.id_pfr1,
7567 #else
7568               .type = ARM_CP_NO_RAW,
7569               .accessfn = access_aa32_tid3,
7570               .readfn = id_pfr1_read,
7571               .writefn = arm_cp_write_ignore
7572 #endif
7573             },
7574             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7575               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7576               .access = PL1_R, .type = ARM_CP_CONST,
7577               .accessfn = access_aa32_tid3,
7578               .resetvalue = cpu->isar.id_dfr0 },
7579             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7580               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7581               .access = PL1_R, .type = ARM_CP_CONST,
7582               .accessfn = access_aa32_tid3,
7583               .resetvalue = cpu->id_afr0 },
7584             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7585               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7586               .access = PL1_R, .type = ARM_CP_CONST,
7587               .accessfn = access_aa32_tid3,
7588               .resetvalue = cpu->isar.id_mmfr0 },
7589             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7590               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7591               .access = PL1_R, .type = ARM_CP_CONST,
7592               .accessfn = access_aa32_tid3,
7593               .resetvalue = cpu->isar.id_mmfr1 },
7594             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7595               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7596               .access = PL1_R, .type = ARM_CP_CONST,
7597               .accessfn = access_aa32_tid3,
7598               .resetvalue = cpu->isar.id_mmfr2 },
7599             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7600               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7601               .access = PL1_R, .type = ARM_CP_CONST,
7602               .accessfn = access_aa32_tid3,
7603               .resetvalue = cpu->isar.id_mmfr3 },
7604             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7605               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7606               .access = PL1_R, .type = ARM_CP_CONST,
7607               .accessfn = access_aa32_tid3,
7608               .resetvalue = cpu->isar.id_isar0 },
7609             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7610               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7611               .access = PL1_R, .type = ARM_CP_CONST,
7612               .accessfn = access_aa32_tid3,
7613               .resetvalue = cpu->isar.id_isar1 },
7614             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7615               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7616               .access = PL1_R, .type = ARM_CP_CONST,
7617               .accessfn = access_aa32_tid3,
7618               .resetvalue = cpu->isar.id_isar2 },
7619             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7620               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7621               .access = PL1_R, .type = ARM_CP_CONST,
7622               .accessfn = access_aa32_tid3,
7623               .resetvalue = cpu->isar.id_isar3 },
7624             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7625               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7626               .access = PL1_R, .type = ARM_CP_CONST,
7627               .accessfn = access_aa32_tid3,
7628               .resetvalue = cpu->isar.id_isar4 },
7629             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7630               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7631               .access = PL1_R, .type = ARM_CP_CONST,
7632               .accessfn = access_aa32_tid3,
7633               .resetvalue = cpu->isar.id_isar5 },
7634             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7635               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7636               .access = PL1_R, .type = ARM_CP_CONST,
7637               .accessfn = access_aa32_tid3,
7638               .resetvalue = cpu->isar.id_mmfr4 },
7639             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7640               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7641               .access = PL1_R, .type = ARM_CP_CONST,
7642               .accessfn = access_aa32_tid3,
7643               .resetvalue = cpu->isar.id_isar6 },
7644         };
7645         define_arm_cp_regs(cpu, v6_idregs);
7646         define_arm_cp_regs(cpu, v6_cp_reginfo);
7647     } else {
7648         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7649     }
7650     if (arm_feature(env, ARM_FEATURE_V6K)) {
7651         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7652     }
7653     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7654         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7655     }
7656     if (arm_feature(env, ARM_FEATURE_V7)) {
7657         ARMCPRegInfo clidr = {
7658             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7659             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7660             .access = PL1_R, .type = ARM_CP_CONST,
7661             .accessfn = access_tid4,
7662             .fgt = FGT_CLIDR_EL1,
7663             .resetvalue = cpu->clidr
7664         };
7665         define_one_arm_cp_reg(cpu, &clidr);
7666         define_arm_cp_regs(cpu, v7_cp_reginfo);
7667         define_debug_regs(cpu);
7668         define_pmu_regs(cpu);
7669     } else {
7670         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7671     }
7672     if (arm_feature(env, ARM_FEATURE_V8)) {
7673         /*
7674          * v8 ID registers, which all have impdef reset values.
7675          * Note that within the ID register ranges the unused slots
7676          * must all RAZ, not UNDEF; future architecture versions may
7677          * define new registers here.
7678          * ID registers which are AArch64 views of the AArch32 ID registers
7679          * which already existed in v6 and v7 are handled elsewhere,
7680          * in v6_idregs[].
7681          */
7682         int i;
7683         ARMCPRegInfo v8_idregs[] = {
7684             /*
7685              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7686              * emulation because we don't know the right value for the
7687              * GIC field until after we define these regs.
7688              */
7689             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7690               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7691               .access = PL1_R,
7692 #ifdef CONFIG_USER_ONLY
7693               .type = ARM_CP_CONST,
7694               .resetvalue = cpu->isar.id_aa64pfr0
7695 #else
7696               .type = ARM_CP_NO_RAW,
7697               .accessfn = access_aa64_tid3,
7698               .readfn = id_aa64pfr0_read,
7699               .writefn = arm_cp_write_ignore
7700 #endif
7701             },
7702             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7703               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7704               .access = PL1_R, .type = ARM_CP_CONST,
7705               .accessfn = access_aa64_tid3,
7706               .resetvalue = cpu->isar.id_aa64pfr1},
7707             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7708               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7709               .access = PL1_R, .type = ARM_CP_CONST,
7710               .accessfn = access_aa64_tid3,
7711               .resetvalue = 0 },
7712             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7713               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7714               .access = PL1_R, .type = ARM_CP_CONST,
7715               .accessfn = access_aa64_tid3,
7716               .resetvalue = 0 },
7717             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7718               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7719               .access = PL1_R, .type = ARM_CP_CONST,
7720               .accessfn = access_aa64_tid3,
7721               .resetvalue = cpu->isar.id_aa64zfr0 },
7722             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7723               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7724               .access = PL1_R, .type = ARM_CP_CONST,
7725               .accessfn = access_aa64_tid3,
7726               .resetvalue = cpu->isar.id_aa64smfr0 },
7727             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7728               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7729               .access = PL1_R, .type = ARM_CP_CONST,
7730               .accessfn = access_aa64_tid3,
7731               .resetvalue = 0 },
7732             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7733               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7734               .access = PL1_R, .type = ARM_CP_CONST,
7735               .accessfn = access_aa64_tid3,
7736               .resetvalue = 0 },
7737             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7738               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7739               .access = PL1_R, .type = ARM_CP_CONST,
7740               .accessfn = access_aa64_tid3,
7741               .resetvalue = cpu->isar.id_aa64dfr0 },
7742             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7743               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7744               .access = PL1_R, .type = ARM_CP_CONST,
7745               .accessfn = access_aa64_tid3,
7746               .resetvalue = cpu->isar.id_aa64dfr1 },
7747             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7748               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7749               .access = PL1_R, .type = ARM_CP_CONST,
7750               .accessfn = access_aa64_tid3,
7751               .resetvalue = 0 },
7752             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7753               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7754               .access = PL1_R, .type = ARM_CP_CONST,
7755               .accessfn = access_aa64_tid3,
7756               .resetvalue = 0 },
7757             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7758               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7759               .access = PL1_R, .type = ARM_CP_CONST,
7760               .accessfn = access_aa64_tid3,
7761               .resetvalue = cpu->id_aa64afr0 },
7762             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7763               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7764               .access = PL1_R, .type = ARM_CP_CONST,
7765               .accessfn = access_aa64_tid3,
7766               .resetvalue = cpu->id_aa64afr1 },
7767             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7768               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7769               .access = PL1_R, .type = ARM_CP_CONST,
7770               .accessfn = access_aa64_tid3,
7771               .resetvalue = 0 },
7772             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7773               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7774               .access = PL1_R, .type = ARM_CP_CONST,
7775               .accessfn = access_aa64_tid3,
7776               .resetvalue = 0 },
7777             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7778               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7779               .access = PL1_R, .type = ARM_CP_CONST,
7780               .accessfn = access_aa64_tid3,
7781               .resetvalue = cpu->isar.id_aa64isar0 },
7782             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7783               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7784               .access = PL1_R, .type = ARM_CP_CONST,
7785               .accessfn = access_aa64_tid3,
7786               .resetvalue = cpu->isar.id_aa64isar1 },
7787             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
7788               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7789               .access = PL1_R, .type = ARM_CP_CONST,
7790               .accessfn = access_aa64_tid3,
7791               .resetvalue = cpu->isar.id_aa64isar2 },
7792             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7793               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7794               .access = PL1_R, .type = ARM_CP_CONST,
7795               .accessfn = access_aa64_tid3,
7796               .resetvalue = 0 },
7797             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7798               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7799               .access = PL1_R, .type = ARM_CP_CONST,
7800               .accessfn = access_aa64_tid3,
7801               .resetvalue = 0 },
7802             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7803               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7804               .access = PL1_R, .type = ARM_CP_CONST,
7805               .accessfn = access_aa64_tid3,
7806               .resetvalue = 0 },
7807             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7808               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7809               .access = PL1_R, .type = ARM_CP_CONST,
7810               .accessfn = access_aa64_tid3,
7811               .resetvalue = 0 },
7812             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7813               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7814               .access = PL1_R, .type = ARM_CP_CONST,
7815               .accessfn = access_aa64_tid3,
7816               .resetvalue = 0 },
7817             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7818               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7819               .access = PL1_R, .type = ARM_CP_CONST,
7820               .accessfn = access_aa64_tid3,
7821               .resetvalue = cpu->isar.id_aa64mmfr0 },
7822             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7823               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7824               .access = PL1_R, .type = ARM_CP_CONST,
7825               .accessfn = access_aa64_tid3,
7826               .resetvalue = cpu->isar.id_aa64mmfr1 },
7827             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7828               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7829               .access = PL1_R, .type = ARM_CP_CONST,
7830               .accessfn = access_aa64_tid3,
7831               .resetvalue = cpu->isar.id_aa64mmfr2 },
7832             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
7833               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7834               .access = PL1_R, .type = ARM_CP_CONST,
7835               .accessfn = access_aa64_tid3,
7836               .resetvalue = cpu->isar.id_aa64mmfr3 },
7837             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7838               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7839               .access = PL1_R, .type = ARM_CP_CONST,
7840               .accessfn = access_aa64_tid3,
7841               .resetvalue = 0 },
7842             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7843               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7844               .access = PL1_R, .type = ARM_CP_CONST,
7845               .accessfn = access_aa64_tid3,
7846               .resetvalue = 0 },
7847             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7848               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7849               .access = PL1_R, .type = ARM_CP_CONST,
7850               .accessfn = access_aa64_tid3,
7851               .resetvalue = 0 },
7852             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7853               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7854               .access = PL1_R, .type = ARM_CP_CONST,
7855               .accessfn = access_aa64_tid3,
7856               .resetvalue = 0 },
7857             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7858               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7859               .access = PL1_R, .type = ARM_CP_CONST,
7860               .accessfn = access_aa64_tid3,
7861               .resetvalue = cpu->isar.mvfr0 },
7862             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7863               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7864               .access = PL1_R, .type = ARM_CP_CONST,
7865               .accessfn = access_aa64_tid3,
7866               .resetvalue = cpu->isar.mvfr1 },
7867             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7868               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7869               .access = PL1_R, .type = ARM_CP_CONST,
7870               .accessfn = access_aa64_tid3,
7871               .resetvalue = cpu->isar.mvfr2 },
7872             /*
7873              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7874              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7875              * as RAZ, since it is in the "reserved for future ID
7876              * registers, RAZ" part of the AArch32 encoding space.
7877              */
7878             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7879               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7880               .access = PL1_R, .type = ARM_CP_CONST,
7881               .accessfn = access_aa64_tid3,
7882               .resetvalue = 0 },
7883             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7884               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7885               .access = PL1_R, .type = ARM_CP_CONST,
7886               .accessfn = access_aa64_tid3,
7887               .resetvalue = 0 },
7888             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7889               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7890               .access = PL1_R, .type = ARM_CP_CONST,
7891               .accessfn = access_aa64_tid3,
7892               .resetvalue = 0 },
7893             /*
7894              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7895              * they're also RAZ for AArch64, and in v8 are gradually
7896              * being filled with AArch64-view-of-AArch32-ID-register
7897              * for new ID registers.
7898              */
7899             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7900               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7901               .access = PL1_R, .type = ARM_CP_CONST,
7902               .accessfn = access_aa64_tid3,
7903               .resetvalue = 0 },
7904             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7905               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7906               .access = PL1_R, .type = ARM_CP_CONST,
7907               .accessfn = access_aa64_tid3,
7908               .resetvalue = cpu->isar.id_pfr2 },
7909             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7910               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7911               .access = PL1_R, .type = ARM_CP_CONST,
7912               .accessfn = access_aa64_tid3,
7913               .resetvalue = cpu->isar.id_dfr1 },
7914             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7915               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7916               .access = PL1_R, .type = ARM_CP_CONST,
7917               .accessfn = access_aa64_tid3,
7918               .resetvalue = cpu->isar.id_mmfr5 },
7919             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7920               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7921               .access = PL1_R, .type = ARM_CP_CONST,
7922               .accessfn = access_aa64_tid3,
7923               .resetvalue = 0 },
7924             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7925               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7926               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7927               .fgt = FGT_PMCEIDN_EL0,
7928               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7929             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7930               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7931               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7932               .fgt = FGT_PMCEIDN_EL0,
7933               .resetvalue = cpu->pmceid0 },
7934             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7935               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7936               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7937               .fgt = FGT_PMCEIDN_EL0,
7938               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7939             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7940               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7941               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7942               .fgt = FGT_PMCEIDN_EL0,
7943               .resetvalue = cpu->pmceid1 },
7944         };
7945 #ifdef CONFIG_USER_ONLY
7946         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7947             { .name = "ID_AA64PFR0_EL1",
7948               .exported_bits = R_ID_AA64PFR0_FP_MASK |
7949                                R_ID_AA64PFR0_ADVSIMD_MASK |
7950                                R_ID_AA64PFR0_SVE_MASK |
7951                                R_ID_AA64PFR0_DIT_MASK,
7952               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
7953                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
7954             { .name = "ID_AA64PFR1_EL1",
7955               .exported_bits = R_ID_AA64PFR1_BT_MASK |
7956                                R_ID_AA64PFR1_SSBS_MASK |
7957                                R_ID_AA64PFR1_MTE_MASK |
7958                                R_ID_AA64PFR1_SME_MASK },
7959             { .name = "ID_AA64PFR*_EL1_RESERVED",
7960               .is_glob = true },
7961             { .name = "ID_AA64ZFR0_EL1",
7962               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
7963                                R_ID_AA64ZFR0_AES_MASK |
7964                                R_ID_AA64ZFR0_BITPERM_MASK |
7965                                R_ID_AA64ZFR0_BFLOAT16_MASK |
7966                                R_ID_AA64ZFR0_B16B16_MASK |
7967                                R_ID_AA64ZFR0_SHA3_MASK |
7968                                R_ID_AA64ZFR0_SM4_MASK |
7969                                R_ID_AA64ZFR0_I8MM_MASK |
7970                                R_ID_AA64ZFR0_F32MM_MASK |
7971                                R_ID_AA64ZFR0_F64MM_MASK },
7972             { .name = "ID_AA64SMFR0_EL1",
7973               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
7974                                R_ID_AA64SMFR0_BI32I32_MASK |
7975                                R_ID_AA64SMFR0_B16F32_MASK |
7976                                R_ID_AA64SMFR0_F16F32_MASK |
7977                                R_ID_AA64SMFR0_I8I32_MASK |
7978                                R_ID_AA64SMFR0_F16F16_MASK |
7979                                R_ID_AA64SMFR0_B16B16_MASK |
7980                                R_ID_AA64SMFR0_I16I32_MASK |
7981                                R_ID_AA64SMFR0_F64F64_MASK |
7982                                R_ID_AA64SMFR0_I16I64_MASK |
7983                                R_ID_AA64SMFR0_SMEVER_MASK |
7984                                R_ID_AA64SMFR0_FA64_MASK },
7985             { .name = "ID_AA64MMFR0_EL1",
7986               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
7987               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
7988                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
7989             { .name = "ID_AA64MMFR1_EL1",
7990               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
7991             { .name = "ID_AA64MMFR2_EL1",
7992               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
7993             { .name = "ID_AA64MMFR3_EL1",
7994               .exported_bits = 0 },
7995             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7996               .is_glob = true },
7997             { .name = "ID_AA64DFR0_EL1",
7998               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
7999             { .name = "ID_AA64DFR1_EL1" },
8000             { .name = "ID_AA64DFR*_EL1_RESERVED",
8001               .is_glob = true },
8002             { .name = "ID_AA64AFR*",
8003               .is_glob = true },
8004             { .name = "ID_AA64ISAR0_EL1",
8005               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8006                                R_ID_AA64ISAR0_SHA1_MASK |
8007                                R_ID_AA64ISAR0_SHA2_MASK |
8008                                R_ID_AA64ISAR0_CRC32_MASK |
8009                                R_ID_AA64ISAR0_ATOMIC_MASK |
8010                                R_ID_AA64ISAR0_RDM_MASK |
8011                                R_ID_AA64ISAR0_SHA3_MASK |
8012                                R_ID_AA64ISAR0_SM3_MASK |
8013                                R_ID_AA64ISAR0_SM4_MASK |
8014                                R_ID_AA64ISAR0_DP_MASK |
8015                                R_ID_AA64ISAR0_FHM_MASK |
8016                                R_ID_AA64ISAR0_TS_MASK |
8017                                R_ID_AA64ISAR0_RNDR_MASK },
8018             { .name = "ID_AA64ISAR1_EL1",
8019               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8020                                R_ID_AA64ISAR1_APA_MASK |
8021                                R_ID_AA64ISAR1_API_MASK |
8022                                R_ID_AA64ISAR1_JSCVT_MASK |
8023                                R_ID_AA64ISAR1_FCMA_MASK |
8024                                R_ID_AA64ISAR1_LRCPC_MASK |
8025                                R_ID_AA64ISAR1_GPA_MASK |
8026                                R_ID_AA64ISAR1_GPI_MASK |
8027                                R_ID_AA64ISAR1_FRINTTS_MASK |
8028                                R_ID_AA64ISAR1_SB_MASK |
8029                                R_ID_AA64ISAR1_BF16_MASK |
8030                                R_ID_AA64ISAR1_DGH_MASK |
8031                                R_ID_AA64ISAR1_I8MM_MASK },
8032             { .name = "ID_AA64ISAR2_EL1",
8033               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8034                                R_ID_AA64ISAR2_RPRES_MASK |
8035                                R_ID_AA64ISAR2_GPA3_MASK |
8036                                R_ID_AA64ISAR2_APA3_MASK |
8037                                R_ID_AA64ISAR2_MOPS_MASK |
8038                                R_ID_AA64ISAR2_BC_MASK |
8039                                R_ID_AA64ISAR2_RPRFM_MASK |
8040                                R_ID_AA64ISAR2_CSSC_MASK },
8041             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8042               .is_glob = true },
8043         };
8044         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8045 #endif
8046         /*
8047          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8048          * TODO: For RMR, a write with bit 1 set should do something with
8049          * cpu_reset(). In the meantime, "the bit is strictly a request",
8050          * so we are in spec just ignoring writes.
8051          */
8052         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8053             !arm_feature(env, ARM_FEATURE_EL2)) {
8054             ARMCPRegInfo el1_reset_regs[] = {
8055                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8056                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8057                   .access = PL1_R,
8058                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8059                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8060                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8061                   .access = PL1_RW, .type = ARM_CP_CONST,
8062                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8063             };
8064             define_arm_cp_regs(cpu, el1_reset_regs);
8065         }
8066         define_arm_cp_regs(cpu, v8_idregs);
8067         define_arm_cp_regs(cpu, v8_cp_reginfo);
8068         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8069             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8070         }
8071 
8072         for (i = 4; i < 16; i++) {
8073             /*
8074              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8075              * For pre-v8 cores there are RAZ patterns for these in
8076              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8077              * v8 extends the "must RAZ" part of the ID register space
8078              * to also cover c0, 0, c{8-15}, {0-7}.
8079              * These are STATE_AA32 because in the AArch64 sysreg space
8080              * c4-c7 is where the AArch64 ID registers live (and we've
8081              * already defined those in v8_idregs[]), and c8-c15 are not
8082              * "must RAZ" for AArch64.
8083              */
8084             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8085             ARMCPRegInfo v8_aa32_raz_idregs = {
8086                 .name = name,
8087                 .state = ARM_CP_STATE_AA32,
8088                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8089                 .access = PL1_R, .type = ARM_CP_CONST,
8090                 .accessfn = access_aa64_tid3,
8091                 .resetvalue = 0 };
8092             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8093         }
8094     }
8095 
8096     /*
8097      * Register the base EL2 cpregs.
8098      * Pre v8, these registers are implemented only as part of the
8099      * Virtualization Extensions (EL2 present).  Beginning with v8,
8100      * if EL2 is missing but EL3 is enabled, mostly these become
8101      * RES0 from EL3, with some specific exceptions.
8102      */
8103     if (arm_feature(env, ARM_FEATURE_EL2)
8104         || (arm_feature(env, ARM_FEATURE_EL3)
8105             && arm_feature(env, ARM_FEATURE_V8))) {
8106         uint64_t vmpidr_def = mpidr_read_val(env);
8107         ARMCPRegInfo vpidr_regs[] = {
8108             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8109               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8110               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8111               .resetvalue = cpu->midr,
8112               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8113               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8114             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8115               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8116               .access = PL2_RW, .resetvalue = cpu->midr,
8117               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8118               .nv2_redirect_offset = 0x88,
8119               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8120             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8121               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8122               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8123               .resetvalue = vmpidr_def,
8124               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8125               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8126             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8127               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8128               .access = PL2_RW, .resetvalue = vmpidr_def,
8129               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8130               .nv2_redirect_offset = 0x50,
8131               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8132         };
8133         /*
8134          * The only field of MDCR_EL2 that has a defined architectural reset
8135          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8136          */
8137         ARMCPRegInfo mdcr_el2 = {
8138             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8139             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8140             .writefn = mdcr_el2_write,
8141             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8142             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8143         };
8144         define_one_arm_cp_reg(cpu, &mdcr_el2);
8145         define_arm_cp_regs(cpu, vpidr_regs);
8146         define_arm_cp_regs(cpu, el2_cp_reginfo);
8147         if (arm_feature(env, ARM_FEATURE_V8)) {
8148             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8149         }
8150         if (cpu_isar_feature(aa64_sel2, cpu)) {
8151             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8152         }
8153         /*
8154          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8155          * See commentary near RMR_EL1.
8156          */
8157         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8158             static const ARMCPRegInfo el2_reset_regs[] = {
8159                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8160                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8161                   .access = PL2_R,
8162                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8163                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8164                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8165                   .access = PL2_R,
8166                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8167                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8168                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8169                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8170             };
8171             define_arm_cp_regs(cpu, el2_reset_regs);
8172         }
8173     }
8174 
8175     /* Register the base EL3 cpregs. */
8176     if (arm_feature(env, ARM_FEATURE_EL3)) {
8177         define_arm_cp_regs(cpu, el3_cp_reginfo);
8178         ARMCPRegInfo el3_regs[] = {
8179             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8180               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8181               .access = PL3_R,
8182               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8183             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8184               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8185               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8186             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8187               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8188               .access = PL3_RW, .type = ARM_CP_CONST,
8189               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8190             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8191               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8192               .access = PL3_RW,
8193               .raw_writefn = raw_write, .writefn = sctlr_write,
8194               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8195               .resetvalue = cpu->reset_sctlr },
8196         };
8197 
8198         define_arm_cp_regs(cpu, el3_regs);
8199     }
8200     /*
8201      * The behaviour of NSACR is sufficiently various that we don't
8202      * try to describe it in a single reginfo:
8203      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8204      *     reads as constant 0xc00 from NS EL1 and NS EL2
8205      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8206      *  if v7 without EL3, register doesn't exist
8207      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8208      */
8209     if (arm_feature(env, ARM_FEATURE_EL3)) {
8210         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8211             static const ARMCPRegInfo nsacr = {
8212                 .name = "NSACR", .type = ARM_CP_CONST,
8213                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8214                 .access = PL1_RW, .accessfn = nsacr_access,
8215                 .resetvalue = 0xc00
8216             };
8217             define_one_arm_cp_reg(cpu, &nsacr);
8218         } else {
8219             static const ARMCPRegInfo nsacr = {
8220                 .name = "NSACR",
8221                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8222                 .access = PL3_RW | PL1_R,
8223                 .resetvalue = 0,
8224                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8225             };
8226             define_one_arm_cp_reg(cpu, &nsacr);
8227         }
8228     } else {
8229         if (arm_feature(env, ARM_FEATURE_V8)) {
8230             static const ARMCPRegInfo nsacr = {
8231                 .name = "NSACR", .type = ARM_CP_CONST,
8232                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8233                 .access = PL1_R,
8234                 .resetvalue = 0xc00
8235             };
8236             define_one_arm_cp_reg(cpu, &nsacr);
8237         }
8238     }
8239 
8240     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8241         if (arm_feature(env, ARM_FEATURE_V6)) {
8242             /* PMSAv6 not implemented */
8243             assert(arm_feature(env, ARM_FEATURE_V7));
8244             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8245             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8246         } else {
8247             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8248         }
8249     } else {
8250         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8251         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8252         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8253         if (cpu_isar_feature(aa32_hpd, cpu)) {
8254             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8255         }
8256     }
8257     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8258         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8259     }
8260     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8261         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8262     }
8263     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8264         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8265     }
8266 #ifndef CONFIG_USER_ONLY
8267     if (cpu_isar_feature(aa64_ecv, cpu)) {
8268         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8269     }
8270 #endif
8271     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8272         ARMCPRegInfo vapa_cp_reginfo[] = {
8273             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8274               .access = PL1_RW, .resetvalue = 0,
8275               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8276                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8277               .writefn = par_write},
8278 #ifndef CONFIG_USER_ONLY
8279             /* This underdecoding is safe because the reginfo is NO_RAW. */
8280             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8281               .access = PL1_W, .accessfn = ats_access,
8282               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8283 #endif
8284         };
8285 
8286         /*
8287          * When LPAE exists this 32-bit PAR register is an alias of the
8288          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8289          */
8290         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8291             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8292         }
8293         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8294     }
8295     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8296         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8297     }
8298     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8299         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8300     }
8301     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8302         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8303     }
8304     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8305         define_arm_cp_regs(cpu, omap_cp_reginfo);
8306     }
8307     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8308         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8309     }
8310     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8311         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8312     }
8313     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8314         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8315     }
8316     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8317         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8318     }
8319     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8320         define_arm_cp_regs(cpu, jazelle_regs);
8321     }
8322     /*
8323      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8324      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8325      * be read-only (ie write causes UNDEF exception).
8326      */
8327     {
8328         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8329             /*
8330              * Pre-v8 MIDR space.
8331              * Note that the MIDR isn't a simple constant register because
8332              * of the TI925 behaviour where writes to another register can
8333              * cause the MIDR value to change.
8334              *
8335              * Unimplemented registers in the c15 0 0 0 space default to
8336              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8337              * and friends override accordingly.
8338              */
8339             { .name = "MIDR",
8340               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8341               .access = PL1_R, .resetvalue = cpu->midr,
8342               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8343               .readfn = midr_read,
8344               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8345               .type = ARM_CP_OVERRIDE },
8346             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8347             { .name = "DUMMY",
8348               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8349               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8350             { .name = "DUMMY",
8351               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8352               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8353             { .name = "DUMMY",
8354               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8355               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8356             { .name = "DUMMY",
8357               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8358               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8359             { .name = "DUMMY",
8360               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8361               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8362         };
8363         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8364             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8365               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8366               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8367               .fgt = FGT_MIDR_EL1,
8368               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8369               .readfn = midr_read },
8370             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8371             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8372               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8373               .access = PL1_R, .resetvalue = cpu->midr },
8374             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8375               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8376               .access = PL1_R,
8377               .accessfn = access_aa64_tid1,
8378               .fgt = FGT_REVIDR_EL1,
8379               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8380         };
8381         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8382             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8383             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8384             .access = PL1_R, .resetvalue = cpu->midr
8385         };
8386         ARMCPRegInfo id_cp_reginfo[] = {
8387             /* These are common to v8 and pre-v8 */
8388             { .name = "CTR",
8389               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8390               .access = PL1_R, .accessfn = ctr_el0_access,
8391               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8392             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8393               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8394               .access = PL0_R, .accessfn = ctr_el0_access,
8395               .fgt = FGT_CTR_EL0,
8396               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8397             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8398             { .name = "TCMTR",
8399               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8400               .access = PL1_R,
8401               .accessfn = access_aa32_tid1,
8402               .type = ARM_CP_CONST, .resetvalue = 0 },
8403         };
8404         /* TLBTR is specific to VMSA */
8405         ARMCPRegInfo id_tlbtr_reginfo = {
8406               .name = "TLBTR",
8407               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8408               .access = PL1_R,
8409               .accessfn = access_aa32_tid1,
8410               .type = ARM_CP_CONST, .resetvalue = 0,
8411         };
8412         /* MPUIR is specific to PMSA V6+ */
8413         ARMCPRegInfo id_mpuir_reginfo = {
8414               .name = "MPUIR",
8415               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8416               .access = PL1_R, .type = ARM_CP_CONST,
8417               .resetvalue = cpu->pmsav7_dregion << 8
8418         };
8419         /* HMPUIR is specific to PMSA V8 */
8420         ARMCPRegInfo id_hmpuir_reginfo = {
8421             .name = "HMPUIR",
8422             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8423             .access = PL2_R, .type = ARM_CP_CONST,
8424             .resetvalue = cpu->pmsav8r_hdregion
8425         };
8426         static const ARMCPRegInfo crn0_wi_reginfo = {
8427             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8428             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8429             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8430         };
8431 #ifdef CONFIG_USER_ONLY
8432         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8433             { .name = "MIDR_EL1",
8434               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8435                                R_MIDR_EL1_PARTNUM_MASK |
8436                                R_MIDR_EL1_ARCHITECTURE_MASK |
8437                                R_MIDR_EL1_VARIANT_MASK |
8438                                R_MIDR_EL1_IMPLEMENTER_MASK },
8439             { .name = "REVIDR_EL1" },
8440         };
8441         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8442 #endif
8443         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8444             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8445             size_t i;
8446             /*
8447              * Register the blanket "writes ignored" value first to cover the
8448              * whole space. Then update the specific ID registers to allow write
8449              * access, so that they ignore writes rather than causing them to
8450              * UNDEF.
8451              */
8452             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8453             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8454                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8455             }
8456             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8457                 id_cp_reginfo[i].access = PL1_RW;
8458             }
8459             id_mpuir_reginfo.access = PL1_RW;
8460             id_tlbtr_reginfo.access = PL1_RW;
8461         }
8462         if (arm_feature(env, ARM_FEATURE_V8)) {
8463             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8464             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8465                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8466             }
8467         } else {
8468             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8469         }
8470         define_arm_cp_regs(cpu, id_cp_reginfo);
8471         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8472             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8473         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8474                    arm_feature(env, ARM_FEATURE_V8)) {
8475             uint32_t i = 0;
8476             char *tmp_string;
8477 
8478             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8479             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8480             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8481 
8482             /* Register alias is only valid for first 32 indexes */
8483             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8484                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8485                 uint8_t opc1 = extract32(i, 4, 1);
8486                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8487 
8488                 tmp_string = g_strdup_printf("PRBAR%u", i);
8489                 ARMCPRegInfo tmp_prbarn_reginfo = {
8490                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8491                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8492                     .access = PL1_RW, .resetvalue = 0,
8493                     .accessfn = access_tvm_trvm,
8494                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8495                 };
8496                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8497                 g_free(tmp_string);
8498 
8499                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8500                 tmp_string = g_strdup_printf("PRLAR%u", i);
8501                 ARMCPRegInfo tmp_prlarn_reginfo = {
8502                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8503                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8504                     .access = PL1_RW, .resetvalue = 0,
8505                     .accessfn = access_tvm_trvm,
8506                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8507                 };
8508                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8509                 g_free(tmp_string);
8510             }
8511 
8512             /* Register alias is only valid for first 32 indexes */
8513             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8514                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8515                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8516                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8517 
8518                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8519                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8520                     .name = tmp_string,
8521                     .type = ARM_CP_NO_RAW,
8522                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8523                     .access = PL2_RW, .resetvalue = 0,
8524                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8525                 };
8526                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8527                 g_free(tmp_string);
8528 
8529                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8530                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8531                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8532                     .name = tmp_string,
8533                     .type = ARM_CP_NO_RAW,
8534                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8535                     .access = PL2_RW, .resetvalue = 0,
8536                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8537                 };
8538                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8539                 g_free(tmp_string);
8540             }
8541         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8542             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8543         }
8544     }
8545 
8546     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8547         ARMCPRegInfo mpidr_cp_reginfo[] = {
8548             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8549               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8550               .fgt = FGT_MPIDR_EL1,
8551               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8552         };
8553 #ifdef CONFIG_USER_ONLY
8554         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8555             { .name = "MPIDR_EL1",
8556               .fixed_bits = 0x0000000080000000 },
8557         };
8558         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8559 #endif
8560         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8561     }
8562 
8563     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8564         ARMCPRegInfo auxcr_reginfo[] = {
8565             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8566               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8567               .access = PL1_RW, .accessfn = access_tacr,
8568               .nv2_redirect_offset = 0x118,
8569               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8570             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8571               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8572               .access = PL2_RW, .type = ARM_CP_CONST,
8573               .resetvalue = 0 },
8574             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8575               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8576               .access = PL3_RW, .type = ARM_CP_CONST,
8577               .resetvalue = 0 },
8578         };
8579         define_arm_cp_regs(cpu, auxcr_reginfo);
8580         if (cpu_isar_feature(aa32_ac2, cpu)) {
8581             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8582         }
8583     }
8584 
8585     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8586         /*
8587          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8588          * There are two flavours:
8589          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8590          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8591          *      32-bit register visible to AArch32 at a different encoding
8592          *      to the "flavour 1" register and with the bits rearranged to
8593          *      be able to squash a 64-bit address into the 32-bit view.
8594          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8595          * in future if we support AArch32-only configs of some of the
8596          * AArch64 cores we might need to add a specific feature flag
8597          * to indicate cores with "flavour 2" CBAR.
8598          */
8599         if (arm_feature(env, ARM_FEATURE_V8)) {
8600             /* 32 bit view is [31:18] 0...0 [43:32]. */
8601             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8602                 | extract64(cpu->reset_cbar, 32, 12);
8603             ARMCPRegInfo cbar_reginfo[] = {
8604                 { .name = "CBAR",
8605                   .type = ARM_CP_CONST,
8606                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8607                   .access = PL1_R, .resetvalue = cbar32 },
8608                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8609                   .type = ARM_CP_CONST,
8610                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8611                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8612             };
8613             /* We don't implement a r/w 64 bit CBAR currently */
8614             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8615             define_arm_cp_regs(cpu, cbar_reginfo);
8616         } else {
8617             ARMCPRegInfo cbar = {
8618                 .name = "CBAR",
8619                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8620                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8621                 .fieldoffset = offsetof(CPUARMState,
8622                                         cp15.c15_config_base_address)
8623             };
8624             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8625                 cbar.access = PL1_R;
8626                 cbar.fieldoffset = 0;
8627                 cbar.type = ARM_CP_CONST;
8628             }
8629             define_one_arm_cp_reg(cpu, &cbar);
8630         }
8631     }
8632 
8633     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8634         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8635             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8636               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8637               .access = PL1_RW, .writefn = vbar_write,
8638               .accessfn = access_nv1,
8639               .fgt = FGT_VBAR_EL1,
8640               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8641               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8642                                      offsetof(CPUARMState, cp15.vbar_ns) },
8643               .resetvalue = 0 },
8644         };
8645         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8646     }
8647 
8648     /* Generic registers whose values depend on the implementation */
8649     {
8650         ARMCPRegInfo sctlr = {
8651             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8652             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8653             .access = PL1_RW, .accessfn = access_tvm_trvm,
8654             .fgt = FGT_SCTLR_EL1,
8655             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8656             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8657                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8658             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8659             .raw_writefn = raw_write,
8660         };
8661         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8662             /*
8663              * Normally we would always end the TB on an SCTLR write, but Linux
8664              * arch/arm/mach-pxa/sleep.S expects two instructions following
8665              * an MMU enable to execute from cache.  Imitate this behaviour.
8666              */
8667             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8668         }
8669         define_one_arm_cp_reg(cpu, &sctlr);
8670 
8671         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8672             arm_feature(env, ARM_FEATURE_V8)) {
8673             ARMCPRegInfo vsctlr = {
8674                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8675                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8676                 .access = PL2_RW, .resetvalue = 0x0,
8677                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8678             };
8679             define_one_arm_cp_reg(cpu, &vsctlr);
8680         }
8681     }
8682 
8683     if (cpu_isar_feature(aa64_lor, cpu)) {
8684         define_arm_cp_regs(cpu, lor_reginfo);
8685     }
8686     if (cpu_isar_feature(aa64_pan, cpu)) {
8687         define_one_arm_cp_reg(cpu, &pan_reginfo);
8688     }
8689 #ifndef CONFIG_USER_ONLY
8690     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8691         define_arm_cp_regs(cpu, ats1e1_reginfo);
8692     }
8693     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8694         define_arm_cp_regs(cpu, ats1cp_reginfo);
8695     }
8696 #endif
8697     if (cpu_isar_feature(aa64_uao, cpu)) {
8698         define_one_arm_cp_reg(cpu, &uao_reginfo);
8699     }
8700 
8701     if (cpu_isar_feature(aa64_dit, cpu)) {
8702         define_one_arm_cp_reg(cpu, &dit_reginfo);
8703     }
8704     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8705         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8706     }
8707     if (cpu_isar_feature(any_ras, cpu)) {
8708         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8709     }
8710 
8711     if (cpu_isar_feature(aa64_vh, cpu) ||
8712         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8713         define_one_arm_cp_reg(cpu, &contextidr_el2);
8714     }
8715     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8716         define_arm_cp_regs(cpu, vhe_reginfo);
8717     }
8718 
8719     if (cpu_isar_feature(aa64_sve, cpu)) {
8720         define_arm_cp_regs(cpu, zcr_reginfo);
8721     }
8722 
8723     if (cpu_isar_feature(aa64_hcx, cpu)) {
8724         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8725     }
8726 
8727 #ifdef TARGET_AARCH64
8728     if (cpu_isar_feature(aa64_sme, cpu)) {
8729         define_arm_cp_regs(cpu, sme_reginfo);
8730     }
8731     if (cpu_isar_feature(aa64_pauth, cpu)) {
8732         define_arm_cp_regs(cpu, pauth_reginfo);
8733     }
8734     if (cpu_isar_feature(aa64_rndr, cpu)) {
8735         define_arm_cp_regs(cpu, rndr_reginfo);
8736     }
8737     /* Data Cache clean instructions up to PoP */
8738     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8739         define_one_arm_cp_reg(cpu, dcpop_reg);
8740 
8741         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8742             define_one_arm_cp_reg(cpu, dcpodp_reg);
8743         }
8744     }
8745 
8746     /*
8747      * If full MTE is enabled, add all of the system registers.
8748      * If only "instructions available at EL0" are enabled,
8749      * then define only a RAZ/WI version of PSTATE.TCO.
8750      */
8751     if (cpu_isar_feature(aa64_mte, cpu)) {
8752         ARMCPRegInfo gmid_reginfo = {
8753             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8754             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8755             .access = PL1_R, .accessfn = access_aa64_tid5,
8756             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8757         };
8758         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8759         define_arm_cp_regs(cpu, mte_reginfo);
8760         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8761     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8762         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8763         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8764     }
8765 
8766     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8767         define_arm_cp_regs(cpu, scxtnum_reginfo);
8768     }
8769 
8770     if (cpu_isar_feature(aa64_fgt, cpu)) {
8771         define_arm_cp_regs(cpu, fgt_reginfo);
8772     }
8773 
8774     if (cpu_isar_feature(aa64_rme, cpu)) {
8775         define_arm_cp_regs(cpu, rme_reginfo);
8776         if (cpu_isar_feature(aa64_mte, cpu)) {
8777             define_arm_cp_regs(cpu, rme_mte_reginfo);
8778         }
8779     }
8780 
8781     if (cpu_isar_feature(aa64_nv2, cpu)) {
8782         define_arm_cp_regs(cpu, nv2_reginfo);
8783     }
8784 
8785     if (cpu_isar_feature(aa64_nmi, cpu)) {
8786         define_arm_cp_regs(cpu, nmi_reginfo);
8787     }
8788 #endif
8789 
8790     if (cpu_isar_feature(any_predinv, cpu)) {
8791         define_arm_cp_regs(cpu, predinv_reginfo);
8792     }
8793 
8794     if (cpu_isar_feature(any_ccidx, cpu)) {
8795         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8796     }
8797 
8798 #ifndef CONFIG_USER_ONLY
8799     /*
8800      * Register redirections and aliases must be done last,
8801      * after the registers from the other extensions have been defined.
8802      */
8803     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8804         define_arm_vh_e2h_redirects_aliases(cpu);
8805     }
8806 #endif
8807 }
8808 
8809 /*
8810  * Private utility function for define_one_arm_cp_reg_with_opaque():
8811  * add a single reginfo struct to the hash table.
8812  */
8813 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8814                                    void *opaque, CPState state,
8815                                    CPSecureState secstate,
8816                                    int crm, int opc1, int opc2,
8817                                    const char *name)
8818 {
8819     CPUARMState *env = &cpu->env;
8820     uint32_t key;
8821     ARMCPRegInfo *r2;
8822     bool is64 = r->type & ARM_CP_64BIT;
8823     bool ns = secstate & ARM_CP_SECSTATE_NS;
8824     int cp = r->cp;
8825     size_t name_len;
8826     bool make_const;
8827 
8828     switch (state) {
8829     case ARM_CP_STATE_AA32:
8830         /* We assume it is a cp15 register if the .cp field is left unset. */
8831         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8832             cp = 15;
8833         }
8834         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8835         break;
8836     case ARM_CP_STATE_AA64:
8837         /*
8838          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8839          * cp == 0 as equivalent to the value for "standard guest-visible
8840          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8841          * in their AArch64 view (the .cp value may be non-zero for the
8842          * benefit of the AArch32 view).
8843          */
8844         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8845             cp = CP_REG_ARM64_SYSREG_CP;
8846         }
8847         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8848         break;
8849     default:
8850         g_assert_not_reached();
8851     }
8852 
8853     /* Overriding of an existing definition must be explicitly requested. */
8854     if (!(r->type & ARM_CP_OVERRIDE)) {
8855         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8856         if (oldreg) {
8857             assert(oldreg->type & ARM_CP_OVERRIDE);
8858         }
8859     }
8860 
8861     /*
8862      * Eliminate registers that are not present because the EL is missing.
8863      * Doing this here makes it easier to put all registers for a given
8864      * feature into the same ARMCPRegInfo array and define them all at once.
8865      */
8866     make_const = false;
8867     if (arm_feature(env, ARM_FEATURE_EL3)) {
8868         /*
8869          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8870          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8871          */
8872         int min_el = ctz32(r->access) / 2;
8873         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8874             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8875                 return;
8876             }
8877             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8878         }
8879     } else {
8880         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8881                                  ? PL2_RW : PL1_RW);
8882         if ((r->access & max_el) == 0) {
8883             return;
8884         }
8885     }
8886 
8887     /* Combine cpreg and name into one allocation. */
8888     name_len = strlen(name) + 1;
8889     r2 = g_malloc(sizeof(*r2) + name_len);
8890     *r2 = *r;
8891     r2->name = memcpy(r2 + 1, name, name_len);
8892 
8893     /*
8894      * Update fields to match the instantiation, overwiting wildcards
8895      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8896      */
8897     r2->cp = cp;
8898     r2->crm = crm;
8899     r2->opc1 = opc1;
8900     r2->opc2 = opc2;
8901     r2->state = state;
8902     r2->secure = secstate;
8903     if (opaque) {
8904         r2->opaque = opaque;
8905     }
8906 
8907     if (make_const) {
8908         /* This should not have been a very special register to begin. */
8909         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8910         assert(old_special == 0 || old_special == ARM_CP_NOP);
8911         /*
8912          * Set the special function to CONST, retaining the other flags.
8913          * This is important for e.g. ARM_CP_SVE so that we still
8914          * take the SVE trap if CPTR_EL3.EZ == 0.
8915          */
8916         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8917         /*
8918          * Usually, these registers become RES0, but there are a few
8919          * special cases like VPIDR_EL2 which have a constant non-zero
8920          * value with writes ignored.
8921          */
8922         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8923             r2->resetvalue = 0;
8924         }
8925         /*
8926          * ARM_CP_CONST has precedence, so removing the callbacks and
8927          * offsets are not strictly necessary, but it is potentially
8928          * less confusing to debug later.
8929          */
8930         r2->readfn = NULL;
8931         r2->writefn = NULL;
8932         r2->raw_readfn = NULL;
8933         r2->raw_writefn = NULL;
8934         r2->resetfn = NULL;
8935         r2->fieldoffset = 0;
8936         r2->bank_fieldoffsets[0] = 0;
8937         r2->bank_fieldoffsets[1] = 0;
8938     } else {
8939         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8940 
8941         if (isbanked) {
8942             /*
8943              * Register is banked (using both entries in array).
8944              * Overwriting fieldoffset as the array is only used to define
8945              * banked registers but later only fieldoffset is used.
8946              */
8947             r2->fieldoffset = r->bank_fieldoffsets[ns];
8948         }
8949         if (state == ARM_CP_STATE_AA32) {
8950             if (isbanked) {
8951                 /*
8952                  * If the register is banked then we don't need to migrate or
8953                  * reset the 32-bit instance in certain cases:
8954                  *
8955                  * 1) If the register has both 32-bit and 64-bit instances
8956                  *    then we can count on the 64-bit instance taking care
8957                  *    of the non-secure bank.
8958                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8959                  *    version taking care of the secure bank.  This requires
8960                  *    that separate 32 and 64-bit definitions are provided.
8961                  */
8962                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8963                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8964                     r2->type |= ARM_CP_ALIAS;
8965                 }
8966             } else if ((secstate != r->secure) && !ns) {
8967                 /*
8968                  * The register is not banked so we only want to allow
8969                  * migration of the non-secure instance.
8970                  */
8971                 r2->type |= ARM_CP_ALIAS;
8972             }
8973 
8974             if (HOST_BIG_ENDIAN &&
8975                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8976                 r2->fieldoffset += sizeof(uint32_t);
8977             }
8978         }
8979     }
8980 
8981     /*
8982      * By convention, for wildcarded registers only the first
8983      * entry is used for migration; the others are marked as
8984      * ALIAS so we don't try to transfer the register
8985      * multiple times. Special registers (ie NOP/WFI) are
8986      * never migratable and not even raw-accessible.
8987      */
8988     if (r2->type & ARM_CP_SPECIAL_MASK) {
8989         r2->type |= ARM_CP_NO_RAW;
8990     }
8991     if (((r->crm == CP_ANY) && crm != 0) ||
8992         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8993         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8994         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8995     }
8996 
8997     /*
8998      * Check that raw accesses are either forbidden or handled. Note that
8999      * we can't assert this earlier because the setup of fieldoffset for
9000      * banked registers has to be done first.
9001      */
9002     if (!(r2->type & ARM_CP_NO_RAW)) {
9003         assert(!raw_accessors_invalid(r2));
9004     }
9005 
9006     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9007 }
9008 
9009 
9010 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9011                                        const ARMCPRegInfo *r, void *opaque)
9012 {
9013     /*
9014      * Define implementations of coprocessor registers.
9015      * We store these in a hashtable because typically
9016      * there are less than 150 registers in a space which
9017      * is 16*16*16*8*8 = 262144 in size.
9018      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9019      * If a register is defined twice then the second definition is
9020      * used, so this can be used to define some generic registers and
9021      * then override them with implementation specific variations.
9022      * At least one of the original and the second definition should
9023      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9024      * against accidental use.
9025      *
9026      * The state field defines whether the register is to be
9027      * visible in the AArch32 or AArch64 execution state. If the
9028      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9029      * reginfo structure for the AArch32 view, which sees the lower
9030      * 32 bits of the 64 bit register.
9031      *
9032      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9033      * be wildcarded. AArch64 registers are always considered to be 64
9034      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9035      * the register, if any.
9036      */
9037     int crm, opc1, opc2;
9038     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9039     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9040     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9041     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9042     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9043     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9044     CPState state;
9045 
9046     /* 64 bit registers have only CRm and Opc1 fields */
9047     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9048     /* op0 only exists in the AArch64 encodings */
9049     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9050     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9051     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9052     /*
9053      * This API is only for Arm's system coprocessors (14 and 15) or
9054      * (M-profile or v7A-and-earlier only) for implementation defined
9055      * coprocessors in the range 0..7.  Our decode assumes this, since
9056      * 8..13 can be used for other insns including VFP and Neon. See
9057      * valid_cp() in translate.c.  Assert here that we haven't tried
9058      * to use an invalid coprocessor number.
9059      */
9060     switch (r->state) {
9061     case ARM_CP_STATE_BOTH:
9062         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9063         if (r->cp == 0) {
9064             break;
9065         }
9066         /* fall through */
9067     case ARM_CP_STATE_AA32:
9068         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9069             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9070             assert(r->cp >= 14 && r->cp <= 15);
9071         } else {
9072             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9073         }
9074         break;
9075     case ARM_CP_STATE_AA64:
9076         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9077         break;
9078     default:
9079         g_assert_not_reached();
9080     }
9081     /*
9082      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9083      * encodes a minimum access level for the register. We roll this
9084      * runtime check into our general permission check code, so check
9085      * here that the reginfo's specified permissions are strict enough
9086      * to encompass the generic architectural permission check.
9087      */
9088     if (r->state != ARM_CP_STATE_AA32) {
9089         CPAccessRights mask;
9090         switch (r->opc1) {
9091         case 0:
9092             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9093             mask = PL0U_R | PL1_RW;
9094             break;
9095         case 1: case 2:
9096             /* min_EL EL1 */
9097             mask = PL1_RW;
9098             break;
9099         case 3:
9100             /* min_EL EL0 */
9101             mask = PL0_RW;
9102             break;
9103         case 4:
9104         case 5:
9105             /* min_EL EL2 */
9106             mask = PL2_RW;
9107             break;
9108         case 6:
9109             /* min_EL EL3 */
9110             mask = PL3_RW;
9111             break;
9112         case 7:
9113             /* min_EL EL1, secure mode only (we don't check the latter) */
9114             mask = PL1_RW;
9115             break;
9116         default:
9117             /* broken reginfo with out-of-range opc1 */
9118             g_assert_not_reached();
9119         }
9120         /* assert our permissions are not too lax (stricter is fine) */
9121         assert((r->access & ~mask) == 0);
9122     }
9123 
9124     /*
9125      * Check that the register definition has enough info to handle
9126      * reads and writes if they are permitted.
9127      */
9128     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9129         if (r->access & PL3_R) {
9130             assert((r->fieldoffset ||
9131                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9132                    r->readfn);
9133         }
9134         if (r->access & PL3_W) {
9135             assert((r->fieldoffset ||
9136                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9137                    r->writefn);
9138         }
9139     }
9140 
9141     for (crm = crmmin; crm <= crmmax; crm++) {
9142         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9143             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9144                 for (state = ARM_CP_STATE_AA32;
9145                      state <= ARM_CP_STATE_AA64; state++) {
9146                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9147                         continue;
9148                     }
9149                     if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
9150                         cpu_isar_feature(aa64_xs, cpu)) {
9151                         /*
9152                          * This is a TLBI insn which has an NXS variant. The
9153                          * NXS variant is at the same encoding except that
9154                          * crn is +1, and has the same behaviour except for
9155                          * fine-grained trapping. Add the NXS insn here and
9156                          * then fall through to add the normal register.
9157                          * add_cpreg_to_hashtable() copies the cpreg struct
9158                          * and name that it is passed, so it's OK to use
9159                          * a local struct here.
9160                          */
9161                         ARMCPRegInfo nxs_ri = *r;
9162                         g_autofree char *name = g_strdup_printf("%sNXS", r->name);
9163 
9164                         assert(state == ARM_CP_STATE_AA64);
9165                         assert(nxs_ri.crn < 0xf);
9166                         nxs_ri.crn++;
9167                         if (nxs_ri.fgt) {
9168                             nxs_ri.fgt |= R_FGT_NXS_MASK;
9169                         }
9170                         add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
9171                                                ARM_CP_SECSTATE_NS,
9172                                                crm, opc1, opc2, name);
9173                     }
9174                     if (state == ARM_CP_STATE_AA32) {
9175                         /*
9176                          * Under AArch32 CP registers can be common
9177                          * (same for secure and non-secure world) or banked.
9178                          */
9179                         char *name;
9180 
9181                         switch (r->secure) {
9182                         case ARM_CP_SECSTATE_S:
9183                         case ARM_CP_SECSTATE_NS:
9184                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9185                                                    r->secure, crm, opc1, opc2,
9186                                                    r->name);
9187                             break;
9188                         case ARM_CP_SECSTATE_BOTH:
9189                             name = g_strdup_printf("%s_S", r->name);
9190                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9191                                                    ARM_CP_SECSTATE_S,
9192                                                    crm, opc1, opc2, name);
9193                             g_free(name);
9194                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9195                                                    ARM_CP_SECSTATE_NS,
9196                                                    crm, opc1, opc2, r->name);
9197                             break;
9198                         default:
9199                             g_assert_not_reached();
9200                         }
9201                     } else {
9202                         /*
9203                          * AArch64 registers get mapped to non-secure instance
9204                          * of AArch32
9205                          */
9206                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9207                                                ARM_CP_SECSTATE_NS,
9208                                                crm, opc1, opc2, r->name);
9209                     }
9210                 }
9211             }
9212         }
9213     }
9214 }
9215 
9216 /* Define a whole list of registers */
9217 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9218                                         void *opaque, size_t len)
9219 {
9220     size_t i;
9221     for (i = 0; i < len; ++i) {
9222         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9223     }
9224 }
9225 
9226 /*
9227  * Modify ARMCPRegInfo for access from userspace.
9228  *
9229  * This is a data driven modification directed by
9230  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9231  * user-space cannot alter any values and dynamic values pertaining to
9232  * execution state are hidden from user space view anyway.
9233  */
9234 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9235                                  const ARMCPRegUserSpaceInfo *mods,
9236                                  size_t mods_len)
9237 {
9238     for (size_t mi = 0; mi < mods_len; ++mi) {
9239         const ARMCPRegUserSpaceInfo *m = mods + mi;
9240         GPatternSpec *pat = NULL;
9241 
9242         if (m->is_glob) {
9243             pat = g_pattern_spec_new(m->name);
9244         }
9245         for (size_t ri = 0; ri < regs_len; ++ri) {
9246             ARMCPRegInfo *r = regs + ri;
9247 
9248             if (pat && g_pattern_match_string(pat, r->name)) {
9249                 r->type = ARM_CP_CONST;
9250                 r->access = PL0U_R;
9251                 r->resetvalue = 0;
9252                 /* continue */
9253             } else if (strcmp(r->name, m->name) == 0) {
9254                 r->type = ARM_CP_CONST;
9255                 r->access = PL0U_R;
9256                 r->resetvalue &= m->exported_bits;
9257                 r->resetvalue |= m->fixed_bits;
9258                 break;
9259             }
9260         }
9261         if (pat) {
9262             g_pattern_spec_free(pat);
9263         }
9264     }
9265 }
9266 
9267 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9268 {
9269     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9270 }
9271 
9272 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9273                          uint64_t value)
9274 {
9275     /* Helper coprocessor write function for write-ignore registers */
9276 }
9277 
9278 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9279 {
9280     /* Helper coprocessor write function for read-as-zero registers */
9281     return 0;
9282 }
9283 
9284 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9285 {
9286     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9287 }
9288 
9289 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9290 {
9291     /*
9292      * Return true if it is not valid for us to switch to
9293      * this CPU mode (ie all the UNPREDICTABLE cases in
9294      * the ARM ARM CPSRWriteByInstr pseudocode).
9295      */
9296 
9297     /* Changes to or from Hyp via MSR and CPS are illegal. */
9298     if (write_type == CPSRWriteByInstr &&
9299         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9300          mode == ARM_CPU_MODE_HYP)) {
9301         return 1;
9302     }
9303 
9304     switch (mode) {
9305     case ARM_CPU_MODE_USR:
9306         return 0;
9307     case ARM_CPU_MODE_SYS:
9308     case ARM_CPU_MODE_SVC:
9309     case ARM_CPU_MODE_ABT:
9310     case ARM_CPU_MODE_UND:
9311     case ARM_CPU_MODE_IRQ:
9312     case ARM_CPU_MODE_FIQ:
9313         /*
9314          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9315          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9316          */
9317         /*
9318          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9319          * and CPS are treated as illegal mode changes.
9320          */
9321         if (write_type == CPSRWriteByInstr &&
9322             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9323             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9324             return 1;
9325         }
9326         return 0;
9327     case ARM_CPU_MODE_HYP:
9328         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9329     case ARM_CPU_MODE_MON:
9330         return arm_current_el(env) < 3;
9331     default:
9332         return 1;
9333     }
9334 }
9335 
9336 uint32_t cpsr_read(CPUARMState *env)
9337 {
9338     int ZF;
9339     ZF = (env->ZF == 0);
9340     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9341         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9342         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9343         | ((env->condexec_bits & 0xfc) << 8)
9344         | (env->GE << 16) | (env->daif & CPSR_AIF);
9345 }
9346 
9347 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9348                 CPSRWriteType write_type)
9349 {
9350     uint32_t changed_daif;
9351     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9352         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9353 
9354     if (mask & CPSR_NZCV) {
9355         env->ZF = (~val) & CPSR_Z;
9356         env->NF = val;
9357         env->CF = (val >> 29) & 1;
9358         env->VF = (val << 3) & 0x80000000;
9359     }
9360     if (mask & CPSR_Q) {
9361         env->QF = ((val & CPSR_Q) != 0);
9362     }
9363     if (mask & CPSR_T) {
9364         env->thumb = ((val & CPSR_T) != 0);
9365     }
9366     if (mask & CPSR_IT_0_1) {
9367         env->condexec_bits &= ~3;
9368         env->condexec_bits |= (val >> 25) & 3;
9369     }
9370     if (mask & CPSR_IT_2_7) {
9371         env->condexec_bits &= 3;
9372         env->condexec_bits |= (val >> 8) & 0xfc;
9373     }
9374     if (mask & CPSR_GE) {
9375         env->GE = (val >> 16) & 0xf;
9376     }
9377 
9378     /*
9379      * In a V7 implementation that includes the security extensions but does
9380      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9381      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9382      * bits respectively.
9383      *
9384      * In a V8 implementation, it is permitted for privileged software to
9385      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9386      */
9387     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9388         arm_feature(env, ARM_FEATURE_EL3) &&
9389         !arm_feature(env, ARM_FEATURE_EL2) &&
9390         !arm_is_secure(env)) {
9391 
9392         changed_daif = (env->daif ^ val) & mask;
9393 
9394         if (changed_daif & CPSR_A) {
9395             /*
9396              * Check to see if we are allowed to change the masking of async
9397              * abort exceptions from a non-secure state.
9398              */
9399             if (!(env->cp15.scr_el3 & SCR_AW)) {
9400                 qemu_log_mask(LOG_GUEST_ERROR,
9401                               "Ignoring attempt to switch CPSR_A flag from "
9402                               "non-secure world with SCR.AW bit clear\n");
9403                 mask &= ~CPSR_A;
9404             }
9405         }
9406 
9407         if (changed_daif & CPSR_F) {
9408             /*
9409              * Check to see if we are allowed to change the masking of FIQ
9410              * exceptions from a non-secure state.
9411              */
9412             if (!(env->cp15.scr_el3 & SCR_FW)) {
9413                 qemu_log_mask(LOG_GUEST_ERROR,
9414                               "Ignoring attempt to switch CPSR_F flag from "
9415                               "non-secure world with SCR.FW bit clear\n");
9416                 mask &= ~CPSR_F;
9417             }
9418 
9419             /*
9420              * Check whether non-maskable FIQ (NMFI) support is enabled.
9421              * If this bit is set software is not allowed to mask
9422              * FIQs, but is allowed to set CPSR_F to 0.
9423              */
9424             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9425                 (val & CPSR_F)) {
9426                 qemu_log_mask(LOG_GUEST_ERROR,
9427                               "Ignoring attempt to enable CPSR_F flag "
9428                               "(non-maskable FIQ [NMFI] support enabled)\n");
9429                 mask &= ~CPSR_F;
9430             }
9431         }
9432     }
9433 
9434     env->daif &= ~(CPSR_AIF & mask);
9435     env->daif |= val & CPSR_AIF & mask;
9436 
9437     if (write_type != CPSRWriteRaw &&
9438         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9439         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9440             /*
9441              * Note that we can only get here in USR mode if this is a
9442              * gdb stub write; for this case we follow the architectural
9443              * behaviour for guest writes in USR mode of ignoring an attempt
9444              * to switch mode. (Those are caught by translate.c for writes
9445              * triggered by guest instructions.)
9446              */
9447             mask &= ~CPSR_M;
9448         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9449             /*
9450              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9451              * v7, and has defined behaviour in v8:
9452              *  + leave CPSR.M untouched
9453              *  + allow changes to the other CPSR fields
9454              *  + set PSTATE.IL
9455              * For user changes via the GDB stub, we don't set PSTATE.IL,
9456              * as this would be unnecessarily harsh for a user error.
9457              */
9458             mask &= ~CPSR_M;
9459             if (write_type != CPSRWriteByGDBStub &&
9460                 arm_feature(env, ARM_FEATURE_V8)) {
9461                 mask |= CPSR_IL;
9462                 val |= CPSR_IL;
9463             }
9464             qemu_log_mask(LOG_GUEST_ERROR,
9465                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9466                           aarch32_mode_name(env->uncached_cpsr),
9467                           aarch32_mode_name(val));
9468         } else {
9469             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9470                           write_type == CPSRWriteExceptionReturn ?
9471                           "Exception return from AArch32" :
9472                           "AArch32 mode switch from",
9473                           aarch32_mode_name(env->uncached_cpsr),
9474                           aarch32_mode_name(val), env->regs[15]);
9475             switch_mode(env, val & CPSR_M);
9476         }
9477     }
9478     mask &= ~CACHED_CPSR_BITS;
9479     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9480     if (tcg_enabled() && rebuild_hflags) {
9481         arm_rebuild_hflags(env);
9482     }
9483 }
9484 
9485 #ifdef CONFIG_USER_ONLY
9486 
9487 static void switch_mode(CPUARMState *env, int mode)
9488 {
9489     ARMCPU *cpu = env_archcpu(env);
9490 
9491     if (mode != ARM_CPU_MODE_USR) {
9492         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9493     }
9494 }
9495 
9496 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9497                                  uint32_t cur_el, bool secure)
9498 {
9499     return 1;
9500 }
9501 
9502 void aarch64_sync_64_to_32(CPUARMState *env)
9503 {
9504     g_assert_not_reached();
9505 }
9506 
9507 #else
9508 
9509 static void switch_mode(CPUARMState *env, int mode)
9510 {
9511     int old_mode;
9512     int i;
9513 
9514     old_mode = env->uncached_cpsr & CPSR_M;
9515     if (mode == old_mode) {
9516         return;
9517     }
9518 
9519     if (old_mode == ARM_CPU_MODE_FIQ) {
9520         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9521         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9522     } else if (mode == ARM_CPU_MODE_FIQ) {
9523         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9524         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9525     }
9526 
9527     i = bank_number(old_mode);
9528     env->banked_r13[i] = env->regs[13];
9529     env->banked_spsr[i] = env->spsr;
9530 
9531     i = bank_number(mode);
9532     env->regs[13] = env->banked_r13[i];
9533     env->spsr = env->banked_spsr[i];
9534 
9535     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9536     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9537 }
9538 
9539 /*
9540  * Physical Interrupt Target EL Lookup Table
9541  *
9542  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9543  *
9544  * The below multi-dimensional table is used for looking up the target
9545  * exception level given numerous condition criteria.  Specifically, the
9546  * target EL is based on SCR and HCR routing controls as well as the
9547  * currently executing EL and secure state.
9548  *
9549  *    Dimensions:
9550  *    target_el_table[2][2][2][2][2][4]
9551  *                    |  |  |  |  |  +--- Current EL
9552  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9553  *                    |  |  |  +--------- HCR mask override
9554  *                    |  |  +------------ SCR exec state control
9555  *                    |  +--------------- SCR mask override
9556  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9557  *
9558  *    The table values are as such:
9559  *    0-3 = EL0-EL3
9560  *     -1 = Cannot occur
9561  *
9562  * The ARM ARM target EL table includes entries indicating that an "exception
9563  * is not taken".  The two cases where this is applicable are:
9564  *    1) An exception is taken from EL3 but the SCR does not have the exception
9565  *    routed to EL3.
9566  *    2) An exception is taken from EL2 but the HCR does not have the exception
9567  *    routed to EL2.
9568  * In these two cases, the below table contain a target of EL1.  This value is
9569  * returned as it is expected that the consumer of the table data will check
9570  * for "target EL >= current EL" to ensure the exception is not taken.
9571  *
9572  *            SCR     HCR
9573  *         64  EA     AMO                 From
9574  *        BIT IRQ     IMO      Non-secure         Secure
9575  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9576  */
9577 static const int8_t target_el_table[2][2][2][2][2][4] = {
9578     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9579        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9580       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9581        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9582      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9583        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9584       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9585        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9586     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9587        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9588       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9589        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9590      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9591        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9592       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9593        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9594 };
9595 
9596 /*
9597  * Determine the target EL for physical exceptions
9598  */
9599 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9600                                  uint32_t cur_el, bool secure)
9601 {
9602     CPUARMState *env = cpu_env(cs);
9603     bool rw;
9604     bool scr;
9605     bool hcr;
9606     int target_el;
9607     /* Is the highest EL AArch64? */
9608     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9609     uint64_t hcr_el2;
9610 
9611     if (arm_feature(env, ARM_FEATURE_EL3)) {
9612         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9613     } else {
9614         /*
9615          * Either EL2 is the highest EL (and so the EL2 register width
9616          * is given by is64); or there is no EL2 or EL3, in which case
9617          * the value of 'rw' does not affect the table lookup anyway.
9618          */
9619         rw = is64;
9620     }
9621 
9622     hcr_el2 = arm_hcr_el2_eff(env);
9623     switch (excp_idx) {
9624     case EXCP_IRQ:
9625     case EXCP_NMI:
9626         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9627         hcr = hcr_el2 & HCR_IMO;
9628         break;
9629     case EXCP_FIQ:
9630         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9631         hcr = hcr_el2 & HCR_FMO;
9632         break;
9633     default:
9634         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9635         hcr = hcr_el2 & HCR_AMO;
9636         break;
9637     };
9638 
9639     /*
9640      * For these purposes, TGE and AMO/IMO/FMO both force the
9641      * interrupt to EL2.  Fold TGE into the bit extracted above.
9642      */
9643     hcr |= (hcr_el2 & HCR_TGE) != 0;
9644 
9645     /* Perform a table-lookup for the target EL given the current state */
9646     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9647 
9648     assert(target_el > 0);
9649 
9650     return target_el;
9651 }
9652 
9653 void arm_log_exception(CPUState *cs)
9654 {
9655     int idx = cs->exception_index;
9656 
9657     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9658         const char *exc = NULL;
9659         static const char * const excnames[] = {
9660             [EXCP_UDEF] = "Undefined Instruction",
9661             [EXCP_SWI] = "SVC",
9662             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9663             [EXCP_DATA_ABORT] = "Data Abort",
9664             [EXCP_IRQ] = "IRQ",
9665             [EXCP_FIQ] = "FIQ",
9666             [EXCP_BKPT] = "Breakpoint",
9667             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9668             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9669             [EXCP_HVC] = "Hypervisor Call",
9670             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9671             [EXCP_SMC] = "Secure Monitor Call",
9672             [EXCP_VIRQ] = "Virtual IRQ",
9673             [EXCP_VFIQ] = "Virtual FIQ",
9674             [EXCP_SEMIHOST] = "Semihosting call",
9675             [EXCP_NOCP] = "v7M NOCP UsageFault",
9676             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9677             [EXCP_STKOF] = "v8M STKOF UsageFault",
9678             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9679             [EXCP_LSERR] = "v8M LSERR UsageFault",
9680             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9681             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9682             [EXCP_VSERR] = "Virtual SERR",
9683             [EXCP_GPC] = "Granule Protection Check",
9684             [EXCP_NMI] = "NMI",
9685             [EXCP_VINMI] = "Virtual IRQ NMI",
9686             [EXCP_VFNMI] = "Virtual FIQ NMI",
9687         };
9688 
9689         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9690             exc = excnames[idx];
9691         }
9692         if (!exc) {
9693             exc = "unknown";
9694         }
9695         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9696                       idx, exc, cs->cpu_index);
9697     }
9698 }
9699 
9700 /*
9701  * Function used to synchronize QEMU's AArch64 register set with AArch32
9702  * register set.  This is necessary when switching between AArch32 and AArch64
9703  * execution state.
9704  */
9705 void aarch64_sync_32_to_64(CPUARMState *env)
9706 {
9707     int i;
9708     uint32_t mode = env->uncached_cpsr & CPSR_M;
9709 
9710     /* We can blanket copy R[0:7] to X[0:7] */
9711     for (i = 0; i < 8; i++) {
9712         env->xregs[i] = env->regs[i];
9713     }
9714 
9715     /*
9716      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9717      * Otherwise, they come from the banked user regs.
9718      */
9719     if (mode == ARM_CPU_MODE_FIQ) {
9720         for (i = 8; i < 13; i++) {
9721             env->xregs[i] = env->usr_regs[i - 8];
9722         }
9723     } else {
9724         for (i = 8; i < 13; i++) {
9725             env->xregs[i] = env->regs[i];
9726         }
9727     }
9728 
9729     /*
9730      * Registers x13-x23 are the various mode SP and FP registers. Registers
9731      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9732      * from the mode banked register.
9733      */
9734     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9735         env->xregs[13] = env->regs[13];
9736         env->xregs[14] = env->regs[14];
9737     } else {
9738         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9739         /* HYP is an exception in that it is copied from r14 */
9740         if (mode == ARM_CPU_MODE_HYP) {
9741             env->xregs[14] = env->regs[14];
9742         } else {
9743             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9744         }
9745     }
9746 
9747     if (mode == ARM_CPU_MODE_HYP) {
9748         env->xregs[15] = env->regs[13];
9749     } else {
9750         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9751     }
9752 
9753     if (mode == ARM_CPU_MODE_IRQ) {
9754         env->xregs[16] = env->regs[14];
9755         env->xregs[17] = env->regs[13];
9756     } else {
9757         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9758         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9759     }
9760 
9761     if (mode == ARM_CPU_MODE_SVC) {
9762         env->xregs[18] = env->regs[14];
9763         env->xregs[19] = env->regs[13];
9764     } else {
9765         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9766         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9767     }
9768 
9769     if (mode == ARM_CPU_MODE_ABT) {
9770         env->xregs[20] = env->regs[14];
9771         env->xregs[21] = env->regs[13];
9772     } else {
9773         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9774         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9775     }
9776 
9777     if (mode == ARM_CPU_MODE_UND) {
9778         env->xregs[22] = env->regs[14];
9779         env->xregs[23] = env->regs[13];
9780     } else {
9781         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9782         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9783     }
9784 
9785     /*
9786      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9787      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9788      * FIQ bank for r8-r14.
9789      */
9790     if (mode == ARM_CPU_MODE_FIQ) {
9791         for (i = 24; i < 31; i++) {
9792             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9793         }
9794     } else {
9795         for (i = 24; i < 29; i++) {
9796             env->xregs[i] = env->fiq_regs[i - 24];
9797         }
9798         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9799         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9800     }
9801 
9802     env->pc = env->regs[15];
9803 }
9804 
9805 /*
9806  * Function used to synchronize QEMU's AArch32 register set with AArch64
9807  * register set.  This is necessary when switching between AArch32 and AArch64
9808  * execution state.
9809  */
9810 void aarch64_sync_64_to_32(CPUARMState *env)
9811 {
9812     int i;
9813     uint32_t mode = env->uncached_cpsr & CPSR_M;
9814 
9815     /* We can blanket copy X[0:7] to R[0:7] */
9816     for (i = 0; i < 8; i++) {
9817         env->regs[i] = env->xregs[i];
9818     }
9819 
9820     /*
9821      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9822      * Otherwise, we copy x8-x12 into the banked user regs.
9823      */
9824     if (mode == ARM_CPU_MODE_FIQ) {
9825         for (i = 8; i < 13; i++) {
9826             env->usr_regs[i - 8] = env->xregs[i];
9827         }
9828     } else {
9829         for (i = 8; i < 13; i++) {
9830             env->regs[i] = env->xregs[i];
9831         }
9832     }
9833 
9834     /*
9835      * Registers r13 & r14 depend on the current mode.
9836      * If we are in a given mode, we copy the corresponding x registers to r13
9837      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9838      * for the mode.
9839      */
9840     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9841         env->regs[13] = env->xregs[13];
9842         env->regs[14] = env->xregs[14];
9843     } else {
9844         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9845 
9846         /*
9847          * HYP is an exception in that it does not have its own banked r14 but
9848          * shares the USR r14
9849          */
9850         if (mode == ARM_CPU_MODE_HYP) {
9851             env->regs[14] = env->xregs[14];
9852         } else {
9853             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9854         }
9855     }
9856 
9857     if (mode == ARM_CPU_MODE_HYP) {
9858         env->regs[13] = env->xregs[15];
9859     } else {
9860         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9861     }
9862 
9863     if (mode == ARM_CPU_MODE_IRQ) {
9864         env->regs[14] = env->xregs[16];
9865         env->regs[13] = env->xregs[17];
9866     } else {
9867         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9868         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9869     }
9870 
9871     if (mode == ARM_CPU_MODE_SVC) {
9872         env->regs[14] = env->xregs[18];
9873         env->regs[13] = env->xregs[19];
9874     } else {
9875         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9876         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9877     }
9878 
9879     if (mode == ARM_CPU_MODE_ABT) {
9880         env->regs[14] = env->xregs[20];
9881         env->regs[13] = env->xregs[21];
9882     } else {
9883         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9884         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9885     }
9886 
9887     if (mode == ARM_CPU_MODE_UND) {
9888         env->regs[14] = env->xregs[22];
9889         env->regs[13] = env->xregs[23];
9890     } else {
9891         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9892         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9893     }
9894 
9895     /*
9896      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9897      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9898      * FIQ bank for r8-r14.
9899      */
9900     if (mode == ARM_CPU_MODE_FIQ) {
9901         for (i = 24; i < 31; i++) {
9902             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9903         }
9904     } else {
9905         for (i = 24; i < 29; i++) {
9906             env->fiq_regs[i - 24] = env->xregs[i];
9907         }
9908         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9909         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9910     }
9911 
9912     env->regs[15] = env->pc;
9913 }
9914 
9915 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9916                                    uint32_t mask, uint32_t offset,
9917                                    uint32_t newpc)
9918 {
9919     int new_el;
9920 
9921     /* Change the CPU state so as to actually take the exception. */
9922     switch_mode(env, new_mode);
9923 
9924     /*
9925      * For exceptions taken to AArch32 we must clear the SS bit in both
9926      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9927      */
9928     env->pstate &= ~PSTATE_SS;
9929     env->spsr = cpsr_read(env);
9930     /* Clear IT bits.  */
9931     env->condexec_bits = 0;
9932     /* Switch to the new mode, and to the correct instruction set.  */
9933     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9934 
9935     /* This must be after mode switching. */
9936     new_el = arm_current_el(env);
9937 
9938     /* Set new mode endianness */
9939     env->uncached_cpsr &= ~CPSR_E;
9940     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9941         env->uncached_cpsr |= CPSR_E;
9942     }
9943     /* J and IL must always be cleared for exception entry */
9944     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9945     env->daif |= mask;
9946 
9947     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9948         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9949             env->uncached_cpsr |= CPSR_SSBS;
9950         } else {
9951             env->uncached_cpsr &= ~CPSR_SSBS;
9952         }
9953     }
9954 
9955     if (new_mode == ARM_CPU_MODE_HYP) {
9956         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9957         env->elr_el[2] = env->regs[15];
9958     } else {
9959         /* CPSR.PAN is normally preserved preserved unless...  */
9960         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9961             switch (new_el) {
9962             case 3:
9963                 if (!arm_is_secure_below_el3(env)) {
9964                     /* ... the target is EL3, from non-secure state.  */
9965                     env->uncached_cpsr &= ~CPSR_PAN;
9966                     break;
9967                 }
9968                 /* ... the target is EL3, from secure state ... */
9969                 /* fall through */
9970             case 1:
9971                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9972                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9973                     env->uncached_cpsr |= CPSR_PAN;
9974                 }
9975                 break;
9976             }
9977         }
9978         /*
9979          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9980          * and we should just guard the thumb mode on V4
9981          */
9982         if (arm_feature(env, ARM_FEATURE_V4T)) {
9983             env->thumb =
9984                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9985         }
9986         env->regs[14] = env->regs[15] + offset;
9987     }
9988     env->regs[15] = newpc;
9989 
9990     if (tcg_enabled()) {
9991         arm_rebuild_hflags(env);
9992     }
9993 }
9994 
9995 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9996 {
9997     /*
9998      * Handle exception entry to Hyp mode; this is sufficiently
9999      * different to entry to other AArch32 modes that we handle it
10000      * separately here.
10001      *
10002      * The vector table entry used is always the 0x14 Hyp mode entry point,
10003      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10004      * The offset applied to the preferred return address is always zero
10005      * (see DDI0487C.a section G1.12.3).
10006      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10007      */
10008     uint32_t addr, mask;
10009     ARMCPU *cpu = ARM_CPU(cs);
10010     CPUARMState *env = &cpu->env;
10011 
10012     switch (cs->exception_index) {
10013     case EXCP_UDEF:
10014         addr = 0x04;
10015         break;
10016     case EXCP_SWI:
10017         addr = 0x08;
10018         break;
10019     case EXCP_BKPT:
10020         /* Fall through to prefetch abort.  */
10021     case EXCP_PREFETCH_ABORT:
10022         env->cp15.ifar_s = env->exception.vaddress;
10023         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10024                       (uint32_t)env->exception.vaddress);
10025         addr = 0x0c;
10026         break;
10027     case EXCP_DATA_ABORT:
10028         env->cp15.dfar_s = env->exception.vaddress;
10029         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10030                       (uint32_t)env->exception.vaddress);
10031         addr = 0x10;
10032         break;
10033     case EXCP_IRQ:
10034         addr = 0x18;
10035         break;
10036     case EXCP_FIQ:
10037         addr = 0x1c;
10038         break;
10039     case EXCP_HVC:
10040         addr = 0x08;
10041         break;
10042     case EXCP_HYP_TRAP:
10043         addr = 0x14;
10044         break;
10045     default:
10046         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10047     }
10048 
10049     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10050         if (!arm_feature(env, ARM_FEATURE_V8)) {
10051             /*
10052              * QEMU syndrome values are v8-style. v7 has the IL bit
10053              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10054              * If this is a v7 CPU, squash the IL bit in those cases.
10055              */
10056             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10057                 (cs->exception_index == EXCP_DATA_ABORT &&
10058                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10059                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10060                 env->exception.syndrome &= ~ARM_EL_IL;
10061             }
10062         }
10063         env->cp15.esr_el[2] = env->exception.syndrome;
10064     }
10065 
10066     if (arm_current_el(env) != 2 && addr < 0x14) {
10067         addr = 0x14;
10068     }
10069 
10070     mask = 0;
10071     if (!(env->cp15.scr_el3 & SCR_EA)) {
10072         mask |= CPSR_A;
10073     }
10074     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10075         mask |= CPSR_I;
10076     }
10077     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10078         mask |= CPSR_F;
10079     }
10080 
10081     addr += env->cp15.hvbar;
10082 
10083     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10084 }
10085 
10086 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10087 {
10088     ARMCPU *cpu = ARM_CPU(cs);
10089     CPUARMState *env = &cpu->env;
10090     uint32_t addr;
10091     uint32_t mask;
10092     int new_mode;
10093     uint32_t offset;
10094     uint32_t moe;
10095 
10096     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10097     switch (syn_get_ec(env->exception.syndrome)) {
10098     case EC_BREAKPOINT:
10099     case EC_BREAKPOINT_SAME_EL:
10100         moe = 1;
10101         break;
10102     case EC_WATCHPOINT:
10103     case EC_WATCHPOINT_SAME_EL:
10104         moe = 10;
10105         break;
10106     case EC_AA32_BKPT:
10107         moe = 3;
10108         break;
10109     case EC_VECTORCATCH:
10110         moe = 5;
10111         break;
10112     default:
10113         moe = 0;
10114         break;
10115     }
10116 
10117     if (moe) {
10118         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10119     }
10120 
10121     if (env->exception.target_el == 2) {
10122         /* Debug exceptions are reported differently on AArch32 */
10123         switch (syn_get_ec(env->exception.syndrome)) {
10124         case EC_BREAKPOINT:
10125         case EC_BREAKPOINT_SAME_EL:
10126         case EC_AA32_BKPT:
10127         case EC_VECTORCATCH:
10128             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10129                                                      0, 0, 0x22);
10130             break;
10131         case EC_WATCHPOINT:
10132             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10133                                                  EC_DATAABORT);
10134             break;
10135         case EC_WATCHPOINT_SAME_EL:
10136             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10137                                                  EC_DATAABORT_SAME_EL);
10138             break;
10139         }
10140         arm_cpu_do_interrupt_aarch32_hyp(cs);
10141         return;
10142     }
10143 
10144     switch (cs->exception_index) {
10145     case EXCP_UDEF:
10146         new_mode = ARM_CPU_MODE_UND;
10147         addr = 0x04;
10148         mask = CPSR_I;
10149         if (env->thumb) {
10150             offset = 2;
10151         } else {
10152             offset = 4;
10153         }
10154         break;
10155     case EXCP_SWI:
10156         new_mode = ARM_CPU_MODE_SVC;
10157         addr = 0x08;
10158         mask = CPSR_I;
10159         /* The PC already points to the next instruction.  */
10160         offset = 0;
10161         break;
10162     case EXCP_BKPT:
10163         /* Fall through to prefetch abort.  */
10164     case EXCP_PREFETCH_ABORT:
10165         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10166         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10167         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10168                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10169         new_mode = ARM_CPU_MODE_ABT;
10170         addr = 0x0c;
10171         mask = CPSR_A | CPSR_I;
10172         offset = 4;
10173         break;
10174     case EXCP_DATA_ABORT:
10175         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10176         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10177         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10178                       env->exception.fsr,
10179                       (uint32_t)env->exception.vaddress);
10180         new_mode = ARM_CPU_MODE_ABT;
10181         addr = 0x10;
10182         mask = CPSR_A | CPSR_I;
10183         offset = 8;
10184         break;
10185     case EXCP_IRQ:
10186         new_mode = ARM_CPU_MODE_IRQ;
10187         addr = 0x18;
10188         /* Disable IRQ and imprecise data aborts.  */
10189         mask = CPSR_A | CPSR_I;
10190         offset = 4;
10191         if (env->cp15.scr_el3 & SCR_IRQ) {
10192             /* IRQ routed to monitor mode */
10193             new_mode = ARM_CPU_MODE_MON;
10194             mask |= CPSR_F;
10195         }
10196         break;
10197     case EXCP_FIQ:
10198         new_mode = ARM_CPU_MODE_FIQ;
10199         addr = 0x1c;
10200         /* Disable FIQ, IRQ and imprecise data aborts.  */
10201         mask = CPSR_A | CPSR_I | CPSR_F;
10202         if (env->cp15.scr_el3 & SCR_FIQ) {
10203             /* FIQ routed to monitor mode */
10204             new_mode = ARM_CPU_MODE_MON;
10205         }
10206         offset = 4;
10207         break;
10208     case EXCP_VIRQ:
10209         new_mode = ARM_CPU_MODE_IRQ;
10210         addr = 0x18;
10211         /* Disable IRQ and imprecise data aborts.  */
10212         mask = CPSR_A | CPSR_I;
10213         offset = 4;
10214         break;
10215     case EXCP_VFIQ:
10216         new_mode = ARM_CPU_MODE_FIQ;
10217         addr = 0x1c;
10218         /* Disable FIQ, IRQ and imprecise data aborts.  */
10219         mask = CPSR_A | CPSR_I | CPSR_F;
10220         offset = 4;
10221         break;
10222     case EXCP_VSERR:
10223         {
10224             /*
10225              * Note that this is reported as a data abort, but the DFAR
10226              * has an UNKNOWN value.  Construct the SError syndrome from
10227              * AET and ExT fields.
10228              */
10229             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10230 
10231             if (extended_addresses_enabled(env)) {
10232                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10233             } else {
10234                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10235             }
10236             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10237             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10238             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10239                           env->exception.fsr);
10240 
10241             new_mode = ARM_CPU_MODE_ABT;
10242             addr = 0x10;
10243             mask = CPSR_A | CPSR_I;
10244             offset = 8;
10245         }
10246         break;
10247     case EXCP_SMC:
10248         new_mode = ARM_CPU_MODE_MON;
10249         addr = 0x08;
10250         mask = CPSR_A | CPSR_I | CPSR_F;
10251         offset = 0;
10252         break;
10253     default:
10254         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10255         return; /* Never happens.  Keep compiler happy.  */
10256     }
10257 
10258     if (new_mode == ARM_CPU_MODE_MON) {
10259         addr += env->cp15.mvbar;
10260     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10261         /* High vectors. When enabled, base address cannot be remapped. */
10262         addr += 0xffff0000;
10263     } else {
10264         /*
10265          * ARM v7 architectures provide a vector base address register to remap
10266          * the interrupt vector table.
10267          * This register is only followed in non-monitor mode, and is banked.
10268          * Note: only bits 31:5 are valid.
10269          */
10270         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10271     }
10272 
10273     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10274         env->cp15.scr_el3 &= ~SCR_NS;
10275     }
10276 
10277     take_aarch32_exception(env, new_mode, mask, offset, addr);
10278 }
10279 
10280 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10281 {
10282     /*
10283      * Return the register number of the AArch64 view of the AArch32
10284      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10285      * be that of the AArch32 mode the exception came from.
10286      */
10287     int mode = env->uncached_cpsr & CPSR_M;
10288 
10289     switch (aarch32_reg) {
10290     case 0 ... 7:
10291         return aarch32_reg;
10292     case 8 ... 12:
10293         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10294     case 13:
10295         switch (mode) {
10296         case ARM_CPU_MODE_USR:
10297         case ARM_CPU_MODE_SYS:
10298             return 13;
10299         case ARM_CPU_MODE_HYP:
10300             return 15;
10301         case ARM_CPU_MODE_IRQ:
10302             return 17;
10303         case ARM_CPU_MODE_SVC:
10304             return 19;
10305         case ARM_CPU_MODE_ABT:
10306             return 21;
10307         case ARM_CPU_MODE_UND:
10308             return 23;
10309         case ARM_CPU_MODE_FIQ:
10310             return 29;
10311         default:
10312             g_assert_not_reached();
10313         }
10314     case 14:
10315         switch (mode) {
10316         case ARM_CPU_MODE_USR:
10317         case ARM_CPU_MODE_SYS:
10318         case ARM_CPU_MODE_HYP:
10319             return 14;
10320         case ARM_CPU_MODE_IRQ:
10321             return 16;
10322         case ARM_CPU_MODE_SVC:
10323             return 18;
10324         case ARM_CPU_MODE_ABT:
10325             return 20;
10326         case ARM_CPU_MODE_UND:
10327             return 22;
10328         case ARM_CPU_MODE_FIQ:
10329             return 30;
10330         default:
10331             g_assert_not_reached();
10332         }
10333     case 15:
10334         return 31;
10335     default:
10336         g_assert_not_reached();
10337     }
10338 }
10339 
10340 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10341 {
10342     uint32_t ret = cpsr_read(env);
10343 
10344     /* Move DIT to the correct location for SPSR_ELx */
10345     if (ret & CPSR_DIT) {
10346         ret &= ~CPSR_DIT;
10347         ret |= PSTATE_DIT;
10348     }
10349     /* Merge PSTATE.SS into SPSR_ELx */
10350     ret |= env->pstate & PSTATE_SS;
10351 
10352     return ret;
10353 }
10354 
10355 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10356 {
10357     /* Return true if this syndrome value is a synchronous external abort */
10358     switch (syn_get_ec(syndrome)) {
10359     case EC_INSNABORT:
10360     case EC_INSNABORT_SAME_EL:
10361     case EC_DATAABORT:
10362     case EC_DATAABORT_SAME_EL:
10363         /* Look at fault status code for all the synchronous ext abort cases */
10364         switch (syndrome & 0x3f) {
10365         case 0x10:
10366         case 0x13:
10367         case 0x14:
10368         case 0x15:
10369         case 0x16:
10370         case 0x17:
10371             return true;
10372         default:
10373             return false;
10374         }
10375     default:
10376         return false;
10377     }
10378 }
10379 
10380 /* Handle exception entry to a target EL which is using AArch64 */
10381 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10382 {
10383     ARMCPU *cpu = ARM_CPU(cs);
10384     CPUARMState *env = &cpu->env;
10385     unsigned int new_el = env->exception.target_el;
10386     target_ulong addr = env->cp15.vbar_el[new_el];
10387     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10388     unsigned int old_mode;
10389     unsigned int cur_el = arm_current_el(env);
10390     int rt;
10391 
10392     if (tcg_enabled()) {
10393         /*
10394          * Note that new_el can never be 0.  If cur_el is 0, then
10395          * el0_a64 is is_a64(), else el0_a64 is ignored.
10396          */
10397         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10398     }
10399 
10400     if (cur_el < new_el) {
10401         /*
10402          * Entry vector offset depends on whether the implemented EL
10403          * immediately lower than the target level is using AArch32 or AArch64
10404          */
10405         bool is_aa64;
10406         uint64_t hcr;
10407 
10408         switch (new_el) {
10409         case 3:
10410             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10411             break;
10412         case 2:
10413             hcr = arm_hcr_el2_eff(env);
10414             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10415                 is_aa64 = (hcr & HCR_RW) != 0;
10416                 break;
10417             }
10418             /* fall through */
10419         case 1:
10420             is_aa64 = is_a64(env);
10421             break;
10422         default:
10423             g_assert_not_reached();
10424         }
10425 
10426         if (is_aa64) {
10427             addr += 0x400;
10428         } else {
10429             addr += 0x600;
10430         }
10431     } else if (pstate_read(env) & PSTATE_SP) {
10432         addr += 0x200;
10433     }
10434 
10435     switch (cs->exception_index) {
10436     case EXCP_GPC:
10437         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10438                       env->cp15.mfar_el3);
10439         /* fall through */
10440     case EXCP_PREFETCH_ABORT:
10441     case EXCP_DATA_ABORT:
10442         /*
10443          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10444          * to be taken to the SError vector entrypoint.
10445          */
10446         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10447             syndrome_is_sync_extabt(env->exception.syndrome)) {
10448             addr += 0x180;
10449         }
10450         env->cp15.far_el[new_el] = env->exception.vaddress;
10451         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10452                       env->cp15.far_el[new_el]);
10453         /* fall through */
10454     case EXCP_BKPT:
10455     case EXCP_UDEF:
10456     case EXCP_SWI:
10457     case EXCP_HVC:
10458     case EXCP_HYP_TRAP:
10459     case EXCP_SMC:
10460         switch (syn_get_ec(env->exception.syndrome)) {
10461         case EC_ADVSIMDFPACCESSTRAP:
10462             /*
10463              * QEMU internal FP/SIMD syndromes from AArch32 include the
10464              * TA and coproc fields which are only exposed if the exception
10465              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10466              * AArch64 format syndrome.
10467              */
10468             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10469             break;
10470         case EC_CP14RTTRAP:
10471         case EC_CP15RTTRAP:
10472         case EC_CP14DTTRAP:
10473             /*
10474              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10475              * the raw register field from the insn; when taking this to
10476              * AArch64 we must convert it to the AArch64 view of the register
10477              * number. Notice that we read a 4-bit AArch32 register number and
10478              * write back a 5-bit AArch64 one.
10479              */
10480             rt = extract32(env->exception.syndrome, 5, 4);
10481             rt = aarch64_regnum(env, rt);
10482             env->exception.syndrome = deposit32(env->exception.syndrome,
10483                                                 5, 5, rt);
10484             break;
10485         case EC_CP15RRTTRAP:
10486         case EC_CP14RRTTRAP:
10487             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10488             rt = extract32(env->exception.syndrome, 5, 4);
10489             rt = aarch64_regnum(env, rt);
10490             env->exception.syndrome = deposit32(env->exception.syndrome,
10491                                                 5, 5, rt);
10492             rt = extract32(env->exception.syndrome, 10, 4);
10493             rt = aarch64_regnum(env, rt);
10494             env->exception.syndrome = deposit32(env->exception.syndrome,
10495                                                 10, 5, rt);
10496             break;
10497         }
10498         env->cp15.esr_el[new_el] = env->exception.syndrome;
10499         break;
10500     case EXCP_IRQ:
10501     case EXCP_VIRQ:
10502     case EXCP_NMI:
10503     case EXCP_VINMI:
10504         addr += 0x80;
10505         break;
10506     case EXCP_FIQ:
10507     case EXCP_VFIQ:
10508     case EXCP_VFNMI:
10509         addr += 0x100;
10510         break;
10511     case EXCP_VSERR:
10512         addr += 0x180;
10513         /* Construct the SError syndrome from IDS and ISS fields. */
10514         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10515         env->cp15.esr_el[new_el] = env->exception.syndrome;
10516         break;
10517     default:
10518         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10519     }
10520 
10521     if (is_a64(env)) {
10522         old_mode = pstate_read(env);
10523         aarch64_save_sp(env, arm_current_el(env));
10524         env->elr_el[new_el] = env->pc;
10525 
10526         if (cur_el == 1 && new_el == 1) {
10527             uint64_t hcr = arm_hcr_el2_eff(env);
10528             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10529                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10530                 /*
10531                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10532                  * by setting M[3:2] to 0b10.
10533                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10534                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10535                  */
10536                 old_mode = deposit32(old_mode, 2, 2, 2);
10537             }
10538         }
10539     } else {
10540         old_mode = cpsr_read_for_spsr_elx(env);
10541         env->elr_el[new_el] = env->regs[15];
10542 
10543         aarch64_sync_32_to_64(env);
10544 
10545         env->condexec_bits = 0;
10546     }
10547     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10548 
10549     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10550     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10551                   env->elr_el[new_el]);
10552 
10553     if (cpu_isar_feature(aa64_pan, cpu)) {
10554         /* The value of PSTATE.PAN is normally preserved, except when ... */
10555         new_mode |= old_mode & PSTATE_PAN;
10556         switch (new_el) {
10557         case 2:
10558             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10559             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10560                 != (HCR_E2H | HCR_TGE)) {
10561                 break;
10562             }
10563             /* fall through */
10564         case 1:
10565             /* ... the target is EL1 ... */
10566             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10567             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10568                 new_mode |= PSTATE_PAN;
10569             }
10570             break;
10571         }
10572     }
10573     if (cpu_isar_feature(aa64_mte, cpu)) {
10574         new_mode |= PSTATE_TCO;
10575     }
10576 
10577     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10578         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10579             new_mode |= PSTATE_SSBS;
10580         } else {
10581             new_mode &= ~PSTATE_SSBS;
10582         }
10583     }
10584 
10585     if (cpu_isar_feature(aa64_nmi, cpu)) {
10586         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10587             new_mode |= PSTATE_ALLINT;
10588         } else {
10589             new_mode &= ~PSTATE_ALLINT;
10590         }
10591     }
10592 
10593     pstate_write(env, PSTATE_DAIF | new_mode);
10594     env->aarch64 = true;
10595     aarch64_restore_sp(env, new_el);
10596 
10597     if (tcg_enabled()) {
10598         helper_rebuild_hflags_a64(env, new_el);
10599     }
10600 
10601     env->pc = addr;
10602 
10603     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10604                   new_el, env->pc, pstate_read(env));
10605 }
10606 
10607 /*
10608  * Do semihosting call and set the appropriate return value. All the
10609  * permission and validity checks have been done at translate time.
10610  *
10611  * We only see semihosting exceptions in TCG only as they are not
10612  * trapped to the hypervisor in KVM.
10613  */
10614 #ifdef CONFIG_TCG
10615 static void tcg_handle_semihosting(CPUState *cs)
10616 {
10617     ARMCPU *cpu = ARM_CPU(cs);
10618     CPUARMState *env = &cpu->env;
10619 
10620     if (is_a64(env)) {
10621         qemu_log_mask(CPU_LOG_INT,
10622                       "...handling as semihosting call 0x%" PRIx64 "\n",
10623                       env->xregs[0]);
10624         do_common_semihosting(cs);
10625         env->pc += 4;
10626     } else {
10627         qemu_log_mask(CPU_LOG_INT,
10628                       "...handling as semihosting call 0x%x\n",
10629                       env->regs[0]);
10630         do_common_semihosting(cs);
10631         env->regs[15] += env->thumb ? 2 : 4;
10632     }
10633 }
10634 #endif
10635 
10636 /*
10637  * Handle a CPU exception for A and R profile CPUs.
10638  * Do any appropriate logging, handle PSCI calls, and then hand off
10639  * to the AArch64-entry or AArch32-entry function depending on the
10640  * target exception level's register width.
10641  *
10642  * Note: this is used for both TCG (as the do_interrupt tcg op),
10643  *       and KVM to re-inject guest debug exceptions, and to
10644  *       inject a Synchronous-External-Abort.
10645  */
10646 void arm_cpu_do_interrupt(CPUState *cs)
10647 {
10648     ARMCPU *cpu = ARM_CPU(cs);
10649     CPUARMState *env = &cpu->env;
10650     unsigned int new_el = env->exception.target_el;
10651 
10652     assert(!arm_feature(env, ARM_FEATURE_M));
10653 
10654     arm_log_exception(cs);
10655     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10656                   new_el);
10657     if (qemu_loglevel_mask(CPU_LOG_INT)
10658         && !excp_is_internal(cs->exception_index)) {
10659         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10660                       syn_get_ec(env->exception.syndrome),
10661                       env->exception.syndrome);
10662     }
10663 
10664     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10665         arm_handle_psci_call(cpu);
10666         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10667         return;
10668     }
10669 
10670     /*
10671      * Semihosting semantics depend on the register width of the code
10672      * that caused the exception, not the target exception level, so
10673      * must be handled here.
10674      */
10675 #ifdef CONFIG_TCG
10676     if (cs->exception_index == EXCP_SEMIHOST) {
10677         tcg_handle_semihosting(cs);
10678         return;
10679     }
10680 #endif
10681 
10682     /*
10683      * Hooks may change global state so BQL should be held, also the
10684      * BQL needs to be held for any modification of
10685      * cs->interrupt_request.
10686      */
10687     g_assert(bql_locked());
10688 
10689     arm_call_pre_el_change_hook(cpu);
10690 
10691     assert(!excp_is_internal(cs->exception_index));
10692     if (arm_el_is_aa64(env, new_el)) {
10693         arm_cpu_do_interrupt_aarch64(cs);
10694     } else {
10695         arm_cpu_do_interrupt_aarch32(cs);
10696     }
10697 
10698     arm_call_el_change_hook(cpu);
10699 
10700     if (!kvm_enabled()) {
10701         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10702     }
10703 }
10704 #endif /* !CONFIG_USER_ONLY */
10705 
10706 uint64_t arm_sctlr(CPUARMState *env, int el)
10707 {
10708     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10709     if (el == 0) {
10710         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10711         switch (mmu_idx) {
10712         case ARMMMUIdx_E20_0:
10713             el = 2;
10714             break;
10715         case ARMMMUIdx_E30_0:
10716             el = 3;
10717             break;
10718         default:
10719             el = 1;
10720             break;
10721         }
10722     }
10723     return env->cp15.sctlr_el[el];
10724 }
10725 
10726 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10727 {
10728     if (regime_has_2_ranges(mmu_idx)) {
10729         return extract64(tcr, 37, 2);
10730     } else if (regime_is_stage2(mmu_idx)) {
10731         return 0; /* VTCR_EL2 */
10732     } else {
10733         /* Replicate the single TBI bit so we always have 2 bits.  */
10734         return extract32(tcr, 20, 1) * 3;
10735     }
10736 }
10737 
10738 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10739 {
10740     if (regime_has_2_ranges(mmu_idx)) {
10741         return extract64(tcr, 51, 2);
10742     } else if (regime_is_stage2(mmu_idx)) {
10743         return 0; /* VTCR_EL2 */
10744     } else {
10745         /* Replicate the single TBID bit so we always have 2 bits.  */
10746         return extract32(tcr, 29, 1) * 3;
10747     }
10748 }
10749 
10750 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10751 {
10752     if (regime_has_2_ranges(mmu_idx)) {
10753         return extract64(tcr, 57, 2);
10754     } else {
10755         /* Replicate the single TCMA bit so we always have 2 bits.  */
10756         return extract32(tcr, 30, 1) * 3;
10757     }
10758 }
10759 
10760 static ARMGranuleSize tg0_to_gran_size(int tg)
10761 {
10762     switch (tg) {
10763     case 0:
10764         return Gran4K;
10765     case 1:
10766         return Gran64K;
10767     case 2:
10768         return Gran16K;
10769     default:
10770         return GranInvalid;
10771     }
10772 }
10773 
10774 static ARMGranuleSize tg1_to_gran_size(int tg)
10775 {
10776     switch (tg) {
10777     case 1:
10778         return Gran16K;
10779     case 2:
10780         return Gran4K;
10781     case 3:
10782         return Gran64K;
10783     default:
10784         return GranInvalid;
10785     }
10786 }
10787 
10788 static inline bool have4k(ARMCPU *cpu, bool stage2)
10789 {
10790     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10791         : cpu_isar_feature(aa64_tgran4, cpu);
10792 }
10793 
10794 static inline bool have16k(ARMCPU *cpu, bool stage2)
10795 {
10796     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10797         : cpu_isar_feature(aa64_tgran16, cpu);
10798 }
10799 
10800 static inline bool have64k(ARMCPU *cpu, bool stage2)
10801 {
10802     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10803         : cpu_isar_feature(aa64_tgran64, cpu);
10804 }
10805 
10806 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10807                                          bool stage2)
10808 {
10809     switch (gran) {
10810     case Gran4K:
10811         if (have4k(cpu, stage2)) {
10812             return gran;
10813         }
10814         break;
10815     case Gran16K:
10816         if (have16k(cpu, stage2)) {
10817             return gran;
10818         }
10819         break;
10820     case Gran64K:
10821         if (have64k(cpu, stage2)) {
10822             return gran;
10823         }
10824         break;
10825     case GranInvalid:
10826         break;
10827     }
10828     /*
10829      * If the guest selects a granule size that isn't implemented,
10830      * the architecture requires that we behave as if it selected one
10831      * that is (with an IMPDEF choice of which one to pick). We choose
10832      * to implement the smallest supported granule size.
10833      */
10834     if (have4k(cpu, stage2)) {
10835         return Gran4K;
10836     }
10837     if (have16k(cpu, stage2)) {
10838         return Gran16K;
10839     }
10840     assert(have64k(cpu, stage2));
10841     return Gran64K;
10842 }
10843 
10844 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10845                                    ARMMMUIdx mmu_idx, bool data,
10846                                    bool el1_is_aa32)
10847 {
10848     uint64_t tcr = regime_tcr(env, mmu_idx);
10849     bool epd, hpd, tsz_oob, ds, ha, hd;
10850     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10851     ARMGranuleSize gran;
10852     ARMCPU *cpu = env_archcpu(env);
10853     bool stage2 = regime_is_stage2(mmu_idx);
10854 
10855     if (!regime_has_2_ranges(mmu_idx)) {
10856         select = 0;
10857         tsz = extract32(tcr, 0, 6);
10858         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10859         if (stage2) {
10860             /* VTCR_EL2 */
10861             hpd = false;
10862         } else {
10863             hpd = extract32(tcr, 24, 1);
10864         }
10865         epd = false;
10866         sh = extract32(tcr, 12, 2);
10867         ps = extract32(tcr, 16, 3);
10868         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
10869         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10870         ds = extract64(tcr, 32, 1);
10871     } else {
10872         bool e0pd;
10873 
10874         /*
10875          * Bit 55 is always between the two regions, and is canonical for
10876          * determining if address tagging is enabled.
10877          */
10878         select = extract64(va, 55, 1);
10879         if (!select) {
10880             tsz = extract32(tcr, 0, 6);
10881             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10882             epd = extract32(tcr, 7, 1);
10883             sh = extract32(tcr, 12, 2);
10884             hpd = extract64(tcr, 41, 1);
10885             e0pd = extract64(tcr, 55, 1);
10886         } else {
10887             tsz = extract32(tcr, 16, 6);
10888             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10889             epd = extract32(tcr, 23, 1);
10890             sh = extract32(tcr, 28, 2);
10891             hpd = extract64(tcr, 42, 1);
10892             e0pd = extract64(tcr, 56, 1);
10893         }
10894         ps = extract64(tcr, 32, 3);
10895         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
10896         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10897         ds = extract64(tcr, 59, 1);
10898 
10899         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
10900             regime_is_user(env, mmu_idx)) {
10901             epd = true;
10902         }
10903     }
10904 
10905     gran = sanitize_gran_size(cpu, gran, stage2);
10906 
10907     if (cpu_isar_feature(aa64_st, cpu)) {
10908         max_tsz = 48 - (gran == Gran64K);
10909     } else {
10910         max_tsz = 39;
10911     }
10912 
10913     /*
10914      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10915      * adjust the effective value of DS, as documented.
10916      */
10917     min_tsz = 16;
10918     if (gran == Gran64K) {
10919         if (cpu_isar_feature(aa64_lva, cpu)) {
10920             min_tsz = 12;
10921         }
10922         ds = false;
10923     } else if (ds) {
10924         if (regime_is_stage2(mmu_idx)) {
10925             if (gran == Gran16K) {
10926                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10927             } else {
10928                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10929             }
10930         } else {
10931             if (gran == Gran16K) {
10932                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10933             } else {
10934                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10935             }
10936         }
10937         if (ds) {
10938             min_tsz = 12;
10939         }
10940     }
10941 
10942     if (stage2 && el1_is_aa32) {
10943         /*
10944          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
10945          * are loosened: a configured IPA of 40 bits is permitted even if
10946          * the implemented PA is less than that (and so a 40 bit IPA would
10947          * fault for an AArch64 EL1). See R_DTLMN.
10948          */
10949         min_tsz = MIN(min_tsz, 24);
10950     }
10951 
10952     if (tsz > max_tsz) {
10953         tsz = max_tsz;
10954         tsz_oob = true;
10955     } else if (tsz < min_tsz) {
10956         tsz = min_tsz;
10957         tsz_oob = true;
10958     } else {
10959         tsz_oob = false;
10960     }
10961 
10962     /* Present TBI as a composite with TBID.  */
10963     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10964     if (!data) {
10965         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10966     }
10967     tbi = (tbi >> select) & 1;
10968 
10969     return (ARMVAParameters) {
10970         .tsz = tsz,
10971         .ps = ps,
10972         .sh = sh,
10973         .select = select,
10974         .tbi = tbi,
10975         .epd = epd,
10976         .hpd = hpd,
10977         .tsz_oob = tsz_oob,
10978         .ds = ds,
10979         .ha = ha,
10980         .hd = ha && hd,
10981         .gran = gran,
10982     };
10983 }
10984 
10985 
10986 /*
10987  * Return the exception level to which FP-disabled exceptions should
10988  * be taken, or 0 if FP is enabled.
10989  */
10990 int fp_exception_el(CPUARMState *env, int cur_el)
10991 {
10992 #ifndef CONFIG_USER_ONLY
10993     uint64_t hcr_el2;
10994 
10995     /*
10996      * CPACR and the CPTR registers don't exist before v6, so FP is
10997      * always accessible
10998      */
10999     if (!arm_feature(env, ARM_FEATURE_V6)) {
11000         return 0;
11001     }
11002 
11003     if (arm_feature(env, ARM_FEATURE_M)) {
11004         /* CPACR can cause a NOCP UsageFault taken to current security state */
11005         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11006             return 1;
11007         }
11008 
11009         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11010             if (!extract32(env->v7m.nsacr, 10, 1)) {
11011                 /* FP insns cause a NOCP UsageFault taken to Secure */
11012                 return 3;
11013             }
11014         }
11015 
11016         return 0;
11017     }
11018 
11019     hcr_el2 = arm_hcr_el2_eff(env);
11020 
11021     /*
11022      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11023      * 0, 2 : trap EL0 and EL1/PL1 accesses
11024      * 1    : trap only EL0 accesses
11025      * 3    : trap no accesses
11026      * This register is ignored if E2H+TGE are both set.
11027      */
11028     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11029         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11030 
11031         switch (fpen) {
11032         case 1:
11033             if (cur_el != 0) {
11034                 break;
11035             }
11036             /* fall through */
11037         case 0:
11038         case 2:
11039             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11040             if (!arm_el_is_aa64(env, 3)
11041                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11042                 return 3;
11043             }
11044             if (cur_el <= 1) {
11045                 return 1;
11046             }
11047             break;
11048         }
11049     }
11050 
11051     /*
11052      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11053      * to control non-secure access to the FPU. It doesn't have any
11054      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11055      */
11056     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11057          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11058         if (!extract32(env->cp15.nsacr, 10, 1)) {
11059             /* FP insns act as UNDEF */
11060             return cur_el == 2 ? 2 : 1;
11061         }
11062     }
11063 
11064     /*
11065      * CPTR_EL2 is present in v7VE or v8, and changes format
11066      * with HCR_EL2.E2H (regardless of TGE).
11067      */
11068     if (cur_el <= 2) {
11069         if (hcr_el2 & HCR_E2H) {
11070             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11071             case 1:
11072                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11073                     break;
11074                 }
11075                 /* fall through */
11076             case 0:
11077             case 2:
11078                 return 2;
11079             }
11080         } else if (arm_is_el2_enabled(env)) {
11081             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11082                 return 2;
11083             }
11084         }
11085     }
11086 
11087     /* CPTR_EL3 : present in v8 */
11088     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11089         /* Trap all FP ops to EL3 */
11090         return 3;
11091     }
11092 #endif
11093     return 0;
11094 }
11095 
11096 /* Return the exception level we're running at if this is our mmu_idx */
11097 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11098 {
11099     if (mmu_idx & ARM_MMU_IDX_M) {
11100         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11101     }
11102 
11103     switch (mmu_idx) {
11104     case ARMMMUIdx_E10_0:
11105     case ARMMMUIdx_E20_0:
11106     case ARMMMUIdx_E30_0:
11107         return 0;
11108     case ARMMMUIdx_E10_1:
11109     case ARMMMUIdx_E10_1_PAN:
11110         return 1;
11111     case ARMMMUIdx_E2:
11112     case ARMMMUIdx_E20_2:
11113     case ARMMMUIdx_E20_2_PAN:
11114         return 2;
11115     case ARMMMUIdx_E3:
11116     case ARMMMUIdx_E30_3_PAN:
11117         return 3;
11118     default:
11119         g_assert_not_reached();
11120     }
11121 }
11122 
11123 #ifndef CONFIG_TCG
11124 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11125 {
11126     g_assert_not_reached();
11127 }
11128 #endif
11129 
11130 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11131 {
11132     ARMMMUIdx idx;
11133     uint64_t hcr;
11134 
11135     if (arm_feature(env, ARM_FEATURE_M)) {
11136         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11137     }
11138 
11139     /* See ARM pseudo-function ELIsInHost.  */
11140     switch (el) {
11141     case 0:
11142         hcr = arm_hcr_el2_eff(env);
11143         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11144             idx = ARMMMUIdx_E20_0;
11145         } else if (arm_is_secure_below_el3(env) &&
11146                    !arm_el_is_aa64(env, 3)) {
11147             idx = ARMMMUIdx_E30_0;
11148         } else {
11149             idx = ARMMMUIdx_E10_0;
11150         }
11151         break;
11152     case 1:
11153         if (arm_pan_enabled(env)) {
11154             idx = ARMMMUIdx_E10_1_PAN;
11155         } else {
11156             idx = ARMMMUIdx_E10_1;
11157         }
11158         break;
11159     case 2:
11160         /* Note that TGE does not apply at EL2.  */
11161         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11162             if (arm_pan_enabled(env)) {
11163                 idx = ARMMMUIdx_E20_2_PAN;
11164             } else {
11165                 idx = ARMMMUIdx_E20_2;
11166             }
11167         } else {
11168             idx = ARMMMUIdx_E2;
11169         }
11170         break;
11171     case 3:
11172         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11173             return ARMMMUIdx_E30_3_PAN;
11174         }
11175         return ARMMMUIdx_E3;
11176     default:
11177         g_assert_not_reached();
11178     }
11179 
11180     return idx;
11181 }
11182 
11183 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11184 {
11185     return arm_mmu_idx_el(env, arm_current_el(env));
11186 }
11187 
11188 static bool mve_no_pred(CPUARMState *env)
11189 {
11190     /*
11191      * Return true if there is definitely no predication of MVE
11192      * instructions by VPR or LTPSIZE. (Returning false even if there
11193      * isn't any predication is OK; generated code will just be
11194      * a little worse.)
11195      * If the CPU does not implement MVE then this TB flag is always 0.
11196      *
11197      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11198      * logic in gen_update_fp_context() needs to be updated to match.
11199      *
11200      * We do not include the effect of the ECI bits here -- they are
11201      * tracked in other TB flags. This simplifies the logic for
11202      * "when did we emit code that changes the MVE_NO_PRED TB flag
11203      * and thus need to end the TB?".
11204      */
11205     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11206         return false;
11207     }
11208     if (env->v7m.vpr) {
11209         return false;
11210     }
11211     if (env->v7m.ltpsize < 4) {
11212         return false;
11213     }
11214     return true;
11215 }
11216 
11217 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11218                           uint64_t *cs_base, uint32_t *pflags)
11219 {
11220     CPUARMTBFlags flags;
11221 
11222     assert_hflags_rebuild_correctly(env);
11223     flags = env->hflags;
11224 
11225     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11226         *pc = env->pc;
11227         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11228             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11229         }
11230     } else {
11231         *pc = env->regs[15];
11232 
11233         if (arm_feature(env, ARM_FEATURE_M)) {
11234             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11235                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11236                 != env->v7m.secure) {
11237                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11238             }
11239 
11240             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11241                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11242                  (env->v7m.secure &&
11243                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11244                 /*
11245                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11246                  * active FP context; we must create a new FP context before
11247                  * executing any FP insn.
11248                  */
11249                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11250             }
11251 
11252             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11253             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11254                 DP_TBFLAG_M32(flags, LSPACT, 1);
11255             }
11256 
11257             if (mve_no_pred(env)) {
11258                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11259             }
11260         } else {
11261             /*
11262              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11263              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11264              */
11265             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11266                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11267             } else {
11268                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11269                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11270             }
11271             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11272                 DP_TBFLAG_A32(flags, VFPEN, 1);
11273             }
11274         }
11275 
11276         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11277         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11278     }
11279 
11280     /*
11281      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11282      * states defined in the ARM ARM for software singlestep:
11283      *  SS_ACTIVE   PSTATE.SS   State
11284      *     0            x       Inactive (the TB flag for SS is always 0)
11285      *     1            0       Active-pending
11286      *     1            1       Active-not-pending
11287      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11288      */
11289     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11290         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11291     }
11292 
11293     *pflags = flags.flags;
11294     *cs_base = flags.flags2;
11295 }
11296 
11297 #ifdef TARGET_AARCH64
11298 /*
11299  * The manual says that when SVE is enabled and VQ is widened the
11300  * implementation is allowed to zero the previously inaccessible
11301  * portion of the registers.  The corollary to that is that when
11302  * SVE is enabled and VQ is narrowed we are also allowed to zero
11303  * the now inaccessible portion of the registers.
11304  *
11305  * The intent of this is that no predicate bit beyond VQ is ever set.
11306  * Which means that some operations on predicate registers themselves
11307  * may operate on full uint64_t or even unrolled across the maximum
11308  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11309  * may well be cheaper than conditionals to restrict the operation
11310  * to the relevant portion of a uint16_t[16].
11311  */
11312 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11313 {
11314     int i, j;
11315     uint64_t pmask;
11316 
11317     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11318     assert(vq <= env_archcpu(env)->sve_max_vq);
11319 
11320     /* Zap the high bits of the zregs.  */
11321     for (i = 0; i < 32; i++) {
11322         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11323     }
11324 
11325     /* Zap the high bits of the pregs and ffr.  */
11326     pmask = 0;
11327     if (vq & 3) {
11328         pmask = ~(-1ULL << (16 * (vq & 3)));
11329     }
11330     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11331         for (i = 0; i < 17; ++i) {
11332             env->vfp.pregs[i].p[j] &= pmask;
11333         }
11334         pmask = 0;
11335     }
11336 }
11337 
11338 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11339 {
11340     int exc_el;
11341 
11342     if (sm) {
11343         exc_el = sme_exception_el(env, el);
11344     } else {
11345         exc_el = sve_exception_el(env, el);
11346     }
11347     if (exc_el) {
11348         return 0; /* disabled */
11349     }
11350     return sve_vqm1_for_el_sm(env, el, sm);
11351 }
11352 
11353 /*
11354  * Notice a change in SVE vector size when changing EL.
11355  */
11356 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11357                            int new_el, bool el0_a64)
11358 {
11359     ARMCPU *cpu = env_archcpu(env);
11360     int old_len, new_len;
11361     bool old_a64, new_a64, sm;
11362 
11363     /* Nothing to do if no SVE.  */
11364     if (!cpu_isar_feature(aa64_sve, cpu)) {
11365         return;
11366     }
11367 
11368     /* Nothing to do if FP is disabled in either EL.  */
11369     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11370         return;
11371     }
11372 
11373     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11374     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11375 
11376     /*
11377      * Both AArch64.TakeException and AArch64.ExceptionReturn
11378      * invoke ResetSVEState when taking an exception from, or
11379      * returning to, AArch32 state when PSTATE.SM is enabled.
11380      */
11381     sm = FIELD_EX64(env->svcr, SVCR, SM);
11382     if (old_a64 != new_a64 && sm) {
11383         arm_reset_sve_state(env);
11384         return;
11385     }
11386 
11387     /*
11388      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11389      * at ELx, or not available because the EL is in AArch32 state, then
11390      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11391      * has an effective value of 0".
11392      *
11393      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11394      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11395      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11396      * we already have the correct register contents when encountering the
11397      * vq0->vq0 transition between EL0->EL1.
11398      */
11399     old_len = new_len = 0;
11400     if (old_a64) {
11401         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11402     }
11403     if (new_a64) {
11404         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11405     }
11406 
11407     /* When changing vector length, clear inaccessible state.  */
11408     if (new_len < old_len) {
11409         aarch64_sve_narrow_vq(env, new_len + 1);
11410     }
11411 }
11412 #endif
11413 
11414 #ifndef CONFIG_USER_ONLY
11415 ARMSecuritySpace arm_security_space(CPUARMState *env)
11416 {
11417     if (arm_feature(env, ARM_FEATURE_M)) {
11418         return arm_secure_to_space(env->v7m.secure);
11419     }
11420 
11421     /*
11422      * If EL3 is not supported then the secure state is implementation
11423      * defined, in which case QEMU defaults to non-secure.
11424      */
11425     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11426         return ARMSS_NonSecure;
11427     }
11428 
11429     /* Check for AArch64 EL3 or AArch32 Mon. */
11430     if (is_a64(env)) {
11431         if (extract32(env->pstate, 2, 2) == 3) {
11432             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11433                 return ARMSS_Root;
11434             } else {
11435                 return ARMSS_Secure;
11436             }
11437         }
11438     } else {
11439         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11440             return ARMSS_Secure;
11441         }
11442     }
11443 
11444     return arm_security_space_below_el3(env);
11445 }
11446 
11447 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11448 {
11449     assert(!arm_feature(env, ARM_FEATURE_M));
11450 
11451     /*
11452      * If EL3 is not supported then the secure state is implementation
11453      * defined, in which case QEMU defaults to non-secure.
11454      */
11455     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11456         return ARMSS_NonSecure;
11457     }
11458 
11459     /*
11460      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11461      * Ignoring NSE when !NS retains consistency without having to
11462      * modify other predicates.
11463      */
11464     if (!(env->cp15.scr_el3 & SCR_NS)) {
11465         return ARMSS_Secure;
11466     } else if (env->cp15.scr_el3 & SCR_NSE) {
11467         return ARMSS_Realm;
11468     } else {
11469         return ARMSS_NonSecure;
11470     }
11471 }
11472 #endif /* !CONFIG_USER_ONLY */
11473