xref: /qemu/target/arm/helper.c (revision a0e93dd8)
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 #include "target/arm/gtimer.h"
34 
35 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
36 
37 static void switch_mode(CPUARMState *env, int mode);
38 
39 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
40 {
41     assert(ri->fieldoffset);
42     if (cpreg_field_is_64bit(ri)) {
43         return CPREG_FIELD64(env, ri);
44     } else {
45         return CPREG_FIELD32(env, ri);
46     }
47 }
48 
49 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
50 {
51     assert(ri->fieldoffset);
52     if (cpreg_field_is_64bit(ri)) {
53         CPREG_FIELD64(env, ri) = value;
54     } else {
55         CPREG_FIELD32(env, ri) = value;
56     }
57 }
58 
59 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
60 {
61     return (char *)env + ri->fieldoffset;
62 }
63 
64 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66     /* Raw read of a coprocessor register (as needed for migration, etc). */
67     if (ri->type & ARM_CP_CONST) {
68         return ri->resetvalue;
69     } else if (ri->raw_readfn) {
70         return ri->raw_readfn(env, ri);
71     } else if (ri->readfn) {
72         return ri->readfn(env, ri);
73     } else {
74         return raw_read(env, ri);
75     }
76 }
77 
78 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
79                              uint64_t v)
80 {
81     /*
82      * Raw write of a coprocessor register (as needed for migration, etc).
83      * Note that constant registers are treated as write-ignored; the
84      * caller should check for success by whether a readback gives the
85      * value written.
86      */
87     if (ri->type & ARM_CP_CONST) {
88         return;
89     } else if (ri->raw_writefn) {
90         ri->raw_writefn(env, ri, v);
91     } else if (ri->writefn) {
92         ri->writefn(env, ri, v);
93     } else {
94         raw_write(env, ri, v);
95     }
96 }
97 
98 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
99 {
100    /*
101     * Return true if the regdef would cause an assertion if you called
102     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
103     * program bug for it not to have the NO_RAW flag).
104     * NB that returning false here doesn't necessarily mean that calling
105     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
106     * read/write access functions which are safe for raw use" from "has
107     * read/write access functions which have side effects but has forgotten
108     * to provide raw access functions".
109     * The tests here line up with the conditions in read/write_raw_cp_reg()
110     * and assertions in raw_read()/raw_write().
111     */
112     if ((ri->type & ARM_CP_CONST) ||
113         ri->fieldoffset ||
114         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
115         return false;
116     }
117     return true;
118 }
119 
120 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
121 {
122     /* Write the coprocessor state from cpu->env to the (index,value) list. */
123     int i;
124     bool ok = true;
125 
126     for (i = 0; i < cpu->cpreg_array_len; i++) {
127         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
128         const ARMCPRegInfo *ri;
129         uint64_t newval;
130 
131         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
132         if (!ri) {
133             ok = false;
134             continue;
135         }
136         if (ri->type & ARM_CP_NO_RAW) {
137             continue;
138         }
139 
140         newval = read_raw_cp_reg(&cpu->env, ri);
141         if (kvm_sync) {
142             /*
143              * Only sync if the previous list->cpustate sync succeeded.
144              * Rather than tracking the success/failure state for every
145              * item in the list, we just recheck "does the raw write we must
146              * have made in write_list_to_cpustate() read back OK" here.
147              */
148             uint64_t oldval = cpu->cpreg_values[i];
149 
150             if (oldval == newval) {
151                 continue;
152             }
153 
154             write_raw_cp_reg(&cpu->env, ri, oldval);
155             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
156                 continue;
157             }
158 
159             write_raw_cp_reg(&cpu->env, ri, newval);
160         }
161         cpu->cpreg_values[i] = newval;
162     }
163     return ok;
164 }
165 
166 bool write_list_to_cpustate(ARMCPU *cpu)
167 {
168     int i;
169     bool ok = true;
170 
171     for (i = 0; i < cpu->cpreg_array_len; i++) {
172         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
173         uint64_t v = cpu->cpreg_values[i];
174         const ARMCPRegInfo *ri;
175 
176         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
177         if (!ri) {
178             ok = false;
179             continue;
180         }
181         if (ri->type & ARM_CP_NO_RAW) {
182             continue;
183         }
184         /*
185          * Write value and confirm it reads back as written
186          * (to catch read-only registers and partially read-only
187          * registers where the incoming migration value doesn't match)
188          */
189         write_raw_cp_reg(&cpu->env, ri, v);
190         if (read_raw_cp_reg(&cpu->env, ri) != v) {
191             ok = false;
192         }
193     }
194     return ok;
195 }
196 
197 static void add_cpreg_to_list(gpointer key, gpointer opaque)
198 {
199     ARMCPU *cpu = opaque;
200     uint32_t regidx = (uintptr_t)key;
201     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
202 
203     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
204         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
205         /* The value array need not be initialized at this point */
206         cpu->cpreg_array_len++;
207     }
208 }
209 
210 static void count_cpreg(gpointer key, gpointer opaque)
211 {
212     ARMCPU *cpu = opaque;
213     const ARMCPRegInfo *ri;
214 
215     ri = g_hash_table_lookup(cpu->cp_regs, key);
216 
217     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
218         cpu->cpreg_array_len++;
219     }
220 }
221 
222 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
223 {
224     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
225     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
226 
227     if (aidx > bidx) {
228         return 1;
229     }
230     if (aidx < bidx) {
231         return -1;
232     }
233     return 0;
234 }
235 
236 void init_cpreg_list(ARMCPU *cpu)
237 {
238     /*
239      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
240      * Note that we require cpreg_tuples[] to be sorted by key ID.
241      */
242     GList *keys;
243     int arraylen;
244 
245     keys = g_hash_table_get_keys(cpu->cp_regs);
246     keys = g_list_sort(keys, cpreg_key_compare);
247 
248     cpu->cpreg_array_len = 0;
249 
250     g_list_foreach(keys, count_cpreg, cpu);
251 
252     arraylen = cpu->cpreg_array_len;
253     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
254     cpu->cpreg_values = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
258     cpu->cpreg_array_len = 0;
259 
260     g_list_foreach(keys, add_cpreg_to_list, cpu);
261 
262     assert(cpu->cpreg_array_len == arraylen);
263 
264     g_list_free(keys);
265 }
266 
267 static bool arm_pan_enabled(CPUARMState *env)
268 {
269     if (is_a64(env)) {
270         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
271             return false;
272         }
273         return env->pstate & PSTATE_PAN;
274     } else {
275         return env->uncached_cpsr & CPSR_PAN;
276     }
277 }
278 
279 /*
280  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
281  */
282 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
283                                         const ARMCPRegInfo *ri,
284                                         bool isread)
285 {
286     if (!is_a64(env) && arm_current_el(env) == 3 &&
287         arm_is_secure_below_el3(env)) {
288         return CP_ACCESS_TRAP_UNCATEGORIZED;
289     }
290     return CP_ACCESS_OK;
291 }
292 
293 /*
294  * Some secure-only AArch32 registers trap to EL3 if used from
295  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
296  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
297  * We assume that the .access field is set to PL1_RW.
298  */
299 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
300                                             const ARMCPRegInfo *ri,
301                                             bool isread)
302 {
303     if (arm_current_el(env) == 3) {
304         return CP_ACCESS_OK;
305     }
306     if (arm_is_secure_below_el3(env)) {
307         if (env->cp15.scr_el3 & SCR_EEL2) {
308             return CP_ACCESS_TRAP_EL2;
309         }
310         return CP_ACCESS_TRAP_EL3;
311     }
312     /* This will be EL1 NS and EL2 NS, which just UNDEF */
313     return CP_ACCESS_TRAP_UNCATEGORIZED;
314 }
315 
316 /*
317  * Check for traps to performance monitor registers, which are controlled
318  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
319  */
320 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
321                                  bool isread)
322 {
323     int el = arm_current_el(env);
324     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
325 
326     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
327         return CP_ACCESS_TRAP_EL2;
328     }
329     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
330         return CP_ACCESS_TRAP_EL3;
331     }
332     return CP_ACCESS_OK;
333 }
334 
335 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
336 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
337                                bool isread)
338 {
339     if (arm_current_el(env) == 1) {
340         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
341         if (arm_hcr_el2_eff(env) & trap) {
342             return CP_ACCESS_TRAP_EL2;
343         }
344     }
345     return CP_ACCESS_OK;
346 }
347 
348 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
349 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
350                                  bool isread)
351 {
352     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
353         return CP_ACCESS_TRAP_EL2;
354     }
355     return CP_ACCESS_OK;
356 }
357 
358 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
359 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
360                                   bool isread)
361 {
362     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
363         return CP_ACCESS_TRAP_EL2;
364     }
365     return CP_ACCESS_OK;
366 }
367 
368 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
369 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
370                                   bool isread)
371 {
372     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
373         return CP_ACCESS_TRAP_EL2;
374     }
375     return CP_ACCESS_OK;
376 }
377 
378 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
379 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
380                                     bool isread)
381 {
382     if (arm_current_el(env) == 1 &&
383         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
384         return CP_ACCESS_TRAP_EL2;
385     }
386     return CP_ACCESS_OK;
387 }
388 
389 #ifdef TARGET_AARCH64
390 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
391 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
392                                     bool isread)
393 {
394     if (arm_current_el(env) == 1 &&
395         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
396         return CP_ACCESS_TRAP_EL2;
397     }
398     return CP_ACCESS_OK;
399 }
400 #endif
401 
402 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
403 {
404     ARMCPU *cpu = env_archcpu(env);
405 
406     raw_write(env, ri, value);
407     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
408 }
409 
410 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
411 {
412     ARMCPU *cpu = env_archcpu(env);
413 
414     if (raw_read(env, ri) != value) {
415         /*
416          * Unlike real hardware the qemu TLB uses virtual addresses,
417          * not modified virtual addresses, so this causes a TLB flush.
418          */
419         tlb_flush(CPU(cpu));
420         raw_write(env, ri, value);
421     }
422 }
423 
424 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
425                              uint64_t value)
426 {
427     ARMCPU *cpu = env_archcpu(env);
428 
429     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
430         && !extended_addresses_enabled(env)) {
431         /*
432          * For VMSA (when not using the LPAE long descriptor page table
433          * format) this register includes the ASID, so do a TLB flush.
434          * For PMSA it is purely a process ID and no action is needed.
435          */
436         tlb_flush(CPU(cpu));
437     }
438     raw_write(env, ri, value);
439 }
440 
441 static int alle1_tlbmask(CPUARMState *env)
442 {
443     /*
444      * Note that the 'ALL' scope must invalidate both stage 1 and
445      * stage 2 translations, whereas most other scopes only invalidate
446      * stage 1 translations.
447      */
448     return (ARMMMUIdxBit_E10_1 |
449             ARMMMUIdxBit_E10_1_PAN |
450             ARMMMUIdxBit_E10_0 |
451             ARMMMUIdxBit_Stage2 |
452             ARMMMUIdxBit_Stage2_S);
453 }
454 
455 
456 /* IS variants of TLB operations must affect all cores */
457 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
458                              uint64_t value)
459 {
460     CPUState *cs = env_cpu(env);
461 
462     tlb_flush_all_cpus_synced(cs);
463 }
464 
465 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
466                              uint64_t value)
467 {
468     CPUState *cs = env_cpu(env);
469 
470     tlb_flush_all_cpus_synced(cs);
471 }
472 
473 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
474                              uint64_t value)
475 {
476     CPUState *cs = env_cpu(env);
477 
478     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
479 }
480 
481 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
482                              uint64_t value)
483 {
484     CPUState *cs = env_cpu(env);
485 
486     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
487 }
488 
489 /*
490  * Non-IS variants of TLB operations are upgraded to
491  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
492  * force broadcast of these operations.
493  */
494 static bool tlb_force_broadcast(CPUARMState *env)
495 {
496     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
497 }
498 
499 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                           uint64_t value)
501 {
502     /* Invalidate all (TLBIALL) */
503     CPUState *cs = env_cpu(env);
504 
505     if (tlb_force_broadcast(env)) {
506         tlb_flush_all_cpus_synced(cs);
507     } else {
508         tlb_flush(cs);
509     }
510 }
511 
512 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
513                           uint64_t value)
514 {
515     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
516     CPUState *cs = env_cpu(env);
517 
518     value &= TARGET_PAGE_MASK;
519     if (tlb_force_broadcast(env)) {
520         tlb_flush_page_all_cpus_synced(cs, value);
521     } else {
522         tlb_flush_page(cs, value);
523     }
524 }
525 
526 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
527                            uint64_t value)
528 {
529     /* Invalidate by ASID (TLBIASID) */
530     CPUState *cs = env_cpu(env);
531 
532     if (tlb_force_broadcast(env)) {
533         tlb_flush_all_cpus_synced(cs);
534     } else {
535         tlb_flush(cs);
536     }
537 }
538 
539 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                            uint64_t value)
541 {
542     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
543     CPUState *cs = env_cpu(env);
544 
545     value &= TARGET_PAGE_MASK;
546     if (tlb_force_broadcast(env)) {
547         tlb_flush_page_all_cpus_synced(cs, value);
548     } else {
549         tlb_flush_page(cs, value);
550     }
551 }
552 
553 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
554                                uint64_t value)
555 {
556     CPUState *cs = env_cpu(env);
557 
558     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
559 }
560 
561 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
562                                   uint64_t value)
563 {
564     CPUState *cs = env_cpu(env);
565 
566     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
567 }
568 
569 
570 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
571                               uint64_t value)
572 {
573     CPUState *cs = env_cpu(env);
574 
575     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
576 }
577 
578 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
579                                  uint64_t value)
580 {
581     CPUState *cs = env_cpu(env);
582 
583     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
584 }
585 
586 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
587                               uint64_t value)
588 {
589     CPUState *cs = env_cpu(env);
590     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
591 
592     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
593 }
594 
595 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
596                                  uint64_t value)
597 {
598     CPUState *cs = env_cpu(env);
599     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
600 
601     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
602                                              ARMMMUIdxBit_E2);
603 }
604 
605 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
606                                 uint64_t value)
607 {
608     CPUState *cs = env_cpu(env);
609     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
610 
611     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
612 }
613 
614 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
615                                 uint64_t value)
616 {
617     CPUState *cs = env_cpu(env);
618     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
619 
620     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
621 }
622 
623 static const ARMCPRegInfo cp_reginfo[] = {
624     /*
625      * Define the secure and non-secure FCSE identifier CP registers
626      * separately because there is no secure bank in V8 (no _EL3).  This allows
627      * the secure register to be properly reset and migrated. There is also no
628      * v8 EL1 version of the register so the non-secure instance stands alone.
629      */
630     { .name = "FCSEIDR",
631       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
632       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
633       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
634       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
635     { .name = "FCSEIDR_S",
636       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
637       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
638       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
639       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
640     /*
641      * Define the secure and non-secure context identifier CP registers
642      * separately because there is no secure bank in V8 (no _EL3).  This allows
643      * the secure register to be properly reset and migrated.  In the
644      * non-secure case, the 32-bit register will have reset and migration
645      * disabled during registration as it is handled by the 64-bit instance.
646      */
647     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
648       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
649       .access = PL1_RW, .accessfn = access_tvm_trvm,
650       .fgt = FGT_CONTEXTIDR_EL1,
651       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
652       .secure = ARM_CP_SECSTATE_NS,
653       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
654       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
655     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
656       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
657       .access = PL1_RW, .accessfn = access_tvm_trvm,
658       .secure = ARM_CP_SECSTATE_S,
659       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
660       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
661 };
662 
663 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
664     /*
665      * NB: Some of these registers exist in v8 but with more precise
666      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
667      */
668     /* MMU Domain access control / MPU write buffer control */
669     { .name = "DACR",
670       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
671       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
672       .writefn = dacr_write, .raw_writefn = raw_write,
673       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
674                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
675     /*
676      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
677      * For v6 and v5, these mappings are overly broad.
678      */
679     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
680       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
682       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
683     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
684       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
685     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
686       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
687     /* Cache maintenance ops; some of this space may be overridden later. */
688     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
689       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
690       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
691 };
692 
693 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
694     /*
695      * Not all pre-v6 cores implemented this WFI, so this is slightly
696      * over-broad.
697      */
698     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
699       .access = PL1_W, .type = ARM_CP_WFI },
700 };
701 
702 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
703     /*
704      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
705      * is UNPREDICTABLE; we choose to NOP as most implementations do).
706      */
707     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
708       .access = PL1_W, .type = ARM_CP_WFI },
709     /*
710      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
711      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
712      * OMAPCP will override this space.
713      */
714     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
715       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
716       .resetvalue = 0 },
717     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
718       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
719       .resetvalue = 0 },
720     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
721     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
722       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
723       .resetvalue = 0 },
724     /*
725      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
726      * implementing it as RAZ means the "debug architecture version" bits
727      * will read as a reserved value, which should cause Linux to not try
728      * to use the debug hardware.
729      */
730     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
731       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
732     /*
733      * MMU TLB control. Note that the wildcarding means we cover not just
734      * the unified TLB ops but also the dside/iside/inner-shareable variants.
735      */
736     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
737       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
738       .type = ARM_CP_NO_RAW },
739     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
740       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
741       .type = ARM_CP_NO_RAW },
742     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
743       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
744       .type = ARM_CP_NO_RAW },
745     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
746       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
747       .type = ARM_CP_NO_RAW },
748     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
749       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
750     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
751       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
752 };
753 
754 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
755                         uint64_t value)
756 {
757     uint32_t mask = 0;
758 
759     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
760     if (!arm_feature(env, ARM_FEATURE_V8)) {
761         /*
762          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
763          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
764          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
765          */
766         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
767             /* VFP coprocessor: cp10 & cp11 [23:20] */
768             mask |= R_CPACR_ASEDIS_MASK |
769                     R_CPACR_D32DIS_MASK |
770                     R_CPACR_CP11_MASK |
771                     R_CPACR_CP10_MASK;
772 
773             if (!arm_feature(env, ARM_FEATURE_NEON)) {
774                 /* ASEDIS [31] bit is RAO/WI */
775                 value |= R_CPACR_ASEDIS_MASK;
776             }
777 
778             /*
779              * VFPv3 and upwards with NEON implement 32 double precision
780              * registers (D0-D31).
781              */
782             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
783                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
784                 value |= R_CPACR_D32DIS_MASK;
785             }
786         }
787         value &= mask;
788     }
789 
790     /*
791      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
792      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
793      */
794     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
795         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
796         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
797         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
798     }
799 
800     env->cp15.cpacr_el1 = value;
801 }
802 
803 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
804 {
805     /*
806      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
807      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
808      */
809     uint64_t value = env->cp15.cpacr_el1;
810 
811     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
812         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
813         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
814     }
815     return value;
816 }
817 
818 
819 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
820 {
821     /*
822      * Call cpacr_write() so that we reset with the correct RAO bits set
823      * for our CPU features.
824      */
825     cpacr_write(env, ri, 0);
826 }
827 
828 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
829                                    bool isread)
830 {
831     if (arm_feature(env, ARM_FEATURE_V8)) {
832         /* Check if CPACR accesses are to be trapped to EL2 */
833         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
834             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
835             return CP_ACCESS_TRAP_EL2;
836         /* Check if CPACR accesses are to be trapped to EL3 */
837         } else if (arm_current_el(env) < 3 &&
838                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
839             return CP_ACCESS_TRAP_EL3;
840         }
841     }
842 
843     return CP_ACCESS_OK;
844 }
845 
846 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
847                                   bool isread)
848 {
849     /* Check if CPTR accesses are set to trap to EL3 */
850     if (arm_current_el(env) == 2 &&
851         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
852         return CP_ACCESS_TRAP_EL3;
853     }
854 
855     return CP_ACCESS_OK;
856 }
857 
858 static const ARMCPRegInfo v6_cp_reginfo[] = {
859     /* prefetch by MVA in v6, NOP in v7 */
860     { .name = "MVA_prefetch",
861       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
862       .access = PL1_W, .type = ARM_CP_NOP },
863     /*
864      * We need to break the TB after ISB to execute self-modifying code
865      * correctly and also to take any pending interrupts immediately.
866      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
867      */
868     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
869       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
870     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
871       .access = PL0_W, .type = ARM_CP_NOP },
872     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
873       .access = PL0_W, .type = ARM_CP_NOP },
874     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
875       .access = PL1_RW, .accessfn = access_tvm_trvm,
876       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
877                              offsetof(CPUARMState, cp15.ifar_ns) },
878       .resetvalue = 0, },
879     /*
880      * Watchpoint Fault Address Register : should actually only be present
881      * for 1136, 1176, 11MPCore.
882      */
883     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
884       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
885     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
886       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
887       .fgt = FGT_CPACR_EL1,
888       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
889       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
890       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
891 };
892 
893 typedef struct pm_event {
894     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
895     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
896     bool (*supported)(CPUARMState *);
897     /*
898      * Retrieve the current count of the underlying event. The programmed
899      * counters hold a difference from the return value from this function
900      */
901     uint64_t (*get_count)(CPUARMState *);
902     /*
903      * Return how many nanoseconds it will take (at a minimum) for count events
904      * to occur. A negative value indicates the counter will never overflow, or
905      * that the counter has otherwise arranged for the overflow bit to be set
906      * and the PMU interrupt to be raised on overflow.
907      */
908     int64_t (*ns_per_count)(uint64_t);
909 } pm_event;
910 
911 static bool event_always_supported(CPUARMState *env)
912 {
913     return true;
914 }
915 
916 static uint64_t swinc_get_count(CPUARMState *env)
917 {
918     /*
919      * SW_INCR events are written directly to the pmevcntr's by writes to
920      * PMSWINC, so there is no underlying count maintained by the PMU itself
921      */
922     return 0;
923 }
924 
925 static int64_t swinc_ns_per(uint64_t ignored)
926 {
927     return -1;
928 }
929 
930 /*
931  * Return the underlying cycle count for the PMU cycle counters. If we're in
932  * usermode, simply return 0.
933  */
934 static uint64_t cycles_get_count(CPUARMState *env)
935 {
936 #ifndef CONFIG_USER_ONLY
937     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
938                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
939 #else
940     return cpu_get_host_ticks();
941 #endif
942 }
943 
944 #ifndef CONFIG_USER_ONLY
945 static int64_t cycles_ns_per(uint64_t cycles)
946 {
947     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
948 }
949 
950 static bool instructions_supported(CPUARMState *env)
951 {
952     /* Precise instruction counting */
953     return icount_enabled() == ICOUNT_PRECISE;
954 }
955 
956 static uint64_t instructions_get_count(CPUARMState *env)
957 {
958     assert(icount_enabled() == ICOUNT_PRECISE);
959     return (uint64_t)icount_get_raw();
960 }
961 
962 static int64_t instructions_ns_per(uint64_t icount)
963 {
964     assert(icount_enabled() == ICOUNT_PRECISE);
965     return icount_to_ns((int64_t)icount);
966 }
967 #endif
968 
969 static bool pmuv3p1_events_supported(CPUARMState *env)
970 {
971     /* For events which are supported in any v8.1 PMU */
972     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
973 }
974 
975 static bool pmuv3p4_events_supported(CPUARMState *env)
976 {
977     /* For events which are supported in any v8.1 PMU */
978     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
979 }
980 
981 static uint64_t zero_event_get_count(CPUARMState *env)
982 {
983     /* For events which on QEMU never fire, so their count is always zero */
984     return 0;
985 }
986 
987 static int64_t zero_event_ns_per(uint64_t cycles)
988 {
989     /* An event which never fires can never overflow */
990     return -1;
991 }
992 
993 static const pm_event pm_events[] = {
994     { .number = 0x000, /* SW_INCR */
995       .supported = event_always_supported,
996       .get_count = swinc_get_count,
997       .ns_per_count = swinc_ns_per,
998     },
999 #ifndef CONFIG_USER_ONLY
1000     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1001       .supported = instructions_supported,
1002       .get_count = instructions_get_count,
1003       .ns_per_count = instructions_ns_per,
1004     },
1005     { .number = 0x011, /* CPU_CYCLES, Cycle */
1006       .supported = event_always_supported,
1007       .get_count = cycles_get_count,
1008       .ns_per_count = cycles_ns_per,
1009     },
1010 #endif
1011     { .number = 0x023, /* STALL_FRONTEND */
1012       .supported = pmuv3p1_events_supported,
1013       .get_count = zero_event_get_count,
1014       .ns_per_count = zero_event_ns_per,
1015     },
1016     { .number = 0x024, /* STALL_BACKEND */
1017       .supported = pmuv3p1_events_supported,
1018       .get_count = zero_event_get_count,
1019       .ns_per_count = zero_event_ns_per,
1020     },
1021     { .number = 0x03c, /* STALL */
1022       .supported = pmuv3p4_events_supported,
1023       .get_count = zero_event_get_count,
1024       .ns_per_count = zero_event_ns_per,
1025     },
1026 };
1027 
1028 /*
1029  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1030  * events (i.e. the statistical profiling extension), this implementation
1031  * should first be updated to something sparse instead of the current
1032  * supported_event_map[] array.
1033  */
1034 #define MAX_EVENT_ID 0x3c
1035 #define UNSUPPORTED_EVENT UINT16_MAX
1036 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1037 
1038 /*
1039  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1040  * of ARM event numbers to indices in our pm_events array.
1041  *
1042  * Note: Events in the 0x40XX range are not currently supported.
1043  */
1044 void pmu_init(ARMCPU *cpu)
1045 {
1046     unsigned int i;
1047 
1048     /*
1049      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1050      * events to them
1051      */
1052     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1053         supported_event_map[i] = UNSUPPORTED_EVENT;
1054     }
1055     cpu->pmceid0 = 0;
1056     cpu->pmceid1 = 0;
1057 
1058     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1059         const pm_event *cnt = &pm_events[i];
1060         assert(cnt->number <= MAX_EVENT_ID);
1061         /* We do not currently support events in the 0x40xx range */
1062         assert(cnt->number <= 0x3f);
1063 
1064         if (cnt->supported(&cpu->env)) {
1065             supported_event_map[cnt->number] = i;
1066             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1067             if (cnt->number & 0x20) {
1068                 cpu->pmceid1 |= event_mask;
1069             } else {
1070                 cpu->pmceid0 |= event_mask;
1071             }
1072         }
1073     }
1074 }
1075 
1076 /*
1077  * Check at runtime whether a PMU event is supported for the current machine
1078  */
1079 static bool event_supported(uint16_t number)
1080 {
1081     if (number > MAX_EVENT_ID) {
1082         return false;
1083     }
1084     return supported_event_map[number] != UNSUPPORTED_EVENT;
1085 }
1086 
1087 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1088                                    bool isread)
1089 {
1090     /*
1091      * Performance monitor registers user accessibility is controlled
1092      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1093      * trapping to EL2 or EL3 for other accesses.
1094      */
1095     int el = arm_current_el(env);
1096     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1097 
1098     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1099         return CP_ACCESS_TRAP;
1100     }
1101     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1102         return CP_ACCESS_TRAP_EL2;
1103     }
1104     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1105         return CP_ACCESS_TRAP_EL3;
1106     }
1107 
1108     return CP_ACCESS_OK;
1109 }
1110 
1111 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1112                                            const ARMCPRegInfo *ri,
1113                                            bool isread)
1114 {
1115     /* ER: event counter read trap control */
1116     if (arm_feature(env, ARM_FEATURE_V8)
1117         && arm_current_el(env) == 0
1118         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1119         && isread) {
1120         return CP_ACCESS_OK;
1121     }
1122 
1123     return pmreg_access(env, ri, isread);
1124 }
1125 
1126 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1127                                          const ARMCPRegInfo *ri,
1128                                          bool isread)
1129 {
1130     /* SW: software increment write trap control */
1131     if (arm_feature(env, ARM_FEATURE_V8)
1132         && arm_current_el(env) == 0
1133         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1134         && !isread) {
1135         return CP_ACCESS_OK;
1136     }
1137 
1138     return pmreg_access(env, ri, isread);
1139 }
1140 
1141 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1142                                         const ARMCPRegInfo *ri,
1143                                         bool isread)
1144 {
1145     /* ER: event counter read trap control */
1146     if (arm_feature(env, ARM_FEATURE_V8)
1147         && arm_current_el(env) == 0
1148         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1149         return CP_ACCESS_OK;
1150     }
1151 
1152     return pmreg_access(env, ri, isread);
1153 }
1154 
1155 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1156                                          const ARMCPRegInfo *ri,
1157                                          bool isread)
1158 {
1159     /* CR: cycle counter read trap control */
1160     if (arm_feature(env, ARM_FEATURE_V8)
1161         && arm_current_el(env) == 0
1162         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1163         && isread) {
1164         return CP_ACCESS_OK;
1165     }
1166 
1167     return pmreg_access(env, ri, isread);
1168 }
1169 
1170 /*
1171  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1172  * We use these to decide whether we need to wrap a write to MDCR_EL2
1173  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1174  */
1175 #define MDCR_EL2_PMU_ENABLE_BITS \
1176     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1177 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1178 
1179 /*
1180  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1181  * the current EL, security state, and register configuration.
1182  */
1183 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1184 {
1185     uint64_t filter;
1186     bool e, p, u, nsk, nsu, nsh, m;
1187     bool enabled, prohibited = false, filtered;
1188     bool secure = arm_is_secure(env);
1189     int el = arm_current_el(env);
1190     uint64_t mdcr_el2;
1191     uint8_t hpmn;
1192 
1193     /*
1194      * We might be called for M-profile cores where MDCR_EL2 doesn't
1195      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
1196      * must be before we read that value.
1197      */
1198     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1199         return false;
1200     }
1201 
1202     mdcr_el2 = arm_mdcr_el2_eff(env);
1203     hpmn = mdcr_el2 & MDCR_HPMN;
1204 
1205     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1206             (counter < hpmn || counter == 31)) {
1207         e = env->cp15.c9_pmcr & PMCRE;
1208     } else {
1209         e = mdcr_el2 & MDCR_HPME;
1210     }
1211     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1212 
1213     /* Is event counting prohibited? */
1214     if (el == 2 && (counter < hpmn || counter == 31)) {
1215         prohibited = mdcr_el2 & MDCR_HPMD;
1216     }
1217     if (secure) {
1218         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1219     }
1220 
1221     if (counter == 31) {
1222         /*
1223          * The cycle counter defaults to running. PMCR.DP says "disable
1224          * the cycle counter when event counting is prohibited".
1225          * Some MDCR bits disable the cycle counter specifically.
1226          */
1227         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1228         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1229             if (secure) {
1230                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1231             }
1232             if (el == 2) {
1233                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1234             }
1235         }
1236     }
1237 
1238     if (counter == 31) {
1239         filter = env->cp15.pmccfiltr_el0;
1240     } else {
1241         filter = env->cp15.c14_pmevtyper[counter];
1242     }
1243 
1244     p   = filter & PMXEVTYPER_P;
1245     u   = filter & PMXEVTYPER_U;
1246     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1247     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1248     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1249     m   = arm_el_is_aa64(env, 1) &&
1250               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1251 
1252     if (el == 0) {
1253         filtered = secure ? u : u != nsu;
1254     } else if (el == 1) {
1255         filtered = secure ? p : p != nsk;
1256     } else if (el == 2) {
1257         filtered = !nsh;
1258     } else { /* EL3 */
1259         filtered = m != p;
1260     }
1261 
1262     if (counter != 31) {
1263         /*
1264          * If not checking PMCCNTR, ensure the counter is setup to an event we
1265          * support
1266          */
1267         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1268         if (!event_supported(event)) {
1269             return false;
1270         }
1271     }
1272 
1273     return enabled && !prohibited && !filtered;
1274 }
1275 
1276 static void pmu_update_irq(CPUARMState *env)
1277 {
1278     ARMCPU *cpu = env_archcpu(env);
1279     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1280             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1281 }
1282 
1283 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1284 {
1285     /*
1286      * Return true if the clock divider is enabled and the cycle counter
1287      * is supposed to tick only once every 64 clock cycles. This is
1288      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1289      * (64-bit) cycle counter PMCR.D has no effect.
1290      */
1291     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1292 }
1293 
1294 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1295 {
1296     /* Return true if the specified event counter is configured to be 64 bit */
1297 
1298     /* This isn't intended to be used with the cycle counter */
1299     assert(counter < 31);
1300 
1301     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1302         return false;
1303     }
1304 
1305     if (arm_feature(env, ARM_FEATURE_EL2)) {
1306         /*
1307          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1308          * current security state, so we don't use arm_mdcr_el2_eff() here.
1309          */
1310         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1311         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1312 
1313         if (counter >= hpmn) {
1314             return hlp;
1315         }
1316     }
1317     return env->cp15.c9_pmcr & PMCRLP;
1318 }
1319 
1320 /*
1321  * Ensure c15_ccnt is the guest-visible count so that operations such as
1322  * enabling/disabling the counter or filtering, modifying the count itself,
1323  * etc. can be done logically. This is essentially a no-op if the counter is
1324  * not enabled at the time of the call.
1325  */
1326 static void pmccntr_op_start(CPUARMState *env)
1327 {
1328     uint64_t cycles = cycles_get_count(env);
1329 
1330     if (pmu_counter_enabled(env, 31)) {
1331         uint64_t eff_cycles = cycles;
1332         if (pmccntr_clockdiv_enabled(env)) {
1333             eff_cycles /= 64;
1334         }
1335 
1336         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1337 
1338         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1339                                  1ull << 63 : 1ull << 31;
1340         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1341             env->cp15.c9_pmovsr |= (1ULL << 31);
1342             pmu_update_irq(env);
1343         }
1344 
1345         env->cp15.c15_ccnt = new_pmccntr;
1346     }
1347     env->cp15.c15_ccnt_delta = cycles;
1348 }
1349 
1350 /*
1351  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1352  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1353  * pmccntr_op_start.
1354  */
1355 static void pmccntr_op_finish(CPUARMState *env)
1356 {
1357     if (pmu_counter_enabled(env, 31)) {
1358 #ifndef CONFIG_USER_ONLY
1359         /* Calculate when the counter will next overflow */
1360         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1361         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1362             remaining_cycles = (uint32_t)remaining_cycles;
1363         }
1364         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1365 
1366         if (overflow_in > 0) {
1367             int64_t overflow_at;
1368 
1369             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1370                                  overflow_in, &overflow_at)) {
1371                 ARMCPU *cpu = env_archcpu(env);
1372                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1373             }
1374         }
1375 #endif
1376 
1377         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1378         if (pmccntr_clockdiv_enabled(env)) {
1379             prev_cycles /= 64;
1380         }
1381         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1382     }
1383 }
1384 
1385 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1386 {
1387 
1388     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1389     uint64_t count = 0;
1390     if (event_supported(event)) {
1391         uint16_t event_idx = supported_event_map[event];
1392         count = pm_events[event_idx].get_count(env);
1393     }
1394 
1395     if (pmu_counter_enabled(env, counter)) {
1396         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1397         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1398             1ULL << 63 : 1ULL << 31;
1399 
1400         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1401             env->cp15.c9_pmovsr |= (1 << counter);
1402             pmu_update_irq(env);
1403         }
1404         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1405     }
1406     env->cp15.c14_pmevcntr_delta[counter] = count;
1407 }
1408 
1409 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1410 {
1411     if (pmu_counter_enabled(env, counter)) {
1412 #ifndef CONFIG_USER_ONLY
1413         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1414         uint16_t event_idx = supported_event_map[event];
1415         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1416         int64_t overflow_in;
1417 
1418         if (!pmevcntr_is_64_bit(env, counter)) {
1419             delta = (uint32_t)delta;
1420         }
1421         overflow_in = pm_events[event_idx].ns_per_count(delta);
1422 
1423         if (overflow_in > 0) {
1424             int64_t overflow_at;
1425 
1426             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1427                                  overflow_in, &overflow_at)) {
1428                 ARMCPU *cpu = env_archcpu(env);
1429                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1430             }
1431         }
1432 #endif
1433 
1434         env->cp15.c14_pmevcntr_delta[counter] -=
1435             env->cp15.c14_pmevcntr[counter];
1436     }
1437 }
1438 
1439 void pmu_op_start(CPUARMState *env)
1440 {
1441     unsigned int i;
1442     pmccntr_op_start(env);
1443     for (i = 0; i < pmu_num_counters(env); i++) {
1444         pmevcntr_op_start(env, i);
1445     }
1446 }
1447 
1448 void pmu_op_finish(CPUARMState *env)
1449 {
1450     unsigned int i;
1451     pmccntr_op_finish(env);
1452     for (i = 0; i < pmu_num_counters(env); i++) {
1453         pmevcntr_op_finish(env, i);
1454     }
1455 }
1456 
1457 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1458 {
1459     pmu_op_start(&cpu->env);
1460 }
1461 
1462 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1463 {
1464     pmu_op_finish(&cpu->env);
1465 }
1466 
1467 void arm_pmu_timer_cb(void *opaque)
1468 {
1469     ARMCPU *cpu = opaque;
1470 
1471     /*
1472      * Update all the counter values based on the current underlying counts,
1473      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1474      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1475      * counter may expire.
1476      */
1477     pmu_op_start(&cpu->env);
1478     pmu_op_finish(&cpu->env);
1479 }
1480 
1481 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1482                        uint64_t value)
1483 {
1484     pmu_op_start(env);
1485 
1486     if (value & PMCRC) {
1487         /* The counter has been reset */
1488         env->cp15.c15_ccnt = 0;
1489     }
1490 
1491     if (value & PMCRP) {
1492         unsigned int i;
1493         for (i = 0; i < pmu_num_counters(env); i++) {
1494             env->cp15.c14_pmevcntr[i] = 0;
1495         }
1496     }
1497 
1498     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1499     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1500 
1501     pmu_op_finish(env);
1502 }
1503 
1504 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1505 {
1506     uint64_t pmcr = env->cp15.c9_pmcr;
1507 
1508     /*
1509      * If EL2 is implemented and enabled for the current security state, reads
1510      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1511      */
1512     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1513         pmcr &= ~PMCRN_MASK;
1514         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1515     }
1516 
1517     return pmcr;
1518 }
1519 
1520 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521                           uint64_t value)
1522 {
1523     unsigned int i;
1524     uint64_t overflow_mask, new_pmswinc;
1525 
1526     for (i = 0; i < pmu_num_counters(env); i++) {
1527         /* Increment a counter's count iff: */
1528         if ((value & (1 << i)) && /* counter's bit is set */
1529                 /* counter is enabled and not filtered */
1530                 pmu_counter_enabled(env, i) &&
1531                 /* counter is SW_INCR */
1532                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1533             pmevcntr_op_start(env, i);
1534 
1535             /*
1536              * Detect if this write causes an overflow since we can't predict
1537              * PMSWINC overflows like we can for other events
1538              */
1539             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1540 
1541             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1542                 1ULL << 63 : 1ULL << 31;
1543 
1544             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1545                 env->cp15.c9_pmovsr |= (1 << i);
1546                 pmu_update_irq(env);
1547             }
1548 
1549             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1550 
1551             pmevcntr_op_finish(env, i);
1552         }
1553     }
1554 }
1555 
1556 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1557 {
1558     uint64_t ret;
1559     pmccntr_op_start(env);
1560     ret = env->cp15.c15_ccnt;
1561     pmccntr_op_finish(env);
1562     return ret;
1563 }
1564 
1565 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1566                          uint64_t value)
1567 {
1568     /*
1569      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1570      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1571      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1572      * accessed.
1573      */
1574     env->cp15.c9_pmselr = value & 0x1f;
1575 }
1576 
1577 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1578                         uint64_t value)
1579 {
1580     pmccntr_op_start(env);
1581     env->cp15.c15_ccnt = value;
1582     pmccntr_op_finish(env);
1583 }
1584 
1585 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1586                             uint64_t value)
1587 {
1588     uint64_t cur_val = pmccntr_read(env, NULL);
1589 
1590     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1591 }
1592 
1593 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594                             uint64_t value)
1595 {
1596     pmccntr_op_start(env);
1597     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1598     pmccntr_op_finish(env);
1599 }
1600 
1601 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1602                             uint64_t value)
1603 {
1604     pmccntr_op_start(env);
1605     /* M is not accessible from AArch32 */
1606     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1607         (value & PMCCFILTR);
1608     pmccntr_op_finish(env);
1609 }
1610 
1611 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1612 {
1613     /* M is not visible in AArch32 */
1614     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1615 }
1616 
1617 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618                             uint64_t value)
1619 {
1620     pmu_op_start(env);
1621     value &= pmu_counter_mask(env);
1622     env->cp15.c9_pmcnten |= value;
1623     pmu_op_finish(env);
1624 }
1625 
1626 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627                              uint64_t value)
1628 {
1629     pmu_op_start(env);
1630     value &= pmu_counter_mask(env);
1631     env->cp15.c9_pmcnten &= ~value;
1632     pmu_op_finish(env);
1633 }
1634 
1635 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                          uint64_t value)
1637 {
1638     value &= pmu_counter_mask(env);
1639     env->cp15.c9_pmovsr &= ~value;
1640     pmu_update_irq(env);
1641 }
1642 
1643 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1644                          uint64_t value)
1645 {
1646     value &= pmu_counter_mask(env);
1647     env->cp15.c9_pmovsr |= value;
1648     pmu_update_irq(env);
1649 }
1650 
1651 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1652                              uint64_t value, const uint8_t counter)
1653 {
1654     if (counter == 31) {
1655         pmccfiltr_write(env, ri, value);
1656     } else if (counter < pmu_num_counters(env)) {
1657         pmevcntr_op_start(env, counter);
1658 
1659         /*
1660          * If this counter's event type is changing, store the current
1661          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1662          * pmevcntr_op_finish has the correct baseline when it converts back to
1663          * a delta.
1664          */
1665         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1666             PMXEVTYPER_EVTCOUNT;
1667         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1668         if (old_event != new_event) {
1669             uint64_t count = 0;
1670             if (event_supported(new_event)) {
1671                 uint16_t event_idx = supported_event_map[new_event];
1672                 count = pm_events[event_idx].get_count(env);
1673             }
1674             env->cp15.c14_pmevcntr_delta[counter] = count;
1675         }
1676 
1677         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1678         pmevcntr_op_finish(env, counter);
1679     }
1680     /*
1681      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1682      * PMSELR value is equal to or greater than the number of implemented
1683      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1684      */
1685 }
1686 
1687 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1688                                const uint8_t counter)
1689 {
1690     if (counter == 31) {
1691         return env->cp15.pmccfiltr_el0;
1692     } else if (counter < pmu_num_counters(env)) {
1693         return env->cp15.c14_pmevtyper[counter];
1694     } else {
1695       /*
1696        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1697        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1698        */
1699         return 0;
1700     }
1701 }
1702 
1703 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1704                               uint64_t value)
1705 {
1706     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1707     pmevtyper_write(env, ri, value, counter);
1708 }
1709 
1710 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1711                                uint64_t value)
1712 {
1713     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1714     env->cp15.c14_pmevtyper[counter] = value;
1715 
1716     /*
1717      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1718      * pmu_op_finish calls when loading saved state for a migration. Because
1719      * we're potentially updating the type of event here, the value written to
1720      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1721      * different counter type. Therefore, we need to set this value to the
1722      * current count for the counter type we're writing so that pmu_op_finish
1723      * has the correct count for its calculation.
1724      */
1725     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1726     if (event_supported(event)) {
1727         uint16_t event_idx = supported_event_map[event];
1728         env->cp15.c14_pmevcntr_delta[counter] =
1729             pm_events[event_idx].get_count(env);
1730     }
1731 }
1732 
1733 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1734 {
1735     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1736     return pmevtyper_read(env, ri, counter);
1737 }
1738 
1739 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1740                              uint64_t value)
1741 {
1742     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1743 }
1744 
1745 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1746 {
1747     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1748 }
1749 
1750 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1751                              uint64_t value, uint8_t counter)
1752 {
1753     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1754         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1755         value &= MAKE_64BIT_MASK(0, 32);
1756     }
1757     if (counter < pmu_num_counters(env)) {
1758         pmevcntr_op_start(env, counter);
1759         env->cp15.c14_pmevcntr[counter] = value;
1760         pmevcntr_op_finish(env, counter);
1761     }
1762     /*
1763      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1764      * are CONSTRAINED UNPREDICTABLE.
1765      */
1766 }
1767 
1768 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1769                               uint8_t counter)
1770 {
1771     if (counter < pmu_num_counters(env)) {
1772         uint64_t ret;
1773         pmevcntr_op_start(env, counter);
1774         ret = env->cp15.c14_pmevcntr[counter];
1775         pmevcntr_op_finish(env, counter);
1776         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1777             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1778             ret &= MAKE_64BIT_MASK(0, 32);
1779         }
1780         return ret;
1781     } else {
1782       /*
1783        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1784        * are CONSTRAINED UNPREDICTABLE.
1785        */
1786         return 0;
1787     }
1788 }
1789 
1790 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1791                              uint64_t value)
1792 {
1793     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1794     pmevcntr_write(env, ri, value, counter);
1795 }
1796 
1797 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1798 {
1799     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1800     return pmevcntr_read(env, ri, counter);
1801 }
1802 
1803 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1804                              uint64_t value)
1805 {
1806     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1807     assert(counter < pmu_num_counters(env));
1808     env->cp15.c14_pmevcntr[counter] = value;
1809     pmevcntr_write(env, ri, value, counter);
1810 }
1811 
1812 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1813 {
1814     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1815     assert(counter < pmu_num_counters(env));
1816     return env->cp15.c14_pmevcntr[counter];
1817 }
1818 
1819 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1820                              uint64_t value)
1821 {
1822     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1823 }
1824 
1825 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1826 {
1827     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1828 }
1829 
1830 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1831                             uint64_t value)
1832 {
1833     if (arm_feature(env, ARM_FEATURE_V8)) {
1834         env->cp15.c9_pmuserenr = value & 0xf;
1835     } else {
1836         env->cp15.c9_pmuserenr = value & 1;
1837     }
1838 }
1839 
1840 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1841                              uint64_t value)
1842 {
1843     /* We have no event counters so only the C bit can be changed */
1844     value &= pmu_counter_mask(env);
1845     env->cp15.c9_pminten |= value;
1846     pmu_update_irq(env);
1847 }
1848 
1849 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1850                              uint64_t value)
1851 {
1852     value &= pmu_counter_mask(env);
1853     env->cp15.c9_pminten &= ~value;
1854     pmu_update_irq(env);
1855 }
1856 
1857 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1858                        uint64_t value)
1859 {
1860     /*
1861      * Note that even though the AArch64 view of this register has bits
1862      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1863      * architectural requirements for bits which are RES0 only in some
1864      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1865      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1866      */
1867     raw_write(env, ri, value & ~0x1FULL);
1868 }
1869 
1870 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1871 {
1872     /* Begin with base v8.0 state.  */
1873     uint64_t valid_mask = 0x3fff;
1874     ARMCPU *cpu = env_archcpu(env);
1875     uint64_t changed;
1876 
1877     /*
1878      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1879      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1880      * Instead, choose the format based on the mode of EL3.
1881      */
1882     if (arm_el_is_aa64(env, 3)) {
1883         value |= SCR_FW | SCR_AW;      /* RES1 */
1884         valid_mask &= ~SCR_NET;        /* RES0 */
1885 
1886         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1887             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1888             value |= SCR_RW;           /* RAO/WI */
1889         }
1890         if (cpu_isar_feature(aa64_ras, cpu)) {
1891             valid_mask |= SCR_TERR;
1892         }
1893         if (cpu_isar_feature(aa64_lor, cpu)) {
1894             valid_mask |= SCR_TLOR;
1895         }
1896         if (cpu_isar_feature(aa64_pauth, cpu)) {
1897             valid_mask |= SCR_API | SCR_APK;
1898         }
1899         if (cpu_isar_feature(aa64_sel2, cpu)) {
1900             valid_mask |= SCR_EEL2;
1901         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1902             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1903             value |= SCR_NS;
1904         }
1905         if (cpu_isar_feature(aa64_mte, cpu)) {
1906             valid_mask |= SCR_ATA;
1907         }
1908         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1909             valid_mask |= SCR_ENSCXT;
1910         }
1911         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1912             valid_mask |= SCR_EASE | SCR_NMEA;
1913         }
1914         if (cpu_isar_feature(aa64_sme, cpu)) {
1915             valid_mask |= SCR_ENTP2;
1916         }
1917         if (cpu_isar_feature(aa64_hcx, cpu)) {
1918             valid_mask |= SCR_HXEN;
1919         }
1920         if (cpu_isar_feature(aa64_fgt, cpu)) {
1921             valid_mask |= SCR_FGTEN;
1922         }
1923         if (cpu_isar_feature(aa64_rme, cpu)) {
1924             valid_mask |= SCR_NSE | SCR_GPF;
1925         }
1926     } else {
1927         valid_mask &= ~(SCR_RW | SCR_ST);
1928         if (cpu_isar_feature(aa32_ras, cpu)) {
1929             valid_mask |= SCR_TERR;
1930         }
1931     }
1932 
1933     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1934         valid_mask &= ~SCR_HCE;
1935 
1936         /*
1937          * On ARMv7, SMD (or SCD as it is called in v7) is only
1938          * supported if EL2 exists. The bit is UNK/SBZP when
1939          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1940          * when EL2 is unavailable.
1941          * On ARMv8, this bit is always available.
1942          */
1943         if (arm_feature(env, ARM_FEATURE_V7) &&
1944             !arm_feature(env, ARM_FEATURE_V8)) {
1945             valid_mask &= ~SCR_SMD;
1946         }
1947     }
1948 
1949     /* Clear all-context RES0 bits.  */
1950     value &= valid_mask;
1951     changed = env->cp15.scr_el3 ^ value;
1952     env->cp15.scr_el3 = value;
1953 
1954     /*
1955      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1956      * we must invalidate all TLBs below EL3.
1957      */
1958     if (changed & (SCR_NS | SCR_NSE)) {
1959         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1960                                            ARMMMUIdxBit_E20_0 |
1961                                            ARMMMUIdxBit_E10_1 |
1962                                            ARMMMUIdxBit_E20_2 |
1963                                            ARMMMUIdxBit_E10_1_PAN |
1964                                            ARMMMUIdxBit_E20_2_PAN |
1965                                            ARMMMUIdxBit_E2));
1966     }
1967 }
1968 
1969 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1970 {
1971     /*
1972      * scr_write will set the RES1 bits on an AArch64-only CPU.
1973      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1974      */
1975     scr_write(env, ri, 0);
1976 }
1977 
1978 static CPAccessResult access_tid4(CPUARMState *env,
1979                                   const ARMCPRegInfo *ri,
1980                                   bool isread)
1981 {
1982     if (arm_current_el(env) == 1 &&
1983         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1984         return CP_ACCESS_TRAP_EL2;
1985     }
1986 
1987     return CP_ACCESS_OK;
1988 }
1989 
1990 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1991 {
1992     ARMCPU *cpu = env_archcpu(env);
1993 
1994     /*
1995      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1996      * bank
1997      */
1998     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1999                                         ri->secure & ARM_CP_SECSTATE_S);
2000 
2001     return cpu->ccsidr[index];
2002 }
2003 
2004 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2005                          uint64_t value)
2006 {
2007     raw_write(env, ri, value & 0xf);
2008 }
2009 
2010 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2011 {
2012     CPUState *cs = env_cpu(env);
2013     bool el1 = arm_current_el(env) == 1;
2014     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2015     uint64_t ret = 0;
2016 
2017     if (hcr_el2 & HCR_IMO) {
2018         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2019             ret |= CPSR_I;
2020         }
2021     } else {
2022         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2023             ret |= CPSR_I;
2024         }
2025     }
2026 
2027     if (hcr_el2 & HCR_FMO) {
2028         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2029             ret |= CPSR_F;
2030         }
2031     } else {
2032         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2033             ret |= CPSR_F;
2034         }
2035     }
2036 
2037     if (hcr_el2 & HCR_AMO) {
2038         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2039             ret |= CPSR_A;
2040         }
2041     }
2042 
2043     return ret;
2044 }
2045 
2046 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2047                                        bool isread)
2048 {
2049     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2050         return CP_ACCESS_TRAP_EL2;
2051     }
2052 
2053     return CP_ACCESS_OK;
2054 }
2055 
2056 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2057                                        bool isread)
2058 {
2059     if (arm_feature(env, ARM_FEATURE_V8)) {
2060         return access_aa64_tid1(env, ri, isread);
2061     }
2062 
2063     return CP_ACCESS_OK;
2064 }
2065 
2066 static const ARMCPRegInfo v7_cp_reginfo[] = {
2067     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2068     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2069       .access = PL1_W, .type = ARM_CP_NOP },
2070     /*
2071      * Performance monitors are implementation defined in v7,
2072      * but with an ARM recommended set of registers, which we
2073      * follow.
2074      *
2075      * Performance registers fall into three categories:
2076      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2077      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2078      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2079      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2080      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2081      */
2082     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2083       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2084       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2085       .writefn = pmcntenset_write,
2086       .accessfn = pmreg_access,
2087       .fgt = FGT_PMCNTEN,
2088       .raw_writefn = raw_write },
2089     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2090       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2091       .access = PL0_RW, .accessfn = pmreg_access,
2092       .fgt = FGT_PMCNTEN,
2093       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2094       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2095     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2096       .access = PL0_RW,
2097       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2098       .accessfn = pmreg_access,
2099       .fgt = FGT_PMCNTEN,
2100       .writefn = pmcntenclr_write,
2101       .type = ARM_CP_ALIAS | ARM_CP_IO },
2102     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2103       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2104       .access = PL0_RW, .accessfn = pmreg_access,
2105       .fgt = FGT_PMCNTEN,
2106       .type = ARM_CP_ALIAS | ARM_CP_IO,
2107       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2108       .writefn = pmcntenclr_write },
2109     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2110       .access = PL0_RW, .type = ARM_CP_IO,
2111       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2112       .accessfn = pmreg_access,
2113       .fgt = FGT_PMOVS,
2114       .writefn = pmovsr_write,
2115       .raw_writefn = raw_write },
2116     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2117       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2118       .access = PL0_RW, .accessfn = pmreg_access,
2119       .fgt = FGT_PMOVS,
2120       .type = ARM_CP_ALIAS | ARM_CP_IO,
2121       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2122       .writefn = pmovsr_write,
2123       .raw_writefn = raw_write },
2124     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2125       .access = PL0_W, .accessfn = pmreg_access_swinc,
2126       .fgt = FGT_PMSWINC_EL0,
2127       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2128       .writefn = pmswinc_write },
2129     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2130       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2131       .access = PL0_W, .accessfn = pmreg_access_swinc,
2132       .fgt = FGT_PMSWINC_EL0,
2133       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134       .writefn = pmswinc_write },
2135     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2136       .access = PL0_RW, .type = ARM_CP_ALIAS,
2137       .fgt = FGT_PMSELR_EL0,
2138       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2139       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2140       .raw_writefn = raw_write},
2141     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2142       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2143       .access = PL0_RW, .accessfn = pmreg_access_selr,
2144       .fgt = FGT_PMSELR_EL0,
2145       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2146       .writefn = pmselr_write, .raw_writefn = raw_write, },
2147     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2148       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2149       .fgt = FGT_PMCCNTR_EL0,
2150       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2151       .accessfn = pmreg_access_ccntr },
2152     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2153       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2154       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2155       .fgt = FGT_PMCCNTR_EL0,
2156       .type = ARM_CP_IO,
2157       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2158       .readfn = pmccntr_read, .writefn = pmccntr_write,
2159       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2160     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2161       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2162       .access = PL0_RW, .accessfn = pmreg_access,
2163       .fgt = FGT_PMCCFILTR_EL0,
2164       .type = ARM_CP_ALIAS | ARM_CP_IO,
2165       .resetvalue = 0, },
2166     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2167       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2168       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2169       .access = PL0_RW, .accessfn = pmreg_access,
2170       .fgt = FGT_PMCCFILTR_EL0,
2171       .type = ARM_CP_IO,
2172       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2173       .resetvalue = 0, },
2174     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2175       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2176       .accessfn = pmreg_access,
2177       .fgt = FGT_PMEVTYPERN_EL0,
2178       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2179     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2180       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2181       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2182       .accessfn = pmreg_access,
2183       .fgt = FGT_PMEVTYPERN_EL0,
2184       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2185     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2186       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2187       .accessfn = pmreg_access_xevcntr,
2188       .fgt = FGT_PMEVCNTRN_EL0,
2189       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2190     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2191       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2192       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2193       .accessfn = pmreg_access_xevcntr,
2194       .fgt = FGT_PMEVCNTRN_EL0,
2195       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2196     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2197       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2198       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2199       .resetvalue = 0,
2200       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2201     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2202       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2203       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2204       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2205       .resetvalue = 0,
2206       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2207     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2208       .access = PL1_RW, .accessfn = access_tpm,
2209       .fgt = FGT_PMINTEN,
2210       .type = ARM_CP_ALIAS | ARM_CP_IO,
2211       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2212       .resetvalue = 0,
2213       .writefn = pmintenset_write, .raw_writefn = raw_write },
2214     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2215       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2216       .access = PL1_RW, .accessfn = access_tpm,
2217       .fgt = FGT_PMINTEN,
2218       .type = ARM_CP_IO,
2219       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2220       .writefn = pmintenset_write, .raw_writefn = raw_write,
2221       .resetvalue = 0x0 },
2222     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2223       .access = PL1_RW, .accessfn = access_tpm,
2224       .fgt = FGT_PMINTEN,
2225       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2226       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2227       .writefn = pmintenclr_write, },
2228     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2229       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2230       .access = PL1_RW, .accessfn = access_tpm,
2231       .fgt = FGT_PMINTEN,
2232       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2233       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2234       .writefn = pmintenclr_write },
2235     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2236       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2237       .access = PL1_R,
2238       .accessfn = access_tid4,
2239       .fgt = FGT_CCSIDR_EL1,
2240       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2241     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2242       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2243       .access = PL1_RW,
2244       .accessfn = access_tid4,
2245       .fgt = FGT_CSSELR_EL1,
2246       .writefn = csselr_write, .resetvalue = 0,
2247       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2248                              offsetof(CPUARMState, cp15.csselr_ns) } },
2249     /*
2250      * Auxiliary ID register: this actually has an IMPDEF value but for now
2251      * just RAZ for all cores:
2252      */
2253     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2254       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2255       .access = PL1_R, .type = ARM_CP_CONST,
2256       .accessfn = access_aa64_tid1,
2257       .fgt = FGT_AIDR_EL1,
2258       .resetvalue = 0 },
2259     /*
2260      * Auxiliary fault status registers: these also are IMPDEF, and we
2261      * choose to RAZ/WI for all cores.
2262      */
2263     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2264       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2265       .access = PL1_RW, .accessfn = access_tvm_trvm,
2266       .fgt = FGT_AFSR0_EL1,
2267       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2268       .type = ARM_CP_CONST, .resetvalue = 0 },
2269     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2270       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2271       .access = PL1_RW, .accessfn = access_tvm_trvm,
2272       .fgt = FGT_AFSR1_EL1,
2273       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2274       .type = ARM_CP_CONST, .resetvalue = 0 },
2275     /*
2276      * MAIR can just read-as-written because we don't implement caches
2277      * and so don't need to care about memory attributes.
2278      */
2279     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2280       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2281       .access = PL1_RW, .accessfn = access_tvm_trvm,
2282       .fgt = FGT_MAIR_EL1,
2283       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2284       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2285       .resetvalue = 0 },
2286     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2287       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2288       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2289       .resetvalue = 0 },
2290     /*
2291      * For non-long-descriptor page tables these are PRRR and NMRR;
2292      * regardless they still act as reads-as-written for QEMU.
2293      */
2294      /*
2295       * MAIR0/1 are defined separately from their 64-bit counterpart which
2296       * allows them to assign the correct fieldoffset based on the endianness
2297       * handled in the field definitions.
2298       */
2299     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2300       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2301       .access = PL1_RW, .accessfn = access_tvm_trvm,
2302       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2303                              offsetof(CPUARMState, cp15.mair0_ns) },
2304       .resetfn = arm_cp_reset_ignore },
2305     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2306       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2307       .access = PL1_RW, .accessfn = access_tvm_trvm,
2308       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2309                              offsetof(CPUARMState, cp15.mair1_ns) },
2310       .resetfn = arm_cp_reset_ignore },
2311     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2312       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2313       .fgt = FGT_ISR_EL1,
2314       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2315     /* 32 bit ITLB invalidates */
2316     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2317       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2318       .writefn = tlbiall_write },
2319     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2320       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2321       .writefn = tlbimva_write },
2322     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2323       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2324       .writefn = tlbiasid_write },
2325     /* 32 bit DTLB invalidates */
2326     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2327       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2328       .writefn = tlbiall_write },
2329     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2330       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2331       .writefn = tlbimva_write },
2332     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2333       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2334       .writefn = tlbiasid_write },
2335     /* 32 bit TLB invalidates */
2336     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2337       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2338       .writefn = tlbiall_write },
2339     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2340       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2341       .writefn = tlbimva_write },
2342     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2343       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2344       .writefn = tlbiasid_write },
2345     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2346       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2347       .writefn = tlbimvaa_write },
2348 };
2349 
2350 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2351     /* 32 bit TLB invalidates, Inner Shareable */
2352     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2353       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2354       .writefn = tlbiall_is_write },
2355     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2356       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2357       .writefn = tlbimva_is_write },
2358     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2359       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2360       .writefn = tlbiasid_is_write },
2361     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2362       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2363       .writefn = tlbimvaa_is_write },
2364 };
2365 
2366 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2367     /* PMOVSSET is not implemented in v7 before v7ve */
2368     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2369       .access = PL0_RW, .accessfn = pmreg_access,
2370       .fgt = FGT_PMOVS,
2371       .type = ARM_CP_ALIAS | ARM_CP_IO,
2372       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2373       .writefn = pmovsset_write,
2374       .raw_writefn = raw_write },
2375     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2376       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2377       .access = PL0_RW, .accessfn = pmreg_access,
2378       .fgt = FGT_PMOVS,
2379       .type = ARM_CP_ALIAS | ARM_CP_IO,
2380       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2381       .writefn = pmovsset_write,
2382       .raw_writefn = raw_write },
2383 };
2384 
2385 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2386                         uint64_t value)
2387 {
2388     value &= 1;
2389     env->teecr = value;
2390 }
2391 
2392 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2393                                    bool isread)
2394 {
2395     /*
2396      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2397      * at all, so we don't need to check whether we're v8A.
2398      */
2399     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2400         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2401         return CP_ACCESS_TRAP_EL2;
2402     }
2403     return CP_ACCESS_OK;
2404 }
2405 
2406 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2407                                     bool isread)
2408 {
2409     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2410         return CP_ACCESS_TRAP;
2411     }
2412     return teecr_access(env, ri, isread);
2413 }
2414 
2415 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2416     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2417       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2418       .resetvalue = 0,
2419       .writefn = teecr_write, .accessfn = teecr_access },
2420     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2421       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2422       .accessfn = teehbr_access, .resetvalue = 0 },
2423 };
2424 
2425 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2426     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2427       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2428       .access = PL0_RW,
2429       .fgt = FGT_TPIDR_EL0,
2430       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2431     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2432       .access = PL0_RW,
2433       .fgt = FGT_TPIDR_EL0,
2434       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2435                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2436       .resetfn = arm_cp_reset_ignore },
2437     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2438       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2439       .access = PL0_R | PL1_W,
2440       .fgt = FGT_TPIDRRO_EL0,
2441       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2442       .resetvalue = 0},
2443     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2444       .access = PL0_R | PL1_W,
2445       .fgt = FGT_TPIDRRO_EL0,
2446       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2447                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2448       .resetfn = arm_cp_reset_ignore },
2449     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2450       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2451       .access = PL1_RW,
2452       .fgt = FGT_TPIDR_EL1,
2453       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2454     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2455       .access = PL1_RW,
2456       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2457                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2458       .resetvalue = 0 },
2459 };
2460 
2461 #ifndef CONFIG_USER_ONLY
2462 
2463 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2464                                        bool isread)
2465 {
2466     /*
2467      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2468      * Writable only at the highest implemented exception level.
2469      */
2470     int el = arm_current_el(env);
2471     uint64_t hcr;
2472     uint32_t cntkctl;
2473 
2474     switch (el) {
2475     case 0:
2476         hcr = arm_hcr_el2_eff(env);
2477         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2478             cntkctl = env->cp15.cnthctl_el2;
2479         } else {
2480             cntkctl = env->cp15.c14_cntkctl;
2481         }
2482         if (!extract32(cntkctl, 0, 2)) {
2483             return CP_ACCESS_TRAP;
2484         }
2485         break;
2486     case 1:
2487         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2488             arm_is_secure_below_el3(env)) {
2489             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2490             return CP_ACCESS_TRAP_UNCATEGORIZED;
2491         }
2492         break;
2493     case 2:
2494     case 3:
2495         break;
2496     }
2497 
2498     if (!isread && el < arm_highest_el(env)) {
2499         return CP_ACCESS_TRAP_UNCATEGORIZED;
2500     }
2501 
2502     return CP_ACCESS_OK;
2503 }
2504 
2505 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2506                                         bool isread)
2507 {
2508     unsigned int cur_el = arm_current_el(env);
2509     bool has_el2 = arm_is_el2_enabled(env);
2510     uint64_t hcr = arm_hcr_el2_eff(env);
2511 
2512     switch (cur_el) {
2513     case 0:
2514         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2515         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2516             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2517                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2518         }
2519 
2520         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2521         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2522             return CP_ACCESS_TRAP;
2523         }
2524         /* fall through */
2525     case 1:
2526         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2527         if (has_el2 && timeridx == GTIMER_PHYS &&
2528             (hcr & HCR_E2H
2529              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2530              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2531             return CP_ACCESS_TRAP_EL2;
2532         }
2533         break;
2534     }
2535     return CP_ACCESS_OK;
2536 }
2537 
2538 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2539                                       bool isread)
2540 {
2541     unsigned int cur_el = arm_current_el(env);
2542     bool has_el2 = arm_is_el2_enabled(env);
2543     uint64_t hcr = arm_hcr_el2_eff(env);
2544 
2545     switch (cur_el) {
2546     case 0:
2547         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2548             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2549             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2550                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2551         }
2552 
2553         /*
2554          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2555          * EL0 if EL0[PV]TEN is zero.
2556          */
2557         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2558             return CP_ACCESS_TRAP;
2559         }
2560         /* fall through */
2561 
2562     case 1:
2563         if (has_el2 && timeridx == GTIMER_PHYS) {
2564             if (hcr & HCR_E2H) {
2565                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2566                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2567                     return CP_ACCESS_TRAP_EL2;
2568                 }
2569             } else {
2570                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2571                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2572                     return CP_ACCESS_TRAP_EL2;
2573                 }
2574             }
2575         }
2576         break;
2577     }
2578     return CP_ACCESS_OK;
2579 }
2580 
2581 static CPAccessResult gt_pct_access(CPUARMState *env,
2582                                     const ARMCPRegInfo *ri,
2583                                     bool isread)
2584 {
2585     return gt_counter_access(env, GTIMER_PHYS, isread);
2586 }
2587 
2588 static CPAccessResult gt_vct_access(CPUARMState *env,
2589                                     const ARMCPRegInfo *ri,
2590                                     bool isread)
2591 {
2592     return gt_counter_access(env, GTIMER_VIRT, isread);
2593 }
2594 
2595 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2596                                        bool isread)
2597 {
2598     return gt_timer_access(env, GTIMER_PHYS, isread);
2599 }
2600 
2601 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2602                                        bool isread)
2603 {
2604     return gt_timer_access(env, GTIMER_VIRT, isread);
2605 }
2606 
2607 static CPAccessResult gt_stimer_access(CPUARMState *env,
2608                                        const ARMCPRegInfo *ri,
2609                                        bool isread)
2610 {
2611     /*
2612      * The AArch64 register view of the secure physical timer is
2613      * always accessible from EL3, and configurably accessible from
2614      * Secure EL1.
2615      */
2616     switch (arm_current_el(env)) {
2617     case 1:
2618         if (!arm_is_secure(env)) {
2619             return CP_ACCESS_TRAP;
2620         }
2621         if (!(env->cp15.scr_el3 & SCR_ST)) {
2622             return CP_ACCESS_TRAP_EL3;
2623         }
2624         return CP_ACCESS_OK;
2625     case 0:
2626     case 2:
2627         return CP_ACCESS_TRAP;
2628     case 3:
2629         return CP_ACCESS_OK;
2630     default:
2631         g_assert_not_reached();
2632     }
2633 }
2634 
2635 static uint64_t gt_get_countervalue(CPUARMState *env)
2636 {
2637     ARMCPU *cpu = env_archcpu(env);
2638 
2639     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2640 }
2641 
2642 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2643 {
2644     CPUARMState *env = &cpu->env;
2645     uint64_t cnthctl = env->cp15.cnthctl_el2;
2646     ARMSecuritySpace ss = arm_security_space(env);
2647     /* ISTATUS && !IMASK */
2648     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2649 
2650     /*
2651      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2652      * It is RES0 in Secure and NonSecure state.
2653      */
2654     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2655         ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2656          (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2657         irqstate = 0;
2658     }
2659 
2660     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2661     trace_arm_gt_update_irq(timeridx, irqstate);
2662 }
2663 
2664 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2665 {
2666     /*
2667      * Changing security state between Root and Secure/NonSecure, which may
2668      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2669      * mask bits. Update the IRQ state accordingly.
2670      */
2671     gt_update_irq(cpu, GTIMER_VIRT);
2672     gt_update_irq(cpu, GTIMER_PHYS);
2673 }
2674 
2675 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2676 {
2677     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2678 
2679     if (gt->ctl & 1) {
2680         /*
2681          * Timer enabled: calculate and set current ISTATUS, irq, and
2682          * reset timer to when ISTATUS next has to change
2683          */
2684         uint64_t offset = timeridx == GTIMER_VIRT ?
2685                                       cpu->env.cp15.cntvoff_el2 : 0;
2686         uint64_t count = gt_get_countervalue(&cpu->env);
2687         /* Note that this must be unsigned 64 bit arithmetic: */
2688         int istatus = count - offset >= gt->cval;
2689         uint64_t nexttick;
2690 
2691         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2692 
2693         if (istatus) {
2694             /*
2695              * Next transition is when (count - offset) rolls back over to 0.
2696              * If offset > count then this is when count == offset;
2697              * if offset <= count then this is when count == offset + 2^64
2698              * For the latter case we set nexttick to an "as far in future
2699              * as possible" value and let the code below handle it.
2700              */
2701             if (offset > count) {
2702                 nexttick = offset;
2703             } else {
2704                 nexttick = UINT64_MAX;
2705             }
2706         } else {
2707             /*
2708              * Next transition is when (count - offset) == cval, i.e.
2709              * when count == (cval + offset).
2710              * If that would overflow, then again we set up the next interrupt
2711              * for "as far in the future as possible" for the code below.
2712              */
2713             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2714                 nexttick = UINT64_MAX;
2715             }
2716         }
2717         /*
2718          * Note that the desired next expiry time might be beyond the
2719          * signed-64-bit range of a QEMUTimer -- in this case we just
2720          * set the timer for as far in the future as possible. When the
2721          * timer expires we will reset the timer for any remaining period.
2722          */
2723         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2724             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2725         } else {
2726             timer_mod(cpu->gt_timer[timeridx], nexttick);
2727         }
2728         trace_arm_gt_recalc(timeridx, nexttick);
2729     } else {
2730         /* Timer disabled: ISTATUS and timer output always clear */
2731         gt->ctl &= ~4;
2732         timer_del(cpu->gt_timer[timeridx]);
2733         trace_arm_gt_recalc_disabled(timeridx);
2734     }
2735     gt_update_irq(cpu, timeridx);
2736 }
2737 
2738 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2739                            int timeridx)
2740 {
2741     ARMCPU *cpu = env_archcpu(env);
2742 
2743     timer_del(cpu->gt_timer[timeridx]);
2744 }
2745 
2746 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2747 {
2748     return gt_get_countervalue(env);
2749 }
2750 
2751 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2752 {
2753     uint64_t hcr;
2754 
2755     switch (arm_current_el(env)) {
2756     case 2:
2757         hcr = arm_hcr_el2_eff(env);
2758         if (hcr & HCR_E2H) {
2759             return 0;
2760         }
2761         break;
2762     case 0:
2763         hcr = arm_hcr_el2_eff(env);
2764         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2765             return 0;
2766         }
2767         break;
2768     }
2769 
2770     return env->cp15.cntvoff_el2;
2771 }
2772 
2773 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2774 {
2775     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2776 }
2777 
2778 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779                           int timeridx,
2780                           uint64_t value)
2781 {
2782     trace_arm_gt_cval_write(timeridx, value);
2783     env->cp15.c14_timer[timeridx].cval = value;
2784     gt_recalc_timer(env_archcpu(env), timeridx);
2785 }
2786 
2787 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2788                              int timeridx)
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     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2800                       (gt_get_countervalue(env) - offset));
2801 }
2802 
2803 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2804                           int timeridx,
2805                           uint64_t value)
2806 {
2807     uint64_t offset = 0;
2808 
2809     switch (timeridx) {
2810     case GTIMER_VIRT:
2811     case GTIMER_HYPVIRT:
2812         offset = gt_virt_cnt_offset(env);
2813         break;
2814     }
2815 
2816     trace_arm_gt_tval_write(timeridx, value);
2817     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2818                                          sextract64(value, 0, 32);
2819     gt_recalc_timer(env_archcpu(env), timeridx);
2820 }
2821 
2822 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823                          int timeridx,
2824                          uint64_t value)
2825 {
2826     ARMCPU *cpu = env_archcpu(env);
2827     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2828 
2829     trace_arm_gt_ctl_write(timeridx, value);
2830     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2831     if ((oldval ^ value) & 1) {
2832         /* Enable toggled */
2833         gt_recalc_timer(cpu, timeridx);
2834     } else if ((oldval ^ value) & 2) {
2835         /*
2836          * IMASK toggled: don't need to recalculate,
2837          * just set the interrupt line based on ISTATUS
2838          */
2839         trace_arm_gt_imask_toggle(timeridx);
2840         gt_update_irq(cpu, timeridx);
2841     }
2842 }
2843 
2844 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2845 {
2846     gt_timer_reset(env, ri, GTIMER_PHYS);
2847 }
2848 
2849 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850                                uint64_t value)
2851 {
2852     gt_cval_write(env, ri, GTIMER_PHYS, value);
2853 }
2854 
2855 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2856 {
2857     return gt_tval_read(env, ri, GTIMER_PHYS);
2858 }
2859 
2860 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861                                uint64_t value)
2862 {
2863     gt_tval_write(env, ri, GTIMER_PHYS, value);
2864 }
2865 
2866 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867                               uint64_t value)
2868 {
2869     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2870 }
2871 
2872 static int gt_phys_redir_timeridx(CPUARMState *env)
2873 {
2874     switch (arm_mmu_idx(env)) {
2875     case ARMMMUIdx_E20_0:
2876     case ARMMMUIdx_E20_2:
2877     case ARMMMUIdx_E20_2_PAN:
2878         return GTIMER_HYP;
2879     default:
2880         return GTIMER_PHYS;
2881     }
2882 }
2883 
2884 static int gt_virt_redir_timeridx(CPUARMState *env)
2885 {
2886     switch (arm_mmu_idx(env)) {
2887     case ARMMMUIdx_E20_0:
2888     case ARMMMUIdx_E20_2:
2889     case ARMMMUIdx_E20_2_PAN:
2890         return GTIMER_HYPVIRT;
2891     default:
2892         return GTIMER_VIRT;
2893     }
2894 }
2895 
2896 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2897                                         const ARMCPRegInfo *ri)
2898 {
2899     int timeridx = gt_phys_redir_timeridx(env);
2900     return env->cp15.c14_timer[timeridx].cval;
2901 }
2902 
2903 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2904                                      uint64_t value)
2905 {
2906     int timeridx = gt_phys_redir_timeridx(env);
2907     gt_cval_write(env, ri, timeridx, value);
2908 }
2909 
2910 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2911                                         const ARMCPRegInfo *ri)
2912 {
2913     int timeridx = gt_phys_redir_timeridx(env);
2914     return gt_tval_read(env, ri, timeridx);
2915 }
2916 
2917 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2918                                      uint64_t value)
2919 {
2920     int timeridx = gt_phys_redir_timeridx(env);
2921     gt_tval_write(env, ri, timeridx, value);
2922 }
2923 
2924 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2925                                        const ARMCPRegInfo *ri)
2926 {
2927     int timeridx = gt_phys_redir_timeridx(env);
2928     return env->cp15.c14_timer[timeridx].ctl;
2929 }
2930 
2931 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2932                                     uint64_t value)
2933 {
2934     int timeridx = gt_phys_redir_timeridx(env);
2935     gt_ctl_write(env, ri, timeridx, value);
2936 }
2937 
2938 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2939 {
2940     gt_timer_reset(env, ri, GTIMER_VIRT);
2941 }
2942 
2943 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944                                uint64_t value)
2945 {
2946     gt_cval_write(env, ri, GTIMER_VIRT, value);
2947 }
2948 
2949 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2950 {
2951     return gt_tval_read(env, ri, GTIMER_VIRT);
2952 }
2953 
2954 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955                                uint64_t value)
2956 {
2957     gt_tval_write(env, ri, GTIMER_VIRT, value);
2958 }
2959 
2960 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961                               uint64_t value)
2962 {
2963     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2964 }
2965 
2966 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2967                              uint64_t value)
2968 {
2969     ARMCPU *cpu = env_archcpu(env);
2970     uint32_t oldval = env->cp15.cnthctl_el2;
2971 
2972     raw_write(env, ri, value);
2973 
2974     if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2975         gt_update_irq(cpu, GTIMER_VIRT);
2976     } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2977         gt_update_irq(cpu, GTIMER_PHYS);
2978     }
2979 }
2980 
2981 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982                               uint64_t value)
2983 {
2984     ARMCPU *cpu = env_archcpu(env);
2985 
2986     trace_arm_gt_cntvoff_write(value);
2987     raw_write(env, ri, value);
2988     gt_recalc_timer(cpu, GTIMER_VIRT);
2989 }
2990 
2991 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2992                                         const ARMCPRegInfo *ri)
2993 {
2994     int timeridx = gt_virt_redir_timeridx(env);
2995     return env->cp15.c14_timer[timeridx].cval;
2996 }
2997 
2998 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2999                                      uint64_t value)
3000 {
3001     int timeridx = gt_virt_redir_timeridx(env);
3002     gt_cval_write(env, ri, timeridx, value);
3003 }
3004 
3005 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3006                                         const ARMCPRegInfo *ri)
3007 {
3008     int timeridx = gt_virt_redir_timeridx(env);
3009     return gt_tval_read(env, ri, timeridx);
3010 }
3011 
3012 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3013                                      uint64_t value)
3014 {
3015     int timeridx = gt_virt_redir_timeridx(env);
3016     gt_tval_write(env, ri, timeridx, value);
3017 }
3018 
3019 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3020                                        const ARMCPRegInfo *ri)
3021 {
3022     int timeridx = gt_virt_redir_timeridx(env);
3023     return env->cp15.c14_timer[timeridx].ctl;
3024 }
3025 
3026 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3027                                     uint64_t value)
3028 {
3029     int timeridx = gt_virt_redir_timeridx(env);
3030     gt_ctl_write(env, ri, timeridx, value);
3031 }
3032 
3033 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3034 {
3035     gt_timer_reset(env, ri, GTIMER_HYP);
3036 }
3037 
3038 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3039                               uint64_t value)
3040 {
3041     gt_cval_write(env, ri, GTIMER_HYP, value);
3042 }
3043 
3044 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3045 {
3046     return gt_tval_read(env, ri, GTIMER_HYP);
3047 }
3048 
3049 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050                               uint64_t value)
3051 {
3052     gt_tval_write(env, ri, GTIMER_HYP, value);
3053 }
3054 
3055 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3056                               uint64_t value)
3057 {
3058     gt_ctl_write(env, ri, GTIMER_HYP, value);
3059 }
3060 
3061 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3062 {
3063     gt_timer_reset(env, ri, GTIMER_SEC);
3064 }
3065 
3066 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3067                               uint64_t value)
3068 {
3069     gt_cval_write(env, ri, GTIMER_SEC, value);
3070 }
3071 
3072 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3073 {
3074     return gt_tval_read(env, ri, GTIMER_SEC);
3075 }
3076 
3077 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078                               uint64_t value)
3079 {
3080     gt_tval_write(env, ri, GTIMER_SEC, value);
3081 }
3082 
3083 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3084                               uint64_t value)
3085 {
3086     gt_ctl_write(env, ri, GTIMER_SEC, value);
3087 }
3088 
3089 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3090 {
3091     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3092 }
3093 
3094 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3095                              uint64_t value)
3096 {
3097     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3098 }
3099 
3100 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3101 {
3102     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3103 }
3104 
3105 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3106                              uint64_t value)
3107 {
3108     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3109 }
3110 
3111 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3112                             uint64_t value)
3113 {
3114     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3115 }
3116 
3117 void arm_gt_ptimer_cb(void *opaque)
3118 {
3119     ARMCPU *cpu = opaque;
3120 
3121     gt_recalc_timer(cpu, GTIMER_PHYS);
3122 }
3123 
3124 void arm_gt_vtimer_cb(void *opaque)
3125 {
3126     ARMCPU *cpu = opaque;
3127 
3128     gt_recalc_timer(cpu, GTIMER_VIRT);
3129 }
3130 
3131 void arm_gt_htimer_cb(void *opaque)
3132 {
3133     ARMCPU *cpu = opaque;
3134 
3135     gt_recalc_timer(cpu, GTIMER_HYP);
3136 }
3137 
3138 void arm_gt_stimer_cb(void *opaque)
3139 {
3140     ARMCPU *cpu = opaque;
3141 
3142     gt_recalc_timer(cpu, GTIMER_SEC);
3143 }
3144 
3145 void arm_gt_hvtimer_cb(void *opaque)
3146 {
3147     ARMCPU *cpu = opaque;
3148 
3149     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3150 }
3151 
3152 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3153 {
3154     ARMCPU *cpu = env_archcpu(env);
3155 
3156     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3157 }
3158 
3159 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3160     /*
3161      * Note that CNTFRQ is purely reads-as-written for the benefit
3162      * of software; writing it doesn't actually change the timer frequency.
3163      * Our reset value matches the fixed frequency we implement the timer at.
3164      */
3165     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3166       .type = ARM_CP_ALIAS,
3167       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3168       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3169     },
3170     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3171       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3172       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3173       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3174       .resetfn = arm_gt_cntfrq_reset,
3175     },
3176     /* overall control: mostly access permissions */
3177     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3178       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3179       .access = PL1_RW,
3180       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3181       .resetvalue = 0,
3182     },
3183     /* per-timer control */
3184     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3185       .secure = ARM_CP_SECSTATE_NS,
3186       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3187       .accessfn = gt_ptimer_access,
3188       .fieldoffset = offsetoflow32(CPUARMState,
3189                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3190       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3191       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3192     },
3193     { .name = "CNTP_CTL_S",
3194       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3195       .secure = ARM_CP_SECSTATE_S,
3196       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3197       .accessfn = gt_ptimer_access,
3198       .fieldoffset = offsetoflow32(CPUARMState,
3199                                    cp15.c14_timer[GTIMER_SEC].ctl),
3200       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3201     },
3202     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3203       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3204       .type = ARM_CP_IO, .access = PL0_RW,
3205       .accessfn = gt_ptimer_access,
3206       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3207       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3208       .resetvalue = 0,
3209       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3210       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3211     },
3212     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3213       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3214       .accessfn = gt_vtimer_access,
3215       .fieldoffset = offsetoflow32(CPUARMState,
3216                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3217       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3218       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3219     },
3220     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3221       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3222       .type = ARM_CP_IO, .access = PL0_RW,
3223       .accessfn = gt_vtimer_access,
3224       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3225       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3226       .resetvalue = 0,
3227       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3228       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3229     },
3230     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3231     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3232       .secure = ARM_CP_SECSTATE_NS,
3233       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3234       .accessfn = gt_ptimer_access,
3235       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3236     },
3237     { .name = "CNTP_TVAL_S",
3238       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3239       .secure = ARM_CP_SECSTATE_S,
3240       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3241       .accessfn = gt_ptimer_access,
3242       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3243     },
3244     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3245       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3246       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3247       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3248       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3249     },
3250     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3251       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3252       .accessfn = gt_vtimer_access,
3253       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3254     },
3255     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3256       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3257       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3258       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3259       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3260     },
3261     /* The counter itself */
3262     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3263       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3264       .accessfn = gt_pct_access,
3265       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3266     },
3267     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3268       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3269       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3270       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3271     },
3272     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3273       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3274       .accessfn = gt_vct_access,
3275       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3276     },
3277     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3278       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3279       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3280       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3281     },
3282     /* Comparison value, indicating when the timer goes off */
3283     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3284       .secure = ARM_CP_SECSTATE_NS,
3285       .access = PL0_RW,
3286       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3287       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3288       .accessfn = gt_ptimer_access,
3289       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3290       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3291     },
3292     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3293       .secure = ARM_CP_SECSTATE_S,
3294       .access = PL0_RW,
3295       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3296       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3297       .accessfn = gt_ptimer_access,
3298       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3299     },
3300     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3301       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3302       .access = PL0_RW,
3303       .type = ARM_CP_IO,
3304       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3305       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3306       .resetvalue = 0, .accessfn = gt_ptimer_access,
3307       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3308       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3309     },
3310     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3311       .access = PL0_RW,
3312       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3313       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3314       .accessfn = gt_vtimer_access,
3315       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3316       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3317     },
3318     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3319       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3320       .access = PL0_RW,
3321       .type = ARM_CP_IO,
3322       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3323       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3324       .resetvalue = 0, .accessfn = gt_vtimer_access,
3325       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3326       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3327     },
3328     /*
3329      * Secure timer -- this is actually restricted to only EL3
3330      * and configurably Secure-EL1 via the accessfn.
3331      */
3332     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3333       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3334       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3335       .accessfn = gt_stimer_access,
3336       .readfn = gt_sec_tval_read,
3337       .writefn = gt_sec_tval_write,
3338       .resetfn = gt_sec_timer_reset,
3339     },
3340     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3341       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3342       .type = ARM_CP_IO, .access = PL1_RW,
3343       .accessfn = gt_stimer_access,
3344       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3345       .resetvalue = 0,
3346       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3347     },
3348     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3349       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3350       .type = ARM_CP_IO, .access = PL1_RW,
3351       .accessfn = gt_stimer_access,
3352       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3353       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3354     },
3355 };
3356 
3357 #else
3358 
3359 /*
3360  * In user-mode most of the generic timer registers are inaccessible
3361  * however modern kernels (4.12+) allow access to cntvct_el0
3362  */
3363 
3364 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3365 {
3366     ARMCPU *cpu = env_archcpu(env);
3367 
3368     /*
3369      * Currently we have no support for QEMUTimer in linux-user so we
3370      * can't call gt_get_countervalue(env), instead we directly
3371      * call the lower level functions.
3372      */
3373     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3374 }
3375 
3376 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3377     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3378       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3379       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3380       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3381       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3382     },
3383     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3384       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3385       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3386       .readfn = gt_virt_cnt_read,
3387     },
3388 };
3389 
3390 #endif
3391 
3392 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3393 {
3394     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3395         raw_write(env, ri, value);
3396     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3397         raw_write(env, ri, value & 0xfffff6ff);
3398     } else {
3399         raw_write(env, ri, value & 0xfffff1ff);
3400     }
3401 }
3402 
3403 #ifndef CONFIG_USER_ONLY
3404 /* get_phys_addr() isn't present for user-mode-only targets */
3405 
3406 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3407                                  bool isread)
3408 {
3409     if (ri->opc2 & 4) {
3410         /*
3411          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3412          * Secure EL1 (which can only happen if EL3 is AArch64).
3413          * They are simply UNDEF if executed from NS EL1.
3414          * They function normally from EL2 or EL3.
3415          */
3416         if (arm_current_el(env) == 1) {
3417             if (arm_is_secure_below_el3(env)) {
3418                 if (env->cp15.scr_el3 & SCR_EEL2) {
3419                     return CP_ACCESS_TRAP_EL2;
3420                 }
3421                 return CP_ACCESS_TRAP_EL3;
3422             }
3423             return CP_ACCESS_TRAP_UNCATEGORIZED;
3424         }
3425     }
3426     return CP_ACCESS_OK;
3427 }
3428 
3429 #ifdef CONFIG_TCG
3430 static int par_el1_shareability(GetPhysAddrResult *res)
3431 {
3432     /*
3433      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3434      * memory -- see pseudocode PAREncodeShareability().
3435      */
3436     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3437         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3438         return 2;
3439     }
3440     return res->cacheattrs.shareability;
3441 }
3442 
3443 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3444                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3445                              ARMSecuritySpace ss)
3446 {
3447     bool ret;
3448     uint64_t par64;
3449     bool format64 = false;
3450     ARMMMUFaultInfo fi = {};
3451     GetPhysAddrResult res = {};
3452 
3453     /*
3454      * I_MXTJT: Granule protection checks are not performed on the final address
3455      * of a successful translation.
3456      */
3457     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3458                                          &res, &fi);
3459 
3460     /*
3461      * ATS operations only do S1 or S1+S2 translations, so we never
3462      * have to deal with the ARMCacheAttrs format for S2 only.
3463      */
3464     assert(!res.cacheattrs.is_s2_format);
3465 
3466     if (ret) {
3467         /*
3468          * Some kinds of translation fault must cause exceptions rather
3469          * than being reported in the PAR.
3470          */
3471         int current_el = arm_current_el(env);
3472         int target_el;
3473         uint32_t syn, fsr, fsc;
3474         bool take_exc = false;
3475 
3476         if (fi.s1ptw && current_el == 1
3477             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3478             /*
3479              * Synchronous stage 2 fault on an access made as part of the
3480              * translation table walk for AT S1E0* or AT S1E1* insn
3481              * executed from NS EL1. If this is a synchronous external abort
3482              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3483              * to EL3. Otherwise the fault is taken as an exception to EL2,
3484              * and HPFAR_EL2 holds the faulting IPA.
3485              */
3486             if (fi.type == ARMFault_SyncExternalOnWalk &&
3487                 (env->cp15.scr_el3 & SCR_EA)) {
3488                 target_el = 3;
3489             } else {
3490                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3491                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3492                     env->cp15.hpfar_el2 |= HPFAR_NS;
3493                 }
3494                 target_el = 2;
3495             }
3496             take_exc = true;
3497         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3498             /*
3499              * Synchronous external aborts during a translation table walk
3500              * are taken as Data Abort exceptions.
3501              */
3502             if (fi.stage2) {
3503                 if (current_el == 3) {
3504                     target_el = 3;
3505                 } else {
3506                     target_el = 2;
3507                 }
3508             } else {
3509                 target_el = exception_target_el(env);
3510             }
3511             take_exc = true;
3512         }
3513 
3514         if (take_exc) {
3515             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3516             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3517                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3518                 fsr = arm_fi_to_lfsc(&fi);
3519                 fsc = extract32(fsr, 0, 6);
3520             } else {
3521                 fsr = arm_fi_to_sfsc(&fi);
3522                 fsc = 0x3f;
3523             }
3524             /*
3525              * Report exception with ESR indicating a fault due to a
3526              * translation table walk for a cache maintenance instruction.
3527              */
3528             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3529                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3530             env->exception.vaddress = value;
3531             env->exception.fsr = fsr;
3532             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3533         }
3534     }
3535 
3536     if (is_a64(env)) {
3537         format64 = true;
3538     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3539         /*
3540          * ATS1Cxx:
3541          * * TTBCR.EAE determines whether the result is returned using the
3542          *   32-bit or the 64-bit PAR format
3543          * * Instructions executed in Hyp mode always use the 64bit format
3544          *
3545          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3546          * * The Non-secure TTBCR.EAE bit is set to 1
3547          * * The implementation includes EL2, and the value of HCR.VM is 1
3548          *
3549          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3550          *
3551          * ATS1Hx always uses the 64bit format.
3552          */
3553         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3554 
3555         if (arm_feature(env, ARM_FEATURE_EL2)) {
3556             if (mmu_idx == ARMMMUIdx_E10_0 ||
3557                 mmu_idx == ARMMMUIdx_E10_1 ||
3558                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3559                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3560             } else {
3561                 format64 |= arm_current_el(env) == 2;
3562             }
3563         }
3564     }
3565 
3566     if (format64) {
3567         /* Create a 64-bit PAR */
3568         par64 = (1 << 11); /* LPAE bit always set */
3569         if (!ret) {
3570             par64 |= res.f.phys_addr & ~0xfffULL;
3571             if (!res.f.attrs.secure) {
3572                 par64 |= (1 << 9); /* NS */
3573             }
3574             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3575             par64 |= par_el1_shareability(&res) << 7; /* SH */
3576         } else {
3577             uint32_t fsr = arm_fi_to_lfsc(&fi);
3578 
3579             par64 |= 1; /* F */
3580             par64 |= (fsr & 0x3f) << 1; /* FS */
3581             if (fi.stage2) {
3582                 par64 |= (1 << 9); /* S */
3583             }
3584             if (fi.s1ptw) {
3585                 par64 |= (1 << 8); /* PTW */
3586             }
3587         }
3588     } else {
3589         /*
3590          * fsr is a DFSR/IFSR value for the short descriptor
3591          * translation table format (with WnR always clear).
3592          * Convert it to a 32-bit PAR.
3593          */
3594         if (!ret) {
3595             /* We do not set any attribute bits in the PAR */
3596             if (res.f.lg_page_size == 24
3597                 && arm_feature(env, ARM_FEATURE_V7)) {
3598                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3599             } else {
3600                 par64 = res.f.phys_addr & 0xfffff000;
3601             }
3602             if (!res.f.attrs.secure) {
3603                 par64 |= (1 << 9); /* NS */
3604             }
3605         } else {
3606             uint32_t fsr = arm_fi_to_sfsc(&fi);
3607 
3608             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3609                     ((fsr & 0xf) << 1) | 1;
3610         }
3611     }
3612     return par64;
3613 }
3614 #endif /* CONFIG_TCG */
3615 
3616 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3617 {
3618 #ifdef CONFIG_TCG
3619     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3620     uint64_t par64;
3621     ARMMMUIdx mmu_idx;
3622     int el = arm_current_el(env);
3623     ARMSecuritySpace ss = arm_security_space(env);
3624 
3625     switch (ri->opc2 & 6) {
3626     case 0:
3627         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3628         switch (el) {
3629         case 3:
3630             mmu_idx = ARMMMUIdx_E3;
3631             break;
3632         case 2:
3633             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3634             /* fall through */
3635         case 1:
3636             if (ri->crm == 9 && arm_pan_enabled(env)) {
3637                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3638             } else {
3639                 mmu_idx = ARMMMUIdx_Stage1_E1;
3640             }
3641             break;
3642         default:
3643             g_assert_not_reached();
3644         }
3645         break;
3646     case 2:
3647         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3648         switch (el) {
3649         case 3:
3650             mmu_idx = ARMMMUIdx_E10_0;
3651             break;
3652         case 2:
3653             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3654             mmu_idx = ARMMMUIdx_Stage1_E0;
3655             break;
3656         case 1:
3657             mmu_idx = ARMMMUIdx_Stage1_E0;
3658             break;
3659         default:
3660             g_assert_not_reached();
3661         }
3662         break;
3663     case 4:
3664         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3665         mmu_idx = ARMMMUIdx_E10_1;
3666         ss = ARMSS_NonSecure;
3667         break;
3668     case 6:
3669         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3670         mmu_idx = ARMMMUIdx_E10_0;
3671         ss = ARMSS_NonSecure;
3672         break;
3673     default:
3674         g_assert_not_reached();
3675     }
3676 
3677     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3678 
3679     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3680 #else
3681     /* Handled by hardware accelerator. */
3682     g_assert_not_reached();
3683 #endif /* CONFIG_TCG */
3684 }
3685 
3686 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3687                         uint64_t value)
3688 {
3689 #ifdef CONFIG_TCG
3690     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3691     uint64_t par64;
3692 
3693     /* There is no SecureEL2 for AArch32. */
3694     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3695                          ARMSS_NonSecure);
3696 
3697     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3698 #else
3699     /* Handled by hardware accelerator. */
3700     g_assert_not_reached();
3701 #endif /* CONFIG_TCG */
3702 }
3703 
3704 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3705                                      bool isread)
3706 {
3707     /*
3708      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3709      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3710      * only happen when executing at EL3 because that combination also causes an
3711      * illegal exception return. We don't need to check FEAT_RME either, because
3712      * scr_write() ensures that the NSE bit is not set otherwise.
3713      */
3714     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3715         return CP_ACCESS_TRAP;
3716     }
3717     return CP_ACCESS_OK;
3718 }
3719 
3720 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3721                                      bool isread)
3722 {
3723     if (arm_current_el(env) == 3 &&
3724         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3725         return CP_ACCESS_TRAP;
3726     }
3727     return at_e012_access(env, ri, isread);
3728 }
3729 
3730 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3731                                       bool isread)
3732 {
3733     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3734         return CP_ACCESS_TRAP_EL2;
3735     }
3736     return at_e012_access(env, ri, isread);
3737 }
3738 
3739 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3740                         uint64_t value)
3741 {
3742 #ifdef CONFIG_TCG
3743     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3744     ARMMMUIdx mmu_idx;
3745     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3746     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3747 
3748     switch (ri->opc2 & 6) {
3749     case 0:
3750         switch (ri->opc1) {
3751         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3752             if (ri->crm == 9 && arm_pan_enabled(env)) {
3753                 mmu_idx = regime_e20 ?
3754                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3755             } else {
3756                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3757             }
3758             break;
3759         case 4: /* AT S1E2R, AT S1E2W */
3760             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3761             break;
3762         case 6: /* AT S1E3R, AT S1E3W */
3763             mmu_idx = ARMMMUIdx_E3;
3764             break;
3765         default:
3766             g_assert_not_reached();
3767         }
3768         break;
3769     case 2: /* AT S1E0R, AT S1E0W */
3770         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3771         break;
3772     case 4: /* AT S12E1R, AT S12E1W */
3773         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3774         break;
3775     case 6: /* AT S12E0R, AT S12E0W */
3776         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3777         break;
3778     default:
3779         g_assert_not_reached();
3780     }
3781 
3782     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3783                                        mmu_idx, arm_security_space(env));
3784 #else
3785     /* Handled by hardware accelerator. */
3786     g_assert_not_reached();
3787 #endif /* CONFIG_TCG */
3788 }
3789 #endif
3790 
3791 /* Return basic MPU access permission bits.  */
3792 static uint32_t simple_mpu_ap_bits(uint32_t val)
3793 {
3794     uint32_t ret;
3795     uint32_t mask;
3796     int i;
3797     ret = 0;
3798     mask = 3;
3799     for (i = 0; i < 16; i += 2) {
3800         ret |= (val >> i) & mask;
3801         mask <<= 2;
3802     }
3803     return ret;
3804 }
3805 
3806 /* Pad basic MPU access permission bits to extended format.  */
3807 static uint32_t extended_mpu_ap_bits(uint32_t val)
3808 {
3809     uint32_t ret;
3810     uint32_t mask;
3811     int i;
3812     ret = 0;
3813     mask = 3;
3814     for (i = 0; i < 16; i += 2) {
3815         ret |= (val & mask) << i;
3816         mask <<= 2;
3817     }
3818     return ret;
3819 }
3820 
3821 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3822                                  uint64_t value)
3823 {
3824     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3825 }
3826 
3827 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3828 {
3829     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3830 }
3831 
3832 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3833                                  uint64_t value)
3834 {
3835     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3836 }
3837 
3838 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3839 {
3840     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3841 }
3842 
3843 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3844 {
3845     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3846 
3847     if (!u32p) {
3848         return 0;
3849     }
3850 
3851     u32p += env->pmsav7.rnr[M_REG_NS];
3852     return *u32p;
3853 }
3854 
3855 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3856                          uint64_t value)
3857 {
3858     ARMCPU *cpu = env_archcpu(env);
3859     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3860 
3861     if (!u32p) {
3862         return;
3863     }
3864 
3865     u32p += env->pmsav7.rnr[M_REG_NS];
3866     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3867     *u32p = value;
3868 }
3869 
3870 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3871                               uint64_t value)
3872 {
3873     ARMCPU *cpu = env_archcpu(env);
3874     uint32_t nrgs = cpu->pmsav7_dregion;
3875 
3876     if (value >= nrgs) {
3877         qemu_log_mask(LOG_GUEST_ERROR,
3878                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3879                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3880         return;
3881     }
3882 
3883     raw_write(env, ri, value);
3884 }
3885 
3886 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3887                           uint64_t value)
3888 {
3889     ARMCPU *cpu = env_archcpu(env);
3890 
3891     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3892     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3893 }
3894 
3895 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3896 {
3897     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3898 }
3899 
3900 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3901                           uint64_t value)
3902 {
3903     ARMCPU *cpu = env_archcpu(env);
3904 
3905     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3906     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3907 }
3908 
3909 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3910 {
3911     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3912 }
3913 
3914 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3915                            uint64_t value)
3916 {
3917     ARMCPU *cpu = env_archcpu(env);
3918 
3919     /*
3920      * Ignore writes that would select not implemented region.
3921      * This is architecturally UNPREDICTABLE.
3922      */
3923     if (value >= cpu->pmsav7_dregion) {
3924         return;
3925     }
3926 
3927     env->pmsav7.rnr[M_REG_NS] = value;
3928 }
3929 
3930 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3931                           uint64_t value)
3932 {
3933     ARMCPU *cpu = env_archcpu(env);
3934 
3935     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3936     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3937 }
3938 
3939 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3940 {
3941     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3942 }
3943 
3944 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3945                           uint64_t value)
3946 {
3947     ARMCPU *cpu = env_archcpu(env);
3948 
3949     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3950     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3951 }
3952 
3953 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3954 {
3955     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3956 }
3957 
3958 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3959                           uint64_t value)
3960 {
3961     uint32_t n;
3962     uint32_t bit;
3963     ARMCPU *cpu = env_archcpu(env);
3964 
3965     /* Ignore writes to unimplemented regions */
3966     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3967     value &= MAKE_64BIT_MASK(0, rmax);
3968 
3969     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3970 
3971     /* Register alias is only valid for first 32 indexes */
3972     for (n = 0; n < rmax; ++n) {
3973         bit = extract32(value, n, 1);
3974         env->pmsav8.hprlar[n] = deposit32(
3975                     env->pmsav8.hprlar[n], 0, 1, bit);
3976     }
3977 }
3978 
3979 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3980 {
3981     uint32_t n;
3982     uint32_t result = 0x0;
3983     ARMCPU *cpu = env_archcpu(env);
3984 
3985     /* Register alias is only valid for first 32 indexes */
3986     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3987         if (env->pmsav8.hprlar[n] & 0x1) {
3988             result |= (0x1 << n);
3989         }
3990     }
3991     return result;
3992 }
3993 
3994 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3995                            uint64_t value)
3996 {
3997     ARMCPU *cpu = env_archcpu(env);
3998 
3999     /*
4000      * Ignore writes that would select not implemented region.
4001      * This is architecturally UNPREDICTABLE.
4002      */
4003     if (value >= cpu->pmsav8r_hdregion) {
4004         return;
4005     }
4006 
4007     env->pmsav8.hprselr = value;
4008 }
4009 
4010 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4011                           uint64_t value)
4012 {
4013     ARMCPU *cpu = env_archcpu(env);
4014     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4015                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4016 
4017     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4018 
4019     if (ri->opc1 & 4) {
4020         if (index >= cpu->pmsav8r_hdregion) {
4021             return;
4022         }
4023         if (ri->opc2 & 0x1) {
4024             env->pmsav8.hprlar[index] = value;
4025         } else {
4026             env->pmsav8.hprbar[index] = value;
4027         }
4028     } else {
4029         if (index >= cpu->pmsav7_dregion) {
4030             return;
4031         }
4032         if (ri->opc2 & 0x1) {
4033             env->pmsav8.rlar[M_REG_NS][index] = value;
4034         } else {
4035             env->pmsav8.rbar[M_REG_NS][index] = value;
4036         }
4037     }
4038 }
4039 
4040 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4041 {
4042     ARMCPU *cpu = env_archcpu(env);
4043     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4044                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4045 
4046     if (ri->opc1 & 4) {
4047         if (index >= cpu->pmsav8r_hdregion) {
4048             return 0x0;
4049         }
4050         if (ri->opc2 & 0x1) {
4051             return env->pmsav8.hprlar[index];
4052         } else {
4053             return env->pmsav8.hprbar[index];
4054         }
4055     } else {
4056         if (index >= cpu->pmsav7_dregion) {
4057             return 0x0;
4058         }
4059         if (ri->opc2 & 0x1) {
4060             return env->pmsav8.rlar[M_REG_NS][index];
4061         } else {
4062             return env->pmsav8.rbar[M_REG_NS][index];
4063         }
4064     }
4065 }
4066 
4067 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4068     { .name = "PRBAR",
4069       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4070       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4071       .accessfn = access_tvm_trvm,
4072       .readfn = prbar_read, .writefn = prbar_write },
4073     { .name = "PRLAR",
4074       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4075       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4076       .accessfn = access_tvm_trvm,
4077       .readfn = prlar_read, .writefn = prlar_write },
4078     { .name = "PRSELR", .resetvalue = 0,
4079       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4080       .access = PL1_RW, .accessfn = access_tvm_trvm,
4081       .writefn = prselr_write,
4082       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4083     { .name = "HPRBAR", .resetvalue = 0,
4084       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4085       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4086       .readfn = hprbar_read, .writefn = hprbar_write },
4087     { .name = "HPRLAR",
4088       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4089       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4090       .readfn = hprlar_read, .writefn = hprlar_write },
4091     { .name = "HPRSELR", .resetvalue = 0,
4092       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4093       .access = PL2_RW,
4094       .writefn = hprselr_write,
4095       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4096     { .name = "HPRENR",
4097       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4098       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4099       .readfn = hprenr_read, .writefn = hprenr_write },
4100 };
4101 
4102 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4103     /*
4104      * Reset for all these registers is handled in arm_cpu_reset(),
4105      * because the PMSAv7 is also used by M-profile CPUs, which do
4106      * not register cpregs but still need the state to be reset.
4107      */
4108     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4109       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4110       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4111       .readfn = pmsav7_read, .writefn = pmsav7_write,
4112       .resetfn = arm_cp_reset_ignore },
4113     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4114       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4115       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4116       .readfn = pmsav7_read, .writefn = pmsav7_write,
4117       .resetfn = arm_cp_reset_ignore },
4118     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4119       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4120       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4121       .readfn = pmsav7_read, .writefn = pmsav7_write,
4122       .resetfn = arm_cp_reset_ignore },
4123     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4124       .access = PL1_RW,
4125       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4126       .writefn = pmsav7_rgnr_write,
4127       .resetfn = arm_cp_reset_ignore },
4128 };
4129 
4130 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4131     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4132       .access = PL1_RW, .type = ARM_CP_ALIAS,
4133       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4134       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4135     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4136       .access = PL1_RW, .type = ARM_CP_ALIAS,
4137       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4138       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4139     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4140       .access = PL1_RW,
4141       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4142       .resetvalue = 0, },
4143     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4144       .access = PL1_RW,
4145       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4146       .resetvalue = 0, },
4147     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4148       .access = PL1_RW,
4149       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4150     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4151       .access = PL1_RW,
4152       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4153     /* Protection region base and size registers */
4154     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4155       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4156       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4157     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4158       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4159       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4160     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4161       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4162       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4163     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4164       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4165       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4166     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4167       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4168       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4169     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4170       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4171       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4172     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4173       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4174       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4175     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4176       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4177       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4178 };
4179 
4180 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181                              uint64_t value)
4182 {
4183     ARMCPU *cpu = env_archcpu(env);
4184 
4185     if (!arm_feature(env, ARM_FEATURE_V8)) {
4186         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4187             /*
4188              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4189              * using Long-descriptor translation table format
4190              */
4191             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4192         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4193             /*
4194              * In an implementation that includes the Security Extensions
4195              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4196              * Short-descriptor translation table format.
4197              */
4198             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4199         } else {
4200             value &= TTBCR_N;
4201         }
4202     }
4203 
4204     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4205         /*
4206          * With LPAE the TTBCR could result in a change of ASID
4207          * via the TTBCR.A1 bit, so do a TLB flush.
4208          */
4209         tlb_flush(CPU(cpu));
4210     }
4211     raw_write(env, ri, value);
4212 }
4213 
4214 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4215                                uint64_t value)
4216 {
4217     ARMCPU *cpu = env_archcpu(env);
4218 
4219     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4220     tlb_flush(CPU(cpu));
4221     raw_write(env, ri, value);
4222 }
4223 
4224 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4225                             uint64_t value)
4226 {
4227     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4228     if (cpreg_field_is_64bit(ri) &&
4229         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4230         ARMCPU *cpu = env_archcpu(env);
4231         tlb_flush(CPU(cpu));
4232     }
4233     raw_write(env, ri, value);
4234 }
4235 
4236 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4237                                     uint64_t value)
4238 {
4239     /*
4240      * If we are running with E2&0 regime, then an ASID is active.
4241      * Flush if that might be changing.  Note we're not checking
4242      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4243      * holds the active ASID, only checking the field that might.
4244      */
4245     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4246         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4247         uint16_t mask = ARMMMUIdxBit_E20_2 |
4248                         ARMMMUIdxBit_E20_2_PAN |
4249                         ARMMMUIdxBit_E20_0;
4250         tlb_flush_by_mmuidx(env_cpu(env), mask);
4251     }
4252     raw_write(env, ri, value);
4253 }
4254 
4255 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4256                         uint64_t value)
4257 {
4258     ARMCPU *cpu = env_archcpu(env);
4259     CPUState *cs = CPU(cpu);
4260 
4261     /*
4262      * A change in VMID to the stage2 page table (Stage2) invalidates
4263      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4264      */
4265     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4266         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4267     }
4268     raw_write(env, ri, value);
4269 }
4270 
4271 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4272     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4273       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4274       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4275                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4276     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4277       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4278       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4279                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4280     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4281       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4282       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4283                              offsetof(CPUARMState, cp15.dfar_ns) } },
4284     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4285       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4286       .access = PL1_RW, .accessfn = access_tvm_trvm,
4287       .fgt = FGT_FAR_EL1,
4288       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4289       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4290       .resetvalue = 0, },
4291 };
4292 
4293 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4294     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4295       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4296       .access = PL1_RW, .accessfn = access_tvm_trvm,
4297       .fgt = FGT_ESR_EL1,
4298       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4299       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4300     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4301       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4302       .access = PL1_RW, .accessfn = access_tvm_trvm,
4303       .fgt = FGT_TTBR0_EL1,
4304       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4305       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4306       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4307                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4308     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4309       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4310       .access = PL1_RW, .accessfn = access_tvm_trvm,
4311       .fgt = FGT_TTBR1_EL1,
4312       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4313       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4314       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4315                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4316     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4317       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4318       .access = PL1_RW, .accessfn = access_tvm_trvm,
4319       .fgt = FGT_TCR_EL1,
4320       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4321       .writefn = vmsa_tcr_el12_write,
4322       .raw_writefn = raw_write,
4323       .resetvalue = 0,
4324       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4325     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4326       .access = PL1_RW, .accessfn = access_tvm_trvm,
4327       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4328       .raw_writefn = raw_write,
4329       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4330                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4331 };
4332 
4333 /*
4334  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4335  * qemu tlbs nor adjusting cached masks.
4336  */
4337 static const ARMCPRegInfo ttbcr2_reginfo = {
4338     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4339     .access = PL1_RW, .accessfn = access_tvm_trvm,
4340     .type = ARM_CP_ALIAS,
4341     .bank_fieldoffsets = {
4342         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4343         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4344     },
4345 };
4346 
4347 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4348                                 uint64_t value)
4349 {
4350     env->cp15.c15_ticonfig = value & 0xe7;
4351     /* The OS_TYPE bit in this register changes the reported CPUID! */
4352     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4353         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4354 }
4355 
4356 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4357                                 uint64_t value)
4358 {
4359     env->cp15.c15_threadid = value & 0xffff;
4360 }
4361 
4362 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4363                            uint64_t value)
4364 {
4365     /* Wait-for-interrupt (deprecated) */
4366     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4367 }
4368 
4369 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4370                                   uint64_t value)
4371 {
4372     /*
4373      * On OMAP there are registers indicating the max/min index of dcache lines
4374      * containing a dirty line; cache flush operations have to reset these.
4375      */
4376     env->cp15.c15_i_max = 0x000;
4377     env->cp15.c15_i_min = 0xff0;
4378 }
4379 
4380 static const ARMCPRegInfo omap_cp_reginfo[] = {
4381     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4382       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4383       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4384       .resetvalue = 0, },
4385     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4386       .access = PL1_RW, .type = ARM_CP_NOP },
4387     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4388       .access = PL1_RW,
4389       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4390       .writefn = omap_ticonfig_write },
4391     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4392       .access = PL1_RW,
4393       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4394     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4395       .access = PL1_RW, .resetvalue = 0xff0,
4396       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4397     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4398       .access = PL1_RW,
4399       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4400       .writefn = omap_threadid_write },
4401     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4402       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4403       .type = ARM_CP_NO_RAW,
4404       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4405     /*
4406      * TODO: Peripheral port remap register:
4407      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4408      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4409      * when MMU is off.
4410      */
4411     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4412       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4413       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4414       .writefn = omap_cachemaint_write },
4415     { .name = "C9", .cp = 15, .crn = 9,
4416       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4417       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4418 };
4419 
4420 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421                               uint64_t value)
4422 {
4423     env->cp15.c15_cpar = value & 0x3fff;
4424 }
4425 
4426 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4427     { .name = "XSCALE_CPAR",
4428       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4429       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4430       .writefn = xscale_cpar_write, },
4431     { .name = "XSCALE_AUXCR",
4432       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4433       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4434       .resetvalue = 0, },
4435     /*
4436      * XScale specific cache-lockdown: since we have no cache we NOP these
4437      * and hope the guest does not really rely on cache behaviour.
4438      */
4439     { .name = "XSCALE_LOCK_ICACHE_LINE",
4440       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4441       .access = PL1_W, .type = ARM_CP_NOP },
4442     { .name = "XSCALE_UNLOCK_ICACHE",
4443       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4444       .access = PL1_W, .type = ARM_CP_NOP },
4445     { .name = "XSCALE_DCACHE_LOCK",
4446       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4447       .access = PL1_RW, .type = ARM_CP_NOP },
4448     { .name = "XSCALE_UNLOCK_DCACHE",
4449       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4450       .access = PL1_W, .type = ARM_CP_NOP },
4451 };
4452 
4453 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4454     /*
4455      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4456      * implementation of this implementation-defined space.
4457      * Ideally this should eventually disappear in favour of actually
4458      * implementing the correct behaviour for all cores.
4459      */
4460     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4461       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4462       .access = PL1_RW,
4463       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4464       .resetvalue = 0 },
4465 };
4466 
4467 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4468     /* Cache status: RAZ because we have no cache so it's always clean */
4469     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4470       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4471       .resetvalue = 0 },
4472 };
4473 
4474 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4475     /* We never have a block transfer operation in progress */
4476     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4477       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4478       .resetvalue = 0 },
4479     /* The cache ops themselves: these all NOP for QEMU */
4480     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4481       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4482     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4483       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4484     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4485       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4486     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4487       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4488     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4489       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4490     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4491       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4492 };
4493 
4494 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4495     /*
4496      * The cache test-and-clean instructions always return (1 << 30)
4497      * to indicate that there are no dirty cache lines.
4498      */
4499     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4500       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4501       .resetvalue = (1 << 30) },
4502     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4503       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4504       .resetvalue = (1 << 30) },
4505 };
4506 
4507 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4508     /* Ignore ReadBuffer accesses */
4509     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4510       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4511       .access = PL1_RW, .resetvalue = 0,
4512       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4513 };
4514 
4515 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4516 {
4517     unsigned int cur_el = arm_current_el(env);
4518 
4519     if (arm_is_el2_enabled(env) && cur_el == 1) {
4520         return env->cp15.vpidr_el2;
4521     }
4522     return raw_read(env, ri);
4523 }
4524 
4525 static uint64_t mpidr_read_val(CPUARMState *env)
4526 {
4527     ARMCPU *cpu = env_archcpu(env);
4528     uint64_t mpidr = cpu->mp_affinity;
4529 
4530     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4531         mpidr |= (1U << 31);
4532         /*
4533          * Cores which are uniprocessor (non-coherent)
4534          * but still implement the MP extensions set
4535          * bit 30. (For instance, Cortex-R5).
4536          */
4537         if (cpu->mp_is_up) {
4538             mpidr |= (1u << 30);
4539         }
4540     }
4541     return mpidr;
4542 }
4543 
4544 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4545 {
4546     unsigned int cur_el = arm_current_el(env);
4547 
4548     if (arm_is_el2_enabled(env) && cur_el == 1) {
4549         return env->cp15.vmpidr_el2;
4550     }
4551     return mpidr_read_val(env);
4552 }
4553 
4554 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4555     /* NOP AMAIR0/1 */
4556     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4557       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4558       .access = PL1_RW, .accessfn = access_tvm_trvm,
4559       .fgt = FGT_AMAIR_EL1,
4560       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4561       .type = ARM_CP_CONST, .resetvalue = 0 },
4562     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4563     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4564       .access = PL1_RW, .accessfn = access_tvm_trvm,
4565       .type = ARM_CP_CONST, .resetvalue = 0 },
4566     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4567       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4568       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4569                              offsetof(CPUARMState, cp15.par_ns)} },
4570     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4571       .access = PL1_RW, .accessfn = access_tvm_trvm,
4572       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4573       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4574                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4575       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4576     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4577       .access = PL1_RW, .accessfn = access_tvm_trvm,
4578       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4579       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4580                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4581       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4582 };
4583 
4584 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4585 {
4586     return vfp_get_fpcr(env);
4587 }
4588 
4589 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4590                             uint64_t value)
4591 {
4592     vfp_set_fpcr(env, value);
4593 }
4594 
4595 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4596 {
4597     return vfp_get_fpsr(env);
4598 }
4599 
4600 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4601                             uint64_t value)
4602 {
4603     vfp_set_fpsr(env, value);
4604 }
4605 
4606 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4607                                        bool isread)
4608 {
4609     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4610         return CP_ACCESS_TRAP;
4611     }
4612     return CP_ACCESS_OK;
4613 }
4614 
4615 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4616                             uint64_t value)
4617 {
4618     env->daif = value & PSTATE_DAIF;
4619 }
4620 
4621 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4622 {
4623     return env->pstate & PSTATE_PAN;
4624 }
4625 
4626 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4627                            uint64_t value)
4628 {
4629     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4630 }
4631 
4632 static const ARMCPRegInfo pan_reginfo = {
4633     .name = "PAN", .state = ARM_CP_STATE_AA64,
4634     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4635     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4636     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4637 };
4638 
4639 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4640 {
4641     return env->pstate & PSTATE_UAO;
4642 }
4643 
4644 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4645                            uint64_t value)
4646 {
4647     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4648 }
4649 
4650 static const ARMCPRegInfo uao_reginfo = {
4651     .name = "UAO", .state = ARM_CP_STATE_AA64,
4652     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4653     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4654     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4655 };
4656 
4657 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4658 {
4659     return env->pstate & PSTATE_DIT;
4660 }
4661 
4662 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4663                            uint64_t value)
4664 {
4665     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4666 }
4667 
4668 static const ARMCPRegInfo dit_reginfo = {
4669     .name = "DIT", .state = ARM_CP_STATE_AA64,
4670     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4671     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4672     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4673 };
4674 
4675 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4676 {
4677     return env->pstate & PSTATE_SSBS;
4678 }
4679 
4680 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4681                            uint64_t value)
4682 {
4683     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4684 }
4685 
4686 static const ARMCPRegInfo ssbs_reginfo = {
4687     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4688     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4689     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4690     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4691 };
4692 
4693 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4694                                               const ARMCPRegInfo *ri,
4695                                               bool isread)
4696 {
4697     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4698     switch (arm_current_el(env)) {
4699     case 0:
4700         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4701         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4702             return CP_ACCESS_TRAP;
4703         }
4704         /* fall through */
4705     case 1:
4706         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4707         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4708             return CP_ACCESS_TRAP_EL2;
4709         }
4710         break;
4711     }
4712     return CP_ACCESS_OK;
4713 }
4714 
4715 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4716 {
4717     /* Cache invalidate/clean to Point of Unification... */
4718     switch (arm_current_el(env)) {
4719     case 0:
4720         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4721         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4722             return CP_ACCESS_TRAP;
4723         }
4724         /* fall through */
4725     case 1:
4726         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4727         if (arm_hcr_el2_eff(env) & hcrflags) {
4728             return CP_ACCESS_TRAP_EL2;
4729         }
4730         break;
4731     }
4732     return CP_ACCESS_OK;
4733 }
4734 
4735 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4736                                    bool isread)
4737 {
4738     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4739 }
4740 
4741 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4742                                   bool isread)
4743 {
4744     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4745 }
4746 
4747 /*
4748  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4749  * Page D4-1736 (DDI0487A.b)
4750  */
4751 
4752 static int vae1_tlbmask(CPUARMState *env)
4753 {
4754     uint64_t hcr = arm_hcr_el2_eff(env);
4755     uint16_t mask;
4756 
4757     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4758         mask = ARMMMUIdxBit_E20_2 |
4759                ARMMMUIdxBit_E20_2_PAN |
4760                ARMMMUIdxBit_E20_0;
4761     } else {
4762         mask = ARMMMUIdxBit_E10_1 |
4763                ARMMMUIdxBit_E10_1_PAN |
4764                ARMMMUIdxBit_E10_0;
4765     }
4766     return mask;
4767 }
4768 
4769 static int vae2_tlbmask(CPUARMState *env)
4770 {
4771     uint64_t hcr = arm_hcr_el2_eff(env);
4772     uint16_t mask;
4773 
4774     if (hcr & HCR_E2H) {
4775         mask = ARMMMUIdxBit_E20_2 |
4776                ARMMMUIdxBit_E20_2_PAN |
4777                ARMMMUIdxBit_E20_0;
4778     } else {
4779         mask = ARMMMUIdxBit_E2;
4780     }
4781     return mask;
4782 }
4783 
4784 /* Return 56 if TBI is enabled, 64 otherwise. */
4785 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4786                               uint64_t addr)
4787 {
4788     uint64_t tcr = regime_tcr(env, mmu_idx);
4789     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4790     int select = extract64(addr, 55, 1);
4791 
4792     return (tbi >> select) & 1 ? 56 : 64;
4793 }
4794 
4795 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4796 {
4797     uint64_t hcr = arm_hcr_el2_eff(env);
4798     ARMMMUIdx mmu_idx;
4799 
4800     /* Only the regime of the mmu_idx below is significant. */
4801     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4802         mmu_idx = ARMMMUIdx_E20_0;
4803     } else {
4804         mmu_idx = ARMMMUIdx_E10_0;
4805     }
4806 
4807     return tlbbits_for_regime(env, mmu_idx, addr);
4808 }
4809 
4810 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4811 {
4812     uint64_t hcr = arm_hcr_el2_eff(env);
4813     ARMMMUIdx mmu_idx;
4814 
4815     /*
4816      * Only the regime of the mmu_idx below is significant.
4817      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4818      * only has one.
4819      */
4820     if (hcr & HCR_E2H) {
4821         mmu_idx = ARMMMUIdx_E20_2;
4822     } else {
4823         mmu_idx = ARMMMUIdx_E2;
4824     }
4825 
4826     return tlbbits_for_regime(env, mmu_idx, addr);
4827 }
4828 
4829 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4830                                       uint64_t value)
4831 {
4832     CPUState *cs = env_cpu(env);
4833     int mask = vae1_tlbmask(env);
4834 
4835     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4836 }
4837 
4838 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4839                                     uint64_t value)
4840 {
4841     CPUState *cs = env_cpu(env);
4842     int mask = vae1_tlbmask(env);
4843 
4844     if (tlb_force_broadcast(env)) {
4845         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4846     } else {
4847         tlb_flush_by_mmuidx(cs, mask);
4848     }
4849 }
4850 
4851 static int e2_tlbmask(CPUARMState *env)
4852 {
4853     return (ARMMMUIdxBit_E20_0 |
4854             ARMMMUIdxBit_E20_2 |
4855             ARMMMUIdxBit_E20_2_PAN |
4856             ARMMMUIdxBit_E2);
4857 }
4858 
4859 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4860                                   uint64_t value)
4861 {
4862     CPUState *cs = env_cpu(env);
4863     int mask = alle1_tlbmask(env);
4864 
4865     tlb_flush_by_mmuidx(cs, mask);
4866 }
4867 
4868 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4869                                   uint64_t value)
4870 {
4871     CPUState *cs = env_cpu(env);
4872     int mask = e2_tlbmask(env);
4873 
4874     tlb_flush_by_mmuidx(cs, mask);
4875 }
4876 
4877 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4878                                   uint64_t value)
4879 {
4880     ARMCPU *cpu = env_archcpu(env);
4881     CPUState *cs = CPU(cpu);
4882 
4883     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4884 }
4885 
4886 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4887                                     uint64_t value)
4888 {
4889     CPUState *cs = env_cpu(env);
4890     int mask = alle1_tlbmask(env);
4891 
4892     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4893 }
4894 
4895 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4896                                     uint64_t value)
4897 {
4898     CPUState *cs = env_cpu(env);
4899     int mask = e2_tlbmask(env);
4900 
4901     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4902 }
4903 
4904 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4905                                     uint64_t value)
4906 {
4907     CPUState *cs = env_cpu(env);
4908 
4909     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4910 }
4911 
4912 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4913                                  uint64_t value)
4914 {
4915     /*
4916      * Invalidate by VA, EL2
4917      * Currently handles both VAE2 and VALE2, since we don't support
4918      * flush-last-level-only.
4919      */
4920     CPUState *cs = env_cpu(env);
4921     int mask = vae2_tlbmask(env);
4922     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4923     int bits = vae2_tlbbits(env, pageaddr);
4924 
4925     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4926 }
4927 
4928 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4929                                  uint64_t value)
4930 {
4931     /*
4932      * Invalidate by VA, EL3
4933      * Currently handles both VAE3 and VALE3, since we don't support
4934      * flush-last-level-only.
4935      */
4936     ARMCPU *cpu = env_archcpu(env);
4937     CPUState *cs = CPU(cpu);
4938     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4939 
4940     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4941 }
4942 
4943 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4944                                    uint64_t value)
4945 {
4946     CPUState *cs = env_cpu(env);
4947     int mask = vae1_tlbmask(env);
4948     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4949     int bits = vae1_tlbbits(env, pageaddr);
4950 
4951     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4952 }
4953 
4954 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4955                                  uint64_t value)
4956 {
4957     /*
4958      * Invalidate by VA, EL1&0 (AArch64 version).
4959      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4960      * since we don't support flush-for-specific-ASID-only or
4961      * flush-last-level-only.
4962      */
4963     CPUState *cs = env_cpu(env);
4964     int mask = vae1_tlbmask(env);
4965     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4966     int bits = vae1_tlbbits(env, pageaddr);
4967 
4968     if (tlb_force_broadcast(env)) {
4969         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4970     } else {
4971         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4972     }
4973 }
4974 
4975 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4976                                    uint64_t value)
4977 {
4978     CPUState *cs = env_cpu(env);
4979     int mask = vae2_tlbmask(env);
4980     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4981     int bits = vae2_tlbbits(env, pageaddr);
4982 
4983     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4984 }
4985 
4986 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4987                                    uint64_t value)
4988 {
4989     CPUState *cs = env_cpu(env);
4990     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4991     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4992 
4993     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4994                                                   ARMMMUIdxBit_E3, bits);
4995 }
4996 
4997 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4998 {
4999     /*
5000      * The MSB of value is the NS field, which only applies if SEL2
5001      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5002      */
5003     return (value >= 0
5004             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5005             && arm_is_secure_below_el3(env)
5006             ? ARMMMUIdxBit_Stage2_S
5007             : ARMMMUIdxBit_Stage2);
5008 }
5009 
5010 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5011                                     uint64_t value)
5012 {
5013     CPUState *cs = env_cpu(env);
5014     int mask = ipas2e1_tlbmask(env, value);
5015     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5016 
5017     if (tlb_force_broadcast(env)) {
5018         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5019     } else {
5020         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5021     }
5022 }
5023 
5024 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5025                                       uint64_t value)
5026 {
5027     CPUState *cs = env_cpu(env);
5028     int mask = ipas2e1_tlbmask(env, value);
5029     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5030 
5031     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5032 }
5033 
5034 #ifdef TARGET_AARCH64
5035 typedef struct {
5036     uint64_t base;
5037     uint64_t length;
5038 } TLBIRange;
5039 
5040 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5041 {
5042     /*
5043      * Note that the TLBI range TG field encoding differs from both
5044      * TG0 and TG1 encodings.
5045      */
5046     switch (tg) {
5047     case 1:
5048         return Gran4K;
5049     case 2:
5050         return Gran16K;
5051     case 3:
5052         return Gran64K;
5053     default:
5054         return GranInvalid;
5055     }
5056 }
5057 
5058 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5059                                      uint64_t value)
5060 {
5061     unsigned int page_size_granule, page_shift, num, scale, exponent;
5062     /* Extract one bit to represent the va selector in use. */
5063     uint64_t select = sextract64(value, 36, 1);
5064     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5065     TLBIRange ret = { };
5066     ARMGranuleSize gran;
5067 
5068     page_size_granule = extract64(value, 46, 2);
5069     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5070 
5071     /* The granule encoded in value must match the granule in use. */
5072     if (gran != param.gran) {
5073         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5074                       page_size_granule);
5075         return ret;
5076     }
5077 
5078     page_shift = arm_granule_bits(gran);
5079     num = extract64(value, 39, 5);
5080     scale = extract64(value, 44, 2);
5081     exponent = (5 * scale) + 1;
5082 
5083     ret.length = (num + 1) << (exponent + page_shift);
5084 
5085     if (param.select) {
5086         ret.base = sextract64(value, 0, 37);
5087     } else {
5088         ret.base = extract64(value, 0, 37);
5089     }
5090     if (param.ds) {
5091         /*
5092          * With DS=1, BaseADDR is always shifted 16 so that it is able
5093          * to address all 52 va bits.  The input address is perforce
5094          * aligned on a 64k boundary regardless of translation granule.
5095          */
5096         page_shift = 16;
5097     }
5098     ret.base <<= page_shift;
5099 
5100     return ret;
5101 }
5102 
5103 static void do_rvae_write(CPUARMState *env, uint64_t value,
5104                           int idxmap, bool synced)
5105 {
5106     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5107     TLBIRange range;
5108     int bits;
5109 
5110     range = tlbi_aa64_get_range(env, one_idx, value);
5111     bits = tlbbits_for_regime(env, one_idx, range.base);
5112 
5113     if (synced) {
5114         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5115                                                   range.base,
5116                                                   range.length,
5117                                                   idxmap,
5118                                                   bits);
5119     } else {
5120         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5121                                   range.length, idxmap, bits);
5122     }
5123 }
5124 
5125 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5126                                   const ARMCPRegInfo *ri,
5127                                   uint64_t value)
5128 {
5129     /*
5130      * Invalidate by VA range, EL1&0.
5131      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5132      * since we don't support flush-for-specific-ASID-only or
5133      * flush-last-level-only.
5134      */
5135 
5136     do_rvae_write(env, value, vae1_tlbmask(env),
5137                   tlb_force_broadcast(env));
5138 }
5139 
5140 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5141                                     const ARMCPRegInfo *ri,
5142                                     uint64_t value)
5143 {
5144     /*
5145      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5146      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5147      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5148      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5149      * shareable specific flushes.
5150      */
5151 
5152     do_rvae_write(env, value, vae1_tlbmask(env), true);
5153 }
5154 
5155 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5156                                   const ARMCPRegInfo *ri,
5157                                   uint64_t value)
5158 {
5159     /*
5160      * Invalidate by VA range, EL2.
5161      * Currently handles all of RVAE2 and RVALE2,
5162      * since we don't support flush-for-specific-ASID-only or
5163      * flush-last-level-only.
5164      */
5165 
5166     do_rvae_write(env, value, vae2_tlbmask(env),
5167                   tlb_force_broadcast(env));
5168 
5169 
5170 }
5171 
5172 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5173                                     const ARMCPRegInfo *ri,
5174                                     uint64_t value)
5175 {
5176     /*
5177      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5178      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5179      * since we don't support flush-for-specific-ASID-only,
5180      * flush-last-level-only or inner/outer shareable specific flushes.
5181      */
5182 
5183     do_rvae_write(env, value, vae2_tlbmask(env), true);
5184 
5185 }
5186 
5187 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5188                                   const ARMCPRegInfo *ri,
5189                                   uint64_t value)
5190 {
5191     /*
5192      * Invalidate by VA range, EL3.
5193      * Currently handles all of RVAE3 and RVALE3,
5194      * since we don't support flush-for-specific-ASID-only or
5195      * flush-last-level-only.
5196      */
5197 
5198     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5199 }
5200 
5201 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5202                                     const ARMCPRegInfo *ri,
5203                                     uint64_t value)
5204 {
5205     /*
5206      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5207      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5208      * since we don't support flush-for-specific-ASID-only,
5209      * flush-last-level-only or inner/outer specific flushes.
5210      */
5211 
5212     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5213 }
5214 
5215 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5216                                      uint64_t value)
5217 {
5218     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5219                   tlb_force_broadcast(env));
5220 }
5221 
5222 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5223                                        const ARMCPRegInfo *ri,
5224                                        uint64_t value)
5225 {
5226     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5227 }
5228 #endif
5229 
5230 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5231                                       bool isread)
5232 {
5233     int cur_el = arm_current_el(env);
5234 
5235     if (cur_el < 2) {
5236         uint64_t hcr = arm_hcr_el2_eff(env);
5237 
5238         if (cur_el == 0) {
5239             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5240                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5241                     return CP_ACCESS_TRAP_EL2;
5242                 }
5243             } else {
5244                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5245                     return CP_ACCESS_TRAP;
5246                 }
5247                 if (hcr & HCR_TDZ) {
5248                     return CP_ACCESS_TRAP_EL2;
5249                 }
5250             }
5251         } else if (hcr & HCR_TDZ) {
5252             return CP_ACCESS_TRAP_EL2;
5253         }
5254     }
5255     return CP_ACCESS_OK;
5256 }
5257 
5258 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5259 {
5260     ARMCPU *cpu = env_archcpu(env);
5261     int dzp_bit = 1 << 4;
5262 
5263     /* DZP indicates whether DC ZVA access is allowed */
5264     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5265         dzp_bit = 0;
5266     }
5267     return cpu->dcz_blocksize | dzp_bit;
5268 }
5269 
5270 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5271                                     bool isread)
5272 {
5273     if (!(env->pstate & PSTATE_SP)) {
5274         /*
5275          * Access to SP_EL0 is undefined if it's being used as
5276          * the stack pointer.
5277          */
5278         return CP_ACCESS_TRAP_UNCATEGORIZED;
5279     }
5280     return CP_ACCESS_OK;
5281 }
5282 
5283 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5284 {
5285     return env->pstate & PSTATE_SP;
5286 }
5287 
5288 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5289 {
5290     update_spsel(env, val);
5291 }
5292 
5293 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5294                         uint64_t value)
5295 {
5296     ARMCPU *cpu = env_archcpu(env);
5297 
5298     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5299         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5300         value &= ~SCTLR_M;
5301     }
5302 
5303     /* ??? Lots of these bits are not implemented.  */
5304 
5305     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5306         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5307             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5308         } else {
5309             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5310                        SCTLR_ATA0 | SCTLR_ATA);
5311         }
5312     }
5313 
5314     if (raw_read(env, ri) == value) {
5315         /*
5316          * Skip the TLB flush if nothing actually changed; Linux likes
5317          * to do a lot of pointless SCTLR writes.
5318          */
5319         return;
5320     }
5321 
5322     raw_write(env, ri, value);
5323 
5324     /* This may enable/disable the MMU, so do a TLB flush.  */
5325     tlb_flush(CPU(cpu));
5326 
5327     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5328         /*
5329          * Normally we would always end the TB on an SCTLR write; see the
5330          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5331          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5332          * of hflags from the translator, so do it here.
5333          */
5334         arm_rebuild_hflags(env);
5335     }
5336 }
5337 
5338 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5339                            uint64_t value)
5340 {
5341     /*
5342      * Some MDCR_EL3 bits affect whether PMU counters are running:
5343      * if we are trying to change any of those then we must
5344      * bracket this update with PMU start/finish calls.
5345      */
5346     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5347 
5348     if (pmu_op) {
5349         pmu_op_start(env);
5350     }
5351     env->cp15.mdcr_el3 = value;
5352     if (pmu_op) {
5353         pmu_op_finish(env);
5354     }
5355 }
5356 
5357 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5358                        uint64_t value)
5359 {
5360     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5361     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5362 }
5363 
5364 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5365                            uint64_t value)
5366 {
5367     /*
5368      * Some MDCR_EL2 bits affect whether PMU counters are running:
5369      * if we are trying to change any of those then we must
5370      * bracket this update with PMU start/finish calls.
5371      */
5372     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5373 
5374     if (pmu_op) {
5375         pmu_op_start(env);
5376     }
5377     env->cp15.mdcr_el2 = value;
5378     if (pmu_op) {
5379         pmu_op_finish(env);
5380     }
5381 }
5382 
5383 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5384                                  bool isread)
5385 {
5386     if (arm_current_el(env) == 1) {
5387         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5388 
5389         if (hcr_nv == (HCR_NV | HCR_NV1)) {
5390             return CP_ACCESS_TRAP_EL2;
5391         }
5392     }
5393     return CP_ACCESS_OK;
5394 }
5395 
5396 #ifdef CONFIG_USER_ONLY
5397 /*
5398  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5399  * code to get around W^X restrictions, where one region is writable and the
5400  * other is executable.
5401  *
5402  * Since the executable region is never written to we cannot detect code
5403  * changes when running in user mode, and rely on the emulated JIT telling us
5404  * that the code has changed by executing this instruction.
5405  */
5406 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5407                           uint64_t value)
5408 {
5409     uint64_t icache_line_mask, start_address, end_address;
5410     const ARMCPU *cpu;
5411 
5412     cpu = env_archcpu(env);
5413 
5414     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5415     start_address = value & ~icache_line_mask;
5416     end_address = value | icache_line_mask;
5417 
5418     mmap_lock();
5419 
5420     tb_invalidate_phys_range(start_address, end_address);
5421 
5422     mmap_unlock();
5423 }
5424 #endif
5425 
5426 static const ARMCPRegInfo v8_cp_reginfo[] = {
5427     /*
5428      * Minimal set of EL0-visible registers. This will need to be expanded
5429      * significantly for system emulation of AArch64 CPUs.
5430      */
5431     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5432       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5433       .access = PL0_RW, .type = ARM_CP_NZCV },
5434     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5435       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5436       .type = ARM_CP_NO_RAW,
5437       .access = PL0_RW, .accessfn = aa64_daif_access,
5438       .fieldoffset = offsetof(CPUARMState, daif),
5439       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5440     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5441       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5442       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5443       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5444     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5445       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5446       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5447       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5448     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5449       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5450       .access = PL0_R, .type = ARM_CP_NO_RAW,
5451       .fgt = FGT_DCZID_EL0,
5452       .readfn = aa64_dczid_read },
5453     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5454       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5455       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5456 #ifndef CONFIG_USER_ONLY
5457       /* Avoid overhead of an access check that always passes in user-mode */
5458       .accessfn = aa64_zva_access,
5459       .fgt = FGT_DCZVA,
5460 #endif
5461     },
5462     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5463       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5464       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5465     /*
5466      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5467      * don't emulate caches.
5468      */
5469     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5470       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5471       .access = PL1_W, .type = ARM_CP_NOP,
5472       .fgt = FGT_ICIALLUIS,
5473       .accessfn = access_ticab },
5474     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5475       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5476       .access = PL1_W, .type = ARM_CP_NOP,
5477       .fgt = FGT_ICIALLU,
5478       .accessfn = access_tocu },
5479     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5481       .access = PL0_W,
5482       .fgt = FGT_ICIVAU,
5483       .accessfn = access_tocu,
5484 #ifdef CONFIG_USER_ONLY
5485       .type = ARM_CP_NO_RAW,
5486       .writefn = ic_ivau_write
5487 #else
5488       .type = ARM_CP_NOP
5489 #endif
5490     },
5491     /* Cache ops: all NOPs since we don't emulate caches */
5492     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5494       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5495       .fgt = FGT_DCIVAC,
5496       .type = ARM_CP_NOP },
5497     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5498       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5499       .fgt = FGT_DCISW,
5500       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5501     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5502       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5503       .access = PL0_W, .type = ARM_CP_NOP,
5504       .fgt = FGT_DCCVAC,
5505       .accessfn = aa64_cacheop_poc_access },
5506     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5507       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5508       .fgt = FGT_DCCSW,
5509       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5510     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5511       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5512       .access = PL0_W, .type = ARM_CP_NOP,
5513       .fgt = FGT_DCCVAU,
5514       .accessfn = access_tocu },
5515     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5516       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5517       .access = PL0_W, .type = ARM_CP_NOP,
5518       .fgt = FGT_DCCIVAC,
5519       .accessfn = aa64_cacheop_poc_access },
5520     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5521       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5522       .fgt = FGT_DCCISW,
5523       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5524     /* TLBI operations */
5525     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5526       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5527       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5528       .fgt = FGT_TLBIVMALLE1IS,
5529       .writefn = tlbi_aa64_vmalle1is_write },
5530     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5531       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5532       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5533       .fgt = FGT_TLBIVAE1IS,
5534       .writefn = tlbi_aa64_vae1is_write },
5535     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5536       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5537       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5538       .fgt = FGT_TLBIASIDE1IS,
5539       .writefn = tlbi_aa64_vmalle1is_write },
5540     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5541       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5542       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5543       .fgt = FGT_TLBIVAAE1IS,
5544       .writefn = tlbi_aa64_vae1is_write },
5545     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5546       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5547       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5548       .fgt = FGT_TLBIVALE1IS,
5549       .writefn = tlbi_aa64_vae1is_write },
5550     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5551       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5552       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5553       .fgt = FGT_TLBIVAALE1IS,
5554       .writefn = tlbi_aa64_vae1is_write },
5555     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5556       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5557       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5558       .fgt = FGT_TLBIVMALLE1,
5559       .writefn = tlbi_aa64_vmalle1_write },
5560     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5561       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5562       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5563       .fgt = FGT_TLBIVAE1,
5564       .writefn = tlbi_aa64_vae1_write },
5565     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5566       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5567       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5568       .fgt = FGT_TLBIASIDE1,
5569       .writefn = tlbi_aa64_vmalle1_write },
5570     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5571       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5572       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5573       .fgt = FGT_TLBIVAAE1,
5574       .writefn = tlbi_aa64_vae1_write },
5575     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5576       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5577       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5578       .fgt = FGT_TLBIVALE1,
5579       .writefn = tlbi_aa64_vae1_write },
5580     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5581       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5582       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5583       .fgt = FGT_TLBIVAALE1,
5584       .writefn = tlbi_aa64_vae1_write },
5585     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5586       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5587       .access = PL2_W, .type = ARM_CP_NO_RAW,
5588       .writefn = tlbi_aa64_ipas2e1is_write },
5589     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5590       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5591       .access = PL2_W, .type = ARM_CP_NO_RAW,
5592       .writefn = tlbi_aa64_ipas2e1is_write },
5593     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5594       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5595       .access = PL2_W, .type = ARM_CP_NO_RAW,
5596       .writefn = tlbi_aa64_alle1is_write },
5597     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5598       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5599       .access = PL2_W, .type = ARM_CP_NO_RAW,
5600       .writefn = tlbi_aa64_alle1is_write },
5601     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5602       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5603       .access = PL2_W, .type = ARM_CP_NO_RAW,
5604       .writefn = tlbi_aa64_ipas2e1_write },
5605     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5606       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5607       .access = PL2_W, .type = ARM_CP_NO_RAW,
5608       .writefn = tlbi_aa64_ipas2e1_write },
5609     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5610       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5611       .access = PL2_W, .type = ARM_CP_NO_RAW,
5612       .writefn = tlbi_aa64_alle1_write },
5613     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5614       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5615       .access = PL2_W, .type = ARM_CP_NO_RAW,
5616       .writefn = tlbi_aa64_alle1is_write },
5617 #ifndef CONFIG_USER_ONLY
5618     /* 64 bit address translation operations */
5619     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5620       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5621       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5622       .fgt = FGT_ATS1E1R,
5623       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5624     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5625       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5626       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5627       .fgt = FGT_ATS1E1W,
5628       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5629     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5630       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5631       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5632       .fgt = FGT_ATS1E0R,
5633       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5634     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5635       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5636       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5637       .fgt = FGT_ATS1E0W,
5638       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5639     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5640       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5641       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5642       .accessfn = at_e012_access, .writefn = ats_write64 },
5643     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5644       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5645       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5646       .accessfn = at_e012_access, .writefn = ats_write64 },
5647     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5648       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5649       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5650       .accessfn = at_e012_access, .writefn = ats_write64 },
5651     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5652       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5653       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5654       .accessfn = at_e012_access, .writefn = ats_write64 },
5655     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5656     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5657       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5658       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5659       .writefn = ats_write64 },
5660     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5661       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5662       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5663       .writefn = ats_write64 },
5664     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5665       .type = ARM_CP_ALIAS,
5666       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5667       .access = PL1_RW, .resetvalue = 0,
5668       .fgt = FGT_PAR_EL1,
5669       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5670       .writefn = par_write },
5671 #endif
5672     /* TLB invalidate last level of translation table walk */
5673     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5674       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5675       .writefn = tlbimva_is_write },
5676     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5677       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5678       .writefn = tlbimvaa_is_write },
5679     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5680       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5681       .writefn = tlbimva_write },
5682     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5683       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5684       .writefn = tlbimvaa_write },
5685     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5686       .type = ARM_CP_NO_RAW, .access = PL2_W,
5687       .writefn = tlbimva_hyp_write },
5688     { .name = "TLBIMVALHIS",
5689       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5690       .type = ARM_CP_NO_RAW, .access = PL2_W,
5691       .writefn = tlbimva_hyp_is_write },
5692     { .name = "TLBIIPAS2",
5693       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5694       .type = ARM_CP_NO_RAW, .access = PL2_W,
5695       .writefn = tlbiipas2_hyp_write },
5696     { .name = "TLBIIPAS2IS",
5697       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5698       .type = ARM_CP_NO_RAW, .access = PL2_W,
5699       .writefn = tlbiipas2is_hyp_write },
5700     { .name = "TLBIIPAS2L",
5701       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5702       .type = ARM_CP_NO_RAW, .access = PL2_W,
5703       .writefn = tlbiipas2_hyp_write },
5704     { .name = "TLBIIPAS2LIS",
5705       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5706       .type = ARM_CP_NO_RAW, .access = PL2_W,
5707       .writefn = tlbiipas2is_hyp_write },
5708     /* 32 bit cache operations */
5709     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5710       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5711     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5712       .type = ARM_CP_NOP, .access = PL1_W },
5713     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5714       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5715     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5716       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5717     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5718       .type = ARM_CP_NOP, .access = PL1_W },
5719     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5720       .type = ARM_CP_NOP, .access = PL1_W },
5721     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5722       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5723     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5724       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5725     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5726       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5727     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5728       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5729     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5730       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5731     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5732       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5733     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5734       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5735     /* MMU Domain access control / MPU write buffer control */
5736     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5737       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5738       .writefn = dacr_write, .raw_writefn = raw_write,
5739       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5740                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5741     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5742       .type = ARM_CP_ALIAS,
5743       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5744       .access = PL1_RW, .accessfn = access_nv1,
5745       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5746       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5747     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5748       .type = ARM_CP_ALIAS,
5749       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5750       .access = PL1_RW, .accessfn = access_nv1,
5751       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5752       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5753     /*
5754      * We rely on the access checks not allowing the guest to write to the
5755      * state field when SPSel indicates that it's being used as the stack
5756      * pointer.
5757      */
5758     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5760       .access = PL1_RW, .accessfn = sp_el0_access,
5761       .type = ARM_CP_ALIAS,
5762       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5763     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5764       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5765       .nv2_redirect_offset = 0x240,
5766       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5767       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5768     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5769       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5770       .type = ARM_CP_NO_RAW,
5771       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5772     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5773       .type = ARM_CP_ALIAS,
5774       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5775       .access = PL2_RW,
5776       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5777     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5778       .type = ARM_CP_ALIAS,
5779       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5780       .access = PL2_RW,
5781       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5782     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5783       .type = ARM_CP_ALIAS,
5784       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5785       .access = PL2_RW,
5786       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5787     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5788       .type = ARM_CP_ALIAS,
5789       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5790       .access = PL2_RW,
5791       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5792     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5793       .type = ARM_CP_IO,
5794       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5795       .resetvalue = 0,
5796       .access = PL3_RW,
5797       .writefn = mdcr_el3_write,
5798       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5799     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5800       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5801       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5802       .writefn = sdcr_write,
5803       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5804 };
5805 
5806 /* These are present only when EL1 supports AArch32 */
5807 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5808     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5809       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5810       .access = PL2_RW,
5811       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5812       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5813     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5814       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5815       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5816       .writefn = dacr_write, .raw_writefn = raw_write,
5817       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5818     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5819       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5820       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5821       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5822 };
5823 
5824 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5825 {
5826     ARMCPU *cpu = env_archcpu(env);
5827 
5828     if (arm_feature(env, ARM_FEATURE_V8)) {
5829         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5830     } else {
5831         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5832     }
5833 
5834     if (arm_feature(env, ARM_FEATURE_EL3)) {
5835         valid_mask &= ~HCR_HCD;
5836     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5837         /*
5838          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5839          * However, if we're using the SMC PSCI conduit then QEMU is
5840          * effectively acting like EL3 firmware and so the guest at
5841          * EL2 should retain the ability to prevent EL1 from being
5842          * able to make SMC calls into the ersatz firmware, so in
5843          * that case HCR.TSC should be read/write.
5844          */
5845         valid_mask &= ~HCR_TSC;
5846     }
5847 
5848     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5849         if (cpu_isar_feature(aa64_vh, cpu)) {
5850             valid_mask |= HCR_E2H;
5851         }
5852         if (cpu_isar_feature(aa64_ras, cpu)) {
5853             valid_mask |= HCR_TERR | HCR_TEA;
5854         }
5855         if (cpu_isar_feature(aa64_lor, cpu)) {
5856             valid_mask |= HCR_TLOR;
5857         }
5858         if (cpu_isar_feature(aa64_pauth, cpu)) {
5859             valid_mask |= HCR_API | HCR_APK;
5860         }
5861         if (cpu_isar_feature(aa64_mte, cpu)) {
5862             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5863         }
5864         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5865             valid_mask |= HCR_ENSCXT;
5866         }
5867         if (cpu_isar_feature(aa64_fwb, cpu)) {
5868             valid_mask |= HCR_FWB;
5869         }
5870         if (cpu_isar_feature(aa64_rme, cpu)) {
5871             valid_mask |= HCR_GPF;
5872         }
5873         if (cpu_isar_feature(aa64_nv, cpu)) {
5874             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5875         }
5876         if (cpu_isar_feature(aa64_nv2, cpu)) {
5877             valid_mask |= HCR_NV2;
5878         }
5879     }
5880 
5881     if (cpu_isar_feature(any_evt, cpu)) {
5882         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5883     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5884         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5885     }
5886 
5887     /* Clear RES0 bits.  */
5888     value &= valid_mask;
5889 
5890     /*
5891      * These bits change the MMU setup:
5892      * HCR_VM enables stage 2 translation
5893      * HCR_PTW forbids certain page-table setups
5894      * HCR_DC disables stage1 and enables stage2 translation
5895      * HCR_DCT enables tagging on (disabled) stage1 translation
5896      * HCR_FWB changes the interpretation of stage2 descriptor bits
5897      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5898      */
5899     if ((env->cp15.hcr_el2 ^ value) &
5900         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5901         tlb_flush(CPU(cpu));
5902     }
5903     env->cp15.hcr_el2 = value;
5904 
5905     /*
5906      * Updates to VI and VF require us to update the status of
5907      * virtual interrupts, which are the logical OR of these bits
5908      * and the state of the input lines from the GIC. (This requires
5909      * that we have the BQL, which is done by marking the
5910      * reginfo structs as ARM_CP_IO.)
5911      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5912      * possible for it to be taken immediately, because VIRQ and
5913      * VFIQ are masked unless running at EL0 or EL1, and HCR
5914      * can only be written at EL2.
5915      */
5916     g_assert(bql_locked());
5917     arm_cpu_update_virq(cpu);
5918     arm_cpu_update_vfiq(cpu);
5919     arm_cpu_update_vserr(cpu);
5920 }
5921 
5922 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5923 {
5924     do_hcr_write(env, value, 0);
5925 }
5926 
5927 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5928                           uint64_t value)
5929 {
5930     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5931     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5932     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5933 }
5934 
5935 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5936                          uint64_t value)
5937 {
5938     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5939     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5940     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5941 }
5942 
5943 /*
5944  * Return the effective value of HCR_EL2, at the given security state.
5945  * Bits that are not included here:
5946  * RW       (read from SCR_EL3.RW as needed)
5947  */
5948 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5949 {
5950     uint64_t ret = env->cp15.hcr_el2;
5951 
5952     assert(space != ARMSS_Root);
5953 
5954     if (!arm_is_el2_enabled_secstate(env, space)) {
5955         /*
5956          * "This register has no effect if EL2 is not enabled in the
5957          * current Security state".  This is ARMv8.4-SecEL2 speak for
5958          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5959          *
5960          * Prior to that, the language was "In an implementation that
5961          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5962          * as if this field is 0 for all purposes other than a direct
5963          * read or write access of HCR_EL2".  With lots of enumeration
5964          * on a per-field basis.  In current QEMU, this is condition
5965          * is arm_is_secure_below_el3.
5966          *
5967          * Since the v8.4 language applies to the entire register, and
5968          * appears to be backward compatible, use that.
5969          */
5970         return 0;
5971     }
5972 
5973     /*
5974      * For a cpu that supports both aarch64 and aarch32, we can set bits
5975      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5976      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5977      */
5978     if (!arm_el_is_aa64(env, 2)) {
5979         uint64_t aa32_valid;
5980 
5981         /*
5982          * These bits are up-to-date as of ARMv8.6.
5983          * For HCR, it's easiest to list just the 2 bits that are invalid.
5984          * For HCR2, list those that are valid.
5985          */
5986         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5987         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5988                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5989         ret &= aa32_valid;
5990     }
5991 
5992     if (ret & HCR_TGE) {
5993         /* These bits are up-to-date as of ARMv8.6.  */
5994         if (ret & HCR_E2H) {
5995             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5996                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5997                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5998                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5999                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6000                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6001         } else {
6002             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6003         }
6004         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6005                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6006                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6007                  HCR_TLOR);
6008     }
6009 
6010     return ret;
6011 }
6012 
6013 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6014 {
6015     if (arm_feature(env, ARM_FEATURE_M)) {
6016         return 0;
6017     }
6018     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6019 }
6020 
6021 /*
6022  * Corresponds to ARM pseudocode function ELIsInHost().
6023  */
6024 bool el_is_in_host(CPUARMState *env, int el)
6025 {
6026     uint64_t mask;
6027 
6028     /*
6029      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6030      * Perform the simplest bit tests first, and validate EL2 afterward.
6031      */
6032     if (el & 1) {
6033         return false; /* EL1 or EL3 */
6034     }
6035 
6036     /*
6037      * Note that hcr_write() checks isar_feature_aa64_vh(),
6038      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6039      */
6040     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6041     if ((env->cp15.hcr_el2 & mask) != mask) {
6042         return false;
6043     }
6044 
6045     /* TGE and/or E2H set: double check those bits are currently legal. */
6046     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6047 }
6048 
6049 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6050                        uint64_t value)
6051 {
6052     uint64_t valid_mask = 0;
6053 
6054     /* FEAT_MOPS adds MSCEn and MCE2 */
6055     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6056         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6057     }
6058 
6059     /* Clear RES0 bits.  */
6060     env->cp15.hcrx_el2 = value & valid_mask;
6061 }
6062 
6063 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6064                                   bool isread)
6065 {
6066     if (arm_current_el(env) == 2
6067         && arm_feature(env, ARM_FEATURE_EL3)
6068         && !(env->cp15.scr_el3 & SCR_HXEN)) {
6069         return CP_ACCESS_TRAP_EL3;
6070     }
6071     return CP_ACCESS_OK;
6072 }
6073 
6074 static const ARMCPRegInfo hcrx_el2_reginfo = {
6075     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6076     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6077     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6078     .nv2_redirect_offset = 0xa0,
6079     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6080 };
6081 
6082 /* Return the effective value of HCRX_EL2.  */
6083 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6084 {
6085     /*
6086      * The bits in this register behave as 0 for all purposes other than
6087      * direct reads of the register if SCR_EL3.HXEn is 0.
6088      * If EL2 is not enabled in the current security state, then the
6089      * bit may behave as if 0, or as if 1, depending on the bit.
6090      * For the moment, we treat the EL2-disabled case as taking
6091      * priority over the HXEn-disabled case. This is true for the only
6092      * bit for a feature which we implement where the answer is different
6093      * for the two cases (MSCEn for FEAT_MOPS).
6094      * This may need to be revisited for future bits.
6095      */
6096     if (!arm_is_el2_enabled(env)) {
6097         uint64_t hcrx = 0;
6098         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6099             /* MSCEn behaves as 1 if EL2 is not enabled */
6100             hcrx |= HCRX_MSCEN;
6101         }
6102         return hcrx;
6103     }
6104     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6105         return 0;
6106     }
6107     return env->cp15.hcrx_el2;
6108 }
6109 
6110 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6111                            uint64_t value)
6112 {
6113     /*
6114      * For A-profile AArch32 EL3, if NSACR.CP10
6115      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6116      */
6117     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6118         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6119         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6120         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6121     }
6122     env->cp15.cptr_el[2] = value;
6123 }
6124 
6125 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6126 {
6127     /*
6128      * For A-profile AArch32 EL3, if NSACR.CP10
6129      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6130      */
6131     uint64_t value = env->cp15.cptr_el[2];
6132 
6133     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6134         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6135         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6136     }
6137     return value;
6138 }
6139 
6140 static const ARMCPRegInfo el2_cp_reginfo[] = {
6141     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6142       .type = ARM_CP_IO,
6143       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6144       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6145       .nv2_redirect_offset = 0x78,
6146       .writefn = hcr_write, .raw_writefn = raw_write },
6147     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6148       .type = ARM_CP_ALIAS | ARM_CP_IO,
6149       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6150       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6151       .writefn = hcr_writelow },
6152     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6153       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6154       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6155     { .name = "ELR_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 = 1,
6158       .access = PL2_RW,
6159       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6160     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6161       .type = ARM_CP_NV2_REDIRECT,
6162       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6163       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6164     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6165       .type = ARM_CP_NV2_REDIRECT,
6166       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6167       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6168     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6169       .type = ARM_CP_ALIAS,
6170       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6171       .access = PL2_RW,
6172       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6173     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6174       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6175       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6176       .access = PL2_RW,
6177       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6178     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6179       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6180       .access = PL2_RW, .writefn = vbar_write,
6181       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6182       .resetvalue = 0 },
6183     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6184       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6185       .access = PL3_RW, .type = ARM_CP_ALIAS,
6186       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6187     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6188       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6189       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6190       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6191       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6192     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6193       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6194       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6195       .resetvalue = 0 },
6196     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6197       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6198       .access = PL2_RW, .type = ARM_CP_ALIAS,
6199       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6200     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6201       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6202       .access = PL2_RW, .type = ARM_CP_CONST,
6203       .resetvalue = 0 },
6204     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6205     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6206       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6207       .access = PL2_RW, .type = ARM_CP_CONST,
6208       .resetvalue = 0 },
6209     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6210       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6211       .access = PL2_RW, .type = ARM_CP_CONST,
6212       .resetvalue = 0 },
6213     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6214       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6215       .access = PL2_RW, .type = ARM_CP_CONST,
6216       .resetvalue = 0 },
6217     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6218       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6219       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6220       .raw_writefn = raw_write,
6221       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6222     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6223       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6224       .type = ARM_CP_ALIAS,
6225       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6226       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6227     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6228       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6229       .access = PL2_RW,
6230       .nv2_redirect_offset = 0x40,
6231       /* no .writefn needed as this can't cause an ASID change */
6232       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6233     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6234       .cp = 15, .opc1 = 6, .crm = 2,
6235       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6236       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6237       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6238       .writefn = vttbr_write, .raw_writefn = raw_write },
6239     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6240       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6241       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6242       .nv2_redirect_offset = 0x20,
6243       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6244     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6245       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6246       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6247       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6248     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6249       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6250       .access = PL2_RW, .resetvalue = 0,
6251       .nv2_redirect_offset = 0x90,
6252       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6253     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6254       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6255       .access = PL2_RW, .resetvalue = 0,
6256       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6257       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6258     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6259       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6260       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6261     { .name = "TLBIALLNSNH",
6262       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6263       .type = ARM_CP_NO_RAW, .access = PL2_W,
6264       .writefn = tlbiall_nsnh_write },
6265     { .name = "TLBIALLNSNHIS",
6266       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6267       .type = ARM_CP_NO_RAW, .access = PL2_W,
6268       .writefn = tlbiall_nsnh_is_write },
6269     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6270       .type = ARM_CP_NO_RAW, .access = PL2_W,
6271       .writefn = tlbiall_hyp_write },
6272     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6273       .type = ARM_CP_NO_RAW, .access = PL2_W,
6274       .writefn = tlbiall_hyp_is_write },
6275     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6276       .type = ARM_CP_NO_RAW, .access = PL2_W,
6277       .writefn = tlbimva_hyp_write },
6278     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6279       .type = ARM_CP_NO_RAW, .access = PL2_W,
6280       .writefn = tlbimva_hyp_is_write },
6281     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6282       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6283       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6284       .writefn = tlbi_aa64_alle2_write },
6285     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6286       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6287       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6288       .writefn = tlbi_aa64_vae2_write },
6289     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6290       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6291       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6292       .writefn = tlbi_aa64_vae2_write },
6293     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6294       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6295       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6296       .writefn = tlbi_aa64_alle2is_write },
6297     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6298       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6299       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6300       .writefn = tlbi_aa64_vae2is_write },
6301     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6302       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6303       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6304       .writefn = tlbi_aa64_vae2is_write },
6305 #ifndef CONFIG_USER_ONLY
6306     /*
6307      * Unlike the other EL2-related AT operations, these must
6308      * UNDEF from EL3 if EL2 is not implemented, which is why we
6309      * define them here rather than with the rest of the AT ops.
6310      */
6311     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6312       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6313       .access = PL2_W, .accessfn = at_s1e2_access,
6314       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6315       .writefn = ats_write64 },
6316     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6317       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6318       .access = PL2_W, .accessfn = at_s1e2_access,
6319       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6320       .writefn = ats_write64 },
6321     /*
6322      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6323      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6324      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6325      * to behave as if SCR.NS was 1.
6326      */
6327     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6328       .access = PL2_W,
6329       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6330     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6331       .access = PL2_W,
6332       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6333     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6334       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6335       /*
6336        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6337        * reset values as IMPDEF. We choose to reset to 3 to comply with
6338        * both ARMv7 and ARMv8.
6339        */
6340       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6341       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6342       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6343     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6344       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6345       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6346       .writefn = gt_cntvoff_write,
6347       .nv2_redirect_offset = 0x60,
6348       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6349     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6350       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6351       .writefn = gt_cntvoff_write,
6352       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6353     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6354       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6355       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6356       .type = ARM_CP_IO, .access = PL2_RW,
6357       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6358     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6359       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6360       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6361       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6362     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6363       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6364       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6365       .resetfn = gt_hyp_timer_reset,
6366       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6367     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6368       .type = ARM_CP_IO,
6369       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6370       .access = PL2_RW,
6371       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6372       .resetvalue = 0,
6373       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6374 #endif
6375     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6376       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6377       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6378       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6379     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6380       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6381       .access = PL2_RW,
6382       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6383     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6384       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6385       .access = PL2_RW,
6386       .nv2_redirect_offset = 0x80,
6387       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6388 };
6389 
6390 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6391     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6392       .type = ARM_CP_ALIAS | ARM_CP_IO,
6393       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6394       .access = PL2_RW,
6395       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6396       .writefn = hcr_writehigh },
6397 };
6398 
6399 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6400                                   bool isread)
6401 {
6402     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6403         return CP_ACCESS_OK;
6404     }
6405     return CP_ACCESS_TRAP_UNCATEGORIZED;
6406 }
6407 
6408 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6409     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6410       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6411       .access = PL2_RW, .accessfn = sel2_access,
6412       .nv2_redirect_offset = 0x30,
6413       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6414     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6415       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6416       .access = PL2_RW, .accessfn = sel2_access,
6417       .nv2_redirect_offset = 0x48,
6418       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6419 };
6420 
6421 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6422                                    bool isread)
6423 {
6424     /*
6425      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6426      * At Secure EL1 it traps to EL3 or EL2.
6427      */
6428     if (arm_current_el(env) == 3) {
6429         return CP_ACCESS_OK;
6430     }
6431     if (arm_is_secure_below_el3(env)) {
6432         if (env->cp15.scr_el3 & SCR_EEL2) {
6433             return CP_ACCESS_TRAP_EL2;
6434         }
6435         return CP_ACCESS_TRAP_EL3;
6436     }
6437     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6438     if (isread) {
6439         return CP_ACCESS_OK;
6440     }
6441     return CP_ACCESS_TRAP_UNCATEGORIZED;
6442 }
6443 
6444 static const ARMCPRegInfo el3_cp_reginfo[] = {
6445     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6446       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6447       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6448       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6449     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6450       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6451       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6452       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6453       .writefn = scr_write, .raw_writefn = raw_write },
6454     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6455       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6456       .access = PL3_RW, .resetvalue = 0,
6457       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6458     { .name = "SDER",
6459       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6460       .access = PL3_RW, .resetvalue = 0,
6461       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6462     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6463       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6464       .writefn = vbar_write, .resetvalue = 0,
6465       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6466     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6467       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6468       .access = PL3_RW, .resetvalue = 0,
6469       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6470     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6471       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6472       .access = PL3_RW,
6473       /* no .writefn needed as this can't cause an ASID change */
6474       .resetvalue = 0,
6475       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6476     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6477       .type = ARM_CP_ALIAS,
6478       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6479       .access = PL3_RW,
6480       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6481     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6482       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6483       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6484     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6485       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6486       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6487     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6488       .type = ARM_CP_ALIAS,
6489       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6490       .access = PL3_RW,
6491       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6492     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6493       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6494       .access = PL3_RW, .writefn = vbar_write,
6495       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6496       .resetvalue = 0 },
6497     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6498       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6499       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6500       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6501     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6502       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6503       .access = PL3_RW, .resetvalue = 0,
6504       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6505     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6506       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6507       .access = PL3_RW, .type = ARM_CP_CONST,
6508       .resetvalue = 0 },
6509     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6510       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6511       .access = PL3_RW, .type = ARM_CP_CONST,
6512       .resetvalue = 0 },
6513     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6514       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6515       .access = PL3_RW, .type = ARM_CP_CONST,
6516       .resetvalue = 0 },
6517     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6518       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6519       .access = PL3_W, .type = ARM_CP_NO_RAW,
6520       .writefn = tlbi_aa64_alle3is_write },
6521     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6522       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6523       .access = PL3_W, .type = ARM_CP_NO_RAW,
6524       .writefn = tlbi_aa64_vae3is_write },
6525     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6526       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6527       .access = PL3_W, .type = ARM_CP_NO_RAW,
6528       .writefn = tlbi_aa64_vae3is_write },
6529     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6530       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6531       .access = PL3_W, .type = ARM_CP_NO_RAW,
6532       .writefn = tlbi_aa64_alle3_write },
6533     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6534       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6535       .access = PL3_W, .type = ARM_CP_NO_RAW,
6536       .writefn = tlbi_aa64_vae3_write },
6537     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6538       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6539       .access = PL3_W, .type = ARM_CP_NO_RAW,
6540       .writefn = tlbi_aa64_vae3_write },
6541 };
6542 
6543 #ifndef CONFIG_USER_ONLY
6544 
6545 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6546                                  bool isread)
6547 {
6548     if (arm_current_el(env) == 1) {
6549         /* This must be a FEAT_NV access */
6550         /* TODO: FEAT_ECV will need to check CNTHCTL_EL2 here */
6551         return CP_ACCESS_OK;
6552     }
6553     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6554         return CP_ACCESS_TRAP;
6555     }
6556     return CP_ACCESS_OK;
6557 }
6558 
6559 /* Test if system register redirection is to occur in the current state.  */
6560 static bool redirect_for_e2h(CPUARMState *env)
6561 {
6562     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6563 }
6564 
6565 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6566 {
6567     CPReadFn *readfn;
6568 
6569     if (redirect_for_e2h(env)) {
6570         /* Switch to the saved EL2 version of the register.  */
6571         ri = ri->opaque;
6572         readfn = ri->readfn;
6573     } else {
6574         readfn = ri->orig_readfn;
6575     }
6576     if (readfn == NULL) {
6577         readfn = raw_read;
6578     }
6579     return readfn(env, ri);
6580 }
6581 
6582 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6583                           uint64_t value)
6584 {
6585     CPWriteFn *writefn;
6586 
6587     if (redirect_for_e2h(env)) {
6588         /* Switch to the saved EL2 version of the register.  */
6589         ri = ri->opaque;
6590         writefn = ri->writefn;
6591     } else {
6592         writefn = ri->orig_writefn;
6593     }
6594     if (writefn == NULL) {
6595         writefn = raw_write;
6596     }
6597     writefn(env, ri, value);
6598 }
6599 
6600 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6601 {
6602     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6603     return ri->orig_readfn(env, ri->opaque);
6604 }
6605 
6606 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6607                               uint64_t value)
6608 {
6609     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6610     return ri->orig_writefn(env, ri->opaque, value);
6611 }
6612 
6613 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6614                                          const ARMCPRegInfo *ri,
6615                                          bool isread)
6616 {
6617     if (arm_current_el(env) == 1) {
6618         /*
6619          * This must be a FEAT_NV access (will either trap or redirect
6620          * to memory). None of the registers with _EL12 aliases want to
6621          * apply their trap controls for this kind of access, so don't
6622          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6623          */
6624         return CP_ACCESS_OK;
6625     }
6626     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6627     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6628         return CP_ACCESS_TRAP_UNCATEGORIZED;
6629     }
6630     if (ri->orig_accessfn) {
6631         return ri->orig_accessfn(env, ri->opaque, isread);
6632     }
6633     return CP_ACCESS_OK;
6634 }
6635 
6636 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6637 {
6638     struct E2HAlias {
6639         uint32_t src_key, dst_key, new_key;
6640         const char *src_name, *dst_name, *new_name;
6641         bool (*feature)(const ARMISARegisters *id);
6642     };
6643 
6644 #define K(op0, op1, crn, crm, op2) \
6645     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6646 
6647     static const struct E2HAlias aliases[] = {
6648         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6649           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6650         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6651           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6652         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6653           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6654         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6655           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6656         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6657           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6658         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6659           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6660         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6661           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6662         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6663           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6664         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6665           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6666         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6667           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6668         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6669           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6670         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6671           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6672         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6673           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6674         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6675           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6676         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6677           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6678         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6679           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6680 
6681         /*
6682          * Note that redirection of ZCR is mentioned in the description
6683          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6684          * not in the summary table.
6685          */
6686         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6687           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6688         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6689           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6690 
6691         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6692           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6693 
6694         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6695           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6696           isar_feature_aa64_scxtnum },
6697 
6698         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6699         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6700     };
6701 #undef K
6702 
6703     size_t i;
6704 
6705     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6706         const struct E2HAlias *a = &aliases[i];
6707         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6708         bool ok;
6709 
6710         if (a->feature && !a->feature(&cpu->isar)) {
6711             continue;
6712         }
6713 
6714         src_reg = g_hash_table_lookup(cpu->cp_regs,
6715                                       (gpointer)(uintptr_t)a->src_key);
6716         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6717                                       (gpointer)(uintptr_t)a->dst_key);
6718         g_assert(src_reg != NULL);
6719         g_assert(dst_reg != NULL);
6720 
6721         /* Cross-compare names to detect typos in the keys.  */
6722         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6723         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6724 
6725         /* None of the core system registers use opaque; we will.  */
6726         g_assert(src_reg->opaque == NULL);
6727 
6728         /* Create alias before redirection so we dup the right data. */
6729         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6730 
6731         new_reg->name = a->new_name;
6732         new_reg->type |= ARM_CP_ALIAS;
6733         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6734         new_reg->access &= PL2_RW | PL3_RW;
6735         /* The new_reg op fields are as per new_key, not the target reg */
6736         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6737             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6738         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6739             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6740         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6741             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6742         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6743             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6744         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6745             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6746         new_reg->opaque = src_reg;
6747         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6748         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6749         new_reg->orig_accessfn = src_reg->accessfn;
6750         if (!new_reg->raw_readfn) {
6751             new_reg->raw_readfn = raw_read;
6752         }
6753         if (!new_reg->raw_writefn) {
6754             new_reg->raw_writefn = raw_write;
6755         }
6756         new_reg->readfn = el2_e2h_e12_read;
6757         new_reg->writefn = el2_e2h_e12_write;
6758         new_reg->accessfn = el2_e2h_e12_access;
6759 
6760         /*
6761          * If the _EL1 register is redirected to memory by FEAT_NV2,
6762          * then it shares the offset with the _EL12 register,
6763          * and which one is redirected depends on HCR_EL2.NV1.
6764          */
6765         if (new_reg->nv2_redirect_offset) {
6766             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6767             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6768             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6769         }
6770 
6771         ok = g_hash_table_insert(cpu->cp_regs,
6772                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6773         g_assert(ok);
6774 
6775         src_reg->opaque = dst_reg;
6776         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6777         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6778         if (!src_reg->raw_readfn) {
6779             src_reg->raw_readfn = raw_read;
6780         }
6781         if (!src_reg->raw_writefn) {
6782             src_reg->raw_writefn = raw_write;
6783         }
6784         src_reg->readfn = el2_e2h_read;
6785         src_reg->writefn = el2_e2h_write;
6786     }
6787 }
6788 #endif
6789 
6790 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6791                                      bool isread)
6792 {
6793     int cur_el = arm_current_el(env);
6794 
6795     if (cur_el < 2) {
6796         uint64_t hcr = arm_hcr_el2_eff(env);
6797 
6798         if (cur_el == 0) {
6799             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6800                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6801                     return CP_ACCESS_TRAP_EL2;
6802                 }
6803             } else {
6804                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6805                     return CP_ACCESS_TRAP;
6806                 }
6807                 if (hcr & HCR_TID2) {
6808                     return CP_ACCESS_TRAP_EL2;
6809                 }
6810             }
6811         } else if (hcr & HCR_TID2) {
6812             return CP_ACCESS_TRAP_EL2;
6813         }
6814     }
6815 
6816     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6817         return CP_ACCESS_TRAP_EL2;
6818     }
6819 
6820     return CP_ACCESS_OK;
6821 }
6822 
6823 /*
6824  * Check for traps to RAS registers, which are controlled
6825  * by HCR_EL2.TERR and SCR_EL3.TERR.
6826  */
6827 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6828                                   bool isread)
6829 {
6830     int el = arm_current_el(env);
6831 
6832     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6833         return CP_ACCESS_TRAP_EL2;
6834     }
6835     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6836         return CP_ACCESS_TRAP_EL3;
6837     }
6838     return CP_ACCESS_OK;
6839 }
6840 
6841 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6842 {
6843     int el = arm_current_el(env);
6844 
6845     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6846         return env->cp15.vdisr_el2;
6847     }
6848     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6849         return 0; /* RAZ/WI */
6850     }
6851     return env->cp15.disr_el1;
6852 }
6853 
6854 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6855 {
6856     int el = arm_current_el(env);
6857 
6858     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6859         env->cp15.vdisr_el2 = val;
6860         return;
6861     }
6862     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6863         return; /* RAZ/WI */
6864     }
6865     env->cp15.disr_el1 = val;
6866 }
6867 
6868 /*
6869  * Minimal RAS implementation with no Error Records.
6870  * Which means that all of the Error Record registers:
6871  *   ERXADDR_EL1
6872  *   ERXCTLR_EL1
6873  *   ERXFR_EL1
6874  *   ERXMISC0_EL1
6875  *   ERXMISC1_EL1
6876  *   ERXMISC2_EL1
6877  *   ERXMISC3_EL1
6878  *   ERXPFGCDN_EL1  (RASv1p1)
6879  *   ERXPFGCTL_EL1  (RASv1p1)
6880  *   ERXPFGF_EL1    (RASv1p1)
6881  *   ERXSTATUS_EL1
6882  * and
6883  *   ERRSELR_EL1
6884  * may generate UNDEFINED, which is the effect we get by not
6885  * listing them at all.
6886  *
6887  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6888  * is higher priority than FGT-to-EL2 so we do not need to list them
6889  * in order to check for an FGT.
6890  */
6891 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6892     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6893       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6894       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6895       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6896     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6897       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6898       .access = PL1_R, .accessfn = access_terr,
6899       .fgt = FGT_ERRIDR_EL1,
6900       .type = ARM_CP_CONST, .resetvalue = 0 },
6901     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6902       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6903       .nv2_redirect_offset = 0x500,
6904       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6905     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6906       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6907       .nv2_redirect_offset = 0x508,
6908       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6909 };
6910 
6911 /*
6912  * Return the exception level to which exceptions should be taken
6913  * via SVEAccessTrap.  This excludes the check for whether the exception
6914  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6915  * be found by testing 0 < fp_exception_el < sve_exception_el.
6916  *
6917  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6918  * pseudocode does *not* separate out the FP trap checks, but has them
6919  * all in one function.
6920  */
6921 int sve_exception_el(CPUARMState *env, int el)
6922 {
6923 #ifndef CONFIG_USER_ONLY
6924     if (el <= 1 && !el_is_in_host(env, el)) {
6925         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6926         case 1:
6927             if (el != 0) {
6928                 break;
6929             }
6930             /* fall through */
6931         case 0:
6932         case 2:
6933             return 1;
6934         }
6935     }
6936 
6937     if (el <= 2 && arm_is_el2_enabled(env)) {
6938         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6939         if (env->cp15.hcr_el2 & HCR_E2H) {
6940             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6941             case 1:
6942                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6943                     break;
6944                 }
6945                 /* fall through */
6946             case 0:
6947             case 2:
6948                 return 2;
6949             }
6950         } else {
6951             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6952                 return 2;
6953             }
6954         }
6955     }
6956 
6957     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6958     if (arm_feature(env, ARM_FEATURE_EL3)
6959         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6960         return 3;
6961     }
6962 #endif
6963     return 0;
6964 }
6965 
6966 /*
6967  * Return the exception level to which exceptions should be taken for SME.
6968  * C.f. the ARM pseudocode function CheckSMEAccess.
6969  */
6970 int sme_exception_el(CPUARMState *env, int el)
6971 {
6972 #ifndef CONFIG_USER_ONLY
6973     if (el <= 1 && !el_is_in_host(env, el)) {
6974         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6975         case 1:
6976             if (el != 0) {
6977                 break;
6978             }
6979             /* fall through */
6980         case 0:
6981         case 2:
6982             return 1;
6983         }
6984     }
6985 
6986     if (el <= 2 && arm_is_el2_enabled(env)) {
6987         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6988         if (env->cp15.hcr_el2 & HCR_E2H) {
6989             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6990             case 1:
6991                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6992                     break;
6993                 }
6994                 /* fall through */
6995             case 0:
6996             case 2:
6997                 return 2;
6998             }
6999         } else {
7000             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7001                 return 2;
7002             }
7003         }
7004     }
7005 
7006     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
7007     if (arm_feature(env, ARM_FEATURE_EL3)
7008         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7009         return 3;
7010     }
7011 #endif
7012     return 0;
7013 }
7014 
7015 /*
7016  * Given that SVE is enabled, return the vector length for EL.
7017  */
7018 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7019 {
7020     ARMCPU *cpu = env_archcpu(env);
7021     uint64_t *cr = env->vfp.zcr_el;
7022     uint32_t map = cpu->sve_vq.map;
7023     uint32_t len = ARM_MAX_VQ - 1;
7024 
7025     if (sm) {
7026         cr = env->vfp.smcr_el;
7027         map = cpu->sme_vq.map;
7028     }
7029 
7030     if (el <= 1 && !el_is_in_host(env, el)) {
7031         len = MIN(len, 0xf & (uint32_t)cr[1]);
7032     }
7033     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
7034         len = MIN(len, 0xf & (uint32_t)cr[2]);
7035     }
7036     if (arm_feature(env, ARM_FEATURE_EL3)) {
7037         len = MIN(len, 0xf & (uint32_t)cr[3]);
7038     }
7039 
7040     map &= MAKE_64BIT_MASK(0, len + 1);
7041     if (map != 0) {
7042         return 31 - clz32(map);
7043     }
7044 
7045     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7046     assert(sm);
7047     return ctz32(cpu->sme_vq.map);
7048 }
7049 
7050 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7051 {
7052     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7053 }
7054 
7055 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7056                       uint64_t value)
7057 {
7058     int cur_el = arm_current_el(env);
7059     int old_len = sve_vqm1_for_el(env, cur_el);
7060     int new_len;
7061 
7062     /* Bits other than [3:0] are RAZ/WI.  */
7063     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7064     raw_write(env, ri, value & 0xf);
7065 
7066     /*
7067      * Because we arrived here, we know both FP and SVE are enabled;
7068      * otherwise we would have trapped access to the ZCR_ELn register.
7069      */
7070     new_len = sve_vqm1_for_el(env, cur_el);
7071     if (new_len < old_len) {
7072         aarch64_sve_narrow_vq(env, new_len + 1);
7073     }
7074 }
7075 
7076 static const ARMCPRegInfo zcr_reginfo[] = {
7077     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7078       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7079       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7080       .access = PL1_RW, .type = ARM_CP_SVE,
7081       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7082       .writefn = zcr_write, .raw_writefn = raw_write },
7083     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7084       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7085       .access = PL2_RW, .type = ARM_CP_SVE,
7086       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7087       .writefn = zcr_write, .raw_writefn = raw_write },
7088     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7089       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7090       .access = PL3_RW, .type = ARM_CP_SVE,
7091       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7092       .writefn = zcr_write, .raw_writefn = raw_write },
7093 };
7094 
7095 #ifdef TARGET_AARCH64
7096 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7097                                     bool isread)
7098 {
7099     int el = arm_current_el(env);
7100 
7101     if (el == 0) {
7102         uint64_t sctlr = arm_sctlr(env, el);
7103         if (!(sctlr & SCTLR_EnTP2)) {
7104             return CP_ACCESS_TRAP;
7105         }
7106     }
7107     /* TODO: FEAT_FGT */
7108     if (el < 3
7109         && arm_feature(env, ARM_FEATURE_EL3)
7110         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7111         return CP_ACCESS_TRAP_EL3;
7112     }
7113     return CP_ACCESS_OK;
7114 }
7115 
7116 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7117                                       bool isread)
7118 {
7119     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7120     if (arm_current_el(env) == 2
7121         && arm_feature(env, ARM_FEATURE_EL3)
7122         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7123         return CP_ACCESS_TRAP_EL3;
7124     }
7125     return CP_ACCESS_OK;
7126 }
7127 
7128 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7129                                    bool isread)
7130 {
7131     if (arm_current_el(env) < 3
7132         && arm_feature(env, ARM_FEATURE_EL3)
7133         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7134         return CP_ACCESS_TRAP_EL3;
7135     }
7136     return CP_ACCESS_OK;
7137 }
7138 
7139 /* ResetSVEState */
7140 static void arm_reset_sve_state(CPUARMState *env)
7141 {
7142     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7143     /* Recall that FFR is stored as pregs[16]. */
7144     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7145     vfp_set_fpcr(env, 0x0800009f);
7146 }
7147 
7148 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7149 {
7150     uint64_t change = (env->svcr ^ new) & mask;
7151 
7152     if (change == 0) {
7153         return;
7154     }
7155     env->svcr ^= change;
7156 
7157     if (change & R_SVCR_SM_MASK) {
7158         arm_reset_sve_state(env);
7159     }
7160 
7161     /*
7162      * ResetSMEState.
7163      *
7164      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
7165      * on enable: while disabled, the storage is inaccessible and the
7166      * value does not matter.  We're not saving the storage in vmstate
7167      * when disabled either.
7168      */
7169     if (change & new & R_SVCR_ZA_MASK) {
7170         memset(env->zarray, 0, sizeof(env->zarray));
7171     }
7172 
7173     if (tcg_enabled()) {
7174         arm_rebuild_hflags(env);
7175     }
7176 }
7177 
7178 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7179                        uint64_t value)
7180 {
7181     aarch64_set_svcr(env, value, -1);
7182 }
7183 
7184 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7185                        uint64_t value)
7186 {
7187     int cur_el = arm_current_el(env);
7188     int old_len = sve_vqm1_for_el(env, cur_el);
7189     int new_len;
7190 
7191     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7192     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7193     raw_write(env, ri, value);
7194 
7195     /*
7196      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7197      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7198      * current values for simplicity.  But for QEMU internals, we must still
7199      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7200      * above aarch64_sve_narrow_vq.
7201      */
7202     new_len = sve_vqm1_for_el(env, cur_el);
7203     if (new_len < old_len) {
7204         aarch64_sve_narrow_vq(env, new_len + 1);
7205     }
7206 }
7207 
7208 static const ARMCPRegInfo sme_reginfo[] = {
7209     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7210       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7211       .access = PL0_RW, .accessfn = access_tpidr2,
7212       .fgt = FGT_NTPIDR2_EL0,
7213       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7214     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7215       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7216       .access = PL0_RW, .type = ARM_CP_SME,
7217       .fieldoffset = offsetof(CPUARMState, svcr),
7218       .writefn = svcr_write, .raw_writefn = raw_write },
7219     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7220       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7221       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7222       .access = PL1_RW, .type = ARM_CP_SME,
7223       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7224       .writefn = smcr_write, .raw_writefn = raw_write },
7225     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7226       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7227       .access = PL2_RW, .type = ARM_CP_SME,
7228       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7229       .writefn = smcr_write, .raw_writefn = raw_write },
7230     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7231       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7232       .access = PL3_RW, .type = ARM_CP_SME,
7233       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7234       .writefn = smcr_write, .raw_writefn = raw_write },
7235     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7236       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7237       .access = PL1_R, .accessfn = access_aa64_tid1,
7238       /*
7239        * IMPLEMENTOR = 0 (software)
7240        * REVISION    = 0 (implementation defined)
7241        * SMPS        = 0 (no streaming execution priority in QEMU)
7242        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7243        */
7244       .type = ARM_CP_CONST, .resetvalue = 0, },
7245     /*
7246      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7247      */
7248     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7249       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7250       .access = PL1_RW, .accessfn = access_smpri,
7251       .fgt = FGT_NSMPRI_EL1,
7252       .type = ARM_CP_CONST, .resetvalue = 0 },
7253     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7254       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7255       .nv2_redirect_offset = 0x1f8,
7256       .access = PL2_RW, .accessfn = access_smprimap,
7257       .type = ARM_CP_CONST, .resetvalue = 0 },
7258 };
7259 
7260 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7261                                   uint64_t value)
7262 {
7263     CPUState *cs = env_cpu(env);
7264 
7265     tlb_flush(cs);
7266 }
7267 
7268 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7269                         uint64_t value)
7270 {
7271     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7272     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7273         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7274         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7275 
7276     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7277 }
7278 
7279 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7280 {
7281     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7282                                      env_archcpu(env)->reset_l0gptsz);
7283 }
7284 
7285 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7286                                     uint64_t value)
7287 {
7288     CPUState *cs = env_cpu(env);
7289 
7290     tlb_flush_all_cpus_synced(cs);
7291 }
7292 
7293 static const ARMCPRegInfo rme_reginfo[] = {
7294     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7295       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7296       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7297       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7298     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7299       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7300       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7301     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7302       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7303       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7304     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7305       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7306       .access = PL3_W, .type = ARM_CP_NO_RAW,
7307       .writefn = tlbi_aa64_paall_write },
7308     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7309       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7310       .access = PL3_W, .type = ARM_CP_NO_RAW,
7311       .writefn = tlbi_aa64_paallos_write },
7312     /*
7313      * QEMU does not have a way to invalidate by physical address, thus
7314      * invalidating a range of physical addresses is accomplished by
7315      * flushing all tlb entries in the outer shareable domain,
7316      * just like PAALLOS.
7317      */
7318     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7319       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7320       .access = PL3_W, .type = ARM_CP_NO_RAW,
7321       .writefn = tlbi_aa64_paallos_write },
7322     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7323       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7324       .access = PL3_W, .type = ARM_CP_NO_RAW,
7325       .writefn = tlbi_aa64_paallos_write },
7326     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7327       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7328       .access = PL3_W, .type = ARM_CP_NOP },
7329 };
7330 
7331 static const ARMCPRegInfo rme_mte_reginfo[] = {
7332     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7333       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7334       .access = PL3_W, .type = ARM_CP_NOP },
7335 };
7336 #endif /* TARGET_AARCH64 */
7337 
7338 static void define_pmu_regs(ARMCPU *cpu)
7339 {
7340     /*
7341      * v7 performance monitor control register: same implementor
7342      * field as main ID register, and we implement four counters in
7343      * addition to the cycle count register.
7344      */
7345     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7346     ARMCPRegInfo pmcr = {
7347         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7348         .access = PL0_RW,
7349         .fgt = FGT_PMCR_EL0,
7350         .type = ARM_CP_IO | ARM_CP_ALIAS,
7351         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7352         .accessfn = pmreg_access,
7353         .readfn = pmcr_read, .raw_readfn = raw_read,
7354         .writefn = pmcr_write, .raw_writefn = raw_write,
7355     };
7356     ARMCPRegInfo pmcr64 = {
7357         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7358         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7359         .access = PL0_RW, .accessfn = pmreg_access,
7360         .fgt = FGT_PMCR_EL0,
7361         .type = ARM_CP_IO,
7362         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7363         .resetvalue = cpu->isar.reset_pmcr_el0,
7364         .readfn = pmcr_read, .raw_readfn = raw_read,
7365         .writefn = pmcr_write, .raw_writefn = raw_write,
7366     };
7367 
7368     define_one_arm_cp_reg(cpu, &pmcr);
7369     define_one_arm_cp_reg(cpu, &pmcr64);
7370     for (i = 0; i < pmcrn; i++) {
7371         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7372         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7373         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7374         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7375         ARMCPRegInfo pmev_regs[] = {
7376             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7377               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7378               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7379               .fgt = FGT_PMEVCNTRN_EL0,
7380               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7381               .accessfn = pmreg_access_xevcntr },
7382             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7383               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7384               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7385               .type = ARM_CP_IO,
7386               .fgt = FGT_PMEVCNTRN_EL0,
7387               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7388               .raw_readfn = pmevcntr_rawread,
7389               .raw_writefn = pmevcntr_rawwrite },
7390             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7391               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7392               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7393               .fgt = FGT_PMEVTYPERN_EL0,
7394               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7395               .accessfn = pmreg_access },
7396             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7397               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7398               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7399               .fgt = FGT_PMEVTYPERN_EL0,
7400               .type = ARM_CP_IO,
7401               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7402               .raw_writefn = pmevtyper_rawwrite },
7403         };
7404         define_arm_cp_regs(cpu, pmev_regs);
7405         g_free(pmevcntr_name);
7406         g_free(pmevcntr_el0_name);
7407         g_free(pmevtyper_name);
7408         g_free(pmevtyper_el0_name);
7409     }
7410     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7411         ARMCPRegInfo v81_pmu_regs[] = {
7412             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7413               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7414               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7415               .fgt = FGT_PMCEIDN_EL0,
7416               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7417             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7418               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7419               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7420               .fgt = FGT_PMCEIDN_EL0,
7421               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7422         };
7423         define_arm_cp_regs(cpu, v81_pmu_regs);
7424     }
7425     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7426         static const ARMCPRegInfo v84_pmmir = {
7427             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7428             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7429             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7430             .fgt = FGT_PMMIR_EL1,
7431             .resetvalue = 0
7432         };
7433         define_one_arm_cp_reg(cpu, &v84_pmmir);
7434     }
7435 }
7436 
7437 #ifndef CONFIG_USER_ONLY
7438 /*
7439  * We don't know until after realize whether there's a GICv3
7440  * attached, and that is what registers the gicv3 sysregs.
7441  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7442  * at runtime.
7443  */
7444 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7445 {
7446     ARMCPU *cpu = env_archcpu(env);
7447     uint64_t pfr1 = cpu->isar.id_pfr1;
7448 
7449     if (env->gicv3state) {
7450         pfr1 |= 1 << 28;
7451     }
7452     return pfr1;
7453 }
7454 
7455 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7456 {
7457     ARMCPU *cpu = env_archcpu(env);
7458     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7459 
7460     if (env->gicv3state) {
7461         pfr0 |= 1 << 24;
7462     }
7463     return pfr0;
7464 }
7465 #endif
7466 
7467 /*
7468  * Shared logic between LORID and the rest of the LOR* registers.
7469  * Secure state exclusion has already been dealt with.
7470  */
7471 static CPAccessResult access_lor_ns(CPUARMState *env,
7472                                     const ARMCPRegInfo *ri, bool isread)
7473 {
7474     int el = arm_current_el(env);
7475 
7476     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7477         return CP_ACCESS_TRAP_EL2;
7478     }
7479     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7480         return CP_ACCESS_TRAP_EL3;
7481     }
7482     return CP_ACCESS_OK;
7483 }
7484 
7485 static CPAccessResult access_lor_other(CPUARMState *env,
7486                                        const ARMCPRegInfo *ri, bool isread)
7487 {
7488     if (arm_is_secure_below_el3(env)) {
7489         /* Access denied in secure mode.  */
7490         return CP_ACCESS_TRAP;
7491     }
7492     return access_lor_ns(env, ri, isread);
7493 }
7494 
7495 /*
7496  * A trivial implementation of ARMv8.1-LOR leaves all of these
7497  * registers fixed at 0, which indicates that there are zero
7498  * supported Limited Ordering regions.
7499  */
7500 static const ARMCPRegInfo lor_reginfo[] = {
7501     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7502       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7503       .access = PL1_RW, .accessfn = access_lor_other,
7504       .fgt = FGT_LORSA_EL1,
7505       .type = ARM_CP_CONST, .resetvalue = 0 },
7506     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7507       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7508       .access = PL1_RW, .accessfn = access_lor_other,
7509       .fgt = FGT_LOREA_EL1,
7510       .type = ARM_CP_CONST, .resetvalue = 0 },
7511     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7512       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7513       .access = PL1_RW, .accessfn = access_lor_other,
7514       .fgt = FGT_LORN_EL1,
7515       .type = ARM_CP_CONST, .resetvalue = 0 },
7516     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7517       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7518       .access = PL1_RW, .accessfn = access_lor_other,
7519       .fgt = FGT_LORC_EL1,
7520       .type = ARM_CP_CONST, .resetvalue = 0 },
7521     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7522       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7523       .access = PL1_R, .accessfn = access_lor_ns,
7524       .fgt = FGT_LORID_EL1,
7525       .type = ARM_CP_CONST, .resetvalue = 0 },
7526 };
7527 
7528 #ifdef TARGET_AARCH64
7529 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7530                                    bool isread)
7531 {
7532     int el = arm_current_el(env);
7533 
7534     if (el < 2 &&
7535         arm_is_el2_enabled(env) &&
7536         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7537         return CP_ACCESS_TRAP_EL2;
7538     }
7539     if (el < 3 &&
7540         arm_feature(env, ARM_FEATURE_EL3) &&
7541         !(env->cp15.scr_el3 & SCR_APK)) {
7542         return CP_ACCESS_TRAP_EL3;
7543     }
7544     return CP_ACCESS_OK;
7545 }
7546 
7547 static const ARMCPRegInfo pauth_reginfo[] = {
7548     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7549       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7550       .access = PL1_RW, .accessfn = access_pauth,
7551       .fgt = FGT_APDAKEY,
7552       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7553     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7554       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7555       .access = PL1_RW, .accessfn = access_pauth,
7556       .fgt = FGT_APDAKEY,
7557       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7558     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7559       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7560       .access = PL1_RW, .accessfn = access_pauth,
7561       .fgt = FGT_APDBKEY,
7562       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7563     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7564       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7565       .access = PL1_RW, .accessfn = access_pauth,
7566       .fgt = FGT_APDBKEY,
7567       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7568     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7569       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7570       .access = PL1_RW, .accessfn = access_pauth,
7571       .fgt = FGT_APGAKEY,
7572       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7573     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7574       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7575       .access = PL1_RW, .accessfn = access_pauth,
7576       .fgt = FGT_APGAKEY,
7577       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7578     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7579       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7580       .access = PL1_RW, .accessfn = access_pauth,
7581       .fgt = FGT_APIAKEY,
7582       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7583     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7584       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7585       .access = PL1_RW, .accessfn = access_pauth,
7586       .fgt = FGT_APIAKEY,
7587       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7588     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7589       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7590       .access = PL1_RW, .accessfn = access_pauth,
7591       .fgt = FGT_APIBKEY,
7592       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7593     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7594       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7595       .access = PL1_RW, .accessfn = access_pauth,
7596       .fgt = FGT_APIBKEY,
7597       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7598 };
7599 
7600 static const ARMCPRegInfo tlbirange_reginfo[] = {
7601     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7602       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7603       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7604       .fgt = FGT_TLBIRVAE1IS,
7605       .writefn = tlbi_aa64_rvae1is_write },
7606     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7607       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7608       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7609       .fgt = FGT_TLBIRVAAE1IS,
7610       .writefn = tlbi_aa64_rvae1is_write },
7611    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7612       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7613       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7614       .fgt = FGT_TLBIRVALE1IS,
7615       .writefn = tlbi_aa64_rvae1is_write },
7616     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7617       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7618       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7619       .fgt = FGT_TLBIRVAALE1IS,
7620       .writefn = tlbi_aa64_rvae1is_write },
7621     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7622       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7623       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7624       .fgt = FGT_TLBIRVAE1OS,
7625       .writefn = tlbi_aa64_rvae1is_write },
7626     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7627       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7628       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7629       .fgt = FGT_TLBIRVAAE1OS,
7630       .writefn = tlbi_aa64_rvae1is_write },
7631    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7632       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7633       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7634       .fgt = FGT_TLBIRVALE1OS,
7635       .writefn = tlbi_aa64_rvae1is_write },
7636     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7637       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7638       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7639       .fgt = FGT_TLBIRVAALE1OS,
7640       .writefn = tlbi_aa64_rvae1is_write },
7641     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7642       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7643       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7644       .fgt = FGT_TLBIRVAE1,
7645       .writefn = tlbi_aa64_rvae1_write },
7646     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7647       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7648       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7649       .fgt = FGT_TLBIRVAAE1,
7650       .writefn = tlbi_aa64_rvae1_write },
7651    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7652       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7653       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7654       .fgt = FGT_TLBIRVALE1,
7655       .writefn = tlbi_aa64_rvae1_write },
7656     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7657       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7658       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7659       .fgt = FGT_TLBIRVAALE1,
7660       .writefn = tlbi_aa64_rvae1_write },
7661     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7662       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7663       .access = PL2_W, .type = ARM_CP_NO_RAW,
7664       .writefn = tlbi_aa64_ripas2e1is_write },
7665     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7666       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7667       .access = PL2_W, .type = ARM_CP_NO_RAW,
7668       .writefn = tlbi_aa64_ripas2e1is_write },
7669     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7670       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7671       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7672       .writefn = tlbi_aa64_rvae2is_write },
7673    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7674       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7675       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7676       .writefn = tlbi_aa64_rvae2is_write },
7677     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7678       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7679       .access = PL2_W, .type = ARM_CP_NO_RAW,
7680       .writefn = tlbi_aa64_ripas2e1_write },
7681     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7683       .access = PL2_W, .type = ARM_CP_NO_RAW,
7684       .writefn = tlbi_aa64_ripas2e1_write },
7685    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7686       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7687       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7688       .writefn = tlbi_aa64_rvae2is_write },
7689    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7690       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7691       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7692       .writefn = tlbi_aa64_rvae2is_write },
7693     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7694       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7695       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7696       .writefn = tlbi_aa64_rvae2_write },
7697    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7698       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7699       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7700       .writefn = tlbi_aa64_rvae2_write },
7701    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7702       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7703       .access = PL3_W, .type = ARM_CP_NO_RAW,
7704       .writefn = tlbi_aa64_rvae3is_write },
7705    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7706       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7707       .access = PL3_W, .type = ARM_CP_NO_RAW,
7708       .writefn = tlbi_aa64_rvae3is_write },
7709    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7710       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7711       .access = PL3_W, .type = ARM_CP_NO_RAW,
7712       .writefn = tlbi_aa64_rvae3is_write },
7713    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7714       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7715       .access = PL3_W, .type = ARM_CP_NO_RAW,
7716       .writefn = tlbi_aa64_rvae3is_write },
7717    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7718       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7719       .access = PL3_W, .type = ARM_CP_NO_RAW,
7720       .writefn = tlbi_aa64_rvae3_write },
7721    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7722       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7723       .access = PL3_W, .type = ARM_CP_NO_RAW,
7724       .writefn = tlbi_aa64_rvae3_write },
7725 };
7726 
7727 static const ARMCPRegInfo tlbios_reginfo[] = {
7728     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7729       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7730       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7731       .fgt = FGT_TLBIVMALLE1OS,
7732       .writefn = tlbi_aa64_vmalle1is_write },
7733     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7734       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7735       .fgt = FGT_TLBIVAE1OS,
7736       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7737       .writefn = tlbi_aa64_vae1is_write },
7738     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7739       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7740       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7741       .fgt = FGT_TLBIASIDE1OS,
7742       .writefn = tlbi_aa64_vmalle1is_write },
7743     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7744       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7745       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7746       .fgt = FGT_TLBIVAAE1OS,
7747       .writefn = tlbi_aa64_vae1is_write },
7748     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7749       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7750       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7751       .fgt = FGT_TLBIVALE1OS,
7752       .writefn = tlbi_aa64_vae1is_write },
7753     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7754       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7755       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7756       .fgt = FGT_TLBIVAALE1OS,
7757       .writefn = tlbi_aa64_vae1is_write },
7758     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7759       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7760       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7761       .writefn = tlbi_aa64_alle2is_write },
7762     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7763       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7764       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7765       .writefn = tlbi_aa64_vae2is_write },
7766    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7767       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7768       .access = PL2_W, .type = ARM_CP_NO_RAW,
7769       .writefn = tlbi_aa64_alle1is_write },
7770     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7771       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7772       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7773       .writefn = tlbi_aa64_vae2is_write },
7774     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7775       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7776       .access = PL2_W, .type = ARM_CP_NO_RAW,
7777       .writefn = tlbi_aa64_alle1is_write },
7778     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7779       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7780       .access = PL2_W, .type = ARM_CP_NOP },
7781     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7782       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7783       .access = PL2_W, .type = ARM_CP_NOP },
7784     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7785       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7786       .access = PL2_W, .type = ARM_CP_NOP },
7787     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7788       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7789       .access = PL2_W, .type = ARM_CP_NOP },
7790     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7791       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7792       .access = PL3_W, .type = ARM_CP_NO_RAW,
7793       .writefn = tlbi_aa64_alle3is_write },
7794     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7795       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7796       .access = PL3_W, .type = ARM_CP_NO_RAW,
7797       .writefn = tlbi_aa64_vae3is_write },
7798     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7799       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7800       .access = PL3_W, .type = ARM_CP_NO_RAW,
7801       .writefn = tlbi_aa64_vae3is_write },
7802 };
7803 
7804 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7805 {
7806     Error *err = NULL;
7807     uint64_t ret;
7808 
7809     /* Success sets NZCV = 0000.  */
7810     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7811 
7812     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7813         /*
7814          * ??? Failed, for unknown reasons in the crypto subsystem.
7815          * The best we can do is log the reason and return the
7816          * timed-out indication to the guest.  There is no reason
7817          * we know to expect this failure to be transitory, so the
7818          * guest may well hang retrying the operation.
7819          */
7820         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7821                       ri->name, error_get_pretty(err));
7822         error_free(err);
7823 
7824         env->ZF = 0; /* NZCF = 0100 */
7825         return 0;
7826     }
7827     return ret;
7828 }
7829 
7830 /* We do not support re-seeding, so the two registers operate the same.  */
7831 static const ARMCPRegInfo rndr_reginfo[] = {
7832     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7833       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7834       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7835       .access = PL0_R, .readfn = rndr_readfn },
7836     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7837       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7838       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7839       .access = PL0_R, .readfn = rndr_readfn },
7840 };
7841 
7842 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7843                           uint64_t value)
7844 {
7845 #ifdef CONFIG_TCG
7846     ARMCPU *cpu = env_archcpu(env);
7847     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7848     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7849     uint64_t vaddr_in = (uint64_t) value;
7850     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7851     void *haddr;
7852     int mem_idx = arm_env_mmu_index(env);
7853 
7854     /* This won't be crossing page boundaries */
7855     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7856     if (haddr) {
7857 #ifndef CONFIG_USER_ONLY
7858 
7859         ram_addr_t offset;
7860         MemoryRegion *mr;
7861 
7862         /* RCU lock is already being held */
7863         mr = memory_region_from_host(haddr, &offset);
7864 
7865         if (mr) {
7866             memory_region_writeback(mr, offset, dline_size);
7867         }
7868 #endif /*CONFIG_USER_ONLY*/
7869     }
7870 #else
7871     /* Handled by hardware accelerator. */
7872     g_assert_not_reached();
7873 #endif /* CONFIG_TCG */
7874 }
7875 
7876 static const ARMCPRegInfo dcpop_reg[] = {
7877     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7878       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7879       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7880       .fgt = FGT_DCCVAP,
7881       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7882 };
7883 
7884 static const ARMCPRegInfo dcpodp_reg[] = {
7885     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7886       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7887       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7888       .fgt = FGT_DCCVADP,
7889       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7890 };
7891 
7892 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7893                                        bool isread)
7894 {
7895     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7896         return CP_ACCESS_TRAP_EL2;
7897     }
7898 
7899     return CP_ACCESS_OK;
7900 }
7901 
7902 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7903                                  bool isread)
7904 {
7905     int el = arm_current_el(env);
7906     if (el < 2 && arm_is_el2_enabled(env)) {
7907         uint64_t hcr = arm_hcr_el2_eff(env);
7908         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7909             return CP_ACCESS_TRAP_EL2;
7910         }
7911     }
7912     if (el < 3 &&
7913         arm_feature(env, ARM_FEATURE_EL3) &&
7914         !(env->cp15.scr_el3 & SCR_ATA)) {
7915         return CP_ACCESS_TRAP_EL3;
7916     }
7917     return CP_ACCESS_OK;
7918 }
7919 
7920 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7921                                       bool isread)
7922 {
7923     CPAccessResult nv1 = access_nv1(env, ri, isread);
7924 
7925     if (nv1 != CP_ACCESS_OK) {
7926         return nv1;
7927     }
7928     return access_mte(env, ri, isread);
7929 }
7930 
7931 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7932                                       bool isread)
7933 {
7934     /*
7935      * TFSR_EL2: similar to generic access_mte(), but we need to
7936      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7937      * if NV2 is enabled then we will redirect this to TFSR_EL1
7938      * after doing the HCR and SCR ATA traps; otherwise this will
7939      * be a trap to EL2 and the HCR/SCR traps do not apply.
7940      */
7941     int el = arm_current_el(env);
7942 
7943     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7944         return CP_ACCESS_OK;
7945     }
7946     if (el < 2 && arm_is_el2_enabled(env)) {
7947         uint64_t hcr = arm_hcr_el2_eff(env);
7948         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7949             return CP_ACCESS_TRAP_EL2;
7950         }
7951     }
7952     if (el < 3 &&
7953         arm_feature(env, ARM_FEATURE_EL3) &&
7954         !(env->cp15.scr_el3 & SCR_ATA)) {
7955         return CP_ACCESS_TRAP_EL3;
7956     }
7957     return CP_ACCESS_OK;
7958 }
7959 
7960 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7961 {
7962     return env->pstate & PSTATE_TCO;
7963 }
7964 
7965 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7966 {
7967     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7968 }
7969 
7970 static const ARMCPRegInfo mte_reginfo[] = {
7971     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7972       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7973       .access = PL1_RW, .accessfn = access_mte,
7974       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7975     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7976       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7977       .access = PL1_RW, .accessfn = access_tfsr_el1,
7978       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7979       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7980     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7981       .type = ARM_CP_NV2_REDIRECT,
7982       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7983       .access = PL2_RW, .accessfn = access_tfsr_el2,
7984       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7985     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7986       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7987       .access = PL3_RW,
7988       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7989     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7990       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7991       .access = PL1_RW, .accessfn = access_mte,
7992       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7993     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7994       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7995       .access = PL1_RW, .accessfn = access_mte,
7996       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7997     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7998       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7999       .type = ARM_CP_NO_RAW,
8000       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8001     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8002       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8003       .type = ARM_CP_NOP, .access = PL1_W,
8004       .fgt = FGT_DCIVAC,
8005       .accessfn = aa64_cacheop_poc_access },
8006     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8007       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8008       .fgt = FGT_DCISW,
8009       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8010     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8011       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8012       .type = ARM_CP_NOP, .access = PL1_W,
8013       .fgt = FGT_DCIVAC,
8014       .accessfn = aa64_cacheop_poc_access },
8015     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8016       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8017       .fgt = FGT_DCISW,
8018       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8019     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8020       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8021       .fgt = FGT_DCCSW,
8022       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8023     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8024       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8025       .fgt = FGT_DCCSW,
8026       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8027     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8028       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8029       .fgt = FGT_DCCISW,
8030       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8031     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8032       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8033       .fgt = FGT_DCCISW,
8034       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8035 };
8036 
8037 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8038     { .name = "TCO", .state = ARM_CP_STATE_AA64,
8039       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8040       .type = ARM_CP_CONST, .access = PL0_RW, },
8041 };
8042 
8043 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8044     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8045       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8046       .type = ARM_CP_NOP, .access = PL0_W,
8047       .fgt = FGT_DCCVAC,
8048       .accessfn = aa64_cacheop_poc_access },
8049     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8050       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8051       .type = ARM_CP_NOP, .access = PL0_W,
8052       .fgt = FGT_DCCVAC,
8053       .accessfn = aa64_cacheop_poc_access },
8054     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8055       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8056       .type = ARM_CP_NOP, .access = PL0_W,
8057       .fgt = FGT_DCCVAP,
8058       .accessfn = aa64_cacheop_poc_access },
8059     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8060       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8061       .type = ARM_CP_NOP, .access = PL0_W,
8062       .fgt = FGT_DCCVAP,
8063       .accessfn = aa64_cacheop_poc_access },
8064     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8065       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8066       .type = ARM_CP_NOP, .access = PL0_W,
8067       .fgt = FGT_DCCVADP,
8068       .accessfn = aa64_cacheop_poc_access },
8069     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8070       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8071       .type = ARM_CP_NOP, .access = PL0_W,
8072       .fgt = FGT_DCCVADP,
8073       .accessfn = aa64_cacheop_poc_access },
8074     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8075       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8076       .type = ARM_CP_NOP, .access = PL0_W,
8077       .fgt = FGT_DCCIVAC,
8078       .accessfn = aa64_cacheop_poc_access },
8079     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8080       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8081       .type = ARM_CP_NOP, .access = PL0_W,
8082       .fgt = FGT_DCCIVAC,
8083       .accessfn = aa64_cacheop_poc_access },
8084     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8085       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8086       .access = PL0_W, .type = ARM_CP_DC_GVA,
8087 #ifndef CONFIG_USER_ONLY
8088       /* Avoid overhead of an access check that always passes in user-mode */
8089       .accessfn = aa64_zva_access,
8090       .fgt = FGT_DCZVA,
8091 #endif
8092     },
8093     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8094       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8095       .access = PL0_W, .type = ARM_CP_DC_GZVA,
8096 #ifndef CONFIG_USER_ONLY
8097       /* Avoid overhead of an access check that always passes in user-mode */
8098       .accessfn = aa64_zva_access,
8099       .fgt = FGT_DCZVA,
8100 #endif
8101     },
8102 };
8103 
8104 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8105                                      bool isread)
8106 {
8107     uint64_t hcr = arm_hcr_el2_eff(env);
8108     int el = arm_current_el(env);
8109 
8110     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8111         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8112             if (hcr & HCR_TGE) {
8113                 return CP_ACCESS_TRAP_EL2;
8114             }
8115             return CP_ACCESS_TRAP;
8116         }
8117     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8118         return CP_ACCESS_TRAP_EL2;
8119     }
8120     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8121         return CP_ACCESS_TRAP_EL2;
8122     }
8123     if (el < 3
8124         && arm_feature(env, ARM_FEATURE_EL3)
8125         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8126         return CP_ACCESS_TRAP_EL3;
8127     }
8128     return CP_ACCESS_OK;
8129 }
8130 
8131 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8132                                          const ARMCPRegInfo *ri,
8133                                          bool isread)
8134 {
8135     CPAccessResult nv1 = access_nv1(env, ri, isread);
8136 
8137     if (nv1 != CP_ACCESS_OK) {
8138         return nv1;
8139     }
8140     return access_scxtnum(env, ri, isread);
8141 }
8142 
8143 static const ARMCPRegInfo scxtnum_reginfo[] = {
8144     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8145       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8146       .access = PL0_RW, .accessfn = access_scxtnum,
8147       .fgt = FGT_SCXTNUM_EL0,
8148       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8149     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8150       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8151       .access = PL1_RW, .accessfn = access_scxtnum_el1,
8152       .fgt = FGT_SCXTNUM_EL1,
8153       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8154       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8155     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8156       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8157       .access = PL2_RW, .accessfn = access_scxtnum,
8158       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8159     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8160       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8161       .access = PL3_RW,
8162       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8163 };
8164 
8165 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8166                                  bool isread)
8167 {
8168     if (arm_current_el(env) == 2 &&
8169         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8170         return CP_ACCESS_TRAP_EL3;
8171     }
8172     return CP_ACCESS_OK;
8173 }
8174 
8175 static const ARMCPRegInfo fgt_reginfo[] = {
8176     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8177       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8178       .nv2_redirect_offset = 0x1b8,
8179       .access = PL2_RW, .accessfn = access_fgt,
8180       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8181     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8182       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8183       .nv2_redirect_offset = 0x1c0,
8184       .access = PL2_RW, .accessfn = access_fgt,
8185       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8186     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8187       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8188       .nv2_redirect_offset = 0x1d0,
8189       .access = PL2_RW, .accessfn = access_fgt,
8190       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8191     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8192       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8193       .nv2_redirect_offset = 0x1d8,
8194       .access = PL2_RW, .accessfn = access_fgt,
8195       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8196     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8197       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8198       .nv2_redirect_offset = 0x1c8,
8199       .access = PL2_RW, .accessfn = access_fgt,
8200       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8201 };
8202 
8203 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8204                        uint64_t value)
8205 {
8206     /*
8207      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8208      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8209      * about the RESS bits at the top -- we choose the "generate an EL2
8210      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8211      * the ptw.c code detect the resulting invalid address).
8212      */
8213     env->cp15.vncr_el2 = value & ~0xfffULL;
8214 }
8215 
8216 static const ARMCPRegInfo nv2_reginfo[] = {
8217     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8218       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8219       .access = PL2_RW,
8220       .writefn = vncr_write,
8221       .nv2_redirect_offset = 0xb0,
8222       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8223 };
8224 
8225 #endif /* TARGET_AARCH64 */
8226 
8227 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8228                                      bool isread)
8229 {
8230     int el = arm_current_el(env);
8231 
8232     if (el == 0) {
8233         uint64_t sctlr = arm_sctlr(env, el);
8234         if (!(sctlr & SCTLR_EnRCTX)) {
8235             return CP_ACCESS_TRAP;
8236         }
8237     } else if (el == 1) {
8238         uint64_t hcr = arm_hcr_el2_eff(env);
8239         if (hcr & HCR_NV) {
8240             return CP_ACCESS_TRAP_EL2;
8241         }
8242     }
8243     return CP_ACCESS_OK;
8244 }
8245 
8246 static const ARMCPRegInfo predinv_reginfo[] = {
8247     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8248       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8249       .fgt = FGT_CFPRCTX,
8250       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8251     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8252       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8253       .fgt = FGT_DVPRCTX,
8254       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8255     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8256       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8257       .fgt = FGT_CPPRCTX,
8258       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8259     /*
8260      * Note the AArch32 opcodes have a different OPC1.
8261      */
8262     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8263       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8264       .fgt = FGT_CFPRCTX,
8265       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8266     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8267       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8268       .fgt = FGT_DVPRCTX,
8269       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8270     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8271       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8272       .fgt = FGT_CPPRCTX,
8273       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8274 };
8275 
8276 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8277 {
8278     /* Read the high 32 bits of the current CCSIDR */
8279     return extract64(ccsidr_read(env, ri), 32, 32);
8280 }
8281 
8282 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8283     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8284       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8285       .access = PL1_R,
8286       .accessfn = access_tid4,
8287       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8288 };
8289 
8290 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8291                                        bool isread)
8292 {
8293     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8294         return CP_ACCESS_TRAP_EL2;
8295     }
8296 
8297     return CP_ACCESS_OK;
8298 }
8299 
8300 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8301                                        bool isread)
8302 {
8303     if (arm_feature(env, ARM_FEATURE_V8)) {
8304         return access_aa64_tid3(env, ri, isread);
8305     }
8306 
8307     return CP_ACCESS_OK;
8308 }
8309 
8310 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8311                                      bool isread)
8312 {
8313     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8314         return CP_ACCESS_TRAP_EL2;
8315     }
8316 
8317     return CP_ACCESS_OK;
8318 }
8319 
8320 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8321                                         const ARMCPRegInfo *ri, bool isread)
8322 {
8323     /*
8324      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8325      * in v7A, not in v8A.
8326      */
8327     if (!arm_feature(env, ARM_FEATURE_V8) &&
8328         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8329         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8330         return CP_ACCESS_TRAP_EL2;
8331     }
8332     return CP_ACCESS_OK;
8333 }
8334 
8335 static const ARMCPRegInfo jazelle_regs[] = {
8336     { .name = "JIDR",
8337       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8338       .access = PL1_R, .accessfn = access_jazelle,
8339       .type = ARM_CP_CONST, .resetvalue = 0 },
8340     { .name = "JOSCR",
8341       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8342       .accessfn = access_joscr_jmcr,
8343       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8344     { .name = "JMCR",
8345       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8346       .accessfn = access_joscr_jmcr,
8347       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8348 };
8349 
8350 static const ARMCPRegInfo contextidr_el2 = {
8351     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8352     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8353     .access = PL2_RW,
8354     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8355 };
8356 
8357 static const ARMCPRegInfo vhe_reginfo[] = {
8358     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8359       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8360       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8361       .raw_writefn = raw_write,
8362       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8363 #ifndef CONFIG_USER_ONLY
8364     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8365       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8366       .fieldoffset =
8367         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8368       .type = ARM_CP_IO, .access = PL2_RW,
8369       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8370     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8371       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8372       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8373       .resetfn = gt_hv_timer_reset,
8374       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8375     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8376       .type = ARM_CP_IO,
8377       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8378       .access = PL2_RW,
8379       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8380       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8381     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8382       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8383       .type = ARM_CP_IO | ARM_CP_ALIAS,
8384       .access = PL2_RW, .accessfn = e2h_access,
8385       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8386       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8387       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8388     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8389       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8390       .type = ARM_CP_IO | ARM_CP_ALIAS,
8391       .access = PL2_RW, .accessfn = e2h_access,
8392       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8393       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8394       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8395     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8396       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8397       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8398       .access = PL2_RW, .accessfn = e2h_access,
8399       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8400     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8401       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8402       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8403       .access = PL2_RW, .accessfn = e2h_access,
8404       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8405     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8406       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8407       .type = ARM_CP_IO | ARM_CP_ALIAS,
8408       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8409       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8410       .access = PL2_RW, .accessfn = e2h_access,
8411       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8412     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8413       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8414       .type = ARM_CP_IO | ARM_CP_ALIAS,
8415       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8416       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8417       .access = PL2_RW, .accessfn = e2h_access,
8418       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8419 #endif
8420 };
8421 
8422 #ifndef CONFIG_USER_ONLY
8423 static const ARMCPRegInfo ats1e1_reginfo[] = {
8424     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8425       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8426       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8427       .fgt = FGT_ATS1E1RP,
8428       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8429     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8430       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8431       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8432       .fgt = FGT_ATS1E1WP,
8433       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8434 };
8435 
8436 static const ARMCPRegInfo ats1cp_reginfo[] = {
8437     { .name = "ATS1CPRP",
8438       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8439       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8440       .writefn = ats_write },
8441     { .name = "ATS1CPWP",
8442       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8443       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8444       .writefn = ats_write },
8445 };
8446 #endif
8447 
8448 /*
8449  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8450  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8451  * is non-zero, which is never for ARMv7, optionally in ARMv8
8452  * and mandatorily for ARMv8.2 and up.
8453  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8454  * implementation is RAZ/WI we can ignore this detail, as we
8455  * do for ACTLR.
8456  */
8457 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8458     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8459       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8460       .access = PL1_RW, .accessfn = access_tacr,
8461       .type = ARM_CP_CONST, .resetvalue = 0 },
8462     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8463       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8464       .access = PL2_RW, .type = ARM_CP_CONST,
8465       .resetvalue = 0 },
8466 };
8467 
8468 void register_cp_regs_for_features(ARMCPU *cpu)
8469 {
8470     /* Register all the coprocessor registers based on feature bits */
8471     CPUARMState *env = &cpu->env;
8472     if (arm_feature(env, ARM_FEATURE_M)) {
8473         /* M profile has no coprocessor registers */
8474         return;
8475     }
8476 
8477     define_arm_cp_regs(cpu, cp_reginfo);
8478     if (!arm_feature(env, ARM_FEATURE_V8)) {
8479         /*
8480          * Must go early as it is full of wildcards that may be
8481          * overridden by later definitions.
8482          */
8483         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8484     }
8485 
8486     if (arm_feature(env, ARM_FEATURE_V6)) {
8487         /* The ID registers all have impdef reset values */
8488         ARMCPRegInfo v6_idregs[] = {
8489             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8490               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8491               .access = PL1_R, .type = ARM_CP_CONST,
8492               .accessfn = access_aa32_tid3,
8493               .resetvalue = cpu->isar.id_pfr0 },
8494             /*
8495              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8496              * the value of the GIC field until after we define these regs.
8497              */
8498             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8499               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8500               .access = PL1_R, .type = ARM_CP_NO_RAW,
8501               .accessfn = access_aa32_tid3,
8502 #ifdef CONFIG_USER_ONLY
8503               .type = ARM_CP_CONST,
8504               .resetvalue = cpu->isar.id_pfr1,
8505 #else
8506               .type = ARM_CP_NO_RAW,
8507               .accessfn = access_aa32_tid3,
8508               .readfn = id_pfr1_read,
8509               .writefn = arm_cp_write_ignore
8510 #endif
8511             },
8512             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8513               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8514               .access = PL1_R, .type = ARM_CP_CONST,
8515               .accessfn = access_aa32_tid3,
8516               .resetvalue = cpu->isar.id_dfr0 },
8517             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8518               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8519               .access = PL1_R, .type = ARM_CP_CONST,
8520               .accessfn = access_aa32_tid3,
8521               .resetvalue = cpu->id_afr0 },
8522             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8523               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8524               .access = PL1_R, .type = ARM_CP_CONST,
8525               .accessfn = access_aa32_tid3,
8526               .resetvalue = cpu->isar.id_mmfr0 },
8527             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8528               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8529               .access = PL1_R, .type = ARM_CP_CONST,
8530               .accessfn = access_aa32_tid3,
8531               .resetvalue = cpu->isar.id_mmfr1 },
8532             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8533               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8534               .access = PL1_R, .type = ARM_CP_CONST,
8535               .accessfn = access_aa32_tid3,
8536               .resetvalue = cpu->isar.id_mmfr2 },
8537             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8538               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8539               .access = PL1_R, .type = ARM_CP_CONST,
8540               .accessfn = access_aa32_tid3,
8541               .resetvalue = cpu->isar.id_mmfr3 },
8542             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8543               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8544               .access = PL1_R, .type = ARM_CP_CONST,
8545               .accessfn = access_aa32_tid3,
8546               .resetvalue = cpu->isar.id_isar0 },
8547             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8548               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8549               .access = PL1_R, .type = ARM_CP_CONST,
8550               .accessfn = access_aa32_tid3,
8551               .resetvalue = cpu->isar.id_isar1 },
8552             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8553               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8554               .access = PL1_R, .type = ARM_CP_CONST,
8555               .accessfn = access_aa32_tid3,
8556               .resetvalue = cpu->isar.id_isar2 },
8557             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8558               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8559               .access = PL1_R, .type = ARM_CP_CONST,
8560               .accessfn = access_aa32_tid3,
8561               .resetvalue = cpu->isar.id_isar3 },
8562             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8563               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8564               .access = PL1_R, .type = ARM_CP_CONST,
8565               .accessfn = access_aa32_tid3,
8566               .resetvalue = cpu->isar.id_isar4 },
8567             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8568               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8569               .access = PL1_R, .type = ARM_CP_CONST,
8570               .accessfn = access_aa32_tid3,
8571               .resetvalue = cpu->isar.id_isar5 },
8572             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8573               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8574               .access = PL1_R, .type = ARM_CP_CONST,
8575               .accessfn = access_aa32_tid3,
8576               .resetvalue = cpu->isar.id_mmfr4 },
8577             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8578               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8579               .access = PL1_R, .type = ARM_CP_CONST,
8580               .accessfn = access_aa32_tid3,
8581               .resetvalue = cpu->isar.id_isar6 },
8582         };
8583         define_arm_cp_regs(cpu, v6_idregs);
8584         define_arm_cp_regs(cpu, v6_cp_reginfo);
8585     } else {
8586         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8587     }
8588     if (arm_feature(env, ARM_FEATURE_V6K)) {
8589         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8590     }
8591     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8592         !arm_feature(env, ARM_FEATURE_PMSA)) {
8593         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8594     }
8595     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8596         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8597     }
8598     if (arm_feature(env, ARM_FEATURE_V7)) {
8599         ARMCPRegInfo clidr = {
8600             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8601             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8602             .access = PL1_R, .type = ARM_CP_CONST,
8603             .accessfn = access_tid4,
8604             .fgt = FGT_CLIDR_EL1,
8605             .resetvalue = cpu->clidr
8606         };
8607         define_one_arm_cp_reg(cpu, &clidr);
8608         define_arm_cp_regs(cpu, v7_cp_reginfo);
8609         define_debug_regs(cpu);
8610         define_pmu_regs(cpu);
8611     } else {
8612         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8613     }
8614     if (arm_feature(env, ARM_FEATURE_V8)) {
8615         /*
8616          * v8 ID registers, which all have impdef reset values.
8617          * Note that within the ID register ranges the unused slots
8618          * must all RAZ, not UNDEF; future architecture versions may
8619          * define new registers here.
8620          * ID registers which are AArch64 views of the AArch32 ID registers
8621          * which already existed in v6 and v7 are handled elsewhere,
8622          * in v6_idregs[].
8623          */
8624         int i;
8625         ARMCPRegInfo v8_idregs[] = {
8626             /*
8627              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8628              * emulation because we don't know the right value for the
8629              * GIC field until after we define these regs.
8630              */
8631             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8632               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8633               .access = PL1_R,
8634 #ifdef CONFIG_USER_ONLY
8635               .type = ARM_CP_CONST,
8636               .resetvalue = cpu->isar.id_aa64pfr0
8637 #else
8638               .type = ARM_CP_NO_RAW,
8639               .accessfn = access_aa64_tid3,
8640               .readfn = id_aa64pfr0_read,
8641               .writefn = arm_cp_write_ignore
8642 #endif
8643             },
8644             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8645               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8646               .access = PL1_R, .type = ARM_CP_CONST,
8647               .accessfn = access_aa64_tid3,
8648               .resetvalue = cpu->isar.id_aa64pfr1},
8649             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8650               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8651               .access = PL1_R, .type = ARM_CP_CONST,
8652               .accessfn = access_aa64_tid3,
8653               .resetvalue = 0 },
8654             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8655               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8656               .access = PL1_R, .type = ARM_CP_CONST,
8657               .accessfn = access_aa64_tid3,
8658               .resetvalue = 0 },
8659             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8660               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8661               .access = PL1_R, .type = ARM_CP_CONST,
8662               .accessfn = access_aa64_tid3,
8663               .resetvalue = cpu->isar.id_aa64zfr0 },
8664             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8665               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8666               .access = PL1_R, .type = ARM_CP_CONST,
8667               .accessfn = access_aa64_tid3,
8668               .resetvalue = cpu->isar.id_aa64smfr0 },
8669             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8670               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8671               .access = PL1_R, .type = ARM_CP_CONST,
8672               .accessfn = access_aa64_tid3,
8673               .resetvalue = 0 },
8674             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8675               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8676               .access = PL1_R, .type = ARM_CP_CONST,
8677               .accessfn = access_aa64_tid3,
8678               .resetvalue = 0 },
8679             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8680               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8681               .access = PL1_R, .type = ARM_CP_CONST,
8682               .accessfn = access_aa64_tid3,
8683               .resetvalue = cpu->isar.id_aa64dfr0 },
8684             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8685               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8686               .access = PL1_R, .type = ARM_CP_CONST,
8687               .accessfn = access_aa64_tid3,
8688               .resetvalue = cpu->isar.id_aa64dfr1 },
8689             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8690               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8691               .access = PL1_R, .type = ARM_CP_CONST,
8692               .accessfn = access_aa64_tid3,
8693               .resetvalue = 0 },
8694             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8695               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8696               .access = PL1_R, .type = ARM_CP_CONST,
8697               .accessfn = access_aa64_tid3,
8698               .resetvalue = 0 },
8699             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8700               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8701               .access = PL1_R, .type = ARM_CP_CONST,
8702               .accessfn = access_aa64_tid3,
8703               .resetvalue = cpu->id_aa64afr0 },
8704             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8705               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8706               .access = PL1_R, .type = ARM_CP_CONST,
8707               .accessfn = access_aa64_tid3,
8708               .resetvalue = cpu->id_aa64afr1 },
8709             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8710               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8711               .access = PL1_R, .type = ARM_CP_CONST,
8712               .accessfn = access_aa64_tid3,
8713               .resetvalue = 0 },
8714             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8715               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8716               .access = PL1_R, .type = ARM_CP_CONST,
8717               .accessfn = access_aa64_tid3,
8718               .resetvalue = 0 },
8719             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8720               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8721               .access = PL1_R, .type = ARM_CP_CONST,
8722               .accessfn = access_aa64_tid3,
8723               .resetvalue = cpu->isar.id_aa64isar0 },
8724             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8725               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8726               .access = PL1_R, .type = ARM_CP_CONST,
8727               .accessfn = access_aa64_tid3,
8728               .resetvalue = cpu->isar.id_aa64isar1 },
8729             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8730               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8731               .access = PL1_R, .type = ARM_CP_CONST,
8732               .accessfn = access_aa64_tid3,
8733               .resetvalue = cpu->isar.id_aa64isar2 },
8734             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8735               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8736               .access = PL1_R, .type = ARM_CP_CONST,
8737               .accessfn = access_aa64_tid3,
8738               .resetvalue = 0 },
8739             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8740               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8741               .access = PL1_R, .type = ARM_CP_CONST,
8742               .accessfn = access_aa64_tid3,
8743               .resetvalue = 0 },
8744             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8745               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8746               .access = PL1_R, .type = ARM_CP_CONST,
8747               .accessfn = access_aa64_tid3,
8748               .resetvalue = 0 },
8749             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8750               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8751               .access = PL1_R, .type = ARM_CP_CONST,
8752               .accessfn = access_aa64_tid3,
8753               .resetvalue = 0 },
8754             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8755               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8756               .access = PL1_R, .type = ARM_CP_CONST,
8757               .accessfn = access_aa64_tid3,
8758               .resetvalue = 0 },
8759             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8760               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8761               .access = PL1_R, .type = ARM_CP_CONST,
8762               .accessfn = access_aa64_tid3,
8763               .resetvalue = cpu->isar.id_aa64mmfr0 },
8764             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8765               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8766               .access = PL1_R, .type = ARM_CP_CONST,
8767               .accessfn = access_aa64_tid3,
8768               .resetvalue = cpu->isar.id_aa64mmfr1 },
8769             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8770               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8771               .access = PL1_R, .type = ARM_CP_CONST,
8772               .accessfn = access_aa64_tid3,
8773               .resetvalue = cpu->isar.id_aa64mmfr2 },
8774             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8775               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8776               .access = PL1_R, .type = ARM_CP_CONST,
8777               .accessfn = access_aa64_tid3,
8778               .resetvalue = 0 },
8779             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8780               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8781               .access = PL1_R, .type = ARM_CP_CONST,
8782               .accessfn = access_aa64_tid3,
8783               .resetvalue = 0 },
8784             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8785               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8786               .access = PL1_R, .type = ARM_CP_CONST,
8787               .accessfn = access_aa64_tid3,
8788               .resetvalue = 0 },
8789             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8790               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8791               .access = PL1_R, .type = ARM_CP_CONST,
8792               .accessfn = access_aa64_tid3,
8793               .resetvalue = 0 },
8794             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8795               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8796               .access = PL1_R, .type = ARM_CP_CONST,
8797               .accessfn = access_aa64_tid3,
8798               .resetvalue = 0 },
8799             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8800               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8801               .access = PL1_R, .type = ARM_CP_CONST,
8802               .accessfn = access_aa64_tid3,
8803               .resetvalue = cpu->isar.mvfr0 },
8804             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8805               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8806               .access = PL1_R, .type = ARM_CP_CONST,
8807               .accessfn = access_aa64_tid3,
8808               .resetvalue = cpu->isar.mvfr1 },
8809             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8810               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8811               .access = PL1_R, .type = ARM_CP_CONST,
8812               .accessfn = access_aa64_tid3,
8813               .resetvalue = cpu->isar.mvfr2 },
8814             /*
8815              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8816              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8817              * as RAZ, since it is in the "reserved for future ID
8818              * registers, RAZ" part of the AArch32 encoding space.
8819              */
8820             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8821               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8822               .access = PL1_R, .type = ARM_CP_CONST,
8823               .accessfn = access_aa64_tid3,
8824               .resetvalue = 0 },
8825             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8826               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8827               .access = PL1_R, .type = ARM_CP_CONST,
8828               .accessfn = access_aa64_tid3,
8829               .resetvalue = 0 },
8830             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8831               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8832               .access = PL1_R, .type = ARM_CP_CONST,
8833               .accessfn = access_aa64_tid3,
8834               .resetvalue = 0 },
8835             /*
8836              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8837              * they're also RAZ for AArch64, and in v8 are gradually
8838              * being filled with AArch64-view-of-AArch32-ID-register
8839              * for new ID registers.
8840              */
8841             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8842               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8843               .access = PL1_R, .type = ARM_CP_CONST,
8844               .accessfn = access_aa64_tid3,
8845               .resetvalue = 0 },
8846             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8847               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8848               .access = PL1_R, .type = ARM_CP_CONST,
8849               .accessfn = access_aa64_tid3,
8850               .resetvalue = cpu->isar.id_pfr2 },
8851             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8852               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8853               .access = PL1_R, .type = ARM_CP_CONST,
8854               .accessfn = access_aa64_tid3,
8855               .resetvalue = cpu->isar.id_dfr1 },
8856             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8857               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8858               .access = PL1_R, .type = ARM_CP_CONST,
8859               .accessfn = access_aa64_tid3,
8860               .resetvalue = cpu->isar.id_mmfr5 },
8861             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8862               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8863               .access = PL1_R, .type = ARM_CP_CONST,
8864               .accessfn = access_aa64_tid3,
8865               .resetvalue = 0 },
8866             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8867               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8868               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8869               .fgt = FGT_PMCEIDN_EL0,
8870               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8871             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8872               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8873               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8874               .fgt = FGT_PMCEIDN_EL0,
8875               .resetvalue = cpu->pmceid0 },
8876             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8877               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8878               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8879               .fgt = FGT_PMCEIDN_EL0,
8880               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8881             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8882               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8883               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8884               .fgt = FGT_PMCEIDN_EL0,
8885               .resetvalue = cpu->pmceid1 },
8886         };
8887 #ifdef CONFIG_USER_ONLY
8888         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8889             { .name = "ID_AA64PFR0_EL1",
8890               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8891                                R_ID_AA64PFR0_ADVSIMD_MASK |
8892                                R_ID_AA64PFR0_SVE_MASK |
8893                                R_ID_AA64PFR0_DIT_MASK,
8894               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8895                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8896             { .name = "ID_AA64PFR1_EL1",
8897               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8898                                R_ID_AA64PFR1_SSBS_MASK |
8899                                R_ID_AA64PFR1_MTE_MASK |
8900                                R_ID_AA64PFR1_SME_MASK },
8901             { .name = "ID_AA64PFR*_EL1_RESERVED",
8902               .is_glob = true },
8903             { .name = "ID_AA64ZFR0_EL1",
8904               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8905                                R_ID_AA64ZFR0_AES_MASK |
8906                                R_ID_AA64ZFR0_BITPERM_MASK |
8907                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8908                                R_ID_AA64ZFR0_B16B16_MASK |
8909                                R_ID_AA64ZFR0_SHA3_MASK |
8910                                R_ID_AA64ZFR0_SM4_MASK |
8911                                R_ID_AA64ZFR0_I8MM_MASK |
8912                                R_ID_AA64ZFR0_F32MM_MASK |
8913                                R_ID_AA64ZFR0_F64MM_MASK },
8914             { .name = "ID_AA64SMFR0_EL1",
8915               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8916                                R_ID_AA64SMFR0_BI32I32_MASK |
8917                                R_ID_AA64SMFR0_B16F32_MASK |
8918                                R_ID_AA64SMFR0_F16F32_MASK |
8919                                R_ID_AA64SMFR0_I8I32_MASK |
8920                                R_ID_AA64SMFR0_F16F16_MASK |
8921                                R_ID_AA64SMFR0_B16B16_MASK |
8922                                R_ID_AA64SMFR0_I16I32_MASK |
8923                                R_ID_AA64SMFR0_F64F64_MASK |
8924                                R_ID_AA64SMFR0_I16I64_MASK |
8925                                R_ID_AA64SMFR0_SMEVER_MASK |
8926                                R_ID_AA64SMFR0_FA64_MASK },
8927             { .name = "ID_AA64MMFR0_EL1",
8928               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8929               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8930                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8931             { .name = "ID_AA64MMFR1_EL1",
8932               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8933             { .name = "ID_AA64MMFR2_EL1",
8934               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8935             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8936               .is_glob = true },
8937             { .name = "ID_AA64DFR0_EL1",
8938               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8939             { .name = "ID_AA64DFR1_EL1" },
8940             { .name = "ID_AA64DFR*_EL1_RESERVED",
8941               .is_glob = true },
8942             { .name = "ID_AA64AFR*",
8943               .is_glob = true },
8944             { .name = "ID_AA64ISAR0_EL1",
8945               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8946                                R_ID_AA64ISAR0_SHA1_MASK |
8947                                R_ID_AA64ISAR0_SHA2_MASK |
8948                                R_ID_AA64ISAR0_CRC32_MASK |
8949                                R_ID_AA64ISAR0_ATOMIC_MASK |
8950                                R_ID_AA64ISAR0_RDM_MASK |
8951                                R_ID_AA64ISAR0_SHA3_MASK |
8952                                R_ID_AA64ISAR0_SM3_MASK |
8953                                R_ID_AA64ISAR0_SM4_MASK |
8954                                R_ID_AA64ISAR0_DP_MASK |
8955                                R_ID_AA64ISAR0_FHM_MASK |
8956                                R_ID_AA64ISAR0_TS_MASK |
8957                                R_ID_AA64ISAR0_RNDR_MASK },
8958             { .name = "ID_AA64ISAR1_EL1",
8959               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8960                                R_ID_AA64ISAR1_APA_MASK |
8961                                R_ID_AA64ISAR1_API_MASK |
8962                                R_ID_AA64ISAR1_JSCVT_MASK |
8963                                R_ID_AA64ISAR1_FCMA_MASK |
8964                                R_ID_AA64ISAR1_LRCPC_MASK |
8965                                R_ID_AA64ISAR1_GPA_MASK |
8966                                R_ID_AA64ISAR1_GPI_MASK |
8967                                R_ID_AA64ISAR1_FRINTTS_MASK |
8968                                R_ID_AA64ISAR1_SB_MASK |
8969                                R_ID_AA64ISAR1_BF16_MASK |
8970                                R_ID_AA64ISAR1_DGH_MASK |
8971                                R_ID_AA64ISAR1_I8MM_MASK },
8972             { .name = "ID_AA64ISAR2_EL1",
8973               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8974                                R_ID_AA64ISAR2_RPRES_MASK |
8975                                R_ID_AA64ISAR2_GPA3_MASK |
8976                                R_ID_AA64ISAR2_APA3_MASK |
8977                                R_ID_AA64ISAR2_MOPS_MASK |
8978                                R_ID_AA64ISAR2_BC_MASK |
8979                                R_ID_AA64ISAR2_RPRFM_MASK |
8980                                R_ID_AA64ISAR2_CSSC_MASK },
8981             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8982               .is_glob = true },
8983         };
8984         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8985 #endif
8986         /*
8987          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8988          * TODO: For RMR, a write with bit 1 set should do something with
8989          * cpu_reset(). In the meantime, "the bit is strictly a request",
8990          * so we are in spec just ignoring writes.
8991          */
8992         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8993             !arm_feature(env, ARM_FEATURE_EL2)) {
8994             ARMCPRegInfo el1_reset_regs[] = {
8995                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8996                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8997                   .access = PL1_R,
8998                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8999                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9000                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9001                   .access = PL1_RW, .type = ARM_CP_CONST,
9002                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9003             };
9004             define_arm_cp_regs(cpu, el1_reset_regs);
9005         }
9006         define_arm_cp_regs(cpu, v8_idregs);
9007         define_arm_cp_regs(cpu, v8_cp_reginfo);
9008         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9009             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9010         }
9011 
9012         for (i = 4; i < 16; i++) {
9013             /*
9014              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9015              * For pre-v8 cores there are RAZ patterns for these in
9016              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9017              * v8 extends the "must RAZ" part of the ID register space
9018              * to also cover c0, 0, c{8-15}, {0-7}.
9019              * These are STATE_AA32 because in the AArch64 sysreg space
9020              * c4-c7 is where the AArch64 ID registers live (and we've
9021              * already defined those in v8_idregs[]), and c8-c15 are not
9022              * "must RAZ" for AArch64.
9023              */
9024             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9025             ARMCPRegInfo v8_aa32_raz_idregs = {
9026                 .name = name,
9027                 .state = ARM_CP_STATE_AA32,
9028                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9029                 .access = PL1_R, .type = ARM_CP_CONST,
9030                 .accessfn = access_aa64_tid3,
9031                 .resetvalue = 0 };
9032             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9033         }
9034     }
9035 
9036     /*
9037      * Register the base EL2 cpregs.
9038      * Pre v8, these registers are implemented only as part of the
9039      * Virtualization Extensions (EL2 present).  Beginning with v8,
9040      * if EL2 is missing but EL3 is enabled, mostly these become
9041      * RES0 from EL3, with some specific exceptions.
9042      */
9043     if (arm_feature(env, ARM_FEATURE_EL2)
9044         || (arm_feature(env, ARM_FEATURE_EL3)
9045             && arm_feature(env, ARM_FEATURE_V8))) {
9046         uint64_t vmpidr_def = mpidr_read_val(env);
9047         ARMCPRegInfo vpidr_regs[] = {
9048             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9049               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9050               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9051               .resetvalue = cpu->midr,
9052               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9053               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9054             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9055               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9056               .access = PL2_RW, .resetvalue = cpu->midr,
9057               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9058               .nv2_redirect_offset = 0x88,
9059               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9060             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9061               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9062               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9063               .resetvalue = vmpidr_def,
9064               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9065               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9066             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9067               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9068               .access = PL2_RW, .resetvalue = vmpidr_def,
9069               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9070               .nv2_redirect_offset = 0x50,
9071               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9072         };
9073         /*
9074          * The only field of MDCR_EL2 that has a defined architectural reset
9075          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9076          */
9077         ARMCPRegInfo mdcr_el2 = {
9078             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9079             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9080             .writefn = mdcr_el2_write,
9081             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9082             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9083         };
9084         define_one_arm_cp_reg(cpu, &mdcr_el2);
9085         define_arm_cp_regs(cpu, vpidr_regs);
9086         define_arm_cp_regs(cpu, el2_cp_reginfo);
9087         if (arm_feature(env, ARM_FEATURE_V8)) {
9088             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9089         }
9090         if (cpu_isar_feature(aa64_sel2, cpu)) {
9091             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9092         }
9093         /*
9094          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9095          * See commentary near RMR_EL1.
9096          */
9097         if (!arm_feature(env, ARM_FEATURE_EL3)) {
9098             static const ARMCPRegInfo el2_reset_regs[] = {
9099                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9100                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9101                   .access = PL2_R,
9102                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9103                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9104                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9105                   .access = PL2_R,
9106                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9107                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9108                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9109                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9110             };
9111             define_arm_cp_regs(cpu, el2_reset_regs);
9112         }
9113     }
9114 
9115     /* Register the base EL3 cpregs. */
9116     if (arm_feature(env, ARM_FEATURE_EL3)) {
9117         define_arm_cp_regs(cpu, el3_cp_reginfo);
9118         ARMCPRegInfo el3_regs[] = {
9119             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9120               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9121               .access = PL3_R,
9122               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9123             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9124               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9125               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9126             { .name = "RMR", .state = ARM_CP_STATE_AA32,
9127               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9128               .access = PL3_RW, .type = ARM_CP_CONST,
9129               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9130             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9131               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9132               .access = PL3_RW,
9133               .raw_writefn = raw_write, .writefn = sctlr_write,
9134               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9135               .resetvalue = cpu->reset_sctlr },
9136         };
9137 
9138         define_arm_cp_regs(cpu, el3_regs);
9139     }
9140     /*
9141      * The behaviour of NSACR is sufficiently various that we don't
9142      * try to describe it in a single reginfo:
9143      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
9144      *     reads as constant 0xc00 from NS EL1 and NS EL2
9145      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9146      *  if v7 without EL3, register doesn't exist
9147      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9148      */
9149     if (arm_feature(env, ARM_FEATURE_EL3)) {
9150         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9151             static const ARMCPRegInfo nsacr = {
9152                 .name = "NSACR", .type = ARM_CP_CONST,
9153                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9154                 .access = PL1_RW, .accessfn = nsacr_access,
9155                 .resetvalue = 0xc00
9156             };
9157             define_one_arm_cp_reg(cpu, &nsacr);
9158         } else {
9159             static const ARMCPRegInfo nsacr = {
9160                 .name = "NSACR",
9161                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9162                 .access = PL3_RW | PL1_R,
9163                 .resetvalue = 0,
9164                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9165             };
9166             define_one_arm_cp_reg(cpu, &nsacr);
9167         }
9168     } else {
9169         if (arm_feature(env, ARM_FEATURE_V8)) {
9170             static const ARMCPRegInfo nsacr = {
9171                 .name = "NSACR", .type = ARM_CP_CONST,
9172                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9173                 .access = PL1_R,
9174                 .resetvalue = 0xc00
9175             };
9176             define_one_arm_cp_reg(cpu, &nsacr);
9177         }
9178     }
9179 
9180     if (arm_feature(env, ARM_FEATURE_PMSA)) {
9181         if (arm_feature(env, ARM_FEATURE_V6)) {
9182             /* PMSAv6 not implemented */
9183             assert(arm_feature(env, ARM_FEATURE_V7));
9184             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9185             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9186         } else {
9187             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9188         }
9189     } else {
9190         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9191         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9192         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
9193         if (cpu_isar_feature(aa32_hpd, cpu)) {
9194             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9195         }
9196     }
9197     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9198         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9199     }
9200     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9201         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9202     }
9203     if (arm_feature(env, ARM_FEATURE_VAPA)) {
9204         ARMCPRegInfo vapa_cp_reginfo[] = {
9205             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9206               .access = PL1_RW, .resetvalue = 0,
9207               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9208                                      offsetoflow32(CPUARMState, cp15.par_ns) },
9209               .writefn = par_write},
9210 #ifndef CONFIG_USER_ONLY
9211             /* This underdecoding is safe because the reginfo is NO_RAW. */
9212             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9213               .access = PL1_W, .accessfn = ats_access,
9214               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9215 #endif
9216         };
9217 
9218         /*
9219          * When LPAE exists this 32-bit PAR register is an alias of the
9220          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9221          */
9222         if (arm_feature(env, ARM_FEATURE_LPAE)) {
9223             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9224         }
9225         define_arm_cp_regs(cpu, vapa_cp_reginfo);
9226     }
9227     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9228         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9229     }
9230     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9231         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9232     }
9233     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9234         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9235     }
9236     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9237         define_arm_cp_regs(cpu, omap_cp_reginfo);
9238     }
9239     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9240         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9241     }
9242     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9243         define_arm_cp_regs(cpu, xscale_cp_reginfo);
9244     }
9245     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9246         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9247     }
9248     if (arm_feature(env, ARM_FEATURE_LPAE)) {
9249         define_arm_cp_regs(cpu, lpae_cp_reginfo);
9250     }
9251     if (cpu_isar_feature(aa32_jazelle, cpu)) {
9252         define_arm_cp_regs(cpu, jazelle_regs);
9253     }
9254     /*
9255      * Slightly awkwardly, the OMAP and StrongARM cores need all of
9256      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9257      * be read-only (ie write causes UNDEF exception).
9258      */
9259     {
9260         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9261             /*
9262              * Pre-v8 MIDR space.
9263              * Note that the MIDR isn't a simple constant register because
9264              * of the TI925 behaviour where writes to another register can
9265              * cause the MIDR value to change.
9266              *
9267              * Unimplemented registers in the c15 0 0 0 space default to
9268              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9269              * and friends override accordingly.
9270              */
9271             { .name = "MIDR",
9272               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9273               .access = PL1_R, .resetvalue = cpu->midr,
9274               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9275               .readfn = midr_read,
9276               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9277               .type = ARM_CP_OVERRIDE },
9278             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9279             { .name = "DUMMY",
9280               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9281               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9282             { .name = "DUMMY",
9283               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9284               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9285             { .name = "DUMMY",
9286               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9287               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9288             { .name = "DUMMY",
9289               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9290               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9291             { .name = "DUMMY",
9292               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9293               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9294         };
9295         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9296             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9297               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9298               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9299               .fgt = FGT_MIDR_EL1,
9300               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9301               .readfn = midr_read },
9302             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9303             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9304               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9305               .access = PL1_R, .resetvalue = cpu->midr },
9306             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9307               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9308               .access = PL1_R,
9309               .accessfn = access_aa64_tid1,
9310               .fgt = FGT_REVIDR_EL1,
9311               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9312         };
9313         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9314             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9315             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9316             .access = PL1_R, .resetvalue = cpu->midr
9317         };
9318         ARMCPRegInfo id_cp_reginfo[] = {
9319             /* These are common to v8 and pre-v8 */
9320             { .name = "CTR",
9321               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9322               .access = PL1_R, .accessfn = ctr_el0_access,
9323               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9324             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9325               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9326               .access = PL0_R, .accessfn = ctr_el0_access,
9327               .fgt = FGT_CTR_EL0,
9328               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9329             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9330             { .name = "TCMTR",
9331               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9332               .access = PL1_R,
9333               .accessfn = access_aa32_tid1,
9334               .type = ARM_CP_CONST, .resetvalue = 0 },
9335         };
9336         /* TLBTR is specific to VMSA */
9337         ARMCPRegInfo id_tlbtr_reginfo = {
9338               .name = "TLBTR",
9339               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9340               .access = PL1_R,
9341               .accessfn = access_aa32_tid1,
9342               .type = ARM_CP_CONST, .resetvalue = 0,
9343         };
9344         /* MPUIR is specific to PMSA V6+ */
9345         ARMCPRegInfo id_mpuir_reginfo = {
9346               .name = "MPUIR",
9347               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9348               .access = PL1_R, .type = ARM_CP_CONST,
9349               .resetvalue = cpu->pmsav7_dregion << 8
9350         };
9351         /* HMPUIR is specific to PMSA V8 */
9352         ARMCPRegInfo id_hmpuir_reginfo = {
9353             .name = "HMPUIR",
9354             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9355             .access = PL2_R, .type = ARM_CP_CONST,
9356             .resetvalue = cpu->pmsav8r_hdregion
9357         };
9358         static const ARMCPRegInfo crn0_wi_reginfo = {
9359             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9360             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9361             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9362         };
9363 #ifdef CONFIG_USER_ONLY
9364         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9365             { .name = "MIDR_EL1",
9366               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9367                                R_MIDR_EL1_PARTNUM_MASK |
9368                                R_MIDR_EL1_ARCHITECTURE_MASK |
9369                                R_MIDR_EL1_VARIANT_MASK |
9370                                R_MIDR_EL1_IMPLEMENTER_MASK },
9371             { .name = "REVIDR_EL1" },
9372         };
9373         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9374 #endif
9375         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9376             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9377             size_t i;
9378             /*
9379              * Register the blanket "writes ignored" value first to cover the
9380              * whole space. Then update the specific ID registers to allow write
9381              * access, so that they ignore writes rather than causing them to
9382              * UNDEF.
9383              */
9384             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9385             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9386                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9387             }
9388             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9389                 id_cp_reginfo[i].access = PL1_RW;
9390             }
9391             id_mpuir_reginfo.access = PL1_RW;
9392             id_tlbtr_reginfo.access = PL1_RW;
9393         }
9394         if (arm_feature(env, ARM_FEATURE_V8)) {
9395             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9396             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9397                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9398             }
9399         } else {
9400             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9401         }
9402         define_arm_cp_regs(cpu, id_cp_reginfo);
9403         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9404             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9405         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9406                    arm_feature(env, ARM_FEATURE_V8)) {
9407             uint32_t i = 0;
9408             char *tmp_string;
9409 
9410             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9411             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9412             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9413 
9414             /* Register alias is only valid for first 32 indexes */
9415             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9416                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9417                 uint8_t opc1 = extract32(i, 4, 1);
9418                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9419 
9420                 tmp_string = g_strdup_printf("PRBAR%u", i);
9421                 ARMCPRegInfo tmp_prbarn_reginfo = {
9422                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9423                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9424                     .access = PL1_RW, .resetvalue = 0,
9425                     .accessfn = access_tvm_trvm,
9426                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9427                 };
9428                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9429                 g_free(tmp_string);
9430 
9431                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9432                 tmp_string = g_strdup_printf("PRLAR%u", i);
9433                 ARMCPRegInfo tmp_prlarn_reginfo = {
9434                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9435                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9436                     .access = PL1_RW, .resetvalue = 0,
9437                     .accessfn = access_tvm_trvm,
9438                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9439                 };
9440                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9441                 g_free(tmp_string);
9442             }
9443 
9444             /* Register alias is only valid for first 32 indexes */
9445             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9446                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9447                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9448                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9449 
9450                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9451                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9452                     .name = tmp_string,
9453                     .type = ARM_CP_NO_RAW,
9454                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9455                     .access = PL2_RW, .resetvalue = 0,
9456                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9457                 };
9458                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9459                 g_free(tmp_string);
9460 
9461                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9462                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9463                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9464                     .name = tmp_string,
9465                     .type = ARM_CP_NO_RAW,
9466                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9467                     .access = PL2_RW, .resetvalue = 0,
9468                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9469                 };
9470                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9471                 g_free(tmp_string);
9472             }
9473         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9474             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9475         }
9476     }
9477 
9478     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9479         ARMCPRegInfo mpidr_cp_reginfo[] = {
9480             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9481               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9482               .fgt = FGT_MPIDR_EL1,
9483               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9484         };
9485 #ifdef CONFIG_USER_ONLY
9486         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9487             { .name = "MPIDR_EL1",
9488               .fixed_bits = 0x0000000080000000 },
9489         };
9490         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9491 #endif
9492         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9493     }
9494 
9495     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9496         ARMCPRegInfo auxcr_reginfo[] = {
9497             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9498               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9499               .access = PL1_RW, .accessfn = access_tacr,
9500               .nv2_redirect_offset = 0x118,
9501               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9502             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9503               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9504               .access = PL2_RW, .type = ARM_CP_CONST,
9505               .resetvalue = 0 },
9506             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9507               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9508               .access = PL3_RW, .type = ARM_CP_CONST,
9509               .resetvalue = 0 },
9510         };
9511         define_arm_cp_regs(cpu, auxcr_reginfo);
9512         if (cpu_isar_feature(aa32_ac2, cpu)) {
9513             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9514         }
9515     }
9516 
9517     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9518         /*
9519          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9520          * There are two flavours:
9521          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9522          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9523          *      32-bit register visible to AArch32 at a different encoding
9524          *      to the "flavour 1" register and with the bits rearranged to
9525          *      be able to squash a 64-bit address into the 32-bit view.
9526          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9527          * in future if we support AArch32-only configs of some of the
9528          * AArch64 cores we might need to add a specific feature flag
9529          * to indicate cores with "flavour 2" CBAR.
9530          */
9531         if (arm_feature(env, ARM_FEATURE_V8)) {
9532             /* 32 bit view is [31:18] 0...0 [43:32]. */
9533             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9534                 | extract64(cpu->reset_cbar, 32, 12);
9535             ARMCPRegInfo cbar_reginfo[] = {
9536                 { .name = "CBAR",
9537                   .type = ARM_CP_CONST,
9538                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9539                   .access = PL1_R, .resetvalue = cbar32 },
9540                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9541                   .type = ARM_CP_CONST,
9542                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9543                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9544             };
9545             /* We don't implement a r/w 64 bit CBAR currently */
9546             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9547             define_arm_cp_regs(cpu, cbar_reginfo);
9548         } else {
9549             ARMCPRegInfo cbar = {
9550                 .name = "CBAR",
9551                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9552                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9553                 .fieldoffset = offsetof(CPUARMState,
9554                                         cp15.c15_config_base_address)
9555             };
9556             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9557                 cbar.access = PL1_R;
9558                 cbar.fieldoffset = 0;
9559                 cbar.type = ARM_CP_CONST;
9560             }
9561             define_one_arm_cp_reg(cpu, &cbar);
9562         }
9563     }
9564 
9565     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9566         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9567             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9568               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9569               .access = PL1_RW, .writefn = vbar_write,
9570               .accessfn = access_nv1,
9571               .fgt = FGT_VBAR_EL1,
9572               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
9573               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9574                                      offsetof(CPUARMState, cp15.vbar_ns) },
9575               .resetvalue = 0 },
9576         };
9577         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9578     }
9579 
9580     /* Generic registers whose values depend on the implementation */
9581     {
9582         ARMCPRegInfo sctlr = {
9583             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9584             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9585             .access = PL1_RW, .accessfn = access_tvm_trvm,
9586             .fgt = FGT_SCTLR_EL1,
9587             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
9588             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9589                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9590             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9591             .raw_writefn = raw_write,
9592         };
9593         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9594             /*
9595              * Normally we would always end the TB on an SCTLR write, but Linux
9596              * arch/arm/mach-pxa/sleep.S expects two instructions following
9597              * an MMU enable to execute from cache.  Imitate this behaviour.
9598              */
9599             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9600         }
9601         define_one_arm_cp_reg(cpu, &sctlr);
9602 
9603         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9604             arm_feature(env, ARM_FEATURE_V8)) {
9605             ARMCPRegInfo vsctlr = {
9606                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9607                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9608                 .access = PL2_RW, .resetvalue = 0x0,
9609                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9610             };
9611             define_one_arm_cp_reg(cpu, &vsctlr);
9612         }
9613     }
9614 
9615     if (cpu_isar_feature(aa64_lor, cpu)) {
9616         define_arm_cp_regs(cpu, lor_reginfo);
9617     }
9618     if (cpu_isar_feature(aa64_pan, cpu)) {
9619         define_one_arm_cp_reg(cpu, &pan_reginfo);
9620     }
9621 #ifndef CONFIG_USER_ONLY
9622     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9623         define_arm_cp_regs(cpu, ats1e1_reginfo);
9624     }
9625     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9626         define_arm_cp_regs(cpu, ats1cp_reginfo);
9627     }
9628 #endif
9629     if (cpu_isar_feature(aa64_uao, cpu)) {
9630         define_one_arm_cp_reg(cpu, &uao_reginfo);
9631     }
9632 
9633     if (cpu_isar_feature(aa64_dit, cpu)) {
9634         define_one_arm_cp_reg(cpu, &dit_reginfo);
9635     }
9636     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9637         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9638     }
9639     if (cpu_isar_feature(any_ras, cpu)) {
9640         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9641     }
9642 
9643     if (cpu_isar_feature(aa64_vh, cpu) ||
9644         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9645         define_one_arm_cp_reg(cpu, &contextidr_el2);
9646     }
9647     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9648         define_arm_cp_regs(cpu, vhe_reginfo);
9649     }
9650 
9651     if (cpu_isar_feature(aa64_sve, cpu)) {
9652         define_arm_cp_regs(cpu, zcr_reginfo);
9653     }
9654 
9655     if (cpu_isar_feature(aa64_hcx, cpu)) {
9656         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9657     }
9658 
9659 #ifdef TARGET_AARCH64
9660     if (cpu_isar_feature(aa64_sme, cpu)) {
9661         define_arm_cp_regs(cpu, sme_reginfo);
9662     }
9663     if (cpu_isar_feature(aa64_pauth, cpu)) {
9664         define_arm_cp_regs(cpu, pauth_reginfo);
9665     }
9666     if (cpu_isar_feature(aa64_rndr, cpu)) {
9667         define_arm_cp_regs(cpu, rndr_reginfo);
9668     }
9669     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9670         define_arm_cp_regs(cpu, tlbirange_reginfo);
9671     }
9672     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9673         define_arm_cp_regs(cpu, tlbios_reginfo);
9674     }
9675     /* Data Cache clean instructions up to PoP */
9676     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9677         define_one_arm_cp_reg(cpu, dcpop_reg);
9678 
9679         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9680             define_one_arm_cp_reg(cpu, dcpodp_reg);
9681         }
9682     }
9683 
9684     /*
9685      * If full MTE is enabled, add all of the system registers.
9686      * If only "instructions available at EL0" are enabled,
9687      * then define only a RAZ/WI version of PSTATE.TCO.
9688      */
9689     if (cpu_isar_feature(aa64_mte, cpu)) {
9690         ARMCPRegInfo gmid_reginfo = {
9691             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9692             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9693             .access = PL1_R, .accessfn = access_aa64_tid5,
9694             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9695         };
9696         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9697         define_arm_cp_regs(cpu, mte_reginfo);
9698         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9699     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9700         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9701         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9702     }
9703 
9704     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9705         define_arm_cp_regs(cpu, scxtnum_reginfo);
9706     }
9707 
9708     if (cpu_isar_feature(aa64_fgt, cpu)) {
9709         define_arm_cp_regs(cpu, fgt_reginfo);
9710     }
9711 
9712     if (cpu_isar_feature(aa64_rme, cpu)) {
9713         define_arm_cp_regs(cpu, rme_reginfo);
9714         if (cpu_isar_feature(aa64_mte, cpu)) {
9715             define_arm_cp_regs(cpu, rme_mte_reginfo);
9716         }
9717     }
9718 
9719     if (cpu_isar_feature(aa64_nv2, cpu)) {
9720         define_arm_cp_regs(cpu, nv2_reginfo);
9721     }
9722 #endif
9723 
9724     if (cpu_isar_feature(any_predinv, cpu)) {
9725         define_arm_cp_regs(cpu, predinv_reginfo);
9726     }
9727 
9728     if (cpu_isar_feature(any_ccidx, cpu)) {
9729         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9730     }
9731 
9732 #ifndef CONFIG_USER_ONLY
9733     /*
9734      * Register redirections and aliases must be done last,
9735      * after the registers from the other extensions have been defined.
9736      */
9737     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9738         define_arm_vh_e2h_redirects_aliases(cpu);
9739     }
9740 #endif
9741 }
9742 
9743 /*
9744  * Private utility function for define_one_arm_cp_reg_with_opaque():
9745  * add a single reginfo struct to the hash table.
9746  */
9747 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9748                                    void *opaque, CPState state,
9749                                    CPSecureState secstate,
9750                                    int crm, int opc1, int opc2,
9751                                    const char *name)
9752 {
9753     CPUARMState *env = &cpu->env;
9754     uint32_t key;
9755     ARMCPRegInfo *r2;
9756     bool is64 = r->type & ARM_CP_64BIT;
9757     bool ns = secstate & ARM_CP_SECSTATE_NS;
9758     int cp = r->cp;
9759     size_t name_len;
9760     bool make_const;
9761 
9762     switch (state) {
9763     case ARM_CP_STATE_AA32:
9764         /* We assume it is a cp15 register if the .cp field is left unset. */
9765         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9766             cp = 15;
9767         }
9768         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9769         break;
9770     case ARM_CP_STATE_AA64:
9771         /*
9772          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9773          * cp == 0 as equivalent to the value for "standard guest-visible
9774          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9775          * in their AArch64 view (the .cp value may be non-zero for the
9776          * benefit of the AArch32 view).
9777          */
9778         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9779             cp = CP_REG_ARM64_SYSREG_CP;
9780         }
9781         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9782         break;
9783     default:
9784         g_assert_not_reached();
9785     }
9786 
9787     /* Overriding of an existing definition must be explicitly requested. */
9788     if (!(r->type & ARM_CP_OVERRIDE)) {
9789         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9790         if (oldreg) {
9791             assert(oldreg->type & ARM_CP_OVERRIDE);
9792         }
9793     }
9794 
9795     /*
9796      * Eliminate registers that are not present because the EL is missing.
9797      * Doing this here makes it easier to put all registers for a given
9798      * feature into the same ARMCPRegInfo array and define them all at once.
9799      */
9800     make_const = false;
9801     if (arm_feature(env, ARM_FEATURE_EL3)) {
9802         /*
9803          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9804          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9805          */
9806         int min_el = ctz32(r->access) / 2;
9807         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9808             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9809                 return;
9810             }
9811             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9812         }
9813     } else {
9814         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9815                                  ? PL2_RW : PL1_RW);
9816         if ((r->access & max_el) == 0) {
9817             return;
9818         }
9819     }
9820 
9821     /* Combine cpreg and name into one allocation. */
9822     name_len = strlen(name) + 1;
9823     r2 = g_malloc(sizeof(*r2) + name_len);
9824     *r2 = *r;
9825     r2->name = memcpy(r2 + 1, name, name_len);
9826 
9827     /*
9828      * Update fields to match the instantiation, overwiting wildcards
9829      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9830      */
9831     r2->cp = cp;
9832     r2->crm = crm;
9833     r2->opc1 = opc1;
9834     r2->opc2 = opc2;
9835     r2->state = state;
9836     r2->secure = secstate;
9837     if (opaque) {
9838         r2->opaque = opaque;
9839     }
9840 
9841     if (make_const) {
9842         /* This should not have been a very special register to begin. */
9843         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9844         assert(old_special == 0 || old_special == ARM_CP_NOP);
9845         /*
9846          * Set the special function to CONST, retaining the other flags.
9847          * This is important for e.g. ARM_CP_SVE so that we still
9848          * take the SVE trap if CPTR_EL3.EZ == 0.
9849          */
9850         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9851         /*
9852          * Usually, these registers become RES0, but there are a few
9853          * special cases like VPIDR_EL2 which have a constant non-zero
9854          * value with writes ignored.
9855          */
9856         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9857             r2->resetvalue = 0;
9858         }
9859         /*
9860          * ARM_CP_CONST has precedence, so removing the callbacks and
9861          * offsets are not strictly necessary, but it is potentially
9862          * less confusing to debug later.
9863          */
9864         r2->readfn = NULL;
9865         r2->writefn = NULL;
9866         r2->raw_readfn = NULL;
9867         r2->raw_writefn = NULL;
9868         r2->resetfn = NULL;
9869         r2->fieldoffset = 0;
9870         r2->bank_fieldoffsets[0] = 0;
9871         r2->bank_fieldoffsets[1] = 0;
9872     } else {
9873         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9874 
9875         if (isbanked) {
9876             /*
9877              * Register is banked (using both entries in array).
9878              * Overwriting fieldoffset as the array is only used to define
9879              * banked registers but later only fieldoffset is used.
9880              */
9881             r2->fieldoffset = r->bank_fieldoffsets[ns];
9882         }
9883         if (state == ARM_CP_STATE_AA32) {
9884             if (isbanked) {
9885                 /*
9886                  * If the register is banked then we don't need to migrate or
9887                  * reset the 32-bit instance in certain cases:
9888                  *
9889                  * 1) If the register has both 32-bit and 64-bit instances
9890                  *    then we can count on the 64-bit instance taking care
9891                  *    of the non-secure bank.
9892                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9893                  *    version taking care of the secure bank.  This requires
9894                  *    that separate 32 and 64-bit definitions are provided.
9895                  */
9896                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9897                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9898                     r2->type |= ARM_CP_ALIAS;
9899                 }
9900             } else if ((secstate != r->secure) && !ns) {
9901                 /*
9902                  * The register is not banked so we only want to allow
9903                  * migration of the non-secure instance.
9904                  */
9905                 r2->type |= ARM_CP_ALIAS;
9906             }
9907 
9908             if (HOST_BIG_ENDIAN &&
9909                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9910                 r2->fieldoffset += sizeof(uint32_t);
9911             }
9912         }
9913     }
9914 
9915     /*
9916      * By convention, for wildcarded registers only the first
9917      * entry is used for migration; the others are marked as
9918      * ALIAS so we don't try to transfer the register
9919      * multiple times. Special registers (ie NOP/WFI) are
9920      * never migratable and not even raw-accessible.
9921      */
9922     if (r2->type & ARM_CP_SPECIAL_MASK) {
9923         r2->type |= ARM_CP_NO_RAW;
9924     }
9925     if (((r->crm == CP_ANY) && crm != 0) ||
9926         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9927         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9928         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9929     }
9930 
9931     /*
9932      * Check that raw accesses are either forbidden or handled. Note that
9933      * we can't assert this earlier because the setup of fieldoffset for
9934      * banked registers has to be done first.
9935      */
9936     if (!(r2->type & ARM_CP_NO_RAW)) {
9937         assert(!raw_accessors_invalid(r2));
9938     }
9939 
9940     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9941 }
9942 
9943 
9944 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9945                                        const ARMCPRegInfo *r, void *opaque)
9946 {
9947     /*
9948      * Define implementations of coprocessor registers.
9949      * We store these in a hashtable because typically
9950      * there are less than 150 registers in a space which
9951      * is 16*16*16*8*8 = 262144 in size.
9952      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9953      * If a register is defined twice then the second definition is
9954      * used, so this can be used to define some generic registers and
9955      * then override them with implementation specific variations.
9956      * At least one of the original and the second definition should
9957      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9958      * against accidental use.
9959      *
9960      * The state field defines whether the register is to be
9961      * visible in the AArch32 or AArch64 execution state. If the
9962      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9963      * reginfo structure for the AArch32 view, which sees the lower
9964      * 32 bits of the 64 bit register.
9965      *
9966      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9967      * be wildcarded. AArch64 registers are always considered to be 64
9968      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9969      * the register, if any.
9970      */
9971     int crm, opc1, opc2;
9972     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9973     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9974     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9975     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9976     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9977     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9978     CPState state;
9979 
9980     /* 64 bit registers have only CRm and Opc1 fields */
9981     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9982     /* op0 only exists in the AArch64 encodings */
9983     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9984     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9985     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9986     /*
9987      * This API is only for Arm's system coprocessors (14 and 15) or
9988      * (M-profile or v7A-and-earlier only) for implementation defined
9989      * coprocessors in the range 0..7.  Our decode assumes this, since
9990      * 8..13 can be used for other insns including VFP and Neon. See
9991      * valid_cp() in translate.c.  Assert here that we haven't tried
9992      * to use an invalid coprocessor number.
9993      */
9994     switch (r->state) {
9995     case ARM_CP_STATE_BOTH:
9996         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9997         if (r->cp == 0) {
9998             break;
9999         }
10000         /* fall through */
10001     case ARM_CP_STATE_AA32:
10002         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10003             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10004             assert(r->cp >= 14 && r->cp <= 15);
10005         } else {
10006             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10007         }
10008         break;
10009     case ARM_CP_STATE_AA64:
10010         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10011         break;
10012     default:
10013         g_assert_not_reached();
10014     }
10015     /*
10016      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10017      * encodes a minimum access level for the register. We roll this
10018      * runtime check into our general permission check code, so check
10019      * here that the reginfo's specified permissions are strict enough
10020      * to encompass the generic architectural permission check.
10021      */
10022     if (r->state != ARM_CP_STATE_AA32) {
10023         CPAccessRights mask;
10024         switch (r->opc1) {
10025         case 0:
10026             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10027             mask = PL0U_R | PL1_RW;
10028             break;
10029         case 1: case 2:
10030             /* min_EL EL1 */
10031             mask = PL1_RW;
10032             break;
10033         case 3:
10034             /* min_EL EL0 */
10035             mask = PL0_RW;
10036             break;
10037         case 4:
10038         case 5:
10039             /* min_EL EL2 */
10040             mask = PL2_RW;
10041             break;
10042         case 6:
10043             /* min_EL EL3 */
10044             mask = PL3_RW;
10045             break;
10046         case 7:
10047             /* min_EL EL1, secure mode only (we don't check the latter) */
10048             mask = PL1_RW;
10049             break;
10050         default:
10051             /* broken reginfo with out-of-range opc1 */
10052             g_assert_not_reached();
10053         }
10054         /* assert our permissions are not too lax (stricter is fine) */
10055         assert((r->access & ~mask) == 0);
10056     }
10057 
10058     /*
10059      * Check that the register definition has enough info to handle
10060      * reads and writes if they are permitted.
10061      */
10062     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10063         if (r->access & PL3_R) {
10064             assert((r->fieldoffset ||
10065                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10066                    r->readfn);
10067         }
10068         if (r->access & PL3_W) {
10069             assert((r->fieldoffset ||
10070                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10071                    r->writefn);
10072         }
10073     }
10074 
10075     for (crm = crmmin; crm <= crmmax; crm++) {
10076         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10077             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10078                 for (state = ARM_CP_STATE_AA32;
10079                      state <= ARM_CP_STATE_AA64; state++) {
10080                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10081                         continue;
10082                     }
10083                     if (state == ARM_CP_STATE_AA32) {
10084                         /*
10085                          * Under AArch32 CP registers can be common
10086                          * (same for secure and non-secure world) or banked.
10087                          */
10088                         char *name;
10089 
10090                         switch (r->secure) {
10091                         case ARM_CP_SECSTATE_S:
10092                         case ARM_CP_SECSTATE_NS:
10093                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10094                                                    r->secure, crm, opc1, opc2,
10095                                                    r->name);
10096                             break;
10097                         case ARM_CP_SECSTATE_BOTH:
10098                             name = g_strdup_printf("%s_S", r->name);
10099                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10100                                                    ARM_CP_SECSTATE_S,
10101                                                    crm, opc1, opc2, name);
10102                             g_free(name);
10103                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10104                                                    ARM_CP_SECSTATE_NS,
10105                                                    crm, opc1, opc2, r->name);
10106                             break;
10107                         default:
10108                             g_assert_not_reached();
10109                         }
10110                     } else {
10111                         /*
10112                          * AArch64 registers get mapped to non-secure instance
10113                          * of AArch32
10114                          */
10115                         add_cpreg_to_hashtable(cpu, r, opaque, state,
10116                                                ARM_CP_SECSTATE_NS,
10117                                                crm, opc1, opc2, r->name);
10118                     }
10119                 }
10120             }
10121         }
10122     }
10123 }
10124 
10125 /* Define a whole list of registers */
10126 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10127                                         void *opaque, size_t len)
10128 {
10129     size_t i;
10130     for (i = 0; i < len; ++i) {
10131         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10132     }
10133 }
10134 
10135 /*
10136  * Modify ARMCPRegInfo for access from userspace.
10137  *
10138  * This is a data driven modification directed by
10139  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10140  * user-space cannot alter any values and dynamic values pertaining to
10141  * execution state are hidden from user space view anyway.
10142  */
10143 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10144                                  const ARMCPRegUserSpaceInfo *mods,
10145                                  size_t mods_len)
10146 {
10147     for (size_t mi = 0; mi < mods_len; ++mi) {
10148         const ARMCPRegUserSpaceInfo *m = mods + mi;
10149         GPatternSpec *pat = NULL;
10150 
10151         if (m->is_glob) {
10152             pat = g_pattern_spec_new(m->name);
10153         }
10154         for (size_t ri = 0; ri < regs_len; ++ri) {
10155             ARMCPRegInfo *r = regs + ri;
10156 
10157             if (pat && g_pattern_match_string(pat, r->name)) {
10158                 r->type = ARM_CP_CONST;
10159                 r->access = PL0U_R;
10160                 r->resetvalue = 0;
10161                 /* continue */
10162             } else if (strcmp(r->name, m->name) == 0) {
10163                 r->type = ARM_CP_CONST;
10164                 r->access = PL0U_R;
10165                 r->resetvalue &= m->exported_bits;
10166                 r->resetvalue |= m->fixed_bits;
10167                 break;
10168             }
10169         }
10170         if (pat) {
10171             g_pattern_spec_free(pat);
10172         }
10173     }
10174 }
10175 
10176 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10177 {
10178     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10179 }
10180 
10181 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10182                          uint64_t value)
10183 {
10184     /* Helper coprocessor write function for write-ignore registers */
10185 }
10186 
10187 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10188 {
10189     /* Helper coprocessor write function for read-as-zero registers */
10190     return 0;
10191 }
10192 
10193 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10194 {
10195     /* Helper coprocessor reset function for do-nothing-on-reset registers */
10196 }
10197 
10198 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10199 {
10200     /*
10201      * Return true if it is not valid for us to switch to
10202      * this CPU mode (ie all the UNPREDICTABLE cases in
10203      * the ARM ARM CPSRWriteByInstr pseudocode).
10204      */
10205 
10206     /* Changes to or from Hyp via MSR and CPS are illegal. */
10207     if (write_type == CPSRWriteByInstr &&
10208         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10209          mode == ARM_CPU_MODE_HYP)) {
10210         return 1;
10211     }
10212 
10213     switch (mode) {
10214     case ARM_CPU_MODE_USR:
10215         return 0;
10216     case ARM_CPU_MODE_SYS:
10217     case ARM_CPU_MODE_SVC:
10218     case ARM_CPU_MODE_ABT:
10219     case ARM_CPU_MODE_UND:
10220     case ARM_CPU_MODE_IRQ:
10221     case ARM_CPU_MODE_FIQ:
10222         /*
10223          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10224          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10225          */
10226         /*
10227          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10228          * and CPS are treated as illegal mode changes.
10229          */
10230         if (write_type == CPSRWriteByInstr &&
10231             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10232             (arm_hcr_el2_eff(env) & HCR_TGE)) {
10233             return 1;
10234         }
10235         return 0;
10236     case ARM_CPU_MODE_HYP:
10237         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10238     case ARM_CPU_MODE_MON:
10239         return arm_current_el(env) < 3;
10240     default:
10241         return 1;
10242     }
10243 }
10244 
10245 uint32_t cpsr_read(CPUARMState *env)
10246 {
10247     int ZF;
10248     ZF = (env->ZF == 0);
10249     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10250         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10251         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10252         | ((env->condexec_bits & 0xfc) << 8)
10253         | (env->GE << 16) | (env->daif & CPSR_AIF);
10254 }
10255 
10256 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10257                 CPSRWriteType write_type)
10258 {
10259     uint32_t changed_daif;
10260     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10261         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10262 
10263     if (mask & CPSR_NZCV) {
10264         env->ZF = (~val) & CPSR_Z;
10265         env->NF = val;
10266         env->CF = (val >> 29) & 1;
10267         env->VF = (val << 3) & 0x80000000;
10268     }
10269     if (mask & CPSR_Q) {
10270         env->QF = ((val & CPSR_Q) != 0);
10271     }
10272     if (mask & CPSR_T) {
10273         env->thumb = ((val & CPSR_T) != 0);
10274     }
10275     if (mask & CPSR_IT_0_1) {
10276         env->condexec_bits &= ~3;
10277         env->condexec_bits |= (val >> 25) & 3;
10278     }
10279     if (mask & CPSR_IT_2_7) {
10280         env->condexec_bits &= 3;
10281         env->condexec_bits |= (val >> 8) & 0xfc;
10282     }
10283     if (mask & CPSR_GE) {
10284         env->GE = (val >> 16) & 0xf;
10285     }
10286 
10287     /*
10288      * In a V7 implementation that includes the security extensions but does
10289      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10290      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10291      * bits respectively.
10292      *
10293      * In a V8 implementation, it is permitted for privileged software to
10294      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10295      */
10296     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10297         arm_feature(env, ARM_FEATURE_EL3) &&
10298         !arm_feature(env, ARM_FEATURE_EL2) &&
10299         !arm_is_secure(env)) {
10300 
10301         changed_daif = (env->daif ^ val) & mask;
10302 
10303         if (changed_daif & CPSR_A) {
10304             /*
10305              * Check to see if we are allowed to change the masking of async
10306              * abort exceptions from a non-secure state.
10307              */
10308             if (!(env->cp15.scr_el3 & SCR_AW)) {
10309                 qemu_log_mask(LOG_GUEST_ERROR,
10310                               "Ignoring attempt to switch CPSR_A flag from "
10311                               "non-secure world with SCR.AW bit clear\n");
10312                 mask &= ~CPSR_A;
10313             }
10314         }
10315 
10316         if (changed_daif & CPSR_F) {
10317             /*
10318              * Check to see if we are allowed to change the masking of FIQ
10319              * exceptions from a non-secure state.
10320              */
10321             if (!(env->cp15.scr_el3 & SCR_FW)) {
10322                 qemu_log_mask(LOG_GUEST_ERROR,
10323                               "Ignoring attempt to switch CPSR_F flag from "
10324                               "non-secure world with SCR.FW bit clear\n");
10325                 mask &= ~CPSR_F;
10326             }
10327 
10328             /*
10329              * Check whether non-maskable FIQ (NMFI) support is enabled.
10330              * If this bit is set software is not allowed to mask
10331              * FIQs, but is allowed to set CPSR_F to 0.
10332              */
10333             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10334                 (val & CPSR_F)) {
10335                 qemu_log_mask(LOG_GUEST_ERROR,
10336                               "Ignoring attempt to enable CPSR_F flag "
10337                               "(non-maskable FIQ [NMFI] support enabled)\n");
10338                 mask &= ~CPSR_F;
10339             }
10340         }
10341     }
10342 
10343     env->daif &= ~(CPSR_AIF & mask);
10344     env->daif |= val & CPSR_AIF & mask;
10345 
10346     if (write_type != CPSRWriteRaw &&
10347         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10348         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10349             /*
10350              * Note that we can only get here in USR mode if this is a
10351              * gdb stub write; for this case we follow the architectural
10352              * behaviour for guest writes in USR mode of ignoring an attempt
10353              * to switch mode. (Those are caught by translate.c for writes
10354              * triggered by guest instructions.)
10355              */
10356             mask &= ~CPSR_M;
10357         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10358             /*
10359              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10360              * v7, and has defined behaviour in v8:
10361              *  + leave CPSR.M untouched
10362              *  + allow changes to the other CPSR fields
10363              *  + set PSTATE.IL
10364              * For user changes via the GDB stub, we don't set PSTATE.IL,
10365              * as this would be unnecessarily harsh for a user error.
10366              */
10367             mask &= ~CPSR_M;
10368             if (write_type != CPSRWriteByGDBStub &&
10369                 arm_feature(env, ARM_FEATURE_V8)) {
10370                 mask |= CPSR_IL;
10371                 val |= CPSR_IL;
10372             }
10373             qemu_log_mask(LOG_GUEST_ERROR,
10374                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10375                           aarch32_mode_name(env->uncached_cpsr),
10376                           aarch32_mode_name(val));
10377         } else {
10378             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10379                           write_type == CPSRWriteExceptionReturn ?
10380                           "Exception return from AArch32" :
10381                           "AArch32 mode switch from",
10382                           aarch32_mode_name(env->uncached_cpsr),
10383                           aarch32_mode_name(val), env->regs[15]);
10384             switch_mode(env, val & CPSR_M);
10385         }
10386     }
10387     mask &= ~CACHED_CPSR_BITS;
10388     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10389     if (tcg_enabled() && rebuild_hflags) {
10390         arm_rebuild_hflags(env);
10391     }
10392 }
10393 
10394 #ifdef CONFIG_USER_ONLY
10395 
10396 static void switch_mode(CPUARMState *env, int mode)
10397 {
10398     ARMCPU *cpu = env_archcpu(env);
10399 
10400     if (mode != ARM_CPU_MODE_USR) {
10401         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10402     }
10403 }
10404 
10405 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10406                                  uint32_t cur_el, bool secure)
10407 {
10408     return 1;
10409 }
10410 
10411 void aarch64_sync_64_to_32(CPUARMState *env)
10412 {
10413     g_assert_not_reached();
10414 }
10415 
10416 #else
10417 
10418 static void switch_mode(CPUARMState *env, int mode)
10419 {
10420     int old_mode;
10421     int i;
10422 
10423     old_mode = env->uncached_cpsr & CPSR_M;
10424     if (mode == old_mode) {
10425         return;
10426     }
10427 
10428     if (old_mode == ARM_CPU_MODE_FIQ) {
10429         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10430         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10431     } else if (mode == ARM_CPU_MODE_FIQ) {
10432         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10433         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10434     }
10435 
10436     i = bank_number(old_mode);
10437     env->banked_r13[i] = env->regs[13];
10438     env->banked_spsr[i] = env->spsr;
10439 
10440     i = bank_number(mode);
10441     env->regs[13] = env->banked_r13[i];
10442     env->spsr = env->banked_spsr[i];
10443 
10444     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10445     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10446 }
10447 
10448 /*
10449  * Physical Interrupt Target EL Lookup Table
10450  *
10451  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10452  *
10453  * The below multi-dimensional table is used for looking up the target
10454  * exception level given numerous condition criteria.  Specifically, the
10455  * target EL is based on SCR and HCR routing controls as well as the
10456  * currently executing EL and secure state.
10457  *
10458  *    Dimensions:
10459  *    target_el_table[2][2][2][2][2][4]
10460  *                    |  |  |  |  |  +--- Current EL
10461  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10462  *                    |  |  |  +--------- HCR mask override
10463  *                    |  |  +------------ SCR exec state control
10464  *                    |  +--------------- SCR mask override
10465  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10466  *
10467  *    The table values are as such:
10468  *    0-3 = EL0-EL3
10469  *     -1 = Cannot occur
10470  *
10471  * The ARM ARM target EL table includes entries indicating that an "exception
10472  * is not taken".  The two cases where this is applicable are:
10473  *    1) An exception is taken from EL3 but the SCR does not have the exception
10474  *    routed to EL3.
10475  *    2) An exception is taken from EL2 but the HCR does not have the exception
10476  *    routed to EL2.
10477  * In these two cases, the below table contain a target of EL1.  This value is
10478  * returned as it is expected that the consumer of the table data will check
10479  * for "target EL >= current EL" to ensure the exception is not taken.
10480  *
10481  *            SCR     HCR
10482  *         64  EA     AMO                 From
10483  *        BIT IRQ     IMO      Non-secure         Secure
10484  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10485  */
10486 static const int8_t target_el_table[2][2][2][2][2][4] = {
10487     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10488        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10489       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10490        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10491      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10492        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10493       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10494        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10495     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10496        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10497       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10498        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10499      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10500        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10501       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10502        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10503 };
10504 
10505 /*
10506  * Determine the target EL for physical exceptions
10507  */
10508 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10509                                  uint32_t cur_el, bool secure)
10510 {
10511     CPUARMState *env = cpu_env(cs);
10512     bool rw;
10513     bool scr;
10514     bool hcr;
10515     int target_el;
10516     /* Is the highest EL AArch64? */
10517     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10518     uint64_t hcr_el2;
10519 
10520     if (arm_feature(env, ARM_FEATURE_EL3)) {
10521         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10522     } else {
10523         /*
10524          * Either EL2 is the highest EL (and so the EL2 register width
10525          * is given by is64); or there is no EL2 or EL3, in which case
10526          * the value of 'rw' does not affect the table lookup anyway.
10527          */
10528         rw = is64;
10529     }
10530 
10531     hcr_el2 = arm_hcr_el2_eff(env);
10532     switch (excp_idx) {
10533     case EXCP_IRQ:
10534         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10535         hcr = hcr_el2 & HCR_IMO;
10536         break;
10537     case EXCP_FIQ:
10538         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10539         hcr = hcr_el2 & HCR_FMO;
10540         break;
10541     default:
10542         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10543         hcr = hcr_el2 & HCR_AMO;
10544         break;
10545     };
10546 
10547     /*
10548      * For these purposes, TGE and AMO/IMO/FMO both force the
10549      * interrupt to EL2.  Fold TGE into the bit extracted above.
10550      */
10551     hcr |= (hcr_el2 & HCR_TGE) != 0;
10552 
10553     /* Perform a table-lookup for the target EL given the current state */
10554     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10555 
10556     assert(target_el > 0);
10557 
10558     return target_el;
10559 }
10560 
10561 void arm_log_exception(CPUState *cs)
10562 {
10563     int idx = cs->exception_index;
10564 
10565     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10566         const char *exc = NULL;
10567         static const char * const excnames[] = {
10568             [EXCP_UDEF] = "Undefined Instruction",
10569             [EXCP_SWI] = "SVC",
10570             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10571             [EXCP_DATA_ABORT] = "Data Abort",
10572             [EXCP_IRQ] = "IRQ",
10573             [EXCP_FIQ] = "FIQ",
10574             [EXCP_BKPT] = "Breakpoint",
10575             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10576             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10577             [EXCP_HVC] = "Hypervisor Call",
10578             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10579             [EXCP_SMC] = "Secure Monitor Call",
10580             [EXCP_VIRQ] = "Virtual IRQ",
10581             [EXCP_VFIQ] = "Virtual FIQ",
10582             [EXCP_SEMIHOST] = "Semihosting call",
10583             [EXCP_NOCP] = "v7M NOCP UsageFault",
10584             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10585             [EXCP_STKOF] = "v8M STKOF UsageFault",
10586             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10587             [EXCP_LSERR] = "v8M LSERR UsageFault",
10588             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10589             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10590             [EXCP_VSERR] = "Virtual SERR",
10591             [EXCP_GPC] = "Granule Protection Check",
10592         };
10593 
10594         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10595             exc = excnames[idx];
10596         }
10597         if (!exc) {
10598             exc = "unknown";
10599         }
10600         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10601                       idx, exc, cs->cpu_index);
10602     }
10603 }
10604 
10605 /*
10606  * Function used to synchronize QEMU's AArch64 register set with AArch32
10607  * register set.  This is necessary when switching between AArch32 and AArch64
10608  * execution state.
10609  */
10610 void aarch64_sync_32_to_64(CPUARMState *env)
10611 {
10612     int i;
10613     uint32_t mode = env->uncached_cpsr & CPSR_M;
10614 
10615     /* We can blanket copy R[0:7] to X[0:7] */
10616     for (i = 0; i < 8; i++) {
10617         env->xregs[i] = env->regs[i];
10618     }
10619 
10620     /*
10621      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10622      * Otherwise, they come from the banked user regs.
10623      */
10624     if (mode == ARM_CPU_MODE_FIQ) {
10625         for (i = 8; i < 13; i++) {
10626             env->xregs[i] = env->usr_regs[i - 8];
10627         }
10628     } else {
10629         for (i = 8; i < 13; i++) {
10630             env->xregs[i] = env->regs[i];
10631         }
10632     }
10633 
10634     /*
10635      * Registers x13-x23 are the various mode SP and FP registers. Registers
10636      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10637      * from the mode banked register.
10638      */
10639     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10640         env->xregs[13] = env->regs[13];
10641         env->xregs[14] = env->regs[14];
10642     } else {
10643         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10644         /* HYP is an exception in that it is copied from r14 */
10645         if (mode == ARM_CPU_MODE_HYP) {
10646             env->xregs[14] = env->regs[14];
10647         } else {
10648             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10649         }
10650     }
10651 
10652     if (mode == ARM_CPU_MODE_HYP) {
10653         env->xregs[15] = env->regs[13];
10654     } else {
10655         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10656     }
10657 
10658     if (mode == ARM_CPU_MODE_IRQ) {
10659         env->xregs[16] = env->regs[14];
10660         env->xregs[17] = env->regs[13];
10661     } else {
10662         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10663         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10664     }
10665 
10666     if (mode == ARM_CPU_MODE_SVC) {
10667         env->xregs[18] = env->regs[14];
10668         env->xregs[19] = env->regs[13];
10669     } else {
10670         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10671         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10672     }
10673 
10674     if (mode == ARM_CPU_MODE_ABT) {
10675         env->xregs[20] = env->regs[14];
10676         env->xregs[21] = env->regs[13];
10677     } else {
10678         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10679         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10680     }
10681 
10682     if (mode == ARM_CPU_MODE_UND) {
10683         env->xregs[22] = env->regs[14];
10684         env->xregs[23] = env->regs[13];
10685     } else {
10686         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10687         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10688     }
10689 
10690     /*
10691      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10692      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10693      * FIQ bank for r8-r14.
10694      */
10695     if (mode == ARM_CPU_MODE_FIQ) {
10696         for (i = 24; i < 31; i++) {
10697             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10698         }
10699     } else {
10700         for (i = 24; i < 29; i++) {
10701             env->xregs[i] = env->fiq_regs[i - 24];
10702         }
10703         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10704         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10705     }
10706 
10707     env->pc = env->regs[15];
10708 }
10709 
10710 /*
10711  * Function used to synchronize QEMU's AArch32 register set with AArch64
10712  * register set.  This is necessary when switching between AArch32 and AArch64
10713  * execution state.
10714  */
10715 void aarch64_sync_64_to_32(CPUARMState *env)
10716 {
10717     int i;
10718     uint32_t mode = env->uncached_cpsr & CPSR_M;
10719 
10720     /* We can blanket copy X[0:7] to R[0:7] */
10721     for (i = 0; i < 8; i++) {
10722         env->regs[i] = env->xregs[i];
10723     }
10724 
10725     /*
10726      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10727      * Otherwise, we copy x8-x12 into the banked user regs.
10728      */
10729     if (mode == ARM_CPU_MODE_FIQ) {
10730         for (i = 8; i < 13; i++) {
10731             env->usr_regs[i - 8] = env->xregs[i];
10732         }
10733     } else {
10734         for (i = 8; i < 13; i++) {
10735             env->regs[i] = env->xregs[i];
10736         }
10737     }
10738 
10739     /*
10740      * Registers r13 & r14 depend on the current mode.
10741      * If we are in a given mode, we copy the corresponding x registers to r13
10742      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10743      * for the mode.
10744      */
10745     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10746         env->regs[13] = env->xregs[13];
10747         env->regs[14] = env->xregs[14];
10748     } else {
10749         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10750 
10751         /*
10752          * HYP is an exception in that it does not have its own banked r14 but
10753          * shares the USR r14
10754          */
10755         if (mode == ARM_CPU_MODE_HYP) {
10756             env->regs[14] = env->xregs[14];
10757         } else {
10758             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10759         }
10760     }
10761 
10762     if (mode == ARM_CPU_MODE_HYP) {
10763         env->regs[13] = env->xregs[15];
10764     } else {
10765         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10766     }
10767 
10768     if (mode == ARM_CPU_MODE_IRQ) {
10769         env->regs[14] = env->xregs[16];
10770         env->regs[13] = env->xregs[17];
10771     } else {
10772         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10773         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10774     }
10775 
10776     if (mode == ARM_CPU_MODE_SVC) {
10777         env->regs[14] = env->xregs[18];
10778         env->regs[13] = env->xregs[19];
10779     } else {
10780         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10781         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10782     }
10783 
10784     if (mode == ARM_CPU_MODE_ABT) {
10785         env->regs[14] = env->xregs[20];
10786         env->regs[13] = env->xregs[21];
10787     } else {
10788         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10789         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10790     }
10791 
10792     if (mode == ARM_CPU_MODE_UND) {
10793         env->regs[14] = env->xregs[22];
10794         env->regs[13] = env->xregs[23];
10795     } else {
10796         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10797         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10798     }
10799 
10800     /*
10801      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10802      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10803      * FIQ bank for r8-r14.
10804      */
10805     if (mode == ARM_CPU_MODE_FIQ) {
10806         for (i = 24; i < 31; i++) {
10807             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10808         }
10809     } else {
10810         for (i = 24; i < 29; i++) {
10811             env->fiq_regs[i - 24] = env->xregs[i];
10812         }
10813         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10814         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10815     }
10816 
10817     env->regs[15] = env->pc;
10818 }
10819 
10820 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10821                                    uint32_t mask, uint32_t offset,
10822                                    uint32_t newpc)
10823 {
10824     int new_el;
10825 
10826     /* Change the CPU state so as to actually take the exception. */
10827     switch_mode(env, new_mode);
10828 
10829     /*
10830      * For exceptions taken to AArch32 we must clear the SS bit in both
10831      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10832      */
10833     env->pstate &= ~PSTATE_SS;
10834     env->spsr = cpsr_read(env);
10835     /* Clear IT bits.  */
10836     env->condexec_bits = 0;
10837     /* Switch to the new mode, and to the correct instruction set.  */
10838     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10839 
10840     /* This must be after mode switching. */
10841     new_el = arm_current_el(env);
10842 
10843     /* Set new mode endianness */
10844     env->uncached_cpsr &= ~CPSR_E;
10845     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10846         env->uncached_cpsr |= CPSR_E;
10847     }
10848     /* J and IL must always be cleared for exception entry */
10849     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10850     env->daif |= mask;
10851 
10852     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10853         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10854             env->uncached_cpsr |= CPSR_SSBS;
10855         } else {
10856             env->uncached_cpsr &= ~CPSR_SSBS;
10857         }
10858     }
10859 
10860     if (new_mode == ARM_CPU_MODE_HYP) {
10861         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10862         env->elr_el[2] = env->regs[15];
10863     } else {
10864         /* CPSR.PAN is normally preserved preserved unless...  */
10865         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10866             switch (new_el) {
10867             case 3:
10868                 if (!arm_is_secure_below_el3(env)) {
10869                     /* ... the target is EL3, from non-secure state.  */
10870                     env->uncached_cpsr &= ~CPSR_PAN;
10871                     break;
10872                 }
10873                 /* ... the target is EL3, from secure state ... */
10874                 /* fall through */
10875             case 1:
10876                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10877                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10878                     env->uncached_cpsr |= CPSR_PAN;
10879                 }
10880                 break;
10881             }
10882         }
10883         /*
10884          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10885          * and we should just guard the thumb mode on V4
10886          */
10887         if (arm_feature(env, ARM_FEATURE_V4T)) {
10888             env->thumb =
10889                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10890         }
10891         env->regs[14] = env->regs[15] + offset;
10892     }
10893     env->regs[15] = newpc;
10894 
10895     if (tcg_enabled()) {
10896         arm_rebuild_hflags(env);
10897     }
10898 }
10899 
10900 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10901 {
10902     /*
10903      * Handle exception entry to Hyp mode; this is sufficiently
10904      * different to entry to other AArch32 modes that we handle it
10905      * separately here.
10906      *
10907      * The vector table entry used is always the 0x14 Hyp mode entry point,
10908      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10909      * The offset applied to the preferred return address is always zero
10910      * (see DDI0487C.a section G1.12.3).
10911      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10912      */
10913     uint32_t addr, mask;
10914     ARMCPU *cpu = ARM_CPU(cs);
10915     CPUARMState *env = &cpu->env;
10916 
10917     switch (cs->exception_index) {
10918     case EXCP_UDEF:
10919         addr = 0x04;
10920         break;
10921     case EXCP_SWI:
10922         addr = 0x08;
10923         break;
10924     case EXCP_BKPT:
10925         /* Fall through to prefetch abort.  */
10926     case EXCP_PREFETCH_ABORT:
10927         env->cp15.ifar_s = env->exception.vaddress;
10928         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10929                       (uint32_t)env->exception.vaddress);
10930         addr = 0x0c;
10931         break;
10932     case EXCP_DATA_ABORT:
10933         env->cp15.dfar_s = env->exception.vaddress;
10934         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10935                       (uint32_t)env->exception.vaddress);
10936         addr = 0x10;
10937         break;
10938     case EXCP_IRQ:
10939         addr = 0x18;
10940         break;
10941     case EXCP_FIQ:
10942         addr = 0x1c;
10943         break;
10944     case EXCP_HVC:
10945         addr = 0x08;
10946         break;
10947     case EXCP_HYP_TRAP:
10948         addr = 0x14;
10949         break;
10950     default:
10951         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10952     }
10953 
10954     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10955         if (!arm_feature(env, ARM_FEATURE_V8)) {
10956             /*
10957              * QEMU syndrome values are v8-style. v7 has the IL bit
10958              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10959              * If this is a v7 CPU, squash the IL bit in those cases.
10960              */
10961             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10962                 (cs->exception_index == EXCP_DATA_ABORT &&
10963                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10964                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10965                 env->exception.syndrome &= ~ARM_EL_IL;
10966             }
10967         }
10968         env->cp15.esr_el[2] = env->exception.syndrome;
10969     }
10970 
10971     if (arm_current_el(env) != 2 && addr < 0x14) {
10972         addr = 0x14;
10973     }
10974 
10975     mask = 0;
10976     if (!(env->cp15.scr_el3 & SCR_EA)) {
10977         mask |= CPSR_A;
10978     }
10979     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10980         mask |= CPSR_I;
10981     }
10982     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10983         mask |= CPSR_F;
10984     }
10985 
10986     addr += env->cp15.hvbar;
10987 
10988     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10989 }
10990 
10991 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10992 {
10993     ARMCPU *cpu = ARM_CPU(cs);
10994     CPUARMState *env = &cpu->env;
10995     uint32_t addr;
10996     uint32_t mask;
10997     int new_mode;
10998     uint32_t offset;
10999     uint32_t moe;
11000 
11001     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11002     switch (syn_get_ec(env->exception.syndrome)) {
11003     case EC_BREAKPOINT:
11004     case EC_BREAKPOINT_SAME_EL:
11005         moe = 1;
11006         break;
11007     case EC_WATCHPOINT:
11008     case EC_WATCHPOINT_SAME_EL:
11009         moe = 10;
11010         break;
11011     case EC_AA32_BKPT:
11012         moe = 3;
11013         break;
11014     case EC_VECTORCATCH:
11015         moe = 5;
11016         break;
11017     default:
11018         moe = 0;
11019         break;
11020     }
11021 
11022     if (moe) {
11023         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11024     }
11025 
11026     if (env->exception.target_el == 2) {
11027         /* Debug exceptions are reported differently on AArch32 */
11028         switch (syn_get_ec(env->exception.syndrome)) {
11029         case EC_BREAKPOINT:
11030         case EC_BREAKPOINT_SAME_EL:
11031         case EC_AA32_BKPT:
11032         case EC_VECTORCATCH:
11033             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11034                                                      0, 0, 0x22);
11035             break;
11036         case EC_WATCHPOINT:
11037             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11038                                                  EC_DATAABORT);
11039             break;
11040         case EC_WATCHPOINT_SAME_EL:
11041             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11042                                                  EC_DATAABORT_SAME_EL);
11043             break;
11044         }
11045         arm_cpu_do_interrupt_aarch32_hyp(cs);
11046         return;
11047     }
11048 
11049     switch (cs->exception_index) {
11050     case EXCP_UDEF:
11051         new_mode = ARM_CPU_MODE_UND;
11052         addr = 0x04;
11053         mask = CPSR_I;
11054         if (env->thumb) {
11055             offset = 2;
11056         } else {
11057             offset = 4;
11058         }
11059         break;
11060     case EXCP_SWI:
11061         new_mode = ARM_CPU_MODE_SVC;
11062         addr = 0x08;
11063         mask = CPSR_I;
11064         /* The PC already points to the next instruction.  */
11065         offset = 0;
11066         break;
11067     case EXCP_BKPT:
11068         /* Fall through to prefetch abort.  */
11069     case EXCP_PREFETCH_ABORT:
11070         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11071         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11072         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11073                       env->exception.fsr, (uint32_t)env->exception.vaddress);
11074         new_mode = ARM_CPU_MODE_ABT;
11075         addr = 0x0c;
11076         mask = CPSR_A | CPSR_I;
11077         offset = 4;
11078         break;
11079     case EXCP_DATA_ABORT:
11080         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11081         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11082         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11083                       env->exception.fsr,
11084                       (uint32_t)env->exception.vaddress);
11085         new_mode = ARM_CPU_MODE_ABT;
11086         addr = 0x10;
11087         mask = CPSR_A | CPSR_I;
11088         offset = 8;
11089         break;
11090     case EXCP_IRQ:
11091         new_mode = ARM_CPU_MODE_IRQ;
11092         addr = 0x18;
11093         /* Disable IRQ and imprecise data aborts.  */
11094         mask = CPSR_A | CPSR_I;
11095         offset = 4;
11096         if (env->cp15.scr_el3 & SCR_IRQ) {
11097             /* IRQ routed to monitor mode */
11098             new_mode = ARM_CPU_MODE_MON;
11099             mask |= CPSR_F;
11100         }
11101         break;
11102     case EXCP_FIQ:
11103         new_mode = ARM_CPU_MODE_FIQ;
11104         addr = 0x1c;
11105         /* Disable FIQ, IRQ and imprecise data aborts.  */
11106         mask = CPSR_A | CPSR_I | CPSR_F;
11107         if (env->cp15.scr_el3 & SCR_FIQ) {
11108             /* FIQ routed to monitor mode */
11109             new_mode = ARM_CPU_MODE_MON;
11110         }
11111         offset = 4;
11112         break;
11113     case EXCP_VIRQ:
11114         new_mode = ARM_CPU_MODE_IRQ;
11115         addr = 0x18;
11116         /* Disable IRQ and imprecise data aborts.  */
11117         mask = CPSR_A | CPSR_I;
11118         offset = 4;
11119         break;
11120     case EXCP_VFIQ:
11121         new_mode = ARM_CPU_MODE_FIQ;
11122         addr = 0x1c;
11123         /* Disable FIQ, IRQ and imprecise data aborts.  */
11124         mask = CPSR_A | CPSR_I | CPSR_F;
11125         offset = 4;
11126         break;
11127     case EXCP_VSERR:
11128         {
11129             /*
11130              * Note that this is reported as a data abort, but the DFAR
11131              * has an UNKNOWN value.  Construct the SError syndrome from
11132              * AET and ExT fields.
11133              */
11134             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11135 
11136             if (extended_addresses_enabled(env)) {
11137                 env->exception.fsr = arm_fi_to_lfsc(&fi);
11138             } else {
11139                 env->exception.fsr = arm_fi_to_sfsc(&fi);
11140             }
11141             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11142             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11143             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11144                           env->exception.fsr);
11145 
11146             new_mode = ARM_CPU_MODE_ABT;
11147             addr = 0x10;
11148             mask = CPSR_A | CPSR_I;
11149             offset = 8;
11150         }
11151         break;
11152     case EXCP_SMC:
11153         new_mode = ARM_CPU_MODE_MON;
11154         addr = 0x08;
11155         mask = CPSR_A | CPSR_I | CPSR_F;
11156         offset = 0;
11157         break;
11158     default:
11159         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11160         return; /* Never happens.  Keep compiler happy.  */
11161     }
11162 
11163     if (new_mode == ARM_CPU_MODE_MON) {
11164         addr += env->cp15.mvbar;
11165     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11166         /* High vectors. When enabled, base address cannot be remapped. */
11167         addr += 0xffff0000;
11168     } else {
11169         /*
11170          * ARM v7 architectures provide a vector base address register to remap
11171          * the interrupt vector table.
11172          * This register is only followed in non-monitor mode, and is banked.
11173          * Note: only bits 31:5 are valid.
11174          */
11175         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11176     }
11177 
11178     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11179         env->cp15.scr_el3 &= ~SCR_NS;
11180     }
11181 
11182     take_aarch32_exception(env, new_mode, mask, offset, addr);
11183 }
11184 
11185 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11186 {
11187     /*
11188      * Return the register number of the AArch64 view of the AArch32
11189      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11190      * be that of the AArch32 mode the exception came from.
11191      */
11192     int mode = env->uncached_cpsr & CPSR_M;
11193 
11194     switch (aarch32_reg) {
11195     case 0 ... 7:
11196         return aarch32_reg;
11197     case 8 ... 12:
11198         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11199     case 13:
11200         switch (mode) {
11201         case ARM_CPU_MODE_USR:
11202         case ARM_CPU_MODE_SYS:
11203             return 13;
11204         case ARM_CPU_MODE_HYP:
11205             return 15;
11206         case ARM_CPU_MODE_IRQ:
11207             return 17;
11208         case ARM_CPU_MODE_SVC:
11209             return 19;
11210         case ARM_CPU_MODE_ABT:
11211             return 21;
11212         case ARM_CPU_MODE_UND:
11213             return 23;
11214         case ARM_CPU_MODE_FIQ:
11215             return 29;
11216         default:
11217             g_assert_not_reached();
11218         }
11219     case 14:
11220         switch (mode) {
11221         case ARM_CPU_MODE_USR:
11222         case ARM_CPU_MODE_SYS:
11223         case ARM_CPU_MODE_HYP:
11224             return 14;
11225         case ARM_CPU_MODE_IRQ:
11226             return 16;
11227         case ARM_CPU_MODE_SVC:
11228             return 18;
11229         case ARM_CPU_MODE_ABT:
11230             return 20;
11231         case ARM_CPU_MODE_UND:
11232             return 22;
11233         case ARM_CPU_MODE_FIQ:
11234             return 30;
11235         default:
11236             g_assert_not_reached();
11237         }
11238     case 15:
11239         return 31;
11240     default:
11241         g_assert_not_reached();
11242     }
11243 }
11244 
11245 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11246 {
11247     uint32_t ret = cpsr_read(env);
11248 
11249     /* Move DIT to the correct location for SPSR_ELx */
11250     if (ret & CPSR_DIT) {
11251         ret &= ~CPSR_DIT;
11252         ret |= PSTATE_DIT;
11253     }
11254     /* Merge PSTATE.SS into SPSR_ELx */
11255     ret |= env->pstate & PSTATE_SS;
11256 
11257     return ret;
11258 }
11259 
11260 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11261 {
11262     /* Return true if this syndrome value is a synchronous external abort */
11263     switch (syn_get_ec(syndrome)) {
11264     case EC_INSNABORT:
11265     case EC_INSNABORT_SAME_EL:
11266     case EC_DATAABORT:
11267     case EC_DATAABORT_SAME_EL:
11268         /* Look at fault status code for all the synchronous ext abort cases */
11269         switch (syndrome & 0x3f) {
11270         case 0x10:
11271         case 0x13:
11272         case 0x14:
11273         case 0x15:
11274         case 0x16:
11275         case 0x17:
11276             return true;
11277         default:
11278             return false;
11279         }
11280     default:
11281         return false;
11282     }
11283 }
11284 
11285 /* Handle exception entry to a target EL which is using AArch64 */
11286 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11287 {
11288     ARMCPU *cpu = ARM_CPU(cs);
11289     CPUARMState *env = &cpu->env;
11290     unsigned int new_el = env->exception.target_el;
11291     target_ulong addr = env->cp15.vbar_el[new_el];
11292     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11293     unsigned int old_mode;
11294     unsigned int cur_el = arm_current_el(env);
11295     int rt;
11296 
11297     if (tcg_enabled()) {
11298         /*
11299          * Note that new_el can never be 0.  If cur_el is 0, then
11300          * el0_a64 is is_a64(), else el0_a64 is ignored.
11301          */
11302         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11303     }
11304 
11305     if (cur_el < new_el) {
11306         /*
11307          * Entry vector offset depends on whether the implemented EL
11308          * immediately lower than the target level is using AArch32 or AArch64
11309          */
11310         bool is_aa64;
11311         uint64_t hcr;
11312 
11313         switch (new_el) {
11314         case 3:
11315             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11316             break;
11317         case 2:
11318             hcr = arm_hcr_el2_eff(env);
11319             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11320                 is_aa64 = (hcr & HCR_RW) != 0;
11321                 break;
11322             }
11323             /* fall through */
11324         case 1:
11325             is_aa64 = is_a64(env);
11326             break;
11327         default:
11328             g_assert_not_reached();
11329         }
11330 
11331         if (is_aa64) {
11332             addr += 0x400;
11333         } else {
11334             addr += 0x600;
11335         }
11336     } else if (pstate_read(env) & PSTATE_SP) {
11337         addr += 0x200;
11338     }
11339 
11340     switch (cs->exception_index) {
11341     case EXCP_GPC:
11342         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11343                       env->cp15.mfar_el3);
11344         /* fall through */
11345     case EXCP_PREFETCH_ABORT:
11346     case EXCP_DATA_ABORT:
11347         /*
11348          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11349          * to be taken to the SError vector entrypoint.
11350          */
11351         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11352             syndrome_is_sync_extabt(env->exception.syndrome)) {
11353             addr += 0x180;
11354         }
11355         env->cp15.far_el[new_el] = env->exception.vaddress;
11356         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11357                       env->cp15.far_el[new_el]);
11358         /* fall through */
11359     case EXCP_BKPT:
11360     case EXCP_UDEF:
11361     case EXCP_SWI:
11362     case EXCP_HVC:
11363     case EXCP_HYP_TRAP:
11364     case EXCP_SMC:
11365         switch (syn_get_ec(env->exception.syndrome)) {
11366         case EC_ADVSIMDFPACCESSTRAP:
11367             /*
11368              * QEMU internal FP/SIMD syndromes from AArch32 include the
11369              * TA and coproc fields which are only exposed if the exception
11370              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11371              * AArch64 format syndrome.
11372              */
11373             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11374             break;
11375         case EC_CP14RTTRAP:
11376         case EC_CP15RTTRAP:
11377         case EC_CP14DTTRAP:
11378             /*
11379              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11380              * the raw register field from the insn; when taking this to
11381              * AArch64 we must convert it to the AArch64 view of the register
11382              * number. Notice that we read a 4-bit AArch32 register number and
11383              * write back a 5-bit AArch64 one.
11384              */
11385             rt = extract32(env->exception.syndrome, 5, 4);
11386             rt = aarch64_regnum(env, rt);
11387             env->exception.syndrome = deposit32(env->exception.syndrome,
11388                                                 5, 5, rt);
11389             break;
11390         case EC_CP15RRTTRAP:
11391         case EC_CP14RRTTRAP:
11392             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11393             rt = extract32(env->exception.syndrome, 5, 4);
11394             rt = aarch64_regnum(env, rt);
11395             env->exception.syndrome = deposit32(env->exception.syndrome,
11396                                                 5, 5, rt);
11397             rt = extract32(env->exception.syndrome, 10, 4);
11398             rt = aarch64_regnum(env, rt);
11399             env->exception.syndrome = deposit32(env->exception.syndrome,
11400                                                 10, 5, rt);
11401             break;
11402         }
11403         env->cp15.esr_el[new_el] = env->exception.syndrome;
11404         break;
11405     case EXCP_IRQ:
11406     case EXCP_VIRQ:
11407         addr += 0x80;
11408         break;
11409     case EXCP_FIQ:
11410     case EXCP_VFIQ:
11411         addr += 0x100;
11412         break;
11413     case EXCP_VSERR:
11414         addr += 0x180;
11415         /* Construct the SError syndrome from IDS and ISS fields. */
11416         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11417         env->cp15.esr_el[new_el] = env->exception.syndrome;
11418         break;
11419     default:
11420         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11421     }
11422 
11423     if (is_a64(env)) {
11424         old_mode = pstate_read(env);
11425         aarch64_save_sp(env, arm_current_el(env));
11426         env->elr_el[new_el] = env->pc;
11427 
11428         if (cur_el == 1 && new_el == 1) {
11429             uint64_t hcr = arm_hcr_el2_eff(env);
11430             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11431                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11432                 /*
11433                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11434                  * by setting M[3:2] to 0b10.
11435                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11436                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11437                  */
11438                 old_mode = deposit32(old_mode, 2, 2, 2);
11439             }
11440         }
11441     } else {
11442         old_mode = cpsr_read_for_spsr_elx(env);
11443         env->elr_el[new_el] = env->regs[15];
11444 
11445         aarch64_sync_32_to_64(env);
11446 
11447         env->condexec_bits = 0;
11448     }
11449     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11450 
11451     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11452     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11453                   env->elr_el[new_el]);
11454 
11455     if (cpu_isar_feature(aa64_pan, cpu)) {
11456         /* The value of PSTATE.PAN is normally preserved, except when ... */
11457         new_mode |= old_mode & PSTATE_PAN;
11458         switch (new_el) {
11459         case 2:
11460             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11461             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11462                 != (HCR_E2H | HCR_TGE)) {
11463                 break;
11464             }
11465             /* fall through */
11466         case 1:
11467             /* ... the target is EL1 ... */
11468             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11469             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11470                 new_mode |= PSTATE_PAN;
11471             }
11472             break;
11473         }
11474     }
11475     if (cpu_isar_feature(aa64_mte, cpu)) {
11476         new_mode |= PSTATE_TCO;
11477     }
11478 
11479     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11480         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11481             new_mode |= PSTATE_SSBS;
11482         } else {
11483             new_mode &= ~PSTATE_SSBS;
11484         }
11485     }
11486 
11487     pstate_write(env, PSTATE_DAIF | new_mode);
11488     env->aarch64 = true;
11489     aarch64_restore_sp(env, new_el);
11490 
11491     if (tcg_enabled()) {
11492         helper_rebuild_hflags_a64(env, new_el);
11493     }
11494 
11495     env->pc = addr;
11496 
11497     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11498                   new_el, env->pc, pstate_read(env));
11499 }
11500 
11501 /*
11502  * Do semihosting call and set the appropriate return value. All the
11503  * permission and validity checks have been done at translate time.
11504  *
11505  * We only see semihosting exceptions in TCG only as they are not
11506  * trapped to the hypervisor in KVM.
11507  */
11508 #ifdef CONFIG_TCG
11509 static void tcg_handle_semihosting(CPUState *cs)
11510 {
11511     ARMCPU *cpu = ARM_CPU(cs);
11512     CPUARMState *env = &cpu->env;
11513 
11514     if (is_a64(env)) {
11515         qemu_log_mask(CPU_LOG_INT,
11516                       "...handling as semihosting call 0x%" PRIx64 "\n",
11517                       env->xregs[0]);
11518         do_common_semihosting(cs);
11519         env->pc += 4;
11520     } else {
11521         qemu_log_mask(CPU_LOG_INT,
11522                       "...handling as semihosting call 0x%x\n",
11523                       env->regs[0]);
11524         do_common_semihosting(cs);
11525         env->regs[15] += env->thumb ? 2 : 4;
11526     }
11527 }
11528 #endif
11529 
11530 /*
11531  * Handle a CPU exception for A and R profile CPUs.
11532  * Do any appropriate logging, handle PSCI calls, and then hand off
11533  * to the AArch64-entry or AArch32-entry function depending on the
11534  * target exception level's register width.
11535  *
11536  * Note: this is used for both TCG (as the do_interrupt tcg op),
11537  *       and KVM to re-inject guest debug exceptions, and to
11538  *       inject a Synchronous-External-Abort.
11539  */
11540 void arm_cpu_do_interrupt(CPUState *cs)
11541 {
11542     ARMCPU *cpu = ARM_CPU(cs);
11543     CPUARMState *env = &cpu->env;
11544     unsigned int new_el = env->exception.target_el;
11545 
11546     assert(!arm_feature(env, ARM_FEATURE_M));
11547 
11548     arm_log_exception(cs);
11549     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11550                   new_el);
11551     if (qemu_loglevel_mask(CPU_LOG_INT)
11552         && !excp_is_internal(cs->exception_index)) {
11553         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11554                       syn_get_ec(env->exception.syndrome),
11555                       env->exception.syndrome);
11556     }
11557 
11558     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11559         arm_handle_psci_call(cpu);
11560         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11561         return;
11562     }
11563 
11564     /*
11565      * Semihosting semantics depend on the register width of the code
11566      * that caused the exception, not the target exception level, so
11567      * must be handled here.
11568      */
11569 #ifdef CONFIG_TCG
11570     if (cs->exception_index == EXCP_SEMIHOST) {
11571         tcg_handle_semihosting(cs);
11572         return;
11573     }
11574 #endif
11575 
11576     /*
11577      * Hooks may change global state so BQL should be held, also the
11578      * BQL needs to be held for any modification of
11579      * cs->interrupt_request.
11580      */
11581     g_assert(bql_locked());
11582 
11583     arm_call_pre_el_change_hook(cpu);
11584 
11585     assert(!excp_is_internal(cs->exception_index));
11586     if (arm_el_is_aa64(env, new_el)) {
11587         arm_cpu_do_interrupt_aarch64(cs);
11588     } else {
11589         arm_cpu_do_interrupt_aarch32(cs);
11590     }
11591 
11592     arm_call_el_change_hook(cpu);
11593 
11594     if (!kvm_enabled()) {
11595         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11596     }
11597 }
11598 #endif /* !CONFIG_USER_ONLY */
11599 
11600 uint64_t arm_sctlr(CPUARMState *env, int el)
11601 {
11602     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11603     if (el == 0) {
11604         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11605         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11606     }
11607     return env->cp15.sctlr_el[el];
11608 }
11609 
11610 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11611 {
11612     if (regime_has_2_ranges(mmu_idx)) {
11613         return extract64(tcr, 37, 2);
11614     } else if (regime_is_stage2(mmu_idx)) {
11615         return 0; /* VTCR_EL2 */
11616     } else {
11617         /* Replicate the single TBI bit so we always have 2 bits.  */
11618         return extract32(tcr, 20, 1) * 3;
11619     }
11620 }
11621 
11622 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11623 {
11624     if (regime_has_2_ranges(mmu_idx)) {
11625         return extract64(tcr, 51, 2);
11626     } else if (regime_is_stage2(mmu_idx)) {
11627         return 0; /* VTCR_EL2 */
11628     } else {
11629         /* Replicate the single TBID bit so we always have 2 bits.  */
11630         return extract32(tcr, 29, 1) * 3;
11631     }
11632 }
11633 
11634 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11635 {
11636     if (regime_has_2_ranges(mmu_idx)) {
11637         return extract64(tcr, 57, 2);
11638     } else {
11639         /* Replicate the single TCMA bit so we always have 2 bits.  */
11640         return extract32(tcr, 30, 1) * 3;
11641     }
11642 }
11643 
11644 static ARMGranuleSize tg0_to_gran_size(int tg)
11645 {
11646     switch (tg) {
11647     case 0:
11648         return Gran4K;
11649     case 1:
11650         return Gran64K;
11651     case 2:
11652         return Gran16K;
11653     default:
11654         return GranInvalid;
11655     }
11656 }
11657 
11658 static ARMGranuleSize tg1_to_gran_size(int tg)
11659 {
11660     switch (tg) {
11661     case 1:
11662         return Gran16K;
11663     case 2:
11664         return Gran4K;
11665     case 3:
11666         return Gran64K;
11667     default:
11668         return GranInvalid;
11669     }
11670 }
11671 
11672 static inline bool have4k(ARMCPU *cpu, bool stage2)
11673 {
11674     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11675         : cpu_isar_feature(aa64_tgran4, cpu);
11676 }
11677 
11678 static inline bool have16k(ARMCPU *cpu, bool stage2)
11679 {
11680     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11681         : cpu_isar_feature(aa64_tgran16, cpu);
11682 }
11683 
11684 static inline bool have64k(ARMCPU *cpu, bool stage2)
11685 {
11686     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11687         : cpu_isar_feature(aa64_tgran64, cpu);
11688 }
11689 
11690 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11691                                          bool stage2)
11692 {
11693     switch (gran) {
11694     case Gran4K:
11695         if (have4k(cpu, stage2)) {
11696             return gran;
11697         }
11698         break;
11699     case Gran16K:
11700         if (have16k(cpu, stage2)) {
11701             return gran;
11702         }
11703         break;
11704     case Gran64K:
11705         if (have64k(cpu, stage2)) {
11706             return gran;
11707         }
11708         break;
11709     case GranInvalid:
11710         break;
11711     }
11712     /*
11713      * If the guest selects a granule size that isn't implemented,
11714      * the architecture requires that we behave as if it selected one
11715      * that is (with an IMPDEF choice of which one to pick). We choose
11716      * to implement the smallest supported granule size.
11717      */
11718     if (have4k(cpu, stage2)) {
11719         return Gran4K;
11720     }
11721     if (have16k(cpu, stage2)) {
11722         return Gran16K;
11723     }
11724     assert(have64k(cpu, stage2));
11725     return Gran64K;
11726 }
11727 
11728 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11729                                    ARMMMUIdx mmu_idx, bool data,
11730                                    bool el1_is_aa32)
11731 {
11732     uint64_t tcr = regime_tcr(env, mmu_idx);
11733     bool epd, hpd, tsz_oob, ds, ha, hd;
11734     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11735     ARMGranuleSize gran;
11736     ARMCPU *cpu = env_archcpu(env);
11737     bool stage2 = regime_is_stage2(mmu_idx);
11738 
11739     if (!regime_has_2_ranges(mmu_idx)) {
11740         select = 0;
11741         tsz = extract32(tcr, 0, 6);
11742         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11743         if (stage2) {
11744             /* VTCR_EL2 */
11745             hpd = false;
11746         } else {
11747             hpd = extract32(tcr, 24, 1);
11748         }
11749         epd = false;
11750         sh = extract32(tcr, 12, 2);
11751         ps = extract32(tcr, 16, 3);
11752         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11753         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11754         ds = extract64(tcr, 32, 1);
11755     } else {
11756         bool e0pd;
11757 
11758         /*
11759          * Bit 55 is always between the two regions, and is canonical for
11760          * determining if address tagging is enabled.
11761          */
11762         select = extract64(va, 55, 1);
11763         if (!select) {
11764             tsz = extract32(tcr, 0, 6);
11765             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11766             epd = extract32(tcr, 7, 1);
11767             sh = extract32(tcr, 12, 2);
11768             hpd = extract64(tcr, 41, 1);
11769             e0pd = extract64(tcr, 55, 1);
11770         } else {
11771             tsz = extract32(tcr, 16, 6);
11772             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11773             epd = extract32(tcr, 23, 1);
11774             sh = extract32(tcr, 28, 2);
11775             hpd = extract64(tcr, 42, 1);
11776             e0pd = extract64(tcr, 56, 1);
11777         }
11778         ps = extract64(tcr, 32, 3);
11779         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11780         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11781         ds = extract64(tcr, 59, 1);
11782 
11783         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11784             regime_is_user(env, mmu_idx)) {
11785             epd = true;
11786         }
11787     }
11788 
11789     gran = sanitize_gran_size(cpu, gran, stage2);
11790 
11791     if (cpu_isar_feature(aa64_st, cpu)) {
11792         max_tsz = 48 - (gran == Gran64K);
11793     } else {
11794         max_tsz = 39;
11795     }
11796 
11797     /*
11798      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11799      * adjust the effective value of DS, as documented.
11800      */
11801     min_tsz = 16;
11802     if (gran == Gran64K) {
11803         if (cpu_isar_feature(aa64_lva, cpu)) {
11804             min_tsz = 12;
11805         }
11806         ds = false;
11807     } else if (ds) {
11808         if (regime_is_stage2(mmu_idx)) {
11809             if (gran == Gran16K) {
11810                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11811             } else {
11812                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11813             }
11814         } else {
11815             if (gran == Gran16K) {
11816                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11817             } else {
11818                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11819             }
11820         }
11821         if (ds) {
11822             min_tsz = 12;
11823         }
11824     }
11825 
11826     if (stage2 && el1_is_aa32) {
11827         /*
11828          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11829          * are loosened: a configured IPA of 40 bits is permitted even if
11830          * the implemented PA is less than that (and so a 40 bit IPA would
11831          * fault for an AArch64 EL1). See R_DTLMN.
11832          */
11833         min_tsz = MIN(min_tsz, 24);
11834     }
11835 
11836     if (tsz > max_tsz) {
11837         tsz = max_tsz;
11838         tsz_oob = true;
11839     } else if (tsz < min_tsz) {
11840         tsz = min_tsz;
11841         tsz_oob = true;
11842     } else {
11843         tsz_oob = false;
11844     }
11845 
11846     /* Present TBI as a composite with TBID.  */
11847     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11848     if (!data) {
11849         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11850     }
11851     tbi = (tbi >> select) & 1;
11852 
11853     return (ARMVAParameters) {
11854         .tsz = tsz,
11855         .ps = ps,
11856         .sh = sh,
11857         .select = select,
11858         .tbi = tbi,
11859         .epd = epd,
11860         .hpd = hpd,
11861         .tsz_oob = tsz_oob,
11862         .ds = ds,
11863         .ha = ha,
11864         .hd = ha && hd,
11865         .gran = gran,
11866     };
11867 }
11868 
11869 /*
11870  * Note that signed overflow is undefined in C.  The following routines are
11871  * careful to use unsigned types where modulo arithmetic is required.
11872  * Failure to do so _will_ break on newer gcc.
11873  */
11874 
11875 /* Signed saturating arithmetic.  */
11876 
11877 /* Perform 16-bit signed saturating addition.  */
11878 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11879 {
11880     uint16_t res;
11881 
11882     res = a + b;
11883     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11884         if (a & 0x8000) {
11885             res = 0x8000;
11886         } else {
11887             res = 0x7fff;
11888         }
11889     }
11890     return res;
11891 }
11892 
11893 /* Perform 8-bit signed saturating addition.  */
11894 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11895 {
11896     uint8_t res;
11897 
11898     res = a + b;
11899     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11900         if (a & 0x80) {
11901             res = 0x80;
11902         } else {
11903             res = 0x7f;
11904         }
11905     }
11906     return res;
11907 }
11908 
11909 /* Perform 16-bit signed saturating subtraction.  */
11910 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11911 {
11912     uint16_t res;
11913 
11914     res = a - b;
11915     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11916         if (a & 0x8000) {
11917             res = 0x8000;
11918         } else {
11919             res = 0x7fff;
11920         }
11921     }
11922     return res;
11923 }
11924 
11925 /* Perform 8-bit signed saturating subtraction.  */
11926 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11927 {
11928     uint8_t res;
11929 
11930     res = a - b;
11931     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11932         if (a & 0x80) {
11933             res = 0x80;
11934         } else {
11935             res = 0x7f;
11936         }
11937     }
11938     return res;
11939 }
11940 
11941 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11942 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11943 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11944 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11945 #define PFX q
11946 
11947 #include "op_addsub.h"
11948 
11949 /* Unsigned saturating arithmetic.  */
11950 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11951 {
11952     uint16_t res;
11953     res = a + b;
11954     if (res < a) {
11955         res = 0xffff;
11956     }
11957     return res;
11958 }
11959 
11960 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11961 {
11962     if (a > b) {
11963         return a - b;
11964     } else {
11965         return 0;
11966     }
11967 }
11968 
11969 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11970 {
11971     uint8_t res;
11972     res = a + b;
11973     if (res < a) {
11974         res = 0xff;
11975     }
11976     return res;
11977 }
11978 
11979 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11980 {
11981     if (a > b) {
11982         return a - b;
11983     } else {
11984         return 0;
11985     }
11986 }
11987 
11988 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11989 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11990 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11991 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11992 #define PFX uq
11993 
11994 #include "op_addsub.h"
11995 
11996 /* Signed modulo arithmetic.  */
11997 #define SARITH16(a, b, n, op) do { \
11998     int32_t sum; \
11999     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12000     RESULT(sum, n, 16); \
12001     if (sum >= 0) \
12002         ge |= 3 << (n * 2); \
12003     } while (0)
12004 
12005 #define SARITH8(a, b, n, op) do { \
12006     int32_t sum; \
12007     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12008     RESULT(sum, n, 8); \
12009     if (sum >= 0) \
12010         ge |= 1 << n; \
12011     } while (0)
12012 
12013 
12014 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12015 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12016 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
12017 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
12018 #define PFX s
12019 #define ARITH_GE
12020 
12021 #include "op_addsub.h"
12022 
12023 /* Unsigned modulo arithmetic.  */
12024 #define ADD16(a, b, n) do { \
12025     uint32_t sum; \
12026     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12027     RESULT(sum, n, 16); \
12028     if ((sum >> 16) == 1) \
12029         ge |= 3 << (n * 2); \
12030     } while (0)
12031 
12032 #define ADD8(a, b, n) do { \
12033     uint32_t sum; \
12034     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12035     RESULT(sum, n, 8); \
12036     if ((sum >> 8) == 1) \
12037         ge |= 1 << n; \
12038     } while (0)
12039 
12040 #define SUB16(a, b, n) do { \
12041     uint32_t sum; \
12042     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12043     RESULT(sum, n, 16); \
12044     if ((sum >> 16) == 0) \
12045         ge |= 3 << (n * 2); \
12046     } while (0)
12047 
12048 #define SUB8(a, b, n) do { \
12049     uint32_t sum; \
12050     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12051     RESULT(sum, n, 8); \
12052     if ((sum >> 8) == 0) \
12053         ge |= 1 << n; \
12054     } while (0)
12055 
12056 #define PFX u
12057 #define ARITH_GE
12058 
12059 #include "op_addsub.h"
12060 
12061 /* Halved signed arithmetic.  */
12062 #define ADD16(a, b, n) \
12063   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12064 #define SUB16(a, b, n) \
12065   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12066 #define ADD8(a, b, n) \
12067   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12068 #define SUB8(a, b, n) \
12069   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12070 #define PFX sh
12071 
12072 #include "op_addsub.h"
12073 
12074 /* Halved unsigned arithmetic.  */
12075 #define ADD16(a, b, n) \
12076   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12077 #define SUB16(a, b, n) \
12078   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12079 #define ADD8(a, b, n) \
12080   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12081 #define SUB8(a, b, n) \
12082   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12083 #define PFX uh
12084 
12085 #include "op_addsub.h"
12086 
12087 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12088 {
12089     if (a > b) {
12090         return a - b;
12091     } else {
12092         return b - a;
12093     }
12094 }
12095 
12096 /* Unsigned sum of absolute byte differences.  */
12097 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12098 {
12099     uint32_t sum;
12100     sum = do_usad(a, b);
12101     sum += do_usad(a >> 8, b >> 8);
12102     sum += do_usad(a >> 16, b >> 16);
12103     sum += do_usad(a >> 24, b >> 24);
12104     return sum;
12105 }
12106 
12107 /* For ARMv6 SEL instruction.  */
12108 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12109 {
12110     uint32_t mask;
12111 
12112     mask = 0;
12113     if (flags & 1) {
12114         mask |= 0xff;
12115     }
12116     if (flags & 2) {
12117         mask |= 0xff00;
12118     }
12119     if (flags & 4) {
12120         mask |= 0xff0000;
12121     }
12122     if (flags & 8) {
12123         mask |= 0xff000000;
12124     }
12125     return (a & mask) | (b & ~mask);
12126 }
12127 
12128 /*
12129  * CRC helpers.
12130  * The upper bytes of val (above the number specified by 'bytes') must have
12131  * been zeroed out by the caller.
12132  */
12133 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12134 {
12135     uint8_t buf[4];
12136 
12137     stl_le_p(buf, val);
12138 
12139     /* zlib crc32 converts the accumulator and output to one's complement.  */
12140     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12141 }
12142 
12143 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12144 {
12145     uint8_t buf[4];
12146 
12147     stl_le_p(buf, val);
12148 
12149     /* Linux crc32c converts the output to one's complement.  */
12150     return crc32c(acc, buf, bytes) ^ 0xffffffff;
12151 }
12152 
12153 /*
12154  * Return the exception level to which FP-disabled exceptions should
12155  * be taken, or 0 if FP is enabled.
12156  */
12157 int fp_exception_el(CPUARMState *env, int cur_el)
12158 {
12159 #ifndef CONFIG_USER_ONLY
12160     uint64_t hcr_el2;
12161 
12162     /*
12163      * CPACR and the CPTR registers don't exist before v6, so FP is
12164      * always accessible
12165      */
12166     if (!arm_feature(env, ARM_FEATURE_V6)) {
12167         return 0;
12168     }
12169 
12170     if (arm_feature(env, ARM_FEATURE_M)) {
12171         /* CPACR can cause a NOCP UsageFault taken to current security state */
12172         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12173             return 1;
12174         }
12175 
12176         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12177             if (!extract32(env->v7m.nsacr, 10, 1)) {
12178                 /* FP insns cause a NOCP UsageFault taken to Secure */
12179                 return 3;
12180             }
12181         }
12182 
12183         return 0;
12184     }
12185 
12186     hcr_el2 = arm_hcr_el2_eff(env);
12187 
12188     /*
12189      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12190      * 0, 2 : trap EL0 and EL1/PL1 accesses
12191      * 1    : trap only EL0 accesses
12192      * 3    : trap no accesses
12193      * This register is ignored if E2H+TGE are both set.
12194      */
12195     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12196         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12197 
12198         switch (fpen) {
12199         case 1:
12200             if (cur_el != 0) {
12201                 break;
12202             }
12203             /* fall through */
12204         case 0:
12205         case 2:
12206             /* Trap from Secure PL0 or PL1 to Secure PL1. */
12207             if (!arm_el_is_aa64(env, 3)
12208                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12209                 return 3;
12210             }
12211             if (cur_el <= 1) {
12212                 return 1;
12213             }
12214             break;
12215         }
12216     }
12217 
12218     /*
12219      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12220      * to control non-secure access to the FPU. It doesn't have any
12221      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12222      */
12223     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12224          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12225         if (!extract32(env->cp15.nsacr, 10, 1)) {
12226             /* FP insns act as UNDEF */
12227             return cur_el == 2 ? 2 : 1;
12228         }
12229     }
12230 
12231     /*
12232      * CPTR_EL2 is present in v7VE or v8, and changes format
12233      * with HCR_EL2.E2H (regardless of TGE).
12234      */
12235     if (cur_el <= 2) {
12236         if (hcr_el2 & HCR_E2H) {
12237             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12238             case 1:
12239                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12240                     break;
12241                 }
12242                 /* fall through */
12243             case 0:
12244             case 2:
12245                 return 2;
12246             }
12247         } else if (arm_is_el2_enabled(env)) {
12248             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12249                 return 2;
12250             }
12251         }
12252     }
12253 
12254     /* CPTR_EL3 : present in v8 */
12255     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12256         /* Trap all FP ops to EL3 */
12257         return 3;
12258     }
12259 #endif
12260     return 0;
12261 }
12262 
12263 /* Return the exception level we're running at if this is our mmu_idx */
12264 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12265 {
12266     if (mmu_idx & ARM_MMU_IDX_M) {
12267         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12268     }
12269 
12270     switch (mmu_idx) {
12271     case ARMMMUIdx_E10_0:
12272     case ARMMMUIdx_E20_0:
12273         return 0;
12274     case ARMMMUIdx_E10_1:
12275     case ARMMMUIdx_E10_1_PAN:
12276         return 1;
12277     case ARMMMUIdx_E2:
12278     case ARMMMUIdx_E20_2:
12279     case ARMMMUIdx_E20_2_PAN:
12280         return 2;
12281     case ARMMMUIdx_E3:
12282         return 3;
12283     default:
12284         g_assert_not_reached();
12285     }
12286 }
12287 
12288 #ifndef CONFIG_TCG
12289 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12290 {
12291     g_assert_not_reached();
12292 }
12293 #endif
12294 
12295 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12296 {
12297     ARMMMUIdx idx;
12298     uint64_t hcr;
12299 
12300     if (arm_feature(env, ARM_FEATURE_M)) {
12301         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12302     }
12303 
12304     /* See ARM pseudo-function ELIsInHost.  */
12305     switch (el) {
12306     case 0:
12307         hcr = arm_hcr_el2_eff(env);
12308         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12309             idx = ARMMMUIdx_E20_0;
12310         } else {
12311             idx = ARMMMUIdx_E10_0;
12312         }
12313         break;
12314     case 1:
12315         if (arm_pan_enabled(env)) {
12316             idx = ARMMMUIdx_E10_1_PAN;
12317         } else {
12318             idx = ARMMMUIdx_E10_1;
12319         }
12320         break;
12321     case 2:
12322         /* Note that TGE does not apply at EL2.  */
12323         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12324             if (arm_pan_enabled(env)) {
12325                 idx = ARMMMUIdx_E20_2_PAN;
12326             } else {
12327                 idx = ARMMMUIdx_E20_2;
12328             }
12329         } else {
12330             idx = ARMMMUIdx_E2;
12331         }
12332         break;
12333     case 3:
12334         return ARMMMUIdx_E3;
12335     default:
12336         g_assert_not_reached();
12337     }
12338 
12339     return idx;
12340 }
12341 
12342 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12343 {
12344     return arm_mmu_idx_el(env, arm_current_el(env));
12345 }
12346 
12347 static bool mve_no_pred(CPUARMState *env)
12348 {
12349     /*
12350      * Return true if there is definitely no predication of MVE
12351      * instructions by VPR or LTPSIZE. (Returning false even if there
12352      * isn't any predication is OK; generated code will just be
12353      * a little worse.)
12354      * If the CPU does not implement MVE then this TB flag is always 0.
12355      *
12356      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12357      * logic in gen_update_fp_context() needs to be updated to match.
12358      *
12359      * We do not include the effect of the ECI bits here -- they are
12360      * tracked in other TB flags. This simplifies the logic for
12361      * "when did we emit code that changes the MVE_NO_PRED TB flag
12362      * and thus need to end the TB?".
12363      */
12364     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12365         return false;
12366     }
12367     if (env->v7m.vpr) {
12368         return false;
12369     }
12370     if (env->v7m.ltpsize < 4) {
12371         return false;
12372     }
12373     return true;
12374 }
12375 
12376 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12377                           uint64_t *cs_base, uint32_t *pflags)
12378 {
12379     CPUARMTBFlags flags;
12380 
12381     assert_hflags_rebuild_correctly(env);
12382     flags = env->hflags;
12383 
12384     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12385         *pc = env->pc;
12386         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12387             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12388         }
12389     } else {
12390         *pc = env->regs[15];
12391 
12392         if (arm_feature(env, ARM_FEATURE_M)) {
12393             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12394                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12395                 != env->v7m.secure) {
12396                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12397             }
12398 
12399             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12400                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12401                  (env->v7m.secure &&
12402                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12403                 /*
12404                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12405                  * active FP context; we must create a new FP context before
12406                  * executing any FP insn.
12407                  */
12408                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12409             }
12410 
12411             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12412             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12413                 DP_TBFLAG_M32(flags, LSPACT, 1);
12414             }
12415 
12416             if (mve_no_pred(env)) {
12417                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12418             }
12419         } else {
12420             /*
12421              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12422              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12423              */
12424             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12425                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12426             } else {
12427                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12428                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12429             }
12430             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12431                 DP_TBFLAG_A32(flags, VFPEN, 1);
12432             }
12433         }
12434 
12435         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12436         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12437     }
12438 
12439     /*
12440      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12441      * states defined in the ARM ARM for software singlestep:
12442      *  SS_ACTIVE   PSTATE.SS   State
12443      *     0            x       Inactive (the TB flag for SS is always 0)
12444      *     1            0       Active-pending
12445      *     1            1       Active-not-pending
12446      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12447      */
12448     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12449         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12450     }
12451 
12452     *pflags = flags.flags;
12453     *cs_base = flags.flags2;
12454 }
12455 
12456 #ifdef TARGET_AARCH64
12457 /*
12458  * The manual says that when SVE is enabled and VQ is widened the
12459  * implementation is allowed to zero the previously inaccessible
12460  * portion of the registers.  The corollary to that is that when
12461  * SVE is enabled and VQ is narrowed we are also allowed to zero
12462  * the now inaccessible portion of the registers.
12463  *
12464  * The intent of this is that no predicate bit beyond VQ is ever set.
12465  * Which means that some operations on predicate registers themselves
12466  * may operate on full uint64_t or even unrolled across the maximum
12467  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12468  * may well be cheaper than conditionals to restrict the operation
12469  * to the relevant portion of a uint16_t[16].
12470  */
12471 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12472 {
12473     int i, j;
12474     uint64_t pmask;
12475 
12476     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12477     assert(vq <= env_archcpu(env)->sve_max_vq);
12478 
12479     /* Zap the high bits of the zregs.  */
12480     for (i = 0; i < 32; i++) {
12481         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12482     }
12483 
12484     /* Zap the high bits of the pregs and ffr.  */
12485     pmask = 0;
12486     if (vq & 3) {
12487         pmask = ~(-1ULL << (16 * (vq & 3)));
12488     }
12489     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12490         for (i = 0; i < 17; ++i) {
12491             env->vfp.pregs[i].p[j] &= pmask;
12492         }
12493         pmask = 0;
12494     }
12495 }
12496 
12497 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12498 {
12499     int exc_el;
12500 
12501     if (sm) {
12502         exc_el = sme_exception_el(env, el);
12503     } else {
12504         exc_el = sve_exception_el(env, el);
12505     }
12506     if (exc_el) {
12507         return 0; /* disabled */
12508     }
12509     return sve_vqm1_for_el_sm(env, el, sm);
12510 }
12511 
12512 /*
12513  * Notice a change in SVE vector size when changing EL.
12514  */
12515 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12516                            int new_el, bool el0_a64)
12517 {
12518     ARMCPU *cpu = env_archcpu(env);
12519     int old_len, new_len;
12520     bool old_a64, new_a64, sm;
12521 
12522     /* Nothing to do if no SVE.  */
12523     if (!cpu_isar_feature(aa64_sve, cpu)) {
12524         return;
12525     }
12526 
12527     /* Nothing to do if FP is disabled in either EL.  */
12528     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12529         return;
12530     }
12531 
12532     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12533     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12534 
12535     /*
12536      * Both AArch64.TakeException and AArch64.ExceptionReturn
12537      * invoke ResetSVEState when taking an exception from, or
12538      * returning to, AArch32 state when PSTATE.SM is enabled.
12539      */
12540     sm = FIELD_EX64(env->svcr, SVCR, SM);
12541     if (old_a64 != new_a64 && sm) {
12542         arm_reset_sve_state(env);
12543         return;
12544     }
12545 
12546     /*
12547      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12548      * at ELx, or not available because the EL is in AArch32 state, then
12549      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12550      * has an effective value of 0".
12551      *
12552      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12553      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12554      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12555      * we already have the correct register contents when encountering the
12556      * vq0->vq0 transition between EL0->EL1.
12557      */
12558     old_len = new_len = 0;
12559     if (old_a64) {
12560         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12561     }
12562     if (new_a64) {
12563         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12564     }
12565 
12566     /* When changing vector length, clear inaccessible state.  */
12567     if (new_len < old_len) {
12568         aarch64_sve_narrow_vq(env, new_len + 1);
12569     }
12570 }
12571 #endif
12572 
12573 #ifndef CONFIG_USER_ONLY
12574 ARMSecuritySpace arm_security_space(CPUARMState *env)
12575 {
12576     if (arm_feature(env, ARM_FEATURE_M)) {
12577         return arm_secure_to_space(env->v7m.secure);
12578     }
12579 
12580     /*
12581      * If EL3 is not supported then the secure state is implementation
12582      * defined, in which case QEMU defaults to non-secure.
12583      */
12584     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12585         return ARMSS_NonSecure;
12586     }
12587 
12588     /* Check for AArch64 EL3 or AArch32 Mon. */
12589     if (is_a64(env)) {
12590         if (extract32(env->pstate, 2, 2) == 3) {
12591             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12592                 return ARMSS_Root;
12593             } else {
12594                 return ARMSS_Secure;
12595             }
12596         }
12597     } else {
12598         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12599             return ARMSS_Secure;
12600         }
12601     }
12602 
12603     return arm_security_space_below_el3(env);
12604 }
12605 
12606 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12607 {
12608     assert(!arm_feature(env, ARM_FEATURE_M));
12609 
12610     /*
12611      * If EL3 is not supported then the secure state is implementation
12612      * defined, in which case QEMU defaults to non-secure.
12613      */
12614     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12615         return ARMSS_NonSecure;
12616     }
12617 
12618     /*
12619      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12620      * Ignoring NSE when !NS retains consistency without having to
12621      * modify other predicates.
12622      */
12623     if (!(env->cp15.scr_el3 & SCR_NS)) {
12624         return ARMSS_Secure;
12625     } else if (env->cp15.scr_el3 & SCR_NSE) {
12626         return ARMSS_Realm;
12627     } else {
12628         return ARMSS_NonSecure;
12629     }
12630 }
12631 #endif /* !CONFIG_USER_ONLY */
12632