xref: /openbmc/qemu/target/arm/helper.c (revision 6c187695)
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/units.h"
11 #include "qemu/log.h"
12 #include "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/irq.h"
25 #include "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
33 #ifdef CONFIG_TCG
34 #include "arm_ldst.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
37 #endif
38 #include "cpregs.h"
39 
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41 
42 static void switch_mode(CPUARMState *env, int mode);
43 
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
45 {
46     assert(ri->fieldoffset);
47     if (cpreg_field_is_64bit(ri)) {
48         return CPREG_FIELD64(env, ri);
49     } else {
50         return CPREG_FIELD32(env, ri);
51     }
52 }
53 
54 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
55 {
56     assert(ri->fieldoffset);
57     if (cpreg_field_is_64bit(ri)) {
58         CPREG_FIELD64(env, ri) = value;
59     } else {
60         CPREG_FIELD32(env, ri) = value;
61     }
62 }
63 
64 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66     return (char *)env + ri->fieldoffset;
67 }
68 
69 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
70 {
71     /* Raw read of a coprocessor register (as needed for migration, etc). */
72     if (ri->type & ARM_CP_CONST) {
73         return ri->resetvalue;
74     } else if (ri->raw_readfn) {
75         return ri->raw_readfn(env, ri);
76     } else if (ri->readfn) {
77         return ri->readfn(env, ri);
78     } else {
79         return raw_read(env, ri);
80     }
81 }
82 
83 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
84                              uint64_t v)
85 {
86     /* Raw write of a coprocessor register (as needed for migration, etc).
87      * Note that constant registers are treated as write-ignored; the
88      * caller should check for success by whether a readback gives the
89      * value written.
90      */
91     if (ri->type & ARM_CP_CONST) {
92         return;
93     } else if (ri->raw_writefn) {
94         ri->raw_writefn(env, ri, v);
95     } else if (ri->writefn) {
96         ri->writefn(env, ri, v);
97     } else {
98         raw_write(env, ri, v);
99     }
100 }
101 
102 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
103 {
104    /* Return true if the regdef would cause an assertion if you called
105     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
106     * program bug for it not to have the NO_RAW flag).
107     * NB that returning false here doesn't necessarily mean that calling
108     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
109     * read/write access functions which are safe for raw use" from "has
110     * read/write access functions which have side effects but has forgotten
111     * to provide raw access functions".
112     * The tests here line up with the conditions in read/write_raw_cp_reg()
113     * and assertions in raw_read()/raw_write().
114     */
115     if ((ri->type & ARM_CP_CONST) ||
116         ri->fieldoffset ||
117         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
118         return false;
119     }
120     return true;
121 }
122 
123 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
124 {
125     /* Write the coprocessor state from cpu->env to the (index,value) list. */
126     int i;
127     bool ok = true;
128 
129     for (i = 0; i < cpu->cpreg_array_len; i++) {
130         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
131         const ARMCPRegInfo *ri;
132         uint64_t newval;
133 
134         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
135         if (!ri) {
136             ok = false;
137             continue;
138         }
139         if (ri->type & ARM_CP_NO_RAW) {
140             continue;
141         }
142 
143         newval = read_raw_cp_reg(&cpu->env, ri);
144         if (kvm_sync) {
145             /*
146              * Only sync if the previous list->cpustate sync succeeded.
147              * Rather than tracking the success/failure state for every
148              * item in the list, we just recheck "does the raw write we must
149              * have made in write_list_to_cpustate() read back OK" here.
150              */
151             uint64_t oldval = cpu->cpreg_values[i];
152 
153             if (oldval == newval) {
154                 continue;
155             }
156 
157             write_raw_cp_reg(&cpu->env, ri, oldval);
158             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
159                 continue;
160             }
161 
162             write_raw_cp_reg(&cpu->env, ri, newval);
163         }
164         cpu->cpreg_values[i] = newval;
165     }
166     return ok;
167 }
168 
169 bool write_list_to_cpustate(ARMCPU *cpu)
170 {
171     int i;
172     bool ok = true;
173 
174     for (i = 0; i < cpu->cpreg_array_len; i++) {
175         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176         uint64_t v = cpu->cpreg_values[i];
177         const ARMCPRegInfo *ri;
178 
179         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
180         if (!ri) {
181             ok = false;
182             continue;
183         }
184         if (ri->type & ARM_CP_NO_RAW) {
185             continue;
186         }
187         /* Write value and confirm it reads back as written
188          * (to catch read-only registers and partially read-only
189          * registers where the incoming migration value doesn't match)
190          */
191         write_raw_cp_reg(&cpu->env, ri, v);
192         if (read_raw_cp_reg(&cpu->env, ri) != v) {
193             ok = false;
194         }
195     }
196     return ok;
197 }
198 
199 static void add_cpreg_to_list(gpointer key, gpointer opaque)
200 {
201     ARMCPU *cpu = opaque;
202     uint32_t regidx = (uintptr_t)key;
203     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
204 
205     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
206         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207         /* The value array need not be initialized at this point */
208         cpu->cpreg_array_len++;
209     }
210 }
211 
212 static void count_cpreg(gpointer key, gpointer opaque)
213 {
214     ARMCPU *cpu = opaque;
215     const ARMCPRegInfo *ri;
216 
217     ri = g_hash_table_lookup(cpu->cp_regs, key);
218 
219     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
220         cpu->cpreg_array_len++;
221     }
222 }
223 
224 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
225 {
226     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
228 
229     if (aidx > bidx) {
230         return 1;
231     }
232     if (aidx < bidx) {
233         return -1;
234     }
235     return 0;
236 }
237 
238 void init_cpreg_list(ARMCPU *cpu)
239 {
240     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
241      * Note that we require cpreg_tuples[] to be sorted by key ID.
242      */
243     GList *keys;
244     int arraylen;
245 
246     keys = g_hash_table_get_keys(cpu->cp_regs);
247     keys = g_list_sort(keys, cpreg_key_compare);
248 
249     cpu->cpreg_array_len = 0;
250 
251     g_list_foreach(keys, count_cpreg, cpu);
252 
253     arraylen = cpu->cpreg_array_len;
254     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259     cpu->cpreg_array_len = 0;
260 
261     g_list_foreach(keys, add_cpreg_to_list, cpu);
262 
263     assert(cpu->cpreg_array_len == arraylen);
264 
265     g_list_free(keys);
266 }
267 
268 /*
269  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
270  */
271 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
272                                         const ARMCPRegInfo *ri,
273                                         bool isread)
274 {
275     if (!is_a64(env) && arm_current_el(env) == 3 &&
276         arm_is_secure_below_el3(env)) {
277         return CP_ACCESS_TRAP_UNCATEGORIZED;
278     }
279     return CP_ACCESS_OK;
280 }
281 
282 /* Some secure-only AArch32 registers trap to EL3 if used from
283  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
284  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
285  * We assume that the .access field is set to PL1_RW.
286  */
287 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
288                                             const ARMCPRegInfo *ri,
289                                             bool isread)
290 {
291     if (arm_current_el(env) == 3) {
292         return CP_ACCESS_OK;
293     }
294     if (arm_is_secure_below_el3(env)) {
295         if (env->cp15.scr_el3 & SCR_EEL2) {
296             return CP_ACCESS_TRAP_EL2;
297         }
298         return CP_ACCESS_TRAP_EL3;
299     }
300     /* This will be EL1 NS and EL2 NS, which just UNDEF */
301     return CP_ACCESS_TRAP_UNCATEGORIZED;
302 }
303 
304 /* Check for traps to performance monitor registers, which are controlled
305  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306  */
307 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308                                  bool isread)
309 {
310     int el = arm_current_el(env);
311     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
312 
313     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
314         return CP_ACCESS_TRAP_EL2;
315     }
316     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317         return CP_ACCESS_TRAP_EL3;
318     }
319     return CP_ACCESS_OK;
320 }
321 
322 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
323 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324                                       bool isread)
325 {
326     if (arm_current_el(env) == 1) {
327         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328         if (arm_hcr_el2_eff(env) & trap) {
329             return CP_ACCESS_TRAP_EL2;
330         }
331     }
332     return CP_ACCESS_OK;
333 }
334 
335 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
336 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337                                  bool isread)
338 {
339     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340         return CP_ACCESS_TRAP_EL2;
341     }
342     return CP_ACCESS_OK;
343 }
344 
345 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
346 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347                                   bool isread)
348 {
349     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350         return CP_ACCESS_TRAP_EL2;
351     }
352     return CP_ACCESS_OK;
353 }
354 
355 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
356 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357                                   bool isread)
358 {
359     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360         return CP_ACCESS_TRAP_EL2;
361     }
362     return CP_ACCESS_OK;
363 }
364 
365 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
366 {
367     ARMCPU *cpu = env_archcpu(env);
368 
369     raw_write(env, ri, value);
370     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
371 }
372 
373 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
374 {
375     ARMCPU *cpu = env_archcpu(env);
376 
377     if (raw_read(env, ri) != value) {
378         /* Unlike real hardware the qemu TLB uses virtual addresses,
379          * not modified virtual addresses, so this causes a TLB flush.
380          */
381         tlb_flush(CPU(cpu));
382         raw_write(env, ri, value);
383     }
384 }
385 
386 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
387                              uint64_t value)
388 {
389     ARMCPU *cpu = env_archcpu(env);
390 
391     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
392         && !extended_addresses_enabled(env)) {
393         /* For VMSA (when not using the LPAE long descriptor page table
394          * format) this register includes the ASID, so do a TLB flush.
395          * For PMSA it is purely a process ID and no action is needed.
396          */
397         tlb_flush(CPU(cpu));
398     }
399     raw_write(env, ri, value);
400 }
401 
402 /* IS variants of TLB operations must affect all cores */
403 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
404                              uint64_t value)
405 {
406     CPUState *cs = env_cpu(env);
407 
408     tlb_flush_all_cpus_synced(cs);
409 }
410 
411 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                              uint64_t value)
413 {
414     CPUState *cs = env_cpu(env);
415 
416     tlb_flush_all_cpus_synced(cs);
417 }
418 
419 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420                              uint64_t value)
421 {
422     CPUState *cs = env_cpu(env);
423 
424     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
425 }
426 
427 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
428                              uint64_t value)
429 {
430     CPUState *cs = env_cpu(env);
431 
432     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
433 }
434 
435 /*
436  * Non-IS variants of TLB operations are upgraded to
437  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
438  * force broadcast of these operations.
439  */
440 static bool tlb_force_broadcast(CPUARMState *env)
441 {
442     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
443 }
444 
445 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
446                           uint64_t value)
447 {
448     /* Invalidate all (TLBIALL) */
449     CPUState *cs = env_cpu(env);
450 
451     if (tlb_force_broadcast(env)) {
452         tlb_flush_all_cpus_synced(cs);
453     } else {
454         tlb_flush(cs);
455     }
456 }
457 
458 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
459                           uint64_t value)
460 {
461     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
462     CPUState *cs = env_cpu(env);
463 
464     value &= TARGET_PAGE_MASK;
465     if (tlb_force_broadcast(env)) {
466         tlb_flush_page_all_cpus_synced(cs, value);
467     } else {
468         tlb_flush_page(cs, value);
469     }
470 }
471 
472 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
473                            uint64_t value)
474 {
475     /* Invalidate by ASID (TLBIASID) */
476     CPUState *cs = env_cpu(env);
477 
478     if (tlb_force_broadcast(env)) {
479         tlb_flush_all_cpus_synced(cs);
480     } else {
481         tlb_flush(cs);
482     }
483 }
484 
485 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                            uint64_t value)
487 {
488     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
489     CPUState *cs = env_cpu(env);
490 
491     value &= TARGET_PAGE_MASK;
492     if (tlb_force_broadcast(env)) {
493         tlb_flush_page_all_cpus_synced(cs, value);
494     } else {
495         tlb_flush_page(cs, value);
496     }
497 }
498 
499 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                                uint64_t value)
501 {
502     CPUState *cs = env_cpu(env);
503 
504     tlb_flush_by_mmuidx(cs,
505                         ARMMMUIdxBit_E10_1 |
506                         ARMMMUIdxBit_E10_1_PAN |
507                         ARMMMUIdxBit_E10_0);
508 }
509 
510 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
511                                   uint64_t value)
512 {
513     CPUState *cs = env_cpu(env);
514 
515     tlb_flush_by_mmuidx_all_cpus_synced(cs,
516                                         ARMMMUIdxBit_E10_1 |
517                                         ARMMMUIdxBit_E10_1_PAN |
518                                         ARMMMUIdxBit_E10_0);
519 }
520 
521 
522 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
523                               uint64_t value)
524 {
525     CPUState *cs = env_cpu(env);
526 
527     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
528 }
529 
530 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
531                                  uint64_t value)
532 {
533     CPUState *cs = env_cpu(env);
534 
535     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
536 }
537 
538 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                               uint64_t value)
540 {
541     CPUState *cs = env_cpu(env);
542     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
543 
544     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
545 }
546 
547 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548                                  uint64_t value)
549 {
550     CPUState *cs = env_cpu(env);
551     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
552 
553     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
554                                              ARMMMUIdxBit_E2);
555 }
556 
557 static const ARMCPRegInfo cp_reginfo[] = {
558     /* Define the secure and non-secure FCSE identifier CP registers
559      * separately because there is no secure bank in V8 (no _EL3).  This allows
560      * the secure register to be properly reset and migrated. There is also no
561      * v8 EL1 version of the register so the non-secure instance stands alone.
562      */
563     { .name = "FCSEIDR",
564       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
565       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
566       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
567       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
568     { .name = "FCSEIDR_S",
569       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
570       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
571       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
572       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
573     /* Define the secure and non-secure context identifier CP registers
574      * separately because there is no secure bank in V8 (no _EL3).  This allows
575      * the secure register to be properly reset and migrated.  In the
576      * non-secure case, the 32-bit register will have reset and migration
577      * disabled during registration as it is handled by the 64-bit instance.
578      */
579     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
580       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
581       .access = PL1_RW, .accessfn = access_tvm_trvm,
582       .secure = ARM_CP_SECSTATE_NS,
583       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
584       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
585     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
586       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
587       .access = PL1_RW, .accessfn = access_tvm_trvm,
588       .secure = ARM_CP_SECSTATE_S,
589       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
590       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
591 };
592 
593 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
594     /* NB: Some of these registers exist in v8 but with more precise
595      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
596      */
597     /* MMU Domain access control / MPU write buffer control */
598     { .name = "DACR",
599       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
600       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
601       .writefn = dacr_write, .raw_writefn = raw_write,
602       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
603                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
604     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
605      * For v6 and v5, these mappings are overly broad.
606      */
607     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
608       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
610       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
612       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
614       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
615     /* Cache maintenance ops; some of this space may be overridden later. */
616     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
617       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
618       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
619 };
620 
621 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
622     /* Not all pre-v6 cores implemented this WFI, so this is slightly
623      * over-broad.
624      */
625     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
626       .access = PL1_W, .type = ARM_CP_WFI },
627 };
628 
629 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631      * is UNPREDICTABLE; we choose to NOP as most implementations do).
632      */
633     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634       .access = PL1_W, .type = ARM_CP_WFI },
635     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637      * OMAPCP will override this space.
638      */
639     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
641       .resetvalue = 0 },
642     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
644       .resetvalue = 0 },
645     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
647       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
648       .resetvalue = 0 },
649     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650      * implementing it as RAZ means the "debug architecture version" bits
651      * will read as a reserved value, which should cause Linux to not try
652      * to use the debug hardware.
653      */
654     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
656     /* MMU TLB control. Note that the wildcarding means we cover not just
657      * the unified TLB ops but also the dside/iside/inner-shareable variants.
658      */
659     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
661       .type = ARM_CP_NO_RAW },
662     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
664       .type = ARM_CP_NO_RAW },
665     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
667       .type = ARM_CP_NO_RAW },
668     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
670       .type = ARM_CP_NO_RAW },
671     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
675 };
676 
677 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
678                         uint64_t value)
679 {
680     uint32_t mask = 0;
681 
682     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
683     if (!arm_feature(env, ARM_FEATURE_V8)) {
684         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
685          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
686          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
687          */
688         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
689             /* VFP coprocessor: cp10 & cp11 [23:20] */
690             mask |= R_CPACR_ASEDIS_MASK |
691                     R_CPACR_D32DIS_MASK |
692                     R_CPACR_CP11_MASK |
693                     R_CPACR_CP10_MASK;
694 
695             if (!arm_feature(env, ARM_FEATURE_NEON)) {
696                 /* ASEDIS [31] bit is RAO/WI */
697                 value |= R_CPACR_ASEDIS_MASK;
698             }
699 
700             /* VFPv3 and upwards with NEON implement 32 double precision
701              * registers (D0-D31).
702              */
703             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
704                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
705                 value |= R_CPACR_D32DIS_MASK;
706             }
707         }
708         value &= mask;
709     }
710 
711     /*
712      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
713      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
714      */
715     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
716         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
717         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
718         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
719     }
720 
721     env->cp15.cpacr_el1 = value;
722 }
723 
724 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
725 {
726     /*
727      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
728      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
729      */
730     uint64_t value = env->cp15.cpacr_el1;
731 
732     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
733         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
734         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
735     }
736     return value;
737 }
738 
739 
740 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
741 {
742     /* Call cpacr_write() so that we reset with the correct RAO bits set
743      * for our CPU features.
744      */
745     cpacr_write(env, ri, 0);
746 }
747 
748 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749                                    bool isread)
750 {
751     if (arm_feature(env, ARM_FEATURE_V8)) {
752         /* Check if CPACR accesses are to be trapped to EL2 */
753         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
754             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
755             return CP_ACCESS_TRAP_EL2;
756         /* Check if CPACR accesses are to be trapped to EL3 */
757         } else if (arm_current_el(env) < 3 &&
758                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
759             return CP_ACCESS_TRAP_EL3;
760         }
761     }
762 
763     return CP_ACCESS_OK;
764 }
765 
766 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
767                                   bool isread)
768 {
769     /* Check if CPTR accesses are set to trap to EL3 */
770     if (arm_current_el(env) == 2 &&
771         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
772         return CP_ACCESS_TRAP_EL3;
773     }
774 
775     return CP_ACCESS_OK;
776 }
777 
778 static const ARMCPRegInfo v6_cp_reginfo[] = {
779     /* prefetch by MVA in v6, NOP in v7 */
780     { .name = "MVA_prefetch",
781       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
782       .access = PL1_W, .type = ARM_CP_NOP },
783     /* We need to break the TB after ISB to execute self-modifying code
784      * correctly and also to take any pending interrupts immediately.
785      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
786      */
787     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
788       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
789     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
790       .access = PL0_W, .type = ARM_CP_NOP },
791     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
792       .access = PL0_W, .type = ARM_CP_NOP },
793     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
794       .access = PL1_RW, .accessfn = access_tvm_trvm,
795       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
796                              offsetof(CPUARMState, cp15.ifar_ns) },
797       .resetvalue = 0, },
798     /* Watchpoint Fault Address Register : should actually only be present
799      * for 1136, 1176, 11MPCore.
800      */
801     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
802       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
803     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
804       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
805       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
806       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
807 };
808 
809 typedef struct pm_event {
810     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
811     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
812     bool (*supported)(CPUARMState *);
813     /*
814      * Retrieve the current count of the underlying event. The programmed
815      * counters hold a difference from the return value from this function
816      */
817     uint64_t (*get_count)(CPUARMState *);
818     /*
819      * Return how many nanoseconds it will take (at a minimum) for count events
820      * to occur. A negative value indicates the counter will never overflow, or
821      * that the counter has otherwise arranged for the overflow bit to be set
822      * and the PMU interrupt to be raised on overflow.
823      */
824     int64_t (*ns_per_count)(uint64_t);
825 } pm_event;
826 
827 static bool event_always_supported(CPUARMState *env)
828 {
829     return true;
830 }
831 
832 static uint64_t swinc_get_count(CPUARMState *env)
833 {
834     /*
835      * SW_INCR events are written directly to the pmevcntr's by writes to
836      * PMSWINC, so there is no underlying count maintained by the PMU itself
837      */
838     return 0;
839 }
840 
841 static int64_t swinc_ns_per(uint64_t ignored)
842 {
843     return -1;
844 }
845 
846 /*
847  * Return the underlying cycle count for the PMU cycle counters. If we're in
848  * usermode, simply return 0.
849  */
850 static uint64_t cycles_get_count(CPUARMState *env)
851 {
852 #ifndef CONFIG_USER_ONLY
853     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
854                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
855 #else
856     return cpu_get_host_ticks();
857 #endif
858 }
859 
860 #ifndef CONFIG_USER_ONLY
861 static int64_t cycles_ns_per(uint64_t cycles)
862 {
863     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
864 }
865 
866 static bool instructions_supported(CPUARMState *env)
867 {
868     return icount_enabled() == 1; /* Precise instruction counting */
869 }
870 
871 static uint64_t instructions_get_count(CPUARMState *env)
872 {
873     return (uint64_t)icount_get_raw();
874 }
875 
876 static int64_t instructions_ns_per(uint64_t icount)
877 {
878     return icount_to_ns((int64_t)icount);
879 }
880 #endif
881 
882 static bool pmuv3p1_events_supported(CPUARMState *env)
883 {
884     /* For events which are supported in any v8.1 PMU */
885     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
886 }
887 
888 static bool pmuv3p4_events_supported(CPUARMState *env)
889 {
890     /* For events which are supported in any v8.1 PMU */
891     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
892 }
893 
894 static uint64_t zero_event_get_count(CPUARMState *env)
895 {
896     /* For events which on QEMU never fire, so their count is always zero */
897     return 0;
898 }
899 
900 static int64_t zero_event_ns_per(uint64_t cycles)
901 {
902     /* An event which never fires can never overflow */
903     return -1;
904 }
905 
906 static const pm_event pm_events[] = {
907     { .number = 0x000, /* SW_INCR */
908       .supported = event_always_supported,
909       .get_count = swinc_get_count,
910       .ns_per_count = swinc_ns_per,
911     },
912 #ifndef CONFIG_USER_ONLY
913     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
914       .supported = instructions_supported,
915       .get_count = instructions_get_count,
916       .ns_per_count = instructions_ns_per,
917     },
918     { .number = 0x011, /* CPU_CYCLES, Cycle */
919       .supported = event_always_supported,
920       .get_count = cycles_get_count,
921       .ns_per_count = cycles_ns_per,
922     },
923 #endif
924     { .number = 0x023, /* STALL_FRONTEND */
925       .supported = pmuv3p1_events_supported,
926       .get_count = zero_event_get_count,
927       .ns_per_count = zero_event_ns_per,
928     },
929     { .number = 0x024, /* STALL_BACKEND */
930       .supported = pmuv3p1_events_supported,
931       .get_count = zero_event_get_count,
932       .ns_per_count = zero_event_ns_per,
933     },
934     { .number = 0x03c, /* STALL */
935       .supported = pmuv3p4_events_supported,
936       .get_count = zero_event_get_count,
937       .ns_per_count = zero_event_ns_per,
938     },
939 };
940 
941 /*
942  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
943  * events (i.e. the statistical profiling extension), this implementation
944  * should first be updated to something sparse instead of the current
945  * supported_event_map[] array.
946  */
947 #define MAX_EVENT_ID 0x3c
948 #define UNSUPPORTED_EVENT UINT16_MAX
949 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
950 
951 /*
952  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
953  * of ARM event numbers to indices in our pm_events array.
954  *
955  * Note: Events in the 0x40XX range are not currently supported.
956  */
957 void pmu_init(ARMCPU *cpu)
958 {
959     unsigned int i;
960 
961     /*
962      * Empty supported_event_map and cpu->pmceid[01] before adding supported
963      * events to them
964      */
965     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
966         supported_event_map[i] = UNSUPPORTED_EVENT;
967     }
968     cpu->pmceid0 = 0;
969     cpu->pmceid1 = 0;
970 
971     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
972         const pm_event *cnt = &pm_events[i];
973         assert(cnt->number <= MAX_EVENT_ID);
974         /* We do not currently support events in the 0x40xx range */
975         assert(cnt->number <= 0x3f);
976 
977         if (cnt->supported(&cpu->env)) {
978             supported_event_map[cnt->number] = i;
979             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
980             if (cnt->number & 0x20) {
981                 cpu->pmceid1 |= event_mask;
982             } else {
983                 cpu->pmceid0 |= event_mask;
984             }
985         }
986     }
987 }
988 
989 /*
990  * Check at runtime whether a PMU event is supported for the current machine
991  */
992 static bool event_supported(uint16_t number)
993 {
994     if (number > MAX_EVENT_ID) {
995         return false;
996     }
997     return supported_event_map[number] != UNSUPPORTED_EVENT;
998 }
999 
1000 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1001                                    bool isread)
1002 {
1003     /* Performance monitor registers user accessibility is controlled
1004      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1005      * trapping to EL2 or EL3 for other accesses.
1006      */
1007     int el = arm_current_el(env);
1008     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1009 
1010     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1011         return CP_ACCESS_TRAP;
1012     }
1013     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1014         return CP_ACCESS_TRAP_EL2;
1015     }
1016     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1017         return CP_ACCESS_TRAP_EL3;
1018     }
1019 
1020     return CP_ACCESS_OK;
1021 }
1022 
1023 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1024                                            const ARMCPRegInfo *ri,
1025                                            bool isread)
1026 {
1027     /* ER: event counter read trap control */
1028     if (arm_feature(env, ARM_FEATURE_V8)
1029         && arm_current_el(env) == 0
1030         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1031         && isread) {
1032         return CP_ACCESS_OK;
1033     }
1034 
1035     return pmreg_access(env, ri, isread);
1036 }
1037 
1038 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1039                                          const ARMCPRegInfo *ri,
1040                                          bool isread)
1041 {
1042     /* SW: software increment write trap control */
1043     if (arm_feature(env, ARM_FEATURE_V8)
1044         && arm_current_el(env) == 0
1045         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1046         && !isread) {
1047         return CP_ACCESS_OK;
1048     }
1049 
1050     return pmreg_access(env, ri, isread);
1051 }
1052 
1053 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1054                                         const ARMCPRegInfo *ri,
1055                                         bool isread)
1056 {
1057     /* ER: event counter read trap control */
1058     if (arm_feature(env, ARM_FEATURE_V8)
1059         && arm_current_el(env) == 0
1060         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1061         return CP_ACCESS_OK;
1062     }
1063 
1064     return pmreg_access(env, ri, isread);
1065 }
1066 
1067 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1068                                          const ARMCPRegInfo *ri,
1069                                          bool isread)
1070 {
1071     /* CR: cycle counter read trap control */
1072     if (arm_feature(env, ARM_FEATURE_V8)
1073         && arm_current_el(env) == 0
1074         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1075         && isread) {
1076         return CP_ACCESS_OK;
1077     }
1078 
1079     return pmreg_access(env, ri, isread);
1080 }
1081 
1082 /*
1083  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1084  * We use these to decide whether we need to wrap a write to MDCR_EL2
1085  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1086  */
1087 #define MDCR_EL2_PMU_ENABLE_BITS \
1088     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1089 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1090 
1091 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1092  * the current EL, security state, and register configuration.
1093  */
1094 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1095 {
1096     uint64_t filter;
1097     bool e, p, u, nsk, nsu, nsh, m;
1098     bool enabled, prohibited = false, filtered;
1099     bool secure = arm_is_secure(env);
1100     int el = arm_current_el(env);
1101     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1102     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1103 
1104     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1105         return false;
1106     }
1107 
1108     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1109             (counter < hpmn || counter == 31)) {
1110         e = env->cp15.c9_pmcr & PMCRE;
1111     } else {
1112         e = mdcr_el2 & MDCR_HPME;
1113     }
1114     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1115 
1116     /* Is event counting prohibited? */
1117     if (el == 2 && (counter < hpmn || counter == 31)) {
1118         prohibited = mdcr_el2 & MDCR_HPMD;
1119     }
1120     if (secure) {
1121         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1122     }
1123 
1124     if (counter == 31) {
1125         /*
1126          * The cycle counter defaults to running. PMCR.DP says "disable
1127          * the cycle counter when event counting is prohibited".
1128          * Some MDCR bits disable the cycle counter specifically.
1129          */
1130         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1131         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1132             if (secure) {
1133                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1134             }
1135             if (el == 2) {
1136                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1137             }
1138         }
1139     }
1140 
1141     if (counter == 31) {
1142         filter = env->cp15.pmccfiltr_el0;
1143     } else {
1144         filter = env->cp15.c14_pmevtyper[counter];
1145     }
1146 
1147     p   = filter & PMXEVTYPER_P;
1148     u   = filter & PMXEVTYPER_U;
1149     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1150     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1151     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1152     m   = arm_el_is_aa64(env, 1) &&
1153               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1154 
1155     if (el == 0) {
1156         filtered = secure ? u : u != nsu;
1157     } else if (el == 1) {
1158         filtered = secure ? p : p != nsk;
1159     } else if (el == 2) {
1160         filtered = !nsh;
1161     } else { /* EL3 */
1162         filtered = m != p;
1163     }
1164 
1165     if (counter != 31) {
1166         /*
1167          * If not checking PMCCNTR, ensure the counter is setup to an event we
1168          * support
1169          */
1170         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1171         if (!event_supported(event)) {
1172             return false;
1173         }
1174     }
1175 
1176     return enabled && !prohibited && !filtered;
1177 }
1178 
1179 static void pmu_update_irq(CPUARMState *env)
1180 {
1181     ARMCPU *cpu = env_archcpu(env);
1182     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1183             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1184 }
1185 
1186 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1187 {
1188     /*
1189      * Return true if the clock divider is enabled and the cycle counter
1190      * is supposed to tick only once every 64 clock cycles. This is
1191      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1192      * (64-bit) cycle counter PMCR.D has no effect.
1193      */
1194     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1195 }
1196 
1197 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1198 {
1199     /* Return true if the specified event counter is configured to be 64 bit */
1200 
1201     /* This isn't intended to be used with the cycle counter */
1202     assert(counter < 31);
1203 
1204     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1205         return false;
1206     }
1207 
1208     if (arm_feature(env, ARM_FEATURE_EL2)) {
1209         /*
1210          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1211          * current security state, so we don't use arm_mdcr_el2_eff() here.
1212          */
1213         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1214         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1215 
1216         if (hpmn != 0 && counter >= hpmn) {
1217             return hlp;
1218         }
1219     }
1220     return env->cp15.c9_pmcr & PMCRLP;
1221 }
1222 
1223 /*
1224  * Ensure c15_ccnt is the guest-visible count so that operations such as
1225  * enabling/disabling the counter or filtering, modifying the count itself,
1226  * etc. can be done logically. This is essentially a no-op if the counter is
1227  * not enabled at the time of the call.
1228  */
1229 static void pmccntr_op_start(CPUARMState *env)
1230 {
1231     uint64_t cycles = cycles_get_count(env);
1232 
1233     if (pmu_counter_enabled(env, 31)) {
1234         uint64_t eff_cycles = cycles;
1235         if (pmccntr_clockdiv_enabled(env)) {
1236             eff_cycles /= 64;
1237         }
1238 
1239         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1240 
1241         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1242                                  1ull << 63 : 1ull << 31;
1243         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1244             env->cp15.c9_pmovsr |= (1ULL << 31);
1245             pmu_update_irq(env);
1246         }
1247 
1248         env->cp15.c15_ccnt = new_pmccntr;
1249     }
1250     env->cp15.c15_ccnt_delta = cycles;
1251 }
1252 
1253 /*
1254  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1255  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1256  * pmccntr_op_start.
1257  */
1258 static void pmccntr_op_finish(CPUARMState *env)
1259 {
1260     if (pmu_counter_enabled(env, 31)) {
1261 #ifndef CONFIG_USER_ONLY
1262         /* Calculate when the counter will next overflow */
1263         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1264         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1265             remaining_cycles = (uint32_t)remaining_cycles;
1266         }
1267         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1268 
1269         if (overflow_in > 0) {
1270             int64_t overflow_at;
1271 
1272             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1273                                  overflow_in, &overflow_at)) {
1274                 ARMCPU *cpu = env_archcpu(env);
1275                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1276             }
1277         }
1278 #endif
1279 
1280         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1281         if (pmccntr_clockdiv_enabled(env)) {
1282             prev_cycles /= 64;
1283         }
1284         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1285     }
1286 }
1287 
1288 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1289 {
1290 
1291     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1292     uint64_t count = 0;
1293     if (event_supported(event)) {
1294         uint16_t event_idx = supported_event_map[event];
1295         count = pm_events[event_idx].get_count(env);
1296     }
1297 
1298     if (pmu_counter_enabled(env, counter)) {
1299         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1300         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1301             1ULL << 63 : 1ULL << 31;
1302 
1303         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1304             env->cp15.c9_pmovsr |= (1 << counter);
1305             pmu_update_irq(env);
1306         }
1307         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1308     }
1309     env->cp15.c14_pmevcntr_delta[counter] = count;
1310 }
1311 
1312 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1313 {
1314     if (pmu_counter_enabled(env, counter)) {
1315 #ifndef CONFIG_USER_ONLY
1316         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1317         uint16_t event_idx = supported_event_map[event];
1318         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1319         int64_t overflow_in;
1320 
1321         if (!pmevcntr_is_64_bit(env, counter)) {
1322             delta = (uint32_t)delta;
1323         }
1324         overflow_in = pm_events[event_idx].ns_per_count(delta);
1325 
1326         if (overflow_in > 0) {
1327             int64_t overflow_at;
1328 
1329             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1330                                  overflow_in, &overflow_at)) {
1331                 ARMCPU *cpu = env_archcpu(env);
1332                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1333             }
1334         }
1335 #endif
1336 
1337         env->cp15.c14_pmevcntr_delta[counter] -=
1338             env->cp15.c14_pmevcntr[counter];
1339     }
1340 }
1341 
1342 void pmu_op_start(CPUARMState *env)
1343 {
1344     unsigned int i;
1345     pmccntr_op_start(env);
1346     for (i = 0; i < pmu_num_counters(env); i++) {
1347         pmevcntr_op_start(env, i);
1348     }
1349 }
1350 
1351 void pmu_op_finish(CPUARMState *env)
1352 {
1353     unsigned int i;
1354     pmccntr_op_finish(env);
1355     for (i = 0; i < pmu_num_counters(env); i++) {
1356         pmevcntr_op_finish(env, i);
1357     }
1358 }
1359 
1360 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1361 {
1362     pmu_op_start(&cpu->env);
1363 }
1364 
1365 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1366 {
1367     pmu_op_finish(&cpu->env);
1368 }
1369 
1370 void arm_pmu_timer_cb(void *opaque)
1371 {
1372     ARMCPU *cpu = opaque;
1373 
1374     /*
1375      * Update all the counter values based on the current underlying counts,
1376      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378      * counter may expire.
1379      */
1380     pmu_op_start(&cpu->env);
1381     pmu_op_finish(&cpu->env);
1382 }
1383 
1384 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385                        uint64_t value)
1386 {
1387     pmu_op_start(env);
1388 
1389     if (value & PMCRC) {
1390         /* The counter has been reset */
1391         env->cp15.c15_ccnt = 0;
1392     }
1393 
1394     if (value & PMCRP) {
1395         unsigned int i;
1396         for (i = 0; i < pmu_num_counters(env); i++) {
1397             env->cp15.c14_pmevcntr[i] = 0;
1398         }
1399     }
1400 
1401     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1403 
1404     pmu_op_finish(env);
1405 }
1406 
1407 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408                           uint64_t value)
1409 {
1410     unsigned int i;
1411     uint64_t overflow_mask, new_pmswinc;
1412 
1413     for (i = 0; i < pmu_num_counters(env); i++) {
1414         /* Increment a counter's count iff: */
1415         if ((value & (1 << i)) && /* counter's bit is set */
1416                 /* counter is enabled and not filtered */
1417                 pmu_counter_enabled(env, i) &&
1418                 /* counter is SW_INCR */
1419                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1420             pmevcntr_op_start(env, i);
1421 
1422             /*
1423              * Detect if this write causes an overflow since we can't predict
1424              * PMSWINC overflows like we can for other events
1425              */
1426             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1427 
1428             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1429                 1ULL << 63 : 1ULL << 31;
1430 
1431             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1432                 env->cp15.c9_pmovsr |= (1 << i);
1433                 pmu_update_irq(env);
1434             }
1435 
1436             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1437 
1438             pmevcntr_op_finish(env, i);
1439         }
1440     }
1441 }
1442 
1443 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1444 {
1445     uint64_t ret;
1446     pmccntr_op_start(env);
1447     ret = env->cp15.c15_ccnt;
1448     pmccntr_op_finish(env);
1449     return ret;
1450 }
1451 
1452 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                          uint64_t value)
1454 {
1455     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1456      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1457      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1458      * accessed.
1459      */
1460     env->cp15.c9_pmselr = value & 0x1f;
1461 }
1462 
1463 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1464                         uint64_t value)
1465 {
1466     pmccntr_op_start(env);
1467     env->cp15.c15_ccnt = value;
1468     pmccntr_op_finish(env);
1469 }
1470 
1471 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1472                             uint64_t value)
1473 {
1474     uint64_t cur_val = pmccntr_read(env, NULL);
1475 
1476     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1477 }
1478 
1479 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1480                             uint64_t value)
1481 {
1482     pmccntr_op_start(env);
1483     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1484     pmccntr_op_finish(env);
1485 }
1486 
1487 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1488                             uint64_t value)
1489 {
1490     pmccntr_op_start(env);
1491     /* M is not accessible from AArch32 */
1492     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1493         (value & PMCCFILTR);
1494     pmccntr_op_finish(env);
1495 }
1496 
1497 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1498 {
1499     /* M is not visible in AArch32 */
1500     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1501 }
1502 
1503 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                             uint64_t value)
1505 {
1506     pmu_op_start(env);
1507     value &= pmu_counter_mask(env);
1508     env->cp15.c9_pmcnten |= value;
1509     pmu_op_finish(env);
1510 }
1511 
1512 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513                              uint64_t value)
1514 {
1515     pmu_op_start(env);
1516     value &= pmu_counter_mask(env);
1517     env->cp15.c9_pmcnten &= ~value;
1518     pmu_op_finish(env);
1519 }
1520 
1521 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1522                          uint64_t value)
1523 {
1524     value &= pmu_counter_mask(env);
1525     env->cp15.c9_pmovsr &= ~value;
1526     pmu_update_irq(env);
1527 }
1528 
1529 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1530                          uint64_t value)
1531 {
1532     value &= pmu_counter_mask(env);
1533     env->cp15.c9_pmovsr |= value;
1534     pmu_update_irq(env);
1535 }
1536 
1537 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1538                              uint64_t value, const uint8_t counter)
1539 {
1540     if (counter == 31) {
1541         pmccfiltr_write(env, ri, value);
1542     } else if (counter < pmu_num_counters(env)) {
1543         pmevcntr_op_start(env, counter);
1544 
1545         /*
1546          * If this counter's event type is changing, store the current
1547          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1548          * pmevcntr_op_finish has the correct baseline when it converts back to
1549          * a delta.
1550          */
1551         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1552             PMXEVTYPER_EVTCOUNT;
1553         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1554         if (old_event != new_event) {
1555             uint64_t count = 0;
1556             if (event_supported(new_event)) {
1557                 uint16_t event_idx = supported_event_map[new_event];
1558                 count = pm_events[event_idx].get_count(env);
1559             }
1560             env->cp15.c14_pmevcntr_delta[counter] = count;
1561         }
1562 
1563         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1564         pmevcntr_op_finish(env, counter);
1565     }
1566     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1567      * PMSELR value is equal to or greater than the number of implemented
1568      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1569      */
1570 }
1571 
1572 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1573                                const uint8_t counter)
1574 {
1575     if (counter == 31) {
1576         return env->cp15.pmccfiltr_el0;
1577     } else if (counter < pmu_num_counters(env)) {
1578         return env->cp15.c14_pmevtyper[counter];
1579     } else {
1580       /*
1581        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1582        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1583        */
1584         return 0;
1585     }
1586 }
1587 
1588 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1589                               uint64_t value)
1590 {
1591     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1592     pmevtyper_write(env, ri, value, counter);
1593 }
1594 
1595 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1596                                uint64_t value)
1597 {
1598     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1599     env->cp15.c14_pmevtyper[counter] = value;
1600 
1601     /*
1602      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1603      * pmu_op_finish calls when loading saved state for a migration. Because
1604      * we're potentially updating the type of event here, the value written to
1605      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1606      * different counter type. Therefore, we need to set this value to the
1607      * current count for the counter type we're writing so that pmu_op_finish
1608      * has the correct count for its calculation.
1609      */
1610     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1611     if (event_supported(event)) {
1612         uint16_t event_idx = supported_event_map[event];
1613         env->cp15.c14_pmevcntr_delta[counter] =
1614             pm_events[event_idx].get_count(env);
1615     }
1616 }
1617 
1618 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1619 {
1620     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1621     return pmevtyper_read(env, ri, counter);
1622 }
1623 
1624 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1625                              uint64_t value)
1626 {
1627     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1628 }
1629 
1630 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1631 {
1632     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1633 }
1634 
1635 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                              uint64_t value, uint8_t counter)
1637 {
1638     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1639         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1640         value &= MAKE_64BIT_MASK(0, 32);
1641     }
1642     if (counter < pmu_num_counters(env)) {
1643         pmevcntr_op_start(env, counter);
1644         env->cp15.c14_pmevcntr[counter] = value;
1645         pmevcntr_op_finish(env, counter);
1646     }
1647     /*
1648      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1649      * are CONSTRAINED UNPREDICTABLE.
1650      */
1651 }
1652 
1653 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1654                               uint8_t counter)
1655 {
1656     if (counter < pmu_num_counters(env)) {
1657         uint64_t ret;
1658         pmevcntr_op_start(env, counter);
1659         ret = env->cp15.c14_pmevcntr[counter];
1660         pmevcntr_op_finish(env, counter);
1661         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1662             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1663             ret &= MAKE_64BIT_MASK(0, 32);
1664         }
1665         return ret;
1666     } else {
1667       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1668        * are CONSTRAINED UNPREDICTABLE. */
1669         return 0;
1670     }
1671 }
1672 
1673 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1674                              uint64_t value)
1675 {
1676     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1677     pmevcntr_write(env, ri, value, counter);
1678 }
1679 
1680 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1681 {
1682     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683     return pmevcntr_read(env, ri, counter);
1684 }
1685 
1686 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1687                              uint64_t value)
1688 {
1689     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1690     assert(counter < pmu_num_counters(env));
1691     env->cp15.c14_pmevcntr[counter] = value;
1692     pmevcntr_write(env, ri, value, counter);
1693 }
1694 
1695 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1696 {
1697     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1698     assert(counter < pmu_num_counters(env));
1699     return env->cp15.c14_pmevcntr[counter];
1700 }
1701 
1702 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1703                              uint64_t value)
1704 {
1705     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1706 }
1707 
1708 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1709 {
1710     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1711 }
1712 
1713 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1714                             uint64_t value)
1715 {
1716     if (arm_feature(env, ARM_FEATURE_V8)) {
1717         env->cp15.c9_pmuserenr = value & 0xf;
1718     } else {
1719         env->cp15.c9_pmuserenr = value & 1;
1720     }
1721 }
1722 
1723 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724                              uint64_t value)
1725 {
1726     /* We have no event counters so only the C bit can be changed */
1727     value &= pmu_counter_mask(env);
1728     env->cp15.c9_pminten |= value;
1729     pmu_update_irq(env);
1730 }
1731 
1732 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733                              uint64_t value)
1734 {
1735     value &= pmu_counter_mask(env);
1736     env->cp15.c9_pminten &= ~value;
1737     pmu_update_irq(env);
1738 }
1739 
1740 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741                        uint64_t value)
1742 {
1743     /* Note that even though the AArch64 view of this register has bits
1744      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1745      * architectural requirements for bits which are RES0 only in some
1746      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1747      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1748      */
1749     raw_write(env, ri, value & ~0x1FULL);
1750 }
1751 
1752 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1753 {
1754     /* Begin with base v8.0 state.  */
1755     uint64_t valid_mask = 0x3fff;
1756     ARMCPU *cpu = env_archcpu(env);
1757     uint64_t changed;
1758 
1759     /*
1760      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1761      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1762      * Instead, choose the format based on the mode of EL3.
1763      */
1764     if (arm_el_is_aa64(env, 3)) {
1765         value |= SCR_FW | SCR_AW;      /* RES1 */
1766         valid_mask &= ~SCR_NET;        /* RES0 */
1767 
1768         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1769             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1770             value |= SCR_RW;           /* RAO/WI */
1771         }
1772         if (cpu_isar_feature(aa64_ras, cpu)) {
1773             valid_mask |= SCR_TERR;
1774         }
1775         if (cpu_isar_feature(aa64_lor, cpu)) {
1776             valid_mask |= SCR_TLOR;
1777         }
1778         if (cpu_isar_feature(aa64_pauth, cpu)) {
1779             valid_mask |= SCR_API | SCR_APK;
1780         }
1781         if (cpu_isar_feature(aa64_sel2, cpu)) {
1782             valid_mask |= SCR_EEL2;
1783         }
1784         if (cpu_isar_feature(aa64_mte, cpu)) {
1785             valid_mask |= SCR_ATA;
1786         }
1787         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1788             valid_mask |= SCR_ENSCXT;
1789         }
1790         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1791             valid_mask |= SCR_EASE | SCR_NMEA;
1792         }
1793         if (cpu_isar_feature(aa64_sme, cpu)) {
1794             valid_mask |= SCR_ENTP2;
1795         }
1796     } else {
1797         valid_mask &= ~(SCR_RW | SCR_ST);
1798         if (cpu_isar_feature(aa32_ras, cpu)) {
1799             valid_mask |= SCR_TERR;
1800         }
1801     }
1802 
1803     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1804         valid_mask &= ~SCR_HCE;
1805 
1806         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1807          * supported if EL2 exists. The bit is UNK/SBZP when
1808          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1809          * when EL2 is unavailable.
1810          * On ARMv8, this bit is always available.
1811          */
1812         if (arm_feature(env, ARM_FEATURE_V7) &&
1813             !arm_feature(env, ARM_FEATURE_V8)) {
1814             valid_mask &= ~SCR_SMD;
1815         }
1816     }
1817 
1818     /* Clear all-context RES0 bits.  */
1819     value &= valid_mask;
1820     changed = env->cp15.scr_el3 ^ value;
1821     env->cp15.scr_el3 = value;
1822 
1823     /*
1824      * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1825      * we must invalidate all TLBs below EL3.
1826      */
1827     if (changed & SCR_NS) {
1828         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1829                                            ARMMMUIdxBit_E20_0 |
1830                                            ARMMMUIdxBit_E10_1 |
1831                                            ARMMMUIdxBit_E20_2 |
1832                                            ARMMMUIdxBit_E10_1_PAN |
1833                                            ARMMMUIdxBit_E20_2_PAN |
1834                                            ARMMMUIdxBit_E2));
1835     }
1836 }
1837 
1838 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1839 {
1840     /*
1841      * scr_write will set the RES1 bits on an AArch64-only CPU.
1842      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1843      */
1844     scr_write(env, ri, 0);
1845 }
1846 
1847 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1848                                        const ARMCPRegInfo *ri,
1849                                        bool isread)
1850 {
1851     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1852         return CP_ACCESS_TRAP_EL2;
1853     }
1854 
1855     return CP_ACCESS_OK;
1856 }
1857 
1858 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1859 {
1860     ARMCPU *cpu = env_archcpu(env);
1861 
1862     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1863      * bank
1864      */
1865     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1866                                         ri->secure & ARM_CP_SECSTATE_S);
1867 
1868     return cpu->ccsidr[index];
1869 }
1870 
1871 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1872                          uint64_t value)
1873 {
1874     raw_write(env, ri, value & 0xf);
1875 }
1876 
1877 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1878 {
1879     CPUState *cs = env_cpu(env);
1880     bool el1 = arm_current_el(env) == 1;
1881     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1882     uint64_t ret = 0;
1883 
1884     if (hcr_el2 & HCR_IMO) {
1885         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1886             ret |= CPSR_I;
1887         }
1888     } else {
1889         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1890             ret |= CPSR_I;
1891         }
1892     }
1893 
1894     if (hcr_el2 & HCR_FMO) {
1895         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1896             ret |= CPSR_F;
1897         }
1898     } else {
1899         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1900             ret |= CPSR_F;
1901         }
1902     }
1903 
1904     if (hcr_el2 & HCR_AMO) {
1905         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1906             ret |= CPSR_A;
1907         }
1908     }
1909 
1910     return ret;
1911 }
1912 
1913 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1914                                        bool isread)
1915 {
1916     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1917         return CP_ACCESS_TRAP_EL2;
1918     }
1919 
1920     return CP_ACCESS_OK;
1921 }
1922 
1923 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1924                                        bool isread)
1925 {
1926     if (arm_feature(env, ARM_FEATURE_V8)) {
1927         return access_aa64_tid1(env, ri, isread);
1928     }
1929 
1930     return CP_ACCESS_OK;
1931 }
1932 
1933 static const ARMCPRegInfo v7_cp_reginfo[] = {
1934     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1935     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1936       .access = PL1_W, .type = ARM_CP_NOP },
1937     /* Performance monitors are implementation defined in v7,
1938      * but with an ARM recommended set of registers, which we
1939      * follow.
1940      *
1941      * Performance registers fall into three categories:
1942      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1943      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1944      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1945      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1946      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1947      */
1948     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1949       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1950       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1951       .writefn = pmcntenset_write,
1952       .accessfn = pmreg_access,
1953       .raw_writefn = raw_write },
1954     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1955       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1956       .access = PL0_RW, .accessfn = pmreg_access,
1957       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1958       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1959     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1960       .access = PL0_RW,
1961       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1962       .accessfn = pmreg_access,
1963       .writefn = pmcntenclr_write,
1964       .type = ARM_CP_ALIAS | ARM_CP_IO },
1965     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1966       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1967       .access = PL0_RW, .accessfn = pmreg_access,
1968       .type = ARM_CP_ALIAS | ARM_CP_IO,
1969       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1970       .writefn = pmcntenclr_write },
1971     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1972       .access = PL0_RW, .type = ARM_CP_IO,
1973       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1974       .accessfn = pmreg_access,
1975       .writefn = pmovsr_write,
1976       .raw_writefn = raw_write },
1977     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1978       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1979       .access = PL0_RW, .accessfn = pmreg_access,
1980       .type = ARM_CP_ALIAS | ARM_CP_IO,
1981       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1982       .writefn = pmovsr_write,
1983       .raw_writefn = raw_write },
1984     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1985       .access = PL0_W, .accessfn = pmreg_access_swinc,
1986       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1987       .writefn = pmswinc_write },
1988     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1989       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1990       .access = PL0_W, .accessfn = pmreg_access_swinc,
1991       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1992       .writefn = pmswinc_write },
1993     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1994       .access = PL0_RW, .type = ARM_CP_ALIAS,
1995       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1996       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1997       .raw_writefn = raw_write},
1998     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1999       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2000       .access = PL0_RW, .accessfn = pmreg_access_selr,
2001       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2002       .writefn = pmselr_write, .raw_writefn = raw_write, },
2003     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2004       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2005       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2006       .accessfn = pmreg_access_ccntr },
2007     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2008       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2009       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2010       .type = ARM_CP_IO,
2011       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2012       .readfn = pmccntr_read, .writefn = pmccntr_write,
2013       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2014     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2015       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2016       .access = PL0_RW, .accessfn = pmreg_access,
2017       .type = ARM_CP_ALIAS | ARM_CP_IO,
2018       .resetvalue = 0, },
2019     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2020       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2021       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2022       .access = PL0_RW, .accessfn = pmreg_access,
2023       .type = ARM_CP_IO,
2024       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2025       .resetvalue = 0, },
2026     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2027       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2028       .accessfn = pmreg_access,
2029       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2030     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2031       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2032       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2033       .accessfn = pmreg_access,
2034       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2035     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2036       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2037       .accessfn = pmreg_access_xevcntr,
2038       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2039     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2040       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2041       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2042       .accessfn = pmreg_access_xevcntr,
2043       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2044     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2045       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2046       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2047       .resetvalue = 0,
2048       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2049     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2050       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2051       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2052       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2053       .resetvalue = 0,
2054       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2055     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2056       .access = PL1_RW, .accessfn = access_tpm,
2057       .type = ARM_CP_ALIAS | ARM_CP_IO,
2058       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2059       .resetvalue = 0,
2060       .writefn = pmintenset_write, .raw_writefn = raw_write },
2061     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2062       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2063       .access = PL1_RW, .accessfn = access_tpm,
2064       .type = ARM_CP_IO,
2065       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2066       .writefn = pmintenset_write, .raw_writefn = raw_write,
2067       .resetvalue = 0x0 },
2068     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2069       .access = PL1_RW, .accessfn = access_tpm,
2070       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2071       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2072       .writefn = pmintenclr_write, },
2073     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2074       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2075       .access = PL1_RW, .accessfn = access_tpm,
2076       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2077       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2078       .writefn = pmintenclr_write },
2079     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2080       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2081       .access = PL1_R,
2082       .accessfn = access_aa64_tid2,
2083       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2084     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2085       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2086       .access = PL1_RW,
2087       .accessfn = access_aa64_tid2,
2088       .writefn = csselr_write, .resetvalue = 0,
2089       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2090                              offsetof(CPUARMState, cp15.csselr_ns) } },
2091     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2092      * just RAZ for all cores:
2093      */
2094     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2095       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2096       .access = PL1_R, .type = ARM_CP_CONST,
2097       .accessfn = access_aa64_tid1,
2098       .resetvalue = 0 },
2099     /* Auxiliary fault status registers: these also are IMPDEF, and we
2100      * choose to RAZ/WI for all cores.
2101      */
2102     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2103       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2104       .access = PL1_RW, .accessfn = access_tvm_trvm,
2105       .type = ARM_CP_CONST, .resetvalue = 0 },
2106     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2107       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2108       .access = PL1_RW, .accessfn = access_tvm_trvm,
2109       .type = ARM_CP_CONST, .resetvalue = 0 },
2110     /* MAIR can just read-as-written because we don't implement caches
2111      * and so don't need to care about memory attributes.
2112      */
2113     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2114       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2115       .access = PL1_RW, .accessfn = access_tvm_trvm,
2116       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2117       .resetvalue = 0 },
2118     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2119       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2120       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2121       .resetvalue = 0 },
2122     /* For non-long-descriptor page tables these are PRRR and NMRR;
2123      * regardless they still act as reads-as-written for QEMU.
2124      */
2125      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2126       * allows them to assign the correct fieldoffset based on the endianness
2127       * handled in the field definitions.
2128       */
2129     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2130       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2131       .access = PL1_RW, .accessfn = access_tvm_trvm,
2132       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2133                              offsetof(CPUARMState, cp15.mair0_ns) },
2134       .resetfn = arm_cp_reset_ignore },
2135     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2136       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2137       .access = PL1_RW, .accessfn = access_tvm_trvm,
2138       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2139                              offsetof(CPUARMState, cp15.mair1_ns) },
2140       .resetfn = arm_cp_reset_ignore },
2141     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2142       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2143       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2144     /* 32 bit ITLB invalidates */
2145     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2146       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2147       .writefn = tlbiall_write },
2148     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2149       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2150       .writefn = tlbimva_write },
2151     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2152       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2153       .writefn = tlbiasid_write },
2154     /* 32 bit DTLB invalidates */
2155     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2156       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2157       .writefn = tlbiall_write },
2158     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2159       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2160       .writefn = tlbimva_write },
2161     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2162       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2163       .writefn = tlbiasid_write },
2164     /* 32 bit TLB invalidates */
2165     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2166       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2167       .writefn = tlbiall_write },
2168     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2169       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2170       .writefn = tlbimva_write },
2171     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2172       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2173       .writefn = tlbiasid_write },
2174     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2175       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2176       .writefn = tlbimvaa_write },
2177 };
2178 
2179 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2180     /* 32 bit TLB invalidates, Inner Shareable */
2181     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2182       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2183       .writefn = tlbiall_is_write },
2184     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2185       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2186       .writefn = tlbimva_is_write },
2187     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2188       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2189       .writefn = tlbiasid_is_write },
2190     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2191       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2192       .writefn = tlbimvaa_is_write },
2193 };
2194 
2195 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2196     /* PMOVSSET is not implemented in v7 before v7ve */
2197     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2198       .access = PL0_RW, .accessfn = pmreg_access,
2199       .type = ARM_CP_ALIAS | ARM_CP_IO,
2200       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2201       .writefn = pmovsset_write,
2202       .raw_writefn = raw_write },
2203     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2204       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2205       .access = PL0_RW, .accessfn = pmreg_access,
2206       .type = ARM_CP_ALIAS | ARM_CP_IO,
2207       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2208       .writefn = pmovsset_write,
2209       .raw_writefn = raw_write },
2210 };
2211 
2212 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2213                         uint64_t value)
2214 {
2215     value &= 1;
2216     env->teecr = value;
2217 }
2218 
2219 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2220                                    bool isread)
2221 {
2222     /*
2223      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2224      * at all, so we don't need to check whether we're v8A.
2225      */
2226     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2227         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2228         return CP_ACCESS_TRAP_EL2;
2229     }
2230     return CP_ACCESS_OK;
2231 }
2232 
2233 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2234                                     bool isread)
2235 {
2236     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2237         return CP_ACCESS_TRAP;
2238     }
2239     return teecr_access(env, ri, isread);
2240 }
2241 
2242 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2243     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2244       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2245       .resetvalue = 0,
2246       .writefn = teecr_write, .accessfn = teecr_access },
2247     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2248       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2249       .accessfn = teehbr_access, .resetvalue = 0 },
2250 };
2251 
2252 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2253     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2254       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2255       .access = PL0_RW,
2256       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2257     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2258       .access = PL0_RW,
2259       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2260                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2261       .resetfn = arm_cp_reset_ignore },
2262     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2263       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2264       .access = PL0_R|PL1_W,
2265       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2266       .resetvalue = 0},
2267     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2268       .access = PL0_R|PL1_W,
2269       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2270                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2271       .resetfn = arm_cp_reset_ignore },
2272     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2273       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2274       .access = PL1_RW,
2275       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2276     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2277       .access = PL1_RW,
2278       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2279                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2280       .resetvalue = 0 },
2281 };
2282 
2283 #ifndef CONFIG_USER_ONLY
2284 
2285 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2286                                        bool isread)
2287 {
2288     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2289      * Writable only at the highest implemented exception level.
2290      */
2291     int el = arm_current_el(env);
2292     uint64_t hcr;
2293     uint32_t cntkctl;
2294 
2295     switch (el) {
2296     case 0:
2297         hcr = arm_hcr_el2_eff(env);
2298         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2299             cntkctl = env->cp15.cnthctl_el2;
2300         } else {
2301             cntkctl = env->cp15.c14_cntkctl;
2302         }
2303         if (!extract32(cntkctl, 0, 2)) {
2304             return CP_ACCESS_TRAP;
2305         }
2306         break;
2307     case 1:
2308         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2309             arm_is_secure_below_el3(env)) {
2310             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2311             return CP_ACCESS_TRAP_UNCATEGORIZED;
2312         }
2313         break;
2314     case 2:
2315     case 3:
2316         break;
2317     }
2318 
2319     if (!isread && el < arm_highest_el(env)) {
2320         return CP_ACCESS_TRAP_UNCATEGORIZED;
2321     }
2322 
2323     return CP_ACCESS_OK;
2324 }
2325 
2326 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2327                                         bool isread)
2328 {
2329     unsigned int cur_el = arm_current_el(env);
2330     bool has_el2 = arm_is_el2_enabled(env);
2331     uint64_t hcr = arm_hcr_el2_eff(env);
2332 
2333     switch (cur_el) {
2334     case 0:
2335         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2336         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2337             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2338                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2339         }
2340 
2341         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2342         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2343             return CP_ACCESS_TRAP;
2344         }
2345 
2346         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2347         if (hcr & HCR_E2H) {
2348             if (timeridx == GTIMER_PHYS &&
2349                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2350                 return CP_ACCESS_TRAP_EL2;
2351             }
2352         } else {
2353             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2354             if (has_el2 && timeridx == GTIMER_PHYS &&
2355                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2356                 return CP_ACCESS_TRAP_EL2;
2357             }
2358         }
2359         break;
2360 
2361     case 1:
2362         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2363         if (has_el2 && timeridx == GTIMER_PHYS &&
2364             (hcr & HCR_E2H
2365              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2366              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2367             return CP_ACCESS_TRAP_EL2;
2368         }
2369         break;
2370     }
2371     return CP_ACCESS_OK;
2372 }
2373 
2374 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2375                                       bool isread)
2376 {
2377     unsigned int cur_el = arm_current_el(env);
2378     bool has_el2 = arm_is_el2_enabled(env);
2379     uint64_t hcr = arm_hcr_el2_eff(env);
2380 
2381     switch (cur_el) {
2382     case 0:
2383         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2384             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2385             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2386                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2387         }
2388 
2389         /*
2390          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2391          * EL0 if EL0[PV]TEN is zero.
2392          */
2393         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2394             return CP_ACCESS_TRAP;
2395         }
2396         /* fall through */
2397 
2398     case 1:
2399         if (has_el2 && timeridx == GTIMER_PHYS) {
2400             if (hcr & HCR_E2H) {
2401                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2402                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2403                     return CP_ACCESS_TRAP_EL2;
2404                 }
2405             } else {
2406                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2407                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2408                     return CP_ACCESS_TRAP_EL2;
2409                 }
2410             }
2411         }
2412         break;
2413     }
2414     return CP_ACCESS_OK;
2415 }
2416 
2417 static CPAccessResult gt_pct_access(CPUARMState *env,
2418                                     const ARMCPRegInfo *ri,
2419                                     bool isread)
2420 {
2421     return gt_counter_access(env, GTIMER_PHYS, isread);
2422 }
2423 
2424 static CPAccessResult gt_vct_access(CPUARMState *env,
2425                                     const ARMCPRegInfo *ri,
2426                                     bool isread)
2427 {
2428     return gt_counter_access(env, GTIMER_VIRT, isread);
2429 }
2430 
2431 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2432                                        bool isread)
2433 {
2434     return gt_timer_access(env, GTIMER_PHYS, isread);
2435 }
2436 
2437 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2438                                        bool isread)
2439 {
2440     return gt_timer_access(env, GTIMER_VIRT, isread);
2441 }
2442 
2443 static CPAccessResult gt_stimer_access(CPUARMState *env,
2444                                        const ARMCPRegInfo *ri,
2445                                        bool isread)
2446 {
2447     /* The AArch64 register view of the secure physical timer is
2448      * always accessible from EL3, and configurably accessible from
2449      * Secure EL1.
2450      */
2451     switch (arm_current_el(env)) {
2452     case 1:
2453         if (!arm_is_secure(env)) {
2454             return CP_ACCESS_TRAP;
2455         }
2456         if (!(env->cp15.scr_el3 & SCR_ST)) {
2457             return CP_ACCESS_TRAP_EL3;
2458         }
2459         return CP_ACCESS_OK;
2460     case 0:
2461     case 2:
2462         return CP_ACCESS_TRAP;
2463     case 3:
2464         return CP_ACCESS_OK;
2465     default:
2466         g_assert_not_reached();
2467     }
2468 }
2469 
2470 static uint64_t gt_get_countervalue(CPUARMState *env)
2471 {
2472     ARMCPU *cpu = env_archcpu(env);
2473 
2474     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2475 }
2476 
2477 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2478 {
2479     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2480 
2481     if (gt->ctl & 1) {
2482         /* Timer enabled: calculate and set current ISTATUS, irq, and
2483          * reset timer to when ISTATUS next has to change
2484          */
2485         uint64_t offset = timeridx == GTIMER_VIRT ?
2486                                       cpu->env.cp15.cntvoff_el2 : 0;
2487         uint64_t count = gt_get_countervalue(&cpu->env);
2488         /* Note that this must be unsigned 64 bit arithmetic: */
2489         int istatus = count - offset >= gt->cval;
2490         uint64_t nexttick;
2491         int irqstate;
2492 
2493         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2494 
2495         irqstate = (istatus && !(gt->ctl & 2));
2496         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2497 
2498         if (istatus) {
2499             /* Next transition is when count rolls back over to zero */
2500             nexttick = UINT64_MAX;
2501         } else {
2502             /* Next transition is when we hit cval */
2503             nexttick = gt->cval + offset;
2504         }
2505         /* Note that the desired next expiry time might be beyond the
2506          * signed-64-bit range of a QEMUTimer -- in this case we just
2507          * set the timer for as far in the future as possible. When the
2508          * timer expires we will reset the timer for any remaining period.
2509          */
2510         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2511             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2512         } else {
2513             timer_mod(cpu->gt_timer[timeridx], nexttick);
2514         }
2515         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2516     } else {
2517         /* Timer disabled: ISTATUS and timer output always clear */
2518         gt->ctl &= ~4;
2519         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2520         timer_del(cpu->gt_timer[timeridx]);
2521         trace_arm_gt_recalc_disabled(timeridx);
2522     }
2523 }
2524 
2525 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2526                            int timeridx)
2527 {
2528     ARMCPU *cpu = env_archcpu(env);
2529 
2530     timer_del(cpu->gt_timer[timeridx]);
2531 }
2532 
2533 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2534 {
2535     return gt_get_countervalue(env);
2536 }
2537 
2538 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2539 {
2540     uint64_t hcr;
2541 
2542     switch (arm_current_el(env)) {
2543     case 2:
2544         hcr = arm_hcr_el2_eff(env);
2545         if (hcr & HCR_E2H) {
2546             return 0;
2547         }
2548         break;
2549     case 0:
2550         hcr = arm_hcr_el2_eff(env);
2551         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2552             return 0;
2553         }
2554         break;
2555     }
2556 
2557     return env->cp15.cntvoff_el2;
2558 }
2559 
2560 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2561 {
2562     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2563 }
2564 
2565 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2566                           int timeridx,
2567                           uint64_t value)
2568 {
2569     trace_arm_gt_cval_write(timeridx, value);
2570     env->cp15.c14_timer[timeridx].cval = value;
2571     gt_recalc_timer(env_archcpu(env), timeridx);
2572 }
2573 
2574 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2575                              int timeridx)
2576 {
2577     uint64_t offset = 0;
2578 
2579     switch (timeridx) {
2580     case GTIMER_VIRT:
2581     case GTIMER_HYPVIRT:
2582         offset = gt_virt_cnt_offset(env);
2583         break;
2584     }
2585 
2586     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2587                       (gt_get_countervalue(env) - offset));
2588 }
2589 
2590 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2591                           int timeridx,
2592                           uint64_t value)
2593 {
2594     uint64_t offset = 0;
2595 
2596     switch (timeridx) {
2597     case GTIMER_VIRT:
2598     case GTIMER_HYPVIRT:
2599         offset = gt_virt_cnt_offset(env);
2600         break;
2601     }
2602 
2603     trace_arm_gt_tval_write(timeridx, value);
2604     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2605                                          sextract64(value, 0, 32);
2606     gt_recalc_timer(env_archcpu(env), timeridx);
2607 }
2608 
2609 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2610                          int timeridx,
2611                          uint64_t value)
2612 {
2613     ARMCPU *cpu = env_archcpu(env);
2614     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2615 
2616     trace_arm_gt_ctl_write(timeridx, value);
2617     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2618     if ((oldval ^ value) & 1) {
2619         /* Enable toggled */
2620         gt_recalc_timer(cpu, timeridx);
2621     } else if ((oldval ^ value) & 2) {
2622         /* IMASK toggled: don't need to recalculate,
2623          * just set the interrupt line based on ISTATUS
2624          */
2625         int irqstate = (oldval & 4) && !(value & 2);
2626 
2627         trace_arm_gt_imask_toggle(timeridx, irqstate);
2628         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2629     }
2630 }
2631 
2632 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2633 {
2634     gt_timer_reset(env, ri, GTIMER_PHYS);
2635 }
2636 
2637 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2638                                uint64_t value)
2639 {
2640     gt_cval_write(env, ri, GTIMER_PHYS, value);
2641 }
2642 
2643 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2644 {
2645     return gt_tval_read(env, ri, GTIMER_PHYS);
2646 }
2647 
2648 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2649                                uint64_t value)
2650 {
2651     gt_tval_write(env, ri, GTIMER_PHYS, value);
2652 }
2653 
2654 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655                               uint64_t value)
2656 {
2657     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2658 }
2659 
2660 static int gt_phys_redir_timeridx(CPUARMState *env)
2661 {
2662     switch (arm_mmu_idx(env)) {
2663     case ARMMMUIdx_E20_0:
2664     case ARMMMUIdx_E20_2:
2665     case ARMMMUIdx_E20_2_PAN:
2666         return GTIMER_HYP;
2667     default:
2668         return GTIMER_PHYS;
2669     }
2670 }
2671 
2672 static int gt_virt_redir_timeridx(CPUARMState *env)
2673 {
2674     switch (arm_mmu_idx(env)) {
2675     case ARMMMUIdx_E20_0:
2676     case ARMMMUIdx_E20_2:
2677     case ARMMMUIdx_E20_2_PAN:
2678         return GTIMER_HYPVIRT;
2679     default:
2680         return GTIMER_VIRT;
2681     }
2682 }
2683 
2684 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2685                                         const ARMCPRegInfo *ri)
2686 {
2687     int timeridx = gt_phys_redir_timeridx(env);
2688     return env->cp15.c14_timer[timeridx].cval;
2689 }
2690 
2691 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692                                      uint64_t value)
2693 {
2694     int timeridx = gt_phys_redir_timeridx(env);
2695     gt_cval_write(env, ri, timeridx, value);
2696 }
2697 
2698 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2699                                         const ARMCPRegInfo *ri)
2700 {
2701     int timeridx = gt_phys_redir_timeridx(env);
2702     return gt_tval_read(env, ri, timeridx);
2703 }
2704 
2705 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2706                                      uint64_t value)
2707 {
2708     int timeridx = gt_phys_redir_timeridx(env);
2709     gt_tval_write(env, ri, timeridx, value);
2710 }
2711 
2712 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2713                                        const ARMCPRegInfo *ri)
2714 {
2715     int timeridx = gt_phys_redir_timeridx(env);
2716     return env->cp15.c14_timer[timeridx].ctl;
2717 }
2718 
2719 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2720                                     uint64_t value)
2721 {
2722     int timeridx = gt_phys_redir_timeridx(env);
2723     gt_ctl_write(env, ri, timeridx, value);
2724 }
2725 
2726 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2727 {
2728     gt_timer_reset(env, ri, GTIMER_VIRT);
2729 }
2730 
2731 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2732                                uint64_t value)
2733 {
2734     gt_cval_write(env, ri, GTIMER_VIRT, value);
2735 }
2736 
2737 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2738 {
2739     return gt_tval_read(env, ri, GTIMER_VIRT);
2740 }
2741 
2742 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743                                uint64_t value)
2744 {
2745     gt_tval_write(env, ri, GTIMER_VIRT, value);
2746 }
2747 
2748 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2749                               uint64_t value)
2750 {
2751     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2752 }
2753 
2754 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2755                               uint64_t value)
2756 {
2757     ARMCPU *cpu = env_archcpu(env);
2758 
2759     trace_arm_gt_cntvoff_write(value);
2760     raw_write(env, ri, value);
2761     gt_recalc_timer(cpu, GTIMER_VIRT);
2762 }
2763 
2764 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2765                                         const ARMCPRegInfo *ri)
2766 {
2767     int timeridx = gt_virt_redir_timeridx(env);
2768     return env->cp15.c14_timer[timeridx].cval;
2769 }
2770 
2771 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2772                                      uint64_t value)
2773 {
2774     int timeridx = gt_virt_redir_timeridx(env);
2775     gt_cval_write(env, ri, timeridx, value);
2776 }
2777 
2778 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2779                                         const ARMCPRegInfo *ri)
2780 {
2781     int timeridx = gt_virt_redir_timeridx(env);
2782     return gt_tval_read(env, ri, timeridx);
2783 }
2784 
2785 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2786                                      uint64_t value)
2787 {
2788     int timeridx = gt_virt_redir_timeridx(env);
2789     gt_tval_write(env, ri, timeridx, value);
2790 }
2791 
2792 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2793                                        const ARMCPRegInfo *ri)
2794 {
2795     int timeridx = gt_virt_redir_timeridx(env);
2796     return env->cp15.c14_timer[timeridx].ctl;
2797 }
2798 
2799 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2800                                     uint64_t value)
2801 {
2802     int timeridx = gt_virt_redir_timeridx(env);
2803     gt_ctl_write(env, ri, timeridx, value);
2804 }
2805 
2806 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2807 {
2808     gt_timer_reset(env, ri, GTIMER_HYP);
2809 }
2810 
2811 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2812                               uint64_t value)
2813 {
2814     gt_cval_write(env, ri, GTIMER_HYP, value);
2815 }
2816 
2817 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2818 {
2819     return gt_tval_read(env, ri, GTIMER_HYP);
2820 }
2821 
2822 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823                               uint64_t value)
2824 {
2825     gt_tval_write(env, ri, GTIMER_HYP, value);
2826 }
2827 
2828 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2829                               uint64_t value)
2830 {
2831     gt_ctl_write(env, ri, GTIMER_HYP, value);
2832 }
2833 
2834 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2835 {
2836     gt_timer_reset(env, ri, GTIMER_SEC);
2837 }
2838 
2839 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2840                               uint64_t value)
2841 {
2842     gt_cval_write(env, ri, GTIMER_SEC, value);
2843 }
2844 
2845 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2846 {
2847     return gt_tval_read(env, ri, GTIMER_SEC);
2848 }
2849 
2850 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851                               uint64_t value)
2852 {
2853     gt_tval_write(env, ri, GTIMER_SEC, value);
2854 }
2855 
2856 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857                               uint64_t value)
2858 {
2859     gt_ctl_write(env, ri, GTIMER_SEC, value);
2860 }
2861 
2862 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2863 {
2864     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2865 }
2866 
2867 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2868                              uint64_t value)
2869 {
2870     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2871 }
2872 
2873 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2874 {
2875     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2876 }
2877 
2878 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2879                              uint64_t value)
2880 {
2881     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2882 }
2883 
2884 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2885                             uint64_t value)
2886 {
2887     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2888 }
2889 
2890 void arm_gt_ptimer_cb(void *opaque)
2891 {
2892     ARMCPU *cpu = opaque;
2893 
2894     gt_recalc_timer(cpu, GTIMER_PHYS);
2895 }
2896 
2897 void arm_gt_vtimer_cb(void *opaque)
2898 {
2899     ARMCPU *cpu = opaque;
2900 
2901     gt_recalc_timer(cpu, GTIMER_VIRT);
2902 }
2903 
2904 void arm_gt_htimer_cb(void *opaque)
2905 {
2906     ARMCPU *cpu = opaque;
2907 
2908     gt_recalc_timer(cpu, GTIMER_HYP);
2909 }
2910 
2911 void arm_gt_stimer_cb(void *opaque)
2912 {
2913     ARMCPU *cpu = opaque;
2914 
2915     gt_recalc_timer(cpu, GTIMER_SEC);
2916 }
2917 
2918 void arm_gt_hvtimer_cb(void *opaque)
2919 {
2920     ARMCPU *cpu = opaque;
2921 
2922     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2923 }
2924 
2925 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2926 {
2927     ARMCPU *cpu = env_archcpu(env);
2928 
2929     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2930 }
2931 
2932 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2933     /* Note that CNTFRQ is purely reads-as-written for the benefit
2934      * of software; writing it doesn't actually change the timer frequency.
2935      * Our reset value matches the fixed frequency we implement the timer at.
2936      */
2937     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2938       .type = ARM_CP_ALIAS,
2939       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2940       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2941     },
2942     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2943       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2944       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2945       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2946       .resetfn = arm_gt_cntfrq_reset,
2947     },
2948     /* overall control: mostly access permissions */
2949     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2950       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2951       .access = PL1_RW,
2952       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2953       .resetvalue = 0,
2954     },
2955     /* per-timer control */
2956     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2957       .secure = ARM_CP_SECSTATE_NS,
2958       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2959       .accessfn = gt_ptimer_access,
2960       .fieldoffset = offsetoflow32(CPUARMState,
2961                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2962       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2963       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2964     },
2965     { .name = "CNTP_CTL_S",
2966       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2967       .secure = ARM_CP_SECSTATE_S,
2968       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2969       .accessfn = gt_ptimer_access,
2970       .fieldoffset = offsetoflow32(CPUARMState,
2971                                    cp15.c14_timer[GTIMER_SEC].ctl),
2972       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2973     },
2974     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2975       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2976       .type = ARM_CP_IO, .access = PL0_RW,
2977       .accessfn = gt_ptimer_access,
2978       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2979       .resetvalue = 0,
2980       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2981       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2982     },
2983     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2984       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2985       .accessfn = gt_vtimer_access,
2986       .fieldoffset = offsetoflow32(CPUARMState,
2987                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2988       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2989       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2990     },
2991     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2992       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2993       .type = ARM_CP_IO, .access = PL0_RW,
2994       .accessfn = gt_vtimer_access,
2995       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2996       .resetvalue = 0,
2997       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2998       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2999     },
3000     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3001     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3002       .secure = ARM_CP_SECSTATE_NS,
3003       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3004       .accessfn = gt_ptimer_access,
3005       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3006     },
3007     { .name = "CNTP_TVAL_S",
3008       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3009       .secure = ARM_CP_SECSTATE_S,
3010       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3011       .accessfn = gt_ptimer_access,
3012       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3013     },
3014     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3015       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3016       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3017       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3018       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3019     },
3020     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3021       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3022       .accessfn = gt_vtimer_access,
3023       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3024     },
3025     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3026       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3027       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3028       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3029       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3030     },
3031     /* The counter itself */
3032     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3033       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3034       .accessfn = gt_pct_access,
3035       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3036     },
3037     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3038       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3039       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3040       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3041     },
3042     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3043       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3044       .accessfn = gt_vct_access,
3045       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3046     },
3047     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3048       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3049       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3050       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3051     },
3052     /* Comparison value, indicating when the timer goes off */
3053     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3054       .secure = ARM_CP_SECSTATE_NS,
3055       .access = PL0_RW,
3056       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3057       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3058       .accessfn = gt_ptimer_access,
3059       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3060       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3061     },
3062     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3063       .secure = ARM_CP_SECSTATE_S,
3064       .access = PL0_RW,
3065       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3066       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3067       .accessfn = gt_ptimer_access,
3068       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3069     },
3070     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3071       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3072       .access = PL0_RW,
3073       .type = ARM_CP_IO,
3074       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3075       .resetvalue = 0, .accessfn = gt_ptimer_access,
3076       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3077       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3078     },
3079     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3080       .access = PL0_RW,
3081       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3082       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3083       .accessfn = gt_vtimer_access,
3084       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3085       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3086     },
3087     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3088       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3089       .access = PL0_RW,
3090       .type = ARM_CP_IO,
3091       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3092       .resetvalue = 0, .accessfn = gt_vtimer_access,
3093       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3094       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3095     },
3096     /* Secure timer -- this is actually restricted to only EL3
3097      * and configurably Secure-EL1 via the accessfn.
3098      */
3099     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3100       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3101       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3102       .accessfn = gt_stimer_access,
3103       .readfn = gt_sec_tval_read,
3104       .writefn = gt_sec_tval_write,
3105       .resetfn = gt_sec_timer_reset,
3106     },
3107     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3108       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3109       .type = ARM_CP_IO, .access = PL1_RW,
3110       .accessfn = gt_stimer_access,
3111       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3112       .resetvalue = 0,
3113       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3114     },
3115     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3116       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3117       .type = ARM_CP_IO, .access = PL1_RW,
3118       .accessfn = gt_stimer_access,
3119       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3120       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3121     },
3122 };
3123 
3124 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3125                                  bool isread)
3126 {
3127     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3128         return CP_ACCESS_TRAP;
3129     }
3130     return CP_ACCESS_OK;
3131 }
3132 
3133 #else
3134 
3135 /* In user-mode most of the generic timer registers are inaccessible
3136  * however modern kernels (4.12+) allow access to cntvct_el0
3137  */
3138 
3139 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3140 {
3141     ARMCPU *cpu = env_archcpu(env);
3142 
3143     /* Currently we have no support for QEMUTimer in linux-user so we
3144      * can't call gt_get_countervalue(env), instead we directly
3145      * call the lower level functions.
3146      */
3147     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3148 }
3149 
3150 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3151     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3152       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3153       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3154       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3155       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3156     },
3157     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3158       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3159       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3160       .readfn = gt_virt_cnt_read,
3161     },
3162 };
3163 
3164 #endif
3165 
3166 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3167 {
3168     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3169         raw_write(env, ri, value);
3170     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3171         raw_write(env, ri, value & 0xfffff6ff);
3172     } else {
3173         raw_write(env, ri, value & 0xfffff1ff);
3174     }
3175 }
3176 
3177 #ifndef CONFIG_USER_ONLY
3178 /* get_phys_addr() isn't present for user-mode-only targets */
3179 
3180 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3181                                  bool isread)
3182 {
3183     if (ri->opc2 & 4) {
3184         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3185          * Secure EL1 (which can only happen if EL3 is AArch64).
3186          * They are simply UNDEF if executed from NS EL1.
3187          * They function normally from EL2 or EL3.
3188          */
3189         if (arm_current_el(env) == 1) {
3190             if (arm_is_secure_below_el3(env)) {
3191                 if (env->cp15.scr_el3 & SCR_EEL2) {
3192                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3193                 }
3194                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3195             }
3196             return CP_ACCESS_TRAP_UNCATEGORIZED;
3197         }
3198     }
3199     return CP_ACCESS_OK;
3200 }
3201 
3202 #ifdef CONFIG_TCG
3203 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3204                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3205                              bool is_secure)
3206 {
3207     bool ret;
3208     uint64_t par64;
3209     bool format64 = false;
3210     ARMMMUFaultInfo fi = {};
3211     GetPhysAddrResult res = {};
3212 
3213     ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3214                                     is_secure, &res, &fi);
3215 
3216     /*
3217      * ATS operations only do S1 or S1+S2 translations, so we never
3218      * have to deal with the ARMCacheAttrs format for S2 only.
3219      */
3220     assert(!res.cacheattrs.is_s2_format);
3221 
3222     if (ret) {
3223         /*
3224          * Some kinds of translation fault must cause exceptions rather
3225          * than being reported in the PAR.
3226          */
3227         int current_el = arm_current_el(env);
3228         int target_el;
3229         uint32_t syn, fsr, fsc;
3230         bool take_exc = false;
3231 
3232         if (fi.s1ptw && current_el == 1
3233             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3234             /*
3235              * Synchronous stage 2 fault on an access made as part of the
3236              * translation table walk for AT S1E0* or AT S1E1* insn
3237              * executed from NS EL1. If this is a synchronous external abort
3238              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3239              * to EL3. Otherwise the fault is taken as an exception to EL2,
3240              * and HPFAR_EL2 holds the faulting IPA.
3241              */
3242             if (fi.type == ARMFault_SyncExternalOnWalk &&
3243                 (env->cp15.scr_el3 & SCR_EA)) {
3244                 target_el = 3;
3245             } else {
3246                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3247                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3248                     env->cp15.hpfar_el2 |= HPFAR_NS;
3249                 }
3250                 target_el = 2;
3251             }
3252             take_exc = true;
3253         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3254             /*
3255              * Synchronous external aborts during a translation table walk
3256              * are taken as Data Abort exceptions.
3257              */
3258             if (fi.stage2) {
3259                 if (current_el == 3) {
3260                     target_el = 3;
3261                 } else {
3262                     target_el = 2;
3263                 }
3264             } else {
3265                 target_el = exception_target_el(env);
3266             }
3267             take_exc = true;
3268         }
3269 
3270         if (take_exc) {
3271             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3272             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3273                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3274                 fsr = arm_fi_to_lfsc(&fi);
3275                 fsc = extract32(fsr, 0, 6);
3276             } else {
3277                 fsr = arm_fi_to_sfsc(&fi);
3278                 fsc = 0x3f;
3279             }
3280             /*
3281              * Report exception with ESR indicating a fault due to a
3282              * translation table walk for a cache maintenance instruction.
3283              */
3284             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3285                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3286             env->exception.vaddress = value;
3287             env->exception.fsr = fsr;
3288             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3289         }
3290     }
3291 
3292     if (is_a64(env)) {
3293         format64 = true;
3294     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3295         /*
3296          * ATS1Cxx:
3297          * * TTBCR.EAE determines whether the result is returned using the
3298          *   32-bit or the 64-bit PAR format
3299          * * Instructions executed in Hyp mode always use the 64bit format
3300          *
3301          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3302          * * The Non-secure TTBCR.EAE bit is set to 1
3303          * * The implementation includes EL2, and the value of HCR.VM is 1
3304          *
3305          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3306          *
3307          * ATS1Hx always uses the 64bit format.
3308          */
3309         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3310 
3311         if (arm_feature(env, ARM_FEATURE_EL2)) {
3312             if (mmu_idx == ARMMMUIdx_E10_0 ||
3313                 mmu_idx == ARMMMUIdx_E10_1 ||
3314                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3315                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3316             } else {
3317                 format64 |= arm_current_el(env) == 2;
3318             }
3319         }
3320     }
3321 
3322     if (format64) {
3323         /* Create a 64-bit PAR */
3324         par64 = (1 << 11); /* LPAE bit always set */
3325         if (!ret) {
3326             par64 |= res.f.phys_addr & ~0xfffULL;
3327             if (!res.f.attrs.secure) {
3328                 par64 |= (1 << 9); /* NS */
3329             }
3330             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3331             par64 |= res.cacheattrs.shareability << 7; /* SH */
3332         } else {
3333             uint32_t fsr = arm_fi_to_lfsc(&fi);
3334 
3335             par64 |= 1; /* F */
3336             par64 |= (fsr & 0x3f) << 1; /* FS */
3337             if (fi.stage2) {
3338                 par64 |= (1 << 9); /* S */
3339             }
3340             if (fi.s1ptw) {
3341                 par64 |= (1 << 8); /* PTW */
3342             }
3343         }
3344     } else {
3345         /* fsr is a DFSR/IFSR value for the short descriptor
3346          * translation table format (with WnR always clear).
3347          * Convert it to a 32-bit PAR.
3348          */
3349         if (!ret) {
3350             /* We do not set any attribute bits in the PAR */
3351             if (res.f.lg_page_size == 24
3352                 && arm_feature(env, ARM_FEATURE_V7)) {
3353                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3354             } else {
3355                 par64 = res.f.phys_addr & 0xfffff000;
3356             }
3357             if (!res.f.attrs.secure) {
3358                 par64 |= (1 << 9); /* NS */
3359             }
3360         } else {
3361             uint32_t fsr = arm_fi_to_sfsc(&fi);
3362 
3363             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3364                     ((fsr & 0xf) << 1) | 1;
3365         }
3366     }
3367     return par64;
3368 }
3369 #endif /* CONFIG_TCG */
3370 
3371 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3372 {
3373 #ifdef CONFIG_TCG
3374     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3375     uint64_t par64;
3376     ARMMMUIdx mmu_idx;
3377     int el = arm_current_el(env);
3378     bool secure = arm_is_secure_below_el3(env);
3379 
3380     switch (ri->opc2 & 6) {
3381     case 0:
3382         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3383         switch (el) {
3384         case 3:
3385             mmu_idx = ARMMMUIdx_E3;
3386             secure = true;
3387             break;
3388         case 2:
3389             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3390             /* fall through */
3391         case 1:
3392             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3393                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3394             } else {
3395                 mmu_idx = ARMMMUIdx_Stage1_E1;
3396             }
3397             break;
3398         default:
3399             g_assert_not_reached();
3400         }
3401         break;
3402     case 2:
3403         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3404         switch (el) {
3405         case 3:
3406             mmu_idx = ARMMMUIdx_E10_0;
3407             secure = true;
3408             break;
3409         case 2:
3410             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3411             mmu_idx = ARMMMUIdx_Stage1_E0;
3412             break;
3413         case 1:
3414             mmu_idx = ARMMMUIdx_Stage1_E0;
3415             break;
3416         default:
3417             g_assert_not_reached();
3418         }
3419         break;
3420     case 4:
3421         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3422         mmu_idx = ARMMMUIdx_E10_1;
3423         secure = false;
3424         break;
3425     case 6:
3426         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3427         mmu_idx = ARMMMUIdx_E10_0;
3428         secure = false;
3429         break;
3430     default:
3431         g_assert_not_reached();
3432     }
3433 
3434     par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
3435 
3436     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3437 #else
3438     /* Handled by hardware accelerator. */
3439     g_assert_not_reached();
3440 #endif /* CONFIG_TCG */
3441 }
3442 
3443 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3444                         uint64_t value)
3445 {
3446 #ifdef CONFIG_TCG
3447     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3448     uint64_t par64;
3449 
3450     /* There is no SecureEL2 for AArch32. */
3451     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
3452 
3453     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3454 #else
3455     /* Handled by hardware accelerator. */
3456     g_assert_not_reached();
3457 #endif /* CONFIG_TCG */
3458 }
3459 
3460 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3461                                      bool isread)
3462 {
3463     if (arm_current_el(env) == 3 &&
3464         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3465         return CP_ACCESS_TRAP;
3466     }
3467     return CP_ACCESS_OK;
3468 }
3469 
3470 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3471                         uint64_t value)
3472 {
3473 #ifdef CONFIG_TCG
3474     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3475     ARMMMUIdx mmu_idx;
3476     int secure = arm_is_secure_below_el3(env);
3477 
3478     switch (ri->opc2 & 6) {
3479     case 0:
3480         switch (ri->opc1) {
3481         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3482             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3483                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3484             } else {
3485                 mmu_idx = ARMMMUIdx_Stage1_E1;
3486             }
3487             break;
3488         case 4: /* AT S1E2R, AT S1E2W */
3489             mmu_idx = ARMMMUIdx_E2;
3490             break;
3491         case 6: /* AT S1E3R, AT S1E3W */
3492             mmu_idx = ARMMMUIdx_E3;
3493             secure = true;
3494             break;
3495         default:
3496             g_assert_not_reached();
3497         }
3498         break;
3499     case 2: /* AT S1E0R, AT S1E0W */
3500         mmu_idx = ARMMMUIdx_Stage1_E0;
3501         break;
3502     case 4: /* AT S12E1R, AT S12E1W */
3503         mmu_idx = ARMMMUIdx_E10_1;
3504         break;
3505     case 6: /* AT S12E0R, AT S12E0W */
3506         mmu_idx = ARMMMUIdx_E10_0;
3507         break;
3508     default:
3509         g_assert_not_reached();
3510     }
3511 
3512     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3513                                        mmu_idx, secure);
3514 #else
3515     /* Handled by hardware accelerator. */
3516     g_assert_not_reached();
3517 #endif /* CONFIG_TCG */
3518 }
3519 #endif
3520 
3521 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3522     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3523       .access = PL1_RW, .resetvalue = 0,
3524       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3525                              offsetoflow32(CPUARMState, cp15.par_ns) },
3526       .writefn = par_write },
3527 #ifndef CONFIG_USER_ONLY
3528     /* This underdecoding is safe because the reginfo is NO_RAW. */
3529     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3530       .access = PL1_W, .accessfn = ats_access,
3531       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3532 #endif
3533 };
3534 
3535 /* Return basic MPU access permission bits.  */
3536 static uint32_t simple_mpu_ap_bits(uint32_t val)
3537 {
3538     uint32_t ret;
3539     uint32_t mask;
3540     int i;
3541     ret = 0;
3542     mask = 3;
3543     for (i = 0; i < 16; i += 2) {
3544         ret |= (val >> i) & mask;
3545         mask <<= 2;
3546     }
3547     return ret;
3548 }
3549 
3550 /* Pad basic MPU access permission bits to extended format.  */
3551 static uint32_t extended_mpu_ap_bits(uint32_t val)
3552 {
3553     uint32_t ret;
3554     uint32_t mask;
3555     int i;
3556     ret = 0;
3557     mask = 3;
3558     for (i = 0; i < 16; i += 2) {
3559         ret |= (val & mask) << i;
3560         mask <<= 2;
3561     }
3562     return ret;
3563 }
3564 
3565 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3566                                  uint64_t value)
3567 {
3568     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3569 }
3570 
3571 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3572 {
3573     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3574 }
3575 
3576 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3577                                  uint64_t value)
3578 {
3579     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3580 }
3581 
3582 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3583 {
3584     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3585 }
3586 
3587 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3588 {
3589     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3590 
3591     if (!u32p) {
3592         return 0;
3593     }
3594 
3595     u32p += env->pmsav7.rnr[M_REG_NS];
3596     return *u32p;
3597 }
3598 
3599 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3600                          uint64_t value)
3601 {
3602     ARMCPU *cpu = env_archcpu(env);
3603     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3604 
3605     if (!u32p) {
3606         return;
3607     }
3608 
3609     u32p += env->pmsav7.rnr[M_REG_NS];
3610     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3611     *u32p = value;
3612 }
3613 
3614 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3615                               uint64_t value)
3616 {
3617     ARMCPU *cpu = env_archcpu(env);
3618     uint32_t nrgs = cpu->pmsav7_dregion;
3619 
3620     if (value >= nrgs) {
3621         qemu_log_mask(LOG_GUEST_ERROR,
3622                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3623                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3624         return;
3625     }
3626 
3627     raw_write(env, ri, value);
3628 }
3629 
3630 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3631     /* Reset for all these registers is handled in arm_cpu_reset(),
3632      * because the PMSAv7 is also used by M-profile CPUs, which do
3633      * not register cpregs but still need the state to be reset.
3634      */
3635     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3636       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3637       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3638       .readfn = pmsav7_read, .writefn = pmsav7_write,
3639       .resetfn = arm_cp_reset_ignore },
3640     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3641       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3642       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3643       .readfn = pmsav7_read, .writefn = pmsav7_write,
3644       .resetfn = arm_cp_reset_ignore },
3645     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3646       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3647       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3648       .readfn = pmsav7_read, .writefn = pmsav7_write,
3649       .resetfn = arm_cp_reset_ignore },
3650     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3651       .access = PL1_RW,
3652       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3653       .writefn = pmsav7_rgnr_write,
3654       .resetfn = arm_cp_reset_ignore },
3655 };
3656 
3657 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3658     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3659       .access = PL1_RW, .type = ARM_CP_ALIAS,
3660       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3661       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3662     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3663       .access = PL1_RW, .type = ARM_CP_ALIAS,
3664       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3665       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3666     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3667       .access = PL1_RW,
3668       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3669       .resetvalue = 0, },
3670     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3671       .access = PL1_RW,
3672       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3673       .resetvalue = 0, },
3674     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3675       .access = PL1_RW,
3676       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3677     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3678       .access = PL1_RW,
3679       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3680     /* Protection region base and size registers */
3681     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3682       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3683       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3684     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3685       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3686       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3687     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3688       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3689       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3690     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3691       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3692       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3693     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3694       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3695       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3696     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3697       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3698       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3699     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3700       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3701       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3702     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3703       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3704       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3705 };
3706 
3707 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3708                              uint64_t value)
3709 {
3710     ARMCPU *cpu = env_archcpu(env);
3711 
3712     if (!arm_feature(env, ARM_FEATURE_V8)) {
3713         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3714             /*
3715              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3716              * using Long-descriptor translation table format
3717              */
3718             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3719         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3720             /*
3721              * In an implementation that includes the Security Extensions
3722              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3723              * Short-descriptor translation table format.
3724              */
3725             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3726         } else {
3727             value &= TTBCR_N;
3728         }
3729     }
3730 
3731     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3732         /* With LPAE the TTBCR could result in a change of ASID
3733          * via the TTBCR.A1 bit, so do a TLB flush.
3734          */
3735         tlb_flush(CPU(cpu));
3736     }
3737     raw_write(env, ri, value);
3738 }
3739 
3740 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3741                                uint64_t value)
3742 {
3743     ARMCPU *cpu = env_archcpu(env);
3744 
3745     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3746     tlb_flush(CPU(cpu));
3747     raw_write(env, ri, value);
3748 }
3749 
3750 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3751                             uint64_t value)
3752 {
3753     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3754     if (cpreg_field_is_64bit(ri) &&
3755         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3756         ARMCPU *cpu = env_archcpu(env);
3757         tlb_flush(CPU(cpu));
3758     }
3759     raw_write(env, ri, value);
3760 }
3761 
3762 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3763                                     uint64_t value)
3764 {
3765     /*
3766      * If we are running with E2&0 regime, then an ASID is active.
3767      * Flush if that might be changing.  Note we're not checking
3768      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3769      * holds the active ASID, only checking the field that might.
3770      */
3771     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3772         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3773         uint16_t mask = ARMMMUIdxBit_E20_2 |
3774                         ARMMMUIdxBit_E20_2_PAN |
3775                         ARMMMUIdxBit_E20_0;
3776         tlb_flush_by_mmuidx(env_cpu(env), mask);
3777     }
3778     raw_write(env, ri, value);
3779 }
3780 
3781 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3782                         uint64_t value)
3783 {
3784     ARMCPU *cpu = env_archcpu(env);
3785     CPUState *cs = CPU(cpu);
3786 
3787     /*
3788      * A change in VMID to the stage2 page table (Stage2) invalidates
3789      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3790      */
3791     if (raw_read(env, ri) != value) {
3792         uint16_t mask = ARMMMUIdxBit_E10_1 |
3793                         ARMMMUIdxBit_E10_1_PAN |
3794                         ARMMMUIdxBit_E10_0;
3795         tlb_flush_by_mmuidx(cs, mask);
3796         raw_write(env, ri, value);
3797     }
3798 }
3799 
3800 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3801     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3802       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3803       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3804                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3805     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3806       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3807       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3808                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3809     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3810       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3811       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3812                              offsetof(CPUARMState, cp15.dfar_ns) } },
3813     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3814       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3815       .access = PL1_RW, .accessfn = access_tvm_trvm,
3816       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3817       .resetvalue = 0, },
3818 };
3819 
3820 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3821     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3822       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3823       .access = PL1_RW, .accessfn = access_tvm_trvm,
3824       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3825     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3826       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3827       .access = PL1_RW, .accessfn = access_tvm_trvm,
3828       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3829       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3830                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3831     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3832       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3833       .access = PL1_RW, .accessfn = access_tvm_trvm,
3834       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3835       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3836                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3837     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3838       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3839       .access = PL1_RW, .accessfn = access_tvm_trvm,
3840       .writefn = vmsa_tcr_el12_write,
3841       .raw_writefn = raw_write,
3842       .resetvalue = 0,
3843       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3844     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3845       .access = PL1_RW, .accessfn = access_tvm_trvm,
3846       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3847       .raw_writefn = raw_write,
3848       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3849                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3850 };
3851 
3852 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3853  * qemu tlbs nor adjusting cached masks.
3854  */
3855 static const ARMCPRegInfo ttbcr2_reginfo = {
3856     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3857     .access = PL1_RW, .accessfn = access_tvm_trvm,
3858     .type = ARM_CP_ALIAS,
3859     .bank_fieldoffsets = {
3860         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3861         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3862     },
3863 };
3864 
3865 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3866                                 uint64_t value)
3867 {
3868     env->cp15.c15_ticonfig = value & 0xe7;
3869     /* The OS_TYPE bit in this register changes the reported CPUID! */
3870     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3871         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3872 }
3873 
3874 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3875                                 uint64_t value)
3876 {
3877     env->cp15.c15_threadid = value & 0xffff;
3878 }
3879 
3880 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3881                            uint64_t value)
3882 {
3883     /* Wait-for-interrupt (deprecated) */
3884     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3885 }
3886 
3887 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3888                                   uint64_t value)
3889 {
3890     /* On OMAP there are registers indicating the max/min index of dcache lines
3891      * containing a dirty line; cache flush operations have to reset these.
3892      */
3893     env->cp15.c15_i_max = 0x000;
3894     env->cp15.c15_i_min = 0xff0;
3895 }
3896 
3897 static const ARMCPRegInfo omap_cp_reginfo[] = {
3898     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3899       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3900       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3901       .resetvalue = 0, },
3902     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3903       .access = PL1_RW, .type = ARM_CP_NOP },
3904     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3905       .access = PL1_RW,
3906       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3907       .writefn = omap_ticonfig_write },
3908     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3909       .access = PL1_RW,
3910       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3911     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3912       .access = PL1_RW, .resetvalue = 0xff0,
3913       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3914     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3915       .access = PL1_RW,
3916       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3917       .writefn = omap_threadid_write },
3918     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3919       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3920       .type = ARM_CP_NO_RAW,
3921       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3922     /* TODO: Peripheral port remap register:
3923      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3924      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3925      * when MMU is off.
3926      */
3927     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3928       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3929       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3930       .writefn = omap_cachemaint_write },
3931     { .name = "C9", .cp = 15, .crn = 9,
3932       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3933       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3934 };
3935 
3936 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3937                               uint64_t value)
3938 {
3939     env->cp15.c15_cpar = value & 0x3fff;
3940 }
3941 
3942 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3943     { .name = "XSCALE_CPAR",
3944       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3945       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3946       .writefn = xscale_cpar_write, },
3947     { .name = "XSCALE_AUXCR",
3948       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3949       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3950       .resetvalue = 0, },
3951     /* XScale specific cache-lockdown: since we have no cache we NOP these
3952      * and hope the guest does not really rely on cache behaviour.
3953      */
3954     { .name = "XSCALE_LOCK_ICACHE_LINE",
3955       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3956       .access = PL1_W, .type = ARM_CP_NOP },
3957     { .name = "XSCALE_UNLOCK_ICACHE",
3958       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3959       .access = PL1_W, .type = ARM_CP_NOP },
3960     { .name = "XSCALE_DCACHE_LOCK",
3961       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3962       .access = PL1_RW, .type = ARM_CP_NOP },
3963     { .name = "XSCALE_UNLOCK_DCACHE",
3964       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3965       .access = PL1_W, .type = ARM_CP_NOP },
3966 };
3967 
3968 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3969     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3970      * implementation of this implementation-defined space.
3971      * Ideally this should eventually disappear in favour of actually
3972      * implementing the correct behaviour for all cores.
3973      */
3974     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3975       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3976       .access = PL1_RW,
3977       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3978       .resetvalue = 0 },
3979 };
3980 
3981 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3982     /* Cache status: RAZ because we have no cache so it's always clean */
3983     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3984       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3985       .resetvalue = 0 },
3986 };
3987 
3988 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3989     /* We never have a block transfer operation in progress */
3990     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3991       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3992       .resetvalue = 0 },
3993     /* The cache ops themselves: these all NOP for QEMU */
3994     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3995       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3996     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3997       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3998     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3999       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4000     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4001       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4002     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4003       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4004     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4005       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4006 };
4007 
4008 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4009     /* The cache test-and-clean instructions always return (1 << 30)
4010      * to indicate that there are no dirty cache lines.
4011      */
4012     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4013       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4014       .resetvalue = (1 << 30) },
4015     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4016       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4017       .resetvalue = (1 << 30) },
4018 };
4019 
4020 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4021     /* Ignore ReadBuffer accesses */
4022     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4023       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4024       .access = PL1_RW, .resetvalue = 0,
4025       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4026 };
4027 
4028 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4029 {
4030     unsigned int cur_el = arm_current_el(env);
4031 
4032     if (arm_is_el2_enabled(env) && cur_el == 1) {
4033         return env->cp15.vpidr_el2;
4034     }
4035     return raw_read(env, ri);
4036 }
4037 
4038 static uint64_t mpidr_read_val(CPUARMState *env)
4039 {
4040     ARMCPU *cpu = env_archcpu(env);
4041     uint64_t mpidr = cpu->mp_affinity;
4042 
4043     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4044         mpidr |= (1U << 31);
4045         /* Cores which are uniprocessor (non-coherent)
4046          * but still implement the MP extensions set
4047          * bit 30. (For instance, Cortex-R5).
4048          */
4049         if (cpu->mp_is_up) {
4050             mpidr |= (1u << 30);
4051         }
4052     }
4053     return mpidr;
4054 }
4055 
4056 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4057 {
4058     unsigned int cur_el = arm_current_el(env);
4059 
4060     if (arm_is_el2_enabled(env) && cur_el == 1) {
4061         return env->cp15.vmpidr_el2;
4062     }
4063     return mpidr_read_val(env);
4064 }
4065 
4066 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4067     /* NOP AMAIR0/1 */
4068     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4069       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4070       .access = PL1_RW, .accessfn = access_tvm_trvm,
4071       .type = ARM_CP_CONST, .resetvalue = 0 },
4072     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4073     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4074       .access = PL1_RW, .accessfn = access_tvm_trvm,
4075       .type = ARM_CP_CONST, .resetvalue = 0 },
4076     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4077       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4078       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4079                              offsetof(CPUARMState, cp15.par_ns)} },
4080     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4081       .access = PL1_RW, .accessfn = access_tvm_trvm,
4082       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4083       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4084                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4085       .writefn = vmsa_ttbr_write, },
4086     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4087       .access = PL1_RW, .accessfn = access_tvm_trvm,
4088       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4089       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4090                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4091       .writefn = vmsa_ttbr_write, },
4092 };
4093 
4094 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4095 {
4096     return vfp_get_fpcr(env);
4097 }
4098 
4099 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4100                             uint64_t value)
4101 {
4102     vfp_set_fpcr(env, value);
4103 }
4104 
4105 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4106 {
4107     return vfp_get_fpsr(env);
4108 }
4109 
4110 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4111                             uint64_t value)
4112 {
4113     vfp_set_fpsr(env, value);
4114 }
4115 
4116 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4117                                        bool isread)
4118 {
4119     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4120         return CP_ACCESS_TRAP;
4121     }
4122     return CP_ACCESS_OK;
4123 }
4124 
4125 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4126                             uint64_t value)
4127 {
4128     env->daif = value & PSTATE_DAIF;
4129 }
4130 
4131 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4132 {
4133     return env->pstate & PSTATE_PAN;
4134 }
4135 
4136 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4137                            uint64_t value)
4138 {
4139     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4140 }
4141 
4142 static const ARMCPRegInfo pan_reginfo = {
4143     .name = "PAN", .state = ARM_CP_STATE_AA64,
4144     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4145     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4146     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4147 };
4148 
4149 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4150 {
4151     return env->pstate & PSTATE_UAO;
4152 }
4153 
4154 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4155                            uint64_t value)
4156 {
4157     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4158 }
4159 
4160 static const ARMCPRegInfo uao_reginfo = {
4161     .name = "UAO", .state = ARM_CP_STATE_AA64,
4162     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4163     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4164     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4165 };
4166 
4167 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4168 {
4169     return env->pstate & PSTATE_DIT;
4170 }
4171 
4172 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4173                            uint64_t value)
4174 {
4175     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4176 }
4177 
4178 static const ARMCPRegInfo dit_reginfo = {
4179     .name = "DIT", .state = ARM_CP_STATE_AA64,
4180     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4181     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4182     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4183 };
4184 
4185 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4186 {
4187     return env->pstate & PSTATE_SSBS;
4188 }
4189 
4190 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4191                            uint64_t value)
4192 {
4193     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4194 }
4195 
4196 static const ARMCPRegInfo ssbs_reginfo = {
4197     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4198     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4199     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4200     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4201 };
4202 
4203 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4204                                               const ARMCPRegInfo *ri,
4205                                               bool isread)
4206 {
4207     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4208     switch (arm_current_el(env)) {
4209     case 0:
4210         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4211         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4212             return CP_ACCESS_TRAP;
4213         }
4214         /* fall through */
4215     case 1:
4216         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4217         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4218             return CP_ACCESS_TRAP_EL2;
4219         }
4220         break;
4221     }
4222     return CP_ACCESS_OK;
4223 }
4224 
4225 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4226                                               const ARMCPRegInfo *ri,
4227                                               bool isread)
4228 {
4229     /* Cache invalidate/clean to Point of Unification... */
4230     switch (arm_current_el(env)) {
4231     case 0:
4232         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4233         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4234             return CP_ACCESS_TRAP;
4235         }
4236         /* fall through */
4237     case 1:
4238         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4239         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4240             return CP_ACCESS_TRAP_EL2;
4241         }
4242         break;
4243     }
4244     return CP_ACCESS_OK;
4245 }
4246 
4247 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4248  * Page D4-1736 (DDI0487A.b)
4249  */
4250 
4251 static int vae1_tlbmask(CPUARMState *env)
4252 {
4253     uint64_t hcr = arm_hcr_el2_eff(env);
4254     uint16_t mask;
4255 
4256     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4257         mask = ARMMMUIdxBit_E20_2 |
4258                ARMMMUIdxBit_E20_2_PAN |
4259                ARMMMUIdxBit_E20_0;
4260     } else {
4261         mask = ARMMMUIdxBit_E10_1 |
4262                ARMMMUIdxBit_E10_1_PAN |
4263                ARMMMUIdxBit_E10_0;
4264     }
4265     return mask;
4266 }
4267 
4268 /* Return 56 if TBI is enabled, 64 otherwise. */
4269 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4270                               uint64_t addr)
4271 {
4272     uint64_t tcr = regime_tcr(env, mmu_idx);
4273     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4274     int select = extract64(addr, 55, 1);
4275 
4276     return (tbi >> select) & 1 ? 56 : 64;
4277 }
4278 
4279 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4280 {
4281     uint64_t hcr = arm_hcr_el2_eff(env);
4282     ARMMMUIdx mmu_idx;
4283 
4284     /* Only the regime of the mmu_idx below is significant. */
4285     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4286         mmu_idx = ARMMMUIdx_E20_0;
4287     } else {
4288         mmu_idx = ARMMMUIdx_E10_0;
4289     }
4290 
4291     return tlbbits_for_regime(env, mmu_idx, addr);
4292 }
4293 
4294 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4295                                       uint64_t value)
4296 {
4297     CPUState *cs = env_cpu(env);
4298     int mask = vae1_tlbmask(env);
4299 
4300     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4301 }
4302 
4303 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4304                                     uint64_t value)
4305 {
4306     CPUState *cs = env_cpu(env);
4307     int mask = vae1_tlbmask(env);
4308 
4309     if (tlb_force_broadcast(env)) {
4310         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4311     } else {
4312         tlb_flush_by_mmuidx(cs, mask);
4313     }
4314 }
4315 
4316 static int alle1_tlbmask(CPUARMState *env)
4317 {
4318     /*
4319      * Note that the 'ALL' scope must invalidate both stage 1 and
4320      * stage 2 translations, whereas most other scopes only invalidate
4321      * stage 1 translations.
4322      */
4323     return (ARMMMUIdxBit_E10_1 |
4324             ARMMMUIdxBit_E10_1_PAN |
4325             ARMMMUIdxBit_E10_0);
4326 }
4327 
4328 static int e2_tlbmask(CPUARMState *env)
4329 {
4330     return (ARMMMUIdxBit_E20_0 |
4331             ARMMMUIdxBit_E20_2 |
4332             ARMMMUIdxBit_E20_2_PAN |
4333             ARMMMUIdxBit_E2);
4334 }
4335 
4336 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4337                                   uint64_t value)
4338 {
4339     CPUState *cs = env_cpu(env);
4340     int mask = alle1_tlbmask(env);
4341 
4342     tlb_flush_by_mmuidx(cs, mask);
4343 }
4344 
4345 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4346                                   uint64_t value)
4347 {
4348     CPUState *cs = env_cpu(env);
4349     int mask = e2_tlbmask(env);
4350 
4351     tlb_flush_by_mmuidx(cs, mask);
4352 }
4353 
4354 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4355                                   uint64_t value)
4356 {
4357     ARMCPU *cpu = env_archcpu(env);
4358     CPUState *cs = CPU(cpu);
4359 
4360     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4361 }
4362 
4363 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4364                                     uint64_t value)
4365 {
4366     CPUState *cs = env_cpu(env);
4367     int mask = alle1_tlbmask(env);
4368 
4369     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4370 }
4371 
4372 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4373                                     uint64_t value)
4374 {
4375     CPUState *cs = env_cpu(env);
4376     int mask = e2_tlbmask(env);
4377 
4378     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4379 }
4380 
4381 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4382                                     uint64_t value)
4383 {
4384     CPUState *cs = env_cpu(env);
4385 
4386     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4387 }
4388 
4389 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4390                                  uint64_t value)
4391 {
4392     /* Invalidate by VA, EL2
4393      * Currently handles both VAE2 and VALE2, since we don't support
4394      * flush-last-level-only.
4395      */
4396     CPUState *cs = env_cpu(env);
4397     int mask = e2_tlbmask(env);
4398     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4399 
4400     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4401 }
4402 
4403 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4404                                  uint64_t value)
4405 {
4406     /* Invalidate by VA, EL3
4407      * Currently handles both VAE3 and VALE3, since we don't support
4408      * flush-last-level-only.
4409      */
4410     ARMCPU *cpu = env_archcpu(env);
4411     CPUState *cs = CPU(cpu);
4412     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4413 
4414     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4415 }
4416 
4417 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4418                                    uint64_t value)
4419 {
4420     CPUState *cs = env_cpu(env);
4421     int mask = vae1_tlbmask(env);
4422     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4423     int bits = vae1_tlbbits(env, pageaddr);
4424 
4425     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4426 }
4427 
4428 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429                                  uint64_t value)
4430 {
4431     /* Invalidate by VA, EL1&0 (AArch64 version).
4432      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4433      * since we don't support flush-for-specific-ASID-only or
4434      * flush-last-level-only.
4435      */
4436     CPUState *cs = env_cpu(env);
4437     int mask = vae1_tlbmask(env);
4438     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4439     int bits = vae1_tlbbits(env, pageaddr);
4440 
4441     if (tlb_force_broadcast(env)) {
4442         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4443     } else {
4444         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4445     }
4446 }
4447 
4448 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4449                                    uint64_t value)
4450 {
4451     CPUState *cs = env_cpu(env);
4452     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4453     int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
4454 
4455     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4456                                                   ARMMMUIdxBit_E2, bits);
4457 }
4458 
4459 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460                                    uint64_t value)
4461 {
4462     CPUState *cs = env_cpu(env);
4463     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4464     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4465 
4466     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4467                                                   ARMMMUIdxBit_E3, bits);
4468 }
4469 
4470 #ifdef TARGET_AARCH64
4471 typedef struct {
4472     uint64_t base;
4473     uint64_t length;
4474 } TLBIRange;
4475 
4476 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4477 {
4478     /*
4479      * Note that the TLBI range TG field encoding differs from both
4480      * TG0 and TG1 encodings.
4481      */
4482     switch (tg) {
4483     case 1:
4484         return Gran4K;
4485     case 2:
4486         return Gran16K;
4487     case 3:
4488         return Gran64K;
4489     default:
4490         return GranInvalid;
4491     }
4492 }
4493 
4494 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4495                                      uint64_t value)
4496 {
4497     unsigned int page_size_granule, page_shift, num, scale, exponent;
4498     /* Extract one bit to represent the va selector in use. */
4499     uint64_t select = sextract64(value, 36, 1);
4500     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4501     TLBIRange ret = { };
4502     ARMGranuleSize gran;
4503 
4504     page_size_granule = extract64(value, 46, 2);
4505     gran = tlbi_range_tg_to_gran_size(page_size_granule);
4506 
4507     /* The granule encoded in value must match the granule in use. */
4508     if (gran != param.gran) {
4509         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4510                       page_size_granule);
4511         return ret;
4512     }
4513 
4514     page_shift = arm_granule_bits(gran);
4515     num = extract64(value, 39, 5);
4516     scale = extract64(value, 44, 2);
4517     exponent = (5 * scale) + 1;
4518 
4519     ret.length = (num + 1) << (exponent + page_shift);
4520 
4521     if (param.select) {
4522         ret.base = sextract64(value, 0, 37);
4523     } else {
4524         ret.base = extract64(value, 0, 37);
4525     }
4526     if (param.ds) {
4527         /*
4528          * With DS=1, BaseADDR is always shifted 16 so that it is able
4529          * to address all 52 va bits.  The input address is perforce
4530          * aligned on a 64k boundary regardless of translation granule.
4531          */
4532         page_shift = 16;
4533     }
4534     ret.base <<= page_shift;
4535 
4536     return ret;
4537 }
4538 
4539 static void do_rvae_write(CPUARMState *env, uint64_t value,
4540                           int idxmap, bool synced)
4541 {
4542     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4543     TLBIRange range;
4544     int bits;
4545 
4546     range = tlbi_aa64_get_range(env, one_idx, value);
4547     bits = tlbbits_for_regime(env, one_idx, range.base);
4548 
4549     if (synced) {
4550         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4551                                                   range.base,
4552                                                   range.length,
4553                                                   idxmap,
4554                                                   bits);
4555     } else {
4556         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4557                                   range.length, idxmap, bits);
4558     }
4559 }
4560 
4561 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4562                                   const ARMCPRegInfo *ri,
4563                                   uint64_t value)
4564 {
4565     /*
4566      * Invalidate by VA range, EL1&0.
4567      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4568      * since we don't support flush-for-specific-ASID-only or
4569      * flush-last-level-only.
4570      */
4571 
4572     do_rvae_write(env, value, vae1_tlbmask(env),
4573                   tlb_force_broadcast(env));
4574 }
4575 
4576 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4577                                     const ARMCPRegInfo *ri,
4578                                     uint64_t value)
4579 {
4580     /*
4581      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4582      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4583      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4584      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4585      * shareable specific flushes.
4586      */
4587 
4588     do_rvae_write(env, value, vae1_tlbmask(env), true);
4589 }
4590 
4591 static int vae2_tlbmask(CPUARMState *env)
4592 {
4593     return ARMMMUIdxBit_E2;
4594 }
4595 
4596 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4597                                   const ARMCPRegInfo *ri,
4598                                   uint64_t value)
4599 {
4600     /*
4601      * Invalidate by VA range, EL2.
4602      * Currently handles all of RVAE2 and RVALE2,
4603      * since we don't support flush-for-specific-ASID-only or
4604      * flush-last-level-only.
4605      */
4606 
4607     do_rvae_write(env, value, vae2_tlbmask(env),
4608                   tlb_force_broadcast(env));
4609 
4610 
4611 }
4612 
4613 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4614                                     const ARMCPRegInfo *ri,
4615                                     uint64_t value)
4616 {
4617     /*
4618      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4619      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4620      * since we don't support flush-for-specific-ASID-only,
4621      * flush-last-level-only or inner/outer shareable specific flushes.
4622      */
4623 
4624     do_rvae_write(env, value, vae2_tlbmask(env), true);
4625 
4626 }
4627 
4628 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4629                                   const ARMCPRegInfo *ri,
4630                                   uint64_t value)
4631 {
4632     /*
4633      * Invalidate by VA range, EL3.
4634      * Currently handles all of RVAE3 and RVALE3,
4635      * since we don't support flush-for-specific-ASID-only or
4636      * flush-last-level-only.
4637      */
4638 
4639     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
4640 }
4641 
4642 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4643                                     const ARMCPRegInfo *ri,
4644                                     uint64_t value)
4645 {
4646     /*
4647      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4648      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4649      * since we don't support flush-for-specific-ASID-only,
4650      * flush-last-level-only or inner/outer specific flushes.
4651      */
4652 
4653     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
4654 }
4655 #endif
4656 
4657 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4658                                       bool isread)
4659 {
4660     int cur_el = arm_current_el(env);
4661 
4662     if (cur_el < 2) {
4663         uint64_t hcr = arm_hcr_el2_eff(env);
4664 
4665         if (cur_el == 0) {
4666             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4667                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4668                     return CP_ACCESS_TRAP_EL2;
4669                 }
4670             } else {
4671                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4672                     return CP_ACCESS_TRAP;
4673                 }
4674                 if (hcr & HCR_TDZ) {
4675                     return CP_ACCESS_TRAP_EL2;
4676                 }
4677             }
4678         } else if (hcr & HCR_TDZ) {
4679             return CP_ACCESS_TRAP_EL2;
4680         }
4681     }
4682     return CP_ACCESS_OK;
4683 }
4684 
4685 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4686 {
4687     ARMCPU *cpu = env_archcpu(env);
4688     int dzp_bit = 1 << 4;
4689 
4690     /* DZP indicates whether DC ZVA access is allowed */
4691     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4692         dzp_bit = 0;
4693     }
4694     return cpu->dcz_blocksize | dzp_bit;
4695 }
4696 
4697 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4698                                     bool isread)
4699 {
4700     if (!(env->pstate & PSTATE_SP)) {
4701         /* Access to SP_EL0 is undefined if it's being used as
4702          * the stack pointer.
4703          */
4704         return CP_ACCESS_TRAP_UNCATEGORIZED;
4705     }
4706     return CP_ACCESS_OK;
4707 }
4708 
4709 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4710 {
4711     return env->pstate & PSTATE_SP;
4712 }
4713 
4714 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4715 {
4716     update_spsel(env, val);
4717 }
4718 
4719 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4720                         uint64_t value)
4721 {
4722     ARMCPU *cpu = env_archcpu(env);
4723 
4724     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4725         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4726         value &= ~SCTLR_M;
4727     }
4728 
4729     /* ??? Lots of these bits are not implemented.  */
4730 
4731     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4732         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4733             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4734         } else {
4735             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4736                        SCTLR_ATA0 | SCTLR_ATA);
4737         }
4738     }
4739 
4740     if (raw_read(env, ri) == value) {
4741         /* Skip the TLB flush if nothing actually changed; Linux likes
4742          * to do a lot of pointless SCTLR writes.
4743          */
4744         return;
4745     }
4746 
4747     raw_write(env, ri, value);
4748 
4749     /* This may enable/disable the MMU, so do a TLB flush.  */
4750     tlb_flush(CPU(cpu));
4751 
4752     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4753         /*
4754          * Normally we would always end the TB on an SCTLR write; see the
4755          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4756          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4757          * of hflags from the translator, so do it here.
4758          */
4759         arm_rebuild_hflags(env);
4760     }
4761 }
4762 
4763 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4764                            uint64_t value)
4765 {
4766     /*
4767      * Some MDCR_EL3 bits affect whether PMU counters are running:
4768      * if we are trying to change any of those then we must
4769      * bracket this update with PMU start/finish calls.
4770      */
4771     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4772 
4773     if (pmu_op) {
4774         pmu_op_start(env);
4775     }
4776     env->cp15.mdcr_el3 = value;
4777     if (pmu_op) {
4778         pmu_op_finish(env);
4779     }
4780 }
4781 
4782 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4783                        uint64_t value)
4784 {
4785     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4786     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4787 }
4788 
4789 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4790                            uint64_t value)
4791 {
4792     /*
4793      * Some MDCR_EL2 bits affect whether PMU counters are running:
4794      * if we are trying to change any of those then we must
4795      * bracket this update with PMU start/finish calls.
4796      */
4797     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4798 
4799     if (pmu_op) {
4800         pmu_op_start(env);
4801     }
4802     env->cp15.mdcr_el2 = value;
4803     if (pmu_op) {
4804         pmu_op_finish(env);
4805     }
4806 }
4807 
4808 static const ARMCPRegInfo v8_cp_reginfo[] = {
4809     /* Minimal set of EL0-visible registers. This will need to be expanded
4810      * significantly for system emulation of AArch64 CPUs.
4811      */
4812     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4813       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4814       .access = PL0_RW, .type = ARM_CP_NZCV },
4815     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4816       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4817       .type = ARM_CP_NO_RAW,
4818       .access = PL0_RW, .accessfn = aa64_daif_access,
4819       .fieldoffset = offsetof(CPUARMState, daif),
4820       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4821     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4822       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4823       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4824       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4825     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4826       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4827       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4828       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4829     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4830       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4831       .access = PL0_R, .type = ARM_CP_NO_RAW,
4832       .readfn = aa64_dczid_read },
4833     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4834       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4835       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4836 #ifndef CONFIG_USER_ONLY
4837       /* Avoid overhead of an access check that always passes in user-mode */
4838       .accessfn = aa64_zva_access,
4839 #endif
4840     },
4841     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4842       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4843       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4844     /* Cache ops: all NOPs since we don't emulate caches */
4845     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4846       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4847       .access = PL1_W, .type = ARM_CP_NOP,
4848       .accessfn = aa64_cacheop_pou_access },
4849     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4850       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4851       .access = PL1_W, .type = ARM_CP_NOP,
4852       .accessfn = aa64_cacheop_pou_access },
4853     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4854       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4855       .access = PL0_W, .type = ARM_CP_NOP,
4856       .accessfn = aa64_cacheop_pou_access },
4857     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4858       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4859       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4860       .type = ARM_CP_NOP },
4861     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4862       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4863       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4864     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4865       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4866       .access = PL0_W, .type = ARM_CP_NOP,
4867       .accessfn = aa64_cacheop_poc_access },
4868     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4869       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4870       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4871     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4872       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4873       .access = PL0_W, .type = ARM_CP_NOP,
4874       .accessfn = aa64_cacheop_pou_access },
4875     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4876       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4877       .access = PL0_W, .type = ARM_CP_NOP,
4878       .accessfn = aa64_cacheop_poc_access },
4879     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4880       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4881       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4882     /* TLBI operations */
4883     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4884       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4885       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4886       .writefn = tlbi_aa64_vmalle1is_write },
4887     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4888       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4889       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4890       .writefn = tlbi_aa64_vae1is_write },
4891     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4892       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4893       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4894       .writefn = tlbi_aa64_vmalle1is_write },
4895     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4896       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4897       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4898       .writefn = tlbi_aa64_vae1is_write },
4899     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4900       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4901       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4902       .writefn = tlbi_aa64_vae1is_write },
4903     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4904       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4905       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4906       .writefn = tlbi_aa64_vae1is_write },
4907     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4908       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4909       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4910       .writefn = tlbi_aa64_vmalle1_write },
4911     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4912       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4913       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4914       .writefn = tlbi_aa64_vae1_write },
4915     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4916       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4917       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4918       .writefn = tlbi_aa64_vmalle1_write },
4919     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4920       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4921       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4922       .writefn = tlbi_aa64_vae1_write },
4923     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4924       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4925       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4926       .writefn = tlbi_aa64_vae1_write },
4927     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4928       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4929       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4930       .writefn = tlbi_aa64_vae1_write },
4931     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4932       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4933       .access = PL2_W, .type = ARM_CP_NOP },
4934     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4935       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4936       .access = PL2_W, .type = ARM_CP_NOP },
4937     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4938       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4939       .access = PL2_W, .type = ARM_CP_NO_RAW,
4940       .writefn = tlbi_aa64_alle1is_write },
4941     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4942       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4943       .access = PL2_W, .type = ARM_CP_NO_RAW,
4944       .writefn = tlbi_aa64_alle1is_write },
4945     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4946       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4947       .access = PL2_W, .type = ARM_CP_NOP },
4948     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4949       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4950       .access = PL2_W, .type = ARM_CP_NOP },
4951     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4952       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4953       .access = PL2_W, .type = ARM_CP_NO_RAW,
4954       .writefn = tlbi_aa64_alle1_write },
4955     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4956       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4957       .access = PL2_W, .type = ARM_CP_NO_RAW,
4958       .writefn = tlbi_aa64_alle1is_write },
4959 #ifndef CONFIG_USER_ONLY
4960     /* 64 bit address translation operations */
4961     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4962       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4963       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4964       .writefn = ats_write64 },
4965     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4966       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4967       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4968       .writefn = ats_write64 },
4969     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4970       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4971       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4972       .writefn = ats_write64 },
4973     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4974       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4975       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4976       .writefn = ats_write64 },
4977     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4978       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4979       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4980       .writefn = ats_write64 },
4981     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4982       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4983       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4984       .writefn = ats_write64 },
4985     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4986       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4987       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4988       .writefn = ats_write64 },
4989     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4990       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4991       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4992       .writefn = ats_write64 },
4993     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4994     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4995       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4996       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4997       .writefn = ats_write64 },
4998     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4999       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5000       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5001       .writefn = ats_write64 },
5002     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5003       .type = ARM_CP_ALIAS,
5004       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5005       .access = PL1_RW, .resetvalue = 0,
5006       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5007       .writefn = par_write },
5008 #endif
5009     /* TLB invalidate last level of translation table walk */
5010     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5011       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5012       .writefn = tlbimva_is_write },
5013     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5014       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5015       .writefn = tlbimvaa_is_write },
5016     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5017       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5018       .writefn = tlbimva_write },
5019     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5020       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5021       .writefn = tlbimvaa_write },
5022     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5023       .type = ARM_CP_NO_RAW, .access = PL2_W,
5024       .writefn = tlbimva_hyp_write },
5025     { .name = "TLBIMVALHIS",
5026       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5027       .type = ARM_CP_NO_RAW, .access = PL2_W,
5028       .writefn = tlbimva_hyp_is_write },
5029     { .name = "TLBIIPAS2",
5030       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5031       .type = ARM_CP_NOP, .access = PL2_W },
5032     { .name = "TLBIIPAS2IS",
5033       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5034       .type = ARM_CP_NOP, .access = PL2_W },
5035     { .name = "TLBIIPAS2L",
5036       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5037       .type = ARM_CP_NOP, .access = PL2_W },
5038     { .name = "TLBIIPAS2LIS",
5039       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5040       .type = ARM_CP_NOP, .access = PL2_W },
5041     /* 32 bit cache operations */
5042     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5043       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5044     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5045       .type = ARM_CP_NOP, .access = PL1_W },
5046     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5047       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5048     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5049       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5050     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5051       .type = ARM_CP_NOP, .access = PL1_W },
5052     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5053       .type = ARM_CP_NOP, .access = PL1_W },
5054     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5055       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5056     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5057       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5058     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5059       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5060     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5061       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5062     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5063       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5064     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5065       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5066     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5067       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5068     /* MMU Domain access control / MPU write buffer control */
5069     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5070       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5071       .writefn = dacr_write, .raw_writefn = raw_write,
5072       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5073                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5074     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5075       .type = ARM_CP_ALIAS,
5076       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5077       .access = PL1_RW,
5078       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5079     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5080       .type = ARM_CP_ALIAS,
5081       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5082       .access = PL1_RW,
5083       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5084     /* We rely on the access checks not allowing the guest to write to the
5085      * state field when SPSel indicates that it's being used as the stack
5086      * pointer.
5087      */
5088     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5089       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5090       .access = PL1_RW, .accessfn = sp_el0_access,
5091       .type = ARM_CP_ALIAS,
5092       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5093     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5094       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5095       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5096       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5097     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5098       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5099       .type = ARM_CP_NO_RAW,
5100       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5101     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5102       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5103       .access = PL2_RW,
5104       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5105       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5106     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5107       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5108       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5109       .writefn = dacr_write, .raw_writefn = raw_write,
5110       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5111     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5112       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5113       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5114       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5115     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5116       .type = ARM_CP_ALIAS,
5117       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5118       .access = PL2_RW,
5119       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5120     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5121       .type = ARM_CP_ALIAS,
5122       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5123       .access = PL2_RW,
5124       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5125     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5126       .type = ARM_CP_ALIAS,
5127       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5128       .access = PL2_RW,
5129       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5130     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5131       .type = ARM_CP_ALIAS,
5132       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5133       .access = PL2_RW,
5134       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5135     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5136       .type = ARM_CP_IO,
5137       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5138       .resetvalue = 0,
5139       .access = PL3_RW,
5140       .writefn = mdcr_el3_write,
5141       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5142     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5143       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5144       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5145       .writefn = sdcr_write,
5146       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5147 };
5148 
5149 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5150 {
5151     ARMCPU *cpu = env_archcpu(env);
5152 
5153     if (arm_feature(env, ARM_FEATURE_V8)) {
5154         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5155     } else {
5156         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5157     }
5158 
5159     if (arm_feature(env, ARM_FEATURE_EL3)) {
5160         valid_mask &= ~HCR_HCD;
5161     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5162         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5163          * However, if we're using the SMC PSCI conduit then QEMU is
5164          * effectively acting like EL3 firmware and so the guest at
5165          * EL2 should retain the ability to prevent EL1 from being
5166          * able to make SMC calls into the ersatz firmware, so in
5167          * that case HCR.TSC should be read/write.
5168          */
5169         valid_mask &= ~HCR_TSC;
5170     }
5171 
5172     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5173         if (cpu_isar_feature(aa64_vh, cpu)) {
5174             valid_mask |= HCR_E2H;
5175         }
5176         if (cpu_isar_feature(aa64_ras, cpu)) {
5177             valid_mask |= HCR_TERR | HCR_TEA;
5178         }
5179         if (cpu_isar_feature(aa64_lor, cpu)) {
5180             valid_mask |= HCR_TLOR;
5181         }
5182         if (cpu_isar_feature(aa64_pauth, cpu)) {
5183             valid_mask |= HCR_API | HCR_APK;
5184         }
5185         if (cpu_isar_feature(aa64_mte, cpu)) {
5186             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5187         }
5188         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5189             valid_mask |= HCR_ENSCXT;
5190         }
5191         if (cpu_isar_feature(aa64_fwb, cpu)) {
5192             valid_mask |= HCR_FWB;
5193         }
5194     }
5195 
5196     /* Clear RES0 bits.  */
5197     value &= valid_mask;
5198 
5199     /*
5200      * These bits change the MMU setup:
5201      * HCR_VM enables stage 2 translation
5202      * HCR_PTW forbids certain page-table setups
5203      * HCR_DC disables stage1 and enables stage2 translation
5204      * HCR_DCT enables tagging on (disabled) stage1 translation
5205      * HCR_FWB changes the interpretation of stage2 descriptor bits
5206      */
5207     if ((env->cp15.hcr_el2 ^ value) &
5208         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5209         tlb_flush(CPU(cpu));
5210     }
5211     env->cp15.hcr_el2 = value;
5212 
5213     /*
5214      * Updates to VI and VF require us to update the status of
5215      * virtual interrupts, which are the logical OR of these bits
5216      * and the state of the input lines from the GIC. (This requires
5217      * that we have the iothread lock, which is done by marking the
5218      * reginfo structs as ARM_CP_IO.)
5219      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5220      * possible for it to be taken immediately, because VIRQ and
5221      * VFIQ are masked unless running at EL0 or EL1, and HCR
5222      * can only be written at EL2.
5223      */
5224     g_assert(qemu_mutex_iothread_locked());
5225     arm_cpu_update_virq(cpu);
5226     arm_cpu_update_vfiq(cpu);
5227     arm_cpu_update_vserr(cpu);
5228 }
5229 
5230 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5231 {
5232     do_hcr_write(env, value, 0);
5233 }
5234 
5235 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5236                           uint64_t value)
5237 {
5238     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5239     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5240     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5241 }
5242 
5243 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5244                          uint64_t value)
5245 {
5246     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5247     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5248     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5249 }
5250 
5251 /*
5252  * Return the effective value of HCR_EL2, at the given security state.
5253  * Bits that are not included here:
5254  * RW       (read from SCR_EL3.RW as needed)
5255  */
5256 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
5257 {
5258     uint64_t ret = env->cp15.hcr_el2;
5259 
5260     if (!arm_is_el2_enabled_secstate(env, secure)) {
5261         /*
5262          * "This register has no effect if EL2 is not enabled in the
5263          * current Security state".  This is ARMv8.4-SecEL2 speak for
5264          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5265          *
5266          * Prior to that, the language was "In an implementation that
5267          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5268          * as if this field is 0 for all purposes other than a direct
5269          * read or write access of HCR_EL2".  With lots of enumeration
5270          * on a per-field basis.  In current QEMU, this is condition
5271          * is arm_is_secure_below_el3.
5272          *
5273          * Since the v8.4 language applies to the entire register, and
5274          * appears to be backward compatible, use that.
5275          */
5276         return 0;
5277     }
5278 
5279     /*
5280      * For a cpu that supports both aarch64 and aarch32, we can set bits
5281      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5282      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5283      */
5284     if (!arm_el_is_aa64(env, 2)) {
5285         uint64_t aa32_valid;
5286 
5287         /*
5288          * These bits are up-to-date as of ARMv8.6.
5289          * For HCR, it's easiest to list just the 2 bits that are invalid.
5290          * For HCR2, list those that are valid.
5291          */
5292         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5293         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5294                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5295         ret &= aa32_valid;
5296     }
5297 
5298     if (ret & HCR_TGE) {
5299         /* These bits are up-to-date as of ARMv8.6.  */
5300         if (ret & HCR_E2H) {
5301             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5302                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5303                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5304                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5305                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5306                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5307         } else {
5308             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5309         }
5310         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5311                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5312                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5313                  HCR_TLOR);
5314     }
5315 
5316     return ret;
5317 }
5318 
5319 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5320 {
5321     return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5322 }
5323 
5324 /*
5325  * Corresponds to ARM pseudocode function ELIsInHost().
5326  */
5327 bool el_is_in_host(CPUARMState *env, int el)
5328 {
5329     uint64_t mask;
5330 
5331     /*
5332      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5333      * Perform the simplest bit tests first, and validate EL2 afterward.
5334      */
5335     if (el & 1) {
5336         return false; /* EL1 or EL3 */
5337     }
5338 
5339     /*
5340      * Note that hcr_write() checks isar_feature_aa64_vh(),
5341      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5342      */
5343     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5344     if ((env->cp15.hcr_el2 & mask) != mask) {
5345         return false;
5346     }
5347 
5348     /* TGE and/or E2H set: double check those bits are currently legal. */
5349     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5350 }
5351 
5352 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5353                        uint64_t value)
5354 {
5355     uint64_t valid_mask = 0;
5356 
5357     /* No features adding bits to HCRX are implemented. */
5358 
5359     /* Clear RES0 bits.  */
5360     env->cp15.hcrx_el2 = value & valid_mask;
5361 }
5362 
5363 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5364                                   bool isread)
5365 {
5366     if (arm_current_el(env) < 3
5367         && arm_feature(env, ARM_FEATURE_EL3)
5368         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5369         return CP_ACCESS_TRAP_EL3;
5370     }
5371     return CP_ACCESS_OK;
5372 }
5373 
5374 static const ARMCPRegInfo hcrx_el2_reginfo = {
5375     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5376     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5377     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5378     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5379 };
5380 
5381 /* Return the effective value of HCRX_EL2.  */
5382 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5383 {
5384     /*
5385      * The bits in this register behave as 0 for all purposes other than
5386      * direct reads of the register if:
5387      *   - EL2 is not enabled in the current security state,
5388      *   - SCR_EL3.HXEn is 0.
5389      */
5390     if (!arm_is_el2_enabled(env)
5391         || (arm_feature(env, ARM_FEATURE_EL3)
5392             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5393         return 0;
5394     }
5395     return env->cp15.hcrx_el2;
5396 }
5397 
5398 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5399                            uint64_t value)
5400 {
5401     /*
5402      * For A-profile AArch32 EL3, if NSACR.CP10
5403      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5404      */
5405     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5406         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5407         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5408         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5409     }
5410     env->cp15.cptr_el[2] = value;
5411 }
5412 
5413 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5414 {
5415     /*
5416      * For A-profile AArch32 EL3, if NSACR.CP10
5417      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5418      */
5419     uint64_t value = env->cp15.cptr_el[2];
5420 
5421     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5422         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5423         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5424     }
5425     return value;
5426 }
5427 
5428 static const ARMCPRegInfo el2_cp_reginfo[] = {
5429     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5430       .type = ARM_CP_IO,
5431       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5432       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5433       .writefn = hcr_write },
5434     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5435       .type = ARM_CP_ALIAS | ARM_CP_IO,
5436       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5437       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5438       .writefn = hcr_writelow },
5439     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5440       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5441       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5442     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5443       .type = ARM_CP_ALIAS,
5444       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5445       .access = PL2_RW,
5446       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5447     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5448       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5449       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5450     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5451       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5452       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5453     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5454       .type = ARM_CP_ALIAS,
5455       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5456       .access = PL2_RW,
5457       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5458     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5459       .type = ARM_CP_ALIAS,
5460       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5461       .access = PL2_RW,
5462       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5463     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5464       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5465       .access = PL2_RW, .writefn = vbar_write,
5466       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5467       .resetvalue = 0 },
5468     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5469       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5470       .access = PL3_RW, .type = ARM_CP_ALIAS,
5471       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5472     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5473       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5474       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5475       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5476       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5477     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5478       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5479       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5480       .resetvalue = 0 },
5481     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5482       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5483       .access = PL2_RW, .type = ARM_CP_ALIAS,
5484       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5485     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5486       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5487       .access = PL2_RW, .type = ARM_CP_CONST,
5488       .resetvalue = 0 },
5489     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5490     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5491       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5492       .access = PL2_RW, .type = ARM_CP_CONST,
5493       .resetvalue = 0 },
5494     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5495       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5496       .access = PL2_RW, .type = ARM_CP_CONST,
5497       .resetvalue = 0 },
5498     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5499       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5500       .access = PL2_RW, .type = ARM_CP_CONST,
5501       .resetvalue = 0 },
5502     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5503       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5504       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5505       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5506     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5507       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5508       .type = ARM_CP_ALIAS,
5509       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5510       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5511     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5512       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5513       .access = PL2_RW,
5514       /* no .writefn needed as this can't cause an ASID change */
5515       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5516     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5517       .cp = 15, .opc1 = 6, .crm = 2,
5518       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5519       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5520       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5521       .writefn = vttbr_write },
5522     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5523       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5524       .access = PL2_RW, .writefn = vttbr_write,
5525       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5526     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5527       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5528       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5529       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5530     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5531       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5532       .access = PL2_RW, .resetvalue = 0,
5533       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5534     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5535       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5536       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5537       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5538     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5539       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5540       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5541     { .name = "TLBIALLNSNH",
5542       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5543       .type = ARM_CP_NO_RAW, .access = PL2_W,
5544       .writefn = tlbiall_nsnh_write },
5545     { .name = "TLBIALLNSNHIS",
5546       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5547       .type = ARM_CP_NO_RAW, .access = PL2_W,
5548       .writefn = tlbiall_nsnh_is_write },
5549     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5550       .type = ARM_CP_NO_RAW, .access = PL2_W,
5551       .writefn = tlbiall_hyp_write },
5552     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5553       .type = ARM_CP_NO_RAW, .access = PL2_W,
5554       .writefn = tlbiall_hyp_is_write },
5555     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5556       .type = ARM_CP_NO_RAW, .access = PL2_W,
5557       .writefn = tlbimva_hyp_write },
5558     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5559       .type = ARM_CP_NO_RAW, .access = PL2_W,
5560       .writefn = tlbimva_hyp_is_write },
5561     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5562       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5563       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5564       .writefn = tlbi_aa64_alle2_write },
5565     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5566       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5567       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5568       .writefn = tlbi_aa64_vae2_write },
5569     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5570       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5571       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5572       .writefn = tlbi_aa64_vae2_write },
5573     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5574       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5575       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5576       .writefn = tlbi_aa64_alle2is_write },
5577     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5578       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5579       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5580       .writefn = tlbi_aa64_vae2is_write },
5581     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5582       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5583       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5584       .writefn = tlbi_aa64_vae2is_write },
5585 #ifndef CONFIG_USER_ONLY
5586     /* Unlike the other EL2-related AT operations, these must
5587      * UNDEF from EL3 if EL2 is not implemented, which is why we
5588      * define them here rather than with the rest of the AT ops.
5589      */
5590     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5591       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5592       .access = PL2_W, .accessfn = at_s1e2_access,
5593       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5594       .writefn = ats_write64 },
5595     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5596       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5597       .access = PL2_W, .accessfn = at_s1e2_access,
5598       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5599       .writefn = ats_write64 },
5600     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5601      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5602      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5603      * to behave as if SCR.NS was 1.
5604      */
5605     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5606       .access = PL2_W,
5607       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5608     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5609       .access = PL2_W,
5610       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5611     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5612       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5613       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5614        * reset values as IMPDEF. We choose to reset to 3 to comply with
5615        * both ARMv7 and ARMv8.
5616        */
5617       .access = PL2_RW, .resetvalue = 3,
5618       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5619     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5620       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5621       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5622       .writefn = gt_cntvoff_write,
5623       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5624     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5625       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5626       .writefn = gt_cntvoff_write,
5627       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5628     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5629       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5630       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5631       .type = ARM_CP_IO, .access = PL2_RW,
5632       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5633     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5634       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5635       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5636       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5637     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5638       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5639       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5640       .resetfn = gt_hyp_timer_reset,
5641       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5642     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5643       .type = ARM_CP_IO,
5644       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5645       .access = PL2_RW,
5646       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5647       .resetvalue = 0,
5648       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5649 #endif
5650     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5651       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5652       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5653       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5654     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5655       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5656       .access = PL2_RW,
5657       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5658     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5659       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5660       .access = PL2_RW,
5661       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5662 };
5663 
5664 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5665     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5666       .type = ARM_CP_ALIAS | ARM_CP_IO,
5667       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5668       .access = PL2_RW,
5669       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5670       .writefn = hcr_writehigh },
5671 };
5672 
5673 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5674                                   bool isread)
5675 {
5676     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5677         return CP_ACCESS_OK;
5678     }
5679     return CP_ACCESS_TRAP_UNCATEGORIZED;
5680 }
5681 
5682 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5683     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5684       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5685       .access = PL2_RW, .accessfn = sel2_access,
5686       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5687     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5688       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5689       .access = PL2_RW, .accessfn = sel2_access,
5690       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5691 };
5692 
5693 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5694                                    bool isread)
5695 {
5696     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5697      * At Secure EL1 it traps to EL3 or EL2.
5698      */
5699     if (arm_current_el(env) == 3) {
5700         return CP_ACCESS_OK;
5701     }
5702     if (arm_is_secure_below_el3(env)) {
5703         if (env->cp15.scr_el3 & SCR_EEL2) {
5704             return CP_ACCESS_TRAP_EL2;
5705         }
5706         return CP_ACCESS_TRAP_EL3;
5707     }
5708     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5709     if (isread) {
5710         return CP_ACCESS_OK;
5711     }
5712     return CP_ACCESS_TRAP_UNCATEGORIZED;
5713 }
5714 
5715 static const ARMCPRegInfo el3_cp_reginfo[] = {
5716     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5717       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5718       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5719       .resetfn = scr_reset, .writefn = scr_write },
5720     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5721       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5722       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5723       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5724       .writefn = scr_write },
5725     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5726       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5727       .access = PL3_RW, .resetvalue = 0,
5728       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5729     { .name = "SDER",
5730       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5731       .access = PL3_RW, .resetvalue = 0,
5732       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5733     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5734       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5735       .writefn = vbar_write, .resetvalue = 0,
5736       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5737     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5738       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5739       .access = PL3_RW, .resetvalue = 0,
5740       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5741     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5742       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5743       .access = PL3_RW,
5744       /* no .writefn needed as this can't cause an ASID change */
5745       .resetvalue = 0,
5746       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5747     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5748       .type = ARM_CP_ALIAS,
5749       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5750       .access = PL3_RW,
5751       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5752     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5753       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5754       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5755     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5756       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5757       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5758     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5759       .type = ARM_CP_ALIAS,
5760       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5761       .access = PL3_RW,
5762       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5763     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5764       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5765       .access = PL3_RW, .writefn = vbar_write,
5766       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5767       .resetvalue = 0 },
5768     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5769       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5770       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5771       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5772     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5773       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5774       .access = PL3_RW, .resetvalue = 0,
5775       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5776     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5777       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5778       .access = PL3_RW, .type = ARM_CP_CONST,
5779       .resetvalue = 0 },
5780     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5781       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5782       .access = PL3_RW, .type = ARM_CP_CONST,
5783       .resetvalue = 0 },
5784     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5785       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5786       .access = PL3_RW, .type = ARM_CP_CONST,
5787       .resetvalue = 0 },
5788     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5789       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5790       .access = PL3_W, .type = ARM_CP_NO_RAW,
5791       .writefn = tlbi_aa64_alle3is_write },
5792     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5793       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5794       .access = PL3_W, .type = ARM_CP_NO_RAW,
5795       .writefn = tlbi_aa64_vae3is_write },
5796     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5797       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5798       .access = PL3_W, .type = ARM_CP_NO_RAW,
5799       .writefn = tlbi_aa64_vae3is_write },
5800     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5801       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5802       .access = PL3_W, .type = ARM_CP_NO_RAW,
5803       .writefn = tlbi_aa64_alle3_write },
5804     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5805       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5806       .access = PL3_W, .type = ARM_CP_NO_RAW,
5807       .writefn = tlbi_aa64_vae3_write },
5808     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5809       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5810       .access = PL3_W, .type = ARM_CP_NO_RAW,
5811       .writefn = tlbi_aa64_vae3_write },
5812 };
5813 
5814 #ifndef CONFIG_USER_ONLY
5815 /* Test if system register redirection is to occur in the current state.  */
5816 static bool redirect_for_e2h(CPUARMState *env)
5817 {
5818     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5819 }
5820 
5821 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5822 {
5823     CPReadFn *readfn;
5824 
5825     if (redirect_for_e2h(env)) {
5826         /* Switch to the saved EL2 version of the register.  */
5827         ri = ri->opaque;
5828         readfn = ri->readfn;
5829     } else {
5830         readfn = ri->orig_readfn;
5831     }
5832     if (readfn == NULL) {
5833         readfn = raw_read;
5834     }
5835     return readfn(env, ri);
5836 }
5837 
5838 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5839                           uint64_t value)
5840 {
5841     CPWriteFn *writefn;
5842 
5843     if (redirect_for_e2h(env)) {
5844         /* Switch to the saved EL2 version of the register.  */
5845         ri = ri->opaque;
5846         writefn = ri->writefn;
5847     } else {
5848         writefn = ri->orig_writefn;
5849     }
5850     if (writefn == NULL) {
5851         writefn = raw_write;
5852     }
5853     writefn(env, ri, value);
5854 }
5855 
5856 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5857 {
5858     struct E2HAlias {
5859         uint32_t src_key, dst_key, new_key;
5860         const char *src_name, *dst_name, *new_name;
5861         bool (*feature)(const ARMISARegisters *id);
5862     };
5863 
5864 #define K(op0, op1, crn, crm, op2) \
5865     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5866 
5867     static const struct E2HAlias aliases[] = {
5868         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5869           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5870         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5871           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5872         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5873           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5874         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5875           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5876         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5877           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5878         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5879           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5880         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5881           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5882         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5883           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5884         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5885           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5886         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5887           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5888         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5889           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5890         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5891           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5892         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5893           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5894         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5895           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5896         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5897           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5898         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5899           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5900 
5901         /*
5902          * Note that redirection of ZCR is mentioned in the description
5903          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5904          * not in the summary table.
5905          */
5906         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5907           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5908         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5909           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5910 
5911         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5912           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5913 
5914         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5915           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5916           isar_feature_aa64_scxtnum },
5917 
5918         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5919         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5920     };
5921 #undef K
5922 
5923     size_t i;
5924 
5925     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5926         const struct E2HAlias *a = &aliases[i];
5927         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5928         bool ok;
5929 
5930         if (a->feature && !a->feature(&cpu->isar)) {
5931             continue;
5932         }
5933 
5934         src_reg = g_hash_table_lookup(cpu->cp_regs,
5935                                       (gpointer)(uintptr_t)a->src_key);
5936         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5937                                       (gpointer)(uintptr_t)a->dst_key);
5938         g_assert(src_reg != NULL);
5939         g_assert(dst_reg != NULL);
5940 
5941         /* Cross-compare names to detect typos in the keys.  */
5942         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5943         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5944 
5945         /* None of the core system registers use opaque; we will.  */
5946         g_assert(src_reg->opaque == NULL);
5947 
5948         /* Create alias before redirection so we dup the right data. */
5949         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5950 
5951         new_reg->name = a->new_name;
5952         new_reg->type |= ARM_CP_ALIAS;
5953         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5954         new_reg->access &= PL2_RW | PL3_RW;
5955 
5956         ok = g_hash_table_insert(cpu->cp_regs,
5957                                  (gpointer)(uintptr_t)a->new_key, new_reg);
5958         g_assert(ok);
5959 
5960         src_reg->opaque = dst_reg;
5961         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5962         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5963         if (!src_reg->raw_readfn) {
5964             src_reg->raw_readfn = raw_read;
5965         }
5966         if (!src_reg->raw_writefn) {
5967             src_reg->raw_writefn = raw_write;
5968         }
5969         src_reg->readfn = el2_e2h_read;
5970         src_reg->writefn = el2_e2h_write;
5971     }
5972 }
5973 #endif
5974 
5975 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5976                                      bool isread)
5977 {
5978     int cur_el = arm_current_el(env);
5979 
5980     if (cur_el < 2) {
5981         uint64_t hcr = arm_hcr_el2_eff(env);
5982 
5983         if (cur_el == 0) {
5984             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5985                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5986                     return CP_ACCESS_TRAP_EL2;
5987                 }
5988             } else {
5989                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5990                     return CP_ACCESS_TRAP;
5991                 }
5992                 if (hcr & HCR_TID2) {
5993                     return CP_ACCESS_TRAP_EL2;
5994                 }
5995             }
5996         } else if (hcr & HCR_TID2) {
5997             return CP_ACCESS_TRAP_EL2;
5998         }
5999     }
6000 
6001     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6002         return CP_ACCESS_TRAP_EL2;
6003     }
6004 
6005     return CP_ACCESS_OK;
6006 }
6007 
6008 /*
6009  * Check for traps to RAS registers, which are controlled
6010  * by HCR_EL2.TERR and SCR_EL3.TERR.
6011  */
6012 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6013                                   bool isread)
6014 {
6015     int el = arm_current_el(env);
6016 
6017     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6018         return CP_ACCESS_TRAP_EL2;
6019     }
6020     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6021         return CP_ACCESS_TRAP_EL3;
6022     }
6023     return CP_ACCESS_OK;
6024 }
6025 
6026 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6027 {
6028     int el = arm_current_el(env);
6029 
6030     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6031         return env->cp15.vdisr_el2;
6032     }
6033     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6034         return 0; /* RAZ/WI */
6035     }
6036     return env->cp15.disr_el1;
6037 }
6038 
6039 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6040 {
6041     int el = arm_current_el(env);
6042 
6043     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6044         env->cp15.vdisr_el2 = val;
6045         return;
6046     }
6047     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6048         return; /* RAZ/WI */
6049     }
6050     env->cp15.disr_el1 = val;
6051 }
6052 
6053 /*
6054  * Minimal RAS implementation with no Error Records.
6055  * Which means that all of the Error Record registers:
6056  *   ERXADDR_EL1
6057  *   ERXCTLR_EL1
6058  *   ERXFR_EL1
6059  *   ERXMISC0_EL1
6060  *   ERXMISC1_EL1
6061  *   ERXMISC2_EL1
6062  *   ERXMISC3_EL1
6063  *   ERXPFGCDN_EL1  (RASv1p1)
6064  *   ERXPFGCTL_EL1  (RASv1p1)
6065  *   ERXPFGF_EL1    (RASv1p1)
6066  *   ERXSTATUS_EL1
6067  * and
6068  *   ERRSELR_EL1
6069  * may generate UNDEFINED, which is the effect we get by not
6070  * listing them at all.
6071  */
6072 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6073     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6074       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6075       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6076       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6077     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6078       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6079       .access = PL1_R, .accessfn = access_terr,
6080       .type = ARM_CP_CONST, .resetvalue = 0 },
6081     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6082       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6083       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6084     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6085       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6086       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6087 };
6088 
6089 /*
6090  * Return the exception level to which exceptions should be taken
6091  * via SVEAccessTrap.  This excludes the check for whether the exception
6092  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6093  * be found by testing 0 < fp_exception_el < sve_exception_el.
6094  *
6095  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6096  * pseudocode does *not* separate out the FP trap checks, but has them
6097  * all in one function.
6098  */
6099 int sve_exception_el(CPUARMState *env, int el)
6100 {
6101 #ifndef CONFIG_USER_ONLY
6102     if (el <= 1 && !el_is_in_host(env, el)) {
6103         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6104         case 1:
6105             if (el != 0) {
6106                 break;
6107             }
6108             /* fall through */
6109         case 0:
6110         case 2:
6111             return 1;
6112         }
6113     }
6114 
6115     if (el <= 2 && arm_is_el2_enabled(env)) {
6116         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6117         if (env->cp15.hcr_el2 & HCR_E2H) {
6118             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6119             case 1:
6120                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6121                     break;
6122                 }
6123                 /* fall through */
6124             case 0:
6125             case 2:
6126                 return 2;
6127             }
6128         } else {
6129             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6130                 return 2;
6131             }
6132         }
6133     }
6134 
6135     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6136     if (arm_feature(env, ARM_FEATURE_EL3)
6137         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6138         return 3;
6139     }
6140 #endif
6141     return 0;
6142 }
6143 
6144 /*
6145  * Return the exception level to which exceptions should be taken for SME.
6146  * C.f. the ARM pseudocode function CheckSMEAccess.
6147  */
6148 int sme_exception_el(CPUARMState *env, int el)
6149 {
6150 #ifndef CONFIG_USER_ONLY
6151     if (el <= 1 && !el_is_in_host(env, el)) {
6152         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6153         case 1:
6154             if (el != 0) {
6155                 break;
6156             }
6157             /* fall through */
6158         case 0:
6159         case 2:
6160             return 1;
6161         }
6162     }
6163 
6164     if (el <= 2 && arm_is_el2_enabled(env)) {
6165         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6166         if (env->cp15.hcr_el2 & HCR_E2H) {
6167             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6168             case 1:
6169                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6170                     break;
6171                 }
6172                 /* fall through */
6173             case 0:
6174             case 2:
6175                 return 2;
6176             }
6177         } else {
6178             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6179                 return 2;
6180             }
6181         }
6182     }
6183 
6184     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6185     if (arm_feature(env, ARM_FEATURE_EL3)
6186         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6187         return 3;
6188     }
6189 #endif
6190     return 0;
6191 }
6192 
6193 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6194 static bool sme_fa64(CPUARMState *env, int el)
6195 {
6196     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6197         return false;
6198     }
6199 
6200     if (el <= 1 && !el_is_in_host(env, el)) {
6201         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6202             return false;
6203         }
6204     }
6205     if (el <= 2 && arm_is_el2_enabled(env)) {
6206         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6207             return false;
6208         }
6209     }
6210     if (arm_feature(env, ARM_FEATURE_EL3)) {
6211         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6212             return false;
6213         }
6214     }
6215 
6216     return true;
6217 }
6218 
6219 /*
6220  * Given that SVE is enabled, return the vector length for EL.
6221  */
6222 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6223 {
6224     ARMCPU *cpu = env_archcpu(env);
6225     uint64_t *cr = env->vfp.zcr_el;
6226     uint32_t map = cpu->sve_vq.map;
6227     uint32_t len = ARM_MAX_VQ - 1;
6228 
6229     if (sm) {
6230         cr = env->vfp.smcr_el;
6231         map = cpu->sme_vq.map;
6232     }
6233 
6234     if (el <= 1 && !el_is_in_host(env, el)) {
6235         len = MIN(len, 0xf & (uint32_t)cr[1]);
6236     }
6237     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6238         len = MIN(len, 0xf & (uint32_t)cr[2]);
6239     }
6240     if (arm_feature(env, ARM_FEATURE_EL3)) {
6241         len = MIN(len, 0xf & (uint32_t)cr[3]);
6242     }
6243 
6244     map &= MAKE_64BIT_MASK(0, len + 1);
6245     if (map != 0) {
6246         return 31 - clz32(map);
6247     }
6248 
6249     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6250     assert(sm);
6251     return ctz32(cpu->sme_vq.map);
6252 }
6253 
6254 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6255 {
6256     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6257 }
6258 
6259 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6260                       uint64_t value)
6261 {
6262     int cur_el = arm_current_el(env);
6263     int old_len = sve_vqm1_for_el(env, cur_el);
6264     int new_len;
6265 
6266     /* Bits other than [3:0] are RAZ/WI.  */
6267     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6268     raw_write(env, ri, value & 0xf);
6269 
6270     /*
6271      * Because we arrived here, we know both FP and SVE are enabled;
6272      * otherwise we would have trapped access to the ZCR_ELn register.
6273      */
6274     new_len = sve_vqm1_for_el(env, cur_el);
6275     if (new_len < old_len) {
6276         aarch64_sve_narrow_vq(env, new_len + 1);
6277     }
6278 }
6279 
6280 static const ARMCPRegInfo zcr_reginfo[] = {
6281     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6282       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6283       .access = PL1_RW, .type = ARM_CP_SVE,
6284       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6285       .writefn = zcr_write, .raw_writefn = raw_write },
6286     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6287       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6288       .access = PL2_RW, .type = ARM_CP_SVE,
6289       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6290       .writefn = zcr_write, .raw_writefn = raw_write },
6291     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6292       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6293       .access = PL3_RW, .type = ARM_CP_SVE,
6294       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6295       .writefn = zcr_write, .raw_writefn = raw_write },
6296 };
6297 
6298 #ifdef TARGET_AARCH64
6299 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6300                                     bool isread)
6301 {
6302     int el = arm_current_el(env);
6303 
6304     if (el == 0) {
6305         uint64_t sctlr = arm_sctlr(env, el);
6306         if (!(sctlr & SCTLR_EnTP2)) {
6307             return CP_ACCESS_TRAP;
6308         }
6309     }
6310     /* TODO: FEAT_FGT */
6311     if (el < 3
6312         && arm_feature(env, ARM_FEATURE_EL3)
6313         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6314         return CP_ACCESS_TRAP_EL3;
6315     }
6316     return CP_ACCESS_OK;
6317 }
6318 
6319 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6320                                  bool isread)
6321 {
6322     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6323     if (arm_current_el(env) < 3
6324         && arm_feature(env, ARM_FEATURE_EL3)
6325         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6326         return CP_ACCESS_TRAP_EL3;
6327     }
6328     return CP_ACCESS_OK;
6329 }
6330 
6331 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6332                        uint64_t value)
6333 {
6334     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6335     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6336     arm_rebuild_hflags(env);
6337 }
6338 
6339 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6340                        uint64_t value)
6341 {
6342     int cur_el = arm_current_el(env);
6343     int old_len = sve_vqm1_for_el(env, cur_el);
6344     int new_len;
6345 
6346     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6347     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6348     raw_write(env, ri, value);
6349 
6350     /*
6351      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6352      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6353      * current values for simplicity.  But for QEMU internals, we must still
6354      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6355      * above aarch64_sve_narrow_vq.
6356      */
6357     new_len = sve_vqm1_for_el(env, cur_el);
6358     if (new_len < old_len) {
6359         aarch64_sve_narrow_vq(env, new_len + 1);
6360     }
6361 }
6362 
6363 static const ARMCPRegInfo sme_reginfo[] = {
6364     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6365       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6366       .access = PL0_RW, .accessfn = access_tpidr2,
6367       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6368     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6369       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6370       .access = PL0_RW, .type = ARM_CP_SME,
6371       .fieldoffset = offsetof(CPUARMState, svcr),
6372       .writefn = svcr_write, .raw_writefn = raw_write },
6373     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6374       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6375       .access = PL1_RW, .type = ARM_CP_SME,
6376       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6377       .writefn = smcr_write, .raw_writefn = raw_write },
6378     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6379       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6380       .access = PL2_RW, .type = ARM_CP_SME,
6381       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6382       .writefn = smcr_write, .raw_writefn = raw_write },
6383     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6384       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6385       .access = PL3_RW, .type = ARM_CP_SME,
6386       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6387       .writefn = smcr_write, .raw_writefn = raw_write },
6388     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6389       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6390       .access = PL1_R, .accessfn = access_aa64_tid1,
6391       /*
6392        * IMPLEMENTOR = 0 (software)
6393        * REVISION    = 0 (implementation defined)
6394        * SMPS        = 0 (no streaming execution priority in QEMU)
6395        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6396        */
6397       .type = ARM_CP_CONST, .resetvalue = 0, },
6398     /*
6399      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6400      */
6401     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6402       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6403       .access = PL1_RW, .accessfn = access_esm,
6404       .type = ARM_CP_CONST, .resetvalue = 0 },
6405     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6406       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6407       .access = PL2_RW, .accessfn = access_esm,
6408       .type = ARM_CP_CONST, .resetvalue = 0 },
6409 };
6410 #endif /* TARGET_AARCH64 */
6411 
6412 static void define_pmu_regs(ARMCPU *cpu)
6413 {
6414     /*
6415      * v7 performance monitor control register: same implementor
6416      * field as main ID register, and we implement four counters in
6417      * addition to the cycle count register.
6418      */
6419     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6420     ARMCPRegInfo pmcr = {
6421         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6422         .access = PL0_RW,
6423         .type = ARM_CP_IO | ARM_CP_ALIAS,
6424         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6425         .accessfn = pmreg_access, .writefn = pmcr_write,
6426         .raw_writefn = raw_write,
6427     };
6428     ARMCPRegInfo pmcr64 = {
6429         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6430         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6431         .access = PL0_RW, .accessfn = pmreg_access,
6432         .type = ARM_CP_IO,
6433         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6434         .resetvalue = cpu->isar.reset_pmcr_el0,
6435         .writefn = pmcr_write, .raw_writefn = raw_write,
6436     };
6437 
6438     define_one_arm_cp_reg(cpu, &pmcr);
6439     define_one_arm_cp_reg(cpu, &pmcr64);
6440     for (i = 0; i < pmcrn; i++) {
6441         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6442         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6443         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6444         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6445         ARMCPRegInfo pmev_regs[] = {
6446             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6447               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6448               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6449               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6450               .accessfn = pmreg_access_xevcntr },
6451             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6452               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6453               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6454               .type = ARM_CP_IO,
6455               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6456               .raw_readfn = pmevcntr_rawread,
6457               .raw_writefn = pmevcntr_rawwrite },
6458             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6459               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6460               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6461               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6462               .accessfn = pmreg_access },
6463             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6464               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6465               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6466               .type = ARM_CP_IO,
6467               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6468               .raw_writefn = pmevtyper_rawwrite },
6469         };
6470         define_arm_cp_regs(cpu, pmev_regs);
6471         g_free(pmevcntr_name);
6472         g_free(pmevcntr_el0_name);
6473         g_free(pmevtyper_name);
6474         g_free(pmevtyper_el0_name);
6475     }
6476     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6477         ARMCPRegInfo v81_pmu_regs[] = {
6478             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6479               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6480               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6481               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6482             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6483               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6484               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6485               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6486         };
6487         define_arm_cp_regs(cpu, v81_pmu_regs);
6488     }
6489     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6490         static const ARMCPRegInfo v84_pmmir = {
6491             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6492             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6493             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6494             .resetvalue = 0
6495         };
6496         define_one_arm_cp_reg(cpu, &v84_pmmir);
6497     }
6498 }
6499 
6500 /* We don't know until after realize whether there's a GICv3
6501  * attached, and that is what registers the gicv3 sysregs.
6502  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6503  * at runtime.
6504  */
6505 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6506 {
6507     ARMCPU *cpu = env_archcpu(env);
6508     uint64_t pfr1 = cpu->isar.id_pfr1;
6509 
6510     if (env->gicv3state) {
6511         pfr1 |= 1 << 28;
6512     }
6513     return pfr1;
6514 }
6515 
6516 #ifndef CONFIG_USER_ONLY
6517 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6518 {
6519     ARMCPU *cpu = env_archcpu(env);
6520     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6521 
6522     if (env->gicv3state) {
6523         pfr0 |= 1 << 24;
6524     }
6525     return pfr0;
6526 }
6527 #endif
6528 
6529 /* Shared logic between LORID and the rest of the LOR* registers.
6530  * Secure state exclusion has already been dealt with.
6531  */
6532 static CPAccessResult access_lor_ns(CPUARMState *env,
6533                                     const ARMCPRegInfo *ri, bool isread)
6534 {
6535     int el = arm_current_el(env);
6536 
6537     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6538         return CP_ACCESS_TRAP_EL2;
6539     }
6540     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6541         return CP_ACCESS_TRAP_EL3;
6542     }
6543     return CP_ACCESS_OK;
6544 }
6545 
6546 static CPAccessResult access_lor_other(CPUARMState *env,
6547                                        const ARMCPRegInfo *ri, bool isread)
6548 {
6549     if (arm_is_secure_below_el3(env)) {
6550         /* Access denied in secure mode.  */
6551         return CP_ACCESS_TRAP;
6552     }
6553     return access_lor_ns(env, ri, isread);
6554 }
6555 
6556 /*
6557  * A trivial implementation of ARMv8.1-LOR leaves all of these
6558  * registers fixed at 0, which indicates that there are zero
6559  * supported Limited Ordering regions.
6560  */
6561 static const ARMCPRegInfo lor_reginfo[] = {
6562     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6563       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6564       .access = PL1_RW, .accessfn = access_lor_other,
6565       .type = ARM_CP_CONST, .resetvalue = 0 },
6566     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6567       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6568       .access = PL1_RW, .accessfn = access_lor_other,
6569       .type = ARM_CP_CONST, .resetvalue = 0 },
6570     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6571       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6572       .access = PL1_RW, .accessfn = access_lor_other,
6573       .type = ARM_CP_CONST, .resetvalue = 0 },
6574     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6575       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6576       .access = PL1_RW, .accessfn = access_lor_other,
6577       .type = ARM_CP_CONST, .resetvalue = 0 },
6578     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6579       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6580       .access = PL1_R, .accessfn = access_lor_ns,
6581       .type = ARM_CP_CONST, .resetvalue = 0 },
6582 };
6583 
6584 #ifdef TARGET_AARCH64
6585 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6586                                    bool isread)
6587 {
6588     int el = arm_current_el(env);
6589 
6590     if (el < 2 &&
6591         arm_is_el2_enabled(env) &&
6592         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6593         return CP_ACCESS_TRAP_EL2;
6594     }
6595     if (el < 3 &&
6596         arm_feature(env, ARM_FEATURE_EL3) &&
6597         !(env->cp15.scr_el3 & SCR_APK)) {
6598         return CP_ACCESS_TRAP_EL3;
6599     }
6600     return CP_ACCESS_OK;
6601 }
6602 
6603 static const ARMCPRegInfo pauth_reginfo[] = {
6604     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6605       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6606       .access = PL1_RW, .accessfn = access_pauth,
6607       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6608     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6609       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6610       .access = PL1_RW, .accessfn = access_pauth,
6611       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6612     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6613       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6614       .access = PL1_RW, .accessfn = access_pauth,
6615       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6616     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6617       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6618       .access = PL1_RW, .accessfn = access_pauth,
6619       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6620     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6621       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6622       .access = PL1_RW, .accessfn = access_pauth,
6623       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6624     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6625       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6626       .access = PL1_RW, .accessfn = access_pauth,
6627       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6628     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6629       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6630       .access = PL1_RW, .accessfn = access_pauth,
6631       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6632     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6633       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6634       .access = PL1_RW, .accessfn = access_pauth,
6635       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6636     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6637       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6638       .access = PL1_RW, .accessfn = access_pauth,
6639       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6640     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6641       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6642       .access = PL1_RW, .accessfn = access_pauth,
6643       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6644 };
6645 
6646 static const ARMCPRegInfo tlbirange_reginfo[] = {
6647     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6648       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6649       .access = PL1_W, .type = ARM_CP_NO_RAW,
6650       .writefn = tlbi_aa64_rvae1is_write },
6651     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6652       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6653       .access = PL1_W, .type = ARM_CP_NO_RAW,
6654       .writefn = tlbi_aa64_rvae1is_write },
6655    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6656       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6657       .access = PL1_W, .type = ARM_CP_NO_RAW,
6658       .writefn = tlbi_aa64_rvae1is_write },
6659     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6660       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6661       .access = PL1_W, .type = ARM_CP_NO_RAW,
6662       .writefn = tlbi_aa64_rvae1is_write },
6663     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6664       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6665       .access = PL1_W, .type = ARM_CP_NO_RAW,
6666       .writefn = tlbi_aa64_rvae1is_write },
6667     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6668       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6669       .access = PL1_W, .type = ARM_CP_NO_RAW,
6670       .writefn = tlbi_aa64_rvae1is_write },
6671    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6672       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6673       .access = PL1_W, .type = ARM_CP_NO_RAW,
6674       .writefn = tlbi_aa64_rvae1is_write },
6675     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6676       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6677       .access = PL1_W, .type = ARM_CP_NO_RAW,
6678       .writefn = tlbi_aa64_rvae1is_write },
6679     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6680       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6681       .access = PL1_W, .type = ARM_CP_NO_RAW,
6682       .writefn = tlbi_aa64_rvae1_write },
6683     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6684       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6685       .access = PL1_W, .type = ARM_CP_NO_RAW,
6686       .writefn = tlbi_aa64_rvae1_write },
6687    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6688       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6689       .access = PL1_W, .type = ARM_CP_NO_RAW,
6690       .writefn = tlbi_aa64_rvae1_write },
6691     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6692       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6693       .access = PL1_W, .type = ARM_CP_NO_RAW,
6694       .writefn = tlbi_aa64_rvae1_write },
6695     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6696       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6697       .access = PL2_W, .type = ARM_CP_NOP },
6698     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6699       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6700       .access = PL2_W, .type = ARM_CP_NOP },
6701     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6702       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6703       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6704       .writefn = tlbi_aa64_rvae2is_write },
6705    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6706       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6707       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6708       .writefn = tlbi_aa64_rvae2is_write },
6709     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6710       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6711       .access = PL2_W, .type = ARM_CP_NOP },
6712    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6713       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6714       .access = PL2_W, .type = ARM_CP_NOP },
6715    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6716       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6717       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6718       .writefn = tlbi_aa64_rvae2is_write },
6719    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6720       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6721       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6722       .writefn = tlbi_aa64_rvae2is_write },
6723     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6724       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6725       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6726       .writefn = tlbi_aa64_rvae2_write },
6727    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6728       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6729       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6730       .writefn = tlbi_aa64_rvae2_write },
6731    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6732       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6733       .access = PL3_W, .type = ARM_CP_NO_RAW,
6734       .writefn = tlbi_aa64_rvae3is_write },
6735    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6736       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6737       .access = PL3_W, .type = ARM_CP_NO_RAW,
6738       .writefn = tlbi_aa64_rvae3is_write },
6739    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6740       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6741       .access = PL3_W, .type = ARM_CP_NO_RAW,
6742       .writefn = tlbi_aa64_rvae3is_write },
6743    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6744       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6745       .access = PL3_W, .type = ARM_CP_NO_RAW,
6746       .writefn = tlbi_aa64_rvae3is_write },
6747    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6748       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6749       .access = PL3_W, .type = ARM_CP_NO_RAW,
6750       .writefn = tlbi_aa64_rvae3_write },
6751    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6752       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6753       .access = PL3_W, .type = ARM_CP_NO_RAW,
6754       .writefn = tlbi_aa64_rvae3_write },
6755 };
6756 
6757 static const ARMCPRegInfo tlbios_reginfo[] = {
6758     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6759       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6760       .access = PL1_W, .type = ARM_CP_NO_RAW,
6761       .writefn = tlbi_aa64_vmalle1is_write },
6762     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6763       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6764       .access = PL1_W, .type = ARM_CP_NO_RAW,
6765       .writefn = tlbi_aa64_vae1is_write },
6766     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6767       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6768       .access = PL1_W, .type = ARM_CP_NO_RAW,
6769       .writefn = tlbi_aa64_vmalle1is_write },
6770     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6771       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6772       .access = PL1_W, .type = ARM_CP_NO_RAW,
6773       .writefn = tlbi_aa64_vae1is_write },
6774     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6775       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6776       .access = PL1_W, .type = ARM_CP_NO_RAW,
6777       .writefn = tlbi_aa64_vae1is_write },
6778     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6779       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6780       .access = PL1_W, .type = ARM_CP_NO_RAW,
6781       .writefn = tlbi_aa64_vae1is_write },
6782     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6784       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6785       .writefn = tlbi_aa64_alle2is_write },
6786     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6787       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6788       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6789       .writefn = tlbi_aa64_vae2is_write },
6790    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6791       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6792       .access = PL2_W, .type = ARM_CP_NO_RAW,
6793       .writefn = tlbi_aa64_alle1is_write },
6794     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6795       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6796       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6797       .writefn = tlbi_aa64_vae2is_write },
6798     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6799       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6800       .access = PL2_W, .type = ARM_CP_NO_RAW,
6801       .writefn = tlbi_aa64_alle1is_write },
6802     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6804       .access = PL2_W, .type = ARM_CP_NOP },
6805     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6806       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6807       .access = PL2_W, .type = ARM_CP_NOP },
6808     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6809       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6810       .access = PL2_W, .type = ARM_CP_NOP },
6811     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6812       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6813       .access = PL2_W, .type = ARM_CP_NOP },
6814     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6815       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6816       .access = PL3_W, .type = ARM_CP_NO_RAW,
6817       .writefn = tlbi_aa64_alle3is_write },
6818     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6819       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6820       .access = PL3_W, .type = ARM_CP_NO_RAW,
6821       .writefn = tlbi_aa64_vae3is_write },
6822     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6824       .access = PL3_W, .type = ARM_CP_NO_RAW,
6825       .writefn = tlbi_aa64_vae3is_write },
6826 };
6827 
6828 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6829 {
6830     Error *err = NULL;
6831     uint64_t ret;
6832 
6833     /* Success sets NZCV = 0000.  */
6834     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6835 
6836     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6837         /*
6838          * ??? Failed, for unknown reasons in the crypto subsystem.
6839          * The best we can do is log the reason and return the
6840          * timed-out indication to the guest.  There is no reason
6841          * we know to expect this failure to be transitory, so the
6842          * guest may well hang retrying the operation.
6843          */
6844         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6845                       ri->name, error_get_pretty(err));
6846         error_free(err);
6847 
6848         env->ZF = 0; /* NZCF = 0100 */
6849         return 0;
6850     }
6851     return ret;
6852 }
6853 
6854 /* We do not support re-seeding, so the two registers operate the same.  */
6855 static const ARMCPRegInfo rndr_reginfo[] = {
6856     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6857       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6858       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6859       .access = PL0_R, .readfn = rndr_readfn },
6860     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6861       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6862       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6863       .access = PL0_R, .readfn = rndr_readfn },
6864 };
6865 
6866 #ifndef CONFIG_USER_ONLY
6867 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6868                           uint64_t value)
6869 {
6870     ARMCPU *cpu = env_archcpu(env);
6871     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6872     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6873     uint64_t vaddr_in = (uint64_t) value;
6874     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6875     void *haddr;
6876     int mem_idx = cpu_mmu_index(env, false);
6877 
6878     /* This won't be crossing page boundaries */
6879     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6880     if (haddr) {
6881 
6882         ram_addr_t offset;
6883         MemoryRegion *mr;
6884 
6885         /* RCU lock is already being held */
6886         mr = memory_region_from_host(haddr, &offset);
6887 
6888         if (mr) {
6889             memory_region_writeback(mr, offset, dline_size);
6890         }
6891     }
6892 }
6893 
6894 static const ARMCPRegInfo dcpop_reg[] = {
6895     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6896       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6897       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6898       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6899 };
6900 
6901 static const ARMCPRegInfo dcpodp_reg[] = {
6902     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6903       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6904       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6905       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6906 };
6907 #endif /*CONFIG_USER_ONLY*/
6908 
6909 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6910                                        bool isread)
6911 {
6912     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6913         return CP_ACCESS_TRAP_EL2;
6914     }
6915 
6916     return CP_ACCESS_OK;
6917 }
6918 
6919 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6920                                  bool isread)
6921 {
6922     int el = arm_current_el(env);
6923 
6924     if (el < 2 && arm_is_el2_enabled(env)) {
6925         uint64_t hcr = arm_hcr_el2_eff(env);
6926         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6927             return CP_ACCESS_TRAP_EL2;
6928         }
6929     }
6930     if (el < 3 &&
6931         arm_feature(env, ARM_FEATURE_EL3) &&
6932         !(env->cp15.scr_el3 & SCR_ATA)) {
6933         return CP_ACCESS_TRAP_EL3;
6934     }
6935     return CP_ACCESS_OK;
6936 }
6937 
6938 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6939 {
6940     return env->pstate & PSTATE_TCO;
6941 }
6942 
6943 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6944 {
6945     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6946 }
6947 
6948 static const ARMCPRegInfo mte_reginfo[] = {
6949     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6950       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6951       .access = PL1_RW, .accessfn = access_mte,
6952       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6953     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6954       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6955       .access = PL1_RW, .accessfn = access_mte,
6956       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6957     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6958       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6959       .access = PL2_RW, .accessfn = access_mte,
6960       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6961     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6962       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6963       .access = PL3_RW,
6964       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6965     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6966       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6967       .access = PL1_RW, .accessfn = access_mte,
6968       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6969     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6970       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6971       .access = PL1_RW, .accessfn = access_mte,
6972       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6973     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6974       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6975       .access = PL1_R, .accessfn = access_aa64_tid5,
6976       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6977     { .name = "TCO", .state = ARM_CP_STATE_AA64,
6978       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6979       .type = ARM_CP_NO_RAW,
6980       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6981     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6982       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6983       .type = ARM_CP_NOP, .access = PL1_W,
6984       .accessfn = aa64_cacheop_poc_access },
6985     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6986       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6987       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6988     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6989       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6990       .type = ARM_CP_NOP, .access = PL1_W,
6991       .accessfn = aa64_cacheop_poc_access },
6992     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6993       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6994       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6995     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6996       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6997       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6998     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6999       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7000       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7001     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7002       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7003       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7004     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7005       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7006       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7007 };
7008 
7009 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7010     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7011       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7012       .type = ARM_CP_CONST, .access = PL0_RW, },
7013 };
7014 
7015 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7016     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7017       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7018       .type = ARM_CP_NOP, .access = PL0_W,
7019       .accessfn = aa64_cacheop_poc_access },
7020     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7021       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7022       .type = ARM_CP_NOP, .access = PL0_W,
7023       .accessfn = aa64_cacheop_poc_access },
7024     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7025       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7026       .type = ARM_CP_NOP, .access = PL0_W,
7027       .accessfn = aa64_cacheop_poc_access },
7028     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7029       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7030       .type = ARM_CP_NOP, .access = PL0_W,
7031       .accessfn = aa64_cacheop_poc_access },
7032     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7033       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7034       .type = ARM_CP_NOP, .access = PL0_W,
7035       .accessfn = aa64_cacheop_poc_access },
7036     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7038       .type = ARM_CP_NOP, .access = PL0_W,
7039       .accessfn = aa64_cacheop_poc_access },
7040     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7042       .type = ARM_CP_NOP, .access = PL0_W,
7043       .accessfn = aa64_cacheop_poc_access },
7044     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7046       .type = ARM_CP_NOP, .access = PL0_W,
7047       .accessfn = aa64_cacheop_poc_access },
7048     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7050       .access = PL0_W, .type = ARM_CP_DC_GVA,
7051 #ifndef CONFIG_USER_ONLY
7052       /* Avoid overhead of an access check that always passes in user-mode */
7053       .accessfn = aa64_zva_access,
7054 #endif
7055     },
7056     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7058       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7059 #ifndef CONFIG_USER_ONLY
7060       /* Avoid overhead of an access check that always passes in user-mode */
7061       .accessfn = aa64_zva_access,
7062 #endif
7063     },
7064 };
7065 
7066 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7067                                      bool isread)
7068 {
7069     uint64_t hcr = arm_hcr_el2_eff(env);
7070     int el = arm_current_el(env);
7071 
7072     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7073         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7074             if (hcr & HCR_TGE) {
7075                 return CP_ACCESS_TRAP_EL2;
7076             }
7077             return CP_ACCESS_TRAP;
7078         }
7079     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7080         return CP_ACCESS_TRAP_EL2;
7081     }
7082     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7083         return CP_ACCESS_TRAP_EL2;
7084     }
7085     if (el < 3
7086         && arm_feature(env, ARM_FEATURE_EL3)
7087         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7088         return CP_ACCESS_TRAP_EL3;
7089     }
7090     return CP_ACCESS_OK;
7091 }
7092 
7093 static const ARMCPRegInfo scxtnum_reginfo[] = {
7094     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7095       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7096       .access = PL0_RW, .accessfn = access_scxtnum,
7097       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7098     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7099       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7100       .access = PL1_RW, .accessfn = access_scxtnum,
7101       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7102     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7103       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7104       .access = PL2_RW, .accessfn = access_scxtnum,
7105       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7106     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7107       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7108       .access = PL3_RW,
7109       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7110 };
7111 #endif /* TARGET_AARCH64 */
7112 
7113 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7114                                      bool isread)
7115 {
7116     int el = arm_current_el(env);
7117 
7118     if (el == 0) {
7119         uint64_t sctlr = arm_sctlr(env, el);
7120         if (!(sctlr & SCTLR_EnRCTX)) {
7121             return CP_ACCESS_TRAP;
7122         }
7123     } else if (el == 1) {
7124         uint64_t hcr = arm_hcr_el2_eff(env);
7125         if (hcr & HCR_NV) {
7126             return CP_ACCESS_TRAP_EL2;
7127         }
7128     }
7129     return CP_ACCESS_OK;
7130 }
7131 
7132 static const ARMCPRegInfo predinv_reginfo[] = {
7133     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7134       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7135       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7136     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7137       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7138       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7139     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7141       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7142     /*
7143      * Note the AArch32 opcodes have a different OPC1.
7144      */
7145     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7146       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7147       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7148     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7149       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7150       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7151     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7152       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7153       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7154 };
7155 
7156 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7157 {
7158     /* Read the high 32 bits of the current CCSIDR */
7159     return extract64(ccsidr_read(env, ri), 32, 32);
7160 }
7161 
7162 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7163     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7164       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7165       .access = PL1_R,
7166       .accessfn = access_aa64_tid2,
7167       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7168 };
7169 
7170 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7171                                        bool isread)
7172 {
7173     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7174         return CP_ACCESS_TRAP_EL2;
7175     }
7176 
7177     return CP_ACCESS_OK;
7178 }
7179 
7180 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7181                                        bool isread)
7182 {
7183     if (arm_feature(env, ARM_FEATURE_V8)) {
7184         return access_aa64_tid3(env, ri, isread);
7185     }
7186 
7187     return CP_ACCESS_OK;
7188 }
7189 
7190 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7191                                      bool isread)
7192 {
7193     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7194         return CP_ACCESS_TRAP_EL2;
7195     }
7196 
7197     return CP_ACCESS_OK;
7198 }
7199 
7200 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7201                                         const ARMCPRegInfo *ri, bool isread)
7202 {
7203     /*
7204      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7205      * in v7A, not in v8A.
7206      */
7207     if (!arm_feature(env, ARM_FEATURE_V8) &&
7208         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7209         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7210         return CP_ACCESS_TRAP_EL2;
7211     }
7212     return CP_ACCESS_OK;
7213 }
7214 
7215 static const ARMCPRegInfo jazelle_regs[] = {
7216     { .name = "JIDR",
7217       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7218       .access = PL1_R, .accessfn = access_jazelle,
7219       .type = ARM_CP_CONST, .resetvalue = 0 },
7220     { .name = "JOSCR",
7221       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7222       .accessfn = access_joscr_jmcr,
7223       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7224     { .name = "JMCR",
7225       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7226       .accessfn = access_joscr_jmcr,
7227       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7228 };
7229 
7230 static const ARMCPRegInfo contextidr_el2 = {
7231     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7232     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7233     .access = PL2_RW,
7234     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7235 };
7236 
7237 static const ARMCPRegInfo vhe_reginfo[] = {
7238     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7239       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7240       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7241       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7242 #ifndef CONFIG_USER_ONLY
7243     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7244       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7245       .fieldoffset =
7246         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7247       .type = ARM_CP_IO, .access = PL2_RW,
7248       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7249     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7250       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7251       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7252       .resetfn = gt_hv_timer_reset,
7253       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7254     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7255       .type = ARM_CP_IO,
7256       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7257       .access = PL2_RW,
7258       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7259       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7260     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7261       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7262       .type = ARM_CP_IO | ARM_CP_ALIAS,
7263       .access = PL2_RW, .accessfn = e2h_access,
7264       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7265       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7266     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7267       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7268       .type = ARM_CP_IO | ARM_CP_ALIAS,
7269       .access = PL2_RW, .accessfn = e2h_access,
7270       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7271       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7272     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7273       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7274       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7275       .access = PL2_RW, .accessfn = e2h_access,
7276       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7277     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7278       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7279       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7280       .access = PL2_RW, .accessfn = e2h_access,
7281       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7282     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7283       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7284       .type = ARM_CP_IO | ARM_CP_ALIAS,
7285       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7286       .access = PL2_RW, .accessfn = e2h_access,
7287       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7288     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7290       .type = ARM_CP_IO | ARM_CP_ALIAS,
7291       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7292       .access = PL2_RW, .accessfn = e2h_access,
7293       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7294 #endif
7295 };
7296 
7297 #ifndef CONFIG_USER_ONLY
7298 static const ARMCPRegInfo ats1e1_reginfo[] = {
7299     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7300       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7301       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7302       .writefn = ats_write64 },
7303     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7304       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7305       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7306       .writefn = ats_write64 },
7307 };
7308 
7309 static const ARMCPRegInfo ats1cp_reginfo[] = {
7310     { .name = "ATS1CPRP",
7311       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7312       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7313       .writefn = ats_write },
7314     { .name = "ATS1CPWP",
7315       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7316       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7317       .writefn = ats_write },
7318 };
7319 #endif
7320 
7321 /*
7322  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7323  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7324  * is non-zero, which is never for ARMv7, optionally in ARMv8
7325  * and mandatorily for ARMv8.2 and up.
7326  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7327  * implementation is RAZ/WI we can ignore this detail, as we
7328  * do for ACTLR.
7329  */
7330 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7331     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7332       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7333       .access = PL1_RW, .accessfn = access_tacr,
7334       .type = ARM_CP_CONST, .resetvalue = 0 },
7335     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7336       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7337       .access = PL2_RW, .type = ARM_CP_CONST,
7338       .resetvalue = 0 },
7339 };
7340 
7341 void register_cp_regs_for_features(ARMCPU *cpu)
7342 {
7343     /* Register all the coprocessor registers based on feature bits */
7344     CPUARMState *env = &cpu->env;
7345     if (arm_feature(env, ARM_FEATURE_M)) {
7346         /* M profile has no coprocessor registers */
7347         return;
7348     }
7349 
7350     define_arm_cp_regs(cpu, cp_reginfo);
7351     if (!arm_feature(env, ARM_FEATURE_V8)) {
7352         /* Must go early as it is full of wildcards that may be
7353          * overridden by later definitions.
7354          */
7355         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7356     }
7357 
7358     if (arm_feature(env, ARM_FEATURE_V6)) {
7359         /* The ID registers all have impdef reset values */
7360         ARMCPRegInfo v6_idregs[] = {
7361             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7362               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7363               .access = PL1_R, .type = ARM_CP_CONST,
7364               .accessfn = access_aa32_tid3,
7365               .resetvalue = cpu->isar.id_pfr0 },
7366             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7367              * the value of the GIC field until after we define these regs.
7368              */
7369             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7370               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7371               .access = PL1_R, .type = ARM_CP_NO_RAW,
7372               .accessfn = access_aa32_tid3,
7373               .readfn = id_pfr1_read,
7374               .writefn = arm_cp_write_ignore },
7375             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7376               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7377               .access = PL1_R, .type = ARM_CP_CONST,
7378               .accessfn = access_aa32_tid3,
7379               .resetvalue = cpu->isar.id_dfr0 },
7380             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7381               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7382               .access = PL1_R, .type = ARM_CP_CONST,
7383               .accessfn = access_aa32_tid3,
7384               .resetvalue = cpu->id_afr0 },
7385             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7386               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7387               .access = PL1_R, .type = ARM_CP_CONST,
7388               .accessfn = access_aa32_tid3,
7389               .resetvalue = cpu->isar.id_mmfr0 },
7390             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7391               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7392               .access = PL1_R, .type = ARM_CP_CONST,
7393               .accessfn = access_aa32_tid3,
7394               .resetvalue = cpu->isar.id_mmfr1 },
7395             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7396               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7397               .access = PL1_R, .type = ARM_CP_CONST,
7398               .accessfn = access_aa32_tid3,
7399               .resetvalue = cpu->isar.id_mmfr2 },
7400             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7401               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7402               .access = PL1_R, .type = ARM_CP_CONST,
7403               .accessfn = access_aa32_tid3,
7404               .resetvalue = cpu->isar.id_mmfr3 },
7405             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7406               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7407               .access = PL1_R, .type = ARM_CP_CONST,
7408               .accessfn = access_aa32_tid3,
7409               .resetvalue = cpu->isar.id_isar0 },
7410             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7411               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7412               .access = PL1_R, .type = ARM_CP_CONST,
7413               .accessfn = access_aa32_tid3,
7414               .resetvalue = cpu->isar.id_isar1 },
7415             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7416               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7417               .access = PL1_R, .type = ARM_CP_CONST,
7418               .accessfn = access_aa32_tid3,
7419               .resetvalue = cpu->isar.id_isar2 },
7420             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7421               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7422               .access = PL1_R, .type = ARM_CP_CONST,
7423               .accessfn = access_aa32_tid3,
7424               .resetvalue = cpu->isar.id_isar3 },
7425             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7426               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7427               .access = PL1_R, .type = ARM_CP_CONST,
7428               .accessfn = access_aa32_tid3,
7429               .resetvalue = cpu->isar.id_isar4 },
7430             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7431               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7432               .access = PL1_R, .type = ARM_CP_CONST,
7433               .accessfn = access_aa32_tid3,
7434               .resetvalue = cpu->isar.id_isar5 },
7435             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7436               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7437               .access = PL1_R, .type = ARM_CP_CONST,
7438               .accessfn = access_aa32_tid3,
7439               .resetvalue = cpu->isar.id_mmfr4 },
7440             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7441               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7442               .access = PL1_R, .type = ARM_CP_CONST,
7443               .accessfn = access_aa32_tid3,
7444               .resetvalue = cpu->isar.id_isar6 },
7445         };
7446         define_arm_cp_regs(cpu, v6_idregs);
7447         define_arm_cp_regs(cpu, v6_cp_reginfo);
7448     } else {
7449         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7450     }
7451     if (arm_feature(env, ARM_FEATURE_V6K)) {
7452         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7453     }
7454     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7455         !arm_feature(env, ARM_FEATURE_PMSA)) {
7456         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7457     }
7458     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7459         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7460     }
7461     if (arm_feature(env, ARM_FEATURE_V7)) {
7462         ARMCPRegInfo clidr = {
7463             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7464             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7465             .access = PL1_R, .type = ARM_CP_CONST,
7466             .accessfn = access_aa64_tid2,
7467             .resetvalue = cpu->clidr
7468         };
7469         define_one_arm_cp_reg(cpu, &clidr);
7470         define_arm_cp_regs(cpu, v7_cp_reginfo);
7471         define_debug_regs(cpu);
7472         define_pmu_regs(cpu);
7473     } else {
7474         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7475     }
7476     if (arm_feature(env, ARM_FEATURE_V8)) {
7477         /*
7478          * v8 ID registers, which all have impdef reset values.
7479          * Note that within the ID register ranges the unused slots
7480          * must all RAZ, not UNDEF; future architecture versions may
7481          * define new registers here.
7482          * ID registers which are AArch64 views of the AArch32 ID registers
7483          * which already existed in v6 and v7 are handled elsewhere,
7484          * in v6_idregs[].
7485          */
7486         int i;
7487         ARMCPRegInfo v8_idregs[] = {
7488             /*
7489              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7490              * emulation because we don't know the right value for the
7491              * GIC field until after we define these regs.
7492              */
7493             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7494               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7495               .access = PL1_R,
7496 #ifdef CONFIG_USER_ONLY
7497               .type = ARM_CP_CONST,
7498               .resetvalue = cpu->isar.id_aa64pfr0
7499 #else
7500               .type = ARM_CP_NO_RAW,
7501               .accessfn = access_aa64_tid3,
7502               .readfn = id_aa64pfr0_read,
7503               .writefn = arm_cp_write_ignore
7504 #endif
7505             },
7506             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7507               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7508               .access = PL1_R, .type = ARM_CP_CONST,
7509               .accessfn = access_aa64_tid3,
7510               .resetvalue = cpu->isar.id_aa64pfr1},
7511             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7512               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7513               .access = PL1_R, .type = ARM_CP_CONST,
7514               .accessfn = access_aa64_tid3,
7515               .resetvalue = 0 },
7516             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7517               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7518               .access = PL1_R, .type = ARM_CP_CONST,
7519               .accessfn = access_aa64_tid3,
7520               .resetvalue = 0 },
7521             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7522               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7523               .access = PL1_R, .type = ARM_CP_CONST,
7524               .accessfn = access_aa64_tid3,
7525               .resetvalue = cpu->isar.id_aa64zfr0 },
7526             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7527               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7528               .access = PL1_R, .type = ARM_CP_CONST,
7529               .accessfn = access_aa64_tid3,
7530               .resetvalue = cpu->isar.id_aa64smfr0 },
7531             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7532               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7533               .access = PL1_R, .type = ARM_CP_CONST,
7534               .accessfn = access_aa64_tid3,
7535               .resetvalue = 0 },
7536             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7537               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7538               .access = PL1_R, .type = ARM_CP_CONST,
7539               .accessfn = access_aa64_tid3,
7540               .resetvalue = 0 },
7541             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7542               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7543               .access = PL1_R, .type = ARM_CP_CONST,
7544               .accessfn = access_aa64_tid3,
7545               .resetvalue = cpu->isar.id_aa64dfr0 },
7546             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7547               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7548               .access = PL1_R, .type = ARM_CP_CONST,
7549               .accessfn = access_aa64_tid3,
7550               .resetvalue = cpu->isar.id_aa64dfr1 },
7551             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7552               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7553               .access = PL1_R, .type = ARM_CP_CONST,
7554               .accessfn = access_aa64_tid3,
7555               .resetvalue = 0 },
7556             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7557               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7558               .access = PL1_R, .type = ARM_CP_CONST,
7559               .accessfn = access_aa64_tid3,
7560               .resetvalue = 0 },
7561             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7562               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7563               .access = PL1_R, .type = ARM_CP_CONST,
7564               .accessfn = access_aa64_tid3,
7565               .resetvalue = cpu->id_aa64afr0 },
7566             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7567               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7568               .access = PL1_R, .type = ARM_CP_CONST,
7569               .accessfn = access_aa64_tid3,
7570               .resetvalue = cpu->id_aa64afr1 },
7571             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7572               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7573               .access = PL1_R, .type = ARM_CP_CONST,
7574               .accessfn = access_aa64_tid3,
7575               .resetvalue = 0 },
7576             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7577               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7578               .access = PL1_R, .type = ARM_CP_CONST,
7579               .accessfn = access_aa64_tid3,
7580               .resetvalue = 0 },
7581             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7582               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7583               .access = PL1_R, .type = ARM_CP_CONST,
7584               .accessfn = access_aa64_tid3,
7585               .resetvalue = cpu->isar.id_aa64isar0 },
7586             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7587               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7588               .access = PL1_R, .type = ARM_CP_CONST,
7589               .accessfn = access_aa64_tid3,
7590               .resetvalue = cpu->isar.id_aa64isar1 },
7591             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7592               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7593               .access = PL1_R, .type = ARM_CP_CONST,
7594               .accessfn = access_aa64_tid3,
7595               .resetvalue = 0 },
7596             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7597               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7598               .access = PL1_R, .type = ARM_CP_CONST,
7599               .accessfn = access_aa64_tid3,
7600               .resetvalue = 0 },
7601             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7602               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7603               .access = PL1_R, .type = ARM_CP_CONST,
7604               .accessfn = access_aa64_tid3,
7605               .resetvalue = 0 },
7606             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7607               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7608               .access = PL1_R, .type = ARM_CP_CONST,
7609               .accessfn = access_aa64_tid3,
7610               .resetvalue = 0 },
7611             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7612               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7613               .access = PL1_R, .type = ARM_CP_CONST,
7614               .accessfn = access_aa64_tid3,
7615               .resetvalue = 0 },
7616             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7617               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7618               .access = PL1_R, .type = ARM_CP_CONST,
7619               .accessfn = access_aa64_tid3,
7620               .resetvalue = 0 },
7621             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7622               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7623               .access = PL1_R, .type = ARM_CP_CONST,
7624               .accessfn = access_aa64_tid3,
7625               .resetvalue = cpu->isar.id_aa64mmfr0 },
7626             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7627               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7628               .access = PL1_R, .type = ARM_CP_CONST,
7629               .accessfn = access_aa64_tid3,
7630               .resetvalue = cpu->isar.id_aa64mmfr1 },
7631             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7632               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7633               .access = PL1_R, .type = ARM_CP_CONST,
7634               .accessfn = access_aa64_tid3,
7635               .resetvalue = cpu->isar.id_aa64mmfr2 },
7636             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7637               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7638               .access = PL1_R, .type = ARM_CP_CONST,
7639               .accessfn = access_aa64_tid3,
7640               .resetvalue = 0 },
7641             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7642               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7643               .access = PL1_R, .type = ARM_CP_CONST,
7644               .accessfn = access_aa64_tid3,
7645               .resetvalue = 0 },
7646             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7647               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7648               .access = PL1_R, .type = ARM_CP_CONST,
7649               .accessfn = access_aa64_tid3,
7650               .resetvalue = 0 },
7651             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7652               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7653               .access = PL1_R, .type = ARM_CP_CONST,
7654               .accessfn = access_aa64_tid3,
7655               .resetvalue = 0 },
7656             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7657               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7658               .access = PL1_R, .type = ARM_CP_CONST,
7659               .accessfn = access_aa64_tid3,
7660               .resetvalue = 0 },
7661             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7662               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7663               .access = PL1_R, .type = ARM_CP_CONST,
7664               .accessfn = access_aa64_tid3,
7665               .resetvalue = cpu->isar.mvfr0 },
7666             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7667               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7668               .access = PL1_R, .type = ARM_CP_CONST,
7669               .accessfn = access_aa64_tid3,
7670               .resetvalue = cpu->isar.mvfr1 },
7671             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7672               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7673               .access = PL1_R, .type = ARM_CP_CONST,
7674               .accessfn = access_aa64_tid3,
7675               .resetvalue = cpu->isar.mvfr2 },
7676             /*
7677              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7678              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7679              * as RAZ, since it is in the "reserved for future ID
7680              * registers, RAZ" part of the AArch32 encoding space.
7681              */
7682             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7683               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7684               .access = PL1_R, .type = ARM_CP_CONST,
7685               .accessfn = access_aa64_tid3,
7686               .resetvalue = 0 },
7687             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7688               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7689               .access = PL1_R, .type = ARM_CP_CONST,
7690               .accessfn = access_aa64_tid3,
7691               .resetvalue = 0 },
7692             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7693               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7694               .access = PL1_R, .type = ARM_CP_CONST,
7695               .accessfn = access_aa64_tid3,
7696               .resetvalue = 0 },
7697             /*
7698              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7699              * they're also RAZ for AArch64, and in v8 are gradually
7700              * being filled with AArch64-view-of-AArch32-ID-register
7701              * for new ID registers.
7702              */
7703             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7704               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7705               .access = PL1_R, .type = ARM_CP_CONST,
7706               .accessfn = access_aa64_tid3,
7707               .resetvalue = 0 },
7708             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7709               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7710               .access = PL1_R, .type = ARM_CP_CONST,
7711               .accessfn = access_aa64_tid3,
7712               .resetvalue = cpu->isar.id_pfr2 },
7713             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7714               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7715               .access = PL1_R, .type = ARM_CP_CONST,
7716               .accessfn = access_aa64_tid3,
7717               .resetvalue = cpu->isar.id_dfr1 },
7718             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7719               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7720               .access = PL1_R, .type = ARM_CP_CONST,
7721               .accessfn = access_aa64_tid3,
7722               .resetvalue = cpu->isar.id_mmfr5 },
7723             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7724               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7725               .access = PL1_R, .type = ARM_CP_CONST,
7726               .accessfn = access_aa64_tid3,
7727               .resetvalue = 0 },
7728             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7729               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7730               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7731               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7732             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7733               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7734               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7735               .resetvalue = cpu->pmceid0 },
7736             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7737               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7738               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7739               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7740             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7741               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7742               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7743               .resetvalue = cpu->pmceid1 },
7744         };
7745 #ifdef CONFIG_USER_ONLY
7746         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7747             { .name = "ID_AA64PFR0_EL1",
7748               .exported_bits = 0x000f000f00ff0000,
7749               .fixed_bits    = 0x0000000000000011 },
7750             { .name = "ID_AA64PFR1_EL1",
7751               .exported_bits = 0x00000000000000f0 },
7752             { .name = "ID_AA64PFR*_EL1_RESERVED",
7753               .is_glob = true                     },
7754             { .name = "ID_AA64ZFR0_EL1"           },
7755             { .name = "ID_AA64MMFR0_EL1",
7756               .fixed_bits    = 0x00000000ff000000 },
7757             { .name = "ID_AA64MMFR1_EL1"          },
7758             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7759               .is_glob = true                     },
7760             { .name = "ID_AA64DFR0_EL1",
7761               .fixed_bits    = 0x0000000000000006 },
7762             { .name = "ID_AA64DFR1_EL1"           },
7763             { .name = "ID_AA64DFR*_EL1_RESERVED",
7764               .is_glob = true                     },
7765             { .name = "ID_AA64AFR*",
7766               .is_glob = true                     },
7767             { .name = "ID_AA64ISAR0_EL1",
7768               .exported_bits = 0x00fffffff0fffff0 },
7769             { .name = "ID_AA64ISAR1_EL1",
7770               .exported_bits = 0x000000f0ffffffff },
7771             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7772               .is_glob = true                     },
7773         };
7774         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7775 #endif
7776         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7777         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7778             !arm_feature(env, ARM_FEATURE_EL2)) {
7779             ARMCPRegInfo rvbar = {
7780                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7781                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7782                 .access = PL1_R,
7783                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7784             };
7785             define_one_arm_cp_reg(cpu, &rvbar);
7786         }
7787         define_arm_cp_regs(cpu, v8_idregs);
7788         define_arm_cp_regs(cpu, v8_cp_reginfo);
7789 
7790         for (i = 4; i < 16; i++) {
7791             /*
7792              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7793              * For pre-v8 cores there are RAZ patterns for these in
7794              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7795              * v8 extends the "must RAZ" part of the ID register space
7796              * to also cover c0, 0, c{8-15}, {0-7}.
7797              * These are STATE_AA32 because in the AArch64 sysreg space
7798              * c4-c7 is where the AArch64 ID registers live (and we've
7799              * already defined those in v8_idregs[]), and c8-c15 are not
7800              * "must RAZ" for AArch64.
7801              */
7802             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7803             ARMCPRegInfo v8_aa32_raz_idregs = {
7804                 .name = name,
7805                 .state = ARM_CP_STATE_AA32,
7806                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7807                 .access = PL1_R, .type = ARM_CP_CONST,
7808                 .accessfn = access_aa64_tid3,
7809                 .resetvalue = 0 };
7810             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7811         }
7812     }
7813 
7814     /*
7815      * Register the base EL2 cpregs.
7816      * Pre v8, these registers are implemented only as part of the
7817      * Virtualization Extensions (EL2 present).  Beginning with v8,
7818      * if EL2 is missing but EL3 is enabled, mostly these become
7819      * RES0 from EL3, with some specific exceptions.
7820      */
7821     if (arm_feature(env, ARM_FEATURE_EL2)
7822         || (arm_feature(env, ARM_FEATURE_EL3)
7823             && arm_feature(env, ARM_FEATURE_V8))) {
7824         uint64_t vmpidr_def = mpidr_read_val(env);
7825         ARMCPRegInfo vpidr_regs[] = {
7826             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7827               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7828               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7829               .resetvalue = cpu->midr,
7830               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7831               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7832             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7833               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7834               .access = PL2_RW, .resetvalue = cpu->midr,
7835               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7836               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7837             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7838               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7839               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7840               .resetvalue = vmpidr_def,
7841               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7842               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7843             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7844               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7845               .access = PL2_RW, .resetvalue = vmpidr_def,
7846               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7847               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7848         };
7849         /*
7850          * The only field of MDCR_EL2 that has a defined architectural reset
7851          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7852          */
7853         ARMCPRegInfo mdcr_el2 = {
7854             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
7855             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7856             .writefn = mdcr_el2_write,
7857             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7858             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7859         };
7860         define_one_arm_cp_reg(cpu, &mdcr_el2);
7861         define_arm_cp_regs(cpu, vpidr_regs);
7862         define_arm_cp_regs(cpu, el2_cp_reginfo);
7863         if (arm_feature(env, ARM_FEATURE_V8)) {
7864             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7865         }
7866         if (cpu_isar_feature(aa64_sel2, cpu)) {
7867             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7868         }
7869         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7870         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7871             ARMCPRegInfo rvbar = {
7872                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7873                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7874                 .access = PL2_R,
7875                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7876             };
7877             define_one_arm_cp_reg(cpu, &rvbar);
7878         }
7879     }
7880 
7881     /* Register the base EL3 cpregs. */
7882     if (arm_feature(env, ARM_FEATURE_EL3)) {
7883         define_arm_cp_regs(cpu, el3_cp_reginfo);
7884         ARMCPRegInfo el3_regs[] = {
7885             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7886               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7887               .access = PL3_R,
7888               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7889             },
7890             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7891               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7892               .access = PL3_RW,
7893               .raw_writefn = raw_write, .writefn = sctlr_write,
7894               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7895               .resetvalue = cpu->reset_sctlr },
7896         };
7897 
7898         define_arm_cp_regs(cpu, el3_regs);
7899     }
7900     /* The behaviour of NSACR is sufficiently various that we don't
7901      * try to describe it in a single reginfo:
7902      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7903      *     reads as constant 0xc00 from NS EL1 and NS EL2
7904      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7905      *  if v7 without EL3, register doesn't exist
7906      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7907      */
7908     if (arm_feature(env, ARM_FEATURE_EL3)) {
7909         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7910             static const ARMCPRegInfo nsacr = {
7911                 .name = "NSACR", .type = ARM_CP_CONST,
7912                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7913                 .access = PL1_RW, .accessfn = nsacr_access,
7914                 .resetvalue = 0xc00
7915             };
7916             define_one_arm_cp_reg(cpu, &nsacr);
7917         } else {
7918             static const ARMCPRegInfo nsacr = {
7919                 .name = "NSACR",
7920                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7921                 .access = PL3_RW | PL1_R,
7922                 .resetvalue = 0,
7923                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7924             };
7925             define_one_arm_cp_reg(cpu, &nsacr);
7926         }
7927     } else {
7928         if (arm_feature(env, ARM_FEATURE_V8)) {
7929             static const ARMCPRegInfo nsacr = {
7930                 .name = "NSACR", .type = ARM_CP_CONST,
7931                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7932                 .access = PL1_R,
7933                 .resetvalue = 0xc00
7934             };
7935             define_one_arm_cp_reg(cpu, &nsacr);
7936         }
7937     }
7938 
7939     if (arm_feature(env, ARM_FEATURE_PMSA)) {
7940         if (arm_feature(env, ARM_FEATURE_V6)) {
7941             /* PMSAv6 not implemented */
7942             assert(arm_feature(env, ARM_FEATURE_V7));
7943             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7944             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7945         } else {
7946             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7947         }
7948     } else {
7949         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7950         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7951         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
7952         if (cpu_isar_feature(aa32_hpd, cpu)) {
7953             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7954         }
7955     }
7956     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7957         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7958     }
7959     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7960         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7961     }
7962     if (arm_feature(env, ARM_FEATURE_VAPA)) {
7963         define_arm_cp_regs(cpu, vapa_cp_reginfo);
7964     }
7965     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7966         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7967     }
7968     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7969         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7970     }
7971     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7972         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7973     }
7974     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7975         define_arm_cp_regs(cpu, omap_cp_reginfo);
7976     }
7977     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7978         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7979     }
7980     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7981         define_arm_cp_regs(cpu, xscale_cp_reginfo);
7982     }
7983     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7984         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7985     }
7986     if (arm_feature(env, ARM_FEATURE_LPAE)) {
7987         define_arm_cp_regs(cpu, lpae_cp_reginfo);
7988     }
7989     if (cpu_isar_feature(aa32_jazelle, cpu)) {
7990         define_arm_cp_regs(cpu, jazelle_regs);
7991     }
7992     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7993      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7994      * be read-only (ie write causes UNDEF exception).
7995      */
7996     {
7997         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7998             /* Pre-v8 MIDR space.
7999              * Note that the MIDR isn't a simple constant register because
8000              * of the TI925 behaviour where writes to another register can
8001              * cause the MIDR value to change.
8002              *
8003              * Unimplemented registers in the c15 0 0 0 space default to
8004              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8005              * and friends override accordingly.
8006              */
8007             { .name = "MIDR",
8008               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8009               .access = PL1_R, .resetvalue = cpu->midr,
8010               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8011               .readfn = midr_read,
8012               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8013               .type = ARM_CP_OVERRIDE },
8014             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8015             { .name = "DUMMY",
8016               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8017               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8018             { .name = "DUMMY",
8019               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8020               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8021             { .name = "DUMMY",
8022               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8023               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8024             { .name = "DUMMY",
8025               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8026               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8027             { .name = "DUMMY",
8028               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8029               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8030         };
8031         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8032             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8033               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8034               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8035               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8036               .readfn = midr_read },
8037             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8038             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8039               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8040               .access = PL1_R, .resetvalue = cpu->midr },
8041             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8042               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8043               .access = PL1_R, .resetvalue = cpu->midr },
8044             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8045               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8046               .access = PL1_R,
8047               .accessfn = access_aa64_tid1,
8048               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8049         };
8050         ARMCPRegInfo id_cp_reginfo[] = {
8051             /* These are common to v8 and pre-v8 */
8052             { .name = "CTR",
8053               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8054               .access = PL1_R, .accessfn = ctr_el0_access,
8055               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8056             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8057               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8058               .access = PL0_R, .accessfn = ctr_el0_access,
8059               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8060             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8061             { .name = "TCMTR",
8062               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8063               .access = PL1_R,
8064               .accessfn = access_aa32_tid1,
8065               .type = ARM_CP_CONST, .resetvalue = 0 },
8066         };
8067         /* TLBTR is specific to VMSA */
8068         ARMCPRegInfo id_tlbtr_reginfo = {
8069               .name = "TLBTR",
8070               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8071               .access = PL1_R,
8072               .accessfn = access_aa32_tid1,
8073               .type = ARM_CP_CONST, .resetvalue = 0,
8074         };
8075         /* MPUIR is specific to PMSA V6+ */
8076         ARMCPRegInfo id_mpuir_reginfo = {
8077               .name = "MPUIR",
8078               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8079               .access = PL1_R, .type = ARM_CP_CONST,
8080               .resetvalue = cpu->pmsav7_dregion << 8
8081         };
8082         static const ARMCPRegInfo crn0_wi_reginfo = {
8083             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8084             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8085             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8086         };
8087 #ifdef CONFIG_USER_ONLY
8088         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8089             { .name = "MIDR_EL1",
8090               .exported_bits = 0x00000000ffffffff },
8091             { .name = "REVIDR_EL1"                },
8092         };
8093         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8094 #endif
8095         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8096             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8097             size_t i;
8098             /* Register the blanket "writes ignored" value first to cover the
8099              * whole space. Then update the specific ID registers to allow write
8100              * access, so that they ignore writes rather than causing them to
8101              * UNDEF.
8102              */
8103             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8104             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8105                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8106             }
8107             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8108                 id_cp_reginfo[i].access = PL1_RW;
8109             }
8110             id_mpuir_reginfo.access = PL1_RW;
8111             id_tlbtr_reginfo.access = PL1_RW;
8112         }
8113         if (arm_feature(env, ARM_FEATURE_V8)) {
8114             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8115         } else {
8116             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8117         }
8118         define_arm_cp_regs(cpu, id_cp_reginfo);
8119         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8120             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8121         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8122             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8123         }
8124     }
8125 
8126     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8127         ARMCPRegInfo mpidr_cp_reginfo[] = {
8128             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8129               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8130               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8131         };
8132 #ifdef CONFIG_USER_ONLY
8133         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8134             { .name = "MPIDR_EL1",
8135               .fixed_bits = 0x0000000080000000 },
8136         };
8137         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8138 #endif
8139         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8140     }
8141 
8142     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8143         ARMCPRegInfo auxcr_reginfo[] = {
8144             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8145               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8146               .access = PL1_RW, .accessfn = access_tacr,
8147               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8148             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8149               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8150               .access = PL2_RW, .type = ARM_CP_CONST,
8151               .resetvalue = 0 },
8152             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8153               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8154               .access = PL3_RW, .type = ARM_CP_CONST,
8155               .resetvalue = 0 },
8156         };
8157         define_arm_cp_regs(cpu, auxcr_reginfo);
8158         if (cpu_isar_feature(aa32_ac2, cpu)) {
8159             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8160         }
8161     }
8162 
8163     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8164         /*
8165          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8166          * There are two flavours:
8167          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8168          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8169          *      32-bit register visible to AArch32 at a different encoding
8170          *      to the "flavour 1" register and with the bits rearranged to
8171          *      be able to squash a 64-bit address into the 32-bit view.
8172          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8173          * in future if we support AArch32-only configs of some of the
8174          * AArch64 cores we might need to add a specific feature flag
8175          * to indicate cores with "flavour 2" CBAR.
8176          */
8177         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8178             /* 32 bit view is [31:18] 0...0 [43:32]. */
8179             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8180                 | extract64(cpu->reset_cbar, 32, 12);
8181             ARMCPRegInfo cbar_reginfo[] = {
8182                 { .name = "CBAR",
8183                   .type = ARM_CP_CONST,
8184                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8185                   .access = PL1_R, .resetvalue = cbar32 },
8186                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8187                   .type = ARM_CP_CONST,
8188                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8189                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8190             };
8191             /* We don't implement a r/w 64 bit CBAR currently */
8192             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8193             define_arm_cp_regs(cpu, cbar_reginfo);
8194         } else {
8195             ARMCPRegInfo cbar = {
8196                 .name = "CBAR",
8197                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8198                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8199                 .fieldoffset = offsetof(CPUARMState,
8200                                         cp15.c15_config_base_address)
8201             };
8202             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8203                 cbar.access = PL1_R;
8204                 cbar.fieldoffset = 0;
8205                 cbar.type = ARM_CP_CONST;
8206             }
8207             define_one_arm_cp_reg(cpu, &cbar);
8208         }
8209     }
8210 
8211     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8212         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8213             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8214               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8215               .access = PL1_RW, .writefn = vbar_write,
8216               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8217                                      offsetof(CPUARMState, cp15.vbar_ns) },
8218               .resetvalue = 0 },
8219         };
8220         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8221     }
8222 
8223     /* Generic registers whose values depend on the implementation */
8224     {
8225         ARMCPRegInfo sctlr = {
8226             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8227             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8228             .access = PL1_RW, .accessfn = access_tvm_trvm,
8229             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8230                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8231             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8232             .raw_writefn = raw_write,
8233         };
8234         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8235             /* Normally we would always end the TB on an SCTLR write, but Linux
8236              * arch/arm/mach-pxa/sleep.S expects two instructions following
8237              * an MMU enable to execute from cache.  Imitate this behaviour.
8238              */
8239             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8240         }
8241         define_one_arm_cp_reg(cpu, &sctlr);
8242     }
8243 
8244     if (cpu_isar_feature(aa64_lor, cpu)) {
8245         define_arm_cp_regs(cpu, lor_reginfo);
8246     }
8247     if (cpu_isar_feature(aa64_pan, cpu)) {
8248         define_one_arm_cp_reg(cpu, &pan_reginfo);
8249     }
8250 #ifndef CONFIG_USER_ONLY
8251     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8252         define_arm_cp_regs(cpu, ats1e1_reginfo);
8253     }
8254     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8255         define_arm_cp_regs(cpu, ats1cp_reginfo);
8256     }
8257 #endif
8258     if (cpu_isar_feature(aa64_uao, cpu)) {
8259         define_one_arm_cp_reg(cpu, &uao_reginfo);
8260     }
8261 
8262     if (cpu_isar_feature(aa64_dit, cpu)) {
8263         define_one_arm_cp_reg(cpu, &dit_reginfo);
8264     }
8265     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8266         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8267     }
8268     if (cpu_isar_feature(any_ras, cpu)) {
8269         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8270     }
8271 
8272     if (cpu_isar_feature(aa64_vh, cpu) ||
8273         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8274         define_one_arm_cp_reg(cpu, &contextidr_el2);
8275     }
8276     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8277         define_arm_cp_regs(cpu, vhe_reginfo);
8278     }
8279 
8280     if (cpu_isar_feature(aa64_sve, cpu)) {
8281         define_arm_cp_regs(cpu, zcr_reginfo);
8282     }
8283 
8284     if (cpu_isar_feature(aa64_hcx, cpu)) {
8285         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8286     }
8287 
8288 #ifdef TARGET_AARCH64
8289     if (cpu_isar_feature(aa64_sme, cpu)) {
8290         define_arm_cp_regs(cpu, sme_reginfo);
8291     }
8292     if (cpu_isar_feature(aa64_pauth, cpu)) {
8293         define_arm_cp_regs(cpu, pauth_reginfo);
8294     }
8295     if (cpu_isar_feature(aa64_rndr, cpu)) {
8296         define_arm_cp_regs(cpu, rndr_reginfo);
8297     }
8298     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8299         define_arm_cp_regs(cpu, tlbirange_reginfo);
8300     }
8301     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8302         define_arm_cp_regs(cpu, tlbios_reginfo);
8303     }
8304 #ifndef CONFIG_USER_ONLY
8305     /* Data Cache clean instructions up to PoP */
8306     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8307         define_one_arm_cp_reg(cpu, dcpop_reg);
8308 
8309         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8310             define_one_arm_cp_reg(cpu, dcpodp_reg);
8311         }
8312     }
8313 #endif /*CONFIG_USER_ONLY*/
8314 
8315     /*
8316      * If full MTE is enabled, add all of the system registers.
8317      * If only "instructions available at EL0" are enabled,
8318      * then define only a RAZ/WI version of PSTATE.TCO.
8319      */
8320     if (cpu_isar_feature(aa64_mte, cpu)) {
8321         define_arm_cp_regs(cpu, mte_reginfo);
8322         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8323     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8324         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8325         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8326     }
8327 
8328     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8329         define_arm_cp_regs(cpu, scxtnum_reginfo);
8330     }
8331 #endif
8332 
8333     if (cpu_isar_feature(any_predinv, cpu)) {
8334         define_arm_cp_regs(cpu, predinv_reginfo);
8335     }
8336 
8337     if (cpu_isar_feature(any_ccidx, cpu)) {
8338         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8339     }
8340 
8341 #ifndef CONFIG_USER_ONLY
8342     /*
8343      * Register redirections and aliases must be done last,
8344      * after the registers from the other extensions have been defined.
8345      */
8346     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8347         define_arm_vh_e2h_redirects_aliases(cpu);
8348     }
8349 #endif
8350 }
8351 
8352 /* Sort alphabetically by type name, except for "any". */
8353 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8354 {
8355     ObjectClass *class_a = (ObjectClass *)a;
8356     ObjectClass *class_b = (ObjectClass *)b;
8357     const char *name_a, *name_b;
8358 
8359     name_a = object_class_get_name(class_a);
8360     name_b = object_class_get_name(class_b);
8361     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8362         return 1;
8363     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8364         return -1;
8365     } else {
8366         return strcmp(name_a, name_b);
8367     }
8368 }
8369 
8370 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8371 {
8372     ObjectClass *oc = data;
8373     CPUClass *cc = CPU_CLASS(oc);
8374     const char *typename;
8375     char *name;
8376 
8377     typename = object_class_get_name(oc);
8378     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8379     if (cc->deprecation_note) {
8380         qemu_printf("  %s (deprecated)\n", name);
8381     } else {
8382         qemu_printf("  %s\n", name);
8383     }
8384     g_free(name);
8385 }
8386 
8387 void arm_cpu_list(void)
8388 {
8389     GSList *list;
8390 
8391     list = object_class_get_list(TYPE_ARM_CPU, false);
8392     list = g_slist_sort(list, arm_cpu_list_compare);
8393     qemu_printf("Available CPUs:\n");
8394     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8395     g_slist_free(list);
8396 }
8397 
8398 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8399 {
8400     ObjectClass *oc = data;
8401     CpuDefinitionInfoList **cpu_list = user_data;
8402     CpuDefinitionInfo *info;
8403     const char *typename;
8404 
8405     typename = object_class_get_name(oc);
8406     info = g_malloc0(sizeof(*info));
8407     info->name = g_strndup(typename,
8408                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8409     info->q_typename = g_strdup(typename);
8410 
8411     QAPI_LIST_PREPEND(*cpu_list, info);
8412 }
8413 
8414 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8415 {
8416     CpuDefinitionInfoList *cpu_list = NULL;
8417     GSList *list;
8418 
8419     list = object_class_get_list(TYPE_ARM_CPU, false);
8420     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8421     g_slist_free(list);
8422 
8423     return cpu_list;
8424 }
8425 
8426 /*
8427  * Private utility function for define_one_arm_cp_reg_with_opaque():
8428  * add a single reginfo struct to the hash table.
8429  */
8430 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8431                                    void *opaque, CPState state,
8432                                    CPSecureState secstate,
8433                                    int crm, int opc1, int opc2,
8434                                    const char *name)
8435 {
8436     CPUARMState *env = &cpu->env;
8437     uint32_t key;
8438     ARMCPRegInfo *r2;
8439     bool is64 = r->type & ARM_CP_64BIT;
8440     bool ns = secstate & ARM_CP_SECSTATE_NS;
8441     int cp = r->cp;
8442     size_t name_len;
8443     bool make_const;
8444 
8445     switch (state) {
8446     case ARM_CP_STATE_AA32:
8447         /* We assume it is a cp15 register if the .cp field is left unset. */
8448         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8449             cp = 15;
8450         }
8451         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8452         break;
8453     case ARM_CP_STATE_AA64:
8454         /*
8455          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8456          * cp == 0 as equivalent to the value for "standard guest-visible
8457          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8458          * in their AArch64 view (the .cp value may be non-zero for the
8459          * benefit of the AArch32 view).
8460          */
8461         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8462             cp = CP_REG_ARM64_SYSREG_CP;
8463         }
8464         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8465         break;
8466     default:
8467         g_assert_not_reached();
8468     }
8469 
8470     /* Overriding of an existing definition must be explicitly requested. */
8471     if (!(r->type & ARM_CP_OVERRIDE)) {
8472         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8473         if (oldreg) {
8474             assert(oldreg->type & ARM_CP_OVERRIDE);
8475         }
8476     }
8477 
8478     /*
8479      * Eliminate registers that are not present because the EL is missing.
8480      * Doing this here makes it easier to put all registers for a given
8481      * feature into the same ARMCPRegInfo array and define them all at once.
8482      */
8483     make_const = false;
8484     if (arm_feature(env, ARM_FEATURE_EL3)) {
8485         /*
8486          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8487          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8488          */
8489         int min_el = ctz32(r->access) / 2;
8490         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8491             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8492                 return;
8493             }
8494             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8495         }
8496     } else {
8497         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8498                                  ? PL2_RW : PL1_RW);
8499         if ((r->access & max_el) == 0) {
8500             return;
8501         }
8502     }
8503 
8504     /* Combine cpreg and name into one allocation. */
8505     name_len = strlen(name) + 1;
8506     r2 = g_malloc(sizeof(*r2) + name_len);
8507     *r2 = *r;
8508     r2->name = memcpy(r2 + 1, name, name_len);
8509 
8510     /*
8511      * Update fields to match the instantiation, overwiting wildcards
8512      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8513      */
8514     r2->cp = cp;
8515     r2->crm = crm;
8516     r2->opc1 = opc1;
8517     r2->opc2 = opc2;
8518     r2->state = state;
8519     r2->secure = secstate;
8520     if (opaque) {
8521         r2->opaque = opaque;
8522     }
8523 
8524     if (make_const) {
8525         /* This should not have been a very special register to begin. */
8526         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8527         assert(old_special == 0 || old_special == ARM_CP_NOP);
8528         /*
8529          * Set the special function to CONST, retaining the other flags.
8530          * This is important for e.g. ARM_CP_SVE so that we still
8531          * take the SVE trap if CPTR_EL3.EZ == 0.
8532          */
8533         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8534         /*
8535          * Usually, these registers become RES0, but there are a few
8536          * special cases like VPIDR_EL2 which have a constant non-zero
8537          * value with writes ignored.
8538          */
8539         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8540             r2->resetvalue = 0;
8541         }
8542         /*
8543          * ARM_CP_CONST has precedence, so removing the callbacks and
8544          * offsets are not strictly necessary, but it is potentially
8545          * less confusing to debug later.
8546          */
8547         r2->readfn = NULL;
8548         r2->writefn = NULL;
8549         r2->raw_readfn = NULL;
8550         r2->raw_writefn = NULL;
8551         r2->resetfn = NULL;
8552         r2->fieldoffset = 0;
8553         r2->bank_fieldoffsets[0] = 0;
8554         r2->bank_fieldoffsets[1] = 0;
8555     } else {
8556         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8557 
8558         if (isbanked) {
8559             /*
8560              * Register is banked (using both entries in array).
8561              * Overwriting fieldoffset as the array is only used to define
8562              * banked registers but later only fieldoffset is used.
8563              */
8564             r2->fieldoffset = r->bank_fieldoffsets[ns];
8565         }
8566         if (state == ARM_CP_STATE_AA32) {
8567             if (isbanked) {
8568                 /*
8569                  * If the register is banked then we don't need to migrate or
8570                  * reset the 32-bit instance in certain cases:
8571                  *
8572                  * 1) If the register has both 32-bit and 64-bit instances
8573                  *    then we can count on the 64-bit instance taking care
8574                  *    of the non-secure bank.
8575                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8576                  *    version taking care of the secure bank.  This requires
8577                  *    that separate 32 and 64-bit definitions are provided.
8578                  */
8579                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8580                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8581                     r2->type |= ARM_CP_ALIAS;
8582                 }
8583             } else if ((secstate != r->secure) && !ns) {
8584                 /*
8585                  * The register is not banked so we only want to allow
8586                  * migration of the non-secure instance.
8587                  */
8588                 r2->type |= ARM_CP_ALIAS;
8589             }
8590 
8591             if (HOST_BIG_ENDIAN &&
8592                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8593                 r2->fieldoffset += sizeof(uint32_t);
8594             }
8595         }
8596     }
8597 
8598     /*
8599      * By convention, for wildcarded registers only the first
8600      * entry is used for migration; the others are marked as
8601      * ALIAS so we don't try to transfer the register
8602      * multiple times. Special registers (ie NOP/WFI) are
8603      * never migratable and not even raw-accessible.
8604      */
8605     if (r2->type & ARM_CP_SPECIAL_MASK) {
8606         r2->type |= ARM_CP_NO_RAW;
8607     }
8608     if (((r->crm == CP_ANY) && crm != 0) ||
8609         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8610         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8611         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8612     }
8613 
8614     /*
8615      * Check that raw accesses are either forbidden or handled. Note that
8616      * we can't assert this earlier because the setup of fieldoffset for
8617      * banked registers has to be done first.
8618      */
8619     if (!(r2->type & ARM_CP_NO_RAW)) {
8620         assert(!raw_accessors_invalid(r2));
8621     }
8622 
8623     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8624 }
8625 
8626 
8627 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8628                                        const ARMCPRegInfo *r, void *opaque)
8629 {
8630     /* Define implementations of coprocessor registers.
8631      * We store these in a hashtable because typically
8632      * there are less than 150 registers in a space which
8633      * is 16*16*16*8*8 = 262144 in size.
8634      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8635      * If a register is defined twice then the second definition is
8636      * used, so this can be used to define some generic registers and
8637      * then override them with implementation specific variations.
8638      * At least one of the original and the second definition should
8639      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8640      * against accidental use.
8641      *
8642      * The state field defines whether the register is to be
8643      * visible in the AArch32 or AArch64 execution state. If the
8644      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8645      * reginfo structure for the AArch32 view, which sees the lower
8646      * 32 bits of the 64 bit register.
8647      *
8648      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8649      * be wildcarded. AArch64 registers are always considered to be 64
8650      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8651      * the register, if any.
8652      */
8653     int crm, opc1, opc2;
8654     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8655     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8656     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8657     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8658     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8659     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8660     CPState state;
8661 
8662     /* 64 bit registers have only CRm and Opc1 fields */
8663     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8664     /* op0 only exists in the AArch64 encodings */
8665     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8666     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8667     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8668     /*
8669      * This API is only for Arm's system coprocessors (14 and 15) or
8670      * (M-profile or v7A-and-earlier only) for implementation defined
8671      * coprocessors in the range 0..7.  Our decode assumes this, since
8672      * 8..13 can be used for other insns including VFP and Neon. See
8673      * valid_cp() in translate.c.  Assert here that we haven't tried
8674      * to use an invalid coprocessor number.
8675      */
8676     switch (r->state) {
8677     case ARM_CP_STATE_BOTH:
8678         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8679         if (r->cp == 0) {
8680             break;
8681         }
8682         /* fall through */
8683     case ARM_CP_STATE_AA32:
8684         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8685             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8686             assert(r->cp >= 14 && r->cp <= 15);
8687         } else {
8688             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8689         }
8690         break;
8691     case ARM_CP_STATE_AA64:
8692         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8693         break;
8694     default:
8695         g_assert_not_reached();
8696     }
8697     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8698      * encodes a minimum access level for the register. We roll this
8699      * runtime check into our general permission check code, so check
8700      * here that the reginfo's specified permissions are strict enough
8701      * to encompass the generic architectural permission check.
8702      */
8703     if (r->state != ARM_CP_STATE_AA32) {
8704         CPAccessRights mask;
8705         switch (r->opc1) {
8706         case 0:
8707             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8708             mask = PL0U_R | PL1_RW;
8709             break;
8710         case 1: case 2:
8711             /* min_EL EL1 */
8712             mask = PL1_RW;
8713             break;
8714         case 3:
8715             /* min_EL EL0 */
8716             mask = PL0_RW;
8717             break;
8718         case 4:
8719         case 5:
8720             /* min_EL EL2 */
8721             mask = PL2_RW;
8722             break;
8723         case 6:
8724             /* min_EL EL3 */
8725             mask = PL3_RW;
8726             break;
8727         case 7:
8728             /* min_EL EL1, secure mode only (we don't check the latter) */
8729             mask = PL1_RW;
8730             break;
8731         default:
8732             /* broken reginfo with out-of-range opc1 */
8733             g_assert_not_reached();
8734         }
8735         /* assert our permissions are not too lax (stricter is fine) */
8736         assert((r->access & ~mask) == 0);
8737     }
8738 
8739     /* Check that the register definition has enough info to handle
8740      * reads and writes if they are permitted.
8741      */
8742     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8743         if (r->access & PL3_R) {
8744             assert((r->fieldoffset ||
8745                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8746                    r->readfn);
8747         }
8748         if (r->access & PL3_W) {
8749             assert((r->fieldoffset ||
8750                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8751                    r->writefn);
8752         }
8753     }
8754 
8755     for (crm = crmmin; crm <= crmmax; crm++) {
8756         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8757             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8758                 for (state = ARM_CP_STATE_AA32;
8759                      state <= ARM_CP_STATE_AA64; state++) {
8760                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8761                         continue;
8762                     }
8763                     if (state == ARM_CP_STATE_AA32) {
8764                         /* Under AArch32 CP registers can be common
8765                          * (same for secure and non-secure world) or banked.
8766                          */
8767                         char *name;
8768 
8769                         switch (r->secure) {
8770                         case ARM_CP_SECSTATE_S:
8771                         case ARM_CP_SECSTATE_NS:
8772                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8773                                                    r->secure, crm, opc1, opc2,
8774                                                    r->name);
8775                             break;
8776                         case ARM_CP_SECSTATE_BOTH:
8777                             name = g_strdup_printf("%s_S", r->name);
8778                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8779                                                    ARM_CP_SECSTATE_S,
8780                                                    crm, opc1, opc2, name);
8781                             g_free(name);
8782                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8783                                                    ARM_CP_SECSTATE_NS,
8784                                                    crm, opc1, opc2, r->name);
8785                             break;
8786                         default:
8787                             g_assert_not_reached();
8788                         }
8789                     } else {
8790                         /* AArch64 registers get mapped to non-secure instance
8791                          * of AArch32 */
8792                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8793                                                ARM_CP_SECSTATE_NS,
8794                                                crm, opc1, opc2, r->name);
8795                     }
8796                 }
8797             }
8798         }
8799     }
8800 }
8801 
8802 /* Define a whole list of registers */
8803 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8804                                         void *opaque, size_t len)
8805 {
8806     size_t i;
8807     for (i = 0; i < len; ++i) {
8808         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8809     }
8810 }
8811 
8812 /*
8813  * Modify ARMCPRegInfo for access from userspace.
8814  *
8815  * This is a data driven modification directed by
8816  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8817  * user-space cannot alter any values and dynamic values pertaining to
8818  * execution state are hidden from user space view anyway.
8819  */
8820 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8821                                  const ARMCPRegUserSpaceInfo *mods,
8822                                  size_t mods_len)
8823 {
8824     for (size_t mi = 0; mi < mods_len; ++mi) {
8825         const ARMCPRegUserSpaceInfo *m = mods + mi;
8826         GPatternSpec *pat = NULL;
8827 
8828         if (m->is_glob) {
8829             pat = g_pattern_spec_new(m->name);
8830         }
8831         for (size_t ri = 0; ri < regs_len; ++ri) {
8832             ARMCPRegInfo *r = regs + ri;
8833 
8834             if (pat && g_pattern_match_string(pat, r->name)) {
8835                 r->type = ARM_CP_CONST;
8836                 r->access = PL0U_R;
8837                 r->resetvalue = 0;
8838                 /* continue */
8839             } else if (strcmp(r->name, m->name) == 0) {
8840                 r->type = ARM_CP_CONST;
8841                 r->access = PL0U_R;
8842                 r->resetvalue &= m->exported_bits;
8843                 r->resetvalue |= m->fixed_bits;
8844                 break;
8845             }
8846         }
8847         if (pat) {
8848             g_pattern_spec_free(pat);
8849         }
8850     }
8851 }
8852 
8853 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8854 {
8855     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8856 }
8857 
8858 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8859                          uint64_t value)
8860 {
8861     /* Helper coprocessor write function for write-ignore registers */
8862 }
8863 
8864 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8865 {
8866     /* Helper coprocessor write function for read-as-zero registers */
8867     return 0;
8868 }
8869 
8870 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8871 {
8872     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8873 }
8874 
8875 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8876 {
8877     /* Return true if it is not valid for us to switch to
8878      * this CPU mode (ie all the UNPREDICTABLE cases in
8879      * the ARM ARM CPSRWriteByInstr pseudocode).
8880      */
8881 
8882     /* Changes to or from Hyp via MSR and CPS are illegal. */
8883     if (write_type == CPSRWriteByInstr &&
8884         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8885          mode == ARM_CPU_MODE_HYP)) {
8886         return 1;
8887     }
8888 
8889     switch (mode) {
8890     case ARM_CPU_MODE_USR:
8891         return 0;
8892     case ARM_CPU_MODE_SYS:
8893     case ARM_CPU_MODE_SVC:
8894     case ARM_CPU_MODE_ABT:
8895     case ARM_CPU_MODE_UND:
8896     case ARM_CPU_MODE_IRQ:
8897     case ARM_CPU_MODE_FIQ:
8898         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8899          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8900          */
8901         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8902          * and CPS are treated as illegal mode changes.
8903          */
8904         if (write_type == CPSRWriteByInstr &&
8905             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8906             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8907             return 1;
8908         }
8909         return 0;
8910     case ARM_CPU_MODE_HYP:
8911         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8912     case ARM_CPU_MODE_MON:
8913         return arm_current_el(env) < 3;
8914     default:
8915         return 1;
8916     }
8917 }
8918 
8919 uint32_t cpsr_read(CPUARMState *env)
8920 {
8921     int ZF;
8922     ZF = (env->ZF == 0);
8923     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8924         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8925         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8926         | ((env->condexec_bits & 0xfc) << 8)
8927         | (env->GE << 16) | (env->daif & CPSR_AIF);
8928 }
8929 
8930 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8931                 CPSRWriteType write_type)
8932 {
8933     uint32_t changed_daif;
8934     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8935         (mask & (CPSR_M | CPSR_E | CPSR_IL));
8936 
8937     if (mask & CPSR_NZCV) {
8938         env->ZF = (~val) & CPSR_Z;
8939         env->NF = val;
8940         env->CF = (val >> 29) & 1;
8941         env->VF = (val << 3) & 0x80000000;
8942     }
8943     if (mask & CPSR_Q)
8944         env->QF = ((val & CPSR_Q) != 0);
8945     if (mask & CPSR_T)
8946         env->thumb = ((val & CPSR_T) != 0);
8947     if (mask & CPSR_IT_0_1) {
8948         env->condexec_bits &= ~3;
8949         env->condexec_bits |= (val >> 25) & 3;
8950     }
8951     if (mask & CPSR_IT_2_7) {
8952         env->condexec_bits &= 3;
8953         env->condexec_bits |= (val >> 8) & 0xfc;
8954     }
8955     if (mask & CPSR_GE) {
8956         env->GE = (val >> 16) & 0xf;
8957     }
8958 
8959     /* In a V7 implementation that includes the security extensions but does
8960      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8961      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8962      * bits respectively.
8963      *
8964      * In a V8 implementation, it is permitted for privileged software to
8965      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8966      */
8967     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8968         arm_feature(env, ARM_FEATURE_EL3) &&
8969         !arm_feature(env, ARM_FEATURE_EL2) &&
8970         !arm_is_secure(env)) {
8971 
8972         changed_daif = (env->daif ^ val) & mask;
8973 
8974         if (changed_daif & CPSR_A) {
8975             /* Check to see if we are allowed to change the masking of async
8976              * abort exceptions from a non-secure state.
8977              */
8978             if (!(env->cp15.scr_el3 & SCR_AW)) {
8979                 qemu_log_mask(LOG_GUEST_ERROR,
8980                               "Ignoring attempt to switch CPSR_A flag from "
8981                               "non-secure world with SCR.AW bit clear\n");
8982                 mask &= ~CPSR_A;
8983             }
8984         }
8985 
8986         if (changed_daif & CPSR_F) {
8987             /* Check to see if we are allowed to change the masking of FIQ
8988              * exceptions from a non-secure state.
8989              */
8990             if (!(env->cp15.scr_el3 & SCR_FW)) {
8991                 qemu_log_mask(LOG_GUEST_ERROR,
8992                               "Ignoring attempt to switch CPSR_F flag from "
8993                               "non-secure world with SCR.FW bit clear\n");
8994                 mask &= ~CPSR_F;
8995             }
8996 
8997             /* Check whether non-maskable FIQ (NMFI) support is enabled.
8998              * If this bit is set software is not allowed to mask
8999              * FIQs, but is allowed to set CPSR_F to 0.
9000              */
9001             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9002                 (val & CPSR_F)) {
9003                 qemu_log_mask(LOG_GUEST_ERROR,
9004                               "Ignoring attempt to enable CPSR_F flag "
9005                               "(non-maskable FIQ [NMFI] support enabled)\n");
9006                 mask &= ~CPSR_F;
9007             }
9008         }
9009     }
9010 
9011     env->daif &= ~(CPSR_AIF & mask);
9012     env->daif |= val & CPSR_AIF & mask;
9013 
9014     if (write_type != CPSRWriteRaw &&
9015         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9016         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9017             /* Note that we can only get here in USR mode if this is a
9018              * gdb stub write; for this case we follow the architectural
9019              * behaviour for guest writes in USR mode of ignoring an attempt
9020              * to switch mode. (Those are caught by translate.c for writes
9021              * triggered by guest instructions.)
9022              */
9023             mask &= ~CPSR_M;
9024         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9025             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9026              * v7, and has defined behaviour in v8:
9027              *  + leave CPSR.M untouched
9028              *  + allow changes to the other CPSR fields
9029              *  + set PSTATE.IL
9030              * For user changes via the GDB stub, we don't set PSTATE.IL,
9031              * as this would be unnecessarily harsh for a user error.
9032              */
9033             mask &= ~CPSR_M;
9034             if (write_type != CPSRWriteByGDBStub &&
9035                 arm_feature(env, ARM_FEATURE_V8)) {
9036                 mask |= CPSR_IL;
9037                 val |= CPSR_IL;
9038             }
9039             qemu_log_mask(LOG_GUEST_ERROR,
9040                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9041                           aarch32_mode_name(env->uncached_cpsr),
9042                           aarch32_mode_name(val));
9043         } else {
9044             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9045                           write_type == CPSRWriteExceptionReturn ?
9046                           "Exception return from AArch32" :
9047                           "AArch32 mode switch from",
9048                           aarch32_mode_name(env->uncached_cpsr),
9049                           aarch32_mode_name(val), env->regs[15]);
9050             switch_mode(env, val & CPSR_M);
9051         }
9052     }
9053     mask &= ~CACHED_CPSR_BITS;
9054     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9055     if (rebuild_hflags) {
9056         arm_rebuild_hflags(env);
9057     }
9058 }
9059 
9060 /* Sign/zero extend */
9061 uint32_t HELPER(sxtb16)(uint32_t x)
9062 {
9063     uint32_t res;
9064     res = (uint16_t)(int8_t)x;
9065     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9066     return res;
9067 }
9068 
9069 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9070 {
9071     /*
9072      * Take a division-by-zero exception if necessary; otherwise return
9073      * to get the usual non-trapping division behaviour (result of 0)
9074      */
9075     if (arm_feature(env, ARM_FEATURE_M)
9076         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9077         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9078     }
9079 }
9080 
9081 uint32_t HELPER(uxtb16)(uint32_t x)
9082 {
9083     uint32_t res;
9084     res = (uint16_t)(uint8_t)x;
9085     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9086     return res;
9087 }
9088 
9089 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9090 {
9091     if (den == 0) {
9092         handle_possible_div0_trap(env, GETPC());
9093         return 0;
9094     }
9095     if (num == INT_MIN && den == -1) {
9096         return INT_MIN;
9097     }
9098     return num / den;
9099 }
9100 
9101 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9102 {
9103     if (den == 0) {
9104         handle_possible_div0_trap(env, GETPC());
9105         return 0;
9106     }
9107     return num / den;
9108 }
9109 
9110 uint32_t HELPER(rbit)(uint32_t x)
9111 {
9112     return revbit32(x);
9113 }
9114 
9115 #ifdef CONFIG_USER_ONLY
9116 
9117 static void switch_mode(CPUARMState *env, int mode)
9118 {
9119     ARMCPU *cpu = env_archcpu(env);
9120 
9121     if (mode != ARM_CPU_MODE_USR) {
9122         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9123     }
9124 }
9125 
9126 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9127                                  uint32_t cur_el, bool secure)
9128 {
9129     return 1;
9130 }
9131 
9132 void aarch64_sync_64_to_32(CPUARMState *env)
9133 {
9134     g_assert_not_reached();
9135 }
9136 
9137 #else
9138 
9139 static void switch_mode(CPUARMState *env, int mode)
9140 {
9141     int old_mode;
9142     int i;
9143 
9144     old_mode = env->uncached_cpsr & CPSR_M;
9145     if (mode == old_mode)
9146         return;
9147 
9148     if (old_mode == ARM_CPU_MODE_FIQ) {
9149         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9150         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9151     } else if (mode == ARM_CPU_MODE_FIQ) {
9152         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9153         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9154     }
9155 
9156     i = bank_number(old_mode);
9157     env->banked_r13[i] = env->regs[13];
9158     env->banked_spsr[i] = env->spsr;
9159 
9160     i = bank_number(mode);
9161     env->regs[13] = env->banked_r13[i];
9162     env->spsr = env->banked_spsr[i];
9163 
9164     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9165     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9166 }
9167 
9168 /* Physical Interrupt Target EL Lookup Table
9169  *
9170  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9171  *
9172  * The below multi-dimensional table is used for looking up the target
9173  * exception level given numerous condition criteria.  Specifically, the
9174  * target EL is based on SCR and HCR routing controls as well as the
9175  * currently executing EL and secure state.
9176  *
9177  *    Dimensions:
9178  *    target_el_table[2][2][2][2][2][4]
9179  *                    |  |  |  |  |  +--- Current EL
9180  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9181  *                    |  |  |  +--------- HCR mask override
9182  *                    |  |  +------------ SCR exec state control
9183  *                    |  +--------------- SCR mask override
9184  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9185  *
9186  *    The table values are as such:
9187  *    0-3 = EL0-EL3
9188  *     -1 = Cannot occur
9189  *
9190  * The ARM ARM target EL table includes entries indicating that an "exception
9191  * is not taken".  The two cases where this is applicable are:
9192  *    1) An exception is taken from EL3 but the SCR does not have the exception
9193  *    routed to EL3.
9194  *    2) An exception is taken from EL2 but the HCR does not have the exception
9195  *    routed to EL2.
9196  * In these two cases, the below table contain a target of EL1.  This value is
9197  * returned as it is expected that the consumer of the table data will check
9198  * for "target EL >= current EL" to ensure the exception is not taken.
9199  *
9200  *            SCR     HCR
9201  *         64  EA     AMO                 From
9202  *        BIT IRQ     IMO      Non-secure         Secure
9203  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9204  */
9205 static const int8_t target_el_table[2][2][2][2][2][4] = {
9206     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9207        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9208       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9209        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9210      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9211        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9212       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9213        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9214     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9215        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9216       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9217        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9218      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9219        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9220       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9221        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9222 };
9223 
9224 /*
9225  * Determine the target EL for physical exceptions
9226  */
9227 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9228                                  uint32_t cur_el, bool secure)
9229 {
9230     CPUARMState *env = cs->env_ptr;
9231     bool rw;
9232     bool scr;
9233     bool hcr;
9234     int target_el;
9235     /* Is the highest EL AArch64? */
9236     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9237     uint64_t hcr_el2;
9238 
9239     if (arm_feature(env, ARM_FEATURE_EL3)) {
9240         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9241     } else {
9242         /* Either EL2 is the highest EL (and so the EL2 register width
9243          * is given by is64); or there is no EL2 or EL3, in which case
9244          * the value of 'rw' does not affect the table lookup anyway.
9245          */
9246         rw = is64;
9247     }
9248 
9249     hcr_el2 = arm_hcr_el2_eff(env);
9250     switch (excp_idx) {
9251     case EXCP_IRQ:
9252         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9253         hcr = hcr_el2 & HCR_IMO;
9254         break;
9255     case EXCP_FIQ:
9256         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9257         hcr = hcr_el2 & HCR_FMO;
9258         break;
9259     default:
9260         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9261         hcr = hcr_el2 & HCR_AMO;
9262         break;
9263     };
9264 
9265     /*
9266      * For these purposes, TGE and AMO/IMO/FMO both force the
9267      * interrupt to EL2.  Fold TGE into the bit extracted above.
9268      */
9269     hcr |= (hcr_el2 & HCR_TGE) != 0;
9270 
9271     /* Perform a table-lookup for the target EL given the current state */
9272     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9273 
9274     assert(target_el > 0);
9275 
9276     return target_el;
9277 }
9278 
9279 void arm_log_exception(CPUState *cs)
9280 {
9281     int idx = cs->exception_index;
9282 
9283     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9284         const char *exc = NULL;
9285         static const char * const excnames[] = {
9286             [EXCP_UDEF] = "Undefined Instruction",
9287             [EXCP_SWI] = "SVC",
9288             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9289             [EXCP_DATA_ABORT] = "Data Abort",
9290             [EXCP_IRQ] = "IRQ",
9291             [EXCP_FIQ] = "FIQ",
9292             [EXCP_BKPT] = "Breakpoint",
9293             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9294             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9295             [EXCP_HVC] = "Hypervisor Call",
9296             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9297             [EXCP_SMC] = "Secure Monitor Call",
9298             [EXCP_VIRQ] = "Virtual IRQ",
9299             [EXCP_VFIQ] = "Virtual FIQ",
9300             [EXCP_SEMIHOST] = "Semihosting call",
9301             [EXCP_NOCP] = "v7M NOCP UsageFault",
9302             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9303             [EXCP_STKOF] = "v8M STKOF UsageFault",
9304             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9305             [EXCP_LSERR] = "v8M LSERR UsageFault",
9306             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9307             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9308             [EXCP_VSERR] = "Virtual SERR",
9309         };
9310 
9311         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9312             exc = excnames[idx];
9313         }
9314         if (!exc) {
9315             exc = "unknown";
9316         }
9317         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9318                       idx, exc, cs->cpu_index);
9319     }
9320 }
9321 
9322 /*
9323  * Function used to synchronize QEMU's AArch64 register set with AArch32
9324  * register set.  This is necessary when switching between AArch32 and AArch64
9325  * execution state.
9326  */
9327 void aarch64_sync_32_to_64(CPUARMState *env)
9328 {
9329     int i;
9330     uint32_t mode = env->uncached_cpsr & CPSR_M;
9331 
9332     /* We can blanket copy R[0:7] to X[0:7] */
9333     for (i = 0; i < 8; i++) {
9334         env->xregs[i] = env->regs[i];
9335     }
9336 
9337     /*
9338      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9339      * Otherwise, they come from the banked user regs.
9340      */
9341     if (mode == ARM_CPU_MODE_FIQ) {
9342         for (i = 8; i < 13; i++) {
9343             env->xregs[i] = env->usr_regs[i - 8];
9344         }
9345     } else {
9346         for (i = 8; i < 13; i++) {
9347             env->xregs[i] = env->regs[i];
9348         }
9349     }
9350 
9351     /*
9352      * Registers x13-x23 are the various mode SP and FP registers. Registers
9353      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9354      * from the mode banked register.
9355      */
9356     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9357         env->xregs[13] = env->regs[13];
9358         env->xregs[14] = env->regs[14];
9359     } else {
9360         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9361         /* HYP is an exception in that it is copied from r14 */
9362         if (mode == ARM_CPU_MODE_HYP) {
9363             env->xregs[14] = env->regs[14];
9364         } else {
9365             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9366         }
9367     }
9368 
9369     if (mode == ARM_CPU_MODE_HYP) {
9370         env->xregs[15] = env->regs[13];
9371     } else {
9372         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9373     }
9374 
9375     if (mode == ARM_CPU_MODE_IRQ) {
9376         env->xregs[16] = env->regs[14];
9377         env->xregs[17] = env->regs[13];
9378     } else {
9379         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9380         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9381     }
9382 
9383     if (mode == ARM_CPU_MODE_SVC) {
9384         env->xregs[18] = env->regs[14];
9385         env->xregs[19] = env->regs[13];
9386     } else {
9387         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9388         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9389     }
9390 
9391     if (mode == ARM_CPU_MODE_ABT) {
9392         env->xregs[20] = env->regs[14];
9393         env->xregs[21] = env->regs[13];
9394     } else {
9395         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9396         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9397     }
9398 
9399     if (mode == ARM_CPU_MODE_UND) {
9400         env->xregs[22] = env->regs[14];
9401         env->xregs[23] = env->regs[13];
9402     } else {
9403         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9404         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9405     }
9406 
9407     /*
9408      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9409      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9410      * FIQ bank for r8-r14.
9411      */
9412     if (mode == ARM_CPU_MODE_FIQ) {
9413         for (i = 24; i < 31; i++) {
9414             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9415         }
9416     } else {
9417         for (i = 24; i < 29; i++) {
9418             env->xregs[i] = env->fiq_regs[i - 24];
9419         }
9420         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9421         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9422     }
9423 
9424     env->pc = env->regs[15];
9425 }
9426 
9427 /*
9428  * Function used to synchronize QEMU's AArch32 register set with AArch64
9429  * register set.  This is necessary when switching between AArch32 and AArch64
9430  * execution state.
9431  */
9432 void aarch64_sync_64_to_32(CPUARMState *env)
9433 {
9434     int i;
9435     uint32_t mode = env->uncached_cpsr & CPSR_M;
9436 
9437     /* We can blanket copy X[0:7] to R[0:7] */
9438     for (i = 0; i < 8; i++) {
9439         env->regs[i] = env->xregs[i];
9440     }
9441 
9442     /*
9443      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9444      * Otherwise, we copy x8-x12 into the banked user regs.
9445      */
9446     if (mode == ARM_CPU_MODE_FIQ) {
9447         for (i = 8; i < 13; i++) {
9448             env->usr_regs[i - 8] = env->xregs[i];
9449         }
9450     } else {
9451         for (i = 8; i < 13; i++) {
9452             env->regs[i] = env->xregs[i];
9453         }
9454     }
9455 
9456     /*
9457      * Registers r13 & r14 depend on the current mode.
9458      * If we are in a given mode, we copy the corresponding x registers to r13
9459      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9460      * for the mode.
9461      */
9462     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9463         env->regs[13] = env->xregs[13];
9464         env->regs[14] = env->xregs[14];
9465     } else {
9466         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9467 
9468         /*
9469          * HYP is an exception in that it does not have its own banked r14 but
9470          * shares the USR r14
9471          */
9472         if (mode == ARM_CPU_MODE_HYP) {
9473             env->regs[14] = env->xregs[14];
9474         } else {
9475             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9476         }
9477     }
9478 
9479     if (mode == ARM_CPU_MODE_HYP) {
9480         env->regs[13] = env->xregs[15];
9481     } else {
9482         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9483     }
9484 
9485     if (mode == ARM_CPU_MODE_IRQ) {
9486         env->regs[14] = env->xregs[16];
9487         env->regs[13] = env->xregs[17];
9488     } else {
9489         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9490         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9491     }
9492 
9493     if (mode == ARM_CPU_MODE_SVC) {
9494         env->regs[14] = env->xregs[18];
9495         env->regs[13] = env->xregs[19];
9496     } else {
9497         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9498         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9499     }
9500 
9501     if (mode == ARM_CPU_MODE_ABT) {
9502         env->regs[14] = env->xregs[20];
9503         env->regs[13] = env->xregs[21];
9504     } else {
9505         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9506         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9507     }
9508 
9509     if (mode == ARM_CPU_MODE_UND) {
9510         env->regs[14] = env->xregs[22];
9511         env->regs[13] = env->xregs[23];
9512     } else {
9513         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9514         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9515     }
9516 
9517     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9518      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9519      * FIQ bank for r8-r14.
9520      */
9521     if (mode == ARM_CPU_MODE_FIQ) {
9522         for (i = 24; i < 31; i++) {
9523             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9524         }
9525     } else {
9526         for (i = 24; i < 29; i++) {
9527             env->fiq_regs[i - 24] = env->xregs[i];
9528         }
9529         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9530         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9531     }
9532 
9533     env->regs[15] = env->pc;
9534 }
9535 
9536 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9537                                    uint32_t mask, uint32_t offset,
9538                                    uint32_t newpc)
9539 {
9540     int new_el;
9541 
9542     /* Change the CPU state so as to actually take the exception. */
9543     switch_mode(env, new_mode);
9544 
9545     /*
9546      * For exceptions taken to AArch32 we must clear the SS bit in both
9547      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9548      */
9549     env->pstate &= ~PSTATE_SS;
9550     env->spsr = cpsr_read(env);
9551     /* Clear IT bits.  */
9552     env->condexec_bits = 0;
9553     /* Switch to the new mode, and to the correct instruction set.  */
9554     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9555 
9556     /* This must be after mode switching. */
9557     new_el = arm_current_el(env);
9558 
9559     /* Set new mode endianness */
9560     env->uncached_cpsr &= ~CPSR_E;
9561     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9562         env->uncached_cpsr |= CPSR_E;
9563     }
9564     /* J and IL must always be cleared for exception entry */
9565     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9566     env->daif |= mask;
9567 
9568     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9569         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9570             env->uncached_cpsr |= CPSR_SSBS;
9571         } else {
9572             env->uncached_cpsr &= ~CPSR_SSBS;
9573         }
9574     }
9575 
9576     if (new_mode == ARM_CPU_MODE_HYP) {
9577         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9578         env->elr_el[2] = env->regs[15];
9579     } else {
9580         /* CPSR.PAN is normally preserved preserved unless...  */
9581         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9582             switch (new_el) {
9583             case 3:
9584                 if (!arm_is_secure_below_el3(env)) {
9585                     /* ... the target is EL3, from non-secure state.  */
9586                     env->uncached_cpsr &= ~CPSR_PAN;
9587                     break;
9588                 }
9589                 /* ... the target is EL3, from secure state ... */
9590                 /* fall through */
9591             case 1:
9592                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9593                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9594                     env->uncached_cpsr |= CPSR_PAN;
9595                 }
9596                 break;
9597             }
9598         }
9599         /*
9600          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9601          * and we should just guard the thumb mode on V4
9602          */
9603         if (arm_feature(env, ARM_FEATURE_V4T)) {
9604             env->thumb =
9605                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9606         }
9607         env->regs[14] = env->regs[15] + offset;
9608     }
9609     env->regs[15] = newpc;
9610     arm_rebuild_hflags(env);
9611 }
9612 
9613 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9614 {
9615     /*
9616      * Handle exception entry to Hyp mode; this is sufficiently
9617      * different to entry to other AArch32 modes that we handle it
9618      * separately here.
9619      *
9620      * The vector table entry used is always the 0x14 Hyp mode entry point,
9621      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9622      * The offset applied to the preferred return address is always zero
9623      * (see DDI0487C.a section G1.12.3).
9624      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9625      */
9626     uint32_t addr, mask;
9627     ARMCPU *cpu = ARM_CPU(cs);
9628     CPUARMState *env = &cpu->env;
9629 
9630     switch (cs->exception_index) {
9631     case EXCP_UDEF:
9632         addr = 0x04;
9633         break;
9634     case EXCP_SWI:
9635         addr = 0x08;
9636         break;
9637     case EXCP_BKPT:
9638         /* Fall through to prefetch abort.  */
9639     case EXCP_PREFETCH_ABORT:
9640         env->cp15.ifar_s = env->exception.vaddress;
9641         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9642                       (uint32_t)env->exception.vaddress);
9643         addr = 0x0c;
9644         break;
9645     case EXCP_DATA_ABORT:
9646         env->cp15.dfar_s = env->exception.vaddress;
9647         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9648                       (uint32_t)env->exception.vaddress);
9649         addr = 0x10;
9650         break;
9651     case EXCP_IRQ:
9652         addr = 0x18;
9653         break;
9654     case EXCP_FIQ:
9655         addr = 0x1c;
9656         break;
9657     case EXCP_HVC:
9658         addr = 0x08;
9659         break;
9660     case EXCP_HYP_TRAP:
9661         addr = 0x14;
9662         break;
9663     default:
9664         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9665     }
9666 
9667     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9668         if (!arm_feature(env, ARM_FEATURE_V8)) {
9669             /*
9670              * QEMU syndrome values are v8-style. v7 has the IL bit
9671              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9672              * If this is a v7 CPU, squash the IL bit in those cases.
9673              */
9674             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9675                 (cs->exception_index == EXCP_DATA_ABORT &&
9676                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9677                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9678                 env->exception.syndrome &= ~ARM_EL_IL;
9679             }
9680         }
9681         env->cp15.esr_el[2] = env->exception.syndrome;
9682     }
9683 
9684     if (arm_current_el(env) != 2 && addr < 0x14) {
9685         addr = 0x14;
9686     }
9687 
9688     mask = 0;
9689     if (!(env->cp15.scr_el3 & SCR_EA)) {
9690         mask |= CPSR_A;
9691     }
9692     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9693         mask |= CPSR_I;
9694     }
9695     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9696         mask |= CPSR_F;
9697     }
9698 
9699     addr += env->cp15.hvbar;
9700 
9701     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9702 }
9703 
9704 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9705 {
9706     ARMCPU *cpu = ARM_CPU(cs);
9707     CPUARMState *env = &cpu->env;
9708     uint32_t addr;
9709     uint32_t mask;
9710     int new_mode;
9711     uint32_t offset;
9712     uint32_t moe;
9713 
9714     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9715     switch (syn_get_ec(env->exception.syndrome)) {
9716     case EC_BREAKPOINT:
9717     case EC_BREAKPOINT_SAME_EL:
9718         moe = 1;
9719         break;
9720     case EC_WATCHPOINT:
9721     case EC_WATCHPOINT_SAME_EL:
9722         moe = 10;
9723         break;
9724     case EC_AA32_BKPT:
9725         moe = 3;
9726         break;
9727     case EC_VECTORCATCH:
9728         moe = 5;
9729         break;
9730     default:
9731         moe = 0;
9732         break;
9733     }
9734 
9735     if (moe) {
9736         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9737     }
9738 
9739     if (env->exception.target_el == 2) {
9740         arm_cpu_do_interrupt_aarch32_hyp(cs);
9741         return;
9742     }
9743 
9744     switch (cs->exception_index) {
9745     case EXCP_UDEF:
9746         new_mode = ARM_CPU_MODE_UND;
9747         addr = 0x04;
9748         mask = CPSR_I;
9749         if (env->thumb)
9750             offset = 2;
9751         else
9752             offset = 4;
9753         break;
9754     case EXCP_SWI:
9755         new_mode = ARM_CPU_MODE_SVC;
9756         addr = 0x08;
9757         mask = CPSR_I;
9758         /* The PC already points to the next instruction.  */
9759         offset = 0;
9760         break;
9761     case EXCP_BKPT:
9762         /* Fall through to prefetch abort.  */
9763     case EXCP_PREFETCH_ABORT:
9764         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9765         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9766         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9767                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9768         new_mode = ARM_CPU_MODE_ABT;
9769         addr = 0x0c;
9770         mask = CPSR_A | CPSR_I;
9771         offset = 4;
9772         break;
9773     case EXCP_DATA_ABORT:
9774         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9775         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9776         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9777                       env->exception.fsr,
9778                       (uint32_t)env->exception.vaddress);
9779         new_mode = ARM_CPU_MODE_ABT;
9780         addr = 0x10;
9781         mask = CPSR_A | CPSR_I;
9782         offset = 8;
9783         break;
9784     case EXCP_IRQ:
9785         new_mode = ARM_CPU_MODE_IRQ;
9786         addr = 0x18;
9787         /* Disable IRQ and imprecise data aborts.  */
9788         mask = CPSR_A | CPSR_I;
9789         offset = 4;
9790         if (env->cp15.scr_el3 & SCR_IRQ) {
9791             /* IRQ routed to monitor mode */
9792             new_mode = ARM_CPU_MODE_MON;
9793             mask |= CPSR_F;
9794         }
9795         break;
9796     case EXCP_FIQ:
9797         new_mode = ARM_CPU_MODE_FIQ;
9798         addr = 0x1c;
9799         /* Disable FIQ, IRQ and imprecise data aborts.  */
9800         mask = CPSR_A | CPSR_I | CPSR_F;
9801         if (env->cp15.scr_el3 & SCR_FIQ) {
9802             /* FIQ routed to monitor mode */
9803             new_mode = ARM_CPU_MODE_MON;
9804         }
9805         offset = 4;
9806         break;
9807     case EXCP_VIRQ:
9808         new_mode = ARM_CPU_MODE_IRQ;
9809         addr = 0x18;
9810         /* Disable IRQ and imprecise data aborts.  */
9811         mask = CPSR_A | CPSR_I;
9812         offset = 4;
9813         break;
9814     case EXCP_VFIQ:
9815         new_mode = ARM_CPU_MODE_FIQ;
9816         addr = 0x1c;
9817         /* Disable FIQ, IRQ and imprecise data aborts.  */
9818         mask = CPSR_A | CPSR_I | CPSR_F;
9819         offset = 4;
9820         break;
9821     case EXCP_VSERR:
9822         {
9823             /*
9824              * Note that this is reported as a data abort, but the DFAR
9825              * has an UNKNOWN value.  Construct the SError syndrome from
9826              * AET and ExT fields.
9827              */
9828             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9829 
9830             if (extended_addresses_enabled(env)) {
9831                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9832             } else {
9833                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9834             }
9835             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9836             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9837             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9838                           env->exception.fsr);
9839 
9840             new_mode = ARM_CPU_MODE_ABT;
9841             addr = 0x10;
9842             mask = CPSR_A | CPSR_I;
9843             offset = 8;
9844         }
9845         break;
9846     case EXCP_SMC:
9847         new_mode = ARM_CPU_MODE_MON;
9848         addr = 0x08;
9849         mask = CPSR_A | CPSR_I | CPSR_F;
9850         offset = 0;
9851         break;
9852     default:
9853         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9854         return; /* Never happens.  Keep compiler happy.  */
9855     }
9856 
9857     if (new_mode == ARM_CPU_MODE_MON) {
9858         addr += env->cp15.mvbar;
9859     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9860         /* High vectors. When enabled, base address cannot be remapped. */
9861         addr += 0xffff0000;
9862     } else {
9863         /* ARM v7 architectures provide a vector base address register to remap
9864          * the interrupt vector table.
9865          * This register is only followed in non-monitor mode, and is banked.
9866          * Note: only bits 31:5 are valid.
9867          */
9868         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9869     }
9870 
9871     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9872         env->cp15.scr_el3 &= ~SCR_NS;
9873     }
9874 
9875     take_aarch32_exception(env, new_mode, mask, offset, addr);
9876 }
9877 
9878 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9879 {
9880     /*
9881      * Return the register number of the AArch64 view of the AArch32
9882      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9883      * be that of the AArch32 mode the exception came from.
9884      */
9885     int mode = env->uncached_cpsr & CPSR_M;
9886 
9887     switch (aarch32_reg) {
9888     case 0 ... 7:
9889         return aarch32_reg;
9890     case 8 ... 12:
9891         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9892     case 13:
9893         switch (mode) {
9894         case ARM_CPU_MODE_USR:
9895         case ARM_CPU_MODE_SYS:
9896             return 13;
9897         case ARM_CPU_MODE_HYP:
9898             return 15;
9899         case ARM_CPU_MODE_IRQ:
9900             return 17;
9901         case ARM_CPU_MODE_SVC:
9902             return 19;
9903         case ARM_CPU_MODE_ABT:
9904             return 21;
9905         case ARM_CPU_MODE_UND:
9906             return 23;
9907         case ARM_CPU_MODE_FIQ:
9908             return 29;
9909         default:
9910             g_assert_not_reached();
9911         }
9912     case 14:
9913         switch (mode) {
9914         case ARM_CPU_MODE_USR:
9915         case ARM_CPU_MODE_SYS:
9916         case ARM_CPU_MODE_HYP:
9917             return 14;
9918         case ARM_CPU_MODE_IRQ:
9919             return 16;
9920         case ARM_CPU_MODE_SVC:
9921             return 18;
9922         case ARM_CPU_MODE_ABT:
9923             return 20;
9924         case ARM_CPU_MODE_UND:
9925             return 22;
9926         case ARM_CPU_MODE_FIQ:
9927             return 30;
9928         default:
9929             g_assert_not_reached();
9930         }
9931     case 15:
9932         return 31;
9933     default:
9934         g_assert_not_reached();
9935     }
9936 }
9937 
9938 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9939 {
9940     uint32_t ret = cpsr_read(env);
9941 
9942     /* Move DIT to the correct location for SPSR_ELx */
9943     if (ret & CPSR_DIT) {
9944         ret &= ~CPSR_DIT;
9945         ret |= PSTATE_DIT;
9946     }
9947     /* Merge PSTATE.SS into SPSR_ELx */
9948     ret |= env->pstate & PSTATE_SS;
9949 
9950     return ret;
9951 }
9952 
9953 static bool syndrome_is_sync_extabt(uint32_t syndrome)
9954 {
9955     /* Return true if this syndrome value is a synchronous external abort */
9956     switch (syn_get_ec(syndrome)) {
9957     case EC_INSNABORT:
9958     case EC_INSNABORT_SAME_EL:
9959     case EC_DATAABORT:
9960     case EC_DATAABORT_SAME_EL:
9961         /* Look at fault status code for all the synchronous ext abort cases */
9962         switch (syndrome & 0x3f) {
9963         case 0x10:
9964         case 0x13:
9965         case 0x14:
9966         case 0x15:
9967         case 0x16:
9968         case 0x17:
9969             return true;
9970         default:
9971             return false;
9972         }
9973     default:
9974         return false;
9975     }
9976 }
9977 
9978 /* Handle exception entry to a target EL which is using AArch64 */
9979 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9980 {
9981     ARMCPU *cpu = ARM_CPU(cs);
9982     CPUARMState *env = &cpu->env;
9983     unsigned int new_el = env->exception.target_el;
9984     target_ulong addr = env->cp15.vbar_el[new_el];
9985     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9986     unsigned int old_mode;
9987     unsigned int cur_el = arm_current_el(env);
9988     int rt;
9989 
9990     /*
9991      * Note that new_el can never be 0.  If cur_el is 0, then
9992      * el0_a64 is is_a64(), else el0_a64 is ignored.
9993      */
9994     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9995 
9996     if (cur_el < new_el) {
9997         /* Entry vector offset depends on whether the implemented EL
9998          * immediately lower than the target level is using AArch32 or AArch64
9999          */
10000         bool is_aa64;
10001         uint64_t hcr;
10002 
10003         switch (new_el) {
10004         case 3:
10005             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10006             break;
10007         case 2:
10008             hcr = arm_hcr_el2_eff(env);
10009             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10010                 is_aa64 = (hcr & HCR_RW) != 0;
10011                 break;
10012             }
10013             /* fall through */
10014         case 1:
10015             is_aa64 = is_a64(env);
10016             break;
10017         default:
10018             g_assert_not_reached();
10019         }
10020 
10021         if (is_aa64) {
10022             addr += 0x400;
10023         } else {
10024             addr += 0x600;
10025         }
10026     } else if (pstate_read(env) & PSTATE_SP) {
10027         addr += 0x200;
10028     }
10029 
10030     switch (cs->exception_index) {
10031     case EXCP_PREFETCH_ABORT:
10032     case EXCP_DATA_ABORT:
10033         /*
10034          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10035          * to be taken to the SError vector entrypoint.
10036          */
10037         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10038             syndrome_is_sync_extabt(env->exception.syndrome)) {
10039             addr += 0x180;
10040         }
10041         env->cp15.far_el[new_el] = env->exception.vaddress;
10042         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10043                       env->cp15.far_el[new_el]);
10044         /* fall through */
10045     case EXCP_BKPT:
10046     case EXCP_UDEF:
10047     case EXCP_SWI:
10048     case EXCP_HVC:
10049     case EXCP_HYP_TRAP:
10050     case EXCP_SMC:
10051         switch (syn_get_ec(env->exception.syndrome)) {
10052         case EC_ADVSIMDFPACCESSTRAP:
10053             /*
10054              * QEMU internal FP/SIMD syndromes from AArch32 include the
10055              * TA and coproc fields which are only exposed if the exception
10056              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10057              * AArch64 format syndrome.
10058              */
10059             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10060             break;
10061         case EC_CP14RTTRAP:
10062         case EC_CP15RTTRAP:
10063         case EC_CP14DTTRAP:
10064             /*
10065              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10066              * the raw register field from the insn; when taking this to
10067              * AArch64 we must convert it to the AArch64 view of the register
10068              * number. Notice that we read a 4-bit AArch32 register number and
10069              * write back a 5-bit AArch64 one.
10070              */
10071             rt = extract32(env->exception.syndrome, 5, 4);
10072             rt = aarch64_regnum(env, rt);
10073             env->exception.syndrome = deposit32(env->exception.syndrome,
10074                                                 5, 5, rt);
10075             break;
10076         case EC_CP15RRTTRAP:
10077         case EC_CP14RRTTRAP:
10078             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10079             rt = extract32(env->exception.syndrome, 5, 4);
10080             rt = aarch64_regnum(env, rt);
10081             env->exception.syndrome = deposit32(env->exception.syndrome,
10082                                                 5, 5, rt);
10083             rt = extract32(env->exception.syndrome, 10, 4);
10084             rt = aarch64_regnum(env, rt);
10085             env->exception.syndrome = deposit32(env->exception.syndrome,
10086                                                 10, 5, rt);
10087             break;
10088         }
10089         env->cp15.esr_el[new_el] = env->exception.syndrome;
10090         break;
10091     case EXCP_IRQ:
10092     case EXCP_VIRQ:
10093         addr += 0x80;
10094         break;
10095     case EXCP_FIQ:
10096     case EXCP_VFIQ:
10097         addr += 0x100;
10098         break;
10099     case EXCP_VSERR:
10100         addr += 0x180;
10101         /* Construct the SError syndrome from IDS and ISS fields. */
10102         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10103         env->cp15.esr_el[new_el] = env->exception.syndrome;
10104         break;
10105     default:
10106         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10107     }
10108 
10109     if (is_a64(env)) {
10110         old_mode = pstate_read(env);
10111         aarch64_save_sp(env, arm_current_el(env));
10112         env->elr_el[new_el] = env->pc;
10113     } else {
10114         old_mode = cpsr_read_for_spsr_elx(env);
10115         env->elr_el[new_el] = env->regs[15];
10116 
10117         aarch64_sync_32_to_64(env);
10118 
10119         env->condexec_bits = 0;
10120     }
10121     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10122 
10123     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10124                   env->elr_el[new_el]);
10125 
10126     if (cpu_isar_feature(aa64_pan, cpu)) {
10127         /* The value of PSTATE.PAN is normally preserved, except when ... */
10128         new_mode |= old_mode & PSTATE_PAN;
10129         switch (new_el) {
10130         case 2:
10131             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10132             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10133                 != (HCR_E2H | HCR_TGE)) {
10134                 break;
10135             }
10136             /* fall through */
10137         case 1:
10138             /* ... the target is EL1 ... */
10139             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10140             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10141                 new_mode |= PSTATE_PAN;
10142             }
10143             break;
10144         }
10145     }
10146     if (cpu_isar_feature(aa64_mte, cpu)) {
10147         new_mode |= PSTATE_TCO;
10148     }
10149 
10150     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10151         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10152             new_mode |= PSTATE_SSBS;
10153         } else {
10154             new_mode &= ~PSTATE_SSBS;
10155         }
10156     }
10157 
10158     pstate_write(env, PSTATE_DAIF | new_mode);
10159     env->aarch64 = true;
10160     aarch64_restore_sp(env, new_el);
10161     helper_rebuild_hflags_a64(env, new_el);
10162 
10163     env->pc = addr;
10164 
10165     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10166                   new_el, env->pc, pstate_read(env));
10167 }
10168 
10169 /*
10170  * Do semihosting call and set the appropriate return value. All the
10171  * permission and validity checks have been done at translate time.
10172  *
10173  * We only see semihosting exceptions in TCG only as they are not
10174  * trapped to the hypervisor in KVM.
10175  */
10176 #ifdef CONFIG_TCG
10177 static void handle_semihosting(CPUState *cs)
10178 {
10179     ARMCPU *cpu = ARM_CPU(cs);
10180     CPUARMState *env = &cpu->env;
10181 
10182     if (is_a64(env)) {
10183         qemu_log_mask(CPU_LOG_INT,
10184                       "...handling as semihosting call 0x%" PRIx64 "\n",
10185                       env->xregs[0]);
10186         do_common_semihosting(cs);
10187         env->pc += 4;
10188     } else {
10189         qemu_log_mask(CPU_LOG_INT,
10190                       "...handling as semihosting call 0x%x\n",
10191                       env->regs[0]);
10192         do_common_semihosting(cs);
10193         env->regs[15] += env->thumb ? 2 : 4;
10194     }
10195 }
10196 #endif
10197 
10198 /* Handle a CPU exception for A and R profile CPUs.
10199  * Do any appropriate logging, handle PSCI calls, and then hand off
10200  * to the AArch64-entry or AArch32-entry function depending on the
10201  * target exception level's register width.
10202  *
10203  * Note: this is used for both TCG (as the do_interrupt tcg op),
10204  *       and KVM to re-inject guest debug exceptions, and to
10205  *       inject a Synchronous-External-Abort.
10206  */
10207 void arm_cpu_do_interrupt(CPUState *cs)
10208 {
10209     ARMCPU *cpu = ARM_CPU(cs);
10210     CPUARMState *env = &cpu->env;
10211     unsigned int new_el = env->exception.target_el;
10212 
10213     assert(!arm_feature(env, ARM_FEATURE_M));
10214 
10215     arm_log_exception(cs);
10216     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10217                   new_el);
10218     if (qemu_loglevel_mask(CPU_LOG_INT)
10219         && !excp_is_internal(cs->exception_index)) {
10220         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10221                       syn_get_ec(env->exception.syndrome),
10222                       env->exception.syndrome);
10223     }
10224 
10225     if (arm_is_psci_call(cpu, cs->exception_index)) {
10226         arm_handle_psci_call(cpu);
10227         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10228         return;
10229     }
10230 
10231     /*
10232      * Semihosting semantics depend on the register width of the code
10233      * that caused the exception, not the target exception level, so
10234      * must be handled here.
10235      */
10236 #ifdef CONFIG_TCG
10237     if (cs->exception_index == EXCP_SEMIHOST) {
10238         handle_semihosting(cs);
10239         return;
10240     }
10241 #endif
10242 
10243     /* Hooks may change global state so BQL should be held, also the
10244      * BQL needs to be held for any modification of
10245      * cs->interrupt_request.
10246      */
10247     g_assert(qemu_mutex_iothread_locked());
10248 
10249     arm_call_pre_el_change_hook(cpu);
10250 
10251     assert(!excp_is_internal(cs->exception_index));
10252     if (arm_el_is_aa64(env, new_el)) {
10253         arm_cpu_do_interrupt_aarch64(cs);
10254     } else {
10255         arm_cpu_do_interrupt_aarch32(cs);
10256     }
10257 
10258     arm_call_el_change_hook(cpu);
10259 
10260     if (!kvm_enabled()) {
10261         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10262     }
10263 }
10264 #endif /* !CONFIG_USER_ONLY */
10265 
10266 uint64_t arm_sctlr(CPUARMState *env, int el)
10267 {
10268     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10269     if (el == 0) {
10270         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10271         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10272     }
10273     return env->cp15.sctlr_el[el];
10274 }
10275 
10276 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10277 {
10278     if (regime_has_2_ranges(mmu_idx)) {
10279         return extract64(tcr, 37, 2);
10280     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10281         return 0; /* VTCR_EL2 */
10282     } else {
10283         /* Replicate the single TBI bit so we always have 2 bits.  */
10284         return extract32(tcr, 20, 1) * 3;
10285     }
10286 }
10287 
10288 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10289 {
10290     if (regime_has_2_ranges(mmu_idx)) {
10291         return extract64(tcr, 51, 2);
10292     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10293         return 0; /* VTCR_EL2 */
10294     } else {
10295         /* Replicate the single TBID bit so we always have 2 bits.  */
10296         return extract32(tcr, 29, 1) * 3;
10297     }
10298 }
10299 
10300 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10301 {
10302     if (regime_has_2_ranges(mmu_idx)) {
10303         return extract64(tcr, 57, 2);
10304     } else {
10305         /* Replicate the single TCMA bit so we always have 2 bits.  */
10306         return extract32(tcr, 30, 1) * 3;
10307     }
10308 }
10309 
10310 static ARMGranuleSize tg0_to_gran_size(int tg)
10311 {
10312     switch (tg) {
10313     case 0:
10314         return Gran4K;
10315     case 1:
10316         return Gran64K;
10317     case 2:
10318         return Gran16K;
10319     default:
10320         return GranInvalid;
10321     }
10322 }
10323 
10324 static ARMGranuleSize tg1_to_gran_size(int tg)
10325 {
10326     switch (tg) {
10327     case 1:
10328         return Gran16K;
10329     case 2:
10330         return Gran4K;
10331     case 3:
10332         return Gran64K;
10333     default:
10334         return GranInvalid;
10335     }
10336 }
10337 
10338 static inline bool have4k(ARMCPU *cpu, bool stage2)
10339 {
10340     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10341         : cpu_isar_feature(aa64_tgran4, cpu);
10342 }
10343 
10344 static inline bool have16k(ARMCPU *cpu, bool stage2)
10345 {
10346     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10347         : cpu_isar_feature(aa64_tgran16, cpu);
10348 }
10349 
10350 static inline bool have64k(ARMCPU *cpu, bool stage2)
10351 {
10352     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10353         : cpu_isar_feature(aa64_tgran64, cpu);
10354 }
10355 
10356 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10357                                          bool stage2)
10358 {
10359     switch (gran) {
10360     case Gran4K:
10361         if (have4k(cpu, stage2)) {
10362             return gran;
10363         }
10364         break;
10365     case Gran16K:
10366         if (have16k(cpu, stage2)) {
10367             return gran;
10368         }
10369         break;
10370     case Gran64K:
10371         if (have64k(cpu, stage2)) {
10372             return gran;
10373         }
10374         break;
10375     case GranInvalid:
10376         break;
10377     }
10378     /*
10379      * If the guest selects a granule size that isn't implemented,
10380      * the architecture requires that we behave as if it selected one
10381      * that is (with an IMPDEF choice of which one to pick). We choose
10382      * to implement the smallest supported granule size.
10383      */
10384     if (have4k(cpu, stage2)) {
10385         return Gran4K;
10386     }
10387     if (have16k(cpu, stage2)) {
10388         return Gran16K;
10389     }
10390     assert(have64k(cpu, stage2));
10391     return Gran64K;
10392 }
10393 
10394 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10395                                    ARMMMUIdx mmu_idx, bool data)
10396 {
10397     uint64_t tcr = regime_tcr(env, mmu_idx);
10398     bool epd, hpd, tsz_oob, ds;
10399     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10400     ARMGranuleSize gran;
10401     ARMCPU *cpu = env_archcpu(env);
10402     bool stage2 = mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S;
10403 
10404     if (!regime_has_2_ranges(mmu_idx)) {
10405         select = 0;
10406         tsz = extract32(tcr, 0, 6);
10407         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10408         if (stage2) {
10409             /* VTCR_EL2 */
10410             hpd = false;
10411         } else {
10412             hpd = extract32(tcr, 24, 1);
10413         }
10414         epd = false;
10415         sh = extract32(tcr, 12, 2);
10416         ps = extract32(tcr, 16, 3);
10417         ds = extract64(tcr, 32, 1);
10418     } else {
10419         /*
10420          * Bit 55 is always between the two regions, and is canonical for
10421          * determining if address tagging is enabled.
10422          */
10423         select = extract64(va, 55, 1);
10424         if (!select) {
10425             tsz = extract32(tcr, 0, 6);
10426             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10427             epd = extract32(tcr, 7, 1);
10428             sh = extract32(tcr, 12, 2);
10429             hpd = extract64(tcr, 41, 1);
10430         } else {
10431             tsz = extract32(tcr, 16, 6);
10432             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10433             epd = extract32(tcr, 23, 1);
10434             sh = extract32(tcr, 28, 2);
10435             hpd = extract64(tcr, 42, 1);
10436         }
10437         ps = extract64(tcr, 32, 3);
10438         ds = extract64(tcr, 59, 1);
10439     }
10440 
10441     gran = sanitize_gran_size(cpu, gran, stage2);
10442 
10443     if (cpu_isar_feature(aa64_st, cpu)) {
10444         max_tsz = 48 - (gran == Gran64K);
10445     } else {
10446         max_tsz = 39;
10447     }
10448 
10449     /*
10450      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10451      * adjust the effective value of DS, as documented.
10452      */
10453     min_tsz = 16;
10454     if (gran == Gran64K) {
10455         if (cpu_isar_feature(aa64_lva, cpu)) {
10456             min_tsz = 12;
10457         }
10458         ds = false;
10459     } else if (ds) {
10460         switch (mmu_idx) {
10461         case ARMMMUIdx_Stage2:
10462         case ARMMMUIdx_Stage2_S:
10463             if (gran == Gran16K) {
10464                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10465             } else {
10466                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10467             }
10468             break;
10469         default:
10470             if (gran == Gran16K) {
10471                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10472             } else {
10473                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10474             }
10475             break;
10476         }
10477         if (ds) {
10478             min_tsz = 12;
10479         }
10480     }
10481 
10482     if (tsz > max_tsz) {
10483         tsz = max_tsz;
10484         tsz_oob = true;
10485     } else if (tsz < min_tsz) {
10486         tsz = min_tsz;
10487         tsz_oob = true;
10488     } else {
10489         tsz_oob = false;
10490     }
10491 
10492     /* Present TBI as a composite with TBID.  */
10493     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10494     if (!data) {
10495         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10496     }
10497     tbi = (tbi >> select) & 1;
10498 
10499     return (ARMVAParameters) {
10500         .tsz = tsz,
10501         .ps = ps,
10502         .sh = sh,
10503         .select = select,
10504         .tbi = tbi,
10505         .epd = epd,
10506         .hpd = hpd,
10507         .tsz_oob = tsz_oob,
10508         .ds = ds,
10509         .gran = gran,
10510     };
10511 }
10512 
10513 /* Note that signed overflow is undefined in C.  The following routines are
10514    careful to use unsigned types where modulo arithmetic is required.
10515    Failure to do so _will_ break on newer gcc.  */
10516 
10517 /* Signed saturating arithmetic.  */
10518 
10519 /* Perform 16-bit signed saturating addition.  */
10520 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10521 {
10522     uint16_t res;
10523 
10524     res = a + b;
10525     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10526         if (a & 0x8000)
10527             res = 0x8000;
10528         else
10529             res = 0x7fff;
10530     }
10531     return res;
10532 }
10533 
10534 /* Perform 8-bit signed saturating addition.  */
10535 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10536 {
10537     uint8_t res;
10538 
10539     res = a + b;
10540     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10541         if (a & 0x80)
10542             res = 0x80;
10543         else
10544             res = 0x7f;
10545     }
10546     return res;
10547 }
10548 
10549 /* Perform 16-bit signed saturating subtraction.  */
10550 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10551 {
10552     uint16_t res;
10553 
10554     res = a - b;
10555     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10556         if (a & 0x8000)
10557             res = 0x8000;
10558         else
10559             res = 0x7fff;
10560     }
10561     return res;
10562 }
10563 
10564 /* Perform 8-bit signed saturating subtraction.  */
10565 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10566 {
10567     uint8_t res;
10568 
10569     res = a - b;
10570     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10571         if (a & 0x80)
10572             res = 0x80;
10573         else
10574             res = 0x7f;
10575     }
10576     return res;
10577 }
10578 
10579 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10580 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10581 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10582 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10583 #define PFX q
10584 
10585 #include "op_addsub.h"
10586 
10587 /* Unsigned saturating arithmetic.  */
10588 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10589 {
10590     uint16_t res;
10591     res = a + b;
10592     if (res < a)
10593         res = 0xffff;
10594     return res;
10595 }
10596 
10597 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10598 {
10599     if (a > b)
10600         return a - b;
10601     else
10602         return 0;
10603 }
10604 
10605 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10606 {
10607     uint8_t res;
10608     res = a + b;
10609     if (res < a)
10610         res = 0xff;
10611     return res;
10612 }
10613 
10614 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10615 {
10616     if (a > b)
10617         return a - b;
10618     else
10619         return 0;
10620 }
10621 
10622 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10623 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10624 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10625 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10626 #define PFX uq
10627 
10628 #include "op_addsub.h"
10629 
10630 /* Signed modulo arithmetic.  */
10631 #define SARITH16(a, b, n, op) do { \
10632     int32_t sum; \
10633     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10634     RESULT(sum, n, 16); \
10635     if (sum >= 0) \
10636         ge |= 3 << (n * 2); \
10637     } while(0)
10638 
10639 #define SARITH8(a, b, n, op) do { \
10640     int32_t sum; \
10641     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10642     RESULT(sum, n, 8); \
10643     if (sum >= 0) \
10644         ge |= 1 << n; \
10645     } while(0)
10646 
10647 
10648 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10649 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10650 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10651 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10652 #define PFX s
10653 #define ARITH_GE
10654 
10655 #include "op_addsub.h"
10656 
10657 /* Unsigned modulo arithmetic.  */
10658 #define ADD16(a, b, n) do { \
10659     uint32_t sum; \
10660     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10661     RESULT(sum, n, 16); \
10662     if ((sum >> 16) == 1) \
10663         ge |= 3 << (n * 2); \
10664     } while(0)
10665 
10666 #define ADD8(a, b, n) do { \
10667     uint32_t sum; \
10668     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10669     RESULT(sum, n, 8); \
10670     if ((sum >> 8) == 1) \
10671         ge |= 1 << n; \
10672     } while(0)
10673 
10674 #define SUB16(a, b, n) do { \
10675     uint32_t sum; \
10676     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10677     RESULT(sum, n, 16); \
10678     if ((sum >> 16) == 0) \
10679         ge |= 3 << (n * 2); \
10680     } while(0)
10681 
10682 #define SUB8(a, b, n) do { \
10683     uint32_t sum; \
10684     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10685     RESULT(sum, n, 8); \
10686     if ((sum >> 8) == 0) \
10687         ge |= 1 << n; \
10688     } while(0)
10689 
10690 #define PFX u
10691 #define ARITH_GE
10692 
10693 #include "op_addsub.h"
10694 
10695 /* Halved signed arithmetic.  */
10696 #define ADD16(a, b, n) \
10697   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10698 #define SUB16(a, b, n) \
10699   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10700 #define ADD8(a, b, n) \
10701   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10702 #define SUB8(a, b, n) \
10703   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10704 #define PFX sh
10705 
10706 #include "op_addsub.h"
10707 
10708 /* Halved unsigned arithmetic.  */
10709 #define ADD16(a, b, n) \
10710   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10711 #define SUB16(a, b, n) \
10712   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10713 #define ADD8(a, b, n) \
10714   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10715 #define SUB8(a, b, n) \
10716   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10717 #define PFX uh
10718 
10719 #include "op_addsub.h"
10720 
10721 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10722 {
10723     if (a > b)
10724         return a - b;
10725     else
10726         return b - a;
10727 }
10728 
10729 /* Unsigned sum of absolute byte differences.  */
10730 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10731 {
10732     uint32_t sum;
10733     sum = do_usad(a, b);
10734     sum += do_usad(a >> 8, b >> 8);
10735     sum += do_usad(a >> 16, b >> 16);
10736     sum += do_usad(a >> 24, b >> 24);
10737     return sum;
10738 }
10739 
10740 /* For ARMv6 SEL instruction.  */
10741 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10742 {
10743     uint32_t mask;
10744 
10745     mask = 0;
10746     if (flags & 1)
10747         mask |= 0xff;
10748     if (flags & 2)
10749         mask |= 0xff00;
10750     if (flags & 4)
10751         mask |= 0xff0000;
10752     if (flags & 8)
10753         mask |= 0xff000000;
10754     return (a & mask) | (b & ~mask);
10755 }
10756 
10757 /* CRC helpers.
10758  * The upper bytes of val (above the number specified by 'bytes') must have
10759  * been zeroed out by the caller.
10760  */
10761 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10762 {
10763     uint8_t buf[4];
10764 
10765     stl_le_p(buf, val);
10766 
10767     /* zlib crc32 converts the accumulator and output to one's complement.  */
10768     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10769 }
10770 
10771 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10772 {
10773     uint8_t buf[4];
10774 
10775     stl_le_p(buf, val);
10776 
10777     /* Linux crc32c converts the output to one's complement.  */
10778     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10779 }
10780 
10781 /* Return the exception level to which FP-disabled exceptions should
10782  * be taken, or 0 if FP is enabled.
10783  */
10784 int fp_exception_el(CPUARMState *env, int cur_el)
10785 {
10786 #ifndef CONFIG_USER_ONLY
10787     uint64_t hcr_el2;
10788 
10789     /* CPACR and the CPTR registers don't exist before v6, so FP is
10790      * always accessible
10791      */
10792     if (!arm_feature(env, ARM_FEATURE_V6)) {
10793         return 0;
10794     }
10795 
10796     if (arm_feature(env, ARM_FEATURE_M)) {
10797         /* CPACR can cause a NOCP UsageFault taken to current security state */
10798         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10799             return 1;
10800         }
10801 
10802         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10803             if (!extract32(env->v7m.nsacr, 10, 1)) {
10804                 /* FP insns cause a NOCP UsageFault taken to Secure */
10805                 return 3;
10806             }
10807         }
10808 
10809         return 0;
10810     }
10811 
10812     hcr_el2 = arm_hcr_el2_eff(env);
10813 
10814     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10815      * 0, 2 : trap EL0 and EL1/PL1 accesses
10816      * 1    : trap only EL0 accesses
10817      * 3    : trap no accesses
10818      * This register is ignored if E2H+TGE are both set.
10819      */
10820     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10821         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10822 
10823         switch (fpen) {
10824         case 1:
10825             if (cur_el != 0) {
10826                 break;
10827             }
10828             /* fall through */
10829         case 0:
10830         case 2:
10831             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10832             if (!arm_el_is_aa64(env, 3)
10833                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10834                 return 3;
10835             }
10836             if (cur_el <= 1) {
10837                 return 1;
10838             }
10839             break;
10840         }
10841     }
10842 
10843     /*
10844      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10845      * to control non-secure access to the FPU. It doesn't have any
10846      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10847      */
10848     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10849          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10850         if (!extract32(env->cp15.nsacr, 10, 1)) {
10851             /* FP insns act as UNDEF */
10852             return cur_el == 2 ? 2 : 1;
10853         }
10854     }
10855 
10856     /*
10857      * CPTR_EL2 is present in v7VE or v8, and changes format
10858      * with HCR_EL2.E2H (regardless of TGE).
10859      */
10860     if (cur_el <= 2) {
10861         if (hcr_el2 & HCR_E2H) {
10862             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10863             case 1:
10864                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10865                     break;
10866                 }
10867                 /* fall through */
10868             case 0:
10869             case 2:
10870                 return 2;
10871             }
10872         } else if (arm_is_el2_enabled(env)) {
10873             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10874                 return 2;
10875             }
10876         }
10877     }
10878 
10879     /* CPTR_EL3 : present in v8 */
10880     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10881         /* Trap all FP ops to EL3 */
10882         return 3;
10883     }
10884 #endif
10885     return 0;
10886 }
10887 
10888 /* Return the exception level we're running at if this is our mmu_idx */
10889 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10890 {
10891     if (mmu_idx & ARM_MMU_IDX_M) {
10892         return mmu_idx & ARM_MMU_IDX_M_PRIV;
10893     }
10894 
10895     switch (mmu_idx) {
10896     case ARMMMUIdx_E10_0:
10897     case ARMMMUIdx_E20_0:
10898         return 0;
10899     case ARMMMUIdx_E10_1:
10900     case ARMMMUIdx_E10_1_PAN:
10901         return 1;
10902     case ARMMMUIdx_E2:
10903     case ARMMMUIdx_E20_2:
10904     case ARMMMUIdx_E20_2_PAN:
10905         return 2;
10906     case ARMMMUIdx_E3:
10907         return 3;
10908     default:
10909         g_assert_not_reached();
10910     }
10911 }
10912 
10913 #ifndef CONFIG_TCG
10914 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10915 {
10916     g_assert_not_reached();
10917 }
10918 #endif
10919 
10920 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10921 {
10922     ARMMMUIdx idx;
10923     uint64_t hcr;
10924 
10925     if (arm_feature(env, ARM_FEATURE_M)) {
10926         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10927     }
10928 
10929     /* See ARM pseudo-function ELIsInHost.  */
10930     switch (el) {
10931     case 0:
10932         hcr = arm_hcr_el2_eff(env);
10933         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10934             idx = ARMMMUIdx_E20_0;
10935         } else {
10936             idx = ARMMMUIdx_E10_0;
10937         }
10938         break;
10939     case 1:
10940         if (env->pstate & PSTATE_PAN) {
10941             idx = ARMMMUIdx_E10_1_PAN;
10942         } else {
10943             idx = ARMMMUIdx_E10_1;
10944         }
10945         break;
10946     case 2:
10947         /* Note that TGE does not apply at EL2.  */
10948         if (arm_hcr_el2_eff(env) & HCR_E2H) {
10949             if (env->pstate & PSTATE_PAN) {
10950                 idx = ARMMMUIdx_E20_2_PAN;
10951             } else {
10952                 idx = ARMMMUIdx_E20_2;
10953             }
10954         } else {
10955             idx = ARMMMUIdx_E2;
10956         }
10957         break;
10958     case 3:
10959         return ARMMMUIdx_E3;
10960     default:
10961         g_assert_not_reached();
10962     }
10963 
10964     return idx;
10965 }
10966 
10967 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10968 {
10969     return arm_mmu_idx_el(env, arm_current_el(env));
10970 }
10971 
10972 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10973                                            ARMMMUIdx mmu_idx,
10974                                            CPUARMTBFlags flags)
10975 {
10976     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10977     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
10978 
10979     if (arm_singlestep_active(env)) {
10980         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
10981     }
10982     return flags;
10983 }
10984 
10985 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10986                                               ARMMMUIdx mmu_idx,
10987                                               CPUARMTBFlags flags)
10988 {
10989     bool sctlr_b = arm_sctlr_b(env);
10990 
10991     if (sctlr_b) {
10992         DP_TBFLAG_A32(flags, SCTLR__B, 1);
10993     }
10994     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
10995         DP_TBFLAG_ANY(flags, BE_DATA, 1);
10996     }
10997     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
10998 
10999     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11000 }
11001 
11002 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11003                                         ARMMMUIdx mmu_idx)
11004 {
11005     CPUARMTBFlags flags = {};
11006     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11007 
11008     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11009     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11010         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11011     }
11012 
11013     if (arm_v7m_is_handler_mode(env)) {
11014         DP_TBFLAG_M32(flags, HANDLER, 1);
11015     }
11016 
11017     /*
11018      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11019      * is suppressing them because the requested execution priority
11020      * is less than 0.
11021      */
11022     if (arm_feature(env, ARM_FEATURE_V8) &&
11023         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11024           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11025         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11026     }
11027 
11028     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11029         DP_TBFLAG_M32(flags, SECURE, 1);
11030     }
11031 
11032     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11033 }
11034 
11035 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11036                                         ARMMMUIdx mmu_idx)
11037 {
11038     CPUARMTBFlags flags = {};
11039     int el = arm_current_el(env);
11040 
11041     if (arm_sctlr(env, el) & SCTLR_A) {
11042         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11043     }
11044 
11045     if (arm_el_is_aa64(env, 1)) {
11046         DP_TBFLAG_A32(flags, VFPEN, 1);
11047     }
11048 
11049     if (el < 2 && env->cp15.hstr_el2 &&
11050         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11051         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11052     }
11053 
11054     if (env->uncached_cpsr & CPSR_IL) {
11055         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11056     }
11057 
11058     /*
11059      * The SME exception we are testing for is raised via
11060      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11061      * AArch32.CheckAdvSIMDOrFPEnabled().
11062      */
11063     if (el == 0
11064         && FIELD_EX64(env->svcr, SVCR, SM)
11065         && (!arm_is_el2_enabled(env)
11066             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11067         && arm_el_is_aa64(env, 1)
11068         && !sme_fa64(env, el)) {
11069         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11070     }
11071 
11072     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11073 }
11074 
11075 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11076                                         ARMMMUIdx mmu_idx)
11077 {
11078     CPUARMTBFlags flags = {};
11079     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11080     uint64_t tcr = regime_tcr(env, mmu_idx);
11081     uint64_t sctlr;
11082     int tbii, tbid;
11083 
11084     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11085 
11086     /* Get control bits for tagged addresses.  */
11087     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11088     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11089 
11090     DP_TBFLAG_A64(flags, TBII, tbii);
11091     DP_TBFLAG_A64(flags, TBID, tbid);
11092 
11093     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11094         int sve_el = sve_exception_el(env, el);
11095 
11096         /*
11097          * If either FP or SVE are disabled, translator does not need len.
11098          * If SVE EL > FP EL, FP exception has precedence, and translator
11099          * does not need SVE EL.  Save potential re-translations by forcing
11100          * the unneeded data to zero.
11101          */
11102         if (fp_el != 0) {
11103             if (sve_el > fp_el) {
11104                 sve_el = 0;
11105             }
11106         } else if (sve_el == 0) {
11107             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11108         }
11109         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11110     }
11111     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11112         int sme_el = sme_exception_el(env, el);
11113         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11114 
11115         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11116         if (sme_el == 0) {
11117             /* Similarly, do not compute SVL if SME is disabled. */
11118             int svl = sve_vqm1_for_el_sm(env, el, true);
11119             DP_TBFLAG_A64(flags, SVL, svl);
11120             if (sm) {
11121                 /* If SVE is disabled, we will not have set VL above. */
11122                 DP_TBFLAG_A64(flags, VL, svl);
11123             }
11124         }
11125         if (sm) {
11126             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11127             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11128         }
11129         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11130     }
11131 
11132     sctlr = regime_sctlr(env, stage1);
11133 
11134     if (sctlr & SCTLR_A) {
11135         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11136     }
11137 
11138     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11139         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11140     }
11141 
11142     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11143         /*
11144          * In order to save space in flags, we record only whether
11145          * pauth is "inactive", meaning all insns are implemented as
11146          * a nop, or "active" when some action must be performed.
11147          * The decision of which action to take is left to a helper.
11148          */
11149         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11150             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11151         }
11152     }
11153 
11154     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11155         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11156         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11157             DP_TBFLAG_A64(flags, BT, 1);
11158         }
11159     }
11160 
11161     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11162     if (!(env->pstate & PSTATE_UAO)) {
11163         switch (mmu_idx) {
11164         case ARMMMUIdx_E10_1:
11165         case ARMMMUIdx_E10_1_PAN:
11166             /* TODO: ARMv8.3-NV */
11167             DP_TBFLAG_A64(flags, UNPRIV, 1);
11168             break;
11169         case ARMMMUIdx_E20_2:
11170         case ARMMMUIdx_E20_2_PAN:
11171             /*
11172              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11173              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11174              */
11175             if (env->cp15.hcr_el2 & HCR_TGE) {
11176                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11177             }
11178             break;
11179         default:
11180             break;
11181         }
11182     }
11183 
11184     if (env->pstate & PSTATE_IL) {
11185         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11186     }
11187 
11188     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11189         /*
11190          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11191          * if all accesses must be Unchecked:
11192          * 1) If no TBI, then there are no tags in the address to check,
11193          * 2) If Tag Check Override, then all accesses are Unchecked,
11194          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11195          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11196          */
11197         if (allocation_tag_access_enabled(env, el, sctlr)) {
11198             DP_TBFLAG_A64(flags, ATA, 1);
11199             if (tbid
11200                 && !(env->pstate & PSTATE_TCO)
11201                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11202                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11203             }
11204         }
11205         /* And again for unprivileged accesses, if required.  */
11206         if (EX_TBFLAG_A64(flags, UNPRIV)
11207             && tbid
11208             && !(env->pstate & PSTATE_TCO)
11209             && (sctlr & SCTLR_TCF0)
11210             && allocation_tag_access_enabled(env, 0, sctlr)) {
11211             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11212         }
11213         /* Cache TCMA as well as TBI. */
11214         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11215     }
11216 
11217     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11218 }
11219 
11220 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11221 {
11222     int el = arm_current_el(env);
11223     int fp_el = fp_exception_el(env, el);
11224     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11225 
11226     if (is_a64(env)) {
11227         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11228     } else if (arm_feature(env, ARM_FEATURE_M)) {
11229         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11230     } else {
11231         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11232     }
11233 }
11234 
11235 void arm_rebuild_hflags(CPUARMState *env)
11236 {
11237     env->hflags = rebuild_hflags_internal(env);
11238 }
11239 
11240 /*
11241  * If we have triggered a EL state change we can't rely on the
11242  * translator having passed it to us, we need to recompute.
11243  */
11244 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11245 {
11246     int el = arm_current_el(env);
11247     int fp_el = fp_exception_el(env, el);
11248     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11249 
11250     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11251 }
11252 
11253 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11254 {
11255     int fp_el = fp_exception_el(env, el);
11256     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11257 
11258     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11259 }
11260 
11261 /*
11262  * If we have triggered a EL state change we can't rely on the
11263  * translator having passed it to us, we need to recompute.
11264  */
11265 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11266 {
11267     int el = arm_current_el(env);
11268     int fp_el = fp_exception_el(env, el);
11269     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11270     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11271 }
11272 
11273 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11274 {
11275     int fp_el = fp_exception_el(env, el);
11276     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11277 
11278     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11279 }
11280 
11281 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11282 {
11283     int fp_el = fp_exception_el(env, el);
11284     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11285 
11286     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11287 }
11288 
11289 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11290 {
11291 #ifdef CONFIG_DEBUG_TCG
11292     CPUARMTBFlags c = env->hflags;
11293     CPUARMTBFlags r = rebuild_hflags_internal(env);
11294 
11295     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11296         fprintf(stderr, "TCG hflags mismatch "
11297                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11298                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11299                 c.flags, c.flags2, r.flags, r.flags2);
11300         abort();
11301     }
11302 #endif
11303 }
11304 
11305 static bool mve_no_pred(CPUARMState *env)
11306 {
11307     /*
11308      * Return true if there is definitely no predication of MVE
11309      * instructions by VPR or LTPSIZE. (Returning false even if there
11310      * isn't any predication is OK; generated code will just be
11311      * a little worse.)
11312      * If the CPU does not implement MVE then this TB flag is always 0.
11313      *
11314      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11315      * logic in gen_update_fp_context() needs to be updated to match.
11316      *
11317      * We do not include the effect of the ECI bits here -- they are
11318      * tracked in other TB flags. This simplifies the logic for
11319      * "when did we emit code that changes the MVE_NO_PRED TB flag
11320      * and thus need to end the TB?".
11321      */
11322     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11323         return false;
11324     }
11325     if (env->v7m.vpr) {
11326         return false;
11327     }
11328     if (env->v7m.ltpsize < 4) {
11329         return false;
11330     }
11331     return true;
11332 }
11333 
11334 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11335                           target_ulong *cs_base, uint32_t *pflags)
11336 {
11337     CPUARMTBFlags flags;
11338 
11339     assert_hflags_rebuild_correctly(env);
11340     flags = env->hflags;
11341 
11342     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11343         *pc = env->pc;
11344         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11345             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11346         }
11347     } else {
11348         *pc = env->regs[15];
11349 
11350         if (arm_feature(env, ARM_FEATURE_M)) {
11351             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11352                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11353                 != env->v7m.secure) {
11354                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11355             }
11356 
11357             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11358                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11359                  (env->v7m.secure &&
11360                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11361                 /*
11362                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11363                  * active FP context; we must create a new FP context before
11364                  * executing any FP insn.
11365                  */
11366                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11367             }
11368 
11369             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11370             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11371                 DP_TBFLAG_M32(flags, LSPACT, 1);
11372             }
11373 
11374             if (mve_no_pred(env)) {
11375                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11376             }
11377         } else {
11378             /*
11379              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11380              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11381              */
11382             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11383                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11384             } else {
11385                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11386                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11387             }
11388             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11389                 DP_TBFLAG_A32(flags, VFPEN, 1);
11390             }
11391         }
11392 
11393         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11394         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11395     }
11396 
11397     /*
11398      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11399      * states defined in the ARM ARM for software singlestep:
11400      *  SS_ACTIVE   PSTATE.SS   State
11401      *     0            x       Inactive (the TB flag for SS is always 0)
11402      *     1            0       Active-pending
11403      *     1            1       Active-not-pending
11404      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11405      */
11406     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11407         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11408     }
11409 
11410     *pflags = flags.flags;
11411     *cs_base = flags.flags2;
11412 }
11413 
11414 #ifdef TARGET_AARCH64
11415 /*
11416  * The manual says that when SVE is enabled and VQ is widened the
11417  * implementation is allowed to zero the previously inaccessible
11418  * portion of the registers.  The corollary to that is that when
11419  * SVE is enabled and VQ is narrowed we are also allowed to zero
11420  * the now inaccessible portion of the registers.
11421  *
11422  * The intent of this is that no predicate bit beyond VQ is ever set.
11423  * Which means that some operations on predicate registers themselves
11424  * may operate on full uint64_t or even unrolled across the maximum
11425  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11426  * may well be cheaper than conditionals to restrict the operation
11427  * to the relevant portion of a uint16_t[16].
11428  */
11429 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11430 {
11431     int i, j;
11432     uint64_t pmask;
11433 
11434     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11435     assert(vq <= env_archcpu(env)->sve_max_vq);
11436 
11437     /* Zap the high bits of the zregs.  */
11438     for (i = 0; i < 32; i++) {
11439         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11440     }
11441 
11442     /* Zap the high bits of the pregs and ffr.  */
11443     pmask = 0;
11444     if (vq & 3) {
11445         pmask = ~(-1ULL << (16 * (vq & 3)));
11446     }
11447     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11448         for (i = 0; i < 17; ++i) {
11449             env->vfp.pregs[i].p[j] &= pmask;
11450         }
11451         pmask = 0;
11452     }
11453 }
11454 
11455 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11456 {
11457     int exc_el;
11458 
11459     if (sm) {
11460         exc_el = sme_exception_el(env, el);
11461     } else {
11462         exc_el = sve_exception_el(env, el);
11463     }
11464     if (exc_el) {
11465         return 0; /* disabled */
11466     }
11467     return sve_vqm1_for_el_sm(env, el, sm);
11468 }
11469 
11470 /*
11471  * Notice a change in SVE vector size when changing EL.
11472  */
11473 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11474                            int new_el, bool el0_a64)
11475 {
11476     ARMCPU *cpu = env_archcpu(env);
11477     int old_len, new_len;
11478     bool old_a64, new_a64, sm;
11479 
11480     /* Nothing to do if no SVE.  */
11481     if (!cpu_isar_feature(aa64_sve, cpu)) {
11482         return;
11483     }
11484 
11485     /* Nothing to do if FP is disabled in either EL.  */
11486     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11487         return;
11488     }
11489 
11490     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11491     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11492 
11493     /*
11494      * Both AArch64.TakeException and AArch64.ExceptionReturn
11495      * invoke ResetSVEState when taking an exception from, or
11496      * returning to, AArch32 state when PSTATE.SM is enabled.
11497      */
11498     sm = FIELD_EX64(env->svcr, SVCR, SM);
11499     if (old_a64 != new_a64 && sm) {
11500         arm_reset_sve_state(env);
11501         return;
11502     }
11503 
11504     /*
11505      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11506      * at ELx, or not available because the EL is in AArch32 state, then
11507      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11508      * has an effective value of 0".
11509      *
11510      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11511      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11512      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11513      * we already have the correct register contents when encountering the
11514      * vq0->vq0 transition between EL0->EL1.
11515      */
11516     old_len = new_len = 0;
11517     if (old_a64) {
11518         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11519     }
11520     if (new_a64) {
11521         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11522     }
11523 
11524     /* When changing vector length, clear inaccessible state.  */
11525     if (new_len < old_len) {
11526         aarch64_sve_narrow_vq(env, new_len + 1);
11527     }
11528 }
11529 #endif
11530