xref: /openbmc/qemu/target/arm/helper.c (revision ab04d2c5)
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 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
366 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
367                                     bool isread)
368 {
369     if (arm_current_el(env) == 1 &&
370         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
371         return CP_ACCESS_TRAP_EL2;
372     }
373     return CP_ACCESS_OK;
374 }
375 
376 #ifdef TARGET_AARCH64
377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
378 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
379                                     bool isread)
380 {
381     if (arm_current_el(env) == 1 &&
382         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
383         return CP_ACCESS_TRAP_EL2;
384     }
385     return CP_ACCESS_OK;
386 }
387 #endif
388 
389 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
390 {
391     ARMCPU *cpu = env_archcpu(env);
392 
393     raw_write(env, ri, value);
394     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
395 }
396 
397 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
398 {
399     ARMCPU *cpu = env_archcpu(env);
400 
401     if (raw_read(env, ri) != value) {
402         /* Unlike real hardware the qemu TLB uses virtual addresses,
403          * not modified virtual addresses, so this causes a TLB flush.
404          */
405         tlb_flush(CPU(cpu));
406         raw_write(env, ri, value);
407     }
408 }
409 
410 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
411                              uint64_t value)
412 {
413     ARMCPU *cpu = env_archcpu(env);
414 
415     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
416         && !extended_addresses_enabled(env)) {
417         /* For VMSA (when not using the LPAE long descriptor page table
418          * format) this register includes the ASID, so do a TLB flush.
419          * For PMSA it is purely a process ID and no action is needed.
420          */
421         tlb_flush(CPU(cpu));
422     }
423     raw_write(env, ri, value);
424 }
425 
426 static int alle1_tlbmask(CPUARMState *env)
427 {
428     /*
429      * Note that the 'ALL' scope must invalidate both stage 1 and
430      * stage 2 translations, whereas most other scopes only invalidate
431      * stage 1 translations.
432      */
433     return (ARMMMUIdxBit_E10_1 |
434             ARMMMUIdxBit_E10_1_PAN |
435             ARMMMUIdxBit_E10_0 |
436             ARMMMUIdxBit_Stage2 |
437             ARMMMUIdxBit_Stage2_S);
438 }
439 
440 
441 /* IS variants of TLB operations must affect all cores */
442 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
443                              uint64_t value)
444 {
445     CPUState *cs = env_cpu(env);
446 
447     tlb_flush_all_cpus_synced(cs);
448 }
449 
450 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
451                              uint64_t value)
452 {
453     CPUState *cs = env_cpu(env);
454 
455     tlb_flush_all_cpus_synced(cs);
456 }
457 
458 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
459                              uint64_t value)
460 {
461     CPUState *cs = env_cpu(env);
462 
463     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
464 }
465 
466 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
467                              uint64_t value)
468 {
469     CPUState *cs = env_cpu(env);
470 
471     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
472 }
473 
474 /*
475  * Non-IS variants of TLB operations are upgraded to
476  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
477  * force broadcast of these operations.
478  */
479 static bool tlb_force_broadcast(CPUARMState *env)
480 {
481     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
482 }
483 
484 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
485                           uint64_t value)
486 {
487     /* Invalidate all (TLBIALL) */
488     CPUState *cs = env_cpu(env);
489 
490     if (tlb_force_broadcast(env)) {
491         tlb_flush_all_cpus_synced(cs);
492     } else {
493         tlb_flush(cs);
494     }
495 }
496 
497 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
498                           uint64_t value)
499 {
500     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
501     CPUState *cs = env_cpu(env);
502 
503     value &= TARGET_PAGE_MASK;
504     if (tlb_force_broadcast(env)) {
505         tlb_flush_page_all_cpus_synced(cs, value);
506     } else {
507         tlb_flush_page(cs, value);
508     }
509 }
510 
511 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
512                            uint64_t value)
513 {
514     /* Invalidate by ASID (TLBIASID) */
515     CPUState *cs = env_cpu(env);
516 
517     if (tlb_force_broadcast(env)) {
518         tlb_flush_all_cpus_synced(cs);
519     } else {
520         tlb_flush(cs);
521     }
522 }
523 
524 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
525                            uint64_t value)
526 {
527     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
528     CPUState *cs = env_cpu(env);
529 
530     value &= TARGET_PAGE_MASK;
531     if (tlb_force_broadcast(env)) {
532         tlb_flush_page_all_cpus_synced(cs, value);
533     } else {
534         tlb_flush_page(cs, value);
535     }
536 }
537 
538 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                                uint64_t value)
540 {
541     CPUState *cs = env_cpu(env);
542 
543     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
544 }
545 
546 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
547                                   uint64_t value)
548 {
549     CPUState *cs = env_cpu(env);
550 
551     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
552 }
553 
554 
555 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
556                               uint64_t value)
557 {
558     CPUState *cs = env_cpu(env);
559 
560     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
561 }
562 
563 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
564                                  uint64_t value)
565 {
566     CPUState *cs = env_cpu(env);
567 
568     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
569 }
570 
571 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
572                               uint64_t value)
573 {
574     CPUState *cs = env_cpu(env);
575     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
576 
577     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
578 }
579 
580 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
581                                  uint64_t value)
582 {
583     CPUState *cs = env_cpu(env);
584     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
585 
586     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
587                                              ARMMMUIdxBit_E2);
588 }
589 
590 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
591                                 uint64_t value)
592 {
593     CPUState *cs = env_cpu(env);
594     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
595 
596     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
597 }
598 
599 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
600                                 uint64_t value)
601 {
602     CPUState *cs = env_cpu(env);
603     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
604 
605     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
606 }
607 
608 static const ARMCPRegInfo cp_reginfo[] = {
609     /* Define the secure and non-secure FCSE identifier CP registers
610      * separately because there is no secure bank in V8 (no _EL3).  This allows
611      * the secure register to be properly reset and migrated. There is also no
612      * v8 EL1 version of the register so the non-secure instance stands alone.
613      */
614     { .name = "FCSEIDR",
615       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
616       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
617       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
618       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
619     { .name = "FCSEIDR_S",
620       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
621       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
622       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
623       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
624     /* Define the secure and non-secure context identifier CP registers
625      * separately because there is no secure bank in V8 (no _EL3).  This allows
626      * the secure register to be properly reset and migrated.  In the
627      * non-secure case, the 32-bit register will have reset and migration
628      * disabled during registration as it is handled by the 64-bit instance.
629      */
630     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
631       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
632       .access = PL1_RW, .accessfn = access_tvm_trvm,
633       .secure = ARM_CP_SECSTATE_NS,
634       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
635       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
636     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
637       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
638       .access = PL1_RW, .accessfn = access_tvm_trvm,
639       .secure = ARM_CP_SECSTATE_S,
640       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
641       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
642 };
643 
644 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
645     /* NB: Some of these registers exist in v8 but with more precise
646      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
647      */
648     /* MMU Domain access control / MPU write buffer control */
649     { .name = "DACR",
650       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
651       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
652       .writefn = dacr_write, .raw_writefn = raw_write,
653       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
654                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
655     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
656      * For v6 and v5, these mappings are overly broad.
657      */
658     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
659       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
660     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
661       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
662     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
663       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
664     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
665       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
666     /* Cache maintenance ops; some of this space may be overridden later. */
667     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
668       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
669       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
670 };
671 
672 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
673     /* Not all pre-v6 cores implemented this WFI, so this is slightly
674      * over-broad.
675      */
676     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
677       .access = PL1_W, .type = ARM_CP_WFI },
678 };
679 
680 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
681     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
682      * is UNPREDICTABLE; we choose to NOP as most implementations do).
683      */
684     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
685       .access = PL1_W, .type = ARM_CP_WFI },
686     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
687      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
688      * OMAPCP will override this space.
689      */
690     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
691       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
692       .resetvalue = 0 },
693     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
694       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
695       .resetvalue = 0 },
696     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
697     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
698       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
699       .resetvalue = 0 },
700     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
701      * implementing it as RAZ means the "debug architecture version" bits
702      * will read as a reserved value, which should cause Linux to not try
703      * to use the debug hardware.
704      */
705     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
706       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
707     /* MMU TLB control. Note that the wildcarding means we cover not just
708      * the unified TLB ops but also the dside/iside/inner-shareable variants.
709      */
710     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
711       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
712       .type = ARM_CP_NO_RAW },
713     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
714       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
715       .type = ARM_CP_NO_RAW },
716     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
717       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
718       .type = ARM_CP_NO_RAW },
719     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
720       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
721       .type = ARM_CP_NO_RAW },
722     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
723       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
724     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
725       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
726 };
727 
728 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
729                         uint64_t value)
730 {
731     uint32_t mask = 0;
732 
733     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
734     if (!arm_feature(env, ARM_FEATURE_V8)) {
735         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
736          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
737          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
738          */
739         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
740             /* VFP coprocessor: cp10 & cp11 [23:20] */
741             mask |= R_CPACR_ASEDIS_MASK |
742                     R_CPACR_D32DIS_MASK |
743                     R_CPACR_CP11_MASK |
744                     R_CPACR_CP10_MASK;
745 
746             if (!arm_feature(env, ARM_FEATURE_NEON)) {
747                 /* ASEDIS [31] bit is RAO/WI */
748                 value |= R_CPACR_ASEDIS_MASK;
749             }
750 
751             /* VFPv3 and upwards with NEON implement 32 double precision
752              * registers (D0-D31).
753              */
754             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
755                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
756                 value |= R_CPACR_D32DIS_MASK;
757             }
758         }
759         value &= mask;
760     }
761 
762     /*
763      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
764      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
765      */
766     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
767         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
768         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
769         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
770     }
771 
772     env->cp15.cpacr_el1 = value;
773 }
774 
775 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776 {
777     /*
778      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
779      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
780      */
781     uint64_t value = env->cp15.cpacr_el1;
782 
783     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
784         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
785         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
786     }
787     return value;
788 }
789 
790 
791 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
792 {
793     /* Call cpacr_write() so that we reset with the correct RAO bits set
794      * for our CPU features.
795      */
796     cpacr_write(env, ri, 0);
797 }
798 
799 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
800                                    bool isread)
801 {
802     if (arm_feature(env, ARM_FEATURE_V8)) {
803         /* Check if CPACR accesses are to be trapped to EL2 */
804         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
805             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
806             return CP_ACCESS_TRAP_EL2;
807         /* Check if CPACR accesses are to be trapped to EL3 */
808         } else if (arm_current_el(env) < 3 &&
809                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
810             return CP_ACCESS_TRAP_EL3;
811         }
812     }
813 
814     return CP_ACCESS_OK;
815 }
816 
817 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
818                                   bool isread)
819 {
820     /* Check if CPTR accesses are set to trap to EL3 */
821     if (arm_current_el(env) == 2 &&
822         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
823         return CP_ACCESS_TRAP_EL3;
824     }
825 
826     return CP_ACCESS_OK;
827 }
828 
829 static const ARMCPRegInfo v6_cp_reginfo[] = {
830     /* prefetch by MVA in v6, NOP in v7 */
831     { .name = "MVA_prefetch",
832       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
833       .access = PL1_W, .type = ARM_CP_NOP },
834     /* We need to break the TB after ISB to execute self-modifying code
835      * correctly and also to take any pending interrupts immediately.
836      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
837      */
838     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
839       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
840     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
841       .access = PL0_W, .type = ARM_CP_NOP },
842     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
843       .access = PL0_W, .type = ARM_CP_NOP },
844     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
845       .access = PL1_RW, .accessfn = access_tvm_trvm,
846       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
847                              offsetof(CPUARMState, cp15.ifar_ns) },
848       .resetvalue = 0, },
849     /* Watchpoint Fault Address Register : should actually only be present
850      * for 1136, 1176, 11MPCore.
851      */
852     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
853       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
854     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
855       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
856       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
857       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
858 };
859 
860 typedef struct pm_event {
861     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
862     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
863     bool (*supported)(CPUARMState *);
864     /*
865      * Retrieve the current count of the underlying event. The programmed
866      * counters hold a difference from the return value from this function
867      */
868     uint64_t (*get_count)(CPUARMState *);
869     /*
870      * Return how many nanoseconds it will take (at a minimum) for count events
871      * to occur. A negative value indicates the counter will never overflow, or
872      * that the counter has otherwise arranged for the overflow bit to be set
873      * and the PMU interrupt to be raised on overflow.
874      */
875     int64_t (*ns_per_count)(uint64_t);
876 } pm_event;
877 
878 static bool event_always_supported(CPUARMState *env)
879 {
880     return true;
881 }
882 
883 static uint64_t swinc_get_count(CPUARMState *env)
884 {
885     /*
886      * SW_INCR events are written directly to the pmevcntr's by writes to
887      * PMSWINC, so there is no underlying count maintained by the PMU itself
888      */
889     return 0;
890 }
891 
892 static int64_t swinc_ns_per(uint64_t ignored)
893 {
894     return -1;
895 }
896 
897 /*
898  * Return the underlying cycle count for the PMU cycle counters. If we're in
899  * usermode, simply return 0.
900  */
901 static uint64_t cycles_get_count(CPUARMState *env)
902 {
903 #ifndef CONFIG_USER_ONLY
904     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
905                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
906 #else
907     return cpu_get_host_ticks();
908 #endif
909 }
910 
911 #ifndef CONFIG_USER_ONLY
912 static int64_t cycles_ns_per(uint64_t cycles)
913 {
914     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
915 }
916 
917 static bool instructions_supported(CPUARMState *env)
918 {
919     return icount_enabled() == 1; /* Precise instruction counting */
920 }
921 
922 static uint64_t instructions_get_count(CPUARMState *env)
923 {
924     return (uint64_t)icount_get_raw();
925 }
926 
927 static int64_t instructions_ns_per(uint64_t icount)
928 {
929     return icount_to_ns((int64_t)icount);
930 }
931 #endif
932 
933 static bool pmuv3p1_events_supported(CPUARMState *env)
934 {
935     /* For events which are supported in any v8.1 PMU */
936     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
937 }
938 
939 static bool pmuv3p4_events_supported(CPUARMState *env)
940 {
941     /* For events which are supported in any v8.1 PMU */
942     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
943 }
944 
945 static uint64_t zero_event_get_count(CPUARMState *env)
946 {
947     /* For events which on QEMU never fire, so their count is always zero */
948     return 0;
949 }
950 
951 static int64_t zero_event_ns_per(uint64_t cycles)
952 {
953     /* An event which never fires can never overflow */
954     return -1;
955 }
956 
957 static const pm_event pm_events[] = {
958     { .number = 0x000, /* SW_INCR */
959       .supported = event_always_supported,
960       .get_count = swinc_get_count,
961       .ns_per_count = swinc_ns_per,
962     },
963 #ifndef CONFIG_USER_ONLY
964     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
965       .supported = instructions_supported,
966       .get_count = instructions_get_count,
967       .ns_per_count = instructions_ns_per,
968     },
969     { .number = 0x011, /* CPU_CYCLES, Cycle */
970       .supported = event_always_supported,
971       .get_count = cycles_get_count,
972       .ns_per_count = cycles_ns_per,
973     },
974 #endif
975     { .number = 0x023, /* STALL_FRONTEND */
976       .supported = pmuv3p1_events_supported,
977       .get_count = zero_event_get_count,
978       .ns_per_count = zero_event_ns_per,
979     },
980     { .number = 0x024, /* STALL_BACKEND */
981       .supported = pmuv3p1_events_supported,
982       .get_count = zero_event_get_count,
983       .ns_per_count = zero_event_ns_per,
984     },
985     { .number = 0x03c, /* STALL */
986       .supported = pmuv3p4_events_supported,
987       .get_count = zero_event_get_count,
988       .ns_per_count = zero_event_ns_per,
989     },
990 };
991 
992 /*
993  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
994  * events (i.e. the statistical profiling extension), this implementation
995  * should first be updated to something sparse instead of the current
996  * supported_event_map[] array.
997  */
998 #define MAX_EVENT_ID 0x3c
999 #define UNSUPPORTED_EVENT UINT16_MAX
1000 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1001 
1002 /*
1003  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1004  * of ARM event numbers to indices in our pm_events array.
1005  *
1006  * Note: Events in the 0x40XX range are not currently supported.
1007  */
1008 void pmu_init(ARMCPU *cpu)
1009 {
1010     unsigned int i;
1011 
1012     /*
1013      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1014      * events to them
1015      */
1016     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1017         supported_event_map[i] = UNSUPPORTED_EVENT;
1018     }
1019     cpu->pmceid0 = 0;
1020     cpu->pmceid1 = 0;
1021 
1022     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1023         const pm_event *cnt = &pm_events[i];
1024         assert(cnt->number <= MAX_EVENT_ID);
1025         /* We do not currently support events in the 0x40xx range */
1026         assert(cnt->number <= 0x3f);
1027 
1028         if (cnt->supported(&cpu->env)) {
1029             supported_event_map[cnt->number] = i;
1030             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1031             if (cnt->number & 0x20) {
1032                 cpu->pmceid1 |= event_mask;
1033             } else {
1034                 cpu->pmceid0 |= event_mask;
1035             }
1036         }
1037     }
1038 }
1039 
1040 /*
1041  * Check at runtime whether a PMU event is supported for the current machine
1042  */
1043 static bool event_supported(uint16_t number)
1044 {
1045     if (number > MAX_EVENT_ID) {
1046         return false;
1047     }
1048     return supported_event_map[number] != UNSUPPORTED_EVENT;
1049 }
1050 
1051 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1052                                    bool isread)
1053 {
1054     /* Performance monitor registers user accessibility is controlled
1055      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1056      * trapping to EL2 or EL3 for other accesses.
1057      */
1058     int el = arm_current_el(env);
1059     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1060 
1061     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1062         return CP_ACCESS_TRAP;
1063     }
1064     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1065         return CP_ACCESS_TRAP_EL2;
1066     }
1067     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1068         return CP_ACCESS_TRAP_EL3;
1069     }
1070 
1071     return CP_ACCESS_OK;
1072 }
1073 
1074 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1075                                            const ARMCPRegInfo *ri,
1076                                            bool isread)
1077 {
1078     /* ER: event counter read trap control */
1079     if (arm_feature(env, ARM_FEATURE_V8)
1080         && arm_current_el(env) == 0
1081         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1082         && isread) {
1083         return CP_ACCESS_OK;
1084     }
1085 
1086     return pmreg_access(env, ri, isread);
1087 }
1088 
1089 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1090                                          const ARMCPRegInfo *ri,
1091                                          bool isread)
1092 {
1093     /* SW: software increment write trap control */
1094     if (arm_feature(env, ARM_FEATURE_V8)
1095         && arm_current_el(env) == 0
1096         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1097         && !isread) {
1098         return CP_ACCESS_OK;
1099     }
1100 
1101     return pmreg_access(env, ri, isread);
1102 }
1103 
1104 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1105                                         const ARMCPRegInfo *ri,
1106                                         bool isread)
1107 {
1108     /* ER: event counter read trap control */
1109     if (arm_feature(env, ARM_FEATURE_V8)
1110         && arm_current_el(env) == 0
1111         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1112         return CP_ACCESS_OK;
1113     }
1114 
1115     return pmreg_access(env, ri, isread);
1116 }
1117 
1118 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1119                                          const ARMCPRegInfo *ri,
1120                                          bool isread)
1121 {
1122     /* CR: cycle counter read trap control */
1123     if (arm_feature(env, ARM_FEATURE_V8)
1124         && arm_current_el(env) == 0
1125         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1126         && isread) {
1127         return CP_ACCESS_OK;
1128     }
1129 
1130     return pmreg_access(env, ri, isread);
1131 }
1132 
1133 /*
1134  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1135  * We use these to decide whether we need to wrap a write to MDCR_EL2
1136  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1137  */
1138 #define MDCR_EL2_PMU_ENABLE_BITS \
1139     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1140 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1141 
1142 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1143  * the current EL, security state, and register configuration.
1144  */
1145 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1146 {
1147     uint64_t filter;
1148     bool e, p, u, nsk, nsu, nsh, m;
1149     bool enabled, prohibited = false, filtered;
1150     bool secure = arm_is_secure(env);
1151     int el = arm_current_el(env);
1152     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1153     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1154 
1155     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1156         return false;
1157     }
1158 
1159     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1160             (counter < hpmn || counter == 31)) {
1161         e = env->cp15.c9_pmcr & PMCRE;
1162     } else {
1163         e = mdcr_el2 & MDCR_HPME;
1164     }
1165     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1166 
1167     /* Is event counting prohibited? */
1168     if (el == 2 && (counter < hpmn || counter == 31)) {
1169         prohibited = mdcr_el2 & MDCR_HPMD;
1170     }
1171     if (secure) {
1172         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1173     }
1174 
1175     if (counter == 31) {
1176         /*
1177          * The cycle counter defaults to running. PMCR.DP says "disable
1178          * the cycle counter when event counting is prohibited".
1179          * Some MDCR bits disable the cycle counter specifically.
1180          */
1181         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1182         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1183             if (secure) {
1184                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1185             }
1186             if (el == 2) {
1187                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1188             }
1189         }
1190     }
1191 
1192     if (counter == 31) {
1193         filter = env->cp15.pmccfiltr_el0;
1194     } else {
1195         filter = env->cp15.c14_pmevtyper[counter];
1196     }
1197 
1198     p   = filter & PMXEVTYPER_P;
1199     u   = filter & PMXEVTYPER_U;
1200     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1201     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1202     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1203     m   = arm_el_is_aa64(env, 1) &&
1204               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1205 
1206     if (el == 0) {
1207         filtered = secure ? u : u != nsu;
1208     } else if (el == 1) {
1209         filtered = secure ? p : p != nsk;
1210     } else if (el == 2) {
1211         filtered = !nsh;
1212     } else { /* EL3 */
1213         filtered = m != p;
1214     }
1215 
1216     if (counter != 31) {
1217         /*
1218          * If not checking PMCCNTR, ensure the counter is setup to an event we
1219          * support
1220          */
1221         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1222         if (!event_supported(event)) {
1223             return false;
1224         }
1225     }
1226 
1227     return enabled && !prohibited && !filtered;
1228 }
1229 
1230 static void pmu_update_irq(CPUARMState *env)
1231 {
1232     ARMCPU *cpu = env_archcpu(env);
1233     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1234             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1235 }
1236 
1237 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1238 {
1239     /*
1240      * Return true if the clock divider is enabled and the cycle counter
1241      * is supposed to tick only once every 64 clock cycles. This is
1242      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1243      * (64-bit) cycle counter PMCR.D has no effect.
1244      */
1245     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1246 }
1247 
1248 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1249 {
1250     /* Return true if the specified event counter is configured to be 64 bit */
1251 
1252     /* This isn't intended to be used with the cycle counter */
1253     assert(counter < 31);
1254 
1255     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1256         return false;
1257     }
1258 
1259     if (arm_feature(env, ARM_FEATURE_EL2)) {
1260         /*
1261          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1262          * current security state, so we don't use arm_mdcr_el2_eff() here.
1263          */
1264         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1265         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1266 
1267         if (hpmn != 0 && counter >= hpmn) {
1268             return hlp;
1269         }
1270     }
1271     return env->cp15.c9_pmcr & PMCRLP;
1272 }
1273 
1274 /*
1275  * Ensure c15_ccnt is the guest-visible count so that operations such as
1276  * enabling/disabling the counter or filtering, modifying the count itself,
1277  * etc. can be done logically. This is essentially a no-op if the counter is
1278  * not enabled at the time of the call.
1279  */
1280 static void pmccntr_op_start(CPUARMState *env)
1281 {
1282     uint64_t cycles = cycles_get_count(env);
1283 
1284     if (pmu_counter_enabled(env, 31)) {
1285         uint64_t eff_cycles = cycles;
1286         if (pmccntr_clockdiv_enabled(env)) {
1287             eff_cycles /= 64;
1288         }
1289 
1290         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1291 
1292         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1293                                  1ull << 63 : 1ull << 31;
1294         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1295             env->cp15.c9_pmovsr |= (1ULL << 31);
1296             pmu_update_irq(env);
1297         }
1298 
1299         env->cp15.c15_ccnt = new_pmccntr;
1300     }
1301     env->cp15.c15_ccnt_delta = cycles;
1302 }
1303 
1304 /*
1305  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1306  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1307  * pmccntr_op_start.
1308  */
1309 static void pmccntr_op_finish(CPUARMState *env)
1310 {
1311     if (pmu_counter_enabled(env, 31)) {
1312 #ifndef CONFIG_USER_ONLY
1313         /* Calculate when the counter will next overflow */
1314         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1315         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1316             remaining_cycles = (uint32_t)remaining_cycles;
1317         }
1318         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1319 
1320         if (overflow_in > 0) {
1321             int64_t overflow_at;
1322 
1323             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1324                                  overflow_in, &overflow_at)) {
1325                 ARMCPU *cpu = env_archcpu(env);
1326                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1327             }
1328         }
1329 #endif
1330 
1331         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1332         if (pmccntr_clockdiv_enabled(env)) {
1333             prev_cycles /= 64;
1334         }
1335         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1336     }
1337 }
1338 
1339 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1340 {
1341 
1342     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1343     uint64_t count = 0;
1344     if (event_supported(event)) {
1345         uint16_t event_idx = supported_event_map[event];
1346         count = pm_events[event_idx].get_count(env);
1347     }
1348 
1349     if (pmu_counter_enabled(env, counter)) {
1350         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1351         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1352             1ULL << 63 : 1ULL << 31;
1353 
1354         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1355             env->cp15.c9_pmovsr |= (1 << counter);
1356             pmu_update_irq(env);
1357         }
1358         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1359     }
1360     env->cp15.c14_pmevcntr_delta[counter] = count;
1361 }
1362 
1363 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1364 {
1365     if (pmu_counter_enabled(env, counter)) {
1366 #ifndef CONFIG_USER_ONLY
1367         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1368         uint16_t event_idx = supported_event_map[event];
1369         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1370         int64_t overflow_in;
1371 
1372         if (!pmevcntr_is_64_bit(env, counter)) {
1373             delta = (uint32_t)delta;
1374         }
1375         overflow_in = pm_events[event_idx].ns_per_count(delta);
1376 
1377         if (overflow_in > 0) {
1378             int64_t overflow_at;
1379 
1380             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1381                                  overflow_in, &overflow_at)) {
1382                 ARMCPU *cpu = env_archcpu(env);
1383                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1384             }
1385         }
1386 #endif
1387 
1388         env->cp15.c14_pmevcntr_delta[counter] -=
1389             env->cp15.c14_pmevcntr[counter];
1390     }
1391 }
1392 
1393 void pmu_op_start(CPUARMState *env)
1394 {
1395     unsigned int i;
1396     pmccntr_op_start(env);
1397     for (i = 0; i < pmu_num_counters(env); i++) {
1398         pmevcntr_op_start(env, i);
1399     }
1400 }
1401 
1402 void pmu_op_finish(CPUARMState *env)
1403 {
1404     unsigned int i;
1405     pmccntr_op_finish(env);
1406     for (i = 0; i < pmu_num_counters(env); i++) {
1407         pmevcntr_op_finish(env, i);
1408     }
1409 }
1410 
1411 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1412 {
1413     pmu_op_start(&cpu->env);
1414 }
1415 
1416 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1417 {
1418     pmu_op_finish(&cpu->env);
1419 }
1420 
1421 void arm_pmu_timer_cb(void *opaque)
1422 {
1423     ARMCPU *cpu = opaque;
1424 
1425     /*
1426      * Update all the counter values based on the current underlying counts,
1427      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1428      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1429      * counter may expire.
1430      */
1431     pmu_op_start(&cpu->env);
1432     pmu_op_finish(&cpu->env);
1433 }
1434 
1435 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1436                        uint64_t value)
1437 {
1438     pmu_op_start(env);
1439 
1440     if (value & PMCRC) {
1441         /* The counter has been reset */
1442         env->cp15.c15_ccnt = 0;
1443     }
1444 
1445     if (value & PMCRP) {
1446         unsigned int i;
1447         for (i = 0; i < pmu_num_counters(env); i++) {
1448             env->cp15.c14_pmevcntr[i] = 0;
1449         }
1450     }
1451 
1452     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1453     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1454 
1455     pmu_op_finish(env);
1456 }
1457 
1458 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1459                           uint64_t value)
1460 {
1461     unsigned int i;
1462     uint64_t overflow_mask, new_pmswinc;
1463 
1464     for (i = 0; i < pmu_num_counters(env); i++) {
1465         /* Increment a counter's count iff: */
1466         if ((value & (1 << i)) && /* counter's bit is set */
1467                 /* counter is enabled and not filtered */
1468                 pmu_counter_enabled(env, i) &&
1469                 /* counter is SW_INCR */
1470                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1471             pmevcntr_op_start(env, i);
1472 
1473             /*
1474              * Detect if this write causes an overflow since we can't predict
1475              * PMSWINC overflows like we can for other events
1476              */
1477             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1478 
1479             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1480                 1ULL << 63 : 1ULL << 31;
1481 
1482             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1483                 env->cp15.c9_pmovsr |= (1 << i);
1484                 pmu_update_irq(env);
1485             }
1486 
1487             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1488 
1489             pmevcntr_op_finish(env, i);
1490         }
1491     }
1492 }
1493 
1494 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1495 {
1496     uint64_t ret;
1497     pmccntr_op_start(env);
1498     ret = env->cp15.c15_ccnt;
1499     pmccntr_op_finish(env);
1500     return ret;
1501 }
1502 
1503 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                          uint64_t value)
1505 {
1506     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1507      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1508      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1509      * accessed.
1510      */
1511     env->cp15.c9_pmselr = value & 0x1f;
1512 }
1513 
1514 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1515                         uint64_t value)
1516 {
1517     pmccntr_op_start(env);
1518     env->cp15.c15_ccnt = value;
1519     pmccntr_op_finish(env);
1520 }
1521 
1522 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1523                             uint64_t value)
1524 {
1525     uint64_t cur_val = pmccntr_read(env, NULL);
1526 
1527     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1528 }
1529 
1530 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531                             uint64_t value)
1532 {
1533     pmccntr_op_start(env);
1534     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1535     pmccntr_op_finish(env);
1536 }
1537 
1538 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1539                             uint64_t value)
1540 {
1541     pmccntr_op_start(env);
1542     /* M is not accessible from AArch32 */
1543     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1544         (value & PMCCFILTR);
1545     pmccntr_op_finish(env);
1546 }
1547 
1548 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1549 {
1550     /* M is not visible in AArch32 */
1551     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1552 }
1553 
1554 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1555                             uint64_t value)
1556 {
1557     pmu_op_start(env);
1558     value &= pmu_counter_mask(env);
1559     env->cp15.c9_pmcnten |= value;
1560     pmu_op_finish(env);
1561 }
1562 
1563 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1564                              uint64_t value)
1565 {
1566     pmu_op_start(env);
1567     value &= pmu_counter_mask(env);
1568     env->cp15.c9_pmcnten &= ~value;
1569     pmu_op_finish(env);
1570 }
1571 
1572 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1573                          uint64_t value)
1574 {
1575     value &= pmu_counter_mask(env);
1576     env->cp15.c9_pmovsr &= ~value;
1577     pmu_update_irq(env);
1578 }
1579 
1580 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1581                          uint64_t value)
1582 {
1583     value &= pmu_counter_mask(env);
1584     env->cp15.c9_pmovsr |= value;
1585     pmu_update_irq(env);
1586 }
1587 
1588 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1589                              uint64_t value, const uint8_t counter)
1590 {
1591     if (counter == 31) {
1592         pmccfiltr_write(env, ri, value);
1593     } else if (counter < pmu_num_counters(env)) {
1594         pmevcntr_op_start(env, counter);
1595 
1596         /*
1597          * If this counter's event type is changing, store the current
1598          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1599          * pmevcntr_op_finish has the correct baseline when it converts back to
1600          * a delta.
1601          */
1602         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1603             PMXEVTYPER_EVTCOUNT;
1604         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1605         if (old_event != new_event) {
1606             uint64_t count = 0;
1607             if (event_supported(new_event)) {
1608                 uint16_t event_idx = supported_event_map[new_event];
1609                 count = pm_events[event_idx].get_count(env);
1610             }
1611             env->cp15.c14_pmevcntr_delta[counter] = count;
1612         }
1613 
1614         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1615         pmevcntr_op_finish(env, counter);
1616     }
1617     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1618      * PMSELR value is equal to or greater than the number of implemented
1619      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1620      */
1621 }
1622 
1623 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1624                                const uint8_t counter)
1625 {
1626     if (counter == 31) {
1627         return env->cp15.pmccfiltr_el0;
1628     } else if (counter < pmu_num_counters(env)) {
1629         return env->cp15.c14_pmevtyper[counter];
1630     } else {
1631       /*
1632        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1633        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1634        */
1635         return 0;
1636     }
1637 }
1638 
1639 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1640                               uint64_t value)
1641 {
1642     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1643     pmevtyper_write(env, ri, value, counter);
1644 }
1645 
1646 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1647                                uint64_t value)
1648 {
1649     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1650     env->cp15.c14_pmevtyper[counter] = value;
1651 
1652     /*
1653      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1654      * pmu_op_finish calls when loading saved state for a migration. Because
1655      * we're potentially updating the type of event here, the value written to
1656      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1657      * different counter type. Therefore, we need to set this value to the
1658      * current count for the counter type we're writing so that pmu_op_finish
1659      * has the correct count for its calculation.
1660      */
1661     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1662     if (event_supported(event)) {
1663         uint16_t event_idx = supported_event_map[event];
1664         env->cp15.c14_pmevcntr_delta[counter] =
1665             pm_events[event_idx].get_count(env);
1666     }
1667 }
1668 
1669 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1670 {
1671     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1672     return pmevtyper_read(env, ri, counter);
1673 }
1674 
1675 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1676                              uint64_t value)
1677 {
1678     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1679 }
1680 
1681 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1682 {
1683     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1684 }
1685 
1686 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1687                              uint64_t value, uint8_t counter)
1688 {
1689     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1690         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1691         value &= MAKE_64BIT_MASK(0, 32);
1692     }
1693     if (counter < pmu_num_counters(env)) {
1694         pmevcntr_op_start(env, counter);
1695         env->cp15.c14_pmevcntr[counter] = value;
1696         pmevcntr_op_finish(env, counter);
1697     }
1698     /*
1699      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1700      * are CONSTRAINED UNPREDICTABLE.
1701      */
1702 }
1703 
1704 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1705                               uint8_t counter)
1706 {
1707     if (counter < pmu_num_counters(env)) {
1708         uint64_t ret;
1709         pmevcntr_op_start(env, counter);
1710         ret = env->cp15.c14_pmevcntr[counter];
1711         pmevcntr_op_finish(env, counter);
1712         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1713             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1714             ret &= MAKE_64BIT_MASK(0, 32);
1715         }
1716         return ret;
1717     } else {
1718       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1719        * are CONSTRAINED UNPREDICTABLE. */
1720         return 0;
1721     }
1722 }
1723 
1724 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1725                              uint64_t value)
1726 {
1727     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1728     pmevcntr_write(env, ri, value, counter);
1729 }
1730 
1731 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1732 {
1733     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1734     return pmevcntr_read(env, ri, counter);
1735 }
1736 
1737 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1738                              uint64_t value)
1739 {
1740     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1741     assert(counter < pmu_num_counters(env));
1742     env->cp15.c14_pmevcntr[counter] = value;
1743     pmevcntr_write(env, ri, value, counter);
1744 }
1745 
1746 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1747 {
1748     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749     assert(counter < pmu_num_counters(env));
1750     return env->cp15.c14_pmevcntr[counter];
1751 }
1752 
1753 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1754                              uint64_t value)
1755 {
1756     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1757 }
1758 
1759 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1760 {
1761     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1762 }
1763 
1764 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1765                             uint64_t value)
1766 {
1767     if (arm_feature(env, ARM_FEATURE_V8)) {
1768         env->cp15.c9_pmuserenr = value & 0xf;
1769     } else {
1770         env->cp15.c9_pmuserenr = value & 1;
1771     }
1772 }
1773 
1774 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1775                              uint64_t value)
1776 {
1777     /* We have no event counters so only the C bit can be changed */
1778     value &= pmu_counter_mask(env);
1779     env->cp15.c9_pminten |= value;
1780     pmu_update_irq(env);
1781 }
1782 
1783 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1784                              uint64_t value)
1785 {
1786     value &= pmu_counter_mask(env);
1787     env->cp15.c9_pminten &= ~value;
1788     pmu_update_irq(env);
1789 }
1790 
1791 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1792                        uint64_t value)
1793 {
1794     /* Note that even though the AArch64 view of this register has bits
1795      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1796      * architectural requirements for bits which are RES0 only in some
1797      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1798      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1799      */
1800     raw_write(env, ri, value & ~0x1FULL);
1801 }
1802 
1803 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1804 {
1805     /* Begin with base v8.0 state.  */
1806     uint64_t valid_mask = 0x3fff;
1807     ARMCPU *cpu = env_archcpu(env);
1808     uint64_t changed;
1809 
1810     /*
1811      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1812      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1813      * Instead, choose the format based on the mode of EL3.
1814      */
1815     if (arm_el_is_aa64(env, 3)) {
1816         value |= SCR_FW | SCR_AW;      /* RES1 */
1817         valid_mask &= ~SCR_NET;        /* RES0 */
1818 
1819         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1820             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1821             value |= SCR_RW;           /* RAO/WI */
1822         }
1823         if (cpu_isar_feature(aa64_ras, cpu)) {
1824             valid_mask |= SCR_TERR;
1825         }
1826         if (cpu_isar_feature(aa64_lor, cpu)) {
1827             valid_mask |= SCR_TLOR;
1828         }
1829         if (cpu_isar_feature(aa64_pauth, cpu)) {
1830             valid_mask |= SCR_API | SCR_APK;
1831         }
1832         if (cpu_isar_feature(aa64_sel2, cpu)) {
1833             valid_mask |= SCR_EEL2;
1834         }
1835         if (cpu_isar_feature(aa64_mte, cpu)) {
1836             valid_mask |= SCR_ATA;
1837         }
1838         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1839             valid_mask |= SCR_ENSCXT;
1840         }
1841         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1842             valid_mask |= SCR_EASE | SCR_NMEA;
1843         }
1844         if (cpu_isar_feature(aa64_sme, cpu)) {
1845             valid_mask |= SCR_ENTP2;
1846         }
1847     } else {
1848         valid_mask &= ~(SCR_RW | SCR_ST);
1849         if (cpu_isar_feature(aa32_ras, cpu)) {
1850             valid_mask |= SCR_TERR;
1851         }
1852     }
1853 
1854     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1855         valid_mask &= ~SCR_HCE;
1856 
1857         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1858          * supported if EL2 exists. The bit is UNK/SBZP when
1859          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1860          * when EL2 is unavailable.
1861          * On ARMv8, this bit is always available.
1862          */
1863         if (arm_feature(env, ARM_FEATURE_V7) &&
1864             !arm_feature(env, ARM_FEATURE_V8)) {
1865             valid_mask &= ~SCR_SMD;
1866         }
1867     }
1868 
1869     /* Clear all-context RES0 bits.  */
1870     value &= valid_mask;
1871     changed = env->cp15.scr_el3 ^ value;
1872     env->cp15.scr_el3 = value;
1873 
1874     /*
1875      * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1876      * we must invalidate all TLBs below EL3.
1877      */
1878     if (changed & SCR_NS) {
1879         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1880                                            ARMMMUIdxBit_E20_0 |
1881                                            ARMMMUIdxBit_E10_1 |
1882                                            ARMMMUIdxBit_E20_2 |
1883                                            ARMMMUIdxBit_E10_1_PAN |
1884                                            ARMMMUIdxBit_E20_2_PAN |
1885                                            ARMMMUIdxBit_E2));
1886     }
1887 }
1888 
1889 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1890 {
1891     /*
1892      * scr_write will set the RES1 bits on an AArch64-only CPU.
1893      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1894      */
1895     scr_write(env, ri, 0);
1896 }
1897 
1898 static CPAccessResult access_tid4(CPUARMState *env,
1899                                   const ARMCPRegInfo *ri,
1900                                   bool isread)
1901 {
1902     if (arm_current_el(env) == 1 &&
1903         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1904         return CP_ACCESS_TRAP_EL2;
1905     }
1906 
1907     return CP_ACCESS_OK;
1908 }
1909 
1910 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1911 {
1912     ARMCPU *cpu = env_archcpu(env);
1913 
1914     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1915      * bank
1916      */
1917     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1918                                         ri->secure & ARM_CP_SECSTATE_S);
1919 
1920     return cpu->ccsidr[index];
1921 }
1922 
1923 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1924                          uint64_t value)
1925 {
1926     raw_write(env, ri, value & 0xf);
1927 }
1928 
1929 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1930 {
1931     CPUState *cs = env_cpu(env);
1932     bool el1 = arm_current_el(env) == 1;
1933     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1934     uint64_t ret = 0;
1935 
1936     if (hcr_el2 & HCR_IMO) {
1937         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1938             ret |= CPSR_I;
1939         }
1940     } else {
1941         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1942             ret |= CPSR_I;
1943         }
1944     }
1945 
1946     if (hcr_el2 & HCR_FMO) {
1947         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1948             ret |= CPSR_F;
1949         }
1950     } else {
1951         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1952             ret |= CPSR_F;
1953         }
1954     }
1955 
1956     if (hcr_el2 & HCR_AMO) {
1957         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1958             ret |= CPSR_A;
1959         }
1960     }
1961 
1962     return ret;
1963 }
1964 
1965 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1966                                        bool isread)
1967 {
1968     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1969         return CP_ACCESS_TRAP_EL2;
1970     }
1971 
1972     return CP_ACCESS_OK;
1973 }
1974 
1975 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1976                                        bool isread)
1977 {
1978     if (arm_feature(env, ARM_FEATURE_V8)) {
1979         return access_aa64_tid1(env, ri, isread);
1980     }
1981 
1982     return CP_ACCESS_OK;
1983 }
1984 
1985 static const ARMCPRegInfo v7_cp_reginfo[] = {
1986     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1987     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1988       .access = PL1_W, .type = ARM_CP_NOP },
1989     /* Performance monitors are implementation defined in v7,
1990      * but with an ARM recommended set of registers, which we
1991      * follow.
1992      *
1993      * Performance registers fall into three categories:
1994      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1995      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1996      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1997      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1998      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1999      */
2000     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2001       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2002       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2003       .writefn = pmcntenset_write,
2004       .accessfn = pmreg_access,
2005       .raw_writefn = raw_write },
2006     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2007       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2008       .access = PL0_RW, .accessfn = pmreg_access,
2009       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2010       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2011     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2012       .access = PL0_RW,
2013       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2014       .accessfn = pmreg_access,
2015       .writefn = pmcntenclr_write,
2016       .type = ARM_CP_ALIAS | ARM_CP_IO },
2017     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2018       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2019       .access = PL0_RW, .accessfn = pmreg_access,
2020       .type = ARM_CP_ALIAS | ARM_CP_IO,
2021       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2022       .writefn = pmcntenclr_write },
2023     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2024       .access = PL0_RW, .type = ARM_CP_IO,
2025       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2026       .accessfn = pmreg_access,
2027       .writefn = pmovsr_write,
2028       .raw_writefn = raw_write },
2029     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2030       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2031       .access = PL0_RW, .accessfn = pmreg_access,
2032       .type = ARM_CP_ALIAS | ARM_CP_IO,
2033       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2034       .writefn = pmovsr_write,
2035       .raw_writefn = raw_write },
2036     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2037       .access = PL0_W, .accessfn = pmreg_access_swinc,
2038       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2039       .writefn = pmswinc_write },
2040     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2041       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2042       .access = PL0_W, .accessfn = pmreg_access_swinc,
2043       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2044       .writefn = pmswinc_write },
2045     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2046       .access = PL0_RW, .type = ARM_CP_ALIAS,
2047       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2048       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2049       .raw_writefn = raw_write},
2050     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2051       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2052       .access = PL0_RW, .accessfn = pmreg_access_selr,
2053       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2054       .writefn = pmselr_write, .raw_writefn = raw_write, },
2055     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2056       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2057       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2058       .accessfn = pmreg_access_ccntr },
2059     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2060       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2061       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2062       .type = ARM_CP_IO,
2063       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2064       .readfn = pmccntr_read, .writefn = pmccntr_write,
2065       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2066     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2067       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2068       .access = PL0_RW, .accessfn = pmreg_access,
2069       .type = ARM_CP_ALIAS | ARM_CP_IO,
2070       .resetvalue = 0, },
2071     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2072       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2073       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2074       .access = PL0_RW, .accessfn = pmreg_access,
2075       .type = ARM_CP_IO,
2076       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2077       .resetvalue = 0, },
2078     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2079       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2080       .accessfn = pmreg_access,
2081       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2082     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2083       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2084       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2085       .accessfn = pmreg_access,
2086       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2087     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2088       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2089       .accessfn = pmreg_access_xevcntr,
2090       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2091     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2092       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2093       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2094       .accessfn = pmreg_access_xevcntr,
2095       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2096     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2097       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2098       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2099       .resetvalue = 0,
2100       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2101     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2102       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2103       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2104       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2105       .resetvalue = 0,
2106       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2107     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2108       .access = PL1_RW, .accessfn = access_tpm,
2109       .type = ARM_CP_ALIAS | ARM_CP_IO,
2110       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2111       .resetvalue = 0,
2112       .writefn = pmintenset_write, .raw_writefn = raw_write },
2113     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2114       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2115       .access = PL1_RW, .accessfn = access_tpm,
2116       .type = ARM_CP_IO,
2117       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2118       .writefn = pmintenset_write, .raw_writefn = raw_write,
2119       .resetvalue = 0x0 },
2120     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2121       .access = PL1_RW, .accessfn = access_tpm,
2122       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2123       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2124       .writefn = pmintenclr_write, },
2125     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2126       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2127       .access = PL1_RW, .accessfn = access_tpm,
2128       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2129       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2130       .writefn = pmintenclr_write },
2131     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2132       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2133       .access = PL1_R,
2134       .accessfn = access_tid4,
2135       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2136     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2137       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2138       .access = PL1_RW,
2139       .accessfn = access_tid4,
2140       .writefn = csselr_write, .resetvalue = 0,
2141       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2142                              offsetof(CPUARMState, cp15.csselr_ns) } },
2143     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2144      * just RAZ for all cores:
2145      */
2146     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2147       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2148       .access = PL1_R, .type = ARM_CP_CONST,
2149       .accessfn = access_aa64_tid1,
2150       .resetvalue = 0 },
2151     /* Auxiliary fault status registers: these also are IMPDEF, and we
2152      * choose to RAZ/WI for all cores.
2153      */
2154     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2155       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2156       .access = PL1_RW, .accessfn = access_tvm_trvm,
2157       .type = ARM_CP_CONST, .resetvalue = 0 },
2158     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2159       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2160       .access = PL1_RW, .accessfn = access_tvm_trvm,
2161       .type = ARM_CP_CONST, .resetvalue = 0 },
2162     /* MAIR can just read-as-written because we don't implement caches
2163      * and so don't need to care about memory attributes.
2164      */
2165     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2166       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2167       .access = PL1_RW, .accessfn = access_tvm_trvm,
2168       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2169       .resetvalue = 0 },
2170     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2171       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2172       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2173       .resetvalue = 0 },
2174     /* For non-long-descriptor page tables these are PRRR and NMRR;
2175      * regardless they still act as reads-as-written for QEMU.
2176      */
2177      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2178       * allows them to assign the correct fieldoffset based on the endianness
2179       * handled in the field definitions.
2180       */
2181     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2182       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2183       .access = PL1_RW, .accessfn = access_tvm_trvm,
2184       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2185                              offsetof(CPUARMState, cp15.mair0_ns) },
2186       .resetfn = arm_cp_reset_ignore },
2187     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2188       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2189       .access = PL1_RW, .accessfn = access_tvm_trvm,
2190       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2191                              offsetof(CPUARMState, cp15.mair1_ns) },
2192       .resetfn = arm_cp_reset_ignore },
2193     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2194       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2195       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2196     /* 32 bit ITLB invalidates */
2197     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2198       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2199       .writefn = tlbiall_write },
2200     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2201       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2202       .writefn = tlbimva_write },
2203     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2204       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2205       .writefn = tlbiasid_write },
2206     /* 32 bit DTLB invalidates */
2207     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2208       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2209       .writefn = tlbiall_write },
2210     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2211       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2212       .writefn = tlbimva_write },
2213     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2214       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2215       .writefn = tlbiasid_write },
2216     /* 32 bit TLB invalidates */
2217     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2218       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2219       .writefn = tlbiall_write },
2220     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2221       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2222       .writefn = tlbimva_write },
2223     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2224       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2225       .writefn = tlbiasid_write },
2226     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2227       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2228       .writefn = tlbimvaa_write },
2229 };
2230 
2231 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2232     /* 32 bit TLB invalidates, Inner Shareable */
2233     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2234       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2235       .writefn = tlbiall_is_write },
2236     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2237       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2238       .writefn = tlbimva_is_write },
2239     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2240       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2241       .writefn = tlbiasid_is_write },
2242     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2243       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2244       .writefn = tlbimvaa_is_write },
2245 };
2246 
2247 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2248     /* PMOVSSET is not implemented in v7 before v7ve */
2249     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2250       .access = PL0_RW, .accessfn = pmreg_access,
2251       .type = ARM_CP_ALIAS | ARM_CP_IO,
2252       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2253       .writefn = pmovsset_write,
2254       .raw_writefn = raw_write },
2255     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2256       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2257       .access = PL0_RW, .accessfn = pmreg_access,
2258       .type = ARM_CP_ALIAS | ARM_CP_IO,
2259       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2260       .writefn = pmovsset_write,
2261       .raw_writefn = raw_write },
2262 };
2263 
2264 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2265                         uint64_t value)
2266 {
2267     value &= 1;
2268     env->teecr = value;
2269 }
2270 
2271 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2272                                    bool isread)
2273 {
2274     /*
2275      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2276      * at all, so we don't need to check whether we're v8A.
2277      */
2278     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2279         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2280         return CP_ACCESS_TRAP_EL2;
2281     }
2282     return CP_ACCESS_OK;
2283 }
2284 
2285 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2286                                     bool isread)
2287 {
2288     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2289         return CP_ACCESS_TRAP;
2290     }
2291     return teecr_access(env, ri, isread);
2292 }
2293 
2294 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2295     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2296       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2297       .resetvalue = 0,
2298       .writefn = teecr_write, .accessfn = teecr_access },
2299     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2300       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2301       .accessfn = teehbr_access, .resetvalue = 0 },
2302 };
2303 
2304 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2305     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2306       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2307       .access = PL0_RW,
2308       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2309     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2310       .access = PL0_RW,
2311       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2312                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2313       .resetfn = arm_cp_reset_ignore },
2314     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2315       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2316       .access = PL0_R|PL1_W,
2317       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2318       .resetvalue = 0},
2319     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2320       .access = PL0_R|PL1_W,
2321       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2322                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2323       .resetfn = arm_cp_reset_ignore },
2324     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2325       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2326       .access = PL1_RW,
2327       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2328     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2329       .access = PL1_RW,
2330       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2331                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2332       .resetvalue = 0 },
2333 };
2334 
2335 #ifndef CONFIG_USER_ONLY
2336 
2337 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2338                                        bool isread)
2339 {
2340     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2341      * Writable only at the highest implemented exception level.
2342      */
2343     int el = arm_current_el(env);
2344     uint64_t hcr;
2345     uint32_t cntkctl;
2346 
2347     switch (el) {
2348     case 0:
2349         hcr = arm_hcr_el2_eff(env);
2350         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2351             cntkctl = env->cp15.cnthctl_el2;
2352         } else {
2353             cntkctl = env->cp15.c14_cntkctl;
2354         }
2355         if (!extract32(cntkctl, 0, 2)) {
2356             return CP_ACCESS_TRAP;
2357         }
2358         break;
2359     case 1:
2360         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2361             arm_is_secure_below_el3(env)) {
2362             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2363             return CP_ACCESS_TRAP_UNCATEGORIZED;
2364         }
2365         break;
2366     case 2:
2367     case 3:
2368         break;
2369     }
2370 
2371     if (!isread && el < arm_highest_el(env)) {
2372         return CP_ACCESS_TRAP_UNCATEGORIZED;
2373     }
2374 
2375     return CP_ACCESS_OK;
2376 }
2377 
2378 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2379                                         bool isread)
2380 {
2381     unsigned int cur_el = arm_current_el(env);
2382     bool has_el2 = arm_is_el2_enabled(env);
2383     uint64_t hcr = arm_hcr_el2_eff(env);
2384 
2385     switch (cur_el) {
2386     case 0:
2387         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2388         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2389             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2390                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2391         }
2392 
2393         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2394         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2395             return CP_ACCESS_TRAP;
2396         }
2397 
2398         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2399         if (hcr & HCR_E2H) {
2400             if (timeridx == GTIMER_PHYS &&
2401                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2402                 return CP_ACCESS_TRAP_EL2;
2403             }
2404         } else {
2405             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2406             if (has_el2 && timeridx == GTIMER_PHYS &&
2407                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2408                 return CP_ACCESS_TRAP_EL2;
2409             }
2410         }
2411         break;
2412 
2413     case 1:
2414         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2415         if (has_el2 && timeridx == GTIMER_PHYS &&
2416             (hcr & HCR_E2H
2417              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2418              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2419             return CP_ACCESS_TRAP_EL2;
2420         }
2421         break;
2422     }
2423     return CP_ACCESS_OK;
2424 }
2425 
2426 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2427                                       bool isread)
2428 {
2429     unsigned int cur_el = arm_current_el(env);
2430     bool has_el2 = arm_is_el2_enabled(env);
2431     uint64_t hcr = arm_hcr_el2_eff(env);
2432 
2433     switch (cur_el) {
2434     case 0:
2435         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2436             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2437             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2438                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2439         }
2440 
2441         /*
2442          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2443          * EL0 if EL0[PV]TEN is zero.
2444          */
2445         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2446             return CP_ACCESS_TRAP;
2447         }
2448         /* fall through */
2449 
2450     case 1:
2451         if (has_el2 && timeridx == GTIMER_PHYS) {
2452             if (hcr & HCR_E2H) {
2453                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2454                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2455                     return CP_ACCESS_TRAP_EL2;
2456                 }
2457             } else {
2458                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2459                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2460                     return CP_ACCESS_TRAP_EL2;
2461                 }
2462             }
2463         }
2464         break;
2465     }
2466     return CP_ACCESS_OK;
2467 }
2468 
2469 static CPAccessResult gt_pct_access(CPUARMState *env,
2470                                     const ARMCPRegInfo *ri,
2471                                     bool isread)
2472 {
2473     return gt_counter_access(env, GTIMER_PHYS, isread);
2474 }
2475 
2476 static CPAccessResult gt_vct_access(CPUARMState *env,
2477                                     const ARMCPRegInfo *ri,
2478                                     bool isread)
2479 {
2480     return gt_counter_access(env, GTIMER_VIRT, isread);
2481 }
2482 
2483 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2484                                        bool isread)
2485 {
2486     return gt_timer_access(env, GTIMER_PHYS, isread);
2487 }
2488 
2489 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2490                                        bool isread)
2491 {
2492     return gt_timer_access(env, GTIMER_VIRT, isread);
2493 }
2494 
2495 static CPAccessResult gt_stimer_access(CPUARMState *env,
2496                                        const ARMCPRegInfo *ri,
2497                                        bool isread)
2498 {
2499     /* The AArch64 register view of the secure physical timer is
2500      * always accessible from EL3, and configurably accessible from
2501      * Secure EL1.
2502      */
2503     switch (arm_current_el(env)) {
2504     case 1:
2505         if (!arm_is_secure(env)) {
2506             return CP_ACCESS_TRAP;
2507         }
2508         if (!(env->cp15.scr_el3 & SCR_ST)) {
2509             return CP_ACCESS_TRAP_EL3;
2510         }
2511         return CP_ACCESS_OK;
2512     case 0:
2513     case 2:
2514         return CP_ACCESS_TRAP;
2515     case 3:
2516         return CP_ACCESS_OK;
2517     default:
2518         g_assert_not_reached();
2519     }
2520 }
2521 
2522 static uint64_t gt_get_countervalue(CPUARMState *env)
2523 {
2524     ARMCPU *cpu = env_archcpu(env);
2525 
2526     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2527 }
2528 
2529 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2530 {
2531     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2532 
2533     if (gt->ctl & 1) {
2534         /* Timer enabled: calculate and set current ISTATUS, irq, and
2535          * reset timer to when ISTATUS next has to change
2536          */
2537         uint64_t offset = timeridx == GTIMER_VIRT ?
2538                                       cpu->env.cp15.cntvoff_el2 : 0;
2539         uint64_t count = gt_get_countervalue(&cpu->env);
2540         /* Note that this must be unsigned 64 bit arithmetic: */
2541         int istatus = count - offset >= gt->cval;
2542         uint64_t nexttick;
2543         int irqstate;
2544 
2545         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2546 
2547         irqstate = (istatus && !(gt->ctl & 2));
2548         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2549 
2550         if (istatus) {
2551             /* Next transition is when count rolls back over to zero */
2552             nexttick = UINT64_MAX;
2553         } else {
2554             /* Next transition is when we hit cval */
2555             nexttick = gt->cval + offset;
2556         }
2557         /* Note that the desired next expiry time might be beyond the
2558          * signed-64-bit range of a QEMUTimer -- in this case we just
2559          * set the timer for as far in the future as possible. When the
2560          * timer expires we will reset the timer for any remaining period.
2561          */
2562         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2563             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2564         } else {
2565             timer_mod(cpu->gt_timer[timeridx], nexttick);
2566         }
2567         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2568     } else {
2569         /* Timer disabled: ISTATUS and timer output always clear */
2570         gt->ctl &= ~4;
2571         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2572         timer_del(cpu->gt_timer[timeridx]);
2573         trace_arm_gt_recalc_disabled(timeridx);
2574     }
2575 }
2576 
2577 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2578                            int timeridx)
2579 {
2580     ARMCPU *cpu = env_archcpu(env);
2581 
2582     timer_del(cpu->gt_timer[timeridx]);
2583 }
2584 
2585 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2586 {
2587     return gt_get_countervalue(env);
2588 }
2589 
2590 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2591 {
2592     uint64_t hcr;
2593 
2594     switch (arm_current_el(env)) {
2595     case 2:
2596         hcr = arm_hcr_el2_eff(env);
2597         if (hcr & HCR_E2H) {
2598             return 0;
2599         }
2600         break;
2601     case 0:
2602         hcr = arm_hcr_el2_eff(env);
2603         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2604             return 0;
2605         }
2606         break;
2607     }
2608 
2609     return env->cp15.cntvoff_el2;
2610 }
2611 
2612 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2613 {
2614     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2615 }
2616 
2617 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2618                           int timeridx,
2619                           uint64_t value)
2620 {
2621     trace_arm_gt_cval_write(timeridx, value);
2622     env->cp15.c14_timer[timeridx].cval = value;
2623     gt_recalc_timer(env_archcpu(env), timeridx);
2624 }
2625 
2626 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2627                              int timeridx)
2628 {
2629     uint64_t offset = 0;
2630 
2631     switch (timeridx) {
2632     case GTIMER_VIRT:
2633     case GTIMER_HYPVIRT:
2634         offset = gt_virt_cnt_offset(env);
2635         break;
2636     }
2637 
2638     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2639                       (gt_get_countervalue(env) - offset));
2640 }
2641 
2642 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2643                           int timeridx,
2644                           uint64_t value)
2645 {
2646     uint64_t offset = 0;
2647 
2648     switch (timeridx) {
2649     case GTIMER_VIRT:
2650     case GTIMER_HYPVIRT:
2651         offset = gt_virt_cnt_offset(env);
2652         break;
2653     }
2654 
2655     trace_arm_gt_tval_write(timeridx, value);
2656     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2657                                          sextract64(value, 0, 32);
2658     gt_recalc_timer(env_archcpu(env), timeridx);
2659 }
2660 
2661 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2662                          int timeridx,
2663                          uint64_t value)
2664 {
2665     ARMCPU *cpu = env_archcpu(env);
2666     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2667 
2668     trace_arm_gt_ctl_write(timeridx, value);
2669     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2670     if ((oldval ^ value) & 1) {
2671         /* Enable toggled */
2672         gt_recalc_timer(cpu, timeridx);
2673     } else if ((oldval ^ value) & 2) {
2674         /* IMASK toggled: don't need to recalculate,
2675          * just set the interrupt line based on ISTATUS
2676          */
2677         int irqstate = (oldval & 4) && !(value & 2);
2678 
2679         trace_arm_gt_imask_toggle(timeridx, irqstate);
2680         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2681     }
2682 }
2683 
2684 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2685 {
2686     gt_timer_reset(env, ri, GTIMER_PHYS);
2687 }
2688 
2689 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690                                uint64_t value)
2691 {
2692     gt_cval_write(env, ri, GTIMER_PHYS, value);
2693 }
2694 
2695 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2696 {
2697     return gt_tval_read(env, ri, GTIMER_PHYS);
2698 }
2699 
2700 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2701                                uint64_t value)
2702 {
2703     gt_tval_write(env, ri, GTIMER_PHYS, value);
2704 }
2705 
2706 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2707                               uint64_t value)
2708 {
2709     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2710 }
2711 
2712 static int gt_phys_redir_timeridx(CPUARMState *env)
2713 {
2714     switch (arm_mmu_idx(env)) {
2715     case ARMMMUIdx_E20_0:
2716     case ARMMMUIdx_E20_2:
2717     case ARMMMUIdx_E20_2_PAN:
2718         return GTIMER_HYP;
2719     default:
2720         return GTIMER_PHYS;
2721     }
2722 }
2723 
2724 static int gt_virt_redir_timeridx(CPUARMState *env)
2725 {
2726     switch (arm_mmu_idx(env)) {
2727     case ARMMMUIdx_E20_0:
2728     case ARMMMUIdx_E20_2:
2729     case ARMMMUIdx_E20_2_PAN:
2730         return GTIMER_HYPVIRT;
2731     default:
2732         return GTIMER_VIRT;
2733     }
2734 }
2735 
2736 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2737                                         const ARMCPRegInfo *ri)
2738 {
2739     int timeridx = gt_phys_redir_timeridx(env);
2740     return env->cp15.c14_timer[timeridx].cval;
2741 }
2742 
2743 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744                                      uint64_t value)
2745 {
2746     int timeridx = gt_phys_redir_timeridx(env);
2747     gt_cval_write(env, ri, timeridx, value);
2748 }
2749 
2750 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2751                                         const ARMCPRegInfo *ri)
2752 {
2753     int timeridx = gt_phys_redir_timeridx(env);
2754     return gt_tval_read(env, ri, timeridx);
2755 }
2756 
2757 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2758                                      uint64_t value)
2759 {
2760     int timeridx = gt_phys_redir_timeridx(env);
2761     gt_tval_write(env, ri, timeridx, value);
2762 }
2763 
2764 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2765                                        const ARMCPRegInfo *ri)
2766 {
2767     int timeridx = gt_phys_redir_timeridx(env);
2768     return env->cp15.c14_timer[timeridx].ctl;
2769 }
2770 
2771 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2772                                     uint64_t value)
2773 {
2774     int timeridx = gt_phys_redir_timeridx(env);
2775     gt_ctl_write(env, ri, timeridx, value);
2776 }
2777 
2778 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2779 {
2780     gt_timer_reset(env, ri, GTIMER_VIRT);
2781 }
2782 
2783 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2784                                uint64_t value)
2785 {
2786     gt_cval_write(env, ri, GTIMER_VIRT, value);
2787 }
2788 
2789 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2790 {
2791     return gt_tval_read(env, ri, GTIMER_VIRT);
2792 }
2793 
2794 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2795                                uint64_t value)
2796 {
2797     gt_tval_write(env, ri, GTIMER_VIRT, value);
2798 }
2799 
2800 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2801                               uint64_t value)
2802 {
2803     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2804 }
2805 
2806 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2807                               uint64_t value)
2808 {
2809     ARMCPU *cpu = env_archcpu(env);
2810 
2811     trace_arm_gt_cntvoff_write(value);
2812     raw_write(env, ri, value);
2813     gt_recalc_timer(cpu, GTIMER_VIRT);
2814 }
2815 
2816 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2817                                         const ARMCPRegInfo *ri)
2818 {
2819     int timeridx = gt_virt_redir_timeridx(env);
2820     return env->cp15.c14_timer[timeridx].cval;
2821 }
2822 
2823 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2824                                      uint64_t value)
2825 {
2826     int timeridx = gt_virt_redir_timeridx(env);
2827     gt_cval_write(env, ri, timeridx, value);
2828 }
2829 
2830 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2831                                         const ARMCPRegInfo *ri)
2832 {
2833     int timeridx = gt_virt_redir_timeridx(env);
2834     return gt_tval_read(env, ri, timeridx);
2835 }
2836 
2837 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                                      uint64_t value)
2839 {
2840     int timeridx = gt_virt_redir_timeridx(env);
2841     gt_tval_write(env, ri, timeridx, value);
2842 }
2843 
2844 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2845                                        const ARMCPRegInfo *ri)
2846 {
2847     int timeridx = gt_virt_redir_timeridx(env);
2848     return env->cp15.c14_timer[timeridx].ctl;
2849 }
2850 
2851 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2852                                     uint64_t value)
2853 {
2854     int timeridx = gt_virt_redir_timeridx(env);
2855     gt_ctl_write(env, ri, timeridx, value);
2856 }
2857 
2858 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2859 {
2860     gt_timer_reset(env, ri, GTIMER_HYP);
2861 }
2862 
2863 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2864                               uint64_t value)
2865 {
2866     gt_cval_write(env, ri, GTIMER_HYP, value);
2867 }
2868 
2869 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2870 {
2871     return gt_tval_read(env, ri, GTIMER_HYP);
2872 }
2873 
2874 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2875                               uint64_t value)
2876 {
2877     gt_tval_write(env, ri, GTIMER_HYP, value);
2878 }
2879 
2880 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2881                               uint64_t value)
2882 {
2883     gt_ctl_write(env, ri, GTIMER_HYP, value);
2884 }
2885 
2886 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2887 {
2888     gt_timer_reset(env, ri, GTIMER_SEC);
2889 }
2890 
2891 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2892                               uint64_t value)
2893 {
2894     gt_cval_write(env, ri, GTIMER_SEC, value);
2895 }
2896 
2897 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2898 {
2899     return gt_tval_read(env, ri, GTIMER_SEC);
2900 }
2901 
2902 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2903                               uint64_t value)
2904 {
2905     gt_tval_write(env, ri, GTIMER_SEC, value);
2906 }
2907 
2908 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2909                               uint64_t value)
2910 {
2911     gt_ctl_write(env, ri, GTIMER_SEC, value);
2912 }
2913 
2914 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2915 {
2916     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2917 }
2918 
2919 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2920                              uint64_t value)
2921 {
2922     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2923 }
2924 
2925 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2926 {
2927     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2928 }
2929 
2930 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2931                              uint64_t value)
2932 {
2933     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2934 }
2935 
2936 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2937                             uint64_t value)
2938 {
2939     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2940 }
2941 
2942 void arm_gt_ptimer_cb(void *opaque)
2943 {
2944     ARMCPU *cpu = opaque;
2945 
2946     gt_recalc_timer(cpu, GTIMER_PHYS);
2947 }
2948 
2949 void arm_gt_vtimer_cb(void *opaque)
2950 {
2951     ARMCPU *cpu = opaque;
2952 
2953     gt_recalc_timer(cpu, GTIMER_VIRT);
2954 }
2955 
2956 void arm_gt_htimer_cb(void *opaque)
2957 {
2958     ARMCPU *cpu = opaque;
2959 
2960     gt_recalc_timer(cpu, GTIMER_HYP);
2961 }
2962 
2963 void arm_gt_stimer_cb(void *opaque)
2964 {
2965     ARMCPU *cpu = opaque;
2966 
2967     gt_recalc_timer(cpu, GTIMER_SEC);
2968 }
2969 
2970 void arm_gt_hvtimer_cb(void *opaque)
2971 {
2972     ARMCPU *cpu = opaque;
2973 
2974     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2975 }
2976 
2977 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2978 {
2979     ARMCPU *cpu = env_archcpu(env);
2980 
2981     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2982 }
2983 
2984 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2985     /* Note that CNTFRQ is purely reads-as-written for the benefit
2986      * of software; writing it doesn't actually change the timer frequency.
2987      * Our reset value matches the fixed frequency we implement the timer at.
2988      */
2989     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2990       .type = ARM_CP_ALIAS,
2991       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2992       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2993     },
2994     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2995       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2996       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2997       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2998       .resetfn = arm_gt_cntfrq_reset,
2999     },
3000     /* overall control: mostly access permissions */
3001     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3002       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3003       .access = PL1_RW,
3004       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3005       .resetvalue = 0,
3006     },
3007     /* per-timer control */
3008     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3009       .secure = ARM_CP_SECSTATE_NS,
3010       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3011       .accessfn = gt_ptimer_access,
3012       .fieldoffset = offsetoflow32(CPUARMState,
3013                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3014       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3015       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3016     },
3017     { .name = "CNTP_CTL_S",
3018       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3019       .secure = ARM_CP_SECSTATE_S,
3020       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3021       .accessfn = gt_ptimer_access,
3022       .fieldoffset = offsetoflow32(CPUARMState,
3023                                    cp15.c14_timer[GTIMER_SEC].ctl),
3024       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3025     },
3026     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3027       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3028       .type = ARM_CP_IO, .access = PL0_RW,
3029       .accessfn = gt_ptimer_access,
3030       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3031       .resetvalue = 0,
3032       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3033       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3034     },
3035     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3036       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3037       .accessfn = gt_vtimer_access,
3038       .fieldoffset = offsetoflow32(CPUARMState,
3039                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3040       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3041       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3042     },
3043     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3044       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3045       .type = ARM_CP_IO, .access = PL0_RW,
3046       .accessfn = gt_vtimer_access,
3047       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3048       .resetvalue = 0,
3049       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3050       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3051     },
3052     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3053     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3054       .secure = ARM_CP_SECSTATE_NS,
3055       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3056       .accessfn = gt_ptimer_access,
3057       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3058     },
3059     { .name = "CNTP_TVAL_S",
3060       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3061       .secure = ARM_CP_SECSTATE_S,
3062       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3063       .accessfn = gt_ptimer_access,
3064       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3065     },
3066     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3067       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3068       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3069       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3070       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3071     },
3072     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3073       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3074       .accessfn = gt_vtimer_access,
3075       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3076     },
3077     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3078       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3079       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3080       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3081       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3082     },
3083     /* The counter itself */
3084     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3085       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3086       .accessfn = gt_pct_access,
3087       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3088     },
3089     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3090       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3091       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3092       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3093     },
3094     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3095       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3096       .accessfn = gt_vct_access,
3097       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3098     },
3099     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3100       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3101       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3102       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3103     },
3104     /* Comparison value, indicating when the timer goes off */
3105     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3106       .secure = ARM_CP_SECSTATE_NS,
3107       .access = PL0_RW,
3108       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3109       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3110       .accessfn = gt_ptimer_access,
3111       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3112       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3113     },
3114     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3115       .secure = ARM_CP_SECSTATE_S,
3116       .access = PL0_RW,
3117       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3118       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3119       .accessfn = gt_ptimer_access,
3120       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3121     },
3122     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3123       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3124       .access = PL0_RW,
3125       .type = ARM_CP_IO,
3126       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3127       .resetvalue = 0, .accessfn = gt_ptimer_access,
3128       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3129       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3130     },
3131     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3132       .access = PL0_RW,
3133       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3134       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3135       .accessfn = gt_vtimer_access,
3136       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3137       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3138     },
3139     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3140       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3141       .access = PL0_RW,
3142       .type = ARM_CP_IO,
3143       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3144       .resetvalue = 0, .accessfn = gt_vtimer_access,
3145       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3146       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3147     },
3148     /* Secure timer -- this is actually restricted to only EL3
3149      * and configurably Secure-EL1 via the accessfn.
3150      */
3151     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3152       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3153       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3154       .accessfn = gt_stimer_access,
3155       .readfn = gt_sec_tval_read,
3156       .writefn = gt_sec_tval_write,
3157       .resetfn = gt_sec_timer_reset,
3158     },
3159     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3160       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3161       .type = ARM_CP_IO, .access = PL1_RW,
3162       .accessfn = gt_stimer_access,
3163       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3164       .resetvalue = 0,
3165       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3166     },
3167     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3168       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3169       .type = ARM_CP_IO, .access = PL1_RW,
3170       .accessfn = gt_stimer_access,
3171       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3172       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3173     },
3174 };
3175 
3176 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3177                                  bool isread)
3178 {
3179     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3180         return CP_ACCESS_TRAP;
3181     }
3182     return CP_ACCESS_OK;
3183 }
3184 
3185 #else
3186 
3187 /* In user-mode most of the generic timer registers are inaccessible
3188  * however modern kernels (4.12+) allow access to cntvct_el0
3189  */
3190 
3191 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3192 {
3193     ARMCPU *cpu = env_archcpu(env);
3194 
3195     /* Currently we have no support for QEMUTimer in linux-user so we
3196      * can't call gt_get_countervalue(env), instead we directly
3197      * call the lower level functions.
3198      */
3199     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3200 }
3201 
3202 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3203     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3204       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3205       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3206       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3207       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3208     },
3209     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3210       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3211       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3212       .readfn = gt_virt_cnt_read,
3213     },
3214 };
3215 
3216 #endif
3217 
3218 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3219 {
3220     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3221         raw_write(env, ri, value);
3222     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3223         raw_write(env, ri, value & 0xfffff6ff);
3224     } else {
3225         raw_write(env, ri, value & 0xfffff1ff);
3226     }
3227 }
3228 
3229 #ifndef CONFIG_USER_ONLY
3230 /* get_phys_addr() isn't present for user-mode-only targets */
3231 
3232 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3233                                  bool isread)
3234 {
3235     if (ri->opc2 & 4) {
3236         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3237          * Secure EL1 (which can only happen if EL3 is AArch64).
3238          * They are simply UNDEF if executed from NS EL1.
3239          * They function normally from EL2 or EL3.
3240          */
3241         if (arm_current_el(env) == 1) {
3242             if (arm_is_secure_below_el3(env)) {
3243                 if (env->cp15.scr_el3 & SCR_EEL2) {
3244                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3245                 }
3246                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3247             }
3248             return CP_ACCESS_TRAP_UNCATEGORIZED;
3249         }
3250     }
3251     return CP_ACCESS_OK;
3252 }
3253 
3254 #ifdef CONFIG_TCG
3255 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3256                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3257                              bool is_secure)
3258 {
3259     bool ret;
3260     uint64_t par64;
3261     bool format64 = false;
3262     ARMMMUFaultInfo fi = {};
3263     GetPhysAddrResult res = {};
3264 
3265     ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3266                                     is_secure, &res, &fi);
3267 
3268     /*
3269      * ATS operations only do S1 or S1+S2 translations, so we never
3270      * have to deal with the ARMCacheAttrs format for S2 only.
3271      */
3272     assert(!res.cacheattrs.is_s2_format);
3273 
3274     if (ret) {
3275         /*
3276          * Some kinds of translation fault must cause exceptions rather
3277          * than being reported in the PAR.
3278          */
3279         int current_el = arm_current_el(env);
3280         int target_el;
3281         uint32_t syn, fsr, fsc;
3282         bool take_exc = false;
3283 
3284         if (fi.s1ptw && current_el == 1
3285             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3286             /*
3287              * Synchronous stage 2 fault on an access made as part of the
3288              * translation table walk for AT S1E0* or AT S1E1* insn
3289              * executed from NS EL1. If this is a synchronous external abort
3290              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3291              * to EL3. Otherwise the fault is taken as an exception to EL2,
3292              * and HPFAR_EL2 holds the faulting IPA.
3293              */
3294             if (fi.type == ARMFault_SyncExternalOnWalk &&
3295                 (env->cp15.scr_el3 & SCR_EA)) {
3296                 target_el = 3;
3297             } else {
3298                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3299                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3300                     env->cp15.hpfar_el2 |= HPFAR_NS;
3301                 }
3302                 target_el = 2;
3303             }
3304             take_exc = true;
3305         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3306             /*
3307              * Synchronous external aborts during a translation table walk
3308              * are taken as Data Abort exceptions.
3309              */
3310             if (fi.stage2) {
3311                 if (current_el == 3) {
3312                     target_el = 3;
3313                 } else {
3314                     target_el = 2;
3315                 }
3316             } else {
3317                 target_el = exception_target_el(env);
3318             }
3319             take_exc = true;
3320         }
3321 
3322         if (take_exc) {
3323             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3324             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3325                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3326                 fsr = arm_fi_to_lfsc(&fi);
3327                 fsc = extract32(fsr, 0, 6);
3328             } else {
3329                 fsr = arm_fi_to_sfsc(&fi);
3330                 fsc = 0x3f;
3331             }
3332             /*
3333              * Report exception with ESR indicating a fault due to a
3334              * translation table walk for a cache maintenance instruction.
3335              */
3336             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3337                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3338             env->exception.vaddress = value;
3339             env->exception.fsr = fsr;
3340             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3341         }
3342     }
3343 
3344     if (is_a64(env)) {
3345         format64 = true;
3346     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3347         /*
3348          * ATS1Cxx:
3349          * * TTBCR.EAE determines whether the result is returned using the
3350          *   32-bit or the 64-bit PAR format
3351          * * Instructions executed in Hyp mode always use the 64bit format
3352          *
3353          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3354          * * The Non-secure TTBCR.EAE bit is set to 1
3355          * * The implementation includes EL2, and the value of HCR.VM is 1
3356          *
3357          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3358          *
3359          * ATS1Hx always uses the 64bit format.
3360          */
3361         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3362 
3363         if (arm_feature(env, ARM_FEATURE_EL2)) {
3364             if (mmu_idx == ARMMMUIdx_E10_0 ||
3365                 mmu_idx == ARMMMUIdx_E10_1 ||
3366                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3367                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3368             } else {
3369                 format64 |= arm_current_el(env) == 2;
3370             }
3371         }
3372     }
3373 
3374     if (format64) {
3375         /* Create a 64-bit PAR */
3376         par64 = (1 << 11); /* LPAE bit always set */
3377         if (!ret) {
3378             par64 |= res.f.phys_addr & ~0xfffULL;
3379             if (!res.f.attrs.secure) {
3380                 par64 |= (1 << 9); /* NS */
3381             }
3382             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3383             par64 |= res.cacheattrs.shareability << 7; /* SH */
3384         } else {
3385             uint32_t fsr = arm_fi_to_lfsc(&fi);
3386 
3387             par64 |= 1; /* F */
3388             par64 |= (fsr & 0x3f) << 1; /* FS */
3389             if (fi.stage2) {
3390                 par64 |= (1 << 9); /* S */
3391             }
3392             if (fi.s1ptw) {
3393                 par64 |= (1 << 8); /* PTW */
3394             }
3395         }
3396     } else {
3397         /* fsr is a DFSR/IFSR value for the short descriptor
3398          * translation table format (with WnR always clear).
3399          * Convert it to a 32-bit PAR.
3400          */
3401         if (!ret) {
3402             /* We do not set any attribute bits in the PAR */
3403             if (res.f.lg_page_size == 24
3404                 && arm_feature(env, ARM_FEATURE_V7)) {
3405                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3406             } else {
3407                 par64 = res.f.phys_addr & 0xfffff000;
3408             }
3409             if (!res.f.attrs.secure) {
3410                 par64 |= (1 << 9); /* NS */
3411             }
3412         } else {
3413             uint32_t fsr = arm_fi_to_sfsc(&fi);
3414 
3415             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3416                     ((fsr & 0xf) << 1) | 1;
3417         }
3418     }
3419     return par64;
3420 }
3421 #endif /* CONFIG_TCG */
3422 
3423 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3424 {
3425 #ifdef CONFIG_TCG
3426     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3427     uint64_t par64;
3428     ARMMMUIdx mmu_idx;
3429     int el = arm_current_el(env);
3430     bool secure = arm_is_secure_below_el3(env);
3431 
3432     switch (ri->opc2 & 6) {
3433     case 0:
3434         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3435         switch (el) {
3436         case 3:
3437             mmu_idx = ARMMMUIdx_E3;
3438             secure = true;
3439             break;
3440         case 2:
3441             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3442             /* fall through */
3443         case 1:
3444             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3445                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3446             } else {
3447                 mmu_idx = ARMMMUIdx_Stage1_E1;
3448             }
3449             break;
3450         default:
3451             g_assert_not_reached();
3452         }
3453         break;
3454     case 2:
3455         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3456         switch (el) {
3457         case 3:
3458             mmu_idx = ARMMMUIdx_E10_0;
3459             secure = true;
3460             break;
3461         case 2:
3462             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3463             mmu_idx = ARMMMUIdx_Stage1_E0;
3464             break;
3465         case 1:
3466             mmu_idx = ARMMMUIdx_Stage1_E0;
3467             break;
3468         default:
3469             g_assert_not_reached();
3470         }
3471         break;
3472     case 4:
3473         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3474         mmu_idx = ARMMMUIdx_E10_1;
3475         secure = false;
3476         break;
3477     case 6:
3478         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3479         mmu_idx = ARMMMUIdx_E10_0;
3480         secure = false;
3481         break;
3482     default:
3483         g_assert_not_reached();
3484     }
3485 
3486     par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
3487 
3488     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3489 #else
3490     /* Handled by hardware accelerator. */
3491     g_assert_not_reached();
3492 #endif /* CONFIG_TCG */
3493 }
3494 
3495 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3496                         uint64_t value)
3497 {
3498 #ifdef CONFIG_TCG
3499     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3500     uint64_t par64;
3501 
3502     /* There is no SecureEL2 for AArch32. */
3503     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
3504 
3505     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3506 #else
3507     /* Handled by hardware accelerator. */
3508     g_assert_not_reached();
3509 #endif /* CONFIG_TCG */
3510 }
3511 
3512 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3513                                      bool isread)
3514 {
3515     if (arm_current_el(env) == 3 &&
3516         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3517         return CP_ACCESS_TRAP;
3518     }
3519     return CP_ACCESS_OK;
3520 }
3521 
3522 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3523                         uint64_t value)
3524 {
3525 #ifdef CONFIG_TCG
3526     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3527     ARMMMUIdx mmu_idx;
3528     int secure = arm_is_secure_below_el3(env);
3529     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3530     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3531 
3532     switch (ri->opc2 & 6) {
3533     case 0:
3534         switch (ri->opc1) {
3535         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3536             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3537                 mmu_idx = regime_e20 ?
3538                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3539             } else {
3540                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3541             }
3542             break;
3543         case 4: /* AT S1E2R, AT S1E2W */
3544             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3545             break;
3546         case 6: /* AT S1E3R, AT S1E3W */
3547             mmu_idx = ARMMMUIdx_E3;
3548             secure = true;
3549             break;
3550         default:
3551             g_assert_not_reached();
3552         }
3553         break;
3554     case 2: /* AT S1E0R, AT S1E0W */
3555         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3556         break;
3557     case 4: /* AT S12E1R, AT S12E1W */
3558         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3559         break;
3560     case 6: /* AT S12E0R, AT S12E0W */
3561         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3562         break;
3563     default:
3564         g_assert_not_reached();
3565     }
3566 
3567     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3568                                        mmu_idx, secure);
3569 #else
3570     /* Handled by hardware accelerator. */
3571     g_assert_not_reached();
3572 #endif /* CONFIG_TCG */
3573 }
3574 #endif
3575 
3576 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3577     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3578       .access = PL1_RW, .resetvalue = 0,
3579       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3580                              offsetoflow32(CPUARMState, cp15.par_ns) },
3581       .writefn = par_write },
3582 #ifndef CONFIG_USER_ONLY
3583     /* This underdecoding is safe because the reginfo is NO_RAW. */
3584     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3585       .access = PL1_W, .accessfn = ats_access,
3586       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3587 #endif
3588 };
3589 
3590 /* Return basic MPU access permission bits.  */
3591 static uint32_t simple_mpu_ap_bits(uint32_t val)
3592 {
3593     uint32_t ret;
3594     uint32_t mask;
3595     int i;
3596     ret = 0;
3597     mask = 3;
3598     for (i = 0; i < 16; i += 2) {
3599         ret |= (val >> i) & mask;
3600         mask <<= 2;
3601     }
3602     return ret;
3603 }
3604 
3605 /* Pad basic MPU access permission bits to extended format.  */
3606 static uint32_t extended_mpu_ap_bits(uint32_t val)
3607 {
3608     uint32_t ret;
3609     uint32_t mask;
3610     int i;
3611     ret = 0;
3612     mask = 3;
3613     for (i = 0; i < 16; i += 2) {
3614         ret |= (val & mask) << i;
3615         mask <<= 2;
3616     }
3617     return ret;
3618 }
3619 
3620 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3621                                  uint64_t value)
3622 {
3623     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3624 }
3625 
3626 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3627 {
3628     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3629 }
3630 
3631 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3632                                  uint64_t value)
3633 {
3634     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3635 }
3636 
3637 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3638 {
3639     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3640 }
3641 
3642 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3643 {
3644     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3645 
3646     if (!u32p) {
3647         return 0;
3648     }
3649 
3650     u32p += env->pmsav7.rnr[M_REG_NS];
3651     return *u32p;
3652 }
3653 
3654 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3655                          uint64_t value)
3656 {
3657     ARMCPU *cpu = env_archcpu(env);
3658     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3659 
3660     if (!u32p) {
3661         return;
3662     }
3663 
3664     u32p += env->pmsav7.rnr[M_REG_NS];
3665     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3666     *u32p = value;
3667 }
3668 
3669 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3670                               uint64_t value)
3671 {
3672     ARMCPU *cpu = env_archcpu(env);
3673     uint32_t nrgs = cpu->pmsav7_dregion;
3674 
3675     if (value >= nrgs) {
3676         qemu_log_mask(LOG_GUEST_ERROR,
3677                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3678                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3679         return;
3680     }
3681 
3682     raw_write(env, ri, value);
3683 }
3684 
3685 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3686     /* Reset for all these registers is handled in arm_cpu_reset(),
3687      * because the PMSAv7 is also used by M-profile CPUs, which do
3688      * not register cpregs but still need the state to be reset.
3689      */
3690     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3691       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3692       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3693       .readfn = pmsav7_read, .writefn = pmsav7_write,
3694       .resetfn = arm_cp_reset_ignore },
3695     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3696       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3697       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3698       .readfn = pmsav7_read, .writefn = pmsav7_write,
3699       .resetfn = arm_cp_reset_ignore },
3700     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3701       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3702       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3703       .readfn = pmsav7_read, .writefn = pmsav7_write,
3704       .resetfn = arm_cp_reset_ignore },
3705     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3706       .access = PL1_RW,
3707       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3708       .writefn = pmsav7_rgnr_write,
3709       .resetfn = arm_cp_reset_ignore },
3710 };
3711 
3712 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3713     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3714       .access = PL1_RW, .type = ARM_CP_ALIAS,
3715       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3716       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3717     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3718       .access = PL1_RW, .type = ARM_CP_ALIAS,
3719       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3720       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3721     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3722       .access = PL1_RW,
3723       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3724       .resetvalue = 0, },
3725     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3726       .access = PL1_RW,
3727       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3728       .resetvalue = 0, },
3729     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3730       .access = PL1_RW,
3731       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3732     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3733       .access = PL1_RW,
3734       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3735     /* Protection region base and size registers */
3736     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3737       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3738       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3739     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3740       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3741       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3742     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3743       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3744       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3745     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3746       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3747       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3748     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3749       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3750       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3751     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3752       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3753       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3754     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3755       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3756       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3757     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3758       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3759       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3760 };
3761 
3762 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3763                              uint64_t value)
3764 {
3765     ARMCPU *cpu = env_archcpu(env);
3766 
3767     if (!arm_feature(env, ARM_FEATURE_V8)) {
3768         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3769             /*
3770              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3771              * using Long-descriptor translation table format
3772              */
3773             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3774         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3775             /*
3776              * In an implementation that includes the Security Extensions
3777              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3778              * Short-descriptor translation table format.
3779              */
3780             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3781         } else {
3782             value &= TTBCR_N;
3783         }
3784     }
3785 
3786     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3787         /* With LPAE the TTBCR could result in a change of ASID
3788          * via the TTBCR.A1 bit, so do a TLB flush.
3789          */
3790         tlb_flush(CPU(cpu));
3791     }
3792     raw_write(env, ri, value);
3793 }
3794 
3795 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3796                                uint64_t value)
3797 {
3798     ARMCPU *cpu = env_archcpu(env);
3799 
3800     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3801     tlb_flush(CPU(cpu));
3802     raw_write(env, ri, value);
3803 }
3804 
3805 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3806                             uint64_t value)
3807 {
3808     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3809     if (cpreg_field_is_64bit(ri) &&
3810         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3811         ARMCPU *cpu = env_archcpu(env);
3812         tlb_flush(CPU(cpu));
3813     }
3814     raw_write(env, ri, value);
3815 }
3816 
3817 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3818                                     uint64_t value)
3819 {
3820     /*
3821      * If we are running with E2&0 regime, then an ASID is active.
3822      * Flush if that might be changing.  Note we're not checking
3823      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3824      * holds the active ASID, only checking the field that might.
3825      */
3826     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3827         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3828         uint16_t mask = ARMMMUIdxBit_E20_2 |
3829                         ARMMMUIdxBit_E20_2_PAN |
3830                         ARMMMUIdxBit_E20_0;
3831         tlb_flush_by_mmuidx(env_cpu(env), mask);
3832     }
3833     raw_write(env, ri, value);
3834 }
3835 
3836 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3837                         uint64_t value)
3838 {
3839     ARMCPU *cpu = env_archcpu(env);
3840     CPUState *cs = CPU(cpu);
3841 
3842     /*
3843      * A change in VMID to the stage2 page table (Stage2) invalidates
3844      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
3845      */
3846     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3847         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
3848     }
3849     raw_write(env, ri, value);
3850 }
3851 
3852 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3853     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3854       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3855       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3856                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3857     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3858       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3859       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3860                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3861     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3862       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3863       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3864                              offsetof(CPUARMState, cp15.dfar_ns) } },
3865     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3866       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3867       .access = PL1_RW, .accessfn = access_tvm_trvm,
3868       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3869       .resetvalue = 0, },
3870 };
3871 
3872 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3873     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3874       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3875       .access = PL1_RW, .accessfn = access_tvm_trvm,
3876       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3877     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3878       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3879       .access = PL1_RW, .accessfn = access_tvm_trvm,
3880       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3881       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3882                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3883     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3884       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3885       .access = PL1_RW, .accessfn = access_tvm_trvm,
3886       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3887       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3888                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3889     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3890       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3891       .access = PL1_RW, .accessfn = access_tvm_trvm,
3892       .writefn = vmsa_tcr_el12_write,
3893       .raw_writefn = raw_write,
3894       .resetvalue = 0,
3895       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3896     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3897       .access = PL1_RW, .accessfn = access_tvm_trvm,
3898       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3899       .raw_writefn = raw_write,
3900       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3901                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3902 };
3903 
3904 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3905  * qemu tlbs nor adjusting cached masks.
3906  */
3907 static const ARMCPRegInfo ttbcr2_reginfo = {
3908     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3909     .access = PL1_RW, .accessfn = access_tvm_trvm,
3910     .type = ARM_CP_ALIAS,
3911     .bank_fieldoffsets = {
3912         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3913         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3914     },
3915 };
3916 
3917 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3918                                 uint64_t value)
3919 {
3920     env->cp15.c15_ticonfig = value & 0xe7;
3921     /* The OS_TYPE bit in this register changes the reported CPUID! */
3922     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3923         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3924 }
3925 
3926 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3927                                 uint64_t value)
3928 {
3929     env->cp15.c15_threadid = value & 0xffff;
3930 }
3931 
3932 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3933                            uint64_t value)
3934 {
3935     /* Wait-for-interrupt (deprecated) */
3936     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3937 }
3938 
3939 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3940                                   uint64_t value)
3941 {
3942     /* On OMAP there are registers indicating the max/min index of dcache lines
3943      * containing a dirty line; cache flush operations have to reset these.
3944      */
3945     env->cp15.c15_i_max = 0x000;
3946     env->cp15.c15_i_min = 0xff0;
3947 }
3948 
3949 static const ARMCPRegInfo omap_cp_reginfo[] = {
3950     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3951       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3952       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3953       .resetvalue = 0, },
3954     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3955       .access = PL1_RW, .type = ARM_CP_NOP },
3956     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3957       .access = PL1_RW,
3958       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3959       .writefn = omap_ticonfig_write },
3960     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3961       .access = PL1_RW,
3962       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3963     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3964       .access = PL1_RW, .resetvalue = 0xff0,
3965       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3966     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3967       .access = PL1_RW,
3968       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3969       .writefn = omap_threadid_write },
3970     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3971       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3972       .type = ARM_CP_NO_RAW,
3973       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3974     /* TODO: Peripheral port remap register:
3975      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3976      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3977      * when MMU is off.
3978      */
3979     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3980       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3981       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3982       .writefn = omap_cachemaint_write },
3983     { .name = "C9", .cp = 15, .crn = 9,
3984       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3985       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3986 };
3987 
3988 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3989                               uint64_t value)
3990 {
3991     env->cp15.c15_cpar = value & 0x3fff;
3992 }
3993 
3994 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3995     { .name = "XSCALE_CPAR",
3996       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3997       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3998       .writefn = xscale_cpar_write, },
3999     { .name = "XSCALE_AUXCR",
4000       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4001       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4002       .resetvalue = 0, },
4003     /* XScale specific cache-lockdown: since we have no cache we NOP these
4004      * and hope the guest does not really rely on cache behaviour.
4005      */
4006     { .name = "XSCALE_LOCK_ICACHE_LINE",
4007       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4008       .access = PL1_W, .type = ARM_CP_NOP },
4009     { .name = "XSCALE_UNLOCK_ICACHE",
4010       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4011       .access = PL1_W, .type = ARM_CP_NOP },
4012     { .name = "XSCALE_DCACHE_LOCK",
4013       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4014       .access = PL1_RW, .type = ARM_CP_NOP },
4015     { .name = "XSCALE_UNLOCK_DCACHE",
4016       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4017       .access = PL1_W, .type = ARM_CP_NOP },
4018 };
4019 
4020 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4021     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4022      * implementation of this implementation-defined space.
4023      * Ideally this should eventually disappear in favour of actually
4024      * implementing the correct behaviour for all cores.
4025      */
4026     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4027       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4028       .access = PL1_RW,
4029       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4030       .resetvalue = 0 },
4031 };
4032 
4033 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4034     /* Cache status: RAZ because we have no cache so it's always clean */
4035     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4036       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4037       .resetvalue = 0 },
4038 };
4039 
4040 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4041     /* We never have a block transfer operation in progress */
4042     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4043       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4044       .resetvalue = 0 },
4045     /* The cache ops themselves: these all NOP for QEMU */
4046     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4047       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4048     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4049       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4050     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4051       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4052     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4053       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4054     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4055       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4056     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4057       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4058 };
4059 
4060 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4061     /* The cache test-and-clean instructions always return (1 << 30)
4062      * to indicate that there are no dirty cache lines.
4063      */
4064     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4065       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4066       .resetvalue = (1 << 30) },
4067     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4068       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4069       .resetvalue = (1 << 30) },
4070 };
4071 
4072 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4073     /* Ignore ReadBuffer accesses */
4074     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4075       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4076       .access = PL1_RW, .resetvalue = 0,
4077       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4078 };
4079 
4080 static uint64_t midr_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.vpidr_el2;
4086     }
4087     return raw_read(env, ri);
4088 }
4089 
4090 static uint64_t mpidr_read_val(CPUARMState *env)
4091 {
4092     ARMCPU *cpu = env_archcpu(env);
4093     uint64_t mpidr = cpu->mp_affinity;
4094 
4095     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4096         mpidr |= (1U << 31);
4097         /* Cores which are uniprocessor (non-coherent)
4098          * but still implement the MP extensions set
4099          * bit 30. (For instance, Cortex-R5).
4100          */
4101         if (cpu->mp_is_up) {
4102             mpidr |= (1u << 30);
4103         }
4104     }
4105     return mpidr;
4106 }
4107 
4108 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4109 {
4110     unsigned int cur_el = arm_current_el(env);
4111 
4112     if (arm_is_el2_enabled(env) && cur_el == 1) {
4113         return env->cp15.vmpidr_el2;
4114     }
4115     return mpidr_read_val(env);
4116 }
4117 
4118 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4119     /* NOP AMAIR0/1 */
4120     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4121       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4122       .access = PL1_RW, .accessfn = access_tvm_trvm,
4123       .type = ARM_CP_CONST, .resetvalue = 0 },
4124     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4125     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4126       .access = PL1_RW, .accessfn = access_tvm_trvm,
4127       .type = ARM_CP_CONST, .resetvalue = 0 },
4128     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4129       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4130       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4131                              offsetof(CPUARMState, cp15.par_ns)} },
4132     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4133       .access = PL1_RW, .accessfn = access_tvm_trvm,
4134       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4135       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4136                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4137       .writefn = vmsa_ttbr_write, },
4138     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4139       .access = PL1_RW, .accessfn = access_tvm_trvm,
4140       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4141       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4142                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4143       .writefn = vmsa_ttbr_write, },
4144 };
4145 
4146 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4147 {
4148     return vfp_get_fpcr(env);
4149 }
4150 
4151 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4152                             uint64_t value)
4153 {
4154     vfp_set_fpcr(env, value);
4155 }
4156 
4157 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4158 {
4159     return vfp_get_fpsr(env);
4160 }
4161 
4162 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4163                             uint64_t value)
4164 {
4165     vfp_set_fpsr(env, value);
4166 }
4167 
4168 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4169                                        bool isread)
4170 {
4171     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4172         return CP_ACCESS_TRAP;
4173     }
4174     return CP_ACCESS_OK;
4175 }
4176 
4177 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4178                             uint64_t value)
4179 {
4180     env->daif = value & PSTATE_DAIF;
4181 }
4182 
4183 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4184 {
4185     return env->pstate & PSTATE_PAN;
4186 }
4187 
4188 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4189                            uint64_t value)
4190 {
4191     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4192 }
4193 
4194 static const ARMCPRegInfo pan_reginfo = {
4195     .name = "PAN", .state = ARM_CP_STATE_AA64,
4196     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4197     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4198     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4199 };
4200 
4201 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4202 {
4203     return env->pstate & PSTATE_UAO;
4204 }
4205 
4206 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4207                            uint64_t value)
4208 {
4209     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4210 }
4211 
4212 static const ARMCPRegInfo uao_reginfo = {
4213     .name = "UAO", .state = ARM_CP_STATE_AA64,
4214     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4215     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4216     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4217 };
4218 
4219 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4220 {
4221     return env->pstate & PSTATE_DIT;
4222 }
4223 
4224 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4225                            uint64_t value)
4226 {
4227     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4228 }
4229 
4230 static const ARMCPRegInfo dit_reginfo = {
4231     .name = "DIT", .state = ARM_CP_STATE_AA64,
4232     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4233     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4234     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4235 };
4236 
4237 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4238 {
4239     return env->pstate & PSTATE_SSBS;
4240 }
4241 
4242 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4243                            uint64_t value)
4244 {
4245     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4246 }
4247 
4248 static const ARMCPRegInfo ssbs_reginfo = {
4249     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4250     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4251     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4252     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4253 };
4254 
4255 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4256                                               const ARMCPRegInfo *ri,
4257                                               bool isread)
4258 {
4259     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4260     switch (arm_current_el(env)) {
4261     case 0:
4262         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4263         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4264             return CP_ACCESS_TRAP;
4265         }
4266         /* fall through */
4267     case 1:
4268         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4269         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4270             return CP_ACCESS_TRAP_EL2;
4271         }
4272         break;
4273     }
4274     return CP_ACCESS_OK;
4275 }
4276 
4277 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4278 {
4279     /* Cache invalidate/clean to Point of Unification... */
4280     switch (arm_current_el(env)) {
4281     case 0:
4282         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4283         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4284             return CP_ACCESS_TRAP;
4285         }
4286         /* fall through */
4287     case 1:
4288         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4289         if (arm_hcr_el2_eff(env) & hcrflags) {
4290             return CP_ACCESS_TRAP_EL2;
4291         }
4292         break;
4293     }
4294     return CP_ACCESS_OK;
4295 }
4296 
4297 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4298                                    bool isread)
4299 {
4300     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4301 }
4302 
4303 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4304                                   bool isread)
4305 {
4306     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4307 }
4308 
4309 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4310  * Page D4-1736 (DDI0487A.b)
4311  */
4312 
4313 static int vae1_tlbmask(CPUARMState *env)
4314 {
4315     uint64_t hcr = arm_hcr_el2_eff(env);
4316     uint16_t mask;
4317 
4318     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4319         mask = ARMMMUIdxBit_E20_2 |
4320                ARMMMUIdxBit_E20_2_PAN |
4321                ARMMMUIdxBit_E20_0;
4322     } else {
4323         mask = ARMMMUIdxBit_E10_1 |
4324                ARMMMUIdxBit_E10_1_PAN |
4325                ARMMMUIdxBit_E10_0;
4326     }
4327     return mask;
4328 }
4329 
4330 /* Return 56 if TBI is enabled, 64 otherwise. */
4331 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4332                               uint64_t addr)
4333 {
4334     uint64_t tcr = regime_tcr(env, mmu_idx);
4335     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4336     int select = extract64(addr, 55, 1);
4337 
4338     return (tbi >> select) & 1 ? 56 : 64;
4339 }
4340 
4341 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4342 {
4343     uint64_t hcr = arm_hcr_el2_eff(env);
4344     ARMMMUIdx mmu_idx;
4345 
4346     /* Only the regime of the mmu_idx below is significant. */
4347     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4348         mmu_idx = ARMMMUIdx_E20_0;
4349     } else {
4350         mmu_idx = ARMMMUIdx_E10_0;
4351     }
4352 
4353     return tlbbits_for_regime(env, mmu_idx, addr);
4354 }
4355 
4356 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4357                                       uint64_t value)
4358 {
4359     CPUState *cs = env_cpu(env);
4360     int mask = vae1_tlbmask(env);
4361 
4362     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4363 }
4364 
4365 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4366                                     uint64_t value)
4367 {
4368     CPUState *cs = env_cpu(env);
4369     int mask = vae1_tlbmask(env);
4370 
4371     if (tlb_force_broadcast(env)) {
4372         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4373     } else {
4374         tlb_flush_by_mmuidx(cs, mask);
4375     }
4376 }
4377 
4378 static int e2_tlbmask(CPUARMState *env)
4379 {
4380     return (ARMMMUIdxBit_E20_0 |
4381             ARMMMUIdxBit_E20_2 |
4382             ARMMMUIdxBit_E20_2_PAN |
4383             ARMMMUIdxBit_E2);
4384 }
4385 
4386 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4387                                   uint64_t value)
4388 {
4389     CPUState *cs = env_cpu(env);
4390     int mask = alle1_tlbmask(env);
4391 
4392     tlb_flush_by_mmuidx(cs, mask);
4393 }
4394 
4395 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4396                                   uint64_t value)
4397 {
4398     CPUState *cs = env_cpu(env);
4399     int mask = e2_tlbmask(env);
4400 
4401     tlb_flush_by_mmuidx(cs, mask);
4402 }
4403 
4404 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405                                   uint64_t value)
4406 {
4407     ARMCPU *cpu = env_archcpu(env);
4408     CPUState *cs = CPU(cpu);
4409 
4410     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4411 }
4412 
4413 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414                                     uint64_t value)
4415 {
4416     CPUState *cs = env_cpu(env);
4417     int mask = alle1_tlbmask(env);
4418 
4419     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4420 }
4421 
4422 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4423                                     uint64_t value)
4424 {
4425     CPUState *cs = env_cpu(env);
4426     int mask = e2_tlbmask(env);
4427 
4428     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4429 }
4430 
4431 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4432                                     uint64_t value)
4433 {
4434     CPUState *cs = env_cpu(env);
4435 
4436     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4437 }
4438 
4439 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4440                                  uint64_t value)
4441 {
4442     /* Invalidate by VA, EL2
4443      * Currently handles both VAE2 and VALE2, since we don't support
4444      * flush-last-level-only.
4445      */
4446     CPUState *cs = env_cpu(env);
4447     int mask = e2_tlbmask(env);
4448     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4449 
4450     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4451 }
4452 
4453 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4454                                  uint64_t value)
4455 {
4456     /* Invalidate by VA, EL3
4457      * Currently handles both VAE3 and VALE3, since we don't support
4458      * flush-last-level-only.
4459      */
4460     ARMCPU *cpu = env_archcpu(env);
4461     CPUState *cs = CPU(cpu);
4462     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4463 
4464     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4465 }
4466 
4467 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4468                                    uint64_t value)
4469 {
4470     CPUState *cs = env_cpu(env);
4471     int mask = vae1_tlbmask(env);
4472     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4473     int bits = vae1_tlbbits(env, pageaddr);
4474 
4475     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4476 }
4477 
4478 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4479                                  uint64_t value)
4480 {
4481     /* Invalidate by VA, EL1&0 (AArch64 version).
4482      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4483      * since we don't support flush-for-specific-ASID-only or
4484      * flush-last-level-only.
4485      */
4486     CPUState *cs = env_cpu(env);
4487     int mask = vae1_tlbmask(env);
4488     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4489     int bits = vae1_tlbbits(env, pageaddr);
4490 
4491     if (tlb_force_broadcast(env)) {
4492         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4493     } else {
4494         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4495     }
4496 }
4497 
4498 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4499                                    uint64_t value)
4500 {
4501     CPUState *cs = env_cpu(env);
4502     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4503     int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
4504 
4505     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4506                                                   ARMMMUIdxBit_E2, bits);
4507 }
4508 
4509 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4510                                    uint64_t value)
4511 {
4512     CPUState *cs = env_cpu(env);
4513     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4514     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4515 
4516     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4517                                                   ARMMMUIdxBit_E3, bits);
4518 }
4519 
4520 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4521 {
4522     /*
4523      * The MSB of value is the NS field, which only applies if SEL2
4524      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4525      */
4526     return (value >= 0
4527             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4528             && arm_is_secure_below_el3(env)
4529             ? ARMMMUIdxBit_Stage2_S
4530             : ARMMMUIdxBit_Stage2);
4531 }
4532 
4533 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4534                                     uint64_t value)
4535 {
4536     CPUState *cs = env_cpu(env);
4537     int mask = ipas2e1_tlbmask(env, value);
4538     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4539 
4540     if (tlb_force_broadcast(env)) {
4541         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4542     } else {
4543         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4544     }
4545 }
4546 
4547 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4548                                       uint64_t value)
4549 {
4550     CPUState *cs = env_cpu(env);
4551     int mask = ipas2e1_tlbmask(env, value);
4552     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4553 
4554     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4555 }
4556 
4557 #ifdef TARGET_AARCH64
4558 typedef struct {
4559     uint64_t base;
4560     uint64_t length;
4561 } TLBIRange;
4562 
4563 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4564 {
4565     /*
4566      * Note that the TLBI range TG field encoding differs from both
4567      * TG0 and TG1 encodings.
4568      */
4569     switch (tg) {
4570     case 1:
4571         return Gran4K;
4572     case 2:
4573         return Gran16K;
4574     case 3:
4575         return Gran64K;
4576     default:
4577         return GranInvalid;
4578     }
4579 }
4580 
4581 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4582                                      uint64_t value)
4583 {
4584     unsigned int page_size_granule, page_shift, num, scale, exponent;
4585     /* Extract one bit to represent the va selector in use. */
4586     uint64_t select = sextract64(value, 36, 1);
4587     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4588     TLBIRange ret = { };
4589     ARMGranuleSize gran;
4590 
4591     page_size_granule = extract64(value, 46, 2);
4592     gran = tlbi_range_tg_to_gran_size(page_size_granule);
4593 
4594     /* The granule encoded in value must match the granule in use. */
4595     if (gran != param.gran) {
4596         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4597                       page_size_granule);
4598         return ret;
4599     }
4600 
4601     page_shift = arm_granule_bits(gran);
4602     num = extract64(value, 39, 5);
4603     scale = extract64(value, 44, 2);
4604     exponent = (5 * scale) + 1;
4605 
4606     ret.length = (num + 1) << (exponent + page_shift);
4607 
4608     if (param.select) {
4609         ret.base = sextract64(value, 0, 37);
4610     } else {
4611         ret.base = extract64(value, 0, 37);
4612     }
4613     if (param.ds) {
4614         /*
4615          * With DS=1, BaseADDR is always shifted 16 so that it is able
4616          * to address all 52 va bits.  The input address is perforce
4617          * aligned on a 64k boundary regardless of translation granule.
4618          */
4619         page_shift = 16;
4620     }
4621     ret.base <<= page_shift;
4622 
4623     return ret;
4624 }
4625 
4626 static void do_rvae_write(CPUARMState *env, uint64_t value,
4627                           int idxmap, bool synced)
4628 {
4629     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4630     TLBIRange range;
4631     int bits;
4632 
4633     range = tlbi_aa64_get_range(env, one_idx, value);
4634     bits = tlbbits_for_regime(env, one_idx, range.base);
4635 
4636     if (synced) {
4637         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4638                                                   range.base,
4639                                                   range.length,
4640                                                   idxmap,
4641                                                   bits);
4642     } else {
4643         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4644                                   range.length, idxmap, bits);
4645     }
4646 }
4647 
4648 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4649                                   const ARMCPRegInfo *ri,
4650                                   uint64_t value)
4651 {
4652     /*
4653      * Invalidate by VA range, EL1&0.
4654      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4655      * since we don't support flush-for-specific-ASID-only or
4656      * flush-last-level-only.
4657      */
4658 
4659     do_rvae_write(env, value, vae1_tlbmask(env),
4660                   tlb_force_broadcast(env));
4661 }
4662 
4663 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4664                                     const ARMCPRegInfo *ri,
4665                                     uint64_t value)
4666 {
4667     /*
4668      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4669      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4670      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4671      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4672      * shareable specific flushes.
4673      */
4674 
4675     do_rvae_write(env, value, vae1_tlbmask(env), true);
4676 }
4677 
4678 static int vae2_tlbmask(CPUARMState *env)
4679 {
4680     return ARMMMUIdxBit_E2;
4681 }
4682 
4683 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4684                                   const ARMCPRegInfo *ri,
4685                                   uint64_t value)
4686 {
4687     /*
4688      * Invalidate by VA range, EL2.
4689      * Currently handles all of RVAE2 and RVALE2,
4690      * since we don't support flush-for-specific-ASID-only or
4691      * flush-last-level-only.
4692      */
4693 
4694     do_rvae_write(env, value, vae2_tlbmask(env),
4695                   tlb_force_broadcast(env));
4696 
4697 
4698 }
4699 
4700 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4701                                     const ARMCPRegInfo *ri,
4702                                     uint64_t value)
4703 {
4704     /*
4705      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4706      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4707      * since we don't support flush-for-specific-ASID-only,
4708      * flush-last-level-only or inner/outer shareable specific flushes.
4709      */
4710 
4711     do_rvae_write(env, value, vae2_tlbmask(env), true);
4712 
4713 }
4714 
4715 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4716                                   const ARMCPRegInfo *ri,
4717                                   uint64_t value)
4718 {
4719     /*
4720      * Invalidate by VA range, EL3.
4721      * Currently handles all of RVAE3 and RVALE3,
4722      * since we don't support flush-for-specific-ASID-only or
4723      * flush-last-level-only.
4724      */
4725 
4726     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
4727 }
4728 
4729 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4730                                     const ARMCPRegInfo *ri,
4731                                     uint64_t value)
4732 {
4733     /*
4734      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4735      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4736      * since we don't support flush-for-specific-ASID-only,
4737      * flush-last-level-only or inner/outer specific flushes.
4738      */
4739 
4740     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
4741 }
4742 
4743 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4744                                      uint64_t value)
4745 {
4746     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
4747                   tlb_force_broadcast(env));
4748 }
4749 
4750 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
4751                                        const ARMCPRegInfo *ri,
4752                                        uint64_t value)
4753 {
4754     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
4755 }
4756 #endif
4757 
4758 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4759                                       bool isread)
4760 {
4761     int cur_el = arm_current_el(env);
4762 
4763     if (cur_el < 2) {
4764         uint64_t hcr = arm_hcr_el2_eff(env);
4765 
4766         if (cur_el == 0) {
4767             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4768                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4769                     return CP_ACCESS_TRAP_EL2;
4770                 }
4771             } else {
4772                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4773                     return CP_ACCESS_TRAP;
4774                 }
4775                 if (hcr & HCR_TDZ) {
4776                     return CP_ACCESS_TRAP_EL2;
4777                 }
4778             }
4779         } else if (hcr & HCR_TDZ) {
4780             return CP_ACCESS_TRAP_EL2;
4781         }
4782     }
4783     return CP_ACCESS_OK;
4784 }
4785 
4786 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4787 {
4788     ARMCPU *cpu = env_archcpu(env);
4789     int dzp_bit = 1 << 4;
4790 
4791     /* DZP indicates whether DC ZVA access is allowed */
4792     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4793         dzp_bit = 0;
4794     }
4795     return cpu->dcz_blocksize | dzp_bit;
4796 }
4797 
4798 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4799                                     bool isread)
4800 {
4801     if (!(env->pstate & PSTATE_SP)) {
4802         /* Access to SP_EL0 is undefined if it's being used as
4803          * the stack pointer.
4804          */
4805         return CP_ACCESS_TRAP_UNCATEGORIZED;
4806     }
4807     return CP_ACCESS_OK;
4808 }
4809 
4810 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4811 {
4812     return env->pstate & PSTATE_SP;
4813 }
4814 
4815 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4816 {
4817     update_spsel(env, val);
4818 }
4819 
4820 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821                         uint64_t value)
4822 {
4823     ARMCPU *cpu = env_archcpu(env);
4824 
4825     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4826         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4827         value &= ~SCTLR_M;
4828     }
4829 
4830     /* ??? Lots of these bits are not implemented.  */
4831 
4832     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4833         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4834             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4835         } else {
4836             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4837                        SCTLR_ATA0 | SCTLR_ATA);
4838         }
4839     }
4840 
4841     if (raw_read(env, ri) == value) {
4842         /* Skip the TLB flush if nothing actually changed; Linux likes
4843          * to do a lot of pointless SCTLR writes.
4844          */
4845         return;
4846     }
4847 
4848     raw_write(env, ri, value);
4849 
4850     /* This may enable/disable the MMU, so do a TLB flush.  */
4851     tlb_flush(CPU(cpu));
4852 
4853     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4854         /*
4855          * Normally we would always end the TB on an SCTLR write; see the
4856          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4857          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4858          * of hflags from the translator, so do it here.
4859          */
4860         arm_rebuild_hflags(env);
4861     }
4862 }
4863 
4864 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4865                            uint64_t value)
4866 {
4867     /*
4868      * Some MDCR_EL3 bits affect whether PMU counters are running:
4869      * if we are trying to change any of those then we must
4870      * bracket this update with PMU start/finish calls.
4871      */
4872     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4873 
4874     if (pmu_op) {
4875         pmu_op_start(env);
4876     }
4877     env->cp15.mdcr_el3 = value;
4878     if (pmu_op) {
4879         pmu_op_finish(env);
4880     }
4881 }
4882 
4883 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4884                        uint64_t value)
4885 {
4886     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4887     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4888 }
4889 
4890 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4891                            uint64_t value)
4892 {
4893     /*
4894      * Some MDCR_EL2 bits affect whether PMU counters are running:
4895      * if we are trying to change any of those then we must
4896      * bracket this update with PMU start/finish calls.
4897      */
4898     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4899 
4900     if (pmu_op) {
4901         pmu_op_start(env);
4902     }
4903     env->cp15.mdcr_el2 = value;
4904     if (pmu_op) {
4905         pmu_op_finish(env);
4906     }
4907 }
4908 
4909 static const ARMCPRegInfo v8_cp_reginfo[] = {
4910     /* Minimal set of EL0-visible registers. This will need to be expanded
4911      * significantly for system emulation of AArch64 CPUs.
4912      */
4913     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4914       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4915       .access = PL0_RW, .type = ARM_CP_NZCV },
4916     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4917       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4918       .type = ARM_CP_NO_RAW,
4919       .access = PL0_RW, .accessfn = aa64_daif_access,
4920       .fieldoffset = offsetof(CPUARMState, daif),
4921       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4922     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4923       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4924       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4925       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4926     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4927       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4928       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4929       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4930     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4931       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4932       .access = PL0_R, .type = ARM_CP_NO_RAW,
4933       .readfn = aa64_dczid_read },
4934     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4935       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4936       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4937 #ifndef CONFIG_USER_ONLY
4938       /* Avoid overhead of an access check that always passes in user-mode */
4939       .accessfn = aa64_zva_access,
4940 #endif
4941     },
4942     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4943       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4944       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4945     /* Cache ops: all NOPs since we don't emulate caches */
4946     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4947       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4948       .access = PL1_W, .type = ARM_CP_NOP,
4949       .accessfn = access_ticab },
4950     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4952       .access = PL1_W, .type = ARM_CP_NOP,
4953       .accessfn = access_tocu },
4954     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4955       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4956       .access = PL0_W, .type = ARM_CP_NOP,
4957       .accessfn = access_tocu },
4958     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4959       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4960       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4961       .type = ARM_CP_NOP },
4962     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4963       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4964       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4965     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4966       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4967       .access = PL0_W, .type = ARM_CP_NOP,
4968       .accessfn = aa64_cacheop_poc_access },
4969     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4970       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4971       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4972     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4973       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4974       .access = PL0_W, .type = ARM_CP_NOP,
4975       .accessfn = access_tocu },
4976     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4977       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4978       .access = PL0_W, .type = ARM_CP_NOP,
4979       .accessfn = aa64_cacheop_poc_access },
4980     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4981       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4982       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4983     /* TLBI operations */
4984     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4985       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4986       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
4987       .writefn = tlbi_aa64_vmalle1is_write },
4988     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4989       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4990       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
4991       .writefn = tlbi_aa64_vae1is_write },
4992     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4993       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4994       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
4995       .writefn = tlbi_aa64_vmalle1is_write },
4996     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4997       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4998       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
4999       .writefn = tlbi_aa64_vae1is_write },
5000     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5001       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5002       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5003       .writefn = tlbi_aa64_vae1is_write },
5004     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5005       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5006       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5007       .writefn = tlbi_aa64_vae1is_write },
5008     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5009       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5010       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5011       .writefn = tlbi_aa64_vmalle1_write },
5012     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5013       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5014       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5015       .writefn = tlbi_aa64_vae1_write },
5016     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5017       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5018       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5019       .writefn = tlbi_aa64_vmalle1_write },
5020     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5021       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5022       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5023       .writefn = tlbi_aa64_vae1_write },
5024     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5025       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5026       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5027       .writefn = tlbi_aa64_vae1_write },
5028     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5029       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5030       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5031       .writefn = tlbi_aa64_vae1_write },
5032     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5033       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5034       .access = PL2_W, .type = ARM_CP_NO_RAW,
5035       .writefn = tlbi_aa64_ipas2e1is_write },
5036     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5037       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5038       .access = PL2_W, .type = ARM_CP_NO_RAW,
5039       .writefn = tlbi_aa64_ipas2e1is_write },
5040     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5041       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5042       .access = PL2_W, .type = ARM_CP_NO_RAW,
5043       .writefn = tlbi_aa64_alle1is_write },
5044     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5045       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5046       .access = PL2_W, .type = ARM_CP_NO_RAW,
5047       .writefn = tlbi_aa64_alle1is_write },
5048     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5049       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5050       .access = PL2_W, .type = ARM_CP_NO_RAW,
5051       .writefn = tlbi_aa64_ipas2e1_write },
5052     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5053       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5054       .access = PL2_W, .type = ARM_CP_NO_RAW,
5055       .writefn = tlbi_aa64_ipas2e1_write },
5056     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5057       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5058       .access = PL2_W, .type = ARM_CP_NO_RAW,
5059       .writefn = tlbi_aa64_alle1_write },
5060     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5061       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5062       .access = PL2_W, .type = ARM_CP_NO_RAW,
5063       .writefn = tlbi_aa64_alle1is_write },
5064 #ifndef CONFIG_USER_ONLY
5065     /* 64 bit address translation operations */
5066     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5067       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5068       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5069       .writefn = ats_write64 },
5070     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5071       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5072       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5073       .writefn = ats_write64 },
5074     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5075       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5076       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5077       .writefn = ats_write64 },
5078     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5079       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5080       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5081       .writefn = ats_write64 },
5082     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5083       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5084       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5085       .writefn = ats_write64 },
5086     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5087       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5088       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5089       .writefn = ats_write64 },
5090     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5091       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5092       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5093       .writefn = ats_write64 },
5094     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5095       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5096       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5097       .writefn = ats_write64 },
5098     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5099     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5100       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5101       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5102       .writefn = ats_write64 },
5103     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5104       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5105       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5106       .writefn = ats_write64 },
5107     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5108       .type = ARM_CP_ALIAS,
5109       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5110       .access = PL1_RW, .resetvalue = 0,
5111       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5112       .writefn = par_write },
5113 #endif
5114     /* TLB invalidate last level of translation table walk */
5115     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5116       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5117       .writefn = tlbimva_is_write },
5118     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5119       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5120       .writefn = tlbimvaa_is_write },
5121     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5122       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5123       .writefn = tlbimva_write },
5124     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5125       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5126       .writefn = tlbimvaa_write },
5127     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5128       .type = ARM_CP_NO_RAW, .access = PL2_W,
5129       .writefn = tlbimva_hyp_write },
5130     { .name = "TLBIMVALHIS",
5131       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5132       .type = ARM_CP_NO_RAW, .access = PL2_W,
5133       .writefn = tlbimva_hyp_is_write },
5134     { .name = "TLBIIPAS2",
5135       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5136       .type = ARM_CP_NO_RAW, .access = PL2_W,
5137       .writefn = tlbiipas2_hyp_write },
5138     { .name = "TLBIIPAS2IS",
5139       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5140       .type = ARM_CP_NO_RAW, .access = PL2_W,
5141       .writefn = tlbiipas2is_hyp_write },
5142     { .name = "TLBIIPAS2L",
5143       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5144       .type = ARM_CP_NO_RAW, .access = PL2_W,
5145       .writefn = tlbiipas2_hyp_write },
5146     { .name = "TLBIIPAS2LIS",
5147       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5148       .type = ARM_CP_NO_RAW, .access = PL2_W,
5149       .writefn = tlbiipas2is_hyp_write },
5150     /* 32 bit cache operations */
5151     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5152       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5153     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5154       .type = ARM_CP_NOP, .access = PL1_W },
5155     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5156       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5157     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5158       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5159     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5160       .type = ARM_CP_NOP, .access = PL1_W },
5161     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5162       .type = ARM_CP_NOP, .access = PL1_W },
5163     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5164       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5165     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5166       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5167     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5168       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5169     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5170       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5171     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5172       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5173     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5174       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5175     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5176       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5177     /* MMU Domain access control / MPU write buffer control */
5178     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5179       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5180       .writefn = dacr_write, .raw_writefn = raw_write,
5181       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5182                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5183     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5184       .type = ARM_CP_ALIAS,
5185       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5186       .access = PL1_RW,
5187       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5188     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5189       .type = ARM_CP_ALIAS,
5190       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5191       .access = PL1_RW,
5192       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5193     /* We rely on the access checks not allowing the guest to write to the
5194      * state field when SPSel indicates that it's being used as the stack
5195      * pointer.
5196      */
5197     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5198       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5199       .access = PL1_RW, .accessfn = sp_el0_access,
5200       .type = ARM_CP_ALIAS,
5201       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5202     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5203       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5204       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5205       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5206     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5207       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5208       .type = ARM_CP_NO_RAW,
5209       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5210     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5211       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5212       .access = PL2_RW,
5213       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5214       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5215     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5216       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5217       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5218       .writefn = dacr_write, .raw_writefn = raw_write,
5219       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5220     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5221       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5222       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5223       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5224     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5225       .type = ARM_CP_ALIAS,
5226       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5227       .access = PL2_RW,
5228       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5229     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5230       .type = ARM_CP_ALIAS,
5231       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5232       .access = PL2_RW,
5233       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5234     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5235       .type = ARM_CP_ALIAS,
5236       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5237       .access = PL2_RW,
5238       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5239     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5240       .type = ARM_CP_ALIAS,
5241       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5242       .access = PL2_RW,
5243       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5244     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5245       .type = ARM_CP_IO,
5246       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5247       .resetvalue = 0,
5248       .access = PL3_RW,
5249       .writefn = mdcr_el3_write,
5250       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5251     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5252       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5253       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5254       .writefn = sdcr_write,
5255       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5256 };
5257 
5258 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5259 {
5260     ARMCPU *cpu = env_archcpu(env);
5261 
5262     if (arm_feature(env, ARM_FEATURE_V8)) {
5263         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5264     } else {
5265         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5266     }
5267 
5268     if (arm_feature(env, ARM_FEATURE_EL3)) {
5269         valid_mask &= ~HCR_HCD;
5270     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5271         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5272          * However, if we're using the SMC PSCI conduit then QEMU is
5273          * effectively acting like EL3 firmware and so the guest at
5274          * EL2 should retain the ability to prevent EL1 from being
5275          * able to make SMC calls into the ersatz firmware, so in
5276          * that case HCR.TSC should be read/write.
5277          */
5278         valid_mask &= ~HCR_TSC;
5279     }
5280 
5281     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5282         if (cpu_isar_feature(aa64_vh, cpu)) {
5283             valid_mask |= HCR_E2H;
5284         }
5285         if (cpu_isar_feature(aa64_ras, cpu)) {
5286             valid_mask |= HCR_TERR | HCR_TEA;
5287         }
5288         if (cpu_isar_feature(aa64_lor, cpu)) {
5289             valid_mask |= HCR_TLOR;
5290         }
5291         if (cpu_isar_feature(aa64_pauth, cpu)) {
5292             valid_mask |= HCR_API | HCR_APK;
5293         }
5294         if (cpu_isar_feature(aa64_mte, cpu)) {
5295             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5296         }
5297         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5298             valid_mask |= HCR_ENSCXT;
5299         }
5300         if (cpu_isar_feature(aa64_fwb, cpu)) {
5301             valid_mask |= HCR_FWB;
5302         }
5303     }
5304 
5305     if (cpu_isar_feature(any_evt, cpu)) {
5306         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5307     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5308         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5309     }
5310 
5311     /* Clear RES0 bits.  */
5312     value &= valid_mask;
5313 
5314     /*
5315      * These bits change the MMU setup:
5316      * HCR_VM enables stage 2 translation
5317      * HCR_PTW forbids certain page-table setups
5318      * HCR_DC disables stage1 and enables stage2 translation
5319      * HCR_DCT enables tagging on (disabled) stage1 translation
5320      * HCR_FWB changes the interpretation of stage2 descriptor bits
5321      */
5322     if ((env->cp15.hcr_el2 ^ value) &
5323         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5324         tlb_flush(CPU(cpu));
5325     }
5326     env->cp15.hcr_el2 = value;
5327 
5328     /*
5329      * Updates to VI and VF require us to update the status of
5330      * virtual interrupts, which are the logical OR of these bits
5331      * and the state of the input lines from the GIC. (This requires
5332      * that we have the iothread lock, which is done by marking the
5333      * reginfo structs as ARM_CP_IO.)
5334      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5335      * possible for it to be taken immediately, because VIRQ and
5336      * VFIQ are masked unless running at EL0 or EL1, and HCR
5337      * can only be written at EL2.
5338      */
5339     g_assert(qemu_mutex_iothread_locked());
5340     arm_cpu_update_virq(cpu);
5341     arm_cpu_update_vfiq(cpu);
5342     arm_cpu_update_vserr(cpu);
5343 }
5344 
5345 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5346 {
5347     do_hcr_write(env, value, 0);
5348 }
5349 
5350 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5351                           uint64_t value)
5352 {
5353     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5354     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5355     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5356 }
5357 
5358 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5359                          uint64_t value)
5360 {
5361     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5362     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5363     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5364 }
5365 
5366 /*
5367  * Return the effective value of HCR_EL2, at the given security state.
5368  * Bits that are not included here:
5369  * RW       (read from SCR_EL3.RW as needed)
5370  */
5371 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
5372 {
5373     uint64_t ret = env->cp15.hcr_el2;
5374 
5375     if (!arm_is_el2_enabled_secstate(env, secure)) {
5376         /*
5377          * "This register has no effect if EL2 is not enabled in the
5378          * current Security state".  This is ARMv8.4-SecEL2 speak for
5379          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5380          *
5381          * Prior to that, the language was "In an implementation that
5382          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5383          * as if this field is 0 for all purposes other than a direct
5384          * read or write access of HCR_EL2".  With lots of enumeration
5385          * on a per-field basis.  In current QEMU, this is condition
5386          * is arm_is_secure_below_el3.
5387          *
5388          * Since the v8.4 language applies to the entire register, and
5389          * appears to be backward compatible, use that.
5390          */
5391         return 0;
5392     }
5393 
5394     /*
5395      * For a cpu that supports both aarch64 and aarch32, we can set bits
5396      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5397      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5398      */
5399     if (!arm_el_is_aa64(env, 2)) {
5400         uint64_t aa32_valid;
5401 
5402         /*
5403          * These bits are up-to-date as of ARMv8.6.
5404          * For HCR, it's easiest to list just the 2 bits that are invalid.
5405          * For HCR2, list those that are valid.
5406          */
5407         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5408         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5409                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5410         ret &= aa32_valid;
5411     }
5412 
5413     if (ret & HCR_TGE) {
5414         /* These bits are up-to-date as of ARMv8.6.  */
5415         if (ret & HCR_E2H) {
5416             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5417                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5418                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5419                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5420                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5421                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5422         } else {
5423             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5424         }
5425         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5426                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5427                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5428                  HCR_TLOR);
5429     }
5430 
5431     return ret;
5432 }
5433 
5434 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5435 {
5436     return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5437 }
5438 
5439 /*
5440  * Corresponds to ARM pseudocode function ELIsInHost().
5441  */
5442 bool el_is_in_host(CPUARMState *env, int el)
5443 {
5444     uint64_t mask;
5445 
5446     /*
5447      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5448      * Perform the simplest bit tests first, and validate EL2 afterward.
5449      */
5450     if (el & 1) {
5451         return false; /* EL1 or EL3 */
5452     }
5453 
5454     /*
5455      * Note that hcr_write() checks isar_feature_aa64_vh(),
5456      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5457      */
5458     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5459     if ((env->cp15.hcr_el2 & mask) != mask) {
5460         return false;
5461     }
5462 
5463     /* TGE and/or E2H set: double check those bits are currently legal. */
5464     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5465 }
5466 
5467 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5468                        uint64_t value)
5469 {
5470     uint64_t valid_mask = 0;
5471 
5472     /* No features adding bits to HCRX are implemented. */
5473 
5474     /* Clear RES0 bits.  */
5475     env->cp15.hcrx_el2 = value & valid_mask;
5476 }
5477 
5478 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5479                                   bool isread)
5480 {
5481     if (arm_current_el(env) < 3
5482         && arm_feature(env, ARM_FEATURE_EL3)
5483         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5484         return CP_ACCESS_TRAP_EL3;
5485     }
5486     return CP_ACCESS_OK;
5487 }
5488 
5489 static const ARMCPRegInfo hcrx_el2_reginfo = {
5490     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5491     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5492     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5493     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5494 };
5495 
5496 /* Return the effective value of HCRX_EL2.  */
5497 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5498 {
5499     /*
5500      * The bits in this register behave as 0 for all purposes other than
5501      * direct reads of the register if:
5502      *   - EL2 is not enabled in the current security state,
5503      *   - SCR_EL3.HXEn is 0.
5504      */
5505     if (!arm_is_el2_enabled(env)
5506         || (arm_feature(env, ARM_FEATURE_EL3)
5507             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5508         return 0;
5509     }
5510     return env->cp15.hcrx_el2;
5511 }
5512 
5513 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5514                            uint64_t value)
5515 {
5516     /*
5517      * For A-profile AArch32 EL3, if NSACR.CP10
5518      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5519      */
5520     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5521         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5522         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5523         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5524     }
5525     env->cp15.cptr_el[2] = value;
5526 }
5527 
5528 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5529 {
5530     /*
5531      * For A-profile AArch32 EL3, if NSACR.CP10
5532      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5533      */
5534     uint64_t value = env->cp15.cptr_el[2];
5535 
5536     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5537         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5538         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5539     }
5540     return value;
5541 }
5542 
5543 static const ARMCPRegInfo el2_cp_reginfo[] = {
5544     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5545       .type = ARM_CP_IO,
5546       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5547       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5548       .writefn = hcr_write },
5549     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5550       .type = ARM_CP_ALIAS | ARM_CP_IO,
5551       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5552       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5553       .writefn = hcr_writelow },
5554     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5555       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5556       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5557     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5558       .type = ARM_CP_ALIAS,
5559       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5560       .access = PL2_RW,
5561       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5562     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5563       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5564       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5565     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5566       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5567       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5568     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5569       .type = ARM_CP_ALIAS,
5570       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5571       .access = PL2_RW,
5572       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5573     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5574       .type = ARM_CP_ALIAS,
5575       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5576       .access = PL2_RW,
5577       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5578     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5579       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5580       .access = PL2_RW, .writefn = vbar_write,
5581       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5582       .resetvalue = 0 },
5583     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5584       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5585       .access = PL3_RW, .type = ARM_CP_ALIAS,
5586       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5587     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5588       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5589       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5590       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5591       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5592     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5593       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5594       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5595       .resetvalue = 0 },
5596     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5597       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5598       .access = PL2_RW, .type = ARM_CP_ALIAS,
5599       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5600     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5601       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5602       .access = PL2_RW, .type = ARM_CP_CONST,
5603       .resetvalue = 0 },
5604     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5605     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5606       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5607       .access = PL2_RW, .type = ARM_CP_CONST,
5608       .resetvalue = 0 },
5609     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5610       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5611       .access = PL2_RW, .type = ARM_CP_CONST,
5612       .resetvalue = 0 },
5613     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5614       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5615       .access = PL2_RW, .type = ARM_CP_CONST,
5616       .resetvalue = 0 },
5617     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5618       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5619       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5620       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5621     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5622       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5623       .type = ARM_CP_ALIAS,
5624       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5625       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5626     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5627       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5628       .access = PL2_RW,
5629       /* no .writefn needed as this can't cause an ASID change */
5630       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5631     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5632       .cp = 15, .opc1 = 6, .crm = 2,
5633       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5634       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5635       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5636       .writefn = vttbr_write },
5637     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5638       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5639       .access = PL2_RW, .writefn = vttbr_write,
5640       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5641     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5642       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5643       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5644       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5645     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5646       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5647       .access = PL2_RW, .resetvalue = 0,
5648       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5649     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5650       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5651       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5652       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5653     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5654       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5655       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5656     { .name = "TLBIALLNSNH",
5657       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5658       .type = ARM_CP_NO_RAW, .access = PL2_W,
5659       .writefn = tlbiall_nsnh_write },
5660     { .name = "TLBIALLNSNHIS",
5661       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5662       .type = ARM_CP_NO_RAW, .access = PL2_W,
5663       .writefn = tlbiall_nsnh_is_write },
5664     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5665       .type = ARM_CP_NO_RAW, .access = PL2_W,
5666       .writefn = tlbiall_hyp_write },
5667     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5668       .type = ARM_CP_NO_RAW, .access = PL2_W,
5669       .writefn = tlbiall_hyp_is_write },
5670     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5671       .type = ARM_CP_NO_RAW, .access = PL2_W,
5672       .writefn = tlbimva_hyp_write },
5673     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5674       .type = ARM_CP_NO_RAW, .access = PL2_W,
5675       .writefn = tlbimva_hyp_is_write },
5676     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5677       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5678       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5679       .writefn = tlbi_aa64_alle2_write },
5680     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5681       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5682       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5683       .writefn = tlbi_aa64_vae2_write },
5684     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5685       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5686       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5687       .writefn = tlbi_aa64_vae2_write },
5688     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5689       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5690       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5691       .writefn = tlbi_aa64_alle2is_write },
5692     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5693       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5694       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5695       .writefn = tlbi_aa64_vae2is_write },
5696     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5697       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5698       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5699       .writefn = tlbi_aa64_vae2is_write },
5700 #ifndef CONFIG_USER_ONLY
5701     /* Unlike the other EL2-related AT operations, these must
5702      * UNDEF from EL3 if EL2 is not implemented, which is why we
5703      * define them here rather than with the rest of the AT ops.
5704      */
5705     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5706       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5707       .access = PL2_W, .accessfn = at_s1e2_access,
5708       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5709       .writefn = ats_write64 },
5710     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5711       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5712       .access = PL2_W, .accessfn = at_s1e2_access,
5713       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5714       .writefn = ats_write64 },
5715     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5716      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5717      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5718      * to behave as if SCR.NS was 1.
5719      */
5720     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5721       .access = PL2_W,
5722       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5723     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5724       .access = PL2_W,
5725       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5726     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5727       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5728       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5729        * reset values as IMPDEF. We choose to reset to 3 to comply with
5730        * both ARMv7 and ARMv8.
5731        */
5732       .access = PL2_RW, .resetvalue = 3,
5733       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5734     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5735       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5736       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5737       .writefn = gt_cntvoff_write,
5738       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5739     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5740       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5741       .writefn = gt_cntvoff_write,
5742       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5743     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5744       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5745       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5746       .type = ARM_CP_IO, .access = PL2_RW,
5747       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5748     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5749       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5750       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5751       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5752     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5753       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5754       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5755       .resetfn = gt_hyp_timer_reset,
5756       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5757     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5758       .type = ARM_CP_IO,
5759       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5760       .access = PL2_RW,
5761       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5762       .resetvalue = 0,
5763       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5764 #endif
5765     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5766       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5767       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5768       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5769     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5770       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5771       .access = PL2_RW,
5772       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5773     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5774       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5775       .access = PL2_RW,
5776       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5777 };
5778 
5779 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5780     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5781       .type = ARM_CP_ALIAS | ARM_CP_IO,
5782       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5783       .access = PL2_RW,
5784       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5785       .writefn = hcr_writehigh },
5786 };
5787 
5788 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5789                                   bool isread)
5790 {
5791     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5792         return CP_ACCESS_OK;
5793     }
5794     return CP_ACCESS_TRAP_UNCATEGORIZED;
5795 }
5796 
5797 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5798     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5799       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5800       .access = PL2_RW, .accessfn = sel2_access,
5801       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5802     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5803       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5804       .access = PL2_RW, .accessfn = sel2_access,
5805       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5806 };
5807 
5808 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5809                                    bool isread)
5810 {
5811     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5812      * At Secure EL1 it traps to EL3 or EL2.
5813      */
5814     if (arm_current_el(env) == 3) {
5815         return CP_ACCESS_OK;
5816     }
5817     if (arm_is_secure_below_el3(env)) {
5818         if (env->cp15.scr_el3 & SCR_EEL2) {
5819             return CP_ACCESS_TRAP_EL2;
5820         }
5821         return CP_ACCESS_TRAP_EL3;
5822     }
5823     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5824     if (isread) {
5825         return CP_ACCESS_OK;
5826     }
5827     return CP_ACCESS_TRAP_UNCATEGORIZED;
5828 }
5829 
5830 static const ARMCPRegInfo el3_cp_reginfo[] = {
5831     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5832       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5833       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5834       .resetfn = scr_reset, .writefn = scr_write },
5835     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5836       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5837       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5838       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5839       .writefn = scr_write },
5840     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5841       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5842       .access = PL3_RW, .resetvalue = 0,
5843       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5844     { .name = "SDER",
5845       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5846       .access = PL3_RW, .resetvalue = 0,
5847       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5848     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5849       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5850       .writefn = vbar_write, .resetvalue = 0,
5851       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5852     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5853       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5854       .access = PL3_RW, .resetvalue = 0,
5855       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5856     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5857       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5858       .access = PL3_RW,
5859       /* no .writefn needed as this can't cause an ASID change */
5860       .resetvalue = 0,
5861       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5862     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5863       .type = ARM_CP_ALIAS,
5864       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5865       .access = PL3_RW,
5866       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5867     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5868       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5869       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5870     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5871       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5872       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5873     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5874       .type = ARM_CP_ALIAS,
5875       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5876       .access = PL3_RW,
5877       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5878     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5879       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5880       .access = PL3_RW, .writefn = vbar_write,
5881       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5882       .resetvalue = 0 },
5883     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5884       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5885       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5886       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5887     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5888       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5889       .access = PL3_RW, .resetvalue = 0,
5890       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5891     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5892       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5893       .access = PL3_RW, .type = ARM_CP_CONST,
5894       .resetvalue = 0 },
5895     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5896       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5897       .access = PL3_RW, .type = ARM_CP_CONST,
5898       .resetvalue = 0 },
5899     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5900       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5901       .access = PL3_RW, .type = ARM_CP_CONST,
5902       .resetvalue = 0 },
5903     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5904       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5905       .access = PL3_W, .type = ARM_CP_NO_RAW,
5906       .writefn = tlbi_aa64_alle3is_write },
5907     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5908       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5909       .access = PL3_W, .type = ARM_CP_NO_RAW,
5910       .writefn = tlbi_aa64_vae3is_write },
5911     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5912       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5913       .access = PL3_W, .type = ARM_CP_NO_RAW,
5914       .writefn = tlbi_aa64_vae3is_write },
5915     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5916       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5917       .access = PL3_W, .type = ARM_CP_NO_RAW,
5918       .writefn = tlbi_aa64_alle3_write },
5919     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5920       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5921       .access = PL3_W, .type = ARM_CP_NO_RAW,
5922       .writefn = tlbi_aa64_vae3_write },
5923     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5924       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5925       .access = PL3_W, .type = ARM_CP_NO_RAW,
5926       .writefn = tlbi_aa64_vae3_write },
5927 };
5928 
5929 #ifndef CONFIG_USER_ONLY
5930 /* Test if system register redirection is to occur in the current state.  */
5931 static bool redirect_for_e2h(CPUARMState *env)
5932 {
5933     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5934 }
5935 
5936 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5937 {
5938     CPReadFn *readfn;
5939 
5940     if (redirect_for_e2h(env)) {
5941         /* Switch to the saved EL2 version of the register.  */
5942         ri = ri->opaque;
5943         readfn = ri->readfn;
5944     } else {
5945         readfn = ri->orig_readfn;
5946     }
5947     if (readfn == NULL) {
5948         readfn = raw_read;
5949     }
5950     return readfn(env, ri);
5951 }
5952 
5953 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5954                           uint64_t value)
5955 {
5956     CPWriteFn *writefn;
5957 
5958     if (redirect_for_e2h(env)) {
5959         /* Switch to the saved EL2 version of the register.  */
5960         ri = ri->opaque;
5961         writefn = ri->writefn;
5962     } else {
5963         writefn = ri->orig_writefn;
5964     }
5965     if (writefn == NULL) {
5966         writefn = raw_write;
5967     }
5968     writefn(env, ri, value);
5969 }
5970 
5971 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5972 {
5973     struct E2HAlias {
5974         uint32_t src_key, dst_key, new_key;
5975         const char *src_name, *dst_name, *new_name;
5976         bool (*feature)(const ARMISARegisters *id);
5977     };
5978 
5979 #define K(op0, op1, crn, crm, op2) \
5980     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5981 
5982     static const struct E2HAlias aliases[] = {
5983         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5984           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5985         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5986           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5987         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5988           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5989         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5990           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5991         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5992           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5993         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5994           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5995         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5996           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5997         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5998           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5999         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6000           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6001         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6002           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6003         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6004           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6005         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6006           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6007         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6008           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6009         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6010           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6011         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6012           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6013         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6014           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6015 
6016         /*
6017          * Note that redirection of ZCR is mentioned in the description
6018          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6019          * not in the summary table.
6020          */
6021         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6022           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6023         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6024           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6025 
6026         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6027           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6028 
6029         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6030           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6031           isar_feature_aa64_scxtnum },
6032 
6033         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6034         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6035     };
6036 #undef K
6037 
6038     size_t i;
6039 
6040     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6041         const struct E2HAlias *a = &aliases[i];
6042         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6043         bool ok;
6044 
6045         if (a->feature && !a->feature(&cpu->isar)) {
6046             continue;
6047         }
6048 
6049         src_reg = g_hash_table_lookup(cpu->cp_regs,
6050                                       (gpointer)(uintptr_t)a->src_key);
6051         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6052                                       (gpointer)(uintptr_t)a->dst_key);
6053         g_assert(src_reg != NULL);
6054         g_assert(dst_reg != NULL);
6055 
6056         /* Cross-compare names to detect typos in the keys.  */
6057         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6058         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6059 
6060         /* None of the core system registers use opaque; we will.  */
6061         g_assert(src_reg->opaque == NULL);
6062 
6063         /* Create alias before redirection so we dup the right data. */
6064         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6065 
6066         new_reg->name = a->new_name;
6067         new_reg->type |= ARM_CP_ALIAS;
6068         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6069         new_reg->access &= PL2_RW | PL3_RW;
6070 
6071         ok = g_hash_table_insert(cpu->cp_regs,
6072                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6073         g_assert(ok);
6074 
6075         src_reg->opaque = dst_reg;
6076         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6077         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6078         if (!src_reg->raw_readfn) {
6079             src_reg->raw_readfn = raw_read;
6080         }
6081         if (!src_reg->raw_writefn) {
6082             src_reg->raw_writefn = raw_write;
6083         }
6084         src_reg->readfn = el2_e2h_read;
6085         src_reg->writefn = el2_e2h_write;
6086     }
6087 }
6088 #endif
6089 
6090 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6091                                      bool isread)
6092 {
6093     int cur_el = arm_current_el(env);
6094 
6095     if (cur_el < 2) {
6096         uint64_t hcr = arm_hcr_el2_eff(env);
6097 
6098         if (cur_el == 0) {
6099             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6100                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6101                     return CP_ACCESS_TRAP_EL2;
6102                 }
6103             } else {
6104                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6105                     return CP_ACCESS_TRAP;
6106                 }
6107                 if (hcr & HCR_TID2) {
6108                     return CP_ACCESS_TRAP_EL2;
6109                 }
6110             }
6111         } else if (hcr & HCR_TID2) {
6112             return CP_ACCESS_TRAP_EL2;
6113         }
6114     }
6115 
6116     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6117         return CP_ACCESS_TRAP_EL2;
6118     }
6119 
6120     return CP_ACCESS_OK;
6121 }
6122 
6123 /*
6124  * Check for traps to RAS registers, which are controlled
6125  * by HCR_EL2.TERR and SCR_EL3.TERR.
6126  */
6127 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6128                                   bool isread)
6129 {
6130     int el = arm_current_el(env);
6131 
6132     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6133         return CP_ACCESS_TRAP_EL2;
6134     }
6135     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6136         return CP_ACCESS_TRAP_EL3;
6137     }
6138     return CP_ACCESS_OK;
6139 }
6140 
6141 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6142 {
6143     int el = arm_current_el(env);
6144 
6145     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6146         return env->cp15.vdisr_el2;
6147     }
6148     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6149         return 0; /* RAZ/WI */
6150     }
6151     return env->cp15.disr_el1;
6152 }
6153 
6154 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6155 {
6156     int el = arm_current_el(env);
6157 
6158     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6159         env->cp15.vdisr_el2 = val;
6160         return;
6161     }
6162     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6163         return; /* RAZ/WI */
6164     }
6165     env->cp15.disr_el1 = val;
6166 }
6167 
6168 /*
6169  * Minimal RAS implementation with no Error Records.
6170  * Which means that all of the Error Record registers:
6171  *   ERXADDR_EL1
6172  *   ERXCTLR_EL1
6173  *   ERXFR_EL1
6174  *   ERXMISC0_EL1
6175  *   ERXMISC1_EL1
6176  *   ERXMISC2_EL1
6177  *   ERXMISC3_EL1
6178  *   ERXPFGCDN_EL1  (RASv1p1)
6179  *   ERXPFGCTL_EL1  (RASv1p1)
6180  *   ERXPFGF_EL1    (RASv1p1)
6181  *   ERXSTATUS_EL1
6182  * and
6183  *   ERRSELR_EL1
6184  * may generate UNDEFINED, which is the effect we get by not
6185  * listing them at all.
6186  */
6187 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6188     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6189       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6190       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6191       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6192     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6193       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6194       .access = PL1_R, .accessfn = access_terr,
6195       .type = ARM_CP_CONST, .resetvalue = 0 },
6196     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6197       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6198       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6199     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6200       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6201       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6202 };
6203 
6204 /*
6205  * Return the exception level to which exceptions should be taken
6206  * via SVEAccessTrap.  This excludes the check for whether the exception
6207  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6208  * be found by testing 0 < fp_exception_el < sve_exception_el.
6209  *
6210  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6211  * pseudocode does *not* separate out the FP trap checks, but has them
6212  * all in one function.
6213  */
6214 int sve_exception_el(CPUARMState *env, int el)
6215 {
6216 #ifndef CONFIG_USER_ONLY
6217     if (el <= 1 && !el_is_in_host(env, el)) {
6218         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6219         case 1:
6220             if (el != 0) {
6221                 break;
6222             }
6223             /* fall through */
6224         case 0:
6225         case 2:
6226             return 1;
6227         }
6228     }
6229 
6230     if (el <= 2 && arm_is_el2_enabled(env)) {
6231         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6232         if (env->cp15.hcr_el2 & HCR_E2H) {
6233             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6234             case 1:
6235                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6236                     break;
6237                 }
6238                 /* fall through */
6239             case 0:
6240             case 2:
6241                 return 2;
6242             }
6243         } else {
6244             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6245                 return 2;
6246             }
6247         }
6248     }
6249 
6250     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6251     if (arm_feature(env, ARM_FEATURE_EL3)
6252         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6253         return 3;
6254     }
6255 #endif
6256     return 0;
6257 }
6258 
6259 /*
6260  * Return the exception level to which exceptions should be taken for SME.
6261  * C.f. the ARM pseudocode function CheckSMEAccess.
6262  */
6263 int sme_exception_el(CPUARMState *env, int el)
6264 {
6265 #ifndef CONFIG_USER_ONLY
6266     if (el <= 1 && !el_is_in_host(env, el)) {
6267         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6268         case 1:
6269             if (el != 0) {
6270                 break;
6271             }
6272             /* fall through */
6273         case 0:
6274         case 2:
6275             return 1;
6276         }
6277     }
6278 
6279     if (el <= 2 && arm_is_el2_enabled(env)) {
6280         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6281         if (env->cp15.hcr_el2 & HCR_E2H) {
6282             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6283             case 1:
6284                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6285                     break;
6286                 }
6287                 /* fall through */
6288             case 0:
6289             case 2:
6290                 return 2;
6291             }
6292         } else {
6293             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6294                 return 2;
6295             }
6296         }
6297     }
6298 
6299     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6300     if (arm_feature(env, ARM_FEATURE_EL3)
6301         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6302         return 3;
6303     }
6304 #endif
6305     return 0;
6306 }
6307 
6308 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6309 static bool sme_fa64(CPUARMState *env, int el)
6310 {
6311     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6312         return false;
6313     }
6314 
6315     if (el <= 1 && !el_is_in_host(env, el)) {
6316         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6317             return false;
6318         }
6319     }
6320     if (el <= 2 && arm_is_el2_enabled(env)) {
6321         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6322             return false;
6323         }
6324     }
6325     if (arm_feature(env, ARM_FEATURE_EL3)) {
6326         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6327             return false;
6328         }
6329     }
6330 
6331     return true;
6332 }
6333 
6334 /*
6335  * Given that SVE is enabled, return the vector length for EL.
6336  */
6337 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6338 {
6339     ARMCPU *cpu = env_archcpu(env);
6340     uint64_t *cr = env->vfp.zcr_el;
6341     uint32_t map = cpu->sve_vq.map;
6342     uint32_t len = ARM_MAX_VQ - 1;
6343 
6344     if (sm) {
6345         cr = env->vfp.smcr_el;
6346         map = cpu->sme_vq.map;
6347     }
6348 
6349     if (el <= 1 && !el_is_in_host(env, el)) {
6350         len = MIN(len, 0xf & (uint32_t)cr[1]);
6351     }
6352     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6353         len = MIN(len, 0xf & (uint32_t)cr[2]);
6354     }
6355     if (arm_feature(env, ARM_FEATURE_EL3)) {
6356         len = MIN(len, 0xf & (uint32_t)cr[3]);
6357     }
6358 
6359     map &= MAKE_64BIT_MASK(0, len + 1);
6360     if (map != 0) {
6361         return 31 - clz32(map);
6362     }
6363 
6364     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6365     assert(sm);
6366     return ctz32(cpu->sme_vq.map);
6367 }
6368 
6369 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6370 {
6371     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6372 }
6373 
6374 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6375                       uint64_t value)
6376 {
6377     int cur_el = arm_current_el(env);
6378     int old_len = sve_vqm1_for_el(env, cur_el);
6379     int new_len;
6380 
6381     /* Bits other than [3:0] are RAZ/WI.  */
6382     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6383     raw_write(env, ri, value & 0xf);
6384 
6385     /*
6386      * Because we arrived here, we know both FP and SVE are enabled;
6387      * otherwise we would have trapped access to the ZCR_ELn register.
6388      */
6389     new_len = sve_vqm1_for_el(env, cur_el);
6390     if (new_len < old_len) {
6391         aarch64_sve_narrow_vq(env, new_len + 1);
6392     }
6393 }
6394 
6395 static const ARMCPRegInfo zcr_reginfo[] = {
6396     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6397       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6398       .access = PL1_RW, .type = ARM_CP_SVE,
6399       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6400       .writefn = zcr_write, .raw_writefn = raw_write },
6401     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6402       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6403       .access = PL2_RW, .type = ARM_CP_SVE,
6404       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6405       .writefn = zcr_write, .raw_writefn = raw_write },
6406     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6407       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6408       .access = PL3_RW, .type = ARM_CP_SVE,
6409       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6410       .writefn = zcr_write, .raw_writefn = raw_write },
6411 };
6412 
6413 #ifdef TARGET_AARCH64
6414 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6415                                     bool isread)
6416 {
6417     int el = arm_current_el(env);
6418 
6419     if (el == 0) {
6420         uint64_t sctlr = arm_sctlr(env, el);
6421         if (!(sctlr & SCTLR_EnTP2)) {
6422             return CP_ACCESS_TRAP;
6423         }
6424     }
6425     /* TODO: FEAT_FGT */
6426     if (el < 3
6427         && arm_feature(env, ARM_FEATURE_EL3)
6428         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6429         return CP_ACCESS_TRAP_EL3;
6430     }
6431     return CP_ACCESS_OK;
6432 }
6433 
6434 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6435                                  bool isread)
6436 {
6437     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6438     if (arm_current_el(env) < 3
6439         && arm_feature(env, ARM_FEATURE_EL3)
6440         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6441         return CP_ACCESS_TRAP_EL3;
6442     }
6443     return CP_ACCESS_OK;
6444 }
6445 
6446 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6447                        uint64_t value)
6448 {
6449     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6450     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6451     arm_rebuild_hflags(env);
6452 }
6453 
6454 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6455                        uint64_t value)
6456 {
6457     int cur_el = arm_current_el(env);
6458     int old_len = sve_vqm1_for_el(env, cur_el);
6459     int new_len;
6460 
6461     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6462     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6463     raw_write(env, ri, value);
6464 
6465     /*
6466      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6467      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6468      * current values for simplicity.  But for QEMU internals, we must still
6469      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6470      * above aarch64_sve_narrow_vq.
6471      */
6472     new_len = sve_vqm1_for_el(env, cur_el);
6473     if (new_len < old_len) {
6474         aarch64_sve_narrow_vq(env, new_len + 1);
6475     }
6476 }
6477 
6478 static const ARMCPRegInfo sme_reginfo[] = {
6479     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6480       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6481       .access = PL0_RW, .accessfn = access_tpidr2,
6482       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6483     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6484       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6485       .access = PL0_RW, .type = ARM_CP_SME,
6486       .fieldoffset = offsetof(CPUARMState, svcr),
6487       .writefn = svcr_write, .raw_writefn = raw_write },
6488     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6489       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6490       .access = PL1_RW, .type = ARM_CP_SME,
6491       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6492       .writefn = smcr_write, .raw_writefn = raw_write },
6493     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6494       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6495       .access = PL2_RW, .type = ARM_CP_SME,
6496       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6497       .writefn = smcr_write, .raw_writefn = raw_write },
6498     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6499       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6500       .access = PL3_RW, .type = ARM_CP_SME,
6501       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6502       .writefn = smcr_write, .raw_writefn = raw_write },
6503     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6504       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6505       .access = PL1_R, .accessfn = access_aa64_tid1,
6506       /*
6507        * IMPLEMENTOR = 0 (software)
6508        * REVISION    = 0 (implementation defined)
6509        * SMPS        = 0 (no streaming execution priority in QEMU)
6510        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6511        */
6512       .type = ARM_CP_CONST, .resetvalue = 0, },
6513     /*
6514      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6515      */
6516     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6517       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6518       .access = PL1_RW, .accessfn = access_esm,
6519       .type = ARM_CP_CONST, .resetvalue = 0 },
6520     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6521       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6522       .access = PL2_RW, .accessfn = access_esm,
6523       .type = ARM_CP_CONST, .resetvalue = 0 },
6524 };
6525 #endif /* TARGET_AARCH64 */
6526 
6527 static void define_pmu_regs(ARMCPU *cpu)
6528 {
6529     /*
6530      * v7 performance monitor control register: same implementor
6531      * field as main ID register, and we implement four counters in
6532      * addition to the cycle count register.
6533      */
6534     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6535     ARMCPRegInfo pmcr = {
6536         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6537         .access = PL0_RW,
6538         .type = ARM_CP_IO | ARM_CP_ALIAS,
6539         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6540         .accessfn = pmreg_access, .writefn = pmcr_write,
6541         .raw_writefn = raw_write,
6542     };
6543     ARMCPRegInfo pmcr64 = {
6544         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6545         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6546         .access = PL0_RW, .accessfn = pmreg_access,
6547         .type = ARM_CP_IO,
6548         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6549         .resetvalue = cpu->isar.reset_pmcr_el0,
6550         .writefn = pmcr_write, .raw_writefn = raw_write,
6551     };
6552 
6553     define_one_arm_cp_reg(cpu, &pmcr);
6554     define_one_arm_cp_reg(cpu, &pmcr64);
6555     for (i = 0; i < pmcrn; i++) {
6556         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6557         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6558         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6559         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6560         ARMCPRegInfo pmev_regs[] = {
6561             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6562               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6563               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6564               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6565               .accessfn = pmreg_access_xevcntr },
6566             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6567               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6568               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6569               .type = ARM_CP_IO,
6570               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6571               .raw_readfn = pmevcntr_rawread,
6572               .raw_writefn = pmevcntr_rawwrite },
6573             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6574               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6575               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6576               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6577               .accessfn = pmreg_access },
6578             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6579               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6580               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6581               .type = ARM_CP_IO,
6582               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6583               .raw_writefn = pmevtyper_rawwrite },
6584         };
6585         define_arm_cp_regs(cpu, pmev_regs);
6586         g_free(pmevcntr_name);
6587         g_free(pmevcntr_el0_name);
6588         g_free(pmevtyper_name);
6589         g_free(pmevtyper_el0_name);
6590     }
6591     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6592         ARMCPRegInfo v81_pmu_regs[] = {
6593             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6594               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6595               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6596               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6597             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6598               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6599               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6600               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6601         };
6602         define_arm_cp_regs(cpu, v81_pmu_regs);
6603     }
6604     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6605         static const ARMCPRegInfo v84_pmmir = {
6606             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6607             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6608             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6609             .resetvalue = 0
6610         };
6611         define_one_arm_cp_reg(cpu, &v84_pmmir);
6612     }
6613 }
6614 
6615 /* We don't know until after realize whether there's a GICv3
6616  * attached, and that is what registers the gicv3 sysregs.
6617  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6618  * at runtime.
6619  */
6620 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6621 {
6622     ARMCPU *cpu = env_archcpu(env);
6623     uint64_t pfr1 = cpu->isar.id_pfr1;
6624 
6625     if (env->gicv3state) {
6626         pfr1 |= 1 << 28;
6627     }
6628     return pfr1;
6629 }
6630 
6631 #ifndef CONFIG_USER_ONLY
6632 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6633 {
6634     ARMCPU *cpu = env_archcpu(env);
6635     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6636 
6637     if (env->gicv3state) {
6638         pfr0 |= 1 << 24;
6639     }
6640     return pfr0;
6641 }
6642 #endif
6643 
6644 /* Shared logic between LORID and the rest of the LOR* registers.
6645  * Secure state exclusion has already been dealt with.
6646  */
6647 static CPAccessResult access_lor_ns(CPUARMState *env,
6648                                     const ARMCPRegInfo *ri, bool isread)
6649 {
6650     int el = arm_current_el(env);
6651 
6652     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6653         return CP_ACCESS_TRAP_EL2;
6654     }
6655     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6656         return CP_ACCESS_TRAP_EL3;
6657     }
6658     return CP_ACCESS_OK;
6659 }
6660 
6661 static CPAccessResult access_lor_other(CPUARMState *env,
6662                                        const ARMCPRegInfo *ri, bool isread)
6663 {
6664     if (arm_is_secure_below_el3(env)) {
6665         /* Access denied in secure mode.  */
6666         return CP_ACCESS_TRAP;
6667     }
6668     return access_lor_ns(env, ri, isread);
6669 }
6670 
6671 /*
6672  * A trivial implementation of ARMv8.1-LOR leaves all of these
6673  * registers fixed at 0, which indicates that there are zero
6674  * supported Limited Ordering regions.
6675  */
6676 static const ARMCPRegInfo lor_reginfo[] = {
6677     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6678       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6679       .access = PL1_RW, .accessfn = access_lor_other,
6680       .type = ARM_CP_CONST, .resetvalue = 0 },
6681     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6682       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6683       .access = PL1_RW, .accessfn = access_lor_other,
6684       .type = ARM_CP_CONST, .resetvalue = 0 },
6685     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6686       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6687       .access = PL1_RW, .accessfn = access_lor_other,
6688       .type = ARM_CP_CONST, .resetvalue = 0 },
6689     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6690       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6691       .access = PL1_RW, .accessfn = access_lor_other,
6692       .type = ARM_CP_CONST, .resetvalue = 0 },
6693     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6694       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6695       .access = PL1_R, .accessfn = access_lor_ns,
6696       .type = ARM_CP_CONST, .resetvalue = 0 },
6697 };
6698 
6699 #ifdef TARGET_AARCH64
6700 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6701                                    bool isread)
6702 {
6703     int el = arm_current_el(env);
6704 
6705     if (el < 2 &&
6706         arm_is_el2_enabled(env) &&
6707         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6708         return CP_ACCESS_TRAP_EL2;
6709     }
6710     if (el < 3 &&
6711         arm_feature(env, ARM_FEATURE_EL3) &&
6712         !(env->cp15.scr_el3 & SCR_APK)) {
6713         return CP_ACCESS_TRAP_EL3;
6714     }
6715     return CP_ACCESS_OK;
6716 }
6717 
6718 static const ARMCPRegInfo pauth_reginfo[] = {
6719     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6720       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6721       .access = PL1_RW, .accessfn = access_pauth,
6722       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6723     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6724       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6725       .access = PL1_RW, .accessfn = access_pauth,
6726       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6727     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6728       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6729       .access = PL1_RW, .accessfn = access_pauth,
6730       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6731     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6732       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6733       .access = PL1_RW, .accessfn = access_pauth,
6734       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6735     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6736       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6737       .access = PL1_RW, .accessfn = access_pauth,
6738       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6739     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6740       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6741       .access = PL1_RW, .accessfn = access_pauth,
6742       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6743     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6744       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6745       .access = PL1_RW, .accessfn = access_pauth,
6746       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6747     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6748       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6749       .access = PL1_RW, .accessfn = access_pauth,
6750       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6751     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6752       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6753       .access = PL1_RW, .accessfn = access_pauth,
6754       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6755     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6756       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6757       .access = PL1_RW, .accessfn = access_pauth,
6758       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6759 };
6760 
6761 static const ARMCPRegInfo tlbirange_reginfo[] = {
6762     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6763       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6764       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
6765       .writefn = tlbi_aa64_rvae1is_write },
6766     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6767       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6768       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
6769       .writefn = tlbi_aa64_rvae1is_write },
6770    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6771       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6772       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
6773       .writefn = tlbi_aa64_rvae1is_write },
6774     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6775       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6776       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
6777       .writefn = tlbi_aa64_rvae1is_write },
6778     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6779       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6780       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6781       .writefn = tlbi_aa64_rvae1is_write },
6782     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6784       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6785       .writefn = tlbi_aa64_rvae1is_write },
6786    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6787       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6788       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6789       .writefn = tlbi_aa64_rvae1is_write },
6790     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6791       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6792       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6793       .writefn = tlbi_aa64_rvae1is_write },
6794     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6795       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6796       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
6797       .writefn = tlbi_aa64_rvae1_write },
6798     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6799       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6800       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
6801       .writefn = tlbi_aa64_rvae1_write },
6802    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6804       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
6805       .writefn = tlbi_aa64_rvae1_write },
6806     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6807       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6808       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
6809       .writefn = tlbi_aa64_rvae1_write },
6810     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6811       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6812       .access = PL2_W, .type = ARM_CP_NO_RAW,
6813       .writefn = tlbi_aa64_ripas2e1is_write },
6814     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6815       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6816       .access = PL2_W, .type = ARM_CP_NO_RAW,
6817       .writefn = tlbi_aa64_ripas2e1is_write },
6818     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6819       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6820       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6821       .writefn = tlbi_aa64_rvae2is_write },
6822    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6824       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6825       .writefn = tlbi_aa64_rvae2is_write },
6826     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6827       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6828       .access = PL2_W, .type = ARM_CP_NO_RAW,
6829       .writefn = tlbi_aa64_ripas2e1_write },
6830     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6831       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6832       .access = PL2_W, .type = ARM_CP_NO_RAW,
6833       .writefn = tlbi_aa64_ripas2e1_write },
6834    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6835       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6836       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6837       .writefn = tlbi_aa64_rvae2is_write },
6838    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6839       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6840       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6841       .writefn = tlbi_aa64_rvae2is_write },
6842     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6843       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6844       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6845       .writefn = tlbi_aa64_rvae2_write },
6846    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6847       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6848       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6849       .writefn = tlbi_aa64_rvae2_write },
6850    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6851       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6852       .access = PL3_W, .type = ARM_CP_NO_RAW,
6853       .writefn = tlbi_aa64_rvae3is_write },
6854    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6855       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6856       .access = PL3_W, .type = ARM_CP_NO_RAW,
6857       .writefn = tlbi_aa64_rvae3is_write },
6858    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6859       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6860       .access = PL3_W, .type = ARM_CP_NO_RAW,
6861       .writefn = tlbi_aa64_rvae3is_write },
6862    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6863       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6864       .access = PL3_W, .type = ARM_CP_NO_RAW,
6865       .writefn = tlbi_aa64_rvae3is_write },
6866    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6867       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6868       .access = PL3_W, .type = ARM_CP_NO_RAW,
6869       .writefn = tlbi_aa64_rvae3_write },
6870    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6871       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6872       .access = PL3_W, .type = ARM_CP_NO_RAW,
6873       .writefn = tlbi_aa64_rvae3_write },
6874 };
6875 
6876 static const ARMCPRegInfo tlbios_reginfo[] = {
6877     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6878       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6879       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6880       .writefn = tlbi_aa64_vmalle1is_write },
6881     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6882       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6883       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6884       .writefn = tlbi_aa64_vae1is_write },
6885     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6886       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6887       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6888       .writefn = tlbi_aa64_vmalle1is_write },
6889     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6890       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6891       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6892       .writefn = tlbi_aa64_vae1is_write },
6893     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6894       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6895       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6896       .writefn = tlbi_aa64_vae1is_write },
6897     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6898       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6899       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
6900       .writefn = tlbi_aa64_vae1is_write },
6901     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6902       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6903       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6904       .writefn = tlbi_aa64_alle2is_write },
6905     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6906       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6907       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6908       .writefn = tlbi_aa64_vae2is_write },
6909    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6910       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6911       .access = PL2_W, .type = ARM_CP_NO_RAW,
6912       .writefn = tlbi_aa64_alle1is_write },
6913     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6914       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6915       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6916       .writefn = tlbi_aa64_vae2is_write },
6917     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6918       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6919       .access = PL2_W, .type = ARM_CP_NO_RAW,
6920       .writefn = tlbi_aa64_alle1is_write },
6921     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6922       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6923       .access = PL2_W, .type = ARM_CP_NOP },
6924     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6925       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6926       .access = PL2_W, .type = ARM_CP_NOP },
6927     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6928       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6929       .access = PL2_W, .type = ARM_CP_NOP },
6930     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6931       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6932       .access = PL2_W, .type = ARM_CP_NOP },
6933     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6934       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6935       .access = PL3_W, .type = ARM_CP_NO_RAW,
6936       .writefn = tlbi_aa64_alle3is_write },
6937     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6938       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6939       .access = PL3_W, .type = ARM_CP_NO_RAW,
6940       .writefn = tlbi_aa64_vae3is_write },
6941     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6942       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6943       .access = PL3_W, .type = ARM_CP_NO_RAW,
6944       .writefn = tlbi_aa64_vae3is_write },
6945 };
6946 
6947 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6948 {
6949     Error *err = NULL;
6950     uint64_t ret;
6951 
6952     /* Success sets NZCV = 0000.  */
6953     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6954 
6955     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6956         /*
6957          * ??? Failed, for unknown reasons in the crypto subsystem.
6958          * The best we can do is log the reason and return the
6959          * timed-out indication to the guest.  There is no reason
6960          * we know to expect this failure to be transitory, so the
6961          * guest may well hang retrying the operation.
6962          */
6963         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6964                       ri->name, error_get_pretty(err));
6965         error_free(err);
6966 
6967         env->ZF = 0; /* NZCF = 0100 */
6968         return 0;
6969     }
6970     return ret;
6971 }
6972 
6973 /* We do not support re-seeding, so the two registers operate the same.  */
6974 static const ARMCPRegInfo rndr_reginfo[] = {
6975     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6976       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6977       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6978       .access = PL0_R, .readfn = rndr_readfn },
6979     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6980       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6981       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6982       .access = PL0_R, .readfn = rndr_readfn },
6983 };
6984 
6985 #ifndef CONFIG_USER_ONLY
6986 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6987                           uint64_t value)
6988 {
6989     ARMCPU *cpu = env_archcpu(env);
6990     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6991     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6992     uint64_t vaddr_in = (uint64_t) value;
6993     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6994     void *haddr;
6995     int mem_idx = cpu_mmu_index(env, false);
6996 
6997     /* This won't be crossing page boundaries */
6998     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6999     if (haddr) {
7000 
7001         ram_addr_t offset;
7002         MemoryRegion *mr;
7003 
7004         /* RCU lock is already being held */
7005         mr = memory_region_from_host(haddr, &offset);
7006 
7007         if (mr) {
7008             memory_region_writeback(mr, offset, dline_size);
7009         }
7010     }
7011 }
7012 
7013 static const ARMCPRegInfo dcpop_reg[] = {
7014     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7015       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7016       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7017       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7018 };
7019 
7020 static const ARMCPRegInfo dcpodp_reg[] = {
7021     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7022       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7023       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7024       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7025 };
7026 #endif /*CONFIG_USER_ONLY*/
7027 
7028 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7029                                        bool isread)
7030 {
7031     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7032         return CP_ACCESS_TRAP_EL2;
7033     }
7034 
7035     return CP_ACCESS_OK;
7036 }
7037 
7038 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7039                                  bool isread)
7040 {
7041     int el = arm_current_el(env);
7042 
7043     if (el < 2 && arm_is_el2_enabled(env)) {
7044         uint64_t hcr = arm_hcr_el2_eff(env);
7045         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7046             return CP_ACCESS_TRAP_EL2;
7047         }
7048     }
7049     if (el < 3 &&
7050         arm_feature(env, ARM_FEATURE_EL3) &&
7051         !(env->cp15.scr_el3 & SCR_ATA)) {
7052         return CP_ACCESS_TRAP_EL3;
7053     }
7054     return CP_ACCESS_OK;
7055 }
7056 
7057 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7058 {
7059     return env->pstate & PSTATE_TCO;
7060 }
7061 
7062 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7063 {
7064     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7065 }
7066 
7067 static const ARMCPRegInfo mte_reginfo[] = {
7068     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7069       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7070       .access = PL1_RW, .accessfn = access_mte,
7071       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7072     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7073       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7074       .access = PL1_RW, .accessfn = access_mte,
7075       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7076     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7078       .access = PL2_RW, .accessfn = access_mte,
7079       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7080     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7081       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7082       .access = PL3_RW,
7083       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7084     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7085       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7086       .access = PL1_RW, .accessfn = access_mte,
7087       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7088     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7089       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7090       .access = PL1_RW, .accessfn = access_mte,
7091       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7092     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7093       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7094       .access = PL1_R, .accessfn = access_aa64_tid5,
7095       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7096     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7097       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7098       .type = ARM_CP_NO_RAW,
7099       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7100     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7101       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7102       .type = ARM_CP_NOP, .access = PL1_W,
7103       .accessfn = aa64_cacheop_poc_access },
7104     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7105       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7106       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7107     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7108       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7109       .type = ARM_CP_NOP, .access = PL1_W,
7110       .accessfn = aa64_cacheop_poc_access },
7111     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7113       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7114     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7115       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7116       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7117     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7118       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7119       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7120     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7121       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7122       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7123     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7125       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7126 };
7127 
7128 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7129     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7130       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7131       .type = ARM_CP_CONST, .access = PL0_RW, },
7132 };
7133 
7134 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7135     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7136       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7137       .type = ARM_CP_NOP, .access = PL0_W,
7138       .accessfn = aa64_cacheop_poc_access },
7139     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7141       .type = ARM_CP_NOP, .access = PL0_W,
7142       .accessfn = aa64_cacheop_poc_access },
7143     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7144       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7145       .type = ARM_CP_NOP, .access = PL0_W,
7146       .accessfn = aa64_cacheop_poc_access },
7147     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7148       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7149       .type = ARM_CP_NOP, .access = PL0_W,
7150       .accessfn = aa64_cacheop_poc_access },
7151     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7152       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7153       .type = ARM_CP_NOP, .access = PL0_W,
7154       .accessfn = aa64_cacheop_poc_access },
7155     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7156       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7157       .type = ARM_CP_NOP, .access = PL0_W,
7158       .accessfn = aa64_cacheop_poc_access },
7159     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7160       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7161       .type = ARM_CP_NOP, .access = PL0_W,
7162       .accessfn = aa64_cacheop_poc_access },
7163     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7164       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7165       .type = ARM_CP_NOP, .access = PL0_W,
7166       .accessfn = aa64_cacheop_poc_access },
7167     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7168       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7169       .access = PL0_W, .type = ARM_CP_DC_GVA,
7170 #ifndef CONFIG_USER_ONLY
7171       /* Avoid overhead of an access check that always passes in user-mode */
7172       .accessfn = aa64_zva_access,
7173 #endif
7174     },
7175     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7177       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7178 #ifndef CONFIG_USER_ONLY
7179       /* Avoid overhead of an access check that always passes in user-mode */
7180       .accessfn = aa64_zva_access,
7181 #endif
7182     },
7183 };
7184 
7185 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7186                                      bool isread)
7187 {
7188     uint64_t hcr = arm_hcr_el2_eff(env);
7189     int el = arm_current_el(env);
7190 
7191     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7192         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7193             if (hcr & HCR_TGE) {
7194                 return CP_ACCESS_TRAP_EL2;
7195             }
7196             return CP_ACCESS_TRAP;
7197         }
7198     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7199         return CP_ACCESS_TRAP_EL2;
7200     }
7201     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7202         return CP_ACCESS_TRAP_EL2;
7203     }
7204     if (el < 3
7205         && arm_feature(env, ARM_FEATURE_EL3)
7206         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7207         return CP_ACCESS_TRAP_EL3;
7208     }
7209     return CP_ACCESS_OK;
7210 }
7211 
7212 static const ARMCPRegInfo scxtnum_reginfo[] = {
7213     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7214       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7215       .access = PL0_RW, .accessfn = access_scxtnum,
7216       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7217     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7218       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7219       .access = PL1_RW, .accessfn = access_scxtnum,
7220       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7221     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7222       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7223       .access = PL2_RW, .accessfn = access_scxtnum,
7224       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7225     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7226       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7227       .access = PL3_RW,
7228       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7229 };
7230 #endif /* TARGET_AARCH64 */
7231 
7232 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7233                                      bool isread)
7234 {
7235     int el = arm_current_el(env);
7236 
7237     if (el == 0) {
7238         uint64_t sctlr = arm_sctlr(env, el);
7239         if (!(sctlr & SCTLR_EnRCTX)) {
7240             return CP_ACCESS_TRAP;
7241         }
7242     } else if (el == 1) {
7243         uint64_t hcr = arm_hcr_el2_eff(env);
7244         if (hcr & HCR_NV) {
7245             return CP_ACCESS_TRAP_EL2;
7246         }
7247     }
7248     return CP_ACCESS_OK;
7249 }
7250 
7251 static const ARMCPRegInfo predinv_reginfo[] = {
7252     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7253       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7254       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7255     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7256       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7257       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7258     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7259       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7260       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7261     /*
7262      * Note the AArch32 opcodes have a different OPC1.
7263      */
7264     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7265       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7266       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7267     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7268       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7269       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7270     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7271       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7272       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7273 };
7274 
7275 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7276 {
7277     /* Read the high 32 bits of the current CCSIDR */
7278     return extract64(ccsidr_read(env, ri), 32, 32);
7279 }
7280 
7281 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7282     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7283       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7284       .access = PL1_R,
7285       .accessfn = access_tid4,
7286       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7287 };
7288 
7289 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7290                                        bool isread)
7291 {
7292     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7293         return CP_ACCESS_TRAP_EL2;
7294     }
7295 
7296     return CP_ACCESS_OK;
7297 }
7298 
7299 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7300                                        bool isread)
7301 {
7302     if (arm_feature(env, ARM_FEATURE_V8)) {
7303         return access_aa64_tid3(env, ri, isread);
7304     }
7305 
7306     return CP_ACCESS_OK;
7307 }
7308 
7309 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7310                                      bool isread)
7311 {
7312     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7313         return CP_ACCESS_TRAP_EL2;
7314     }
7315 
7316     return CP_ACCESS_OK;
7317 }
7318 
7319 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7320                                         const ARMCPRegInfo *ri, bool isread)
7321 {
7322     /*
7323      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7324      * in v7A, not in v8A.
7325      */
7326     if (!arm_feature(env, ARM_FEATURE_V8) &&
7327         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7328         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7329         return CP_ACCESS_TRAP_EL2;
7330     }
7331     return CP_ACCESS_OK;
7332 }
7333 
7334 static const ARMCPRegInfo jazelle_regs[] = {
7335     { .name = "JIDR",
7336       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7337       .access = PL1_R, .accessfn = access_jazelle,
7338       .type = ARM_CP_CONST, .resetvalue = 0 },
7339     { .name = "JOSCR",
7340       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7341       .accessfn = access_joscr_jmcr,
7342       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7343     { .name = "JMCR",
7344       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7345       .accessfn = access_joscr_jmcr,
7346       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7347 };
7348 
7349 static const ARMCPRegInfo contextidr_el2 = {
7350     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7351     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7352     .access = PL2_RW,
7353     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7354 };
7355 
7356 static const ARMCPRegInfo vhe_reginfo[] = {
7357     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7358       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7359       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7360       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7361 #ifndef CONFIG_USER_ONLY
7362     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7363       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7364       .fieldoffset =
7365         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7366       .type = ARM_CP_IO, .access = PL2_RW,
7367       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7368     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7369       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7370       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7371       .resetfn = gt_hv_timer_reset,
7372       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7373     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7374       .type = ARM_CP_IO,
7375       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7376       .access = PL2_RW,
7377       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7378       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7379     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7380       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7381       .type = ARM_CP_IO | ARM_CP_ALIAS,
7382       .access = PL2_RW, .accessfn = e2h_access,
7383       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7384       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7385     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7386       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7387       .type = ARM_CP_IO | ARM_CP_ALIAS,
7388       .access = PL2_RW, .accessfn = e2h_access,
7389       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7390       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7391     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7392       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7393       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7394       .access = PL2_RW, .accessfn = e2h_access,
7395       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7396     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7397       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7398       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7399       .access = PL2_RW, .accessfn = e2h_access,
7400       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7401     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7402       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7403       .type = ARM_CP_IO | ARM_CP_ALIAS,
7404       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7405       .access = PL2_RW, .accessfn = e2h_access,
7406       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7407     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7408       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7409       .type = ARM_CP_IO | ARM_CP_ALIAS,
7410       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7411       .access = PL2_RW, .accessfn = e2h_access,
7412       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7413 #endif
7414 };
7415 
7416 #ifndef CONFIG_USER_ONLY
7417 static const ARMCPRegInfo ats1e1_reginfo[] = {
7418     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7419       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7420       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7421       .writefn = ats_write64 },
7422     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7423       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7424       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7425       .writefn = ats_write64 },
7426 };
7427 
7428 static const ARMCPRegInfo ats1cp_reginfo[] = {
7429     { .name = "ATS1CPRP",
7430       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7431       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7432       .writefn = ats_write },
7433     { .name = "ATS1CPWP",
7434       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7435       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7436       .writefn = ats_write },
7437 };
7438 #endif
7439 
7440 /*
7441  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7442  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7443  * is non-zero, which is never for ARMv7, optionally in ARMv8
7444  * and mandatorily for ARMv8.2 and up.
7445  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7446  * implementation is RAZ/WI we can ignore this detail, as we
7447  * do for ACTLR.
7448  */
7449 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7450     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7451       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7452       .access = PL1_RW, .accessfn = access_tacr,
7453       .type = ARM_CP_CONST, .resetvalue = 0 },
7454     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7455       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7456       .access = PL2_RW, .type = ARM_CP_CONST,
7457       .resetvalue = 0 },
7458 };
7459 
7460 void register_cp_regs_for_features(ARMCPU *cpu)
7461 {
7462     /* Register all the coprocessor registers based on feature bits */
7463     CPUARMState *env = &cpu->env;
7464     if (arm_feature(env, ARM_FEATURE_M)) {
7465         /* M profile has no coprocessor registers */
7466         return;
7467     }
7468 
7469     define_arm_cp_regs(cpu, cp_reginfo);
7470     if (!arm_feature(env, ARM_FEATURE_V8)) {
7471         /* Must go early as it is full of wildcards that may be
7472          * overridden by later definitions.
7473          */
7474         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7475     }
7476 
7477     if (arm_feature(env, ARM_FEATURE_V6)) {
7478         /* The ID registers all have impdef reset values */
7479         ARMCPRegInfo v6_idregs[] = {
7480             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7481               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7482               .access = PL1_R, .type = ARM_CP_CONST,
7483               .accessfn = access_aa32_tid3,
7484               .resetvalue = cpu->isar.id_pfr0 },
7485             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7486              * the value of the GIC field until after we define these regs.
7487              */
7488             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7489               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7490               .access = PL1_R, .type = ARM_CP_NO_RAW,
7491               .accessfn = access_aa32_tid3,
7492               .readfn = id_pfr1_read,
7493               .writefn = arm_cp_write_ignore },
7494             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7495               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7496               .access = PL1_R, .type = ARM_CP_CONST,
7497               .accessfn = access_aa32_tid3,
7498               .resetvalue = cpu->isar.id_dfr0 },
7499             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7500               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7501               .access = PL1_R, .type = ARM_CP_CONST,
7502               .accessfn = access_aa32_tid3,
7503               .resetvalue = cpu->id_afr0 },
7504             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7505               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7506               .access = PL1_R, .type = ARM_CP_CONST,
7507               .accessfn = access_aa32_tid3,
7508               .resetvalue = cpu->isar.id_mmfr0 },
7509             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7510               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7511               .access = PL1_R, .type = ARM_CP_CONST,
7512               .accessfn = access_aa32_tid3,
7513               .resetvalue = cpu->isar.id_mmfr1 },
7514             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7515               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7516               .access = PL1_R, .type = ARM_CP_CONST,
7517               .accessfn = access_aa32_tid3,
7518               .resetvalue = cpu->isar.id_mmfr2 },
7519             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7520               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7521               .access = PL1_R, .type = ARM_CP_CONST,
7522               .accessfn = access_aa32_tid3,
7523               .resetvalue = cpu->isar.id_mmfr3 },
7524             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7525               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7526               .access = PL1_R, .type = ARM_CP_CONST,
7527               .accessfn = access_aa32_tid3,
7528               .resetvalue = cpu->isar.id_isar0 },
7529             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7530               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7531               .access = PL1_R, .type = ARM_CP_CONST,
7532               .accessfn = access_aa32_tid3,
7533               .resetvalue = cpu->isar.id_isar1 },
7534             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7535               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7536               .access = PL1_R, .type = ARM_CP_CONST,
7537               .accessfn = access_aa32_tid3,
7538               .resetvalue = cpu->isar.id_isar2 },
7539             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7540               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7541               .access = PL1_R, .type = ARM_CP_CONST,
7542               .accessfn = access_aa32_tid3,
7543               .resetvalue = cpu->isar.id_isar3 },
7544             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7545               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7546               .access = PL1_R, .type = ARM_CP_CONST,
7547               .accessfn = access_aa32_tid3,
7548               .resetvalue = cpu->isar.id_isar4 },
7549             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7550               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7551               .access = PL1_R, .type = ARM_CP_CONST,
7552               .accessfn = access_aa32_tid3,
7553               .resetvalue = cpu->isar.id_isar5 },
7554             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7555               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7556               .access = PL1_R, .type = ARM_CP_CONST,
7557               .accessfn = access_aa32_tid3,
7558               .resetvalue = cpu->isar.id_mmfr4 },
7559             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7560               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7561               .access = PL1_R, .type = ARM_CP_CONST,
7562               .accessfn = access_aa32_tid3,
7563               .resetvalue = cpu->isar.id_isar6 },
7564         };
7565         define_arm_cp_regs(cpu, v6_idregs);
7566         define_arm_cp_regs(cpu, v6_cp_reginfo);
7567     } else {
7568         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7569     }
7570     if (arm_feature(env, ARM_FEATURE_V6K)) {
7571         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7572     }
7573     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7574         !arm_feature(env, ARM_FEATURE_PMSA)) {
7575         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7576     }
7577     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7578         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7579     }
7580     if (arm_feature(env, ARM_FEATURE_V7)) {
7581         ARMCPRegInfo clidr = {
7582             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7583             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7584             .access = PL1_R, .type = ARM_CP_CONST,
7585             .accessfn = access_tid4,
7586             .resetvalue = cpu->clidr
7587         };
7588         define_one_arm_cp_reg(cpu, &clidr);
7589         define_arm_cp_regs(cpu, v7_cp_reginfo);
7590         define_debug_regs(cpu);
7591         define_pmu_regs(cpu);
7592     } else {
7593         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7594     }
7595     if (arm_feature(env, ARM_FEATURE_V8)) {
7596         /*
7597          * v8 ID registers, which all have impdef reset values.
7598          * Note that within the ID register ranges the unused slots
7599          * must all RAZ, not UNDEF; future architecture versions may
7600          * define new registers here.
7601          * ID registers which are AArch64 views of the AArch32 ID registers
7602          * which already existed in v6 and v7 are handled elsewhere,
7603          * in v6_idregs[].
7604          */
7605         int i;
7606         ARMCPRegInfo v8_idregs[] = {
7607             /*
7608              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7609              * emulation because we don't know the right value for the
7610              * GIC field until after we define these regs.
7611              */
7612             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7613               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7614               .access = PL1_R,
7615 #ifdef CONFIG_USER_ONLY
7616               .type = ARM_CP_CONST,
7617               .resetvalue = cpu->isar.id_aa64pfr0
7618 #else
7619               .type = ARM_CP_NO_RAW,
7620               .accessfn = access_aa64_tid3,
7621               .readfn = id_aa64pfr0_read,
7622               .writefn = arm_cp_write_ignore
7623 #endif
7624             },
7625             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7626               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7627               .access = PL1_R, .type = ARM_CP_CONST,
7628               .accessfn = access_aa64_tid3,
7629               .resetvalue = cpu->isar.id_aa64pfr1},
7630             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7631               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7632               .access = PL1_R, .type = ARM_CP_CONST,
7633               .accessfn = access_aa64_tid3,
7634               .resetvalue = 0 },
7635             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7636               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7637               .access = PL1_R, .type = ARM_CP_CONST,
7638               .accessfn = access_aa64_tid3,
7639               .resetvalue = 0 },
7640             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7641               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7642               .access = PL1_R, .type = ARM_CP_CONST,
7643               .accessfn = access_aa64_tid3,
7644               .resetvalue = cpu->isar.id_aa64zfr0 },
7645             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7646               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7647               .access = PL1_R, .type = ARM_CP_CONST,
7648               .accessfn = access_aa64_tid3,
7649               .resetvalue = cpu->isar.id_aa64smfr0 },
7650             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7651               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7652               .access = PL1_R, .type = ARM_CP_CONST,
7653               .accessfn = access_aa64_tid3,
7654               .resetvalue = 0 },
7655             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7656               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7657               .access = PL1_R, .type = ARM_CP_CONST,
7658               .accessfn = access_aa64_tid3,
7659               .resetvalue = 0 },
7660             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7661               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7662               .access = PL1_R, .type = ARM_CP_CONST,
7663               .accessfn = access_aa64_tid3,
7664               .resetvalue = cpu->isar.id_aa64dfr0 },
7665             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7666               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7667               .access = PL1_R, .type = ARM_CP_CONST,
7668               .accessfn = access_aa64_tid3,
7669               .resetvalue = cpu->isar.id_aa64dfr1 },
7670             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7671               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7672               .access = PL1_R, .type = ARM_CP_CONST,
7673               .accessfn = access_aa64_tid3,
7674               .resetvalue = 0 },
7675             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7676               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7677               .access = PL1_R, .type = ARM_CP_CONST,
7678               .accessfn = access_aa64_tid3,
7679               .resetvalue = 0 },
7680             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7681               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7682               .access = PL1_R, .type = ARM_CP_CONST,
7683               .accessfn = access_aa64_tid3,
7684               .resetvalue = cpu->id_aa64afr0 },
7685             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7686               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7687               .access = PL1_R, .type = ARM_CP_CONST,
7688               .accessfn = access_aa64_tid3,
7689               .resetvalue = cpu->id_aa64afr1 },
7690             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7691               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7692               .access = PL1_R, .type = ARM_CP_CONST,
7693               .accessfn = access_aa64_tid3,
7694               .resetvalue = 0 },
7695             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7696               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7697               .access = PL1_R, .type = ARM_CP_CONST,
7698               .accessfn = access_aa64_tid3,
7699               .resetvalue = 0 },
7700             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7701               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7702               .access = PL1_R, .type = ARM_CP_CONST,
7703               .accessfn = access_aa64_tid3,
7704               .resetvalue = cpu->isar.id_aa64isar0 },
7705             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7706               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7707               .access = PL1_R, .type = ARM_CP_CONST,
7708               .accessfn = access_aa64_tid3,
7709               .resetvalue = cpu->isar.id_aa64isar1 },
7710             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7711               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7712               .access = PL1_R, .type = ARM_CP_CONST,
7713               .accessfn = access_aa64_tid3,
7714               .resetvalue = 0 },
7715             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7716               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7717               .access = PL1_R, .type = ARM_CP_CONST,
7718               .accessfn = access_aa64_tid3,
7719               .resetvalue = 0 },
7720             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7721               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7722               .access = PL1_R, .type = ARM_CP_CONST,
7723               .accessfn = access_aa64_tid3,
7724               .resetvalue = 0 },
7725             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7726               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7727               .access = PL1_R, .type = ARM_CP_CONST,
7728               .accessfn = access_aa64_tid3,
7729               .resetvalue = 0 },
7730             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7731               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7732               .access = PL1_R, .type = ARM_CP_CONST,
7733               .accessfn = access_aa64_tid3,
7734               .resetvalue = 0 },
7735             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7736               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7737               .access = PL1_R, .type = ARM_CP_CONST,
7738               .accessfn = access_aa64_tid3,
7739               .resetvalue = 0 },
7740             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7741               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7742               .access = PL1_R, .type = ARM_CP_CONST,
7743               .accessfn = access_aa64_tid3,
7744               .resetvalue = cpu->isar.id_aa64mmfr0 },
7745             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7746               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7747               .access = PL1_R, .type = ARM_CP_CONST,
7748               .accessfn = access_aa64_tid3,
7749               .resetvalue = cpu->isar.id_aa64mmfr1 },
7750             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7751               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7752               .access = PL1_R, .type = ARM_CP_CONST,
7753               .accessfn = access_aa64_tid3,
7754               .resetvalue = cpu->isar.id_aa64mmfr2 },
7755             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7756               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7757               .access = PL1_R, .type = ARM_CP_CONST,
7758               .accessfn = access_aa64_tid3,
7759               .resetvalue = 0 },
7760             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7761               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7762               .access = PL1_R, .type = ARM_CP_CONST,
7763               .accessfn = access_aa64_tid3,
7764               .resetvalue = 0 },
7765             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7766               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7767               .access = PL1_R, .type = ARM_CP_CONST,
7768               .accessfn = access_aa64_tid3,
7769               .resetvalue = 0 },
7770             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7771               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7772               .access = PL1_R, .type = ARM_CP_CONST,
7773               .accessfn = access_aa64_tid3,
7774               .resetvalue = 0 },
7775             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7776               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7777               .access = PL1_R, .type = ARM_CP_CONST,
7778               .accessfn = access_aa64_tid3,
7779               .resetvalue = 0 },
7780             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7781               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7782               .access = PL1_R, .type = ARM_CP_CONST,
7783               .accessfn = access_aa64_tid3,
7784               .resetvalue = cpu->isar.mvfr0 },
7785             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7786               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7787               .access = PL1_R, .type = ARM_CP_CONST,
7788               .accessfn = access_aa64_tid3,
7789               .resetvalue = cpu->isar.mvfr1 },
7790             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7791               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7792               .access = PL1_R, .type = ARM_CP_CONST,
7793               .accessfn = access_aa64_tid3,
7794               .resetvalue = cpu->isar.mvfr2 },
7795             /*
7796              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7797              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7798              * as RAZ, since it is in the "reserved for future ID
7799              * registers, RAZ" part of the AArch32 encoding space.
7800              */
7801             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7802               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7803               .access = PL1_R, .type = ARM_CP_CONST,
7804               .accessfn = access_aa64_tid3,
7805               .resetvalue = 0 },
7806             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7807               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7808               .access = PL1_R, .type = ARM_CP_CONST,
7809               .accessfn = access_aa64_tid3,
7810               .resetvalue = 0 },
7811             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7812               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7813               .access = PL1_R, .type = ARM_CP_CONST,
7814               .accessfn = access_aa64_tid3,
7815               .resetvalue = 0 },
7816             /*
7817              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7818              * they're also RAZ for AArch64, and in v8 are gradually
7819              * being filled with AArch64-view-of-AArch32-ID-register
7820              * for new ID registers.
7821              */
7822             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7823               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7824               .access = PL1_R, .type = ARM_CP_CONST,
7825               .accessfn = access_aa64_tid3,
7826               .resetvalue = 0 },
7827             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7828               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7829               .access = PL1_R, .type = ARM_CP_CONST,
7830               .accessfn = access_aa64_tid3,
7831               .resetvalue = cpu->isar.id_pfr2 },
7832             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7833               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7834               .access = PL1_R, .type = ARM_CP_CONST,
7835               .accessfn = access_aa64_tid3,
7836               .resetvalue = cpu->isar.id_dfr1 },
7837             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7838               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7839               .access = PL1_R, .type = ARM_CP_CONST,
7840               .accessfn = access_aa64_tid3,
7841               .resetvalue = cpu->isar.id_mmfr5 },
7842             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7843               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7844               .access = PL1_R, .type = ARM_CP_CONST,
7845               .accessfn = access_aa64_tid3,
7846               .resetvalue = 0 },
7847             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7848               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7849               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7850               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7851             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7852               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7853               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7854               .resetvalue = cpu->pmceid0 },
7855             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7856               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7857               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7858               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7859             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7860               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7861               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7862               .resetvalue = cpu->pmceid1 },
7863         };
7864 #ifdef CONFIG_USER_ONLY
7865         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7866             { .name = "ID_AA64PFR0_EL1",
7867               .exported_bits = 0x000f000f00ff0000,
7868               .fixed_bits    = 0x0000000000000011 },
7869             { .name = "ID_AA64PFR1_EL1",
7870               .exported_bits = 0x00000000000000f0 },
7871             { .name = "ID_AA64PFR*_EL1_RESERVED",
7872               .is_glob = true                     },
7873             { .name = "ID_AA64ZFR0_EL1"           },
7874             { .name = "ID_AA64MMFR0_EL1",
7875               .fixed_bits    = 0x00000000ff000000 },
7876             { .name = "ID_AA64MMFR1_EL1"          },
7877             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7878               .is_glob = true                     },
7879             { .name = "ID_AA64DFR0_EL1",
7880               .fixed_bits    = 0x0000000000000006 },
7881             { .name = "ID_AA64DFR1_EL1"           },
7882             { .name = "ID_AA64DFR*_EL1_RESERVED",
7883               .is_glob = true                     },
7884             { .name = "ID_AA64AFR*",
7885               .is_glob = true                     },
7886             { .name = "ID_AA64ISAR0_EL1",
7887               .exported_bits = 0x00fffffff0fffff0 },
7888             { .name = "ID_AA64ISAR1_EL1",
7889               .exported_bits = 0x000000f0ffffffff },
7890             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7891               .is_glob = true                     },
7892         };
7893         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7894 #endif
7895         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7896         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7897             !arm_feature(env, ARM_FEATURE_EL2)) {
7898             ARMCPRegInfo rvbar = {
7899                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7900                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7901                 .access = PL1_R,
7902                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7903             };
7904             define_one_arm_cp_reg(cpu, &rvbar);
7905         }
7906         define_arm_cp_regs(cpu, v8_idregs);
7907         define_arm_cp_regs(cpu, v8_cp_reginfo);
7908 
7909         for (i = 4; i < 16; i++) {
7910             /*
7911              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7912              * For pre-v8 cores there are RAZ patterns for these in
7913              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7914              * v8 extends the "must RAZ" part of the ID register space
7915              * to also cover c0, 0, c{8-15}, {0-7}.
7916              * These are STATE_AA32 because in the AArch64 sysreg space
7917              * c4-c7 is where the AArch64 ID registers live (and we've
7918              * already defined those in v8_idregs[]), and c8-c15 are not
7919              * "must RAZ" for AArch64.
7920              */
7921             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7922             ARMCPRegInfo v8_aa32_raz_idregs = {
7923                 .name = name,
7924                 .state = ARM_CP_STATE_AA32,
7925                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7926                 .access = PL1_R, .type = ARM_CP_CONST,
7927                 .accessfn = access_aa64_tid3,
7928                 .resetvalue = 0 };
7929             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7930         }
7931     }
7932 
7933     /*
7934      * Register the base EL2 cpregs.
7935      * Pre v8, these registers are implemented only as part of the
7936      * Virtualization Extensions (EL2 present).  Beginning with v8,
7937      * if EL2 is missing but EL3 is enabled, mostly these become
7938      * RES0 from EL3, with some specific exceptions.
7939      */
7940     if (arm_feature(env, ARM_FEATURE_EL2)
7941         || (arm_feature(env, ARM_FEATURE_EL3)
7942             && arm_feature(env, ARM_FEATURE_V8))) {
7943         uint64_t vmpidr_def = mpidr_read_val(env);
7944         ARMCPRegInfo vpidr_regs[] = {
7945             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7946               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7947               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7948               .resetvalue = cpu->midr,
7949               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7950               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7951             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7952               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7953               .access = PL2_RW, .resetvalue = cpu->midr,
7954               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7955               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7956             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7957               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7958               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7959               .resetvalue = vmpidr_def,
7960               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7961               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7962             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7963               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7964               .access = PL2_RW, .resetvalue = vmpidr_def,
7965               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7966               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7967         };
7968         /*
7969          * The only field of MDCR_EL2 that has a defined architectural reset
7970          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7971          */
7972         ARMCPRegInfo mdcr_el2 = {
7973             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
7974             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7975             .writefn = mdcr_el2_write,
7976             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7977             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7978         };
7979         define_one_arm_cp_reg(cpu, &mdcr_el2);
7980         define_arm_cp_regs(cpu, vpidr_regs);
7981         define_arm_cp_regs(cpu, el2_cp_reginfo);
7982         if (arm_feature(env, ARM_FEATURE_V8)) {
7983             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7984         }
7985         if (cpu_isar_feature(aa64_sel2, cpu)) {
7986             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7987         }
7988         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7989         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7990             ARMCPRegInfo rvbar = {
7991                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7992                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7993                 .access = PL2_R,
7994                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7995             };
7996             define_one_arm_cp_reg(cpu, &rvbar);
7997         }
7998     }
7999 
8000     /* Register the base EL3 cpregs. */
8001     if (arm_feature(env, ARM_FEATURE_EL3)) {
8002         define_arm_cp_regs(cpu, el3_cp_reginfo);
8003         ARMCPRegInfo el3_regs[] = {
8004             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8005               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8006               .access = PL3_R,
8007               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8008             },
8009             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8010               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8011               .access = PL3_RW,
8012               .raw_writefn = raw_write, .writefn = sctlr_write,
8013               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8014               .resetvalue = cpu->reset_sctlr },
8015         };
8016 
8017         define_arm_cp_regs(cpu, el3_regs);
8018     }
8019     /* The behaviour of NSACR is sufficiently various that we don't
8020      * try to describe it in a single reginfo:
8021      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8022      *     reads as constant 0xc00 from NS EL1 and NS EL2
8023      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8024      *  if v7 without EL3, register doesn't exist
8025      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8026      */
8027     if (arm_feature(env, ARM_FEATURE_EL3)) {
8028         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8029             static const ARMCPRegInfo nsacr = {
8030                 .name = "NSACR", .type = ARM_CP_CONST,
8031                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8032                 .access = PL1_RW, .accessfn = nsacr_access,
8033                 .resetvalue = 0xc00
8034             };
8035             define_one_arm_cp_reg(cpu, &nsacr);
8036         } else {
8037             static const ARMCPRegInfo nsacr = {
8038                 .name = "NSACR",
8039                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8040                 .access = PL3_RW | PL1_R,
8041                 .resetvalue = 0,
8042                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8043             };
8044             define_one_arm_cp_reg(cpu, &nsacr);
8045         }
8046     } else {
8047         if (arm_feature(env, ARM_FEATURE_V8)) {
8048             static const ARMCPRegInfo nsacr = {
8049                 .name = "NSACR", .type = ARM_CP_CONST,
8050                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8051                 .access = PL1_R,
8052                 .resetvalue = 0xc00
8053             };
8054             define_one_arm_cp_reg(cpu, &nsacr);
8055         }
8056     }
8057 
8058     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8059         if (arm_feature(env, ARM_FEATURE_V6)) {
8060             /* PMSAv6 not implemented */
8061             assert(arm_feature(env, ARM_FEATURE_V7));
8062             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8063             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8064         } else {
8065             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8066         }
8067     } else {
8068         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8069         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8070         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8071         if (cpu_isar_feature(aa32_hpd, cpu)) {
8072             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8073         }
8074     }
8075     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8076         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8077     }
8078     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8079         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8080     }
8081     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8082         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8083     }
8084     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8085         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8086     }
8087     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8088         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8089     }
8090     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8091         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8092     }
8093     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8094         define_arm_cp_regs(cpu, omap_cp_reginfo);
8095     }
8096     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8097         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8098     }
8099     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8100         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8101     }
8102     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8103         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8104     }
8105     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8106         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8107     }
8108     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8109         define_arm_cp_regs(cpu, jazelle_regs);
8110     }
8111     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8112      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8113      * be read-only (ie write causes UNDEF exception).
8114      */
8115     {
8116         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8117             /* Pre-v8 MIDR space.
8118              * Note that the MIDR isn't a simple constant register because
8119              * of the TI925 behaviour where writes to another register can
8120              * cause the MIDR value to change.
8121              *
8122              * Unimplemented registers in the c15 0 0 0 space default to
8123              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8124              * and friends override accordingly.
8125              */
8126             { .name = "MIDR",
8127               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8128               .access = PL1_R, .resetvalue = cpu->midr,
8129               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8130               .readfn = midr_read,
8131               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8132               .type = ARM_CP_OVERRIDE },
8133             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8134             { .name = "DUMMY",
8135               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8136               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8137             { .name = "DUMMY",
8138               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8139               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8140             { .name = "DUMMY",
8141               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8142               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8143             { .name = "DUMMY",
8144               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8145               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8146             { .name = "DUMMY",
8147               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8148               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8149         };
8150         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8151             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8152               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8153               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8154               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8155               .readfn = midr_read },
8156             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8157             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8158               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8159               .access = PL1_R, .resetvalue = cpu->midr },
8160             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8161               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8162               .access = PL1_R, .resetvalue = cpu->midr },
8163             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8164               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8165               .access = PL1_R,
8166               .accessfn = access_aa64_tid1,
8167               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8168         };
8169         ARMCPRegInfo id_cp_reginfo[] = {
8170             /* These are common to v8 and pre-v8 */
8171             { .name = "CTR",
8172               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8173               .access = PL1_R, .accessfn = ctr_el0_access,
8174               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8175             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8176               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8177               .access = PL0_R, .accessfn = ctr_el0_access,
8178               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8179             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8180             { .name = "TCMTR",
8181               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8182               .access = PL1_R,
8183               .accessfn = access_aa32_tid1,
8184               .type = ARM_CP_CONST, .resetvalue = 0 },
8185         };
8186         /* TLBTR is specific to VMSA */
8187         ARMCPRegInfo id_tlbtr_reginfo = {
8188               .name = "TLBTR",
8189               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8190               .access = PL1_R,
8191               .accessfn = access_aa32_tid1,
8192               .type = ARM_CP_CONST, .resetvalue = 0,
8193         };
8194         /* MPUIR is specific to PMSA V6+ */
8195         ARMCPRegInfo id_mpuir_reginfo = {
8196               .name = "MPUIR",
8197               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8198               .access = PL1_R, .type = ARM_CP_CONST,
8199               .resetvalue = cpu->pmsav7_dregion << 8
8200         };
8201         static const ARMCPRegInfo crn0_wi_reginfo = {
8202             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8203             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8204             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8205         };
8206 #ifdef CONFIG_USER_ONLY
8207         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8208             { .name = "MIDR_EL1",
8209               .exported_bits = 0x00000000ffffffff },
8210             { .name = "REVIDR_EL1"                },
8211         };
8212         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8213 #endif
8214         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8215             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8216             size_t i;
8217             /* Register the blanket "writes ignored" value first to cover the
8218              * whole space. Then update the specific ID registers to allow write
8219              * access, so that they ignore writes rather than causing them to
8220              * UNDEF.
8221              */
8222             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8223             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8224                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8225             }
8226             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8227                 id_cp_reginfo[i].access = PL1_RW;
8228             }
8229             id_mpuir_reginfo.access = PL1_RW;
8230             id_tlbtr_reginfo.access = PL1_RW;
8231         }
8232         if (arm_feature(env, ARM_FEATURE_V8)) {
8233             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8234         } else {
8235             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8236         }
8237         define_arm_cp_regs(cpu, id_cp_reginfo);
8238         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8239             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8240         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8241             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8242         }
8243     }
8244 
8245     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8246         ARMCPRegInfo mpidr_cp_reginfo[] = {
8247             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8248               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8249               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8250         };
8251 #ifdef CONFIG_USER_ONLY
8252         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8253             { .name = "MPIDR_EL1",
8254               .fixed_bits = 0x0000000080000000 },
8255         };
8256         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8257 #endif
8258         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8259     }
8260 
8261     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8262         ARMCPRegInfo auxcr_reginfo[] = {
8263             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8264               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8265               .access = PL1_RW, .accessfn = access_tacr,
8266               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8267             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8268               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8269               .access = PL2_RW, .type = ARM_CP_CONST,
8270               .resetvalue = 0 },
8271             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8272               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8273               .access = PL3_RW, .type = ARM_CP_CONST,
8274               .resetvalue = 0 },
8275         };
8276         define_arm_cp_regs(cpu, auxcr_reginfo);
8277         if (cpu_isar_feature(aa32_ac2, cpu)) {
8278             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8279         }
8280     }
8281 
8282     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8283         /*
8284          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8285          * There are two flavours:
8286          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8287          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8288          *      32-bit register visible to AArch32 at a different encoding
8289          *      to the "flavour 1" register and with the bits rearranged to
8290          *      be able to squash a 64-bit address into the 32-bit view.
8291          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8292          * in future if we support AArch32-only configs of some of the
8293          * AArch64 cores we might need to add a specific feature flag
8294          * to indicate cores with "flavour 2" CBAR.
8295          */
8296         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8297             /* 32 bit view is [31:18] 0...0 [43:32]. */
8298             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8299                 | extract64(cpu->reset_cbar, 32, 12);
8300             ARMCPRegInfo cbar_reginfo[] = {
8301                 { .name = "CBAR",
8302                   .type = ARM_CP_CONST,
8303                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8304                   .access = PL1_R, .resetvalue = cbar32 },
8305                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8306                   .type = ARM_CP_CONST,
8307                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8308                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8309             };
8310             /* We don't implement a r/w 64 bit CBAR currently */
8311             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8312             define_arm_cp_regs(cpu, cbar_reginfo);
8313         } else {
8314             ARMCPRegInfo cbar = {
8315                 .name = "CBAR",
8316                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8317                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8318                 .fieldoffset = offsetof(CPUARMState,
8319                                         cp15.c15_config_base_address)
8320             };
8321             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8322                 cbar.access = PL1_R;
8323                 cbar.fieldoffset = 0;
8324                 cbar.type = ARM_CP_CONST;
8325             }
8326             define_one_arm_cp_reg(cpu, &cbar);
8327         }
8328     }
8329 
8330     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8331         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8332             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8333               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8334               .access = PL1_RW, .writefn = vbar_write,
8335               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8336                                      offsetof(CPUARMState, cp15.vbar_ns) },
8337               .resetvalue = 0 },
8338         };
8339         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8340     }
8341 
8342     /* Generic registers whose values depend on the implementation */
8343     {
8344         ARMCPRegInfo sctlr = {
8345             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8346             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8347             .access = PL1_RW, .accessfn = access_tvm_trvm,
8348             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8349                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8350             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8351             .raw_writefn = raw_write,
8352         };
8353         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8354             /* Normally we would always end the TB on an SCTLR write, but Linux
8355              * arch/arm/mach-pxa/sleep.S expects two instructions following
8356              * an MMU enable to execute from cache.  Imitate this behaviour.
8357              */
8358             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8359         }
8360         define_one_arm_cp_reg(cpu, &sctlr);
8361     }
8362 
8363     if (cpu_isar_feature(aa64_lor, cpu)) {
8364         define_arm_cp_regs(cpu, lor_reginfo);
8365     }
8366     if (cpu_isar_feature(aa64_pan, cpu)) {
8367         define_one_arm_cp_reg(cpu, &pan_reginfo);
8368     }
8369 #ifndef CONFIG_USER_ONLY
8370     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8371         define_arm_cp_regs(cpu, ats1e1_reginfo);
8372     }
8373     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8374         define_arm_cp_regs(cpu, ats1cp_reginfo);
8375     }
8376 #endif
8377     if (cpu_isar_feature(aa64_uao, cpu)) {
8378         define_one_arm_cp_reg(cpu, &uao_reginfo);
8379     }
8380 
8381     if (cpu_isar_feature(aa64_dit, cpu)) {
8382         define_one_arm_cp_reg(cpu, &dit_reginfo);
8383     }
8384     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8385         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8386     }
8387     if (cpu_isar_feature(any_ras, cpu)) {
8388         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8389     }
8390 
8391     if (cpu_isar_feature(aa64_vh, cpu) ||
8392         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8393         define_one_arm_cp_reg(cpu, &contextidr_el2);
8394     }
8395     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8396         define_arm_cp_regs(cpu, vhe_reginfo);
8397     }
8398 
8399     if (cpu_isar_feature(aa64_sve, cpu)) {
8400         define_arm_cp_regs(cpu, zcr_reginfo);
8401     }
8402 
8403     if (cpu_isar_feature(aa64_hcx, cpu)) {
8404         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8405     }
8406 
8407 #ifdef TARGET_AARCH64
8408     if (cpu_isar_feature(aa64_sme, cpu)) {
8409         define_arm_cp_regs(cpu, sme_reginfo);
8410     }
8411     if (cpu_isar_feature(aa64_pauth, cpu)) {
8412         define_arm_cp_regs(cpu, pauth_reginfo);
8413     }
8414     if (cpu_isar_feature(aa64_rndr, cpu)) {
8415         define_arm_cp_regs(cpu, rndr_reginfo);
8416     }
8417     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8418         define_arm_cp_regs(cpu, tlbirange_reginfo);
8419     }
8420     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8421         define_arm_cp_regs(cpu, tlbios_reginfo);
8422     }
8423 #ifndef CONFIG_USER_ONLY
8424     /* Data Cache clean instructions up to PoP */
8425     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8426         define_one_arm_cp_reg(cpu, dcpop_reg);
8427 
8428         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8429             define_one_arm_cp_reg(cpu, dcpodp_reg);
8430         }
8431     }
8432 #endif /*CONFIG_USER_ONLY*/
8433 
8434     /*
8435      * If full MTE is enabled, add all of the system registers.
8436      * If only "instructions available at EL0" are enabled,
8437      * then define only a RAZ/WI version of PSTATE.TCO.
8438      */
8439     if (cpu_isar_feature(aa64_mte, cpu)) {
8440         define_arm_cp_regs(cpu, mte_reginfo);
8441         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8442     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8443         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8444         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8445     }
8446 
8447     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8448         define_arm_cp_regs(cpu, scxtnum_reginfo);
8449     }
8450 #endif
8451 
8452     if (cpu_isar_feature(any_predinv, cpu)) {
8453         define_arm_cp_regs(cpu, predinv_reginfo);
8454     }
8455 
8456     if (cpu_isar_feature(any_ccidx, cpu)) {
8457         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8458     }
8459 
8460 #ifndef CONFIG_USER_ONLY
8461     /*
8462      * Register redirections and aliases must be done last,
8463      * after the registers from the other extensions have been defined.
8464      */
8465     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8466         define_arm_vh_e2h_redirects_aliases(cpu);
8467     }
8468 #endif
8469 }
8470 
8471 /* Sort alphabetically by type name, except for "any". */
8472 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8473 {
8474     ObjectClass *class_a = (ObjectClass *)a;
8475     ObjectClass *class_b = (ObjectClass *)b;
8476     const char *name_a, *name_b;
8477 
8478     name_a = object_class_get_name(class_a);
8479     name_b = object_class_get_name(class_b);
8480     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8481         return 1;
8482     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8483         return -1;
8484     } else {
8485         return strcmp(name_a, name_b);
8486     }
8487 }
8488 
8489 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8490 {
8491     ObjectClass *oc = data;
8492     CPUClass *cc = CPU_CLASS(oc);
8493     const char *typename;
8494     char *name;
8495 
8496     typename = object_class_get_name(oc);
8497     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8498     if (cc->deprecation_note) {
8499         qemu_printf("  %s (deprecated)\n", name);
8500     } else {
8501         qemu_printf("  %s\n", name);
8502     }
8503     g_free(name);
8504 }
8505 
8506 void arm_cpu_list(void)
8507 {
8508     GSList *list;
8509 
8510     list = object_class_get_list(TYPE_ARM_CPU, false);
8511     list = g_slist_sort(list, arm_cpu_list_compare);
8512     qemu_printf("Available CPUs:\n");
8513     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8514     g_slist_free(list);
8515 }
8516 
8517 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8518 {
8519     ObjectClass *oc = data;
8520     CpuDefinitionInfoList **cpu_list = user_data;
8521     CpuDefinitionInfo *info;
8522     const char *typename;
8523 
8524     typename = object_class_get_name(oc);
8525     info = g_malloc0(sizeof(*info));
8526     info->name = g_strndup(typename,
8527                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8528     info->q_typename = g_strdup(typename);
8529 
8530     QAPI_LIST_PREPEND(*cpu_list, info);
8531 }
8532 
8533 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8534 {
8535     CpuDefinitionInfoList *cpu_list = NULL;
8536     GSList *list;
8537 
8538     list = object_class_get_list(TYPE_ARM_CPU, false);
8539     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8540     g_slist_free(list);
8541 
8542     return cpu_list;
8543 }
8544 
8545 /*
8546  * Private utility function for define_one_arm_cp_reg_with_opaque():
8547  * add a single reginfo struct to the hash table.
8548  */
8549 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8550                                    void *opaque, CPState state,
8551                                    CPSecureState secstate,
8552                                    int crm, int opc1, int opc2,
8553                                    const char *name)
8554 {
8555     CPUARMState *env = &cpu->env;
8556     uint32_t key;
8557     ARMCPRegInfo *r2;
8558     bool is64 = r->type & ARM_CP_64BIT;
8559     bool ns = secstate & ARM_CP_SECSTATE_NS;
8560     int cp = r->cp;
8561     size_t name_len;
8562     bool make_const;
8563 
8564     switch (state) {
8565     case ARM_CP_STATE_AA32:
8566         /* We assume it is a cp15 register if the .cp field is left unset. */
8567         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8568             cp = 15;
8569         }
8570         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8571         break;
8572     case ARM_CP_STATE_AA64:
8573         /*
8574          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8575          * cp == 0 as equivalent to the value for "standard guest-visible
8576          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8577          * in their AArch64 view (the .cp value may be non-zero for the
8578          * benefit of the AArch32 view).
8579          */
8580         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8581             cp = CP_REG_ARM64_SYSREG_CP;
8582         }
8583         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8584         break;
8585     default:
8586         g_assert_not_reached();
8587     }
8588 
8589     /* Overriding of an existing definition must be explicitly requested. */
8590     if (!(r->type & ARM_CP_OVERRIDE)) {
8591         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8592         if (oldreg) {
8593             assert(oldreg->type & ARM_CP_OVERRIDE);
8594         }
8595     }
8596 
8597     /*
8598      * Eliminate registers that are not present because the EL is missing.
8599      * Doing this here makes it easier to put all registers for a given
8600      * feature into the same ARMCPRegInfo array and define them all at once.
8601      */
8602     make_const = false;
8603     if (arm_feature(env, ARM_FEATURE_EL3)) {
8604         /*
8605          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8606          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8607          */
8608         int min_el = ctz32(r->access) / 2;
8609         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8610             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8611                 return;
8612             }
8613             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8614         }
8615     } else {
8616         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8617                                  ? PL2_RW : PL1_RW);
8618         if ((r->access & max_el) == 0) {
8619             return;
8620         }
8621     }
8622 
8623     /* Combine cpreg and name into one allocation. */
8624     name_len = strlen(name) + 1;
8625     r2 = g_malloc(sizeof(*r2) + name_len);
8626     *r2 = *r;
8627     r2->name = memcpy(r2 + 1, name, name_len);
8628 
8629     /*
8630      * Update fields to match the instantiation, overwiting wildcards
8631      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8632      */
8633     r2->cp = cp;
8634     r2->crm = crm;
8635     r2->opc1 = opc1;
8636     r2->opc2 = opc2;
8637     r2->state = state;
8638     r2->secure = secstate;
8639     if (opaque) {
8640         r2->opaque = opaque;
8641     }
8642 
8643     if (make_const) {
8644         /* This should not have been a very special register to begin. */
8645         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8646         assert(old_special == 0 || old_special == ARM_CP_NOP);
8647         /*
8648          * Set the special function to CONST, retaining the other flags.
8649          * This is important for e.g. ARM_CP_SVE so that we still
8650          * take the SVE trap if CPTR_EL3.EZ == 0.
8651          */
8652         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8653         /*
8654          * Usually, these registers become RES0, but there are a few
8655          * special cases like VPIDR_EL2 which have a constant non-zero
8656          * value with writes ignored.
8657          */
8658         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8659             r2->resetvalue = 0;
8660         }
8661         /*
8662          * ARM_CP_CONST has precedence, so removing the callbacks and
8663          * offsets are not strictly necessary, but it is potentially
8664          * less confusing to debug later.
8665          */
8666         r2->readfn = NULL;
8667         r2->writefn = NULL;
8668         r2->raw_readfn = NULL;
8669         r2->raw_writefn = NULL;
8670         r2->resetfn = NULL;
8671         r2->fieldoffset = 0;
8672         r2->bank_fieldoffsets[0] = 0;
8673         r2->bank_fieldoffsets[1] = 0;
8674     } else {
8675         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8676 
8677         if (isbanked) {
8678             /*
8679              * Register is banked (using both entries in array).
8680              * Overwriting fieldoffset as the array is only used to define
8681              * banked registers but later only fieldoffset is used.
8682              */
8683             r2->fieldoffset = r->bank_fieldoffsets[ns];
8684         }
8685         if (state == ARM_CP_STATE_AA32) {
8686             if (isbanked) {
8687                 /*
8688                  * If the register is banked then we don't need to migrate or
8689                  * reset the 32-bit instance in certain cases:
8690                  *
8691                  * 1) If the register has both 32-bit and 64-bit instances
8692                  *    then we can count on the 64-bit instance taking care
8693                  *    of the non-secure bank.
8694                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8695                  *    version taking care of the secure bank.  This requires
8696                  *    that separate 32 and 64-bit definitions are provided.
8697                  */
8698                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8699                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8700                     r2->type |= ARM_CP_ALIAS;
8701                 }
8702             } else if ((secstate != r->secure) && !ns) {
8703                 /*
8704                  * The register is not banked so we only want to allow
8705                  * migration of the non-secure instance.
8706                  */
8707                 r2->type |= ARM_CP_ALIAS;
8708             }
8709 
8710             if (HOST_BIG_ENDIAN &&
8711                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8712                 r2->fieldoffset += sizeof(uint32_t);
8713             }
8714         }
8715     }
8716 
8717     /*
8718      * By convention, for wildcarded registers only the first
8719      * entry is used for migration; the others are marked as
8720      * ALIAS so we don't try to transfer the register
8721      * multiple times. Special registers (ie NOP/WFI) are
8722      * never migratable and not even raw-accessible.
8723      */
8724     if (r2->type & ARM_CP_SPECIAL_MASK) {
8725         r2->type |= ARM_CP_NO_RAW;
8726     }
8727     if (((r->crm == CP_ANY) && crm != 0) ||
8728         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8729         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8730         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8731     }
8732 
8733     /*
8734      * Check that raw accesses are either forbidden or handled. Note that
8735      * we can't assert this earlier because the setup of fieldoffset for
8736      * banked registers has to be done first.
8737      */
8738     if (!(r2->type & ARM_CP_NO_RAW)) {
8739         assert(!raw_accessors_invalid(r2));
8740     }
8741 
8742     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8743 }
8744 
8745 
8746 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8747                                        const ARMCPRegInfo *r, void *opaque)
8748 {
8749     /* Define implementations of coprocessor registers.
8750      * We store these in a hashtable because typically
8751      * there are less than 150 registers in a space which
8752      * is 16*16*16*8*8 = 262144 in size.
8753      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8754      * If a register is defined twice then the second definition is
8755      * used, so this can be used to define some generic registers and
8756      * then override them with implementation specific variations.
8757      * At least one of the original and the second definition should
8758      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8759      * against accidental use.
8760      *
8761      * The state field defines whether the register is to be
8762      * visible in the AArch32 or AArch64 execution state. If the
8763      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8764      * reginfo structure for the AArch32 view, which sees the lower
8765      * 32 bits of the 64 bit register.
8766      *
8767      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8768      * be wildcarded. AArch64 registers are always considered to be 64
8769      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8770      * the register, if any.
8771      */
8772     int crm, opc1, opc2;
8773     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8774     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8775     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8776     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8777     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8778     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8779     CPState state;
8780 
8781     /* 64 bit registers have only CRm and Opc1 fields */
8782     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8783     /* op0 only exists in the AArch64 encodings */
8784     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8785     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8786     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8787     /*
8788      * This API is only for Arm's system coprocessors (14 and 15) or
8789      * (M-profile or v7A-and-earlier only) for implementation defined
8790      * coprocessors in the range 0..7.  Our decode assumes this, since
8791      * 8..13 can be used for other insns including VFP and Neon. See
8792      * valid_cp() in translate.c.  Assert here that we haven't tried
8793      * to use an invalid coprocessor number.
8794      */
8795     switch (r->state) {
8796     case ARM_CP_STATE_BOTH:
8797         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8798         if (r->cp == 0) {
8799             break;
8800         }
8801         /* fall through */
8802     case ARM_CP_STATE_AA32:
8803         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8804             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8805             assert(r->cp >= 14 && r->cp <= 15);
8806         } else {
8807             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8808         }
8809         break;
8810     case ARM_CP_STATE_AA64:
8811         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8812         break;
8813     default:
8814         g_assert_not_reached();
8815     }
8816     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8817      * encodes a minimum access level for the register. We roll this
8818      * runtime check into our general permission check code, so check
8819      * here that the reginfo's specified permissions are strict enough
8820      * to encompass the generic architectural permission check.
8821      */
8822     if (r->state != ARM_CP_STATE_AA32) {
8823         CPAccessRights mask;
8824         switch (r->opc1) {
8825         case 0:
8826             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8827             mask = PL0U_R | PL1_RW;
8828             break;
8829         case 1: case 2:
8830             /* min_EL EL1 */
8831             mask = PL1_RW;
8832             break;
8833         case 3:
8834             /* min_EL EL0 */
8835             mask = PL0_RW;
8836             break;
8837         case 4:
8838         case 5:
8839             /* min_EL EL2 */
8840             mask = PL2_RW;
8841             break;
8842         case 6:
8843             /* min_EL EL3 */
8844             mask = PL3_RW;
8845             break;
8846         case 7:
8847             /* min_EL EL1, secure mode only (we don't check the latter) */
8848             mask = PL1_RW;
8849             break;
8850         default:
8851             /* broken reginfo with out-of-range opc1 */
8852             g_assert_not_reached();
8853         }
8854         /* assert our permissions are not too lax (stricter is fine) */
8855         assert((r->access & ~mask) == 0);
8856     }
8857 
8858     /* Check that the register definition has enough info to handle
8859      * reads and writes if they are permitted.
8860      */
8861     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8862         if (r->access & PL3_R) {
8863             assert((r->fieldoffset ||
8864                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8865                    r->readfn);
8866         }
8867         if (r->access & PL3_W) {
8868             assert((r->fieldoffset ||
8869                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8870                    r->writefn);
8871         }
8872     }
8873 
8874     for (crm = crmmin; crm <= crmmax; crm++) {
8875         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8876             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8877                 for (state = ARM_CP_STATE_AA32;
8878                      state <= ARM_CP_STATE_AA64; state++) {
8879                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8880                         continue;
8881                     }
8882                     if (state == ARM_CP_STATE_AA32) {
8883                         /* Under AArch32 CP registers can be common
8884                          * (same for secure and non-secure world) or banked.
8885                          */
8886                         char *name;
8887 
8888                         switch (r->secure) {
8889                         case ARM_CP_SECSTATE_S:
8890                         case ARM_CP_SECSTATE_NS:
8891                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8892                                                    r->secure, crm, opc1, opc2,
8893                                                    r->name);
8894                             break;
8895                         case ARM_CP_SECSTATE_BOTH:
8896                             name = g_strdup_printf("%s_S", r->name);
8897                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8898                                                    ARM_CP_SECSTATE_S,
8899                                                    crm, opc1, opc2, name);
8900                             g_free(name);
8901                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8902                                                    ARM_CP_SECSTATE_NS,
8903                                                    crm, opc1, opc2, r->name);
8904                             break;
8905                         default:
8906                             g_assert_not_reached();
8907                         }
8908                     } else {
8909                         /* AArch64 registers get mapped to non-secure instance
8910                          * of AArch32 */
8911                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8912                                                ARM_CP_SECSTATE_NS,
8913                                                crm, opc1, opc2, r->name);
8914                     }
8915                 }
8916             }
8917         }
8918     }
8919 }
8920 
8921 /* Define a whole list of registers */
8922 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8923                                         void *opaque, size_t len)
8924 {
8925     size_t i;
8926     for (i = 0; i < len; ++i) {
8927         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8928     }
8929 }
8930 
8931 /*
8932  * Modify ARMCPRegInfo for access from userspace.
8933  *
8934  * This is a data driven modification directed by
8935  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8936  * user-space cannot alter any values and dynamic values pertaining to
8937  * execution state are hidden from user space view anyway.
8938  */
8939 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8940                                  const ARMCPRegUserSpaceInfo *mods,
8941                                  size_t mods_len)
8942 {
8943     for (size_t mi = 0; mi < mods_len; ++mi) {
8944         const ARMCPRegUserSpaceInfo *m = mods + mi;
8945         GPatternSpec *pat = NULL;
8946 
8947         if (m->is_glob) {
8948             pat = g_pattern_spec_new(m->name);
8949         }
8950         for (size_t ri = 0; ri < regs_len; ++ri) {
8951             ARMCPRegInfo *r = regs + ri;
8952 
8953             if (pat && g_pattern_match_string(pat, r->name)) {
8954                 r->type = ARM_CP_CONST;
8955                 r->access = PL0U_R;
8956                 r->resetvalue = 0;
8957                 /* continue */
8958             } else if (strcmp(r->name, m->name) == 0) {
8959                 r->type = ARM_CP_CONST;
8960                 r->access = PL0U_R;
8961                 r->resetvalue &= m->exported_bits;
8962                 r->resetvalue |= m->fixed_bits;
8963                 break;
8964             }
8965         }
8966         if (pat) {
8967             g_pattern_spec_free(pat);
8968         }
8969     }
8970 }
8971 
8972 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8973 {
8974     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8975 }
8976 
8977 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8978                          uint64_t value)
8979 {
8980     /* Helper coprocessor write function for write-ignore registers */
8981 }
8982 
8983 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8984 {
8985     /* Helper coprocessor write function for read-as-zero registers */
8986     return 0;
8987 }
8988 
8989 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8990 {
8991     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8992 }
8993 
8994 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8995 {
8996     /* Return true if it is not valid for us to switch to
8997      * this CPU mode (ie all the UNPREDICTABLE cases in
8998      * the ARM ARM CPSRWriteByInstr pseudocode).
8999      */
9000 
9001     /* Changes to or from Hyp via MSR and CPS are illegal. */
9002     if (write_type == CPSRWriteByInstr &&
9003         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9004          mode == ARM_CPU_MODE_HYP)) {
9005         return 1;
9006     }
9007 
9008     switch (mode) {
9009     case ARM_CPU_MODE_USR:
9010         return 0;
9011     case ARM_CPU_MODE_SYS:
9012     case ARM_CPU_MODE_SVC:
9013     case ARM_CPU_MODE_ABT:
9014     case ARM_CPU_MODE_UND:
9015     case ARM_CPU_MODE_IRQ:
9016     case ARM_CPU_MODE_FIQ:
9017         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
9018          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9019          */
9020         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9021          * and CPS are treated as illegal mode changes.
9022          */
9023         if (write_type == CPSRWriteByInstr &&
9024             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9025             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9026             return 1;
9027         }
9028         return 0;
9029     case ARM_CPU_MODE_HYP:
9030         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9031     case ARM_CPU_MODE_MON:
9032         return arm_current_el(env) < 3;
9033     default:
9034         return 1;
9035     }
9036 }
9037 
9038 uint32_t cpsr_read(CPUARMState *env)
9039 {
9040     int ZF;
9041     ZF = (env->ZF == 0);
9042     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9043         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9044         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9045         | ((env->condexec_bits & 0xfc) << 8)
9046         | (env->GE << 16) | (env->daif & CPSR_AIF);
9047 }
9048 
9049 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9050                 CPSRWriteType write_type)
9051 {
9052     uint32_t changed_daif;
9053     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9054         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9055 
9056     if (mask & CPSR_NZCV) {
9057         env->ZF = (~val) & CPSR_Z;
9058         env->NF = val;
9059         env->CF = (val >> 29) & 1;
9060         env->VF = (val << 3) & 0x80000000;
9061     }
9062     if (mask & CPSR_Q)
9063         env->QF = ((val & CPSR_Q) != 0);
9064     if (mask & CPSR_T)
9065         env->thumb = ((val & CPSR_T) != 0);
9066     if (mask & CPSR_IT_0_1) {
9067         env->condexec_bits &= ~3;
9068         env->condexec_bits |= (val >> 25) & 3;
9069     }
9070     if (mask & CPSR_IT_2_7) {
9071         env->condexec_bits &= 3;
9072         env->condexec_bits |= (val >> 8) & 0xfc;
9073     }
9074     if (mask & CPSR_GE) {
9075         env->GE = (val >> 16) & 0xf;
9076     }
9077 
9078     /* In a V7 implementation that includes the security extensions but does
9079      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9080      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9081      * bits respectively.
9082      *
9083      * In a V8 implementation, it is permitted for privileged software to
9084      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9085      */
9086     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9087         arm_feature(env, ARM_FEATURE_EL3) &&
9088         !arm_feature(env, ARM_FEATURE_EL2) &&
9089         !arm_is_secure(env)) {
9090 
9091         changed_daif = (env->daif ^ val) & mask;
9092 
9093         if (changed_daif & CPSR_A) {
9094             /* Check to see if we are allowed to change the masking of async
9095              * abort exceptions from a non-secure state.
9096              */
9097             if (!(env->cp15.scr_el3 & SCR_AW)) {
9098                 qemu_log_mask(LOG_GUEST_ERROR,
9099                               "Ignoring attempt to switch CPSR_A flag from "
9100                               "non-secure world with SCR.AW bit clear\n");
9101                 mask &= ~CPSR_A;
9102             }
9103         }
9104 
9105         if (changed_daif & CPSR_F) {
9106             /* Check to see if we are allowed to change the masking of FIQ
9107              * exceptions from a non-secure state.
9108              */
9109             if (!(env->cp15.scr_el3 & SCR_FW)) {
9110                 qemu_log_mask(LOG_GUEST_ERROR,
9111                               "Ignoring attempt to switch CPSR_F flag from "
9112                               "non-secure world with SCR.FW bit clear\n");
9113                 mask &= ~CPSR_F;
9114             }
9115 
9116             /* Check whether non-maskable FIQ (NMFI) support is enabled.
9117              * If this bit is set software is not allowed to mask
9118              * FIQs, but is allowed to set CPSR_F to 0.
9119              */
9120             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9121                 (val & CPSR_F)) {
9122                 qemu_log_mask(LOG_GUEST_ERROR,
9123                               "Ignoring attempt to enable CPSR_F flag "
9124                               "(non-maskable FIQ [NMFI] support enabled)\n");
9125                 mask &= ~CPSR_F;
9126             }
9127         }
9128     }
9129 
9130     env->daif &= ~(CPSR_AIF & mask);
9131     env->daif |= val & CPSR_AIF & mask;
9132 
9133     if (write_type != CPSRWriteRaw &&
9134         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9135         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9136             /* Note that we can only get here in USR mode if this is a
9137              * gdb stub write; for this case we follow the architectural
9138              * behaviour for guest writes in USR mode of ignoring an attempt
9139              * to switch mode. (Those are caught by translate.c for writes
9140              * triggered by guest instructions.)
9141              */
9142             mask &= ~CPSR_M;
9143         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9144             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9145              * v7, and has defined behaviour in v8:
9146              *  + leave CPSR.M untouched
9147              *  + allow changes to the other CPSR fields
9148              *  + set PSTATE.IL
9149              * For user changes via the GDB stub, we don't set PSTATE.IL,
9150              * as this would be unnecessarily harsh for a user error.
9151              */
9152             mask &= ~CPSR_M;
9153             if (write_type != CPSRWriteByGDBStub &&
9154                 arm_feature(env, ARM_FEATURE_V8)) {
9155                 mask |= CPSR_IL;
9156                 val |= CPSR_IL;
9157             }
9158             qemu_log_mask(LOG_GUEST_ERROR,
9159                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9160                           aarch32_mode_name(env->uncached_cpsr),
9161                           aarch32_mode_name(val));
9162         } else {
9163             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9164                           write_type == CPSRWriteExceptionReturn ?
9165                           "Exception return from AArch32" :
9166                           "AArch32 mode switch from",
9167                           aarch32_mode_name(env->uncached_cpsr),
9168                           aarch32_mode_name(val), env->regs[15]);
9169             switch_mode(env, val & CPSR_M);
9170         }
9171     }
9172     mask &= ~CACHED_CPSR_BITS;
9173     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9174     if (rebuild_hflags) {
9175         arm_rebuild_hflags(env);
9176     }
9177 }
9178 
9179 /* Sign/zero extend */
9180 uint32_t HELPER(sxtb16)(uint32_t x)
9181 {
9182     uint32_t res;
9183     res = (uint16_t)(int8_t)x;
9184     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9185     return res;
9186 }
9187 
9188 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9189 {
9190     /*
9191      * Take a division-by-zero exception if necessary; otherwise return
9192      * to get the usual non-trapping division behaviour (result of 0)
9193      */
9194     if (arm_feature(env, ARM_FEATURE_M)
9195         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9196         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9197     }
9198 }
9199 
9200 uint32_t HELPER(uxtb16)(uint32_t x)
9201 {
9202     uint32_t res;
9203     res = (uint16_t)(uint8_t)x;
9204     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9205     return res;
9206 }
9207 
9208 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9209 {
9210     if (den == 0) {
9211         handle_possible_div0_trap(env, GETPC());
9212         return 0;
9213     }
9214     if (num == INT_MIN && den == -1) {
9215         return INT_MIN;
9216     }
9217     return num / den;
9218 }
9219 
9220 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9221 {
9222     if (den == 0) {
9223         handle_possible_div0_trap(env, GETPC());
9224         return 0;
9225     }
9226     return num / den;
9227 }
9228 
9229 uint32_t HELPER(rbit)(uint32_t x)
9230 {
9231     return revbit32(x);
9232 }
9233 
9234 #ifdef CONFIG_USER_ONLY
9235 
9236 static void switch_mode(CPUARMState *env, int mode)
9237 {
9238     ARMCPU *cpu = env_archcpu(env);
9239 
9240     if (mode != ARM_CPU_MODE_USR) {
9241         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9242     }
9243 }
9244 
9245 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9246                                  uint32_t cur_el, bool secure)
9247 {
9248     return 1;
9249 }
9250 
9251 void aarch64_sync_64_to_32(CPUARMState *env)
9252 {
9253     g_assert_not_reached();
9254 }
9255 
9256 #else
9257 
9258 static void switch_mode(CPUARMState *env, int mode)
9259 {
9260     int old_mode;
9261     int i;
9262 
9263     old_mode = env->uncached_cpsr & CPSR_M;
9264     if (mode == old_mode)
9265         return;
9266 
9267     if (old_mode == ARM_CPU_MODE_FIQ) {
9268         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9269         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9270     } else if (mode == ARM_CPU_MODE_FIQ) {
9271         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9272         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9273     }
9274 
9275     i = bank_number(old_mode);
9276     env->banked_r13[i] = env->regs[13];
9277     env->banked_spsr[i] = env->spsr;
9278 
9279     i = bank_number(mode);
9280     env->regs[13] = env->banked_r13[i];
9281     env->spsr = env->banked_spsr[i];
9282 
9283     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9284     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9285 }
9286 
9287 /* Physical Interrupt Target EL Lookup Table
9288  *
9289  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9290  *
9291  * The below multi-dimensional table is used for looking up the target
9292  * exception level given numerous condition criteria.  Specifically, the
9293  * target EL is based on SCR and HCR routing controls as well as the
9294  * currently executing EL and secure state.
9295  *
9296  *    Dimensions:
9297  *    target_el_table[2][2][2][2][2][4]
9298  *                    |  |  |  |  |  +--- Current EL
9299  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9300  *                    |  |  |  +--------- HCR mask override
9301  *                    |  |  +------------ SCR exec state control
9302  *                    |  +--------------- SCR mask override
9303  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9304  *
9305  *    The table values are as such:
9306  *    0-3 = EL0-EL3
9307  *     -1 = Cannot occur
9308  *
9309  * The ARM ARM target EL table includes entries indicating that an "exception
9310  * is not taken".  The two cases where this is applicable are:
9311  *    1) An exception is taken from EL3 but the SCR does not have the exception
9312  *    routed to EL3.
9313  *    2) An exception is taken from EL2 but the HCR does not have the exception
9314  *    routed to EL2.
9315  * In these two cases, the below table contain a target of EL1.  This value is
9316  * returned as it is expected that the consumer of the table data will check
9317  * for "target EL >= current EL" to ensure the exception is not taken.
9318  *
9319  *            SCR     HCR
9320  *         64  EA     AMO                 From
9321  *        BIT IRQ     IMO      Non-secure         Secure
9322  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9323  */
9324 static const int8_t target_el_table[2][2][2][2][2][4] = {
9325     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9326        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9327       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9328        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9329      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9330        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9331       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9332        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9333     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9334        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9335       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9336        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9337      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9338        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9339       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9340        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9341 };
9342 
9343 /*
9344  * Determine the target EL for physical exceptions
9345  */
9346 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9347                                  uint32_t cur_el, bool secure)
9348 {
9349     CPUARMState *env = cs->env_ptr;
9350     bool rw;
9351     bool scr;
9352     bool hcr;
9353     int target_el;
9354     /* Is the highest EL AArch64? */
9355     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9356     uint64_t hcr_el2;
9357 
9358     if (arm_feature(env, ARM_FEATURE_EL3)) {
9359         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9360     } else {
9361         /* Either EL2 is the highest EL (and so the EL2 register width
9362          * is given by is64); or there is no EL2 or EL3, in which case
9363          * the value of 'rw' does not affect the table lookup anyway.
9364          */
9365         rw = is64;
9366     }
9367 
9368     hcr_el2 = arm_hcr_el2_eff(env);
9369     switch (excp_idx) {
9370     case EXCP_IRQ:
9371         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9372         hcr = hcr_el2 & HCR_IMO;
9373         break;
9374     case EXCP_FIQ:
9375         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9376         hcr = hcr_el2 & HCR_FMO;
9377         break;
9378     default:
9379         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9380         hcr = hcr_el2 & HCR_AMO;
9381         break;
9382     };
9383 
9384     /*
9385      * For these purposes, TGE and AMO/IMO/FMO both force the
9386      * interrupt to EL2.  Fold TGE into the bit extracted above.
9387      */
9388     hcr |= (hcr_el2 & HCR_TGE) != 0;
9389 
9390     /* Perform a table-lookup for the target EL given the current state */
9391     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9392 
9393     assert(target_el > 0);
9394 
9395     return target_el;
9396 }
9397 
9398 void arm_log_exception(CPUState *cs)
9399 {
9400     int idx = cs->exception_index;
9401 
9402     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9403         const char *exc = NULL;
9404         static const char * const excnames[] = {
9405             [EXCP_UDEF] = "Undefined Instruction",
9406             [EXCP_SWI] = "SVC",
9407             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9408             [EXCP_DATA_ABORT] = "Data Abort",
9409             [EXCP_IRQ] = "IRQ",
9410             [EXCP_FIQ] = "FIQ",
9411             [EXCP_BKPT] = "Breakpoint",
9412             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9413             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9414             [EXCP_HVC] = "Hypervisor Call",
9415             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9416             [EXCP_SMC] = "Secure Monitor Call",
9417             [EXCP_VIRQ] = "Virtual IRQ",
9418             [EXCP_VFIQ] = "Virtual FIQ",
9419             [EXCP_SEMIHOST] = "Semihosting call",
9420             [EXCP_NOCP] = "v7M NOCP UsageFault",
9421             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9422             [EXCP_STKOF] = "v8M STKOF UsageFault",
9423             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9424             [EXCP_LSERR] = "v8M LSERR UsageFault",
9425             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9426             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9427             [EXCP_VSERR] = "Virtual SERR",
9428         };
9429 
9430         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9431             exc = excnames[idx];
9432         }
9433         if (!exc) {
9434             exc = "unknown";
9435         }
9436         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9437                       idx, exc, cs->cpu_index);
9438     }
9439 }
9440 
9441 /*
9442  * Function used to synchronize QEMU's AArch64 register set with AArch32
9443  * register set.  This is necessary when switching between AArch32 and AArch64
9444  * execution state.
9445  */
9446 void aarch64_sync_32_to_64(CPUARMState *env)
9447 {
9448     int i;
9449     uint32_t mode = env->uncached_cpsr & CPSR_M;
9450 
9451     /* We can blanket copy R[0:7] to X[0:7] */
9452     for (i = 0; i < 8; i++) {
9453         env->xregs[i] = env->regs[i];
9454     }
9455 
9456     /*
9457      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9458      * Otherwise, they come from the banked user regs.
9459      */
9460     if (mode == ARM_CPU_MODE_FIQ) {
9461         for (i = 8; i < 13; i++) {
9462             env->xregs[i] = env->usr_regs[i - 8];
9463         }
9464     } else {
9465         for (i = 8; i < 13; i++) {
9466             env->xregs[i] = env->regs[i];
9467         }
9468     }
9469 
9470     /*
9471      * Registers x13-x23 are the various mode SP and FP registers. Registers
9472      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9473      * from the mode banked register.
9474      */
9475     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9476         env->xregs[13] = env->regs[13];
9477         env->xregs[14] = env->regs[14];
9478     } else {
9479         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9480         /* HYP is an exception in that it is copied from r14 */
9481         if (mode == ARM_CPU_MODE_HYP) {
9482             env->xregs[14] = env->regs[14];
9483         } else {
9484             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9485         }
9486     }
9487 
9488     if (mode == ARM_CPU_MODE_HYP) {
9489         env->xregs[15] = env->regs[13];
9490     } else {
9491         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9492     }
9493 
9494     if (mode == ARM_CPU_MODE_IRQ) {
9495         env->xregs[16] = env->regs[14];
9496         env->xregs[17] = env->regs[13];
9497     } else {
9498         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9499         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9500     }
9501 
9502     if (mode == ARM_CPU_MODE_SVC) {
9503         env->xregs[18] = env->regs[14];
9504         env->xregs[19] = env->regs[13];
9505     } else {
9506         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9507         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9508     }
9509 
9510     if (mode == ARM_CPU_MODE_ABT) {
9511         env->xregs[20] = env->regs[14];
9512         env->xregs[21] = env->regs[13];
9513     } else {
9514         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9515         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9516     }
9517 
9518     if (mode == ARM_CPU_MODE_UND) {
9519         env->xregs[22] = env->regs[14];
9520         env->xregs[23] = env->regs[13];
9521     } else {
9522         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9523         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9524     }
9525 
9526     /*
9527      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9528      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9529      * FIQ bank for r8-r14.
9530      */
9531     if (mode == ARM_CPU_MODE_FIQ) {
9532         for (i = 24; i < 31; i++) {
9533             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9534         }
9535     } else {
9536         for (i = 24; i < 29; i++) {
9537             env->xregs[i] = env->fiq_regs[i - 24];
9538         }
9539         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9540         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9541     }
9542 
9543     env->pc = env->regs[15];
9544 }
9545 
9546 /*
9547  * Function used to synchronize QEMU's AArch32 register set with AArch64
9548  * register set.  This is necessary when switching between AArch32 and AArch64
9549  * execution state.
9550  */
9551 void aarch64_sync_64_to_32(CPUARMState *env)
9552 {
9553     int i;
9554     uint32_t mode = env->uncached_cpsr & CPSR_M;
9555 
9556     /* We can blanket copy X[0:7] to R[0:7] */
9557     for (i = 0; i < 8; i++) {
9558         env->regs[i] = env->xregs[i];
9559     }
9560 
9561     /*
9562      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9563      * Otherwise, we copy x8-x12 into the banked user regs.
9564      */
9565     if (mode == ARM_CPU_MODE_FIQ) {
9566         for (i = 8; i < 13; i++) {
9567             env->usr_regs[i - 8] = env->xregs[i];
9568         }
9569     } else {
9570         for (i = 8; i < 13; i++) {
9571             env->regs[i] = env->xregs[i];
9572         }
9573     }
9574 
9575     /*
9576      * Registers r13 & r14 depend on the current mode.
9577      * If we are in a given mode, we copy the corresponding x registers to r13
9578      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9579      * for the mode.
9580      */
9581     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9582         env->regs[13] = env->xregs[13];
9583         env->regs[14] = env->xregs[14];
9584     } else {
9585         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9586 
9587         /*
9588          * HYP is an exception in that it does not have its own banked r14 but
9589          * shares the USR r14
9590          */
9591         if (mode == ARM_CPU_MODE_HYP) {
9592             env->regs[14] = env->xregs[14];
9593         } else {
9594             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9595         }
9596     }
9597 
9598     if (mode == ARM_CPU_MODE_HYP) {
9599         env->regs[13] = env->xregs[15];
9600     } else {
9601         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9602     }
9603 
9604     if (mode == ARM_CPU_MODE_IRQ) {
9605         env->regs[14] = env->xregs[16];
9606         env->regs[13] = env->xregs[17];
9607     } else {
9608         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9609         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9610     }
9611 
9612     if (mode == ARM_CPU_MODE_SVC) {
9613         env->regs[14] = env->xregs[18];
9614         env->regs[13] = env->xregs[19];
9615     } else {
9616         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9617         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9618     }
9619 
9620     if (mode == ARM_CPU_MODE_ABT) {
9621         env->regs[14] = env->xregs[20];
9622         env->regs[13] = env->xregs[21];
9623     } else {
9624         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9625         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9626     }
9627 
9628     if (mode == ARM_CPU_MODE_UND) {
9629         env->regs[14] = env->xregs[22];
9630         env->regs[13] = env->xregs[23];
9631     } else {
9632         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9633         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9634     }
9635 
9636     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9637      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9638      * FIQ bank for r8-r14.
9639      */
9640     if (mode == ARM_CPU_MODE_FIQ) {
9641         for (i = 24; i < 31; i++) {
9642             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9643         }
9644     } else {
9645         for (i = 24; i < 29; i++) {
9646             env->fiq_regs[i - 24] = env->xregs[i];
9647         }
9648         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9649         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9650     }
9651 
9652     env->regs[15] = env->pc;
9653 }
9654 
9655 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9656                                    uint32_t mask, uint32_t offset,
9657                                    uint32_t newpc)
9658 {
9659     int new_el;
9660 
9661     /* Change the CPU state so as to actually take the exception. */
9662     switch_mode(env, new_mode);
9663 
9664     /*
9665      * For exceptions taken to AArch32 we must clear the SS bit in both
9666      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9667      */
9668     env->pstate &= ~PSTATE_SS;
9669     env->spsr = cpsr_read(env);
9670     /* Clear IT bits.  */
9671     env->condexec_bits = 0;
9672     /* Switch to the new mode, and to the correct instruction set.  */
9673     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9674 
9675     /* This must be after mode switching. */
9676     new_el = arm_current_el(env);
9677 
9678     /* Set new mode endianness */
9679     env->uncached_cpsr &= ~CPSR_E;
9680     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9681         env->uncached_cpsr |= CPSR_E;
9682     }
9683     /* J and IL must always be cleared for exception entry */
9684     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9685     env->daif |= mask;
9686 
9687     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9688         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9689             env->uncached_cpsr |= CPSR_SSBS;
9690         } else {
9691             env->uncached_cpsr &= ~CPSR_SSBS;
9692         }
9693     }
9694 
9695     if (new_mode == ARM_CPU_MODE_HYP) {
9696         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9697         env->elr_el[2] = env->regs[15];
9698     } else {
9699         /* CPSR.PAN is normally preserved preserved unless...  */
9700         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9701             switch (new_el) {
9702             case 3:
9703                 if (!arm_is_secure_below_el3(env)) {
9704                     /* ... the target is EL3, from non-secure state.  */
9705                     env->uncached_cpsr &= ~CPSR_PAN;
9706                     break;
9707                 }
9708                 /* ... the target is EL3, from secure state ... */
9709                 /* fall through */
9710             case 1:
9711                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9712                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9713                     env->uncached_cpsr |= CPSR_PAN;
9714                 }
9715                 break;
9716             }
9717         }
9718         /*
9719          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9720          * and we should just guard the thumb mode on V4
9721          */
9722         if (arm_feature(env, ARM_FEATURE_V4T)) {
9723             env->thumb =
9724                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9725         }
9726         env->regs[14] = env->regs[15] + offset;
9727     }
9728     env->regs[15] = newpc;
9729     arm_rebuild_hflags(env);
9730 }
9731 
9732 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9733 {
9734     /*
9735      * Handle exception entry to Hyp mode; this is sufficiently
9736      * different to entry to other AArch32 modes that we handle it
9737      * separately here.
9738      *
9739      * The vector table entry used is always the 0x14 Hyp mode entry point,
9740      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9741      * The offset applied to the preferred return address is always zero
9742      * (see DDI0487C.a section G1.12.3).
9743      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9744      */
9745     uint32_t addr, mask;
9746     ARMCPU *cpu = ARM_CPU(cs);
9747     CPUARMState *env = &cpu->env;
9748 
9749     switch (cs->exception_index) {
9750     case EXCP_UDEF:
9751         addr = 0x04;
9752         break;
9753     case EXCP_SWI:
9754         addr = 0x08;
9755         break;
9756     case EXCP_BKPT:
9757         /* Fall through to prefetch abort.  */
9758     case EXCP_PREFETCH_ABORT:
9759         env->cp15.ifar_s = env->exception.vaddress;
9760         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9761                       (uint32_t)env->exception.vaddress);
9762         addr = 0x0c;
9763         break;
9764     case EXCP_DATA_ABORT:
9765         env->cp15.dfar_s = env->exception.vaddress;
9766         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9767                       (uint32_t)env->exception.vaddress);
9768         addr = 0x10;
9769         break;
9770     case EXCP_IRQ:
9771         addr = 0x18;
9772         break;
9773     case EXCP_FIQ:
9774         addr = 0x1c;
9775         break;
9776     case EXCP_HVC:
9777         addr = 0x08;
9778         break;
9779     case EXCP_HYP_TRAP:
9780         addr = 0x14;
9781         break;
9782     default:
9783         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9784     }
9785 
9786     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9787         if (!arm_feature(env, ARM_FEATURE_V8)) {
9788             /*
9789              * QEMU syndrome values are v8-style. v7 has the IL bit
9790              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9791              * If this is a v7 CPU, squash the IL bit in those cases.
9792              */
9793             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9794                 (cs->exception_index == EXCP_DATA_ABORT &&
9795                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9796                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9797                 env->exception.syndrome &= ~ARM_EL_IL;
9798             }
9799         }
9800         env->cp15.esr_el[2] = env->exception.syndrome;
9801     }
9802 
9803     if (arm_current_el(env) != 2 && addr < 0x14) {
9804         addr = 0x14;
9805     }
9806 
9807     mask = 0;
9808     if (!(env->cp15.scr_el3 & SCR_EA)) {
9809         mask |= CPSR_A;
9810     }
9811     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9812         mask |= CPSR_I;
9813     }
9814     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9815         mask |= CPSR_F;
9816     }
9817 
9818     addr += env->cp15.hvbar;
9819 
9820     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9821 }
9822 
9823 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9824 {
9825     ARMCPU *cpu = ARM_CPU(cs);
9826     CPUARMState *env = &cpu->env;
9827     uint32_t addr;
9828     uint32_t mask;
9829     int new_mode;
9830     uint32_t offset;
9831     uint32_t moe;
9832 
9833     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9834     switch (syn_get_ec(env->exception.syndrome)) {
9835     case EC_BREAKPOINT:
9836     case EC_BREAKPOINT_SAME_EL:
9837         moe = 1;
9838         break;
9839     case EC_WATCHPOINT:
9840     case EC_WATCHPOINT_SAME_EL:
9841         moe = 10;
9842         break;
9843     case EC_AA32_BKPT:
9844         moe = 3;
9845         break;
9846     case EC_VECTORCATCH:
9847         moe = 5;
9848         break;
9849     default:
9850         moe = 0;
9851         break;
9852     }
9853 
9854     if (moe) {
9855         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9856     }
9857 
9858     if (env->exception.target_el == 2) {
9859         arm_cpu_do_interrupt_aarch32_hyp(cs);
9860         return;
9861     }
9862 
9863     switch (cs->exception_index) {
9864     case EXCP_UDEF:
9865         new_mode = ARM_CPU_MODE_UND;
9866         addr = 0x04;
9867         mask = CPSR_I;
9868         if (env->thumb)
9869             offset = 2;
9870         else
9871             offset = 4;
9872         break;
9873     case EXCP_SWI:
9874         new_mode = ARM_CPU_MODE_SVC;
9875         addr = 0x08;
9876         mask = CPSR_I;
9877         /* The PC already points to the next instruction.  */
9878         offset = 0;
9879         break;
9880     case EXCP_BKPT:
9881         /* Fall through to prefetch abort.  */
9882     case EXCP_PREFETCH_ABORT:
9883         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9884         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9885         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9886                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9887         new_mode = ARM_CPU_MODE_ABT;
9888         addr = 0x0c;
9889         mask = CPSR_A | CPSR_I;
9890         offset = 4;
9891         break;
9892     case EXCP_DATA_ABORT:
9893         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9894         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9895         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9896                       env->exception.fsr,
9897                       (uint32_t)env->exception.vaddress);
9898         new_mode = ARM_CPU_MODE_ABT;
9899         addr = 0x10;
9900         mask = CPSR_A | CPSR_I;
9901         offset = 8;
9902         break;
9903     case EXCP_IRQ:
9904         new_mode = ARM_CPU_MODE_IRQ;
9905         addr = 0x18;
9906         /* Disable IRQ and imprecise data aborts.  */
9907         mask = CPSR_A | CPSR_I;
9908         offset = 4;
9909         if (env->cp15.scr_el3 & SCR_IRQ) {
9910             /* IRQ routed to monitor mode */
9911             new_mode = ARM_CPU_MODE_MON;
9912             mask |= CPSR_F;
9913         }
9914         break;
9915     case EXCP_FIQ:
9916         new_mode = ARM_CPU_MODE_FIQ;
9917         addr = 0x1c;
9918         /* Disable FIQ, IRQ and imprecise data aborts.  */
9919         mask = CPSR_A | CPSR_I | CPSR_F;
9920         if (env->cp15.scr_el3 & SCR_FIQ) {
9921             /* FIQ routed to monitor mode */
9922             new_mode = ARM_CPU_MODE_MON;
9923         }
9924         offset = 4;
9925         break;
9926     case EXCP_VIRQ:
9927         new_mode = ARM_CPU_MODE_IRQ;
9928         addr = 0x18;
9929         /* Disable IRQ and imprecise data aborts.  */
9930         mask = CPSR_A | CPSR_I;
9931         offset = 4;
9932         break;
9933     case EXCP_VFIQ:
9934         new_mode = ARM_CPU_MODE_FIQ;
9935         addr = 0x1c;
9936         /* Disable FIQ, IRQ and imprecise data aborts.  */
9937         mask = CPSR_A | CPSR_I | CPSR_F;
9938         offset = 4;
9939         break;
9940     case EXCP_VSERR:
9941         {
9942             /*
9943              * Note that this is reported as a data abort, but the DFAR
9944              * has an UNKNOWN value.  Construct the SError syndrome from
9945              * AET and ExT fields.
9946              */
9947             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9948 
9949             if (extended_addresses_enabled(env)) {
9950                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9951             } else {
9952                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9953             }
9954             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9955             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9956             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9957                           env->exception.fsr);
9958 
9959             new_mode = ARM_CPU_MODE_ABT;
9960             addr = 0x10;
9961             mask = CPSR_A | CPSR_I;
9962             offset = 8;
9963         }
9964         break;
9965     case EXCP_SMC:
9966         new_mode = ARM_CPU_MODE_MON;
9967         addr = 0x08;
9968         mask = CPSR_A | CPSR_I | CPSR_F;
9969         offset = 0;
9970         break;
9971     default:
9972         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9973         return; /* Never happens.  Keep compiler happy.  */
9974     }
9975 
9976     if (new_mode == ARM_CPU_MODE_MON) {
9977         addr += env->cp15.mvbar;
9978     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9979         /* High vectors. When enabled, base address cannot be remapped. */
9980         addr += 0xffff0000;
9981     } else {
9982         /* ARM v7 architectures provide a vector base address register to remap
9983          * the interrupt vector table.
9984          * This register is only followed in non-monitor mode, and is banked.
9985          * Note: only bits 31:5 are valid.
9986          */
9987         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9988     }
9989 
9990     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9991         env->cp15.scr_el3 &= ~SCR_NS;
9992     }
9993 
9994     take_aarch32_exception(env, new_mode, mask, offset, addr);
9995 }
9996 
9997 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9998 {
9999     /*
10000      * Return the register number of the AArch64 view of the AArch32
10001      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10002      * be that of the AArch32 mode the exception came from.
10003      */
10004     int mode = env->uncached_cpsr & CPSR_M;
10005 
10006     switch (aarch32_reg) {
10007     case 0 ... 7:
10008         return aarch32_reg;
10009     case 8 ... 12:
10010         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10011     case 13:
10012         switch (mode) {
10013         case ARM_CPU_MODE_USR:
10014         case ARM_CPU_MODE_SYS:
10015             return 13;
10016         case ARM_CPU_MODE_HYP:
10017             return 15;
10018         case ARM_CPU_MODE_IRQ:
10019             return 17;
10020         case ARM_CPU_MODE_SVC:
10021             return 19;
10022         case ARM_CPU_MODE_ABT:
10023             return 21;
10024         case ARM_CPU_MODE_UND:
10025             return 23;
10026         case ARM_CPU_MODE_FIQ:
10027             return 29;
10028         default:
10029             g_assert_not_reached();
10030         }
10031     case 14:
10032         switch (mode) {
10033         case ARM_CPU_MODE_USR:
10034         case ARM_CPU_MODE_SYS:
10035         case ARM_CPU_MODE_HYP:
10036             return 14;
10037         case ARM_CPU_MODE_IRQ:
10038             return 16;
10039         case ARM_CPU_MODE_SVC:
10040             return 18;
10041         case ARM_CPU_MODE_ABT:
10042             return 20;
10043         case ARM_CPU_MODE_UND:
10044             return 22;
10045         case ARM_CPU_MODE_FIQ:
10046             return 30;
10047         default:
10048             g_assert_not_reached();
10049         }
10050     case 15:
10051         return 31;
10052     default:
10053         g_assert_not_reached();
10054     }
10055 }
10056 
10057 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10058 {
10059     uint32_t ret = cpsr_read(env);
10060 
10061     /* Move DIT to the correct location for SPSR_ELx */
10062     if (ret & CPSR_DIT) {
10063         ret &= ~CPSR_DIT;
10064         ret |= PSTATE_DIT;
10065     }
10066     /* Merge PSTATE.SS into SPSR_ELx */
10067     ret |= env->pstate & PSTATE_SS;
10068 
10069     return ret;
10070 }
10071 
10072 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10073 {
10074     /* Return true if this syndrome value is a synchronous external abort */
10075     switch (syn_get_ec(syndrome)) {
10076     case EC_INSNABORT:
10077     case EC_INSNABORT_SAME_EL:
10078     case EC_DATAABORT:
10079     case EC_DATAABORT_SAME_EL:
10080         /* Look at fault status code for all the synchronous ext abort cases */
10081         switch (syndrome & 0x3f) {
10082         case 0x10:
10083         case 0x13:
10084         case 0x14:
10085         case 0x15:
10086         case 0x16:
10087         case 0x17:
10088             return true;
10089         default:
10090             return false;
10091         }
10092     default:
10093         return false;
10094     }
10095 }
10096 
10097 /* Handle exception entry to a target EL which is using AArch64 */
10098 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10099 {
10100     ARMCPU *cpu = ARM_CPU(cs);
10101     CPUARMState *env = &cpu->env;
10102     unsigned int new_el = env->exception.target_el;
10103     target_ulong addr = env->cp15.vbar_el[new_el];
10104     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10105     unsigned int old_mode;
10106     unsigned int cur_el = arm_current_el(env);
10107     int rt;
10108 
10109     /*
10110      * Note that new_el can never be 0.  If cur_el is 0, then
10111      * el0_a64 is is_a64(), else el0_a64 is ignored.
10112      */
10113     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10114 
10115     if (cur_el < new_el) {
10116         /* Entry vector offset depends on whether the implemented EL
10117          * immediately lower than the target level is using AArch32 or AArch64
10118          */
10119         bool is_aa64;
10120         uint64_t hcr;
10121 
10122         switch (new_el) {
10123         case 3:
10124             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10125             break;
10126         case 2:
10127             hcr = arm_hcr_el2_eff(env);
10128             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10129                 is_aa64 = (hcr & HCR_RW) != 0;
10130                 break;
10131             }
10132             /* fall through */
10133         case 1:
10134             is_aa64 = is_a64(env);
10135             break;
10136         default:
10137             g_assert_not_reached();
10138         }
10139 
10140         if (is_aa64) {
10141             addr += 0x400;
10142         } else {
10143             addr += 0x600;
10144         }
10145     } else if (pstate_read(env) & PSTATE_SP) {
10146         addr += 0x200;
10147     }
10148 
10149     switch (cs->exception_index) {
10150     case EXCP_PREFETCH_ABORT:
10151     case EXCP_DATA_ABORT:
10152         /*
10153          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10154          * to be taken to the SError vector entrypoint.
10155          */
10156         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10157             syndrome_is_sync_extabt(env->exception.syndrome)) {
10158             addr += 0x180;
10159         }
10160         env->cp15.far_el[new_el] = env->exception.vaddress;
10161         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10162                       env->cp15.far_el[new_el]);
10163         /* fall through */
10164     case EXCP_BKPT:
10165     case EXCP_UDEF:
10166     case EXCP_SWI:
10167     case EXCP_HVC:
10168     case EXCP_HYP_TRAP:
10169     case EXCP_SMC:
10170         switch (syn_get_ec(env->exception.syndrome)) {
10171         case EC_ADVSIMDFPACCESSTRAP:
10172             /*
10173              * QEMU internal FP/SIMD syndromes from AArch32 include the
10174              * TA and coproc fields which are only exposed if the exception
10175              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10176              * AArch64 format syndrome.
10177              */
10178             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10179             break;
10180         case EC_CP14RTTRAP:
10181         case EC_CP15RTTRAP:
10182         case EC_CP14DTTRAP:
10183             /*
10184              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10185              * the raw register field from the insn; when taking this to
10186              * AArch64 we must convert it to the AArch64 view of the register
10187              * number. Notice that we read a 4-bit AArch32 register number and
10188              * write back a 5-bit AArch64 one.
10189              */
10190             rt = extract32(env->exception.syndrome, 5, 4);
10191             rt = aarch64_regnum(env, rt);
10192             env->exception.syndrome = deposit32(env->exception.syndrome,
10193                                                 5, 5, rt);
10194             break;
10195         case EC_CP15RRTTRAP:
10196         case EC_CP14RRTTRAP:
10197             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10198             rt = extract32(env->exception.syndrome, 5, 4);
10199             rt = aarch64_regnum(env, rt);
10200             env->exception.syndrome = deposit32(env->exception.syndrome,
10201                                                 5, 5, rt);
10202             rt = extract32(env->exception.syndrome, 10, 4);
10203             rt = aarch64_regnum(env, rt);
10204             env->exception.syndrome = deposit32(env->exception.syndrome,
10205                                                 10, 5, rt);
10206             break;
10207         }
10208         env->cp15.esr_el[new_el] = env->exception.syndrome;
10209         break;
10210     case EXCP_IRQ:
10211     case EXCP_VIRQ:
10212         addr += 0x80;
10213         break;
10214     case EXCP_FIQ:
10215     case EXCP_VFIQ:
10216         addr += 0x100;
10217         break;
10218     case EXCP_VSERR:
10219         addr += 0x180;
10220         /* Construct the SError syndrome from IDS and ISS fields. */
10221         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10222         env->cp15.esr_el[new_el] = env->exception.syndrome;
10223         break;
10224     default:
10225         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10226     }
10227 
10228     if (is_a64(env)) {
10229         old_mode = pstate_read(env);
10230         aarch64_save_sp(env, arm_current_el(env));
10231         env->elr_el[new_el] = env->pc;
10232     } else {
10233         old_mode = cpsr_read_for_spsr_elx(env);
10234         env->elr_el[new_el] = env->regs[15];
10235 
10236         aarch64_sync_32_to_64(env);
10237 
10238         env->condexec_bits = 0;
10239     }
10240     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10241 
10242     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10243                   env->elr_el[new_el]);
10244 
10245     if (cpu_isar_feature(aa64_pan, cpu)) {
10246         /* The value of PSTATE.PAN is normally preserved, except when ... */
10247         new_mode |= old_mode & PSTATE_PAN;
10248         switch (new_el) {
10249         case 2:
10250             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10251             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10252                 != (HCR_E2H | HCR_TGE)) {
10253                 break;
10254             }
10255             /* fall through */
10256         case 1:
10257             /* ... the target is EL1 ... */
10258             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10259             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10260                 new_mode |= PSTATE_PAN;
10261             }
10262             break;
10263         }
10264     }
10265     if (cpu_isar_feature(aa64_mte, cpu)) {
10266         new_mode |= PSTATE_TCO;
10267     }
10268 
10269     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10270         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10271             new_mode |= PSTATE_SSBS;
10272         } else {
10273             new_mode &= ~PSTATE_SSBS;
10274         }
10275     }
10276 
10277     pstate_write(env, PSTATE_DAIF | new_mode);
10278     env->aarch64 = true;
10279     aarch64_restore_sp(env, new_el);
10280     helper_rebuild_hflags_a64(env, new_el);
10281 
10282     env->pc = addr;
10283 
10284     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10285                   new_el, env->pc, pstate_read(env));
10286 }
10287 
10288 /*
10289  * Do semihosting call and set the appropriate return value. All the
10290  * permission and validity checks have been done at translate time.
10291  *
10292  * We only see semihosting exceptions in TCG only as they are not
10293  * trapped to the hypervisor in KVM.
10294  */
10295 #ifdef CONFIG_TCG
10296 static void handle_semihosting(CPUState *cs)
10297 {
10298     ARMCPU *cpu = ARM_CPU(cs);
10299     CPUARMState *env = &cpu->env;
10300 
10301     if (is_a64(env)) {
10302         qemu_log_mask(CPU_LOG_INT,
10303                       "...handling as semihosting call 0x%" PRIx64 "\n",
10304                       env->xregs[0]);
10305         do_common_semihosting(cs);
10306         env->pc += 4;
10307     } else {
10308         qemu_log_mask(CPU_LOG_INT,
10309                       "...handling as semihosting call 0x%x\n",
10310                       env->regs[0]);
10311         do_common_semihosting(cs);
10312         env->regs[15] += env->thumb ? 2 : 4;
10313     }
10314 }
10315 #endif
10316 
10317 /* Handle a CPU exception for A and R profile CPUs.
10318  * Do any appropriate logging, handle PSCI calls, and then hand off
10319  * to the AArch64-entry or AArch32-entry function depending on the
10320  * target exception level's register width.
10321  *
10322  * Note: this is used for both TCG (as the do_interrupt tcg op),
10323  *       and KVM to re-inject guest debug exceptions, and to
10324  *       inject a Synchronous-External-Abort.
10325  */
10326 void arm_cpu_do_interrupt(CPUState *cs)
10327 {
10328     ARMCPU *cpu = ARM_CPU(cs);
10329     CPUARMState *env = &cpu->env;
10330     unsigned int new_el = env->exception.target_el;
10331 
10332     assert(!arm_feature(env, ARM_FEATURE_M));
10333 
10334     arm_log_exception(cs);
10335     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10336                   new_el);
10337     if (qemu_loglevel_mask(CPU_LOG_INT)
10338         && !excp_is_internal(cs->exception_index)) {
10339         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10340                       syn_get_ec(env->exception.syndrome),
10341                       env->exception.syndrome);
10342     }
10343 
10344     if (arm_is_psci_call(cpu, cs->exception_index)) {
10345         arm_handle_psci_call(cpu);
10346         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10347         return;
10348     }
10349 
10350     /*
10351      * Semihosting semantics depend on the register width of the code
10352      * that caused the exception, not the target exception level, so
10353      * must be handled here.
10354      */
10355 #ifdef CONFIG_TCG
10356     if (cs->exception_index == EXCP_SEMIHOST) {
10357         handle_semihosting(cs);
10358         return;
10359     }
10360 #endif
10361 
10362     /* Hooks may change global state so BQL should be held, also the
10363      * BQL needs to be held for any modification of
10364      * cs->interrupt_request.
10365      */
10366     g_assert(qemu_mutex_iothread_locked());
10367 
10368     arm_call_pre_el_change_hook(cpu);
10369 
10370     assert(!excp_is_internal(cs->exception_index));
10371     if (arm_el_is_aa64(env, new_el)) {
10372         arm_cpu_do_interrupt_aarch64(cs);
10373     } else {
10374         arm_cpu_do_interrupt_aarch32(cs);
10375     }
10376 
10377     arm_call_el_change_hook(cpu);
10378 
10379     if (!kvm_enabled()) {
10380         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10381     }
10382 }
10383 #endif /* !CONFIG_USER_ONLY */
10384 
10385 uint64_t arm_sctlr(CPUARMState *env, int el)
10386 {
10387     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10388     if (el == 0) {
10389         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10390         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10391     }
10392     return env->cp15.sctlr_el[el];
10393 }
10394 
10395 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10396 {
10397     if (regime_has_2_ranges(mmu_idx)) {
10398         return extract64(tcr, 37, 2);
10399     } else if (regime_is_stage2(mmu_idx)) {
10400         return 0; /* VTCR_EL2 */
10401     } else {
10402         /* Replicate the single TBI bit so we always have 2 bits.  */
10403         return extract32(tcr, 20, 1) * 3;
10404     }
10405 }
10406 
10407 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10408 {
10409     if (regime_has_2_ranges(mmu_idx)) {
10410         return extract64(tcr, 51, 2);
10411     } else if (regime_is_stage2(mmu_idx)) {
10412         return 0; /* VTCR_EL2 */
10413     } else {
10414         /* Replicate the single TBID bit so we always have 2 bits.  */
10415         return extract32(tcr, 29, 1) * 3;
10416     }
10417 }
10418 
10419 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10420 {
10421     if (regime_has_2_ranges(mmu_idx)) {
10422         return extract64(tcr, 57, 2);
10423     } else {
10424         /* Replicate the single TCMA bit so we always have 2 bits.  */
10425         return extract32(tcr, 30, 1) * 3;
10426     }
10427 }
10428 
10429 static ARMGranuleSize tg0_to_gran_size(int tg)
10430 {
10431     switch (tg) {
10432     case 0:
10433         return Gran4K;
10434     case 1:
10435         return Gran64K;
10436     case 2:
10437         return Gran16K;
10438     default:
10439         return GranInvalid;
10440     }
10441 }
10442 
10443 static ARMGranuleSize tg1_to_gran_size(int tg)
10444 {
10445     switch (tg) {
10446     case 1:
10447         return Gran16K;
10448     case 2:
10449         return Gran4K;
10450     case 3:
10451         return Gran64K;
10452     default:
10453         return GranInvalid;
10454     }
10455 }
10456 
10457 static inline bool have4k(ARMCPU *cpu, bool stage2)
10458 {
10459     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10460         : cpu_isar_feature(aa64_tgran4, cpu);
10461 }
10462 
10463 static inline bool have16k(ARMCPU *cpu, bool stage2)
10464 {
10465     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10466         : cpu_isar_feature(aa64_tgran16, cpu);
10467 }
10468 
10469 static inline bool have64k(ARMCPU *cpu, bool stage2)
10470 {
10471     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10472         : cpu_isar_feature(aa64_tgran64, cpu);
10473 }
10474 
10475 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10476                                          bool stage2)
10477 {
10478     switch (gran) {
10479     case Gran4K:
10480         if (have4k(cpu, stage2)) {
10481             return gran;
10482         }
10483         break;
10484     case Gran16K:
10485         if (have16k(cpu, stage2)) {
10486             return gran;
10487         }
10488         break;
10489     case Gran64K:
10490         if (have64k(cpu, stage2)) {
10491             return gran;
10492         }
10493         break;
10494     case GranInvalid:
10495         break;
10496     }
10497     /*
10498      * If the guest selects a granule size that isn't implemented,
10499      * the architecture requires that we behave as if it selected one
10500      * that is (with an IMPDEF choice of which one to pick). We choose
10501      * to implement the smallest supported granule size.
10502      */
10503     if (have4k(cpu, stage2)) {
10504         return Gran4K;
10505     }
10506     if (have16k(cpu, stage2)) {
10507         return Gran16K;
10508     }
10509     assert(have64k(cpu, stage2));
10510     return Gran64K;
10511 }
10512 
10513 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10514                                    ARMMMUIdx mmu_idx, bool data)
10515 {
10516     uint64_t tcr = regime_tcr(env, mmu_idx);
10517     bool epd, hpd, tsz_oob, ds, ha, hd;
10518     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10519     ARMGranuleSize gran;
10520     ARMCPU *cpu = env_archcpu(env);
10521     bool stage2 = regime_is_stage2(mmu_idx);
10522 
10523     if (!regime_has_2_ranges(mmu_idx)) {
10524         select = 0;
10525         tsz = extract32(tcr, 0, 6);
10526         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10527         if (stage2) {
10528             /* VTCR_EL2 */
10529             hpd = false;
10530         } else {
10531             hpd = extract32(tcr, 24, 1);
10532         }
10533         epd = false;
10534         sh = extract32(tcr, 12, 2);
10535         ps = extract32(tcr, 16, 3);
10536         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
10537         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10538         ds = extract64(tcr, 32, 1);
10539     } else {
10540         bool e0pd;
10541 
10542         /*
10543          * Bit 55 is always between the two regions, and is canonical for
10544          * determining if address tagging is enabled.
10545          */
10546         select = extract64(va, 55, 1);
10547         if (!select) {
10548             tsz = extract32(tcr, 0, 6);
10549             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10550             epd = extract32(tcr, 7, 1);
10551             sh = extract32(tcr, 12, 2);
10552             hpd = extract64(tcr, 41, 1);
10553             e0pd = extract64(tcr, 55, 1);
10554         } else {
10555             tsz = extract32(tcr, 16, 6);
10556             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10557             epd = extract32(tcr, 23, 1);
10558             sh = extract32(tcr, 28, 2);
10559             hpd = extract64(tcr, 42, 1);
10560             e0pd = extract64(tcr, 56, 1);
10561         }
10562         ps = extract64(tcr, 32, 3);
10563         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
10564         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10565         ds = extract64(tcr, 59, 1);
10566 
10567         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
10568             regime_is_user(env, mmu_idx)) {
10569             epd = true;
10570         }
10571     }
10572 
10573     gran = sanitize_gran_size(cpu, gran, stage2);
10574 
10575     if (cpu_isar_feature(aa64_st, cpu)) {
10576         max_tsz = 48 - (gran == Gran64K);
10577     } else {
10578         max_tsz = 39;
10579     }
10580 
10581     /*
10582      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10583      * adjust the effective value of DS, as documented.
10584      */
10585     min_tsz = 16;
10586     if (gran == Gran64K) {
10587         if (cpu_isar_feature(aa64_lva, cpu)) {
10588             min_tsz = 12;
10589         }
10590         ds = false;
10591     } else if (ds) {
10592         if (regime_is_stage2(mmu_idx)) {
10593             if (gran == Gran16K) {
10594                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10595             } else {
10596                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10597             }
10598         } else {
10599             if (gran == Gran16K) {
10600                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10601             } else {
10602                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10603             }
10604         }
10605         if (ds) {
10606             min_tsz = 12;
10607         }
10608     }
10609 
10610     if (tsz > max_tsz) {
10611         tsz = max_tsz;
10612         tsz_oob = true;
10613     } else if (tsz < min_tsz) {
10614         tsz = min_tsz;
10615         tsz_oob = true;
10616     } else {
10617         tsz_oob = false;
10618     }
10619 
10620     /* Present TBI as a composite with TBID.  */
10621     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10622     if (!data) {
10623         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10624     }
10625     tbi = (tbi >> select) & 1;
10626 
10627     return (ARMVAParameters) {
10628         .tsz = tsz,
10629         .ps = ps,
10630         .sh = sh,
10631         .select = select,
10632         .tbi = tbi,
10633         .epd = epd,
10634         .hpd = hpd,
10635         .tsz_oob = tsz_oob,
10636         .ds = ds,
10637         .ha = ha,
10638         .hd = ha && hd,
10639         .gran = gran,
10640     };
10641 }
10642 
10643 /* Note that signed overflow is undefined in C.  The following routines are
10644    careful to use unsigned types where modulo arithmetic is required.
10645    Failure to do so _will_ break on newer gcc.  */
10646 
10647 /* Signed saturating arithmetic.  */
10648 
10649 /* Perform 16-bit signed saturating addition.  */
10650 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10651 {
10652     uint16_t res;
10653 
10654     res = a + b;
10655     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10656         if (a & 0x8000)
10657             res = 0x8000;
10658         else
10659             res = 0x7fff;
10660     }
10661     return res;
10662 }
10663 
10664 /* Perform 8-bit signed saturating addition.  */
10665 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10666 {
10667     uint8_t res;
10668 
10669     res = a + b;
10670     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10671         if (a & 0x80)
10672             res = 0x80;
10673         else
10674             res = 0x7f;
10675     }
10676     return res;
10677 }
10678 
10679 /* Perform 16-bit signed saturating subtraction.  */
10680 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10681 {
10682     uint16_t res;
10683 
10684     res = a - b;
10685     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10686         if (a & 0x8000)
10687             res = 0x8000;
10688         else
10689             res = 0x7fff;
10690     }
10691     return res;
10692 }
10693 
10694 /* Perform 8-bit signed saturating subtraction.  */
10695 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10696 {
10697     uint8_t res;
10698 
10699     res = a - b;
10700     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10701         if (a & 0x80)
10702             res = 0x80;
10703         else
10704             res = 0x7f;
10705     }
10706     return res;
10707 }
10708 
10709 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10710 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10711 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10712 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10713 #define PFX q
10714 
10715 #include "op_addsub.h"
10716 
10717 /* Unsigned saturating arithmetic.  */
10718 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10719 {
10720     uint16_t res;
10721     res = a + b;
10722     if (res < a)
10723         res = 0xffff;
10724     return res;
10725 }
10726 
10727 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10728 {
10729     if (a > b)
10730         return a - b;
10731     else
10732         return 0;
10733 }
10734 
10735 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10736 {
10737     uint8_t res;
10738     res = a + b;
10739     if (res < a)
10740         res = 0xff;
10741     return res;
10742 }
10743 
10744 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10745 {
10746     if (a > b)
10747         return a - b;
10748     else
10749         return 0;
10750 }
10751 
10752 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10753 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10754 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10755 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10756 #define PFX uq
10757 
10758 #include "op_addsub.h"
10759 
10760 /* Signed modulo arithmetic.  */
10761 #define SARITH16(a, b, n, op) do { \
10762     int32_t sum; \
10763     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10764     RESULT(sum, n, 16); \
10765     if (sum >= 0) \
10766         ge |= 3 << (n * 2); \
10767     } while(0)
10768 
10769 #define SARITH8(a, b, n, op) do { \
10770     int32_t sum; \
10771     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10772     RESULT(sum, n, 8); \
10773     if (sum >= 0) \
10774         ge |= 1 << n; \
10775     } while(0)
10776 
10777 
10778 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10779 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10780 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10781 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10782 #define PFX s
10783 #define ARITH_GE
10784 
10785 #include "op_addsub.h"
10786 
10787 /* Unsigned modulo arithmetic.  */
10788 #define ADD16(a, b, n) do { \
10789     uint32_t sum; \
10790     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10791     RESULT(sum, n, 16); \
10792     if ((sum >> 16) == 1) \
10793         ge |= 3 << (n * 2); \
10794     } while(0)
10795 
10796 #define ADD8(a, b, n) do { \
10797     uint32_t sum; \
10798     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10799     RESULT(sum, n, 8); \
10800     if ((sum >> 8) == 1) \
10801         ge |= 1 << n; \
10802     } while(0)
10803 
10804 #define SUB16(a, b, n) do { \
10805     uint32_t sum; \
10806     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10807     RESULT(sum, n, 16); \
10808     if ((sum >> 16) == 0) \
10809         ge |= 3 << (n * 2); \
10810     } while(0)
10811 
10812 #define SUB8(a, b, n) do { \
10813     uint32_t sum; \
10814     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10815     RESULT(sum, n, 8); \
10816     if ((sum >> 8) == 0) \
10817         ge |= 1 << n; \
10818     } while(0)
10819 
10820 #define PFX u
10821 #define ARITH_GE
10822 
10823 #include "op_addsub.h"
10824 
10825 /* Halved signed arithmetic.  */
10826 #define ADD16(a, b, n) \
10827   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10828 #define SUB16(a, b, n) \
10829   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10830 #define ADD8(a, b, n) \
10831   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10832 #define SUB8(a, b, n) \
10833   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10834 #define PFX sh
10835 
10836 #include "op_addsub.h"
10837 
10838 /* Halved unsigned arithmetic.  */
10839 #define ADD16(a, b, n) \
10840   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10841 #define SUB16(a, b, n) \
10842   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10843 #define ADD8(a, b, n) \
10844   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10845 #define SUB8(a, b, n) \
10846   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10847 #define PFX uh
10848 
10849 #include "op_addsub.h"
10850 
10851 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10852 {
10853     if (a > b)
10854         return a - b;
10855     else
10856         return b - a;
10857 }
10858 
10859 /* Unsigned sum of absolute byte differences.  */
10860 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10861 {
10862     uint32_t sum;
10863     sum = do_usad(a, b);
10864     sum += do_usad(a >> 8, b >> 8);
10865     sum += do_usad(a >> 16, b >> 16);
10866     sum += do_usad(a >> 24, b >> 24);
10867     return sum;
10868 }
10869 
10870 /* For ARMv6 SEL instruction.  */
10871 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10872 {
10873     uint32_t mask;
10874 
10875     mask = 0;
10876     if (flags & 1)
10877         mask |= 0xff;
10878     if (flags & 2)
10879         mask |= 0xff00;
10880     if (flags & 4)
10881         mask |= 0xff0000;
10882     if (flags & 8)
10883         mask |= 0xff000000;
10884     return (a & mask) | (b & ~mask);
10885 }
10886 
10887 /* CRC helpers.
10888  * The upper bytes of val (above the number specified by 'bytes') must have
10889  * been zeroed out by the caller.
10890  */
10891 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10892 {
10893     uint8_t buf[4];
10894 
10895     stl_le_p(buf, val);
10896 
10897     /* zlib crc32 converts the accumulator and output to one's complement.  */
10898     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10899 }
10900 
10901 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10902 {
10903     uint8_t buf[4];
10904 
10905     stl_le_p(buf, val);
10906 
10907     /* Linux crc32c converts the output to one's complement.  */
10908     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10909 }
10910 
10911 /* Return the exception level to which FP-disabled exceptions should
10912  * be taken, or 0 if FP is enabled.
10913  */
10914 int fp_exception_el(CPUARMState *env, int cur_el)
10915 {
10916 #ifndef CONFIG_USER_ONLY
10917     uint64_t hcr_el2;
10918 
10919     /* CPACR and the CPTR registers don't exist before v6, so FP is
10920      * always accessible
10921      */
10922     if (!arm_feature(env, ARM_FEATURE_V6)) {
10923         return 0;
10924     }
10925 
10926     if (arm_feature(env, ARM_FEATURE_M)) {
10927         /* CPACR can cause a NOCP UsageFault taken to current security state */
10928         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10929             return 1;
10930         }
10931 
10932         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10933             if (!extract32(env->v7m.nsacr, 10, 1)) {
10934                 /* FP insns cause a NOCP UsageFault taken to Secure */
10935                 return 3;
10936             }
10937         }
10938 
10939         return 0;
10940     }
10941 
10942     hcr_el2 = arm_hcr_el2_eff(env);
10943 
10944     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10945      * 0, 2 : trap EL0 and EL1/PL1 accesses
10946      * 1    : trap only EL0 accesses
10947      * 3    : trap no accesses
10948      * This register is ignored if E2H+TGE are both set.
10949      */
10950     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10951         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10952 
10953         switch (fpen) {
10954         case 1:
10955             if (cur_el != 0) {
10956                 break;
10957             }
10958             /* fall through */
10959         case 0:
10960         case 2:
10961             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10962             if (!arm_el_is_aa64(env, 3)
10963                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10964                 return 3;
10965             }
10966             if (cur_el <= 1) {
10967                 return 1;
10968             }
10969             break;
10970         }
10971     }
10972 
10973     /*
10974      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10975      * to control non-secure access to the FPU. It doesn't have any
10976      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10977      */
10978     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10979          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10980         if (!extract32(env->cp15.nsacr, 10, 1)) {
10981             /* FP insns act as UNDEF */
10982             return cur_el == 2 ? 2 : 1;
10983         }
10984     }
10985 
10986     /*
10987      * CPTR_EL2 is present in v7VE or v8, and changes format
10988      * with HCR_EL2.E2H (regardless of TGE).
10989      */
10990     if (cur_el <= 2) {
10991         if (hcr_el2 & HCR_E2H) {
10992             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10993             case 1:
10994                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10995                     break;
10996                 }
10997                 /* fall through */
10998             case 0:
10999             case 2:
11000                 return 2;
11001             }
11002         } else if (arm_is_el2_enabled(env)) {
11003             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11004                 return 2;
11005             }
11006         }
11007     }
11008 
11009     /* CPTR_EL3 : present in v8 */
11010     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11011         /* Trap all FP ops to EL3 */
11012         return 3;
11013     }
11014 #endif
11015     return 0;
11016 }
11017 
11018 /* Return the exception level we're running at if this is our mmu_idx */
11019 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11020 {
11021     if (mmu_idx & ARM_MMU_IDX_M) {
11022         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11023     }
11024 
11025     switch (mmu_idx) {
11026     case ARMMMUIdx_E10_0:
11027     case ARMMMUIdx_E20_0:
11028         return 0;
11029     case ARMMMUIdx_E10_1:
11030     case ARMMMUIdx_E10_1_PAN:
11031         return 1;
11032     case ARMMMUIdx_E2:
11033     case ARMMMUIdx_E20_2:
11034     case ARMMMUIdx_E20_2_PAN:
11035         return 2;
11036     case ARMMMUIdx_E3:
11037         return 3;
11038     default:
11039         g_assert_not_reached();
11040     }
11041 }
11042 
11043 #ifndef CONFIG_TCG
11044 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11045 {
11046     g_assert_not_reached();
11047 }
11048 #endif
11049 
11050 static bool arm_pan_enabled(CPUARMState *env)
11051 {
11052     if (is_a64(env)) {
11053         return env->pstate & PSTATE_PAN;
11054     } else {
11055         return env->uncached_cpsr & CPSR_PAN;
11056     }
11057 }
11058 
11059 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11060 {
11061     ARMMMUIdx idx;
11062     uint64_t hcr;
11063 
11064     if (arm_feature(env, ARM_FEATURE_M)) {
11065         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11066     }
11067 
11068     /* See ARM pseudo-function ELIsInHost.  */
11069     switch (el) {
11070     case 0:
11071         hcr = arm_hcr_el2_eff(env);
11072         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11073             idx = ARMMMUIdx_E20_0;
11074         } else {
11075             idx = ARMMMUIdx_E10_0;
11076         }
11077         break;
11078     case 1:
11079         if (arm_pan_enabled(env)) {
11080             idx = ARMMMUIdx_E10_1_PAN;
11081         } else {
11082             idx = ARMMMUIdx_E10_1;
11083         }
11084         break;
11085     case 2:
11086         /* Note that TGE does not apply at EL2.  */
11087         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11088             if (arm_pan_enabled(env)) {
11089                 idx = ARMMMUIdx_E20_2_PAN;
11090             } else {
11091                 idx = ARMMMUIdx_E20_2;
11092             }
11093         } else {
11094             idx = ARMMMUIdx_E2;
11095         }
11096         break;
11097     case 3:
11098         return ARMMMUIdx_E3;
11099     default:
11100         g_assert_not_reached();
11101     }
11102 
11103     return idx;
11104 }
11105 
11106 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11107 {
11108     return arm_mmu_idx_el(env, arm_current_el(env));
11109 }
11110 
11111 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11112                                            ARMMMUIdx mmu_idx,
11113                                            CPUARMTBFlags flags)
11114 {
11115     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11116     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11117 
11118     if (arm_singlestep_active(env)) {
11119         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11120     }
11121     return flags;
11122 }
11123 
11124 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11125                                               ARMMMUIdx mmu_idx,
11126                                               CPUARMTBFlags flags)
11127 {
11128     bool sctlr_b = arm_sctlr_b(env);
11129 
11130     if (sctlr_b) {
11131         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11132     }
11133     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11134         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11135     }
11136     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11137 
11138     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11139 }
11140 
11141 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11142                                         ARMMMUIdx mmu_idx)
11143 {
11144     CPUARMTBFlags flags = {};
11145     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11146 
11147     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11148     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11149         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11150     }
11151 
11152     if (arm_v7m_is_handler_mode(env)) {
11153         DP_TBFLAG_M32(flags, HANDLER, 1);
11154     }
11155 
11156     /*
11157      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11158      * is suppressing them because the requested execution priority
11159      * is less than 0.
11160      */
11161     if (arm_feature(env, ARM_FEATURE_V8) &&
11162         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11163           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11164         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11165     }
11166 
11167     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11168         DP_TBFLAG_M32(flags, SECURE, 1);
11169     }
11170 
11171     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11172 }
11173 
11174 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11175                                         ARMMMUIdx mmu_idx)
11176 {
11177     CPUARMTBFlags flags = {};
11178     int el = arm_current_el(env);
11179 
11180     if (arm_sctlr(env, el) & SCTLR_A) {
11181         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11182     }
11183 
11184     if (arm_el_is_aa64(env, 1)) {
11185         DP_TBFLAG_A32(flags, VFPEN, 1);
11186     }
11187 
11188     if (el < 2 && env->cp15.hstr_el2 &&
11189         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11190         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11191     }
11192 
11193     if (env->uncached_cpsr & CPSR_IL) {
11194         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11195     }
11196 
11197     /*
11198      * The SME exception we are testing for is raised via
11199      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11200      * AArch32.CheckAdvSIMDOrFPEnabled().
11201      */
11202     if (el == 0
11203         && FIELD_EX64(env->svcr, SVCR, SM)
11204         && (!arm_is_el2_enabled(env)
11205             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11206         && arm_el_is_aa64(env, 1)
11207         && !sme_fa64(env, el)) {
11208         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11209     }
11210 
11211     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11212 }
11213 
11214 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11215                                         ARMMMUIdx mmu_idx)
11216 {
11217     CPUARMTBFlags flags = {};
11218     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11219     uint64_t tcr = regime_tcr(env, mmu_idx);
11220     uint64_t sctlr;
11221     int tbii, tbid;
11222 
11223     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11224 
11225     /* Get control bits for tagged addresses.  */
11226     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11227     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11228 
11229     DP_TBFLAG_A64(flags, TBII, tbii);
11230     DP_TBFLAG_A64(flags, TBID, tbid);
11231 
11232     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11233         int sve_el = sve_exception_el(env, el);
11234 
11235         /*
11236          * If either FP or SVE are disabled, translator does not need len.
11237          * If SVE EL > FP EL, FP exception has precedence, and translator
11238          * does not need SVE EL.  Save potential re-translations by forcing
11239          * the unneeded data to zero.
11240          */
11241         if (fp_el != 0) {
11242             if (sve_el > fp_el) {
11243                 sve_el = 0;
11244             }
11245         } else if (sve_el == 0) {
11246             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11247         }
11248         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11249     }
11250     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11251         int sme_el = sme_exception_el(env, el);
11252         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11253 
11254         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11255         if (sme_el == 0) {
11256             /* Similarly, do not compute SVL if SME is disabled. */
11257             int svl = sve_vqm1_for_el_sm(env, el, true);
11258             DP_TBFLAG_A64(flags, SVL, svl);
11259             if (sm) {
11260                 /* If SVE is disabled, we will not have set VL above. */
11261                 DP_TBFLAG_A64(flags, VL, svl);
11262             }
11263         }
11264         if (sm) {
11265             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11266             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11267         }
11268         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11269     }
11270 
11271     sctlr = regime_sctlr(env, stage1);
11272 
11273     if (sctlr & SCTLR_A) {
11274         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11275     }
11276 
11277     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11278         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11279     }
11280 
11281     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11282         /*
11283          * In order to save space in flags, we record only whether
11284          * pauth is "inactive", meaning all insns are implemented as
11285          * a nop, or "active" when some action must be performed.
11286          * The decision of which action to take is left to a helper.
11287          */
11288         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11289             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11290         }
11291     }
11292 
11293     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11294         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11295         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11296             DP_TBFLAG_A64(flags, BT, 1);
11297         }
11298     }
11299 
11300     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11301     if (!(env->pstate & PSTATE_UAO)) {
11302         switch (mmu_idx) {
11303         case ARMMMUIdx_E10_1:
11304         case ARMMMUIdx_E10_1_PAN:
11305             /* TODO: ARMv8.3-NV */
11306             DP_TBFLAG_A64(flags, UNPRIV, 1);
11307             break;
11308         case ARMMMUIdx_E20_2:
11309         case ARMMMUIdx_E20_2_PAN:
11310             /*
11311              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11312              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11313              */
11314             if (env->cp15.hcr_el2 & HCR_TGE) {
11315                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11316             }
11317             break;
11318         default:
11319             break;
11320         }
11321     }
11322 
11323     if (env->pstate & PSTATE_IL) {
11324         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11325     }
11326 
11327     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11328         /*
11329          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11330          * if all accesses must be Unchecked:
11331          * 1) If no TBI, then there are no tags in the address to check,
11332          * 2) If Tag Check Override, then all accesses are Unchecked,
11333          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11334          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11335          */
11336         if (allocation_tag_access_enabled(env, el, sctlr)) {
11337             DP_TBFLAG_A64(flags, ATA, 1);
11338             if (tbid
11339                 && !(env->pstate & PSTATE_TCO)
11340                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11341                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11342             }
11343         }
11344         /* And again for unprivileged accesses, if required.  */
11345         if (EX_TBFLAG_A64(flags, UNPRIV)
11346             && tbid
11347             && !(env->pstate & PSTATE_TCO)
11348             && (sctlr & SCTLR_TCF0)
11349             && allocation_tag_access_enabled(env, 0, sctlr)) {
11350             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11351         }
11352         /* Cache TCMA as well as TBI. */
11353         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11354     }
11355 
11356     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11357 }
11358 
11359 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11360 {
11361     int el = arm_current_el(env);
11362     int fp_el = fp_exception_el(env, el);
11363     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11364 
11365     if (is_a64(env)) {
11366         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11367     } else if (arm_feature(env, ARM_FEATURE_M)) {
11368         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11369     } else {
11370         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11371     }
11372 }
11373 
11374 void arm_rebuild_hflags(CPUARMState *env)
11375 {
11376     env->hflags = rebuild_hflags_internal(env);
11377 }
11378 
11379 /*
11380  * If we have triggered a EL state change we can't rely on the
11381  * translator having passed it to us, we need to recompute.
11382  */
11383 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11384 {
11385     int el = arm_current_el(env);
11386     int fp_el = fp_exception_el(env, el);
11387     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11388 
11389     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11390 }
11391 
11392 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11393 {
11394     int fp_el = fp_exception_el(env, el);
11395     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11396 
11397     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11398 }
11399 
11400 /*
11401  * If we have triggered a EL state change we can't rely on the
11402  * translator having passed it to us, we need to recompute.
11403  */
11404 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11405 {
11406     int el = arm_current_el(env);
11407     int fp_el = fp_exception_el(env, el);
11408     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11409     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11410 }
11411 
11412 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11413 {
11414     int fp_el = fp_exception_el(env, el);
11415     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11416 
11417     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11418 }
11419 
11420 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11421 {
11422     int fp_el = fp_exception_el(env, el);
11423     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11424 
11425     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11426 }
11427 
11428 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11429 {
11430 #ifdef CONFIG_DEBUG_TCG
11431     CPUARMTBFlags c = env->hflags;
11432     CPUARMTBFlags r = rebuild_hflags_internal(env);
11433 
11434     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11435         fprintf(stderr, "TCG hflags mismatch "
11436                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11437                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11438                 c.flags, c.flags2, r.flags, r.flags2);
11439         abort();
11440     }
11441 #endif
11442 }
11443 
11444 static bool mve_no_pred(CPUARMState *env)
11445 {
11446     /*
11447      * Return true if there is definitely no predication of MVE
11448      * instructions by VPR or LTPSIZE. (Returning false even if there
11449      * isn't any predication is OK; generated code will just be
11450      * a little worse.)
11451      * If the CPU does not implement MVE then this TB flag is always 0.
11452      *
11453      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11454      * logic in gen_update_fp_context() needs to be updated to match.
11455      *
11456      * We do not include the effect of the ECI bits here -- they are
11457      * tracked in other TB flags. This simplifies the logic for
11458      * "when did we emit code that changes the MVE_NO_PRED TB flag
11459      * and thus need to end the TB?".
11460      */
11461     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11462         return false;
11463     }
11464     if (env->v7m.vpr) {
11465         return false;
11466     }
11467     if (env->v7m.ltpsize < 4) {
11468         return false;
11469     }
11470     return true;
11471 }
11472 
11473 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11474                           target_ulong *cs_base, uint32_t *pflags)
11475 {
11476     CPUARMTBFlags flags;
11477 
11478     assert_hflags_rebuild_correctly(env);
11479     flags = env->hflags;
11480 
11481     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11482         *pc = env->pc;
11483         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11484             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11485         }
11486     } else {
11487         *pc = env->regs[15];
11488 
11489         if (arm_feature(env, ARM_FEATURE_M)) {
11490             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11491                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11492                 != env->v7m.secure) {
11493                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11494             }
11495 
11496             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11497                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11498                  (env->v7m.secure &&
11499                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11500                 /*
11501                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11502                  * active FP context; we must create a new FP context before
11503                  * executing any FP insn.
11504                  */
11505                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11506             }
11507 
11508             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11509             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11510                 DP_TBFLAG_M32(flags, LSPACT, 1);
11511             }
11512 
11513             if (mve_no_pred(env)) {
11514                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11515             }
11516         } else {
11517             /*
11518              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11519              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11520              */
11521             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11522                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11523             } else {
11524                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11525                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11526             }
11527             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11528                 DP_TBFLAG_A32(flags, VFPEN, 1);
11529             }
11530         }
11531 
11532         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11533         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11534     }
11535 
11536     /*
11537      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11538      * states defined in the ARM ARM for software singlestep:
11539      *  SS_ACTIVE   PSTATE.SS   State
11540      *     0            x       Inactive (the TB flag for SS is always 0)
11541      *     1            0       Active-pending
11542      *     1            1       Active-not-pending
11543      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11544      */
11545     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11546         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11547     }
11548 
11549     *pflags = flags.flags;
11550     *cs_base = flags.flags2;
11551 }
11552 
11553 #ifdef TARGET_AARCH64
11554 /*
11555  * The manual says that when SVE is enabled and VQ is widened the
11556  * implementation is allowed to zero the previously inaccessible
11557  * portion of the registers.  The corollary to that is that when
11558  * SVE is enabled and VQ is narrowed we are also allowed to zero
11559  * the now inaccessible portion of the registers.
11560  *
11561  * The intent of this is that no predicate bit beyond VQ is ever set.
11562  * Which means that some operations on predicate registers themselves
11563  * may operate on full uint64_t or even unrolled across the maximum
11564  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11565  * may well be cheaper than conditionals to restrict the operation
11566  * to the relevant portion of a uint16_t[16].
11567  */
11568 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11569 {
11570     int i, j;
11571     uint64_t pmask;
11572 
11573     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11574     assert(vq <= env_archcpu(env)->sve_max_vq);
11575 
11576     /* Zap the high bits of the zregs.  */
11577     for (i = 0; i < 32; i++) {
11578         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11579     }
11580 
11581     /* Zap the high bits of the pregs and ffr.  */
11582     pmask = 0;
11583     if (vq & 3) {
11584         pmask = ~(-1ULL << (16 * (vq & 3)));
11585     }
11586     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11587         for (i = 0; i < 17; ++i) {
11588             env->vfp.pregs[i].p[j] &= pmask;
11589         }
11590         pmask = 0;
11591     }
11592 }
11593 
11594 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11595 {
11596     int exc_el;
11597 
11598     if (sm) {
11599         exc_el = sme_exception_el(env, el);
11600     } else {
11601         exc_el = sve_exception_el(env, el);
11602     }
11603     if (exc_el) {
11604         return 0; /* disabled */
11605     }
11606     return sve_vqm1_for_el_sm(env, el, sm);
11607 }
11608 
11609 /*
11610  * Notice a change in SVE vector size when changing EL.
11611  */
11612 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11613                            int new_el, bool el0_a64)
11614 {
11615     ARMCPU *cpu = env_archcpu(env);
11616     int old_len, new_len;
11617     bool old_a64, new_a64, sm;
11618 
11619     /* Nothing to do if no SVE.  */
11620     if (!cpu_isar_feature(aa64_sve, cpu)) {
11621         return;
11622     }
11623 
11624     /* Nothing to do if FP is disabled in either EL.  */
11625     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11626         return;
11627     }
11628 
11629     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11630     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11631 
11632     /*
11633      * Both AArch64.TakeException and AArch64.ExceptionReturn
11634      * invoke ResetSVEState when taking an exception from, or
11635      * returning to, AArch32 state when PSTATE.SM is enabled.
11636      */
11637     sm = FIELD_EX64(env->svcr, SVCR, SM);
11638     if (old_a64 != new_a64 && sm) {
11639         arm_reset_sve_state(env);
11640         return;
11641     }
11642 
11643     /*
11644      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11645      * at ELx, or not available because the EL is in AArch32 state, then
11646      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11647      * has an effective value of 0".
11648      *
11649      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11650      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11651      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11652      * we already have the correct register contents when encountering the
11653      * vq0->vq0 transition between EL0->EL1.
11654      */
11655     old_len = new_len = 0;
11656     if (old_a64) {
11657         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11658     }
11659     if (new_a64) {
11660         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11661     }
11662 
11663     /* When changing vector length, clear inaccessible state.  */
11664     if (new_len < old_len) {
11665         aarch64_sve_narrow_vq(env, new_len + 1);
11666     }
11667 }
11668 #endif
11669