xref: /openbmc/qemu/target/arm/helper.c (revision c35da11d)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* For crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 
34 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35 
36 static void switch_mode(CPUARMState *env, int mode);
37 
38 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
39 {
40     assert(ri->fieldoffset);
41     if (cpreg_field_is_64bit(ri)) {
42         return CPREG_FIELD64(env, ri);
43     } else {
44         return CPREG_FIELD32(env, ri);
45     }
46 }
47 
48 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
49 {
50     assert(ri->fieldoffset);
51     if (cpreg_field_is_64bit(ri)) {
52         CPREG_FIELD64(env, ri) = value;
53     } else {
54         CPREG_FIELD32(env, ri) = value;
55     }
56 }
57 
58 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59 {
60     return (char *)env + ri->fieldoffset;
61 }
62 
63 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
64 {
65     /* Raw read of a coprocessor register (as needed for migration, etc). */
66     if (ri->type & ARM_CP_CONST) {
67         return ri->resetvalue;
68     } else if (ri->raw_readfn) {
69         return ri->raw_readfn(env, ri);
70     } else if (ri->readfn) {
71         return ri->readfn(env, ri);
72     } else {
73         return raw_read(env, ri);
74     }
75 }
76 
77 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
78                              uint64_t v)
79 {
80     /*
81      * Raw write of a coprocessor register (as needed for migration, etc).
82      * Note that constant registers are treated as write-ignored; the
83      * caller should check for success by whether a readback gives the
84      * value written.
85      */
86     if (ri->type & ARM_CP_CONST) {
87         return;
88     } else if (ri->raw_writefn) {
89         ri->raw_writefn(env, ri, v);
90     } else if (ri->writefn) {
91         ri->writefn(env, ri, v);
92     } else {
93         raw_write(env, ri, v);
94     }
95 }
96 
97 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98 {
99    /*
100     * Return true if the regdef would cause an assertion if you called
101     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102     * program bug for it not to have the NO_RAW flag).
103     * NB that returning false here doesn't necessarily mean that calling
104     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105     * read/write access functions which are safe for raw use" from "has
106     * read/write access functions which have side effects but has forgotten
107     * to provide raw access functions".
108     * The tests here line up with the conditions in read/write_raw_cp_reg()
109     * and assertions in raw_read()/raw_write().
110     */
111     if ((ri->type & ARM_CP_CONST) ||
112         ri->fieldoffset ||
113         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114         return false;
115     }
116     return true;
117 }
118 
119 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
120 {
121     /* Write the coprocessor state from cpu->env to the (index,value) list. */
122     int i;
123     bool ok = true;
124 
125     for (i = 0; i < cpu->cpreg_array_len; i++) {
126         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127         const ARMCPRegInfo *ri;
128         uint64_t newval;
129 
130         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
131         if (!ri) {
132             ok = false;
133             continue;
134         }
135         if (ri->type & ARM_CP_NO_RAW) {
136             continue;
137         }
138 
139         newval = read_raw_cp_reg(&cpu->env, ri);
140         if (kvm_sync) {
141             /*
142              * Only sync if the previous list->cpustate sync succeeded.
143              * Rather than tracking the success/failure state for every
144              * item in the list, we just recheck "does the raw write we must
145              * have made in write_list_to_cpustate() read back OK" here.
146              */
147             uint64_t oldval = cpu->cpreg_values[i];
148 
149             if (oldval == newval) {
150                 continue;
151             }
152 
153             write_raw_cp_reg(&cpu->env, ri, oldval);
154             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155                 continue;
156             }
157 
158             write_raw_cp_reg(&cpu->env, ri, newval);
159         }
160         cpu->cpreg_values[i] = newval;
161     }
162     return ok;
163 }
164 
165 bool write_list_to_cpustate(ARMCPU *cpu)
166 {
167     int i;
168     bool ok = true;
169 
170     for (i = 0; i < cpu->cpreg_array_len; i++) {
171         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172         uint64_t v = cpu->cpreg_values[i];
173         const ARMCPRegInfo *ri;
174 
175         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
176         if (!ri) {
177             ok = false;
178             continue;
179         }
180         if (ri->type & ARM_CP_NO_RAW) {
181             continue;
182         }
183         /*
184          * Write value and confirm it reads back as written
185          * (to catch read-only registers and partially read-only
186          * registers where the incoming migration value doesn't match)
187          */
188         write_raw_cp_reg(&cpu->env, ri, v);
189         if (read_raw_cp_reg(&cpu->env, ri) != v) {
190             ok = false;
191         }
192     }
193     return ok;
194 }
195 
196 static void add_cpreg_to_list(gpointer key, gpointer opaque)
197 {
198     ARMCPU *cpu = opaque;
199     uint32_t regidx = (uintptr_t)key;
200     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
201 
202     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
203         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204         /* The value array need not be initialized at this point */
205         cpu->cpreg_array_len++;
206     }
207 }
208 
209 static void count_cpreg(gpointer key, gpointer opaque)
210 {
211     ARMCPU *cpu = opaque;
212     const ARMCPRegInfo *ri;
213 
214     ri = g_hash_table_lookup(cpu->cp_regs, key);
215 
216     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
217         cpu->cpreg_array_len++;
218     }
219 }
220 
221 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222 {
223     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
225 
226     if (aidx > bidx) {
227         return 1;
228     }
229     if (aidx < bidx) {
230         return -1;
231     }
232     return 0;
233 }
234 
235 void init_cpreg_list(ARMCPU *cpu)
236 {
237     /*
238      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
239      * Note that we require cpreg_tuples[] to be sorted by key ID.
240      */
241     GList *keys;
242     int arraylen;
243 
244     keys = g_hash_table_get_keys(cpu->cp_regs);
245     keys = g_list_sort(keys, cpreg_key_compare);
246 
247     cpu->cpreg_array_len = 0;
248 
249     g_list_foreach(keys, count_cpreg, cpu);
250 
251     arraylen = cpu->cpreg_array_len;
252     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253     cpu->cpreg_values = g_new(uint64_t, arraylen);
254     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257     cpu->cpreg_array_len = 0;
258 
259     g_list_foreach(keys, add_cpreg_to_list, cpu);
260 
261     assert(cpu->cpreg_array_len == arraylen);
262 
263     g_list_free(keys);
264 }
265 
266 static bool arm_pan_enabled(CPUARMState *env)
267 {
268     if (is_a64(env)) {
269         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
270             return false;
271         }
272         return env->pstate & PSTATE_PAN;
273     } else {
274         return env->uncached_cpsr & CPSR_PAN;
275     }
276 }
277 
278 /*
279  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
280  */
281 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
282                                         const ARMCPRegInfo *ri,
283                                         bool isread)
284 {
285     if (!is_a64(env) && arm_current_el(env) == 3 &&
286         arm_is_secure_below_el3(env)) {
287         return CP_ACCESS_TRAP_UNCATEGORIZED;
288     }
289     return CP_ACCESS_OK;
290 }
291 
292 /*
293  * Some secure-only AArch32 registers trap to EL3 if used from
294  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
295  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
296  * We assume that the .access field is set to PL1_RW.
297  */
298 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
299                                             const ARMCPRegInfo *ri,
300                                             bool isread)
301 {
302     if (arm_current_el(env) == 3) {
303         return CP_ACCESS_OK;
304     }
305     if (arm_is_secure_below_el3(env)) {
306         if (env->cp15.scr_el3 & SCR_EEL2) {
307             return CP_ACCESS_TRAP_EL2;
308         }
309         return CP_ACCESS_TRAP_EL3;
310     }
311     /* This will be EL1 NS and EL2 NS, which just UNDEF */
312     return CP_ACCESS_TRAP_UNCATEGORIZED;
313 }
314 
315 /*
316  * Check for traps to performance monitor registers, which are controlled
317  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
318  */
319 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
320                                  bool isread)
321 {
322     int el = arm_current_el(env);
323     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
324 
325     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
326         return CP_ACCESS_TRAP_EL2;
327     }
328     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
329         return CP_ACCESS_TRAP_EL3;
330     }
331     return CP_ACCESS_OK;
332 }
333 
334 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
335 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
336                                bool isread)
337 {
338     if (arm_current_el(env) == 1) {
339         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
340         if (arm_hcr_el2_eff(env) & trap) {
341             return CP_ACCESS_TRAP_EL2;
342         }
343     }
344     return CP_ACCESS_OK;
345 }
346 
347 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
348 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
349                                  bool isread)
350 {
351     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
352         return CP_ACCESS_TRAP_EL2;
353     }
354     return CP_ACCESS_OK;
355 }
356 
357 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
358 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
359                                   bool isread)
360 {
361     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
362         return CP_ACCESS_TRAP_EL2;
363     }
364     return CP_ACCESS_OK;
365 }
366 
367 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
368 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
369                                   bool isread)
370 {
371     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
372         return CP_ACCESS_TRAP_EL2;
373     }
374     return CP_ACCESS_OK;
375 }
376 
377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
378 static CPAccessResult access_ttlbis(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_TTLBIS))) {
383         return CP_ACCESS_TRAP_EL2;
384     }
385     return CP_ACCESS_OK;
386 }
387 
388 #ifdef TARGET_AARCH64
389 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
390 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
391                                     bool isread)
392 {
393     if (arm_current_el(env) == 1 &&
394         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
395         return CP_ACCESS_TRAP_EL2;
396     }
397     return CP_ACCESS_OK;
398 }
399 #endif
400 
401 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
402 {
403     ARMCPU *cpu = env_archcpu(env);
404 
405     raw_write(env, ri, value);
406     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
407 }
408 
409 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
410 {
411     ARMCPU *cpu = env_archcpu(env);
412 
413     if (raw_read(env, ri) != value) {
414         /*
415          * Unlike real hardware the qemu TLB uses virtual addresses,
416          * not modified virtual addresses, so this causes a TLB flush.
417          */
418         tlb_flush(CPU(cpu));
419         raw_write(env, ri, value);
420     }
421 }
422 
423 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
424                              uint64_t value)
425 {
426     ARMCPU *cpu = env_archcpu(env);
427 
428     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
429         && !extended_addresses_enabled(env)) {
430         /*
431          * For VMSA (when not using the LPAE long descriptor page table
432          * format) this register includes the ASID, so do a TLB flush.
433          * For PMSA it is purely a process ID and no action is needed.
434          */
435         tlb_flush(CPU(cpu));
436     }
437     raw_write(env, ri, value);
438 }
439 
440 static int alle1_tlbmask(CPUARMState *env)
441 {
442     /*
443      * Note that the 'ALL' scope must invalidate both stage 1 and
444      * stage 2 translations, whereas most other scopes only invalidate
445      * stage 1 translations.
446      */
447     return (ARMMMUIdxBit_E10_1 |
448             ARMMMUIdxBit_E10_1_PAN |
449             ARMMMUIdxBit_E10_0 |
450             ARMMMUIdxBit_Stage2 |
451             ARMMMUIdxBit_Stage2_S);
452 }
453 
454 
455 /* IS variants of TLB operations must affect all cores */
456 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
457                              uint64_t value)
458 {
459     CPUState *cs = env_cpu(env);
460 
461     tlb_flush_all_cpus_synced(cs);
462 }
463 
464 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
465                              uint64_t value)
466 {
467     CPUState *cs = env_cpu(env);
468 
469     tlb_flush_all_cpus_synced(cs);
470 }
471 
472 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
473                              uint64_t value)
474 {
475     CPUState *cs = env_cpu(env);
476 
477     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
478 }
479 
480 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
481                              uint64_t value)
482 {
483     CPUState *cs = env_cpu(env);
484 
485     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
486 }
487 
488 /*
489  * Non-IS variants of TLB operations are upgraded to
490  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
491  * force broadcast of these operations.
492  */
493 static bool tlb_force_broadcast(CPUARMState *env)
494 {
495     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
496 }
497 
498 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
499                           uint64_t value)
500 {
501     /* Invalidate all (TLBIALL) */
502     CPUState *cs = env_cpu(env);
503 
504     if (tlb_force_broadcast(env)) {
505         tlb_flush_all_cpus_synced(cs);
506     } else {
507         tlb_flush(cs);
508     }
509 }
510 
511 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
512                           uint64_t value)
513 {
514     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
515     CPUState *cs = env_cpu(env);
516 
517     value &= TARGET_PAGE_MASK;
518     if (tlb_force_broadcast(env)) {
519         tlb_flush_page_all_cpus_synced(cs, value);
520     } else {
521         tlb_flush_page(cs, value);
522     }
523 }
524 
525 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
526                            uint64_t value)
527 {
528     /* Invalidate by ASID (TLBIASID) */
529     CPUState *cs = env_cpu(env);
530 
531     if (tlb_force_broadcast(env)) {
532         tlb_flush_all_cpus_synced(cs);
533     } else {
534         tlb_flush(cs);
535     }
536 }
537 
538 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                            uint64_t value)
540 {
541     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
542     CPUState *cs = env_cpu(env);
543 
544     value &= TARGET_PAGE_MASK;
545     if (tlb_force_broadcast(env)) {
546         tlb_flush_page_all_cpus_synced(cs, value);
547     } else {
548         tlb_flush_page(cs, value);
549     }
550 }
551 
552 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
553                                uint64_t value)
554 {
555     CPUState *cs = env_cpu(env);
556 
557     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
558 }
559 
560 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
561                                   uint64_t value)
562 {
563     CPUState *cs = env_cpu(env);
564 
565     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
566 }
567 
568 
569 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
570                               uint64_t value)
571 {
572     CPUState *cs = env_cpu(env);
573 
574     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
575 }
576 
577 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
578                                  uint64_t value)
579 {
580     CPUState *cs = env_cpu(env);
581 
582     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
583 }
584 
585 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
586                               uint64_t value)
587 {
588     CPUState *cs = env_cpu(env);
589     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
590 
591     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
592 }
593 
594 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
595                                  uint64_t value)
596 {
597     CPUState *cs = env_cpu(env);
598     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
599 
600     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
601                                              ARMMMUIdxBit_E2);
602 }
603 
604 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
605                                 uint64_t value)
606 {
607     CPUState *cs = env_cpu(env);
608     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
609 
610     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
611 }
612 
613 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
614                                 uint64_t value)
615 {
616     CPUState *cs = env_cpu(env);
617     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
618 
619     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
620 }
621 
622 static const ARMCPRegInfo cp_reginfo[] = {
623     /*
624      * Define the secure and non-secure FCSE 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. There is also no
627      * v8 EL1 version of the register so the non-secure instance stands alone.
628      */
629     { .name = "FCSEIDR",
630       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
631       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
632       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
633       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
634     { .name = "FCSEIDR_S",
635       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
636       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
637       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
638       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
639     /*
640      * Define the secure and non-secure context identifier CP registers
641      * separately because there is no secure bank in V8 (no _EL3).  This allows
642      * the secure register to be properly reset and migrated.  In the
643      * non-secure case, the 32-bit register will have reset and migration
644      * disabled during registration as it is handled by the 64-bit instance.
645      */
646     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
647       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
648       .access = PL1_RW, .accessfn = access_tvm_trvm,
649       .fgt = FGT_CONTEXTIDR_EL1,
650       .secure = ARM_CP_SECSTATE_NS,
651       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
652       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
653     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
654       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
655       .access = PL1_RW, .accessfn = access_tvm_trvm,
656       .secure = ARM_CP_SECSTATE_S,
657       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
658       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
659 };
660 
661 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
662     /*
663      * NB: Some of these registers exist in v8 but with more precise
664      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
665      */
666     /* MMU Domain access control / MPU write buffer control */
667     { .name = "DACR",
668       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
669       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
670       .writefn = dacr_write, .raw_writefn = raw_write,
671       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
672                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
673     /*
674      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
675      * For v6 and v5, these mappings are overly broad.
676      */
677     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
678       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
679     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
680       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
682       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
683     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
684       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
685     /* Cache maintenance ops; some of this space may be overridden later. */
686     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
687       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
688       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
689 };
690 
691 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
692     /*
693      * Not all pre-v6 cores implemented this WFI, so this is slightly
694      * over-broad.
695      */
696     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
697       .access = PL1_W, .type = ARM_CP_WFI },
698 };
699 
700 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
701     /*
702      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
703      * is UNPREDICTABLE; we choose to NOP as most implementations do).
704      */
705     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
706       .access = PL1_W, .type = ARM_CP_WFI },
707     /*
708      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
709      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
710      * OMAPCP will override this space.
711      */
712     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
713       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
714       .resetvalue = 0 },
715     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
716       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
717       .resetvalue = 0 },
718     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
719     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
720       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
721       .resetvalue = 0 },
722     /*
723      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
724      * implementing it as RAZ means the "debug architecture version" bits
725      * will read as a reserved value, which should cause Linux to not try
726      * to use the debug hardware.
727      */
728     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
729       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
730     /*
731      * MMU TLB control. Note that the wildcarding means we cover not just
732      * the unified TLB ops but also the dside/iside/inner-shareable variants.
733      */
734     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
735       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
736       .type = ARM_CP_NO_RAW },
737     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
738       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
739       .type = ARM_CP_NO_RAW },
740     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
741       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
742       .type = ARM_CP_NO_RAW },
743     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
744       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
745       .type = ARM_CP_NO_RAW },
746     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
747       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
748     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
749       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
750 };
751 
752 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
753                         uint64_t value)
754 {
755     uint32_t mask = 0;
756 
757     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
758     if (!arm_feature(env, ARM_FEATURE_V8)) {
759         /*
760          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
761          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
762          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
763          */
764         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
765             /* VFP coprocessor: cp10 & cp11 [23:20] */
766             mask |= R_CPACR_ASEDIS_MASK |
767                     R_CPACR_D32DIS_MASK |
768                     R_CPACR_CP11_MASK |
769                     R_CPACR_CP10_MASK;
770 
771             if (!arm_feature(env, ARM_FEATURE_NEON)) {
772                 /* ASEDIS [31] bit is RAO/WI */
773                 value |= R_CPACR_ASEDIS_MASK;
774             }
775 
776             /*
777              * VFPv3 and upwards with NEON implement 32 double precision
778              * registers (D0-D31).
779              */
780             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
781                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
782                 value |= R_CPACR_D32DIS_MASK;
783             }
784         }
785         value &= mask;
786     }
787 
788     /*
789      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
790      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
791      */
792     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
793         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
794         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
795         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
796     }
797 
798     env->cp15.cpacr_el1 = value;
799 }
800 
801 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
802 {
803     /*
804      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
805      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
806      */
807     uint64_t value = env->cp15.cpacr_el1;
808 
809     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
810         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
811         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
812     }
813     return value;
814 }
815 
816 
817 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
818 {
819     /*
820      * Call cpacr_write() so that we reset with the correct RAO bits set
821      * for our CPU features.
822      */
823     cpacr_write(env, ri, 0);
824 }
825 
826 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
827                                    bool isread)
828 {
829     if (arm_feature(env, ARM_FEATURE_V8)) {
830         /* Check if CPACR accesses are to be trapped to EL2 */
831         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
832             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
833             return CP_ACCESS_TRAP_EL2;
834         /* Check if CPACR accesses are to be trapped to EL3 */
835         } else if (arm_current_el(env) < 3 &&
836                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
837             return CP_ACCESS_TRAP_EL3;
838         }
839     }
840 
841     return CP_ACCESS_OK;
842 }
843 
844 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
845                                   bool isread)
846 {
847     /* Check if CPTR accesses are set to trap to EL3 */
848     if (arm_current_el(env) == 2 &&
849         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
850         return CP_ACCESS_TRAP_EL3;
851     }
852 
853     return CP_ACCESS_OK;
854 }
855 
856 static const ARMCPRegInfo v6_cp_reginfo[] = {
857     /* prefetch by MVA in v6, NOP in v7 */
858     { .name = "MVA_prefetch",
859       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
860       .access = PL1_W, .type = ARM_CP_NOP },
861     /*
862      * We need to break the TB after ISB to execute self-modifying code
863      * correctly and also to take any pending interrupts immediately.
864      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
865      */
866     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
867       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
868     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
869       .access = PL0_W, .type = ARM_CP_NOP },
870     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
871       .access = PL0_W, .type = ARM_CP_NOP },
872     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
873       .access = PL1_RW, .accessfn = access_tvm_trvm,
874       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
875                              offsetof(CPUARMState, cp15.ifar_ns) },
876       .resetvalue = 0, },
877     /*
878      * Watchpoint Fault Address Register : should actually only be present
879      * for 1136, 1176, 11MPCore.
880      */
881     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
882       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
883     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
884       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
885       .fgt = FGT_CPACR_EL1,
886       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
887       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
888 };
889 
890 typedef struct pm_event {
891     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
892     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
893     bool (*supported)(CPUARMState *);
894     /*
895      * Retrieve the current count of the underlying event. The programmed
896      * counters hold a difference from the return value from this function
897      */
898     uint64_t (*get_count)(CPUARMState *);
899     /*
900      * Return how many nanoseconds it will take (at a minimum) for count events
901      * to occur. A negative value indicates the counter will never overflow, or
902      * that the counter has otherwise arranged for the overflow bit to be set
903      * and the PMU interrupt to be raised on overflow.
904      */
905     int64_t (*ns_per_count)(uint64_t);
906 } pm_event;
907 
908 static bool event_always_supported(CPUARMState *env)
909 {
910     return true;
911 }
912 
913 static uint64_t swinc_get_count(CPUARMState *env)
914 {
915     /*
916      * SW_INCR events are written directly to the pmevcntr's by writes to
917      * PMSWINC, so there is no underlying count maintained by the PMU itself
918      */
919     return 0;
920 }
921 
922 static int64_t swinc_ns_per(uint64_t ignored)
923 {
924     return -1;
925 }
926 
927 /*
928  * Return the underlying cycle count for the PMU cycle counters. If we're in
929  * usermode, simply return 0.
930  */
931 static uint64_t cycles_get_count(CPUARMState *env)
932 {
933 #ifndef CONFIG_USER_ONLY
934     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
935                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
936 #else
937     return cpu_get_host_ticks();
938 #endif
939 }
940 
941 #ifndef CONFIG_USER_ONLY
942 static int64_t cycles_ns_per(uint64_t cycles)
943 {
944     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
945 }
946 
947 static bool instructions_supported(CPUARMState *env)
948 {
949     return icount_enabled() == 1; /* Precise instruction counting */
950 }
951 
952 static uint64_t instructions_get_count(CPUARMState *env)
953 {
954     return (uint64_t)icount_get_raw();
955 }
956 
957 static int64_t instructions_ns_per(uint64_t icount)
958 {
959     return icount_to_ns((int64_t)icount);
960 }
961 #endif
962 
963 static bool pmuv3p1_events_supported(CPUARMState *env)
964 {
965     /* For events which are supported in any v8.1 PMU */
966     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
967 }
968 
969 static bool pmuv3p4_events_supported(CPUARMState *env)
970 {
971     /* For events which are supported in any v8.1 PMU */
972     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
973 }
974 
975 static uint64_t zero_event_get_count(CPUARMState *env)
976 {
977     /* For events which on QEMU never fire, so their count is always zero */
978     return 0;
979 }
980 
981 static int64_t zero_event_ns_per(uint64_t cycles)
982 {
983     /* An event which never fires can never overflow */
984     return -1;
985 }
986 
987 static const pm_event pm_events[] = {
988     { .number = 0x000, /* SW_INCR */
989       .supported = event_always_supported,
990       .get_count = swinc_get_count,
991       .ns_per_count = swinc_ns_per,
992     },
993 #ifndef CONFIG_USER_ONLY
994     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
995       .supported = instructions_supported,
996       .get_count = instructions_get_count,
997       .ns_per_count = instructions_ns_per,
998     },
999     { .number = 0x011, /* CPU_CYCLES, Cycle */
1000       .supported = event_always_supported,
1001       .get_count = cycles_get_count,
1002       .ns_per_count = cycles_ns_per,
1003     },
1004 #endif
1005     { .number = 0x023, /* STALL_FRONTEND */
1006       .supported = pmuv3p1_events_supported,
1007       .get_count = zero_event_get_count,
1008       .ns_per_count = zero_event_ns_per,
1009     },
1010     { .number = 0x024, /* STALL_BACKEND */
1011       .supported = pmuv3p1_events_supported,
1012       .get_count = zero_event_get_count,
1013       .ns_per_count = zero_event_ns_per,
1014     },
1015     { .number = 0x03c, /* STALL */
1016       .supported = pmuv3p4_events_supported,
1017       .get_count = zero_event_get_count,
1018       .ns_per_count = zero_event_ns_per,
1019     },
1020 };
1021 
1022 /*
1023  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1024  * events (i.e. the statistical profiling extension), this implementation
1025  * should first be updated to something sparse instead of the current
1026  * supported_event_map[] array.
1027  */
1028 #define MAX_EVENT_ID 0x3c
1029 #define UNSUPPORTED_EVENT UINT16_MAX
1030 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1031 
1032 /*
1033  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1034  * of ARM event numbers to indices in our pm_events array.
1035  *
1036  * Note: Events in the 0x40XX range are not currently supported.
1037  */
1038 void pmu_init(ARMCPU *cpu)
1039 {
1040     unsigned int i;
1041 
1042     /*
1043      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1044      * events to them
1045      */
1046     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1047         supported_event_map[i] = UNSUPPORTED_EVENT;
1048     }
1049     cpu->pmceid0 = 0;
1050     cpu->pmceid1 = 0;
1051 
1052     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1053         const pm_event *cnt = &pm_events[i];
1054         assert(cnt->number <= MAX_EVENT_ID);
1055         /* We do not currently support events in the 0x40xx range */
1056         assert(cnt->number <= 0x3f);
1057 
1058         if (cnt->supported(&cpu->env)) {
1059             supported_event_map[cnt->number] = i;
1060             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1061             if (cnt->number & 0x20) {
1062                 cpu->pmceid1 |= event_mask;
1063             } else {
1064                 cpu->pmceid0 |= event_mask;
1065             }
1066         }
1067     }
1068 }
1069 
1070 /*
1071  * Check at runtime whether a PMU event is supported for the current machine
1072  */
1073 static bool event_supported(uint16_t number)
1074 {
1075     if (number > MAX_EVENT_ID) {
1076         return false;
1077     }
1078     return supported_event_map[number] != UNSUPPORTED_EVENT;
1079 }
1080 
1081 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1082                                    bool isread)
1083 {
1084     /*
1085      * Performance monitor registers user accessibility is controlled
1086      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1087      * trapping to EL2 or EL3 for other accesses.
1088      */
1089     int el = arm_current_el(env);
1090     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1091 
1092     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1093         return CP_ACCESS_TRAP;
1094     }
1095     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1096         return CP_ACCESS_TRAP_EL2;
1097     }
1098     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1099         return CP_ACCESS_TRAP_EL3;
1100     }
1101 
1102     return CP_ACCESS_OK;
1103 }
1104 
1105 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1106                                            const ARMCPRegInfo *ri,
1107                                            bool isread)
1108 {
1109     /* ER: event counter read trap control */
1110     if (arm_feature(env, ARM_FEATURE_V8)
1111         && arm_current_el(env) == 0
1112         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1113         && isread) {
1114         return CP_ACCESS_OK;
1115     }
1116 
1117     return pmreg_access(env, ri, isread);
1118 }
1119 
1120 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1121                                          const ARMCPRegInfo *ri,
1122                                          bool isread)
1123 {
1124     /* SW: software increment write trap control */
1125     if (arm_feature(env, ARM_FEATURE_V8)
1126         && arm_current_el(env) == 0
1127         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1128         && !isread) {
1129         return CP_ACCESS_OK;
1130     }
1131 
1132     return pmreg_access(env, ri, isread);
1133 }
1134 
1135 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1136                                         const ARMCPRegInfo *ri,
1137                                         bool isread)
1138 {
1139     /* ER: event counter read trap control */
1140     if (arm_feature(env, ARM_FEATURE_V8)
1141         && arm_current_el(env) == 0
1142         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1143         return CP_ACCESS_OK;
1144     }
1145 
1146     return pmreg_access(env, ri, isread);
1147 }
1148 
1149 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1150                                          const ARMCPRegInfo *ri,
1151                                          bool isread)
1152 {
1153     /* CR: cycle counter read trap control */
1154     if (arm_feature(env, ARM_FEATURE_V8)
1155         && arm_current_el(env) == 0
1156         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1157         && isread) {
1158         return CP_ACCESS_OK;
1159     }
1160 
1161     return pmreg_access(env, ri, isread);
1162 }
1163 
1164 /*
1165  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1166  * We use these to decide whether we need to wrap a write to MDCR_EL2
1167  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1168  */
1169 #define MDCR_EL2_PMU_ENABLE_BITS \
1170     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1171 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1172 
1173 /*
1174  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1175  * the current EL, security state, and register configuration.
1176  */
1177 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1178 {
1179     uint64_t filter;
1180     bool e, p, u, nsk, nsu, nsh, m;
1181     bool enabled, prohibited = false, filtered;
1182     bool secure = arm_is_secure(env);
1183     int el = arm_current_el(env);
1184     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1185     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1186 
1187     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1188         return false;
1189     }
1190 
1191     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1192             (counter < hpmn || counter == 31)) {
1193         e = env->cp15.c9_pmcr & PMCRE;
1194     } else {
1195         e = mdcr_el2 & MDCR_HPME;
1196     }
1197     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1198 
1199     /* Is event counting prohibited? */
1200     if (el == 2 && (counter < hpmn || counter == 31)) {
1201         prohibited = mdcr_el2 & MDCR_HPMD;
1202     }
1203     if (secure) {
1204         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1205     }
1206 
1207     if (counter == 31) {
1208         /*
1209          * The cycle counter defaults to running. PMCR.DP says "disable
1210          * the cycle counter when event counting is prohibited".
1211          * Some MDCR bits disable the cycle counter specifically.
1212          */
1213         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1214         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1215             if (secure) {
1216                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1217             }
1218             if (el == 2) {
1219                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1220             }
1221         }
1222     }
1223 
1224     if (counter == 31) {
1225         filter = env->cp15.pmccfiltr_el0;
1226     } else {
1227         filter = env->cp15.c14_pmevtyper[counter];
1228     }
1229 
1230     p   = filter & PMXEVTYPER_P;
1231     u   = filter & PMXEVTYPER_U;
1232     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1233     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1234     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1235     m   = arm_el_is_aa64(env, 1) &&
1236               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1237 
1238     if (el == 0) {
1239         filtered = secure ? u : u != nsu;
1240     } else if (el == 1) {
1241         filtered = secure ? p : p != nsk;
1242     } else if (el == 2) {
1243         filtered = !nsh;
1244     } else { /* EL3 */
1245         filtered = m != p;
1246     }
1247 
1248     if (counter != 31) {
1249         /*
1250          * If not checking PMCCNTR, ensure the counter is setup to an event we
1251          * support
1252          */
1253         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1254         if (!event_supported(event)) {
1255             return false;
1256         }
1257     }
1258 
1259     return enabled && !prohibited && !filtered;
1260 }
1261 
1262 static void pmu_update_irq(CPUARMState *env)
1263 {
1264     ARMCPU *cpu = env_archcpu(env);
1265     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1266             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1267 }
1268 
1269 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1270 {
1271     /*
1272      * Return true if the clock divider is enabled and the cycle counter
1273      * is supposed to tick only once every 64 clock cycles. This is
1274      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1275      * (64-bit) cycle counter PMCR.D has no effect.
1276      */
1277     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1278 }
1279 
1280 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1281 {
1282     /* Return true if the specified event counter is configured to be 64 bit */
1283 
1284     /* This isn't intended to be used with the cycle counter */
1285     assert(counter < 31);
1286 
1287     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1288         return false;
1289     }
1290 
1291     if (arm_feature(env, ARM_FEATURE_EL2)) {
1292         /*
1293          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1294          * current security state, so we don't use arm_mdcr_el2_eff() here.
1295          */
1296         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1297         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1298 
1299         if (counter >= hpmn) {
1300             return hlp;
1301         }
1302     }
1303     return env->cp15.c9_pmcr & PMCRLP;
1304 }
1305 
1306 /*
1307  * Ensure c15_ccnt is the guest-visible count so that operations such as
1308  * enabling/disabling the counter or filtering, modifying the count itself,
1309  * etc. can be done logically. This is essentially a no-op if the counter is
1310  * not enabled at the time of the call.
1311  */
1312 static void pmccntr_op_start(CPUARMState *env)
1313 {
1314     uint64_t cycles = cycles_get_count(env);
1315 
1316     if (pmu_counter_enabled(env, 31)) {
1317         uint64_t eff_cycles = cycles;
1318         if (pmccntr_clockdiv_enabled(env)) {
1319             eff_cycles /= 64;
1320         }
1321 
1322         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1323 
1324         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1325                                  1ull << 63 : 1ull << 31;
1326         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1327             env->cp15.c9_pmovsr |= (1ULL << 31);
1328             pmu_update_irq(env);
1329         }
1330 
1331         env->cp15.c15_ccnt = new_pmccntr;
1332     }
1333     env->cp15.c15_ccnt_delta = cycles;
1334 }
1335 
1336 /*
1337  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1338  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1339  * pmccntr_op_start.
1340  */
1341 static void pmccntr_op_finish(CPUARMState *env)
1342 {
1343     if (pmu_counter_enabled(env, 31)) {
1344 #ifndef CONFIG_USER_ONLY
1345         /* Calculate when the counter will next overflow */
1346         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1347         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1348             remaining_cycles = (uint32_t)remaining_cycles;
1349         }
1350         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1351 
1352         if (overflow_in > 0) {
1353             int64_t overflow_at;
1354 
1355             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1356                                  overflow_in, &overflow_at)) {
1357                 ARMCPU *cpu = env_archcpu(env);
1358                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1359             }
1360         }
1361 #endif
1362 
1363         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1364         if (pmccntr_clockdiv_enabled(env)) {
1365             prev_cycles /= 64;
1366         }
1367         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1368     }
1369 }
1370 
1371 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1372 {
1373 
1374     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1375     uint64_t count = 0;
1376     if (event_supported(event)) {
1377         uint16_t event_idx = supported_event_map[event];
1378         count = pm_events[event_idx].get_count(env);
1379     }
1380 
1381     if (pmu_counter_enabled(env, counter)) {
1382         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1383         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1384             1ULL << 63 : 1ULL << 31;
1385 
1386         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1387             env->cp15.c9_pmovsr |= (1 << counter);
1388             pmu_update_irq(env);
1389         }
1390         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1391     }
1392     env->cp15.c14_pmevcntr_delta[counter] = count;
1393 }
1394 
1395 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1396 {
1397     if (pmu_counter_enabled(env, counter)) {
1398 #ifndef CONFIG_USER_ONLY
1399         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1400         uint16_t event_idx = supported_event_map[event];
1401         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1402         int64_t overflow_in;
1403 
1404         if (!pmevcntr_is_64_bit(env, counter)) {
1405             delta = (uint32_t)delta;
1406         }
1407         overflow_in = pm_events[event_idx].ns_per_count(delta);
1408 
1409         if (overflow_in > 0) {
1410             int64_t overflow_at;
1411 
1412             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1413                                  overflow_in, &overflow_at)) {
1414                 ARMCPU *cpu = env_archcpu(env);
1415                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1416             }
1417         }
1418 #endif
1419 
1420         env->cp15.c14_pmevcntr_delta[counter] -=
1421             env->cp15.c14_pmevcntr[counter];
1422     }
1423 }
1424 
1425 void pmu_op_start(CPUARMState *env)
1426 {
1427     unsigned int i;
1428     pmccntr_op_start(env);
1429     for (i = 0; i < pmu_num_counters(env); i++) {
1430         pmevcntr_op_start(env, i);
1431     }
1432 }
1433 
1434 void pmu_op_finish(CPUARMState *env)
1435 {
1436     unsigned int i;
1437     pmccntr_op_finish(env);
1438     for (i = 0; i < pmu_num_counters(env); i++) {
1439         pmevcntr_op_finish(env, i);
1440     }
1441 }
1442 
1443 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1444 {
1445     pmu_op_start(&cpu->env);
1446 }
1447 
1448 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1449 {
1450     pmu_op_finish(&cpu->env);
1451 }
1452 
1453 void arm_pmu_timer_cb(void *opaque)
1454 {
1455     ARMCPU *cpu = opaque;
1456 
1457     /*
1458      * Update all the counter values based on the current underlying counts,
1459      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1460      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1461      * counter may expire.
1462      */
1463     pmu_op_start(&cpu->env);
1464     pmu_op_finish(&cpu->env);
1465 }
1466 
1467 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1468                        uint64_t value)
1469 {
1470     pmu_op_start(env);
1471 
1472     if (value & PMCRC) {
1473         /* The counter has been reset */
1474         env->cp15.c15_ccnt = 0;
1475     }
1476 
1477     if (value & PMCRP) {
1478         unsigned int i;
1479         for (i = 0; i < pmu_num_counters(env); i++) {
1480             env->cp15.c14_pmevcntr[i] = 0;
1481         }
1482     }
1483 
1484     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1485     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1486 
1487     pmu_op_finish(env);
1488 }
1489 
1490 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1491 {
1492     uint64_t pmcr = env->cp15.c9_pmcr;
1493 
1494     /*
1495      * If EL2 is implemented and enabled for the current security state, reads
1496      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1497      */
1498     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1499         pmcr &= ~PMCRN_MASK;
1500         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1501     }
1502 
1503     return pmcr;
1504 }
1505 
1506 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1507                           uint64_t value)
1508 {
1509     unsigned int i;
1510     uint64_t overflow_mask, new_pmswinc;
1511 
1512     for (i = 0; i < pmu_num_counters(env); i++) {
1513         /* Increment a counter's count iff: */
1514         if ((value & (1 << i)) && /* counter's bit is set */
1515                 /* counter is enabled and not filtered */
1516                 pmu_counter_enabled(env, i) &&
1517                 /* counter is SW_INCR */
1518                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1519             pmevcntr_op_start(env, i);
1520 
1521             /*
1522              * Detect if this write causes an overflow since we can't predict
1523              * PMSWINC overflows like we can for other events
1524              */
1525             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1526 
1527             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1528                 1ULL << 63 : 1ULL << 31;
1529 
1530             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1531                 env->cp15.c9_pmovsr |= (1 << i);
1532                 pmu_update_irq(env);
1533             }
1534 
1535             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1536 
1537             pmevcntr_op_finish(env, i);
1538         }
1539     }
1540 }
1541 
1542 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1543 {
1544     uint64_t ret;
1545     pmccntr_op_start(env);
1546     ret = env->cp15.c15_ccnt;
1547     pmccntr_op_finish(env);
1548     return ret;
1549 }
1550 
1551 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552                          uint64_t value)
1553 {
1554     /*
1555      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1556      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1557      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1558      * accessed.
1559      */
1560     env->cp15.c9_pmselr = value & 0x1f;
1561 }
1562 
1563 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1564                         uint64_t value)
1565 {
1566     pmccntr_op_start(env);
1567     env->cp15.c15_ccnt = value;
1568     pmccntr_op_finish(env);
1569 }
1570 
1571 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1572                             uint64_t value)
1573 {
1574     uint64_t cur_val = pmccntr_read(env, NULL);
1575 
1576     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1577 }
1578 
1579 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1580                             uint64_t value)
1581 {
1582     pmccntr_op_start(env);
1583     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1584     pmccntr_op_finish(env);
1585 }
1586 
1587 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1588                             uint64_t value)
1589 {
1590     pmccntr_op_start(env);
1591     /* M is not accessible from AArch32 */
1592     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1593         (value & PMCCFILTR);
1594     pmccntr_op_finish(env);
1595 }
1596 
1597 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1598 {
1599     /* M is not visible in AArch32 */
1600     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1601 }
1602 
1603 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1604                             uint64_t value)
1605 {
1606     pmu_op_start(env);
1607     value &= pmu_counter_mask(env);
1608     env->cp15.c9_pmcnten |= value;
1609     pmu_op_finish(env);
1610 }
1611 
1612 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1613                              uint64_t value)
1614 {
1615     pmu_op_start(env);
1616     value &= pmu_counter_mask(env);
1617     env->cp15.c9_pmcnten &= ~value;
1618     pmu_op_finish(env);
1619 }
1620 
1621 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1622                          uint64_t value)
1623 {
1624     value &= pmu_counter_mask(env);
1625     env->cp15.c9_pmovsr &= ~value;
1626     pmu_update_irq(env);
1627 }
1628 
1629 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1630                          uint64_t value)
1631 {
1632     value &= pmu_counter_mask(env);
1633     env->cp15.c9_pmovsr |= value;
1634     pmu_update_irq(env);
1635 }
1636 
1637 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1638                              uint64_t value, const uint8_t counter)
1639 {
1640     if (counter == 31) {
1641         pmccfiltr_write(env, ri, value);
1642     } else if (counter < pmu_num_counters(env)) {
1643         pmevcntr_op_start(env, counter);
1644 
1645         /*
1646          * If this counter's event type is changing, store the current
1647          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1648          * pmevcntr_op_finish has the correct baseline when it converts back to
1649          * a delta.
1650          */
1651         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1652             PMXEVTYPER_EVTCOUNT;
1653         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1654         if (old_event != new_event) {
1655             uint64_t count = 0;
1656             if (event_supported(new_event)) {
1657                 uint16_t event_idx = supported_event_map[new_event];
1658                 count = pm_events[event_idx].get_count(env);
1659             }
1660             env->cp15.c14_pmevcntr_delta[counter] = count;
1661         }
1662 
1663         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1664         pmevcntr_op_finish(env, counter);
1665     }
1666     /*
1667      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1668      * PMSELR value is equal to or greater than the number of implemented
1669      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1670      */
1671 }
1672 
1673 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1674                                const uint8_t counter)
1675 {
1676     if (counter == 31) {
1677         return env->cp15.pmccfiltr_el0;
1678     } else if (counter < pmu_num_counters(env)) {
1679         return env->cp15.c14_pmevtyper[counter];
1680     } else {
1681       /*
1682        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1683        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1684        */
1685         return 0;
1686     }
1687 }
1688 
1689 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1690                               uint64_t value)
1691 {
1692     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1693     pmevtyper_write(env, ri, value, counter);
1694 }
1695 
1696 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1697                                uint64_t value)
1698 {
1699     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1700     env->cp15.c14_pmevtyper[counter] = value;
1701 
1702     /*
1703      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1704      * pmu_op_finish calls when loading saved state for a migration. Because
1705      * we're potentially updating the type of event here, the value written to
1706      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1707      * different counter type. Therefore, we need to set this value to the
1708      * current count for the counter type we're writing so that pmu_op_finish
1709      * has the correct count for its calculation.
1710      */
1711     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1712     if (event_supported(event)) {
1713         uint16_t event_idx = supported_event_map[event];
1714         env->cp15.c14_pmevcntr_delta[counter] =
1715             pm_events[event_idx].get_count(env);
1716     }
1717 }
1718 
1719 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1720 {
1721     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1722     return pmevtyper_read(env, ri, counter);
1723 }
1724 
1725 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726                              uint64_t value)
1727 {
1728     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1729 }
1730 
1731 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1732 {
1733     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1734 }
1735 
1736 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1737                              uint64_t value, uint8_t counter)
1738 {
1739     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1740         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1741         value &= MAKE_64BIT_MASK(0, 32);
1742     }
1743     if (counter < pmu_num_counters(env)) {
1744         pmevcntr_op_start(env, counter);
1745         env->cp15.c14_pmevcntr[counter] = value;
1746         pmevcntr_op_finish(env, counter);
1747     }
1748     /*
1749      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1750      * are CONSTRAINED UNPREDICTABLE.
1751      */
1752 }
1753 
1754 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1755                               uint8_t counter)
1756 {
1757     if (counter < pmu_num_counters(env)) {
1758         uint64_t ret;
1759         pmevcntr_op_start(env, counter);
1760         ret = env->cp15.c14_pmevcntr[counter];
1761         pmevcntr_op_finish(env, counter);
1762         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1763             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1764             ret &= MAKE_64BIT_MASK(0, 32);
1765         }
1766         return ret;
1767     } else {
1768       /*
1769        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1770        * are CONSTRAINED UNPREDICTABLE.
1771        */
1772         return 0;
1773     }
1774 }
1775 
1776 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1777                              uint64_t value)
1778 {
1779     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1780     pmevcntr_write(env, ri, value, counter);
1781 }
1782 
1783 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1784 {
1785     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1786     return pmevcntr_read(env, ri, counter);
1787 }
1788 
1789 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1790                              uint64_t value)
1791 {
1792     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1793     assert(counter < pmu_num_counters(env));
1794     env->cp15.c14_pmevcntr[counter] = value;
1795     pmevcntr_write(env, ri, value, counter);
1796 }
1797 
1798 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1799 {
1800     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1801     assert(counter < pmu_num_counters(env));
1802     return env->cp15.c14_pmevcntr[counter];
1803 }
1804 
1805 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1806                              uint64_t value)
1807 {
1808     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1809 }
1810 
1811 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1812 {
1813     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1814 }
1815 
1816 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1817                             uint64_t value)
1818 {
1819     if (arm_feature(env, ARM_FEATURE_V8)) {
1820         env->cp15.c9_pmuserenr = value & 0xf;
1821     } else {
1822         env->cp15.c9_pmuserenr = value & 1;
1823     }
1824 }
1825 
1826 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1827                              uint64_t value)
1828 {
1829     /* We have no event counters so only the C bit can be changed */
1830     value &= pmu_counter_mask(env);
1831     env->cp15.c9_pminten |= value;
1832     pmu_update_irq(env);
1833 }
1834 
1835 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1836                              uint64_t value)
1837 {
1838     value &= pmu_counter_mask(env);
1839     env->cp15.c9_pminten &= ~value;
1840     pmu_update_irq(env);
1841 }
1842 
1843 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844                        uint64_t value)
1845 {
1846     /*
1847      * Note that even though the AArch64 view of this register has bits
1848      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1849      * architectural requirements for bits which are RES0 only in some
1850      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1851      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1852      */
1853     raw_write(env, ri, value & ~0x1FULL);
1854 }
1855 
1856 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1857 {
1858     /* Begin with base v8.0 state.  */
1859     uint64_t valid_mask = 0x3fff;
1860     ARMCPU *cpu = env_archcpu(env);
1861     uint64_t changed;
1862 
1863     /*
1864      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1865      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1866      * Instead, choose the format based on the mode of EL3.
1867      */
1868     if (arm_el_is_aa64(env, 3)) {
1869         value |= SCR_FW | SCR_AW;      /* RES1 */
1870         valid_mask &= ~SCR_NET;        /* RES0 */
1871 
1872         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1873             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1874             value |= SCR_RW;           /* RAO/WI */
1875         }
1876         if (cpu_isar_feature(aa64_ras, cpu)) {
1877             valid_mask |= SCR_TERR;
1878         }
1879         if (cpu_isar_feature(aa64_lor, cpu)) {
1880             valid_mask |= SCR_TLOR;
1881         }
1882         if (cpu_isar_feature(aa64_pauth, cpu)) {
1883             valid_mask |= SCR_API | SCR_APK;
1884         }
1885         if (cpu_isar_feature(aa64_sel2, cpu)) {
1886             valid_mask |= SCR_EEL2;
1887         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1888             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1889             value |= SCR_NS;
1890         }
1891         if (cpu_isar_feature(aa64_mte, cpu)) {
1892             valid_mask |= SCR_ATA;
1893         }
1894         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1895             valid_mask |= SCR_ENSCXT;
1896         }
1897         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1898             valid_mask |= SCR_EASE | SCR_NMEA;
1899         }
1900         if (cpu_isar_feature(aa64_sme, cpu)) {
1901             valid_mask |= SCR_ENTP2;
1902         }
1903         if (cpu_isar_feature(aa64_hcx, cpu)) {
1904             valid_mask |= SCR_HXEN;
1905         }
1906         if (cpu_isar_feature(aa64_fgt, cpu)) {
1907             valid_mask |= SCR_FGTEN;
1908         }
1909         if (cpu_isar_feature(aa64_rme, cpu)) {
1910             valid_mask |= SCR_NSE | SCR_GPF;
1911         }
1912     } else {
1913         valid_mask &= ~(SCR_RW | SCR_ST);
1914         if (cpu_isar_feature(aa32_ras, cpu)) {
1915             valid_mask |= SCR_TERR;
1916         }
1917     }
1918 
1919     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1920         valid_mask &= ~SCR_HCE;
1921 
1922         /*
1923          * On ARMv7, SMD (or SCD as it is called in v7) is only
1924          * supported if EL2 exists. The bit is UNK/SBZP when
1925          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1926          * when EL2 is unavailable.
1927          * On ARMv8, this bit is always available.
1928          */
1929         if (arm_feature(env, ARM_FEATURE_V7) &&
1930             !arm_feature(env, ARM_FEATURE_V8)) {
1931             valid_mask &= ~SCR_SMD;
1932         }
1933     }
1934 
1935     /* Clear all-context RES0 bits.  */
1936     value &= valid_mask;
1937     changed = env->cp15.scr_el3 ^ value;
1938     env->cp15.scr_el3 = value;
1939 
1940     /*
1941      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1942      * we must invalidate all TLBs below EL3.
1943      */
1944     if (changed & (SCR_NS | SCR_NSE)) {
1945         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1946                                            ARMMMUIdxBit_E20_0 |
1947                                            ARMMMUIdxBit_E10_1 |
1948                                            ARMMMUIdxBit_E20_2 |
1949                                            ARMMMUIdxBit_E10_1_PAN |
1950                                            ARMMMUIdxBit_E20_2_PAN |
1951                                            ARMMMUIdxBit_E2));
1952     }
1953 }
1954 
1955 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1956 {
1957     /*
1958      * scr_write will set the RES1 bits on an AArch64-only CPU.
1959      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1960      */
1961     scr_write(env, ri, 0);
1962 }
1963 
1964 static CPAccessResult access_tid4(CPUARMState *env,
1965                                   const ARMCPRegInfo *ri,
1966                                   bool isread)
1967 {
1968     if (arm_current_el(env) == 1 &&
1969         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1970         return CP_ACCESS_TRAP_EL2;
1971     }
1972 
1973     return CP_ACCESS_OK;
1974 }
1975 
1976 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1977 {
1978     ARMCPU *cpu = env_archcpu(env);
1979 
1980     /*
1981      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1982      * bank
1983      */
1984     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1985                                         ri->secure & ARM_CP_SECSTATE_S);
1986 
1987     return cpu->ccsidr[index];
1988 }
1989 
1990 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1991                          uint64_t value)
1992 {
1993     raw_write(env, ri, value & 0xf);
1994 }
1995 
1996 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1997 {
1998     CPUState *cs = env_cpu(env);
1999     bool el1 = arm_current_el(env) == 1;
2000     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2001     uint64_t ret = 0;
2002 
2003     if (hcr_el2 & HCR_IMO) {
2004         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2005             ret |= CPSR_I;
2006         }
2007     } else {
2008         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2009             ret |= CPSR_I;
2010         }
2011     }
2012 
2013     if (hcr_el2 & HCR_FMO) {
2014         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2015             ret |= CPSR_F;
2016         }
2017     } else {
2018         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2019             ret |= CPSR_F;
2020         }
2021     }
2022 
2023     if (hcr_el2 & HCR_AMO) {
2024         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2025             ret |= CPSR_A;
2026         }
2027     }
2028 
2029     return ret;
2030 }
2031 
2032 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2033                                        bool isread)
2034 {
2035     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2036         return CP_ACCESS_TRAP_EL2;
2037     }
2038 
2039     return CP_ACCESS_OK;
2040 }
2041 
2042 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2043                                        bool isread)
2044 {
2045     if (arm_feature(env, ARM_FEATURE_V8)) {
2046         return access_aa64_tid1(env, ri, isread);
2047     }
2048 
2049     return CP_ACCESS_OK;
2050 }
2051 
2052 static const ARMCPRegInfo v7_cp_reginfo[] = {
2053     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2054     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2055       .access = PL1_W, .type = ARM_CP_NOP },
2056     /*
2057      * Performance monitors are implementation defined in v7,
2058      * but with an ARM recommended set of registers, which we
2059      * follow.
2060      *
2061      * Performance registers fall into three categories:
2062      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2063      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2064      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2065      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2066      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2067      */
2068     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2069       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2070       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2071       .writefn = pmcntenset_write,
2072       .accessfn = pmreg_access,
2073       .fgt = FGT_PMCNTEN,
2074       .raw_writefn = raw_write },
2075     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2076       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2077       .access = PL0_RW, .accessfn = pmreg_access,
2078       .fgt = FGT_PMCNTEN,
2079       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2080       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2081     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2082       .access = PL0_RW,
2083       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2084       .accessfn = pmreg_access,
2085       .fgt = FGT_PMCNTEN,
2086       .writefn = pmcntenclr_write,
2087       .type = ARM_CP_ALIAS | ARM_CP_IO },
2088     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2089       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2090       .access = PL0_RW, .accessfn = pmreg_access,
2091       .fgt = FGT_PMCNTEN,
2092       .type = ARM_CP_ALIAS | ARM_CP_IO,
2093       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2094       .writefn = pmcntenclr_write },
2095     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2096       .access = PL0_RW, .type = ARM_CP_IO,
2097       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2098       .accessfn = pmreg_access,
2099       .fgt = FGT_PMOVS,
2100       .writefn = pmovsr_write,
2101       .raw_writefn = raw_write },
2102     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2103       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2104       .access = PL0_RW, .accessfn = pmreg_access,
2105       .fgt = FGT_PMOVS,
2106       .type = ARM_CP_ALIAS | ARM_CP_IO,
2107       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2108       .writefn = pmovsr_write,
2109       .raw_writefn = raw_write },
2110     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2111       .access = PL0_W, .accessfn = pmreg_access_swinc,
2112       .fgt = FGT_PMSWINC_EL0,
2113       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2114       .writefn = pmswinc_write },
2115     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2116       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2117       .access = PL0_W, .accessfn = pmreg_access_swinc,
2118       .fgt = FGT_PMSWINC_EL0,
2119       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2120       .writefn = pmswinc_write },
2121     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2122       .access = PL0_RW, .type = ARM_CP_ALIAS,
2123       .fgt = FGT_PMSELR_EL0,
2124       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2125       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2126       .raw_writefn = raw_write},
2127     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2128       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2129       .access = PL0_RW, .accessfn = pmreg_access_selr,
2130       .fgt = FGT_PMSELR_EL0,
2131       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2132       .writefn = pmselr_write, .raw_writefn = raw_write, },
2133     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2134       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2135       .fgt = FGT_PMCCNTR_EL0,
2136       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2137       .accessfn = pmreg_access_ccntr },
2138     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2139       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2140       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2141       .fgt = FGT_PMCCNTR_EL0,
2142       .type = ARM_CP_IO,
2143       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2144       .readfn = pmccntr_read, .writefn = pmccntr_write,
2145       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2146     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2147       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2148       .access = PL0_RW, .accessfn = pmreg_access,
2149       .fgt = FGT_PMCCFILTR_EL0,
2150       .type = ARM_CP_ALIAS | ARM_CP_IO,
2151       .resetvalue = 0, },
2152     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2153       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2154       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2155       .access = PL0_RW, .accessfn = pmreg_access,
2156       .fgt = FGT_PMCCFILTR_EL0,
2157       .type = ARM_CP_IO,
2158       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2159       .resetvalue = 0, },
2160     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2161       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2162       .accessfn = pmreg_access,
2163       .fgt = FGT_PMEVTYPERN_EL0,
2164       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2165     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2166       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2167       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2168       .accessfn = pmreg_access,
2169       .fgt = FGT_PMEVTYPERN_EL0,
2170       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2171     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2172       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2173       .accessfn = pmreg_access_xevcntr,
2174       .fgt = FGT_PMEVCNTRN_EL0,
2175       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2176     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2177       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2178       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2179       .accessfn = pmreg_access_xevcntr,
2180       .fgt = FGT_PMEVCNTRN_EL0,
2181       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2182     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2183       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2184       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2185       .resetvalue = 0,
2186       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2187     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2188       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2189       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2190       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2191       .resetvalue = 0,
2192       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2193     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2194       .access = PL1_RW, .accessfn = access_tpm,
2195       .fgt = FGT_PMINTEN,
2196       .type = ARM_CP_ALIAS | ARM_CP_IO,
2197       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2198       .resetvalue = 0,
2199       .writefn = pmintenset_write, .raw_writefn = raw_write },
2200     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2201       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2202       .access = PL1_RW, .accessfn = access_tpm,
2203       .fgt = FGT_PMINTEN,
2204       .type = ARM_CP_IO,
2205       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2206       .writefn = pmintenset_write, .raw_writefn = raw_write,
2207       .resetvalue = 0x0 },
2208     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2209       .access = PL1_RW, .accessfn = access_tpm,
2210       .fgt = FGT_PMINTEN,
2211       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2212       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2213       .writefn = pmintenclr_write, },
2214     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2215       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2216       .access = PL1_RW, .accessfn = access_tpm,
2217       .fgt = FGT_PMINTEN,
2218       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2219       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2220       .writefn = pmintenclr_write },
2221     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2222       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2223       .access = PL1_R,
2224       .accessfn = access_tid4,
2225       .fgt = FGT_CCSIDR_EL1,
2226       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2227     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2228       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2229       .access = PL1_RW,
2230       .accessfn = access_tid4,
2231       .fgt = FGT_CSSELR_EL1,
2232       .writefn = csselr_write, .resetvalue = 0,
2233       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2234                              offsetof(CPUARMState, cp15.csselr_ns) } },
2235     /*
2236      * Auxiliary ID register: this actually has an IMPDEF value but for now
2237      * just RAZ for all cores:
2238      */
2239     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2240       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2241       .access = PL1_R, .type = ARM_CP_CONST,
2242       .accessfn = access_aa64_tid1,
2243       .fgt = FGT_AIDR_EL1,
2244       .resetvalue = 0 },
2245     /*
2246      * Auxiliary fault status registers: these also are IMPDEF, and we
2247      * choose to RAZ/WI for all cores.
2248      */
2249     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2250       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2251       .access = PL1_RW, .accessfn = access_tvm_trvm,
2252       .fgt = FGT_AFSR0_EL1,
2253       .type = ARM_CP_CONST, .resetvalue = 0 },
2254     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2255       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2256       .access = PL1_RW, .accessfn = access_tvm_trvm,
2257       .fgt = FGT_AFSR1_EL1,
2258       .type = ARM_CP_CONST, .resetvalue = 0 },
2259     /*
2260      * MAIR can just read-as-written because we don't implement caches
2261      * and so don't need to care about memory attributes.
2262      */
2263     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2264       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2265       .access = PL1_RW, .accessfn = access_tvm_trvm,
2266       .fgt = FGT_MAIR_EL1,
2267       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2268       .resetvalue = 0 },
2269     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2270       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2271       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2272       .resetvalue = 0 },
2273     /*
2274      * For non-long-descriptor page tables these are PRRR and NMRR;
2275      * regardless they still act as reads-as-written for QEMU.
2276      */
2277      /*
2278       * MAIR0/1 are defined separately from their 64-bit counterpart which
2279       * allows them to assign the correct fieldoffset based on the endianness
2280       * handled in the field definitions.
2281       */
2282     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2283       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2284       .access = PL1_RW, .accessfn = access_tvm_trvm,
2285       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2286                              offsetof(CPUARMState, cp15.mair0_ns) },
2287       .resetfn = arm_cp_reset_ignore },
2288     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2289       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2290       .access = PL1_RW, .accessfn = access_tvm_trvm,
2291       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2292                              offsetof(CPUARMState, cp15.mair1_ns) },
2293       .resetfn = arm_cp_reset_ignore },
2294     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2295       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2296       .fgt = FGT_ISR_EL1,
2297       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2298     /* 32 bit ITLB invalidates */
2299     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2300       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2301       .writefn = tlbiall_write },
2302     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2303       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2304       .writefn = tlbimva_write },
2305     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2306       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2307       .writefn = tlbiasid_write },
2308     /* 32 bit DTLB invalidates */
2309     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2310       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2311       .writefn = tlbiall_write },
2312     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2313       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2314       .writefn = tlbimva_write },
2315     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2316       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2317       .writefn = tlbiasid_write },
2318     /* 32 bit TLB invalidates */
2319     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2320       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2321       .writefn = tlbiall_write },
2322     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2323       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2324       .writefn = tlbimva_write },
2325     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2326       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2327       .writefn = tlbiasid_write },
2328     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2329       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2330       .writefn = tlbimvaa_write },
2331 };
2332 
2333 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2334     /* 32 bit TLB invalidates, Inner Shareable */
2335     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2336       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2337       .writefn = tlbiall_is_write },
2338     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2339       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2340       .writefn = tlbimva_is_write },
2341     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2342       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2343       .writefn = tlbiasid_is_write },
2344     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2345       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2346       .writefn = tlbimvaa_is_write },
2347 };
2348 
2349 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2350     /* PMOVSSET is not implemented in v7 before v7ve */
2351     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2352       .access = PL0_RW, .accessfn = pmreg_access,
2353       .fgt = FGT_PMOVS,
2354       .type = ARM_CP_ALIAS | ARM_CP_IO,
2355       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2356       .writefn = pmovsset_write,
2357       .raw_writefn = raw_write },
2358     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2359       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2360       .access = PL0_RW, .accessfn = pmreg_access,
2361       .fgt = FGT_PMOVS,
2362       .type = ARM_CP_ALIAS | ARM_CP_IO,
2363       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2364       .writefn = pmovsset_write,
2365       .raw_writefn = raw_write },
2366 };
2367 
2368 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2369                         uint64_t value)
2370 {
2371     value &= 1;
2372     env->teecr = value;
2373 }
2374 
2375 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2376                                    bool isread)
2377 {
2378     /*
2379      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2380      * at all, so we don't need to check whether we're v8A.
2381      */
2382     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2383         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2384         return CP_ACCESS_TRAP_EL2;
2385     }
2386     return CP_ACCESS_OK;
2387 }
2388 
2389 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2390                                     bool isread)
2391 {
2392     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2393         return CP_ACCESS_TRAP;
2394     }
2395     return teecr_access(env, ri, isread);
2396 }
2397 
2398 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2399     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2400       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2401       .resetvalue = 0,
2402       .writefn = teecr_write, .accessfn = teecr_access },
2403     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2404       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2405       .accessfn = teehbr_access, .resetvalue = 0 },
2406 };
2407 
2408 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2409     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2410       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2411       .access = PL0_RW,
2412       .fgt = FGT_TPIDR_EL0,
2413       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2414     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2415       .access = PL0_RW,
2416       .fgt = FGT_TPIDR_EL0,
2417       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2418                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2419       .resetfn = arm_cp_reset_ignore },
2420     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2421       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2422       .access = PL0_R | PL1_W,
2423       .fgt = FGT_TPIDRRO_EL0,
2424       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2425       .resetvalue = 0},
2426     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2427       .access = PL0_R | PL1_W,
2428       .fgt = FGT_TPIDRRO_EL0,
2429       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2430                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2431       .resetfn = arm_cp_reset_ignore },
2432     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2433       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2434       .access = PL1_RW,
2435       .fgt = FGT_TPIDR_EL1,
2436       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2437     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2438       .access = PL1_RW,
2439       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2440                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2441       .resetvalue = 0 },
2442 };
2443 
2444 #ifndef CONFIG_USER_ONLY
2445 
2446 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2447                                        bool isread)
2448 {
2449     /*
2450      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2451      * Writable only at the highest implemented exception level.
2452      */
2453     int el = arm_current_el(env);
2454     uint64_t hcr;
2455     uint32_t cntkctl;
2456 
2457     switch (el) {
2458     case 0:
2459         hcr = arm_hcr_el2_eff(env);
2460         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2461             cntkctl = env->cp15.cnthctl_el2;
2462         } else {
2463             cntkctl = env->cp15.c14_cntkctl;
2464         }
2465         if (!extract32(cntkctl, 0, 2)) {
2466             return CP_ACCESS_TRAP;
2467         }
2468         break;
2469     case 1:
2470         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2471             arm_is_secure_below_el3(env)) {
2472             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2473             return CP_ACCESS_TRAP_UNCATEGORIZED;
2474         }
2475         break;
2476     case 2:
2477     case 3:
2478         break;
2479     }
2480 
2481     if (!isread && el < arm_highest_el(env)) {
2482         return CP_ACCESS_TRAP_UNCATEGORIZED;
2483     }
2484 
2485     return CP_ACCESS_OK;
2486 }
2487 
2488 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2489                                         bool isread)
2490 {
2491     unsigned int cur_el = arm_current_el(env);
2492     bool has_el2 = arm_is_el2_enabled(env);
2493     uint64_t hcr = arm_hcr_el2_eff(env);
2494 
2495     switch (cur_el) {
2496     case 0:
2497         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2498         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2499             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2500                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2501         }
2502 
2503         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2504         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2505             return CP_ACCESS_TRAP;
2506         }
2507         /* fall through */
2508     case 1:
2509         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2510         if (has_el2 && timeridx == GTIMER_PHYS &&
2511             (hcr & HCR_E2H
2512              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2513              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2514             return CP_ACCESS_TRAP_EL2;
2515         }
2516         break;
2517     }
2518     return CP_ACCESS_OK;
2519 }
2520 
2521 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2522                                       bool isread)
2523 {
2524     unsigned int cur_el = arm_current_el(env);
2525     bool has_el2 = arm_is_el2_enabled(env);
2526     uint64_t hcr = arm_hcr_el2_eff(env);
2527 
2528     switch (cur_el) {
2529     case 0:
2530         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2531             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2532             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2533                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2534         }
2535 
2536         /*
2537          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2538          * EL0 if EL0[PV]TEN is zero.
2539          */
2540         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2541             return CP_ACCESS_TRAP;
2542         }
2543         /* fall through */
2544 
2545     case 1:
2546         if (has_el2 && timeridx == GTIMER_PHYS) {
2547             if (hcr & HCR_E2H) {
2548                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2549                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2550                     return CP_ACCESS_TRAP_EL2;
2551                 }
2552             } else {
2553                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2554                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2555                     return CP_ACCESS_TRAP_EL2;
2556                 }
2557             }
2558         }
2559         break;
2560     }
2561     return CP_ACCESS_OK;
2562 }
2563 
2564 static CPAccessResult gt_pct_access(CPUARMState *env,
2565                                     const ARMCPRegInfo *ri,
2566                                     bool isread)
2567 {
2568     return gt_counter_access(env, GTIMER_PHYS, isread);
2569 }
2570 
2571 static CPAccessResult gt_vct_access(CPUARMState *env,
2572                                     const ARMCPRegInfo *ri,
2573                                     bool isread)
2574 {
2575     return gt_counter_access(env, GTIMER_VIRT, isread);
2576 }
2577 
2578 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2579                                        bool isread)
2580 {
2581     return gt_timer_access(env, GTIMER_PHYS, isread);
2582 }
2583 
2584 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2585                                        bool isread)
2586 {
2587     return gt_timer_access(env, GTIMER_VIRT, isread);
2588 }
2589 
2590 static CPAccessResult gt_stimer_access(CPUARMState *env,
2591                                        const ARMCPRegInfo *ri,
2592                                        bool isread)
2593 {
2594     /*
2595      * The AArch64 register view of the secure physical timer is
2596      * always accessible from EL3, and configurably accessible from
2597      * Secure EL1.
2598      */
2599     switch (arm_current_el(env)) {
2600     case 1:
2601         if (!arm_is_secure(env)) {
2602             return CP_ACCESS_TRAP;
2603         }
2604         if (!(env->cp15.scr_el3 & SCR_ST)) {
2605             return CP_ACCESS_TRAP_EL3;
2606         }
2607         return CP_ACCESS_OK;
2608     case 0:
2609     case 2:
2610         return CP_ACCESS_TRAP;
2611     case 3:
2612         return CP_ACCESS_OK;
2613     default:
2614         g_assert_not_reached();
2615     }
2616 }
2617 
2618 static uint64_t gt_get_countervalue(CPUARMState *env)
2619 {
2620     ARMCPU *cpu = env_archcpu(env);
2621 
2622     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2623 }
2624 
2625 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2626 {
2627     CPUARMState *env = &cpu->env;
2628     uint64_t cnthctl = env->cp15.cnthctl_el2;
2629     ARMSecuritySpace ss = arm_security_space(env);
2630     /* ISTATUS && !IMASK */
2631     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2632 
2633     /*
2634      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2635      * It is RES0 in Secure and NonSecure state.
2636      */
2637     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2638         ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2639          (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2640         irqstate = 0;
2641     }
2642 
2643     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2644     trace_arm_gt_update_irq(timeridx, irqstate);
2645 }
2646 
2647 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2648 {
2649     /*
2650      * Changing security state between Root and Secure/NonSecure, which may
2651      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2652      * mask bits. Update the IRQ state accordingly.
2653      */
2654     gt_update_irq(cpu, GTIMER_VIRT);
2655     gt_update_irq(cpu, GTIMER_PHYS);
2656 }
2657 
2658 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2659 {
2660     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2661 
2662     if (gt->ctl & 1) {
2663         /*
2664          * Timer enabled: calculate and set current ISTATUS, irq, and
2665          * reset timer to when ISTATUS next has to change
2666          */
2667         uint64_t offset = timeridx == GTIMER_VIRT ?
2668                                       cpu->env.cp15.cntvoff_el2 : 0;
2669         uint64_t count = gt_get_countervalue(&cpu->env);
2670         /* Note that this must be unsigned 64 bit arithmetic: */
2671         int istatus = count - offset >= gt->cval;
2672         uint64_t nexttick;
2673 
2674         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2675 
2676         if (istatus) {
2677             /*
2678              * Next transition is when (count - offset) rolls back over to 0.
2679              * If offset > count then this is when count == offset;
2680              * if offset <= count then this is when count == offset + 2^64
2681              * For the latter case we set nexttick to an "as far in future
2682              * as possible" value and let the code below handle it.
2683              */
2684             if (offset > count) {
2685                 nexttick = offset;
2686             } else {
2687                 nexttick = UINT64_MAX;
2688             }
2689         } else {
2690             /*
2691              * Next transition is when (count - offset) == cval, i.e.
2692              * when count == (cval + offset).
2693              * If that would overflow, then again we set up the next interrupt
2694              * for "as far in the future as possible" for the code below.
2695              */
2696             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2697                 nexttick = UINT64_MAX;
2698             }
2699         }
2700         /*
2701          * Note that the desired next expiry time might be beyond the
2702          * signed-64-bit range of a QEMUTimer -- in this case we just
2703          * set the timer for as far in the future as possible. When the
2704          * timer expires we will reset the timer for any remaining period.
2705          */
2706         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2707             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2708         } else {
2709             timer_mod(cpu->gt_timer[timeridx], nexttick);
2710         }
2711         trace_arm_gt_recalc(timeridx, nexttick);
2712     } else {
2713         /* Timer disabled: ISTATUS and timer output always clear */
2714         gt->ctl &= ~4;
2715         timer_del(cpu->gt_timer[timeridx]);
2716         trace_arm_gt_recalc_disabled(timeridx);
2717     }
2718     gt_update_irq(cpu, timeridx);
2719 }
2720 
2721 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2722                            int timeridx)
2723 {
2724     ARMCPU *cpu = env_archcpu(env);
2725 
2726     timer_del(cpu->gt_timer[timeridx]);
2727 }
2728 
2729 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2730 {
2731     return gt_get_countervalue(env);
2732 }
2733 
2734 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2735 {
2736     uint64_t hcr;
2737 
2738     switch (arm_current_el(env)) {
2739     case 2:
2740         hcr = arm_hcr_el2_eff(env);
2741         if (hcr & HCR_E2H) {
2742             return 0;
2743         }
2744         break;
2745     case 0:
2746         hcr = arm_hcr_el2_eff(env);
2747         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2748             return 0;
2749         }
2750         break;
2751     }
2752 
2753     return env->cp15.cntvoff_el2;
2754 }
2755 
2756 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2757 {
2758     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2759 }
2760 
2761 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2762                           int timeridx,
2763                           uint64_t value)
2764 {
2765     trace_arm_gt_cval_write(timeridx, value);
2766     env->cp15.c14_timer[timeridx].cval = value;
2767     gt_recalc_timer(env_archcpu(env), timeridx);
2768 }
2769 
2770 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2771                              int timeridx)
2772 {
2773     uint64_t offset = 0;
2774 
2775     switch (timeridx) {
2776     case GTIMER_VIRT:
2777     case GTIMER_HYPVIRT:
2778         offset = gt_virt_cnt_offset(env);
2779         break;
2780     }
2781 
2782     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2783                       (gt_get_countervalue(env) - offset));
2784 }
2785 
2786 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787                           int timeridx,
2788                           uint64_t value)
2789 {
2790     uint64_t offset = 0;
2791 
2792     switch (timeridx) {
2793     case GTIMER_VIRT:
2794     case GTIMER_HYPVIRT:
2795         offset = gt_virt_cnt_offset(env);
2796         break;
2797     }
2798 
2799     trace_arm_gt_tval_write(timeridx, value);
2800     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2801                                          sextract64(value, 0, 32);
2802     gt_recalc_timer(env_archcpu(env), timeridx);
2803 }
2804 
2805 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2806                          int timeridx,
2807                          uint64_t value)
2808 {
2809     ARMCPU *cpu = env_archcpu(env);
2810     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2811 
2812     trace_arm_gt_ctl_write(timeridx, value);
2813     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2814     if ((oldval ^ value) & 1) {
2815         /* Enable toggled */
2816         gt_recalc_timer(cpu, timeridx);
2817     } else if ((oldval ^ value) & 2) {
2818         /*
2819          * IMASK toggled: don't need to recalculate,
2820          * just set the interrupt line based on ISTATUS
2821          */
2822         trace_arm_gt_imask_toggle(timeridx);
2823         gt_update_irq(cpu, timeridx);
2824     }
2825 }
2826 
2827 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     gt_timer_reset(env, ri, GTIMER_PHYS);
2830 }
2831 
2832 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833                                uint64_t value)
2834 {
2835     gt_cval_write(env, ri, GTIMER_PHYS, value);
2836 }
2837 
2838 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2839 {
2840     return gt_tval_read(env, ri, GTIMER_PHYS);
2841 }
2842 
2843 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844                                uint64_t value)
2845 {
2846     gt_tval_write(env, ri, GTIMER_PHYS, value);
2847 }
2848 
2849 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850                               uint64_t value)
2851 {
2852     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2853 }
2854 
2855 static int gt_phys_redir_timeridx(CPUARMState *env)
2856 {
2857     switch (arm_mmu_idx(env)) {
2858     case ARMMMUIdx_E20_0:
2859     case ARMMMUIdx_E20_2:
2860     case ARMMMUIdx_E20_2_PAN:
2861         return GTIMER_HYP;
2862     default:
2863         return GTIMER_PHYS;
2864     }
2865 }
2866 
2867 static int gt_virt_redir_timeridx(CPUARMState *env)
2868 {
2869     switch (arm_mmu_idx(env)) {
2870     case ARMMMUIdx_E20_0:
2871     case ARMMMUIdx_E20_2:
2872     case ARMMMUIdx_E20_2_PAN:
2873         return GTIMER_HYPVIRT;
2874     default:
2875         return GTIMER_VIRT;
2876     }
2877 }
2878 
2879 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2880                                         const ARMCPRegInfo *ri)
2881 {
2882     int timeridx = gt_phys_redir_timeridx(env);
2883     return env->cp15.c14_timer[timeridx].cval;
2884 }
2885 
2886 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887                                      uint64_t value)
2888 {
2889     int timeridx = gt_phys_redir_timeridx(env);
2890     gt_cval_write(env, ri, timeridx, value);
2891 }
2892 
2893 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2894                                         const ARMCPRegInfo *ri)
2895 {
2896     int timeridx = gt_phys_redir_timeridx(env);
2897     return gt_tval_read(env, ri, timeridx);
2898 }
2899 
2900 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2901                                      uint64_t value)
2902 {
2903     int timeridx = gt_phys_redir_timeridx(env);
2904     gt_tval_write(env, ri, timeridx, value);
2905 }
2906 
2907 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2908                                        const ARMCPRegInfo *ri)
2909 {
2910     int timeridx = gt_phys_redir_timeridx(env);
2911     return env->cp15.c14_timer[timeridx].ctl;
2912 }
2913 
2914 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915                                     uint64_t value)
2916 {
2917     int timeridx = gt_phys_redir_timeridx(env);
2918     gt_ctl_write(env, ri, timeridx, value);
2919 }
2920 
2921 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2922 {
2923     gt_timer_reset(env, ri, GTIMER_VIRT);
2924 }
2925 
2926 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927                                uint64_t value)
2928 {
2929     gt_cval_write(env, ri, GTIMER_VIRT, value);
2930 }
2931 
2932 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2933 {
2934     return gt_tval_read(env, ri, GTIMER_VIRT);
2935 }
2936 
2937 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938                                uint64_t value)
2939 {
2940     gt_tval_write(env, ri, GTIMER_VIRT, value);
2941 }
2942 
2943 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944                               uint64_t value)
2945 {
2946     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2947 }
2948 
2949 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2950                              uint64_t value)
2951 {
2952     ARMCPU *cpu = env_archcpu(env);
2953     uint32_t oldval = env->cp15.cnthctl_el2;
2954 
2955     raw_write(env, ri, value);
2956 
2957     if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2958         gt_update_irq(cpu, GTIMER_VIRT);
2959     } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2960         gt_update_irq(cpu, GTIMER_PHYS);
2961     }
2962 }
2963 
2964 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965                               uint64_t value)
2966 {
2967     ARMCPU *cpu = env_archcpu(env);
2968 
2969     trace_arm_gt_cntvoff_write(value);
2970     raw_write(env, ri, value);
2971     gt_recalc_timer(cpu, GTIMER_VIRT);
2972 }
2973 
2974 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2975                                         const ARMCPRegInfo *ri)
2976 {
2977     int timeridx = gt_virt_redir_timeridx(env);
2978     return env->cp15.c14_timer[timeridx].cval;
2979 }
2980 
2981 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982                                      uint64_t value)
2983 {
2984     int timeridx = gt_virt_redir_timeridx(env);
2985     gt_cval_write(env, ri, timeridx, value);
2986 }
2987 
2988 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2989                                         const ARMCPRegInfo *ri)
2990 {
2991     int timeridx = gt_virt_redir_timeridx(env);
2992     return gt_tval_read(env, ri, timeridx);
2993 }
2994 
2995 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2996                                      uint64_t value)
2997 {
2998     int timeridx = gt_virt_redir_timeridx(env);
2999     gt_tval_write(env, ri, timeridx, value);
3000 }
3001 
3002 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3003                                        const ARMCPRegInfo *ri)
3004 {
3005     int timeridx = gt_virt_redir_timeridx(env);
3006     return env->cp15.c14_timer[timeridx].ctl;
3007 }
3008 
3009 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3010                                     uint64_t value)
3011 {
3012     int timeridx = gt_virt_redir_timeridx(env);
3013     gt_ctl_write(env, ri, timeridx, value);
3014 }
3015 
3016 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3017 {
3018     gt_timer_reset(env, ri, GTIMER_HYP);
3019 }
3020 
3021 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022                               uint64_t value)
3023 {
3024     gt_cval_write(env, ri, GTIMER_HYP, value);
3025 }
3026 
3027 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3028 {
3029     return gt_tval_read(env, ri, GTIMER_HYP);
3030 }
3031 
3032 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033                               uint64_t value)
3034 {
3035     gt_tval_write(env, ri, GTIMER_HYP, value);
3036 }
3037 
3038 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3039                               uint64_t value)
3040 {
3041     gt_ctl_write(env, ri, GTIMER_HYP, value);
3042 }
3043 
3044 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3045 {
3046     gt_timer_reset(env, ri, GTIMER_SEC);
3047 }
3048 
3049 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050                               uint64_t value)
3051 {
3052     gt_cval_write(env, ri, GTIMER_SEC, value);
3053 }
3054 
3055 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3056 {
3057     return gt_tval_read(env, ri, GTIMER_SEC);
3058 }
3059 
3060 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3061                               uint64_t value)
3062 {
3063     gt_tval_write(env, ri, GTIMER_SEC, value);
3064 }
3065 
3066 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3067                               uint64_t value)
3068 {
3069     gt_ctl_write(env, ri, GTIMER_SEC, value);
3070 }
3071 
3072 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3073 {
3074     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3075 }
3076 
3077 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078                              uint64_t value)
3079 {
3080     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3081 }
3082 
3083 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3084 {
3085     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3086 }
3087 
3088 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3089                              uint64_t value)
3090 {
3091     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3092 }
3093 
3094 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3095                             uint64_t value)
3096 {
3097     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3098 }
3099 
3100 void arm_gt_ptimer_cb(void *opaque)
3101 {
3102     ARMCPU *cpu = opaque;
3103 
3104     gt_recalc_timer(cpu, GTIMER_PHYS);
3105 }
3106 
3107 void arm_gt_vtimer_cb(void *opaque)
3108 {
3109     ARMCPU *cpu = opaque;
3110 
3111     gt_recalc_timer(cpu, GTIMER_VIRT);
3112 }
3113 
3114 void arm_gt_htimer_cb(void *opaque)
3115 {
3116     ARMCPU *cpu = opaque;
3117 
3118     gt_recalc_timer(cpu, GTIMER_HYP);
3119 }
3120 
3121 void arm_gt_stimer_cb(void *opaque)
3122 {
3123     ARMCPU *cpu = opaque;
3124 
3125     gt_recalc_timer(cpu, GTIMER_SEC);
3126 }
3127 
3128 void arm_gt_hvtimer_cb(void *opaque)
3129 {
3130     ARMCPU *cpu = opaque;
3131 
3132     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3133 }
3134 
3135 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3136 {
3137     ARMCPU *cpu = env_archcpu(env);
3138 
3139     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3140 }
3141 
3142 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3143     /*
3144      * Note that CNTFRQ is purely reads-as-written for the benefit
3145      * of software; writing it doesn't actually change the timer frequency.
3146      * Our reset value matches the fixed frequency we implement the timer at.
3147      */
3148     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3149       .type = ARM_CP_ALIAS,
3150       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3151       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3152     },
3153     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3154       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3155       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3156       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3157       .resetfn = arm_gt_cntfrq_reset,
3158     },
3159     /* overall control: mostly access permissions */
3160     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3161       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3162       .access = PL1_RW,
3163       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3164       .resetvalue = 0,
3165     },
3166     /* per-timer control */
3167     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3168       .secure = ARM_CP_SECSTATE_NS,
3169       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3170       .accessfn = gt_ptimer_access,
3171       .fieldoffset = offsetoflow32(CPUARMState,
3172                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3173       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3174       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3175     },
3176     { .name = "CNTP_CTL_S",
3177       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3178       .secure = ARM_CP_SECSTATE_S,
3179       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3180       .accessfn = gt_ptimer_access,
3181       .fieldoffset = offsetoflow32(CPUARMState,
3182                                    cp15.c14_timer[GTIMER_SEC].ctl),
3183       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3184     },
3185     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3186       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3187       .type = ARM_CP_IO, .access = PL0_RW,
3188       .accessfn = gt_ptimer_access,
3189       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3190       .resetvalue = 0,
3191       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3192       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3193     },
3194     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3195       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3196       .accessfn = gt_vtimer_access,
3197       .fieldoffset = offsetoflow32(CPUARMState,
3198                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3199       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3200       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3201     },
3202     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3203       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3204       .type = ARM_CP_IO, .access = PL0_RW,
3205       .accessfn = gt_vtimer_access,
3206       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3207       .resetvalue = 0,
3208       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3209       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3210     },
3211     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3212     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3213       .secure = ARM_CP_SECSTATE_NS,
3214       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3215       .accessfn = gt_ptimer_access,
3216       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3217     },
3218     { .name = "CNTP_TVAL_S",
3219       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3220       .secure = ARM_CP_SECSTATE_S,
3221       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3222       .accessfn = gt_ptimer_access,
3223       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3224     },
3225     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3226       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3227       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3228       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3229       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3230     },
3231     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3232       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3233       .accessfn = gt_vtimer_access,
3234       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3235     },
3236     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3237       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3238       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3239       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3240       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3241     },
3242     /* The counter itself */
3243     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3244       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3245       .accessfn = gt_pct_access,
3246       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3247     },
3248     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3249       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3250       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3251       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3252     },
3253     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3254       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3255       .accessfn = gt_vct_access,
3256       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3257     },
3258     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3259       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3260       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3261       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3262     },
3263     /* Comparison value, indicating when the timer goes off */
3264     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3265       .secure = ARM_CP_SECSTATE_NS,
3266       .access = PL0_RW,
3267       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3268       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3269       .accessfn = gt_ptimer_access,
3270       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3271       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3272     },
3273     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3274       .secure = ARM_CP_SECSTATE_S,
3275       .access = PL0_RW,
3276       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3277       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3278       .accessfn = gt_ptimer_access,
3279       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3280     },
3281     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3282       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3283       .access = PL0_RW,
3284       .type = ARM_CP_IO,
3285       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3286       .resetvalue = 0, .accessfn = gt_ptimer_access,
3287       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3288       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3289     },
3290     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3291       .access = PL0_RW,
3292       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3293       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3294       .accessfn = gt_vtimer_access,
3295       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3296       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3297     },
3298     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3299       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3300       .access = PL0_RW,
3301       .type = ARM_CP_IO,
3302       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3303       .resetvalue = 0, .accessfn = gt_vtimer_access,
3304       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3305       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3306     },
3307     /*
3308      * Secure timer -- this is actually restricted to only EL3
3309      * and configurably Secure-EL1 via the accessfn.
3310      */
3311     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3312       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3313       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3314       .accessfn = gt_stimer_access,
3315       .readfn = gt_sec_tval_read,
3316       .writefn = gt_sec_tval_write,
3317       .resetfn = gt_sec_timer_reset,
3318     },
3319     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3320       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3321       .type = ARM_CP_IO, .access = PL1_RW,
3322       .accessfn = gt_stimer_access,
3323       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3324       .resetvalue = 0,
3325       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3326     },
3327     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3328       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3329       .type = ARM_CP_IO, .access = PL1_RW,
3330       .accessfn = gt_stimer_access,
3331       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3332       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3333     },
3334 };
3335 
3336 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3337                                  bool isread)
3338 {
3339     if (arm_current_el(env) == 1) {
3340         /* This must be a FEAT_NV access */
3341         /* TODO: FEAT_ECV will need to check CNTHCTL_EL2 here */
3342         return CP_ACCESS_OK;
3343     }
3344     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3345         return CP_ACCESS_TRAP;
3346     }
3347     return CP_ACCESS_OK;
3348 }
3349 
3350 #else
3351 
3352 /*
3353  * In user-mode most of the generic timer registers are inaccessible
3354  * however modern kernels (4.12+) allow access to cntvct_el0
3355  */
3356 
3357 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3358 {
3359     ARMCPU *cpu = env_archcpu(env);
3360 
3361     /*
3362      * Currently we have no support for QEMUTimer in linux-user so we
3363      * can't call gt_get_countervalue(env), instead we directly
3364      * call the lower level functions.
3365      */
3366     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3367 }
3368 
3369 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3370     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3371       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3372       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3373       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3374       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3375     },
3376     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3377       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3378       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3379       .readfn = gt_virt_cnt_read,
3380     },
3381 };
3382 
3383 #endif
3384 
3385 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3386 {
3387     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3388         raw_write(env, ri, value);
3389     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3390         raw_write(env, ri, value & 0xfffff6ff);
3391     } else {
3392         raw_write(env, ri, value & 0xfffff1ff);
3393     }
3394 }
3395 
3396 #ifndef CONFIG_USER_ONLY
3397 /* get_phys_addr() isn't present for user-mode-only targets */
3398 
3399 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3400                                  bool isread)
3401 {
3402     if (ri->opc2 & 4) {
3403         /*
3404          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3405          * Secure EL1 (which can only happen if EL3 is AArch64).
3406          * They are simply UNDEF if executed from NS EL1.
3407          * They function normally from EL2 or EL3.
3408          */
3409         if (arm_current_el(env) == 1) {
3410             if (arm_is_secure_below_el3(env)) {
3411                 if (env->cp15.scr_el3 & SCR_EEL2) {
3412                     return CP_ACCESS_TRAP_EL2;
3413                 }
3414                 return CP_ACCESS_TRAP_EL3;
3415             }
3416             return CP_ACCESS_TRAP_UNCATEGORIZED;
3417         }
3418     }
3419     return CP_ACCESS_OK;
3420 }
3421 
3422 #ifdef CONFIG_TCG
3423 static int par_el1_shareability(GetPhysAddrResult *res)
3424 {
3425     /*
3426      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3427      * memory -- see pseudocode PAREncodeShareability().
3428      */
3429     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3430         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3431         return 2;
3432     }
3433     return res->cacheattrs.shareability;
3434 }
3435 
3436 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3437                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3438                              ARMSecuritySpace ss)
3439 {
3440     bool ret;
3441     uint64_t par64;
3442     bool format64 = false;
3443     ARMMMUFaultInfo fi = {};
3444     GetPhysAddrResult res = {};
3445 
3446     /*
3447      * I_MXTJT: Granule protection checks are not performed on the final address
3448      * of a successful translation.
3449      */
3450     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3451                                          &res, &fi);
3452 
3453     /*
3454      * ATS operations only do S1 or S1+S2 translations, so we never
3455      * have to deal with the ARMCacheAttrs format for S2 only.
3456      */
3457     assert(!res.cacheattrs.is_s2_format);
3458 
3459     if (ret) {
3460         /*
3461          * Some kinds of translation fault must cause exceptions rather
3462          * than being reported in the PAR.
3463          */
3464         int current_el = arm_current_el(env);
3465         int target_el;
3466         uint32_t syn, fsr, fsc;
3467         bool take_exc = false;
3468 
3469         if (fi.s1ptw && current_el == 1
3470             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3471             /*
3472              * Synchronous stage 2 fault on an access made as part of the
3473              * translation table walk for AT S1E0* or AT S1E1* insn
3474              * executed from NS EL1. If this is a synchronous external abort
3475              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3476              * to EL3. Otherwise the fault is taken as an exception to EL2,
3477              * and HPFAR_EL2 holds the faulting IPA.
3478              */
3479             if (fi.type == ARMFault_SyncExternalOnWalk &&
3480                 (env->cp15.scr_el3 & SCR_EA)) {
3481                 target_el = 3;
3482             } else {
3483                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3484                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3485                     env->cp15.hpfar_el2 |= HPFAR_NS;
3486                 }
3487                 target_el = 2;
3488             }
3489             take_exc = true;
3490         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3491             /*
3492              * Synchronous external aborts during a translation table walk
3493              * are taken as Data Abort exceptions.
3494              */
3495             if (fi.stage2) {
3496                 if (current_el == 3) {
3497                     target_el = 3;
3498                 } else {
3499                     target_el = 2;
3500                 }
3501             } else {
3502                 target_el = exception_target_el(env);
3503             }
3504             take_exc = true;
3505         }
3506 
3507         if (take_exc) {
3508             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3509             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3510                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3511                 fsr = arm_fi_to_lfsc(&fi);
3512                 fsc = extract32(fsr, 0, 6);
3513             } else {
3514                 fsr = arm_fi_to_sfsc(&fi);
3515                 fsc = 0x3f;
3516             }
3517             /*
3518              * Report exception with ESR indicating a fault due to a
3519              * translation table walk for a cache maintenance instruction.
3520              */
3521             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3522                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3523             env->exception.vaddress = value;
3524             env->exception.fsr = fsr;
3525             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3526         }
3527     }
3528 
3529     if (is_a64(env)) {
3530         format64 = true;
3531     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3532         /*
3533          * ATS1Cxx:
3534          * * TTBCR.EAE determines whether the result is returned using the
3535          *   32-bit or the 64-bit PAR format
3536          * * Instructions executed in Hyp mode always use the 64bit format
3537          *
3538          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3539          * * The Non-secure TTBCR.EAE bit is set to 1
3540          * * The implementation includes EL2, and the value of HCR.VM is 1
3541          *
3542          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3543          *
3544          * ATS1Hx always uses the 64bit format.
3545          */
3546         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3547 
3548         if (arm_feature(env, ARM_FEATURE_EL2)) {
3549             if (mmu_idx == ARMMMUIdx_E10_0 ||
3550                 mmu_idx == ARMMMUIdx_E10_1 ||
3551                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3552                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3553             } else {
3554                 format64 |= arm_current_el(env) == 2;
3555             }
3556         }
3557     }
3558 
3559     if (format64) {
3560         /* Create a 64-bit PAR */
3561         par64 = (1 << 11); /* LPAE bit always set */
3562         if (!ret) {
3563             par64 |= res.f.phys_addr & ~0xfffULL;
3564             if (!res.f.attrs.secure) {
3565                 par64 |= (1 << 9); /* NS */
3566             }
3567             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3568             par64 |= par_el1_shareability(&res) << 7; /* SH */
3569         } else {
3570             uint32_t fsr = arm_fi_to_lfsc(&fi);
3571 
3572             par64 |= 1; /* F */
3573             par64 |= (fsr & 0x3f) << 1; /* FS */
3574             if (fi.stage2) {
3575                 par64 |= (1 << 9); /* S */
3576             }
3577             if (fi.s1ptw) {
3578                 par64 |= (1 << 8); /* PTW */
3579             }
3580         }
3581     } else {
3582         /*
3583          * fsr is a DFSR/IFSR value for the short descriptor
3584          * translation table format (with WnR always clear).
3585          * Convert it to a 32-bit PAR.
3586          */
3587         if (!ret) {
3588             /* We do not set any attribute bits in the PAR */
3589             if (res.f.lg_page_size == 24
3590                 && arm_feature(env, ARM_FEATURE_V7)) {
3591                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3592             } else {
3593                 par64 = res.f.phys_addr & 0xfffff000;
3594             }
3595             if (!res.f.attrs.secure) {
3596                 par64 |= (1 << 9); /* NS */
3597             }
3598         } else {
3599             uint32_t fsr = arm_fi_to_sfsc(&fi);
3600 
3601             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3602                     ((fsr & 0xf) << 1) | 1;
3603         }
3604     }
3605     return par64;
3606 }
3607 #endif /* CONFIG_TCG */
3608 
3609 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3610 {
3611 #ifdef CONFIG_TCG
3612     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3613     uint64_t par64;
3614     ARMMMUIdx mmu_idx;
3615     int el = arm_current_el(env);
3616     ARMSecuritySpace ss = arm_security_space(env);
3617 
3618     switch (ri->opc2 & 6) {
3619     case 0:
3620         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3621         switch (el) {
3622         case 3:
3623             mmu_idx = ARMMMUIdx_E3;
3624             break;
3625         case 2:
3626             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3627             /* fall through */
3628         case 1:
3629             if (ri->crm == 9 && arm_pan_enabled(env)) {
3630                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3631             } else {
3632                 mmu_idx = ARMMMUIdx_Stage1_E1;
3633             }
3634             break;
3635         default:
3636             g_assert_not_reached();
3637         }
3638         break;
3639     case 2:
3640         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3641         switch (el) {
3642         case 3:
3643             mmu_idx = ARMMMUIdx_E10_0;
3644             break;
3645         case 2:
3646             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3647             mmu_idx = ARMMMUIdx_Stage1_E0;
3648             break;
3649         case 1:
3650             mmu_idx = ARMMMUIdx_Stage1_E0;
3651             break;
3652         default:
3653             g_assert_not_reached();
3654         }
3655         break;
3656     case 4:
3657         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3658         mmu_idx = ARMMMUIdx_E10_1;
3659         ss = ARMSS_NonSecure;
3660         break;
3661     case 6:
3662         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3663         mmu_idx = ARMMMUIdx_E10_0;
3664         ss = ARMSS_NonSecure;
3665         break;
3666     default:
3667         g_assert_not_reached();
3668     }
3669 
3670     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3671 
3672     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3673 #else
3674     /* Handled by hardware accelerator. */
3675     g_assert_not_reached();
3676 #endif /* CONFIG_TCG */
3677 }
3678 
3679 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3680                         uint64_t value)
3681 {
3682 #ifdef CONFIG_TCG
3683     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3684     uint64_t par64;
3685 
3686     /* There is no SecureEL2 for AArch32. */
3687     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3688                          ARMSS_NonSecure);
3689 
3690     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3691 #else
3692     /* Handled by hardware accelerator. */
3693     g_assert_not_reached();
3694 #endif /* CONFIG_TCG */
3695 }
3696 
3697 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3698                                      bool isread)
3699 {
3700     /*
3701      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3702      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3703      * only happen when executing at EL3 because that combination also causes an
3704      * illegal exception return. We don't need to check FEAT_RME either, because
3705      * scr_write() ensures that the NSE bit is not set otherwise.
3706      */
3707     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3708         return CP_ACCESS_TRAP;
3709     }
3710     return CP_ACCESS_OK;
3711 }
3712 
3713 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3714                                      bool isread)
3715 {
3716     if (arm_current_el(env) == 3 &&
3717         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3718         return CP_ACCESS_TRAP;
3719     }
3720     return at_e012_access(env, ri, isread);
3721 }
3722 
3723 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3724                                       bool isread)
3725 {
3726     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3727         return CP_ACCESS_TRAP_EL2;
3728     }
3729     return at_e012_access(env, ri, isread);
3730 }
3731 
3732 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3733                         uint64_t value)
3734 {
3735 #ifdef CONFIG_TCG
3736     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3737     ARMMMUIdx mmu_idx;
3738     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3739     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3740 
3741     switch (ri->opc2 & 6) {
3742     case 0:
3743         switch (ri->opc1) {
3744         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3745             if (ri->crm == 9 && arm_pan_enabled(env)) {
3746                 mmu_idx = regime_e20 ?
3747                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3748             } else {
3749                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3750             }
3751             break;
3752         case 4: /* AT S1E2R, AT S1E2W */
3753             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3754             break;
3755         case 6: /* AT S1E3R, AT S1E3W */
3756             mmu_idx = ARMMMUIdx_E3;
3757             break;
3758         default:
3759             g_assert_not_reached();
3760         }
3761         break;
3762     case 2: /* AT S1E0R, AT S1E0W */
3763         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3764         break;
3765     case 4: /* AT S12E1R, AT S12E1W */
3766         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3767         break;
3768     case 6: /* AT S12E0R, AT S12E0W */
3769         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3770         break;
3771     default:
3772         g_assert_not_reached();
3773     }
3774 
3775     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3776                                        mmu_idx, arm_security_space(env));
3777 #else
3778     /* Handled by hardware accelerator. */
3779     g_assert_not_reached();
3780 #endif /* CONFIG_TCG */
3781 }
3782 #endif
3783 
3784 /* Return basic MPU access permission bits.  */
3785 static uint32_t simple_mpu_ap_bits(uint32_t val)
3786 {
3787     uint32_t ret;
3788     uint32_t mask;
3789     int i;
3790     ret = 0;
3791     mask = 3;
3792     for (i = 0; i < 16; i += 2) {
3793         ret |= (val >> i) & mask;
3794         mask <<= 2;
3795     }
3796     return ret;
3797 }
3798 
3799 /* Pad basic MPU access permission bits to extended format.  */
3800 static uint32_t extended_mpu_ap_bits(uint32_t val)
3801 {
3802     uint32_t ret;
3803     uint32_t mask;
3804     int i;
3805     ret = 0;
3806     mask = 3;
3807     for (i = 0; i < 16; i += 2) {
3808         ret |= (val & mask) << i;
3809         mask <<= 2;
3810     }
3811     return ret;
3812 }
3813 
3814 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3815                                  uint64_t value)
3816 {
3817     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3818 }
3819 
3820 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3821 {
3822     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3823 }
3824 
3825 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3826                                  uint64_t value)
3827 {
3828     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3829 }
3830 
3831 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3832 {
3833     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3834 }
3835 
3836 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3837 {
3838     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3839 
3840     if (!u32p) {
3841         return 0;
3842     }
3843 
3844     u32p += env->pmsav7.rnr[M_REG_NS];
3845     return *u32p;
3846 }
3847 
3848 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3849                          uint64_t value)
3850 {
3851     ARMCPU *cpu = env_archcpu(env);
3852     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3853 
3854     if (!u32p) {
3855         return;
3856     }
3857 
3858     u32p += env->pmsav7.rnr[M_REG_NS];
3859     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3860     *u32p = value;
3861 }
3862 
3863 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3864                               uint64_t value)
3865 {
3866     ARMCPU *cpu = env_archcpu(env);
3867     uint32_t nrgs = cpu->pmsav7_dregion;
3868 
3869     if (value >= nrgs) {
3870         qemu_log_mask(LOG_GUEST_ERROR,
3871                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3872                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3873         return;
3874     }
3875 
3876     raw_write(env, ri, value);
3877 }
3878 
3879 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3880                           uint64_t value)
3881 {
3882     ARMCPU *cpu = env_archcpu(env);
3883 
3884     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3885     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3886 }
3887 
3888 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3889 {
3890     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3891 }
3892 
3893 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3894                           uint64_t value)
3895 {
3896     ARMCPU *cpu = env_archcpu(env);
3897 
3898     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3899     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3900 }
3901 
3902 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3903 {
3904     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3905 }
3906 
3907 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3908                            uint64_t value)
3909 {
3910     ARMCPU *cpu = env_archcpu(env);
3911 
3912     /*
3913      * Ignore writes that would select not implemented region.
3914      * This is architecturally UNPREDICTABLE.
3915      */
3916     if (value >= cpu->pmsav7_dregion) {
3917         return;
3918     }
3919 
3920     env->pmsav7.rnr[M_REG_NS] = value;
3921 }
3922 
3923 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3924                           uint64_t value)
3925 {
3926     ARMCPU *cpu = env_archcpu(env);
3927 
3928     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3929     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3930 }
3931 
3932 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3933 {
3934     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3935 }
3936 
3937 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3938                           uint64_t value)
3939 {
3940     ARMCPU *cpu = env_archcpu(env);
3941 
3942     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3943     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3944 }
3945 
3946 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3947 {
3948     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3949 }
3950 
3951 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3952                           uint64_t value)
3953 {
3954     uint32_t n;
3955     uint32_t bit;
3956     ARMCPU *cpu = env_archcpu(env);
3957 
3958     /* Ignore writes to unimplemented regions */
3959     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3960     value &= MAKE_64BIT_MASK(0, rmax);
3961 
3962     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3963 
3964     /* Register alias is only valid for first 32 indexes */
3965     for (n = 0; n < rmax; ++n) {
3966         bit = extract32(value, n, 1);
3967         env->pmsav8.hprlar[n] = deposit32(
3968                     env->pmsav8.hprlar[n], 0, 1, bit);
3969     }
3970 }
3971 
3972 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3973 {
3974     uint32_t n;
3975     uint32_t result = 0x0;
3976     ARMCPU *cpu = env_archcpu(env);
3977 
3978     /* Register alias is only valid for first 32 indexes */
3979     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3980         if (env->pmsav8.hprlar[n] & 0x1) {
3981             result |= (0x1 << n);
3982         }
3983     }
3984     return result;
3985 }
3986 
3987 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3988                            uint64_t value)
3989 {
3990     ARMCPU *cpu = env_archcpu(env);
3991 
3992     /*
3993      * Ignore writes that would select not implemented region.
3994      * This is architecturally UNPREDICTABLE.
3995      */
3996     if (value >= cpu->pmsav8r_hdregion) {
3997         return;
3998     }
3999 
4000     env->pmsav8.hprselr = value;
4001 }
4002 
4003 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4004                           uint64_t value)
4005 {
4006     ARMCPU *cpu = env_archcpu(env);
4007     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4008                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4009 
4010     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4011 
4012     if (ri->opc1 & 4) {
4013         if (index >= cpu->pmsav8r_hdregion) {
4014             return;
4015         }
4016         if (ri->opc2 & 0x1) {
4017             env->pmsav8.hprlar[index] = value;
4018         } else {
4019             env->pmsav8.hprbar[index] = value;
4020         }
4021     } else {
4022         if (index >= cpu->pmsav7_dregion) {
4023             return;
4024         }
4025         if (ri->opc2 & 0x1) {
4026             env->pmsav8.rlar[M_REG_NS][index] = value;
4027         } else {
4028             env->pmsav8.rbar[M_REG_NS][index] = value;
4029         }
4030     }
4031 }
4032 
4033 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4034 {
4035     ARMCPU *cpu = env_archcpu(env);
4036     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4037                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4038 
4039     if (ri->opc1 & 4) {
4040         if (index >= cpu->pmsav8r_hdregion) {
4041             return 0x0;
4042         }
4043         if (ri->opc2 & 0x1) {
4044             return env->pmsav8.hprlar[index];
4045         } else {
4046             return env->pmsav8.hprbar[index];
4047         }
4048     } else {
4049         if (index >= cpu->pmsav7_dregion) {
4050             return 0x0;
4051         }
4052         if (ri->opc2 & 0x1) {
4053             return env->pmsav8.rlar[M_REG_NS][index];
4054         } else {
4055             return env->pmsav8.rbar[M_REG_NS][index];
4056         }
4057     }
4058 }
4059 
4060 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4061     { .name = "PRBAR",
4062       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4063       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4064       .accessfn = access_tvm_trvm,
4065       .readfn = prbar_read, .writefn = prbar_write },
4066     { .name = "PRLAR",
4067       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4068       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4069       .accessfn = access_tvm_trvm,
4070       .readfn = prlar_read, .writefn = prlar_write },
4071     { .name = "PRSELR", .resetvalue = 0,
4072       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4073       .access = PL1_RW, .accessfn = access_tvm_trvm,
4074       .writefn = prselr_write,
4075       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4076     { .name = "HPRBAR", .resetvalue = 0,
4077       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4078       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4079       .readfn = hprbar_read, .writefn = hprbar_write },
4080     { .name = "HPRLAR",
4081       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4082       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4083       .readfn = hprlar_read, .writefn = hprlar_write },
4084     { .name = "HPRSELR", .resetvalue = 0,
4085       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4086       .access = PL2_RW,
4087       .writefn = hprselr_write,
4088       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4089     { .name = "HPRENR",
4090       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4091       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4092       .readfn = hprenr_read, .writefn = hprenr_write },
4093 };
4094 
4095 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4096     /*
4097      * Reset for all these registers is handled in arm_cpu_reset(),
4098      * because the PMSAv7 is also used by M-profile CPUs, which do
4099      * not register cpregs but still need the state to be reset.
4100      */
4101     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4102       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4103       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4104       .readfn = pmsav7_read, .writefn = pmsav7_write,
4105       .resetfn = arm_cp_reset_ignore },
4106     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4107       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4108       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4109       .readfn = pmsav7_read, .writefn = pmsav7_write,
4110       .resetfn = arm_cp_reset_ignore },
4111     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4112       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4113       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4114       .readfn = pmsav7_read, .writefn = pmsav7_write,
4115       .resetfn = arm_cp_reset_ignore },
4116     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4117       .access = PL1_RW,
4118       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4119       .writefn = pmsav7_rgnr_write,
4120       .resetfn = arm_cp_reset_ignore },
4121 };
4122 
4123 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4124     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4125       .access = PL1_RW, .type = ARM_CP_ALIAS,
4126       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4127       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4128     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4129       .access = PL1_RW, .type = ARM_CP_ALIAS,
4130       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4131       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4132     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4133       .access = PL1_RW,
4134       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4135       .resetvalue = 0, },
4136     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4137       .access = PL1_RW,
4138       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4139       .resetvalue = 0, },
4140     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4141       .access = PL1_RW,
4142       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4143     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4144       .access = PL1_RW,
4145       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4146     /* Protection region base and size registers */
4147     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4148       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4149       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4150     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4151       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4152       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4153     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4154       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4155       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4156     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4157       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4158       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4159     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4160       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4161       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4162     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4163       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4164       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4165     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4166       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4167       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4168     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4169       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4170       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4171 };
4172 
4173 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4174                              uint64_t value)
4175 {
4176     ARMCPU *cpu = env_archcpu(env);
4177 
4178     if (!arm_feature(env, ARM_FEATURE_V8)) {
4179         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4180             /*
4181              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4182              * using Long-descriptor translation table format
4183              */
4184             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4185         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4186             /*
4187              * In an implementation that includes the Security Extensions
4188              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4189              * Short-descriptor translation table format.
4190              */
4191             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4192         } else {
4193             value &= TTBCR_N;
4194         }
4195     }
4196 
4197     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4198         /*
4199          * With LPAE the TTBCR could result in a change of ASID
4200          * via the TTBCR.A1 bit, so do a TLB flush.
4201          */
4202         tlb_flush(CPU(cpu));
4203     }
4204     raw_write(env, ri, value);
4205 }
4206 
4207 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4208                                uint64_t value)
4209 {
4210     ARMCPU *cpu = env_archcpu(env);
4211 
4212     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4213     tlb_flush(CPU(cpu));
4214     raw_write(env, ri, value);
4215 }
4216 
4217 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4218                             uint64_t value)
4219 {
4220     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4221     if (cpreg_field_is_64bit(ri) &&
4222         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4223         ARMCPU *cpu = env_archcpu(env);
4224         tlb_flush(CPU(cpu));
4225     }
4226     raw_write(env, ri, value);
4227 }
4228 
4229 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4230                                     uint64_t value)
4231 {
4232     /*
4233      * If we are running with E2&0 regime, then an ASID is active.
4234      * Flush if that might be changing.  Note we're not checking
4235      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4236      * holds the active ASID, only checking the field that might.
4237      */
4238     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4239         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4240         uint16_t mask = ARMMMUIdxBit_E20_2 |
4241                         ARMMMUIdxBit_E20_2_PAN |
4242                         ARMMMUIdxBit_E20_0;
4243         tlb_flush_by_mmuidx(env_cpu(env), mask);
4244     }
4245     raw_write(env, ri, value);
4246 }
4247 
4248 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4249                         uint64_t value)
4250 {
4251     ARMCPU *cpu = env_archcpu(env);
4252     CPUState *cs = CPU(cpu);
4253 
4254     /*
4255      * A change in VMID to the stage2 page table (Stage2) invalidates
4256      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4257      */
4258     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4259         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4260     }
4261     raw_write(env, ri, value);
4262 }
4263 
4264 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4265     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4266       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4267       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4268                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4269     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4270       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4271       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4272                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4273     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4274       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4275       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4276                              offsetof(CPUARMState, cp15.dfar_ns) } },
4277     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4278       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4279       .access = PL1_RW, .accessfn = access_tvm_trvm,
4280       .fgt = FGT_FAR_EL1,
4281       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4282       .resetvalue = 0, },
4283 };
4284 
4285 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4286     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4287       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4288       .access = PL1_RW, .accessfn = access_tvm_trvm,
4289       .fgt = FGT_ESR_EL1,
4290       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4291     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4292       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4293       .access = PL1_RW, .accessfn = access_tvm_trvm,
4294       .fgt = FGT_TTBR0_EL1,
4295       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4296       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4297                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4298     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4299       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4300       .access = PL1_RW, .accessfn = access_tvm_trvm,
4301       .fgt = FGT_TTBR1_EL1,
4302       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4303       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4304                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4305     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4306       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4307       .access = PL1_RW, .accessfn = access_tvm_trvm,
4308       .fgt = FGT_TCR_EL1,
4309       .writefn = vmsa_tcr_el12_write,
4310       .raw_writefn = raw_write,
4311       .resetvalue = 0,
4312       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4313     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4314       .access = PL1_RW, .accessfn = access_tvm_trvm,
4315       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4316       .raw_writefn = raw_write,
4317       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4318                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4319 };
4320 
4321 /*
4322  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4323  * qemu tlbs nor adjusting cached masks.
4324  */
4325 static const ARMCPRegInfo ttbcr2_reginfo = {
4326     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4327     .access = PL1_RW, .accessfn = access_tvm_trvm,
4328     .type = ARM_CP_ALIAS,
4329     .bank_fieldoffsets = {
4330         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4331         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4332     },
4333 };
4334 
4335 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4336                                 uint64_t value)
4337 {
4338     env->cp15.c15_ticonfig = value & 0xe7;
4339     /* The OS_TYPE bit in this register changes the reported CPUID! */
4340     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4341         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4342 }
4343 
4344 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4345                                 uint64_t value)
4346 {
4347     env->cp15.c15_threadid = value & 0xffff;
4348 }
4349 
4350 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4351                            uint64_t value)
4352 {
4353     /* Wait-for-interrupt (deprecated) */
4354     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4355 }
4356 
4357 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4358                                   uint64_t value)
4359 {
4360     /*
4361      * On OMAP there are registers indicating the max/min index of dcache lines
4362      * containing a dirty line; cache flush operations have to reset these.
4363      */
4364     env->cp15.c15_i_max = 0x000;
4365     env->cp15.c15_i_min = 0xff0;
4366 }
4367 
4368 static const ARMCPRegInfo omap_cp_reginfo[] = {
4369     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4370       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4371       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4372       .resetvalue = 0, },
4373     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4374       .access = PL1_RW, .type = ARM_CP_NOP },
4375     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4376       .access = PL1_RW,
4377       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4378       .writefn = omap_ticonfig_write },
4379     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4380       .access = PL1_RW,
4381       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4382     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4383       .access = PL1_RW, .resetvalue = 0xff0,
4384       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4385     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4386       .access = PL1_RW,
4387       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4388       .writefn = omap_threadid_write },
4389     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4390       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4391       .type = ARM_CP_NO_RAW,
4392       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4393     /*
4394      * TODO: Peripheral port remap register:
4395      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4396      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4397      * when MMU is off.
4398      */
4399     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4400       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4401       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4402       .writefn = omap_cachemaint_write },
4403     { .name = "C9", .cp = 15, .crn = 9,
4404       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4405       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4406 };
4407 
4408 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4409                               uint64_t value)
4410 {
4411     env->cp15.c15_cpar = value & 0x3fff;
4412 }
4413 
4414 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4415     { .name = "XSCALE_CPAR",
4416       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4417       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4418       .writefn = xscale_cpar_write, },
4419     { .name = "XSCALE_AUXCR",
4420       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4421       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4422       .resetvalue = 0, },
4423     /*
4424      * XScale specific cache-lockdown: since we have no cache we NOP these
4425      * and hope the guest does not really rely on cache behaviour.
4426      */
4427     { .name = "XSCALE_LOCK_ICACHE_LINE",
4428       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4429       .access = PL1_W, .type = ARM_CP_NOP },
4430     { .name = "XSCALE_UNLOCK_ICACHE",
4431       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4432       .access = PL1_W, .type = ARM_CP_NOP },
4433     { .name = "XSCALE_DCACHE_LOCK",
4434       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4435       .access = PL1_RW, .type = ARM_CP_NOP },
4436     { .name = "XSCALE_UNLOCK_DCACHE",
4437       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4438       .access = PL1_W, .type = ARM_CP_NOP },
4439 };
4440 
4441 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4442     /*
4443      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4444      * implementation of this implementation-defined space.
4445      * Ideally this should eventually disappear in favour of actually
4446      * implementing the correct behaviour for all cores.
4447      */
4448     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4449       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4450       .access = PL1_RW,
4451       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4452       .resetvalue = 0 },
4453 };
4454 
4455 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4456     /* Cache status: RAZ because we have no cache so it's always clean */
4457     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4458       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4459       .resetvalue = 0 },
4460 };
4461 
4462 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4463     /* We never have a block transfer operation in progress */
4464     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4465       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4466       .resetvalue = 0 },
4467     /* The cache ops themselves: these all NOP for QEMU */
4468     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4469       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4470     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4471       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4472     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4473       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4474     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4475       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4476     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4477       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4478     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4479       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4480 };
4481 
4482 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4483     /*
4484      * The cache test-and-clean instructions always return (1 << 30)
4485      * to indicate that there are no dirty cache lines.
4486      */
4487     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4488       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4489       .resetvalue = (1 << 30) },
4490     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4491       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4492       .resetvalue = (1 << 30) },
4493 };
4494 
4495 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4496     /* Ignore ReadBuffer accesses */
4497     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4498       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4499       .access = PL1_RW, .resetvalue = 0,
4500       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4501 };
4502 
4503 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4504 {
4505     unsigned int cur_el = arm_current_el(env);
4506 
4507     if (arm_is_el2_enabled(env) && cur_el == 1) {
4508         return env->cp15.vpidr_el2;
4509     }
4510     return raw_read(env, ri);
4511 }
4512 
4513 static uint64_t mpidr_read_val(CPUARMState *env)
4514 {
4515     ARMCPU *cpu = env_archcpu(env);
4516     uint64_t mpidr = cpu->mp_affinity;
4517 
4518     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4519         mpidr |= (1U << 31);
4520         /*
4521          * Cores which are uniprocessor (non-coherent)
4522          * but still implement the MP extensions set
4523          * bit 30. (For instance, Cortex-R5).
4524          */
4525         if (cpu->mp_is_up) {
4526             mpidr |= (1u << 30);
4527         }
4528     }
4529     return mpidr;
4530 }
4531 
4532 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4533 {
4534     unsigned int cur_el = arm_current_el(env);
4535 
4536     if (arm_is_el2_enabled(env) && cur_el == 1) {
4537         return env->cp15.vmpidr_el2;
4538     }
4539     return mpidr_read_val(env);
4540 }
4541 
4542 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4543     /* NOP AMAIR0/1 */
4544     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4545       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4546       .access = PL1_RW, .accessfn = access_tvm_trvm,
4547       .fgt = FGT_AMAIR_EL1,
4548       .type = ARM_CP_CONST, .resetvalue = 0 },
4549     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4550     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4551       .access = PL1_RW, .accessfn = access_tvm_trvm,
4552       .type = ARM_CP_CONST, .resetvalue = 0 },
4553     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4554       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4555       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4556                              offsetof(CPUARMState, cp15.par_ns)} },
4557     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4558       .access = PL1_RW, .accessfn = access_tvm_trvm,
4559       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4560       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4561                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4562       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4563     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4564       .access = PL1_RW, .accessfn = access_tvm_trvm,
4565       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4566       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4567                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4568       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4569 };
4570 
4571 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4572 {
4573     return vfp_get_fpcr(env);
4574 }
4575 
4576 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4577                             uint64_t value)
4578 {
4579     vfp_set_fpcr(env, value);
4580 }
4581 
4582 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4583 {
4584     return vfp_get_fpsr(env);
4585 }
4586 
4587 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4588                             uint64_t value)
4589 {
4590     vfp_set_fpsr(env, value);
4591 }
4592 
4593 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4594                                        bool isread)
4595 {
4596     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4597         return CP_ACCESS_TRAP;
4598     }
4599     return CP_ACCESS_OK;
4600 }
4601 
4602 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4603                             uint64_t value)
4604 {
4605     env->daif = value & PSTATE_DAIF;
4606 }
4607 
4608 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4609 {
4610     return env->pstate & PSTATE_PAN;
4611 }
4612 
4613 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4614                            uint64_t value)
4615 {
4616     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4617 }
4618 
4619 static const ARMCPRegInfo pan_reginfo = {
4620     .name = "PAN", .state = ARM_CP_STATE_AA64,
4621     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4622     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4623     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4624 };
4625 
4626 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4627 {
4628     return env->pstate & PSTATE_UAO;
4629 }
4630 
4631 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4632                            uint64_t value)
4633 {
4634     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4635 }
4636 
4637 static const ARMCPRegInfo uao_reginfo = {
4638     .name = "UAO", .state = ARM_CP_STATE_AA64,
4639     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4640     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4641     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4642 };
4643 
4644 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4645 {
4646     return env->pstate & PSTATE_DIT;
4647 }
4648 
4649 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4650                            uint64_t value)
4651 {
4652     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4653 }
4654 
4655 static const ARMCPRegInfo dit_reginfo = {
4656     .name = "DIT", .state = ARM_CP_STATE_AA64,
4657     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4658     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4659     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4660 };
4661 
4662 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4663 {
4664     return env->pstate & PSTATE_SSBS;
4665 }
4666 
4667 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4668                            uint64_t value)
4669 {
4670     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4671 }
4672 
4673 static const ARMCPRegInfo ssbs_reginfo = {
4674     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4675     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4676     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4677     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4678 };
4679 
4680 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4681                                               const ARMCPRegInfo *ri,
4682                                               bool isread)
4683 {
4684     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4685     switch (arm_current_el(env)) {
4686     case 0:
4687         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4688         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4689             return CP_ACCESS_TRAP;
4690         }
4691         /* fall through */
4692     case 1:
4693         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4694         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4695             return CP_ACCESS_TRAP_EL2;
4696         }
4697         break;
4698     }
4699     return CP_ACCESS_OK;
4700 }
4701 
4702 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4703 {
4704     /* Cache invalidate/clean to Point of Unification... */
4705     switch (arm_current_el(env)) {
4706     case 0:
4707         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4708         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4709             return CP_ACCESS_TRAP;
4710         }
4711         /* fall through */
4712     case 1:
4713         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4714         if (arm_hcr_el2_eff(env) & hcrflags) {
4715             return CP_ACCESS_TRAP_EL2;
4716         }
4717         break;
4718     }
4719     return CP_ACCESS_OK;
4720 }
4721 
4722 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4723                                    bool isread)
4724 {
4725     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4726 }
4727 
4728 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4729                                   bool isread)
4730 {
4731     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4732 }
4733 
4734 /*
4735  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4736  * Page D4-1736 (DDI0487A.b)
4737  */
4738 
4739 static int vae1_tlbmask(CPUARMState *env)
4740 {
4741     uint64_t hcr = arm_hcr_el2_eff(env);
4742     uint16_t mask;
4743 
4744     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4745         mask = ARMMMUIdxBit_E20_2 |
4746                ARMMMUIdxBit_E20_2_PAN |
4747                ARMMMUIdxBit_E20_0;
4748     } else {
4749         mask = ARMMMUIdxBit_E10_1 |
4750                ARMMMUIdxBit_E10_1_PAN |
4751                ARMMMUIdxBit_E10_0;
4752     }
4753     return mask;
4754 }
4755 
4756 static int vae2_tlbmask(CPUARMState *env)
4757 {
4758     uint64_t hcr = arm_hcr_el2_eff(env);
4759     uint16_t mask;
4760 
4761     if (hcr & HCR_E2H) {
4762         mask = ARMMMUIdxBit_E20_2 |
4763                ARMMMUIdxBit_E20_2_PAN |
4764                ARMMMUIdxBit_E20_0;
4765     } else {
4766         mask = ARMMMUIdxBit_E2;
4767     }
4768     return mask;
4769 }
4770 
4771 /* Return 56 if TBI is enabled, 64 otherwise. */
4772 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4773                               uint64_t addr)
4774 {
4775     uint64_t tcr = regime_tcr(env, mmu_idx);
4776     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4777     int select = extract64(addr, 55, 1);
4778 
4779     return (tbi >> select) & 1 ? 56 : 64;
4780 }
4781 
4782 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4783 {
4784     uint64_t hcr = arm_hcr_el2_eff(env);
4785     ARMMMUIdx mmu_idx;
4786 
4787     /* Only the regime of the mmu_idx below is significant. */
4788     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4789         mmu_idx = ARMMMUIdx_E20_0;
4790     } else {
4791         mmu_idx = ARMMMUIdx_E10_0;
4792     }
4793 
4794     return tlbbits_for_regime(env, mmu_idx, addr);
4795 }
4796 
4797 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4798 {
4799     uint64_t hcr = arm_hcr_el2_eff(env);
4800     ARMMMUIdx mmu_idx;
4801 
4802     /*
4803      * Only the regime of the mmu_idx below is significant.
4804      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4805      * only has one.
4806      */
4807     if (hcr & HCR_E2H) {
4808         mmu_idx = ARMMMUIdx_E20_2;
4809     } else {
4810         mmu_idx = ARMMMUIdx_E2;
4811     }
4812 
4813     return tlbbits_for_regime(env, mmu_idx, addr);
4814 }
4815 
4816 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4817                                       uint64_t value)
4818 {
4819     CPUState *cs = env_cpu(env);
4820     int mask = vae1_tlbmask(env);
4821 
4822     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4823 }
4824 
4825 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4826                                     uint64_t value)
4827 {
4828     CPUState *cs = env_cpu(env);
4829     int mask = vae1_tlbmask(env);
4830 
4831     if (tlb_force_broadcast(env)) {
4832         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4833     } else {
4834         tlb_flush_by_mmuidx(cs, mask);
4835     }
4836 }
4837 
4838 static int e2_tlbmask(CPUARMState *env)
4839 {
4840     return (ARMMMUIdxBit_E20_0 |
4841             ARMMMUIdxBit_E20_2 |
4842             ARMMMUIdxBit_E20_2_PAN |
4843             ARMMMUIdxBit_E2);
4844 }
4845 
4846 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4847                                   uint64_t value)
4848 {
4849     CPUState *cs = env_cpu(env);
4850     int mask = alle1_tlbmask(env);
4851 
4852     tlb_flush_by_mmuidx(cs, mask);
4853 }
4854 
4855 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4856                                   uint64_t value)
4857 {
4858     CPUState *cs = env_cpu(env);
4859     int mask = e2_tlbmask(env);
4860 
4861     tlb_flush_by_mmuidx(cs, mask);
4862 }
4863 
4864 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4865                                   uint64_t value)
4866 {
4867     ARMCPU *cpu = env_archcpu(env);
4868     CPUState *cs = CPU(cpu);
4869 
4870     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4871 }
4872 
4873 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4874                                     uint64_t value)
4875 {
4876     CPUState *cs = env_cpu(env);
4877     int mask = alle1_tlbmask(env);
4878 
4879     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4880 }
4881 
4882 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4883                                     uint64_t value)
4884 {
4885     CPUState *cs = env_cpu(env);
4886     int mask = e2_tlbmask(env);
4887 
4888     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4889 }
4890 
4891 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4892                                     uint64_t value)
4893 {
4894     CPUState *cs = env_cpu(env);
4895 
4896     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4897 }
4898 
4899 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4900                                  uint64_t value)
4901 {
4902     /*
4903      * Invalidate by VA, EL2
4904      * Currently handles both VAE2 and VALE2, since we don't support
4905      * flush-last-level-only.
4906      */
4907     CPUState *cs = env_cpu(env);
4908     int mask = vae2_tlbmask(env);
4909     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4910     int bits = vae2_tlbbits(env, pageaddr);
4911 
4912     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4913 }
4914 
4915 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4916                                  uint64_t value)
4917 {
4918     /*
4919      * Invalidate by VA, EL3
4920      * Currently handles both VAE3 and VALE3, since we don't support
4921      * flush-last-level-only.
4922      */
4923     ARMCPU *cpu = env_archcpu(env);
4924     CPUState *cs = CPU(cpu);
4925     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4926 
4927     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4928 }
4929 
4930 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4931                                    uint64_t value)
4932 {
4933     CPUState *cs = env_cpu(env);
4934     int mask = vae1_tlbmask(env);
4935     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4936     int bits = vae1_tlbbits(env, pageaddr);
4937 
4938     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4939 }
4940 
4941 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4942                                  uint64_t value)
4943 {
4944     /*
4945      * Invalidate by VA, EL1&0 (AArch64 version).
4946      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4947      * since we don't support flush-for-specific-ASID-only or
4948      * flush-last-level-only.
4949      */
4950     CPUState *cs = env_cpu(env);
4951     int mask = vae1_tlbmask(env);
4952     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4953     int bits = vae1_tlbbits(env, pageaddr);
4954 
4955     if (tlb_force_broadcast(env)) {
4956         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4957     } else {
4958         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4959     }
4960 }
4961 
4962 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4963                                    uint64_t value)
4964 {
4965     CPUState *cs = env_cpu(env);
4966     int mask = vae2_tlbmask(env);
4967     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4968     int bits = vae2_tlbbits(env, pageaddr);
4969 
4970     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4971 }
4972 
4973 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4974                                    uint64_t value)
4975 {
4976     CPUState *cs = env_cpu(env);
4977     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4978     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4979 
4980     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4981                                                   ARMMMUIdxBit_E3, bits);
4982 }
4983 
4984 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4985 {
4986     /*
4987      * The MSB of value is the NS field, which only applies if SEL2
4988      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4989      */
4990     return (value >= 0
4991             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4992             && arm_is_secure_below_el3(env)
4993             ? ARMMMUIdxBit_Stage2_S
4994             : ARMMMUIdxBit_Stage2);
4995 }
4996 
4997 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4998                                     uint64_t value)
4999 {
5000     CPUState *cs = env_cpu(env);
5001     int mask = ipas2e1_tlbmask(env, value);
5002     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5003 
5004     if (tlb_force_broadcast(env)) {
5005         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5006     } else {
5007         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5008     }
5009 }
5010 
5011 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5012                                       uint64_t value)
5013 {
5014     CPUState *cs = env_cpu(env);
5015     int mask = ipas2e1_tlbmask(env, value);
5016     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5017 
5018     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5019 }
5020 
5021 #ifdef TARGET_AARCH64
5022 typedef struct {
5023     uint64_t base;
5024     uint64_t length;
5025 } TLBIRange;
5026 
5027 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5028 {
5029     /*
5030      * Note that the TLBI range TG field encoding differs from both
5031      * TG0 and TG1 encodings.
5032      */
5033     switch (tg) {
5034     case 1:
5035         return Gran4K;
5036     case 2:
5037         return Gran16K;
5038     case 3:
5039         return Gran64K;
5040     default:
5041         return GranInvalid;
5042     }
5043 }
5044 
5045 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5046                                      uint64_t value)
5047 {
5048     unsigned int page_size_granule, page_shift, num, scale, exponent;
5049     /* Extract one bit to represent the va selector in use. */
5050     uint64_t select = sextract64(value, 36, 1);
5051     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5052     TLBIRange ret = { };
5053     ARMGranuleSize gran;
5054 
5055     page_size_granule = extract64(value, 46, 2);
5056     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5057 
5058     /* The granule encoded in value must match the granule in use. */
5059     if (gran != param.gran) {
5060         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5061                       page_size_granule);
5062         return ret;
5063     }
5064 
5065     page_shift = arm_granule_bits(gran);
5066     num = extract64(value, 39, 5);
5067     scale = extract64(value, 44, 2);
5068     exponent = (5 * scale) + 1;
5069 
5070     ret.length = (num + 1) << (exponent + page_shift);
5071 
5072     if (param.select) {
5073         ret.base = sextract64(value, 0, 37);
5074     } else {
5075         ret.base = extract64(value, 0, 37);
5076     }
5077     if (param.ds) {
5078         /*
5079          * With DS=1, BaseADDR is always shifted 16 so that it is able
5080          * to address all 52 va bits.  The input address is perforce
5081          * aligned on a 64k boundary regardless of translation granule.
5082          */
5083         page_shift = 16;
5084     }
5085     ret.base <<= page_shift;
5086 
5087     return ret;
5088 }
5089 
5090 static void do_rvae_write(CPUARMState *env, uint64_t value,
5091                           int idxmap, bool synced)
5092 {
5093     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5094     TLBIRange range;
5095     int bits;
5096 
5097     range = tlbi_aa64_get_range(env, one_idx, value);
5098     bits = tlbbits_for_regime(env, one_idx, range.base);
5099 
5100     if (synced) {
5101         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5102                                                   range.base,
5103                                                   range.length,
5104                                                   idxmap,
5105                                                   bits);
5106     } else {
5107         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5108                                   range.length, idxmap, bits);
5109     }
5110 }
5111 
5112 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5113                                   const ARMCPRegInfo *ri,
5114                                   uint64_t value)
5115 {
5116     /*
5117      * Invalidate by VA range, EL1&0.
5118      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5119      * since we don't support flush-for-specific-ASID-only or
5120      * flush-last-level-only.
5121      */
5122 
5123     do_rvae_write(env, value, vae1_tlbmask(env),
5124                   tlb_force_broadcast(env));
5125 }
5126 
5127 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5128                                     const ARMCPRegInfo *ri,
5129                                     uint64_t value)
5130 {
5131     /*
5132      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5133      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5134      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5135      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5136      * shareable specific flushes.
5137      */
5138 
5139     do_rvae_write(env, value, vae1_tlbmask(env), true);
5140 }
5141 
5142 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5143                                   const ARMCPRegInfo *ri,
5144                                   uint64_t value)
5145 {
5146     /*
5147      * Invalidate by VA range, EL2.
5148      * Currently handles all of RVAE2 and RVALE2,
5149      * since we don't support flush-for-specific-ASID-only or
5150      * flush-last-level-only.
5151      */
5152 
5153     do_rvae_write(env, value, vae2_tlbmask(env),
5154                   tlb_force_broadcast(env));
5155 
5156 
5157 }
5158 
5159 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5160                                     const ARMCPRegInfo *ri,
5161                                     uint64_t value)
5162 {
5163     /*
5164      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5165      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5166      * since we don't support flush-for-specific-ASID-only,
5167      * flush-last-level-only or inner/outer shareable specific flushes.
5168      */
5169 
5170     do_rvae_write(env, value, vae2_tlbmask(env), true);
5171 
5172 }
5173 
5174 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5175                                   const ARMCPRegInfo *ri,
5176                                   uint64_t value)
5177 {
5178     /*
5179      * Invalidate by VA range, EL3.
5180      * Currently handles all of RVAE3 and RVALE3,
5181      * since we don't support flush-for-specific-ASID-only or
5182      * flush-last-level-only.
5183      */
5184 
5185     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5186 }
5187 
5188 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5189                                     const ARMCPRegInfo *ri,
5190                                     uint64_t value)
5191 {
5192     /*
5193      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5194      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5195      * since we don't support flush-for-specific-ASID-only,
5196      * flush-last-level-only or inner/outer specific flushes.
5197      */
5198 
5199     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5200 }
5201 
5202 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5203                                      uint64_t value)
5204 {
5205     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5206                   tlb_force_broadcast(env));
5207 }
5208 
5209 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5210                                        const ARMCPRegInfo *ri,
5211                                        uint64_t value)
5212 {
5213     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5214 }
5215 #endif
5216 
5217 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5218                                       bool isread)
5219 {
5220     int cur_el = arm_current_el(env);
5221 
5222     if (cur_el < 2) {
5223         uint64_t hcr = arm_hcr_el2_eff(env);
5224 
5225         if (cur_el == 0) {
5226             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5227                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5228                     return CP_ACCESS_TRAP_EL2;
5229                 }
5230             } else {
5231                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5232                     return CP_ACCESS_TRAP;
5233                 }
5234                 if (hcr & HCR_TDZ) {
5235                     return CP_ACCESS_TRAP_EL2;
5236                 }
5237             }
5238         } else if (hcr & HCR_TDZ) {
5239             return CP_ACCESS_TRAP_EL2;
5240         }
5241     }
5242     return CP_ACCESS_OK;
5243 }
5244 
5245 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5246 {
5247     ARMCPU *cpu = env_archcpu(env);
5248     int dzp_bit = 1 << 4;
5249 
5250     /* DZP indicates whether DC ZVA access is allowed */
5251     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5252         dzp_bit = 0;
5253     }
5254     return cpu->dcz_blocksize | dzp_bit;
5255 }
5256 
5257 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5258                                     bool isread)
5259 {
5260     if (!(env->pstate & PSTATE_SP)) {
5261         /*
5262          * Access to SP_EL0 is undefined if it's being used as
5263          * the stack pointer.
5264          */
5265         return CP_ACCESS_TRAP_UNCATEGORIZED;
5266     }
5267     return CP_ACCESS_OK;
5268 }
5269 
5270 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5271 {
5272     return env->pstate & PSTATE_SP;
5273 }
5274 
5275 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5276 {
5277     update_spsel(env, val);
5278 }
5279 
5280 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5281                         uint64_t value)
5282 {
5283     ARMCPU *cpu = env_archcpu(env);
5284 
5285     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5286         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5287         value &= ~SCTLR_M;
5288     }
5289 
5290     /* ??? Lots of these bits are not implemented.  */
5291 
5292     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5293         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5294             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5295         } else {
5296             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5297                        SCTLR_ATA0 | SCTLR_ATA);
5298         }
5299     }
5300 
5301     if (raw_read(env, ri) == value) {
5302         /*
5303          * Skip the TLB flush if nothing actually changed; Linux likes
5304          * to do a lot of pointless SCTLR writes.
5305          */
5306         return;
5307     }
5308 
5309     raw_write(env, ri, value);
5310 
5311     /* This may enable/disable the MMU, so do a TLB flush.  */
5312     tlb_flush(CPU(cpu));
5313 
5314     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5315         /*
5316          * Normally we would always end the TB on an SCTLR write; see the
5317          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5318          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5319          * of hflags from the translator, so do it here.
5320          */
5321         arm_rebuild_hflags(env);
5322     }
5323 }
5324 
5325 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5326                            uint64_t value)
5327 {
5328     /*
5329      * Some MDCR_EL3 bits affect whether PMU counters are running:
5330      * if we are trying to change any of those then we must
5331      * bracket this update with PMU start/finish calls.
5332      */
5333     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5334 
5335     if (pmu_op) {
5336         pmu_op_start(env);
5337     }
5338     env->cp15.mdcr_el3 = value;
5339     if (pmu_op) {
5340         pmu_op_finish(env);
5341     }
5342 }
5343 
5344 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5345                        uint64_t value)
5346 {
5347     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5348     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5349 }
5350 
5351 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5352                            uint64_t value)
5353 {
5354     /*
5355      * Some MDCR_EL2 bits affect whether PMU counters are running:
5356      * if we are trying to change any of those then we must
5357      * bracket this update with PMU start/finish calls.
5358      */
5359     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5360 
5361     if (pmu_op) {
5362         pmu_op_start(env);
5363     }
5364     env->cp15.mdcr_el2 = value;
5365     if (pmu_op) {
5366         pmu_op_finish(env);
5367     }
5368 }
5369 
5370 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5371                                  bool isread)
5372 {
5373     if (arm_current_el(env) == 1) {
5374         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5375 
5376         if (hcr_nv == (HCR_NV | HCR_NV1)) {
5377             return CP_ACCESS_TRAP_EL2;
5378         }
5379     }
5380     return CP_ACCESS_OK;
5381 }
5382 
5383 #ifdef CONFIG_USER_ONLY
5384 /*
5385  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5386  * code to get around W^X restrictions, where one region is writable and the
5387  * other is executable.
5388  *
5389  * Since the executable region is never written to we cannot detect code
5390  * changes when running in user mode, and rely on the emulated JIT telling us
5391  * that the code has changed by executing this instruction.
5392  */
5393 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5394                           uint64_t value)
5395 {
5396     uint64_t icache_line_mask, start_address, end_address;
5397     const ARMCPU *cpu;
5398 
5399     cpu = env_archcpu(env);
5400 
5401     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5402     start_address = value & ~icache_line_mask;
5403     end_address = value | icache_line_mask;
5404 
5405     mmap_lock();
5406 
5407     tb_invalidate_phys_range(start_address, end_address);
5408 
5409     mmap_unlock();
5410 }
5411 #endif
5412 
5413 static const ARMCPRegInfo v8_cp_reginfo[] = {
5414     /*
5415      * Minimal set of EL0-visible registers. This will need to be expanded
5416      * significantly for system emulation of AArch64 CPUs.
5417      */
5418     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5419       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5420       .access = PL0_RW, .type = ARM_CP_NZCV },
5421     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5422       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5423       .type = ARM_CP_NO_RAW,
5424       .access = PL0_RW, .accessfn = aa64_daif_access,
5425       .fieldoffset = offsetof(CPUARMState, daif),
5426       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5427     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5428       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5429       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5430       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5431     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5432       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5433       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5434       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5435     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5436       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5437       .access = PL0_R, .type = ARM_CP_NO_RAW,
5438       .fgt = FGT_DCZID_EL0,
5439       .readfn = aa64_dczid_read },
5440     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5441       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5442       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5443 #ifndef CONFIG_USER_ONLY
5444       /* Avoid overhead of an access check that always passes in user-mode */
5445       .accessfn = aa64_zva_access,
5446       .fgt = FGT_DCZVA,
5447 #endif
5448     },
5449     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5450       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5451       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5452     /*
5453      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5454      * don't emulate caches.
5455      */
5456     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5457       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5458       .access = PL1_W, .type = ARM_CP_NOP,
5459       .fgt = FGT_ICIALLUIS,
5460       .accessfn = access_ticab },
5461     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5462       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5463       .access = PL1_W, .type = ARM_CP_NOP,
5464       .fgt = FGT_ICIALLU,
5465       .accessfn = access_tocu },
5466     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5467       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5468       .access = PL0_W,
5469       .fgt = FGT_ICIVAU,
5470       .accessfn = access_tocu,
5471 #ifdef CONFIG_USER_ONLY
5472       .type = ARM_CP_NO_RAW,
5473       .writefn = ic_ivau_write
5474 #else
5475       .type = ARM_CP_NOP
5476 #endif
5477     },
5478     /* Cache ops: all NOPs since we don't emulate caches */
5479     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5481       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5482       .fgt = FGT_DCIVAC,
5483       .type = ARM_CP_NOP },
5484     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5485       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5486       .fgt = FGT_DCISW,
5487       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5488     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5489       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5490       .access = PL0_W, .type = ARM_CP_NOP,
5491       .fgt = FGT_DCCVAC,
5492       .accessfn = aa64_cacheop_poc_access },
5493     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5494       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5495       .fgt = FGT_DCCSW,
5496       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5497     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5498       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5499       .access = PL0_W, .type = ARM_CP_NOP,
5500       .fgt = FGT_DCCVAU,
5501       .accessfn = access_tocu },
5502     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5503       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5504       .access = PL0_W, .type = ARM_CP_NOP,
5505       .fgt = FGT_DCCIVAC,
5506       .accessfn = aa64_cacheop_poc_access },
5507     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5508       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5509       .fgt = FGT_DCCISW,
5510       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5511     /* TLBI operations */
5512     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5513       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5514       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5515       .fgt = FGT_TLBIVMALLE1IS,
5516       .writefn = tlbi_aa64_vmalle1is_write },
5517     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5518       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5519       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5520       .fgt = FGT_TLBIVAE1IS,
5521       .writefn = tlbi_aa64_vae1is_write },
5522     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5523       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5524       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5525       .fgt = FGT_TLBIASIDE1IS,
5526       .writefn = tlbi_aa64_vmalle1is_write },
5527     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5528       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5529       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5530       .fgt = FGT_TLBIVAAE1IS,
5531       .writefn = tlbi_aa64_vae1is_write },
5532     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5533       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5534       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5535       .fgt = FGT_TLBIVALE1IS,
5536       .writefn = tlbi_aa64_vae1is_write },
5537     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5538       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5539       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5540       .fgt = FGT_TLBIVAALE1IS,
5541       .writefn = tlbi_aa64_vae1is_write },
5542     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5543       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5544       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5545       .fgt = FGT_TLBIVMALLE1,
5546       .writefn = tlbi_aa64_vmalle1_write },
5547     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5548       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5549       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5550       .fgt = FGT_TLBIVAE1,
5551       .writefn = tlbi_aa64_vae1_write },
5552     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5553       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5554       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5555       .fgt = FGT_TLBIASIDE1,
5556       .writefn = tlbi_aa64_vmalle1_write },
5557     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5558       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5559       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5560       .fgt = FGT_TLBIVAAE1,
5561       .writefn = tlbi_aa64_vae1_write },
5562     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5563       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5564       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5565       .fgt = FGT_TLBIVALE1,
5566       .writefn = tlbi_aa64_vae1_write },
5567     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5568       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5569       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5570       .fgt = FGT_TLBIVAALE1,
5571       .writefn = tlbi_aa64_vae1_write },
5572     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5573       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5574       .access = PL2_W, .type = ARM_CP_NO_RAW,
5575       .writefn = tlbi_aa64_ipas2e1is_write },
5576     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5577       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5578       .access = PL2_W, .type = ARM_CP_NO_RAW,
5579       .writefn = tlbi_aa64_ipas2e1is_write },
5580     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5581       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5582       .access = PL2_W, .type = ARM_CP_NO_RAW,
5583       .writefn = tlbi_aa64_alle1is_write },
5584     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5585       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5586       .access = PL2_W, .type = ARM_CP_NO_RAW,
5587       .writefn = tlbi_aa64_alle1is_write },
5588     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5589       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5590       .access = PL2_W, .type = ARM_CP_NO_RAW,
5591       .writefn = tlbi_aa64_ipas2e1_write },
5592     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5593       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5594       .access = PL2_W, .type = ARM_CP_NO_RAW,
5595       .writefn = tlbi_aa64_ipas2e1_write },
5596     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5597       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5598       .access = PL2_W, .type = ARM_CP_NO_RAW,
5599       .writefn = tlbi_aa64_alle1_write },
5600     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5601       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5602       .access = PL2_W, .type = ARM_CP_NO_RAW,
5603       .writefn = tlbi_aa64_alle1is_write },
5604 #ifndef CONFIG_USER_ONLY
5605     /* 64 bit address translation operations */
5606     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5607       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5608       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5609       .fgt = FGT_ATS1E1R,
5610       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5611     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5612       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5613       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5614       .fgt = FGT_ATS1E1W,
5615       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5616     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5617       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5618       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5619       .fgt = FGT_ATS1E0R,
5620       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5621     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5622       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5623       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5624       .fgt = FGT_ATS1E0W,
5625       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5626     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5627       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5628       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5629       .accessfn = at_e012_access, .writefn = ats_write64 },
5630     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5631       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5632       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5633       .accessfn = at_e012_access, .writefn = ats_write64 },
5634     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5635       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5636       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5637       .accessfn = at_e012_access, .writefn = ats_write64 },
5638     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5639       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5640       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5641       .accessfn = at_e012_access, .writefn = ats_write64 },
5642     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5643     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5644       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5645       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5646       .writefn = ats_write64 },
5647     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5648       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5649       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5650       .writefn = ats_write64 },
5651     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5652       .type = ARM_CP_ALIAS,
5653       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5654       .access = PL1_RW, .resetvalue = 0,
5655       .fgt = FGT_PAR_EL1,
5656       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5657       .writefn = par_write },
5658 #endif
5659     /* TLB invalidate last level of translation table walk */
5660     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5661       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5662       .writefn = tlbimva_is_write },
5663     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5664       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5665       .writefn = tlbimvaa_is_write },
5666     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5667       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5668       .writefn = tlbimva_write },
5669     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5670       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5671       .writefn = tlbimvaa_write },
5672     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5673       .type = ARM_CP_NO_RAW, .access = PL2_W,
5674       .writefn = tlbimva_hyp_write },
5675     { .name = "TLBIMVALHIS",
5676       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5677       .type = ARM_CP_NO_RAW, .access = PL2_W,
5678       .writefn = tlbimva_hyp_is_write },
5679     { .name = "TLBIIPAS2",
5680       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5681       .type = ARM_CP_NO_RAW, .access = PL2_W,
5682       .writefn = tlbiipas2_hyp_write },
5683     { .name = "TLBIIPAS2IS",
5684       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5685       .type = ARM_CP_NO_RAW, .access = PL2_W,
5686       .writefn = tlbiipas2is_hyp_write },
5687     { .name = "TLBIIPAS2L",
5688       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5689       .type = ARM_CP_NO_RAW, .access = PL2_W,
5690       .writefn = tlbiipas2_hyp_write },
5691     { .name = "TLBIIPAS2LIS",
5692       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5693       .type = ARM_CP_NO_RAW, .access = PL2_W,
5694       .writefn = tlbiipas2is_hyp_write },
5695     /* 32 bit cache operations */
5696     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5697       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5698     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5699       .type = ARM_CP_NOP, .access = PL1_W },
5700     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5701       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5702     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5703       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5704     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5705       .type = ARM_CP_NOP, .access = PL1_W },
5706     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5707       .type = ARM_CP_NOP, .access = PL1_W },
5708     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5709       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5710     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5711       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5712     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5713       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5714     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5715       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5716     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5717       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5718     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5719       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5720     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5721       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5722     /* MMU Domain access control / MPU write buffer control */
5723     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5724       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5725       .writefn = dacr_write, .raw_writefn = raw_write,
5726       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5727                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5728     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5729       .type = ARM_CP_ALIAS,
5730       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5731       .access = PL1_RW, .accessfn = access_nv1,
5732       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5733     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5734       .type = ARM_CP_ALIAS,
5735       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5736       .access = PL1_RW, .accessfn = access_nv1,
5737       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5738     /*
5739      * We rely on the access checks not allowing the guest to write to the
5740      * state field when SPSel indicates that it's being used as the stack
5741      * pointer.
5742      */
5743     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5744       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5745       .access = PL1_RW, .accessfn = sp_el0_access,
5746       .type = ARM_CP_ALIAS,
5747       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5748     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5749       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5750       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5751       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5752     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5753       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5754       .type = ARM_CP_NO_RAW,
5755       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5756     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5757       .type = ARM_CP_ALIAS,
5758       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5759       .access = PL2_RW,
5760       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5761     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5762       .type = ARM_CP_ALIAS,
5763       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5764       .access = PL2_RW,
5765       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5766     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5767       .type = ARM_CP_ALIAS,
5768       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5769       .access = PL2_RW,
5770       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5771     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5772       .type = ARM_CP_ALIAS,
5773       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5774       .access = PL2_RW,
5775       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5776     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5777       .type = ARM_CP_IO,
5778       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5779       .resetvalue = 0,
5780       .access = PL3_RW,
5781       .writefn = mdcr_el3_write,
5782       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5783     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5784       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5785       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5786       .writefn = sdcr_write,
5787       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5788 };
5789 
5790 /* These are present only when EL1 supports AArch32 */
5791 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5792     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5793       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5794       .access = PL2_RW,
5795       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5796       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5797     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5798       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5799       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5800       .writefn = dacr_write, .raw_writefn = raw_write,
5801       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5802     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5803       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5804       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5805       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5806 };
5807 
5808 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5809 {
5810     ARMCPU *cpu = env_archcpu(env);
5811 
5812     if (arm_feature(env, ARM_FEATURE_V8)) {
5813         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5814     } else {
5815         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5816     }
5817 
5818     if (arm_feature(env, ARM_FEATURE_EL3)) {
5819         valid_mask &= ~HCR_HCD;
5820     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5821         /*
5822          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5823          * However, if we're using the SMC PSCI conduit then QEMU is
5824          * effectively acting like EL3 firmware and so the guest at
5825          * EL2 should retain the ability to prevent EL1 from being
5826          * able to make SMC calls into the ersatz firmware, so in
5827          * that case HCR.TSC should be read/write.
5828          */
5829         valid_mask &= ~HCR_TSC;
5830     }
5831 
5832     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5833         if (cpu_isar_feature(aa64_vh, cpu)) {
5834             valid_mask |= HCR_E2H;
5835         }
5836         if (cpu_isar_feature(aa64_ras, cpu)) {
5837             valid_mask |= HCR_TERR | HCR_TEA;
5838         }
5839         if (cpu_isar_feature(aa64_lor, cpu)) {
5840             valid_mask |= HCR_TLOR;
5841         }
5842         if (cpu_isar_feature(aa64_pauth, cpu)) {
5843             valid_mask |= HCR_API | HCR_APK;
5844         }
5845         if (cpu_isar_feature(aa64_mte, cpu)) {
5846             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5847         }
5848         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5849             valid_mask |= HCR_ENSCXT;
5850         }
5851         if (cpu_isar_feature(aa64_fwb, cpu)) {
5852             valid_mask |= HCR_FWB;
5853         }
5854         if (cpu_isar_feature(aa64_rme, cpu)) {
5855             valid_mask |= HCR_GPF;
5856         }
5857         if (cpu_isar_feature(aa64_nv, cpu)) {
5858             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5859         }
5860         if (cpu_isar_feature(aa64_nv2, cpu)) {
5861             valid_mask |= HCR_NV2;
5862         }
5863     }
5864 
5865     if (cpu_isar_feature(any_evt, cpu)) {
5866         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5867     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5868         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5869     }
5870 
5871     /* Clear RES0 bits.  */
5872     value &= valid_mask;
5873 
5874     /*
5875      * These bits change the MMU setup:
5876      * HCR_VM enables stage 2 translation
5877      * HCR_PTW forbids certain page-table setups
5878      * HCR_DC disables stage1 and enables stage2 translation
5879      * HCR_DCT enables tagging on (disabled) stage1 translation
5880      * HCR_FWB changes the interpretation of stage2 descriptor bits
5881      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5882      */
5883     if ((env->cp15.hcr_el2 ^ value) &
5884         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5885         tlb_flush(CPU(cpu));
5886     }
5887     env->cp15.hcr_el2 = value;
5888 
5889     /*
5890      * Updates to VI and VF require us to update the status of
5891      * virtual interrupts, which are the logical OR of these bits
5892      * and the state of the input lines from the GIC. (This requires
5893      * that we have the BQL, which is done by marking the
5894      * reginfo structs as ARM_CP_IO.)
5895      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5896      * possible for it to be taken immediately, because VIRQ and
5897      * VFIQ are masked unless running at EL0 or EL1, and HCR
5898      * can only be written at EL2.
5899      */
5900     g_assert(bql_locked());
5901     arm_cpu_update_virq(cpu);
5902     arm_cpu_update_vfiq(cpu);
5903     arm_cpu_update_vserr(cpu);
5904 }
5905 
5906 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5907 {
5908     do_hcr_write(env, value, 0);
5909 }
5910 
5911 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5912                           uint64_t value)
5913 {
5914     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5915     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5916     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5917 }
5918 
5919 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5920                          uint64_t value)
5921 {
5922     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5923     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5924     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5925 }
5926 
5927 /*
5928  * Return the effective value of HCR_EL2, at the given security state.
5929  * Bits that are not included here:
5930  * RW       (read from SCR_EL3.RW as needed)
5931  */
5932 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5933 {
5934     uint64_t ret = env->cp15.hcr_el2;
5935 
5936     assert(space != ARMSS_Root);
5937 
5938     if (!arm_is_el2_enabled_secstate(env, space)) {
5939         /*
5940          * "This register has no effect if EL2 is not enabled in the
5941          * current Security state".  This is ARMv8.4-SecEL2 speak for
5942          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5943          *
5944          * Prior to that, the language was "In an implementation that
5945          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5946          * as if this field is 0 for all purposes other than a direct
5947          * read or write access of HCR_EL2".  With lots of enumeration
5948          * on a per-field basis.  In current QEMU, this is condition
5949          * is arm_is_secure_below_el3.
5950          *
5951          * Since the v8.4 language applies to the entire register, and
5952          * appears to be backward compatible, use that.
5953          */
5954         return 0;
5955     }
5956 
5957     /*
5958      * For a cpu that supports both aarch64 and aarch32, we can set bits
5959      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5960      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5961      */
5962     if (!arm_el_is_aa64(env, 2)) {
5963         uint64_t aa32_valid;
5964 
5965         /*
5966          * These bits are up-to-date as of ARMv8.6.
5967          * For HCR, it's easiest to list just the 2 bits that are invalid.
5968          * For HCR2, list those that are valid.
5969          */
5970         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5971         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5972                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5973         ret &= aa32_valid;
5974     }
5975 
5976     if (ret & HCR_TGE) {
5977         /* These bits are up-to-date as of ARMv8.6.  */
5978         if (ret & HCR_E2H) {
5979             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5980                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5981                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5982                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5983                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5984                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5985         } else {
5986             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5987         }
5988         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5989                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5990                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5991                  HCR_TLOR);
5992     }
5993 
5994     return ret;
5995 }
5996 
5997 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5998 {
5999     if (arm_feature(env, ARM_FEATURE_M)) {
6000         return 0;
6001     }
6002     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6003 }
6004 
6005 /*
6006  * Corresponds to ARM pseudocode function ELIsInHost().
6007  */
6008 bool el_is_in_host(CPUARMState *env, int el)
6009 {
6010     uint64_t mask;
6011 
6012     /*
6013      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6014      * Perform the simplest bit tests first, and validate EL2 afterward.
6015      */
6016     if (el & 1) {
6017         return false; /* EL1 or EL3 */
6018     }
6019 
6020     /*
6021      * Note that hcr_write() checks isar_feature_aa64_vh(),
6022      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6023      */
6024     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6025     if ((env->cp15.hcr_el2 & mask) != mask) {
6026         return false;
6027     }
6028 
6029     /* TGE and/or E2H set: double check those bits are currently legal. */
6030     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6031 }
6032 
6033 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6034                        uint64_t value)
6035 {
6036     uint64_t valid_mask = 0;
6037 
6038     /* FEAT_MOPS adds MSCEn and MCE2 */
6039     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6040         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6041     }
6042 
6043     /* Clear RES0 bits.  */
6044     env->cp15.hcrx_el2 = value & valid_mask;
6045 }
6046 
6047 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6048                                   bool isread)
6049 {
6050     if (arm_current_el(env) == 2
6051         && arm_feature(env, ARM_FEATURE_EL3)
6052         && !(env->cp15.scr_el3 & SCR_HXEN)) {
6053         return CP_ACCESS_TRAP_EL3;
6054     }
6055     return CP_ACCESS_OK;
6056 }
6057 
6058 static const ARMCPRegInfo hcrx_el2_reginfo = {
6059     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6060     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6061     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6062     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6063 };
6064 
6065 /* Return the effective value of HCRX_EL2.  */
6066 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6067 {
6068     /*
6069      * The bits in this register behave as 0 for all purposes other than
6070      * direct reads of the register if SCR_EL3.HXEn is 0.
6071      * If EL2 is not enabled in the current security state, then the
6072      * bit may behave as if 0, or as if 1, depending on the bit.
6073      * For the moment, we treat the EL2-disabled case as taking
6074      * priority over the HXEn-disabled case. This is true for the only
6075      * bit for a feature which we implement where the answer is different
6076      * for the two cases (MSCEn for FEAT_MOPS).
6077      * This may need to be revisited for future bits.
6078      */
6079     if (!arm_is_el2_enabled(env)) {
6080         uint64_t hcrx = 0;
6081         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6082             /* MSCEn behaves as 1 if EL2 is not enabled */
6083             hcrx |= HCRX_MSCEN;
6084         }
6085         return hcrx;
6086     }
6087     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6088         return 0;
6089     }
6090     return env->cp15.hcrx_el2;
6091 }
6092 
6093 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6094                            uint64_t value)
6095 {
6096     /*
6097      * For A-profile AArch32 EL3, if NSACR.CP10
6098      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6099      */
6100     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6101         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6102         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6103         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6104     }
6105     env->cp15.cptr_el[2] = value;
6106 }
6107 
6108 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6109 {
6110     /*
6111      * For A-profile AArch32 EL3, if NSACR.CP10
6112      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6113      */
6114     uint64_t value = env->cp15.cptr_el[2];
6115 
6116     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6117         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6118         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6119     }
6120     return value;
6121 }
6122 
6123 static const ARMCPRegInfo el2_cp_reginfo[] = {
6124     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6125       .type = ARM_CP_IO,
6126       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6127       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6128       .writefn = hcr_write, .raw_writefn = raw_write },
6129     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6130       .type = ARM_CP_ALIAS | ARM_CP_IO,
6131       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6132       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6133       .writefn = hcr_writelow },
6134     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6135       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6136       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6137     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6138       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6139       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6140       .access = PL2_RW,
6141       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6142     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6143       .type = ARM_CP_NV2_REDIRECT,
6144       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6145       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6146     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6147       .type = ARM_CP_NV2_REDIRECT,
6148       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6149       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6150     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6151       .type = ARM_CP_ALIAS,
6152       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6153       .access = PL2_RW,
6154       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6155     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6156       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6157       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6158       .access = PL2_RW,
6159       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6160     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6161       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6162       .access = PL2_RW, .writefn = vbar_write,
6163       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6164       .resetvalue = 0 },
6165     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6166       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6167       .access = PL3_RW, .type = ARM_CP_ALIAS,
6168       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6169     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6170       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6171       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6172       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6173       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6174     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6175       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6176       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6177       .resetvalue = 0 },
6178     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6179       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6180       .access = PL2_RW, .type = ARM_CP_ALIAS,
6181       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6182     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6183       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6184       .access = PL2_RW, .type = ARM_CP_CONST,
6185       .resetvalue = 0 },
6186     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6187     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6188       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6189       .access = PL2_RW, .type = ARM_CP_CONST,
6190       .resetvalue = 0 },
6191     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6192       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6193       .access = PL2_RW, .type = ARM_CP_CONST,
6194       .resetvalue = 0 },
6195     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6196       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6197       .access = PL2_RW, .type = ARM_CP_CONST,
6198       .resetvalue = 0 },
6199     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6200       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6201       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6202       .raw_writefn = raw_write,
6203       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6204     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6205       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6206       .type = ARM_CP_ALIAS,
6207       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6208       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6209     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6210       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6211       .access = PL2_RW,
6212       /* no .writefn needed as this can't cause an ASID change */
6213       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6214     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6215       .cp = 15, .opc1 = 6, .crm = 2,
6216       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6217       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6218       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6219       .writefn = vttbr_write, .raw_writefn = raw_write },
6220     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6221       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6222       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6223       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6224     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6225       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6226       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6227       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6228     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6229       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6230       .access = PL2_RW, .resetvalue = 0,
6231       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6232     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6233       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6234       .access = PL2_RW, .resetvalue = 0,
6235       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6236       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6237     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6238       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6239       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6240     { .name = "TLBIALLNSNH",
6241       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6242       .type = ARM_CP_NO_RAW, .access = PL2_W,
6243       .writefn = tlbiall_nsnh_write },
6244     { .name = "TLBIALLNSNHIS",
6245       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6246       .type = ARM_CP_NO_RAW, .access = PL2_W,
6247       .writefn = tlbiall_nsnh_is_write },
6248     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6249       .type = ARM_CP_NO_RAW, .access = PL2_W,
6250       .writefn = tlbiall_hyp_write },
6251     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6252       .type = ARM_CP_NO_RAW, .access = PL2_W,
6253       .writefn = tlbiall_hyp_is_write },
6254     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6255       .type = ARM_CP_NO_RAW, .access = PL2_W,
6256       .writefn = tlbimva_hyp_write },
6257     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6258       .type = ARM_CP_NO_RAW, .access = PL2_W,
6259       .writefn = tlbimva_hyp_is_write },
6260     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6261       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6262       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6263       .writefn = tlbi_aa64_alle2_write },
6264     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6265       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6266       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6267       .writefn = tlbi_aa64_vae2_write },
6268     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6269       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6270       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6271       .writefn = tlbi_aa64_vae2_write },
6272     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6273       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6274       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6275       .writefn = tlbi_aa64_alle2is_write },
6276     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6277       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6278       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6279       .writefn = tlbi_aa64_vae2is_write },
6280     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6281       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6282       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6283       .writefn = tlbi_aa64_vae2is_write },
6284 #ifndef CONFIG_USER_ONLY
6285     /*
6286      * Unlike the other EL2-related AT operations, these must
6287      * UNDEF from EL3 if EL2 is not implemented, which is why we
6288      * define them here rather than with the rest of the AT ops.
6289      */
6290     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6291       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6292       .access = PL2_W, .accessfn = at_s1e2_access,
6293       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6294       .writefn = ats_write64 },
6295     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6296       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6297       .access = PL2_W, .accessfn = at_s1e2_access,
6298       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6299       .writefn = ats_write64 },
6300     /*
6301      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6302      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6303      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6304      * to behave as if SCR.NS was 1.
6305      */
6306     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6307       .access = PL2_W,
6308       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6309     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6310       .access = PL2_W,
6311       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6312     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6313       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6314       /*
6315        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6316        * reset values as IMPDEF. We choose to reset to 3 to comply with
6317        * both ARMv7 and ARMv8.
6318        */
6319       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6320       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6321       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6322     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6323       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6324       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6325       .writefn = gt_cntvoff_write,
6326       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6327     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6328       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6329       .writefn = gt_cntvoff_write,
6330       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6331     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6332       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6333       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6334       .type = ARM_CP_IO, .access = PL2_RW,
6335       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6336     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6337       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6338       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6339       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6340     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6341       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6342       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6343       .resetfn = gt_hyp_timer_reset,
6344       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6345     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6346       .type = ARM_CP_IO,
6347       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6348       .access = PL2_RW,
6349       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6350       .resetvalue = 0,
6351       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6352 #endif
6353     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6354       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6355       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6356       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6357     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6358       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6359       .access = PL2_RW,
6360       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6361     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6362       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6363       .access = PL2_RW,
6364       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6365 };
6366 
6367 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6368     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6369       .type = ARM_CP_ALIAS | ARM_CP_IO,
6370       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6371       .access = PL2_RW,
6372       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6373       .writefn = hcr_writehigh },
6374 };
6375 
6376 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6377                                   bool isread)
6378 {
6379     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6380         return CP_ACCESS_OK;
6381     }
6382     return CP_ACCESS_TRAP_UNCATEGORIZED;
6383 }
6384 
6385 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6386     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6387       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6388       .access = PL2_RW, .accessfn = sel2_access,
6389       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6390     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6391       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6392       .access = PL2_RW, .accessfn = sel2_access,
6393       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6394 };
6395 
6396 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6397                                    bool isread)
6398 {
6399     /*
6400      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6401      * At Secure EL1 it traps to EL3 or EL2.
6402      */
6403     if (arm_current_el(env) == 3) {
6404         return CP_ACCESS_OK;
6405     }
6406     if (arm_is_secure_below_el3(env)) {
6407         if (env->cp15.scr_el3 & SCR_EEL2) {
6408             return CP_ACCESS_TRAP_EL2;
6409         }
6410         return CP_ACCESS_TRAP_EL3;
6411     }
6412     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6413     if (isread) {
6414         return CP_ACCESS_OK;
6415     }
6416     return CP_ACCESS_TRAP_UNCATEGORIZED;
6417 }
6418 
6419 static const ARMCPRegInfo el3_cp_reginfo[] = {
6420     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6421       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6422       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6423       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6424     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6425       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6426       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6427       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6428       .writefn = scr_write, .raw_writefn = raw_write },
6429     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6430       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6431       .access = PL3_RW, .resetvalue = 0,
6432       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6433     { .name = "SDER",
6434       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6435       .access = PL3_RW, .resetvalue = 0,
6436       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6437     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6438       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6439       .writefn = vbar_write, .resetvalue = 0,
6440       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6441     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6442       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6443       .access = PL3_RW, .resetvalue = 0,
6444       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6445     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6446       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6447       .access = PL3_RW,
6448       /* no .writefn needed as this can't cause an ASID change */
6449       .resetvalue = 0,
6450       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6451     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6452       .type = ARM_CP_ALIAS,
6453       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6454       .access = PL3_RW,
6455       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6456     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6457       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6458       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6459     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6460       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6461       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6462     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6463       .type = ARM_CP_ALIAS,
6464       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6465       .access = PL3_RW,
6466       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6467     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6468       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6469       .access = PL3_RW, .writefn = vbar_write,
6470       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6471       .resetvalue = 0 },
6472     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6473       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6474       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6475       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6476     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6477       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6478       .access = PL3_RW, .resetvalue = 0,
6479       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6480     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6481       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6482       .access = PL3_RW, .type = ARM_CP_CONST,
6483       .resetvalue = 0 },
6484     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6485       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6486       .access = PL3_RW, .type = ARM_CP_CONST,
6487       .resetvalue = 0 },
6488     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6489       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6490       .access = PL3_RW, .type = ARM_CP_CONST,
6491       .resetvalue = 0 },
6492     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6493       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6494       .access = PL3_W, .type = ARM_CP_NO_RAW,
6495       .writefn = tlbi_aa64_alle3is_write },
6496     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6497       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6498       .access = PL3_W, .type = ARM_CP_NO_RAW,
6499       .writefn = tlbi_aa64_vae3is_write },
6500     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6501       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6502       .access = PL3_W, .type = ARM_CP_NO_RAW,
6503       .writefn = tlbi_aa64_vae3is_write },
6504     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6505       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6506       .access = PL3_W, .type = ARM_CP_NO_RAW,
6507       .writefn = tlbi_aa64_alle3_write },
6508     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6509       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6510       .access = PL3_W, .type = ARM_CP_NO_RAW,
6511       .writefn = tlbi_aa64_vae3_write },
6512     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6513       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6514       .access = PL3_W, .type = ARM_CP_NO_RAW,
6515       .writefn = tlbi_aa64_vae3_write },
6516 };
6517 
6518 #ifndef CONFIG_USER_ONLY
6519 /* Test if system register redirection is to occur in the current state.  */
6520 static bool redirect_for_e2h(CPUARMState *env)
6521 {
6522     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6523 }
6524 
6525 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6526 {
6527     CPReadFn *readfn;
6528 
6529     if (redirect_for_e2h(env)) {
6530         /* Switch to the saved EL2 version of the register.  */
6531         ri = ri->opaque;
6532         readfn = ri->readfn;
6533     } else {
6534         readfn = ri->orig_readfn;
6535     }
6536     if (readfn == NULL) {
6537         readfn = raw_read;
6538     }
6539     return readfn(env, ri);
6540 }
6541 
6542 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6543                           uint64_t value)
6544 {
6545     CPWriteFn *writefn;
6546 
6547     if (redirect_for_e2h(env)) {
6548         /* Switch to the saved EL2 version of the register.  */
6549         ri = ri->opaque;
6550         writefn = ri->writefn;
6551     } else {
6552         writefn = ri->orig_writefn;
6553     }
6554     if (writefn == NULL) {
6555         writefn = raw_write;
6556     }
6557     writefn(env, ri, value);
6558 }
6559 
6560 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6561 {
6562     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6563     return ri->orig_readfn(env, ri->opaque);
6564 }
6565 
6566 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6567                               uint64_t value)
6568 {
6569     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6570     return ri->orig_writefn(env, ri->opaque, value);
6571 }
6572 
6573 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6574                                          const ARMCPRegInfo *ri,
6575                                          bool isread)
6576 {
6577     if (arm_current_el(env) == 1) {
6578         /*
6579          * This must be a FEAT_NV access (will either trap or redirect
6580          * to memory). None of the registers with _EL12 aliases want to
6581          * apply their trap controls for this kind of access, so don't
6582          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6583          */
6584         return CP_ACCESS_OK;
6585     }
6586     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6587     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6588         return CP_ACCESS_TRAP_UNCATEGORIZED;
6589     }
6590     if (ri->orig_accessfn) {
6591         return ri->orig_accessfn(env, ri->opaque, isread);
6592     }
6593     return CP_ACCESS_OK;
6594 }
6595 
6596 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6597 {
6598     struct E2HAlias {
6599         uint32_t src_key, dst_key, new_key;
6600         const char *src_name, *dst_name, *new_name;
6601         bool (*feature)(const ARMISARegisters *id);
6602     };
6603 
6604 #define K(op0, op1, crn, crm, op2) \
6605     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6606 
6607     static const struct E2HAlias aliases[] = {
6608         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6609           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6610         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6611           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6612         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6613           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6614         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6615           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6616         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6617           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6618         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6619           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6620         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6621           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6622         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6623           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6624         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6625           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6626         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6627           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6628         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6629           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6630         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6631           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6632         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6633           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6634         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6635           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6636         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6637           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6638         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6639           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6640 
6641         /*
6642          * Note that redirection of ZCR is mentioned in the description
6643          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6644          * not in the summary table.
6645          */
6646         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6647           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6648         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6649           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6650 
6651         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6652           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6653 
6654         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6655           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6656           isar_feature_aa64_scxtnum },
6657 
6658         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6659         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6660     };
6661 #undef K
6662 
6663     size_t i;
6664 
6665     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6666         const struct E2HAlias *a = &aliases[i];
6667         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6668         bool ok;
6669 
6670         if (a->feature && !a->feature(&cpu->isar)) {
6671             continue;
6672         }
6673 
6674         src_reg = g_hash_table_lookup(cpu->cp_regs,
6675                                       (gpointer)(uintptr_t)a->src_key);
6676         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6677                                       (gpointer)(uintptr_t)a->dst_key);
6678         g_assert(src_reg != NULL);
6679         g_assert(dst_reg != NULL);
6680 
6681         /* Cross-compare names to detect typos in the keys.  */
6682         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6683         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6684 
6685         /* None of the core system registers use opaque; we will.  */
6686         g_assert(src_reg->opaque == NULL);
6687 
6688         /* Create alias before redirection so we dup the right data. */
6689         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6690 
6691         new_reg->name = a->new_name;
6692         new_reg->type |= ARM_CP_ALIAS;
6693         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6694         new_reg->access &= PL2_RW | PL3_RW;
6695         /* The new_reg op fields are as per new_key, not the target reg */
6696         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6697             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6698         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6699             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6700         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6701             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6702         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6703             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6704         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6705             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6706         new_reg->opaque = src_reg;
6707         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6708         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6709         new_reg->orig_accessfn = src_reg->accessfn;
6710         if (!new_reg->raw_readfn) {
6711             new_reg->raw_readfn = raw_read;
6712         }
6713         if (!new_reg->raw_writefn) {
6714             new_reg->raw_writefn = raw_write;
6715         }
6716         new_reg->readfn = el2_e2h_e12_read;
6717         new_reg->writefn = el2_e2h_e12_write;
6718         new_reg->accessfn = el2_e2h_e12_access;
6719 
6720         ok = g_hash_table_insert(cpu->cp_regs,
6721                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6722         g_assert(ok);
6723 
6724         src_reg->opaque = dst_reg;
6725         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6726         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6727         if (!src_reg->raw_readfn) {
6728             src_reg->raw_readfn = raw_read;
6729         }
6730         if (!src_reg->raw_writefn) {
6731             src_reg->raw_writefn = raw_write;
6732         }
6733         src_reg->readfn = el2_e2h_read;
6734         src_reg->writefn = el2_e2h_write;
6735     }
6736 }
6737 #endif
6738 
6739 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6740                                      bool isread)
6741 {
6742     int cur_el = arm_current_el(env);
6743 
6744     if (cur_el < 2) {
6745         uint64_t hcr = arm_hcr_el2_eff(env);
6746 
6747         if (cur_el == 0) {
6748             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6749                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6750                     return CP_ACCESS_TRAP_EL2;
6751                 }
6752             } else {
6753                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6754                     return CP_ACCESS_TRAP;
6755                 }
6756                 if (hcr & HCR_TID2) {
6757                     return CP_ACCESS_TRAP_EL2;
6758                 }
6759             }
6760         } else if (hcr & HCR_TID2) {
6761             return CP_ACCESS_TRAP_EL2;
6762         }
6763     }
6764 
6765     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6766         return CP_ACCESS_TRAP_EL2;
6767     }
6768 
6769     return CP_ACCESS_OK;
6770 }
6771 
6772 /*
6773  * Check for traps to RAS registers, which are controlled
6774  * by HCR_EL2.TERR and SCR_EL3.TERR.
6775  */
6776 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6777                                   bool isread)
6778 {
6779     int el = arm_current_el(env);
6780 
6781     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6782         return CP_ACCESS_TRAP_EL2;
6783     }
6784     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6785         return CP_ACCESS_TRAP_EL3;
6786     }
6787     return CP_ACCESS_OK;
6788 }
6789 
6790 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6791 {
6792     int el = arm_current_el(env);
6793 
6794     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6795         return env->cp15.vdisr_el2;
6796     }
6797     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6798         return 0; /* RAZ/WI */
6799     }
6800     return env->cp15.disr_el1;
6801 }
6802 
6803 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6804 {
6805     int el = arm_current_el(env);
6806 
6807     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6808         env->cp15.vdisr_el2 = val;
6809         return;
6810     }
6811     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6812         return; /* RAZ/WI */
6813     }
6814     env->cp15.disr_el1 = val;
6815 }
6816 
6817 /*
6818  * Minimal RAS implementation with no Error Records.
6819  * Which means that all of the Error Record registers:
6820  *   ERXADDR_EL1
6821  *   ERXCTLR_EL1
6822  *   ERXFR_EL1
6823  *   ERXMISC0_EL1
6824  *   ERXMISC1_EL1
6825  *   ERXMISC2_EL1
6826  *   ERXMISC3_EL1
6827  *   ERXPFGCDN_EL1  (RASv1p1)
6828  *   ERXPFGCTL_EL1  (RASv1p1)
6829  *   ERXPFGF_EL1    (RASv1p1)
6830  *   ERXSTATUS_EL1
6831  * and
6832  *   ERRSELR_EL1
6833  * may generate UNDEFINED, which is the effect we get by not
6834  * listing them at all.
6835  *
6836  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6837  * is higher priority than FGT-to-EL2 so we do not need to list them
6838  * in order to check for an FGT.
6839  */
6840 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6841     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6842       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6843       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6844       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6845     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6846       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6847       .access = PL1_R, .accessfn = access_terr,
6848       .fgt = FGT_ERRIDR_EL1,
6849       .type = ARM_CP_CONST, .resetvalue = 0 },
6850     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6851       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6852       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6853     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6854       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6855       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6856 };
6857 
6858 /*
6859  * Return the exception level to which exceptions should be taken
6860  * via SVEAccessTrap.  This excludes the check for whether the exception
6861  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6862  * be found by testing 0 < fp_exception_el < sve_exception_el.
6863  *
6864  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6865  * pseudocode does *not* separate out the FP trap checks, but has them
6866  * all in one function.
6867  */
6868 int sve_exception_el(CPUARMState *env, int el)
6869 {
6870 #ifndef CONFIG_USER_ONLY
6871     if (el <= 1 && !el_is_in_host(env, el)) {
6872         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6873         case 1:
6874             if (el != 0) {
6875                 break;
6876             }
6877             /* fall through */
6878         case 0:
6879         case 2:
6880             return 1;
6881         }
6882     }
6883 
6884     if (el <= 2 && arm_is_el2_enabled(env)) {
6885         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6886         if (env->cp15.hcr_el2 & HCR_E2H) {
6887             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6888             case 1:
6889                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6890                     break;
6891                 }
6892                 /* fall through */
6893             case 0:
6894             case 2:
6895                 return 2;
6896             }
6897         } else {
6898             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6899                 return 2;
6900             }
6901         }
6902     }
6903 
6904     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6905     if (arm_feature(env, ARM_FEATURE_EL3)
6906         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6907         return 3;
6908     }
6909 #endif
6910     return 0;
6911 }
6912 
6913 /*
6914  * Return the exception level to which exceptions should be taken for SME.
6915  * C.f. the ARM pseudocode function CheckSMEAccess.
6916  */
6917 int sme_exception_el(CPUARMState *env, int el)
6918 {
6919 #ifndef CONFIG_USER_ONLY
6920     if (el <= 1 && !el_is_in_host(env, el)) {
6921         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6922         case 1:
6923             if (el != 0) {
6924                 break;
6925             }
6926             /* fall through */
6927         case 0:
6928         case 2:
6929             return 1;
6930         }
6931     }
6932 
6933     if (el <= 2 && arm_is_el2_enabled(env)) {
6934         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6935         if (env->cp15.hcr_el2 & HCR_E2H) {
6936             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6937             case 1:
6938                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6939                     break;
6940                 }
6941                 /* fall through */
6942             case 0:
6943             case 2:
6944                 return 2;
6945             }
6946         } else {
6947             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6948                 return 2;
6949             }
6950         }
6951     }
6952 
6953     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6954     if (arm_feature(env, ARM_FEATURE_EL3)
6955         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6956         return 3;
6957     }
6958 #endif
6959     return 0;
6960 }
6961 
6962 /*
6963  * Given that SVE is enabled, return the vector length for EL.
6964  */
6965 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6966 {
6967     ARMCPU *cpu = env_archcpu(env);
6968     uint64_t *cr = env->vfp.zcr_el;
6969     uint32_t map = cpu->sve_vq.map;
6970     uint32_t len = ARM_MAX_VQ - 1;
6971 
6972     if (sm) {
6973         cr = env->vfp.smcr_el;
6974         map = cpu->sme_vq.map;
6975     }
6976 
6977     if (el <= 1 && !el_is_in_host(env, el)) {
6978         len = MIN(len, 0xf & (uint32_t)cr[1]);
6979     }
6980     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6981         len = MIN(len, 0xf & (uint32_t)cr[2]);
6982     }
6983     if (arm_feature(env, ARM_FEATURE_EL3)) {
6984         len = MIN(len, 0xf & (uint32_t)cr[3]);
6985     }
6986 
6987     map &= MAKE_64BIT_MASK(0, len + 1);
6988     if (map != 0) {
6989         return 31 - clz32(map);
6990     }
6991 
6992     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6993     assert(sm);
6994     return ctz32(cpu->sme_vq.map);
6995 }
6996 
6997 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6998 {
6999     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7000 }
7001 
7002 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7003                       uint64_t value)
7004 {
7005     int cur_el = arm_current_el(env);
7006     int old_len = sve_vqm1_for_el(env, cur_el);
7007     int new_len;
7008 
7009     /* Bits other than [3:0] are RAZ/WI.  */
7010     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7011     raw_write(env, ri, value & 0xf);
7012 
7013     /*
7014      * Because we arrived here, we know both FP and SVE are enabled;
7015      * otherwise we would have trapped access to the ZCR_ELn register.
7016      */
7017     new_len = sve_vqm1_for_el(env, cur_el);
7018     if (new_len < old_len) {
7019         aarch64_sve_narrow_vq(env, new_len + 1);
7020     }
7021 }
7022 
7023 static const ARMCPRegInfo zcr_reginfo[] = {
7024     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7025       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7026       .access = PL1_RW, .type = ARM_CP_SVE,
7027       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7028       .writefn = zcr_write, .raw_writefn = raw_write },
7029     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7030       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7031       .access = PL2_RW, .type = ARM_CP_SVE,
7032       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7033       .writefn = zcr_write, .raw_writefn = raw_write },
7034     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7035       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7036       .access = PL3_RW, .type = ARM_CP_SVE,
7037       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7038       .writefn = zcr_write, .raw_writefn = raw_write },
7039 };
7040 
7041 #ifdef TARGET_AARCH64
7042 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7043                                     bool isread)
7044 {
7045     int el = arm_current_el(env);
7046 
7047     if (el == 0) {
7048         uint64_t sctlr = arm_sctlr(env, el);
7049         if (!(sctlr & SCTLR_EnTP2)) {
7050             return CP_ACCESS_TRAP;
7051         }
7052     }
7053     /* TODO: FEAT_FGT */
7054     if (el < 3
7055         && arm_feature(env, ARM_FEATURE_EL3)
7056         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7057         return CP_ACCESS_TRAP_EL3;
7058     }
7059     return CP_ACCESS_OK;
7060 }
7061 
7062 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7063                                       bool isread)
7064 {
7065     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7066     if (arm_current_el(env) == 2
7067         && arm_feature(env, ARM_FEATURE_EL3)
7068         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7069         return CP_ACCESS_TRAP_EL3;
7070     }
7071     return CP_ACCESS_OK;
7072 }
7073 
7074 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7075                                    bool isread)
7076 {
7077     if (arm_current_el(env) < 3
7078         && arm_feature(env, ARM_FEATURE_EL3)
7079         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7080         return CP_ACCESS_TRAP_EL3;
7081     }
7082     return CP_ACCESS_OK;
7083 }
7084 
7085 /* ResetSVEState */
7086 static void arm_reset_sve_state(CPUARMState *env)
7087 {
7088     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7089     /* Recall that FFR is stored as pregs[16]. */
7090     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7091     vfp_set_fpcr(env, 0x0800009f);
7092 }
7093 
7094 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7095 {
7096     uint64_t change = (env->svcr ^ new) & mask;
7097 
7098     if (change == 0) {
7099         return;
7100     }
7101     env->svcr ^= change;
7102 
7103     if (change & R_SVCR_SM_MASK) {
7104         arm_reset_sve_state(env);
7105     }
7106 
7107     /*
7108      * ResetSMEState.
7109      *
7110      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
7111      * on enable: while disabled, the storage is inaccessible and the
7112      * value does not matter.  We're not saving the storage in vmstate
7113      * when disabled either.
7114      */
7115     if (change & new & R_SVCR_ZA_MASK) {
7116         memset(env->zarray, 0, sizeof(env->zarray));
7117     }
7118 
7119     if (tcg_enabled()) {
7120         arm_rebuild_hflags(env);
7121     }
7122 }
7123 
7124 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7125                        uint64_t value)
7126 {
7127     aarch64_set_svcr(env, value, -1);
7128 }
7129 
7130 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7131                        uint64_t value)
7132 {
7133     int cur_el = arm_current_el(env);
7134     int old_len = sve_vqm1_for_el(env, cur_el);
7135     int new_len;
7136 
7137     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7138     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7139     raw_write(env, ri, value);
7140 
7141     /*
7142      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7143      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7144      * current values for simplicity.  But for QEMU internals, we must still
7145      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7146      * above aarch64_sve_narrow_vq.
7147      */
7148     new_len = sve_vqm1_for_el(env, cur_el);
7149     if (new_len < old_len) {
7150         aarch64_sve_narrow_vq(env, new_len + 1);
7151     }
7152 }
7153 
7154 static const ARMCPRegInfo sme_reginfo[] = {
7155     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7156       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7157       .access = PL0_RW, .accessfn = access_tpidr2,
7158       .fgt = FGT_NTPIDR2_EL0,
7159       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7160     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7161       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7162       .access = PL0_RW, .type = ARM_CP_SME,
7163       .fieldoffset = offsetof(CPUARMState, svcr),
7164       .writefn = svcr_write, .raw_writefn = raw_write },
7165     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7166       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7167       .access = PL1_RW, .type = ARM_CP_SME,
7168       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7169       .writefn = smcr_write, .raw_writefn = raw_write },
7170     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7171       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7172       .access = PL2_RW, .type = ARM_CP_SME,
7173       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7174       .writefn = smcr_write, .raw_writefn = raw_write },
7175     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7177       .access = PL3_RW, .type = ARM_CP_SME,
7178       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7179       .writefn = smcr_write, .raw_writefn = raw_write },
7180     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7181       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7182       .access = PL1_R, .accessfn = access_aa64_tid1,
7183       /*
7184        * IMPLEMENTOR = 0 (software)
7185        * REVISION    = 0 (implementation defined)
7186        * SMPS        = 0 (no streaming execution priority in QEMU)
7187        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7188        */
7189       .type = ARM_CP_CONST, .resetvalue = 0, },
7190     /*
7191      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7192      */
7193     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7194       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7195       .access = PL1_RW, .accessfn = access_smpri,
7196       .fgt = FGT_NSMPRI_EL1,
7197       .type = ARM_CP_CONST, .resetvalue = 0 },
7198     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7199       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7200       .access = PL2_RW, .accessfn = access_smprimap,
7201       .type = ARM_CP_CONST, .resetvalue = 0 },
7202 };
7203 
7204 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7205                                   uint64_t value)
7206 {
7207     CPUState *cs = env_cpu(env);
7208 
7209     tlb_flush(cs);
7210 }
7211 
7212 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7213                         uint64_t value)
7214 {
7215     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7216     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7217         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7218         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7219 
7220     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7221 }
7222 
7223 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7224 {
7225     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7226                                      env_archcpu(env)->reset_l0gptsz);
7227 }
7228 
7229 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7230                                     uint64_t value)
7231 {
7232     CPUState *cs = env_cpu(env);
7233 
7234     tlb_flush_all_cpus_synced(cs);
7235 }
7236 
7237 static const ARMCPRegInfo rme_reginfo[] = {
7238     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7239       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7240       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7241       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7242     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7243       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7244       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7245     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7246       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7247       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7248     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7249       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7250       .access = PL3_W, .type = ARM_CP_NO_RAW,
7251       .writefn = tlbi_aa64_paall_write },
7252     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7253       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7254       .access = PL3_W, .type = ARM_CP_NO_RAW,
7255       .writefn = tlbi_aa64_paallos_write },
7256     /*
7257      * QEMU does not have a way to invalidate by physical address, thus
7258      * invalidating a range of physical addresses is accomplished by
7259      * flushing all tlb entries in the outer shareable domain,
7260      * just like PAALLOS.
7261      */
7262     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7263       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7264       .access = PL3_W, .type = ARM_CP_NO_RAW,
7265       .writefn = tlbi_aa64_paallos_write },
7266     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7267       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7268       .access = PL3_W, .type = ARM_CP_NO_RAW,
7269       .writefn = tlbi_aa64_paallos_write },
7270     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7271       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7272       .access = PL3_W, .type = ARM_CP_NOP },
7273 };
7274 
7275 static const ARMCPRegInfo rme_mte_reginfo[] = {
7276     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7277       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7278       .access = PL3_W, .type = ARM_CP_NOP },
7279 };
7280 #endif /* TARGET_AARCH64 */
7281 
7282 static void define_pmu_regs(ARMCPU *cpu)
7283 {
7284     /*
7285      * v7 performance monitor control register: same implementor
7286      * field as main ID register, and we implement four counters in
7287      * addition to the cycle count register.
7288      */
7289     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7290     ARMCPRegInfo pmcr = {
7291         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7292         .access = PL0_RW,
7293         .fgt = FGT_PMCR_EL0,
7294         .type = ARM_CP_IO | ARM_CP_ALIAS,
7295         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7296         .accessfn = pmreg_access,
7297         .readfn = pmcr_read, .raw_readfn = raw_read,
7298         .writefn = pmcr_write, .raw_writefn = raw_write,
7299     };
7300     ARMCPRegInfo pmcr64 = {
7301         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7302         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7303         .access = PL0_RW, .accessfn = pmreg_access,
7304         .fgt = FGT_PMCR_EL0,
7305         .type = ARM_CP_IO,
7306         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7307         .resetvalue = cpu->isar.reset_pmcr_el0,
7308         .readfn = pmcr_read, .raw_readfn = raw_read,
7309         .writefn = pmcr_write, .raw_writefn = raw_write,
7310     };
7311 
7312     define_one_arm_cp_reg(cpu, &pmcr);
7313     define_one_arm_cp_reg(cpu, &pmcr64);
7314     for (i = 0; i < pmcrn; i++) {
7315         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7316         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7317         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7318         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7319         ARMCPRegInfo pmev_regs[] = {
7320             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7321               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7322               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7323               .fgt = FGT_PMEVCNTRN_EL0,
7324               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7325               .accessfn = pmreg_access_xevcntr },
7326             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7327               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7328               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7329               .type = ARM_CP_IO,
7330               .fgt = FGT_PMEVCNTRN_EL0,
7331               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7332               .raw_readfn = pmevcntr_rawread,
7333               .raw_writefn = pmevcntr_rawwrite },
7334             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7335               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7336               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7337               .fgt = FGT_PMEVTYPERN_EL0,
7338               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7339               .accessfn = pmreg_access },
7340             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7341               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7342               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7343               .fgt = FGT_PMEVTYPERN_EL0,
7344               .type = ARM_CP_IO,
7345               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7346               .raw_writefn = pmevtyper_rawwrite },
7347         };
7348         define_arm_cp_regs(cpu, pmev_regs);
7349         g_free(pmevcntr_name);
7350         g_free(pmevcntr_el0_name);
7351         g_free(pmevtyper_name);
7352         g_free(pmevtyper_el0_name);
7353     }
7354     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7355         ARMCPRegInfo v81_pmu_regs[] = {
7356             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7357               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7358               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7359               .fgt = FGT_PMCEIDN_EL0,
7360               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7361             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7362               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7363               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7364               .fgt = FGT_PMCEIDN_EL0,
7365               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7366         };
7367         define_arm_cp_regs(cpu, v81_pmu_regs);
7368     }
7369     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7370         static const ARMCPRegInfo v84_pmmir = {
7371             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7372             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7373             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7374             .fgt = FGT_PMMIR_EL1,
7375             .resetvalue = 0
7376         };
7377         define_one_arm_cp_reg(cpu, &v84_pmmir);
7378     }
7379 }
7380 
7381 #ifndef CONFIG_USER_ONLY
7382 /*
7383  * We don't know until after realize whether there's a GICv3
7384  * attached, and that is what registers the gicv3 sysregs.
7385  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7386  * at runtime.
7387  */
7388 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7389 {
7390     ARMCPU *cpu = env_archcpu(env);
7391     uint64_t pfr1 = cpu->isar.id_pfr1;
7392 
7393     if (env->gicv3state) {
7394         pfr1 |= 1 << 28;
7395     }
7396     return pfr1;
7397 }
7398 
7399 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7400 {
7401     ARMCPU *cpu = env_archcpu(env);
7402     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7403 
7404     if (env->gicv3state) {
7405         pfr0 |= 1 << 24;
7406     }
7407     return pfr0;
7408 }
7409 #endif
7410 
7411 /*
7412  * Shared logic between LORID and the rest of the LOR* registers.
7413  * Secure state exclusion has already been dealt with.
7414  */
7415 static CPAccessResult access_lor_ns(CPUARMState *env,
7416                                     const ARMCPRegInfo *ri, bool isread)
7417 {
7418     int el = arm_current_el(env);
7419 
7420     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7421         return CP_ACCESS_TRAP_EL2;
7422     }
7423     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7424         return CP_ACCESS_TRAP_EL3;
7425     }
7426     return CP_ACCESS_OK;
7427 }
7428 
7429 static CPAccessResult access_lor_other(CPUARMState *env,
7430                                        const ARMCPRegInfo *ri, bool isread)
7431 {
7432     if (arm_is_secure_below_el3(env)) {
7433         /* Access denied in secure mode.  */
7434         return CP_ACCESS_TRAP;
7435     }
7436     return access_lor_ns(env, ri, isread);
7437 }
7438 
7439 /*
7440  * A trivial implementation of ARMv8.1-LOR leaves all of these
7441  * registers fixed at 0, which indicates that there are zero
7442  * supported Limited Ordering regions.
7443  */
7444 static const ARMCPRegInfo lor_reginfo[] = {
7445     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7446       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7447       .access = PL1_RW, .accessfn = access_lor_other,
7448       .fgt = FGT_LORSA_EL1,
7449       .type = ARM_CP_CONST, .resetvalue = 0 },
7450     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7451       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7452       .access = PL1_RW, .accessfn = access_lor_other,
7453       .fgt = FGT_LOREA_EL1,
7454       .type = ARM_CP_CONST, .resetvalue = 0 },
7455     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7456       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7457       .access = PL1_RW, .accessfn = access_lor_other,
7458       .fgt = FGT_LORN_EL1,
7459       .type = ARM_CP_CONST, .resetvalue = 0 },
7460     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7461       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7462       .access = PL1_RW, .accessfn = access_lor_other,
7463       .fgt = FGT_LORC_EL1,
7464       .type = ARM_CP_CONST, .resetvalue = 0 },
7465     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7466       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7467       .access = PL1_R, .accessfn = access_lor_ns,
7468       .fgt = FGT_LORID_EL1,
7469       .type = ARM_CP_CONST, .resetvalue = 0 },
7470 };
7471 
7472 #ifdef TARGET_AARCH64
7473 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7474                                    bool isread)
7475 {
7476     int el = arm_current_el(env);
7477 
7478     if (el < 2 &&
7479         arm_is_el2_enabled(env) &&
7480         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7481         return CP_ACCESS_TRAP_EL2;
7482     }
7483     if (el < 3 &&
7484         arm_feature(env, ARM_FEATURE_EL3) &&
7485         !(env->cp15.scr_el3 & SCR_APK)) {
7486         return CP_ACCESS_TRAP_EL3;
7487     }
7488     return CP_ACCESS_OK;
7489 }
7490 
7491 static const ARMCPRegInfo pauth_reginfo[] = {
7492     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7493       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7494       .access = PL1_RW, .accessfn = access_pauth,
7495       .fgt = FGT_APDAKEY,
7496       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7497     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7498       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7499       .access = PL1_RW, .accessfn = access_pauth,
7500       .fgt = FGT_APDAKEY,
7501       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7502     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7503       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7504       .access = PL1_RW, .accessfn = access_pauth,
7505       .fgt = FGT_APDBKEY,
7506       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7507     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7508       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7509       .access = PL1_RW, .accessfn = access_pauth,
7510       .fgt = FGT_APDBKEY,
7511       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7512     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7513       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7514       .access = PL1_RW, .accessfn = access_pauth,
7515       .fgt = FGT_APGAKEY,
7516       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7517     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7518       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7519       .access = PL1_RW, .accessfn = access_pauth,
7520       .fgt = FGT_APGAKEY,
7521       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7522     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7523       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7524       .access = PL1_RW, .accessfn = access_pauth,
7525       .fgt = FGT_APIAKEY,
7526       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7527     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7528       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7529       .access = PL1_RW, .accessfn = access_pauth,
7530       .fgt = FGT_APIAKEY,
7531       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7532     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7533       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7534       .access = PL1_RW, .accessfn = access_pauth,
7535       .fgt = FGT_APIBKEY,
7536       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7537     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7538       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7539       .access = PL1_RW, .accessfn = access_pauth,
7540       .fgt = FGT_APIBKEY,
7541       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7542 };
7543 
7544 static const ARMCPRegInfo tlbirange_reginfo[] = {
7545     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7546       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7547       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7548       .fgt = FGT_TLBIRVAE1IS,
7549       .writefn = tlbi_aa64_rvae1is_write },
7550     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7551       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7552       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7553       .fgt = FGT_TLBIRVAAE1IS,
7554       .writefn = tlbi_aa64_rvae1is_write },
7555    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7556       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7557       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7558       .fgt = FGT_TLBIRVALE1IS,
7559       .writefn = tlbi_aa64_rvae1is_write },
7560     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7561       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7562       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7563       .fgt = FGT_TLBIRVAALE1IS,
7564       .writefn = tlbi_aa64_rvae1is_write },
7565     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7566       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7567       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7568       .fgt = FGT_TLBIRVAE1OS,
7569       .writefn = tlbi_aa64_rvae1is_write },
7570     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7571       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7572       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7573       .fgt = FGT_TLBIRVAAE1OS,
7574       .writefn = tlbi_aa64_rvae1is_write },
7575    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7576       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7577       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7578       .fgt = FGT_TLBIRVALE1OS,
7579       .writefn = tlbi_aa64_rvae1is_write },
7580     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7581       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7582       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7583       .fgt = FGT_TLBIRVAALE1OS,
7584       .writefn = tlbi_aa64_rvae1is_write },
7585     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7586       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7587       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7588       .fgt = FGT_TLBIRVAE1,
7589       .writefn = tlbi_aa64_rvae1_write },
7590     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7591       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7592       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7593       .fgt = FGT_TLBIRVAAE1,
7594       .writefn = tlbi_aa64_rvae1_write },
7595    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7596       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7597       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7598       .fgt = FGT_TLBIRVALE1,
7599       .writefn = tlbi_aa64_rvae1_write },
7600     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7601       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7602       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7603       .fgt = FGT_TLBIRVAALE1,
7604       .writefn = tlbi_aa64_rvae1_write },
7605     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7606       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7607       .access = PL2_W, .type = ARM_CP_NO_RAW,
7608       .writefn = tlbi_aa64_ripas2e1is_write },
7609     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7610       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7611       .access = PL2_W, .type = ARM_CP_NO_RAW,
7612       .writefn = tlbi_aa64_ripas2e1is_write },
7613     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7614       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7615       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7616       .writefn = tlbi_aa64_rvae2is_write },
7617    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7618       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7619       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7620       .writefn = tlbi_aa64_rvae2is_write },
7621     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7622       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7623       .access = PL2_W, .type = ARM_CP_NO_RAW,
7624       .writefn = tlbi_aa64_ripas2e1_write },
7625     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7626       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7627       .access = PL2_W, .type = ARM_CP_NO_RAW,
7628       .writefn = tlbi_aa64_ripas2e1_write },
7629    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7630       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7631       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7632       .writefn = tlbi_aa64_rvae2is_write },
7633    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7634       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7635       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7636       .writefn = tlbi_aa64_rvae2is_write },
7637     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7638       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7639       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7640       .writefn = tlbi_aa64_rvae2_write },
7641    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7642       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7643       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7644       .writefn = tlbi_aa64_rvae2_write },
7645    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7646       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7647       .access = PL3_W, .type = ARM_CP_NO_RAW,
7648       .writefn = tlbi_aa64_rvae3is_write },
7649    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7650       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7651       .access = PL3_W, .type = ARM_CP_NO_RAW,
7652       .writefn = tlbi_aa64_rvae3is_write },
7653    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7654       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7655       .access = PL3_W, .type = ARM_CP_NO_RAW,
7656       .writefn = tlbi_aa64_rvae3is_write },
7657    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7658       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7659       .access = PL3_W, .type = ARM_CP_NO_RAW,
7660       .writefn = tlbi_aa64_rvae3is_write },
7661    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7662       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7663       .access = PL3_W, .type = ARM_CP_NO_RAW,
7664       .writefn = tlbi_aa64_rvae3_write },
7665    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7666       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7667       .access = PL3_W, .type = ARM_CP_NO_RAW,
7668       .writefn = tlbi_aa64_rvae3_write },
7669 };
7670 
7671 static const ARMCPRegInfo tlbios_reginfo[] = {
7672     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7673       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7674       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7675       .fgt = FGT_TLBIVMALLE1OS,
7676       .writefn = tlbi_aa64_vmalle1is_write },
7677     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7678       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7679       .fgt = FGT_TLBIVAE1OS,
7680       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7681       .writefn = tlbi_aa64_vae1is_write },
7682     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7683       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7684       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7685       .fgt = FGT_TLBIASIDE1OS,
7686       .writefn = tlbi_aa64_vmalle1is_write },
7687     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7688       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7689       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7690       .fgt = FGT_TLBIVAAE1OS,
7691       .writefn = tlbi_aa64_vae1is_write },
7692     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7693       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7694       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7695       .fgt = FGT_TLBIVALE1OS,
7696       .writefn = tlbi_aa64_vae1is_write },
7697     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7698       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7699       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7700       .fgt = FGT_TLBIVAALE1OS,
7701       .writefn = tlbi_aa64_vae1is_write },
7702     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7703       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7704       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7705       .writefn = tlbi_aa64_alle2is_write },
7706     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7707       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7708       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7709       .writefn = tlbi_aa64_vae2is_write },
7710    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7712       .access = PL2_W, .type = ARM_CP_NO_RAW,
7713       .writefn = tlbi_aa64_alle1is_write },
7714     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7715       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7716       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7717       .writefn = tlbi_aa64_vae2is_write },
7718     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7719       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7720       .access = PL2_W, .type = ARM_CP_NO_RAW,
7721       .writefn = tlbi_aa64_alle1is_write },
7722     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7723       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7724       .access = PL2_W, .type = ARM_CP_NOP },
7725     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7726       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7727       .access = PL2_W, .type = ARM_CP_NOP },
7728     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7729       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7730       .access = PL2_W, .type = ARM_CP_NOP },
7731     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7732       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7733       .access = PL2_W, .type = ARM_CP_NOP },
7734     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7735       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7736       .access = PL3_W, .type = ARM_CP_NO_RAW,
7737       .writefn = tlbi_aa64_alle3is_write },
7738     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7739       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7740       .access = PL3_W, .type = ARM_CP_NO_RAW,
7741       .writefn = tlbi_aa64_vae3is_write },
7742     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7743       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7744       .access = PL3_W, .type = ARM_CP_NO_RAW,
7745       .writefn = tlbi_aa64_vae3is_write },
7746 };
7747 
7748 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7749 {
7750     Error *err = NULL;
7751     uint64_t ret;
7752 
7753     /* Success sets NZCV = 0000.  */
7754     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7755 
7756     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7757         /*
7758          * ??? Failed, for unknown reasons in the crypto subsystem.
7759          * The best we can do is log the reason and return the
7760          * timed-out indication to the guest.  There is no reason
7761          * we know to expect this failure to be transitory, so the
7762          * guest may well hang retrying the operation.
7763          */
7764         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7765                       ri->name, error_get_pretty(err));
7766         error_free(err);
7767 
7768         env->ZF = 0; /* NZCF = 0100 */
7769         return 0;
7770     }
7771     return ret;
7772 }
7773 
7774 /* We do not support re-seeding, so the two registers operate the same.  */
7775 static const ARMCPRegInfo rndr_reginfo[] = {
7776     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7777       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7778       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7779       .access = PL0_R, .readfn = rndr_readfn },
7780     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7781       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7782       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7783       .access = PL0_R, .readfn = rndr_readfn },
7784 };
7785 
7786 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7787                           uint64_t value)
7788 {
7789 #ifdef CONFIG_TCG
7790     ARMCPU *cpu = env_archcpu(env);
7791     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7792     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7793     uint64_t vaddr_in = (uint64_t) value;
7794     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7795     void *haddr;
7796     int mem_idx = cpu_mmu_index(env, false);
7797 
7798     /* This won't be crossing page boundaries */
7799     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7800     if (haddr) {
7801 #ifndef CONFIG_USER_ONLY
7802 
7803         ram_addr_t offset;
7804         MemoryRegion *mr;
7805 
7806         /* RCU lock is already being held */
7807         mr = memory_region_from_host(haddr, &offset);
7808 
7809         if (mr) {
7810             memory_region_writeback(mr, offset, dline_size);
7811         }
7812 #endif /*CONFIG_USER_ONLY*/
7813     }
7814 #else
7815     /* Handled by hardware accelerator. */
7816     g_assert_not_reached();
7817 #endif /* CONFIG_TCG */
7818 }
7819 
7820 static const ARMCPRegInfo dcpop_reg[] = {
7821     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7822       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7823       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7824       .fgt = FGT_DCCVAP,
7825       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7826 };
7827 
7828 static const ARMCPRegInfo dcpodp_reg[] = {
7829     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7830       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7831       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7832       .fgt = FGT_DCCVADP,
7833       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7834 };
7835 
7836 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7837                                        bool isread)
7838 {
7839     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7840         return CP_ACCESS_TRAP_EL2;
7841     }
7842 
7843     return CP_ACCESS_OK;
7844 }
7845 
7846 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7847                                  bool isread)
7848 {
7849     int el = arm_current_el(env);
7850     if (el < 2 && arm_is_el2_enabled(env)) {
7851         uint64_t hcr = arm_hcr_el2_eff(env);
7852         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7853             return CP_ACCESS_TRAP_EL2;
7854         }
7855     }
7856     if (el < 3 &&
7857         arm_feature(env, ARM_FEATURE_EL3) &&
7858         !(env->cp15.scr_el3 & SCR_ATA)) {
7859         return CP_ACCESS_TRAP_EL3;
7860     }
7861     return CP_ACCESS_OK;
7862 }
7863 
7864 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7865                                       bool isread)
7866 {
7867     CPAccessResult nv1 = access_nv1(env, ri, isread);
7868 
7869     if (nv1 != CP_ACCESS_OK) {
7870         return nv1;
7871     }
7872     return access_mte(env, ri, isread);
7873 }
7874 
7875 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7876                                       bool isread)
7877 {
7878     /*
7879      * TFSR_EL2: similar to generic access_mte(), but we need to
7880      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7881      * if NV2 is enabled then we will redirect this to TFSR_EL1
7882      * after doing the HCR and SCR ATA traps; otherwise this will
7883      * be a trap to EL2 and the HCR/SCR traps do not apply.
7884      */
7885     int el = arm_current_el(env);
7886 
7887     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7888         return CP_ACCESS_OK;
7889     }
7890     if (el < 2 && arm_is_el2_enabled(env)) {
7891         uint64_t hcr = arm_hcr_el2_eff(env);
7892         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7893             return CP_ACCESS_TRAP_EL2;
7894         }
7895     }
7896     if (el < 3 &&
7897         arm_feature(env, ARM_FEATURE_EL3) &&
7898         !(env->cp15.scr_el3 & SCR_ATA)) {
7899         return CP_ACCESS_TRAP_EL3;
7900     }
7901     return CP_ACCESS_OK;
7902 }
7903 
7904 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7905 {
7906     return env->pstate & PSTATE_TCO;
7907 }
7908 
7909 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7910 {
7911     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7912 }
7913 
7914 static const ARMCPRegInfo mte_reginfo[] = {
7915     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7916       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7917       .access = PL1_RW, .accessfn = access_mte,
7918       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7919     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7920       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7921       .access = PL1_RW, .accessfn = access_tfsr_el1,
7922       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7923     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7924       .type = ARM_CP_NV2_REDIRECT,
7925       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7926       .access = PL2_RW, .accessfn = access_tfsr_el2,
7927       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7928     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7929       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7930       .access = PL3_RW,
7931       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7932     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7933       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7934       .access = PL1_RW, .accessfn = access_mte,
7935       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7936     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7937       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7938       .access = PL1_RW, .accessfn = access_mte,
7939       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7940     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7941       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7942       .type = ARM_CP_NO_RAW,
7943       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7944     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7945       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7946       .type = ARM_CP_NOP, .access = PL1_W,
7947       .fgt = FGT_DCIVAC,
7948       .accessfn = aa64_cacheop_poc_access },
7949     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7950       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7951       .fgt = FGT_DCISW,
7952       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7953     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7954       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7955       .type = ARM_CP_NOP, .access = PL1_W,
7956       .fgt = FGT_DCIVAC,
7957       .accessfn = aa64_cacheop_poc_access },
7958     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7959       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7960       .fgt = FGT_DCISW,
7961       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7962     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7963       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7964       .fgt = FGT_DCCSW,
7965       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7966     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7967       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7968       .fgt = FGT_DCCSW,
7969       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7970     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7971       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7972       .fgt = FGT_DCCISW,
7973       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7974     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7975       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7976       .fgt = FGT_DCCISW,
7977       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7978 };
7979 
7980 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7981     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7982       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7983       .type = ARM_CP_CONST, .access = PL0_RW, },
7984 };
7985 
7986 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7987     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7988       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7989       .type = ARM_CP_NOP, .access = PL0_W,
7990       .fgt = FGT_DCCVAC,
7991       .accessfn = aa64_cacheop_poc_access },
7992     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7993       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7994       .type = ARM_CP_NOP, .access = PL0_W,
7995       .fgt = FGT_DCCVAC,
7996       .accessfn = aa64_cacheop_poc_access },
7997     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7998       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7999       .type = ARM_CP_NOP, .access = PL0_W,
8000       .fgt = FGT_DCCVAP,
8001       .accessfn = aa64_cacheop_poc_access },
8002     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8003       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8004       .type = ARM_CP_NOP, .access = PL0_W,
8005       .fgt = FGT_DCCVAP,
8006       .accessfn = aa64_cacheop_poc_access },
8007     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8008       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8009       .type = ARM_CP_NOP, .access = PL0_W,
8010       .fgt = FGT_DCCVADP,
8011       .accessfn = aa64_cacheop_poc_access },
8012     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8013       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8014       .type = ARM_CP_NOP, .access = PL0_W,
8015       .fgt = FGT_DCCVADP,
8016       .accessfn = aa64_cacheop_poc_access },
8017     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8018       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8019       .type = ARM_CP_NOP, .access = PL0_W,
8020       .fgt = FGT_DCCIVAC,
8021       .accessfn = aa64_cacheop_poc_access },
8022     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8023       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8024       .type = ARM_CP_NOP, .access = PL0_W,
8025       .fgt = FGT_DCCIVAC,
8026       .accessfn = aa64_cacheop_poc_access },
8027     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8028       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8029       .access = PL0_W, .type = ARM_CP_DC_GVA,
8030 #ifndef CONFIG_USER_ONLY
8031       /* Avoid overhead of an access check that always passes in user-mode */
8032       .accessfn = aa64_zva_access,
8033       .fgt = FGT_DCZVA,
8034 #endif
8035     },
8036     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8037       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8038       .access = PL0_W, .type = ARM_CP_DC_GZVA,
8039 #ifndef CONFIG_USER_ONLY
8040       /* Avoid overhead of an access check that always passes in user-mode */
8041       .accessfn = aa64_zva_access,
8042       .fgt = FGT_DCZVA,
8043 #endif
8044     },
8045 };
8046 
8047 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8048                                      bool isread)
8049 {
8050     uint64_t hcr = arm_hcr_el2_eff(env);
8051     int el = arm_current_el(env);
8052 
8053     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8054         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8055             if (hcr & HCR_TGE) {
8056                 return CP_ACCESS_TRAP_EL2;
8057             }
8058             return CP_ACCESS_TRAP;
8059         }
8060     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8061         return CP_ACCESS_TRAP_EL2;
8062     }
8063     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8064         return CP_ACCESS_TRAP_EL2;
8065     }
8066     if (el < 3
8067         && arm_feature(env, ARM_FEATURE_EL3)
8068         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8069         return CP_ACCESS_TRAP_EL3;
8070     }
8071     return CP_ACCESS_OK;
8072 }
8073 
8074 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8075                                          const ARMCPRegInfo *ri,
8076                                          bool isread)
8077 {
8078     CPAccessResult nv1 = access_nv1(env, ri, isread);
8079 
8080     if (nv1 != CP_ACCESS_OK) {
8081         return nv1;
8082     }
8083     return access_scxtnum(env, ri, isread);
8084 }
8085 
8086 static const ARMCPRegInfo scxtnum_reginfo[] = {
8087     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8088       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8089       .access = PL0_RW, .accessfn = access_scxtnum,
8090       .fgt = FGT_SCXTNUM_EL0,
8091       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8092     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8093       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8094       .access = PL1_RW, .accessfn = access_scxtnum_el1,
8095       .fgt = FGT_SCXTNUM_EL1,
8096       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8097     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8098       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8099       .access = PL2_RW, .accessfn = access_scxtnum,
8100       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8101     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8102       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8103       .access = PL3_RW,
8104       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8105 };
8106 
8107 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8108                                  bool isread)
8109 {
8110     if (arm_current_el(env) == 2 &&
8111         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8112         return CP_ACCESS_TRAP_EL3;
8113     }
8114     return CP_ACCESS_OK;
8115 }
8116 
8117 static const ARMCPRegInfo fgt_reginfo[] = {
8118     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8119       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8120       .access = PL2_RW, .accessfn = access_fgt,
8121       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8122     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8123       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8124       .access = PL2_RW, .accessfn = access_fgt,
8125       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8126     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8127       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8128       .access = PL2_RW, .accessfn = access_fgt,
8129       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8130     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8131       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8132       .access = PL2_RW, .accessfn = access_fgt,
8133       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8134     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8135       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8136       .access = PL2_RW, .accessfn = access_fgt,
8137       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8138 };
8139 
8140 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8141                        uint64_t value)
8142 {
8143     /*
8144      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8145      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8146      * about the RESS bits at the top -- we choose the "generate an EL2
8147      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8148      * the ptw.c code detect the resulting invalid address).
8149      */
8150     env->cp15.vncr_el2 = value & ~0xfffULL;
8151 }
8152 
8153 static const ARMCPRegInfo nv2_reginfo[] = {
8154     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8155       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8156       .access = PL2_RW,
8157       .writefn = vncr_write,
8158       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8159 };
8160 
8161 #endif /* TARGET_AARCH64 */
8162 
8163 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8164                                      bool isread)
8165 {
8166     int el = arm_current_el(env);
8167 
8168     if (el == 0) {
8169         uint64_t sctlr = arm_sctlr(env, el);
8170         if (!(sctlr & SCTLR_EnRCTX)) {
8171             return CP_ACCESS_TRAP;
8172         }
8173     } else if (el == 1) {
8174         uint64_t hcr = arm_hcr_el2_eff(env);
8175         if (hcr & HCR_NV) {
8176             return CP_ACCESS_TRAP_EL2;
8177         }
8178     }
8179     return CP_ACCESS_OK;
8180 }
8181 
8182 static const ARMCPRegInfo predinv_reginfo[] = {
8183     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8184       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8185       .fgt = FGT_CFPRCTX,
8186       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8187     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8188       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8189       .fgt = FGT_DVPRCTX,
8190       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8191     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8192       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8193       .fgt = FGT_CPPRCTX,
8194       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8195     /*
8196      * Note the AArch32 opcodes have a different OPC1.
8197      */
8198     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8199       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8200       .fgt = FGT_CFPRCTX,
8201       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8202     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8203       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8204       .fgt = FGT_DVPRCTX,
8205       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8206     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8207       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8208       .fgt = FGT_CPPRCTX,
8209       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8210 };
8211 
8212 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8213 {
8214     /* Read the high 32 bits of the current CCSIDR */
8215     return extract64(ccsidr_read(env, ri), 32, 32);
8216 }
8217 
8218 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8219     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8220       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8221       .access = PL1_R,
8222       .accessfn = access_tid4,
8223       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8224 };
8225 
8226 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8227                                        bool isread)
8228 {
8229     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8230         return CP_ACCESS_TRAP_EL2;
8231     }
8232 
8233     return CP_ACCESS_OK;
8234 }
8235 
8236 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8237                                        bool isread)
8238 {
8239     if (arm_feature(env, ARM_FEATURE_V8)) {
8240         return access_aa64_tid3(env, ri, isread);
8241     }
8242 
8243     return CP_ACCESS_OK;
8244 }
8245 
8246 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8247                                      bool isread)
8248 {
8249     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8250         return CP_ACCESS_TRAP_EL2;
8251     }
8252 
8253     return CP_ACCESS_OK;
8254 }
8255 
8256 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8257                                         const ARMCPRegInfo *ri, bool isread)
8258 {
8259     /*
8260      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8261      * in v7A, not in v8A.
8262      */
8263     if (!arm_feature(env, ARM_FEATURE_V8) &&
8264         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8265         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8266         return CP_ACCESS_TRAP_EL2;
8267     }
8268     return CP_ACCESS_OK;
8269 }
8270 
8271 static const ARMCPRegInfo jazelle_regs[] = {
8272     { .name = "JIDR",
8273       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8274       .access = PL1_R, .accessfn = access_jazelle,
8275       .type = ARM_CP_CONST, .resetvalue = 0 },
8276     { .name = "JOSCR",
8277       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8278       .accessfn = access_joscr_jmcr,
8279       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8280     { .name = "JMCR",
8281       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8282       .accessfn = access_joscr_jmcr,
8283       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8284 };
8285 
8286 static const ARMCPRegInfo contextidr_el2 = {
8287     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8288     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8289     .access = PL2_RW,
8290     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8291 };
8292 
8293 static const ARMCPRegInfo vhe_reginfo[] = {
8294     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8295       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8296       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8297       .raw_writefn = raw_write,
8298       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8299 #ifndef CONFIG_USER_ONLY
8300     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8301       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8302       .fieldoffset =
8303         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8304       .type = ARM_CP_IO, .access = PL2_RW,
8305       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8306     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8307       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8308       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8309       .resetfn = gt_hv_timer_reset,
8310       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8311     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8312       .type = ARM_CP_IO,
8313       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8314       .access = PL2_RW,
8315       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8316       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8317     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8318       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8319       .type = ARM_CP_IO | ARM_CP_ALIAS,
8320       .access = PL2_RW, .accessfn = e2h_access,
8321       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8322       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8323     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8324       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8325       .type = ARM_CP_IO | ARM_CP_ALIAS,
8326       .access = PL2_RW, .accessfn = e2h_access,
8327       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8328       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8329     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8330       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8331       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8332       .access = PL2_RW, .accessfn = e2h_access,
8333       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8334     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8335       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8336       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8337       .access = PL2_RW, .accessfn = e2h_access,
8338       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8339     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8340       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8341       .type = ARM_CP_IO | ARM_CP_ALIAS,
8342       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8343       .access = PL2_RW, .accessfn = e2h_access,
8344       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8345     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8346       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8347       .type = ARM_CP_IO | ARM_CP_ALIAS,
8348       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8349       .access = PL2_RW, .accessfn = e2h_access,
8350       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8351 #endif
8352 };
8353 
8354 #ifndef CONFIG_USER_ONLY
8355 static const ARMCPRegInfo ats1e1_reginfo[] = {
8356     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8357       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8358       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8359       .fgt = FGT_ATS1E1RP,
8360       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8361     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8362       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8363       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8364       .fgt = FGT_ATS1E1WP,
8365       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8366 };
8367 
8368 static const ARMCPRegInfo ats1cp_reginfo[] = {
8369     { .name = "ATS1CPRP",
8370       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8371       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8372       .writefn = ats_write },
8373     { .name = "ATS1CPWP",
8374       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8375       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8376       .writefn = ats_write },
8377 };
8378 #endif
8379 
8380 /*
8381  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8382  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8383  * is non-zero, which is never for ARMv7, optionally in ARMv8
8384  * and mandatorily for ARMv8.2 and up.
8385  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8386  * implementation is RAZ/WI we can ignore this detail, as we
8387  * do for ACTLR.
8388  */
8389 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8390     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8391       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8392       .access = PL1_RW, .accessfn = access_tacr,
8393       .type = ARM_CP_CONST, .resetvalue = 0 },
8394     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8395       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8396       .access = PL2_RW, .type = ARM_CP_CONST,
8397       .resetvalue = 0 },
8398 };
8399 
8400 void register_cp_regs_for_features(ARMCPU *cpu)
8401 {
8402     /* Register all the coprocessor registers based on feature bits */
8403     CPUARMState *env = &cpu->env;
8404     if (arm_feature(env, ARM_FEATURE_M)) {
8405         /* M profile has no coprocessor registers */
8406         return;
8407     }
8408 
8409     define_arm_cp_regs(cpu, cp_reginfo);
8410     if (!arm_feature(env, ARM_FEATURE_V8)) {
8411         /*
8412          * Must go early as it is full of wildcards that may be
8413          * overridden by later definitions.
8414          */
8415         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8416     }
8417 
8418     if (arm_feature(env, ARM_FEATURE_V6)) {
8419         /* The ID registers all have impdef reset values */
8420         ARMCPRegInfo v6_idregs[] = {
8421             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8422               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8423               .access = PL1_R, .type = ARM_CP_CONST,
8424               .accessfn = access_aa32_tid3,
8425               .resetvalue = cpu->isar.id_pfr0 },
8426             /*
8427              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8428              * the value of the GIC field until after we define these regs.
8429              */
8430             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8431               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8432               .access = PL1_R, .type = ARM_CP_NO_RAW,
8433               .accessfn = access_aa32_tid3,
8434 #ifdef CONFIG_USER_ONLY
8435               .type = ARM_CP_CONST,
8436               .resetvalue = cpu->isar.id_pfr1,
8437 #else
8438               .type = ARM_CP_NO_RAW,
8439               .accessfn = access_aa32_tid3,
8440               .readfn = id_pfr1_read,
8441               .writefn = arm_cp_write_ignore
8442 #endif
8443             },
8444             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8445               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8446               .access = PL1_R, .type = ARM_CP_CONST,
8447               .accessfn = access_aa32_tid3,
8448               .resetvalue = cpu->isar.id_dfr0 },
8449             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8450               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8451               .access = PL1_R, .type = ARM_CP_CONST,
8452               .accessfn = access_aa32_tid3,
8453               .resetvalue = cpu->id_afr0 },
8454             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8455               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8456               .access = PL1_R, .type = ARM_CP_CONST,
8457               .accessfn = access_aa32_tid3,
8458               .resetvalue = cpu->isar.id_mmfr0 },
8459             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8460               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8461               .access = PL1_R, .type = ARM_CP_CONST,
8462               .accessfn = access_aa32_tid3,
8463               .resetvalue = cpu->isar.id_mmfr1 },
8464             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8465               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8466               .access = PL1_R, .type = ARM_CP_CONST,
8467               .accessfn = access_aa32_tid3,
8468               .resetvalue = cpu->isar.id_mmfr2 },
8469             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8470               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8471               .access = PL1_R, .type = ARM_CP_CONST,
8472               .accessfn = access_aa32_tid3,
8473               .resetvalue = cpu->isar.id_mmfr3 },
8474             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8475               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8476               .access = PL1_R, .type = ARM_CP_CONST,
8477               .accessfn = access_aa32_tid3,
8478               .resetvalue = cpu->isar.id_isar0 },
8479             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8480               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8481               .access = PL1_R, .type = ARM_CP_CONST,
8482               .accessfn = access_aa32_tid3,
8483               .resetvalue = cpu->isar.id_isar1 },
8484             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8485               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8486               .access = PL1_R, .type = ARM_CP_CONST,
8487               .accessfn = access_aa32_tid3,
8488               .resetvalue = cpu->isar.id_isar2 },
8489             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8490               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8491               .access = PL1_R, .type = ARM_CP_CONST,
8492               .accessfn = access_aa32_tid3,
8493               .resetvalue = cpu->isar.id_isar3 },
8494             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8495               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8496               .access = PL1_R, .type = ARM_CP_CONST,
8497               .accessfn = access_aa32_tid3,
8498               .resetvalue = cpu->isar.id_isar4 },
8499             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8500               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8501               .access = PL1_R, .type = ARM_CP_CONST,
8502               .accessfn = access_aa32_tid3,
8503               .resetvalue = cpu->isar.id_isar5 },
8504             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8505               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8506               .access = PL1_R, .type = ARM_CP_CONST,
8507               .accessfn = access_aa32_tid3,
8508               .resetvalue = cpu->isar.id_mmfr4 },
8509             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8510               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8511               .access = PL1_R, .type = ARM_CP_CONST,
8512               .accessfn = access_aa32_tid3,
8513               .resetvalue = cpu->isar.id_isar6 },
8514         };
8515         define_arm_cp_regs(cpu, v6_idregs);
8516         define_arm_cp_regs(cpu, v6_cp_reginfo);
8517     } else {
8518         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8519     }
8520     if (arm_feature(env, ARM_FEATURE_V6K)) {
8521         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8522     }
8523     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8524         !arm_feature(env, ARM_FEATURE_PMSA)) {
8525         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8526     }
8527     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8528         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8529     }
8530     if (arm_feature(env, ARM_FEATURE_V7)) {
8531         ARMCPRegInfo clidr = {
8532             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8533             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8534             .access = PL1_R, .type = ARM_CP_CONST,
8535             .accessfn = access_tid4,
8536             .fgt = FGT_CLIDR_EL1,
8537             .resetvalue = cpu->clidr
8538         };
8539         define_one_arm_cp_reg(cpu, &clidr);
8540         define_arm_cp_regs(cpu, v7_cp_reginfo);
8541         define_debug_regs(cpu);
8542         define_pmu_regs(cpu);
8543     } else {
8544         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8545     }
8546     if (arm_feature(env, ARM_FEATURE_V8)) {
8547         /*
8548          * v8 ID registers, which all have impdef reset values.
8549          * Note that within the ID register ranges the unused slots
8550          * must all RAZ, not UNDEF; future architecture versions may
8551          * define new registers here.
8552          * ID registers which are AArch64 views of the AArch32 ID registers
8553          * which already existed in v6 and v7 are handled elsewhere,
8554          * in v6_idregs[].
8555          */
8556         int i;
8557         ARMCPRegInfo v8_idregs[] = {
8558             /*
8559              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8560              * emulation because we don't know the right value for the
8561              * GIC field until after we define these regs.
8562              */
8563             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8564               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8565               .access = PL1_R,
8566 #ifdef CONFIG_USER_ONLY
8567               .type = ARM_CP_CONST,
8568               .resetvalue = cpu->isar.id_aa64pfr0
8569 #else
8570               .type = ARM_CP_NO_RAW,
8571               .accessfn = access_aa64_tid3,
8572               .readfn = id_aa64pfr0_read,
8573               .writefn = arm_cp_write_ignore
8574 #endif
8575             },
8576             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8577               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8578               .access = PL1_R, .type = ARM_CP_CONST,
8579               .accessfn = access_aa64_tid3,
8580               .resetvalue = cpu->isar.id_aa64pfr1},
8581             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8582               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8583               .access = PL1_R, .type = ARM_CP_CONST,
8584               .accessfn = access_aa64_tid3,
8585               .resetvalue = 0 },
8586             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8587               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8588               .access = PL1_R, .type = ARM_CP_CONST,
8589               .accessfn = access_aa64_tid3,
8590               .resetvalue = 0 },
8591             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8592               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8593               .access = PL1_R, .type = ARM_CP_CONST,
8594               .accessfn = access_aa64_tid3,
8595               .resetvalue = cpu->isar.id_aa64zfr0 },
8596             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8597               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8598               .access = PL1_R, .type = ARM_CP_CONST,
8599               .accessfn = access_aa64_tid3,
8600               .resetvalue = cpu->isar.id_aa64smfr0 },
8601             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8602               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8603               .access = PL1_R, .type = ARM_CP_CONST,
8604               .accessfn = access_aa64_tid3,
8605               .resetvalue = 0 },
8606             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8607               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8608               .access = PL1_R, .type = ARM_CP_CONST,
8609               .accessfn = access_aa64_tid3,
8610               .resetvalue = 0 },
8611             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8612               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8613               .access = PL1_R, .type = ARM_CP_CONST,
8614               .accessfn = access_aa64_tid3,
8615               .resetvalue = cpu->isar.id_aa64dfr0 },
8616             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8617               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8618               .access = PL1_R, .type = ARM_CP_CONST,
8619               .accessfn = access_aa64_tid3,
8620               .resetvalue = cpu->isar.id_aa64dfr1 },
8621             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8622               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8623               .access = PL1_R, .type = ARM_CP_CONST,
8624               .accessfn = access_aa64_tid3,
8625               .resetvalue = 0 },
8626             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8627               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8628               .access = PL1_R, .type = ARM_CP_CONST,
8629               .accessfn = access_aa64_tid3,
8630               .resetvalue = 0 },
8631             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8632               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8633               .access = PL1_R, .type = ARM_CP_CONST,
8634               .accessfn = access_aa64_tid3,
8635               .resetvalue = cpu->id_aa64afr0 },
8636             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8637               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8638               .access = PL1_R, .type = ARM_CP_CONST,
8639               .accessfn = access_aa64_tid3,
8640               .resetvalue = cpu->id_aa64afr1 },
8641             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8642               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8643               .access = PL1_R, .type = ARM_CP_CONST,
8644               .accessfn = access_aa64_tid3,
8645               .resetvalue = 0 },
8646             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8647               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8648               .access = PL1_R, .type = ARM_CP_CONST,
8649               .accessfn = access_aa64_tid3,
8650               .resetvalue = 0 },
8651             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8652               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8653               .access = PL1_R, .type = ARM_CP_CONST,
8654               .accessfn = access_aa64_tid3,
8655               .resetvalue = cpu->isar.id_aa64isar0 },
8656             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8657               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8658               .access = PL1_R, .type = ARM_CP_CONST,
8659               .accessfn = access_aa64_tid3,
8660               .resetvalue = cpu->isar.id_aa64isar1 },
8661             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8662               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8663               .access = PL1_R, .type = ARM_CP_CONST,
8664               .accessfn = access_aa64_tid3,
8665               .resetvalue = cpu->isar.id_aa64isar2 },
8666             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8667               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8668               .access = PL1_R, .type = ARM_CP_CONST,
8669               .accessfn = access_aa64_tid3,
8670               .resetvalue = 0 },
8671             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8672               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8673               .access = PL1_R, .type = ARM_CP_CONST,
8674               .accessfn = access_aa64_tid3,
8675               .resetvalue = 0 },
8676             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8677               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8678               .access = PL1_R, .type = ARM_CP_CONST,
8679               .accessfn = access_aa64_tid3,
8680               .resetvalue = 0 },
8681             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8682               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8683               .access = PL1_R, .type = ARM_CP_CONST,
8684               .accessfn = access_aa64_tid3,
8685               .resetvalue = 0 },
8686             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8687               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8688               .access = PL1_R, .type = ARM_CP_CONST,
8689               .accessfn = access_aa64_tid3,
8690               .resetvalue = 0 },
8691             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8692               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8693               .access = PL1_R, .type = ARM_CP_CONST,
8694               .accessfn = access_aa64_tid3,
8695               .resetvalue = cpu->isar.id_aa64mmfr0 },
8696             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8697               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8698               .access = PL1_R, .type = ARM_CP_CONST,
8699               .accessfn = access_aa64_tid3,
8700               .resetvalue = cpu->isar.id_aa64mmfr1 },
8701             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8702               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8703               .access = PL1_R, .type = ARM_CP_CONST,
8704               .accessfn = access_aa64_tid3,
8705               .resetvalue = cpu->isar.id_aa64mmfr2 },
8706             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8707               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8708               .access = PL1_R, .type = ARM_CP_CONST,
8709               .accessfn = access_aa64_tid3,
8710               .resetvalue = 0 },
8711             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8712               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8713               .access = PL1_R, .type = ARM_CP_CONST,
8714               .accessfn = access_aa64_tid3,
8715               .resetvalue = 0 },
8716             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8717               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8718               .access = PL1_R, .type = ARM_CP_CONST,
8719               .accessfn = access_aa64_tid3,
8720               .resetvalue = 0 },
8721             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8722               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8723               .access = PL1_R, .type = ARM_CP_CONST,
8724               .accessfn = access_aa64_tid3,
8725               .resetvalue = 0 },
8726             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8727               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8728               .access = PL1_R, .type = ARM_CP_CONST,
8729               .accessfn = access_aa64_tid3,
8730               .resetvalue = 0 },
8731             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8732               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8733               .access = PL1_R, .type = ARM_CP_CONST,
8734               .accessfn = access_aa64_tid3,
8735               .resetvalue = cpu->isar.mvfr0 },
8736             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8737               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8738               .access = PL1_R, .type = ARM_CP_CONST,
8739               .accessfn = access_aa64_tid3,
8740               .resetvalue = cpu->isar.mvfr1 },
8741             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8742               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8743               .access = PL1_R, .type = ARM_CP_CONST,
8744               .accessfn = access_aa64_tid3,
8745               .resetvalue = cpu->isar.mvfr2 },
8746             /*
8747              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8748              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8749              * as RAZ, since it is in the "reserved for future ID
8750              * registers, RAZ" part of the AArch32 encoding space.
8751              */
8752             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8753               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8754               .access = PL1_R, .type = ARM_CP_CONST,
8755               .accessfn = access_aa64_tid3,
8756               .resetvalue = 0 },
8757             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8758               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8759               .access = PL1_R, .type = ARM_CP_CONST,
8760               .accessfn = access_aa64_tid3,
8761               .resetvalue = 0 },
8762             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8763               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8764               .access = PL1_R, .type = ARM_CP_CONST,
8765               .accessfn = access_aa64_tid3,
8766               .resetvalue = 0 },
8767             /*
8768              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8769              * they're also RAZ for AArch64, and in v8 are gradually
8770              * being filled with AArch64-view-of-AArch32-ID-register
8771              * for new ID registers.
8772              */
8773             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8774               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8775               .access = PL1_R, .type = ARM_CP_CONST,
8776               .accessfn = access_aa64_tid3,
8777               .resetvalue = 0 },
8778             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8779               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8780               .access = PL1_R, .type = ARM_CP_CONST,
8781               .accessfn = access_aa64_tid3,
8782               .resetvalue = cpu->isar.id_pfr2 },
8783             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8785               .access = PL1_R, .type = ARM_CP_CONST,
8786               .accessfn = access_aa64_tid3,
8787               .resetvalue = cpu->isar.id_dfr1 },
8788             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8790               .access = PL1_R, .type = ARM_CP_CONST,
8791               .accessfn = access_aa64_tid3,
8792               .resetvalue = cpu->isar.id_mmfr5 },
8793             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8794               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8795               .access = PL1_R, .type = ARM_CP_CONST,
8796               .accessfn = access_aa64_tid3,
8797               .resetvalue = 0 },
8798             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8799               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8800               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8801               .fgt = FGT_PMCEIDN_EL0,
8802               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8803             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8804               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8805               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8806               .fgt = FGT_PMCEIDN_EL0,
8807               .resetvalue = cpu->pmceid0 },
8808             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8809               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8810               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8811               .fgt = FGT_PMCEIDN_EL0,
8812               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8813             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8814               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8815               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8816               .fgt = FGT_PMCEIDN_EL0,
8817               .resetvalue = cpu->pmceid1 },
8818         };
8819 #ifdef CONFIG_USER_ONLY
8820         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8821             { .name = "ID_AA64PFR0_EL1",
8822               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8823                                R_ID_AA64PFR0_ADVSIMD_MASK |
8824                                R_ID_AA64PFR0_SVE_MASK |
8825                                R_ID_AA64PFR0_DIT_MASK,
8826               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8827                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8828             { .name = "ID_AA64PFR1_EL1",
8829               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8830                                R_ID_AA64PFR1_SSBS_MASK |
8831                                R_ID_AA64PFR1_MTE_MASK |
8832                                R_ID_AA64PFR1_SME_MASK },
8833             { .name = "ID_AA64PFR*_EL1_RESERVED",
8834               .is_glob = true },
8835             { .name = "ID_AA64ZFR0_EL1",
8836               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8837                                R_ID_AA64ZFR0_AES_MASK |
8838                                R_ID_AA64ZFR0_BITPERM_MASK |
8839                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8840                                R_ID_AA64ZFR0_SHA3_MASK |
8841                                R_ID_AA64ZFR0_SM4_MASK |
8842                                R_ID_AA64ZFR0_I8MM_MASK |
8843                                R_ID_AA64ZFR0_F32MM_MASK |
8844                                R_ID_AA64ZFR0_F64MM_MASK },
8845             { .name = "ID_AA64SMFR0_EL1",
8846               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8847                                R_ID_AA64SMFR0_BI32I32_MASK |
8848                                R_ID_AA64SMFR0_B16F32_MASK |
8849                                R_ID_AA64SMFR0_F16F32_MASK |
8850                                R_ID_AA64SMFR0_I8I32_MASK |
8851                                R_ID_AA64SMFR0_F16F16_MASK |
8852                                R_ID_AA64SMFR0_B16B16_MASK |
8853                                R_ID_AA64SMFR0_I16I32_MASK |
8854                                R_ID_AA64SMFR0_F64F64_MASK |
8855                                R_ID_AA64SMFR0_I16I64_MASK |
8856                                R_ID_AA64SMFR0_SMEVER_MASK |
8857                                R_ID_AA64SMFR0_FA64_MASK },
8858             { .name = "ID_AA64MMFR0_EL1",
8859               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8860               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8861                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8862             { .name = "ID_AA64MMFR1_EL1",
8863               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8864             { .name = "ID_AA64MMFR2_EL1",
8865               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8866             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8867               .is_glob = true },
8868             { .name = "ID_AA64DFR0_EL1",
8869               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8870             { .name = "ID_AA64DFR1_EL1" },
8871             { .name = "ID_AA64DFR*_EL1_RESERVED",
8872               .is_glob = true },
8873             { .name = "ID_AA64AFR*",
8874               .is_glob = true },
8875             { .name = "ID_AA64ISAR0_EL1",
8876               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8877                                R_ID_AA64ISAR0_SHA1_MASK |
8878                                R_ID_AA64ISAR0_SHA2_MASK |
8879                                R_ID_AA64ISAR0_CRC32_MASK |
8880                                R_ID_AA64ISAR0_ATOMIC_MASK |
8881                                R_ID_AA64ISAR0_RDM_MASK |
8882                                R_ID_AA64ISAR0_SHA3_MASK |
8883                                R_ID_AA64ISAR0_SM3_MASK |
8884                                R_ID_AA64ISAR0_SM4_MASK |
8885                                R_ID_AA64ISAR0_DP_MASK |
8886                                R_ID_AA64ISAR0_FHM_MASK |
8887                                R_ID_AA64ISAR0_TS_MASK |
8888                                R_ID_AA64ISAR0_RNDR_MASK },
8889             { .name = "ID_AA64ISAR1_EL1",
8890               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8891                                R_ID_AA64ISAR1_APA_MASK |
8892                                R_ID_AA64ISAR1_API_MASK |
8893                                R_ID_AA64ISAR1_JSCVT_MASK |
8894                                R_ID_AA64ISAR1_FCMA_MASK |
8895                                R_ID_AA64ISAR1_LRCPC_MASK |
8896                                R_ID_AA64ISAR1_GPA_MASK |
8897                                R_ID_AA64ISAR1_GPI_MASK |
8898                                R_ID_AA64ISAR1_FRINTTS_MASK |
8899                                R_ID_AA64ISAR1_SB_MASK |
8900                                R_ID_AA64ISAR1_BF16_MASK |
8901                                R_ID_AA64ISAR1_DGH_MASK |
8902                                R_ID_AA64ISAR1_I8MM_MASK },
8903             { .name = "ID_AA64ISAR2_EL1",
8904               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8905                                R_ID_AA64ISAR2_RPRES_MASK |
8906                                R_ID_AA64ISAR2_GPA3_MASK |
8907                                R_ID_AA64ISAR2_APA3_MASK |
8908                                R_ID_AA64ISAR2_MOPS_MASK |
8909                                R_ID_AA64ISAR2_BC_MASK |
8910                                R_ID_AA64ISAR2_RPRFM_MASK |
8911                                R_ID_AA64ISAR2_CSSC_MASK },
8912             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8913               .is_glob = true },
8914         };
8915         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8916 #endif
8917         /*
8918          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8919          * TODO: For RMR, a write with bit 1 set should do something with
8920          * cpu_reset(). In the meantime, "the bit is strictly a request",
8921          * so we are in spec just ignoring writes.
8922          */
8923         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8924             !arm_feature(env, ARM_FEATURE_EL2)) {
8925             ARMCPRegInfo el1_reset_regs[] = {
8926                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8927                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8928                   .access = PL1_R,
8929                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8930                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8931                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8932                   .access = PL1_RW, .type = ARM_CP_CONST,
8933                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8934             };
8935             define_arm_cp_regs(cpu, el1_reset_regs);
8936         }
8937         define_arm_cp_regs(cpu, v8_idregs);
8938         define_arm_cp_regs(cpu, v8_cp_reginfo);
8939         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8940             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8941         }
8942 
8943         for (i = 4; i < 16; i++) {
8944             /*
8945              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8946              * For pre-v8 cores there are RAZ patterns for these in
8947              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8948              * v8 extends the "must RAZ" part of the ID register space
8949              * to also cover c0, 0, c{8-15}, {0-7}.
8950              * These are STATE_AA32 because in the AArch64 sysreg space
8951              * c4-c7 is where the AArch64 ID registers live (and we've
8952              * already defined those in v8_idregs[]), and c8-c15 are not
8953              * "must RAZ" for AArch64.
8954              */
8955             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8956             ARMCPRegInfo v8_aa32_raz_idregs = {
8957                 .name = name,
8958                 .state = ARM_CP_STATE_AA32,
8959                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8960                 .access = PL1_R, .type = ARM_CP_CONST,
8961                 .accessfn = access_aa64_tid3,
8962                 .resetvalue = 0 };
8963             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8964         }
8965     }
8966 
8967     /*
8968      * Register the base EL2 cpregs.
8969      * Pre v8, these registers are implemented only as part of the
8970      * Virtualization Extensions (EL2 present).  Beginning with v8,
8971      * if EL2 is missing but EL3 is enabled, mostly these become
8972      * RES0 from EL3, with some specific exceptions.
8973      */
8974     if (arm_feature(env, ARM_FEATURE_EL2)
8975         || (arm_feature(env, ARM_FEATURE_EL3)
8976             && arm_feature(env, ARM_FEATURE_V8))) {
8977         uint64_t vmpidr_def = mpidr_read_val(env);
8978         ARMCPRegInfo vpidr_regs[] = {
8979             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8980               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8981               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8982               .resetvalue = cpu->midr,
8983               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8984               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8985             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8986               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8987               .access = PL2_RW, .resetvalue = cpu->midr,
8988               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8989               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8990             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8991               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8992               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8993               .resetvalue = vmpidr_def,
8994               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8995               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8996             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8997               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8998               .access = PL2_RW, .resetvalue = vmpidr_def,
8999               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9000               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9001         };
9002         /*
9003          * The only field of MDCR_EL2 that has a defined architectural reset
9004          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9005          */
9006         ARMCPRegInfo mdcr_el2 = {
9007             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9008             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9009             .writefn = mdcr_el2_write,
9010             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9011             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9012         };
9013         define_one_arm_cp_reg(cpu, &mdcr_el2);
9014         define_arm_cp_regs(cpu, vpidr_regs);
9015         define_arm_cp_regs(cpu, el2_cp_reginfo);
9016         if (arm_feature(env, ARM_FEATURE_V8)) {
9017             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9018         }
9019         if (cpu_isar_feature(aa64_sel2, cpu)) {
9020             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9021         }
9022         /*
9023          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9024          * See commentary near RMR_EL1.
9025          */
9026         if (!arm_feature(env, ARM_FEATURE_EL3)) {
9027             static const ARMCPRegInfo el2_reset_regs[] = {
9028                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9029                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9030                   .access = PL2_R,
9031                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9032                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9033                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9034                   .access = PL2_R,
9035                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9036                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9037                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9038                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9039             };
9040             define_arm_cp_regs(cpu, el2_reset_regs);
9041         }
9042     }
9043 
9044     /* Register the base EL3 cpregs. */
9045     if (arm_feature(env, ARM_FEATURE_EL3)) {
9046         define_arm_cp_regs(cpu, el3_cp_reginfo);
9047         ARMCPRegInfo el3_regs[] = {
9048             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9049               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9050               .access = PL3_R,
9051               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9052             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9053               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9054               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9055             { .name = "RMR", .state = ARM_CP_STATE_AA32,
9056               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9057               .access = PL3_RW, .type = ARM_CP_CONST,
9058               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9059             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9060               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9061               .access = PL3_RW,
9062               .raw_writefn = raw_write, .writefn = sctlr_write,
9063               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9064               .resetvalue = cpu->reset_sctlr },
9065         };
9066 
9067         define_arm_cp_regs(cpu, el3_regs);
9068     }
9069     /*
9070      * The behaviour of NSACR is sufficiently various that we don't
9071      * try to describe it in a single reginfo:
9072      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
9073      *     reads as constant 0xc00 from NS EL1 and NS EL2
9074      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9075      *  if v7 without EL3, register doesn't exist
9076      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9077      */
9078     if (arm_feature(env, ARM_FEATURE_EL3)) {
9079         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9080             static const ARMCPRegInfo nsacr = {
9081                 .name = "NSACR", .type = ARM_CP_CONST,
9082                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9083                 .access = PL1_RW, .accessfn = nsacr_access,
9084                 .resetvalue = 0xc00
9085             };
9086             define_one_arm_cp_reg(cpu, &nsacr);
9087         } else {
9088             static const ARMCPRegInfo nsacr = {
9089                 .name = "NSACR",
9090                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9091                 .access = PL3_RW | PL1_R,
9092                 .resetvalue = 0,
9093                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9094             };
9095             define_one_arm_cp_reg(cpu, &nsacr);
9096         }
9097     } else {
9098         if (arm_feature(env, ARM_FEATURE_V8)) {
9099             static const ARMCPRegInfo nsacr = {
9100                 .name = "NSACR", .type = ARM_CP_CONST,
9101                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9102                 .access = PL1_R,
9103                 .resetvalue = 0xc00
9104             };
9105             define_one_arm_cp_reg(cpu, &nsacr);
9106         }
9107     }
9108 
9109     if (arm_feature(env, ARM_FEATURE_PMSA)) {
9110         if (arm_feature(env, ARM_FEATURE_V6)) {
9111             /* PMSAv6 not implemented */
9112             assert(arm_feature(env, ARM_FEATURE_V7));
9113             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9114             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9115         } else {
9116             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9117         }
9118     } else {
9119         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9120         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9121         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
9122         if (cpu_isar_feature(aa32_hpd, cpu)) {
9123             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9124         }
9125     }
9126     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9127         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9128     }
9129     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9130         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9131     }
9132     if (arm_feature(env, ARM_FEATURE_VAPA)) {
9133         ARMCPRegInfo vapa_cp_reginfo[] = {
9134             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9135               .access = PL1_RW, .resetvalue = 0,
9136               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9137                                      offsetoflow32(CPUARMState, cp15.par_ns) },
9138               .writefn = par_write},
9139 #ifndef CONFIG_USER_ONLY
9140             /* This underdecoding is safe because the reginfo is NO_RAW. */
9141             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9142               .access = PL1_W, .accessfn = ats_access,
9143               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9144 #endif
9145         };
9146 
9147         /*
9148          * When LPAE exists this 32-bit PAR register is an alias of the
9149          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9150          */
9151         if (arm_feature(env, ARM_FEATURE_LPAE)) {
9152             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9153         }
9154         define_arm_cp_regs(cpu, vapa_cp_reginfo);
9155     }
9156     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9157         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9158     }
9159     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9160         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9161     }
9162     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9163         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9164     }
9165     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9166         define_arm_cp_regs(cpu, omap_cp_reginfo);
9167     }
9168     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9169         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9170     }
9171     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9172         define_arm_cp_regs(cpu, xscale_cp_reginfo);
9173     }
9174     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9175         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9176     }
9177     if (arm_feature(env, ARM_FEATURE_LPAE)) {
9178         define_arm_cp_regs(cpu, lpae_cp_reginfo);
9179     }
9180     if (cpu_isar_feature(aa32_jazelle, cpu)) {
9181         define_arm_cp_regs(cpu, jazelle_regs);
9182     }
9183     /*
9184      * Slightly awkwardly, the OMAP and StrongARM cores need all of
9185      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9186      * be read-only (ie write causes UNDEF exception).
9187      */
9188     {
9189         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9190             /*
9191              * Pre-v8 MIDR space.
9192              * Note that the MIDR isn't a simple constant register because
9193              * of the TI925 behaviour where writes to another register can
9194              * cause the MIDR value to change.
9195              *
9196              * Unimplemented registers in the c15 0 0 0 space default to
9197              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9198              * and friends override accordingly.
9199              */
9200             { .name = "MIDR",
9201               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9202               .access = PL1_R, .resetvalue = cpu->midr,
9203               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9204               .readfn = midr_read,
9205               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9206               .type = ARM_CP_OVERRIDE },
9207             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9208             { .name = "DUMMY",
9209               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9210               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9211             { .name = "DUMMY",
9212               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9213               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9214             { .name = "DUMMY",
9215               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9216               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9217             { .name = "DUMMY",
9218               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9219               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9220             { .name = "DUMMY",
9221               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9222               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9223         };
9224         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9225             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9226               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9227               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9228               .fgt = FGT_MIDR_EL1,
9229               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9230               .readfn = midr_read },
9231             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9232             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9233               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9234               .access = PL1_R, .resetvalue = cpu->midr },
9235             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9236               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9237               .access = PL1_R,
9238               .accessfn = access_aa64_tid1,
9239               .fgt = FGT_REVIDR_EL1,
9240               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9241         };
9242         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9243             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9244             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9245             .access = PL1_R, .resetvalue = cpu->midr
9246         };
9247         ARMCPRegInfo id_cp_reginfo[] = {
9248             /* These are common to v8 and pre-v8 */
9249             { .name = "CTR",
9250               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9251               .access = PL1_R, .accessfn = ctr_el0_access,
9252               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9253             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9254               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9255               .access = PL0_R, .accessfn = ctr_el0_access,
9256               .fgt = FGT_CTR_EL0,
9257               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9258             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9259             { .name = "TCMTR",
9260               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9261               .access = PL1_R,
9262               .accessfn = access_aa32_tid1,
9263               .type = ARM_CP_CONST, .resetvalue = 0 },
9264         };
9265         /* TLBTR is specific to VMSA */
9266         ARMCPRegInfo id_tlbtr_reginfo = {
9267               .name = "TLBTR",
9268               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9269               .access = PL1_R,
9270               .accessfn = access_aa32_tid1,
9271               .type = ARM_CP_CONST, .resetvalue = 0,
9272         };
9273         /* MPUIR is specific to PMSA V6+ */
9274         ARMCPRegInfo id_mpuir_reginfo = {
9275               .name = "MPUIR",
9276               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9277               .access = PL1_R, .type = ARM_CP_CONST,
9278               .resetvalue = cpu->pmsav7_dregion << 8
9279         };
9280         /* HMPUIR is specific to PMSA V8 */
9281         ARMCPRegInfo id_hmpuir_reginfo = {
9282             .name = "HMPUIR",
9283             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9284             .access = PL2_R, .type = ARM_CP_CONST,
9285             .resetvalue = cpu->pmsav8r_hdregion
9286         };
9287         static const ARMCPRegInfo crn0_wi_reginfo = {
9288             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9289             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9290             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9291         };
9292 #ifdef CONFIG_USER_ONLY
9293         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9294             { .name = "MIDR_EL1",
9295               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9296                                R_MIDR_EL1_PARTNUM_MASK |
9297                                R_MIDR_EL1_ARCHITECTURE_MASK |
9298                                R_MIDR_EL1_VARIANT_MASK |
9299                                R_MIDR_EL1_IMPLEMENTER_MASK },
9300             { .name = "REVIDR_EL1" },
9301         };
9302         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9303 #endif
9304         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9305             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9306             size_t i;
9307             /*
9308              * Register the blanket "writes ignored" value first to cover the
9309              * whole space. Then update the specific ID registers to allow write
9310              * access, so that they ignore writes rather than causing them to
9311              * UNDEF.
9312              */
9313             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9314             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9315                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9316             }
9317             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9318                 id_cp_reginfo[i].access = PL1_RW;
9319             }
9320             id_mpuir_reginfo.access = PL1_RW;
9321             id_tlbtr_reginfo.access = PL1_RW;
9322         }
9323         if (arm_feature(env, ARM_FEATURE_V8)) {
9324             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9325             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9326                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9327             }
9328         } else {
9329             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9330         }
9331         define_arm_cp_regs(cpu, id_cp_reginfo);
9332         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9333             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9334         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9335                    arm_feature(env, ARM_FEATURE_V8)) {
9336             uint32_t i = 0;
9337             char *tmp_string;
9338 
9339             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9340             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9341             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9342 
9343             /* Register alias is only valid for first 32 indexes */
9344             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9345                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9346                 uint8_t opc1 = extract32(i, 4, 1);
9347                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9348 
9349                 tmp_string = g_strdup_printf("PRBAR%u", i);
9350                 ARMCPRegInfo tmp_prbarn_reginfo = {
9351                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9352                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9353                     .access = PL1_RW, .resetvalue = 0,
9354                     .accessfn = access_tvm_trvm,
9355                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9356                 };
9357                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9358                 g_free(tmp_string);
9359 
9360                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9361                 tmp_string = g_strdup_printf("PRLAR%u", i);
9362                 ARMCPRegInfo tmp_prlarn_reginfo = {
9363                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9364                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9365                     .access = PL1_RW, .resetvalue = 0,
9366                     .accessfn = access_tvm_trvm,
9367                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9368                 };
9369                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9370                 g_free(tmp_string);
9371             }
9372 
9373             /* Register alias is only valid for first 32 indexes */
9374             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9375                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9376                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9377                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9378 
9379                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9380                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9381                     .name = tmp_string,
9382                     .type = ARM_CP_NO_RAW,
9383                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9384                     .access = PL2_RW, .resetvalue = 0,
9385                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9386                 };
9387                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9388                 g_free(tmp_string);
9389 
9390                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9391                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9392                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9393                     .name = tmp_string,
9394                     .type = ARM_CP_NO_RAW,
9395                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9396                     .access = PL2_RW, .resetvalue = 0,
9397                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9398                 };
9399                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9400                 g_free(tmp_string);
9401             }
9402         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9403             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9404         }
9405     }
9406 
9407     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9408         ARMCPRegInfo mpidr_cp_reginfo[] = {
9409             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9410               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9411               .fgt = FGT_MPIDR_EL1,
9412               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9413         };
9414 #ifdef CONFIG_USER_ONLY
9415         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9416             { .name = "MPIDR_EL1",
9417               .fixed_bits = 0x0000000080000000 },
9418         };
9419         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9420 #endif
9421         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9422     }
9423 
9424     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9425         ARMCPRegInfo auxcr_reginfo[] = {
9426             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9427               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9428               .access = PL1_RW, .accessfn = access_tacr,
9429               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9430             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9431               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9432               .access = PL2_RW, .type = ARM_CP_CONST,
9433               .resetvalue = 0 },
9434             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9435               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9436               .access = PL3_RW, .type = ARM_CP_CONST,
9437               .resetvalue = 0 },
9438         };
9439         define_arm_cp_regs(cpu, auxcr_reginfo);
9440         if (cpu_isar_feature(aa32_ac2, cpu)) {
9441             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9442         }
9443     }
9444 
9445     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9446         /*
9447          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9448          * There are two flavours:
9449          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9450          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9451          *      32-bit register visible to AArch32 at a different encoding
9452          *      to the "flavour 1" register and with the bits rearranged to
9453          *      be able to squash a 64-bit address into the 32-bit view.
9454          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9455          * in future if we support AArch32-only configs of some of the
9456          * AArch64 cores we might need to add a specific feature flag
9457          * to indicate cores with "flavour 2" CBAR.
9458          */
9459         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9460             /* 32 bit view is [31:18] 0...0 [43:32]. */
9461             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9462                 | extract64(cpu->reset_cbar, 32, 12);
9463             ARMCPRegInfo cbar_reginfo[] = {
9464                 { .name = "CBAR",
9465                   .type = ARM_CP_CONST,
9466                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9467                   .access = PL1_R, .resetvalue = cbar32 },
9468                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9469                   .type = ARM_CP_CONST,
9470                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9471                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9472             };
9473             /* We don't implement a r/w 64 bit CBAR currently */
9474             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9475             define_arm_cp_regs(cpu, cbar_reginfo);
9476         } else {
9477             ARMCPRegInfo cbar = {
9478                 .name = "CBAR",
9479                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9480                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9481                 .fieldoffset = offsetof(CPUARMState,
9482                                         cp15.c15_config_base_address)
9483             };
9484             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9485                 cbar.access = PL1_R;
9486                 cbar.fieldoffset = 0;
9487                 cbar.type = ARM_CP_CONST;
9488             }
9489             define_one_arm_cp_reg(cpu, &cbar);
9490         }
9491     }
9492 
9493     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9494         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9495             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9496               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9497               .access = PL1_RW, .writefn = vbar_write,
9498               .accessfn = access_nv1,
9499               .fgt = FGT_VBAR_EL1,
9500               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9501                                      offsetof(CPUARMState, cp15.vbar_ns) },
9502               .resetvalue = 0 },
9503         };
9504         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9505     }
9506 
9507     /* Generic registers whose values depend on the implementation */
9508     {
9509         ARMCPRegInfo sctlr = {
9510             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9511             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9512             .access = PL1_RW, .accessfn = access_tvm_trvm,
9513             .fgt = FGT_SCTLR_EL1,
9514             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9515                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9516             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9517             .raw_writefn = raw_write,
9518         };
9519         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9520             /*
9521              * Normally we would always end the TB on an SCTLR write, but Linux
9522              * arch/arm/mach-pxa/sleep.S expects two instructions following
9523              * an MMU enable to execute from cache.  Imitate this behaviour.
9524              */
9525             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9526         }
9527         define_one_arm_cp_reg(cpu, &sctlr);
9528 
9529         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9530             arm_feature(env, ARM_FEATURE_V8)) {
9531             ARMCPRegInfo vsctlr = {
9532                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9533                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9534                 .access = PL2_RW, .resetvalue = 0x0,
9535                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9536             };
9537             define_one_arm_cp_reg(cpu, &vsctlr);
9538         }
9539     }
9540 
9541     if (cpu_isar_feature(aa64_lor, cpu)) {
9542         define_arm_cp_regs(cpu, lor_reginfo);
9543     }
9544     if (cpu_isar_feature(aa64_pan, cpu)) {
9545         define_one_arm_cp_reg(cpu, &pan_reginfo);
9546     }
9547 #ifndef CONFIG_USER_ONLY
9548     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9549         define_arm_cp_regs(cpu, ats1e1_reginfo);
9550     }
9551     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9552         define_arm_cp_regs(cpu, ats1cp_reginfo);
9553     }
9554 #endif
9555     if (cpu_isar_feature(aa64_uao, cpu)) {
9556         define_one_arm_cp_reg(cpu, &uao_reginfo);
9557     }
9558 
9559     if (cpu_isar_feature(aa64_dit, cpu)) {
9560         define_one_arm_cp_reg(cpu, &dit_reginfo);
9561     }
9562     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9563         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9564     }
9565     if (cpu_isar_feature(any_ras, cpu)) {
9566         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9567     }
9568 
9569     if (cpu_isar_feature(aa64_vh, cpu) ||
9570         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9571         define_one_arm_cp_reg(cpu, &contextidr_el2);
9572     }
9573     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9574         define_arm_cp_regs(cpu, vhe_reginfo);
9575     }
9576 
9577     if (cpu_isar_feature(aa64_sve, cpu)) {
9578         define_arm_cp_regs(cpu, zcr_reginfo);
9579     }
9580 
9581     if (cpu_isar_feature(aa64_hcx, cpu)) {
9582         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9583     }
9584 
9585 #ifdef TARGET_AARCH64
9586     if (cpu_isar_feature(aa64_sme, cpu)) {
9587         define_arm_cp_regs(cpu, sme_reginfo);
9588     }
9589     if (cpu_isar_feature(aa64_pauth, cpu)) {
9590         define_arm_cp_regs(cpu, pauth_reginfo);
9591     }
9592     if (cpu_isar_feature(aa64_rndr, cpu)) {
9593         define_arm_cp_regs(cpu, rndr_reginfo);
9594     }
9595     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9596         define_arm_cp_regs(cpu, tlbirange_reginfo);
9597     }
9598     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9599         define_arm_cp_regs(cpu, tlbios_reginfo);
9600     }
9601     /* Data Cache clean instructions up to PoP */
9602     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9603         define_one_arm_cp_reg(cpu, dcpop_reg);
9604 
9605         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9606             define_one_arm_cp_reg(cpu, dcpodp_reg);
9607         }
9608     }
9609 
9610     /*
9611      * If full MTE is enabled, add all of the system registers.
9612      * If only "instructions available at EL0" are enabled,
9613      * then define only a RAZ/WI version of PSTATE.TCO.
9614      */
9615     if (cpu_isar_feature(aa64_mte, cpu)) {
9616         ARMCPRegInfo gmid_reginfo = {
9617             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9618             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9619             .access = PL1_R, .accessfn = access_aa64_tid5,
9620             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9621         };
9622         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9623         define_arm_cp_regs(cpu, mte_reginfo);
9624         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9625     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9626         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9627         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9628     }
9629 
9630     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9631         define_arm_cp_regs(cpu, scxtnum_reginfo);
9632     }
9633 
9634     if (cpu_isar_feature(aa64_fgt, cpu)) {
9635         define_arm_cp_regs(cpu, fgt_reginfo);
9636     }
9637 
9638     if (cpu_isar_feature(aa64_rme, cpu)) {
9639         define_arm_cp_regs(cpu, rme_reginfo);
9640         if (cpu_isar_feature(aa64_mte, cpu)) {
9641             define_arm_cp_regs(cpu, rme_mte_reginfo);
9642         }
9643     }
9644 
9645     if (cpu_isar_feature(aa64_nv2, cpu)) {
9646         define_arm_cp_regs(cpu, nv2_reginfo);
9647     }
9648 #endif
9649 
9650     if (cpu_isar_feature(any_predinv, cpu)) {
9651         define_arm_cp_regs(cpu, predinv_reginfo);
9652     }
9653 
9654     if (cpu_isar_feature(any_ccidx, cpu)) {
9655         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9656     }
9657 
9658 #ifndef CONFIG_USER_ONLY
9659     /*
9660      * Register redirections and aliases must be done last,
9661      * after the registers from the other extensions have been defined.
9662      */
9663     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9664         define_arm_vh_e2h_redirects_aliases(cpu);
9665     }
9666 #endif
9667 }
9668 
9669 /*
9670  * Private utility function for define_one_arm_cp_reg_with_opaque():
9671  * add a single reginfo struct to the hash table.
9672  */
9673 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9674                                    void *opaque, CPState state,
9675                                    CPSecureState secstate,
9676                                    int crm, int opc1, int opc2,
9677                                    const char *name)
9678 {
9679     CPUARMState *env = &cpu->env;
9680     uint32_t key;
9681     ARMCPRegInfo *r2;
9682     bool is64 = r->type & ARM_CP_64BIT;
9683     bool ns = secstate & ARM_CP_SECSTATE_NS;
9684     int cp = r->cp;
9685     size_t name_len;
9686     bool make_const;
9687 
9688     switch (state) {
9689     case ARM_CP_STATE_AA32:
9690         /* We assume it is a cp15 register if the .cp field is left unset. */
9691         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9692             cp = 15;
9693         }
9694         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9695         break;
9696     case ARM_CP_STATE_AA64:
9697         /*
9698          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9699          * cp == 0 as equivalent to the value for "standard guest-visible
9700          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9701          * in their AArch64 view (the .cp value may be non-zero for the
9702          * benefit of the AArch32 view).
9703          */
9704         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9705             cp = CP_REG_ARM64_SYSREG_CP;
9706         }
9707         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9708         break;
9709     default:
9710         g_assert_not_reached();
9711     }
9712 
9713     /* Overriding of an existing definition must be explicitly requested. */
9714     if (!(r->type & ARM_CP_OVERRIDE)) {
9715         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9716         if (oldreg) {
9717             assert(oldreg->type & ARM_CP_OVERRIDE);
9718         }
9719     }
9720 
9721     /*
9722      * Eliminate registers that are not present because the EL is missing.
9723      * Doing this here makes it easier to put all registers for a given
9724      * feature into the same ARMCPRegInfo array and define them all at once.
9725      */
9726     make_const = false;
9727     if (arm_feature(env, ARM_FEATURE_EL3)) {
9728         /*
9729          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9730          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9731          */
9732         int min_el = ctz32(r->access) / 2;
9733         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9734             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9735                 return;
9736             }
9737             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9738         }
9739     } else {
9740         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9741                                  ? PL2_RW : PL1_RW);
9742         if ((r->access & max_el) == 0) {
9743             return;
9744         }
9745     }
9746 
9747     /* Combine cpreg and name into one allocation. */
9748     name_len = strlen(name) + 1;
9749     r2 = g_malloc(sizeof(*r2) + name_len);
9750     *r2 = *r;
9751     r2->name = memcpy(r2 + 1, name, name_len);
9752 
9753     /*
9754      * Update fields to match the instantiation, overwiting wildcards
9755      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9756      */
9757     r2->cp = cp;
9758     r2->crm = crm;
9759     r2->opc1 = opc1;
9760     r2->opc2 = opc2;
9761     r2->state = state;
9762     r2->secure = secstate;
9763     if (opaque) {
9764         r2->opaque = opaque;
9765     }
9766 
9767     if (make_const) {
9768         /* This should not have been a very special register to begin. */
9769         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9770         assert(old_special == 0 || old_special == ARM_CP_NOP);
9771         /*
9772          * Set the special function to CONST, retaining the other flags.
9773          * This is important for e.g. ARM_CP_SVE so that we still
9774          * take the SVE trap if CPTR_EL3.EZ == 0.
9775          */
9776         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9777         /*
9778          * Usually, these registers become RES0, but there are a few
9779          * special cases like VPIDR_EL2 which have a constant non-zero
9780          * value with writes ignored.
9781          */
9782         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9783             r2->resetvalue = 0;
9784         }
9785         /*
9786          * ARM_CP_CONST has precedence, so removing the callbacks and
9787          * offsets are not strictly necessary, but it is potentially
9788          * less confusing to debug later.
9789          */
9790         r2->readfn = NULL;
9791         r2->writefn = NULL;
9792         r2->raw_readfn = NULL;
9793         r2->raw_writefn = NULL;
9794         r2->resetfn = NULL;
9795         r2->fieldoffset = 0;
9796         r2->bank_fieldoffsets[0] = 0;
9797         r2->bank_fieldoffsets[1] = 0;
9798     } else {
9799         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9800 
9801         if (isbanked) {
9802             /*
9803              * Register is banked (using both entries in array).
9804              * Overwriting fieldoffset as the array is only used to define
9805              * banked registers but later only fieldoffset is used.
9806              */
9807             r2->fieldoffset = r->bank_fieldoffsets[ns];
9808         }
9809         if (state == ARM_CP_STATE_AA32) {
9810             if (isbanked) {
9811                 /*
9812                  * If the register is banked then we don't need to migrate or
9813                  * reset the 32-bit instance in certain cases:
9814                  *
9815                  * 1) If the register has both 32-bit and 64-bit instances
9816                  *    then we can count on the 64-bit instance taking care
9817                  *    of the non-secure bank.
9818                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9819                  *    version taking care of the secure bank.  This requires
9820                  *    that separate 32 and 64-bit definitions are provided.
9821                  */
9822                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9823                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9824                     r2->type |= ARM_CP_ALIAS;
9825                 }
9826             } else if ((secstate != r->secure) && !ns) {
9827                 /*
9828                  * The register is not banked so we only want to allow
9829                  * migration of the non-secure instance.
9830                  */
9831                 r2->type |= ARM_CP_ALIAS;
9832             }
9833 
9834             if (HOST_BIG_ENDIAN &&
9835                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9836                 r2->fieldoffset += sizeof(uint32_t);
9837             }
9838         }
9839     }
9840 
9841     /*
9842      * By convention, for wildcarded registers only the first
9843      * entry is used for migration; the others are marked as
9844      * ALIAS so we don't try to transfer the register
9845      * multiple times. Special registers (ie NOP/WFI) are
9846      * never migratable and not even raw-accessible.
9847      */
9848     if (r2->type & ARM_CP_SPECIAL_MASK) {
9849         r2->type |= ARM_CP_NO_RAW;
9850     }
9851     if (((r->crm == CP_ANY) && crm != 0) ||
9852         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9853         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9854         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9855     }
9856 
9857     /*
9858      * Check that raw accesses are either forbidden or handled. Note that
9859      * we can't assert this earlier because the setup of fieldoffset for
9860      * banked registers has to be done first.
9861      */
9862     if (!(r2->type & ARM_CP_NO_RAW)) {
9863         assert(!raw_accessors_invalid(r2));
9864     }
9865 
9866     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9867 }
9868 
9869 
9870 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9871                                        const ARMCPRegInfo *r, void *opaque)
9872 {
9873     /*
9874      * Define implementations of coprocessor registers.
9875      * We store these in a hashtable because typically
9876      * there are less than 150 registers in a space which
9877      * is 16*16*16*8*8 = 262144 in size.
9878      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9879      * If a register is defined twice then the second definition is
9880      * used, so this can be used to define some generic registers and
9881      * then override them with implementation specific variations.
9882      * At least one of the original and the second definition should
9883      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9884      * against accidental use.
9885      *
9886      * The state field defines whether the register is to be
9887      * visible in the AArch32 or AArch64 execution state. If the
9888      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9889      * reginfo structure for the AArch32 view, which sees the lower
9890      * 32 bits of the 64 bit register.
9891      *
9892      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9893      * be wildcarded. AArch64 registers are always considered to be 64
9894      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9895      * the register, if any.
9896      */
9897     int crm, opc1, opc2;
9898     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9899     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9900     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9901     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9902     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9903     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9904     CPState state;
9905 
9906     /* 64 bit registers have only CRm and Opc1 fields */
9907     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9908     /* op0 only exists in the AArch64 encodings */
9909     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9910     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9911     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9912     /*
9913      * This API is only for Arm's system coprocessors (14 and 15) or
9914      * (M-profile or v7A-and-earlier only) for implementation defined
9915      * coprocessors in the range 0..7.  Our decode assumes this, since
9916      * 8..13 can be used for other insns including VFP and Neon. See
9917      * valid_cp() in translate.c.  Assert here that we haven't tried
9918      * to use an invalid coprocessor number.
9919      */
9920     switch (r->state) {
9921     case ARM_CP_STATE_BOTH:
9922         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9923         if (r->cp == 0) {
9924             break;
9925         }
9926         /* fall through */
9927     case ARM_CP_STATE_AA32:
9928         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9929             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9930             assert(r->cp >= 14 && r->cp <= 15);
9931         } else {
9932             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9933         }
9934         break;
9935     case ARM_CP_STATE_AA64:
9936         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9937         break;
9938     default:
9939         g_assert_not_reached();
9940     }
9941     /*
9942      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9943      * encodes a minimum access level for the register. We roll this
9944      * runtime check into our general permission check code, so check
9945      * here that the reginfo's specified permissions are strict enough
9946      * to encompass the generic architectural permission check.
9947      */
9948     if (r->state != ARM_CP_STATE_AA32) {
9949         CPAccessRights mask;
9950         switch (r->opc1) {
9951         case 0:
9952             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9953             mask = PL0U_R | PL1_RW;
9954             break;
9955         case 1: case 2:
9956             /* min_EL EL1 */
9957             mask = PL1_RW;
9958             break;
9959         case 3:
9960             /* min_EL EL0 */
9961             mask = PL0_RW;
9962             break;
9963         case 4:
9964         case 5:
9965             /* min_EL EL2 */
9966             mask = PL2_RW;
9967             break;
9968         case 6:
9969             /* min_EL EL3 */
9970             mask = PL3_RW;
9971             break;
9972         case 7:
9973             /* min_EL EL1, secure mode only (we don't check the latter) */
9974             mask = PL1_RW;
9975             break;
9976         default:
9977             /* broken reginfo with out-of-range opc1 */
9978             g_assert_not_reached();
9979         }
9980         /* assert our permissions are not too lax (stricter is fine) */
9981         assert((r->access & ~mask) == 0);
9982     }
9983 
9984     /*
9985      * Check that the register definition has enough info to handle
9986      * reads and writes if they are permitted.
9987      */
9988     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9989         if (r->access & PL3_R) {
9990             assert((r->fieldoffset ||
9991                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9992                    r->readfn);
9993         }
9994         if (r->access & PL3_W) {
9995             assert((r->fieldoffset ||
9996                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9997                    r->writefn);
9998         }
9999     }
10000 
10001     for (crm = crmmin; crm <= crmmax; crm++) {
10002         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10003             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10004                 for (state = ARM_CP_STATE_AA32;
10005                      state <= ARM_CP_STATE_AA64; state++) {
10006                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10007                         continue;
10008                     }
10009                     if (state == ARM_CP_STATE_AA32) {
10010                         /*
10011                          * Under AArch32 CP registers can be common
10012                          * (same for secure and non-secure world) or banked.
10013                          */
10014                         char *name;
10015 
10016                         switch (r->secure) {
10017                         case ARM_CP_SECSTATE_S:
10018                         case ARM_CP_SECSTATE_NS:
10019                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10020                                                    r->secure, crm, opc1, opc2,
10021                                                    r->name);
10022                             break;
10023                         case ARM_CP_SECSTATE_BOTH:
10024                             name = g_strdup_printf("%s_S", r->name);
10025                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10026                                                    ARM_CP_SECSTATE_S,
10027                                                    crm, opc1, opc2, name);
10028                             g_free(name);
10029                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10030                                                    ARM_CP_SECSTATE_NS,
10031                                                    crm, opc1, opc2, r->name);
10032                             break;
10033                         default:
10034                             g_assert_not_reached();
10035                         }
10036                     } else {
10037                         /*
10038                          * AArch64 registers get mapped to non-secure instance
10039                          * of AArch32
10040                          */
10041                         add_cpreg_to_hashtable(cpu, r, opaque, state,
10042                                                ARM_CP_SECSTATE_NS,
10043                                                crm, opc1, opc2, r->name);
10044                     }
10045                 }
10046             }
10047         }
10048     }
10049 }
10050 
10051 /* Define a whole list of registers */
10052 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10053                                         void *opaque, size_t len)
10054 {
10055     size_t i;
10056     for (i = 0; i < len; ++i) {
10057         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10058     }
10059 }
10060 
10061 /*
10062  * Modify ARMCPRegInfo for access from userspace.
10063  *
10064  * This is a data driven modification directed by
10065  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10066  * user-space cannot alter any values and dynamic values pertaining to
10067  * execution state are hidden from user space view anyway.
10068  */
10069 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10070                                  const ARMCPRegUserSpaceInfo *mods,
10071                                  size_t mods_len)
10072 {
10073     for (size_t mi = 0; mi < mods_len; ++mi) {
10074         const ARMCPRegUserSpaceInfo *m = mods + mi;
10075         GPatternSpec *pat = NULL;
10076 
10077         if (m->is_glob) {
10078             pat = g_pattern_spec_new(m->name);
10079         }
10080         for (size_t ri = 0; ri < regs_len; ++ri) {
10081             ARMCPRegInfo *r = regs + ri;
10082 
10083             if (pat && g_pattern_match_string(pat, r->name)) {
10084                 r->type = ARM_CP_CONST;
10085                 r->access = PL0U_R;
10086                 r->resetvalue = 0;
10087                 /* continue */
10088             } else if (strcmp(r->name, m->name) == 0) {
10089                 r->type = ARM_CP_CONST;
10090                 r->access = PL0U_R;
10091                 r->resetvalue &= m->exported_bits;
10092                 r->resetvalue |= m->fixed_bits;
10093                 break;
10094             }
10095         }
10096         if (pat) {
10097             g_pattern_spec_free(pat);
10098         }
10099     }
10100 }
10101 
10102 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10103 {
10104     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10105 }
10106 
10107 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10108                          uint64_t value)
10109 {
10110     /* Helper coprocessor write function for write-ignore registers */
10111 }
10112 
10113 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10114 {
10115     /* Helper coprocessor write function for read-as-zero registers */
10116     return 0;
10117 }
10118 
10119 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10120 {
10121     /* Helper coprocessor reset function for do-nothing-on-reset registers */
10122 }
10123 
10124 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10125 {
10126     /*
10127      * Return true if it is not valid for us to switch to
10128      * this CPU mode (ie all the UNPREDICTABLE cases in
10129      * the ARM ARM CPSRWriteByInstr pseudocode).
10130      */
10131 
10132     /* Changes to or from Hyp via MSR and CPS are illegal. */
10133     if (write_type == CPSRWriteByInstr &&
10134         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10135          mode == ARM_CPU_MODE_HYP)) {
10136         return 1;
10137     }
10138 
10139     switch (mode) {
10140     case ARM_CPU_MODE_USR:
10141         return 0;
10142     case ARM_CPU_MODE_SYS:
10143     case ARM_CPU_MODE_SVC:
10144     case ARM_CPU_MODE_ABT:
10145     case ARM_CPU_MODE_UND:
10146     case ARM_CPU_MODE_IRQ:
10147     case ARM_CPU_MODE_FIQ:
10148         /*
10149          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10150          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10151          */
10152         /*
10153          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10154          * and CPS are treated as illegal mode changes.
10155          */
10156         if (write_type == CPSRWriteByInstr &&
10157             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10158             (arm_hcr_el2_eff(env) & HCR_TGE)) {
10159             return 1;
10160         }
10161         return 0;
10162     case ARM_CPU_MODE_HYP:
10163         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10164     case ARM_CPU_MODE_MON:
10165         return arm_current_el(env) < 3;
10166     default:
10167         return 1;
10168     }
10169 }
10170 
10171 uint32_t cpsr_read(CPUARMState *env)
10172 {
10173     int ZF;
10174     ZF = (env->ZF == 0);
10175     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10176         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10177         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10178         | ((env->condexec_bits & 0xfc) << 8)
10179         | (env->GE << 16) | (env->daif & CPSR_AIF);
10180 }
10181 
10182 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10183                 CPSRWriteType write_type)
10184 {
10185     uint32_t changed_daif;
10186     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10187         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10188 
10189     if (mask & CPSR_NZCV) {
10190         env->ZF = (~val) & CPSR_Z;
10191         env->NF = val;
10192         env->CF = (val >> 29) & 1;
10193         env->VF = (val << 3) & 0x80000000;
10194     }
10195     if (mask & CPSR_Q) {
10196         env->QF = ((val & CPSR_Q) != 0);
10197     }
10198     if (mask & CPSR_T) {
10199         env->thumb = ((val & CPSR_T) != 0);
10200     }
10201     if (mask & CPSR_IT_0_1) {
10202         env->condexec_bits &= ~3;
10203         env->condexec_bits |= (val >> 25) & 3;
10204     }
10205     if (mask & CPSR_IT_2_7) {
10206         env->condexec_bits &= 3;
10207         env->condexec_bits |= (val >> 8) & 0xfc;
10208     }
10209     if (mask & CPSR_GE) {
10210         env->GE = (val >> 16) & 0xf;
10211     }
10212 
10213     /*
10214      * In a V7 implementation that includes the security extensions but does
10215      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10216      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10217      * bits respectively.
10218      *
10219      * In a V8 implementation, it is permitted for privileged software to
10220      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10221      */
10222     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10223         arm_feature(env, ARM_FEATURE_EL3) &&
10224         !arm_feature(env, ARM_FEATURE_EL2) &&
10225         !arm_is_secure(env)) {
10226 
10227         changed_daif = (env->daif ^ val) & mask;
10228 
10229         if (changed_daif & CPSR_A) {
10230             /*
10231              * Check to see if we are allowed to change the masking of async
10232              * abort exceptions from a non-secure state.
10233              */
10234             if (!(env->cp15.scr_el3 & SCR_AW)) {
10235                 qemu_log_mask(LOG_GUEST_ERROR,
10236                               "Ignoring attempt to switch CPSR_A flag from "
10237                               "non-secure world with SCR.AW bit clear\n");
10238                 mask &= ~CPSR_A;
10239             }
10240         }
10241 
10242         if (changed_daif & CPSR_F) {
10243             /*
10244              * Check to see if we are allowed to change the masking of FIQ
10245              * exceptions from a non-secure state.
10246              */
10247             if (!(env->cp15.scr_el3 & SCR_FW)) {
10248                 qemu_log_mask(LOG_GUEST_ERROR,
10249                               "Ignoring attempt to switch CPSR_F flag from "
10250                               "non-secure world with SCR.FW bit clear\n");
10251                 mask &= ~CPSR_F;
10252             }
10253 
10254             /*
10255              * Check whether non-maskable FIQ (NMFI) support is enabled.
10256              * If this bit is set software is not allowed to mask
10257              * FIQs, but is allowed to set CPSR_F to 0.
10258              */
10259             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10260                 (val & CPSR_F)) {
10261                 qemu_log_mask(LOG_GUEST_ERROR,
10262                               "Ignoring attempt to enable CPSR_F flag "
10263                               "(non-maskable FIQ [NMFI] support enabled)\n");
10264                 mask &= ~CPSR_F;
10265             }
10266         }
10267     }
10268 
10269     env->daif &= ~(CPSR_AIF & mask);
10270     env->daif |= val & CPSR_AIF & mask;
10271 
10272     if (write_type != CPSRWriteRaw &&
10273         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10274         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10275             /*
10276              * Note that we can only get here in USR mode if this is a
10277              * gdb stub write; for this case we follow the architectural
10278              * behaviour for guest writes in USR mode of ignoring an attempt
10279              * to switch mode. (Those are caught by translate.c for writes
10280              * triggered by guest instructions.)
10281              */
10282             mask &= ~CPSR_M;
10283         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10284             /*
10285              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10286              * v7, and has defined behaviour in v8:
10287              *  + leave CPSR.M untouched
10288              *  + allow changes to the other CPSR fields
10289              *  + set PSTATE.IL
10290              * For user changes via the GDB stub, we don't set PSTATE.IL,
10291              * as this would be unnecessarily harsh for a user error.
10292              */
10293             mask &= ~CPSR_M;
10294             if (write_type != CPSRWriteByGDBStub &&
10295                 arm_feature(env, ARM_FEATURE_V8)) {
10296                 mask |= CPSR_IL;
10297                 val |= CPSR_IL;
10298             }
10299             qemu_log_mask(LOG_GUEST_ERROR,
10300                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10301                           aarch32_mode_name(env->uncached_cpsr),
10302                           aarch32_mode_name(val));
10303         } else {
10304             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10305                           write_type == CPSRWriteExceptionReturn ?
10306                           "Exception return from AArch32" :
10307                           "AArch32 mode switch from",
10308                           aarch32_mode_name(env->uncached_cpsr),
10309                           aarch32_mode_name(val), env->regs[15]);
10310             switch_mode(env, val & CPSR_M);
10311         }
10312     }
10313     mask &= ~CACHED_CPSR_BITS;
10314     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10315     if (tcg_enabled() && rebuild_hflags) {
10316         arm_rebuild_hflags(env);
10317     }
10318 }
10319 
10320 #ifdef CONFIG_USER_ONLY
10321 
10322 static void switch_mode(CPUARMState *env, int mode)
10323 {
10324     ARMCPU *cpu = env_archcpu(env);
10325 
10326     if (mode != ARM_CPU_MODE_USR) {
10327         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10328     }
10329 }
10330 
10331 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10332                                  uint32_t cur_el, bool secure)
10333 {
10334     return 1;
10335 }
10336 
10337 void aarch64_sync_64_to_32(CPUARMState *env)
10338 {
10339     g_assert_not_reached();
10340 }
10341 
10342 #else
10343 
10344 static void switch_mode(CPUARMState *env, int mode)
10345 {
10346     int old_mode;
10347     int i;
10348 
10349     old_mode = env->uncached_cpsr & CPSR_M;
10350     if (mode == old_mode) {
10351         return;
10352     }
10353 
10354     if (old_mode == ARM_CPU_MODE_FIQ) {
10355         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10356         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10357     } else if (mode == ARM_CPU_MODE_FIQ) {
10358         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10359         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10360     }
10361 
10362     i = bank_number(old_mode);
10363     env->banked_r13[i] = env->regs[13];
10364     env->banked_spsr[i] = env->spsr;
10365 
10366     i = bank_number(mode);
10367     env->regs[13] = env->banked_r13[i];
10368     env->spsr = env->banked_spsr[i];
10369 
10370     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10371     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10372 }
10373 
10374 /*
10375  * Physical Interrupt Target EL Lookup Table
10376  *
10377  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10378  *
10379  * The below multi-dimensional table is used for looking up the target
10380  * exception level given numerous condition criteria.  Specifically, the
10381  * target EL is based on SCR and HCR routing controls as well as the
10382  * currently executing EL and secure state.
10383  *
10384  *    Dimensions:
10385  *    target_el_table[2][2][2][2][2][4]
10386  *                    |  |  |  |  |  +--- Current EL
10387  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10388  *                    |  |  |  +--------- HCR mask override
10389  *                    |  |  +------------ SCR exec state control
10390  *                    |  +--------------- SCR mask override
10391  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10392  *
10393  *    The table values are as such:
10394  *    0-3 = EL0-EL3
10395  *     -1 = Cannot occur
10396  *
10397  * The ARM ARM target EL table includes entries indicating that an "exception
10398  * is not taken".  The two cases where this is applicable are:
10399  *    1) An exception is taken from EL3 but the SCR does not have the exception
10400  *    routed to EL3.
10401  *    2) An exception is taken from EL2 but the HCR does not have the exception
10402  *    routed to EL2.
10403  * In these two cases, the below table contain a target of EL1.  This value is
10404  * returned as it is expected that the consumer of the table data will check
10405  * for "target EL >= current EL" to ensure the exception is not taken.
10406  *
10407  *            SCR     HCR
10408  *         64  EA     AMO                 From
10409  *        BIT IRQ     IMO      Non-secure         Secure
10410  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10411  */
10412 static const int8_t target_el_table[2][2][2][2][2][4] = {
10413     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10414        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10415       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10416        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10417      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10418        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10419       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10420        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10421     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10422        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10423       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10424        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10425      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10426        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10427       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10428        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10429 };
10430 
10431 /*
10432  * Determine the target EL for physical exceptions
10433  */
10434 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10435                                  uint32_t cur_el, bool secure)
10436 {
10437     CPUARMState *env = cpu_env(cs);
10438     bool rw;
10439     bool scr;
10440     bool hcr;
10441     int target_el;
10442     /* Is the highest EL AArch64? */
10443     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10444     uint64_t hcr_el2;
10445 
10446     if (arm_feature(env, ARM_FEATURE_EL3)) {
10447         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10448     } else {
10449         /*
10450          * Either EL2 is the highest EL (and so the EL2 register width
10451          * is given by is64); or there is no EL2 or EL3, in which case
10452          * the value of 'rw' does not affect the table lookup anyway.
10453          */
10454         rw = is64;
10455     }
10456 
10457     hcr_el2 = arm_hcr_el2_eff(env);
10458     switch (excp_idx) {
10459     case EXCP_IRQ:
10460         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10461         hcr = hcr_el2 & HCR_IMO;
10462         break;
10463     case EXCP_FIQ:
10464         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10465         hcr = hcr_el2 & HCR_FMO;
10466         break;
10467     default:
10468         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10469         hcr = hcr_el2 & HCR_AMO;
10470         break;
10471     };
10472 
10473     /*
10474      * For these purposes, TGE and AMO/IMO/FMO both force the
10475      * interrupt to EL2.  Fold TGE into the bit extracted above.
10476      */
10477     hcr |= (hcr_el2 & HCR_TGE) != 0;
10478 
10479     /* Perform a table-lookup for the target EL given the current state */
10480     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10481 
10482     assert(target_el > 0);
10483 
10484     return target_el;
10485 }
10486 
10487 void arm_log_exception(CPUState *cs)
10488 {
10489     int idx = cs->exception_index;
10490 
10491     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10492         const char *exc = NULL;
10493         static const char * const excnames[] = {
10494             [EXCP_UDEF] = "Undefined Instruction",
10495             [EXCP_SWI] = "SVC",
10496             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10497             [EXCP_DATA_ABORT] = "Data Abort",
10498             [EXCP_IRQ] = "IRQ",
10499             [EXCP_FIQ] = "FIQ",
10500             [EXCP_BKPT] = "Breakpoint",
10501             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10502             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10503             [EXCP_HVC] = "Hypervisor Call",
10504             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10505             [EXCP_SMC] = "Secure Monitor Call",
10506             [EXCP_VIRQ] = "Virtual IRQ",
10507             [EXCP_VFIQ] = "Virtual FIQ",
10508             [EXCP_SEMIHOST] = "Semihosting call",
10509             [EXCP_NOCP] = "v7M NOCP UsageFault",
10510             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10511             [EXCP_STKOF] = "v8M STKOF UsageFault",
10512             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10513             [EXCP_LSERR] = "v8M LSERR UsageFault",
10514             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10515             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10516             [EXCP_VSERR] = "Virtual SERR",
10517             [EXCP_GPC] = "Granule Protection Check",
10518         };
10519 
10520         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10521             exc = excnames[idx];
10522         }
10523         if (!exc) {
10524             exc = "unknown";
10525         }
10526         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10527                       idx, exc, cs->cpu_index);
10528     }
10529 }
10530 
10531 /*
10532  * Function used to synchronize QEMU's AArch64 register set with AArch32
10533  * register set.  This is necessary when switching between AArch32 and AArch64
10534  * execution state.
10535  */
10536 void aarch64_sync_32_to_64(CPUARMState *env)
10537 {
10538     int i;
10539     uint32_t mode = env->uncached_cpsr & CPSR_M;
10540 
10541     /* We can blanket copy R[0:7] to X[0:7] */
10542     for (i = 0; i < 8; i++) {
10543         env->xregs[i] = env->regs[i];
10544     }
10545 
10546     /*
10547      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10548      * Otherwise, they come from the banked user regs.
10549      */
10550     if (mode == ARM_CPU_MODE_FIQ) {
10551         for (i = 8; i < 13; i++) {
10552             env->xregs[i] = env->usr_regs[i - 8];
10553         }
10554     } else {
10555         for (i = 8; i < 13; i++) {
10556             env->xregs[i] = env->regs[i];
10557         }
10558     }
10559 
10560     /*
10561      * Registers x13-x23 are the various mode SP and FP registers. Registers
10562      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10563      * from the mode banked register.
10564      */
10565     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10566         env->xregs[13] = env->regs[13];
10567         env->xregs[14] = env->regs[14];
10568     } else {
10569         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10570         /* HYP is an exception in that it is copied from r14 */
10571         if (mode == ARM_CPU_MODE_HYP) {
10572             env->xregs[14] = env->regs[14];
10573         } else {
10574             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10575         }
10576     }
10577 
10578     if (mode == ARM_CPU_MODE_HYP) {
10579         env->xregs[15] = env->regs[13];
10580     } else {
10581         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10582     }
10583 
10584     if (mode == ARM_CPU_MODE_IRQ) {
10585         env->xregs[16] = env->regs[14];
10586         env->xregs[17] = env->regs[13];
10587     } else {
10588         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10589         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10590     }
10591 
10592     if (mode == ARM_CPU_MODE_SVC) {
10593         env->xregs[18] = env->regs[14];
10594         env->xregs[19] = env->regs[13];
10595     } else {
10596         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10597         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10598     }
10599 
10600     if (mode == ARM_CPU_MODE_ABT) {
10601         env->xregs[20] = env->regs[14];
10602         env->xregs[21] = env->regs[13];
10603     } else {
10604         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10605         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10606     }
10607 
10608     if (mode == ARM_CPU_MODE_UND) {
10609         env->xregs[22] = env->regs[14];
10610         env->xregs[23] = env->regs[13];
10611     } else {
10612         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10613         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10614     }
10615 
10616     /*
10617      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10618      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10619      * FIQ bank for r8-r14.
10620      */
10621     if (mode == ARM_CPU_MODE_FIQ) {
10622         for (i = 24; i < 31; i++) {
10623             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10624         }
10625     } else {
10626         for (i = 24; i < 29; i++) {
10627             env->xregs[i] = env->fiq_regs[i - 24];
10628         }
10629         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10630         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10631     }
10632 
10633     env->pc = env->regs[15];
10634 }
10635 
10636 /*
10637  * Function used to synchronize QEMU's AArch32 register set with AArch64
10638  * register set.  This is necessary when switching between AArch32 and AArch64
10639  * execution state.
10640  */
10641 void aarch64_sync_64_to_32(CPUARMState *env)
10642 {
10643     int i;
10644     uint32_t mode = env->uncached_cpsr & CPSR_M;
10645 
10646     /* We can blanket copy X[0:7] to R[0:7] */
10647     for (i = 0; i < 8; i++) {
10648         env->regs[i] = env->xregs[i];
10649     }
10650 
10651     /*
10652      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10653      * Otherwise, we copy x8-x12 into the banked user regs.
10654      */
10655     if (mode == ARM_CPU_MODE_FIQ) {
10656         for (i = 8; i < 13; i++) {
10657             env->usr_regs[i - 8] = env->xregs[i];
10658         }
10659     } else {
10660         for (i = 8; i < 13; i++) {
10661             env->regs[i] = env->xregs[i];
10662         }
10663     }
10664 
10665     /*
10666      * Registers r13 & r14 depend on the current mode.
10667      * If we are in a given mode, we copy the corresponding x registers to r13
10668      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10669      * for the mode.
10670      */
10671     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10672         env->regs[13] = env->xregs[13];
10673         env->regs[14] = env->xregs[14];
10674     } else {
10675         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10676 
10677         /*
10678          * HYP is an exception in that it does not have its own banked r14 but
10679          * shares the USR r14
10680          */
10681         if (mode == ARM_CPU_MODE_HYP) {
10682             env->regs[14] = env->xregs[14];
10683         } else {
10684             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10685         }
10686     }
10687 
10688     if (mode == ARM_CPU_MODE_HYP) {
10689         env->regs[13] = env->xregs[15];
10690     } else {
10691         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10692     }
10693 
10694     if (mode == ARM_CPU_MODE_IRQ) {
10695         env->regs[14] = env->xregs[16];
10696         env->regs[13] = env->xregs[17];
10697     } else {
10698         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10699         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10700     }
10701 
10702     if (mode == ARM_CPU_MODE_SVC) {
10703         env->regs[14] = env->xregs[18];
10704         env->regs[13] = env->xregs[19];
10705     } else {
10706         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10707         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10708     }
10709 
10710     if (mode == ARM_CPU_MODE_ABT) {
10711         env->regs[14] = env->xregs[20];
10712         env->regs[13] = env->xregs[21];
10713     } else {
10714         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10715         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10716     }
10717 
10718     if (mode == ARM_CPU_MODE_UND) {
10719         env->regs[14] = env->xregs[22];
10720         env->regs[13] = env->xregs[23];
10721     } else {
10722         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10723         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10724     }
10725 
10726     /*
10727      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10728      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10729      * FIQ bank for r8-r14.
10730      */
10731     if (mode == ARM_CPU_MODE_FIQ) {
10732         for (i = 24; i < 31; i++) {
10733             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10734         }
10735     } else {
10736         for (i = 24; i < 29; i++) {
10737             env->fiq_regs[i - 24] = env->xregs[i];
10738         }
10739         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10740         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10741     }
10742 
10743     env->regs[15] = env->pc;
10744 }
10745 
10746 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10747                                    uint32_t mask, uint32_t offset,
10748                                    uint32_t newpc)
10749 {
10750     int new_el;
10751 
10752     /* Change the CPU state so as to actually take the exception. */
10753     switch_mode(env, new_mode);
10754 
10755     /*
10756      * For exceptions taken to AArch32 we must clear the SS bit in both
10757      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10758      */
10759     env->pstate &= ~PSTATE_SS;
10760     env->spsr = cpsr_read(env);
10761     /* Clear IT bits.  */
10762     env->condexec_bits = 0;
10763     /* Switch to the new mode, and to the correct instruction set.  */
10764     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10765 
10766     /* This must be after mode switching. */
10767     new_el = arm_current_el(env);
10768 
10769     /* Set new mode endianness */
10770     env->uncached_cpsr &= ~CPSR_E;
10771     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10772         env->uncached_cpsr |= CPSR_E;
10773     }
10774     /* J and IL must always be cleared for exception entry */
10775     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10776     env->daif |= mask;
10777 
10778     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10779         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10780             env->uncached_cpsr |= CPSR_SSBS;
10781         } else {
10782             env->uncached_cpsr &= ~CPSR_SSBS;
10783         }
10784     }
10785 
10786     if (new_mode == ARM_CPU_MODE_HYP) {
10787         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10788         env->elr_el[2] = env->regs[15];
10789     } else {
10790         /* CPSR.PAN is normally preserved preserved unless...  */
10791         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10792             switch (new_el) {
10793             case 3:
10794                 if (!arm_is_secure_below_el3(env)) {
10795                     /* ... the target is EL3, from non-secure state.  */
10796                     env->uncached_cpsr &= ~CPSR_PAN;
10797                     break;
10798                 }
10799                 /* ... the target is EL3, from secure state ... */
10800                 /* fall through */
10801             case 1:
10802                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10803                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10804                     env->uncached_cpsr |= CPSR_PAN;
10805                 }
10806                 break;
10807             }
10808         }
10809         /*
10810          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10811          * and we should just guard the thumb mode on V4
10812          */
10813         if (arm_feature(env, ARM_FEATURE_V4T)) {
10814             env->thumb =
10815                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10816         }
10817         env->regs[14] = env->regs[15] + offset;
10818     }
10819     env->regs[15] = newpc;
10820 
10821     if (tcg_enabled()) {
10822         arm_rebuild_hflags(env);
10823     }
10824 }
10825 
10826 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10827 {
10828     /*
10829      * Handle exception entry to Hyp mode; this is sufficiently
10830      * different to entry to other AArch32 modes that we handle it
10831      * separately here.
10832      *
10833      * The vector table entry used is always the 0x14 Hyp mode entry point,
10834      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10835      * The offset applied to the preferred return address is always zero
10836      * (see DDI0487C.a section G1.12.3).
10837      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10838      */
10839     uint32_t addr, mask;
10840     ARMCPU *cpu = ARM_CPU(cs);
10841     CPUARMState *env = &cpu->env;
10842 
10843     switch (cs->exception_index) {
10844     case EXCP_UDEF:
10845         addr = 0x04;
10846         break;
10847     case EXCP_SWI:
10848         addr = 0x08;
10849         break;
10850     case EXCP_BKPT:
10851         /* Fall through to prefetch abort.  */
10852     case EXCP_PREFETCH_ABORT:
10853         env->cp15.ifar_s = env->exception.vaddress;
10854         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10855                       (uint32_t)env->exception.vaddress);
10856         addr = 0x0c;
10857         break;
10858     case EXCP_DATA_ABORT:
10859         env->cp15.dfar_s = env->exception.vaddress;
10860         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10861                       (uint32_t)env->exception.vaddress);
10862         addr = 0x10;
10863         break;
10864     case EXCP_IRQ:
10865         addr = 0x18;
10866         break;
10867     case EXCP_FIQ:
10868         addr = 0x1c;
10869         break;
10870     case EXCP_HVC:
10871         addr = 0x08;
10872         break;
10873     case EXCP_HYP_TRAP:
10874         addr = 0x14;
10875         break;
10876     default:
10877         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10878     }
10879 
10880     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10881         if (!arm_feature(env, ARM_FEATURE_V8)) {
10882             /*
10883              * QEMU syndrome values are v8-style. v7 has the IL bit
10884              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10885              * If this is a v7 CPU, squash the IL bit in those cases.
10886              */
10887             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10888                 (cs->exception_index == EXCP_DATA_ABORT &&
10889                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10890                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10891                 env->exception.syndrome &= ~ARM_EL_IL;
10892             }
10893         }
10894         env->cp15.esr_el[2] = env->exception.syndrome;
10895     }
10896 
10897     if (arm_current_el(env) != 2 && addr < 0x14) {
10898         addr = 0x14;
10899     }
10900 
10901     mask = 0;
10902     if (!(env->cp15.scr_el3 & SCR_EA)) {
10903         mask |= CPSR_A;
10904     }
10905     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10906         mask |= CPSR_I;
10907     }
10908     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10909         mask |= CPSR_F;
10910     }
10911 
10912     addr += env->cp15.hvbar;
10913 
10914     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10915 }
10916 
10917 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10918 {
10919     ARMCPU *cpu = ARM_CPU(cs);
10920     CPUARMState *env = &cpu->env;
10921     uint32_t addr;
10922     uint32_t mask;
10923     int new_mode;
10924     uint32_t offset;
10925     uint32_t moe;
10926 
10927     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10928     switch (syn_get_ec(env->exception.syndrome)) {
10929     case EC_BREAKPOINT:
10930     case EC_BREAKPOINT_SAME_EL:
10931         moe = 1;
10932         break;
10933     case EC_WATCHPOINT:
10934     case EC_WATCHPOINT_SAME_EL:
10935         moe = 10;
10936         break;
10937     case EC_AA32_BKPT:
10938         moe = 3;
10939         break;
10940     case EC_VECTORCATCH:
10941         moe = 5;
10942         break;
10943     default:
10944         moe = 0;
10945         break;
10946     }
10947 
10948     if (moe) {
10949         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10950     }
10951 
10952     if (env->exception.target_el == 2) {
10953         arm_cpu_do_interrupt_aarch32_hyp(cs);
10954         return;
10955     }
10956 
10957     switch (cs->exception_index) {
10958     case EXCP_UDEF:
10959         new_mode = ARM_CPU_MODE_UND;
10960         addr = 0x04;
10961         mask = CPSR_I;
10962         if (env->thumb) {
10963             offset = 2;
10964         } else {
10965             offset = 4;
10966         }
10967         break;
10968     case EXCP_SWI:
10969         new_mode = ARM_CPU_MODE_SVC;
10970         addr = 0x08;
10971         mask = CPSR_I;
10972         /* The PC already points to the next instruction.  */
10973         offset = 0;
10974         break;
10975     case EXCP_BKPT:
10976         /* Fall through to prefetch abort.  */
10977     case EXCP_PREFETCH_ABORT:
10978         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10979         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10980         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10981                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10982         new_mode = ARM_CPU_MODE_ABT;
10983         addr = 0x0c;
10984         mask = CPSR_A | CPSR_I;
10985         offset = 4;
10986         break;
10987     case EXCP_DATA_ABORT:
10988         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10989         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10990         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10991                       env->exception.fsr,
10992                       (uint32_t)env->exception.vaddress);
10993         new_mode = ARM_CPU_MODE_ABT;
10994         addr = 0x10;
10995         mask = CPSR_A | CPSR_I;
10996         offset = 8;
10997         break;
10998     case EXCP_IRQ:
10999         new_mode = ARM_CPU_MODE_IRQ;
11000         addr = 0x18;
11001         /* Disable IRQ and imprecise data aborts.  */
11002         mask = CPSR_A | CPSR_I;
11003         offset = 4;
11004         if (env->cp15.scr_el3 & SCR_IRQ) {
11005             /* IRQ routed to monitor mode */
11006             new_mode = ARM_CPU_MODE_MON;
11007             mask |= CPSR_F;
11008         }
11009         break;
11010     case EXCP_FIQ:
11011         new_mode = ARM_CPU_MODE_FIQ;
11012         addr = 0x1c;
11013         /* Disable FIQ, IRQ and imprecise data aborts.  */
11014         mask = CPSR_A | CPSR_I | CPSR_F;
11015         if (env->cp15.scr_el3 & SCR_FIQ) {
11016             /* FIQ routed to monitor mode */
11017             new_mode = ARM_CPU_MODE_MON;
11018         }
11019         offset = 4;
11020         break;
11021     case EXCP_VIRQ:
11022         new_mode = ARM_CPU_MODE_IRQ;
11023         addr = 0x18;
11024         /* Disable IRQ and imprecise data aborts.  */
11025         mask = CPSR_A | CPSR_I;
11026         offset = 4;
11027         break;
11028     case EXCP_VFIQ:
11029         new_mode = ARM_CPU_MODE_FIQ;
11030         addr = 0x1c;
11031         /* Disable FIQ, IRQ and imprecise data aborts.  */
11032         mask = CPSR_A | CPSR_I | CPSR_F;
11033         offset = 4;
11034         break;
11035     case EXCP_VSERR:
11036         {
11037             /*
11038              * Note that this is reported as a data abort, but the DFAR
11039              * has an UNKNOWN value.  Construct the SError syndrome from
11040              * AET and ExT fields.
11041              */
11042             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11043 
11044             if (extended_addresses_enabled(env)) {
11045                 env->exception.fsr = arm_fi_to_lfsc(&fi);
11046             } else {
11047                 env->exception.fsr = arm_fi_to_sfsc(&fi);
11048             }
11049             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11050             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11051             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11052                           env->exception.fsr);
11053 
11054             new_mode = ARM_CPU_MODE_ABT;
11055             addr = 0x10;
11056             mask = CPSR_A | CPSR_I;
11057             offset = 8;
11058         }
11059         break;
11060     case EXCP_SMC:
11061         new_mode = ARM_CPU_MODE_MON;
11062         addr = 0x08;
11063         mask = CPSR_A | CPSR_I | CPSR_F;
11064         offset = 0;
11065         break;
11066     default:
11067         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11068         return; /* Never happens.  Keep compiler happy.  */
11069     }
11070 
11071     if (new_mode == ARM_CPU_MODE_MON) {
11072         addr += env->cp15.mvbar;
11073     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11074         /* High vectors. When enabled, base address cannot be remapped. */
11075         addr += 0xffff0000;
11076     } else {
11077         /*
11078          * ARM v7 architectures provide a vector base address register to remap
11079          * the interrupt vector table.
11080          * This register is only followed in non-monitor mode, and is banked.
11081          * Note: only bits 31:5 are valid.
11082          */
11083         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11084     }
11085 
11086     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11087         env->cp15.scr_el3 &= ~SCR_NS;
11088     }
11089 
11090     take_aarch32_exception(env, new_mode, mask, offset, addr);
11091 }
11092 
11093 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11094 {
11095     /*
11096      * Return the register number of the AArch64 view of the AArch32
11097      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11098      * be that of the AArch32 mode the exception came from.
11099      */
11100     int mode = env->uncached_cpsr & CPSR_M;
11101 
11102     switch (aarch32_reg) {
11103     case 0 ... 7:
11104         return aarch32_reg;
11105     case 8 ... 12:
11106         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11107     case 13:
11108         switch (mode) {
11109         case ARM_CPU_MODE_USR:
11110         case ARM_CPU_MODE_SYS:
11111             return 13;
11112         case ARM_CPU_MODE_HYP:
11113             return 15;
11114         case ARM_CPU_MODE_IRQ:
11115             return 17;
11116         case ARM_CPU_MODE_SVC:
11117             return 19;
11118         case ARM_CPU_MODE_ABT:
11119             return 21;
11120         case ARM_CPU_MODE_UND:
11121             return 23;
11122         case ARM_CPU_MODE_FIQ:
11123             return 29;
11124         default:
11125             g_assert_not_reached();
11126         }
11127     case 14:
11128         switch (mode) {
11129         case ARM_CPU_MODE_USR:
11130         case ARM_CPU_MODE_SYS:
11131         case ARM_CPU_MODE_HYP:
11132             return 14;
11133         case ARM_CPU_MODE_IRQ:
11134             return 16;
11135         case ARM_CPU_MODE_SVC:
11136             return 18;
11137         case ARM_CPU_MODE_ABT:
11138             return 20;
11139         case ARM_CPU_MODE_UND:
11140             return 22;
11141         case ARM_CPU_MODE_FIQ:
11142             return 30;
11143         default:
11144             g_assert_not_reached();
11145         }
11146     case 15:
11147         return 31;
11148     default:
11149         g_assert_not_reached();
11150     }
11151 }
11152 
11153 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11154 {
11155     uint32_t ret = cpsr_read(env);
11156 
11157     /* Move DIT to the correct location for SPSR_ELx */
11158     if (ret & CPSR_DIT) {
11159         ret &= ~CPSR_DIT;
11160         ret |= PSTATE_DIT;
11161     }
11162     /* Merge PSTATE.SS into SPSR_ELx */
11163     ret |= env->pstate & PSTATE_SS;
11164 
11165     return ret;
11166 }
11167 
11168 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11169 {
11170     /* Return true if this syndrome value is a synchronous external abort */
11171     switch (syn_get_ec(syndrome)) {
11172     case EC_INSNABORT:
11173     case EC_INSNABORT_SAME_EL:
11174     case EC_DATAABORT:
11175     case EC_DATAABORT_SAME_EL:
11176         /* Look at fault status code for all the synchronous ext abort cases */
11177         switch (syndrome & 0x3f) {
11178         case 0x10:
11179         case 0x13:
11180         case 0x14:
11181         case 0x15:
11182         case 0x16:
11183         case 0x17:
11184             return true;
11185         default:
11186             return false;
11187         }
11188     default:
11189         return false;
11190     }
11191 }
11192 
11193 /* Handle exception entry to a target EL which is using AArch64 */
11194 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11195 {
11196     ARMCPU *cpu = ARM_CPU(cs);
11197     CPUARMState *env = &cpu->env;
11198     unsigned int new_el = env->exception.target_el;
11199     target_ulong addr = env->cp15.vbar_el[new_el];
11200     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11201     unsigned int old_mode;
11202     unsigned int cur_el = arm_current_el(env);
11203     int rt;
11204 
11205     if (tcg_enabled()) {
11206         /*
11207          * Note that new_el can never be 0.  If cur_el is 0, then
11208          * el0_a64 is is_a64(), else el0_a64 is ignored.
11209          */
11210         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11211     }
11212 
11213     if (cur_el < new_el) {
11214         /*
11215          * Entry vector offset depends on whether the implemented EL
11216          * immediately lower than the target level is using AArch32 or AArch64
11217          */
11218         bool is_aa64;
11219         uint64_t hcr;
11220 
11221         switch (new_el) {
11222         case 3:
11223             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11224             break;
11225         case 2:
11226             hcr = arm_hcr_el2_eff(env);
11227             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11228                 is_aa64 = (hcr & HCR_RW) != 0;
11229                 break;
11230             }
11231             /* fall through */
11232         case 1:
11233             is_aa64 = is_a64(env);
11234             break;
11235         default:
11236             g_assert_not_reached();
11237         }
11238 
11239         if (is_aa64) {
11240             addr += 0x400;
11241         } else {
11242             addr += 0x600;
11243         }
11244     } else if (pstate_read(env) & PSTATE_SP) {
11245         addr += 0x200;
11246     }
11247 
11248     switch (cs->exception_index) {
11249     case EXCP_GPC:
11250         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11251                       env->cp15.mfar_el3);
11252         /* fall through */
11253     case EXCP_PREFETCH_ABORT:
11254     case EXCP_DATA_ABORT:
11255         /*
11256          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11257          * to be taken to the SError vector entrypoint.
11258          */
11259         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11260             syndrome_is_sync_extabt(env->exception.syndrome)) {
11261             addr += 0x180;
11262         }
11263         env->cp15.far_el[new_el] = env->exception.vaddress;
11264         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11265                       env->cp15.far_el[new_el]);
11266         /* fall through */
11267     case EXCP_BKPT:
11268     case EXCP_UDEF:
11269     case EXCP_SWI:
11270     case EXCP_HVC:
11271     case EXCP_HYP_TRAP:
11272     case EXCP_SMC:
11273         switch (syn_get_ec(env->exception.syndrome)) {
11274         case EC_ADVSIMDFPACCESSTRAP:
11275             /*
11276              * QEMU internal FP/SIMD syndromes from AArch32 include the
11277              * TA and coproc fields which are only exposed if the exception
11278              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11279              * AArch64 format syndrome.
11280              */
11281             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11282             break;
11283         case EC_CP14RTTRAP:
11284         case EC_CP15RTTRAP:
11285         case EC_CP14DTTRAP:
11286             /*
11287              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11288              * the raw register field from the insn; when taking this to
11289              * AArch64 we must convert it to the AArch64 view of the register
11290              * number. Notice that we read a 4-bit AArch32 register number and
11291              * write back a 5-bit AArch64 one.
11292              */
11293             rt = extract32(env->exception.syndrome, 5, 4);
11294             rt = aarch64_regnum(env, rt);
11295             env->exception.syndrome = deposit32(env->exception.syndrome,
11296                                                 5, 5, rt);
11297             break;
11298         case EC_CP15RRTTRAP:
11299         case EC_CP14RRTTRAP:
11300             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11301             rt = extract32(env->exception.syndrome, 5, 4);
11302             rt = aarch64_regnum(env, rt);
11303             env->exception.syndrome = deposit32(env->exception.syndrome,
11304                                                 5, 5, rt);
11305             rt = extract32(env->exception.syndrome, 10, 4);
11306             rt = aarch64_regnum(env, rt);
11307             env->exception.syndrome = deposit32(env->exception.syndrome,
11308                                                 10, 5, rt);
11309             break;
11310         }
11311         env->cp15.esr_el[new_el] = env->exception.syndrome;
11312         break;
11313     case EXCP_IRQ:
11314     case EXCP_VIRQ:
11315         addr += 0x80;
11316         break;
11317     case EXCP_FIQ:
11318     case EXCP_VFIQ:
11319         addr += 0x100;
11320         break;
11321     case EXCP_VSERR:
11322         addr += 0x180;
11323         /* Construct the SError syndrome from IDS and ISS fields. */
11324         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11325         env->cp15.esr_el[new_el] = env->exception.syndrome;
11326         break;
11327     default:
11328         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11329     }
11330 
11331     if (is_a64(env)) {
11332         old_mode = pstate_read(env);
11333         aarch64_save_sp(env, arm_current_el(env));
11334         env->elr_el[new_el] = env->pc;
11335 
11336         if (cur_el == 1 && new_el == 1) {
11337             uint64_t hcr = arm_hcr_el2_eff(env);
11338             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11339                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11340                 /*
11341                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11342                  * by setting M[3:2] to 0b10.
11343                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11344                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11345                  */
11346                 old_mode = deposit32(old_mode, 2, 2, 2);
11347             }
11348         }
11349     } else {
11350         old_mode = cpsr_read_for_spsr_elx(env);
11351         env->elr_el[new_el] = env->regs[15];
11352 
11353         aarch64_sync_32_to_64(env);
11354 
11355         env->condexec_bits = 0;
11356     }
11357     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11358 
11359     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11360                   env->elr_el[new_el]);
11361 
11362     if (cpu_isar_feature(aa64_pan, cpu)) {
11363         /* The value of PSTATE.PAN is normally preserved, except when ... */
11364         new_mode |= old_mode & PSTATE_PAN;
11365         switch (new_el) {
11366         case 2:
11367             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11368             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11369                 != (HCR_E2H | HCR_TGE)) {
11370                 break;
11371             }
11372             /* fall through */
11373         case 1:
11374             /* ... the target is EL1 ... */
11375             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11376             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11377                 new_mode |= PSTATE_PAN;
11378             }
11379             break;
11380         }
11381     }
11382     if (cpu_isar_feature(aa64_mte, cpu)) {
11383         new_mode |= PSTATE_TCO;
11384     }
11385 
11386     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11387         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11388             new_mode |= PSTATE_SSBS;
11389         } else {
11390             new_mode &= ~PSTATE_SSBS;
11391         }
11392     }
11393 
11394     pstate_write(env, PSTATE_DAIF | new_mode);
11395     env->aarch64 = true;
11396     aarch64_restore_sp(env, new_el);
11397 
11398     if (tcg_enabled()) {
11399         helper_rebuild_hflags_a64(env, new_el);
11400     }
11401 
11402     env->pc = addr;
11403 
11404     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11405                   new_el, env->pc, pstate_read(env));
11406 }
11407 
11408 /*
11409  * Do semihosting call and set the appropriate return value. All the
11410  * permission and validity checks have been done at translate time.
11411  *
11412  * We only see semihosting exceptions in TCG only as they are not
11413  * trapped to the hypervisor in KVM.
11414  */
11415 #ifdef CONFIG_TCG
11416 static void tcg_handle_semihosting(CPUState *cs)
11417 {
11418     ARMCPU *cpu = ARM_CPU(cs);
11419     CPUARMState *env = &cpu->env;
11420 
11421     if (is_a64(env)) {
11422         qemu_log_mask(CPU_LOG_INT,
11423                       "...handling as semihosting call 0x%" PRIx64 "\n",
11424                       env->xregs[0]);
11425         do_common_semihosting(cs);
11426         env->pc += 4;
11427     } else {
11428         qemu_log_mask(CPU_LOG_INT,
11429                       "...handling as semihosting call 0x%x\n",
11430                       env->regs[0]);
11431         do_common_semihosting(cs);
11432         env->regs[15] += env->thumb ? 2 : 4;
11433     }
11434 }
11435 #endif
11436 
11437 /*
11438  * Handle a CPU exception for A and R profile CPUs.
11439  * Do any appropriate logging, handle PSCI calls, and then hand off
11440  * to the AArch64-entry or AArch32-entry function depending on the
11441  * target exception level's register width.
11442  *
11443  * Note: this is used for both TCG (as the do_interrupt tcg op),
11444  *       and KVM to re-inject guest debug exceptions, and to
11445  *       inject a Synchronous-External-Abort.
11446  */
11447 void arm_cpu_do_interrupt(CPUState *cs)
11448 {
11449     ARMCPU *cpu = ARM_CPU(cs);
11450     CPUARMState *env = &cpu->env;
11451     unsigned int new_el = env->exception.target_el;
11452 
11453     assert(!arm_feature(env, ARM_FEATURE_M));
11454 
11455     arm_log_exception(cs);
11456     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11457                   new_el);
11458     if (qemu_loglevel_mask(CPU_LOG_INT)
11459         && !excp_is_internal(cs->exception_index)) {
11460         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11461                       syn_get_ec(env->exception.syndrome),
11462                       env->exception.syndrome);
11463     }
11464 
11465     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11466         arm_handle_psci_call(cpu);
11467         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11468         return;
11469     }
11470 
11471     /*
11472      * Semihosting semantics depend on the register width of the code
11473      * that caused the exception, not the target exception level, so
11474      * must be handled here.
11475      */
11476 #ifdef CONFIG_TCG
11477     if (cs->exception_index == EXCP_SEMIHOST) {
11478         tcg_handle_semihosting(cs);
11479         return;
11480     }
11481 #endif
11482 
11483     /*
11484      * Hooks may change global state so BQL should be held, also the
11485      * BQL needs to be held for any modification of
11486      * cs->interrupt_request.
11487      */
11488     g_assert(bql_locked());
11489 
11490     arm_call_pre_el_change_hook(cpu);
11491 
11492     assert(!excp_is_internal(cs->exception_index));
11493     if (arm_el_is_aa64(env, new_el)) {
11494         arm_cpu_do_interrupt_aarch64(cs);
11495     } else {
11496         arm_cpu_do_interrupt_aarch32(cs);
11497     }
11498 
11499     arm_call_el_change_hook(cpu);
11500 
11501     if (!kvm_enabled()) {
11502         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11503     }
11504 }
11505 #endif /* !CONFIG_USER_ONLY */
11506 
11507 uint64_t arm_sctlr(CPUARMState *env, int el)
11508 {
11509     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11510     if (el == 0) {
11511         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11512         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11513     }
11514     return env->cp15.sctlr_el[el];
11515 }
11516 
11517 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11518 {
11519     if (regime_has_2_ranges(mmu_idx)) {
11520         return extract64(tcr, 37, 2);
11521     } else if (regime_is_stage2(mmu_idx)) {
11522         return 0; /* VTCR_EL2 */
11523     } else {
11524         /* Replicate the single TBI bit so we always have 2 bits.  */
11525         return extract32(tcr, 20, 1) * 3;
11526     }
11527 }
11528 
11529 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11530 {
11531     if (regime_has_2_ranges(mmu_idx)) {
11532         return extract64(tcr, 51, 2);
11533     } else if (regime_is_stage2(mmu_idx)) {
11534         return 0; /* VTCR_EL2 */
11535     } else {
11536         /* Replicate the single TBID bit so we always have 2 bits.  */
11537         return extract32(tcr, 29, 1) * 3;
11538     }
11539 }
11540 
11541 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11542 {
11543     if (regime_has_2_ranges(mmu_idx)) {
11544         return extract64(tcr, 57, 2);
11545     } else {
11546         /* Replicate the single TCMA bit so we always have 2 bits.  */
11547         return extract32(tcr, 30, 1) * 3;
11548     }
11549 }
11550 
11551 static ARMGranuleSize tg0_to_gran_size(int tg)
11552 {
11553     switch (tg) {
11554     case 0:
11555         return Gran4K;
11556     case 1:
11557         return Gran64K;
11558     case 2:
11559         return Gran16K;
11560     default:
11561         return GranInvalid;
11562     }
11563 }
11564 
11565 static ARMGranuleSize tg1_to_gran_size(int tg)
11566 {
11567     switch (tg) {
11568     case 1:
11569         return Gran16K;
11570     case 2:
11571         return Gran4K;
11572     case 3:
11573         return Gran64K;
11574     default:
11575         return GranInvalid;
11576     }
11577 }
11578 
11579 static inline bool have4k(ARMCPU *cpu, bool stage2)
11580 {
11581     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11582         : cpu_isar_feature(aa64_tgran4, cpu);
11583 }
11584 
11585 static inline bool have16k(ARMCPU *cpu, bool stage2)
11586 {
11587     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11588         : cpu_isar_feature(aa64_tgran16, cpu);
11589 }
11590 
11591 static inline bool have64k(ARMCPU *cpu, bool stage2)
11592 {
11593     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11594         : cpu_isar_feature(aa64_tgran64, cpu);
11595 }
11596 
11597 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11598                                          bool stage2)
11599 {
11600     switch (gran) {
11601     case Gran4K:
11602         if (have4k(cpu, stage2)) {
11603             return gran;
11604         }
11605         break;
11606     case Gran16K:
11607         if (have16k(cpu, stage2)) {
11608             return gran;
11609         }
11610         break;
11611     case Gran64K:
11612         if (have64k(cpu, stage2)) {
11613             return gran;
11614         }
11615         break;
11616     case GranInvalid:
11617         break;
11618     }
11619     /*
11620      * If the guest selects a granule size that isn't implemented,
11621      * the architecture requires that we behave as if it selected one
11622      * that is (with an IMPDEF choice of which one to pick). We choose
11623      * to implement the smallest supported granule size.
11624      */
11625     if (have4k(cpu, stage2)) {
11626         return Gran4K;
11627     }
11628     if (have16k(cpu, stage2)) {
11629         return Gran16K;
11630     }
11631     assert(have64k(cpu, stage2));
11632     return Gran64K;
11633 }
11634 
11635 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11636                                    ARMMMUIdx mmu_idx, bool data,
11637                                    bool el1_is_aa32)
11638 {
11639     uint64_t tcr = regime_tcr(env, mmu_idx);
11640     bool epd, hpd, tsz_oob, ds, ha, hd;
11641     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11642     ARMGranuleSize gran;
11643     ARMCPU *cpu = env_archcpu(env);
11644     bool stage2 = regime_is_stage2(mmu_idx);
11645 
11646     if (!regime_has_2_ranges(mmu_idx)) {
11647         select = 0;
11648         tsz = extract32(tcr, 0, 6);
11649         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11650         if (stage2) {
11651             /* VTCR_EL2 */
11652             hpd = false;
11653         } else {
11654             hpd = extract32(tcr, 24, 1);
11655         }
11656         epd = false;
11657         sh = extract32(tcr, 12, 2);
11658         ps = extract32(tcr, 16, 3);
11659         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11660         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11661         ds = extract64(tcr, 32, 1);
11662     } else {
11663         bool e0pd;
11664 
11665         /*
11666          * Bit 55 is always between the two regions, and is canonical for
11667          * determining if address tagging is enabled.
11668          */
11669         select = extract64(va, 55, 1);
11670         if (!select) {
11671             tsz = extract32(tcr, 0, 6);
11672             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11673             epd = extract32(tcr, 7, 1);
11674             sh = extract32(tcr, 12, 2);
11675             hpd = extract64(tcr, 41, 1);
11676             e0pd = extract64(tcr, 55, 1);
11677         } else {
11678             tsz = extract32(tcr, 16, 6);
11679             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11680             epd = extract32(tcr, 23, 1);
11681             sh = extract32(tcr, 28, 2);
11682             hpd = extract64(tcr, 42, 1);
11683             e0pd = extract64(tcr, 56, 1);
11684         }
11685         ps = extract64(tcr, 32, 3);
11686         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11687         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11688         ds = extract64(tcr, 59, 1);
11689 
11690         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11691             regime_is_user(env, mmu_idx)) {
11692             epd = true;
11693         }
11694     }
11695 
11696     gran = sanitize_gran_size(cpu, gran, stage2);
11697 
11698     if (cpu_isar_feature(aa64_st, cpu)) {
11699         max_tsz = 48 - (gran == Gran64K);
11700     } else {
11701         max_tsz = 39;
11702     }
11703 
11704     /*
11705      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11706      * adjust the effective value of DS, as documented.
11707      */
11708     min_tsz = 16;
11709     if (gran == Gran64K) {
11710         if (cpu_isar_feature(aa64_lva, cpu)) {
11711             min_tsz = 12;
11712         }
11713         ds = false;
11714     } else if (ds) {
11715         if (regime_is_stage2(mmu_idx)) {
11716             if (gran == Gran16K) {
11717                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11718             } else {
11719                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11720             }
11721         } else {
11722             if (gran == Gran16K) {
11723                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11724             } else {
11725                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11726             }
11727         }
11728         if (ds) {
11729             min_tsz = 12;
11730         }
11731     }
11732 
11733     if (stage2 && el1_is_aa32) {
11734         /*
11735          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11736          * are loosened: a configured IPA of 40 bits is permitted even if
11737          * the implemented PA is less than that (and so a 40 bit IPA would
11738          * fault for an AArch64 EL1). See R_DTLMN.
11739          */
11740         min_tsz = MIN(min_tsz, 24);
11741     }
11742 
11743     if (tsz > max_tsz) {
11744         tsz = max_tsz;
11745         tsz_oob = true;
11746     } else if (tsz < min_tsz) {
11747         tsz = min_tsz;
11748         tsz_oob = true;
11749     } else {
11750         tsz_oob = false;
11751     }
11752 
11753     /* Present TBI as a composite with TBID.  */
11754     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11755     if (!data) {
11756         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11757     }
11758     tbi = (tbi >> select) & 1;
11759 
11760     return (ARMVAParameters) {
11761         .tsz = tsz,
11762         .ps = ps,
11763         .sh = sh,
11764         .select = select,
11765         .tbi = tbi,
11766         .epd = epd,
11767         .hpd = hpd,
11768         .tsz_oob = tsz_oob,
11769         .ds = ds,
11770         .ha = ha,
11771         .hd = ha && hd,
11772         .gran = gran,
11773     };
11774 }
11775 
11776 /*
11777  * Note that signed overflow is undefined in C.  The following routines are
11778  * careful to use unsigned types where modulo arithmetic is required.
11779  * Failure to do so _will_ break on newer gcc.
11780  */
11781 
11782 /* Signed saturating arithmetic.  */
11783 
11784 /* Perform 16-bit signed saturating addition.  */
11785 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11786 {
11787     uint16_t res;
11788 
11789     res = a + b;
11790     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11791         if (a & 0x8000) {
11792             res = 0x8000;
11793         } else {
11794             res = 0x7fff;
11795         }
11796     }
11797     return res;
11798 }
11799 
11800 /* Perform 8-bit signed saturating addition.  */
11801 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11802 {
11803     uint8_t res;
11804 
11805     res = a + b;
11806     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11807         if (a & 0x80) {
11808             res = 0x80;
11809         } else {
11810             res = 0x7f;
11811         }
11812     }
11813     return res;
11814 }
11815 
11816 /* Perform 16-bit signed saturating subtraction.  */
11817 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11818 {
11819     uint16_t res;
11820 
11821     res = a - b;
11822     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11823         if (a & 0x8000) {
11824             res = 0x8000;
11825         } else {
11826             res = 0x7fff;
11827         }
11828     }
11829     return res;
11830 }
11831 
11832 /* Perform 8-bit signed saturating subtraction.  */
11833 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11834 {
11835     uint8_t res;
11836 
11837     res = a - b;
11838     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11839         if (a & 0x80) {
11840             res = 0x80;
11841         } else {
11842             res = 0x7f;
11843         }
11844     }
11845     return res;
11846 }
11847 
11848 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11849 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11850 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11851 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11852 #define PFX q
11853 
11854 #include "op_addsub.h"
11855 
11856 /* Unsigned saturating arithmetic.  */
11857 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11858 {
11859     uint16_t res;
11860     res = a + b;
11861     if (res < a) {
11862         res = 0xffff;
11863     }
11864     return res;
11865 }
11866 
11867 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11868 {
11869     if (a > b) {
11870         return a - b;
11871     } else {
11872         return 0;
11873     }
11874 }
11875 
11876 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11877 {
11878     uint8_t res;
11879     res = a + b;
11880     if (res < a) {
11881         res = 0xff;
11882     }
11883     return res;
11884 }
11885 
11886 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11887 {
11888     if (a > b) {
11889         return a - b;
11890     } else {
11891         return 0;
11892     }
11893 }
11894 
11895 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11896 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11897 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11898 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11899 #define PFX uq
11900 
11901 #include "op_addsub.h"
11902 
11903 /* Signed modulo arithmetic.  */
11904 #define SARITH16(a, b, n, op) do { \
11905     int32_t sum; \
11906     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11907     RESULT(sum, n, 16); \
11908     if (sum >= 0) \
11909         ge |= 3 << (n * 2); \
11910     } while (0)
11911 
11912 #define SARITH8(a, b, n, op) do { \
11913     int32_t sum; \
11914     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11915     RESULT(sum, n, 8); \
11916     if (sum >= 0) \
11917         ge |= 1 << n; \
11918     } while (0)
11919 
11920 
11921 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11922 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11923 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11924 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11925 #define PFX s
11926 #define ARITH_GE
11927 
11928 #include "op_addsub.h"
11929 
11930 /* Unsigned modulo arithmetic.  */
11931 #define ADD16(a, b, n) do { \
11932     uint32_t sum; \
11933     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11934     RESULT(sum, n, 16); \
11935     if ((sum >> 16) == 1) \
11936         ge |= 3 << (n * 2); \
11937     } while (0)
11938 
11939 #define ADD8(a, b, n) do { \
11940     uint32_t sum; \
11941     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11942     RESULT(sum, n, 8); \
11943     if ((sum >> 8) == 1) \
11944         ge |= 1 << n; \
11945     } while (0)
11946 
11947 #define SUB16(a, b, n) do { \
11948     uint32_t sum; \
11949     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11950     RESULT(sum, n, 16); \
11951     if ((sum >> 16) == 0) \
11952         ge |= 3 << (n * 2); \
11953     } while (0)
11954 
11955 #define SUB8(a, b, n) do { \
11956     uint32_t sum; \
11957     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11958     RESULT(sum, n, 8); \
11959     if ((sum >> 8) == 0) \
11960         ge |= 1 << n; \
11961     } while (0)
11962 
11963 #define PFX u
11964 #define ARITH_GE
11965 
11966 #include "op_addsub.h"
11967 
11968 /* Halved signed arithmetic.  */
11969 #define ADD16(a, b, n) \
11970   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11971 #define SUB16(a, b, n) \
11972   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11973 #define ADD8(a, b, n) \
11974   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11975 #define SUB8(a, b, n) \
11976   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11977 #define PFX sh
11978 
11979 #include "op_addsub.h"
11980 
11981 /* Halved unsigned arithmetic.  */
11982 #define ADD16(a, b, n) \
11983   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11984 #define SUB16(a, b, n) \
11985   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11986 #define ADD8(a, b, n) \
11987   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11988 #define SUB8(a, b, n) \
11989   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11990 #define PFX uh
11991 
11992 #include "op_addsub.h"
11993 
11994 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11995 {
11996     if (a > b) {
11997         return a - b;
11998     } else {
11999         return b - a;
12000     }
12001 }
12002 
12003 /* Unsigned sum of absolute byte differences.  */
12004 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12005 {
12006     uint32_t sum;
12007     sum = do_usad(a, b);
12008     sum += do_usad(a >> 8, b >> 8);
12009     sum += do_usad(a >> 16, b >> 16);
12010     sum += do_usad(a >> 24, b >> 24);
12011     return sum;
12012 }
12013 
12014 /* For ARMv6 SEL instruction.  */
12015 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12016 {
12017     uint32_t mask;
12018 
12019     mask = 0;
12020     if (flags & 1) {
12021         mask |= 0xff;
12022     }
12023     if (flags & 2) {
12024         mask |= 0xff00;
12025     }
12026     if (flags & 4) {
12027         mask |= 0xff0000;
12028     }
12029     if (flags & 8) {
12030         mask |= 0xff000000;
12031     }
12032     return (a & mask) | (b & ~mask);
12033 }
12034 
12035 /*
12036  * CRC helpers.
12037  * The upper bytes of val (above the number specified by 'bytes') must have
12038  * been zeroed out by the caller.
12039  */
12040 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12041 {
12042     uint8_t buf[4];
12043 
12044     stl_le_p(buf, val);
12045 
12046     /* zlib crc32 converts the accumulator and output to one's complement.  */
12047     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12048 }
12049 
12050 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12051 {
12052     uint8_t buf[4];
12053 
12054     stl_le_p(buf, val);
12055 
12056     /* Linux crc32c converts the output to one's complement.  */
12057     return crc32c(acc, buf, bytes) ^ 0xffffffff;
12058 }
12059 
12060 /*
12061  * Return the exception level to which FP-disabled exceptions should
12062  * be taken, or 0 if FP is enabled.
12063  */
12064 int fp_exception_el(CPUARMState *env, int cur_el)
12065 {
12066 #ifndef CONFIG_USER_ONLY
12067     uint64_t hcr_el2;
12068 
12069     /*
12070      * CPACR and the CPTR registers don't exist before v6, so FP is
12071      * always accessible
12072      */
12073     if (!arm_feature(env, ARM_FEATURE_V6)) {
12074         return 0;
12075     }
12076 
12077     if (arm_feature(env, ARM_FEATURE_M)) {
12078         /* CPACR can cause a NOCP UsageFault taken to current security state */
12079         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12080             return 1;
12081         }
12082 
12083         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12084             if (!extract32(env->v7m.nsacr, 10, 1)) {
12085                 /* FP insns cause a NOCP UsageFault taken to Secure */
12086                 return 3;
12087             }
12088         }
12089 
12090         return 0;
12091     }
12092 
12093     hcr_el2 = arm_hcr_el2_eff(env);
12094 
12095     /*
12096      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12097      * 0, 2 : trap EL0 and EL1/PL1 accesses
12098      * 1    : trap only EL0 accesses
12099      * 3    : trap no accesses
12100      * This register is ignored if E2H+TGE are both set.
12101      */
12102     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12103         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12104 
12105         switch (fpen) {
12106         case 1:
12107             if (cur_el != 0) {
12108                 break;
12109             }
12110             /* fall through */
12111         case 0:
12112         case 2:
12113             /* Trap from Secure PL0 or PL1 to Secure PL1. */
12114             if (!arm_el_is_aa64(env, 3)
12115                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12116                 return 3;
12117             }
12118             if (cur_el <= 1) {
12119                 return 1;
12120             }
12121             break;
12122         }
12123     }
12124 
12125     /*
12126      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12127      * to control non-secure access to the FPU. It doesn't have any
12128      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12129      */
12130     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12131          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12132         if (!extract32(env->cp15.nsacr, 10, 1)) {
12133             /* FP insns act as UNDEF */
12134             return cur_el == 2 ? 2 : 1;
12135         }
12136     }
12137 
12138     /*
12139      * CPTR_EL2 is present in v7VE or v8, and changes format
12140      * with HCR_EL2.E2H (regardless of TGE).
12141      */
12142     if (cur_el <= 2) {
12143         if (hcr_el2 & HCR_E2H) {
12144             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12145             case 1:
12146                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12147                     break;
12148                 }
12149                 /* fall through */
12150             case 0:
12151             case 2:
12152                 return 2;
12153             }
12154         } else if (arm_is_el2_enabled(env)) {
12155             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12156                 return 2;
12157             }
12158         }
12159     }
12160 
12161     /* CPTR_EL3 : present in v8 */
12162     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12163         /* Trap all FP ops to EL3 */
12164         return 3;
12165     }
12166 #endif
12167     return 0;
12168 }
12169 
12170 /* Return the exception level we're running at if this is our mmu_idx */
12171 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12172 {
12173     if (mmu_idx & ARM_MMU_IDX_M) {
12174         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12175     }
12176 
12177     switch (mmu_idx) {
12178     case ARMMMUIdx_E10_0:
12179     case ARMMMUIdx_E20_0:
12180         return 0;
12181     case ARMMMUIdx_E10_1:
12182     case ARMMMUIdx_E10_1_PAN:
12183         return 1;
12184     case ARMMMUIdx_E2:
12185     case ARMMMUIdx_E20_2:
12186     case ARMMMUIdx_E20_2_PAN:
12187         return 2;
12188     case ARMMMUIdx_E3:
12189         return 3;
12190     default:
12191         g_assert_not_reached();
12192     }
12193 }
12194 
12195 #ifndef CONFIG_TCG
12196 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12197 {
12198     g_assert_not_reached();
12199 }
12200 #endif
12201 
12202 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12203 {
12204     ARMMMUIdx idx;
12205     uint64_t hcr;
12206 
12207     if (arm_feature(env, ARM_FEATURE_M)) {
12208         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12209     }
12210 
12211     /* See ARM pseudo-function ELIsInHost.  */
12212     switch (el) {
12213     case 0:
12214         hcr = arm_hcr_el2_eff(env);
12215         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12216             idx = ARMMMUIdx_E20_0;
12217         } else {
12218             idx = ARMMMUIdx_E10_0;
12219         }
12220         break;
12221     case 1:
12222         if (arm_pan_enabled(env)) {
12223             idx = ARMMMUIdx_E10_1_PAN;
12224         } else {
12225             idx = ARMMMUIdx_E10_1;
12226         }
12227         break;
12228     case 2:
12229         /* Note that TGE does not apply at EL2.  */
12230         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12231             if (arm_pan_enabled(env)) {
12232                 idx = ARMMMUIdx_E20_2_PAN;
12233             } else {
12234                 idx = ARMMMUIdx_E20_2;
12235             }
12236         } else {
12237             idx = ARMMMUIdx_E2;
12238         }
12239         break;
12240     case 3:
12241         return ARMMMUIdx_E3;
12242     default:
12243         g_assert_not_reached();
12244     }
12245 
12246     return idx;
12247 }
12248 
12249 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12250 {
12251     return arm_mmu_idx_el(env, arm_current_el(env));
12252 }
12253 
12254 static bool mve_no_pred(CPUARMState *env)
12255 {
12256     /*
12257      * Return true if there is definitely no predication of MVE
12258      * instructions by VPR or LTPSIZE. (Returning false even if there
12259      * isn't any predication is OK; generated code will just be
12260      * a little worse.)
12261      * If the CPU does not implement MVE then this TB flag is always 0.
12262      *
12263      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12264      * logic in gen_update_fp_context() needs to be updated to match.
12265      *
12266      * We do not include the effect of the ECI bits here -- they are
12267      * tracked in other TB flags. This simplifies the logic for
12268      * "when did we emit code that changes the MVE_NO_PRED TB flag
12269      * and thus need to end the TB?".
12270      */
12271     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12272         return false;
12273     }
12274     if (env->v7m.vpr) {
12275         return false;
12276     }
12277     if (env->v7m.ltpsize < 4) {
12278         return false;
12279     }
12280     return true;
12281 }
12282 
12283 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12284                           uint64_t *cs_base, uint32_t *pflags)
12285 {
12286     CPUARMTBFlags flags;
12287 
12288     assert_hflags_rebuild_correctly(env);
12289     flags = env->hflags;
12290 
12291     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12292         *pc = env->pc;
12293         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12294             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12295         }
12296     } else {
12297         *pc = env->regs[15];
12298 
12299         if (arm_feature(env, ARM_FEATURE_M)) {
12300             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12301                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12302                 != env->v7m.secure) {
12303                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12304             }
12305 
12306             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12307                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12308                  (env->v7m.secure &&
12309                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12310                 /*
12311                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12312                  * active FP context; we must create a new FP context before
12313                  * executing any FP insn.
12314                  */
12315                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12316             }
12317 
12318             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12319             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12320                 DP_TBFLAG_M32(flags, LSPACT, 1);
12321             }
12322 
12323             if (mve_no_pred(env)) {
12324                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12325             }
12326         } else {
12327             /*
12328              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12329              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12330              */
12331             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12332                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12333             } else {
12334                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12335                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12336             }
12337             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12338                 DP_TBFLAG_A32(flags, VFPEN, 1);
12339             }
12340         }
12341 
12342         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12343         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12344     }
12345 
12346     /*
12347      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12348      * states defined in the ARM ARM for software singlestep:
12349      *  SS_ACTIVE   PSTATE.SS   State
12350      *     0            x       Inactive (the TB flag for SS is always 0)
12351      *     1            0       Active-pending
12352      *     1            1       Active-not-pending
12353      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12354      */
12355     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12356         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12357     }
12358 
12359     *pflags = flags.flags;
12360     *cs_base = flags.flags2;
12361 }
12362 
12363 #ifdef TARGET_AARCH64
12364 /*
12365  * The manual says that when SVE is enabled and VQ is widened the
12366  * implementation is allowed to zero the previously inaccessible
12367  * portion of the registers.  The corollary to that is that when
12368  * SVE is enabled and VQ is narrowed we are also allowed to zero
12369  * the now inaccessible portion of the registers.
12370  *
12371  * The intent of this is that no predicate bit beyond VQ is ever set.
12372  * Which means that some operations on predicate registers themselves
12373  * may operate on full uint64_t or even unrolled across the maximum
12374  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12375  * may well be cheaper than conditionals to restrict the operation
12376  * to the relevant portion of a uint16_t[16].
12377  */
12378 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12379 {
12380     int i, j;
12381     uint64_t pmask;
12382 
12383     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12384     assert(vq <= env_archcpu(env)->sve_max_vq);
12385 
12386     /* Zap the high bits of the zregs.  */
12387     for (i = 0; i < 32; i++) {
12388         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12389     }
12390 
12391     /* Zap the high bits of the pregs and ffr.  */
12392     pmask = 0;
12393     if (vq & 3) {
12394         pmask = ~(-1ULL << (16 * (vq & 3)));
12395     }
12396     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12397         for (i = 0; i < 17; ++i) {
12398             env->vfp.pregs[i].p[j] &= pmask;
12399         }
12400         pmask = 0;
12401     }
12402 }
12403 
12404 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12405 {
12406     int exc_el;
12407 
12408     if (sm) {
12409         exc_el = sme_exception_el(env, el);
12410     } else {
12411         exc_el = sve_exception_el(env, el);
12412     }
12413     if (exc_el) {
12414         return 0; /* disabled */
12415     }
12416     return sve_vqm1_for_el_sm(env, el, sm);
12417 }
12418 
12419 /*
12420  * Notice a change in SVE vector size when changing EL.
12421  */
12422 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12423                            int new_el, bool el0_a64)
12424 {
12425     ARMCPU *cpu = env_archcpu(env);
12426     int old_len, new_len;
12427     bool old_a64, new_a64, sm;
12428 
12429     /* Nothing to do if no SVE.  */
12430     if (!cpu_isar_feature(aa64_sve, cpu)) {
12431         return;
12432     }
12433 
12434     /* Nothing to do if FP is disabled in either EL.  */
12435     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12436         return;
12437     }
12438 
12439     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12440     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12441 
12442     /*
12443      * Both AArch64.TakeException and AArch64.ExceptionReturn
12444      * invoke ResetSVEState when taking an exception from, or
12445      * returning to, AArch32 state when PSTATE.SM is enabled.
12446      */
12447     sm = FIELD_EX64(env->svcr, SVCR, SM);
12448     if (old_a64 != new_a64 && sm) {
12449         arm_reset_sve_state(env);
12450         return;
12451     }
12452 
12453     /*
12454      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12455      * at ELx, or not available because the EL is in AArch32 state, then
12456      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12457      * has an effective value of 0".
12458      *
12459      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12460      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12461      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12462      * we already have the correct register contents when encountering the
12463      * vq0->vq0 transition between EL0->EL1.
12464      */
12465     old_len = new_len = 0;
12466     if (old_a64) {
12467         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12468     }
12469     if (new_a64) {
12470         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12471     }
12472 
12473     /* When changing vector length, clear inaccessible state.  */
12474     if (new_len < old_len) {
12475         aarch64_sve_narrow_vq(env, new_len + 1);
12476     }
12477 }
12478 #endif
12479 
12480 #ifndef CONFIG_USER_ONLY
12481 ARMSecuritySpace arm_security_space(CPUARMState *env)
12482 {
12483     if (arm_feature(env, ARM_FEATURE_M)) {
12484         return arm_secure_to_space(env->v7m.secure);
12485     }
12486 
12487     /*
12488      * If EL3 is not supported then the secure state is implementation
12489      * defined, in which case QEMU defaults to non-secure.
12490      */
12491     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12492         return ARMSS_NonSecure;
12493     }
12494 
12495     /* Check for AArch64 EL3 or AArch32 Mon. */
12496     if (is_a64(env)) {
12497         if (extract32(env->pstate, 2, 2) == 3) {
12498             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12499                 return ARMSS_Root;
12500             } else {
12501                 return ARMSS_Secure;
12502             }
12503         }
12504     } else {
12505         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12506             return ARMSS_Secure;
12507         }
12508     }
12509 
12510     return arm_security_space_below_el3(env);
12511 }
12512 
12513 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12514 {
12515     assert(!arm_feature(env, ARM_FEATURE_M));
12516 
12517     /*
12518      * If EL3 is not supported then the secure state is implementation
12519      * defined, in which case QEMU defaults to non-secure.
12520      */
12521     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12522         return ARMSS_NonSecure;
12523     }
12524 
12525     /*
12526      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12527      * Ignoring NSE when !NS retains consistency without having to
12528      * modify other predicates.
12529      */
12530     if (!(env->cp15.scr_el3 & SCR_NS)) {
12531         return ARMSS_Secure;
12532     } else if (env->cp15.scr_el3 & SCR_NSE) {
12533         return ARMSS_Realm;
12534     } else {
12535         return ARMSS_NonSecure;
12536     }
12537 }
12538 #endif /* !CONFIG_USER_ONLY */
12539