xref: /openbmc/qemu/target/arm/helper.c (revision 910e4f24975f53645d308aa6c895f4599dd47c43)
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_BOTH,
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                 {
7992                     .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7993                     .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7994                     .access = PL2_R,
7995                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7996                 },
7997                 {   .name = "RVBAR", .type = ARM_CP_ALIAS,
7998                     .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7999                     .access = PL2_R,
8000                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8001                 },
8002             };
8003             define_arm_cp_regs(cpu, rvbar);
8004         }
8005     }
8006 
8007     /* Register the base EL3 cpregs. */
8008     if (arm_feature(env, ARM_FEATURE_EL3)) {
8009         define_arm_cp_regs(cpu, el3_cp_reginfo);
8010         ARMCPRegInfo el3_regs[] = {
8011             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8012               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8013               .access = PL3_R,
8014               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8015             },
8016             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8017               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8018               .access = PL3_RW,
8019               .raw_writefn = raw_write, .writefn = sctlr_write,
8020               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8021               .resetvalue = cpu->reset_sctlr },
8022         };
8023 
8024         define_arm_cp_regs(cpu, el3_regs);
8025     }
8026     /* The behaviour of NSACR is sufficiently various that we don't
8027      * try to describe it in a single reginfo:
8028      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8029      *     reads as constant 0xc00 from NS EL1 and NS EL2
8030      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8031      *  if v7 without EL3, register doesn't exist
8032      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8033      */
8034     if (arm_feature(env, ARM_FEATURE_EL3)) {
8035         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8036             static const ARMCPRegInfo nsacr = {
8037                 .name = "NSACR", .type = ARM_CP_CONST,
8038                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8039                 .access = PL1_RW, .accessfn = nsacr_access,
8040                 .resetvalue = 0xc00
8041             };
8042             define_one_arm_cp_reg(cpu, &nsacr);
8043         } else {
8044             static const ARMCPRegInfo nsacr = {
8045                 .name = "NSACR",
8046                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8047                 .access = PL3_RW | PL1_R,
8048                 .resetvalue = 0,
8049                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8050             };
8051             define_one_arm_cp_reg(cpu, &nsacr);
8052         }
8053     } else {
8054         if (arm_feature(env, ARM_FEATURE_V8)) {
8055             static const ARMCPRegInfo nsacr = {
8056                 .name = "NSACR", .type = ARM_CP_CONST,
8057                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8058                 .access = PL1_R,
8059                 .resetvalue = 0xc00
8060             };
8061             define_one_arm_cp_reg(cpu, &nsacr);
8062         }
8063     }
8064 
8065     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8066         if (arm_feature(env, ARM_FEATURE_V6)) {
8067             /* PMSAv6 not implemented */
8068             assert(arm_feature(env, ARM_FEATURE_V7));
8069             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8070             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8071         } else {
8072             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8073         }
8074     } else {
8075         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8076         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8077         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8078         if (cpu_isar_feature(aa32_hpd, cpu)) {
8079             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8080         }
8081     }
8082     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8083         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8084     }
8085     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8086         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8087     }
8088     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8089         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8090     }
8091     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8092         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8093     }
8094     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8095         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8096     }
8097     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8098         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8099     }
8100     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8101         define_arm_cp_regs(cpu, omap_cp_reginfo);
8102     }
8103     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8104         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8105     }
8106     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8107         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8108     }
8109     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8110         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8111     }
8112     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8113         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8114     }
8115     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8116         define_arm_cp_regs(cpu, jazelle_regs);
8117     }
8118     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8119      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8120      * be read-only (ie write causes UNDEF exception).
8121      */
8122     {
8123         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8124             /* Pre-v8 MIDR space.
8125              * Note that the MIDR isn't a simple constant register because
8126              * of the TI925 behaviour where writes to another register can
8127              * cause the MIDR value to change.
8128              *
8129              * Unimplemented registers in the c15 0 0 0 space default to
8130              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8131              * and friends override accordingly.
8132              */
8133             { .name = "MIDR",
8134               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8135               .access = PL1_R, .resetvalue = cpu->midr,
8136               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8137               .readfn = midr_read,
8138               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8139               .type = ARM_CP_OVERRIDE },
8140             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8141             { .name = "DUMMY",
8142               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8143               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8144             { .name = "DUMMY",
8145               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8146               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8147             { .name = "DUMMY",
8148               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8149               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8150             { .name = "DUMMY",
8151               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8152               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8153             { .name = "DUMMY",
8154               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8155               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8156         };
8157         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8158             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8159               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8160               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8161               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8162               .readfn = midr_read },
8163             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8164             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8165               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8166               .access = PL1_R, .resetvalue = cpu->midr },
8167             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8168               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8169               .access = PL1_R,
8170               .accessfn = access_aa64_tid1,
8171               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8172         };
8173         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8174             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8175             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8176             .access = PL1_R, .resetvalue = cpu->midr
8177         };
8178         ARMCPRegInfo id_cp_reginfo[] = {
8179             /* These are common to v8 and pre-v8 */
8180             { .name = "CTR",
8181               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8182               .access = PL1_R, .accessfn = ctr_el0_access,
8183               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8184             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8185               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8186               .access = PL0_R, .accessfn = ctr_el0_access,
8187               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8188             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8189             { .name = "TCMTR",
8190               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8191               .access = PL1_R,
8192               .accessfn = access_aa32_tid1,
8193               .type = ARM_CP_CONST, .resetvalue = 0 },
8194         };
8195         /* TLBTR is specific to VMSA */
8196         ARMCPRegInfo id_tlbtr_reginfo = {
8197               .name = "TLBTR",
8198               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8199               .access = PL1_R,
8200               .accessfn = access_aa32_tid1,
8201               .type = ARM_CP_CONST, .resetvalue = 0,
8202         };
8203         /* MPUIR is specific to PMSA V6+ */
8204         ARMCPRegInfo id_mpuir_reginfo = {
8205               .name = "MPUIR",
8206               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8207               .access = PL1_R, .type = ARM_CP_CONST,
8208               .resetvalue = cpu->pmsav7_dregion << 8
8209         };
8210         static const ARMCPRegInfo crn0_wi_reginfo = {
8211             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8212             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8213             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8214         };
8215 #ifdef CONFIG_USER_ONLY
8216         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8217             { .name = "MIDR_EL1",
8218               .exported_bits = 0x00000000ffffffff },
8219             { .name = "REVIDR_EL1"                },
8220         };
8221         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8222 #endif
8223         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8224             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8225             size_t i;
8226             /* Register the blanket "writes ignored" value first to cover the
8227              * whole space. Then update the specific ID registers to allow write
8228              * access, so that they ignore writes rather than causing them to
8229              * UNDEF.
8230              */
8231             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8232             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8233                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8234             }
8235             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8236                 id_cp_reginfo[i].access = PL1_RW;
8237             }
8238             id_mpuir_reginfo.access = PL1_RW;
8239             id_tlbtr_reginfo.access = PL1_RW;
8240         }
8241         if (arm_feature(env, ARM_FEATURE_V8)) {
8242             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8243             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8244                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8245             }
8246         } else {
8247             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8248         }
8249         define_arm_cp_regs(cpu, id_cp_reginfo);
8250         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8251             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8252         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8253             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8254         }
8255     }
8256 
8257     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8258         ARMCPRegInfo mpidr_cp_reginfo[] = {
8259             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8260               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8261               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8262         };
8263 #ifdef CONFIG_USER_ONLY
8264         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8265             { .name = "MPIDR_EL1",
8266               .fixed_bits = 0x0000000080000000 },
8267         };
8268         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8269 #endif
8270         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8271     }
8272 
8273     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8274         ARMCPRegInfo auxcr_reginfo[] = {
8275             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8276               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8277               .access = PL1_RW, .accessfn = access_tacr,
8278               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8279             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8280               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8281               .access = PL2_RW, .type = ARM_CP_CONST,
8282               .resetvalue = 0 },
8283             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8284               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8285               .access = PL3_RW, .type = ARM_CP_CONST,
8286               .resetvalue = 0 },
8287         };
8288         define_arm_cp_regs(cpu, auxcr_reginfo);
8289         if (cpu_isar_feature(aa32_ac2, cpu)) {
8290             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8291         }
8292     }
8293 
8294     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8295         /*
8296          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8297          * There are two flavours:
8298          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8299          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8300          *      32-bit register visible to AArch32 at a different encoding
8301          *      to the "flavour 1" register and with the bits rearranged to
8302          *      be able to squash a 64-bit address into the 32-bit view.
8303          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8304          * in future if we support AArch32-only configs of some of the
8305          * AArch64 cores we might need to add a specific feature flag
8306          * to indicate cores with "flavour 2" CBAR.
8307          */
8308         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8309             /* 32 bit view is [31:18] 0...0 [43:32]. */
8310             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8311                 | extract64(cpu->reset_cbar, 32, 12);
8312             ARMCPRegInfo cbar_reginfo[] = {
8313                 { .name = "CBAR",
8314                   .type = ARM_CP_CONST,
8315                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8316                   .access = PL1_R, .resetvalue = cbar32 },
8317                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8318                   .type = ARM_CP_CONST,
8319                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8320                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8321             };
8322             /* We don't implement a r/w 64 bit CBAR currently */
8323             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8324             define_arm_cp_regs(cpu, cbar_reginfo);
8325         } else {
8326             ARMCPRegInfo cbar = {
8327                 .name = "CBAR",
8328                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8329                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8330                 .fieldoffset = offsetof(CPUARMState,
8331                                         cp15.c15_config_base_address)
8332             };
8333             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8334                 cbar.access = PL1_R;
8335                 cbar.fieldoffset = 0;
8336                 cbar.type = ARM_CP_CONST;
8337             }
8338             define_one_arm_cp_reg(cpu, &cbar);
8339         }
8340     }
8341 
8342     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8343         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8344             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8345               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8346               .access = PL1_RW, .writefn = vbar_write,
8347               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8348                                      offsetof(CPUARMState, cp15.vbar_ns) },
8349               .resetvalue = 0 },
8350         };
8351         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8352     }
8353 
8354     /* Generic registers whose values depend on the implementation */
8355     {
8356         ARMCPRegInfo sctlr = {
8357             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8358             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8359             .access = PL1_RW, .accessfn = access_tvm_trvm,
8360             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8361                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8362             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8363             .raw_writefn = raw_write,
8364         };
8365         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8366             /* Normally we would always end the TB on an SCTLR write, but Linux
8367              * arch/arm/mach-pxa/sleep.S expects two instructions following
8368              * an MMU enable to execute from cache.  Imitate this behaviour.
8369              */
8370             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8371         }
8372         define_one_arm_cp_reg(cpu, &sctlr);
8373     }
8374 
8375     if (cpu_isar_feature(aa64_lor, cpu)) {
8376         define_arm_cp_regs(cpu, lor_reginfo);
8377     }
8378     if (cpu_isar_feature(aa64_pan, cpu)) {
8379         define_one_arm_cp_reg(cpu, &pan_reginfo);
8380     }
8381 #ifndef CONFIG_USER_ONLY
8382     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8383         define_arm_cp_regs(cpu, ats1e1_reginfo);
8384     }
8385     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8386         define_arm_cp_regs(cpu, ats1cp_reginfo);
8387     }
8388 #endif
8389     if (cpu_isar_feature(aa64_uao, cpu)) {
8390         define_one_arm_cp_reg(cpu, &uao_reginfo);
8391     }
8392 
8393     if (cpu_isar_feature(aa64_dit, cpu)) {
8394         define_one_arm_cp_reg(cpu, &dit_reginfo);
8395     }
8396     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8397         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8398     }
8399     if (cpu_isar_feature(any_ras, cpu)) {
8400         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8401     }
8402 
8403     if (cpu_isar_feature(aa64_vh, cpu) ||
8404         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8405         define_one_arm_cp_reg(cpu, &contextidr_el2);
8406     }
8407     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8408         define_arm_cp_regs(cpu, vhe_reginfo);
8409     }
8410 
8411     if (cpu_isar_feature(aa64_sve, cpu)) {
8412         define_arm_cp_regs(cpu, zcr_reginfo);
8413     }
8414 
8415     if (cpu_isar_feature(aa64_hcx, cpu)) {
8416         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8417     }
8418 
8419 #ifdef TARGET_AARCH64
8420     if (cpu_isar_feature(aa64_sme, cpu)) {
8421         define_arm_cp_regs(cpu, sme_reginfo);
8422     }
8423     if (cpu_isar_feature(aa64_pauth, cpu)) {
8424         define_arm_cp_regs(cpu, pauth_reginfo);
8425     }
8426     if (cpu_isar_feature(aa64_rndr, cpu)) {
8427         define_arm_cp_regs(cpu, rndr_reginfo);
8428     }
8429     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8430         define_arm_cp_regs(cpu, tlbirange_reginfo);
8431     }
8432     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8433         define_arm_cp_regs(cpu, tlbios_reginfo);
8434     }
8435 #ifndef CONFIG_USER_ONLY
8436     /* Data Cache clean instructions up to PoP */
8437     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8438         define_one_arm_cp_reg(cpu, dcpop_reg);
8439 
8440         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8441             define_one_arm_cp_reg(cpu, dcpodp_reg);
8442         }
8443     }
8444 #endif /*CONFIG_USER_ONLY*/
8445 
8446     /*
8447      * If full MTE is enabled, add all of the system registers.
8448      * If only "instructions available at EL0" are enabled,
8449      * then define only a RAZ/WI version of PSTATE.TCO.
8450      */
8451     if (cpu_isar_feature(aa64_mte, cpu)) {
8452         define_arm_cp_regs(cpu, mte_reginfo);
8453         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8454     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8455         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8456         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8457     }
8458 
8459     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8460         define_arm_cp_regs(cpu, scxtnum_reginfo);
8461     }
8462 #endif
8463 
8464     if (cpu_isar_feature(any_predinv, cpu)) {
8465         define_arm_cp_regs(cpu, predinv_reginfo);
8466     }
8467 
8468     if (cpu_isar_feature(any_ccidx, cpu)) {
8469         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8470     }
8471 
8472 #ifndef CONFIG_USER_ONLY
8473     /*
8474      * Register redirections and aliases must be done last,
8475      * after the registers from the other extensions have been defined.
8476      */
8477     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8478         define_arm_vh_e2h_redirects_aliases(cpu);
8479     }
8480 #endif
8481 }
8482 
8483 /* Sort alphabetically by type name, except for "any". */
8484 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8485 {
8486     ObjectClass *class_a = (ObjectClass *)a;
8487     ObjectClass *class_b = (ObjectClass *)b;
8488     const char *name_a, *name_b;
8489 
8490     name_a = object_class_get_name(class_a);
8491     name_b = object_class_get_name(class_b);
8492     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8493         return 1;
8494     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8495         return -1;
8496     } else {
8497         return strcmp(name_a, name_b);
8498     }
8499 }
8500 
8501 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8502 {
8503     ObjectClass *oc = data;
8504     CPUClass *cc = CPU_CLASS(oc);
8505     const char *typename;
8506     char *name;
8507 
8508     typename = object_class_get_name(oc);
8509     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8510     if (cc->deprecation_note) {
8511         qemu_printf("  %s (deprecated)\n", name);
8512     } else {
8513         qemu_printf("  %s\n", name);
8514     }
8515     g_free(name);
8516 }
8517 
8518 void arm_cpu_list(void)
8519 {
8520     GSList *list;
8521 
8522     list = object_class_get_list(TYPE_ARM_CPU, false);
8523     list = g_slist_sort(list, arm_cpu_list_compare);
8524     qemu_printf("Available CPUs:\n");
8525     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8526     g_slist_free(list);
8527 }
8528 
8529 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8530 {
8531     ObjectClass *oc = data;
8532     CpuDefinitionInfoList **cpu_list = user_data;
8533     CpuDefinitionInfo *info;
8534     const char *typename;
8535 
8536     typename = object_class_get_name(oc);
8537     info = g_malloc0(sizeof(*info));
8538     info->name = g_strndup(typename,
8539                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8540     info->q_typename = g_strdup(typename);
8541 
8542     QAPI_LIST_PREPEND(*cpu_list, info);
8543 }
8544 
8545 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8546 {
8547     CpuDefinitionInfoList *cpu_list = NULL;
8548     GSList *list;
8549 
8550     list = object_class_get_list(TYPE_ARM_CPU, false);
8551     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8552     g_slist_free(list);
8553 
8554     return cpu_list;
8555 }
8556 
8557 /*
8558  * Private utility function for define_one_arm_cp_reg_with_opaque():
8559  * add a single reginfo struct to the hash table.
8560  */
8561 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8562                                    void *opaque, CPState state,
8563                                    CPSecureState secstate,
8564                                    int crm, int opc1, int opc2,
8565                                    const char *name)
8566 {
8567     CPUARMState *env = &cpu->env;
8568     uint32_t key;
8569     ARMCPRegInfo *r2;
8570     bool is64 = r->type & ARM_CP_64BIT;
8571     bool ns = secstate & ARM_CP_SECSTATE_NS;
8572     int cp = r->cp;
8573     size_t name_len;
8574     bool make_const;
8575 
8576     switch (state) {
8577     case ARM_CP_STATE_AA32:
8578         /* We assume it is a cp15 register if the .cp field is left unset. */
8579         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8580             cp = 15;
8581         }
8582         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8583         break;
8584     case ARM_CP_STATE_AA64:
8585         /*
8586          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8587          * cp == 0 as equivalent to the value for "standard guest-visible
8588          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8589          * in their AArch64 view (the .cp value may be non-zero for the
8590          * benefit of the AArch32 view).
8591          */
8592         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8593             cp = CP_REG_ARM64_SYSREG_CP;
8594         }
8595         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8596         break;
8597     default:
8598         g_assert_not_reached();
8599     }
8600 
8601     /* Overriding of an existing definition must be explicitly requested. */
8602     if (!(r->type & ARM_CP_OVERRIDE)) {
8603         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8604         if (oldreg) {
8605             assert(oldreg->type & ARM_CP_OVERRIDE);
8606         }
8607     }
8608 
8609     /*
8610      * Eliminate registers that are not present because the EL is missing.
8611      * Doing this here makes it easier to put all registers for a given
8612      * feature into the same ARMCPRegInfo array and define them all at once.
8613      */
8614     make_const = false;
8615     if (arm_feature(env, ARM_FEATURE_EL3)) {
8616         /*
8617          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8618          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8619          */
8620         int min_el = ctz32(r->access) / 2;
8621         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8622             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8623                 return;
8624             }
8625             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8626         }
8627     } else {
8628         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8629                                  ? PL2_RW : PL1_RW);
8630         if ((r->access & max_el) == 0) {
8631             return;
8632         }
8633     }
8634 
8635     /* Combine cpreg and name into one allocation. */
8636     name_len = strlen(name) + 1;
8637     r2 = g_malloc(sizeof(*r2) + name_len);
8638     *r2 = *r;
8639     r2->name = memcpy(r2 + 1, name, name_len);
8640 
8641     /*
8642      * Update fields to match the instantiation, overwiting wildcards
8643      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8644      */
8645     r2->cp = cp;
8646     r2->crm = crm;
8647     r2->opc1 = opc1;
8648     r2->opc2 = opc2;
8649     r2->state = state;
8650     r2->secure = secstate;
8651     if (opaque) {
8652         r2->opaque = opaque;
8653     }
8654 
8655     if (make_const) {
8656         /* This should not have been a very special register to begin. */
8657         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8658         assert(old_special == 0 || old_special == ARM_CP_NOP);
8659         /*
8660          * Set the special function to CONST, retaining the other flags.
8661          * This is important for e.g. ARM_CP_SVE so that we still
8662          * take the SVE trap if CPTR_EL3.EZ == 0.
8663          */
8664         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8665         /*
8666          * Usually, these registers become RES0, but there are a few
8667          * special cases like VPIDR_EL2 which have a constant non-zero
8668          * value with writes ignored.
8669          */
8670         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8671             r2->resetvalue = 0;
8672         }
8673         /*
8674          * ARM_CP_CONST has precedence, so removing the callbacks and
8675          * offsets are not strictly necessary, but it is potentially
8676          * less confusing to debug later.
8677          */
8678         r2->readfn = NULL;
8679         r2->writefn = NULL;
8680         r2->raw_readfn = NULL;
8681         r2->raw_writefn = NULL;
8682         r2->resetfn = NULL;
8683         r2->fieldoffset = 0;
8684         r2->bank_fieldoffsets[0] = 0;
8685         r2->bank_fieldoffsets[1] = 0;
8686     } else {
8687         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8688 
8689         if (isbanked) {
8690             /*
8691              * Register is banked (using both entries in array).
8692              * Overwriting fieldoffset as the array is only used to define
8693              * banked registers but later only fieldoffset is used.
8694              */
8695             r2->fieldoffset = r->bank_fieldoffsets[ns];
8696         }
8697         if (state == ARM_CP_STATE_AA32) {
8698             if (isbanked) {
8699                 /*
8700                  * If the register is banked then we don't need to migrate or
8701                  * reset the 32-bit instance in certain cases:
8702                  *
8703                  * 1) If the register has both 32-bit and 64-bit instances
8704                  *    then we can count on the 64-bit instance taking care
8705                  *    of the non-secure bank.
8706                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8707                  *    version taking care of the secure bank.  This requires
8708                  *    that separate 32 and 64-bit definitions are provided.
8709                  */
8710                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8711                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8712                     r2->type |= ARM_CP_ALIAS;
8713                 }
8714             } else if ((secstate != r->secure) && !ns) {
8715                 /*
8716                  * The register is not banked so we only want to allow
8717                  * migration of the non-secure instance.
8718                  */
8719                 r2->type |= ARM_CP_ALIAS;
8720             }
8721 
8722             if (HOST_BIG_ENDIAN &&
8723                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8724                 r2->fieldoffset += sizeof(uint32_t);
8725             }
8726         }
8727     }
8728 
8729     /*
8730      * By convention, for wildcarded registers only the first
8731      * entry is used for migration; the others are marked as
8732      * ALIAS so we don't try to transfer the register
8733      * multiple times. Special registers (ie NOP/WFI) are
8734      * never migratable and not even raw-accessible.
8735      */
8736     if (r2->type & ARM_CP_SPECIAL_MASK) {
8737         r2->type |= ARM_CP_NO_RAW;
8738     }
8739     if (((r->crm == CP_ANY) && crm != 0) ||
8740         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8741         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8742         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8743     }
8744 
8745     /*
8746      * Check that raw accesses are either forbidden or handled. Note that
8747      * we can't assert this earlier because the setup of fieldoffset for
8748      * banked registers has to be done first.
8749      */
8750     if (!(r2->type & ARM_CP_NO_RAW)) {
8751         assert(!raw_accessors_invalid(r2));
8752     }
8753 
8754     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8755 }
8756 
8757 
8758 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8759                                        const ARMCPRegInfo *r, void *opaque)
8760 {
8761     /* Define implementations of coprocessor registers.
8762      * We store these in a hashtable because typically
8763      * there are less than 150 registers in a space which
8764      * is 16*16*16*8*8 = 262144 in size.
8765      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8766      * If a register is defined twice then the second definition is
8767      * used, so this can be used to define some generic registers and
8768      * then override them with implementation specific variations.
8769      * At least one of the original and the second definition should
8770      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8771      * against accidental use.
8772      *
8773      * The state field defines whether the register is to be
8774      * visible in the AArch32 or AArch64 execution state. If the
8775      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8776      * reginfo structure for the AArch32 view, which sees the lower
8777      * 32 bits of the 64 bit register.
8778      *
8779      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8780      * be wildcarded. AArch64 registers are always considered to be 64
8781      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8782      * the register, if any.
8783      */
8784     int crm, opc1, opc2;
8785     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8786     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8787     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8788     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8789     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8790     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8791     CPState state;
8792 
8793     /* 64 bit registers have only CRm and Opc1 fields */
8794     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8795     /* op0 only exists in the AArch64 encodings */
8796     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8797     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8798     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8799     /*
8800      * This API is only for Arm's system coprocessors (14 and 15) or
8801      * (M-profile or v7A-and-earlier only) for implementation defined
8802      * coprocessors in the range 0..7.  Our decode assumes this, since
8803      * 8..13 can be used for other insns including VFP and Neon. See
8804      * valid_cp() in translate.c.  Assert here that we haven't tried
8805      * to use an invalid coprocessor number.
8806      */
8807     switch (r->state) {
8808     case ARM_CP_STATE_BOTH:
8809         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8810         if (r->cp == 0) {
8811             break;
8812         }
8813         /* fall through */
8814     case ARM_CP_STATE_AA32:
8815         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8816             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8817             assert(r->cp >= 14 && r->cp <= 15);
8818         } else {
8819             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8820         }
8821         break;
8822     case ARM_CP_STATE_AA64:
8823         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8824         break;
8825     default:
8826         g_assert_not_reached();
8827     }
8828     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8829      * encodes a minimum access level for the register. We roll this
8830      * runtime check into our general permission check code, so check
8831      * here that the reginfo's specified permissions are strict enough
8832      * to encompass the generic architectural permission check.
8833      */
8834     if (r->state != ARM_CP_STATE_AA32) {
8835         CPAccessRights mask;
8836         switch (r->opc1) {
8837         case 0:
8838             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8839             mask = PL0U_R | PL1_RW;
8840             break;
8841         case 1: case 2:
8842             /* min_EL EL1 */
8843             mask = PL1_RW;
8844             break;
8845         case 3:
8846             /* min_EL EL0 */
8847             mask = PL0_RW;
8848             break;
8849         case 4:
8850         case 5:
8851             /* min_EL EL2 */
8852             mask = PL2_RW;
8853             break;
8854         case 6:
8855             /* min_EL EL3 */
8856             mask = PL3_RW;
8857             break;
8858         case 7:
8859             /* min_EL EL1, secure mode only (we don't check the latter) */
8860             mask = PL1_RW;
8861             break;
8862         default:
8863             /* broken reginfo with out-of-range opc1 */
8864             g_assert_not_reached();
8865         }
8866         /* assert our permissions are not too lax (stricter is fine) */
8867         assert((r->access & ~mask) == 0);
8868     }
8869 
8870     /* Check that the register definition has enough info to handle
8871      * reads and writes if they are permitted.
8872      */
8873     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8874         if (r->access & PL3_R) {
8875             assert((r->fieldoffset ||
8876                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8877                    r->readfn);
8878         }
8879         if (r->access & PL3_W) {
8880             assert((r->fieldoffset ||
8881                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8882                    r->writefn);
8883         }
8884     }
8885 
8886     for (crm = crmmin; crm <= crmmax; crm++) {
8887         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8888             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8889                 for (state = ARM_CP_STATE_AA32;
8890                      state <= ARM_CP_STATE_AA64; state++) {
8891                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8892                         continue;
8893                     }
8894                     if (state == ARM_CP_STATE_AA32) {
8895                         /* Under AArch32 CP registers can be common
8896                          * (same for secure and non-secure world) or banked.
8897                          */
8898                         char *name;
8899 
8900                         switch (r->secure) {
8901                         case ARM_CP_SECSTATE_S:
8902                         case ARM_CP_SECSTATE_NS:
8903                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8904                                                    r->secure, crm, opc1, opc2,
8905                                                    r->name);
8906                             break;
8907                         case ARM_CP_SECSTATE_BOTH:
8908                             name = g_strdup_printf("%s_S", r->name);
8909                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8910                                                    ARM_CP_SECSTATE_S,
8911                                                    crm, opc1, opc2, name);
8912                             g_free(name);
8913                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8914                                                    ARM_CP_SECSTATE_NS,
8915                                                    crm, opc1, opc2, r->name);
8916                             break;
8917                         default:
8918                             g_assert_not_reached();
8919                         }
8920                     } else {
8921                         /* AArch64 registers get mapped to non-secure instance
8922                          * of AArch32 */
8923                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8924                                                ARM_CP_SECSTATE_NS,
8925                                                crm, opc1, opc2, r->name);
8926                     }
8927                 }
8928             }
8929         }
8930     }
8931 }
8932 
8933 /* Define a whole list of registers */
8934 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8935                                         void *opaque, size_t len)
8936 {
8937     size_t i;
8938     for (i = 0; i < len; ++i) {
8939         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8940     }
8941 }
8942 
8943 /*
8944  * Modify ARMCPRegInfo for access from userspace.
8945  *
8946  * This is a data driven modification directed by
8947  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8948  * user-space cannot alter any values and dynamic values pertaining to
8949  * execution state are hidden from user space view anyway.
8950  */
8951 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8952                                  const ARMCPRegUserSpaceInfo *mods,
8953                                  size_t mods_len)
8954 {
8955     for (size_t mi = 0; mi < mods_len; ++mi) {
8956         const ARMCPRegUserSpaceInfo *m = mods + mi;
8957         GPatternSpec *pat = NULL;
8958 
8959         if (m->is_glob) {
8960             pat = g_pattern_spec_new(m->name);
8961         }
8962         for (size_t ri = 0; ri < regs_len; ++ri) {
8963             ARMCPRegInfo *r = regs + ri;
8964 
8965             if (pat && g_pattern_match_string(pat, r->name)) {
8966                 r->type = ARM_CP_CONST;
8967                 r->access = PL0U_R;
8968                 r->resetvalue = 0;
8969                 /* continue */
8970             } else if (strcmp(r->name, m->name) == 0) {
8971                 r->type = ARM_CP_CONST;
8972                 r->access = PL0U_R;
8973                 r->resetvalue &= m->exported_bits;
8974                 r->resetvalue |= m->fixed_bits;
8975                 break;
8976             }
8977         }
8978         if (pat) {
8979             g_pattern_spec_free(pat);
8980         }
8981     }
8982 }
8983 
8984 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8985 {
8986     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8987 }
8988 
8989 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8990                          uint64_t value)
8991 {
8992     /* Helper coprocessor write function for write-ignore registers */
8993 }
8994 
8995 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8996 {
8997     /* Helper coprocessor write function for read-as-zero registers */
8998     return 0;
8999 }
9000 
9001 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9002 {
9003     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9004 }
9005 
9006 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9007 {
9008     /* Return true if it is not valid for us to switch to
9009      * this CPU mode (ie all the UNPREDICTABLE cases in
9010      * the ARM ARM CPSRWriteByInstr pseudocode).
9011      */
9012 
9013     /* Changes to or from Hyp via MSR and CPS are illegal. */
9014     if (write_type == CPSRWriteByInstr &&
9015         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9016          mode == ARM_CPU_MODE_HYP)) {
9017         return 1;
9018     }
9019 
9020     switch (mode) {
9021     case ARM_CPU_MODE_USR:
9022         return 0;
9023     case ARM_CPU_MODE_SYS:
9024     case ARM_CPU_MODE_SVC:
9025     case ARM_CPU_MODE_ABT:
9026     case ARM_CPU_MODE_UND:
9027     case ARM_CPU_MODE_IRQ:
9028     case ARM_CPU_MODE_FIQ:
9029         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
9030          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9031          */
9032         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9033          * and CPS are treated as illegal mode changes.
9034          */
9035         if (write_type == CPSRWriteByInstr &&
9036             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9037             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9038             return 1;
9039         }
9040         return 0;
9041     case ARM_CPU_MODE_HYP:
9042         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9043     case ARM_CPU_MODE_MON:
9044         return arm_current_el(env) < 3;
9045     default:
9046         return 1;
9047     }
9048 }
9049 
9050 uint32_t cpsr_read(CPUARMState *env)
9051 {
9052     int ZF;
9053     ZF = (env->ZF == 0);
9054     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9055         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9056         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9057         | ((env->condexec_bits & 0xfc) << 8)
9058         | (env->GE << 16) | (env->daif & CPSR_AIF);
9059 }
9060 
9061 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9062                 CPSRWriteType write_type)
9063 {
9064     uint32_t changed_daif;
9065     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9066         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9067 
9068     if (mask & CPSR_NZCV) {
9069         env->ZF = (~val) & CPSR_Z;
9070         env->NF = val;
9071         env->CF = (val >> 29) & 1;
9072         env->VF = (val << 3) & 0x80000000;
9073     }
9074     if (mask & CPSR_Q)
9075         env->QF = ((val & CPSR_Q) != 0);
9076     if (mask & CPSR_T)
9077         env->thumb = ((val & CPSR_T) != 0);
9078     if (mask & CPSR_IT_0_1) {
9079         env->condexec_bits &= ~3;
9080         env->condexec_bits |= (val >> 25) & 3;
9081     }
9082     if (mask & CPSR_IT_2_7) {
9083         env->condexec_bits &= 3;
9084         env->condexec_bits |= (val >> 8) & 0xfc;
9085     }
9086     if (mask & CPSR_GE) {
9087         env->GE = (val >> 16) & 0xf;
9088     }
9089 
9090     /* In a V7 implementation that includes the security extensions but does
9091      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9092      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9093      * bits respectively.
9094      *
9095      * In a V8 implementation, it is permitted for privileged software to
9096      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9097      */
9098     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9099         arm_feature(env, ARM_FEATURE_EL3) &&
9100         !arm_feature(env, ARM_FEATURE_EL2) &&
9101         !arm_is_secure(env)) {
9102 
9103         changed_daif = (env->daif ^ val) & mask;
9104 
9105         if (changed_daif & CPSR_A) {
9106             /* Check to see if we are allowed to change the masking of async
9107              * abort exceptions from a non-secure state.
9108              */
9109             if (!(env->cp15.scr_el3 & SCR_AW)) {
9110                 qemu_log_mask(LOG_GUEST_ERROR,
9111                               "Ignoring attempt to switch CPSR_A flag from "
9112                               "non-secure world with SCR.AW bit clear\n");
9113                 mask &= ~CPSR_A;
9114             }
9115         }
9116 
9117         if (changed_daif & CPSR_F) {
9118             /* Check to see if we are allowed to change the masking of FIQ
9119              * exceptions from a non-secure state.
9120              */
9121             if (!(env->cp15.scr_el3 & SCR_FW)) {
9122                 qemu_log_mask(LOG_GUEST_ERROR,
9123                               "Ignoring attempt to switch CPSR_F flag from "
9124                               "non-secure world with SCR.FW bit clear\n");
9125                 mask &= ~CPSR_F;
9126             }
9127 
9128             /* Check whether non-maskable FIQ (NMFI) support is enabled.
9129              * If this bit is set software is not allowed to mask
9130              * FIQs, but is allowed to set CPSR_F to 0.
9131              */
9132             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9133                 (val & CPSR_F)) {
9134                 qemu_log_mask(LOG_GUEST_ERROR,
9135                               "Ignoring attempt to enable CPSR_F flag "
9136                               "(non-maskable FIQ [NMFI] support enabled)\n");
9137                 mask &= ~CPSR_F;
9138             }
9139         }
9140     }
9141 
9142     env->daif &= ~(CPSR_AIF & mask);
9143     env->daif |= val & CPSR_AIF & mask;
9144 
9145     if (write_type != CPSRWriteRaw &&
9146         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9147         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9148             /* Note that we can only get here in USR mode if this is a
9149              * gdb stub write; for this case we follow the architectural
9150              * behaviour for guest writes in USR mode of ignoring an attempt
9151              * to switch mode. (Those are caught by translate.c for writes
9152              * triggered by guest instructions.)
9153              */
9154             mask &= ~CPSR_M;
9155         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9156             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9157              * v7, and has defined behaviour in v8:
9158              *  + leave CPSR.M untouched
9159              *  + allow changes to the other CPSR fields
9160              *  + set PSTATE.IL
9161              * For user changes via the GDB stub, we don't set PSTATE.IL,
9162              * as this would be unnecessarily harsh for a user error.
9163              */
9164             mask &= ~CPSR_M;
9165             if (write_type != CPSRWriteByGDBStub &&
9166                 arm_feature(env, ARM_FEATURE_V8)) {
9167                 mask |= CPSR_IL;
9168                 val |= CPSR_IL;
9169             }
9170             qemu_log_mask(LOG_GUEST_ERROR,
9171                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9172                           aarch32_mode_name(env->uncached_cpsr),
9173                           aarch32_mode_name(val));
9174         } else {
9175             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9176                           write_type == CPSRWriteExceptionReturn ?
9177                           "Exception return from AArch32" :
9178                           "AArch32 mode switch from",
9179                           aarch32_mode_name(env->uncached_cpsr),
9180                           aarch32_mode_name(val), env->regs[15]);
9181             switch_mode(env, val & CPSR_M);
9182         }
9183     }
9184     mask &= ~CACHED_CPSR_BITS;
9185     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9186     if (rebuild_hflags) {
9187         arm_rebuild_hflags(env);
9188     }
9189 }
9190 
9191 /* Sign/zero extend */
9192 uint32_t HELPER(sxtb16)(uint32_t x)
9193 {
9194     uint32_t res;
9195     res = (uint16_t)(int8_t)x;
9196     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9197     return res;
9198 }
9199 
9200 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9201 {
9202     /*
9203      * Take a division-by-zero exception if necessary; otherwise return
9204      * to get the usual non-trapping division behaviour (result of 0)
9205      */
9206     if (arm_feature(env, ARM_FEATURE_M)
9207         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9208         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9209     }
9210 }
9211 
9212 uint32_t HELPER(uxtb16)(uint32_t x)
9213 {
9214     uint32_t res;
9215     res = (uint16_t)(uint8_t)x;
9216     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9217     return res;
9218 }
9219 
9220 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9221 {
9222     if (den == 0) {
9223         handle_possible_div0_trap(env, GETPC());
9224         return 0;
9225     }
9226     if (num == INT_MIN && den == -1) {
9227         return INT_MIN;
9228     }
9229     return num / den;
9230 }
9231 
9232 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9233 {
9234     if (den == 0) {
9235         handle_possible_div0_trap(env, GETPC());
9236         return 0;
9237     }
9238     return num / den;
9239 }
9240 
9241 uint32_t HELPER(rbit)(uint32_t x)
9242 {
9243     return revbit32(x);
9244 }
9245 
9246 #ifdef CONFIG_USER_ONLY
9247 
9248 static void switch_mode(CPUARMState *env, int mode)
9249 {
9250     ARMCPU *cpu = env_archcpu(env);
9251 
9252     if (mode != ARM_CPU_MODE_USR) {
9253         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9254     }
9255 }
9256 
9257 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9258                                  uint32_t cur_el, bool secure)
9259 {
9260     return 1;
9261 }
9262 
9263 void aarch64_sync_64_to_32(CPUARMState *env)
9264 {
9265     g_assert_not_reached();
9266 }
9267 
9268 #else
9269 
9270 static void switch_mode(CPUARMState *env, int mode)
9271 {
9272     int old_mode;
9273     int i;
9274 
9275     old_mode = env->uncached_cpsr & CPSR_M;
9276     if (mode == old_mode)
9277         return;
9278 
9279     if (old_mode == ARM_CPU_MODE_FIQ) {
9280         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9281         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9282     } else if (mode == ARM_CPU_MODE_FIQ) {
9283         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9284         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9285     }
9286 
9287     i = bank_number(old_mode);
9288     env->banked_r13[i] = env->regs[13];
9289     env->banked_spsr[i] = env->spsr;
9290 
9291     i = bank_number(mode);
9292     env->regs[13] = env->banked_r13[i];
9293     env->spsr = env->banked_spsr[i];
9294 
9295     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9296     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9297 }
9298 
9299 /* Physical Interrupt Target EL Lookup Table
9300  *
9301  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9302  *
9303  * The below multi-dimensional table is used for looking up the target
9304  * exception level given numerous condition criteria.  Specifically, the
9305  * target EL is based on SCR and HCR routing controls as well as the
9306  * currently executing EL and secure state.
9307  *
9308  *    Dimensions:
9309  *    target_el_table[2][2][2][2][2][4]
9310  *                    |  |  |  |  |  +--- Current EL
9311  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9312  *                    |  |  |  +--------- HCR mask override
9313  *                    |  |  +------------ SCR exec state control
9314  *                    |  +--------------- SCR mask override
9315  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9316  *
9317  *    The table values are as such:
9318  *    0-3 = EL0-EL3
9319  *     -1 = Cannot occur
9320  *
9321  * The ARM ARM target EL table includes entries indicating that an "exception
9322  * is not taken".  The two cases where this is applicable are:
9323  *    1) An exception is taken from EL3 but the SCR does not have the exception
9324  *    routed to EL3.
9325  *    2) An exception is taken from EL2 but the HCR does not have the exception
9326  *    routed to EL2.
9327  * In these two cases, the below table contain a target of EL1.  This value is
9328  * returned as it is expected that the consumer of the table data will check
9329  * for "target EL >= current EL" to ensure the exception is not taken.
9330  *
9331  *            SCR     HCR
9332  *         64  EA     AMO                 From
9333  *        BIT IRQ     IMO      Non-secure         Secure
9334  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9335  */
9336 static const int8_t target_el_table[2][2][2][2][2][4] = {
9337     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9338        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9339       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9340        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9341      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9342        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9343       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9344        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9345     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9346        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9347       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9348        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9349      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9350        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9351       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9352        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9353 };
9354 
9355 /*
9356  * Determine the target EL for physical exceptions
9357  */
9358 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9359                                  uint32_t cur_el, bool secure)
9360 {
9361     CPUARMState *env = cs->env_ptr;
9362     bool rw;
9363     bool scr;
9364     bool hcr;
9365     int target_el;
9366     /* Is the highest EL AArch64? */
9367     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9368     uint64_t hcr_el2;
9369 
9370     if (arm_feature(env, ARM_FEATURE_EL3)) {
9371         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9372     } else {
9373         /* Either EL2 is the highest EL (and so the EL2 register width
9374          * is given by is64); or there is no EL2 or EL3, in which case
9375          * the value of 'rw' does not affect the table lookup anyway.
9376          */
9377         rw = is64;
9378     }
9379 
9380     hcr_el2 = arm_hcr_el2_eff(env);
9381     switch (excp_idx) {
9382     case EXCP_IRQ:
9383         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9384         hcr = hcr_el2 & HCR_IMO;
9385         break;
9386     case EXCP_FIQ:
9387         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9388         hcr = hcr_el2 & HCR_FMO;
9389         break;
9390     default:
9391         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9392         hcr = hcr_el2 & HCR_AMO;
9393         break;
9394     };
9395 
9396     /*
9397      * For these purposes, TGE and AMO/IMO/FMO both force the
9398      * interrupt to EL2.  Fold TGE into the bit extracted above.
9399      */
9400     hcr |= (hcr_el2 & HCR_TGE) != 0;
9401 
9402     /* Perform a table-lookup for the target EL given the current state */
9403     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9404 
9405     assert(target_el > 0);
9406 
9407     return target_el;
9408 }
9409 
9410 void arm_log_exception(CPUState *cs)
9411 {
9412     int idx = cs->exception_index;
9413 
9414     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9415         const char *exc = NULL;
9416         static const char * const excnames[] = {
9417             [EXCP_UDEF] = "Undefined Instruction",
9418             [EXCP_SWI] = "SVC",
9419             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9420             [EXCP_DATA_ABORT] = "Data Abort",
9421             [EXCP_IRQ] = "IRQ",
9422             [EXCP_FIQ] = "FIQ",
9423             [EXCP_BKPT] = "Breakpoint",
9424             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9425             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9426             [EXCP_HVC] = "Hypervisor Call",
9427             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9428             [EXCP_SMC] = "Secure Monitor Call",
9429             [EXCP_VIRQ] = "Virtual IRQ",
9430             [EXCP_VFIQ] = "Virtual FIQ",
9431             [EXCP_SEMIHOST] = "Semihosting call",
9432             [EXCP_NOCP] = "v7M NOCP UsageFault",
9433             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9434             [EXCP_STKOF] = "v8M STKOF UsageFault",
9435             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9436             [EXCP_LSERR] = "v8M LSERR UsageFault",
9437             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9438             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9439             [EXCP_VSERR] = "Virtual SERR",
9440         };
9441 
9442         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9443             exc = excnames[idx];
9444         }
9445         if (!exc) {
9446             exc = "unknown";
9447         }
9448         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9449                       idx, exc, cs->cpu_index);
9450     }
9451 }
9452 
9453 /*
9454  * Function used to synchronize QEMU's AArch64 register set with AArch32
9455  * register set.  This is necessary when switching between AArch32 and AArch64
9456  * execution state.
9457  */
9458 void aarch64_sync_32_to_64(CPUARMState *env)
9459 {
9460     int i;
9461     uint32_t mode = env->uncached_cpsr & CPSR_M;
9462 
9463     /* We can blanket copy R[0:7] to X[0:7] */
9464     for (i = 0; i < 8; i++) {
9465         env->xregs[i] = env->regs[i];
9466     }
9467 
9468     /*
9469      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9470      * Otherwise, they come from the banked user regs.
9471      */
9472     if (mode == ARM_CPU_MODE_FIQ) {
9473         for (i = 8; i < 13; i++) {
9474             env->xregs[i] = env->usr_regs[i - 8];
9475         }
9476     } else {
9477         for (i = 8; i < 13; i++) {
9478             env->xregs[i] = env->regs[i];
9479         }
9480     }
9481 
9482     /*
9483      * Registers x13-x23 are the various mode SP and FP registers. Registers
9484      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9485      * from the mode banked register.
9486      */
9487     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9488         env->xregs[13] = env->regs[13];
9489         env->xregs[14] = env->regs[14];
9490     } else {
9491         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9492         /* HYP is an exception in that it is copied from r14 */
9493         if (mode == ARM_CPU_MODE_HYP) {
9494             env->xregs[14] = env->regs[14];
9495         } else {
9496             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9497         }
9498     }
9499 
9500     if (mode == ARM_CPU_MODE_HYP) {
9501         env->xregs[15] = env->regs[13];
9502     } else {
9503         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9504     }
9505 
9506     if (mode == ARM_CPU_MODE_IRQ) {
9507         env->xregs[16] = env->regs[14];
9508         env->xregs[17] = env->regs[13];
9509     } else {
9510         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9511         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9512     }
9513 
9514     if (mode == ARM_CPU_MODE_SVC) {
9515         env->xregs[18] = env->regs[14];
9516         env->xregs[19] = env->regs[13];
9517     } else {
9518         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9519         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9520     }
9521 
9522     if (mode == ARM_CPU_MODE_ABT) {
9523         env->xregs[20] = env->regs[14];
9524         env->xregs[21] = env->regs[13];
9525     } else {
9526         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9527         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9528     }
9529 
9530     if (mode == ARM_CPU_MODE_UND) {
9531         env->xregs[22] = env->regs[14];
9532         env->xregs[23] = env->regs[13];
9533     } else {
9534         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9535         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9536     }
9537 
9538     /*
9539      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9540      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9541      * FIQ bank for r8-r14.
9542      */
9543     if (mode == ARM_CPU_MODE_FIQ) {
9544         for (i = 24; i < 31; i++) {
9545             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9546         }
9547     } else {
9548         for (i = 24; i < 29; i++) {
9549             env->xregs[i] = env->fiq_regs[i - 24];
9550         }
9551         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9552         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9553     }
9554 
9555     env->pc = env->regs[15];
9556 }
9557 
9558 /*
9559  * Function used to synchronize QEMU's AArch32 register set with AArch64
9560  * register set.  This is necessary when switching between AArch32 and AArch64
9561  * execution state.
9562  */
9563 void aarch64_sync_64_to_32(CPUARMState *env)
9564 {
9565     int i;
9566     uint32_t mode = env->uncached_cpsr & CPSR_M;
9567 
9568     /* We can blanket copy X[0:7] to R[0:7] */
9569     for (i = 0; i < 8; i++) {
9570         env->regs[i] = env->xregs[i];
9571     }
9572 
9573     /*
9574      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9575      * Otherwise, we copy x8-x12 into the banked user regs.
9576      */
9577     if (mode == ARM_CPU_MODE_FIQ) {
9578         for (i = 8; i < 13; i++) {
9579             env->usr_regs[i - 8] = env->xregs[i];
9580         }
9581     } else {
9582         for (i = 8; i < 13; i++) {
9583             env->regs[i] = env->xregs[i];
9584         }
9585     }
9586 
9587     /*
9588      * Registers r13 & r14 depend on the current mode.
9589      * If we are in a given mode, we copy the corresponding x registers to r13
9590      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9591      * for the mode.
9592      */
9593     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9594         env->regs[13] = env->xregs[13];
9595         env->regs[14] = env->xregs[14];
9596     } else {
9597         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9598 
9599         /*
9600          * HYP is an exception in that it does not have its own banked r14 but
9601          * shares the USR r14
9602          */
9603         if (mode == ARM_CPU_MODE_HYP) {
9604             env->regs[14] = env->xregs[14];
9605         } else {
9606             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9607         }
9608     }
9609 
9610     if (mode == ARM_CPU_MODE_HYP) {
9611         env->regs[13] = env->xregs[15];
9612     } else {
9613         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9614     }
9615 
9616     if (mode == ARM_CPU_MODE_IRQ) {
9617         env->regs[14] = env->xregs[16];
9618         env->regs[13] = env->xregs[17];
9619     } else {
9620         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9621         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9622     }
9623 
9624     if (mode == ARM_CPU_MODE_SVC) {
9625         env->regs[14] = env->xregs[18];
9626         env->regs[13] = env->xregs[19];
9627     } else {
9628         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9629         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9630     }
9631 
9632     if (mode == ARM_CPU_MODE_ABT) {
9633         env->regs[14] = env->xregs[20];
9634         env->regs[13] = env->xregs[21];
9635     } else {
9636         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9637         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9638     }
9639 
9640     if (mode == ARM_CPU_MODE_UND) {
9641         env->regs[14] = env->xregs[22];
9642         env->regs[13] = env->xregs[23];
9643     } else {
9644         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9645         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9646     }
9647 
9648     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9649      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9650      * FIQ bank for r8-r14.
9651      */
9652     if (mode == ARM_CPU_MODE_FIQ) {
9653         for (i = 24; i < 31; i++) {
9654             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9655         }
9656     } else {
9657         for (i = 24; i < 29; i++) {
9658             env->fiq_regs[i - 24] = env->xregs[i];
9659         }
9660         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9661         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9662     }
9663 
9664     env->regs[15] = env->pc;
9665 }
9666 
9667 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9668                                    uint32_t mask, uint32_t offset,
9669                                    uint32_t newpc)
9670 {
9671     int new_el;
9672 
9673     /* Change the CPU state so as to actually take the exception. */
9674     switch_mode(env, new_mode);
9675 
9676     /*
9677      * For exceptions taken to AArch32 we must clear the SS bit in both
9678      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9679      */
9680     env->pstate &= ~PSTATE_SS;
9681     env->spsr = cpsr_read(env);
9682     /* Clear IT bits.  */
9683     env->condexec_bits = 0;
9684     /* Switch to the new mode, and to the correct instruction set.  */
9685     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9686 
9687     /* This must be after mode switching. */
9688     new_el = arm_current_el(env);
9689 
9690     /* Set new mode endianness */
9691     env->uncached_cpsr &= ~CPSR_E;
9692     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9693         env->uncached_cpsr |= CPSR_E;
9694     }
9695     /* J and IL must always be cleared for exception entry */
9696     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9697     env->daif |= mask;
9698 
9699     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9700         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9701             env->uncached_cpsr |= CPSR_SSBS;
9702         } else {
9703             env->uncached_cpsr &= ~CPSR_SSBS;
9704         }
9705     }
9706 
9707     if (new_mode == ARM_CPU_MODE_HYP) {
9708         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9709         env->elr_el[2] = env->regs[15];
9710     } else {
9711         /* CPSR.PAN is normally preserved preserved unless...  */
9712         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9713             switch (new_el) {
9714             case 3:
9715                 if (!arm_is_secure_below_el3(env)) {
9716                     /* ... the target is EL3, from non-secure state.  */
9717                     env->uncached_cpsr &= ~CPSR_PAN;
9718                     break;
9719                 }
9720                 /* ... the target is EL3, from secure state ... */
9721                 /* fall through */
9722             case 1:
9723                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9724                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9725                     env->uncached_cpsr |= CPSR_PAN;
9726                 }
9727                 break;
9728             }
9729         }
9730         /*
9731          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9732          * and we should just guard the thumb mode on V4
9733          */
9734         if (arm_feature(env, ARM_FEATURE_V4T)) {
9735             env->thumb =
9736                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9737         }
9738         env->regs[14] = env->regs[15] + offset;
9739     }
9740     env->regs[15] = newpc;
9741     arm_rebuild_hflags(env);
9742 }
9743 
9744 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9745 {
9746     /*
9747      * Handle exception entry to Hyp mode; this is sufficiently
9748      * different to entry to other AArch32 modes that we handle it
9749      * separately here.
9750      *
9751      * The vector table entry used is always the 0x14 Hyp mode entry point,
9752      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9753      * The offset applied to the preferred return address is always zero
9754      * (see DDI0487C.a section G1.12.3).
9755      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9756      */
9757     uint32_t addr, mask;
9758     ARMCPU *cpu = ARM_CPU(cs);
9759     CPUARMState *env = &cpu->env;
9760 
9761     switch (cs->exception_index) {
9762     case EXCP_UDEF:
9763         addr = 0x04;
9764         break;
9765     case EXCP_SWI:
9766         addr = 0x08;
9767         break;
9768     case EXCP_BKPT:
9769         /* Fall through to prefetch abort.  */
9770     case EXCP_PREFETCH_ABORT:
9771         env->cp15.ifar_s = env->exception.vaddress;
9772         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9773                       (uint32_t)env->exception.vaddress);
9774         addr = 0x0c;
9775         break;
9776     case EXCP_DATA_ABORT:
9777         env->cp15.dfar_s = env->exception.vaddress;
9778         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9779                       (uint32_t)env->exception.vaddress);
9780         addr = 0x10;
9781         break;
9782     case EXCP_IRQ:
9783         addr = 0x18;
9784         break;
9785     case EXCP_FIQ:
9786         addr = 0x1c;
9787         break;
9788     case EXCP_HVC:
9789         addr = 0x08;
9790         break;
9791     case EXCP_HYP_TRAP:
9792         addr = 0x14;
9793         break;
9794     default:
9795         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9796     }
9797 
9798     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9799         if (!arm_feature(env, ARM_FEATURE_V8)) {
9800             /*
9801              * QEMU syndrome values are v8-style. v7 has the IL bit
9802              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9803              * If this is a v7 CPU, squash the IL bit in those cases.
9804              */
9805             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9806                 (cs->exception_index == EXCP_DATA_ABORT &&
9807                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9808                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9809                 env->exception.syndrome &= ~ARM_EL_IL;
9810             }
9811         }
9812         env->cp15.esr_el[2] = env->exception.syndrome;
9813     }
9814 
9815     if (arm_current_el(env) != 2 && addr < 0x14) {
9816         addr = 0x14;
9817     }
9818 
9819     mask = 0;
9820     if (!(env->cp15.scr_el3 & SCR_EA)) {
9821         mask |= CPSR_A;
9822     }
9823     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9824         mask |= CPSR_I;
9825     }
9826     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9827         mask |= CPSR_F;
9828     }
9829 
9830     addr += env->cp15.hvbar;
9831 
9832     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9833 }
9834 
9835 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9836 {
9837     ARMCPU *cpu = ARM_CPU(cs);
9838     CPUARMState *env = &cpu->env;
9839     uint32_t addr;
9840     uint32_t mask;
9841     int new_mode;
9842     uint32_t offset;
9843     uint32_t moe;
9844 
9845     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9846     switch (syn_get_ec(env->exception.syndrome)) {
9847     case EC_BREAKPOINT:
9848     case EC_BREAKPOINT_SAME_EL:
9849         moe = 1;
9850         break;
9851     case EC_WATCHPOINT:
9852     case EC_WATCHPOINT_SAME_EL:
9853         moe = 10;
9854         break;
9855     case EC_AA32_BKPT:
9856         moe = 3;
9857         break;
9858     case EC_VECTORCATCH:
9859         moe = 5;
9860         break;
9861     default:
9862         moe = 0;
9863         break;
9864     }
9865 
9866     if (moe) {
9867         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9868     }
9869 
9870     if (env->exception.target_el == 2) {
9871         arm_cpu_do_interrupt_aarch32_hyp(cs);
9872         return;
9873     }
9874 
9875     switch (cs->exception_index) {
9876     case EXCP_UDEF:
9877         new_mode = ARM_CPU_MODE_UND;
9878         addr = 0x04;
9879         mask = CPSR_I;
9880         if (env->thumb)
9881             offset = 2;
9882         else
9883             offset = 4;
9884         break;
9885     case EXCP_SWI:
9886         new_mode = ARM_CPU_MODE_SVC;
9887         addr = 0x08;
9888         mask = CPSR_I;
9889         /* The PC already points to the next instruction.  */
9890         offset = 0;
9891         break;
9892     case EXCP_BKPT:
9893         /* Fall through to prefetch abort.  */
9894     case EXCP_PREFETCH_ABORT:
9895         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9896         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9897         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9898                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9899         new_mode = ARM_CPU_MODE_ABT;
9900         addr = 0x0c;
9901         mask = CPSR_A | CPSR_I;
9902         offset = 4;
9903         break;
9904     case EXCP_DATA_ABORT:
9905         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9906         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9907         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9908                       env->exception.fsr,
9909                       (uint32_t)env->exception.vaddress);
9910         new_mode = ARM_CPU_MODE_ABT;
9911         addr = 0x10;
9912         mask = CPSR_A | CPSR_I;
9913         offset = 8;
9914         break;
9915     case EXCP_IRQ:
9916         new_mode = ARM_CPU_MODE_IRQ;
9917         addr = 0x18;
9918         /* Disable IRQ and imprecise data aborts.  */
9919         mask = CPSR_A | CPSR_I;
9920         offset = 4;
9921         if (env->cp15.scr_el3 & SCR_IRQ) {
9922             /* IRQ routed to monitor mode */
9923             new_mode = ARM_CPU_MODE_MON;
9924             mask |= CPSR_F;
9925         }
9926         break;
9927     case EXCP_FIQ:
9928         new_mode = ARM_CPU_MODE_FIQ;
9929         addr = 0x1c;
9930         /* Disable FIQ, IRQ and imprecise data aborts.  */
9931         mask = CPSR_A | CPSR_I | CPSR_F;
9932         if (env->cp15.scr_el3 & SCR_FIQ) {
9933             /* FIQ routed to monitor mode */
9934             new_mode = ARM_CPU_MODE_MON;
9935         }
9936         offset = 4;
9937         break;
9938     case EXCP_VIRQ:
9939         new_mode = ARM_CPU_MODE_IRQ;
9940         addr = 0x18;
9941         /* Disable IRQ and imprecise data aborts.  */
9942         mask = CPSR_A | CPSR_I;
9943         offset = 4;
9944         break;
9945     case EXCP_VFIQ:
9946         new_mode = ARM_CPU_MODE_FIQ;
9947         addr = 0x1c;
9948         /* Disable FIQ, IRQ and imprecise data aborts.  */
9949         mask = CPSR_A | CPSR_I | CPSR_F;
9950         offset = 4;
9951         break;
9952     case EXCP_VSERR:
9953         {
9954             /*
9955              * Note that this is reported as a data abort, but the DFAR
9956              * has an UNKNOWN value.  Construct the SError syndrome from
9957              * AET and ExT fields.
9958              */
9959             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9960 
9961             if (extended_addresses_enabled(env)) {
9962                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9963             } else {
9964                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9965             }
9966             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9967             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9968             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9969                           env->exception.fsr);
9970 
9971             new_mode = ARM_CPU_MODE_ABT;
9972             addr = 0x10;
9973             mask = CPSR_A | CPSR_I;
9974             offset = 8;
9975         }
9976         break;
9977     case EXCP_SMC:
9978         new_mode = ARM_CPU_MODE_MON;
9979         addr = 0x08;
9980         mask = CPSR_A | CPSR_I | CPSR_F;
9981         offset = 0;
9982         break;
9983     default:
9984         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9985         return; /* Never happens.  Keep compiler happy.  */
9986     }
9987 
9988     if (new_mode == ARM_CPU_MODE_MON) {
9989         addr += env->cp15.mvbar;
9990     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9991         /* High vectors. When enabled, base address cannot be remapped. */
9992         addr += 0xffff0000;
9993     } else {
9994         /* ARM v7 architectures provide a vector base address register to remap
9995          * the interrupt vector table.
9996          * This register is only followed in non-monitor mode, and is banked.
9997          * Note: only bits 31:5 are valid.
9998          */
9999         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10000     }
10001 
10002     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10003         env->cp15.scr_el3 &= ~SCR_NS;
10004     }
10005 
10006     take_aarch32_exception(env, new_mode, mask, offset, addr);
10007 }
10008 
10009 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10010 {
10011     /*
10012      * Return the register number of the AArch64 view of the AArch32
10013      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10014      * be that of the AArch32 mode the exception came from.
10015      */
10016     int mode = env->uncached_cpsr & CPSR_M;
10017 
10018     switch (aarch32_reg) {
10019     case 0 ... 7:
10020         return aarch32_reg;
10021     case 8 ... 12:
10022         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10023     case 13:
10024         switch (mode) {
10025         case ARM_CPU_MODE_USR:
10026         case ARM_CPU_MODE_SYS:
10027             return 13;
10028         case ARM_CPU_MODE_HYP:
10029             return 15;
10030         case ARM_CPU_MODE_IRQ:
10031             return 17;
10032         case ARM_CPU_MODE_SVC:
10033             return 19;
10034         case ARM_CPU_MODE_ABT:
10035             return 21;
10036         case ARM_CPU_MODE_UND:
10037             return 23;
10038         case ARM_CPU_MODE_FIQ:
10039             return 29;
10040         default:
10041             g_assert_not_reached();
10042         }
10043     case 14:
10044         switch (mode) {
10045         case ARM_CPU_MODE_USR:
10046         case ARM_CPU_MODE_SYS:
10047         case ARM_CPU_MODE_HYP:
10048             return 14;
10049         case ARM_CPU_MODE_IRQ:
10050             return 16;
10051         case ARM_CPU_MODE_SVC:
10052             return 18;
10053         case ARM_CPU_MODE_ABT:
10054             return 20;
10055         case ARM_CPU_MODE_UND:
10056             return 22;
10057         case ARM_CPU_MODE_FIQ:
10058             return 30;
10059         default:
10060             g_assert_not_reached();
10061         }
10062     case 15:
10063         return 31;
10064     default:
10065         g_assert_not_reached();
10066     }
10067 }
10068 
10069 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10070 {
10071     uint32_t ret = cpsr_read(env);
10072 
10073     /* Move DIT to the correct location for SPSR_ELx */
10074     if (ret & CPSR_DIT) {
10075         ret &= ~CPSR_DIT;
10076         ret |= PSTATE_DIT;
10077     }
10078     /* Merge PSTATE.SS into SPSR_ELx */
10079     ret |= env->pstate & PSTATE_SS;
10080 
10081     return ret;
10082 }
10083 
10084 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10085 {
10086     /* Return true if this syndrome value is a synchronous external abort */
10087     switch (syn_get_ec(syndrome)) {
10088     case EC_INSNABORT:
10089     case EC_INSNABORT_SAME_EL:
10090     case EC_DATAABORT:
10091     case EC_DATAABORT_SAME_EL:
10092         /* Look at fault status code for all the synchronous ext abort cases */
10093         switch (syndrome & 0x3f) {
10094         case 0x10:
10095         case 0x13:
10096         case 0x14:
10097         case 0x15:
10098         case 0x16:
10099         case 0x17:
10100             return true;
10101         default:
10102             return false;
10103         }
10104     default:
10105         return false;
10106     }
10107 }
10108 
10109 /* Handle exception entry to a target EL which is using AArch64 */
10110 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10111 {
10112     ARMCPU *cpu = ARM_CPU(cs);
10113     CPUARMState *env = &cpu->env;
10114     unsigned int new_el = env->exception.target_el;
10115     target_ulong addr = env->cp15.vbar_el[new_el];
10116     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10117     unsigned int old_mode;
10118     unsigned int cur_el = arm_current_el(env);
10119     int rt;
10120 
10121     /*
10122      * Note that new_el can never be 0.  If cur_el is 0, then
10123      * el0_a64 is is_a64(), else el0_a64 is ignored.
10124      */
10125     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10126 
10127     if (cur_el < new_el) {
10128         /* Entry vector offset depends on whether the implemented EL
10129          * immediately lower than the target level is using AArch32 or AArch64
10130          */
10131         bool is_aa64;
10132         uint64_t hcr;
10133 
10134         switch (new_el) {
10135         case 3:
10136             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10137             break;
10138         case 2:
10139             hcr = arm_hcr_el2_eff(env);
10140             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10141                 is_aa64 = (hcr & HCR_RW) != 0;
10142                 break;
10143             }
10144             /* fall through */
10145         case 1:
10146             is_aa64 = is_a64(env);
10147             break;
10148         default:
10149             g_assert_not_reached();
10150         }
10151 
10152         if (is_aa64) {
10153             addr += 0x400;
10154         } else {
10155             addr += 0x600;
10156         }
10157     } else if (pstate_read(env) & PSTATE_SP) {
10158         addr += 0x200;
10159     }
10160 
10161     switch (cs->exception_index) {
10162     case EXCP_PREFETCH_ABORT:
10163     case EXCP_DATA_ABORT:
10164         /*
10165          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10166          * to be taken to the SError vector entrypoint.
10167          */
10168         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10169             syndrome_is_sync_extabt(env->exception.syndrome)) {
10170             addr += 0x180;
10171         }
10172         env->cp15.far_el[new_el] = env->exception.vaddress;
10173         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10174                       env->cp15.far_el[new_el]);
10175         /* fall through */
10176     case EXCP_BKPT:
10177     case EXCP_UDEF:
10178     case EXCP_SWI:
10179     case EXCP_HVC:
10180     case EXCP_HYP_TRAP:
10181     case EXCP_SMC:
10182         switch (syn_get_ec(env->exception.syndrome)) {
10183         case EC_ADVSIMDFPACCESSTRAP:
10184             /*
10185              * QEMU internal FP/SIMD syndromes from AArch32 include the
10186              * TA and coproc fields which are only exposed if the exception
10187              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10188              * AArch64 format syndrome.
10189              */
10190             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10191             break;
10192         case EC_CP14RTTRAP:
10193         case EC_CP15RTTRAP:
10194         case EC_CP14DTTRAP:
10195             /*
10196              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10197              * the raw register field from the insn; when taking this to
10198              * AArch64 we must convert it to the AArch64 view of the register
10199              * number. Notice that we read a 4-bit AArch32 register number and
10200              * write back a 5-bit AArch64 one.
10201              */
10202             rt = extract32(env->exception.syndrome, 5, 4);
10203             rt = aarch64_regnum(env, rt);
10204             env->exception.syndrome = deposit32(env->exception.syndrome,
10205                                                 5, 5, rt);
10206             break;
10207         case EC_CP15RRTTRAP:
10208         case EC_CP14RRTTRAP:
10209             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10210             rt = extract32(env->exception.syndrome, 5, 4);
10211             rt = aarch64_regnum(env, rt);
10212             env->exception.syndrome = deposit32(env->exception.syndrome,
10213                                                 5, 5, rt);
10214             rt = extract32(env->exception.syndrome, 10, 4);
10215             rt = aarch64_regnum(env, rt);
10216             env->exception.syndrome = deposit32(env->exception.syndrome,
10217                                                 10, 5, rt);
10218             break;
10219         }
10220         env->cp15.esr_el[new_el] = env->exception.syndrome;
10221         break;
10222     case EXCP_IRQ:
10223     case EXCP_VIRQ:
10224         addr += 0x80;
10225         break;
10226     case EXCP_FIQ:
10227     case EXCP_VFIQ:
10228         addr += 0x100;
10229         break;
10230     case EXCP_VSERR:
10231         addr += 0x180;
10232         /* Construct the SError syndrome from IDS and ISS fields. */
10233         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10234         env->cp15.esr_el[new_el] = env->exception.syndrome;
10235         break;
10236     default:
10237         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10238     }
10239 
10240     if (is_a64(env)) {
10241         old_mode = pstate_read(env);
10242         aarch64_save_sp(env, arm_current_el(env));
10243         env->elr_el[new_el] = env->pc;
10244     } else {
10245         old_mode = cpsr_read_for_spsr_elx(env);
10246         env->elr_el[new_el] = env->regs[15];
10247 
10248         aarch64_sync_32_to_64(env);
10249 
10250         env->condexec_bits = 0;
10251     }
10252     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10253 
10254     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10255                   env->elr_el[new_el]);
10256 
10257     if (cpu_isar_feature(aa64_pan, cpu)) {
10258         /* The value of PSTATE.PAN is normally preserved, except when ... */
10259         new_mode |= old_mode & PSTATE_PAN;
10260         switch (new_el) {
10261         case 2:
10262             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10263             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10264                 != (HCR_E2H | HCR_TGE)) {
10265                 break;
10266             }
10267             /* fall through */
10268         case 1:
10269             /* ... the target is EL1 ... */
10270             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10271             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10272                 new_mode |= PSTATE_PAN;
10273             }
10274             break;
10275         }
10276     }
10277     if (cpu_isar_feature(aa64_mte, cpu)) {
10278         new_mode |= PSTATE_TCO;
10279     }
10280 
10281     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10282         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10283             new_mode |= PSTATE_SSBS;
10284         } else {
10285             new_mode &= ~PSTATE_SSBS;
10286         }
10287     }
10288 
10289     pstate_write(env, PSTATE_DAIF | new_mode);
10290     env->aarch64 = true;
10291     aarch64_restore_sp(env, new_el);
10292     helper_rebuild_hflags_a64(env, new_el);
10293 
10294     env->pc = addr;
10295 
10296     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10297                   new_el, env->pc, pstate_read(env));
10298 }
10299 
10300 /*
10301  * Do semihosting call and set the appropriate return value. All the
10302  * permission and validity checks have been done at translate time.
10303  *
10304  * We only see semihosting exceptions in TCG only as they are not
10305  * trapped to the hypervisor in KVM.
10306  */
10307 #ifdef CONFIG_TCG
10308 static void handle_semihosting(CPUState *cs)
10309 {
10310     ARMCPU *cpu = ARM_CPU(cs);
10311     CPUARMState *env = &cpu->env;
10312 
10313     if (is_a64(env)) {
10314         qemu_log_mask(CPU_LOG_INT,
10315                       "...handling as semihosting call 0x%" PRIx64 "\n",
10316                       env->xregs[0]);
10317         do_common_semihosting(cs);
10318         env->pc += 4;
10319     } else {
10320         qemu_log_mask(CPU_LOG_INT,
10321                       "...handling as semihosting call 0x%x\n",
10322                       env->regs[0]);
10323         do_common_semihosting(cs);
10324         env->regs[15] += env->thumb ? 2 : 4;
10325     }
10326 }
10327 #endif
10328 
10329 /* Handle a CPU exception for A and R profile CPUs.
10330  * Do any appropriate logging, handle PSCI calls, and then hand off
10331  * to the AArch64-entry or AArch32-entry function depending on the
10332  * target exception level's register width.
10333  *
10334  * Note: this is used for both TCG (as the do_interrupt tcg op),
10335  *       and KVM to re-inject guest debug exceptions, and to
10336  *       inject a Synchronous-External-Abort.
10337  */
10338 void arm_cpu_do_interrupt(CPUState *cs)
10339 {
10340     ARMCPU *cpu = ARM_CPU(cs);
10341     CPUARMState *env = &cpu->env;
10342     unsigned int new_el = env->exception.target_el;
10343 
10344     assert(!arm_feature(env, ARM_FEATURE_M));
10345 
10346     arm_log_exception(cs);
10347     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10348                   new_el);
10349     if (qemu_loglevel_mask(CPU_LOG_INT)
10350         && !excp_is_internal(cs->exception_index)) {
10351         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10352                       syn_get_ec(env->exception.syndrome),
10353                       env->exception.syndrome);
10354     }
10355 
10356     if (arm_is_psci_call(cpu, cs->exception_index)) {
10357         arm_handle_psci_call(cpu);
10358         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10359         return;
10360     }
10361 
10362     /*
10363      * Semihosting semantics depend on the register width of the code
10364      * that caused the exception, not the target exception level, so
10365      * must be handled here.
10366      */
10367 #ifdef CONFIG_TCG
10368     if (cs->exception_index == EXCP_SEMIHOST) {
10369         handle_semihosting(cs);
10370         return;
10371     }
10372 #endif
10373 
10374     /* Hooks may change global state so BQL should be held, also the
10375      * BQL needs to be held for any modification of
10376      * cs->interrupt_request.
10377      */
10378     g_assert(qemu_mutex_iothread_locked());
10379 
10380     arm_call_pre_el_change_hook(cpu);
10381 
10382     assert(!excp_is_internal(cs->exception_index));
10383     if (arm_el_is_aa64(env, new_el)) {
10384         arm_cpu_do_interrupt_aarch64(cs);
10385     } else {
10386         arm_cpu_do_interrupt_aarch32(cs);
10387     }
10388 
10389     arm_call_el_change_hook(cpu);
10390 
10391     if (!kvm_enabled()) {
10392         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10393     }
10394 }
10395 #endif /* !CONFIG_USER_ONLY */
10396 
10397 uint64_t arm_sctlr(CPUARMState *env, int el)
10398 {
10399     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10400     if (el == 0) {
10401         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10402         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10403     }
10404     return env->cp15.sctlr_el[el];
10405 }
10406 
10407 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10408 {
10409     if (regime_has_2_ranges(mmu_idx)) {
10410         return extract64(tcr, 37, 2);
10411     } else if (regime_is_stage2(mmu_idx)) {
10412         return 0; /* VTCR_EL2 */
10413     } else {
10414         /* Replicate the single TBI bit so we always have 2 bits.  */
10415         return extract32(tcr, 20, 1) * 3;
10416     }
10417 }
10418 
10419 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10420 {
10421     if (regime_has_2_ranges(mmu_idx)) {
10422         return extract64(tcr, 51, 2);
10423     } else if (regime_is_stage2(mmu_idx)) {
10424         return 0; /* VTCR_EL2 */
10425     } else {
10426         /* Replicate the single TBID bit so we always have 2 bits.  */
10427         return extract32(tcr, 29, 1) * 3;
10428     }
10429 }
10430 
10431 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10432 {
10433     if (regime_has_2_ranges(mmu_idx)) {
10434         return extract64(tcr, 57, 2);
10435     } else {
10436         /* Replicate the single TCMA bit so we always have 2 bits.  */
10437         return extract32(tcr, 30, 1) * 3;
10438     }
10439 }
10440 
10441 static ARMGranuleSize tg0_to_gran_size(int tg)
10442 {
10443     switch (tg) {
10444     case 0:
10445         return Gran4K;
10446     case 1:
10447         return Gran64K;
10448     case 2:
10449         return Gran16K;
10450     default:
10451         return GranInvalid;
10452     }
10453 }
10454 
10455 static ARMGranuleSize tg1_to_gran_size(int tg)
10456 {
10457     switch (tg) {
10458     case 1:
10459         return Gran16K;
10460     case 2:
10461         return Gran4K;
10462     case 3:
10463         return Gran64K;
10464     default:
10465         return GranInvalid;
10466     }
10467 }
10468 
10469 static inline bool have4k(ARMCPU *cpu, bool stage2)
10470 {
10471     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10472         : cpu_isar_feature(aa64_tgran4, cpu);
10473 }
10474 
10475 static inline bool have16k(ARMCPU *cpu, bool stage2)
10476 {
10477     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10478         : cpu_isar_feature(aa64_tgran16, cpu);
10479 }
10480 
10481 static inline bool have64k(ARMCPU *cpu, bool stage2)
10482 {
10483     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10484         : cpu_isar_feature(aa64_tgran64, cpu);
10485 }
10486 
10487 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10488                                          bool stage2)
10489 {
10490     switch (gran) {
10491     case Gran4K:
10492         if (have4k(cpu, stage2)) {
10493             return gran;
10494         }
10495         break;
10496     case Gran16K:
10497         if (have16k(cpu, stage2)) {
10498             return gran;
10499         }
10500         break;
10501     case Gran64K:
10502         if (have64k(cpu, stage2)) {
10503             return gran;
10504         }
10505         break;
10506     case GranInvalid:
10507         break;
10508     }
10509     /*
10510      * If the guest selects a granule size that isn't implemented,
10511      * the architecture requires that we behave as if it selected one
10512      * that is (with an IMPDEF choice of which one to pick). We choose
10513      * to implement the smallest supported granule size.
10514      */
10515     if (have4k(cpu, stage2)) {
10516         return Gran4K;
10517     }
10518     if (have16k(cpu, stage2)) {
10519         return Gran16K;
10520     }
10521     assert(have64k(cpu, stage2));
10522     return Gran64K;
10523 }
10524 
10525 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10526                                    ARMMMUIdx mmu_idx, bool data)
10527 {
10528     uint64_t tcr = regime_tcr(env, mmu_idx);
10529     bool epd, hpd, tsz_oob, ds, ha, hd;
10530     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10531     ARMGranuleSize gran;
10532     ARMCPU *cpu = env_archcpu(env);
10533     bool stage2 = regime_is_stage2(mmu_idx);
10534 
10535     if (!regime_has_2_ranges(mmu_idx)) {
10536         select = 0;
10537         tsz = extract32(tcr, 0, 6);
10538         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10539         if (stage2) {
10540             /* VTCR_EL2 */
10541             hpd = false;
10542         } else {
10543             hpd = extract32(tcr, 24, 1);
10544         }
10545         epd = false;
10546         sh = extract32(tcr, 12, 2);
10547         ps = extract32(tcr, 16, 3);
10548         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
10549         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10550         ds = extract64(tcr, 32, 1);
10551     } else {
10552         bool e0pd;
10553 
10554         /*
10555          * Bit 55 is always between the two regions, and is canonical for
10556          * determining if address tagging is enabled.
10557          */
10558         select = extract64(va, 55, 1);
10559         if (!select) {
10560             tsz = extract32(tcr, 0, 6);
10561             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
10562             epd = extract32(tcr, 7, 1);
10563             sh = extract32(tcr, 12, 2);
10564             hpd = extract64(tcr, 41, 1);
10565             e0pd = extract64(tcr, 55, 1);
10566         } else {
10567             tsz = extract32(tcr, 16, 6);
10568             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
10569             epd = extract32(tcr, 23, 1);
10570             sh = extract32(tcr, 28, 2);
10571             hpd = extract64(tcr, 42, 1);
10572             e0pd = extract64(tcr, 56, 1);
10573         }
10574         ps = extract64(tcr, 32, 3);
10575         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
10576         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
10577         ds = extract64(tcr, 59, 1);
10578 
10579         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
10580             regime_is_user(env, mmu_idx)) {
10581             epd = true;
10582         }
10583     }
10584 
10585     gran = sanitize_gran_size(cpu, gran, stage2);
10586 
10587     if (cpu_isar_feature(aa64_st, cpu)) {
10588         max_tsz = 48 - (gran == Gran64K);
10589     } else {
10590         max_tsz = 39;
10591     }
10592 
10593     /*
10594      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10595      * adjust the effective value of DS, as documented.
10596      */
10597     min_tsz = 16;
10598     if (gran == Gran64K) {
10599         if (cpu_isar_feature(aa64_lva, cpu)) {
10600             min_tsz = 12;
10601         }
10602         ds = false;
10603     } else if (ds) {
10604         if (regime_is_stage2(mmu_idx)) {
10605             if (gran == Gran16K) {
10606                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10607             } else {
10608                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10609             }
10610         } else {
10611             if (gran == Gran16K) {
10612                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10613             } else {
10614                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10615             }
10616         }
10617         if (ds) {
10618             min_tsz = 12;
10619         }
10620     }
10621 
10622     if (tsz > max_tsz) {
10623         tsz = max_tsz;
10624         tsz_oob = true;
10625     } else if (tsz < min_tsz) {
10626         tsz = min_tsz;
10627         tsz_oob = true;
10628     } else {
10629         tsz_oob = false;
10630     }
10631 
10632     /* Present TBI as a composite with TBID.  */
10633     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10634     if (!data) {
10635         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10636     }
10637     tbi = (tbi >> select) & 1;
10638 
10639     return (ARMVAParameters) {
10640         .tsz = tsz,
10641         .ps = ps,
10642         .sh = sh,
10643         .select = select,
10644         .tbi = tbi,
10645         .epd = epd,
10646         .hpd = hpd,
10647         .tsz_oob = tsz_oob,
10648         .ds = ds,
10649         .ha = ha,
10650         .hd = ha && hd,
10651         .gran = gran,
10652     };
10653 }
10654 
10655 /* Note that signed overflow is undefined in C.  The following routines are
10656    careful to use unsigned types where modulo arithmetic is required.
10657    Failure to do so _will_ break on newer gcc.  */
10658 
10659 /* Signed saturating arithmetic.  */
10660 
10661 /* Perform 16-bit signed saturating addition.  */
10662 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10663 {
10664     uint16_t res;
10665 
10666     res = a + b;
10667     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10668         if (a & 0x8000)
10669             res = 0x8000;
10670         else
10671             res = 0x7fff;
10672     }
10673     return res;
10674 }
10675 
10676 /* Perform 8-bit signed saturating addition.  */
10677 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10678 {
10679     uint8_t res;
10680 
10681     res = a + b;
10682     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10683         if (a & 0x80)
10684             res = 0x80;
10685         else
10686             res = 0x7f;
10687     }
10688     return res;
10689 }
10690 
10691 /* Perform 16-bit signed saturating subtraction.  */
10692 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10693 {
10694     uint16_t res;
10695 
10696     res = a - b;
10697     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10698         if (a & 0x8000)
10699             res = 0x8000;
10700         else
10701             res = 0x7fff;
10702     }
10703     return res;
10704 }
10705 
10706 /* Perform 8-bit signed saturating subtraction.  */
10707 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10708 {
10709     uint8_t res;
10710 
10711     res = a - b;
10712     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10713         if (a & 0x80)
10714             res = 0x80;
10715         else
10716             res = 0x7f;
10717     }
10718     return res;
10719 }
10720 
10721 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10722 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10723 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10724 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10725 #define PFX q
10726 
10727 #include "op_addsub.h"
10728 
10729 /* Unsigned saturating arithmetic.  */
10730 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10731 {
10732     uint16_t res;
10733     res = a + b;
10734     if (res < a)
10735         res = 0xffff;
10736     return res;
10737 }
10738 
10739 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10740 {
10741     if (a > b)
10742         return a - b;
10743     else
10744         return 0;
10745 }
10746 
10747 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10748 {
10749     uint8_t res;
10750     res = a + b;
10751     if (res < a)
10752         res = 0xff;
10753     return res;
10754 }
10755 
10756 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10757 {
10758     if (a > b)
10759         return a - b;
10760     else
10761         return 0;
10762 }
10763 
10764 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10765 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10766 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10767 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10768 #define PFX uq
10769 
10770 #include "op_addsub.h"
10771 
10772 /* Signed modulo arithmetic.  */
10773 #define SARITH16(a, b, n, op) do { \
10774     int32_t sum; \
10775     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10776     RESULT(sum, n, 16); \
10777     if (sum >= 0) \
10778         ge |= 3 << (n * 2); \
10779     } while(0)
10780 
10781 #define SARITH8(a, b, n, op) do { \
10782     int32_t sum; \
10783     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10784     RESULT(sum, n, 8); \
10785     if (sum >= 0) \
10786         ge |= 1 << n; \
10787     } while(0)
10788 
10789 
10790 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10791 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10792 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10793 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10794 #define PFX s
10795 #define ARITH_GE
10796 
10797 #include "op_addsub.h"
10798 
10799 /* Unsigned modulo arithmetic.  */
10800 #define ADD16(a, b, n) do { \
10801     uint32_t sum; \
10802     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10803     RESULT(sum, n, 16); \
10804     if ((sum >> 16) == 1) \
10805         ge |= 3 << (n * 2); \
10806     } while(0)
10807 
10808 #define ADD8(a, b, n) do { \
10809     uint32_t sum; \
10810     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10811     RESULT(sum, n, 8); \
10812     if ((sum >> 8) == 1) \
10813         ge |= 1 << n; \
10814     } while(0)
10815 
10816 #define SUB16(a, b, n) do { \
10817     uint32_t sum; \
10818     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10819     RESULT(sum, n, 16); \
10820     if ((sum >> 16) == 0) \
10821         ge |= 3 << (n * 2); \
10822     } while(0)
10823 
10824 #define SUB8(a, b, n) do { \
10825     uint32_t sum; \
10826     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10827     RESULT(sum, n, 8); \
10828     if ((sum >> 8) == 0) \
10829         ge |= 1 << n; \
10830     } while(0)
10831 
10832 #define PFX u
10833 #define ARITH_GE
10834 
10835 #include "op_addsub.h"
10836 
10837 /* Halved signed arithmetic.  */
10838 #define ADD16(a, b, n) \
10839   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10840 #define SUB16(a, b, n) \
10841   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10842 #define ADD8(a, b, n) \
10843   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10844 #define SUB8(a, b, n) \
10845   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10846 #define PFX sh
10847 
10848 #include "op_addsub.h"
10849 
10850 /* Halved unsigned arithmetic.  */
10851 #define ADD16(a, b, n) \
10852   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10853 #define SUB16(a, b, n) \
10854   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10855 #define ADD8(a, b, n) \
10856   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10857 #define SUB8(a, b, n) \
10858   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10859 #define PFX uh
10860 
10861 #include "op_addsub.h"
10862 
10863 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10864 {
10865     if (a > b)
10866         return a - b;
10867     else
10868         return b - a;
10869 }
10870 
10871 /* Unsigned sum of absolute byte differences.  */
10872 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10873 {
10874     uint32_t sum;
10875     sum = do_usad(a, b);
10876     sum += do_usad(a >> 8, b >> 8);
10877     sum += do_usad(a >> 16, b >> 16);
10878     sum += do_usad(a >> 24, b >> 24);
10879     return sum;
10880 }
10881 
10882 /* For ARMv6 SEL instruction.  */
10883 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10884 {
10885     uint32_t mask;
10886 
10887     mask = 0;
10888     if (flags & 1)
10889         mask |= 0xff;
10890     if (flags & 2)
10891         mask |= 0xff00;
10892     if (flags & 4)
10893         mask |= 0xff0000;
10894     if (flags & 8)
10895         mask |= 0xff000000;
10896     return (a & mask) | (b & ~mask);
10897 }
10898 
10899 /* CRC helpers.
10900  * The upper bytes of val (above the number specified by 'bytes') must have
10901  * been zeroed out by the caller.
10902  */
10903 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10904 {
10905     uint8_t buf[4];
10906 
10907     stl_le_p(buf, val);
10908 
10909     /* zlib crc32 converts the accumulator and output to one's complement.  */
10910     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10911 }
10912 
10913 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10914 {
10915     uint8_t buf[4];
10916 
10917     stl_le_p(buf, val);
10918 
10919     /* Linux crc32c converts the output to one's complement.  */
10920     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10921 }
10922 
10923 /* Return the exception level to which FP-disabled exceptions should
10924  * be taken, or 0 if FP is enabled.
10925  */
10926 int fp_exception_el(CPUARMState *env, int cur_el)
10927 {
10928 #ifndef CONFIG_USER_ONLY
10929     uint64_t hcr_el2;
10930 
10931     /* CPACR and the CPTR registers don't exist before v6, so FP is
10932      * always accessible
10933      */
10934     if (!arm_feature(env, ARM_FEATURE_V6)) {
10935         return 0;
10936     }
10937 
10938     if (arm_feature(env, ARM_FEATURE_M)) {
10939         /* CPACR can cause a NOCP UsageFault taken to current security state */
10940         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10941             return 1;
10942         }
10943 
10944         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10945             if (!extract32(env->v7m.nsacr, 10, 1)) {
10946                 /* FP insns cause a NOCP UsageFault taken to Secure */
10947                 return 3;
10948             }
10949         }
10950 
10951         return 0;
10952     }
10953 
10954     hcr_el2 = arm_hcr_el2_eff(env);
10955 
10956     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10957      * 0, 2 : trap EL0 and EL1/PL1 accesses
10958      * 1    : trap only EL0 accesses
10959      * 3    : trap no accesses
10960      * This register is ignored if E2H+TGE are both set.
10961      */
10962     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10963         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10964 
10965         switch (fpen) {
10966         case 1:
10967             if (cur_el != 0) {
10968                 break;
10969             }
10970             /* fall through */
10971         case 0:
10972         case 2:
10973             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10974             if (!arm_el_is_aa64(env, 3)
10975                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10976                 return 3;
10977             }
10978             if (cur_el <= 1) {
10979                 return 1;
10980             }
10981             break;
10982         }
10983     }
10984 
10985     /*
10986      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10987      * to control non-secure access to the FPU. It doesn't have any
10988      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10989      */
10990     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10991          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10992         if (!extract32(env->cp15.nsacr, 10, 1)) {
10993             /* FP insns act as UNDEF */
10994             return cur_el == 2 ? 2 : 1;
10995         }
10996     }
10997 
10998     /*
10999      * CPTR_EL2 is present in v7VE or v8, and changes format
11000      * with HCR_EL2.E2H (regardless of TGE).
11001      */
11002     if (cur_el <= 2) {
11003         if (hcr_el2 & HCR_E2H) {
11004             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11005             case 1:
11006                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11007                     break;
11008                 }
11009                 /* fall through */
11010             case 0:
11011             case 2:
11012                 return 2;
11013             }
11014         } else if (arm_is_el2_enabled(env)) {
11015             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11016                 return 2;
11017             }
11018         }
11019     }
11020 
11021     /* CPTR_EL3 : present in v8 */
11022     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11023         /* Trap all FP ops to EL3 */
11024         return 3;
11025     }
11026 #endif
11027     return 0;
11028 }
11029 
11030 /* Return the exception level we're running at if this is our mmu_idx */
11031 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11032 {
11033     if (mmu_idx & ARM_MMU_IDX_M) {
11034         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11035     }
11036 
11037     switch (mmu_idx) {
11038     case ARMMMUIdx_E10_0:
11039     case ARMMMUIdx_E20_0:
11040         return 0;
11041     case ARMMMUIdx_E10_1:
11042     case ARMMMUIdx_E10_1_PAN:
11043         return 1;
11044     case ARMMMUIdx_E2:
11045     case ARMMMUIdx_E20_2:
11046     case ARMMMUIdx_E20_2_PAN:
11047         return 2;
11048     case ARMMMUIdx_E3:
11049         return 3;
11050     default:
11051         g_assert_not_reached();
11052     }
11053 }
11054 
11055 #ifndef CONFIG_TCG
11056 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11057 {
11058     g_assert_not_reached();
11059 }
11060 #endif
11061 
11062 static bool arm_pan_enabled(CPUARMState *env)
11063 {
11064     if (is_a64(env)) {
11065         return env->pstate & PSTATE_PAN;
11066     } else {
11067         return env->uncached_cpsr & CPSR_PAN;
11068     }
11069 }
11070 
11071 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11072 {
11073     ARMMMUIdx idx;
11074     uint64_t hcr;
11075 
11076     if (arm_feature(env, ARM_FEATURE_M)) {
11077         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11078     }
11079 
11080     /* See ARM pseudo-function ELIsInHost.  */
11081     switch (el) {
11082     case 0:
11083         hcr = arm_hcr_el2_eff(env);
11084         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11085             idx = ARMMMUIdx_E20_0;
11086         } else {
11087             idx = ARMMMUIdx_E10_0;
11088         }
11089         break;
11090     case 1:
11091         if (arm_pan_enabled(env)) {
11092             idx = ARMMMUIdx_E10_1_PAN;
11093         } else {
11094             idx = ARMMMUIdx_E10_1;
11095         }
11096         break;
11097     case 2:
11098         /* Note that TGE does not apply at EL2.  */
11099         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11100             if (arm_pan_enabled(env)) {
11101                 idx = ARMMMUIdx_E20_2_PAN;
11102             } else {
11103                 idx = ARMMMUIdx_E20_2;
11104             }
11105         } else {
11106             idx = ARMMMUIdx_E2;
11107         }
11108         break;
11109     case 3:
11110         return ARMMMUIdx_E3;
11111     default:
11112         g_assert_not_reached();
11113     }
11114 
11115     return idx;
11116 }
11117 
11118 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11119 {
11120     return arm_mmu_idx_el(env, arm_current_el(env));
11121 }
11122 
11123 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11124                                            ARMMMUIdx mmu_idx,
11125                                            CPUARMTBFlags flags)
11126 {
11127     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11128     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11129 
11130     if (arm_singlestep_active(env)) {
11131         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11132     }
11133     return flags;
11134 }
11135 
11136 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11137                                               ARMMMUIdx mmu_idx,
11138                                               CPUARMTBFlags flags)
11139 {
11140     bool sctlr_b = arm_sctlr_b(env);
11141 
11142     if (sctlr_b) {
11143         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11144     }
11145     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11146         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11147     }
11148     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11149 
11150     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11151 }
11152 
11153 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11154                                         ARMMMUIdx mmu_idx)
11155 {
11156     CPUARMTBFlags flags = {};
11157     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11158 
11159     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11160     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11161         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11162     }
11163 
11164     if (arm_v7m_is_handler_mode(env)) {
11165         DP_TBFLAG_M32(flags, HANDLER, 1);
11166     }
11167 
11168     /*
11169      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11170      * is suppressing them because the requested execution priority
11171      * is less than 0.
11172      */
11173     if (arm_feature(env, ARM_FEATURE_V8) &&
11174         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11175           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11176         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11177     }
11178 
11179     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11180         DP_TBFLAG_M32(flags, SECURE, 1);
11181     }
11182 
11183     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11184 }
11185 
11186 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11187                                         ARMMMUIdx mmu_idx)
11188 {
11189     CPUARMTBFlags flags = {};
11190     int el = arm_current_el(env);
11191 
11192     if (arm_sctlr(env, el) & SCTLR_A) {
11193         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11194     }
11195 
11196     if (arm_el_is_aa64(env, 1)) {
11197         DP_TBFLAG_A32(flags, VFPEN, 1);
11198     }
11199 
11200     if (el < 2 && env->cp15.hstr_el2 &&
11201         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11202         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11203     }
11204 
11205     if (env->uncached_cpsr & CPSR_IL) {
11206         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11207     }
11208 
11209     /*
11210      * The SME exception we are testing for is raised via
11211      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11212      * AArch32.CheckAdvSIMDOrFPEnabled().
11213      */
11214     if (el == 0
11215         && FIELD_EX64(env->svcr, SVCR, SM)
11216         && (!arm_is_el2_enabled(env)
11217             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11218         && arm_el_is_aa64(env, 1)
11219         && !sme_fa64(env, el)) {
11220         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11221     }
11222 
11223     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11224 }
11225 
11226 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11227                                         ARMMMUIdx mmu_idx)
11228 {
11229     CPUARMTBFlags flags = {};
11230     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11231     uint64_t tcr = regime_tcr(env, mmu_idx);
11232     uint64_t sctlr;
11233     int tbii, tbid;
11234 
11235     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11236 
11237     /* Get control bits for tagged addresses.  */
11238     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11239     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11240 
11241     DP_TBFLAG_A64(flags, TBII, tbii);
11242     DP_TBFLAG_A64(flags, TBID, tbid);
11243 
11244     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11245         int sve_el = sve_exception_el(env, el);
11246 
11247         /*
11248          * If either FP or SVE are disabled, translator does not need len.
11249          * If SVE EL > FP EL, FP exception has precedence, and translator
11250          * does not need SVE EL.  Save potential re-translations by forcing
11251          * the unneeded data to zero.
11252          */
11253         if (fp_el != 0) {
11254             if (sve_el > fp_el) {
11255                 sve_el = 0;
11256             }
11257         } else if (sve_el == 0) {
11258             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11259         }
11260         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11261     }
11262     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11263         int sme_el = sme_exception_el(env, el);
11264         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11265 
11266         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11267         if (sme_el == 0) {
11268             /* Similarly, do not compute SVL if SME is disabled. */
11269             int svl = sve_vqm1_for_el_sm(env, el, true);
11270             DP_TBFLAG_A64(flags, SVL, svl);
11271             if (sm) {
11272                 /* If SVE is disabled, we will not have set VL above. */
11273                 DP_TBFLAG_A64(flags, VL, svl);
11274             }
11275         }
11276         if (sm) {
11277             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11278             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11279         }
11280         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11281     }
11282 
11283     sctlr = regime_sctlr(env, stage1);
11284 
11285     if (sctlr & SCTLR_A) {
11286         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11287     }
11288 
11289     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11290         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11291     }
11292 
11293     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11294         /*
11295          * In order to save space in flags, we record only whether
11296          * pauth is "inactive", meaning all insns are implemented as
11297          * a nop, or "active" when some action must be performed.
11298          * The decision of which action to take is left to a helper.
11299          */
11300         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11301             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11302         }
11303     }
11304 
11305     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11306         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11307         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11308             DP_TBFLAG_A64(flags, BT, 1);
11309         }
11310     }
11311 
11312     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11313     if (!(env->pstate & PSTATE_UAO)) {
11314         switch (mmu_idx) {
11315         case ARMMMUIdx_E10_1:
11316         case ARMMMUIdx_E10_1_PAN:
11317             /* TODO: ARMv8.3-NV */
11318             DP_TBFLAG_A64(flags, UNPRIV, 1);
11319             break;
11320         case ARMMMUIdx_E20_2:
11321         case ARMMMUIdx_E20_2_PAN:
11322             /*
11323              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11324              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11325              */
11326             if (env->cp15.hcr_el2 & HCR_TGE) {
11327                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11328             }
11329             break;
11330         default:
11331             break;
11332         }
11333     }
11334 
11335     if (env->pstate & PSTATE_IL) {
11336         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11337     }
11338 
11339     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11340         /*
11341          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11342          * if all accesses must be Unchecked:
11343          * 1) If no TBI, then there are no tags in the address to check,
11344          * 2) If Tag Check Override, then all accesses are Unchecked,
11345          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11346          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11347          */
11348         if (allocation_tag_access_enabled(env, el, sctlr)) {
11349             DP_TBFLAG_A64(flags, ATA, 1);
11350             if (tbid
11351                 && !(env->pstate & PSTATE_TCO)
11352                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11353                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11354             }
11355         }
11356         /* And again for unprivileged accesses, if required.  */
11357         if (EX_TBFLAG_A64(flags, UNPRIV)
11358             && tbid
11359             && !(env->pstate & PSTATE_TCO)
11360             && (sctlr & SCTLR_TCF0)
11361             && allocation_tag_access_enabled(env, 0, sctlr)) {
11362             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11363         }
11364         /* Cache TCMA as well as TBI. */
11365         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11366     }
11367 
11368     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11369 }
11370 
11371 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11372 {
11373     int el = arm_current_el(env);
11374     int fp_el = fp_exception_el(env, el);
11375     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11376 
11377     if (is_a64(env)) {
11378         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11379     } else if (arm_feature(env, ARM_FEATURE_M)) {
11380         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11381     } else {
11382         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11383     }
11384 }
11385 
11386 void arm_rebuild_hflags(CPUARMState *env)
11387 {
11388     env->hflags = rebuild_hflags_internal(env);
11389 }
11390 
11391 /*
11392  * If we have triggered a EL state change we can't rely on the
11393  * translator having passed it to us, we need to recompute.
11394  */
11395 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11396 {
11397     int el = arm_current_el(env);
11398     int fp_el = fp_exception_el(env, el);
11399     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11400 
11401     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11402 }
11403 
11404 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11405 {
11406     int fp_el = fp_exception_el(env, el);
11407     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11408 
11409     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11410 }
11411 
11412 /*
11413  * If we have triggered a EL state change we can't rely on the
11414  * translator having passed it to us, we need to recompute.
11415  */
11416 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11417 {
11418     int el = arm_current_el(env);
11419     int fp_el = fp_exception_el(env, el);
11420     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11421     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11422 }
11423 
11424 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11425 {
11426     int fp_el = fp_exception_el(env, el);
11427     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11428 
11429     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11430 }
11431 
11432 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11433 {
11434     int fp_el = fp_exception_el(env, el);
11435     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11436 
11437     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11438 }
11439 
11440 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11441 {
11442 #ifdef CONFIG_DEBUG_TCG
11443     CPUARMTBFlags c = env->hflags;
11444     CPUARMTBFlags r = rebuild_hflags_internal(env);
11445 
11446     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11447         fprintf(stderr, "TCG hflags mismatch "
11448                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11449                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11450                 c.flags, c.flags2, r.flags, r.flags2);
11451         abort();
11452     }
11453 #endif
11454 }
11455 
11456 static bool mve_no_pred(CPUARMState *env)
11457 {
11458     /*
11459      * Return true if there is definitely no predication of MVE
11460      * instructions by VPR or LTPSIZE. (Returning false even if there
11461      * isn't any predication is OK; generated code will just be
11462      * a little worse.)
11463      * If the CPU does not implement MVE then this TB flag is always 0.
11464      *
11465      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11466      * logic in gen_update_fp_context() needs to be updated to match.
11467      *
11468      * We do not include the effect of the ECI bits here -- they are
11469      * tracked in other TB flags. This simplifies the logic for
11470      * "when did we emit code that changes the MVE_NO_PRED TB flag
11471      * and thus need to end the TB?".
11472      */
11473     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11474         return false;
11475     }
11476     if (env->v7m.vpr) {
11477         return false;
11478     }
11479     if (env->v7m.ltpsize < 4) {
11480         return false;
11481     }
11482     return true;
11483 }
11484 
11485 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11486                           target_ulong *cs_base, uint32_t *pflags)
11487 {
11488     CPUARMTBFlags flags;
11489 
11490     assert_hflags_rebuild_correctly(env);
11491     flags = env->hflags;
11492 
11493     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11494         *pc = env->pc;
11495         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11496             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11497         }
11498     } else {
11499         *pc = env->regs[15];
11500 
11501         if (arm_feature(env, ARM_FEATURE_M)) {
11502             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11503                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11504                 != env->v7m.secure) {
11505                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11506             }
11507 
11508             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11509                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11510                  (env->v7m.secure &&
11511                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11512                 /*
11513                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11514                  * active FP context; we must create a new FP context before
11515                  * executing any FP insn.
11516                  */
11517                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11518             }
11519 
11520             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11521             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11522                 DP_TBFLAG_M32(flags, LSPACT, 1);
11523             }
11524 
11525             if (mve_no_pred(env)) {
11526                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11527             }
11528         } else {
11529             /*
11530              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11531              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11532              */
11533             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11534                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11535             } else {
11536                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11537                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11538             }
11539             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11540                 DP_TBFLAG_A32(flags, VFPEN, 1);
11541             }
11542         }
11543 
11544         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11545         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11546     }
11547 
11548     /*
11549      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11550      * states defined in the ARM ARM for software singlestep:
11551      *  SS_ACTIVE   PSTATE.SS   State
11552      *     0            x       Inactive (the TB flag for SS is always 0)
11553      *     1            0       Active-pending
11554      *     1            1       Active-not-pending
11555      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11556      */
11557     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11558         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11559     }
11560 
11561     *pflags = flags.flags;
11562     *cs_base = flags.flags2;
11563 }
11564 
11565 #ifdef TARGET_AARCH64
11566 /*
11567  * The manual says that when SVE is enabled and VQ is widened the
11568  * implementation is allowed to zero the previously inaccessible
11569  * portion of the registers.  The corollary to that is that when
11570  * SVE is enabled and VQ is narrowed we are also allowed to zero
11571  * the now inaccessible portion of the registers.
11572  *
11573  * The intent of this is that no predicate bit beyond VQ is ever set.
11574  * Which means that some operations on predicate registers themselves
11575  * may operate on full uint64_t or even unrolled across the maximum
11576  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11577  * may well be cheaper than conditionals to restrict the operation
11578  * to the relevant portion of a uint16_t[16].
11579  */
11580 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11581 {
11582     int i, j;
11583     uint64_t pmask;
11584 
11585     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11586     assert(vq <= env_archcpu(env)->sve_max_vq);
11587 
11588     /* Zap the high bits of the zregs.  */
11589     for (i = 0; i < 32; i++) {
11590         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11591     }
11592 
11593     /* Zap the high bits of the pregs and ffr.  */
11594     pmask = 0;
11595     if (vq & 3) {
11596         pmask = ~(-1ULL << (16 * (vq & 3)));
11597     }
11598     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11599         for (i = 0; i < 17; ++i) {
11600             env->vfp.pregs[i].p[j] &= pmask;
11601         }
11602         pmask = 0;
11603     }
11604 }
11605 
11606 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11607 {
11608     int exc_el;
11609 
11610     if (sm) {
11611         exc_el = sme_exception_el(env, el);
11612     } else {
11613         exc_el = sve_exception_el(env, el);
11614     }
11615     if (exc_el) {
11616         return 0; /* disabled */
11617     }
11618     return sve_vqm1_for_el_sm(env, el, sm);
11619 }
11620 
11621 /*
11622  * Notice a change in SVE vector size when changing EL.
11623  */
11624 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11625                            int new_el, bool el0_a64)
11626 {
11627     ARMCPU *cpu = env_archcpu(env);
11628     int old_len, new_len;
11629     bool old_a64, new_a64, sm;
11630 
11631     /* Nothing to do if no SVE.  */
11632     if (!cpu_isar_feature(aa64_sve, cpu)) {
11633         return;
11634     }
11635 
11636     /* Nothing to do if FP is disabled in either EL.  */
11637     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11638         return;
11639     }
11640 
11641     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11642     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11643 
11644     /*
11645      * Both AArch64.TakeException and AArch64.ExceptionReturn
11646      * invoke ResetSVEState when taking an exception from, or
11647      * returning to, AArch32 state when PSTATE.SM is enabled.
11648      */
11649     sm = FIELD_EX64(env->svcr, SVCR, SM);
11650     if (old_a64 != new_a64 && sm) {
11651         arm_reset_sve_state(env);
11652         return;
11653     }
11654 
11655     /*
11656      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11657      * at ELx, or not available because the EL is in AArch32 state, then
11658      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11659      * has an effective value of 0".
11660      *
11661      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11662      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11663      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11664      * we already have the correct register contents when encountering the
11665      * vq0->vq0 transition between EL0->EL1.
11666      */
11667     old_len = new_len = 0;
11668     if (old_a64) {
11669         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11670     }
11671     if (new_a64) {
11672         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11673     }
11674 
11675     /* When changing vector length, clear inaccessible state.  */
11676     if (new_len < old_len) {
11677         aarch64_sve_narrow_vq(env, new_len + 1);
11678     }
11679 }
11680 #endif
11681