xref: /openbmc/qemu/target/arm/helper.c (revision ad1e84f5)
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 static int alle1_tlbmask(CPUARMState *env)
403 {
404     /*
405      * Note that the 'ALL' scope must invalidate both stage 1 and
406      * stage 2 translations, whereas most other scopes only invalidate
407      * stage 1 translations.
408      */
409     return (ARMMMUIdxBit_E10_1 |
410             ARMMMUIdxBit_E10_1_PAN |
411             ARMMMUIdxBit_E10_0 |
412             ARMMMUIdxBit_Stage2 |
413             ARMMMUIdxBit_Stage2_S);
414 }
415 
416 
417 /* IS variants of TLB operations must affect all cores */
418 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
419                              uint64_t value)
420 {
421     CPUState *cs = env_cpu(env);
422 
423     tlb_flush_all_cpus_synced(cs);
424 }
425 
426 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
427                              uint64_t value)
428 {
429     CPUState *cs = env_cpu(env);
430 
431     tlb_flush_all_cpus_synced(cs);
432 }
433 
434 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
435                              uint64_t value)
436 {
437     CPUState *cs = env_cpu(env);
438 
439     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
440 }
441 
442 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
443                              uint64_t value)
444 {
445     CPUState *cs = env_cpu(env);
446 
447     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
448 }
449 
450 /*
451  * Non-IS variants of TLB operations are upgraded to
452  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
453  * force broadcast of these operations.
454  */
455 static bool tlb_force_broadcast(CPUARMState *env)
456 {
457     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
458 }
459 
460 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
461                           uint64_t value)
462 {
463     /* Invalidate all (TLBIALL) */
464     CPUState *cs = env_cpu(env);
465 
466     if (tlb_force_broadcast(env)) {
467         tlb_flush_all_cpus_synced(cs);
468     } else {
469         tlb_flush(cs);
470     }
471 }
472 
473 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
474                           uint64_t value)
475 {
476     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
477     CPUState *cs = env_cpu(env);
478 
479     value &= TARGET_PAGE_MASK;
480     if (tlb_force_broadcast(env)) {
481         tlb_flush_page_all_cpus_synced(cs, value);
482     } else {
483         tlb_flush_page(cs, value);
484     }
485 }
486 
487 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
488                            uint64_t value)
489 {
490     /* Invalidate by ASID (TLBIASID) */
491     CPUState *cs = env_cpu(env);
492 
493     if (tlb_force_broadcast(env)) {
494         tlb_flush_all_cpus_synced(cs);
495     } else {
496         tlb_flush(cs);
497     }
498 }
499 
500 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
501                            uint64_t value)
502 {
503     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
504     CPUState *cs = env_cpu(env);
505 
506     value &= TARGET_PAGE_MASK;
507     if (tlb_force_broadcast(env)) {
508         tlb_flush_page_all_cpus_synced(cs, value);
509     } else {
510         tlb_flush_page(cs, value);
511     }
512 }
513 
514 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
515                                uint64_t value)
516 {
517     CPUState *cs = env_cpu(env);
518 
519     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
520 }
521 
522 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
523                                   uint64_t value)
524 {
525     CPUState *cs = env_cpu(env);
526 
527     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
528 }
529 
530 
531 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
532                               uint64_t value)
533 {
534     CPUState *cs = env_cpu(env);
535 
536     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
537 }
538 
539 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                                  uint64_t value)
541 {
542     CPUState *cs = env_cpu(env);
543 
544     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
545 }
546 
547 static void tlbimva_hyp_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(cs, pageaddr, ARMMMUIdxBit_E2);
554 }
555 
556 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
557                                  uint64_t value)
558 {
559     CPUState *cs = env_cpu(env);
560     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
561 
562     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
563                                              ARMMMUIdxBit_E2);
564 }
565 
566 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
567                                 uint64_t value)
568 {
569     CPUState *cs = env_cpu(env);
570     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
571 
572     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
573 }
574 
575 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
576                                 uint64_t value)
577 {
578     CPUState *cs = env_cpu(env);
579     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
580 
581     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
582 }
583 
584 static const ARMCPRegInfo cp_reginfo[] = {
585     /* Define the secure and non-secure FCSE identifier CP registers
586      * separately because there is no secure bank in V8 (no _EL3).  This allows
587      * the secure register to be properly reset and migrated. There is also no
588      * v8 EL1 version of the register so the non-secure instance stands alone.
589      */
590     { .name = "FCSEIDR",
591       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
592       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
593       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
594       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
595     { .name = "FCSEIDR_S",
596       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
597       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
598       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
599       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
600     /* Define the secure and non-secure context identifier CP registers
601      * separately because there is no secure bank in V8 (no _EL3).  This allows
602      * the secure register to be properly reset and migrated.  In the
603      * non-secure case, the 32-bit register will have reset and migration
604      * disabled during registration as it is handled by the 64-bit instance.
605      */
606     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
607       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
608       .access = PL1_RW, .accessfn = access_tvm_trvm,
609       .secure = ARM_CP_SECSTATE_NS,
610       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
611       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
612     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
613       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
614       .access = PL1_RW, .accessfn = access_tvm_trvm,
615       .secure = ARM_CP_SECSTATE_S,
616       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
617       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
618 };
619 
620 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
621     /* NB: Some of these registers exist in v8 but with more precise
622      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
623      */
624     /* MMU Domain access control / MPU write buffer control */
625     { .name = "DACR",
626       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
627       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
628       .writefn = dacr_write, .raw_writefn = raw_write,
629       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
630                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
631     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
632      * For v6 and v5, these mappings are overly broad.
633      */
634     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
635       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
636     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
637       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
638     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
639       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
640     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
641       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
642     /* Cache maintenance ops; some of this space may be overridden later. */
643     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
644       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
645       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
646 };
647 
648 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
649     /* Not all pre-v6 cores implemented this WFI, so this is slightly
650      * over-broad.
651      */
652     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
653       .access = PL1_W, .type = ARM_CP_WFI },
654 };
655 
656 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
657     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
658      * is UNPREDICTABLE; we choose to NOP as most implementations do).
659      */
660     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
661       .access = PL1_W, .type = ARM_CP_WFI },
662     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
663      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
664      * OMAPCP will override this space.
665      */
666     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
667       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
668       .resetvalue = 0 },
669     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
670       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
671       .resetvalue = 0 },
672     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
673     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
674       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
675       .resetvalue = 0 },
676     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
677      * implementing it as RAZ means the "debug architecture version" bits
678      * will read as a reserved value, which should cause Linux to not try
679      * to use the debug hardware.
680      */
681     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
682       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
683     /* MMU TLB control. Note that the wildcarding means we cover not just
684      * the unified TLB ops but also the dside/iside/inner-shareable variants.
685      */
686     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
687       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
688       .type = ARM_CP_NO_RAW },
689     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
690       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
691       .type = ARM_CP_NO_RAW },
692     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
693       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
694       .type = ARM_CP_NO_RAW },
695     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
696       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
697       .type = ARM_CP_NO_RAW },
698     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
699       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
700     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
701       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
702 };
703 
704 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
705                         uint64_t value)
706 {
707     uint32_t mask = 0;
708 
709     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
710     if (!arm_feature(env, ARM_FEATURE_V8)) {
711         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
712          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
713          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
714          */
715         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
716             /* VFP coprocessor: cp10 & cp11 [23:20] */
717             mask |= R_CPACR_ASEDIS_MASK |
718                     R_CPACR_D32DIS_MASK |
719                     R_CPACR_CP11_MASK |
720                     R_CPACR_CP10_MASK;
721 
722             if (!arm_feature(env, ARM_FEATURE_NEON)) {
723                 /* ASEDIS [31] bit is RAO/WI */
724                 value |= R_CPACR_ASEDIS_MASK;
725             }
726 
727             /* VFPv3 and upwards with NEON implement 32 double precision
728              * registers (D0-D31).
729              */
730             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
731                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
732                 value |= R_CPACR_D32DIS_MASK;
733             }
734         }
735         value &= mask;
736     }
737 
738     /*
739      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
740      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
741      */
742     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
743         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
744         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
745         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
746     }
747 
748     env->cp15.cpacr_el1 = value;
749 }
750 
751 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
752 {
753     /*
754      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
755      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
756      */
757     uint64_t value = env->cp15.cpacr_el1;
758 
759     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
760         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
761         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
762     }
763     return value;
764 }
765 
766 
767 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
768 {
769     /* Call cpacr_write() so that we reset with the correct RAO bits set
770      * for our CPU features.
771      */
772     cpacr_write(env, ri, 0);
773 }
774 
775 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
776                                    bool isread)
777 {
778     if (arm_feature(env, ARM_FEATURE_V8)) {
779         /* Check if CPACR accesses are to be trapped to EL2 */
780         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
781             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
782             return CP_ACCESS_TRAP_EL2;
783         /* Check if CPACR accesses are to be trapped to EL3 */
784         } else if (arm_current_el(env) < 3 &&
785                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
786             return CP_ACCESS_TRAP_EL3;
787         }
788     }
789 
790     return CP_ACCESS_OK;
791 }
792 
793 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
794                                   bool isread)
795 {
796     /* Check if CPTR accesses are set to trap to EL3 */
797     if (arm_current_el(env) == 2 &&
798         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
799         return CP_ACCESS_TRAP_EL3;
800     }
801 
802     return CP_ACCESS_OK;
803 }
804 
805 static const ARMCPRegInfo v6_cp_reginfo[] = {
806     /* prefetch by MVA in v6, NOP in v7 */
807     { .name = "MVA_prefetch",
808       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
809       .access = PL1_W, .type = ARM_CP_NOP },
810     /* We need to break the TB after ISB to execute self-modifying code
811      * correctly and also to take any pending interrupts immediately.
812      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
813      */
814     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
815       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
816     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
817       .access = PL0_W, .type = ARM_CP_NOP },
818     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
819       .access = PL0_W, .type = ARM_CP_NOP },
820     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
821       .access = PL1_RW, .accessfn = access_tvm_trvm,
822       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
823                              offsetof(CPUARMState, cp15.ifar_ns) },
824       .resetvalue = 0, },
825     /* Watchpoint Fault Address Register : should actually only be present
826      * for 1136, 1176, 11MPCore.
827      */
828     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
829       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
830     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
831       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
832       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
833       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
834 };
835 
836 typedef struct pm_event {
837     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
838     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
839     bool (*supported)(CPUARMState *);
840     /*
841      * Retrieve the current count of the underlying event. The programmed
842      * counters hold a difference from the return value from this function
843      */
844     uint64_t (*get_count)(CPUARMState *);
845     /*
846      * Return how many nanoseconds it will take (at a minimum) for count events
847      * to occur. A negative value indicates the counter will never overflow, or
848      * that the counter has otherwise arranged for the overflow bit to be set
849      * and the PMU interrupt to be raised on overflow.
850      */
851     int64_t (*ns_per_count)(uint64_t);
852 } pm_event;
853 
854 static bool event_always_supported(CPUARMState *env)
855 {
856     return true;
857 }
858 
859 static uint64_t swinc_get_count(CPUARMState *env)
860 {
861     /*
862      * SW_INCR events are written directly to the pmevcntr's by writes to
863      * PMSWINC, so there is no underlying count maintained by the PMU itself
864      */
865     return 0;
866 }
867 
868 static int64_t swinc_ns_per(uint64_t ignored)
869 {
870     return -1;
871 }
872 
873 /*
874  * Return the underlying cycle count for the PMU cycle counters. If we're in
875  * usermode, simply return 0.
876  */
877 static uint64_t cycles_get_count(CPUARMState *env)
878 {
879 #ifndef CONFIG_USER_ONLY
880     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
881                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
882 #else
883     return cpu_get_host_ticks();
884 #endif
885 }
886 
887 #ifndef CONFIG_USER_ONLY
888 static int64_t cycles_ns_per(uint64_t cycles)
889 {
890     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
891 }
892 
893 static bool instructions_supported(CPUARMState *env)
894 {
895     return icount_enabled() == 1; /* Precise instruction counting */
896 }
897 
898 static uint64_t instructions_get_count(CPUARMState *env)
899 {
900     return (uint64_t)icount_get_raw();
901 }
902 
903 static int64_t instructions_ns_per(uint64_t icount)
904 {
905     return icount_to_ns((int64_t)icount);
906 }
907 #endif
908 
909 static bool pmuv3p1_events_supported(CPUARMState *env)
910 {
911     /* For events which are supported in any v8.1 PMU */
912     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
913 }
914 
915 static bool pmuv3p4_events_supported(CPUARMState *env)
916 {
917     /* For events which are supported in any v8.1 PMU */
918     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
919 }
920 
921 static uint64_t zero_event_get_count(CPUARMState *env)
922 {
923     /* For events which on QEMU never fire, so their count is always zero */
924     return 0;
925 }
926 
927 static int64_t zero_event_ns_per(uint64_t cycles)
928 {
929     /* An event which never fires can never overflow */
930     return -1;
931 }
932 
933 static const pm_event pm_events[] = {
934     { .number = 0x000, /* SW_INCR */
935       .supported = event_always_supported,
936       .get_count = swinc_get_count,
937       .ns_per_count = swinc_ns_per,
938     },
939 #ifndef CONFIG_USER_ONLY
940     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
941       .supported = instructions_supported,
942       .get_count = instructions_get_count,
943       .ns_per_count = instructions_ns_per,
944     },
945     { .number = 0x011, /* CPU_CYCLES, Cycle */
946       .supported = event_always_supported,
947       .get_count = cycles_get_count,
948       .ns_per_count = cycles_ns_per,
949     },
950 #endif
951     { .number = 0x023, /* STALL_FRONTEND */
952       .supported = pmuv3p1_events_supported,
953       .get_count = zero_event_get_count,
954       .ns_per_count = zero_event_ns_per,
955     },
956     { .number = 0x024, /* STALL_BACKEND */
957       .supported = pmuv3p1_events_supported,
958       .get_count = zero_event_get_count,
959       .ns_per_count = zero_event_ns_per,
960     },
961     { .number = 0x03c, /* STALL */
962       .supported = pmuv3p4_events_supported,
963       .get_count = zero_event_get_count,
964       .ns_per_count = zero_event_ns_per,
965     },
966 };
967 
968 /*
969  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
970  * events (i.e. the statistical profiling extension), this implementation
971  * should first be updated to something sparse instead of the current
972  * supported_event_map[] array.
973  */
974 #define MAX_EVENT_ID 0x3c
975 #define UNSUPPORTED_EVENT UINT16_MAX
976 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
977 
978 /*
979  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
980  * of ARM event numbers to indices in our pm_events array.
981  *
982  * Note: Events in the 0x40XX range are not currently supported.
983  */
984 void pmu_init(ARMCPU *cpu)
985 {
986     unsigned int i;
987 
988     /*
989      * Empty supported_event_map and cpu->pmceid[01] before adding supported
990      * events to them
991      */
992     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
993         supported_event_map[i] = UNSUPPORTED_EVENT;
994     }
995     cpu->pmceid0 = 0;
996     cpu->pmceid1 = 0;
997 
998     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
999         const pm_event *cnt = &pm_events[i];
1000         assert(cnt->number <= MAX_EVENT_ID);
1001         /* We do not currently support events in the 0x40xx range */
1002         assert(cnt->number <= 0x3f);
1003 
1004         if (cnt->supported(&cpu->env)) {
1005             supported_event_map[cnt->number] = i;
1006             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1007             if (cnt->number & 0x20) {
1008                 cpu->pmceid1 |= event_mask;
1009             } else {
1010                 cpu->pmceid0 |= event_mask;
1011             }
1012         }
1013     }
1014 }
1015 
1016 /*
1017  * Check at runtime whether a PMU event is supported for the current machine
1018  */
1019 static bool event_supported(uint16_t number)
1020 {
1021     if (number > MAX_EVENT_ID) {
1022         return false;
1023     }
1024     return supported_event_map[number] != UNSUPPORTED_EVENT;
1025 }
1026 
1027 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1028                                    bool isread)
1029 {
1030     /* Performance monitor registers user accessibility is controlled
1031      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1032      * trapping to EL2 or EL3 for other accesses.
1033      */
1034     int el = arm_current_el(env);
1035     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1036 
1037     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1038         return CP_ACCESS_TRAP;
1039     }
1040     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1041         return CP_ACCESS_TRAP_EL2;
1042     }
1043     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1044         return CP_ACCESS_TRAP_EL3;
1045     }
1046 
1047     return CP_ACCESS_OK;
1048 }
1049 
1050 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1051                                            const ARMCPRegInfo *ri,
1052                                            bool isread)
1053 {
1054     /* ER: event counter read trap control */
1055     if (arm_feature(env, ARM_FEATURE_V8)
1056         && arm_current_el(env) == 0
1057         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1058         && isread) {
1059         return CP_ACCESS_OK;
1060     }
1061 
1062     return pmreg_access(env, ri, isread);
1063 }
1064 
1065 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1066                                          const ARMCPRegInfo *ri,
1067                                          bool isread)
1068 {
1069     /* SW: software increment write trap control */
1070     if (arm_feature(env, ARM_FEATURE_V8)
1071         && arm_current_el(env) == 0
1072         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1073         && !isread) {
1074         return CP_ACCESS_OK;
1075     }
1076 
1077     return pmreg_access(env, ri, isread);
1078 }
1079 
1080 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1081                                         const ARMCPRegInfo *ri,
1082                                         bool isread)
1083 {
1084     /* ER: event counter read trap control */
1085     if (arm_feature(env, ARM_FEATURE_V8)
1086         && arm_current_el(env) == 0
1087         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1088         return CP_ACCESS_OK;
1089     }
1090 
1091     return pmreg_access(env, ri, isread);
1092 }
1093 
1094 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1095                                          const ARMCPRegInfo *ri,
1096                                          bool isread)
1097 {
1098     /* CR: cycle counter read trap control */
1099     if (arm_feature(env, ARM_FEATURE_V8)
1100         && arm_current_el(env) == 0
1101         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1102         && isread) {
1103         return CP_ACCESS_OK;
1104     }
1105 
1106     return pmreg_access(env, ri, isread);
1107 }
1108 
1109 /*
1110  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1111  * We use these to decide whether we need to wrap a write to MDCR_EL2
1112  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1113  */
1114 #define MDCR_EL2_PMU_ENABLE_BITS \
1115     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1116 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1117 
1118 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1119  * the current EL, security state, and register configuration.
1120  */
1121 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1122 {
1123     uint64_t filter;
1124     bool e, p, u, nsk, nsu, nsh, m;
1125     bool enabled, prohibited = false, filtered;
1126     bool secure = arm_is_secure(env);
1127     int el = arm_current_el(env);
1128     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1129     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1130 
1131     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1132         return false;
1133     }
1134 
1135     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1136             (counter < hpmn || counter == 31)) {
1137         e = env->cp15.c9_pmcr & PMCRE;
1138     } else {
1139         e = mdcr_el2 & MDCR_HPME;
1140     }
1141     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1142 
1143     /* Is event counting prohibited? */
1144     if (el == 2 && (counter < hpmn || counter == 31)) {
1145         prohibited = mdcr_el2 & MDCR_HPMD;
1146     }
1147     if (secure) {
1148         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1149     }
1150 
1151     if (counter == 31) {
1152         /*
1153          * The cycle counter defaults to running. PMCR.DP says "disable
1154          * the cycle counter when event counting is prohibited".
1155          * Some MDCR bits disable the cycle counter specifically.
1156          */
1157         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1158         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1159             if (secure) {
1160                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1161             }
1162             if (el == 2) {
1163                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1164             }
1165         }
1166     }
1167 
1168     if (counter == 31) {
1169         filter = env->cp15.pmccfiltr_el0;
1170     } else {
1171         filter = env->cp15.c14_pmevtyper[counter];
1172     }
1173 
1174     p   = filter & PMXEVTYPER_P;
1175     u   = filter & PMXEVTYPER_U;
1176     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1177     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1178     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1179     m   = arm_el_is_aa64(env, 1) &&
1180               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1181 
1182     if (el == 0) {
1183         filtered = secure ? u : u != nsu;
1184     } else if (el == 1) {
1185         filtered = secure ? p : p != nsk;
1186     } else if (el == 2) {
1187         filtered = !nsh;
1188     } else { /* EL3 */
1189         filtered = m != p;
1190     }
1191 
1192     if (counter != 31) {
1193         /*
1194          * If not checking PMCCNTR, ensure the counter is setup to an event we
1195          * support
1196          */
1197         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1198         if (!event_supported(event)) {
1199             return false;
1200         }
1201     }
1202 
1203     return enabled && !prohibited && !filtered;
1204 }
1205 
1206 static void pmu_update_irq(CPUARMState *env)
1207 {
1208     ARMCPU *cpu = env_archcpu(env);
1209     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1210             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1211 }
1212 
1213 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1214 {
1215     /*
1216      * Return true if the clock divider is enabled and the cycle counter
1217      * is supposed to tick only once every 64 clock cycles. This is
1218      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1219      * (64-bit) cycle counter PMCR.D has no effect.
1220      */
1221     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1222 }
1223 
1224 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1225 {
1226     /* Return true if the specified event counter is configured to be 64 bit */
1227 
1228     /* This isn't intended to be used with the cycle counter */
1229     assert(counter < 31);
1230 
1231     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1232         return false;
1233     }
1234 
1235     if (arm_feature(env, ARM_FEATURE_EL2)) {
1236         /*
1237          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1238          * current security state, so we don't use arm_mdcr_el2_eff() here.
1239          */
1240         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1241         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1242 
1243         if (hpmn != 0 && counter >= hpmn) {
1244             return hlp;
1245         }
1246     }
1247     return env->cp15.c9_pmcr & PMCRLP;
1248 }
1249 
1250 /*
1251  * Ensure c15_ccnt is the guest-visible count so that operations such as
1252  * enabling/disabling the counter or filtering, modifying the count itself,
1253  * etc. can be done logically. This is essentially a no-op if the counter is
1254  * not enabled at the time of the call.
1255  */
1256 static void pmccntr_op_start(CPUARMState *env)
1257 {
1258     uint64_t cycles = cycles_get_count(env);
1259 
1260     if (pmu_counter_enabled(env, 31)) {
1261         uint64_t eff_cycles = cycles;
1262         if (pmccntr_clockdiv_enabled(env)) {
1263             eff_cycles /= 64;
1264         }
1265 
1266         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1267 
1268         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1269                                  1ull << 63 : 1ull << 31;
1270         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1271             env->cp15.c9_pmovsr |= (1ULL << 31);
1272             pmu_update_irq(env);
1273         }
1274 
1275         env->cp15.c15_ccnt = new_pmccntr;
1276     }
1277     env->cp15.c15_ccnt_delta = cycles;
1278 }
1279 
1280 /*
1281  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1282  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1283  * pmccntr_op_start.
1284  */
1285 static void pmccntr_op_finish(CPUARMState *env)
1286 {
1287     if (pmu_counter_enabled(env, 31)) {
1288 #ifndef CONFIG_USER_ONLY
1289         /* Calculate when the counter will next overflow */
1290         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1291         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1292             remaining_cycles = (uint32_t)remaining_cycles;
1293         }
1294         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1295 
1296         if (overflow_in > 0) {
1297             int64_t overflow_at;
1298 
1299             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1300                                  overflow_in, &overflow_at)) {
1301                 ARMCPU *cpu = env_archcpu(env);
1302                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1303             }
1304         }
1305 #endif
1306 
1307         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1308         if (pmccntr_clockdiv_enabled(env)) {
1309             prev_cycles /= 64;
1310         }
1311         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1312     }
1313 }
1314 
1315 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1316 {
1317 
1318     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1319     uint64_t count = 0;
1320     if (event_supported(event)) {
1321         uint16_t event_idx = supported_event_map[event];
1322         count = pm_events[event_idx].get_count(env);
1323     }
1324 
1325     if (pmu_counter_enabled(env, counter)) {
1326         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1327         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1328             1ULL << 63 : 1ULL << 31;
1329 
1330         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1331             env->cp15.c9_pmovsr |= (1 << counter);
1332             pmu_update_irq(env);
1333         }
1334         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1335     }
1336     env->cp15.c14_pmevcntr_delta[counter] = count;
1337 }
1338 
1339 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1340 {
1341     if (pmu_counter_enabled(env, counter)) {
1342 #ifndef CONFIG_USER_ONLY
1343         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1344         uint16_t event_idx = supported_event_map[event];
1345         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1346         int64_t overflow_in;
1347 
1348         if (!pmevcntr_is_64_bit(env, counter)) {
1349             delta = (uint32_t)delta;
1350         }
1351         overflow_in = pm_events[event_idx].ns_per_count(delta);
1352 
1353         if (overflow_in > 0) {
1354             int64_t overflow_at;
1355 
1356             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1357                                  overflow_in, &overflow_at)) {
1358                 ARMCPU *cpu = env_archcpu(env);
1359                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1360             }
1361         }
1362 #endif
1363 
1364         env->cp15.c14_pmevcntr_delta[counter] -=
1365             env->cp15.c14_pmevcntr[counter];
1366     }
1367 }
1368 
1369 void pmu_op_start(CPUARMState *env)
1370 {
1371     unsigned int i;
1372     pmccntr_op_start(env);
1373     for (i = 0; i < pmu_num_counters(env); i++) {
1374         pmevcntr_op_start(env, i);
1375     }
1376 }
1377 
1378 void pmu_op_finish(CPUARMState *env)
1379 {
1380     unsigned int i;
1381     pmccntr_op_finish(env);
1382     for (i = 0; i < pmu_num_counters(env); i++) {
1383         pmevcntr_op_finish(env, i);
1384     }
1385 }
1386 
1387 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1388 {
1389     pmu_op_start(&cpu->env);
1390 }
1391 
1392 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1393 {
1394     pmu_op_finish(&cpu->env);
1395 }
1396 
1397 void arm_pmu_timer_cb(void *opaque)
1398 {
1399     ARMCPU *cpu = opaque;
1400 
1401     /*
1402      * Update all the counter values based on the current underlying counts,
1403      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1404      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1405      * counter may expire.
1406      */
1407     pmu_op_start(&cpu->env);
1408     pmu_op_finish(&cpu->env);
1409 }
1410 
1411 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1412                        uint64_t value)
1413 {
1414     pmu_op_start(env);
1415 
1416     if (value & PMCRC) {
1417         /* The counter has been reset */
1418         env->cp15.c15_ccnt = 0;
1419     }
1420 
1421     if (value & PMCRP) {
1422         unsigned int i;
1423         for (i = 0; i < pmu_num_counters(env); i++) {
1424             env->cp15.c14_pmevcntr[i] = 0;
1425         }
1426     }
1427 
1428     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1429     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1430 
1431     pmu_op_finish(env);
1432 }
1433 
1434 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1435                           uint64_t value)
1436 {
1437     unsigned int i;
1438     uint64_t overflow_mask, new_pmswinc;
1439 
1440     for (i = 0; i < pmu_num_counters(env); i++) {
1441         /* Increment a counter's count iff: */
1442         if ((value & (1 << i)) && /* counter's bit is set */
1443                 /* counter is enabled and not filtered */
1444                 pmu_counter_enabled(env, i) &&
1445                 /* counter is SW_INCR */
1446                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1447             pmevcntr_op_start(env, i);
1448 
1449             /*
1450              * Detect if this write causes an overflow since we can't predict
1451              * PMSWINC overflows like we can for other events
1452              */
1453             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1454 
1455             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1456                 1ULL << 63 : 1ULL << 31;
1457 
1458             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1459                 env->cp15.c9_pmovsr |= (1 << i);
1460                 pmu_update_irq(env);
1461             }
1462 
1463             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1464 
1465             pmevcntr_op_finish(env, i);
1466         }
1467     }
1468 }
1469 
1470 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1471 {
1472     uint64_t ret;
1473     pmccntr_op_start(env);
1474     ret = env->cp15.c15_ccnt;
1475     pmccntr_op_finish(env);
1476     return ret;
1477 }
1478 
1479 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1480                          uint64_t value)
1481 {
1482     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1483      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1484      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1485      * accessed.
1486      */
1487     env->cp15.c9_pmselr = value & 0x1f;
1488 }
1489 
1490 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1491                         uint64_t value)
1492 {
1493     pmccntr_op_start(env);
1494     env->cp15.c15_ccnt = value;
1495     pmccntr_op_finish(env);
1496 }
1497 
1498 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1499                             uint64_t value)
1500 {
1501     uint64_t cur_val = pmccntr_read(env, NULL);
1502 
1503     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1504 }
1505 
1506 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1507                             uint64_t value)
1508 {
1509     pmccntr_op_start(env);
1510     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1511     pmccntr_op_finish(env);
1512 }
1513 
1514 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1515                             uint64_t value)
1516 {
1517     pmccntr_op_start(env);
1518     /* M is not accessible from AArch32 */
1519     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1520         (value & PMCCFILTR);
1521     pmccntr_op_finish(env);
1522 }
1523 
1524 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1525 {
1526     /* M is not visible in AArch32 */
1527     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1528 }
1529 
1530 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531                             uint64_t value)
1532 {
1533     pmu_op_start(env);
1534     value &= pmu_counter_mask(env);
1535     env->cp15.c9_pmcnten |= value;
1536     pmu_op_finish(env);
1537 }
1538 
1539 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1540                              uint64_t value)
1541 {
1542     pmu_op_start(env);
1543     value &= pmu_counter_mask(env);
1544     env->cp15.c9_pmcnten &= ~value;
1545     pmu_op_finish(env);
1546 }
1547 
1548 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549                          uint64_t value)
1550 {
1551     value &= pmu_counter_mask(env);
1552     env->cp15.c9_pmovsr &= ~value;
1553     pmu_update_irq(env);
1554 }
1555 
1556 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1557                          uint64_t value)
1558 {
1559     value &= pmu_counter_mask(env);
1560     env->cp15.c9_pmovsr |= value;
1561     pmu_update_irq(env);
1562 }
1563 
1564 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1565                              uint64_t value, const uint8_t counter)
1566 {
1567     if (counter == 31) {
1568         pmccfiltr_write(env, ri, value);
1569     } else if (counter < pmu_num_counters(env)) {
1570         pmevcntr_op_start(env, counter);
1571 
1572         /*
1573          * If this counter's event type is changing, store the current
1574          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1575          * pmevcntr_op_finish has the correct baseline when it converts back to
1576          * a delta.
1577          */
1578         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1579             PMXEVTYPER_EVTCOUNT;
1580         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1581         if (old_event != new_event) {
1582             uint64_t count = 0;
1583             if (event_supported(new_event)) {
1584                 uint16_t event_idx = supported_event_map[new_event];
1585                 count = pm_events[event_idx].get_count(env);
1586             }
1587             env->cp15.c14_pmevcntr_delta[counter] = count;
1588         }
1589 
1590         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1591         pmevcntr_op_finish(env, counter);
1592     }
1593     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1594      * PMSELR value is equal to or greater than the number of implemented
1595      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1596      */
1597 }
1598 
1599 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1600                                const uint8_t counter)
1601 {
1602     if (counter == 31) {
1603         return env->cp15.pmccfiltr_el0;
1604     } else if (counter < pmu_num_counters(env)) {
1605         return env->cp15.c14_pmevtyper[counter];
1606     } else {
1607       /*
1608        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1609        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1610        */
1611         return 0;
1612     }
1613 }
1614 
1615 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1616                               uint64_t value)
1617 {
1618     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1619     pmevtyper_write(env, ri, value, counter);
1620 }
1621 
1622 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1623                                uint64_t value)
1624 {
1625     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1626     env->cp15.c14_pmevtyper[counter] = value;
1627 
1628     /*
1629      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1630      * pmu_op_finish calls when loading saved state for a migration. Because
1631      * we're potentially updating the type of event here, the value written to
1632      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1633      * different counter type. Therefore, we need to set this value to the
1634      * current count for the counter type we're writing so that pmu_op_finish
1635      * has the correct count for its calculation.
1636      */
1637     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1638     if (event_supported(event)) {
1639         uint16_t event_idx = supported_event_map[event];
1640         env->cp15.c14_pmevcntr_delta[counter] =
1641             pm_events[event_idx].get_count(env);
1642     }
1643 }
1644 
1645 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1646 {
1647     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1648     return pmevtyper_read(env, ri, counter);
1649 }
1650 
1651 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1652                              uint64_t value)
1653 {
1654     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1655 }
1656 
1657 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1658 {
1659     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1660 }
1661 
1662 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1663                              uint64_t value, uint8_t counter)
1664 {
1665     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1666         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1667         value &= MAKE_64BIT_MASK(0, 32);
1668     }
1669     if (counter < pmu_num_counters(env)) {
1670         pmevcntr_op_start(env, counter);
1671         env->cp15.c14_pmevcntr[counter] = value;
1672         pmevcntr_op_finish(env, counter);
1673     }
1674     /*
1675      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1676      * are CONSTRAINED UNPREDICTABLE.
1677      */
1678 }
1679 
1680 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1681                               uint8_t counter)
1682 {
1683     if (counter < pmu_num_counters(env)) {
1684         uint64_t ret;
1685         pmevcntr_op_start(env, counter);
1686         ret = env->cp15.c14_pmevcntr[counter];
1687         pmevcntr_op_finish(env, counter);
1688         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1689             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1690             ret &= MAKE_64BIT_MASK(0, 32);
1691         }
1692         return ret;
1693     } else {
1694       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1695        * are CONSTRAINED UNPREDICTABLE. */
1696         return 0;
1697     }
1698 }
1699 
1700 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1701                              uint64_t value)
1702 {
1703     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1704     pmevcntr_write(env, ri, value, counter);
1705 }
1706 
1707 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1708 {
1709     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1710     return pmevcntr_read(env, ri, counter);
1711 }
1712 
1713 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1714                              uint64_t value)
1715 {
1716     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1717     assert(counter < pmu_num_counters(env));
1718     env->cp15.c14_pmevcntr[counter] = value;
1719     pmevcntr_write(env, ri, value, counter);
1720 }
1721 
1722 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1723 {
1724     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1725     assert(counter < pmu_num_counters(env));
1726     return env->cp15.c14_pmevcntr[counter];
1727 }
1728 
1729 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1730                              uint64_t value)
1731 {
1732     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1733 }
1734 
1735 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1736 {
1737     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1738 }
1739 
1740 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741                             uint64_t value)
1742 {
1743     if (arm_feature(env, ARM_FEATURE_V8)) {
1744         env->cp15.c9_pmuserenr = value & 0xf;
1745     } else {
1746         env->cp15.c9_pmuserenr = value & 1;
1747     }
1748 }
1749 
1750 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1751                              uint64_t value)
1752 {
1753     /* We have no event counters so only the C bit can be changed */
1754     value &= pmu_counter_mask(env);
1755     env->cp15.c9_pminten |= value;
1756     pmu_update_irq(env);
1757 }
1758 
1759 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1760                              uint64_t value)
1761 {
1762     value &= pmu_counter_mask(env);
1763     env->cp15.c9_pminten &= ~value;
1764     pmu_update_irq(env);
1765 }
1766 
1767 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1768                        uint64_t value)
1769 {
1770     /* Note that even though the AArch64 view of this register has bits
1771      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1772      * architectural requirements for bits which are RES0 only in some
1773      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1774      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1775      */
1776     raw_write(env, ri, value & ~0x1FULL);
1777 }
1778 
1779 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1780 {
1781     /* Begin with base v8.0 state.  */
1782     uint64_t valid_mask = 0x3fff;
1783     ARMCPU *cpu = env_archcpu(env);
1784     uint64_t changed;
1785 
1786     /*
1787      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1788      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1789      * Instead, choose the format based on the mode of EL3.
1790      */
1791     if (arm_el_is_aa64(env, 3)) {
1792         value |= SCR_FW | SCR_AW;      /* RES1 */
1793         valid_mask &= ~SCR_NET;        /* RES0 */
1794 
1795         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1796             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1797             value |= SCR_RW;           /* RAO/WI */
1798         }
1799         if (cpu_isar_feature(aa64_ras, cpu)) {
1800             valid_mask |= SCR_TERR;
1801         }
1802         if (cpu_isar_feature(aa64_lor, cpu)) {
1803             valid_mask |= SCR_TLOR;
1804         }
1805         if (cpu_isar_feature(aa64_pauth, cpu)) {
1806             valid_mask |= SCR_API | SCR_APK;
1807         }
1808         if (cpu_isar_feature(aa64_sel2, cpu)) {
1809             valid_mask |= SCR_EEL2;
1810         }
1811         if (cpu_isar_feature(aa64_mte, cpu)) {
1812             valid_mask |= SCR_ATA;
1813         }
1814         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1815             valid_mask |= SCR_ENSCXT;
1816         }
1817         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1818             valid_mask |= SCR_EASE | SCR_NMEA;
1819         }
1820         if (cpu_isar_feature(aa64_sme, cpu)) {
1821             valid_mask |= SCR_ENTP2;
1822         }
1823     } else {
1824         valid_mask &= ~(SCR_RW | SCR_ST);
1825         if (cpu_isar_feature(aa32_ras, cpu)) {
1826             valid_mask |= SCR_TERR;
1827         }
1828     }
1829 
1830     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1831         valid_mask &= ~SCR_HCE;
1832 
1833         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1834          * supported if EL2 exists. The bit is UNK/SBZP when
1835          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1836          * when EL2 is unavailable.
1837          * On ARMv8, this bit is always available.
1838          */
1839         if (arm_feature(env, ARM_FEATURE_V7) &&
1840             !arm_feature(env, ARM_FEATURE_V8)) {
1841             valid_mask &= ~SCR_SMD;
1842         }
1843     }
1844 
1845     /* Clear all-context RES0 bits.  */
1846     value &= valid_mask;
1847     changed = env->cp15.scr_el3 ^ value;
1848     env->cp15.scr_el3 = value;
1849 
1850     /*
1851      * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1852      * we must invalidate all TLBs below EL3.
1853      */
1854     if (changed & SCR_NS) {
1855         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1856                                            ARMMMUIdxBit_E20_0 |
1857                                            ARMMMUIdxBit_E10_1 |
1858                                            ARMMMUIdxBit_E20_2 |
1859                                            ARMMMUIdxBit_E10_1_PAN |
1860                                            ARMMMUIdxBit_E20_2_PAN |
1861                                            ARMMMUIdxBit_E2));
1862     }
1863 }
1864 
1865 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1866 {
1867     /*
1868      * scr_write will set the RES1 bits on an AArch64-only CPU.
1869      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1870      */
1871     scr_write(env, ri, 0);
1872 }
1873 
1874 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1875                                        const ARMCPRegInfo *ri,
1876                                        bool isread)
1877 {
1878     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1879         return CP_ACCESS_TRAP_EL2;
1880     }
1881 
1882     return CP_ACCESS_OK;
1883 }
1884 
1885 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1886 {
1887     ARMCPU *cpu = env_archcpu(env);
1888 
1889     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1890      * bank
1891      */
1892     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1893                                         ri->secure & ARM_CP_SECSTATE_S);
1894 
1895     return cpu->ccsidr[index];
1896 }
1897 
1898 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1899                          uint64_t value)
1900 {
1901     raw_write(env, ri, value & 0xf);
1902 }
1903 
1904 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1905 {
1906     CPUState *cs = env_cpu(env);
1907     bool el1 = arm_current_el(env) == 1;
1908     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1909     uint64_t ret = 0;
1910 
1911     if (hcr_el2 & HCR_IMO) {
1912         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1913             ret |= CPSR_I;
1914         }
1915     } else {
1916         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1917             ret |= CPSR_I;
1918         }
1919     }
1920 
1921     if (hcr_el2 & HCR_FMO) {
1922         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1923             ret |= CPSR_F;
1924         }
1925     } else {
1926         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1927             ret |= CPSR_F;
1928         }
1929     }
1930 
1931     if (hcr_el2 & HCR_AMO) {
1932         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1933             ret |= CPSR_A;
1934         }
1935     }
1936 
1937     return ret;
1938 }
1939 
1940 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1941                                        bool isread)
1942 {
1943     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1944         return CP_ACCESS_TRAP_EL2;
1945     }
1946 
1947     return CP_ACCESS_OK;
1948 }
1949 
1950 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1951                                        bool isread)
1952 {
1953     if (arm_feature(env, ARM_FEATURE_V8)) {
1954         return access_aa64_tid1(env, ri, isread);
1955     }
1956 
1957     return CP_ACCESS_OK;
1958 }
1959 
1960 static const ARMCPRegInfo v7_cp_reginfo[] = {
1961     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1962     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1963       .access = PL1_W, .type = ARM_CP_NOP },
1964     /* Performance monitors are implementation defined in v7,
1965      * but with an ARM recommended set of registers, which we
1966      * follow.
1967      *
1968      * Performance registers fall into three categories:
1969      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1970      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1971      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1972      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1973      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1974      */
1975     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1976       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1977       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1978       .writefn = pmcntenset_write,
1979       .accessfn = pmreg_access,
1980       .raw_writefn = raw_write },
1981     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1982       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1983       .access = PL0_RW, .accessfn = pmreg_access,
1984       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1985       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1986     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1987       .access = PL0_RW,
1988       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1989       .accessfn = pmreg_access,
1990       .writefn = pmcntenclr_write,
1991       .type = ARM_CP_ALIAS | ARM_CP_IO },
1992     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1993       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1994       .access = PL0_RW, .accessfn = pmreg_access,
1995       .type = ARM_CP_ALIAS | ARM_CP_IO,
1996       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1997       .writefn = pmcntenclr_write },
1998     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1999       .access = PL0_RW, .type = ARM_CP_IO,
2000       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2001       .accessfn = pmreg_access,
2002       .writefn = pmovsr_write,
2003       .raw_writefn = raw_write },
2004     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2005       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2006       .access = PL0_RW, .accessfn = pmreg_access,
2007       .type = ARM_CP_ALIAS | ARM_CP_IO,
2008       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2009       .writefn = pmovsr_write,
2010       .raw_writefn = raw_write },
2011     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2012       .access = PL0_W, .accessfn = pmreg_access_swinc,
2013       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2014       .writefn = pmswinc_write },
2015     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2016       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2017       .access = PL0_W, .accessfn = pmreg_access_swinc,
2018       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2019       .writefn = pmswinc_write },
2020     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2021       .access = PL0_RW, .type = ARM_CP_ALIAS,
2022       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2023       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2024       .raw_writefn = raw_write},
2025     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2026       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2027       .access = PL0_RW, .accessfn = pmreg_access_selr,
2028       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2029       .writefn = pmselr_write, .raw_writefn = raw_write, },
2030     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2031       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2032       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2033       .accessfn = pmreg_access_ccntr },
2034     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2035       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2036       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2037       .type = ARM_CP_IO,
2038       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2039       .readfn = pmccntr_read, .writefn = pmccntr_write,
2040       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2041     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2042       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2043       .access = PL0_RW, .accessfn = pmreg_access,
2044       .type = ARM_CP_ALIAS | ARM_CP_IO,
2045       .resetvalue = 0, },
2046     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2047       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2048       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2049       .access = PL0_RW, .accessfn = pmreg_access,
2050       .type = ARM_CP_IO,
2051       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2052       .resetvalue = 0, },
2053     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2054       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2055       .accessfn = pmreg_access,
2056       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2057     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2058       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2059       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2060       .accessfn = pmreg_access,
2061       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2062     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2063       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2064       .accessfn = pmreg_access_xevcntr,
2065       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2066     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2067       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2068       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2069       .accessfn = pmreg_access_xevcntr,
2070       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2071     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2072       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2073       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2074       .resetvalue = 0,
2075       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2076     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2077       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2078       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2079       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2080       .resetvalue = 0,
2081       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2082     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2083       .access = PL1_RW, .accessfn = access_tpm,
2084       .type = ARM_CP_ALIAS | ARM_CP_IO,
2085       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2086       .resetvalue = 0,
2087       .writefn = pmintenset_write, .raw_writefn = raw_write },
2088     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2089       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2090       .access = PL1_RW, .accessfn = access_tpm,
2091       .type = ARM_CP_IO,
2092       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2093       .writefn = pmintenset_write, .raw_writefn = raw_write,
2094       .resetvalue = 0x0 },
2095     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2096       .access = PL1_RW, .accessfn = access_tpm,
2097       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2098       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2099       .writefn = pmintenclr_write, },
2100     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2101       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2102       .access = PL1_RW, .accessfn = access_tpm,
2103       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2104       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2105       .writefn = pmintenclr_write },
2106     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2107       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2108       .access = PL1_R,
2109       .accessfn = access_aa64_tid2,
2110       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2111     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2112       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2113       .access = PL1_RW,
2114       .accessfn = access_aa64_tid2,
2115       .writefn = csselr_write, .resetvalue = 0,
2116       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2117                              offsetof(CPUARMState, cp15.csselr_ns) } },
2118     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2119      * just RAZ for all cores:
2120      */
2121     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2122       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2123       .access = PL1_R, .type = ARM_CP_CONST,
2124       .accessfn = access_aa64_tid1,
2125       .resetvalue = 0 },
2126     /* Auxiliary fault status registers: these also are IMPDEF, and we
2127      * choose to RAZ/WI for all cores.
2128      */
2129     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2130       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2131       .access = PL1_RW, .accessfn = access_tvm_trvm,
2132       .type = ARM_CP_CONST, .resetvalue = 0 },
2133     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2134       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2135       .access = PL1_RW, .accessfn = access_tvm_trvm,
2136       .type = ARM_CP_CONST, .resetvalue = 0 },
2137     /* MAIR can just read-as-written because we don't implement caches
2138      * and so don't need to care about memory attributes.
2139      */
2140     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2141       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2142       .access = PL1_RW, .accessfn = access_tvm_trvm,
2143       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2144       .resetvalue = 0 },
2145     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2146       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2147       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2148       .resetvalue = 0 },
2149     /* For non-long-descriptor page tables these are PRRR and NMRR;
2150      * regardless they still act as reads-as-written for QEMU.
2151      */
2152      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2153       * allows them to assign the correct fieldoffset based on the endianness
2154       * handled in the field definitions.
2155       */
2156     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2157       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2158       .access = PL1_RW, .accessfn = access_tvm_trvm,
2159       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2160                              offsetof(CPUARMState, cp15.mair0_ns) },
2161       .resetfn = arm_cp_reset_ignore },
2162     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2163       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2164       .access = PL1_RW, .accessfn = access_tvm_trvm,
2165       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2166                              offsetof(CPUARMState, cp15.mair1_ns) },
2167       .resetfn = arm_cp_reset_ignore },
2168     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2169       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2170       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2171     /* 32 bit ITLB invalidates */
2172     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2173       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2174       .writefn = tlbiall_write },
2175     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2176       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2177       .writefn = tlbimva_write },
2178     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2179       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2180       .writefn = tlbiasid_write },
2181     /* 32 bit DTLB invalidates */
2182     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2183       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2184       .writefn = tlbiall_write },
2185     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2186       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2187       .writefn = tlbimva_write },
2188     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2189       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2190       .writefn = tlbiasid_write },
2191     /* 32 bit TLB invalidates */
2192     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2193       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2194       .writefn = tlbiall_write },
2195     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2196       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2197       .writefn = tlbimva_write },
2198     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2199       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2200       .writefn = tlbiasid_write },
2201     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2202       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2203       .writefn = tlbimvaa_write },
2204 };
2205 
2206 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2207     /* 32 bit TLB invalidates, Inner Shareable */
2208     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2209       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2210       .writefn = tlbiall_is_write },
2211     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2212       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2213       .writefn = tlbimva_is_write },
2214     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2215       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2216       .writefn = tlbiasid_is_write },
2217     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2218       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2219       .writefn = tlbimvaa_is_write },
2220 };
2221 
2222 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2223     /* PMOVSSET is not implemented in v7 before v7ve */
2224     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2225       .access = PL0_RW, .accessfn = pmreg_access,
2226       .type = ARM_CP_ALIAS | ARM_CP_IO,
2227       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2228       .writefn = pmovsset_write,
2229       .raw_writefn = raw_write },
2230     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2231       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2232       .access = PL0_RW, .accessfn = pmreg_access,
2233       .type = ARM_CP_ALIAS | ARM_CP_IO,
2234       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2235       .writefn = pmovsset_write,
2236       .raw_writefn = raw_write },
2237 };
2238 
2239 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2240                         uint64_t value)
2241 {
2242     value &= 1;
2243     env->teecr = value;
2244 }
2245 
2246 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2247                                    bool isread)
2248 {
2249     /*
2250      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2251      * at all, so we don't need to check whether we're v8A.
2252      */
2253     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2254         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2255         return CP_ACCESS_TRAP_EL2;
2256     }
2257     return CP_ACCESS_OK;
2258 }
2259 
2260 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2261                                     bool isread)
2262 {
2263     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2264         return CP_ACCESS_TRAP;
2265     }
2266     return teecr_access(env, ri, isread);
2267 }
2268 
2269 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2270     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2271       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2272       .resetvalue = 0,
2273       .writefn = teecr_write, .accessfn = teecr_access },
2274     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2275       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2276       .accessfn = teehbr_access, .resetvalue = 0 },
2277 };
2278 
2279 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2280     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2281       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2282       .access = PL0_RW,
2283       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2284     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2285       .access = PL0_RW,
2286       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2287                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2288       .resetfn = arm_cp_reset_ignore },
2289     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2290       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2291       .access = PL0_R|PL1_W,
2292       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2293       .resetvalue = 0},
2294     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2295       .access = PL0_R|PL1_W,
2296       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2297                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2298       .resetfn = arm_cp_reset_ignore },
2299     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2300       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2301       .access = PL1_RW,
2302       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2303     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2304       .access = PL1_RW,
2305       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2306                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2307       .resetvalue = 0 },
2308 };
2309 
2310 #ifndef CONFIG_USER_ONLY
2311 
2312 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2313                                        bool isread)
2314 {
2315     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2316      * Writable only at the highest implemented exception level.
2317      */
2318     int el = arm_current_el(env);
2319     uint64_t hcr;
2320     uint32_t cntkctl;
2321 
2322     switch (el) {
2323     case 0:
2324         hcr = arm_hcr_el2_eff(env);
2325         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2326             cntkctl = env->cp15.cnthctl_el2;
2327         } else {
2328             cntkctl = env->cp15.c14_cntkctl;
2329         }
2330         if (!extract32(cntkctl, 0, 2)) {
2331             return CP_ACCESS_TRAP;
2332         }
2333         break;
2334     case 1:
2335         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2336             arm_is_secure_below_el3(env)) {
2337             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2338             return CP_ACCESS_TRAP_UNCATEGORIZED;
2339         }
2340         break;
2341     case 2:
2342     case 3:
2343         break;
2344     }
2345 
2346     if (!isread && el < arm_highest_el(env)) {
2347         return CP_ACCESS_TRAP_UNCATEGORIZED;
2348     }
2349 
2350     return CP_ACCESS_OK;
2351 }
2352 
2353 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2354                                         bool isread)
2355 {
2356     unsigned int cur_el = arm_current_el(env);
2357     bool has_el2 = arm_is_el2_enabled(env);
2358     uint64_t hcr = arm_hcr_el2_eff(env);
2359 
2360     switch (cur_el) {
2361     case 0:
2362         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2363         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2364             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2365                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2366         }
2367 
2368         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2369         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2370             return CP_ACCESS_TRAP;
2371         }
2372 
2373         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2374         if (hcr & HCR_E2H) {
2375             if (timeridx == GTIMER_PHYS &&
2376                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2377                 return CP_ACCESS_TRAP_EL2;
2378             }
2379         } else {
2380             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2381             if (has_el2 && timeridx == GTIMER_PHYS &&
2382                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2383                 return CP_ACCESS_TRAP_EL2;
2384             }
2385         }
2386         break;
2387 
2388     case 1:
2389         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2390         if (has_el2 && timeridx == GTIMER_PHYS &&
2391             (hcr & HCR_E2H
2392              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2393              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2394             return CP_ACCESS_TRAP_EL2;
2395         }
2396         break;
2397     }
2398     return CP_ACCESS_OK;
2399 }
2400 
2401 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2402                                       bool isread)
2403 {
2404     unsigned int cur_el = arm_current_el(env);
2405     bool has_el2 = arm_is_el2_enabled(env);
2406     uint64_t hcr = arm_hcr_el2_eff(env);
2407 
2408     switch (cur_el) {
2409     case 0:
2410         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2411             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2412             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2413                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2414         }
2415 
2416         /*
2417          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2418          * EL0 if EL0[PV]TEN is zero.
2419          */
2420         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2421             return CP_ACCESS_TRAP;
2422         }
2423         /* fall through */
2424 
2425     case 1:
2426         if (has_el2 && timeridx == GTIMER_PHYS) {
2427             if (hcr & HCR_E2H) {
2428                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2429                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2430                     return CP_ACCESS_TRAP_EL2;
2431                 }
2432             } else {
2433                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2434                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2435                     return CP_ACCESS_TRAP_EL2;
2436                 }
2437             }
2438         }
2439         break;
2440     }
2441     return CP_ACCESS_OK;
2442 }
2443 
2444 static CPAccessResult gt_pct_access(CPUARMState *env,
2445                                     const ARMCPRegInfo *ri,
2446                                     bool isread)
2447 {
2448     return gt_counter_access(env, GTIMER_PHYS, isread);
2449 }
2450 
2451 static CPAccessResult gt_vct_access(CPUARMState *env,
2452                                     const ARMCPRegInfo *ri,
2453                                     bool isread)
2454 {
2455     return gt_counter_access(env, GTIMER_VIRT, isread);
2456 }
2457 
2458 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2459                                        bool isread)
2460 {
2461     return gt_timer_access(env, GTIMER_PHYS, isread);
2462 }
2463 
2464 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2465                                        bool isread)
2466 {
2467     return gt_timer_access(env, GTIMER_VIRT, isread);
2468 }
2469 
2470 static CPAccessResult gt_stimer_access(CPUARMState *env,
2471                                        const ARMCPRegInfo *ri,
2472                                        bool isread)
2473 {
2474     /* The AArch64 register view of the secure physical timer is
2475      * always accessible from EL3, and configurably accessible from
2476      * Secure EL1.
2477      */
2478     switch (arm_current_el(env)) {
2479     case 1:
2480         if (!arm_is_secure(env)) {
2481             return CP_ACCESS_TRAP;
2482         }
2483         if (!(env->cp15.scr_el3 & SCR_ST)) {
2484             return CP_ACCESS_TRAP_EL3;
2485         }
2486         return CP_ACCESS_OK;
2487     case 0:
2488     case 2:
2489         return CP_ACCESS_TRAP;
2490     case 3:
2491         return CP_ACCESS_OK;
2492     default:
2493         g_assert_not_reached();
2494     }
2495 }
2496 
2497 static uint64_t gt_get_countervalue(CPUARMState *env)
2498 {
2499     ARMCPU *cpu = env_archcpu(env);
2500 
2501     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2502 }
2503 
2504 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2505 {
2506     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2507 
2508     if (gt->ctl & 1) {
2509         /* Timer enabled: calculate and set current ISTATUS, irq, and
2510          * reset timer to when ISTATUS next has to change
2511          */
2512         uint64_t offset = timeridx == GTIMER_VIRT ?
2513                                       cpu->env.cp15.cntvoff_el2 : 0;
2514         uint64_t count = gt_get_countervalue(&cpu->env);
2515         /* Note that this must be unsigned 64 bit arithmetic: */
2516         int istatus = count - offset >= gt->cval;
2517         uint64_t nexttick;
2518         int irqstate;
2519 
2520         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2521 
2522         irqstate = (istatus && !(gt->ctl & 2));
2523         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2524 
2525         if (istatus) {
2526             /* Next transition is when count rolls back over to zero */
2527             nexttick = UINT64_MAX;
2528         } else {
2529             /* Next transition is when we hit cval */
2530             nexttick = gt->cval + offset;
2531         }
2532         /* Note that the desired next expiry time might be beyond the
2533          * signed-64-bit range of a QEMUTimer -- in this case we just
2534          * set the timer for as far in the future as possible. When the
2535          * timer expires we will reset the timer for any remaining period.
2536          */
2537         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2538             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2539         } else {
2540             timer_mod(cpu->gt_timer[timeridx], nexttick);
2541         }
2542         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2543     } else {
2544         /* Timer disabled: ISTATUS and timer output always clear */
2545         gt->ctl &= ~4;
2546         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2547         timer_del(cpu->gt_timer[timeridx]);
2548         trace_arm_gt_recalc_disabled(timeridx);
2549     }
2550 }
2551 
2552 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2553                            int timeridx)
2554 {
2555     ARMCPU *cpu = env_archcpu(env);
2556 
2557     timer_del(cpu->gt_timer[timeridx]);
2558 }
2559 
2560 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2561 {
2562     return gt_get_countervalue(env);
2563 }
2564 
2565 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2566 {
2567     uint64_t hcr;
2568 
2569     switch (arm_current_el(env)) {
2570     case 2:
2571         hcr = arm_hcr_el2_eff(env);
2572         if (hcr & HCR_E2H) {
2573             return 0;
2574         }
2575         break;
2576     case 0:
2577         hcr = arm_hcr_el2_eff(env);
2578         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2579             return 0;
2580         }
2581         break;
2582     }
2583 
2584     return env->cp15.cntvoff_el2;
2585 }
2586 
2587 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2588 {
2589     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2590 }
2591 
2592 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2593                           int timeridx,
2594                           uint64_t value)
2595 {
2596     trace_arm_gt_cval_write(timeridx, value);
2597     env->cp15.c14_timer[timeridx].cval = value;
2598     gt_recalc_timer(env_archcpu(env), timeridx);
2599 }
2600 
2601 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2602                              int timeridx)
2603 {
2604     uint64_t offset = 0;
2605 
2606     switch (timeridx) {
2607     case GTIMER_VIRT:
2608     case GTIMER_HYPVIRT:
2609         offset = gt_virt_cnt_offset(env);
2610         break;
2611     }
2612 
2613     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2614                       (gt_get_countervalue(env) - offset));
2615 }
2616 
2617 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2618                           int timeridx,
2619                           uint64_t value)
2620 {
2621     uint64_t offset = 0;
2622 
2623     switch (timeridx) {
2624     case GTIMER_VIRT:
2625     case GTIMER_HYPVIRT:
2626         offset = gt_virt_cnt_offset(env);
2627         break;
2628     }
2629 
2630     trace_arm_gt_tval_write(timeridx, value);
2631     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2632                                          sextract64(value, 0, 32);
2633     gt_recalc_timer(env_archcpu(env), timeridx);
2634 }
2635 
2636 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2637                          int timeridx,
2638                          uint64_t value)
2639 {
2640     ARMCPU *cpu = env_archcpu(env);
2641     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2642 
2643     trace_arm_gt_ctl_write(timeridx, value);
2644     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2645     if ((oldval ^ value) & 1) {
2646         /* Enable toggled */
2647         gt_recalc_timer(cpu, timeridx);
2648     } else if ((oldval ^ value) & 2) {
2649         /* IMASK toggled: don't need to recalculate,
2650          * just set the interrupt line based on ISTATUS
2651          */
2652         int irqstate = (oldval & 4) && !(value & 2);
2653 
2654         trace_arm_gt_imask_toggle(timeridx, irqstate);
2655         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2656     }
2657 }
2658 
2659 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2660 {
2661     gt_timer_reset(env, ri, GTIMER_PHYS);
2662 }
2663 
2664 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2665                                uint64_t value)
2666 {
2667     gt_cval_write(env, ri, GTIMER_PHYS, value);
2668 }
2669 
2670 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2671 {
2672     return gt_tval_read(env, ri, GTIMER_PHYS);
2673 }
2674 
2675 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2676                                uint64_t value)
2677 {
2678     gt_tval_write(env, ri, GTIMER_PHYS, value);
2679 }
2680 
2681 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2682                               uint64_t value)
2683 {
2684     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2685 }
2686 
2687 static int gt_phys_redir_timeridx(CPUARMState *env)
2688 {
2689     switch (arm_mmu_idx(env)) {
2690     case ARMMMUIdx_E20_0:
2691     case ARMMMUIdx_E20_2:
2692     case ARMMMUIdx_E20_2_PAN:
2693         return GTIMER_HYP;
2694     default:
2695         return GTIMER_PHYS;
2696     }
2697 }
2698 
2699 static int gt_virt_redir_timeridx(CPUARMState *env)
2700 {
2701     switch (arm_mmu_idx(env)) {
2702     case ARMMMUIdx_E20_0:
2703     case ARMMMUIdx_E20_2:
2704     case ARMMMUIdx_E20_2_PAN:
2705         return GTIMER_HYPVIRT;
2706     default:
2707         return GTIMER_VIRT;
2708     }
2709 }
2710 
2711 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2712                                         const ARMCPRegInfo *ri)
2713 {
2714     int timeridx = gt_phys_redir_timeridx(env);
2715     return env->cp15.c14_timer[timeridx].cval;
2716 }
2717 
2718 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719                                      uint64_t value)
2720 {
2721     int timeridx = gt_phys_redir_timeridx(env);
2722     gt_cval_write(env, ri, timeridx, value);
2723 }
2724 
2725 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2726                                         const ARMCPRegInfo *ri)
2727 {
2728     int timeridx = gt_phys_redir_timeridx(env);
2729     return gt_tval_read(env, ri, timeridx);
2730 }
2731 
2732 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2733                                      uint64_t value)
2734 {
2735     int timeridx = gt_phys_redir_timeridx(env);
2736     gt_tval_write(env, ri, timeridx, value);
2737 }
2738 
2739 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2740                                        const ARMCPRegInfo *ri)
2741 {
2742     int timeridx = gt_phys_redir_timeridx(env);
2743     return env->cp15.c14_timer[timeridx].ctl;
2744 }
2745 
2746 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2747                                     uint64_t value)
2748 {
2749     int timeridx = gt_phys_redir_timeridx(env);
2750     gt_ctl_write(env, ri, timeridx, value);
2751 }
2752 
2753 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2754 {
2755     gt_timer_reset(env, ri, GTIMER_VIRT);
2756 }
2757 
2758 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2759                                uint64_t value)
2760 {
2761     gt_cval_write(env, ri, GTIMER_VIRT, value);
2762 }
2763 
2764 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2765 {
2766     return gt_tval_read(env, ri, GTIMER_VIRT);
2767 }
2768 
2769 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770                                uint64_t value)
2771 {
2772     gt_tval_write(env, ri, GTIMER_VIRT, value);
2773 }
2774 
2775 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2776                               uint64_t value)
2777 {
2778     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2779 }
2780 
2781 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782                               uint64_t value)
2783 {
2784     ARMCPU *cpu = env_archcpu(env);
2785 
2786     trace_arm_gt_cntvoff_write(value);
2787     raw_write(env, ri, value);
2788     gt_recalc_timer(cpu, GTIMER_VIRT);
2789 }
2790 
2791 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2792                                         const ARMCPRegInfo *ri)
2793 {
2794     int timeridx = gt_virt_redir_timeridx(env);
2795     return env->cp15.c14_timer[timeridx].cval;
2796 }
2797 
2798 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799                                      uint64_t value)
2800 {
2801     int timeridx = gt_virt_redir_timeridx(env);
2802     gt_cval_write(env, ri, timeridx, value);
2803 }
2804 
2805 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2806                                         const ARMCPRegInfo *ri)
2807 {
2808     int timeridx = gt_virt_redir_timeridx(env);
2809     return gt_tval_read(env, ri, timeridx);
2810 }
2811 
2812 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2813                                      uint64_t value)
2814 {
2815     int timeridx = gt_virt_redir_timeridx(env);
2816     gt_tval_write(env, ri, timeridx, value);
2817 }
2818 
2819 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2820                                        const ARMCPRegInfo *ri)
2821 {
2822     int timeridx = gt_virt_redir_timeridx(env);
2823     return env->cp15.c14_timer[timeridx].ctl;
2824 }
2825 
2826 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827                                     uint64_t value)
2828 {
2829     int timeridx = gt_virt_redir_timeridx(env);
2830     gt_ctl_write(env, ri, timeridx, value);
2831 }
2832 
2833 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2834 {
2835     gt_timer_reset(env, ri, GTIMER_HYP);
2836 }
2837 
2838 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839                               uint64_t value)
2840 {
2841     gt_cval_write(env, ri, GTIMER_HYP, value);
2842 }
2843 
2844 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2845 {
2846     return gt_tval_read(env, ri, GTIMER_HYP);
2847 }
2848 
2849 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850                               uint64_t value)
2851 {
2852     gt_tval_write(env, ri, GTIMER_HYP, value);
2853 }
2854 
2855 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856                               uint64_t value)
2857 {
2858     gt_ctl_write(env, ri, GTIMER_HYP, value);
2859 }
2860 
2861 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2862 {
2863     gt_timer_reset(env, ri, GTIMER_SEC);
2864 }
2865 
2866 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867                               uint64_t value)
2868 {
2869     gt_cval_write(env, ri, GTIMER_SEC, value);
2870 }
2871 
2872 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2873 {
2874     return gt_tval_read(env, ri, GTIMER_SEC);
2875 }
2876 
2877 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2878                               uint64_t value)
2879 {
2880     gt_tval_write(env, ri, GTIMER_SEC, value);
2881 }
2882 
2883 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2884                               uint64_t value)
2885 {
2886     gt_ctl_write(env, ri, GTIMER_SEC, value);
2887 }
2888 
2889 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2890 {
2891     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2892 }
2893 
2894 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2895                              uint64_t value)
2896 {
2897     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2898 }
2899 
2900 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2901 {
2902     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2903 }
2904 
2905 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2906                              uint64_t value)
2907 {
2908     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2909 }
2910 
2911 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2912                             uint64_t value)
2913 {
2914     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2915 }
2916 
2917 void arm_gt_ptimer_cb(void *opaque)
2918 {
2919     ARMCPU *cpu = opaque;
2920 
2921     gt_recalc_timer(cpu, GTIMER_PHYS);
2922 }
2923 
2924 void arm_gt_vtimer_cb(void *opaque)
2925 {
2926     ARMCPU *cpu = opaque;
2927 
2928     gt_recalc_timer(cpu, GTIMER_VIRT);
2929 }
2930 
2931 void arm_gt_htimer_cb(void *opaque)
2932 {
2933     ARMCPU *cpu = opaque;
2934 
2935     gt_recalc_timer(cpu, GTIMER_HYP);
2936 }
2937 
2938 void arm_gt_stimer_cb(void *opaque)
2939 {
2940     ARMCPU *cpu = opaque;
2941 
2942     gt_recalc_timer(cpu, GTIMER_SEC);
2943 }
2944 
2945 void arm_gt_hvtimer_cb(void *opaque)
2946 {
2947     ARMCPU *cpu = opaque;
2948 
2949     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2950 }
2951 
2952 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2953 {
2954     ARMCPU *cpu = env_archcpu(env);
2955 
2956     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2957 }
2958 
2959 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2960     /* Note that CNTFRQ is purely reads-as-written for the benefit
2961      * of software; writing it doesn't actually change the timer frequency.
2962      * Our reset value matches the fixed frequency we implement the timer at.
2963      */
2964     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2965       .type = ARM_CP_ALIAS,
2966       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2967       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2968     },
2969     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2970       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2971       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2972       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2973       .resetfn = arm_gt_cntfrq_reset,
2974     },
2975     /* overall control: mostly access permissions */
2976     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2977       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2978       .access = PL1_RW,
2979       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2980       .resetvalue = 0,
2981     },
2982     /* per-timer control */
2983     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2984       .secure = ARM_CP_SECSTATE_NS,
2985       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2986       .accessfn = gt_ptimer_access,
2987       .fieldoffset = offsetoflow32(CPUARMState,
2988                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2989       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2990       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2991     },
2992     { .name = "CNTP_CTL_S",
2993       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2994       .secure = ARM_CP_SECSTATE_S,
2995       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2996       .accessfn = gt_ptimer_access,
2997       .fieldoffset = offsetoflow32(CPUARMState,
2998                                    cp15.c14_timer[GTIMER_SEC].ctl),
2999       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3000     },
3001     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3002       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3003       .type = ARM_CP_IO, .access = PL0_RW,
3004       .accessfn = gt_ptimer_access,
3005       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3006       .resetvalue = 0,
3007       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3008       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3009     },
3010     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3011       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3012       .accessfn = gt_vtimer_access,
3013       .fieldoffset = offsetoflow32(CPUARMState,
3014                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3015       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3016       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3017     },
3018     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3019       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3020       .type = ARM_CP_IO, .access = PL0_RW,
3021       .accessfn = gt_vtimer_access,
3022       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3023       .resetvalue = 0,
3024       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3025       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3026     },
3027     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3028     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3029       .secure = ARM_CP_SECSTATE_NS,
3030       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3031       .accessfn = gt_ptimer_access,
3032       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3033     },
3034     { .name = "CNTP_TVAL_S",
3035       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3036       .secure = ARM_CP_SECSTATE_S,
3037       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3038       .accessfn = gt_ptimer_access,
3039       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3040     },
3041     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3042       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3043       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3044       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3045       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3046     },
3047     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3048       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3049       .accessfn = gt_vtimer_access,
3050       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3051     },
3052     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3053       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3054       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3055       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3056       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3057     },
3058     /* The counter itself */
3059     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3060       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3061       .accessfn = gt_pct_access,
3062       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3063     },
3064     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3065       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3066       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3067       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3068     },
3069     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3070       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3071       .accessfn = gt_vct_access,
3072       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3073     },
3074     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3075       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3076       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3077       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3078     },
3079     /* Comparison value, indicating when the timer goes off */
3080     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3081       .secure = ARM_CP_SECSTATE_NS,
3082       .access = PL0_RW,
3083       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3084       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3085       .accessfn = gt_ptimer_access,
3086       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3087       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3088     },
3089     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3090       .secure = ARM_CP_SECSTATE_S,
3091       .access = PL0_RW,
3092       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3093       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3094       .accessfn = gt_ptimer_access,
3095       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3096     },
3097     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3098       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3099       .access = PL0_RW,
3100       .type = ARM_CP_IO,
3101       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3102       .resetvalue = 0, .accessfn = gt_ptimer_access,
3103       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3104       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3105     },
3106     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3107       .access = PL0_RW,
3108       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3109       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3110       .accessfn = gt_vtimer_access,
3111       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3112       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3113     },
3114     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3115       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3116       .access = PL0_RW,
3117       .type = ARM_CP_IO,
3118       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3119       .resetvalue = 0, .accessfn = gt_vtimer_access,
3120       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3121       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3122     },
3123     /* Secure timer -- this is actually restricted to only EL3
3124      * and configurably Secure-EL1 via the accessfn.
3125      */
3126     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3127       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3128       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3129       .accessfn = gt_stimer_access,
3130       .readfn = gt_sec_tval_read,
3131       .writefn = gt_sec_tval_write,
3132       .resetfn = gt_sec_timer_reset,
3133     },
3134     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3135       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3136       .type = ARM_CP_IO, .access = PL1_RW,
3137       .accessfn = gt_stimer_access,
3138       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3139       .resetvalue = 0,
3140       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3141     },
3142     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3143       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3144       .type = ARM_CP_IO, .access = PL1_RW,
3145       .accessfn = gt_stimer_access,
3146       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3147       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3148     },
3149 };
3150 
3151 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3152                                  bool isread)
3153 {
3154     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3155         return CP_ACCESS_TRAP;
3156     }
3157     return CP_ACCESS_OK;
3158 }
3159 
3160 #else
3161 
3162 /* In user-mode most of the generic timer registers are inaccessible
3163  * however modern kernels (4.12+) allow access to cntvct_el0
3164  */
3165 
3166 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3167 {
3168     ARMCPU *cpu = env_archcpu(env);
3169 
3170     /* Currently we have no support for QEMUTimer in linux-user so we
3171      * can't call gt_get_countervalue(env), instead we directly
3172      * call the lower level functions.
3173      */
3174     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3175 }
3176 
3177 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3178     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3179       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3180       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3181       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3182       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3183     },
3184     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3185       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3186       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3187       .readfn = gt_virt_cnt_read,
3188     },
3189 };
3190 
3191 #endif
3192 
3193 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3194 {
3195     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3196         raw_write(env, ri, value);
3197     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3198         raw_write(env, ri, value & 0xfffff6ff);
3199     } else {
3200         raw_write(env, ri, value & 0xfffff1ff);
3201     }
3202 }
3203 
3204 #ifndef CONFIG_USER_ONLY
3205 /* get_phys_addr() isn't present for user-mode-only targets */
3206 
3207 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3208                                  bool isread)
3209 {
3210     if (ri->opc2 & 4) {
3211         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3212          * Secure EL1 (which can only happen if EL3 is AArch64).
3213          * They are simply UNDEF if executed from NS EL1.
3214          * They function normally from EL2 or EL3.
3215          */
3216         if (arm_current_el(env) == 1) {
3217             if (arm_is_secure_below_el3(env)) {
3218                 if (env->cp15.scr_el3 & SCR_EEL2) {
3219                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3220                 }
3221                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3222             }
3223             return CP_ACCESS_TRAP_UNCATEGORIZED;
3224         }
3225     }
3226     return CP_ACCESS_OK;
3227 }
3228 
3229 #ifdef CONFIG_TCG
3230 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3231                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3232                              bool is_secure)
3233 {
3234     bool ret;
3235     uint64_t par64;
3236     bool format64 = false;
3237     ARMMMUFaultInfo fi = {};
3238     GetPhysAddrResult res = {};
3239 
3240     ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3241                                     is_secure, &res, &fi);
3242 
3243     /*
3244      * ATS operations only do S1 or S1+S2 translations, so we never
3245      * have to deal with the ARMCacheAttrs format for S2 only.
3246      */
3247     assert(!res.cacheattrs.is_s2_format);
3248 
3249     if (ret) {
3250         /*
3251          * Some kinds of translation fault must cause exceptions rather
3252          * than being reported in the PAR.
3253          */
3254         int current_el = arm_current_el(env);
3255         int target_el;
3256         uint32_t syn, fsr, fsc;
3257         bool take_exc = false;
3258 
3259         if (fi.s1ptw && current_el == 1
3260             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3261             /*
3262              * Synchronous stage 2 fault on an access made as part of the
3263              * translation table walk for AT S1E0* or AT S1E1* insn
3264              * executed from NS EL1. If this is a synchronous external abort
3265              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3266              * to EL3. Otherwise the fault is taken as an exception to EL2,
3267              * and HPFAR_EL2 holds the faulting IPA.
3268              */
3269             if (fi.type == ARMFault_SyncExternalOnWalk &&
3270                 (env->cp15.scr_el3 & SCR_EA)) {
3271                 target_el = 3;
3272             } else {
3273                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3274                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3275                     env->cp15.hpfar_el2 |= HPFAR_NS;
3276                 }
3277                 target_el = 2;
3278             }
3279             take_exc = true;
3280         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3281             /*
3282              * Synchronous external aborts during a translation table walk
3283              * are taken as Data Abort exceptions.
3284              */
3285             if (fi.stage2) {
3286                 if (current_el == 3) {
3287                     target_el = 3;
3288                 } else {
3289                     target_el = 2;
3290                 }
3291             } else {
3292                 target_el = exception_target_el(env);
3293             }
3294             take_exc = true;
3295         }
3296 
3297         if (take_exc) {
3298             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3299             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3300                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3301                 fsr = arm_fi_to_lfsc(&fi);
3302                 fsc = extract32(fsr, 0, 6);
3303             } else {
3304                 fsr = arm_fi_to_sfsc(&fi);
3305                 fsc = 0x3f;
3306             }
3307             /*
3308              * Report exception with ESR indicating a fault due to a
3309              * translation table walk for a cache maintenance instruction.
3310              */
3311             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3312                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3313             env->exception.vaddress = value;
3314             env->exception.fsr = fsr;
3315             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3316         }
3317     }
3318 
3319     if (is_a64(env)) {
3320         format64 = true;
3321     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3322         /*
3323          * ATS1Cxx:
3324          * * TTBCR.EAE determines whether the result is returned using the
3325          *   32-bit or the 64-bit PAR format
3326          * * Instructions executed in Hyp mode always use the 64bit format
3327          *
3328          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3329          * * The Non-secure TTBCR.EAE bit is set to 1
3330          * * The implementation includes EL2, and the value of HCR.VM is 1
3331          *
3332          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3333          *
3334          * ATS1Hx always uses the 64bit format.
3335          */
3336         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3337 
3338         if (arm_feature(env, ARM_FEATURE_EL2)) {
3339             if (mmu_idx == ARMMMUIdx_E10_0 ||
3340                 mmu_idx == ARMMMUIdx_E10_1 ||
3341                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3342                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3343             } else {
3344                 format64 |= arm_current_el(env) == 2;
3345             }
3346         }
3347     }
3348 
3349     if (format64) {
3350         /* Create a 64-bit PAR */
3351         par64 = (1 << 11); /* LPAE bit always set */
3352         if (!ret) {
3353             par64 |= res.f.phys_addr & ~0xfffULL;
3354             if (!res.f.attrs.secure) {
3355                 par64 |= (1 << 9); /* NS */
3356             }
3357             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3358             par64 |= res.cacheattrs.shareability << 7; /* SH */
3359         } else {
3360             uint32_t fsr = arm_fi_to_lfsc(&fi);
3361 
3362             par64 |= 1; /* F */
3363             par64 |= (fsr & 0x3f) << 1; /* FS */
3364             if (fi.stage2) {
3365                 par64 |= (1 << 9); /* S */
3366             }
3367             if (fi.s1ptw) {
3368                 par64 |= (1 << 8); /* PTW */
3369             }
3370         }
3371     } else {
3372         /* fsr is a DFSR/IFSR value for the short descriptor
3373          * translation table format (with WnR always clear).
3374          * Convert it to a 32-bit PAR.
3375          */
3376         if (!ret) {
3377             /* We do not set any attribute bits in the PAR */
3378             if (res.f.lg_page_size == 24
3379                 && arm_feature(env, ARM_FEATURE_V7)) {
3380                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3381             } else {
3382                 par64 = res.f.phys_addr & 0xfffff000;
3383             }
3384             if (!res.f.attrs.secure) {
3385                 par64 |= (1 << 9); /* NS */
3386             }
3387         } else {
3388             uint32_t fsr = arm_fi_to_sfsc(&fi);
3389 
3390             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3391                     ((fsr & 0xf) << 1) | 1;
3392         }
3393     }
3394     return par64;
3395 }
3396 #endif /* CONFIG_TCG */
3397 
3398 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3399 {
3400 #ifdef CONFIG_TCG
3401     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3402     uint64_t par64;
3403     ARMMMUIdx mmu_idx;
3404     int el = arm_current_el(env);
3405     bool secure = arm_is_secure_below_el3(env);
3406 
3407     switch (ri->opc2 & 6) {
3408     case 0:
3409         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3410         switch (el) {
3411         case 3:
3412             mmu_idx = ARMMMUIdx_E3;
3413             secure = true;
3414             break;
3415         case 2:
3416             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3417             /* fall through */
3418         case 1:
3419             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3420                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3421             } else {
3422                 mmu_idx = ARMMMUIdx_Stage1_E1;
3423             }
3424             break;
3425         default:
3426             g_assert_not_reached();
3427         }
3428         break;
3429     case 2:
3430         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3431         switch (el) {
3432         case 3:
3433             mmu_idx = ARMMMUIdx_E10_0;
3434             secure = true;
3435             break;
3436         case 2:
3437             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3438             mmu_idx = ARMMMUIdx_Stage1_E0;
3439             break;
3440         case 1:
3441             mmu_idx = ARMMMUIdx_Stage1_E0;
3442             break;
3443         default:
3444             g_assert_not_reached();
3445         }
3446         break;
3447     case 4:
3448         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3449         mmu_idx = ARMMMUIdx_E10_1;
3450         secure = false;
3451         break;
3452     case 6:
3453         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3454         mmu_idx = ARMMMUIdx_E10_0;
3455         secure = false;
3456         break;
3457     default:
3458         g_assert_not_reached();
3459     }
3460 
3461     par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
3462 
3463     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3464 #else
3465     /* Handled by hardware accelerator. */
3466     g_assert_not_reached();
3467 #endif /* CONFIG_TCG */
3468 }
3469 
3470 static void ats1h_write(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     uint64_t par64;
3476 
3477     /* There is no SecureEL2 for AArch32. */
3478     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
3479 
3480     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3481 #else
3482     /* Handled by hardware accelerator. */
3483     g_assert_not_reached();
3484 #endif /* CONFIG_TCG */
3485 }
3486 
3487 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3488                                      bool isread)
3489 {
3490     if (arm_current_el(env) == 3 &&
3491         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3492         return CP_ACCESS_TRAP;
3493     }
3494     return CP_ACCESS_OK;
3495 }
3496 
3497 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3498                         uint64_t value)
3499 {
3500 #ifdef CONFIG_TCG
3501     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3502     ARMMMUIdx mmu_idx;
3503     int secure = arm_is_secure_below_el3(env);
3504 
3505     switch (ri->opc2 & 6) {
3506     case 0:
3507         switch (ri->opc1) {
3508         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3509             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3510                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3511             } else {
3512                 mmu_idx = ARMMMUIdx_Stage1_E1;
3513             }
3514             break;
3515         case 4: /* AT S1E2R, AT S1E2W */
3516             mmu_idx = ARMMMUIdx_E2;
3517             break;
3518         case 6: /* AT S1E3R, AT S1E3W */
3519             mmu_idx = ARMMMUIdx_E3;
3520             secure = true;
3521             break;
3522         default:
3523             g_assert_not_reached();
3524         }
3525         break;
3526     case 2: /* AT S1E0R, AT S1E0W */
3527         mmu_idx = ARMMMUIdx_Stage1_E0;
3528         break;
3529     case 4: /* AT S12E1R, AT S12E1W */
3530         mmu_idx = ARMMMUIdx_E10_1;
3531         break;
3532     case 6: /* AT S12E0R, AT S12E0W */
3533         mmu_idx = ARMMMUIdx_E10_0;
3534         break;
3535     default:
3536         g_assert_not_reached();
3537     }
3538 
3539     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3540                                        mmu_idx, secure);
3541 #else
3542     /* Handled by hardware accelerator. */
3543     g_assert_not_reached();
3544 #endif /* CONFIG_TCG */
3545 }
3546 #endif
3547 
3548 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3549     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3550       .access = PL1_RW, .resetvalue = 0,
3551       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3552                              offsetoflow32(CPUARMState, cp15.par_ns) },
3553       .writefn = par_write },
3554 #ifndef CONFIG_USER_ONLY
3555     /* This underdecoding is safe because the reginfo is NO_RAW. */
3556     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3557       .access = PL1_W, .accessfn = ats_access,
3558       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3559 #endif
3560 };
3561 
3562 /* Return basic MPU access permission bits.  */
3563 static uint32_t simple_mpu_ap_bits(uint32_t val)
3564 {
3565     uint32_t ret;
3566     uint32_t mask;
3567     int i;
3568     ret = 0;
3569     mask = 3;
3570     for (i = 0; i < 16; i += 2) {
3571         ret |= (val >> i) & mask;
3572         mask <<= 2;
3573     }
3574     return ret;
3575 }
3576 
3577 /* Pad basic MPU access permission bits to extended format.  */
3578 static uint32_t extended_mpu_ap_bits(uint32_t val)
3579 {
3580     uint32_t ret;
3581     uint32_t mask;
3582     int i;
3583     ret = 0;
3584     mask = 3;
3585     for (i = 0; i < 16; i += 2) {
3586         ret |= (val & mask) << i;
3587         mask <<= 2;
3588     }
3589     return ret;
3590 }
3591 
3592 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3593                                  uint64_t value)
3594 {
3595     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3596 }
3597 
3598 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3599 {
3600     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3601 }
3602 
3603 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3604                                  uint64_t value)
3605 {
3606     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3607 }
3608 
3609 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3610 {
3611     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3612 }
3613 
3614 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3615 {
3616     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3617 
3618     if (!u32p) {
3619         return 0;
3620     }
3621 
3622     u32p += env->pmsav7.rnr[M_REG_NS];
3623     return *u32p;
3624 }
3625 
3626 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3627                          uint64_t value)
3628 {
3629     ARMCPU *cpu = env_archcpu(env);
3630     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3631 
3632     if (!u32p) {
3633         return;
3634     }
3635 
3636     u32p += env->pmsav7.rnr[M_REG_NS];
3637     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3638     *u32p = value;
3639 }
3640 
3641 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3642                               uint64_t value)
3643 {
3644     ARMCPU *cpu = env_archcpu(env);
3645     uint32_t nrgs = cpu->pmsav7_dregion;
3646 
3647     if (value >= nrgs) {
3648         qemu_log_mask(LOG_GUEST_ERROR,
3649                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3650                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3651         return;
3652     }
3653 
3654     raw_write(env, ri, value);
3655 }
3656 
3657 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3658     /* Reset for all these registers is handled in arm_cpu_reset(),
3659      * because the PMSAv7 is also used by M-profile CPUs, which do
3660      * not register cpregs but still need the state to be reset.
3661      */
3662     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3663       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3664       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3665       .readfn = pmsav7_read, .writefn = pmsav7_write,
3666       .resetfn = arm_cp_reset_ignore },
3667     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3668       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3669       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3670       .readfn = pmsav7_read, .writefn = pmsav7_write,
3671       .resetfn = arm_cp_reset_ignore },
3672     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3673       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3674       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3675       .readfn = pmsav7_read, .writefn = pmsav7_write,
3676       .resetfn = arm_cp_reset_ignore },
3677     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3678       .access = PL1_RW,
3679       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3680       .writefn = pmsav7_rgnr_write,
3681       .resetfn = arm_cp_reset_ignore },
3682 };
3683 
3684 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3685     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3686       .access = PL1_RW, .type = ARM_CP_ALIAS,
3687       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3688       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3689     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3690       .access = PL1_RW, .type = ARM_CP_ALIAS,
3691       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3692       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3693     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3694       .access = PL1_RW,
3695       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3696       .resetvalue = 0, },
3697     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3698       .access = PL1_RW,
3699       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3700       .resetvalue = 0, },
3701     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3702       .access = PL1_RW,
3703       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3704     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3705       .access = PL1_RW,
3706       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3707     /* Protection region base and size registers */
3708     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3709       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3710       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3711     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3712       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3713       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3714     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3715       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3716       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3717     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3718       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3719       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3720     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3721       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3722       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3723     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3724       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3725       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3726     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3727       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3728       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3729     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3730       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3731       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3732 };
3733 
3734 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3735                              uint64_t value)
3736 {
3737     ARMCPU *cpu = env_archcpu(env);
3738 
3739     if (!arm_feature(env, ARM_FEATURE_V8)) {
3740         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3741             /*
3742              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3743              * using Long-descriptor translation table format
3744              */
3745             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3746         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3747             /*
3748              * In an implementation that includes the Security Extensions
3749              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3750              * Short-descriptor translation table format.
3751              */
3752             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3753         } else {
3754             value &= TTBCR_N;
3755         }
3756     }
3757 
3758     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3759         /* With LPAE the TTBCR could result in a change of ASID
3760          * via the TTBCR.A1 bit, so do a TLB flush.
3761          */
3762         tlb_flush(CPU(cpu));
3763     }
3764     raw_write(env, ri, value);
3765 }
3766 
3767 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3768                                uint64_t value)
3769 {
3770     ARMCPU *cpu = env_archcpu(env);
3771 
3772     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3773     tlb_flush(CPU(cpu));
3774     raw_write(env, ri, value);
3775 }
3776 
3777 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3778                             uint64_t value)
3779 {
3780     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3781     if (cpreg_field_is_64bit(ri) &&
3782         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3783         ARMCPU *cpu = env_archcpu(env);
3784         tlb_flush(CPU(cpu));
3785     }
3786     raw_write(env, ri, value);
3787 }
3788 
3789 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3790                                     uint64_t value)
3791 {
3792     /*
3793      * If we are running with E2&0 regime, then an ASID is active.
3794      * Flush if that might be changing.  Note we're not checking
3795      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3796      * holds the active ASID, only checking the field that might.
3797      */
3798     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3799         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3800         uint16_t mask = ARMMMUIdxBit_E20_2 |
3801                         ARMMMUIdxBit_E20_2_PAN |
3802                         ARMMMUIdxBit_E20_0;
3803         tlb_flush_by_mmuidx(env_cpu(env), mask);
3804     }
3805     raw_write(env, ri, value);
3806 }
3807 
3808 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3809                         uint64_t value)
3810 {
3811     ARMCPU *cpu = env_archcpu(env);
3812     CPUState *cs = CPU(cpu);
3813 
3814     /*
3815      * A change in VMID to the stage2 page table (Stage2) invalidates
3816      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
3817      */
3818     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3819         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
3820     }
3821     raw_write(env, ri, value);
3822 }
3823 
3824 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3825     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3826       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3827       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3828                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3829     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3830       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3831       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3832                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3833     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3834       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3835       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3836                              offsetof(CPUARMState, cp15.dfar_ns) } },
3837     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3838       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3839       .access = PL1_RW, .accessfn = access_tvm_trvm,
3840       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3841       .resetvalue = 0, },
3842 };
3843 
3844 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3845     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3846       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3847       .access = PL1_RW, .accessfn = access_tvm_trvm,
3848       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3849     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3850       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3851       .access = PL1_RW, .accessfn = access_tvm_trvm,
3852       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3853       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3854                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3855     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3856       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3857       .access = PL1_RW, .accessfn = access_tvm_trvm,
3858       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3859       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3860                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3861     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3862       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3863       .access = PL1_RW, .accessfn = access_tvm_trvm,
3864       .writefn = vmsa_tcr_el12_write,
3865       .raw_writefn = raw_write,
3866       .resetvalue = 0,
3867       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3868     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3869       .access = PL1_RW, .accessfn = access_tvm_trvm,
3870       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3871       .raw_writefn = raw_write,
3872       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3873                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3874 };
3875 
3876 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3877  * qemu tlbs nor adjusting cached masks.
3878  */
3879 static const ARMCPRegInfo ttbcr2_reginfo = {
3880     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3881     .access = PL1_RW, .accessfn = access_tvm_trvm,
3882     .type = ARM_CP_ALIAS,
3883     .bank_fieldoffsets = {
3884         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3885         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3886     },
3887 };
3888 
3889 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3890                                 uint64_t value)
3891 {
3892     env->cp15.c15_ticonfig = value & 0xe7;
3893     /* The OS_TYPE bit in this register changes the reported CPUID! */
3894     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3895         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3896 }
3897 
3898 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3899                                 uint64_t value)
3900 {
3901     env->cp15.c15_threadid = value & 0xffff;
3902 }
3903 
3904 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3905                            uint64_t value)
3906 {
3907     /* Wait-for-interrupt (deprecated) */
3908     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3909 }
3910 
3911 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3912                                   uint64_t value)
3913 {
3914     /* On OMAP there are registers indicating the max/min index of dcache lines
3915      * containing a dirty line; cache flush operations have to reset these.
3916      */
3917     env->cp15.c15_i_max = 0x000;
3918     env->cp15.c15_i_min = 0xff0;
3919 }
3920 
3921 static const ARMCPRegInfo omap_cp_reginfo[] = {
3922     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3923       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3924       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3925       .resetvalue = 0, },
3926     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3927       .access = PL1_RW, .type = ARM_CP_NOP },
3928     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3929       .access = PL1_RW,
3930       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3931       .writefn = omap_ticonfig_write },
3932     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3933       .access = PL1_RW,
3934       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3935     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3936       .access = PL1_RW, .resetvalue = 0xff0,
3937       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3938     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3939       .access = PL1_RW,
3940       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3941       .writefn = omap_threadid_write },
3942     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3943       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3944       .type = ARM_CP_NO_RAW,
3945       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3946     /* TODO: Peripheral port remap register:
3947      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3948      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3949      * when MMU is off.
3950      */
3951     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3952       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3953       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3954       .writefn = omap_cachemaint_write },
3955     { .name = "C9", .cp = 15, .crn = 9,
3956       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3957       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3958 };
3959 
3960 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3961                               uint64_t value)
3962 {
3963     env->cp15.c15_cpar = value & 0x3fff;
3964 }
3965 
3966 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3967     { .name = "XSCALE_CPAR",
3968       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3969       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3970       .writefn = xscale_cpar_write, },
3971     { .name = "XSCALE_AUXCR",
3972       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3973       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3974       .resetvalue = 0, },
3975     /* XScale specific cache-lockdown: since we have no cache we NOP these
3976      * and hope the guest does not really rely on cache behaviour.
3977      */
3978     { .name = "XSCALE_LOCK_ICACHE_LINE",
3979       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3980       .access = PL1_W, .type = ARM_CP_NOP },
3981     { .name = "XSCALE_UNLOCK_ICACHE",
3982       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3983       .access = PL1_W, .type = ARM_CP_NOP },
3984     { .name = "XSCALE_DCACHE_LOCK",
3985       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3986       .access = PL1_RW, .type = ARM_CP_NOP },
3987     { .name = "XSCALE_UNLOCK_DCACHE",
3988       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3989       .access = PL1_W, .type = ARM_CP_NOP },
3990 };
3991 
3992 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3993     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3994      * implementation of this implementation-defined space.
3995      * Ideally this should eventually disappear in favour of actually
3996      * implementing the correct behaviour for all cores.
3997      */
3998     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3999       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4000       .access = PL1_RW,
4001       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4002       .resetvalue = 0 },
4003 };
4004 
4005 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4006     /* Cache status: RAZ because we have no cache so it's always clean */
4007     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4008       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4009       .resetvalue = 0 },
4010 };
4011 
4012 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4013     /* We never have a block transfer operation in progress */
4014     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4015       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4016       .resetvalue = 0 },
4017     /* The cache ops themselves: these all NOP for QEMU */
4018     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4019       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4020     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4021       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4022     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4023       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4024     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4025       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4026     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4027       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4028     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4029       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4030 };
4031 
4032 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4033     /* The cache test-and-clean instructions always return (1 << 30)
4034      * to indicate that there are no dirty cache lines.
4035      */
4036     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4037       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4038       .resetvalue = (1 << 30) },
4039     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4040       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4041       .resetvalue = (1 << 30) },
4042 };
4043 
4044 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4045     /* Ignore ReadBuffer accesses */
4046     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4047       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4048       .access = PL1_RW, .resetvalue = 0,
4049       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4050 };
4051 
4052 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4053 {
4054     unsigned int cur_el = arm_current_el(env);
4055 
4056     if (arm_is_el2_enabled(env) && cur_el == 1) {
4057         return env->cp15.vpidr_el2;
4058     }
4059     return raw_read(env, ri);
4060 }
4061 
4062 static uint64_t mpidr_read_val(CPUARMState *env)
4063 {
4064     ARMCPU *cpu = env_archcpu(env);
4065     uint64_t mpidr = cpu->mp_affinity;
4066 
4067     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4068         mpidr |= (1U << 31);
4069         /* Cores which are uniprocessor (non-coherent)
4070          * but still implement the MP extensions set
4071          * bit 30. (For instance, Cortex-R5).
4072          */
4073         if (cpu->mp_is_up) {
4074             mpidr |= (1u << 30);
4075         }
4076     }
4077     return mpidr;
4078 }
4079 
4080 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4081 {
4082     unsigned int cur_el = arm_current_el(env);
4083 
4084     if (arm_is_el2_enabled(env) && cur_el == 1) {
4085         return env->cp15.vmpidr_el2;
4086     }
4087     return mpidr_read_val(env);
4088 }
4089 
4090 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4091     /* NOP AMAIR0/1 */
4092     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4093       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4094       .access = PL1_RW, .accessfn = access_tvm_trvm,
4095       .type = ARM_CP_CONST, .resetvalue = 0 },
4096     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4097     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4098       .access = PL1_RW, .accessfn = access_tvm_trvm,
4099       .type = ARM_CP_CONST, .resetvalue = 0 },
4100     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4101       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4102       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4103                              offsetof(CPUARMState, cp15.par_ns)} },
4104     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4105       .access = PL1_RW, .accessfn = access_tvm_trvm,
4106       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4107       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4108                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4109       .writefn = vmsa_ttbr_write, },
4110     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4111       .access = PL1_RW, .accessfn = access_tvm_trvm,
4112       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4113       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4114                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4115       .writefn = vmsa_ttbr_write, },
4116 };
4117 
4118 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4119 {
4120     return vfp_get_fpcr(env);
4121 }
4122 
4123 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4124                             uint64_t value)
4125 {
4126     vfp_set_fpcr(env, value);
4127 }
4128 
4129 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4130 {
4131     return vfp_get_fpsr(env);
4132 }
4133 
4134 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4135                             uint64_t value)
4136 {
4137     vfp_set_fpsr(env, value);
4138 }
4139 
4140 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4141                                        bool isread)
4142 {
4143     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4144         return CP_ACCESS_TRAP;
4145     }
4146     return CP_ACCESS_OK;
4147 }
4148 
4149 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4150                             uint64_t value)
4151 {
4152     env->daif = value & PSTATE_DAIF;
4153 }
4154 
4155 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4156 {
4157     return env->pstate & PSTATE_PAN;
4158 }
4159 
4160 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4161                            uint64_t value)
4162 {
4163     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4164 }
4165 
4166 static const ARMCPRegInfo pan_reginfo = {
4167     .name = "PAN", .state = ARM_CP_STATE_AA64,
4168     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4169     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4170     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4171 };
4172 
4173 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4174 {
4175     return env->pstate & PSTATE_UAO;
4176 }
4177 
4178 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4179                            uint64_t value)
4180 {
4181     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4182 }
4183 
4184 static const ARMCPRegInfo uao_reginfo = {
4185     .name = "UAO", .state = ARM_CP_STATE_AA64,
4186     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4187     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4188     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4189 };
4190 
4191 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4192 {
4193     return env->pstate & PSTATE_DIT;
4194 }
4195 
4196 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4197                            uint64_t value)
4198 {
4199     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4200 }
4201 
4202 static const ARMCPRegInfo dit_reginfo = {
4203     .name = "DIT", .state = ARM_CP_STATE_AA64,
4204     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4205     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4206     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4207 };
4208 
4209 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4210 {
4211     return env->pstate & PSTATE_SSBS;
4212 }
4213 
4214 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4215                            uint64_t value)
4216 {
4217     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4218 }
4219 
4220 static const ARMCPRegInfo ssbs_reginfo = {
4221     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4222     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4223     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4224     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4225 };
4226 
4227 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4228                                               const ARMCPRegInfo *ri,
4229                                               bool isread)
4230 {
4231     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4232     switch (arm_current_el(env)) {
4233     case 0:
4234         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4235         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4236             return CP_ACCESS_TRAP;
4237         }
4238         /* fall through */
4239     case 1:
4240         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4241         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4242             return CP_ACCESS_TRAP_EL2;
4243         }
4244         break;
4245     }
4246     return CP_ACCESS_OK;
4247 }
4248 
4249 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4250                                               const ARMCPRegInfo *ri,
4251                                               bool isread)
4252 {
4253     /* Cache invalidate/clean to Point of Unification... */
4254     switch (arm_current_el(env)) {
4255     case 0:
4256         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4257         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4258             return CP_ACCESS_TRAP;
4259         }
4260         /* fall through */
4261     case 1:
4262         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4263         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4264             return CP_ACCESS_TRAP_EL2;
4265         }
4266         break;
4267     }
4268     return CP_ACCESS_OK;
4269 }
4270 
4271 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4272  * Page D4-1736 (DDI0487A.b)
4273  */
4274 
4275 static int vae1_tlbmask(CPUARMState *env)
4276 {
4277     uint64_t hcr = arm_hcr_el2_eff(env);
4278     uint16_t mask;
4279 
4280     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4281         mask = ARMMMUIdxBit_E20_2 |
4282                ARMMMUIdxBit_E20_2_PAN |
4283                ARMMMUIdxBit_E20_0;
4284     } else {
4285         mask = ARMMMUIdxBit_E10_1 |
4286                ARMMMUIdxBit_E10_1_PAN |
4287                ARMMMUIdxBit_E10_0;
4288     }
4289     return mask;
4290 }
4291 
4292 /* Return 56 if TBI is enabled, 64 otherwise. */
4293 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4294                               uint64_t addr)
4295 {
4296     uint64_t tcr = regime_tcr(env, mmu_idx);
4297     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4298     int select = extract64(addr, 55, 1);
4299 
4300     return (tbi >> select) & 1 ? 56 : 64;
4301 }
4302 
4303 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4304 {
4305     uint64_t hcr = arm_hcr_el2_eff(env);
4306     ARMMMUIdx mmu_idx;
4307 
4308     /* Only the regime of the mmu_idx below is significant. */
4309     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4310         mmu_idx = ARMMMUIdx_E20_0;
4311     } else {
4312         mmu_idx = ARMMMUIdx_E10_0;
4313     }
4314 
4315     return tlbbits_for_regime(env, mmu_idx, addr);
4316 }
4317 
4318 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4319                                       uint64_t value)
4320 {
4321     CPUState *cs = env_cpu(env);
4322     int mask = vae1_tlbmask(env);
4323 
4324     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4325 }
4326 
4327 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4328                                     uint64_t value)
4329 {
4330     CPUState *cs = env_cpu(env);
4331     int mask = vae1_tlbmask(env);
4332 
4333     if (tlb_force_broadcast(env)) {
4334         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4335     } else {
4336         tlb_flush_by_mmuidx(cs, mask);
4337     }
4338 }
4339 
4340 static int e2_tlbmask(CPUARMState *env)
4341 {
4342     return (ARMMMUIdxBit_E20_0 |
4343             ARMMMUIdxBit_E20_2 |
4344             ARMMMUIdxBit_E20_2_PAN |
4345             ARMMMUIdxBit_E2);
4346 }
4347 
4348 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4349                                   uint64_t value)
4350 {
4351     CPUState *cs = env_cpu(env);
4352     int mask = alle1_tlbmask(env);
4353 
4354     tlb_flush_by_mmuidx(cs, mask);
4355 }
4356 
4357 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4358                                   uint64_t value)
4359 {
4360     CPUState *cs = env_cpu(env);
4361     int mask = e2_tlbmask(env);
4362 
4363     tlb_flush_by_mmuidx(cs, mask);
4364 }
4365 
4366 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367                                   uint64_t value)
4368 {
4369     ARMCPU *cpu = env_archcpu(env);
4370     CPUState *cs = CPU(cpu);
4371 
4372     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4373 }
4374 
4375 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4376                                     uint64_t value)
4377 {
4378     CPUState *cs = env_cpu(env);
4379     int mask = alle1_tlbmask(env);
4380 
4381     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4382 }
4383 
4384 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4385                                     uint64_t value)
4386 {
4387     CPUState *cs = env_cpu(env);
4388     int mask = e2_tlbmask(env);
4389 
4390     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4391 }
4392 
4393 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4394                                     uint64_t value)
4395 {
4396     CPUState *cs = env_cpu(env);
4397 
4398     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4399 }
4400 
4401 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4402                                  uint64_t value)
4403 {
4404     /* Invalidate by VA, EL2
4405      * Currently handles both VAE2 and VALE2, since we don't support
4406      * flush-last-level-only.
4407      */
4408     CPUState *cs = env_cpu(env);
4409     int mask = e2_tlbmask(env);
4410     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4411 
4412     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4413 }
4414 
4415 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4416                                  uint64_t value)
4417 {
4418     /* Invalidate by VA, EL3
4419      * Currently handles both VAE3 and VALE3, since we don't support
4420      * flush-last-level-only.
4421      */
4422     ARMCPU *cpu = env_archcpu(env);
4423     CPUState *cs = CPU(cpu);
4424     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4425 
4426     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4427 }
4428 
4429 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4430                                    uint64_t value)
4431 {
4432     CPUState *cs = env_cpu(env);
4433     int mask = vae1_tlbmask(env);
4434     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4435     int bits = vae1_tlbbits(env, pageaddr);
4436 
4437     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4438 }
4439 
4440 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4441                                  uint64_t value)
4442 {
4443     /* Invalidate by VA, EL1&0 (AArch64 version).
4444      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4445      * since we don't support flush-for-specific-ASID-only or
4446      * flush-last-level-only.
4447      */
4448     CPUState *cs = env_cpu(env);
4449     int mask = vae1_tlbmask(env);
4450     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4451     int bits = vae1_tlbbits(env, pageaddr);
4452 
4453     if (tlb_force_broadcast(env)) {
4454         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4455     } else {
4456         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4457     }
4458 }
4459 
4460 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4461                                    uint64_t value)
4462 {
4463     CPUState *cs = env_cpu(env);
4464     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4465     int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
4466 
4467     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4468                                                   ARMMMUIdxBit_E2, bits);
4469 }
4470 
4471 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4472                                    uint64_t value)
4473 {
4474     CPUState *cs = env_cpu(env);
4475     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4476     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4477 
4478     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4479                                                   ARMMMUIdxBit_E3, bits);
4480 }
4481 
4482 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4483 {
4484     /*
4485      * The MSB of value is the NS field, which only applies if SEL2
4486      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4487      */
4488     return (value >= 0
4489             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4490             && arm_is_secure_below_el3(env)
4491             ? ARMMMUIdxBit_Stage2_S
4492             : ARMMMUIdxBit_Stage2);
4493 }
4494 
4495 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4496                                     uint64_t value)
4497 {
4498     CPUState *cs = env_cpu(env);
4499     int mask = ipas2e1_tlbmask(env, value);
4500     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4501 
4502     if (tlb_force_broadcast(env)) {
4503         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4504     } else {
4505         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4506     }
4507 }
4508 
4509 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4510                                       uint64_t value)
4511 {
4512     CPUState *cs = env_cpu(env);
4513     int mask = ipas2e1_tlbmask(env, value);
4514     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4515 
4516     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4517 }
4518 
4519 #ifdef TARGET_AARCH64
4520 typedef struct {
4521     uint64_t base;
4522     uint64_t length;
4523 } TLBIRange;
4524 
4525 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4526 {
4527     /*
4528      * Note that the TLBI range TG field encoding differs from both
4529      * TG0 and TG1 encodings.
4530      */
4531     switch (tg) {
4532     case 1:
4533         return Gran4K;
4534     case 2:
4535         return Gran16K;
4536     case 3:
4537         return Gran64K;
4538     default:
4539         return GranInvalid;
4540     }
4541 }
4542 
4543 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4544                                      uint64_t value)
4545 {
4546     unsigned int page_size_granule, page_shift, num, scale, exponent;
4547     /* Extract one bit to represent the va selector in use. */
4548     uint64_t select = sextract64(value, 36, 1);
4549     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4550     TLBIRange ret = { };
4551     ARMGranuleSize gran;
4552 
4553     page_size_granule = extract64(value, 46, 2);
4554     gran = tlbi_range_tg_to_gran_size(page_size_granule);
4555 
4556     /* The granule encoded in value must match the granule in use. */
4557     if (gran != param.gran) {
4558         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4559                       page_size_granule);
4560         return ret;
4561     }
4562 
4563     page_shift = arm_granule_bits(gran);
4564     num = extract64(value, 39, 5);
4565     scale = extract64(value, 44, 2);
4566     exponent = (5 * scale) + 1;
4567 
4568     ret.length = (num + 1) << (exponent + page_shift);
4569 
4570     if (param.select) {
4571         ret.base = sextract64(value, 0, 37);
4572     } else {
4573         ret.base = extract64(value, 0, 37);
4574     }
4575     if (param.ds) {
4576         /*
4577          * With DS=1, BaseADDR is always shifted 16 so that it is able
4578          * to address all 52 va bits.  The input address is perforce
4579          * aligned on a 64k boundary regardless of translation granule.
4580          */
4581         page_shift = 16;
4582     }
4583     ret.base <<= page_shift;
4584 
4585     return ret;
4586 }
4587 
4588 static void do_rvae_write(CPUARMState *env, uint64_t value,
4589                           int idxmap, bool synced)
4590 {
4591     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4592     TLBIRange range;
4593     int bits;
4594 
4595     range = tlbi_aa64_get_range(env, one_idx, value);
4596     bits = tlbbits_for_regime(env, one_idx, range.base);
4597 
4598     if (synced) {
4599         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4600                                                   range.base,
4601                                                   range.length,
4602                                                   idxmap,
4603                                                   bits);
4604     } else {
4605         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4606                                   range.length, idxmap, bits);
4607     }
4608 }
4609 
4610 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4611                                   const ARMCPRegInfo *ri,
4612                                   uint64_t value)
4613 {
4614     /*
4615      * Invalidate by VA range, EL1&0.
4616      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4617      * since we don't support flush-for-specific-ASID-only or
4618      * flush-last-level-only.
4619      */
4620 
4621     do_rvae_write(env, value, vae1_tlbmask(env),
4622                   tlb_force_broadcast(env));
4623 }
4624 
4625 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4626                                     const ARMCPRegInfo *ri,
4627                                     uint64_t value)
4628 {
4629     /*
4630      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4631      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4632      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4633      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4634      * shareable specific flushes.
4635      */
4636 
4637     do_rvae_write(env, value, vae1_tlbmask(env), true);
4638 }
4639 
4640 static int vae2_tlbmask(CPUARMState *env)
4641 {
4642     return ARMMMUIdxBit_E2;
4643 }
4644 
4645 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4646                                   const ARMCPRegInfo *ri,
4647                                   uint64_t value)
4648 {
4649     /*
4650      * Invalidate by VA range, EL2.
4651      * Currently handles all of RVAE2 and RVALE2,
4652      * since we don't support flush-for-specific-ASID-only or
4653      * flush-last-level-only.
4654      */
4655 
4656     do_rvae_write(env, value, vae2_tlbmask(env),
4657                   tlb_force_broadcast(env));
4658 
4659 
4660 }
4661 
4662 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4663                                     const ARMCPRegInfo *ri,
4664                                     uint64_t value)
4665 {
4666     /*
4667      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4668      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4669      * since we don't support flush-for-specific-ASID-only,
4670      * flush-last-level-only or inner/outer shareable specific flushes.
4671      */
4672 
4673     do_rvae_write(env, value, vae2_tlbmask(env), true);
4674 
4675 }
4676 
4677 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4678                                   const ARMCPRegInfo *ri,
4679                                   uint64_t value)
4680 {
4681     /*
4682      * Invalidate by VA range, EL3.
4683      * Currently handles all of RVAE3 and RVALE3,
4684      * since we don't support flush-for-specific-ASID-only or
4685      * flush-last-level-only.
4686      */
4687 
4688     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
4689 }
4690 
4691 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4692                                     const ARMCPRegInfo *ri,
4693                                     uint64_t value)
4694 {
4695     /*
4696      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4697      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4698      * since we don't support flush-for-specific-ASID-only,
4699      * flush-last-level-only or inner/outer specific flushes.
4700      */
4701 
4702     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
4703 }
4704 
4705 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4706                                      uint64_t value)
4707 {
4708     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
4709                   tlb_force_broadcast(env));
4710 }
4711 
4712 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
4713                                        const ARMCPRegInfo *ri,
4714                                        uint64_t value)
4715 {
4716     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
4717 }
4718 #endif
4719 
4720 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4721                                       bool isread)
4722 {
4723     int cur_el = arm_current_el(env);
4724 
4725     if (cur_el < 2) {
4726         uint64_t hcr = arm_hcr_el2_eff(env);
4727 
4728         if (cur_el == 0) {
4729             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4730                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4731                     return CP_ACCESS_TRAP_EL2;
4732                 }
4733             } else {
4734                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4735                     return CP_ACCESS_TRAP;
4736                 }
4737                 if (hcr & HCR_TDZ) {
4738                     return CP_ACCESS_TRAP_EL2;
4739                 }
4740             }
4741         } else if (hcr & HCR_TDZ) {
4742             return CP_ACCESS_TRAP_EL2;
4743         }
4744     }
4745     return CP_ACCESS_OK;
4746 }
4747 
4748 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4749 {
4750     ARMCPU *cpu = env_archcpu(env);
4751     int dzp_bit = 1 << 4;
4752 
4753     /* DZP indicates whether DC ZVA access is allowed */
4754     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4755         dzp_bit = 0;
4756     }
4757     return cpu->dcz_blocksize | dzp_bit;
4758 }
4759 
4760 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4761                                     bool isread)
4762 {
4763     if (!(env->pstate & PSTATE_SP)) {
4764         /* Access to SP_EL0 is undefined if it's being used as
4765          * the stack pointer.
4766          */
4767         return CP_ACCESS_TRAP_UNCATEGORIZED;
4768     }
4769     return CP_ACCESS_OK;
4770 }
4771 
4772 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4773 {
4774     return env->pstate & PSTATE_SP;
4775 }
4776 
4777 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4778 {
4779     update_spsel(env, val);
4780 }
4781 
4782 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4783                         uint64_t value)
4784 {
4785     ARMCPU *cpu = env_archcpu(env);
4786 
4787     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4788         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4789         value &= ~SCTLR_M;
4790     }
4791 
4792     /* ??? Lots of these bits are not implemented.  */
4793 
4794     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4795         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4796             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4797         } else {
4798             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4799                        SCTLR_ATA0 | SCTLR_ATA);
4800         }
4801     }
4802 
4803     if (raw_read(env, ri) == value) {
4804         /* Skip the TLB flush if nothing actually changed; Linux likes
4805          * to do a lot of pointless SCTLR writes.
4806          */
4807         return;
4808     }
4809 
4810     raw_write(env, ri, value);
4811 
4812     /* This may enable/disable the MMU, so do a TLB flush.  */
4813     tlb_flush(CPU(cpu));
4814 
4815     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4816         /*
4817          * Normally we would always end the TB on an SCTLR write; see the
4818          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4819          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4820          * of hflags from the translator, so do it here.
4821          */
4822         arm_rebuild_hflags(env);
4823     }
4824 }
4825 
4826 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4827                            uint64_t value)
4828 {
4829     /*
4830      * Some MDCR_EL3 bits affect whether PMU counters are running:
4831      * if we are trying to change any of those then we must
4832      * bracket this update with PMU start/finish calls.
4833      */
4834     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4835 
4836     if (pmu_op) {
4837         pmu_op_start(env);
4838     }
4839     env->cp15.mdcr_el3 = value;
4840     if (pmu_op) {
4841         pmu_op_finish(env);
4842     }
4843 }
4844 
4845 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4846                        uint64_t value)
4847 {
4848     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4849     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4850 }
4851 
4852 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4853                            uint64_t value)
4854 {
4855     /*
4856      * Some MDCR_EL2 bits affect whether PMU counters are running:
4857      * if we are trying to change any of those then we must
4858      * bracket this update with PMU start/finish calls.
4859      */
4860     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4861 
4862     if (pmu_op) {
4863         pmu_op_start(env);
4864     }
4865     env->cp15.mdcr_el2 = value;
4866     if (pmu_op) {
4867         pmu_op_finish(env);
4868     }
4869 }
4870 
4871 static const ARMCPRegInfo v8_cp_reginfo[] = {
4872     /* Minimal set of EL0-visible registers. This will need to be expanded
4873      * significantly for system emulation of AArch64 CPUs.
4874      */
4875     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4876       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4877       .access = PL0_RW, .type = ARM_CP_NZCV },
4878     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4879       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4880       .type = ARM_CP_NO_RAW,
4881       .access = PL0_RW, .accessfn = aa64_daif_access,
4882       .fieldoffset = offsetof(CPUARMState, daif),
4883       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4884     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4885       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4886       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4887       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4888     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4889       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4890       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4891       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4892     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4893       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4894       .access = PL0_R, .type = ARM_CP_NO_RAW,
4895       .readfn = aa64_dczid_read },
4896     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4897       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4898       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4899 #ifndef CONFIG_USER_ONLY
4900       /* Avoid overhead of an access check that always passes in user-mode */
4901       .accessfn = aa64_zva_access,
4902 #endif
4903     },
4904     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4905       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4906       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4907     /* Cache ops: all NOPs since we don't emulate caches */
4908     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4909       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4910       .access = PL1_W, .type = ARM_CP_NOP,
4911       .accessfn = aa64_cacheop_pou_access },
4912     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4913       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4914       .access = PL1_W, .type = ARM_CP_NOP,
4915       .accessfn = aa64_cacheop_pou_access },
4916     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4917       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4918       .access = PL0_W, .type = ARM_CP_NOP,
4919       .accessfn = aa64_cacheop_pou_access },
4920     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4921       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4922       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4923       .type = ARM_CP_NOP },
4924     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4925       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4926       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4927     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4928       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4929       .access = PL0_W, .type = ARM_CP_NOP,
4930       .accessfn = aa64_cacheop_poc_access },
4931     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4932       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4933       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4934     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4935       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4936       .access = PL0_W, .type = ARM_CP_NOP,
4937       .accessfn = aa64_cacheop_pou_access },
4938     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4939       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4940       .access = PL0_W, .type = ARM_CP_NOP,
4941       .accessfn = aa64_cacheop_poc_access },
4942     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4943       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4944       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4945     /* TLBI operations */
4946     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4947       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4948       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4949       .writefn = tlbi_aa64_vmalle1is_write },
4950     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4952       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4953       .writefn = tlbi_aa64_vae1is_write },
4954     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4955       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4956       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4957       .writefn = tlbi_aa64_vmalle1is_write },
4958     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4959       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4960       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4961       .writefn = tlbi_aa64_vae1is_write },
4962     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4963       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4964       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4965       .writefn = tlbi_aa64_vae1is_write },
4966     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4967       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4968       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4969       .writefn = tlbi_aa64_vae1is_write },
4970     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4971       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4972       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4973       .writefn = tlbi_aa64_vmalle1_write },
4974     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4975       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4976       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4977       .writefn = tlbi_aa64_vae1_write },
4978     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4979       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4980       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4981       .writefn = tlbi_aa64_vmalle1_write },
4982     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4983       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4984       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4985       .writefn = tlbi_aa64_vae1_write },
4986     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4987       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4988       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4989       .writefn = tlbi_aa64_vae1_write },
4990     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4991       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4992       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4993       .writefn = tlbi_aa64_vae1_write },
4994     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4995       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4996       .access = PL2_W, .type = ARM_CP_NO_RAW,
4997       .writefn = tlbi_aa64_ipas2e1is_write },
4998     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4999       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5000       .access = PL2_W, .type = ARM_CP_NO_RAW,
5001       .writefn = tlbi_aa64_ipas2e1is_write },
5002     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5003       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5004       .access = PL2_W, .type = ARM_CP_NO_RAW,
5005       .writefn = tlbi_aa64_alle1is_write },
5006     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5007       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5008       .access = PL2_W, .type = ARM_CP_NO_RAW,
5009       .writefn = tlbi_aa64_alle1is_write },
5010     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5011       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5012       .access = PL2_W, .type = ARM_CP_NO_RAW,
5013       .writefn = tlbi_aa64_ipas2e1_write },
5014     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5015       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5016       .access = PL2_W, .type = ARM_CP_NO_RAW,
5017       .writefn = tlbi_aa64_ipas2e1_write },
5018     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5019       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5020       .access = PL2_W, .type = ARM_CP_NO_RAW,
5021       .writefn = tlbi_aa64_alle1_write },
5022     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5023       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5024       .access = PL2_W, .type = ARM_CP_NO_RAW,
5025       .writefn = tlbi_aa64_alle1is_write },
5026 #ifndef CONFIG_USER_ONLY
5027     /* 64 bit address translation operations */
5028     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5029       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5030       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5031       .writefn = ats_write64 },
5032     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5033       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5034       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5035       .writefn = ats_write64 },
5036     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5037       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5038       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5039       .writefn = ats_write64 },
5040     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5041       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5042       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5043       .writefn = ats_write64 },
5044     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5045       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5046       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5047       .writefn = ats_write64 },
5048     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5049       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5050       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5051       .writefn = ats_write64 },
5052     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5053       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5054       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5055       .writefn = ats_write64 },
5056     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5057       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5058       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5059       .writefn = ats_write64 },
5060     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5061     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5062       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5063       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5064       .writefn = ats_write64 },
5065     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5066       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5067       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5068       .writefn = ats_write64 },
5069     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5070       .type = ARM_CP_ALIAS,
5071       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5072       .access = PL1_RW, .resetvalue = 0,
5073       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5074       .writefn = par_write },
5075 #endif
5076     /* TLB invalidate last level of translation table walk */
5077     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5078       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5079       .writefn = tlbimva_is_write },
5080     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5081       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5082       .writefn = tlbimvaa_is_write },
5083     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5084       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5085       .writefn = tlbimva_write },
5086     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5087       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5088       .writefn = tlbimvaa_write },
5089     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5090       .type = ARM_CP_NO_RAW, .access = PL2_W,
5091       .writefn = tlbimva_hyp_write },
5092     { .name = "TLBIMVALHIS",
5093       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5094       .type = ARM_CP_NO_RAW, .access = PL2_W,
5095       .writefn = tlbimva_hyp_is_write },
5096     { .name = "TLBIIPAS2",
5097       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5098       .type = ARM_CP_NO_RAW, .access = PL2_W,
5099       .writefn = tlbiipas2_hyp_write },
5100     { .name = "TLBIIPAS2IS",
5101       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5102       .type = ARM_CP_NO_RAW, .access = PL2_W,
5103       .writefn = tlbiipas2is_hyp_write },
5104     { .name = "TLBIIPAS2L",
5105       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5106       .type = ARM_CP_NO_RAW, .access = PL2_W,
5107       .writefn = tlbiipas2_hyp_write },
5108     { .name = "TLBIIPAS2LIS",
5109       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5110       .type = ARM_CP_NO_RAW, .access = PL2_W,
5111       .writefn = tlbiipas2is_hyp_write },
5112     /* 32 bit cache operations */
5113     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5114       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5115     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5116       .type = ARM_CP_NOP, .access = PL1_W },
5117     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5118       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5119     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5120       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5121     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5122       .type = ARM_CP_NOP, .access = PL1_W },
5123     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5124       .type = ARM_CP_NOP, .access = PL1_W },
5125     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5126       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5127     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5128       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5129     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5130       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5131     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5132       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5133     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5134       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5135     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5136       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5137     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5138       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5139     /* MMU Domain access control / MPU write buffer control */
5140     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5141       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5142       .writefn = dacr_write, .raw_writefn = raw_write,
5143       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5144                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5145     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5146       .type = ARM_CP_ALIAS,
5147       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5148       .access = PL1_RW,
5149       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5150     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5151       .type = ARM_CP_ALIAS,
5152       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5153       .access = PL1_RW,
5154       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5155     /* We rely on the access checks not allowing the guest to write to the
5156      * state field when SPSel indicates that it's being used as the stack
5157      * pointer.
5158      */
5159     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5160       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5161       .access = PL1_RW, .accessfn = sp_el0_access,
5162       .type = ARM_CP_ALIAS,
5163       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5164     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5165       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5166       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5167       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5168     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5169       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5170       .type = ARM_CP_NO_RAW,
5171       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5172     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5173       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5174       .access = PL2_RW,
5175       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5176       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5177     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5178       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5179       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5180       .writefn = dacr_write, .raw_writefn = raw_write,
5181       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5182     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5183       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5184       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5185       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5186     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5187       .type = ARM_CP_ALIAS,
5188       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5189       .access = PL2_RW,
5190       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5191     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5192       .type = ARM_CP_ALIAS,
5193       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5194       .access = PL2_RW,
5195       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5196     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5197       .type = ARM_CP_ALIAS,
5198       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5199       .access = PL2_RW,
5200       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5201     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5202       .type = ARM_CP_ALIAS,
5203       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5204       .access = PL2_RW,
5205       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5206     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5207       .type = ARM_CP_IO,
5208       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5209       .resetvalue = 0,
5210       .access = PL3_RW,
5211       .writefn = mdcr_el3_write,
5212       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5213     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5214       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5215       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5216       .writefn = sdcr_write,
5217       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5218 };
5219 
5220 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5221 {
5222     ARMCPU *cpu = env_archcpu(env);
5223 
5224     if (arm_feature(env, ARM_FEATURE_V8)) {
5225         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5226     } else {
5227         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5228     }
5229 
5230     if (arm_feature(env, ARM_FEATURE_EL3)) {
5231         valid_mask &= ~HCR_HCD;
5232     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5233         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5234          * However, if we're using the SMC PSCI conduit then QEMU is
5235          * effectively acting like EL3 firmware and so the guest at
5236          * EL2 should retain the ability to prevent EL1 from being
5237          * able to make SMC calls into the ersatz firmware, so in
5238          * that case HCR.TSC should be read/write.
5239          */
5240         valid_mask &= ~HCR_TSC;
5241     }
5242 
5243     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5244         if (cpu_isar_feature(aa64_vh, cpu)) {
5245             valid_mask |= HCR_E2H;
5246         }
5247         if (cpu_isar_feature(aa64_ras, cpu)) {
5248             valid_mask |= HCR_TERR | HCR_TEA;
5249         }
5250         if (cpu_isar_feature(aa64_lor, cpu)) {
5251             valid_mask |= HCR_TLOR;
5252         }
5253         if (cpu_isar_feature(aa64_pauth, cpu)) {
5254             valid_mask |= HCR_API | HCR_APK;
5255         }
5256         if (cpu_isar_feature(aa64_mte, cpu)) {
5257             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5258         }
5259         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5260             valid_mask |= HCR_ENSCXT;
5261         }
5262         if (cpu_isar_feature(aa64_fwb, cpu)) {
5263             valid_mask |= HCR_FWB;
5264         }
5265     }
5266 
5267     /* Clear RES0 bits.  */
5268     value &= valid_mask;
5269 
5270     /*
5271      * These bits change the MMU setup:
5272      * HCR_VM enables stage 2 translation
5273      * HCR_PTW forbids certain page-table setups
5274      * HCR_DC disables stage1 and enables stage2 translation
5275      * HCR_DCT enables tagging on (disabled) stage1 translation
5276      * HCR_FWB changes the interpretation of stage2 descriptor bits
5277      */
5278     if ((env->cp15.hcr_el2 ^ value) &
5279         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5280         tlb_flush(CPU(cpu));
5281     }
5282     env->cp15.hcr_el2 = value;
5283 
5284     /*
5285      * Updates to VI and VF require us to update the status of
5286      * virtual interrupts, which are the logical OR of these bits
5287      * and the state of the input lines from the GIC. (This requires
5288      * that we have the iothread lock, which is done by marking the
5289      * reginfo structs as ARM_CP_IO.)
5290      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5291      * possible for it to be taken immediately, because VIRQ and
5292      * VFIQ are masked unless running at EL0 or EL1, and HCR
5293      * can only be written at EL2.
5294      */
5295     g_assert(qemu_mutex_iothread_locked());
5296     arm_cpu_update_virq(cpu);
5297     arm_cpu_update_vfiq(cpu);
5298     arm_cpu_update_vserr(cpu);
5299 }
5300 
5301 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5302 {
5303     do_hcr_write(env, value, 0);
5304 }
5305 
5306 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5307                           uint64_t value)
5308 {
5309     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5310     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5311     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5312 }
5313 
5314 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5315                          uint64_t value)
5316 {
5317     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5318     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5319     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5320 }
5321 
5322 /*
5323  * Return the effective value of HCR_EL2, at the given security state.
5324  * Bits that are not included here:
5325  * RW       (read from SCR_EL3.RW as needed)
5326  */
5327 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
5328 {
5329     uint64_t ret = env->cp15.hcr_el2;
5330 
5331     if (!arm_is_el2_enabled_secstate(env, secure)) {
5332         /*
5333          * "This register has no effect if EL2 is not enabled in the
5334          * current Security state".  This is ARMv8.4-SecEL2 speak for
5335          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5336          *
5337          * Prior to that, the language was "In an implementation that
5338          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5339          * as if this field is 0 for all purposes other than a direct
5340          * read or write access of HCR_EL2".  With lots of enumeration
5341          * on a per-field basis.  In current QEMU, this is condition
5342          * is arm_is_secure_below_el3.
5343          *
5344          * Since the v8.4 language applies to the entire register, and
5345          * appears to be backward compatible, use that.
5346          */
5347         return 0;
5348     }
5349 
5350     /*
5351      * For a cpu that supports both aarch64 and aarch32, we can set bits
5352      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5353      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5354      */
5355     if (!arm_el_is_aa64(env, 2)) {
5356         uint64_t aa32_valid;
5357 
5358         /*
5359          * These bits are up-to-date as of ARMv8.6.
5360          * For HCR, it's easiest to list just the 2 bits that are invalid.
5361          * For HCR2, list those that are valid.
5362          */
5363         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5364         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5365                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5366         ret &= aa32_valid;
5367     }
5368 
5369     if (ret & HCR_TGE) {
5370         /* These bits are up-to-date as of ARMv8.6.  */
5371         if (ret & HCR_E2H) {
5372             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5373                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5374                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5375                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5376                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5377                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5378         } else {
5379             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5380         }
5381         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5382                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5383                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5384                  HCR_TLOR);
5385     }
5386 
5387     return ret;
5388 }
5389 
5390 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5391 {
5392     return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5393 }
5394 
5395 /*
5396  * Corresponds to ARM pseudocode function ELIsInHost().
5397  */
5398 bool el_is_in_host(CPUARMState *env, int el)
5399 {
5400     uint64_t mask;
5401 
5402     /*
5403      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5404      * Perform the simplest bit tests first, and validate EL2 afterward.
5405      */
5406     if (el & 1) {
5407         return false; /* EL1 or EL3 */
5408     }
5409 
5410     /*
5411      * Note that hcr_write() checks isar_feature_aa64_vh(),
5412      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5413      */
5414     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5415     if ((env->cp15.hcr_el2 & mask) != mask) {
5416         return false;
5417     }
5418 
5419     /* TGE and/or E2H set: double check those bits are currently legal. */
5420     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5421 }
5422 
5423 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5424                        uint64_t value)
5425 {
5426     uint64_t valid_mask = 0;
5427 
5428     /* No features adding bits to HCRX are implemented. */
5429 
5430     /* Clear RES0 bits.  */
5431     env->cp15.hcrx_el2 = value & valid_mask;
5432 }
5433 
5434 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5435                                   bool isread)
5436 {
5437     if (arm_current_el(env) < 3
5438         && arm_feature(env, ARM_FEATURE_EL3)
5439         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5440         return CP_ACCESS_TRAP_EL3;
5441     }
5442     return CP_ACCESS_OK;
5443 }
5444 
5445 static const ARMCPRegInfo hcrx_el2_reginfo = {
5446     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5447     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5448     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5449     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5450 };
5451 
5452 /* Return the effective value of HCRX_EL2.  */
5453 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5454 {
5455     /*
5456      * The bits in this register behave as 0 for all purposes other than
5457      * direct reads of the register if:
5458      *   - EL2 is not enabled in the current security state,
5459      *   - SCR_EL3.HXEn is 0.
5460      */
5461     if (!arm_is_el2_enabled(env)
5462         || (arm_feature(env, ARM_FEATURE_EL3)
5463             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5464         return 0;
5465     }
5466     return env->cp15.hcrx_el2;
5467 }
5468 
5469 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5470                            uint64_t value)
5471 {
5472     /*
5473      * For A-profile AArch32 EL3, if NSACR.CP10
5474      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5475      */
5476     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5477         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5478         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5479         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5480     }
5481     env->cp15.cptr_el[2] = value;
5482 }
5483 
5484 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5485 {
5486     /*
5487      * For A-profile AArch32 EL3, if NSACR.CP10
5488      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5489      */
5490     uint64_t value = env->cp15.cptr_el[2];
5491 
5492     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5493         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5494         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5495     }
5496     return value;
5497 }
5498 
5499 static const ARMCPRegInfo el2_cp_reginfo[] = {
5500     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5501       .type = ARM_CP_IO,
5502       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5503       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5504       .writefn = hcr_write },
5505     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5506       .type = ARM_CP_ALIAS | ARM_CP_IO,
5507       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5508       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5509       .writefn = hcr_writelow },
5510     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5511       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5512       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5513     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5514       .type = ARM_CP_ALIAS,
5515       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5516       .access = PL2_RW,
5517       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5518     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5519       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5520       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5521     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5522       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5523       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5524     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5525       .type = ARM_CP_ALIAS,
5526       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5527       .access = PL2_RW,
5528       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5529     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5530       .type = ARM_CP_ALIAS,
5531       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5532       .access = PL2_RW,
5533       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5534     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5535       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5536       .access = PL2_RW, .writefn = vbar_write,
5537       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5538       .resetvalue = 0 },
5539     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5540       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5541       .access = PL3_RW, .type = ARM_CP_ALIAS,
5542       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5543     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5544       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5545       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5546       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5547       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5548     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5549       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5550       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5551       .resetvalue = 0 },
5552     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5553       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5554       .access = PL2_RW, .type = ARM_CP_ALIAS,
5555       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5556     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5557       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5558       .access = PL2_RW, .type = ARM_CP_CONST,
5559       .resetvalue = 0 },
5560     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5561     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5562       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5563       .access = PL2_RW, .type = ARM_CP_CONST,
5564       .resetvalue = 0 },
5565     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5566       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5567       .access = PL2_RW, .type = ARM_CP_CONST,
5568       .resetvalue = 0 },
5569     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5570       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5571       .access = PL2_RW, .type = ARM_CP_CONST,
5572       .resetvalue = 0 },
5573     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5574       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5575       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5576       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5577     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5578       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5579       .type = ARM_CP_ALIAS,
5580       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5581       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5582     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5583       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5584       .access = PL2_RW,
5585       /* no .writefn needed as this can't cause an ASID change */
5586       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5587     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5588       .cp = 15, .opc1 = 6, .crm = 2,
5589       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5590       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5591       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5592       .writefn = vttbr_write },
5593     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5594       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5595       .access = PL2_RW, .writefn = vttbr_write,
5596       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5597     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5598       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5599       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5600       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5601     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5602       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5603       .access = PL2_RW, .resetvalue = 0,
5604       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5605     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5606       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5607       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5608       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5609     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5610       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5611       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5612     { .name = "TLBIALLNSNH",
5613       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5614       .type = ARM_CP_NO_RAW, .access = PL2_W,
5615       .writefn = tlbiall_nsnh_write },
5616     { .name = "TLBIALLNSNHIS",
5617       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5618       .type = ARM_CP_NO_RAW, .access = PL2_W,
5619       .writefn = tlbiall_nsnh_is_write },
5620     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5621       .type = ARM_CP_NO_RAW, .access = PL2_W,
5622       .writefn = tlbiall_hyp_write },
5623     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5624       .type = ARM_CP_NO_RAW, .access = PL2_W,
5625       .writefn = tlbiall_hyp_is_write },
5626     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5627       .type = ARM_CP_NO_RAW, .access = PL2_W,
5628       .writefn = tlbimva_hyp_write },
5629     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5630       .type = ARM_CP_NO_RAW, .access = PL2_W,
5631       .writefn = tlbimva_hyp_is_write },
5632     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5633       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5634       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5635       .writefn = tlbi_aa64_alle2_write },
5636     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5637       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5638       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5639       .writefn = tlbi_aa64_vae2_write },
5640     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5641       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5642       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5643       .writefn = tlbi_aa64_vae2_write },
5644     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5645       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5646       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5647       .writefn = tlbi_aa64_alle2is_write },
5648     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5649       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5650       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5651       .writefn = tlbi_aa64_vae2is_write },
5652     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5653       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5654       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5655       .writefn = tlbi_aa64_vae2is_write },
5656 #ifndef CONFIG_USER_ONLY
5657     /* Unlike the other EL2-related AT operations, these must
5658      * UNDEF from EL3 if EL2 is not implemented, which is why we
5659      * define them here rather than with the rest of the AT ops.
5660      */
5661     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5662       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5663       .access = PL2_W, .accessfn = at_s1e2_access,
5664       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5665       .writefn = ats_write64 },
5666     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5667       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5668       .access = PL2_W, .accessfn = at_s1e2_access,
5669       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5670       .writefn = ats_write64 },
5671     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5672      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5673      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5674      * to behave as if SCR.NS was 1.
5675      */
5676     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5677       .access = PL2_W,
5678       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5679     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5680       .access = PL2_W,
5681       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5682     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5683       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5684       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5685        * reset values as IMPDEF. We choose to reset to 3 to comply with
5686        * both ARMv7 and ARMv8.
5687        */
5688       .access = PL2_RW, .resetvalue = 3,
5689       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5690     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5691       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5692       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5693       .writefn = gt_cntvoff_write,
5694       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5695     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5696       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5697       .writefn = gt_cntvoff_write,
5698       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5699     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5700       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5701       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5702       .type = ARM_CP_IO, .access = PL2_RW,
5703       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5704     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5705       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5706       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5707       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5708     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5709       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5710       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5711       .resetfn = gt_hyp_timer_reset,
5712       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5713     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5714       .type = ARM_CP_IO,
5715       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5716       .access = PL2_RW,
5717       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5718       .resetvalue = 0,
5719       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5720 #endif
5721     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5722       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5723       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5724       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5725     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5726       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5727       .access = PL2_RW,
5728       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5729     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5730       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5731       .access = PL2_RW,
5732       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5733 };
5734 
5735 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5736     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5737       .type = ARM_CP_ALIAS | ARM_CP_IO,
5738       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5739       .access = PL2_RW,
5740       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5741       .writefn = hcr_writehigh },
5742 };
5743 
5744 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5745                                   bool isread)
5746 {
5747     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5748         return CP_ACCESS_OK;
5749     }
5750     return CP_ACCESS_TRAP_UNCATEGORIZED;
5751 }
5752 
5753 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5754     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5755       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5756       .access = PL2_RW, .accessfn = sel2_access,
5757       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5758     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5760       .access = PL2_RW, .accessfn = sel2_access,
5761       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5762 };
5763 
5764 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5765                                    bool isread)
5766 {
5767     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5768      * At Secure EL1 it traps to EL3 or EL2.
5769      */
5770     if (arm_current_el(env) == 3) {
5771         return CP_ACCESS_OK;
5772     }
5773     if (arm_is_secure_below_el3(env)) {
5774         if (env->cp15.scr_el3 & SCR_EEL2) {
5775             return CP_ACCESS_TRAP_EL2;
5776         }
5777         return CP_ACCESS_TRAP_EL3;
5778     }
5779     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5780     if (isread) {
5781         return CP_ACCESS_OK;
5782     }
5783     return CP_ACCESS_TRAP_UNCATEGORIZED;
5784 }
5785 
5786 static const ARMCPRegInfo el3_cp_reginfo[] = {
5787     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5788       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5789       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5790       .resetfn = scr_reset, .writefn = scr_write },
5791     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5792       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5793       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5794       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5795       .writefn = scr_write },
5796     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5797       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5798       .access = PL3_RW, .resetvalue = 0,
5799       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5800     { .name = "SDER",
5801       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5802       .access = PL3_RW, .resetvalue = 0,
5803       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5804     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5805       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5806       .writefn = vbar_write, .resetvalue = 0,
5807       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5808     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5809       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5810       .access = PL3_RW, .resetvalue = 0,
5811       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5812     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5813       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5814       .access = PL3_RW,
5815       /* no .writefn needed as this can't cause an ASID change */
5816       .resetvalue = 0,
5817       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5818     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5819       .type = ARM_CP_ALIAS,
5820       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5821       .access = PL3_RW,
5822       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5823     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5824       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5825       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5826     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5827       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5828       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5829     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5830       .type = ARM_CP_ALIAS,
5831       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5832       .access = PL3_RW,
5833       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5834     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5835       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5836       .access = PL3_RW, .writefn = vbar_write,
5837       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5838       .resetvalue = 0 },
5839     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5840       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5841       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5842       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5843     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5844       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5845       .access = PL3_RW, .resetvalue = 0,
5846       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5847     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5848       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5849       .access = PL3_RW, .type = ARM_CP_CONST,
5850       .resetvalue = 0 },
5851     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5852       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5853       .access = PL3_RW, .type = ARM_CP_CONST,
5854       .resetvalue = 0 },
5855     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5856       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5857       .access = PL3_RW, .type = ARM_CP_CONST,
5858       .resetvalue = 0 },
5859     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5860       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5861       .access = PL3_W, .type = ARM_CP_NO_RAW,
5862       .writefn = tlbi_aa64_alle3is_write },
5863     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5864       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5865       .access = PL3_W, .type = ARM_CP_NO_RAW,
5866       .writefn = tlbi_aa64_vae3is_write },
5867     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5868       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5869       .access = PL3_W, .type = ARM_CP_NO_RAW,
5870       .writefn = tlbi_aa64_vae3is_write },
5871     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5872       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5873       .access = PL3_W, .type = ARM_CP_NO_RAW,
5874       .writefn = tlbi_aa64_alle3_write },
5875     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5876       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5877       .access = PL3_W, .type = ARM_CP_NO_RAW,
5878       .writefn = tlbi_aa64_vae3_write },
5879     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5880       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5881       .access = PL3_W, .type = ARM_CP_NO_RAW,
5882       .writefn = tlbi_aa64_vae3_write },
5883 };
5884 
5885 #ifndef CONFIG_USER_ONLY
5886 /* Test if system register redirection is to occur in the current state.  */
5887 static bool redirect_for_e2h(CPUARMState *env)
5888 {
5889     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5890 }
5891 
5892 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5893 {
5894     CPReadFn *readfn;
5895 
5896     if (redirect_for_e2h(env)) {
5897         /* Switch to the saved EL2 version of the register.  */
5898         ri = ri->opaque;
5899         readfn = ri->readfn;
5900     } else {
5901         readfn = ri->orig_readfn;
5902     }
5903     if (readfn == NULL) {
5904         readfn = raw_read;
5905     }
5906     return readfn(env, ri);
5907 }
5908 
5909 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5910                           uint64_t value)
5911 {
5912     CPWriteFn *writefn;
5913 
5914     if (redirect_for_e2h(env)) {
5915         /* Switch to the saved EL2 version of the register.  */
5916         ri = ri->opaque;
5917         writefn = ri->writefn;
5918     } else {
5919         writefn = ri->orig_writefn;
5920     }
5921     if (writefn == NULL) {
5922         writefn = raw_write;
5923     }
5924     writefn(env, ri, value);
5925 }
5926 
5927 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5928 {
5929     struct E2HAlias {
5930         uint32_t src_key, dst_key, new_key;
5931         const char *src_name, *dst_name, *new_name;
5932         bool (*feature)(const ARMISARegisters *id);
5933     };
5934 
5935 #define K(op0, op1, crn, crm, op2) \
5936     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5937 
5938     static const struct E2HAlias aliases[] = {
5939         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5940           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5941         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5942           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5943         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5944           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5945         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5946           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5947         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5948           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5949         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5950           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5951         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5952           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5953         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5954           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5955         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5956           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5957         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5958           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5959         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5960           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5961         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5962           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5963         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5964           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5965         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5966           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5967         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5968           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5969         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5970           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5971 
5972         /*
5973          * Note that redirection of ZCR is mentioned in the description
5974          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5975          * not in the summary table.
5976          */
5977         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5978           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5979         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5980           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5981 
5982         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5983           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5984 
5985         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5986           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5987           isar_feature_aa64_scxtnum },
5988 
5989         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5990         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5991     };
5992 #undef K
5993 
5994     size_t i;
5995 
5996     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5997         const struct E2HAlias *a = &aliases[i];
5998         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5999         bool ok;
6000 
6001         if (a->feature && !a->feature(&cpu->isar)) {
6002             continue;
6003         }
6004 
6005         src_reg = g_hash_table_lookup(cpu->cp_regs,
6006                                       (gpointer)(uintptr_t)a->src_key);
6007         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6008                                       (gpointer)(uintptr_t)a->dst_key);
6009         g_assert(src_reg != NULL);
6010         g_assert(dst_reg != NULL);
6011 
6012         /* Cross-compare names to detect typos in the keys.  */
6013         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6014         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6015 
6016         /* None of the core system registers use opaque; we will.  */
6017         g_assert(src_reg->opaque == NULL);
6018 
6019         /* Create alias before redirection so we dup the right data. */
6020         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6021 
6022         new_reg->name = a->new_name;
6023         new_reg->type |= ARM_CP_ALIAS;
6024         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6025         new_reg->access &= PL2_RW | PL3_RW;
6026 
6027         ok = g_hash_table_insert(cpu->cp_regs,
6028                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6029         g_assert(ok);
6030 
6031         src_reg->opaque = dst_reg;
6032         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6033         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6034         if (!src_reg->raw_readfn) {
6035             src_reg->raw_readfn = raw_read;
6036         }
6037         if (!src_reg->raw_writefn) {
6038             src_reg->raw_writefn = raw_write;
6039         }
6040         src_reg->readfn = el2_e2h_read;
6041         src_reg->writefn = el2_e2h_write;
6042     }
6043 }
6044 #endif
6045 
6046 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6047                                      bool isread)
6048 {
6049     int cur_el = arm_current_el(env);
6050 
6051     if (cur_el < 2) {
6052         uint64_t hcr = arm_hcr_el2_eff(env);
6053 
6054         if (cur_el == 0) {
6055             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6056                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6057                     return CP_ACCESS_TRAP_EL2;
6058                 }
6059             } else {
6060                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6061                     return CP_ACCESS_TRAP;
6062                 }
6063                 if (hcr & HCR_TID2) {
6064                     return CP_ACCESS_TRAP_EL2;
6065                 }
6066             }
6067         } else if (hcr & HCR_TID2) {
6068             return CP_ACCESS_TRAP_EL2;
6069         }
6070     }
6071 
6072     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6073         return CP_ACCESS_TRAP_EL2;
6074     }
6075 
6076     return CP_ACCESS_OK;
6077 }
6078 
6079 /*
6080  * Check for traps to RAS registers, which are controlled
6081  * by HCR_EL2.TERR and SCR_EL3.TERR.
6082  */
6083 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6084                                   bool isread)
6085 {
6086     int el = arm_current_el(env);
6087 
6088     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6089         return CP_ACCESS_TRAP_EL2;
6090     }
6091     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6092         return CP_ACCESS_TRAP_EL3;
6093     }
6094     return CP_ACCESS_OK;
6095 }
6096 
6097 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6098 {
6099     int el = arm_current_el(env);
6100 
6101     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6102         return env->cp15.vdisr_el2;
6103     }
6104     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6105         return 0; /* RAZ/WI */
6106     }
6107     return env->cp15.disr_el1;
6108 }
6109 
6110 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6111 {
6112     int el = arm_current_el(env);
6113 
6114     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6115         env->cp15.vdisr_el2 = val;
6116         return;
6117     }
6118     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6119         return; /* RAZ/WI */
6120     }
6121     env->cp15.disr_el1 = val;
6122 }
6123 
6124 /*
6125  * Minimal RAS implementation with no Error Records.
6126  * Which means that all of the Error Record registers:
6127  *   ERXADDR_EL1
6128  *   ERXCTLR_EL1
6129  *   ERXFR_EL1
6130  *   ERXMISC0_EL1
6131  *   ERXMISC1_EL1
6132  *   ERXMISC2_EL1
6133  *   ERXMISC3_EL1
6134  *   ERXPFGCDN_EL1  (RASv1p1)
6135  *   ERXPFGCTL_EL1  (RASv1p1)
6136  *   ERXPFGF_EL1    (RASv1p1)
6137  *   ERXSTATUS_EL1
6138  * and
6139  *   ERRSELR_EL1
6140  * may generate UNDEFINED, which is the effect we get by not
6141  * listing them at all.
6142  */
6143 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6144     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6145       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6146       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6147       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6148     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6149       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6150       .access = PL1_R, .accessfn = access_terr,
6151       .type = ARM_CP_CONST, .resetvalue = 0 },
6152     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6153       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6154       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6155     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6156       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6157       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6158 };
6159 
6160 /*
6161  * Return the exception level to which exceptions should be taken
6162  * via SVEAccessTrap.  This excludes the check for whether the exception
6163  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6164  * be found by testing 0 < fp_exception_el < sve_exception_el.
6165  *
6166  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6167  * pseudocode does *not* separate out the FP trap checks, but has them
6168  * all in one function.
6169  */
6170 int sve_exception_el(CPUARMState *env, int el)
6171 {
6172 #ifndef CONFIG_USER_ONLY
6173     if (el <= 1 && !el_is_in_host(env, el)) {
6174         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6175         case 1:
6176             if (el != 0) {
6177                 break;
6178             }
6179             /* fall through */
6180         case 0:
6181         case 2:
6182             return 1;
6183         }
6184     }
6185 
6186     if (el <= 2 && arm_is_el2_enabled(env)) {
6187         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6188         if (env->cp15.hcr_el2 & HCR_E2H) {
6189             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6190             case 1:
6191                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6192                     break;
6193                 }
6194                 /* fall through */
6195             case 0:
6196             case 2:
6197                 return 2;
6198             }
6199         } else {
6200             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6201                 return 2;
6202             }
6203         }
6204     }
6205 
6206     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6207     if (arm_feature(env, ARM_FEATURE_EL3)
6208         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6209         return 3;
6210     }
6211 #endif
6212     return 0;
6213 }
6214 
6215 /*
6216  * Return the exception level to which exceptions should be taken for SME.
6217  * C.f. the ARM pseudocode function CheckSMEAccess.
6218  */
6219 int sme_exception_el(CPUARMState *env, int el)
6220 {
6221 #ifndef CONFIG_USER_ONLY
6222     if (el <= 1 && !el_is_in_host(env, el)) {
6223         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6224         case 1:
6225             if (el != 0) {
6226                 break;
6227             }
6228             /* fall through */
6229         case 0:
6230         case 2:
6231             return 1;
6232         }
6233     }
6234 
6235     if (el <= 2 && arm_is_el2_enabled(env)) {
6236         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6237         if (env->cp15.hcr_el2 & HCR_E2H) {
6238             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6239             case 1:
6240                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6241                     break;
6242                 }
6243                 /* fall through */
6244             case 0:
6245             case 2:
6246                 return 2;
6247             }
6248         } else {
6249             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6250                 return 2;
6251             }
6252         }
6253     }
6254 
6255     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6256     if (arm_feature(env, ARM_FEATURE_EL3)
6257         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6258         return 3;
6259     }
6260 #endif
6261     return 0;
6262 }
6263 
6264 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6265 static bool sme_fa64(CPUARMState *env, int el)
6266 {
6267     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6268         return false;
6269     }
6270 
6271     if (el <= 1 && !el_is_in_host(env, el)) {
6272         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6273             return false;
6274         }
6275     }
6276     if (el <= 2 && arm_is_el2_enabled(env)) {
6277         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6278             return false;
6279         }
6280     }
6281     if (arm_feature(env, ARM_FEATURE_EL3)) {
6282         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6283             return false;
6284         }
6285     }
6286 
6287     return true;
6288 }
6289 
6290 /*
6291  * Given that SVE is enabled, return the vector length for EL.
6292  */
6293 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6294 {
6295     ARMCPU *cpu = env_archcpu(env);
6296     uint64_t *cr = env->vfp.zcr_el;
6297     uint32_t map = cpu->sve_vq.map;
6298     uint32_t len = ARM_MAX_VQ - 1;
6299 
6300     if (sm) {
6301         cr = env->vfp.smcr_el;
6302         map = cpu->sme_vq.map;
6303     }
6304 
6305     if (el <= 1 && !el_is_in_host(env, el)) {
6306         len = MIN(len, 0xf & (uint32_t)cr[1]);
6307     }
6308     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6309         len = MIN(len, 0xf & (uint32_t)cr[2]);
6310     }
6311     if (arm_feature(env, ARM_FEATURE_EL3)) {
6312         len = MIN(len, 0xf & (uint32_t)cr[3]);
6313     }
6314 
6315     map &= MAKE_64BIT_MASK(0, len + 1);
6316     if (map != 0) {
6317         return 31 - clz32(map);
6318     }
6319 
6320     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6321     assert(sm);
6322     return ctz32(cpu->sme_vq.map);
6323 }
6324 
6325 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6326 {
6327     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6328 }
6329 
6330 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6331                       uint64_t value)
6332 {
6333     int cur_el = arm_current_el(env);
6334     int old_len = sve_vqm1_for_el(env, cur_el);
6335     int new_len;
6336 
6337     /* Bits other than [3:0] are RAZ/WI.  */
6338     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6339     raw_write(env, ri, value & 0xf);
6340 
6341     /*
6342      * Because we arrived here, we know both FP and SVE are enabled;
6343      * otherwise we would have trapped access to the ZCR_ELn register.
6344      */
6345     new_len = sve_vqm1_for_el(env, cur_el);
6346     if (new_len < old_len) {
6347         aarch64_sve_narrow_vq(env, new_len + 1);
6348     }
6349 }
6350 
6351 static const ARMCPRegInfo zcr_reginfo[] = {
6352     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6353       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6354       .access = PL1_RW, .type = ARM_CP_SVE,
6355       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6356       .writefn = zcr_write, .raw_writefn = raw_write },
6357     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6358       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6359       .access = PL2_RW, .type = ARM_CP_SVE,
6360       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6361       .writefn = zcr_write, .raw_writefn = raw_write },
6362     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6363       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6364       .access = PL3_RW, .type = ARM_CP_SVE,
6365       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6366       .writefn = zcr_write, .raw_writefn = raw_write },
6367 };
6368 
6369 #ifdef TARGET_AARCH64
6370 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6371                                     bool isread)
6372 {
6373     int el = arm_current_el(env);
6374 
6375     if (el == 0) {
6376         uint64_t sctlr = arm_sctlr(env, el);
6377         if (!(sctlr & SCTLR_EnTP2)) {
6378             return CP_ACCESS_TRAP;
6379         }
6380     }
6381     /* TODO: FEAT_FGT */
6382     if (el < 3
6383         && arm_feature(env, ARM_FEATURE_EL3)
6384         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6385         return CP_ACCESS_TRAP_EL3;
6386     }
6387     return CP_ACCESS_OK;
6388 }
6389 
6390 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6391                                  bool isread)
6392 {
6393     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6394     if (arm_current_el(env) < 3
6395         && arm_feature(env, ARM_FEATURE_EL3)
6396         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6397         return CP_ACCESS_TRAP_EL3;
6398     }
6399     return CP_ACCESS_OK;
6400 }
6401 
6402 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6403                        uint64_t value)
6404 {
6405     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6406     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6407     arm_rebuild_hflags(env);
6408 }
6409 
6410 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6411                        uint64_t value)
6412 {
6413     int cur_el = arm_current_el(env);
6414     int old_len = sve_vqm1_for_el(env, cur_el);
6415     int new_len;
6416 
6417     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6418     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6419     raw_write(env, ri, value);
6420 
6421     /*
6422      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6423      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6424      * current values for simplicity.  But for QEMU internals, we must still
6425      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6426      * above aarch64_sve_narrow_vq.
6427      */
6428     new_len = sve_vqm1_for_el(env, cur_el);
6429     if (new_len < old_len) {
6430         aarch64_sve_narrow_vq(env, new_len + 1);
6431     }
6432 }
6433 
6434 static const ARMCPRegInfo sme_reginfo[] = {
6435     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6436       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6437       .access = PL0_RW, .accessfn = access_tpidr2,
6438       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6439     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6440       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6441       .access = PL0_RW, .type = ARM_CP_SME,
6442       .fieldoffset = offsetof(CPUARMState, svcr),
6443       .writefn = svcr_write, .raw_writefn = raw_write },
6444     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6445       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6446       .access = PL1_RW, .type = ARM_CP_SME,
6447       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6448       .writefn = smcr_write, .raw_writefn = raw_write },
6449     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6450       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6451       .access = PL2_RW, .type = ARM_CP_SME,
6452       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6453       .writefn = smcr_write, .raw_writefn = raw_write },
6454     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6455       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6456       .access = PL3_RW, .type = ARM_CP_SME,
6457       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6458       .writefn = smcr_write, .raw_writefn = raw_write },
6459     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6460       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6461       .access = PL1_R, .accessfn = access_aa64_tid1,
6462       /*
6463        * IMPLEMENTOR = 0 (software)
6464        * REVISION    = 0 (implementation defined)
6465        * SMPS        = 0 (no streaming execution priority in QEMU)
6466        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6467        */
6468       .type = ARM_CP_CONST, .resetvalue = 0, },
6469     /*
6470      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6471      */
6472     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6473       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6474       .access = PL1_RW, .accessfn = access_esm,
6475       .type = ARM_CP_CONST, .resetvalue = 0 },
6476     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6477       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6478       .access = PL2_RW, .accessfn = access_esm,
6479       .type = ARM_CP_CONST, .resetvalue = 0 },
6480 };
6481 #endif /* TARGET_AARCH64 */
6482 
6483 static void define_pmu_regs(ARMCPU *cpu)
6484 {
6485     /*
6486      * v7 performance monitor control register: same implementor
6487      * field as main ID register, and we implement four counters in
6488      * addition to the cycle count register.
6489      */
6490     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6491     ARMCPRegInfo pmcr = {
6492         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6493         .access = PL0_RW,
6494         .type = ARM_CP_IO | ARM_CP_ALIAS,
6495         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6496         .accessfn = pmreg_access, .writefn = pmcr_write,
6497         .raw_writefn = raw_write,
6498     };
6499     ARMCPRegInfo pmcr64 = {
6500         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6501         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6502         .access = PL0_RW, .accessfn = pmreg_access,
6503         .type = ARM_CP_IO,
6504         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6505         .resetvalue = cpu->isar.reset_pmcr_el0,
6506         .writefn = pmcr_write, .raw_writefn = raw_write,
6507     };
6508 
6509     define_one_arm_cp_reg(cpu, &pmcr);
6510     define_one_arm_cp_reg(cpu, &pmcr64);
6511     for (i = 0; i < pmcrn; i++) {
6512         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6513         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6514         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6515         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6516         ARMCPRegInfo pmev_regs[] = {
6517             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6518               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6519               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6520               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6521               .accessfn = pmreg_access_xevcntr },
6522             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6523               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6524               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6525               .type = ARM_CP_IO,
6526               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6527               .raw_readfn = pmevcntr_rawread,
6528               .raw_writefn = pmevcntr_rawwrite },
6529             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6530               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6531               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6532               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6533               .accessfn = pmreg_access },
6534             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6535               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6536               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6537               .type = ARM_CP_IO,
6538               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6539               .raw_writefn = pmevtyper_rawwrite },
6540         };
6541         define_arm_cp_regs(cpu, pmev_regs);
6542         g_free(pmevcntr_name);
6543         g_free(pmevcntr_el0_name);
6544         g_free(pmevtyper_name);
6545         g_free(pmevtyper_el0_name);
6546     }
6547     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6548         ARMCPRegInfo v81_pmu_regs[] = {
6549             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6550               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6551               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6552               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6553             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6554               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6555               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6556               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6557         };
6558         define_arm_cp_regs(cpu, v81_pmu_regs);
6559     }
6560     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6561         static const ARMCPRegInfo v84_pmmir = {
6562             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6563             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6564             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6565             .resetvalue = 0
6566         };
6567         define_one_arm_cp_reg(cpu, &v84_pmmir);
6568     }
6569 }
6570 
6571 /* We don't know until after realize whether there's a GICv3
6572  * attached, and that is what registers the gicv3 sysregs.
6573  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6574  * at runtime.
6575  */
6576 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6577 {
6578     ARMCPU *cpu = env_archcpu(env);
6579     uint64_t pfr1 = cpu->isar.id_pfr1;
6580 
6581     if (env->gicv3state) {
6582         pfr1 |= 1 << 28;
6583     }
6584     return pfr1;
6585 }
6586 
6587 #ifndef CONFIG_USER_ONLY
6588 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6589 {
6590     ARMCPU *cpu = env_archcpu(env);
6591     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6592 
6593     if (env->gicv3state) {
6594         pfr0 |= 1 << 24;
6595     }
6596     return pfr0;
6597 }
6598 #endif
6599 
6600 /* Shared logic between LORID and the rest of the LOR* registers.
6601  * Secure state exclusion has already been dealt with.
6602  */
6603 static CPAccessResult access_lor_ns(CPUARMState *env,
6604                                     const ARMCPRegInfo *ri, bool isread)
6605 {
6606     int el = arm_current_el(env);
6607 
6608     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6609         return CP_ACCESS_TRAP_EL2;
6610     }
6611     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6612         return CP_ACCESS_TRAP_EL3;
6613     }
6614     return CP_ACCESS_OK;
6615 }
6616 
6617 static CPAccessResult access_lor_other(CPUARMState *env,
6618                                        const ARMCPRegInfo *ri, bool isread)
6619 {
6620     if (arm_is_secure_below_el3(env)) {
6621         /* Access denied in secure mode.  */
6622         return CP_ACCESS_TRAP;
6623     }
6624     return access_lor_ns(env, ri, isread);
6625 }
6626 
6627 /*
6628  * A trivial implementation of ARMv8.1-LOR leaves all of these
6629  * registers fixed at 0, which indicates that there are zero
6630  * supported Limited Ordering regions.
6631  */
6632 static const ARMCPRegInfo lor_reginfo[] = {
6633     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6634       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6635       .access = PL1_RW, .accessfn = access_lor_other,
6636       .type = ARM_CP_CONST, .resetvalue = 0 },
6637     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6638       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6639       .access = PL1_RW, .accessfn = access_lor_other,
6640       .type = ARM_CP_CONST, .resetvalue = 0 },
6641     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6642       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6643       .access = PL1_RW, .accessfn = access_lor_other,
6644       .type = ARM_CP_CONST, .resetvalue = 0 },
6645     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6646       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6647       .access = PL1_RW, .accessfn = access_lor_other,
6648       .type = ARM_CP_CONST, .resetvalue = 0 },
6649     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6650       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6651       .access = PL1_R, .accessfn = access_lor_ns,
6652       .type = ARM_CP_CONST, .resetvalue = 0 },
6653 };
6654 
6655 #ifdef TARGET_AARCH64
6656 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6657                                    bool isread)
6658 {
6659     int el = arm_current_el(env);
6660 
6661     if (el < 2 &&
6662         arm_is_el2_enabled(env) &&
6663         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6664         return CP_ACCESS_TRAP_EL2;
6665     }
6666     if (el < 3 &&
6667         arm_feature(env, ARM_FEATURE_EL3) &&
6668         !(env->cp15.scr_el3 & SCR_APK)) {
6669         return CP_ACCESS_TRAP_EL3;
6670     }
6671     return CP_ACCESS_OK;
6672 }
6673 
6674 static const ARMCPRegInfo pauth_reginfo[] = {
6675     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6676       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6677       .access = PL1_RW, .accessfn = access_pauth,
6678       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6679     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6680       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6681       .access = PL1_RW, .accessfn = access_pauth,
6682       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6683     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6684       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6685       .access = PL1_RW, .accessfn = access_pauth,
6686       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6687     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6688       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6689       .access = PL1_RW, .accessfn = access_pauth,
6690       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6691     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6692       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6693       .access = PL1_RW, .accessfn = access_pauth,
6694       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6695     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6696       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6697       .access = PL1_RW, .accessfn = access_pauth,
6698       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6699     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6700       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6701       .access = PL1_RW, .accessfn = access_pauth,
6702       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6703     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6704       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6705       .access = PL1_RW, .accessfn = access_pauth,
6706       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6707     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6708       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6709       .access = PL1_RW, .accessfn = access_pauth,
6710       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6711     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6712       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6713       .access = PL1_RW, .accessfn = access_pauth,
6714       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6715 };
6716 
6717 static const ARMCPRegInfo tlbirange_reginfo[] = {
6718     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6719       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6720       .access = PL1_W, .type = ARM_CP_NO_RAW,
6721       .writefn = tlbi_aa64_rvae1is_write },
6722     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6723       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6724       .access = PL1_W, .type = ARM_CP_NO_RAW,
6725       .writefn = tlbi_aa64_rvae1is_write },
6726    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6727       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6728       .access = PL1_W, .type = ARM_CP_NO_RAW,
6729       .writefn = tlbi_aa64_rvae1is_write },
6730     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6731       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6732       .access = PL1_W, .type = ARM_CP_NO_RAW,
6733       .writefn = tlbi_aa64_rvae1is_write },
6734     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6735       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6736       .access = PL1_W, .type = ARM_CP_NO_RAW,
6737       .writefn = tlbi_aa64_rvae1is_write },
6738     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6739       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6740       .access = PL1_W, .type = ARM_CP_NO_RAW,
6741       .writefn = tlbi_aa64_rvae1is_write },
6742    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6743       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6744       .access = PL1_W, .type = ARM_CP_NO_RAW,
6745       .writefn = tlbi_aa64_rvae1is_write },
6746     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6747       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6748       .access = PL1_W, .type = ARM_CP_NO_RAW,
6749       .writefn = tlbi_aa64_rvae1is_write },
6750     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6751       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6752       .access = PL1_W, .type = ARM_CP_NO_RAW,
6753       .writefn = tlbi_aa64_rvae1_write },
6754     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6755       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6756       .access = PL1_W, .type = ARM_CP_NO_RAW,
6757       .writefn = tlbi_aa64_rvae1_write },
6758    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6759       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6760       .access = PL1_W, .type = ARM_CP_NO_RAW,
6761       .writefn = tlbi_aa64_rvae1_write },
6762     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6763       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6764       .access = PL1_W, .type = ARM_CP_NO_RAW,
6765       .writefn = tlbi_aa64_rvae1_write },
6766     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6767       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6768       .access = PL2_W, .type = ARM_CP_NO_RAW,
6769       .writefn = tlbi_aa64_ripas2e1is_write },
6770     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6771       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6772       .access = PL2_W, .type = ARM_CP_NO_RAW,
6773       .writefn = tlbi_aa64_ripas2e1is_write },
6774     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6775       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6776       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6777       .writefn = tlbi_aa64_rvae2is_write },
6778    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6779       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6780       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6781       .writefn = tlbi_aa64_rvae2is_write },
6782     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6784       .access = PL2_W, .type = ARM_CP_NO_RAW,
6785       .writefn = tlbi_aa64_ripas2e1_write },
6786     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6787       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6788       .access = PL2_W, .type = ARM_CP_NO_RAW,
6789       .writefn = tlbi_aa64_ripas2e1_write },
6790    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6791       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6792       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6793       .writefn = tlbi_aa64_rvae2is_write },
6794    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6795       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6796       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6797       .writefn = tlbi_aa64_rvae2is_write },
6798     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6799       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6800       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6801       .writefn = tlbi_aa64_rvae2_write },
6802    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6804       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6805       .writefn = tlbi_aa64_rvae2_write },
6806    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6807       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6808       .access = PL3_W, .type = ARM_CP_NO_RAW,
6809       .writefn = tlbi_aa64_rvae3is_write },
6810    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6811       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6812       .access = PL3_W, .type = ARM_CP_NO_RAW,
6813       .writefn = tlbi_aa64_rvae3is_write },
6814    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6815       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6816       .access = PL3_W, .type = ARM_CP_NO_RAW,
6817       .writefn = tlbi_aa64_rvae3is_write },
6818    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6819       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6820       .access = PL3_W, .type = ARM_CP_NO_RAW,
6821       .writefn = tlbi_aa64_rvae3is_write },
6822    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6824       .access = PL3_W, .type = ARM_CP_NO_RAW,
6825       .writefn = tlbi_aa64_rvae3_write },
6826    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6827       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6828       .access = PL3_W, .type = ARM_CP_NO_RAW,
6829       .writefn = tlbi_aa64_rvae3_write },
6830 };
6831 
6832 static const ARMCPRegInfo tlbios_reginfo[] = {
6833     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6834       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6835       .access = PL1_W, .type = ARM_CP_NO_RAW,
6836       .writefn = tlbi_aa64_vmalle1is_write },
6837     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6838       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6839       .access = PL1_W, .type = ARM_CP_NO_RAW,
6840       .writefn = tlbi_aa64_vae1is_write },
6841     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6842       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6843       .access = PL1_W, .type = ARM_CP_NO_RAW,
6844       .writefn = tlbi_aa64_vmalle1is_write },
6845     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6846       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6847       .access = PL1_W, .type = ARM_CP_NO_RAW,
6848       .writefn = tlbi_aa64_vae1is_write },
6849     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6850       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6851       .access = PL1_W, .type = ARM_CP_NO_RAW,
6852       .writefn = tlbi_aa64_vae1is_write },
6853     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6854       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6855       .access = PL1_W, .type = ARM_CP_NO_RAW,
6856       .writefn = tlbi_aa64_vae1is_write },
6857     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6858       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6859       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6860       .writefn = tlbi_aa64_alle2is_write },
6861     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6862       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6863       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6864       .writefn = tlbi_aa64_vae2is_write },
6865    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6866       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6867       .access = PL2_W, .type = ARM_CP_NO_RAW,
6868       .writefn = tlbi_aa64_alle1is_write },
6869     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6870       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6871       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6872       .writefn = tlbi_aa64_vae2is_write },
6873     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6874       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6875       .access = PL2_W, .type = ARM_CP_NO_RAW,
6876       .writefn = tlbi_aa64_alle1is_write },
6877     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6878       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6879       .access = PL2_W, .type = ARM_CP_NOP },
6880     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6881       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6882       .access = PL2_W, .type = ARM_CP_NOP },
6883     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6884       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6885       .access = PL2_W, .type = ARM_CP_NOP },
6886     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6887       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6888       .access = PL2_W, .type = ARM_CP_NOP },
6889     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6890       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6891       .access = PL3_W, .type = ARM_CP_NO_RAW,
6892       .writefn = tlbi_aa64_alle3is_write },
6893     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6894       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6895       .access = PL3_W, .type = ARM_CP_NO_RAW,
6896       .writefn = tlbi_aa64_vae3is_write },
6897     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6898       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6899       .access = PL3_W, .type = ARM_CP_NO_RAW,
6900       .writefn = tlbi_aa64_vae3is_write },
6901 };
6902 
6903 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6904 {
6905     Error *err = NULL;
6906     uint64_t ret;
6907 
6908     /* Success sets NZCV = 0000.  */
6909     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6910 
6911     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6912         /*
6913          * ??? Failed, for unknown reasons in the crypto subsystem.
6914          * The best we can do is log the reason and return the
6915          * timed-out indication to the guest.  There is no reason
6916          * we know to expect this failure to be transitory, so the
6917          * guest may well hang retrying the operation.
6918          */
6919         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6920                       ri->name, error_get_pretty(err));
6921         error_free(err);
6922 
6923         env->ZF = 0; /* NZCF = 0100 */
6924         return 0;
6925     }
6926     return ret;
6927 }
6928 
6929 /* We do not support re-seeding, so the two registers operate the same.  */
6930 static const ARMCPRegInfo rndr_reginfo[] = {
6931     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6932       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6933       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6934       .access = PL0_R, .readfn = rndr_readfn },
6935     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6936       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6937       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6938       .access = PL0_R, .readfn = rndr_readfn },
6939 };
6940 
6941 #ifndef CONFIG_USER_ONLY
6942 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6943                           uint64_t value)
6944 {
6945     ARMCPU *cpu = env_archcpu(env);
6946     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6947     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6948     uint64_t vaddr_in = (uint64_t) value;
6949     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6950     void *haddr;
6951     int mem_idx = cpu_mmu_index(env, false);
6952 
6953     /* This won't be crossing page boundaries */
6954     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6955     if (haddr) {
6956 
6957         ram_addr_t offset;
6958         MemoryRegion *mr;
6959 
6960         /* RCU lock is already being held */
6961         mr = memory_region_from_host(haddr, &offset);
6962 
6963         if (mr) {
6964             memory_region_writeback(mr, offset, dline_size);
6965         }
6966     }
6967 }
6968 
6969 static const ARMCPRegInfo dcpop_reg[] = {
6970     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6971       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6972       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6973       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6974 };
6975 
6976 static const ARMCPRegInfo dcpodp_reg[] = {
6977     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6978       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6979       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6980       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6981 };
6982 #endif /*CONFIG_USER_ONLY*/
6983 
6984 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6985                                        bool isread)
6986 {
6987     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6988         return CP_ACCESS_TRAP_EL2;
6989     }
6990 
6991     return CP_ACCESS_OK;
6992 }
6993 
6994 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6995                                  bool isread)
6996 {
6997     int el = arm_current_el(env);
6998 
6999     if (el < 2 && arm_is_el2_enabled(env)) {
7000         uint64_t hcr = arm_hcr_el2_eff(env);
7001         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7002             return CP_ACCESS_TRAP_EL2;
7003         }
7004     }
7005     if (el < 3 &&
7006         arm_feature(env, ARM_FEATURE_EL3) &&
7007         !(env->cp15.scr_el3 & SCR_ATA)) {
7008         return CP_ACCESS_TRAP_EL3;
7009     }
7010     return CP_ACCESS_OK;
7011 }
7012 
7013 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7014 {
7015     return env->pstate & PSTATE_TCO;
7016 }
7017 
7018 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7019 {
7020     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7021 }
7022 
7023 static const ARMCPRegInfo mte_reginfo[] = {
7024     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7025       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7026       .access = PL1_RW, .accessfn = access_mte,
7027       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7028     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7029       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7030       .access = PL1_RW, .accessfn = access_mte,
7031       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7032     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7033       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7034       .access = PL2_RW, .accessfn = access_mte,
7035       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7036     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7038       .access = PL3_RW,
7039       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7040     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7042       .access = PL1_RW, .accessfn = access_mte,
7043       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7044     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7046       .access = PL1_RW, .accessfn = access_mte,
7047       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7048     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7050       .access = PL1_R, .accessfn = access_aa64_tid5,
7051       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7052     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7053       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7054       .type = ARM_CP_NO_RAW,
7055       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7056     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7058       .type = ARM_CP_NOP, .access = PL1_W,
7059       .accessfn = aa64_cacheop_poc_access },
7060     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7062       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7063     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7064       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7065       .type = ARM_CP_NOP, .access = PL1_W,
7066       .accessfn = aa64_cacheop_poc_access },
7067     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7068       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7069       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7070     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7071       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7072       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7073     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7074       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7075       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7076     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7078       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7079     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7081       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7082 };
7083 
7084 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7085     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7086       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7087       .type = ARM_CP_CONST, .access = PL0_RW, },
7088 };
7089 
7090 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7091     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7092       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7093       .type = ARM_CP_NOP, .access = PL0_W,
7094       .accessfn = aa64_cacheop_poc_access },
7095     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7096       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7097       .type = ARM_CP_NOP, .access = PL0_W,
7098       .accessfn = aa64_cacheop_poc_access },
7099     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7100       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7101       .type = ARM_CP_NOP, .access = PL0_W,
7102       .accessfn = aa64_cacheop_poc_access },
7103     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7105       .type = ARM_CP_NOP, .access = PL0_W,
7106       .accessfn = aa64_cacheop_poc_access },
7107     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7108       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7109       .type = ARM_CP_NOP, .access = PL0_W,
7110       .accessfn = aa64_cacheop_poc_access },
7111     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7113       .type = ARM_CP_NOP, .access = PL0_W,
7114       .accessfn = aa64_cacheop_poc_access },
7115     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7116       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7117       .type = ARM_CP_NOP, .access = PL0_W,
7118       .accessfn = aa64_cacheop_poc_access },
7119     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7121       .type = ARM_CP_NOP, .access = PL0_W,
7122       .accessfn = aa64_cacheop_poc_access },
7123     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7125       .access = PL0_W, .type = ARM_CP_DC_GVA,
7126 #ifndef CONFIG_USER_ONLY
7127       /* Avoid overhead of an access check that always passes in user-mode */
7128       .accessfn = aa64_zva_access,
7129 #endif
7130     },
7131     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7133       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7134 #ifndef CONFIG_USER_ONLY
7135       /* Avoid overhead of an access check that always passes in user-mode */
7136       .accessfn = aa64_zva_access,
7137 #endif
7138     },
7139 };
7140 
7141 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7142                                      bool isread)
7143 {
7144     uint64_t hcr = arm_hcr_el2_eff(env);
7145     int el = arm_current_el(env);
7146 
7147     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7148         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7149             if (hcr & HCR_TGE) {
7150                 return CP_ACCESS_TRAP_EL2;
7151             }
7152             return CP_ACCESS_TRAP;
7153         }
7154     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7155         return CP_ACCESS_TRAP_EL2;
7156     }
7157     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7158         return CP_ACCESS_TRAP_EL2;
7159     }
7160     if (el < 3
7161         && arm_feature(env, ARM_FEATURE_EL3)
7162         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7163         return CP_ACCESS_TRAP_EL3;
7164     }
7165     return CP_ACCESS_OK;
7166 }
7167 
7168 static const ARMCPRegInfo scxtnum_reginfo[] = {
7169     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7170       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7171       .access = PL0_RW, .accessfn = access_scxtnum,
7172       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7173     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7174       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7175       .access = PL1_RW, .accessfn = access_scxtnum,
7176       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7177     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7178       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7179       .access = PL2_RW, .accessfn = access_scxtnum,
7180       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7181     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7182       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7183       .access = PL3_RW,
7184       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7185 };
7186 #endif /* TARGET_AARCH64 */
7187 
7188 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7189                                      bool isread)
7190 {
7191     int el = arm_current_el(env);
7192 
7193     if (el == 0) {
7194         uint64_t sctlr = arm_sctlr(env, el);
7195         if (!(sctlr & SCTLR_EnRCTX)) {
7196             return CP_ACCESS_TRAP;
7197         }
7198     } else if (el == 1) {
7199         uint64_t hcr = arm_hcr_el2_eff(env);
7200         if (hcr & HCR_NV) {
7201             return CP_ACCESS_TRAP_EL2;
7202         }
7203     }
7204     return CP_ACCESS_OK;
7205 }
7206 
7207 static const ARMCPRegInfo predinv_reginfo[] = {
7208     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7209       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7210       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7211     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7212       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7213       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7214     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7215       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7216       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7217     /*
7218      * Note the AArch32 opcodes have a different OPC1.
7219      */
7220     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7221       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7222       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7223     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7224       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7225       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7226     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7227       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7228       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7229 };
7230 
7231 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7232 {
7233     /* Read the high 32 bits of the current CCSIDR */
7234     return extract64(ccsidr_read(env, ri), 32, 32);
7235 }
7236 
7237 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7238     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7239       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7240       .access = PL1_R,
7241       .accessfn = access_aa64_tid2,
7242       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7243 };
7244 
7245 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7246                                        bool isread)
7247 {
7248     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7249         return CP_ACCESS_TRAP_EL2;
7250     }
7251 
7252     return CP_ACCESS_OK;
7253 }
7254 
7255 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7256                                        bool isread)
7257 {
7258     if (arm_feature(env, ARM_FEATURE_V8)) {
7259         return access_aa64_tid3(env, ri, isread);
7260     }
7261 
7262     return CP_ACCESS_OK;
7263 }
7264 
7265 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7266                                      bool isread)
7267 {
7268     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7269         return CP_ACCESS_TRAP_EL2;
7270     }
7271 
7272     return CP_ACCESS_OK;
7273 }
7274 
7275 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7276                                         const ARMCPRegInfo *ri, bool isread)
7277 {
7278     /*
7279      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7280      * in v7A, not in v8A.
7281      */
7282     if (!arm_feature(env, ARM_FEATURE_V8) &&
7283         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7284         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7285         return CP_ACCESS_TRAP_EL2;
7286     }
7287     return CP_ACCESS_OK;
7288 }
7289 
7290 static const ARMCPRegInfo jazelle_regs[] = {
7291     { .name = "JIDR",
7292       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7293       .access = PL1_R, .accessfn = access_jazelle,
7294       .type = ARM_CP_CONST, .resetvalue = 0 },
7295     { .name = "JOSCR",
7296       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7297       .accessfn = access_joscr_jmcr,
7298       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7299     { .name = "JMCR",
7300       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7301       .accessfn = access_joscr_jmcr,
7302       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7303 };
7304 
7305 static const ARMCPRegInfo contextidr_el2 = {
7306     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7307     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7308     .access = PL2_RW,
7309     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7310 };
7311 
7312 static const ARMCPRegInfo vhe_reginfo[] = {
7313     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7314       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7315       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7316       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7317 #ifndef CONFIG_USER_ONLY
7318     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7319       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7320       .fieldoffset =
7321         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7322       .type = ARM_CP_IO, .access = PL2_RW,
7323       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7324     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7325       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7326       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7327       .resetfn = gt_hv_timer_reset,
7328       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7329     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7330       .type = ARM_CP_IO,
7331       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7332       .access = PL2_RW,
7333       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7334       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7335     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7336       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7337       .type = ARM_CP_IO | ARM_CP_ALIAS,
7338       .access = PL2_RW, .accessfn = e2h_access,
7339       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7340       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7341     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7342       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7343       .type = ARM_CP_IO | ARM_CP_ALIAS,
7344       .access = PL2_RW, .accessfn = e2h_access,
7345       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7346       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7347     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7348       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7349       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7350       .access = PL2_RW, .accessfn = e2h_access,
7351       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7352     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7353       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7354       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7355       .access = PL2_RW, .accessfn = e2h_access,
7356       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7357     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7358       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7359       .type = ARM_CP_IO | ARM_CP_ALIAS,
7360       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7361       .access = PL2_RW, .accessfn = e2h_access,
7362       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7363     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7364       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7365       .type = ARM_CP_IO | ARM_CP_ALIAS,
7366       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7367       .access = PL2_RW, .accessfn = e2h_access,
7368       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7369 #endif
7370 };
7371 
7372 #ifndef CONFIG_USER_ONLY
7373 static const ARMCPRegInfo ats1e1_reginfo[] = {
7374     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7375       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7376       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7377       .writefn = ats_write64 },
7378     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7379       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7380       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7381       .writefn = ats_write64 },
7382 };
7383 
7384 static const ARMCPRegInfo ats1cp_reginfo[] = {
7385     { .name = "ATS1CPRP",
7386       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7387       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7388       .writefn = ats_write },
7389     { .name = "ATS1CPWP",
7390       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7391       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7392       .writefn = ats_write },
7393 };
7394 #endif
7395 
7396 /*
7397  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7398  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7399  * is non-zero, which is never for ARMv7, optionally in ARMv8
7400  * and mandatorily for ARMv8.2 and up.
7401  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7402  * implementation is RAZ/WI we can ignore this detail, as we
7403  * do for ACTLR.
7404  */
7405 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7406     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7407       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7408       .access = PL1_RW, .accessfn = access_tacr,
7409       .type = ARM_CP_CONST, .resetvalue = 0 },
7410     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7411       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7412       .access = PL2_RW, .type = ARM_CP_CONST,
7413       .resetvalue = 0 },
7414 };
7415 
7416 void register_cp_regs_for_features(ARMCPU *cpu)
7417 {
7418     /* Register all the coprocessor registers based on feature bits */
7419     CPUARMState *env = &cpu->env;
7420     if (arm_feature(env, ARM_FEATURE_M)) {
7421         /* M profile has no coprocessor registers */
7422         return;
7423     }
7424 
7425     define_arm_cp_regs(cpu, cp_reginfo);
7426     if (!arm_feature(env, ARM_FEATURE_V8)) {
7427         /* Must go early as it is full of wildcards that may be
7428          * overridden by later definitions.
7429          */
7430         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7431     }
7432 
7433     if (arm_feature(env, ARM_FEATURE_V6)) {
7434         /* The ID registers all have impdef reset values */
7435         ARMCPRegInfo v6_idregs[] = {
7436             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7437               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7438               .access = PL1_R, .type = ARM_CP_CONST,
7439               .accessfn = access_aa32_tid3,
7440               .resetvalue = cpu->isar.id_pfr0 },
7441             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7442              * the value of the GIC field until after we define these regs.
7443              */
7444             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7445               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7446               .access = PL1_R, .type = ARM_CP_NO_RAW,
7447               .accessfn = access_aa32_tid3,
7448               .readfn = id_pfr1_read,
7449               .writefn = arm_cp_write_ignore },
7450             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7451               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7452               .access = PL1_R, .type = ARM_CP_CONST,
7453               .accessfn = access_aa32_tid3,
7454               .resetvalue = cpu->isar.id_dfr0 },
7455             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7456               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7457               .access = PL1_R, .type = ARM_CP_CONST,
7458               .accessfn = access_aa32_tid3,
7459               .resetvalue = cpu->id_afr0 },
7460             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7461               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7462               .access = PL1_R, .type = ARM_CP_CONST,
7463               .accessfn = access_aa32_tid3,
7464               .resetvalue = cpu->isar.id_mmfr0 },
7465             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7466               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7467               .access = PL1_R, .type = ARM_CP_CONST,
7468               .accessfn = access_aa32_tid3,
7469               .resetvalue = cpu->isar.id_mmfr1 },
7470             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7471               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7472               .access = PL1_R, .type = ARM_CP_CONST,
7473               .accessfn = access_aa32_tid3,
7474               .resetvalue = cpu->isar.id_mmfr2 },
7475             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7476               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7477               .access = PL1_R, .type = ARM_CP_CONST,
7478               .accessfn = access_aa32_tid3,
7479               .resetvalue = cpu->isar.id_mmfr3 },
7480             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7481               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7482               .access = PL1_R, .type = ARM_CP_CONST,
7483               .accessfn = access_aa32_tid3,
7484               .resetvalue = cpu->isar.id_isar0 },
7485             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7486               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7487               .access = PL1_R, .type = ARM_CP_CONST,
7488               .accessfn = access_aa32_tid3,
7489               .resetvalue = cpu->isar.id_isar1 },
7490             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7491               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7492               .access = PL1_R, .type = ARM_CP_CONST,
7493               .accessfn = access_aa32_tid3,
7494               .resetvalue = cpu->isar.id_isar2 },
7495             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7496               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7497               .access = PL1_R, .type = ARM_CP_CONST,
7498               .accessfn = access_aa32_tid3,
7499               .resetvalue = cpu->isar.id_isar3 },
7500             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7501               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7502               .access = PL1_R, .type = ARM_CP_CONST,
7503               .accessfn = access_aa32_tid3,
7504               .resetvalue = cpu->isar.id_isar4 },
7505             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7506               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7507               .access = PL1_R, .type = ARM_CP_CONST,
7508               .accessfn = access_aa32_tid3,
7509               .resetvalue = cpu->isar.id_isar5 },
7510             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7511               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7512               .access = PL1_R, .type = ARM_CP_CONST,
7513               .accessfn = access_aa32_tid3,
7514               .resetvalue = cpu->isar.id_mmfr4 },
7515             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7516               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7517               .access = PL1_R, .type = ARM_CP_CONST,
7518               .accessfn = access_aa32_tid3,
7519               .resetvalue = cpu->isar.id_isar6 },
7520         };
7521         define_arm_cp_regs(cpu, v6_idregs);
7522         define_arm_cp_regs(cpu, v6_cp_reginfo);
7523     } else {
7524         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7525     }
7526     if (arm_feature(env, ARM_FEATURE_V6K)) {
7527         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7528     }
7529     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7530         !arm_feature(env, ARM_FEATURE_PMSA)) {
7531         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7532     }
7533     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7534         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7535     }
7536     if (arm_feature(env, ARM_FEATURE_V7)) {
7537         ARMCPRegInfo clidr = {
7538             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7539             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7540             .access = PL1_R, .type = ARM_CP_CONST,
7541             .accessfn = access_aa64_tid2,
7542             .resetvalue = cpu->clidr
7543         };
7544         define_one_arm_cp_reg(cpu, &clidr);
7545         define_arm_cp_regs(cpu, v7_cp_reginfo);
7546         define_debug_regs(cpu);
7547         define_pmu_regs(cpu);
7548     } else {
7549         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7550     }
7551     if (arm_feature(env, ARM_FEATURE_V8)) {
7552         /*
7553          * v8 ID registers, which all have impdef reset values.
7554          * Note that within the ID register ranges the unused slots
7555          * must all RAZ, not UNDEF; future architecture versions may
7556          * define new registers here.
7557          * ID registers which are AArch64 views of the AArch32 ID registers
7558          * which already existed in v6 and v7 are handled elsewhere,
7559          * in v6_idregs[].
7560          */
7561         int i;
7562         ARMCPRegInfo v8_idregs[] = {
7563             /*
7564              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7565              * emulation because we don't know the right value for the
7566              * GIC field until after we define these regs.
7567              */
7568             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7569               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7570               .access = PL1_R,
7571 #ifdef CONFIG_USER_ONLY
7572               .type = ARM_CP_CONST,
7573               .resetvalue = cpu->isar.id_aa64pfr0
7574 #else
7575               .type = ARM_CP_NO_RAW,
7576               .accessfn = access_aa64_tid3,
7577               .readfn = id_aa64pfr0_read,
7578               .writefn = arm_cp_write_ignore
7579 #endif
7580             },
7581             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7582               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7583               .access = PL1_R, .type = ARM_CP_CONST,
7584               .accessfn = access_aa64_tid3,
7585               .resetvalue = cpu->isar.id_aa64pfr1},
7586             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7587               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7588               .access = PL1_R, .type = ARM_CP_CONST,
7589               .accessfn = access_aa64_tid3,
7590               .resetvalue = 0 },
7591             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7592               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7593               .access = PL1_R, .type = ARM_CP_CONST,
7594               .accessfn = access_aa64_tid3,
7595               .resetvalue = 0 },
7596             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7597               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7598               .access = PL1_R, .type = ARM_CP_CONST,
7599               .accessfn = access_aa64_tid3,
7600               .resetvalue = cpu->isar.id_aa64zfr0 },
7601             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7602               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7603               .access = PL1_R, .type = ARM_CP_CONST,
7604               .accessfn = access_aa64_tid3,
7605               .resetvalue = cpu->isar.id_aa64smfr0 },
7606             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7607               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7608               .access = PL1_R, .type = ARM_CP_CONST,
7609               .accessfn = access_aa64_tid3,
7610               .resetvalue = 0 },
7611             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7612               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7613               .access = PL1_R, .type = ARM_CP_CONST,
7614               .accessfn = access_aa64_tid3,
7615               .resetvalue = 0 },
7616             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7617               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7618               .access = PL1_R, .type = ARM_CP_CONST,
7619               .accessfn = access_aa64_tid3,
7620               .resetvalue = cpu->isar.id_aa64dfr0 },
7621             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7622               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7623               .access = PL1_R, .type = ARM_CP_CONST,
7624               .accessfn = access_aa64_tid3,
7625               .resetvalue = cpu->isar.id_aa64dfr1 },
7626             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7627               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7628               .access = PL1_R, .type = ARM_CP_CONST,
7629               .accessfn = access_aa64_tid3,
7630               .resetvalue = 0 },
7631             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7632               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7633               .access = PL1_R, .type = ARM_CP_CONST,
7634               .accessfn = access_aa64_tid3,
7635               .resetvalue = 0 },
7636             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7637               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7638               .access = PL1_R, .type = ARM_CP_CONST,
7639               .accessfn = access_aa64_tid3,
7640               .resetvalue = cpu->id_aa64afr0 },
7641             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7642               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7643               .access = PL1_R, .type = ARM_CP_CONST,
7644               .accessfn = access_aa64_tid3,
7645               .resetvalue = cpu->id_aa64afr1 },
7646             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7647               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7648               .access = PL1_R, .type = ARM_CP_CONST,
7649               .accessfn = access_aa64_tid3,
7650               .resetvalue = 0 },
7651             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7652               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7653               .access = PL1_R, .type = ARM_CP_CONST,
7654               .accessfn = access_aa64_tid3,
7655               .resetvalue = 0 },
7656             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7657               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7658               .access = PL1_R, .type = ARM_CP_CONST,
7659               .accessfn = access_aa64_tid3,
7660               .resetvalue = cpu->isar.id_aa64isar0 },
7661             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7662               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7663               .access = PL1_R, .type = ARM_CP_CONST,
7664               .accessfn = access_aa64_tid3,
7665               .resetvalue = cpu->isar.id_aa64isar1 },
7666             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7667               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7668               .access = PL1_R, .type = ARM_CP_CONST,
7669               .accessfn = access_aa64_tid3,
7670               .resetvalue = 0 },
7671             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7672               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7673               .access = PL1_R, .type = ARM_CP_CONST,
7674               .accessfn = access_aa64_tid3,
7675               .resetvalue = 0 },
7676             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7677               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7678               .access = PL1_R, .type = ARM_CP_CONST,
7679               .accessfn = access_aa64_tid3,
7680               .resetvalue = 0 },
7681             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7682               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7683               .access = PL1_R, .type = ARM_CP_CONST,
7684               .accessfn = access_aa64_tid3,
7685               .resetvalue = 0 },
7686             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7687               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7688               .access = PL1_R, .type = ARM_CP_CONST,
7689               .accessfn = access_aa64_tid3,
7690               .resetvalue = 0 },
7691             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7692               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7693               .access = PL1_R, .type = ARM_CP_CONST,
7694               .accessfn = access_aa64_tid3,
7695               .resetvalue = 0 },
7696             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7697               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7698               .access = PL1_R, .type = ARM_CP_CONST,
7699               .accessfn = access_aa64_tid3,
7700               .resetvalue = cpu->isar.id_aa64mmfr0 },
7701             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7702               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7703               .access = PL1_R, .type = ARM_CP_CONST,
7704               .accessfn = access_aa64_tid3,
7705               .resetvalue = cpu->isar.id_aa64mmfr1 },
7706             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7707               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7708               .access = PL1_R, .type = ARM_CP_CONST,
7709               .accessfn = access_aa64_tid3,
7710               .resetvalue = cpu->isar.id_aa64mmfr2 },
7711             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7712               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7713               .access = PL1_R, .type = ARM_CP_CONST,
7714               .accessfn = access_aa64_tid3,
7715               .resetvalue = 0 },
7716             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7717               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7718               .access = PL1_R, .type = ARM_CP_CONST,
7719               .accessfn = access_aa64_tid3,
7720               .resetvalue = 0 },
7721             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7722               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7723               .access = PL1_R, .type = ARM_CP_CONST,
7724               .accessfn = access_aa64_tid3,
7725               .resetvalue = 0 },
7726             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7727               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7728               .access = PL1_R, .type = ARM_CP_CONST,
7729               .accessfn = access_aa64_tid3,
7730               .resetvalue = 0 },
7731             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7732               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7733               .access = PL1_R, .type = ARM_CP_CONST,
7734               .accessfn = access_aa64_tid3,
7735               .resetvalue = 0 },
7736             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7737               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7738               .access = PL1_R, .type = ARM_CP_CONST,
7739               .accessfn = access_aa64_tid3,
7740               .resetvalue = cpu->isar.mvfr0 },
7741             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7742               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7743               .access = PL1_R, .type = ARM_CP_CONST,
7744               .accessfn = access_aa64_tid3,
7745               .resetvalue = cpu->isar.mvfr1 },
7746             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7747               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7748               .access = PL1_R, .type = ARM_CP_CONST,
7749               .accessfn = access_aa64_tid3,
7750               .resetvalue = cpu->isar.mvfr2 },
7751             /*
7752              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7753              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7754              * as RAZ, since it is in the "reserved for future ID
7755              * registers, RAZ" part of the AArch32 encoding space.
7756              */
7757             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7758               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7759               .access = PL1_R, .type = ARM_CP_CONST,
7760               .accessfn = access_aa64_tid3,
7761               .resetvalue = 0 },
7762             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7763               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7764               .access = PL1_R, .type = ARM_CP_CONST,
7765               .accessfn = access_aa64_tid3,
7766               .resetvalue = 0 },
7767             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7768               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7769               .access = PL1_R, .type = ARM_CP_CONST,
7770               .accessfn = access_aa64_tid3,
7771               .resetvalue = 0 },
7772             /*
7773              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7774              * they're also RAZ for AArch64, and in v8 are gradually
7775              * being filled with AArch64-view-of-AArch32-ID-register
7776              * for new ID registers.
7777              */
7778             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7779               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7780               .access = PL1_R, .type = ARM_CP_CONST,
7781               .accessfn = access_aa64_tid3,
7782               .resetvalue = 0 },
7783             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7785               .access = PL1_R, .type = ARM_CP_CONST,
7786               .accessfn = access_aa64_tid3,
7787               .resetvalue = cpu->isar.id_pfr2 },
7788             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7790               .access = PL1_R, .type = ARM_CP_CONST,
7791               .accessfn = access_aa64_tid3,
7792               .resetvalue = cpu->isar.id_dfr1 },
7793             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7794               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7795               .access = PL1_R, .type = ARM_CP_CONST,
7796               .accessfn = access_aa64_tid3,
7797               .resetvalue = cpu->isar.id_mmfr5 },
7798             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7799               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7800               .access = PL1_R, .type = ARM_CP_CONST,
7801               .accessfn = access_aa64_tid3,
7802               .resetvalue = 0 },
7803             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7804               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7805               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7806               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7807             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7808               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7809               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7810               .resetvalue = cpu->pmceid0 },
7811             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7812               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7813               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7814               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7815             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7816               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7817               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7818               .resetvalue = cpu->pmceid1 },
7819         };
7820 #ifdef CONFIG_USER_ONLY
7821         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7822             { .name = "ID_AA64PFR0_EL1",
7823               .exported_bits = 0x000f000f00ff0000,
7824               .fixed_bits    = 0x0000000000000011 },
7825             { .name = "ID_AA64PFR1_EL1",
7826               .exported_bits = 0x00000000000000f0 },
7827             { .name = "ID_AA64PFR*_EL1_RESERVED",
7828               .is_glob = true                     },
7829             { .name = "ID_AA64ZFR0_EL1"           },
7830             { .name = "ID_AA64MMFR0_EL1",
7831               .fixed_bits    = 0x00000000ff000000 },
7832             { .name = "ID_AA64MMFR1_EL1"          },
7833             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7834               .is_glob = true                     },
7835             { .name = "ID_AA64DFR0_EL1",
7836               .fixed_bits    = 0x0000000000000006 },
7837             { .name = "ID_AA64DFR1_EL1"           },
7838             { .name = "ID_AA64DFR*_EL1_RESERVED",
7839               .is_glob = true                     },
7840             { .name = "ID_AA64AFR*",
7841               .is_glob = true                     },
7842             { .name = "ID_AA64ISAR0_EL1",
7843               .exported_bits = 0x00fffffff0fffff0 },
7844             { .name = "ID_AA64ISAR1_EL1",
7845               .exported_bits = 0x000000f0ffffffff },
7846             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7847               .is_glob = true                     },
7848         };
7849         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7850 #endif
7851         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7852         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7853             !arm_feature(env, ARM_FEATURE_EL2)) {
7854             ARMCPRegInfo rvbar = {
7855                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7856                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7857                 .access = PL1_R,
7858                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7859             };
7860             define_one_arm_cp_reg(cpu, &rvbar);
7861         }
7862         define_arm_cp_regs(cpu, v8_idregs);
7863         define_arm_cp_regs(cpu, v8_cp_reginfo);
7864 
7865         for (i = 4; i < 16; i++) {
7866             /*
7867              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7868              * For pre-v8 cores there are RAZ patterns for these in
7869              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7870              * v8 extends the "must RAZ" part of the ID register space
7871              * to also cover c0, 0, c{8-15}, {0-7}.
7872              * These are STATE_AA32 because in the AArch64 sysreg space
7873              * c4-c7 is where the AArch64 ID registers live (and we've
7874              * already defined those in v8_idregs[]), and c8-c15 are not
7875              * "must RAZ" for AArch64.
7876              */
7877             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7878             ARMCPRegInfo v8_aa32_raz_idregs = {
7879                 .name = name,
7880                 .state = ARM_CP_STATE_AA32,
7881                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7882                 .access = PL1_R, .type = ARM_CP_CONST,
7883                 .accessfn = access_aa64_tid3,
7884                 .resetvalue = 0 };
7885             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7886         }
7887     }
7888 
7889     /*
7890      * Register the base EL2 cpregs.
7891      * Pre v8, these registers are implemented only as part of the
7892      * Virtualization Extensions (EL2 present).  Beginning with v8,
7893      * if EL2 is missing but EL3 is enabled, mostly these become
7894      * RES0 from EL3, with some specific exceptions.
7895      */
7896     if (arm_feature(env, ARM_FEATURE_EL2)
7897         || (arm_feature(env, ARM_FEATURE_EL3)
7898             && arm_feature(env, ARM_FEATURE_V8))) {
7899         uint64_t vmpidr_def = mpidr_read_val(env);
7900         ARMCPRegInfo vpidr_regs[] = {
7901             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7902               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7903               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7904               .resetvalue = cpu->midr,
7905               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7906               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7907             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7908               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7909               .access = PL2_RW, .resetvalue = cpu->midr,
7910               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7911               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7912             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7913               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7914               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7915               .resetvalue = vmpidr_def,
7916               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7917               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7918             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7919               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7920               .access = PL2_RW, .resetvalue = vmpidr_def,
7921               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7922               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7923         };
7924         /*
7925          * The only field of MDCR_EL2 that has a defined architectural reset
7926          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7927          */
7928         ARMCPRegInfo mdcr_el2 = {
7929             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
7930             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7931             .writefn = mdcr_el2_write,
7932             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7933             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7934         };
7935         define_one_arm_cp_reg(cpu, &mdcr_el2);
7936         define_arm_cp_regs(cpu, vpidr_regs);
7937         define_arm_cp_regs(cpu, el2_cp_reginfo);
7938         if (arm_feature(env, ARM_FEATURE_V8)) {
7939             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7940         }
7941         if (cpu_isar_feature(aa64_sel2, cpu)) {
7942             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7943         }
7944         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7945         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7946             ARMCPRegInfo rvbar = {
7947                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7948                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7949                 .access = PL2_R,
7950                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7951             };
7952             define_one_arm_cp_reg(cpu, &rvbar);
7953         }
7954     }
7955 
7956     /* Register the base EL3 cpregs. */
7957     if (arm_feature(env, ARM_FEATURE_EL3)) {
7958         define_arm_cp_regs(cpu, el3_cp_reginfo);
7959         ARMCPRegInfo el3_regs[] = {
7960             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7961               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7962               .access = PL3_R,
7963               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7964             },
7965             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7966               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7967               .access = PL3_RW,
7968               .raw_writefn = raw_write, .writefn = sctlr_write,
7969               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7970               .resetvalue = cpu->reset_sctlr },
7971         };
7972 
7973         define_arm_cp_regs(cpu, el3_regs);
7974     }
7975     /* The behaviour of NSACR is sufficiently various that we don't
7976      * try to describe it in a single reginfo:
7977      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7978      *     reads as constant 0xc00 from NS EL1 and NS EL2
7979      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7980      *  if v7 without EL3, register doesn't exist
7981      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7982      */
7983     if (arm_feature(env, ARM_FEATURE_EL3)) {
7984         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7985             static const ARMCPRegInfo nsacr = {
7986                 .name = "NSACR", .type = ARM_CP_CONST,
7987                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7988                 .access = PL1_RW, .accessfn = nsacr_access,
7989                 .resetvalue = 0xc00
7990             };
7991             define_one_arm_cp_reg(cpu, &nsacr);
7992         } else {
7993             static const ARMCPRegInfo nsacr = {
7994                 .name = "NSACR",
7995                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7996                 .access = PL3_RW | PL1_R,
7997                 .resetvalue = 0,
7998                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7999             };
8000             define_one_arm_cp_reg(cpu, &nsacr);
8001         }
8002     } else {
8003         if (arm_feature(env, ARM_FEATURE_V8)) {
8004             static const ARMCPRegInfo nsacr = {
8005                 .name = "NSACR", .type = ARM_CP_CONST,
8006                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8007                 .access = PL1_R,
8008                 .resetvalue = 0xc00
8009             };
8010             define_one_arm_cp_reg(cpu, &nsacr);
8011         }
8012     }
8013 
8014     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8015         if (arm_feature(env, ARM_FEATURE_V6)) {
8016             /* PMSAv6 not implemented */
8017             assert(arm_feature(env, ARM_FEATURE_V7));
8018             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8019             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8020         } else {
8021             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8022         }
8023     } else {
8024         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8025         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8026         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8027         if (cpu_isar_feature(aa32_hpd, cpu)) {
8028             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8029         }
8030     }
8031     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8032         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8033     }
8034     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8035         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8036     }
8037     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8038         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8039     }
8040     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8041         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8042     }
8043     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8044         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8045     }
8046     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8047         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8048     }
8049     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8050         define_arm_cp_regs(cpu, omap_cp_reginfo);
8051     }
8052     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8053         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8054     }
8055     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8056         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8057     }
8058     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8059         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8060     }
8061     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8062         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8063     }
8064     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8065         define_arm_cp_regs(cpu, jazelle_regs);
8066     }
8067     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8068      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8069      * be read-only (ie write causes UNDEF exception).
8070      */
8071     {
8072         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8073             /* Pre-v8 MIDR space.
8074              * Note that the MIDR isn't a simple constant register because
8075              * of the TI925 behaviour where writes to another register can
8076              * cause the MIDR value to change.
8077              *
8078              * Unimplemented registers in the c15 0 0 0 space default to
8079              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8080              * and friends override accordingly.
8081              */
8082             { .name = "MIDR",
8083               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8084               .access = PL1_R, .resetvalue = cpu->midr,
8085               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8086               .readfn = midr_read,
8087               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8088               .type = ARM_CP_OVERRIDE },
8089             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8090             { .name = "DUMMY",
8091               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8092               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8093             { .name = "DUMMY",
8094               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8095               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8096             { .name = "DUMMY",
8097               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8098               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8099             { .name = "DUMMY",
8100               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8101               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8102             { .name = "DUMMY",
8103               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8104               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8105         };
8106         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8107             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8108               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8109               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8110               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8111               .readfn = midr_read },
8112             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8113             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8114               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8115               .access = PL1_R, .resetvalue = cpu->midr },
8116             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8117               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8118               .access = PL1_R, .resetvalue = cpu->midr },
8119             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8120               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8121               .access = PL1_R,
8122               .accessfn = access_aa64_tid1,
8123               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8124         };
8125         ARMCPRegInfo id_cp_reginfo[] = {
8126             /* These are common to v8 and pre-v8 */
8127             { .name = "CTR",
8128               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8129               .access = PL1_R, .accessfn = ctr_el0_access,
8130               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8131             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8132               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8133               .access = PL0_R, .accessfn = ctr_el0_access,
8134               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8135             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8136             { .name = "TCMTR",
8137               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8138               .access = PL1_R,
8139               .accessfn = access_aa32_tid1,
8140               .type = ARM_CP_CONST, .resetvalue = 0 },
8141         };
8142         /* TLBTR is specific to VMSA */
8143         ARMCPRegInfo id_tlbtr_reginfo = {
8144               .name = "TLBTR",
8145               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8146               .access = PL1_R,
8147               .accessfn = access_aa32_tid1,
8148               .type = ARM_CP_CONST, .resetvalue = 0,
8149         };
8150         /* MPUIR is specific to PMSA V6+ */
8151         ARMCPRegInfo id_mpuir_reginfo = {
8152               .name = "MPUIR",
8153               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8154               .access = PL1_R, .type = ARM_CP_CONST,
8155               .resetvalue = cpu->pmsav7_dregion << 8
8156         };
8157         static const ARMCPRegInfo crn0_wi_reginfo = {
8158             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8159             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8160             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8161         };
8162 #ifdef CONFIG_USER_ONLY
8163         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8164             { .name = "MIDR_EL1",
8165               .exported_bits = 0x00000000ffffffff },
8166             { .name = "REVIDR_EL1"                },
8167         };
8168         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8169 #endif
8170         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8171             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8172             size_t i;
8173             /* Register the blanket "writes ignored" value first to cover the
8174              * whole space. Then update the specific ID registers to allow write
8175              * access, so that they ignore writes rather than causing them to
8176              * UNDEF.
8177              */
8178             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8179             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8180                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8181             }
8182             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8183                 id_cp_reginfo[i].access = PL1_RW;
8184             }
8185             id_mpuir_reginfo.access = PL1_RW;
8186             id_tlbtr_reginfo.access = PL1_RW;
8187         }
8188         if (arm_feature(env, ARM_FEATURE_V8)) {
8189             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8190         } else {
8191             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8192         }
8193         define_arm_cp_regs(cpu, id_cp_reginfo);
8194         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8195             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8196         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8197             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8198         }
8199     }
8200 
8201     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8202         ARMCPRegInfo mpidr_cp_reginfo[] = {
8203             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8204               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8205               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8206         };
8207 #ifdef CONFIG_USER_ONLY
8208         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8209             { .name = "MPIDR_EL1",
8210               .fixed_bits = 0x0000000080000000 },
8211         };
8212         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8213 #endif
8214         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8215     }
8216 
8217     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8218         ARMCPRegInfo auxcr_reginfo[] = {
8219             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8220               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8221               .access = PL1_RW, .accessfn = access_tacr,
8222               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8223             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8224               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8225               .access = PL2_RW, .type = ARM_CP_CONST,
8226               .resetvalue = 0 },
8227             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8228               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8229               .access = PL3_RW, .type = ARM_CP_CONST,
8230               .resetvalue = 0 },
8231         };
8232         define_arm_cp_regs(cpu, auxcr_reginfo);
8233         if (cpu_isar_feature(aa32_ac2, cpu)) {
8234             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8235         }
8236     }
8237 
8238     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8239         /*
8240          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8241          * There are two flavours:
8242          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8243          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8244          *      32-bit register visible to AArch32 at a different encoding
8245          *      to the "flavour 1" register and with the bits rearranged to
8246          *      be able to squash a 64-bit address into the 32-bit view.
8247          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8248          * in future if we support AArch32-only configs of some of the
8249          * AArch64 cores we might need to add a specific feature flag
8250          * to indicate cores with "flavour 2" CBAR.
8251          */
8252         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8253             /* 32 bit view is [31:18] 0...0 [43:32]. */
8254             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8255                 | extract64(cpu->reset_cbar, 32, 12);
8256             ARMCPRegInfo cbar_reginfo[] = {
8257                 { .name = "CBAR",
8258                   .type = ARM_CP_CONST,
8259                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8260                   .access = PL1_R, .resetvalue = cbar32 },
8261                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8262                   .type = ARM_CP_CONST,
8263                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8264                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8265             };
8266             /* We don't implement a r/w 64 bit CBAR currently */
8267             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8268             define_arm_cp_regs(cpu, cbar_reginfo);
8269         } else {
8270             ARMCPRegInfo cbar = {
8271                 .name = "CBAR",
8272                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8273                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8274                 .fieldoffset = offsetof(CPUARMState,
8275                                         cp15.c15_config_base_address)
8276             };
8277             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8278                 cbar.access = PL1_R;
8279                 cbar.fieldoffset = 0;
8280                 cbar.type = ARM_CP_CONST;
8281             }
8282             define_one_arm_cp_reg(cpu, &cbar);
8283         }
8284     }
8285 
8286     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8287         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8288             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8289               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8290               .access = PL1_RW, .writefn = vbar_write,
8291               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8292                                      offsetof(CPUARMState, cp15.vbar_ns) },
8293               .resetvalue = 0 },
8294         };
8295         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8296     }
8297 
8298     /* Generic registers whose values depend on the implementation */
8299     {
8300         ARMCPRegInfo sctlr = {
8301             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8302             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8303             .access = PL1_RW, .accessfn = access_tvm_trvm,
8304             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8305                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8306             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8307             .raw_writefn = raw_write,
8308         };
8309         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8310             /* Normally we would always end the TB on an SCTLR write, but Linux
8311              * arch/arm/mach-pxa/sleep.S expects two instructions following
8312              * an MMU enable to execute from cache.  Imitate this behaviour.
8313              */
8314             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8315         }
8316         define_one_arm_cp_reg(cpu, &sctlr);
8317     }
8318 
8319     if (cpu_isar_feature(aa64_lor, cpu)) {
8320         define_arm_cp_regs(cpu, lor_reginfo);
8321     }
8322     if (cpu_isar_feature(aa64_pan, cpu)) {
8323         define_one_arm_cp_reg(cpu, &pan_reginfo);
8324     }
8325 #ifndef CONFIG_USER_ONLY
8326     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8327         define_arm_cp_regs(cpu, ats1e1_reginfo);
8328     }
8329     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8330         define_arm_cp_regs(cpu, ats1cp_reginfo);
8331     }
8332 #endif
8333     if (cpu_isar_feature(aa64_uao, cpu)) {
8334         define_one_arm_cp_reg(cpu, &uao_reginfo);
8335     }
8336 
8337     if (cpu_isar_feature(aa64_dit, cpu)) {
8338         define_one_arm_cp_reg(cpu, &dit_reginfo);
8339     }
8340     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8341         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8342     }
8343     if (cpu_isar_feature(any_ras, cpu)) {
8344         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8345     }
8346 
8347     if (cpu_isar_feature(aa64_vh, cpu) ||
8348         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8349         define_one_arm_cp_reg(cpu, &contextidr_el2);
8350     }
8351     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8352         define_arm_cp_regs(cpu, vhe_reginfo);
8353     }
8354 
8355     if (cpu_isar_feature(aa64_sve, cpu)) {
8356         define_arm_cp_regs(cpu, zcr_reginfo);
8357     }
8358 
8359     if (cpu_isar_feature(aa64_hcx, cpu)) {
8360         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8361     }
8362 
8363 #ifdef TARGET_AARCH64
8364     if (cpu_isar_feature(aa64_sme, cpu)) {
8365         define_arm_cp_regs(cpu, sme_reginfo);
8366     }
8367     if (cpu_isar_feature(aa64_pauth, cpu)) {
8368         define_arm_cp_regs(cpu, pauth_reginfo);
8369     }
8370     if (cpu_isar_feature(aa64_rndr, cpu)) {
8371         define_arm_cp_regs(cpu, rndr_reginfo);
8372     }
8373     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8374         define_arm_cp_regs(cpu, tlbirange_reginfo);
8375     }
8376     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8377         define_arm_cp_regs(cpu, tlbios_reginfo);
8378     }
8379 #ifndef CONFIG_USER_ONLY
8380     /* Data Cache clean instructions up to PoP */
8381     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8382         define_one_arm_cp_reg(cpu, dcpop_reg);
8383 
8384         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8385             define_one_arm_cp_reg(cpu, dcpodp_reg);
8386         }
8387     }
8388 #endif /*CONFIG_USER_ONLY*/
8389 
8390     /*
8391      * If full MTE is enabled, add all of the system registers.
8392      * If only "instructions available at EL0" are enabled,
8393      * then define only a RAZ/WI version of PSTATE.TCO.
8394      */
8395     if (cpu_isar_feature(aa64_mte, cpu)) {
8396         define_arm_cp_regs(cpu, mte_reginfo);
8397         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8398     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8399         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8400         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8401     }
8402 
8403     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8404         define_arm_cp_regs(cpu, scxtnum_reginfo);
8405     }
8406 #endif
8407 
8408     if (cpu_isar_feature(any_predinv, cpu)) {
8409         define_arm_cp_regs(cpu, predinv_reginfo);
8410     }
8411 
8412     if (cpu_isar_feature(any_ccidx, cpu)) {
8413         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8414     }
8415 
8416 #ifndef CONFIG_USER_ONLY
8417     /*
8418      * Register redirections and aliases must be done last,
8419      * after the registers from the other extensions have been defined.
8420      */
8421     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8422         define_arm_vh_e2h_redirects_aliases(cpu);
8423     }
8424 #endif
8425 }
8426 
8427 /* Sort alphabetically by type name, except for "any". */
8428 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8429 {
8430     ObjectClass *class_a = (ObjectClass *)a;
8431     ObjectClass *class_b = (ObjectClass *)b;
8432     const char *name_a, *name_b;
8433 
8434     name_a = object_class_get_name(class_a);
8435     name_b = object_class_get_name(class_b);
8436     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8437         return 1;
8438     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8439         return -1;
8440     } else {
8441         return strcmp(name_a, name_b);
8442     }
8443 }
8444 
8445 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8446 {
8447     ObjectClass *oc = data;
8448     CPUClass *cc = CPU_CLASS(oc);
8449     const char *typename;
8450     char *name;
8451 
8452     typename = object_class_get_name(oc);
8453     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8454     if (cc->deprecation_note) {
8455         qemu_printf("  %s (deprecated)\n", name);
8456     } else {
8457         qemu_printf("  %s\n", name);
8458     }
8459     g_free(name);
8460 }
8461 
8462 void arm_cpu_list(void)
8463 {
8464     GSList *list;
8465 
8466     list = object_class_get_list(TYPE_ARM_CPU, false);
8467     list = g_slist_sort(list, arm_cpu_list_compare);
8468     qemu_printf("Available CPUs:\n");
8469     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8470     g_slist_free(list);
8471 }
8472 
8473 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8474 {
8475     ObjectClass *oc = data;
8476     CpuDefinitionInfoList **cpu_list = user_data;
8477     CpuDefinitionInfo *info;
8478     const char *typename;
8479 
8480     typename = object_class_get_name(oc);
8481     info = g_malloc0(sizeof(*info));
8482     info->name = g_strndup(typename,
8483                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8484     info->q_typename = g_strdup(typename);
8485 
8486     QAPI_LIST_PREPEND(*cpu_list, info);
8487 }
8488 
8489 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8490 {
8491     CpuDefinitionInfoList *cpu_list = NULL;
8492     GSList *list;
8493 
8494     list = object_class_get_list(TYPE_ARM_CPU, false);
8495     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8496     g_slist_free(list);
8497 
8498     return cpu_list;
8499 }
8500 
8501 /*
8502  * Private utility function for define_one_arm_cp_reg_with_opaque():
8503  * add a single reginfo struct to the hash table.
8504  */
8505 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8506                                    void *opaque, CPState state,
8507                                    CPSecureState secstate,
8508                                    int crm, int opc1, int opc2,
8509                                    const char *name)
8510 {
8511     CPUARMState *env = &cpu->env;
8512     uint32_t key;
8513     ARMCPRegInfo *r2;
8514     bool is64 = r->type & ARM_CP_64BIT;
8515     bool ns = secstate & ARM_CP_SECSTATE_NS;
8516     int cp = r->cp;
8517     size_t name_len;
8518     bool make_const;
8519 
8520     switch (state) {
8521     case ARM_CP_STATE_AA32:
8522         /* We assume it is a cp15 register if the .cp field is left unset. */
8523         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8524             cp = 15;
8525         }
8526         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8527         break;
8528     case ARM_CP_STATE_AA64:
8529         /*
8530          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8531          * cp == 0 as equivalent to the value for "standard guest-visible
8532          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8533          * in their AArch64 view (the .cp value may be non-zero for the
8534          * benefit of the AArch32 view).
8535          */
8536         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8537             cp = CP_REG_ARM64_SYSREG_CP;
8538         }
8539         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8540         break;
8541     default:
8542         g_assert_not_reached();
8543     }
8544 
8545     /* Overriding of an existing definition must be explicitly requested. */
8546     if (!(r->type & ARM_CP_OVERRIDE)) {
8547         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8548         if (oldreg) {
8549             assert(oldreg->type & ARM_CP_OVERRIDE);
8550         }
8551     }
8552 
8553     /*
8554      * Eliminate registers that are not present because the EL is missing.
8555      * Doing this here makes it easier to put all registers for a given
8556      * feature into the same ARMCPRegInfo array and define them all at once.
8557      */
8558     make_const = false;
8559     if (arm_feature(env, ARM_FEATURE_EL3)) {
8560         /*
8561          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8562          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8563          */
8564         int min_el = ctz32(r->access) / 2;
8565         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8566             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8567                 return;
8568             }
8569             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8570         }
8571     } else {
8572         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8573                                  ? PL2_RW : PL1_RW);
8574         if ((r->access & max_el) == 0) {
8575             return;
8576         }
8577     }
8578 
8579     /* Combine cpreg and name into one allocation. */
8580     name_len = strlen(name) + 1;
8581     r2 = g_malloc(sizeof(*r2) + name_len);
8582     *r2 = *r;
8583     r2->name = memcpy(r2 + 1, name, name_len);
8584 
8585     /*
8586      * Update fields to match the instantiation, overwiting wildcards
8587      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8588      */
8589     r2->cp = cp;
8590     r2->crm = crm;
8591     r2->opc1 = opc1;
8592     r2->opc2 = opc2;
8593     r2->state = state;
8594     r2->secure = secstate;
8595     if (opaque) {
8596         r2->opaque = opaque;
8597     }
8598 
8599     if (make_const) {
8600         /* This should not have been a very special register to begin. */
8601         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8602         assert(old_special == 0 || old_special == ARM_CP_NOP);
8603         /*
8604          * Set the special function to CONST, retaining the other flags.
8605          * This is important for e.g. ARM_CP_SVE so that we still
8606          * take the SVE trap if CPTR_EL3.EZ == 0.
8607          */
8608         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8609         /*
8610          * Usually, these registers become RES0, but there are a few
8611          * special cases like VPIDR_EL2 which have a constant non-zero
8612          * value with writes ignored.
8613          */
8614         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8615             r2->resetvalue = 0;
8616         }
8617         /*
8618          * ARM_CP_CONST has precedence, so removing the callbacks and
8619          * offsets are not strictly necessary, but it is potentially
8620          * less confusing to debug later.
8621          */
8622         r2->readfn = NULL;
8623         r2->writefn = NULL;
8624         r2->raw_readfn = NULL;
8625         r2->raw_writefn = NULL;
8626         r2->resetfn = NULL;
8627         r2->fieldoffset = 0;
8628         r2->bank_fieldoffsets[0] = 0;
8629         r2->bank_fieldoffsets[1] = 0;
8630     } else {
8631         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8632 
8633         if (isbanked) {
8634             /*
8635              * Register is banked (using both entries in array).
8636              * Overwriting fieldoffset as the array is only used to define
8637              * banked registers but later only fieldoffset is used.
8638              */
8639             r2->fieldoffset = r->bank_fieldoffsets[ns];
8640         }
8641         if (state == ARM_CP_STATE_AA32) {
8642             if (isbanked) {
8643                 /*
8644                  * If the register is banked then we don't need to migrate or
8645                  * reset the 32-bit instance in certain cases:
8646                  *
8647                  * 1) If the register has both 32-bit and 64-bit instances
8648                  *    then we can count on the 64-bit instance taking care
8649                  *    of the non-secure bank.
8650                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8651                  *    version taking care of the secure bank.  This requires
8652                  *    that separate 32 and 64-bit definitions are provided.
8653                  */
8654                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8655                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8656                     r2->type |= ARM_CP_ALIAS;
8657                 }
8658             } else if ((secstate != r->secure) && !ns) {
8659                 /*
8660                  * The register is not banked so we only want to allow
8661                  * migration of the non-secure instance.
8662                  */
8663                 r2->type |= ARM_CP_ALIAS;
8664             }
8665 
8666             if (HOST_BIG_ENDIAN &&
8667                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8668                 r2->fieldoffset += sizeof(uint32_t);
8669             }
8670         }
8671     }
8672 
8673     /*
8674      * By convention, for wildcarded registers only the first
8675      * entry is used for migration; the others are marked as
8676      * ALIAS so we don't try to transfer the register
8677      * multiple times. Special registers (ie NOP/WFI) are
8678      * never migratable and not even raw-accessible.
8679      */
8680     if (r2->type & ARM_CP_SPECIAL_MASK) {
8681         r2->type |= ARM_CP_NO_RAW;
8682     }
8683     if (((r->crm == CP_ANY) && crm != 0) ||
8684         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8685         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8686         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8687     }
8688 
8689     /*
8690      * Check that raw accesses are either forbidden or handled. Note that
8691      * we can't assert this earlier because the setup of fieldoffset for
8692      * banked registers has to be done first.
8693      */
8694     if (!(r2->type & ARM_CP_NO_RAW)) {
8695         assert(!raw_accessors_invalid(r2));
8696     }
8697 
8698     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8699 }
8700 
8701 
8702 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8703                                        const ARMCPRegInfo *r, void *opaque)
8704 {
8705     /* Define implementations of coprocessor registers.
8706      * We store these in a hashtable because typically
8707      * there are less than 150 registers in a space which
8708      * is 16*16*16*8*8 = 262144 in size.
8709      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8710      * If a register is defined twice then the second definition is
8711      * used, so this can be used to define some generic registers and
8712      * then override them with implementation specific variations.
8713      * At least one of the original and the second definition should
8714      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8715      * against accidental use.
8716      *
8717      * The state field defines whether the register is to be
8718      * visible in the AArch32 or AArch64 execution state. If the
8719      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8720      * reginfo structure for the AArch32 view, which sees the lower
8721      * 32 bits of the 64 bit register.
8722      *
8723      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8724      * be wildcarded. AArch64 registers are always considered to be 64
8725      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8726      * the register, if any.
8727      */
8728     int crm, opc1, opc2;
8729     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8730     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8731     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8732     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8733     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8734     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8735     CPState state;
8736 
8737     /* 64 bit registers have only CRm and Opc1 fields */
8738     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8739     /* op0 only exists in the AArch64 encodings */
8740     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8741     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8742     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8743     /*
8744      * This API is only for Arm's system coprocessors (14 and 15) or
8745      * (M-profile or v7A-and-earlier only) for implementation defined
8746      * coprocessors in the range 0..7.  Our decode assumes this, since
8747      * 8..13 can be used for other insns including VFP and Neon. See
8748      * valid_cp() in translate.c.  Assert here that we haven't tried
8749      * to use an invalid coprocessor number.
8750      */
8751     switch (r->state) {
8752     case ARM_CP_STATE_BOTH:
8753         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8754         if (r->cp == 0) {
8755             break;
8756         }
8757         /* fall through */
8758     case ARM_CP_STATE_AA32:
8759         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8760             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8761             assert(r->cp >= 14 && r->cp <= 15);
8762         } else {
8763             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8764         }
8765         break;
8766     case ARM_CP_STATE_AA64:
8767         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8768         break;
8769     default:
8770         g_assert_not_reached();
8771     }
8772     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8773      * encodes a minimum access level for the register. We roll this
8774      * runtime check into our general permission check code, so check
8775      * here that the reginfo's specified permissions are strict enough
8776      * to encompass the generic architectural permission check.
8777      */
8778     if (r->state != ARM_CP_STATE_AA32) {
8779         CPAccessRights mask;
8780         switch (r->opc1) {
8781         case 0:
8782             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8783             mask = PL0U_R | PL1_RW;
8784             break;
8785         case 1: case 2:
8786             /* min_EL EL1 */
8787             mask = PL1_RW;
8788             break;
8789         case 3:
8790             /* min_EL EL0 */
8791             mask = PL0_RW;
8792             break;
8793         case 4:
8794         case 5:
8795             /* min_EL EL2 */
8796             mask = PL2_RW;
8797             break;
8798         case 6:
8799             /* min_EL EL3 */
8800             mask = PL3_RW;
8801             break;
8802         case 7:
8803             /* min_EL EL1, secure mode only (we don't check the latter) */
8804             mask = PL1_RW;
8805             break;
8806         default:
8807             /* broken reginfo with out-of-range opc1 */
8808             g_assert_not_reached();
8809         }
8810         /* assert our permissions are not too lax (stricter is fine) */
8811         assert((r->access & ~mask) == 0);
8812     }
8813 
8814     /* Check that the register definition has enough info to handle
8815      * reads and writes if they are permitted.
8816      */
8817     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8818         if (r->access & PL3_R) {
8819             assert((r->fieldoffset ||
8820                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8821                    r->readfn);
8822         }
8823         if (r->access & PL3_W) {
8824             assert((r->fieldoffset ||
8825                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8826                    r->writefn);
8827         }
8828     }
8829 
8830     for (crm = crmmin; crm <= crmmax; crm++) {
8831         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8832             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8833                 for (state = ARM_CP_STATE_AA32;
8834                      state <= ARM_CP_STATE_AA64; state++) {
8835                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8836                         continue;
8837                     }
8838                     if (state == ARM_CP_STATE_AA32) {
8839                         /* Under AArch32 CP registers can be common
8840                          * (same for secure and non-secure world) or banked.
8841                          */
8842                         char *name;
8843 
8844                         switch (r->secure) {
8845                         case ARM_CP_SECSTATE_S:
8846                         case ARM_CP_SECSTATE_NS:
8847                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8848                                                    r->secure, crm, opc1, opc2,
8849                                                    r->name);
8850                             break;
8851                         case ARM_CP_SECSTATE_BOTH:
8852                             name = g_strdup_printf("%s_S", r->name);
8853                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8854                                                    ARM_CP_SECSTATE_S,
8855                                                    crm, opc1, opc2, name);
8856                             g_free(name);
8857                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8858                                                    ARM_CP_SECSTATE_NS,
8859                                                    crm, opc1, opc2, r->name);
8860                             break;
8861                         default:
8862                             g_assert_not_reached();
8863                         }
8864                     } else {
8865                         /* AArch64 registers get mapped to non-secure instance
8866                          * of AArch32 */
8867                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8868                                                ARM_CP_SECSTATE_NS,
8869                                                crm, opc1, opc2, r->name);
8870                     }
8871                 }
8872             }
8873         }
8874     }
8875 }
8876 
8877 /* Define a whole list of registers */
8878 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8879                                         void *opaque, size_t len)
8880 {
8881     size_t i;
8882     for (i = 0; i < len; ++i) {
8883         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8884     }
8885 }
8886 
8887 /*
8888  * Modify ARMCPRegInfo for access from userspace.
8889  *
8890  * This is a data driven modification directed by
8891  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8892  * user-space cannot alter any values and dynamic values pertaining to
8893  * execution state are hidden from user space view anyway.
8894  */
8895 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8896                                  const ARMCPRegUserSpaceInfo *mods,
8897                                  size_t mods_len)
8898 {
8899     for (size_t mi = 0; mi < mods_len; ++mi) {
8900         const ARMCPRegUserSpaceInfo *m = mods + mi;
8901         GPatternSpec *pat = NULL;
8902 
8903         if (m->is_glob) {
8904             pat = g_pattern_spec_new(m->name);
8905         }
8906         for (size_t ri = 0; ri < regs_len; ++ri) {
8907             ARMCPRegInfo *r = regs + ri;
8908 
8909             if (pat && g_pattern_match_string(pat, r->name)) {
8910                 r->type = ARM_CP_CONST;
8911                 r->access = PL0U_R;
8912                 r->resetvalue = 0;
8913                 /* continue */
8914             } else if (strcmp(r->name, m->name) == 0) {
8915                 r->type = ARM_CP_CONST;
8916                 r->access = PL0U_R;
8917                 r->resetvalue &= m->exported_bits;
8918                 r->resetvalue |= m->fixed_bits;
8919                 break;
8920             }
8921         }
8922         if (pat) {
8923             g_pattern_spec_free(pat);
8924         }
8925     }
8926 }
8927 
8928 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8929 {
8930     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8931 }
8932 
8933 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8934                          uint64_t value)
8935 {
8936     /* Helper coprocessor write function for write-ignore registers */
8937 }
8938 
8939 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8940 {
8941     /* Helper coprocessor write function for read-as-zero registers */
8942     return 0;
8943 }
8944 
8945 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8946 {
8947     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8948 }
8949 
8950 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8951 {
8952     /* Return true if it is not valid for us to switch to
8953      * this CPU mode (ie all the UNPREDICTABLE cases in
8954      * the ARM ARM CPSRWriteByInstr pseudocode).
8955      */
8956 
8957     /* Changes to or from Hyp via MSR and CPS are illegal. */
8958     if (write_type == CPSRWriteByInstr &&
8959         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8960          mode == ARM_CPU_MODE_HYP)) {
8961         return 1;
8962     }
8963 
8964     switch (mode) {
8965     case ARM_CPU_MODE_USR:
8966         return 0;
8967     case ARM_CPU_MODE_SYS:
8968     case ARM_CPU_MODE_SVC:
8969     case ARM_CPU_MODE_ABT:
8970     case ARM_CPU_MODE_UND:
8971     case ARM_CPU_MODE_IRQ:
8972     case ARM_CPU_MODE_FIQ:
8973         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8974          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8975          */
8976         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8977          * and CPS are treated as illegal mode changes.
8978          */
8979         if (write_type == CPSRWriteByInstr &&
8980             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8981             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8982             return 1;
8983         }
8984         return 0;
8985     case ARM_CPU_MODE_HYP:
8986         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8987     case ARM_CPU_MODE_MON:
8988         return arm_current_el(env) < 3;
8989     default:
8990         return 1;
8991     }
8992 }
8993 
8994 uint32_t cpsr_read(CPUARMState *env)
8995 {
8996     int ZF;
8997     ZF = (env->ZF == 0);
8998     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8999         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9000         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9001         | ((env->condexec_bits & 0xfc) << 8)
9002         | (env->GE << 16) | (env->daif & CPSR_AIF);
9003 }
9004 
9005 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9006                 CPSRWriteType write_type)
9007 {
9008     uint32_t changed_daif;
9009     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9010         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9011 
9012     if (mask & CPSR_NZCV) {
9013         env->ZF = (~val) & CPSR_Z;
9014         env->NF = val;
9015         env->CF = (val >> 29) & 1;
9016         env->VF = (val << 3) & 0x80000000;
9017     }
9018     if (mask & CPSR_Q)
9019         env->QF = ((val & CPSR_Q) != 0);
9020     if (mask & CPSR_T)
9021         env->thumb = ((val & CPSR_T) != 0);
9022     if (mask & CPSR_IT_0_1) {
9023         env->condexec_bits &= ~3;
9024         env->condexec_bits |= (val >> 25) & 3;
9025     }
9026     if (mask & CPSR_IT_2_7) {
9027         env->condexec_bits &= 3;
9028         env->condexec_bits |= (val >> 8) & 0xfc;
9029     }
9030     if (mask & CPSR_GE) {
9031         env->GE = (val >> 16) & 0xf;
9032     }
9033 
9034     /* In a V7 implementation that includes the security extensions but does
9035      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9036      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9037      * bits respectively.
9038      *
9039      * In a V8 implementation, it is permitted for privileged software to
9040      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9041      */
9042     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9043         arm_feature(env, ARM_FEATURE_EL3) &&
9044         !arm_feature(env, ARM_FEATURE_EL2) &&
9045         !arm_is_secure(env)) {
9046 
9047         changed_daif = (env->daif ^ val) & mask;
9048 
9049         if (changed_daif & CPSR_A) {
9050             /* Check to see if we are allowed to change the masking of async
9051              * abort exceptions from a non-secure state.
9052              */
9053             if (!(env->cp15.scr_el3 & SCR_AW)) {
9054                 qemu_log_mask(LOG_GUEST_ERROR,
9055                               "Ignoring attempt to switch CPSR_A flag from "
9056                               "non-secure world with SCR.AW bit clear\n");
9057                 mask &= ~CPSR_A;
9058             }
9059         }
9060 
9061         if (changed_daif & CPSR_F) {
9062             /* Check to see if we are allowed to change the masking of FIQ
9063              * exceptions from a non-secure state.
9064              */
9065             if (!(env->cp15.scr_el3 & SCR_FW)) {
9066                 qemu_log_mask(LOG_GUEST_ERROR,
9067                               "Ignoring attempt to switch CPSR_F flag from "
9068                               "non-secure world with SCR.FW bit clear\n");
9069                 mask &= ~CPSR_F;
9070             }
9071 
9072             /* Check whether non-maskable FIQ (NMFI) support is enabled.
9073              * If this bit is set software is not allowed to mask
9074              * FIQs, but is allowed to set CPSR_F to 0.
9075              */
9076             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9077                 (val & CPSR_F)) {
9078                 qemu_log_mask(LOG_GUEST_ERROR,
9079                               "Ignoring attempt to enable CPSR_F flag "
9080                               "(non-maskable FIQ [NMFI] support enabled)\n");
9081                 mask &= ~CPSR_F;
9082             }
9083         }
9084     }
9085 
9086     env->daif &= ~(CPSR_AIF & mask);
9087     env->daif |= val & CPSR_AIF & mask;
9088 
9089     if (write_type != CPSRWriteRaw &&
9090         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9091         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9092             /* Note that we can only get here in USR mode if this is a
9093              * gdb stub write; for this case we follow the architectural
9094              * behaviour for guest writes in USR mode of ignoring an attempt
9095              * to switch mode. (Those are caught by translate.c for writes
9096              * triggered by guest instructions.)
9097              */
9098             mask &= ~CPSR_M;
9099         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9100             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9101              * v7, and has defined behaviour in v8:
9102              *  + leave CPSR.M untouched
9103              *  + allow changes to the other CPSR fields
9104              *  + set PSTATE.IL
9105              * For user changes via the GDB stub, we don't set PSTATE.IL,
9106              * as this would be unnecessarily harsh for a user error.
9107              */
9108             mask &= ~CPSR_M;
9109             if (write_type != CPSRWriteByGDBStub &&
9110                 arm_feature(env, ARM_FEATURE_V8)) {
9111                 mask |= CPSR_IL;
9112                 val |= CPSR_IL;
9113             }
9114             qemu_log_mask(LOG_GUEST_ERROR,
9115                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9116                           aarch32_mode_name(env->uncached_cpsr),
9117                           aarch32_mode_name(val));
9118         } else {
9119             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9120                           write_type == CPSRWriteExceptionReturn ?
9121                           "Exception return from AArch32" :
9122                           "AArch32 mode switch from",
9123                           aarch32_mode_name(env->uncached_cpsr),
9124                           aarch32_mode_name(val), env->regs[15]);
9125             switch_mode(env, val & CPSR_M);
9126         }
9127     }
9128     mask &= ~CACHED_CPSR_BITS;
9129     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9130     if (rebuild_hflags) {
9131         arm_rebuild_hflags(env);
9132     }
9133 }
9134 
9135 /* Sign/zero extend */
9136 uint32_t HELPER(sxtb16)(uint32_t x)
9137 {
9138     uint32_t res;
9139     res = (uint16_t)(int8_t)x;
9140     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9141     return res;
9142 }
9143 
9144 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9145 {
9146     /*
9147      * Take a division-by-zero exception if necessary; otherwise return
9148      * to get the usual non-trapping division behaviour (result of 0)
9149      */
9150     if (arm_feature(env, ARM_FEATURE_M)
9151         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9152         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9153     }
9154 }
9155 
9156 uint32_t HELPER(uxtb16)(uint32_t x)
9157 {
9158     uint32_t res;
9159     res = (uint16_t)(uint8_t)x;
9160     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9161     return res;
9162 }
9163 
9164 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9165 {
9166     if (den == 0) {
9167         handle_possible_div0_trap(env, GETPC());
9168         return 0;
9169     }
9170     if (num == INT_MIN && den == -1) {
9171         return INT_MIN;
9172     }
9173     return num / den;
9174 }
9175 
9176 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9177 {
9178     if (den == 0) {
9179         handle_possible_div0_trap(env, GETPC());
9180         return 0;
9181     }
9182     return num / den;
9183 }
9184 
9185 uint32_t HELPER(rbit)(uint32_t x)
9186 {
9187     return revbit32(x);
9188 }
9189 
9190 #ifdef CONFIG_USER_ONLY
9191 
9192 static void switch_mode(CPUARMState *env, int mode)
9193 {
9194     ARMCPU *cpu = env_archcpu(env);
9195 
9196     if (mode != ARM_CPU_MODE_USR) {
9197         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9198     }
9199 }
9200 
9201 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9202                                  uint32_t cur_el, bool secure)
9203 {
9204     return 1;
9205 }
9206 
9207 void aarch64_sync_64_to_32(CPUARMState *env)
9208 {
9209     g_assert_not_reached();
9210 }
9211 
9212 #else
9213 
9214 static void switch_mode(CPUARMState *env, int mode)
9215 {
9216     int old_mode;
9217     int i;
9218 
9219     old_mode = env->uncached_cpsr & CPSR_M;
9220     if (mode == old_mode)
9221         return;
9222 
9223     if (old_mode == ARM_CPU_MODE_FIQ) {
9224         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9225         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9226     } else if (mode == ARM_CPU_MODE_FIQ) {
9227         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9228         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9229     }
9230 
9231     i = bank_number(old_mode);
9232     env->banked_r13[i] = env->regs[13];
9233     env->banked_spsr[i] = env->spsr;
9234 
9235     i = bank_number(mode);
9236     env->regs[13] = env->banked_r13[i];
9237     env->spsr = env->banked_spsr[i];
9238 
9239     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9240     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9241 }
9242 
9243 /* Physical Interrupt Target EL Lookup Table
9244  *
9245  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9246  *
9247  * The below multi-dimensional table is used for looking up the target
9248  * exception level given numerous condition criteria.  Specifically, the
9249  * target EL is based on SCR and HCR routing controls as well as the
9250  * currently executing EL and secure state.
9251  *
9252  *    Dimensions:
9253  *    target_el_table[2][2][2][2][2][4]
9254  *                    |  |  |  |  |  +--- Current EL
9255  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9256  *                    |  |  |  +--------- HCR mask override
9257  *                    |  |  +------------ SCR exec state control
9258  *                    |  +--------------- SCR mask override
9259  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9260  *
9261  *    The table values are as such:
9262  *    0-3 = EL0-EL3
9263  *     -1 = Cannot occur
9264  *
9265  * The ARM ARM target EL table includes entries indicating that an "exception
9266  * is not taken".  The two cases where this is applicable are:
9267  *    1) An exception is taken from EL3 but the SCR does not have the exception
9268  *    routed to EL3.
9269  *    2) An exception is taken from EL2 but the HCR does not have the exception
9270  *    routed to EL2.
9271  * In these two cases, the below table contain a target of EL1.  This value is
9272  * returned as it is expected that the consumer of the table data will check
9273  * for "target EL >= current EL" to ensure the exception is not taken.
9274  *
9275  *            SCR     HCR
9276  *         64  EA     AMO                 From
9277  *        BIT IRQ     IMO      Non-secure         Secure
9278  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9279  */
9280 static const int8_t target_el_table[2][2][2][2][2][4] = {
9281     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9282        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9283       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9284        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9285      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9286        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9287       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9288        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9289     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9290        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9291       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9292        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9293      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9294        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9295       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9296        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9297 };
9298 
9299 /*
9300  * Determine the target EL for physical exceptions
9301  */
9302 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9303                                  uint32_t cur_el, bool secure)
9304 {
9305     CPUARMState *env = cs->env_ptr;
9306     bool rw;
9307     bool scr;
9308     bool hcr;
9309     int target_el;
9310     /* Is the highest EL AArch64? */
9311     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9312     uint64_t hcr_el2;
9313 
9314     if (arm_feature(env, ARM_FEATURE_EL3)) {
9315         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9316     } else {
9317         /* Either EL2 is the highest EL (and so the EL2 register width
9318          * is given by is64); or there is no EL2 or EL3, in which case
9319          * the value of 'rw' does not affect the table lookup anyway.
9320          */
9321         rw = is64;
9322     }
9323 
9324     hcr_el2 = arm_hcr_el2_eff(env);
9325     switch (excp_idx) {
9326     case EXCP_IRQ:
9327         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9328         hcr = hcr_el2 & HCR_IMO;
9329         break;
9330     case EXCP_FIQ:
9331         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9332         hcr = hcr_el2 & HCR_FMO;
9333         break;
9334     default:
9335         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9336         hcr = hcr_el2 & HCR_AMO;
9337         break;
9338     };
9339 
9340     /*
9341      * For these purposes, TGE and AMO/IMO/FMO both force the
9342      * interrupt to EL2.  Fold TGE into the bit extracted above.
9343      */
9344     hcr |= (hcr_el2 & HCR_TGE) != 0;
9345 
9346     /* Perform a table-lookup for the target EL given the current state */
9347     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9348 
9349     assert(target_el > 0);
9350 
9351     return target_el;
9352 }
9353 
9354 void arm_log_exception(CPUState *cs)
9355 {
9356     int idx = cs->exception_index;
9357 
9358     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9359         const char *exc = NULL;
9360         static const char * const excnames[] = {
9361             [EXCP_UDEF] = "Undefined Instruction",
9362             [EXCP_SWI] = "SVC",
9363             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9364             [EXCP_DATA_ABORT] = "Data Abort",
9365             [EXCP_IRQ] = "IRQ",
9366             [EXCP_FIQ] = "FIQ",
9367             [EXCP_BKPT] = "Breakpoint",
9368             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9369             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9370             [EXCP_HVC] = "Hypervisor Call",
9371             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9372             [EXCP_SMC] = "Secure Monitor Call",
9373             [EXCP_VIRQ] = "Virtual IRQ",
9374             [EXCP_VFIQ] = "Virtual FIQ",
9375             [EXCP_SEMIHOST] = "Semihosting call",
9376             [EXCP_NOCP] = "v7M NOCP UsageFault",
9377             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9378             [EXCP_STKOF] = "v8M STKOF UsageFault",
9379             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9380             [EXCP_LSERR] = "v8M LSERR UsageFault",
9381             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9382             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9383             [EXCP_VSERR] = "Virtual SERR",
9384         };
9385 
9386         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9387             exc = excnames[idx];
9388         }
9389         if (!exc) {
9390             exc = "unknown";
9391         }
9392         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9393                       idx, exc, cs->cpu_index);
9394     }
9395 }
9396 
9397 /*
9398  * Function used to synchronize QEMU's AArch64 register set with AArch32
9399  * register set.  This is necessary when switching between AArch32 and AArch64
9400  * execution state.
9401  */
9402 void aarch64_sync_32_to_64(CPUARMState *env)
9403 {
9404     int i;
9405     uint32_t mode = env->uncached_cpsr & CPSR_M;
9406 
9407     /* We can blanket copy R[0:7] to X[0:7] */
9408     for (i = 0; i < 8; i++) {
9409         env->xregs[i] = env->regs[i];
9410     }
9411 
9412     /*
9413      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9414      * Otherwise, they come from the banked user regs.
9415      */
9416     if (mode == ARM_CPU_MODE_FIQ) {
9417         for (i = 8; i < 13; i++) {
9418             env->xregs[i] = env->usr_regs[i - 8];
9419         }
9420     } else {
9421         for (i = 8; i < 13; i++) {
9422             env->xregs[i] = env->regs[i];
9423         }
9424     }
9425 
9426     /*
9427      * Registers x13-x23 are the various mode SP and FP registers. Registers
9428      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9429      * from the mode banked register.
9430      */
9431     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9432         env->xregs[13] = env->regs[13];
9433         env->xregs[14] = env->regs[14];
9434     } else {
9435         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9436         /* HYP is an exception in that it is copied from r14 */
9437         if (mode == ARM_CPU_MODE_HYP) {
9438             env->xregs[14] = env->regs[14];
9439         } else {
9440             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9441         }
9442     }
9443 
9444     if (mode == ARM_CPU_MODE_HYP) {
9445         env->xregs[15] = env->regs[13];
9446     } else {
9447         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9448     }
9449 
9450     if (mode == ARM_CPU_MODE_IRQ) {
9451         env->xregs[16] = env->regs[14];
9452         env->xregs[17] = env->regs[13];
9453     } else {
9454         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9455         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9456     }
9457 
9458     if (mode == ARM_CPU_MODE_SVC) {
9459         env->xregs[18] = env->regs[14];
9460         env->xregs[19] = env->regs[13];
9461     } else {
9462         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9463         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9464     }
9465 
9466     if (mode == ARM_CPU_MODE_ABT) {
9467         env->xregs[20] = env->regs[14];
9468         env->xregs[21] = env->regs[13];
9469     } else {
9470         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9471         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9472     }
9473 
9474     if (mode == ARM_CPU_MODE_UND) {
9475         env->xregs[22] = env->regs[14];
9476         env->xregs[23] = env->regs[13];
9477     } else {
9478         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9479         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9480     }
9481 
9482     /*
9483      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9484      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9485      * FIQ bank for r8-r14.
9486      */
9487     if (mode == ARM_CPU_MODE_FIQ) {
9488         for (i = 24; i < 31; i++) {
9489             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9490         }
9491     } else {
9492         for (i = 24; i < 29; i++) {
9493             env->xregs[i] = env->fiq_regs[i - 24];
9494         }
9495         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9496         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9497     }
9498 
9499     env->pc = env->regs[15];
9500 }
9501 
9502 /*
9503  * Function used to synchronize QEMU's AArch32 register set with AArch64
9504  * register set.  This is necessary when switching between AArch32 and AArch64
9505  * execution state.
9506  */
9507 void aarch64_sync_64_to_32(CPUARMState *env)
9508 {
9509     int i;
9510     uint32_t mode = env->uncached_cpsr & CPSR_M;
9511 
9512     /* We can blanket copy X[0:7] to R[0:7] */
9513     for (i = 0; i < 8; i++) {
9514         env->regs[i] = env->xregs[i];
9515     }
9516 
9517     /*
9518      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9519      * Otherwise, we copy x8-x12 into the banked user regs.
9520      */
9521     if (mode == ARM_CPU_MODE_FIQ) {
9522         for (i = 8; i < 13; i++) {
9523             env->usr_regs[i - 8] = env->xregs[i];
9524         }
9525     } else {
9526         for (i = 8; i < 13; i++) {
9527             env->regs[i] = env->xregs[i];
9528         }
9529     }
9530 
9531     /*
9532      * Registers r13 & r14 depend on the current mode.
9533      * If we are in a given mode, we copy the corresponding x registers to r13
9534      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9535      * for the mode.
9536      */
9537     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9538         env->regs[13] = env->xregs[13];
9539         env->regs[14] = env->xregs[14];
9540     } else {
9541         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9542 
9543         /*
9544          * HYP is an exception in that it does not have its own banked r14 but
9545          * shares the USR r14
9546          */
9547         if (mode == ARM_CPU_MODE_HYP) {
9548             env->regs[14] = env->xregs[14];
9549         } else {
9550             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9551         }
9552     }
9553 
9554     if (mode == ARM_CPU_MODE_HYP) {
9555         env->regs[13] = env->xregs[15];
9556     } else {
9557         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9558     }
9559 
9560     if (mode == ARM_CPU_MODE_IRQ) {
9561         env->regs[14] = env->xregs[16];
9562         env->regs[13] = env->xregs[17];
9563     } else {
9564         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9565         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9566     }
9567 
9568     if (mode == ARM_CPU_MODE_SVC) {
9569         env->regs[14] = env->xregs[18];
9570         env->regs[13] = env->xregs[19];
9571     } else {
9572         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9573         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9574     }
9575 
9576     if (mode == ARM_CPU_MODE_ABT) {
9577         env->regs[14] = env->xregs[20];
9578         env->regs[13] = env->xregs[21];
9579     } else {
9580         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9581         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9582     }
9583 
9584     if (mode == ARM_CPU_MODE_UND) {
9585         env->regs[14] = env->xregs[22];
9586         env->regs[13] = env->xregs[23];
9587     } else {
9588         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9589         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9590     }
9591 
9592     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9593      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9594      * FIQ bank for r8-r14.
9595      */
9596     if (mode == ARM_CPU_MODE_FIQ) {
9597         for (i = 24; i < 31; i++) {
9598             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9599         }
9600     } else {
9601         for (i = 24; i < 29; i++) {
9602             env->fiq_regs[i - 24] = env->xregs[i];
9603         }
9604         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9605         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9606     }
9607 
9608     env->regs[15] = env->pc;
9609 }
9610 
9611 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9612                                    uint32_t mask, uint32_t offset,
9613                                    uint32_t newpc)
9614 {
9615     int new_el;
9616 
9617     /* Change the CPU state so as to actually take the exception. */
9618     switch_mode(env, new_mode);
9619 
9620     /*
9621      * For exceptions taken to AArch32 we must clear the SS bit in both
9622      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9623      */
9624     env->pstate &= ~PSTATE_SS;
9625     env->spsr = cpsr_read(env);
9626     /* Clear IT bits.  */
9627     env->condexec_bits = 0;
9628     /* Switch to the new mode, and to the correct instruction set.  */
9629     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9630 
9631     /* This must be after mode switching. */
9632     new_el = arm_current_el(env);
9633 
9634     /* Set new mode endianness */
9635     env->uncached_cpsr &= ~CPSR_E;
9636     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9637         env->uncached_cpsr |= CPSR_E;
9638     }
9639     /* J and IL must always be cleared for exception entry */
9640     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9641     env->daif |= mask;
9642 
9643     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9644         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9645             env->uncached_cpsr |= CPSR_SSBS;
9646         } else {
9647             env->uncached_cpsr &= ~CPSR_SSBS;
9648         }
9649     }
9650 
9651     if (new_mode == ARM_CPU_MODE_HYP) {
9652         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9653         env->elr_el[2] = env->regs[15];
9654     } else {
9655         /* CPSR.PAN is normally preserved preserved unless...  */
9656         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9657             switch (new_el) {
9658             case 3:
9659                 if (!arm_is_secure_below_el3(env)) {
9660                     /* ... the target is EL3, from non-secure state.  */
9661                     env->uncached_cpsr &= ~CPSR_PAN;
9662                     break;
9663                 }
9664                 /* ... the target is EL3, from secure state ... */
9665                 /* fall through */
9666             case 1:
9667                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9668                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9669                     env->uncached_cpsr |= CPSR_PAN;
9670                 }
9671                 break;
9672             }
9673         }
9674         /*
9675          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9676          * and we should just guard the thumb mode on V4
9677          */
9678         if (arm_feature(env, ARM_FEATURE_V4T)) {
9679             env->thumb =
9680                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9681         }
9682         env->regs[14] = env->regs[15] + offset;
9683     }
9684     env->regs[15] = newpc;
9685     arm_rebuild_hflags(env);
9686 }
9687 
9688 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9689 {
9690     /*
9691      * Handle exception entry to Hyp mode; this is sufficiently
9692      * different to entry to other AArch32 modes that we handle it
9693      * separately here.
9694      *
9695      * The vector table entry used is always the 0x14 Hyp mode entry point,
9696      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9697      * The offset applied to the preferred return address is always zero
9698      * (see DDI0487C.a section G1.12.3).
9699      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9700      */
9701     uint32_t addr, mask;
9702     ARMCPU *cpu = ARM_CPU(cs);
9703     CPUARMState *env = &cpu->env;
9704 
9705     switch (cs->exception_index) {
9706     case EXCP_UDEF:
9707         addr = 0x04;
9708         break;
9709     case EXCP_SWI:
9710         addr = 0x08;
9711         break;
9712     case EXCP_BKPT:
9713         /* Fall through to prefetch abort.  */
9714     case EXCP_PREFETCH_ABORT:
9715         env->cp15.ifar_s = env->exception.vaddress;
9716         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9717                       (uint32_t)env->exception.vaddress);
9718         addr = 0x0c;
9719         break;
9720     case EXCP_DATA_ABORT:
9721         env->cp15.dfar_s = env->exception.vaddress;
9722         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9723                       (uint32_t)env->exception.vaddress);
9724         addr = 0x10;
9725         break;
9726     case EXCP_IRQ:
9727         addr = 0x18;
9728         break;
9729     case EXCP_FIQ:
9730         addr = 0x1c;
9731         break;
9732     case EXCP_HVC:
9733         addr = 0x08;
9734         break;
9735     case EXCP_HYP_TRAP:
9736         addr = 0x14;
9737         break;
9738     default:
9739         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9740     }
9741 
9742     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9743         if (!arm_feature(env, ARM_FEATURE_V8)) {
9744             /*
9745              * QEMU syndrome values are v8-style. v7 has the IL bit
9746              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9747              * If this is a v7 CPU, squash the IL bit in those cases.
9748              */
9749             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9750                 (cs->exception_index == EXCP_DATA_ABORT &&
9751                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9752                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9753                 env->exception.syndrome &= ~ARM_EL_IL;
9754             }
9755         }
9756         env->cp15.esr_el[2] = env->exception.syndrome;
9757     }
9758 
9759     if (arm_current_el(env) != 2 && addr < 0x14) {
9760         addr = 0x14;
9761     }
9762 
9763     mask = 0;
9764     if (!(env->cp15.scr_el3 & SCR_EA)) {
9765         mask |= CPSR_A;
9766     }
9767     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9768         mask |= CPSR_I;
9769     }
9770     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9771         mask |= CPSR_F;
9772     }
9773 
9774     addr += env->cp15.hvbar;
9775 
9776     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9777 }
9778 
9779 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9780 {
9781     ARMCPU *cpu = ARM_CPU(cs);
9782     CPUARMState *env = &cpu->env;
9783     uint32_t addr;
9784     uint32_t mask;
9785     int new_mode;
9786     uint32_t offset;
9787     uint32_t moe;
9788 
9789     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9790     switch (syn_get_ec(env->exception.syndrome)) {
9791     case EC_BREAKPOINT:
9792     case EC_BREAKPOINT_SAME_EL:
9793         moe = 1;
9794         break;
9795     case EC_WATCHPOINT:
9796     case EC_WATCHPOINT_SAME_EL:
9797         moe = 10;
9798         break;
9799     case EC_AA32_BKPT:
9800         moe = 3;
9801         break;
9802     case EC_VECTORCATCH:
9803         moe = 5;
9804         break;
9805     default:
9806         moe = 0;
9807         break;
9808     }
9809 
9810     if (moe) {
9811         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9812     }
9813 
9814     if (env->exception.target_el == 2) {
9815         arm_cpu_do_interrupt_aarch32_hyp(cs);
9816         return;
9817     }
9818 
9819     switch (cs->exception_index) {
9820     case EXCP_UDEF:
9821         new_mode = ARM_CPU_MODE_UND;
9822         addr = 0x04;
9823         mask = CPSR_I;
9824         if (env->thumb)
9825             offset = 2;
9826         else
9827             offset = 4;
9828         break;
9829     case EXCP_SWI:
9830         new_mode = ARM_CPU_MODE_SVC;
9831         addr = 0x08;
9832         mask = CPSR_I;
9833         /* The PC already points to the next instruction.  */
9834         offset = 0;
9835         break;
9836     case EXCP_BKPT:
9837         /* Fall through to prefetch abort.  */
9838     case EXCP_PREFETCH_ABORT:
9839         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9840         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9841         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9842                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9843         new_mode = ARM_CPU_MODE_ABT;
9844         addr = 0x0c;
9845         mask = CPSR_A | CPSR_I;
9846         offset = 4;
9847         break;
9848     case EXCP_DATA_ABORT:
9849         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9850         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9851         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9852                       env->exception.fsr,
9853                       (uint32_t)env->exception.vaddress);
9854         new_mode = ARM_CPU_MODE_ABT;
9855         addr = 0x10;
9856         mask = CPSR_A | CPSR_I;
9857         offset = 8;
9858         break;
9859     case EXCP_IRQ:
9860         new_mode = ARM_CPU_MODE_IRQ;
9861         addr = 0x18;
9862         /* Disable IRQ and imprecise data aborts.  */
9863         mask = CPSR_A | CPSR_I;
9864         offset = 4;
9865         if (env->cp15.scr_el3 & SCR_IRQ) {
9866             /* IRQ routed to monitor mode */
9867             new_mode = ARM_CPU_MODE_MON;
9868             mask |= CPSR_F;
9869         }
9870         break;
9871     case EXCP_FIQ:
9872         new_mode = ARM_CPU_MODE_FIQ;
9873         addr = 0x1c;
9874         /* Disable FIQ, IRQ and imprecise data aborts.  */
9875         mask = CPSR_A | CPSR_I | CPSR_F;
9876         if (env->cp15.scr_el3 & SCR_FIQ) {
9877             /* FIQ routed to monitor mode */
9878             new_mode = ARM_CPU_MODE_MON;
9879         }
9880         offset = 4;
9881         break;
9882     case EXCP_VIRQ:
9883         new_mode = ARM_CPU_MODE_IRQ;
9884         addr = 0x18;
9885         /* Disable IRQ and imprecise data aborts.  */
9886         mask = CPSR_A | CPSR_I;
9887         offset = 4;
9888         break;
9889     case EXCP_VFIQ:
9890         new_mode = ARM_CPU_MODE_FIQ;
9891         addr = 0x1c;
9892         /* Disable FIQ, IRQ and imprecise data aborts.  */
9893         mask = CPSR_A | CPSR_I | CPSR_F;
9894         offset = 4;
9895         break;
9896     case EXCP_VSERR:
9897         {
9898             /*
9899              * Note that this is reported as a data abort, but the DFAR
9900              * has an UNKNOWN value.  Construct the SError syndrome from
9901              * AET and ExT fields.
9902              */
9903             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9904 
9905             if (extended_addresses_enabled(env)) {
9906                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9907             } else {
9908                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9909             }
9910             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9911             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9912             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9913                           env->exception.fsr);
9914 
9915             new_mode = ARM_CPU_MODE_ABT;
9916             addr = 0x10;
9917             mask = CPSR_A | CPSR_I;
9918             offset = 8;
9919         }
9920         break;
9921     case EXCP_SMC:
9922         new_mode = ARM_CPU_MODE_MON;
9923         addr = 0x08;
9924         mask = CPSR_A | CPSR_I | CPSR_F;
9925         offset = 0;
9926         break;
9927     default:
9928         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9929         return; /* Never happens.  Keep compiler happy.  */
9930     }
9931 
9932     if (new_mode == ARM_CPU_MODE_MON) {
9933         addr += env->cp15.mvbar;
9934     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9935         /* High vectors. When enabled, base address cannot be remapped. */
9936         addr += 0xffff0000;
9937     } else {
9938         /* ARM v7 architectures provide a vector base address register to remap
9939          * the interrupt vector table.
9940          * This register is only followed in non-monitor mode, and is banked.
9941          * Note: only bits 31:5 are valid.
9942          */
9943         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9944     }
9945 
9946     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9947         env->cp15.scr_el3 &= ~SCR_NS;
9948     }
9949 
9950     take_aarch32_exception(env, new_mode, mask, offset, addr);
9951 }
9952 
9953 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9954 {
9955     /*
9956      * Return the register number of the AArch64 view of the AArch32
9957      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9958      * be that of the AArch32 mode the exception came from.
9959      */
9960     int mode = env->uncached_cpsr & CPSR_M;
9961 
9962     switch (aarch32_reg) {
9963     case 0 ... 7:
9964         return aarch32_reg;
9965     case 8 ... 12:
9966         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9967     case 13:
9968         switch (mode) {
9969         case ARM_CPU_MODE_USR:
9970         case ARM_CPU_MODE_SYS:
9971             return 13;
9972         case ARM_CPU_MODE_HYP:
9973             return 15;
9974         case ARM_CPU_MODE_IRQ:
9975             return 17;
9976         case ARM_CPU_MODE_SVC:
9977             return 19;
9978         case ARM_CPU_MODE_ABT:
9979             return 21;
9980         case ARM_CPU_MODE_UND:
9981             return 23;
9982         case ARM_CPU_MODE_FIQ:
9983             return 29;
9984         default:
9985             g_assert_not_reached();
9986         }
9987     case 14:
9988         switch (mode) {
9989         case ARM_CPU_MODE_USR:
9990         case ARM_CPU_MODE_SYS:
9991         case ARM_CPU_MODE_HYP:
9992             return 14;
9993         case ARM_CPU_MODE_IRQ:
9994             return 16;
9995         case ARM_CPU_MODE_SVC:
9996             return 18;
9997         case ARM_CPU_MODE_ABT:
9998             return 20;
9999         case ARM_CPU_MODE_UND:
10000             return 22;
10001         case ARM_CPU_MODE_FIQ:
10002             return 30;
10003         default:
10004             g_assert_not_reached();
10005         }
10006     case 15:
10007         return 31;
10008     default:
10009         g_assert_not_reached();
10010     }
10011 }
10012 
10013 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10014 {
10015     uint32_t ret = cpsr_read(env);
10016 
10017     /* Move DIT to the correct location for SPSR_ELx */
10018     if (ret & CPSR_DIT) {
10019         ret &= ~CPSR_DIT;
10020         ret |= PSTATE_DIT;
10021     }
10022     /* Merge PSTATE.SS into SPSR_ELx */
10023     ret |= env->pstate & PSTATE_SS;
10024 
10025     return ret;
10026 }
10027 
10028 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10029 {
10030     /* Return true if this syndrome value is a synchronous external abort */
10031     switch (syn_get_ec(syndrome)) {
10032     case EC_INSNABORT:
10033     case EC_INSNABORT_SAME_EL:
10034     case EC_DATAABORT:
10035     case EC_DATAABORT_SAME_EL:
10036         /* Look at fault status code for all the synchronous ext abort cases */
10037         switch (syndrome & 0x3f) {
10038         case 0x10:
10039         case 0x13:
10040         case 0x14:
10041         case 0x15:
10042         case 0x16:
10043         case 0x17:
10044             return true;
10045         default:
10046             return false;
10047         }
10048     default:
10049         return false;
10050     }
10051 }
10052 
10053 /* Handle exception entry to a target EL which is using AArch64 */
10054 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10055 {
10056     ARMCPU *cpu = ARM_CPU(cs);
10057     CPUARMState *env = &cpu->env;
10058     unsigned int new_el = env->exception.target_el;
10059     target_ulong addr = env->cp15.vbar_el[new_el];
10060     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10061     unsigned int old_mode;
10062     unsigned int cur_el = arm_current_el(env);
10063     int rt;
10064 
10065     /*
10066      * Note that new_el can never be 0.  If cur_el is 0, then
10067      * el0_a64 is is_a64(), else el0_a64 is ignored.
10068      */
10069     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10070 
10071     if (cur_el < new_el) {
10072         /* Entry vector offset depends on whether the implemented EL
10073          * immediately lower than the target level is using AArch32 or AArch64
10074          */
10075         bool is_aa64;
10076         uint64_t hcr;
10077 
10078         switch (new_el) {
10079         case 3:
10080             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10081             break;
10082         case 2:
10083             hcr = arm_hcr_el2_eff(env);
10084             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10085                 is_aa64 = (hcr & HCR_RW) != 0;
10086                 break;
10087             }
10088             /* fall through */
10089         case 1:
10090             is_aa64 = is_a64(env);
10091             break;
10092         default:
10093             g_assert_not_reached();
10094         }
10095 
10096         if (is_aa64) {
10097             addr += 0x400;
10098         } else {
10099             addr += 0x600;
10100         }
10101     } else if (pstate_read(env) & PSTATE_SP) {
10102         addr += 0x200;
10103     }
10104 
10105     switch (cs->exception_index) {
10106     case EXCP_PREFETCH_ABORT:
10107     case EXCP_DATA_ABORT:
10108         /*
10109          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10110          * to be taken to the SError vector entrypoint.
10111          */
10112         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10113             syndrome_is_sync_extabt(env->exception.syndrome)) {
10114             addr += 0x180;
10115         }
10116         env->cp15.far_el[new_el] = env->exception.vaddress;
10117         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10118                       env->cp15.far_el[new_el]);
10119         /* fall through */
10120     case EXCP_BKPT:
10121     case EXCP_UDEF:
10122     case EXCP_SWI:
10123     case EXCP_HVC:
10124     case EXCP_HYP_TRAP:
10125     case EXCP_SMC:
10126         switch (syn_get_ec(env->exception.syndrome)) {
10127         case EC_ADVSIMDFPACCESSTRAP:
10128             /*
10129              * QEMU internal FP/SIMD syndromes from AArch32 include the
10130              * TA and coproc fields which are only exposed if the exception
10131              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10132              * AArch64 format syndrome.
10133              */
10134             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10135             break;
10136         case EC_CP14RTTRAP:
10137         case EC_CP15RTTRAP:
10138         case EC_CP14DTTRAP:
10139             /*
10140              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10141              * the raw register field from the insn; when taking this to
10142              * AArch64 we must convert it to the AArch64 view of the register
10143              * number. Notice that we read a 4-bit AArch32 register number and
10144              * write back a 5-bit AArch64 one.
10145              */
10146             rt = extract32(env->exception.syndrome, 5, 4);
10147             rt = aarch64_regnum(env, rt);
10148             env->exception.syndrome = deposit32(env->exception.syndrome,
10149                                                 5, 5, rt);
10150             break;
10151         case EC_CP15RRTTRAP:
10152         case EC_CP14RRTTRAP:
10153             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10154             rt = extract32(env->exception.syndrome, 5, 4);
10155             rt = aarch64_regnum(env, rt);
10156             env->exception.syndrome = deposit32(env->exception.syndrome,
10157                                                 5, 5, rt);
10158             rt = extract32(env->exception.syndrome, 10, 4);
10159             rt = aarch64_regnum(env, rt);
10160             env->exception.syndrome = deposit32(env->exception.syndrome,
10161                                                 10, 5, rt);
10162             break;
10163         }
10164         env->cp15.esr_el[new_el] = env->exception.syndrome;
10165         break;
10166     case EXCP_IRQ:
10167     case EXCP_VIRQ:
10168         addr += 0x80;
10169         break;
10170     case EXCP_FIQ:
10171     case EXCP_VFIQ:
10172         addr += 0x100;
10173         break;
10174     case EXCP_VSERR:
10175         addr += 0x180;
10176         /* Construct the SError syndrome from IDS and ISS fields. */
10177         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10178         env->cp15.esr_el[new_el] = env->exception.syndrome;
10179         break;
10180     default:
10181         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10182     }
10183 
10184     if (is_a64(env)) {
10185         old_mode = pstate_read(env);
10186         aarch64_save_sp(env, arm_current_el(env));
10187         env->elr_el[new_el] = env->pc;
10188     } else {
10189         old_mode = cpsr_read_for_spsr_elx(env);
10190         env->elr_el[new_el] = env->regs[15];
10191 
10192         aarch64_sync_32_to_64(env);
10193 
10194         env->condexec_bits = 0;
10195     }
10196     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10197 
10198     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10199                   env->elr_el[new_el]);
10200 
10201     if (cpu_isar_feature(aa64_pan, cpu)) {
10202         /* The value of PSTATE.PAN is normally preserved, except when ... */
10203         new_mode |= old_mode & PSTATE_PAN;
10204         switch (new_el) {
10205         case 2:
10206             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10207             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10208                 != (HCR_E2H | HCR_TGE)) {
10209                 break;
10210             }
10211             /* fall through */
10212         case 1:
10213             /* ... the target is EL1 ... */
10214             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10215             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10216                 new_mode |= PSTATE_PAN;
10217             }
10218             break;
10219         }
10220     }
10221     if (cpu_isar_feature(aa64_mte, cpu)) {
10222         new_mode |= PSTATE_TCO;
10223     }
10224 
10225     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10226         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10227             new_mode |= PSTATE_SSBS;
10228         } else {
10229             new_mode &= ~PSTATE_SSBS;
10230         }
10231     }
10232 
10233     pstate_write(env, PSTATE_DAIF | new_mode);
10234     env->aarch64 = true;
10235     aarch64_restore_sp(env, new_el);
10236     helper_rebuild_hflags_a64(env, new_el);
10237 
10238     env->pc = addr;
10239 
10240     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10241                   new_el, env->pc, pstate_read(env));
10242 }
10243 
10244 /*
10245  * Do semihosting call and set the appropriate return value. All the
10246  * permission and validity checks have been done at translate time.
10247  *
10248  * We only see semihosting exceptions in TCG only as they are not
10249  * trapped to the hypervisor in KVM.
10250  */
10251 #ifdef CONFIG_TCG
10252 static void handle_semihosting(CPUState *cs)
10253 {
10254     ARMCPU *cpu = ARM_CPU(cs);
10255     CPUARMState *env = &cpu->env;
10256 
10257     if (is_a64(env)) {
10258         qemu_log_mask(CPU_LOG_INT,
10259                       "...handling as semihosting call 0x%" PRIx64 "\n",
10260                       env->xregs[0]);
10261         do_common_semihosting(cs);
10262         env->pc += 4;
10263     } else {
10264         qemu_log_mask(CPU_LOG_INT,
10265                       "...handling as semihosting call 0x%x\n",
10266                       env->regs[0]);
10267         do_common_semihosting(cs);
10268         env->regs[15] += env->thumb ? 2 : 4;
10269     }
10270 }
10271 #endif
10272 
10273 /* Handle a CPU exception for A and R profile CPUs.
10274  * Do any appropriate logging, handle PSCI calls, and then hand off
10275  * to the AArch64-entry or AArch32-entry function depending on the
10276  * target exception level's register width.
10277  *
10278  * Note: this is used for both TCG (as the do_interrupt tcg op),
10279  *       and KVM to re-inject guest debug exceptions, and to
10280  *       inject a Synchronous-External-Abort.
10281  */
10282 void arm_cpu_do_interrupt(CPUState *cs)
10283 {
10284     ARMCPU *cpu = ARM_CPU(cs);
10285     CPUARMState *env = &cpu->env;
10286     unsigned int new_el = env->exception.target_el;
10287 
10288     assert(!arm_feature(env, ARM_FEATURE_M));
10289 
10290     arm_log_exception(cs);
10291     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10292                   new_el);
10293     if (qemu_loglevel_mask(CPU_LOG_INT)
10294         && !excp_is_internal(cs->exception_index)) {
10295         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10296                       syn_get_ec(env->exception.syndrome),
10297                       env->exception.syndrome);
10298     }
10299 
10300     if (arm_is_psci_call(cpu, cs->exception_index)) {
10301         arm_handle_psci_call(cpu);
10302         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10303         return;
10304     }
10305 
10306     /*
10307      * Semihosting semantics depend on the register width of the code
10308      * that caused the exception, not the target exception level, so
10309      * must be handled here.
10310      */
10311 #ifdef CONFIG_TCG
10312     if (cs->exception_index == EXCP_SEMIHOST) {
10313         handle_semihosting(cs);
10314         return;
10315     }
10316 #endif
10317 
10318     /* Hooks may change global state so BQL should be held, also the
10319      * BQL needs to be held for any modification of
10320      * cs->interrupt_request.
10321      */
10322     g_assert(qemu_mutex_iothread_locked());
10323 
10324     arm_call_pre_el_change_hook(cpu);
10325 
10326     assert(!excp_is_internal(cs->exception_index));
10327     if (arm_el_is_aa64(env, new_el)) {
10328         arm_cpu_do_interrupt_aarch64(cs);
10329     } else {
10330         arm_cpu_do_interrupt_aarch32(cs);
10331     }
10332 
10333     arm_call_el_change_hook(cpu);
10334 
10335     if (!kvm_enabled()) {
10336         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10337     }
10338 }
10339 #endif /* !CONFIG_USER_ONLY */
10340 
10341 uint64_t arm_sctlr(CPUARMState *env, int el)
10342 {
10343     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10344     if (el == 0) {
10345         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10346         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10347     }
10348     return env->cp15.sctlr_el[el];
10349 }
10350 
10351 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10352 {
10353     if (regime_has_2_ranges(mmu_idx)) {
10354         return extract64(tcr, 37, 2);
10355     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10356         return 0; /* VTCR_EL2 */
10357     } else {
10358         /* Replicate the single TBI bit so we always have 2 bits.  */
10359         return extract32(tcr, 20, 1) * 3;
10360     }
10361 }
10362 
10363 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10364 {
10365     if (regime_has_2_ranges(mmu_idx)) {
10366         return extract64(tcr, 51, 2);
10367     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10368         return 0; /* VTCR_EL2 */
10369     } else {
10370         /* Replicate the single TBID bit so we always have 2 bits.  */
10371         return extract32(tcr, 29, 1) * 3;
10372     }
10373 }
10374 
10375 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10376 {
10377     if (regime_has_2_ranges(mmu_idx)) {
10378         return extract64(tcr, 57, 2);
10379     } else {
10380         /* Replicate the single TCMA bit so we always have 2 bits.  */
10381         return extract32(tcr, 30, 1) * 3;
10382     }
10383 }
10384 
10385 static ARMGranuleSize tg0_to_gran_size(int tg)
10386 {
10387     switch (tg) {
10388     case 0:
10389         return Gran4K;
10390     case 1:
10391         return Gran64K;
10392     case 2:
10393         return Gran16K;
10394     default:
10395         return GranInvalid;
10396     }
10397 }
10398 
10399 static ARMGranuleSize tg1_to_gran_size(int tg)
10400 {
10401     switch (tg) {
10402     case 1:
10403         return Gran16K;
10404     case 2:
10405         return Gran4K;
10406     case 3:
10407         return Gran64K;
10408     default:
10409         return GranInvalid;
10410     }
10411 }
10412 
10413 static inline bool have4k(ARMCPU *cpu, bool stage2)
10414 {
10415     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10416         : cpu_isar_feature(aa64_tgran4, cpu);
10417 }
10418 
10419 static inline bool have16k(ARMCPU *cpu, bool stage2)
10420 {
10421     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10422         : cpu_isar_feature(aa64_tgran16, cpu);
10423 }
10424 
10425 static inline bool have64k(ARMCPU *cpu, bool stage2)
10426 {
10427     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10428         : cpu_isar_feature(aa64_tgran64, cpu);
10429 }
10430 
10431 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10432                                          bool stage2)
10433 {
10434     switch (gran) {
10435     case Gran4K:
10436         if (have4k(cpu, stage2)) {
10437             return gran;
10438         }
10439         break;
10440     case Gran16K:
10441         if (have16k(cpu, stage2)) {
10442             return gran;
10443         }
10444         break;
10445     case Gran64K:
10446         if (have64k(cpu, stage2)) {
10447             return gran;
10448         }
10449         break;
10450     case GranInvalid:
10451         break;
10452     }
10453     /*
10454      * If the guest selects a granule size that isn't implemented,
10455      * the architecture requires that we behave as if it selected one
10456      * that is (with an IMPDEF choice of which one to pick). We choose
10457      * to implement the smallest supported granule size.
10458      */
10459     if (have4k(cpu, stage2)) {
10460         return Gran4K;
10461     }
10462     if (have16k(cpu, stage2)) {
10463         return Gran16K;
10464     }
10465     assert(have64k(cpu, stage2));
10466     return Gran64K;
10467 }
10468 
10469 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10470                                    ARMMMUIdx mmu_idx, bool data)
10471 {
10472     uint64_t tcr = regime_tcr(env, mmu_idx);
10473     bool epd, hpd, tsz_oob, ds;
10474     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10475     ARMGranuleSize gran;
10476     ARMCPU *cpu = env_archcpu(env);
10477     bool stage2 = mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S;
10478 
10479     if (!regime_has_2_ranges(mmu_idx)) {
10480         select = 0;
10481         tsz = extract32(tcr, 0, 6);
10482         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10483         if (stage2) {
10484             /* VTCR_EL2 */
10485             hpd = false;
10486         } else {
10487             hpd = extract32(tcr, 24, 1);
10488         }
10489         epd = false;
10490         sh = extract32(tcr, 12, 2);
10491         ps = extract32(tcr, 16, 3);
10492         ds = extract64(tcr, 32, 1);
10493     } else {
10494         /*
10495          * Bit 55 is always between the two regions, and is canonical for
10496          * determining if address tagging is enabled.
10497          */
10498         select = extract64(va, 55, 1);
10499         if (!select) {
10500             tsz = extract32(tcr, 0, 6);
10501             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10502             epd = extract32(tcr, 7, 1);
10503             sh = extract32(tcr, 12, 2);
10504             hpd = extract64(tcr, 41, 1);
10505         } else {
10506             tsz = extract32(tcr, 16, 6);
10507             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10508             epd = extract32(tcr, 23, 1);
10509             sh = extract32(tcr, 28, 2);
10510             hpd = extract64(tcr, 42, 1);
10511         }
10512         ps = extract64(tcr, 32, 3);
10513         ds = extract64(tcr, 59, 1);
10514     }
10515 
10516     gran = sanitize_gran_size(cpu, gran, stage2);
10517 
10518     if (cpu_isar_feature(aa64_st, cpu)) {
10519         max_tsz = 48 - (gran == Gran64K);
10520     } else {
10521         max_tsz = 39;
10522     }
10523 
10524     /*
10525      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10526      * adjust the effective value of DS, as documented.
10527      */
10528     min_tsz = 16;
10529     if (gran == Gran64K) {
10530         if (cpu_isar_feature(aa64_lva, cpu)) {
10531             min_tsz = 12;
10532         }
10533         ds = false;
10534     } else if (ds) {
10535         switch (mmu_idx) {
10536         case ARMMMUIdx_Stage2:
10537         case ARMMMUIdx_Stage2_S:
10538             if (gran == Gran16K) {
10539                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10540             } else {
10541                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10542             }
10543             break;
10544         default:
10545             if (gran == Gran16K) {
10546                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10547             } else {
10548                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10549             }
10550             break;
10551         }
10552         if (ds) {
10553             min_tsz = 12;
10554         }
10555     }
10556 
10557     if (tsz > max_tsz) {
10558         tsz = max_tsz;
10559         tsz_oob = true;
10560     } else if (tsz < min_tsz) {
10561         tsz = min_tsz;
10562         tsz_oob = true;
10563     } else {
10564         tsz_oob = false;
10565     }
10566 
10567     /* Present TBI as a composite with TBID.  */
10568     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10569     if (!data) {
10570         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10571     }
10572     tbi = (tbi >> select) & 1;
10573 
10574     return (ARMVAParameters) {
10575         .tsz = tsz,
10576         .ps = ps,
10577         .sh = sh,
10578         .select = select,
10579         .tbi = tbi,
10580         .epd = epd,
10581         .hpd = hpd,
10582         .tsz_oob = tsz_oob,
10583         .ds = ds,
10584         .gran = gran,
10585     };
10586 }
10587 
10588 /* Note that signed overflow is undefined in C.  The following routines are
10589    careful to use unsigned types where modulo arithmetic is required.
10590    Failure to do so _will_ break on newer gcc.  */
10591 
10592 /* Signed saturating arithmetic.  */
10593 
10594 /* Perform 16-bit signed saturating addition.  */
10595 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10596 {
10597     uint16_t res;
10598 
10599     res = a + b;
10600     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10601         if (a & 0x8000)
10602             res = 0x8000;
10603         else
10604             res = 0x7fff;
10605     }
10606     return res;
10607 }
10608 
10609 /* Perform 8-bit signed saturating addition.  */
10610 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10611 {
10612     uint8_t res;
10613 
10614     res = a + b;
10615     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10616         if (a & 0x80)
10617             res = 0x80;
10618         else
10619             res = 0x7f;
10620     }
10621     return res;
10622 }
10623 
10624 /* Perform 16-bit signed saturating subtraction.  */
10625 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10626 {
10627     uint16_t res;
10628 
10629     res = a - b;
10630     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10631         if (a & 0x8000)
10632             res = 0x8000;
10633         else
10634             res = 0x7fff;
10635     }
10636     return res;
10637 }
10638 
10639 /* Perform 8-bit signed saturating subtraction.  */
10640 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10641 {
10642     uint8_t res;
10643 
10644     res = a - b;
10645     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10646         if (a & 0x80)
10647             res = 0x80;
10648         else
10649             res = 0x7f;
10650     }
10651     return res;
10652 }
10653 
10654 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10655 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10656 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10657 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10658 #define PFX q
10659 
10660 #include "op_addsub.h"
10661 
10662 /* Unsigned saturating arithmetic.  */
10663 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10664 {
10665     uint16_t res;
10666     res = a + b;
10667     if (res < a)
10668         res = 0xffff;
10669     return res;
10670 }
10671 
10672 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10673 {
10674     if (a > b)
10675         return a - b;
10676     else
10677         return 0;
10678 }
10679 
10680 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10681 {
10682     uint8_t res;
10683     res = a + b;
10684     if (res < a)
10685         res = 0xff;
10686     return res;
10687 }
10688 
10689 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10690 {
10691     if (a > b)
10692         return a - b;
10693     else
10694         return 0;
10695 }
10696 
10697 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10698 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10699 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10700 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10701 #define PFX uq
10702 
10703 #include "op_addsub.h"
10704 
10705 /* Signed modulo arithmetic.  */
10706 #define SARITH16(a, b, n, op) do { \
10707     int32_t sum; \
10708     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10709     RESULT(sum, n, 16); \
10710     if (sum >= 0) \
10711         ge |= 3 << (n * 2); \
10712     } while(0)
10713 
10714 #define SARITH8(a, b, n, op) do { \
10715     int32_t sum; \
10716     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10717     RESULT(sum, n, 8); \
10718     if (sum >= 0) \
10719         ge |= 1 << n; \
10720     } while(0)
10721 
10722 
10723 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10724 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10725 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10726 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10727 #define PFX s
10728 #define ARITH_GE
10729 
10730 #include "op_addsub.h"
10731 
10732 /* Unsigned modulo arithmetic.  */
10733 #define ADD16(a, b, n) do { \
10734     uint32_t sum; \
10735     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10736     RESULT(sum, n, 16); \
10737     if ((sum >> 16) == 1) \
10738         ge |= 3 << (n * 2); \
10739     } while(0)
10740 
10741 #define ADD8(a, b, n) do { \
10742     uint32_t sum; \
10743     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10744     RESULT(sum, n, 8); \
10745     if ((sum >> 8) == 1) \
10746         ge |= 1 << n; \
10747     } while(0)
10748 
10749 #define SUB16(a, b, n) do { \
10750     uint32_t sum; \
10751     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10752     RESULT(sum, n, 16); \
10753     if ((sum >> 16) == 0) \
10754         ge |= 3 << (n * 2); \
10755     } while(0)
10756 
10757 #define SUB8(a, b, n) do { \
10758     uint32_t sum; \
10759     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10760     RESULT(sum, n, 8); \
10761     if ((sum >> 8) == 0) \
10762         ge |= 1 << n; \
10763     } while(0)
10764 
10765 #define PFX u
10766 #define ARITH_GE
10767 
10768 #include "op_addsub.h"
10769 
10770 /* Halved signed arithmetic.  */
10771 #define ADD16(a, b, n) \
10772   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10773 #define SUB16(a, b, n) \
10774   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10775 #define ADD8(a, b, n) \
10776   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10777 #define SUB8(a, b, n) \
10778   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10779 #define PFX sh
10780 
10781 #include "op_addsub.h"
10782 
10783 /* Halved unsigned arithmetic.  */
10784 #define ADD16(a, b, n) \
10785   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10786 #define SUB16(a, b, n) \
10787   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10788 #define ADD8(a, b, n) \
10789   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10790 #define SUB8(a, b, n) \
10791   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10792 #define PFX uh
10793 
10794 #include "op_addsub.h"
10795 
10796 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10797 {
10798     if (a > b)
10799         return a - b;
10800     else
10801         return b - a;
10802 }
10803 
10804 /* Unsigned sum of absolute byte differences.  */
10805 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10806 {
10807     uint32_t sum;
10808     sum = do_usad(a, b);
10809     sum += do_usad(a >> 8, b >> 8);
10810     sum += do_usad(a >> 16, b >> 16);
10811     sum += do_usad(a >> 24, b >> 24);
10812     return sum;
10813 }
10814 
10815 /* For ARMv6 SEL instruction.  */
10816 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10817 {
10818     uint32_t mask;
10819 
10820     mask = 0;
10821     if (flags & 1)
10822         mask |= 0xff;
10823     if (flags & 2)
10824         mask |= 0xff00;
10825     if (flags & 4)
10826         mask |= 0xff0000;
10827     if (flags & 8)
10828         mask |= 0xff000000;
10829     return (a & mask) | (b & ~mask);
10830 }
10831 
10832 /* CRC helpers.
10833  * The upper bytes of val (above the number specified by 'bytes') must have
10834  * been zeroed out by the caller.
10835  */
10836 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10837 {
10838     uint8_t buf[4];
10839 
10840     stl_le_p(buf, val);
10841 
10842     /* zlib crc32 converts the accumulator and output to one's complement.  */
10843     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10844 }
10845 
10846 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10847 {
10848     uint8_t buf[4];
10849 
10850     stl_le_p(buf, val);
10851 
10852     /* Linux crc32c converts the output to one's complement.  */
10853     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10854 }
10855 
10856 /* Return the exception level to which FP-disabled exceptions should
10857  * be taken, or 0 if FP is enabled.
10858  */
10859 int fp_exception_el(CPUARMState *env, int cur_el)
10860 {
10861 #ifndef CONFIG_USER_ONLY
10862     uint64_t hcr_el2;
10863 
10864     /* CPACR and the CPTR registers don't exist before v6, so FP is
10865      * always accessible
10866      */
10867     if (!arm_feature(env, ARM_FEATURE_V6)) {
10868         return 0;
10869     }
10870 
10871     if (arm_feature(env, ARM_FEATURE_M)) {
10872         /* CPACR can cause a NOCP UsageFault taken to current security state */
10873         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10874             return 1;
10875         }
10876 
10877         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10878             if (!extract32(env->v7m.nsacr, 10, 1)) {
10879                 /* FP insns cause a NOCP UsageFault taken to Secure */
10880                 return 3;
10881             }
10882         }
10883 
10884         return 0;
10885     }
10886 
10887     hcr_el2 = arm_hcr_el2_eff(env);
10888 
10889     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10890      * 0, 2 : trap EL0 and EL1/PL1 accesses
10891      * 1    : trap only EL0 accesses
10892      * 3    : trap no accesses
10893      * This register is ignored if E2H+TGE are both set.
10894      */
10895     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10896         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10897 
10898         switch (fpen) {
10899         case 1:
10900             if (cur_el != 0) {
10901                 break;
10902             }
10903             /* fall through */
10904         case 0:
10905         case 2:
10906             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10907             if (!arm_el_is_aa64(env, 3)
10908                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10909                 return 3;
10910             }
10911             if (cur_el <= 1) {
10912                 return 1;
10913             }
10914             break;
10915         }
10916     }
10917 
10918     /*
10919      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10920      * to control non-secure access to the FPU. It doesn't have any
10921      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10922      */
10923     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10924          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10925         if (!extract32(env->cp15.nsacr, 10, 1)) {
10926             /* FP insns act as UNDEF */
10927             return cur_el == 2 ? 2 : 1;
10928         }
10929     }
10930 
10931     /*
10932      * CPTR_EL2 is present in v7VE or v8, and changes format
10933      * with HCR_EL2.E2H (regardless of TGE).
10934      */
10935     if (cur_el <= 2) {
10936         if (hcr_el2 & HCR_E2H) {
10937             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10938             case 1:
10939                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10940                     break;
10941                 }
10942                 /* fall through */
10943             case 0:
10944             case 2:
10945                 return 2;
10946             }
10947         } else if (arm_is_el2_enabled(env)) {
10948             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10949                 return 2;
10950             }
10951         }
10952     }
10953 
10954     /* CPTR_EL3 : present in v8 */
10955     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10956         /* Trap all FP ops to EL3 */
10957         return 3;
10958     }
10959 #endif
10960     return 0;
10961 }
10962 
10963 /* Return the exception level we're running at if this is our mmu_idx */
10964 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10965 {
10966     if (mmu_idx & ARM_MMU_IDX_M) {
10967         return mmu_idx & ARM_MMU_IDX_M_PRIV;
10968     }
10969 
10970     switch (mmu_idx) {
10971     case ARMMMUIdx_E10_0:
10972     case ARMMMUIdx_E20_0:
10973         return 0;
10974     case ARMMMUIdx_E10_1:
10975     case ARMMMUIdx_E10_1_PAN:
10976         return 1;
10977     case ARMMMUIdx_E2:
10978     case ARMMMUIdx_E20_2:
10979     case ARMMMUIdx_E20_2_PAN:
10980         return 2;
10981     case ARMMMUIdx_E3:
10982         return 3;
10983     default:
10984         g_assert_not_reached();
10985     }
10986 }
10987 
10988 #ifndef CONFIG_TCG
10989 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10990 {
10991     g_assert_not_reached();
10992 }
10993 #endif
10994 
10995 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10996 {
10997     ARMMMUIdx idx;
10998     uint64_t hcr;
10999 
11000     if (arm_feature(env, ARM_FEATURE_M)) {
11001         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11002     }
11003 
11004     /* See ARM pseudo-function ELIsInHost.  */
11005     switch (el) {
11006     case 0:
11007         hcr = arm_hcr_el2_eff(env);
11008         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11009             idx = ARMMMUIdx_E20_0;
11010         } else {
11011             idx = ARMMMUIdx_E10_0;
11012         }
11013         break;
11014     case 1:
11015         if (env->pstate & PSTATE_PAN) {
11016             idx = ARMMMUIdx_E10_1_PAN;
11017         } else {
11018             idx = ARMMMUIdx_E10_1;
11019         }
11020         break;
11021     case 2:
11022         /* Note that TGE does not apply at EL2.  */
11023         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11024             if (env->pstate & PSTATE_PAN) {
11025                 idx = ARMMMUIdx_E20_2_PAN;
11026             } else {
11027                 idx = ARMMMUIdx_E20_2;
11028             }
11029         } else {
11030             idx = ARMMMUIdx_E2;
11031         }
11032         break;
11033     case 3:
11034         return ARMMMUIdx_E3;
11035     default:
11036         g_assert_not_reached();
11037     }
11038 
11039     return idx;
11040 }
11041 
11042 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11043 {
11044     return arm_mmu_idx_el(env, arm_current_el(env));
11045 }
11046 
11047 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11048                                            ARMMMUIdx mmu_idx,
11049                                            CPUARMTBFlags flags)
11050 {
11051     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11052     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11053 
11054     if (arm_singlestep_active(env)) {
11055         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11056     }
11057     return flags;
11058 }
11059 
11060 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11061                                               ARMMMUIdx mmu_idx,
11062                                               CPUARMTBFlags flags)
11063 {
11064     bool sctlr_b = arm_sctlr_b(env);
11065 
11066     if (sctlr_b) {
11067         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11068     }
11069     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11070         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11071     }
11072     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11073 
11074     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11075 }
11076 
11077 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11078                                         ARMMMUIdx mmu_idx)
11079 {
11080     CPUARMTBFlags flags = {};
11081     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11082 
11083     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11084     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11085         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11086     }
11087 
11088     if (arm_v7m_is_handler_mode(env)) {
11089         DP_TBFLAG_M32(flags, HANDLER, 1);
11090     }
11091 
11092     /*
11093      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11094      * is suppressing them because the requested execution priority
11095      * is less than 0.
11096      */
11097     if (arm_feature(env, ARM_FEATURE_V8) &&
11098         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11099           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11100         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11101     }
11102 
11103     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11104         DP_TBFLAG_M32(flags, SECURE, 1);
11105     }
11106 
11107     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11108 }
11109 
11110 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11111                                         ARMMMUIdx mmu_idx)
11112 {
11113     CPUARMTBFlags flags = {};
11114     int el = arm_current_el(env);
11115 
11116     if (arm_sctlr(env, el) & SCTLR_A) {
11117         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11118     }
11119 
11120     if (arm_el_is_aa64(env, 1)) {
11121         DP_TBFLAG_A32(flags, VFPEN, 1);
11122     }
11123 
11124     if (el < 2 && env->cp15.hstr_el2 &&
11125         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11126         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11127     }
11128 
11129     if (env->uncached_cpsr & CPSR_IL) {
11130         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11131     }
11132 
11133     /*
11134      * The SME exception we are testing for is raised via
11135      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11136      * AArch32.CheckAdvSIMDOrFPEnabled().
11137      */
11138     if (el == 0
11139         && FIELD_EX64(env->svcr, SVCR, SM)
11140         && (!arm_is_el2_enabled(env)
11141             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11142         && arm_el_is_aa64(env, 1)
11143         && !sme_fa64(env, el)) {
11144         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11145     }
11146 
11147     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11148 }
11149 
11150 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11151                                         ARMMMUIdx mmu_idx)
11152 {
11153     CPUARMTBFlags flags = {};
11154     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11155     uint64_t tcr = regime_tcr(env, mmu_idx);
11156     uint64_t sctlr;
11157     int tbii, tbid;
11158 
11159     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11160 
11161     /* Get control bits for tagged addresses.  */
11162     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11163     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11164 
11165     DP_TBFLAG_A64(flags, TBII, tbii);
11166     DP_TBFLAG_A64(flags, TBID, tbid);
11167 
11168     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11169         int sve_el = sve_exception_el(env, el);
11170 
11171         /*
11172          * If either FP or SVE are disabled, translator does not need len.
11173          * If SVE EL > FP EL, FP exception has precedence, and translator
11174          * does not need SVE EL.  Save potential re-translations by forcing
11175          * the unneeded data to zero.
11176          */
11177         if (fp_el != 0) {
11178             if (sve_el > fp_el) {
11179                 sve_el = 0;
11180             }
11181         } else if (sve_el == 0) {
11182             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11183         }
11184         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11185     }
11186     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11187         int sme_el = sme_exception_el(env, el);
11188         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11189 
11190         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11191         if (sme_el == 0) {
11192             /* Similarly, do not compute SVL if SME is disabled. */
11193             int svl = sve_vqm1_for_el_sm(env, el, true);
11194             DP_TBFLAG_A64(flags, SVL, svl);
11195             if (sm) {
11196                 /* If SVE is disabled, we will not have set VL above. */
11197                 DP_TBFLAG_A64(flags, VL, svl);
11198             }
11199         }
11200         if (sm) {
11201             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11202             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11203         }
11204         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11205     }
11206 
11207     sctlr = regime_sctlr(env, stage1);
11208 
11209     if (sctlr & SCTLR_A) {
11210         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11211     }
11212 
11213     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11214         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11215     }
11216 
11217     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11218         /*
11219          * In order to save space in flags, we record only whether
11220          * pauth is "inactive", meaning all insns are implemented as
11221          * a nop, or "active" when some action must be performed.
11222          * The decision of which action to take is left to a helper.
11223          */
11224         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11225             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11226         }
11227     }
11228 
11229     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11230         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11231         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11232             DP_TBFLAG_A64(flags, BT, 1);
11233         }
11234     }
11235 
11236     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11237     if (!(env->pstate & PSTATE_UAO)) {
11238         switch (mmu_idx) {
11239         case ARMMMUIdx_E10_1:
11240         case ARMMMUIdx_E10_1_PAN:
11241             /* TODO: ARMv8.3-NV */
11242             DP_TBFLAG_A64(flags, UNPRIV, 1);
11243             break;
11244         case ARMMMUIdx_E20_2:
11245         case ARMMMUIdx_E20_2_PAN:
11246             /*
11247              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11248              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11249              */
11250             if (env->cp15.hcr_el2 & HCR_TGE) {
11251                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11252             }
11253             break;
11254         default:
11255             break;
11256         }
11257     }
11258 
11259     if (env->pstate & PSTATE_IL) {
11260         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11261     }
11262 
11263     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11264         /*
11265          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11266          * if all accesses must be Unchecked:
11267          * 1) If no TBI, then there are no tags in the address to check,
11268          * 2) If Tag Check Override, then all accesses are Unchecked,
11269          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11270          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11271          */
11272         if (allocation_tag_access_enabled(env, el, sctlr)) {
11273             DP_TBFLAG_A64(flags, ATA, 1);
11274             if (tbid
11275                 && !(env->pstate & PSTATE_TCO)
11276                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11277                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11278             }
11279         }
11280         /* And again for unprivileged accesses, if required.  */
11281         if (EX_TBFLAG_A64(flags, UNPRIV)
11282             && tbid
11283             && !(env->pstate & PSTATE_TCO)
11284             && (sctlr & SCTLR_TCF0)
11285             && allocation_tag_access_enabled(env, 0, sctlr)) {
11286             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11287         }
11288         /* Cache TCMA as well as TBI. */
11289         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11290     }
11291 
11292     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11293 }
11294 
11295 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11296 {
11297     int el = arm_current_el(env);
11298     int fp_el = fp_exception_el(env, el);
11299     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11300 
11301     if (is_a64(env)) {
11302         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11303     } else if (arm_feature(env, ARM_FEATURE_M)) {
11304         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11305     } else {
11306         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11307     }
11308 }
11309 
11310 void arm_rebuild_hflags(CPUARMState *env)
11311 {
11312     env->hflags = rebuild_hflags_internal(env);
11313 }
11314 
11315 /*
11316  * If we have triggered a EL state change we can't rely on the
11317  * translator having passed it to us, we need to recompute.
11318  */
11319 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11320 {
11321     int el = arm_current_el(env);
11322     int fp_el = fp_exception_el(env, el);
11323     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11324 
11325     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11326 }
11327 
11328 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11329 {
11330     int fp_el = fp_exception_el(env, el);
11331     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11332 
11333     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11334 }
11335 
11336 /*
11337  * If we have triggered a EL state change we can't rely on the
11338  * translator having passed it to us, we need to recompute.
11339  */
11340 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11341 {
11342     int el = arm_current_el(env);
11343     int fp_el = fp_exception_el(env, el);
11344     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11345     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11346 }
11347 
11348 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11349 {
11350     int fp_el = fp_exception_el(env, el);
11351     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11352 
11353     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11354 }
11355 
11356 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11357 {
11358     int fp_el = fp_exception_el(env, el);
11359     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11360 
11361     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11362 }
11363 
11364 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11365 {
11366 #ifdef CONFIG_DEBUG_TCG
11367     CPUARMTBFlags c = env->hflags;
11368     CPUARMTBFlags r = rebuild_hflags_internal(env);
11369 
11370     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11371         fprintf(stderr, "TCG hflags mismatch "
11372                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11373                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11374                 c.flags, c.flags2, r.flags, r.flags2);
11375         abort();
11376     }
11377 #endif
11378 }
11379 
11380 static bool mve_no_pred(CPUARMState *env)
11381 {
11382     /*
11383      * Return true if there is definitely no predication of MVE
11384      * instructions by VPR or LTPSIZE. (Returning false even if there
11385      * isn't any predication is OK; generated code will just be
11386      * a little worse.)
11387      * If the CPU does not implement MVE then this TB flag is always 0.
11388      *
11389      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11390      * logic in gen_update_fp_context() needs to be updated to match.
11391      *
11392      * We do not include the effect of the ECI bits here -- they are
11393      * tracked in other TB flags. This simplifies the logic for
11394      * "when did we emit code that changes the MVE_NO_PRED TB flag
11395      * and thus need to end the TB?".
11396      */
11397     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11398         return false;
11399     }
11400     if (env->v7m.vpr) {
11401         return false;
11402     }
11403     if (env->v7m.ltpsize < 4) {
11404         return false;
11405     }
11406     return true;
11407 }
11408 
11409 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11410                           target_ulong *cs_base, uint32_t *pflags)
11411 {
11412     CPUARMTBFlags flags;
11413 
11414     assert_hflags_rebuild_correctly(env);
11415     flags = env->hflags;
11416 
11417     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11418         *pc = env->pc;
11419         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11420             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11421         }
11422     } else {
11423         *pc = env->regs[15];
11424 
11425         if (arm_feature(env, ARM_FEATURE_M)) {
11426             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11427                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11428                 != env->v7m.secure) {
11429                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11430             }
11431 
11432             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11433                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11434                  (env->v7m.secure &&
11435                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11436                 /*
11437                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11438                  * active FP context; we must create a new FP context before
11439                  * executing any FP insn.
11440                  */
11441                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11442             }
11443 
11444             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11445             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11446                 DP_TBFLAG_M32(flags, LSPACT, 1);
11447             }
11448 
11449             if (mve_no_pred(env)) {
11450                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11451             }
11452         } else {
11453             /*
11454              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11455              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11456              */
11457             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11458                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11459             } else {
11460                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11461                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11462             }
11463             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11464                 DP_TBFLAG_A32(flags, VFPEN, 1);
11465             }
11466         }
11467 
11468         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11469         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11470     }
11471 
11472     /*
11473      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11474      * states defined in the ARM ARM for software singlestep:
11475      *  SS_ACTIVE   PSTATE.SS   State
11476      *     0            x       Inactive (the TB flag for SS is always 0)
11477      *     1            0       Active-pending
11478      *     1            1       Active-not-pending
11479      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11480      */
11481     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11482         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11483     }
11484 
11485     *pflags = flags.flags;
11486     *cs_base = flags.flags2;
11487 }
11488 
11489 #ifdef TARGET_AARCH64
11490 /*
11491  * The manual says that when SVE is enabled and VQ is widened the
11492  * implementation is allowed to zero the previously inaccessible
11493  * portion of the registers.  The corollary to that is that when
11494  * SVE is enabled and VQ is narrowed we are also allowed to zero
11495  * the now inaccessible portion of the registers.
11496  *
11497  * The intent of this is that no predicate bit beyond VQ is ever set.
11498  * Which means that some operations on predicate registers themselves
11499  * may operate on full uint64_t or even unrolled across the maximum
11500  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11501  * may well be cheaper than conditionals to restrict the operation
11502  * to the relevant portion of a uint16_t[16].
11503  */
11504 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11505 {
11506     int i, j;
11507     uint64_t pmask;
11508 
11509     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11510     assert(vq <= env_archcpu(env)->sve_max_vq);
11511 
11512     /* Zap the high bits of the zregs.  */
11513     for (i = 0; i < 32; i++) {
11514         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11515     }
11516 
11517     /* Zap the high bits of the pregs and ffr.  */
11518     pmask = 0;
11519     if (vq & 3) {
11520         pmask = ~(-1ULL << (16 * (vq & 3)));
11521     }
11522     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11523         for (i = 0; i < 17; ++i) {
11524             env->vfp.pregs[i].p[j] &= pmask;
11525         }
11526         pmask = 0;
11527     }
11528 }
11529 
11530 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11531 {
11532     int exc_el;
11533 
11534     if (sm) {
11535         exc_el = sme_exception_el(env, el);
11536     } else {
11537         exc_el = sve_exception_el(env, el);
11538     }
11539     if (exc_el) {
11540         return 0; /* disabled */
11541     }
11542     return sve_vqm1_for_el_sm(env, el, sm);
11543 }
11544 
11545 /*
11546  * Notice a change in SVE vector size when changing EL.
11547  */
11548 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11549                            int new_el, bool el0_a64)
11550 {
11551     ARMCPU *cpu = env_archcpu(env);
11552     int old_len, new_len;
11553     bool old_a64, new_a64, sm;
11554 
11555     /* Nothing to do if no SVE.  */
11556     if (!cpu_isar_feature(aa64_sve, cpu)) {
11557         return;
11558     }
11559 
11560     /* Nothing to do if FP is disabled in either EL.  */
11561     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11562         return;
11563     }
11564 
11565     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11566     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11567 
11568     /*
11569      * Both AArch64.TakeException and AArch64.ExceptionReturn
11570      * invoke ResetSVEState when taking an exception from, or
11571      * returning to, AArch32 state when PSTATE.SM is enabled.
11572      */
11573     sm = FIELD_EX64(env->svcr, SVCR, SM);
11574     if (old_a64 != new_a64 && sm) {
11575         arm_reset_sve_state(env);
11576         return;
11577     }
11578 
11579     /*
11580      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11581      * at ELx, or not available because the EL is in AArch32 state, then
11582      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11583      * has an effective value of 0".
11584      *
11585      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11586      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11587      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11588      * we already have the correct register contents when encountering the
11589      * vq0->vq0 transition between EL0->EL1.
11590      */
11591     old_len = new_len = 0;
11592     if (old_a64) {
11593         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11594     }
11595     if (new_a64) {
11596         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11597     }
11598 
11599     /* When changing vector length, clear inaccessible state.  */
11600     if (new_len < old_len) {
11601         aarch64_sve_narrow_vq(env, new_len + 1);
11602     }
11603 }
11604 #endif
11605