xref: /openbmc/qemu/target/arm/helper.c (revision 9c707525)
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         if (cpu_isar_feature(aa64_ecv, cpu)) {
1927             valid_mask |= SCR_ECVEN;
1928         }
1929     } else {
1930         valid_mask &= ~(SCR_RW | SCR_ST);
1931         if (cpu_isar_feature(aa32_ras, cpu)) {
1932             valid_mask |= SCR_TERR;
1933         }
1934     }
1935 
1936     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1937         valid_mask &= ~SCR_HCE;
1938 
1939         /*
1940          * On ARMv7, SMD (or SCD as it is called in v7) is only
1941          * supported if EL2 exists. The bit is UNK/SBZP when
1942          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1943          * when EL2 is unavailable.
1944          * On ARMv8, this bit is always available.
1945          */
1946         if (arm_feature(env, ARM_FEATURE_V7) &&
1947             !arm_feature(env, ARM_FEATURE_V8)) {
1948             valid_mask &= ~SCR_SMD;
1949         }
1950     }
1951 
1952     /* Clear all-context RES0 bits.  */
1953     value &= valid_mask;
1954     changed = env->cp15.scr_el3 ^ value;
1955     env->cp15.scr_el3 = value;
1956 
1957     /*
1958      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1959      * we must invalidate all TLBs below EL3.
1960      */
1961     if (changed & (SCR_NS | SCR_NSE)) {
1962         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1963                                            ARMMMUIdxBit_E20_0 |
1964                                            ARMMMUIdxBit_E10_1 |
1965                                            ARMMMUIdxBit_E20_2 |
1966                                            ARMMMUIdxBit_E10_1_PAN |
1967                                            ARMMMUIdxBit_E20_2_PAN |
1968                                            ARMMMUIdxBit_E2));
1969     }
1970 }
1971 
1972 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1973 {
1974     /*
1975      * scr_write will set the RES1 bits on an AArch64-only CPU.
1976      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1977      */
1978     scr_write(env, ri, 0);
1979 }
1980 
1981 static CPAccessResult access_tid4(CPUARMState *env,
1982                                   const ARMCPRegInfo *ri,
1983                                   bool isread)
1984 {
1985     if (arm_current_el(env) == 1 &&
1986         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1987         return CP_ACCESS_TRAP_EL2;
1988     }
1989 
1990     return CP_ACCESS_OK;
1991 }
1992 
1993 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1994 {
1995     ARMCPU *cpu = env_archcpu(env);
1996 
1997     /*
1998      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1999      * bank
2000      */
2001     uint32_t index = A32_BANKED_REG_GET(env, csselr,
2002                                         ri->secure & ARM_CP_SECSTATE_S);
2003 
2004     return cpu->ccsidr[index];
2005 }
2006 
2007 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2008                          uint64_t value)
2009 {
2010     raw_write(env, ri, value & 0xf);
2011 }
2012 
2013 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2014 {
2015     CPUState *cs = env_cpu(env);
2016     bool el1 = arm_current_el(env) == 1;
2017     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2018     uint64_t ret = 0;
2019 
2020     if (hcr_el2 & HCR_IMO) {
2021         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2022             ret |= CPSR_I;
2023         }
2024     } else {
2025         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2026             ret |= CPSR_I;
2027         }
2028     }
2029 
2030     if (hcr_el2 & HCR_FMO) {
2031         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2032             ret |= CPSR_F;
2033         }
2034     } else {
2035         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2036             ret |= CPSR_F;
2037         }
2038     }
2039 
2040     if (hcr_el2 & HCR_AMO) {
2041         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2042             ret |= CPSR_A;
2043         }
2044     }
2045 
2046     return ret;
2047 }
2048 
2049 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2050                                        bool isread)
2051 {
2052     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2053         return CP_ACCESS_TRAP_EL2;
2054     }
2055 
2056     return CP_ACCESS_OK;
2057 }
2058 
2059 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2060                                        bool isread)
2061 {
2062     if (arm_feature(env, ARM_FEATURE_V8)) {
2063         return access_aa64_tid1(env, ri, isread);
2064     }
2065 
2066     return CP_ACCESS_OK;
2067 }
2068 
2069 static const ARMCPRegInfo v7_cp_reginfo[] = {
2070     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2071     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2072       .access = PL1_W, .type = ARM_CP_NOP },
2073     /*
2074      * Performance monitors are implementation defined in v7,
2075      * but with an ARM recommended set of registers, which we
2076      * follow.
2077      *
2078      * Performance registers fall into three categories:
2079      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2080      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2081      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2082      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2083      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2084      */
2085     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2086       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2087       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2088       .writefn = pmcntenset_write,
2089       .accessfn = pmreg_access,
2090       .fgt = FGT_PMCNTEN,
2091       .raw_writefn = raw_write },
2092     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2093       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2094       .access = PL0_RW, .accessfn = pmreg_access,
2095       .fgt = FGT_PMCNTEN,
2096       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2097       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2098     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2099       .access = PL0_RW,
2100       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2101       .accessfn = pmreg_access,
2102       .fgt = FGT_PMCNTEN,
2103       .writefn = pmcntenclr_write,
2104       .type = ARM_CP_ALIAS | ARM_CP_IO },
2105     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2106       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2107       .access = PL0_RW, .accessfn = pmreg_access,
2108       .fgt = FGT_PMCNTEN,
2109       .type = ARM_CP_ALIAS | ARM_CP_IO,
2110       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2111       .writefn = pmcntenclr_write },
2112     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2113       .access = PL0_RW, .type = ARM_CP_IO,
2114       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2115       .accessfn = pmreg_access,
2116       .fgt = FGT_PMOVS,
2117       .writefn = pmovsr_write,
2118       .raw_writefn = raw_write },
2119     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2120       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2121       .access = PL0_RW, .accessfn = pmreg_access,
2122       .fgt = FGT_PMOVS,
2123       .type = ARM_CP_ALIAS | ARM_CP_IO,
2124       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2125       .writefn = pmovsr_write,
2126       .raw_writefn = raw_write },
2127     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2128       .access = PL0_W, .accessfn = pmreg_access_swinc,
2129       .fgt = FGT_PMSWINC_EL0,
2130       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2131       .writefn = pmswinc_write },
2132     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2133       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2134       .access = PL0_W, .accessfn = pmreg_access_swinc,
2135       .fgt = FGT_PMSWINC_EL0,
2136       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2137       .writefn = pmswinc_write },
2138     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2139       .access = PL0_RW, .type = ARM_CP_ALIAS,
2140       .fgt = FGT_PMSELR_EL0,
2141       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2142       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2143       .raw_writefn = raw_write},
2144     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2145       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2146       .access = PL0_RW, .accessfn = pmreg_access_selr,
2147       .fgt = FGT_PMSELR_EL0,
2148       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2149       .writefn = pmselr_write, .raw_writefn = raw_write, },
2150     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2151       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2152       .fgt = FGT_PMCCNTR_EL0,
2153       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2154       .accessfn = pmreg_access_ccntr },
2155     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2156       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2157       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2158       .fgt = FGT_PMCCNTR_EL0,
2159       .type = ARM_CP_IO,
2160       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2161       .readfn = pmccntr_read, .writefn = pmccntr_write,
2162       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2163     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2164       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2165       .access = PL0_RW, .accessfn = pmreg_access,
2166       .fgt = FGT_PMCCFILTR_EL0,
2167       .type = ARM_CP_ALIAS | ARM_CP_IO,
2168       .resetvalue = 0, },
2169     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2170       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2171       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2172       .access = PL0_RW, .accessfn = pmreg_access,
2173       .fgt = FGT_PMCCFILTR_EL0,
2174       .type = ARM_CP_IO,
2175       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2176       .resetvalue = 0, },
2177     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2178       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2179       .accessfn = pmreg_access,
2180       .fgt = FGT_PMEVTYPERN_EL0,
2181       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2182     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2183       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2184       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2185       .accessfn = pmreg_access,
2186       .fgt = FGT_PMEVTYPERN_EL0,
2187       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2188     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2189       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2190       .accessfn = pmreg_access_xevcntr,
2191       .fgt = FGT_PMEVCNTRN_EL0,
2192       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2193     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2194       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2195       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2196       .accessfn = pmreg_access_xevcntr,
2197       .fgt = FGT_PMEVCNTRN_EL0,
2198       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2199     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2200       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2201       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2202       .resetvalue = 0,
2203       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2204     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2205       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2206       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2207       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2208       .resetvalue = 0,
2209       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2210     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2211       .access = PL1_RW, .accessfn = access_tpm,
2212       .fgt = FGT_PMINTEN,
2213       .type = ARM_CP_ALIAS | ARM_CP_IO,
2214       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2215       .resetvalue = 0,
2216       .writefn = pmintenset_write, .raw_writefn = raw_write },
2217     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2218       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2219       .access = PL1_RW, .accessfn = access_tpm,
2220       .fgt = FGT_PMINTEN,
2221       .type = ARM_CP_IO,
2222       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2223       .writefn = pmintenset_write, .raw_writefn = raw_write,
2224       .resetvalue = 0x0 },
2225     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2226       .access = PL1_RW, .accessfn = access_tpm,
2227       .fgt = FGT_PMINTEN,
2228       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2229       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2230       .writefn = pmintenclr_write, },
2231     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2232       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2233       .access = PL1_RW, .accessfn = access_tpm,
2234       .fgt = FGT_PMINTEN,
2235       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2236       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2237       .writefn = pmintenclr_write },
2238     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2239       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2240       .access = PL1_R,
2241       .accessfn = access_tid4,
2242       .fgt = FGT_CCSIDR_EL1,
2243       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2244     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2245       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2246       .access = PL1_RW,
2247       .accessfn = access_tid4,
2248       .fgt = FGT_CSSELR_EL1,
2249       .writefn = csselr_write, .resetvalue = 0,
2250       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2251                              offsetof(CPUARMState, cp15.csselr_ns) } },
2252     /*
2253      * Auxiliary ID register: this actually has an IMPDEF value but for now
2254      * just RAZ for all cores:
2255      */
2256     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2257       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2258       .access = PL1_R, .type = ARM_CP_CONST,
2259       .accessfn = access_aa64_tid1,
2260       .fgt = FGT_AIDR_EL1,
2261       .resetvalue = 0 },
2262     /*
2263      * Auxiliary fault status registers: these also are IMPDEF, and we
2264      * choose to RAZ/WI for all cores.
2265      */
2266     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2267       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2268       .access = PL1_RW, .accessfn = access_tvm_trvm,
2269       .fgt = FGT_AFSR0_EL1,
2270       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2271       .type = ARM_CP_CONST, .resetvalue = 0 },
2272     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2273       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2274       .access = PL1_RW, .accessfn = access_tvm_trvm,
2275       .fgt = FGT_AFSR1_EL1,
2276       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2277       .type = ARM_CP_CONST, .resetvalue = 0 },
2278     /*
2279      * MAIR can just read-as-written because we don't implement caches
2280      * and so don't need to care about memory attributes.
2281      */
2282     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2283       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2284       .access = PL1_RW, .accessfn = access_tvm_trvm,
2285       .fgt = FGT_MAIR_EL1,
2286       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2287       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2288       .resetvalue = 0 },
2289     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2290       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2291       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2292       .resetvalue = 0 },
2293     /*
2294      * For non-long-descriptor page tables these are PRRR and NMRR;
2295      * regardless they still act as reads-as-written for QEMU.
2296      */
2297      /*
2298       * MAIR0/1 are defined separately from their 64-bit counterpart which
2299       * allows them to assign the correct fieldoffset based on the endianness
2300       * handled in the field definitions.
2301       */
2302     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2303       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2304       .access = PL1_RW, .accessfn = access_tvm_trvm,
2305       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2306                              offsetof(CPUARMState, cp15.mair0_ns) },
2307       .resetfn = arm_cp_reset_ignore },
2308     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2309       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2310       .access = PL1_RW, .accessfn = access_tvm_trvm,
2311       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2312                              offsetof(CPUARMState, cp15.mair1_ns) },
2313       .resetfn = arm_cp_reset_ignore },
2314     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2315       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2316       .fgt = FGT_ISR_EL1,
2317       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2318     /* 32 bit ITLB invalidates */
2319     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2320       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2321       .writefn = tlbiall_write },
2322     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2323       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2324       .writefn = tlbimva_write },
2325     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2326       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2327       .writefn = tlbiasid_write },
2328     /* 32 bit DTLB invalidates */
2329     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2330       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2331       .writefn = tlbiall_write },
2332     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2333       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2334       .writefn = tlbimva_write },
2335     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2336       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2337       .writefn = tlbiasid_write },
2338     /* 32 bit TLB invalidates */
2339     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2340       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2341       .writefn = tlbiall_write },
2342     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2343       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2344       .writefn = tlbimva_write },
2345     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2346       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2347       .writefn = tlbiasid_write },
2348     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2349       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2350       .writefn = tlbimvaa_write },
2351 };
2352 
2353 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2354     /* 32 bit TLB invalidates, Inner Shareable */
2355     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2356       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2357       .writefn = tlbiall_is_write },
2358     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2359       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2360       .writefn = tlbimva_is_write },
2361     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2362       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2363       .writefn = tlbiasid_is_write },
2364     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2365       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2366       .writefn = tlbimvaa_is_write },
2367 };
2368 
2369 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2370     /* PMOVSSET is not implemented in v7 before v7ve */
2371     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2372       .access = PL0_RW, .accessfn = pmreg_access,
2373       .fgt = FGT_PMOVS,
2374       .type = ARM_CP_ALIAS | ARM_CP_IO,
2375       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2376       .writefn = pmovsset_write,
2377       .raw_writefn = raw_write },
2378     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2379       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2380       .access = PL0_RW, .accessfn = pmreg_access,
2381       .fgt = FGT_PMOVS,
2382       .type = ARM_CP_ALIAS | ARM_CP_IO,
2383       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2384       .writefn = pmovsset_write,
2385       .raw_writefn = raw_write },
2386 };
2387 
2388 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2389                         uint64_t value)
2390 {
2391     value &= 1;
2392     env->teecr = value;
2393 }
2394 
2395 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2396                                    bool isread)
2397 {
2398     /*
2399      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2400      * at all, so we don't need to check whether we're v8A.
2401      */
2402     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2403         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2404         return CP_ACCESS_TRAP_EL2;
2405     }
2406     return CP_ACCESS_OK;
2407 }
2408 
2409 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2410                                     bool isread)
2411 {
2412     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2413         return CP_ACCESS_TRAP;
2414     }
2415     return teecr_access(env, ri, isread);
2416 }
2417 
2418 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2419     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2420       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2421       .resetvalue = 0,
2422       .writefn = teecr_write, .accessfn = teecr_access },
2423     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2424       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2425       .accessfn = teehbr_access, .resetvalue = 0 },
2426 };
2427 
2428 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2429     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2430       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2431       .access = PL0_RW,
2432       .fgt = FGT_TPIDR_EL0,
2433       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2434     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2435       .access = PL0_RW,
2436       .fgt = FGT_TPIDR_EL0,
2437       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2438                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2439       .resetfn = arm_cp_reset_ignore },
2440     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2441       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2442       .access = PL0_R | PL1_W,
2443       .fgt = FGT_TPIDRRO_EL0,
2444       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2445       .resetvalue = 0},
2446     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2447       .access = PL0_R | PL1_W,
2448       .fgt = FGT_TPIDRRO_EL0,
2449       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2450                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2451       .resetfn = arm_cp_reset_ignore },
2452     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2453       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2454       .access = PL1_RW,
2455       .fgt = FGT_TPIDR_EL1,
2456       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2457     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2458       .access = PL1_RW,
2459       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2460                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2461       .resetvalue = 0 },
2462 };
2463 
2464 #ifndef CONFIG_USER_ONLY
2465 
2466 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2467                                        bool isread)
2468 {
2469     /*
2470      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2471      * Writable only at the highest implemented exception level.
2472      */
2473     int el = arm_current_el(env);
2474     uint64_t hcr;
2475     uint32_t cntkctl;
2476 
2477     switch (el) {
2478     case 0:
2479         hcr = arm_hcr_el2_eff(env);
2480         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2481             cntkctl = env->cp15.cnthctl_el2;
2482         } else {
2483             cntkctl = env->cp15.c14_cntkctl;
2484         }
2485         if (!extract32(cntkctl, 0, 2)) {
2486             return CP_ACCESS_TRAP;
2487         }
2488         break;
2489     case 1:
2490         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2491             arm_is_secure_below_el3(env)) {
2492             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2493             return CP_ACCESS_TRAP_UNCATEGORIZED;
2494         }
2495         break;
2496     case 2:
2497     case 3:
2498         break;
2499     }
2500 
2501     if (!isread && el < arm_highest_el(env)) {
2502         return CP_ACCESS_TRAP_UNCATEGORIZED;
2503     }
2504 
2505     return CP_ACCESS_OK;
2506 }
2507 
2508 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2509                                         bool isread)
2510 {
2511     unsigned int cur_el = arm_current_el(env);
2512     bool has_el2 = arm_is_el2_enabled(env);
2513     uint64_t hcr = arm_hcr_el2_eff(env);
2514 
2515     switch (cur_el) {
2516     case 0:
2517         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2518         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2519             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2520                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2521         }
2522 
2523         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2524         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2525             return CP_ACCESS_TRAP;
2526         }
2527         /* fall through */
2528     case 1:
2529         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2530         if (has_el2 && timeridx == GTIMER_PHYS &&
2531             (hcr & HCR_E2H
2532              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2533              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2534             return CP_ACCESS_TRAP_EL2;
2535         }
2536         if (has_el2 && timeridx == GTIMER_VIRT) {
2537             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2538                 return CP_ACCESS_TRAP_EL2;
2539             }
2540         }
2541         break;
2542     }
2543     return CP_ACCESS_OK;
2544 }
2545 
2546 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2547                                       bool isread)
2548 {
2549     unsigned int cur_el = arm_current_el(env);
2550     bool has_el2 = arm_is_el2_enabled(env);
2551     uint64_t hcr = arm_hcr_el2_eff(env);
2552 
2553     switch (cur_el) {
2554     case 0:
2555         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2556             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2557             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2558                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2559         }
2560 
2561         /*
2562          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2563          * EL0 if EL0[PV]TEN is zero.
2564          */
2565         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2566             return CP_ACCESS_TRAP;
2567         }
2568         /* fall through */
2569 
2570     case 1:
2571         if (has_el2 && timeridx == GTIMER_PHYS) {
2572             if (hcr & HCR_E2H) {
2573                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2574                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2575                     return CP_ACCESS_TRAP_EL2;
2576                 }
2577             } else {
2578                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2579                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2580                     return CP_ACCESS_TRAP_EL2;
2581                 }
2582             }
2583         }
2584         if (has_el2 && timeridx == GTIMER_VIRT) {
2585             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2586                 return CP_ACCESS_TRAP_EL2;
2587             }
2588         }
2589         break;
2590     }
2591     return CP_ACCESS_OK;
2592 }
2593 
2594 static CPAccessResult gt_pct_access(CPUARMState *env,
2595                                     const ARMCPRegInfo *ri,
2596                                     bool isread)
2597 {
2598     return gt_counter_access(env, GTIMER_PHYS, isread);
2599 }
2600 
2601 static CPAccessResult gt_vct_access(CPUARMState *env,
2602                                     const ARMCPRegInfo *ri,
2603                                     bool isread)
2604 {
2605     return gt_counter_access(env, GTIMER_VIRT, isread);
2606 }
2607 
2608 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2609                                        bool isread)
2610 {
2611     return gt_timer_access(env, GTIMER_PHYS, isread);
2612 }
2613 
2614 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2615                                        bool isread)
2616 {
2617     return gt_timer_access(env, GTIMER_VIRT, isread);
2618 }
2619 
2620 static CPAccessResult gt_stimer_access(CPUARMState *env,
2621                                        const ARMCPRegInfo *ri,
2622                                        bool isread)
2623 {
2624     /*
2625      * The AArch64 register view of the secure physical timer is
2626      * always accessible from EL3, and configurably accessible from
2627      * Secure EL1.
2628      */
2629     switch (arm_current_el(env)) {
2630     case 1:
2631         if (!arm_is_secure(env)) {
2632             return CP_ACCESS_TRAP;
2633         }
2634         if (!(env->cp15.scr_el3 & SCR_ST)) {
2635             return CP_ACCESS_TRAP_EL3;
2636         }
2637         return CP_ACCESS_OK;
2638     case 0:
2639     case 2:
2640         return CP_ACCESS_TRAP;
2641     case 3:
2642         return CP_ACCESS_OK;
2643     default:
2644         g_assert_not_reached();
2645     }
2646 }
2647 
2648 static uint64_t gt_get_countervalue(CPUARMState *env)
2649 {
2650     ARMCPU *cpu = env_archcpu(env);
2651 
2652     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2653 }
2654 
2655 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2656 {
2657     CPUARMState *env = &cpu->env;
2658     uint64_t cnthctl = env->cp15.cnthctl_el2;
2659     ARMSecuritySpace ss = arm_security_space(env);
2660     /* ISTATUS && !IMASK */
2661     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2662 
2663     /*
2664      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2665      * It is RES0 in Secure and NonSecure state.
2666      */
2667     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2668         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2669          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2670         irqstate = 0;
2671     }
2672 
2673     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2674     trace_arm_gt_update_irq(timeridx, irqstate);
2675 }
2676 
2677 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2678 {
2679     /*
2680      * Changing security state between Root and Secure/NonSecure, which may
2681      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2682      * mask bits. Update the IRQ state accordingly.
2683      */
2684     gt_update_irq(cpu, GTIMER_VIRT);
2685     gt_update_irq(cpu, GTIMER_PHYS);
2686 }
2687 
2688 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2689 {
2690     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2691         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2692         arm_is_el2_enabled(env) &&
2693         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2694         return env->cp15.cntpoff_el2;
2695     }
2696     return 0;
2697 }
2698 
2699 static uint64_t gt_phys_cnt_offset(CPUARMState *env)
2700 {
2701     if (arm_current_el(env) >= 2) {
2702         return 0;
2703     }
2704     return gt_phys_raw_cnt_offset(env);
2705 }
2706 
2707 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2708 {
2709     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2710 
2711     if (gt->ctl & 1) {
2712         /*
2713          * Timer enabled: calculate and set current ISTATUS, irq, and
2714          * reset timer to when ISTATUS next has to change
2715          */
2716         uint64_t offset = timeridx == GTIMER_VIRT ?
2717             cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
2718         uint64_t count = gt_get_countervalue(&cpu->env);
2719         /* Note that this must be unsigned 64 bit arithmetic: */
2720         int istatus = count - offset >= gt->cval;
2721         uint64_t nexttick;
2722 
2723         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2724 
2725         if (istatus) {
2726             /*
2727              * Next transition is when (count - offset) rolls back over to 0.
2728              * If offset > count then this is when count == offset;
2729              * if offset <= count then this is when count == offset + 2^64
2730              * For the latter case we set nexttick to an "as far in future
2731              * as possible" value and let the code below handle it.
2732              */
2733             if (offset > count) {
2734                 nexttick = offset;
2735             } else {
2736                 nexttick = UINT64_MAX;
2737             }
2738         } else {
2739             /*
2740              * Next transition is when (count - offset) == cval, i.e.
2741              * when count == (cval + offset).
2742              * If that would overflow, then again we set up the next interrupt
2743              * for "as far in the future as possible" for the code below.
2744              */
2745             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2746                 nexttick = UINT64_MAX;
2747             }
2748         }
2749         /*
2750          * Note that the desired next expiry time might be beyond the
2751          * signed-64-bit range of a QEMUTimer -- in this case we just
2752          * set the timer for as far in the future as possible. When the
2753          * timer expires we will reset the timer for any remaining period.
2754          */
2755         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2756             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2757         } else {
2758             timer_mod(cpu->gt_timer[timeridx], nexttick);
2759         }
2760         trace_arm_gt_recalc(timeridx, nexttick);
2761     } else {
2762         /* Timer disabled: ISTATUS and timer output always clear */
2763         gt->ctl &= ~4;
2764         timer_del(cpu->gt_timer[timeridx]);
2765         trace_arm_gt_recalc_disabled(timeridx);
2766     }
2767     gt_update_irq(cpu, timeridx);
2768 }
2769 
2770 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2771                            int timeridx)
2772 {
2773     ARMCPU *cpu = env_archcpu(env);
2774 
2775     timer_del(cpu->gt_timer[timeridx]);
2776 }
2777 
2778 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2779 {
2780     return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
2781 }
2782 
2783 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2784 {
2785     uint64_t hcr;
2786 
2787     switch (arm_current_el(env)) {
2788     case 2:
2789         hcr = arm_hcr_el2_eff(env);
2790         if (hcr & HCR_E2H) {
2791             return 0;
2792         }
2793         break;
2794     case 0:
2795         hcr = arm_hcr_el2_eff(env);
2796         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2797             return 0;
2798         }
2799         break;
2800     }
2801 
2802     return env->cp15.cntvoff_el2;
2803 }
2804 
2805 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2806 {
2807     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2808 }
2809 
2810 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2811                           int timeridx,
2812                           uint64_t value)
2813 {
2814     trace_arm_gt_cval_write(timeridx, value);
2815     env->cp15.c14_timer[timeridx].cval = value;
2816     gt_recalc_timer(env_archcpu(env), timeridx);
2817 }
2818 
2819 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2820                              int timeridx)
2821 {
2822     uint64_t offset = 0;
2823 
2824     switch (timeridx) {
2825     case GTIMER_VIRT:
2826     case GTIMER_HYPVIRT:
2827         offset = gt_virt_cnt_offset(env);
2828         break;
2829     case GTIMER_PHYS:
2830         offset = gt_phys_cnt_offset(env);
2831         break;
2832     }
2833 
2834     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2835                       (gt_get_countervalue(env) - offset));
2836 }
2837 
2838 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839                           int timeridx,
2840                           uint64_t value)
2841 {
2842     uint64_t offset = 0;
2843 
2844     switch (timeridx) {
2845     case GTIMER_VIRT:
2846     case GTIMER_HYPVIRT:
2847         offset = gt_virt_cnt_offset(env);
2848         break;
2849     case GTIMER_PHYS:
2850         offset = gt_phys_cnt_offset(env);
2851         break;
2852     }
2853 
2854     trace_arm_gt_tval_write(timeridx, value);
2855     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2856                                          sextract64(value, 0, 32);
2857     gt_recalc_timer(env_archcpu(env), timeridx);
2858 }
2859 
2860 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861                          int timeridx,
2862                          uint64_t value)
2863 {
2864     ARMCPU *cpu = env_archcpu(env);
2865     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2866 
2867     trace_arm_gt_ctl_write(timeridx, value);
2868     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2869     if ((oldval ^ value) & 1) {
2870         /* Enable toggled */
2871         gt_recalc_timer(cpu, timeridx);
2872     } else if ((oldval ^ value) & 2) {
2873         /*
2874          * IMASK toggled: don't need to recalculate,
2875          * just set the interrupt line based on ISTATUS
2876          */
2877         trace_arm_gt_imask_toggle(timeridx);
2878         gt_update_irq(cpu, timeridx);
2879     }
2880 }
2881 
2882 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2883 {
2884     gt_timer_reset(env, ri, GTIMER_PHYS);
2885 }
2886 
2887 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2888                                uint64_t value)
2889 {
2890     gt_cval_write(env, ri, GTIMER_PHYS, value);
2891 }
2892 
2893 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2894 {
2895     return gt_tval_read(env, ri, GTIMER_PHYS);
2896 }
2897 
2898 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899                                uint64_t value)
2900 {
2901     gt_tval_write(env, ri, GTIMER_PHYS, value);
2902 }
2903 
2904 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2905                               uint64_t value)
2906 {
2907     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2908 }
2909 
2910 static int gt_phys_redir_timeridx(CPUARMState *env)
2911 {
2912     switch (arm_mmu_idx(env)) {
2913     case ARMMMUIdx_E20_0:
2914     case ARMMMUIdx_E20_2:
2915     case ARMMMUIdx_E20_2_PAN:
2916         return GTIMER_HYP;
2917     default:
2918         return GTIMER_PHYS;
2919     }
2920 }
2921 
2922 static int gt_virt_redir_timeridx(CPUARMState *env)
2923 {
2924     switch (arm_mmu_idx(env)) {
2925     case ARMMMUIdx_E20_0:
2926     case ARMMMUIdx_E20_2:
2927     case ARMMMUIdx_E20_2_PAN:
2928         return GTIMER_HYPVIRT;
2929     default:
2930         return GTIMER_VIRT;
2931     }
2932 }
2933 
2934 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2935                                         const ARMCPRegInfo *ri)
2936 {
2937     int timeridx = gt_phys_redir_timeridx(env);
2938     return env->cp15.c14_timer[timeridx].cval;
2939 }
2940 
2941 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2942                                      uint64_t value)
2943 {
2944     int timeridx = gt_phys_redir_timeridx(env);
2945     gt_cval_write(env, ri, timeridx, value);
2946 }
2947 
2948 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2949                                         const ARMCPRegInfo *ri)
2950 {
2951     int timeridx = gt_phys_redir_timeridx(env);
2952     return gt_tval_read(env, ri, timeridx);
2953 }
2954 
2955 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2956                                      uint64_t value)
2957 {
2958     int timeridx = gt_phys_redir_timeridx(env);
2959     gt_tval_write(env, ri, timeridx, value);
2960 }
2961 
2962 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2963                                        const ARMCPRegInfo *ri)
2964 {
2965     int timeridx = gt_phys_redir_timeridx(env);
2966     return env->cp15.c14_timer[timeridx].ctl;
2967 }
2968 
2969 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2970                                     uint64_t value)
2971 {
2972     int timeridx = gt_phys_redir_timeridx(env);
2973     gt_ctl_write(env, ri, timeridx, value);
2974 }
2975 
2976 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2977 {
2978     gt_timer_reset(env, ri, GTIMER_VIRT);
2979 }
2980 
2981 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982                                uint64_t value)
2983 {
2984     gt_cval_write(env, ri, GTIMER_VIRT, value);
2985 }
2986 
2987 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2988 {
2989     return gt_tval_read(env, ri, GTIMER_VIRT);
2990 }
2991 
2992 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2993                                uint64_t value)
2994 {
2995     gt_tval_write(env, ri, GTIMER_VIRT, value);
2996 }
2997 
2998 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2999                               uint64_t value)
3000 {
3001     gt_ctl_write(env, ri, GTIMER_VIRT, value);
3002 }
3003 
3004 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005                              uint64_t value)
3006 {
3007     ARMCPU *cpu = env_archcpu(env);
3008     uint32_t oldval = env->cp15.cnthctl_el2;
3009     uint32_t valid_mask =
3010         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
3011         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
3012         R_CNTHCTL_EVNTEN_MASK |
3013         R_CNTHCTL_EVNTDIR_MASK |
3014         R_CNTHCTL_EVNTI_MASK |
3015         R_CNTHCTL_EL0VTEN_MASK |
3016         R_CNTHCTL_EL0PTEN_MASK |
3017         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
3018         R_CNTHCTL_EL1PTEN_MASK;
3019 
3020     if (cpu_isar_feature(aa64_rme, cpu)) {
3021         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
3022     }
3023     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
3024         valid_mask |=
3025             R_CNTHCTL_EL1TVT_MASK |
3026             R_CNTHCTL_EL1TVCT_MASK |
3027             R_CNTHCTL_EL1NVPCT_MASK |
3028             R_CNTHCTL_EL1NVVCT_MASK |
3029             R_CNTHCTL_EVNTIS_MASK;
3030     }
3031     if (cpu_isar_feature(aa64_ecv, cpu)) {
3032         valid_mask |= R_CNTHCTL_ECV_MASK;
3033     }
3034 
3035     /* Clear RES0 bits */
3036     value &= valid_mask;
3037 
3038     raw_write(env, ri, value);
3039 
3040     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
3041         gt_update_irq(cpu, GTIMER_VIRT);
3042     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
3043         gt_update_irq(cpu, GTIMER_PHYS);
3044     }
3045 }
3046 
3047 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3048                               uint64_t value)
3049 {
3050     ARMCPU *cpu = env_archcpu(env);
3051 
3052     trace_arm_gt_cntvoff_write(value);
3053     raw_write(env, ri, value);
3054     gt_recalc_timer(cpu, GTIMER_VIRT);
3055 }
3056 
3057 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
3058                                         const ARMCPRegInfo *ri)
3059 {
3060     int timeridx = gt_virt_redir_timeridx(env);
3061     return env->cp15.c14_timer[timeridx].cval;
3062 }
3063 
3064 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3065                                      uint64_t value)
3066 {
3067     int timeridx = gt_virt_redir_timeridx(env);
3068     gt_cval_write(env, ri, timeridx, value);
3069 }
3070 
3071 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3072                                         const ARMCPRegInfo *ri)
3073 {
3074     int timeridx = gt_virt_redir_timeridx(env);
3075     return gt_tval_read(env, ri, timeridx);
3076 }
3077 
3078 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3079                                      uint64_t value)
3080 {
3081     int timeridx = gt_virt_redir_timeridx(env);
3082     gt_tval_write(env, ri, timeridx, value);
3083 }
3084 
3085 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3086                                        const ARMCPRegInfo *ri)
3087 {
3088     int timeridx = gt_virt_redir_timeridx(env);
3089     return env->cp15.c14_timer[timeridx].ctl;
3090 }
3091 
3092 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3093                                     uint64_t value)
3094 {
3095     int timeridx = gt_virt_redir_timeridx(env);
3096     gt_ctl_write(env, ri, timeridx, value);
3097 }
3098 
3099 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3100 {
3101     gt_timer_reset(env, ri, GTIMER_HYP);
3102 }
3103 
3104 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3105                               uint64_t value)
3106 {
3107     gt_cval_write(env, ri, GTIMER_HYP, value);
3108 }
3109 
3110 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3111 {
3112     return gt_tval_read(env, ri, GTIMER_HYP);
3113 }
3114 
3115 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3116                               uint64_t value)
3117 {
3118     gt_tval_write(env, ri, GTIMER_HYP, value);
3119 }
3120 
3121 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3122                               uint64_t value)
3123 {
3124     gt_ctl_write(env, ri, GTIMER_HYP, value);
3125 }
3126 
3127 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3128 {
3129     gt_timer_reset(env, ri, GTIMER_SEC);
3130 }
3131 
3132 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3133                               uint64_t value)
3134 {
3135     gt_cval_write(env, ri, GTIMER_SEC, value);
3136 }
3137 
3138 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3139 {
3140     return gt_tval_read(env, ri, GTIMER_SEC);
3141 }
3142 
3143 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3144                               uint64_t value)
3145 {
3146     gt_tval_write(env, ri, GTIMER_SEC, value);
3147 }
3148 
3149 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3150                               uint64_t value)
3151 {
3152     gt_ctl_write(env, ri, GTIMER_SEC, value);
3153 }
3154 
3155 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3156 {
3157     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3158 }
3159 
3160 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3161                              uint64_t value)
3162 {
3163     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3164 }
3165 
3166 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3167 {
3168     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3169 }
3170 
3171 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3172                              uint64_t value)
3173 {
3174     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3175 }
3176 
3177 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3178                             uint64_t value)
3179 {
3180     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3181 }
3182 
3183 void arm_gt_ptimer_cb(void *opaque)
3184 {
3185     ARMCPU *cpu = opaque;
3186 
3187     gt_recalc_timer(cpu, GTIMER_PHYS);
3188 }
3189 
3190 void arm_gt_vtimer_cb(void *opaque)
3191 {
3192     ARMCPU *cpu = opaque;
3193 
3194     gt_recalc_timer(cpu, GTIMER_VIRT);
3195 }
3196 
3197 void arm_gt_htimer_cb(void *opaque)
3198 {
3199     ARMCPU *cpu = opaque;
3200 
3201     gt_recalc_timer(cpu, GTIMER_HYP);
3202 }
3203 
3204 void arm_gt_stimer_cb(void *opaque)
3205 {
3206     ARMCPU *cpu = opaque;
3207 
3208     gt_recalc_timer(cpu, GTIMER_SEC);
3209 }
3210 
3211 void arm_gt_hvtimer_cb(void *opaque)
3212 {
3213     ARMCPU *cpu = opaque;
3214 
3215     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3216 }
3217 
3218 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3219 {
3220     ARMCPU *cpu = env_archcpu(env);
3221 
3222     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3223 }
3224 
3225 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3226     /*
3227      * Note that CNTFRQ is purely reads-as-written for the benefit
3228      * of software; writing it doesn't actually change the timer frequency.
3229      * Our reset value matches the fixed frequency we implement the timer at.
3230      */
3231     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3232       .type = ARM_CP_ALIAS,
3233       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3234       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3235     },
3236     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3237       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3238       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3239       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3240       .resetfn = arm_gt_cntfrq_reset,
3241     },
3242     /* overall control: mostly access permissions */
3243     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3244       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3245       .access = PL1_RW,
3246       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3247       .resetvalue = 0,
3248     },
3249     /* per-timer control */
3250     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3251       .secure = ARM_CP_SECSTATE_NS,
3252       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3253       .accessfn = gt_ptimer_access,
3254       .fieldoffset = offsetoflow32(CPUARMState,
3255                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3256       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3257       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3258     },
3259     { .name = "CNTP_CTL_S",
3260       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3261       .secure = ARM_CP_SECSTATE_S,
3262       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3263       .accessfn = gt_ptimer_access,
3264       .fieldoffset = offsetoflow32(CPUARMState,
3265                                    cp15.c14_timer[GTIMER_SEC].ctl),
3266       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3267     },
3268     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3269       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3270       .type = ARM_CP_IO, .access = PL0_RW,
3271       .accessfn = gt_ptimer_access,
3272       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3273       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3274       .resetvalue = 0,
3275       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3276       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3277     },
3278     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3279       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3280       .accessfn = gt_vtimer_access,
3281       .fieldoffset = offsetoflow32(CPUARMState,
3282                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3283       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3284       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3285     },
3286     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3287       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3288       .type = ARM_CP_IO, .access = PL0_RW,
3289       .accessfn = gt_vtimer_access,
3290       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3291       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3292       .resetvalue = 0,
3293       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3294       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3295     },
3296     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3297     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3298       .secure = ARM_CP_SECSTATE_NS,
3299       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3300       .accessfn = gt_ptimer_access,
3301       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3302     },
3303     { .name = "CNTP_TVAL_S",
3304       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3305       .secure = ARM_CP_SECSTATE_S,
3306       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3307       .accessfn = gt_ptimer_access,
3308       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3309     },
3310     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3311       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3312       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3313       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3314       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3315     },
3316     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3317       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3318       .accessfn = gt_vtimer_access,
3319       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3320     },
3321     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3322       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3323       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3324       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3325       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3326     },
3327     /* The counter itself */
3328     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3329       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3330       .accessfn = gt_pct_access,
3331       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3332     },
3333     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3334       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3335       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3336       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3337     },
3338     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3339       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3340       .accessfn = gt_vct_access,
3341       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3342     },
3343     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3344       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3345       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3346       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3347     },
3348     /* Comparison value, indicating when the timer goes off */
3349     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3350       .secure = ARM_CP_SECSTATE_NS,
3351       .access = PL0_RW,
3352       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3353       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3354       .accessfn = gt_ptimer_access,
3355       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3356       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3357     },
3358     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3359       .secure = ARM_CP_SECSTATE_S,
3360       .access = PL0_RW,
3361       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3362       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3363       .accessfn = gt_ptimer_access,
3364       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3365     },
3366     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3367       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3368       .access = PL0_RW,
3369       .type = ARM_CP_IO,
3370       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3371       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3372       .resetvalue = 0, .accessfn = gt_ptimer_access,
3373       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3374       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3375     },
3376     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3377       .access = PL0_RW,
3378       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3379       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3380       .accessfn = gt_vtimer_access,
3381       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3382       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3383     },
3384     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3385       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3386       .access = PL0_RW,
3387       .type = ARM_CP_IO,
3388       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3389       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3390       .resetvalue = 0, .accessfn = gt_vtimer_access,
3391       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3392       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3393     },
3394     /*
3395      * Secure timer -- this is actually restricted to only EL3
3396      * and configurably Secure-EL1 via the accessfn.
3397      */
3398     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3399       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3400       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3401       .accessfn = gt_stimer_access,
3402       .readfn = gt_sec_tval_read,
3403       .writefn = gt_sec_tval_write,
3404       .resetfn = gt_sec_timer_reset,
3405     },
3406     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3407       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3408       .type = ARM_CP_IO, .access = PL1_RW,
3409       .accessfn = gt_stimer_access,
3410       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3411       .resetvalue = 0,
3412       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3413     },
3414     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3415       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3416       .type = ARM_CP_IO, .access = PL1_RW,
3417       .accessfn = gt_stimer_access,
3418       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3419       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3420     },
3421 };
3422 
3423 /*
3424  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3425  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3426  * so our implementations here are identical to the normal registers.
3427  */
3428 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3429     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3430       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3431       .accessfn = gt_vct_access,
3432       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3433     },
3434     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3435       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3436       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3437       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3438     },
3439     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3440       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3441       .accessfn = gt_pct_access,
3442       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3443     },
3444     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3445       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3446       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3447       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3448     },
3449 };
3450 
3451 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3452                                         const ARMCPRegInfo *ri,
3453                                         bool isread)
3454 {
3455     if (arm_current_el(env) == 2 && !(env->cp15.scr_el3 & SCR_ECVEN)) {
3456         return CP_ACCESS_TRAP_EL3;
3457     }
3458     return CP_ACCESS_OK;
3459 }
3460 
3461 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3462                               uint64_t value)
3463 {
3464     ARMCPU *cpu = env_archcpu(env);
3465 
3466     trace_arm_gt_cntpoff_write(value);
3467     raw_write(env, ri, value);
3468     gt_recalc_timer(cpu, GTIMER_PHYS);
3469 }
3470 
3471 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3472     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3473     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3474     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3475     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3476     .nv2_redirect_offset = 0x1a8,
3477     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3478 };
3479 #else
3480 
3481 /*
3482  * In user-mode most of the generic timer registers are inaccessible
3483  * however modern kernels (4.12+) allow access to cntvct_el0
3484  */
3485 
3486 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3487 {
3488     ARMCPU *cpu = env_archcpu(env);
3489 
3490     /*
3491      * Currently we have no support for QEMUTimer in linux-user so we
3492      * can't call gt_get_countervalue(env), instead we directly
3493      * call the lower level functions.
3494      */
3495     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3496 }
3497 
3498 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3499     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3500       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3501       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3502       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3503       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3504     },
3505     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3506       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3507       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3508       .readfn = gt_virt_cnt_read,
3509     },
3510 };
3511 
3512 /*
3513  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3514  * is exposed to userspace by Linux.
3515  */
3516 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3517     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3518       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3519       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3520       .readfn = gt_virt_cnt_read,
3521     },
3522 };
3523 
3524 #endif
3525 
3526 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3527 {
3528     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3529         raw_write(env, ri, value);
3530     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3531         raw_write(env, ri, value & 0xfffff6ff);
3532     } else {
3533         raw_write(env, ri, value & 0xfffff1ff);
3534     }
3535 }
3536 
3537 #ifndef CONFIG_USER_ONLY
3538 /* get_phys_addr() isn't present for user-mode-only targets */
3539 
3540 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3541                                  bool isread)
3542 {
3543     if (ri->opc2 & 4) {
3544         /*
3545          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3546          * Secure EL1 (which can only happen if EL3 is AArch64).
3547          * They are simply UNDEF if executed from NS EL1.
3548          * They function normally from EL2 or EL3.
3549          */
3550         if (arm_current_el(env) == 1) {
3551             if (arm_is_secure_below_el3(env)) {
3552                 if (env->cp15.scr_el3 & SCR_EEL2) {
3553                     return CP_ACCESS_TRAP_EL2;
3554                 }
3555                 return CP_ACCESS_TRAP_EL3;
3556             }
3557             return CP_ACCESS_TRAP_UNCATEGORIZED;
3558         }
3559     }
3560     return CP_ACCESS_OK;
3561 }
3562 
3563 #ifdef CONFIG_TCG
3564 static int par_el1_shareability(GetPhysAddrResult *res)
3565 {
3566     /*
3567      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3568      * memory -- see pseudocode PAREncodeShareability().
3569      */
3570     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3571         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3572         return 2;
3573     }
3574     return res->cacheattrs.shareability;
3575 }
3576 
3577 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3578                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3579                              ARMSecuritySpace ss)
3580 {
3581     bool ret;
3582     uint64_t par64;
3583     bool format64 = false;
3584     ARMMMUFaultInfo fi = {};
3585     GetPhysAddrResult res = {};
3586 
3587     /*
3588      * I_MXTJT: Granule protection checks are not performed on the final address
3589      * of a successful translation.
3590      */
3591     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3592                                          &res, &fi);
3593 
3594     /*
3595      * ATS operations only do S1 or S1+S2 translations, so we never
3596      * have to deal with the ARMCacheAttrs format for S2 only.
3597      */
3598     assert(!res.cacheattrs.is_s2_format);
3599 
3600     if (ret) {
3601         /*
3602          * Some kinds of translation fault must cause exceptions rather
3603          * than being reported in the PAR.
3604          */
3605         int current_el = arm_current_el(env);
3606         int target_el;
3607         uint32_t syn, fsr, fsc;
3608         bool take_exc = false;
3609 
3610         if (fi.s1ptw && current_el == 1
3611             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3612             /*
3613              * Synchronous stage 2 fault on an access made as part of the
3614              * translation table walk for AT S1E0* or AT S1E1* insn
3615              * executed from NS EL1. If this is a synchronous external abort
3616              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3617              * to EL3. Otherwise the fault is taken as an exception to EL2,
3618              * and HPFAR_EL2 holds the faulting IPA.
3619              */
3620             if (fi.type == ARMFault_SyncExternalOnWalk &&
3621                 (env->cp15.scr_el3 & SCR_EA)) {
3622                 target_el = 3;
3623             } else {
3624                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3625                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3626                     env->cp15.hpfar_el2 |= HPFAR_NS;
3627                 }
3628                 target_el = 2;
3629             }
3630             take_exc = true;
3631         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3632             /*
3633              * Synchronous external aborts during a translation table walk
3634              * are taken as Data Abort exceptions.
3635              */
3636             if (fi.stage2) {
3637                 if (current_el == 3) {
3638                     target_el = 3;
3639                 } else {
3640                     target_el = 2;
3641                 }
3642             } else {
3643                 target_el = exception_target_el(env);
3644             }
3645             take_exc = true;
3646         }
3647 
3648         if (take_exc) {
3649             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3650             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3651                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3652                 fsr = arm_fi_to_lfsc(&fi);
3653                 fsc = extract32(fsr, 0, 6);
3654             } else {
3655                 fsr = arm_fi_to_sfsc(&fi);
3656                 fsc = 0x3f;
3657             }
3658             /*
3659              * Report exception with ESR indicating a fault due to a
3660              * translation table walk for a cache maintenance instruction.
3661              */
3662             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3663                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3664             env->exception.vaddress = value;
3665             env->exception.fsr = fsr;
3666             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3667         }
3668     }
3669 
3670     if (is_a64(env)) {
3671         format64 = true;
3672     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3673         /*
3674          * ATS1Cxx:
3675          * * TTBCR.EAE determines whether the result is returned using the
3676          *   32-bit or the 64-bit PAR format
3677          * * Instructions executed in Hyp mode always use the 64bit format
3678          *
3679          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3680          * * The Non-secure TTBCR.EAE bit is set to 1
3681          * * The implementation includes EL2, and the value of HCR.VM is 1
3682          *
3683          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3684          *
3685          * ATS1Hx always uses the 64bit format.
3686          */
3687         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3688 
3689         if (arm_feature(env, ARM_FEATURE_EL2)) {
3690             if (mmu_idx == ARMMMUIdx_E10_0 ||
3691                 mmu_idx == ARMMMUIdx_E10_1 ||
3692                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3693                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3694             } else {
3695                 format64 |= arm_current_el(env) == 2;
3696             }
3697         }
3698     }
3699 
3700     if (format64) {
3701         /* Create a 64-bit PAR */
3702         par64 = (1 << 11); /* LPAE bit always set */
3703         if (!ret) {
3704             par64 |= res.f.phys_addr & ~0xfffULL;
3705             if (!res.f.attrs.secure) {
3706                 par64 |= (1 << 9); /* NS */
3707             }
3708             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3709             par64 |= par_el1_shareability(&res) << 7; /* SH */
3710         } else {
3711             uint32_t fsr = arm_fi_to_lfsc(&fi);
3712 
3713             par64 |= 1; /* F */
3714             par64 |= (fsr & 0x3f) << 1; /* FS */
3715             if (fi.stage2) {
3716                 par64 |= (1 << 9); /* S */
3717             }
3718             if (fi.s1ptw) {
3719                 par64 |= (1 << 8); /* PTW */
3720             }
3721         }
3722     } else {
3723         /*
3724          * fsr is a DFSR/IFSR value for the short descriptor
3725          * translation table format (with WnR always clear).
3726          * Convert it to a 32-bit PAR.
3727          */
3728         if (!ret) {
3729             /* We do not set any attribute bits in the PAR */
3730             if (res.f.lg_page_size == 24
3731                 && arm_feature(env, ARM_FEATURE_V7)) {
3732                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3733             } else {
3734                 par64 = res.f.phys_addr & 0xfffff000;
3735             }
3736             if (!res.f.attrs.secure) {
3737                 par64 |= (1 << 9); /* NS */
3738             }
3739         } else {
3740             uint32_t fsr = arm_fi_to_sfsc(&fi);
3741 
3742             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3743                     ((fsr & 0xf) << 1) | 1;
3744         }
3745     }
3746     return par64;
3747 }
3748 #endif /* CONFIG_TCG */
3749 
3750 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3751 {
3752 #ifdef CONFIG_TCG
3753     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3754     uint64_t par64;
3755     ARMMMUIdx mmu_idx;
3756     int el = arm_current_el(env);
3757     ARMSecuritySpace ss = arm_security_space(env);
3758 
3759     switch (ri->opc2 & 6) {
3760     case 0:
3761         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3762         switch (el) {
3763         case 3:
3764             mmu_idx = ARMMMUIdx_E3;
3765             break;
3766         case 2:
3767             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3768             /* fall through */
3769         case 1:
3770             if (ri->crm == 9 && arm_pan_enabled(env)) {
3771                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3772             } else {
3773                 mmu_idx = ARMMMUIdx_Stage1_E1;
3774             }
3775             break;
3776         default:
3777             g_assert_not_reached();
3778         }
3779         break;
3780     case 2:
3781         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3782         switch (el) {
3783         case 3:
3784             mmu_idx = ARMMMUIdx_E10_0;
3785             break;
3786         case 2:
3787             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3788             mmu_idx = ARMMMUIdx_Stage1_E0;
3789             break;
3790         case 1:
3791             mmu_idx = ARMMMUIdx_Stage1_E0;
3792             break;
3793         default:
3794             g_assert_not_reached();
3795         }
3796         break;
3797     case 4:
3798         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3799         mmu_idx = ARMMMUIdx_E10_1;
3800         ss = ARMSS_NonSecure;
3801         break;
3802     case 6:
3803         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3804         mmu_idx = ARMMMUIdx_E10_0;
3805         ss = ARMSS_NonSecure;
3806         break;
3807     default:
3808         g_assert_not_reached();
3809     }
3810 
3811     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3812 
3813     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3814 #else
3815     /* Handled by hardware accelerator. */
3816     g_assert_not_reached();
3817 #endif /* CONFIG_TCG */
3818 }
3819 
3820 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3821                         uint64_t value)
3822 {
3823 #ifdef CONFIG_TCG
3824     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3825     uint64_t par64;
3826 
3827     /* There is no SecureEL2 for AArch32. */
3828     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3829                          ARMSS_NonSecure);
3830 
3831     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3832 #else
3833     /* Handled by hardware accelerator. */
3834     g_assert_not_reached();
3835 #endif /* CONFIG_TCG */
3836 }
3837 
3838 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3839                                      bool isread)
3840 {
3841     /*
3842      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3843      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3844      * only happen when executing at EL3 because that combination also causes an
3845      * illegal exception return. We don't need to check FEAT_RME either, because
3846      * scr_write() ensures that the NSE bit is not set otherwise.
3847      */
3848     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3849         return CP_ACCESS_TRAP;
3850     }
3851     return CP_ACCESS_OK;
3852 }
3853 
3854 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3855                                      bool isread)
3856 {
3857     if (arm_current_el(env) == 3 &&
3858         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3859         return CP_ACCESS_TRAP;
3860     }
3861     return at_e012_access(env, ri, isread);
3862 }
3863 
3864 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3865                                       bool isread)
3866 {
3867     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3868         return CP_ACCESS_TRAP_EL2;
3869     }
3870     return at_e012_access(env, ri, isread);
3871 }
3872 
3873 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3874                         uint64_t value)
3875 {
3876 #ifdef CONFIG_TCG
3877     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3878     ARMMMUIdx mmu_idx;
3879     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3880     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3881 
3882     switch (ri->opc2 & 6) {
3883     case 0:
3884         switch (ri->opc1) {
3885         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3886             if (ri->crm == 9 && arm_pan_enabled(env)) {
3887                 mmu_idx = regime_e20 ?
3888                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3889             } else {
3890                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3891             }
3892             break;
3893         case 4: /* AT S1E2R, AT S1E2W */
3894             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3895             break;
3896         case 6: /* AT S1E3R, AT S1E3W */
3897             mmu_idx = ARMMMUIdx_E3;
3898             break;
3899         default:
3900             g_assert_not_reached();
3901         }
3902         break;
3903     case 2: /* AT S1E0R, AT S1E0W */
3904         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3905         break;
3906     case 4: /* AT S12E1R, AT S12E1W */
3907         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3908         break;
3909     case 6: /* AT S12E0R, AT S12E0W */
3910         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3911         break;
3912     default:
3913         g_assert_not_reached();
3914     }
3915 
3916     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3917                                        mmu_idx, arm_security_space(env));
3918 #else
3919     /* Handled by hardware accelerator. */
3920     g_assert_not_reached();
3921 #endif /* CONFIG_TCG */
3922 }
3923 #endif
3924 
3925 /* Return basic MPU access permission bits.  */
3926 static uint32_t simple_mpu_ap_bits(uint32_t val)
3927 {
3928     uint32_t ret;
3929     uint32_t mask;
3930     int i;
3931     ret = 0;
3932     mask = 3;
3933     for (i = 0; i < 16; i += 2) {
3934         ret |= (val >> i) & mask;
3935         mask <<= 2;
3936     }
3937     return ret;
3938 }
3939 
3940 /* Pad basic MPU access permission bits to extended format.  */
3941 static uint32_t extended_mpu_ap_bits(uint32_t val)
3942 {
3943     uint32_t ret;
3944     uint32_t mask;
3945     int i;
3946     ret = 0;
3947     mask = 3;
3948     for (i = 0; i < 16; i += 2) {
3949         ret |= (val & mask) << i;
3950         mask <<= 2;
3951     }
3952     return ret;
3953 }
3954 
3955 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3956                                  uint64_t value)
3957 {
3958     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3959 }
3960 
3961 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3962 {
3963     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3964 }
3965 
3966 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3967                                  uint64_t value)
3968 {
3969     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3970 }
3971 
3972 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3973 {
3974     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3975 }
3976 
3977 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3978 {
3979     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3980 
3981     if (!u32p) {
3982         return 0;
3983     }
3984 
3985     u32p += env->pmsav7.rnr[M_REG_NS];
3986     return *u32p;
3987 }
3988 
3989 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3990                          uint64_t value)
3991 {
3992     ARMCPU *cpu = env_archcpu(env);
3993     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3994 
3995     if (!u32p) {
3996         return;
3997     }
3998 
3999     u32p += env->pmsav7.rnr[M_REG_NS];
4000     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4001     *u32p = value;
4002 }
4003 
4004 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4005                               uint64_t value)
4006 {
4007     ARMCPU *cpu = env_archcpu(env);
4008     uint32_t nrgs = cpu->pmsav7_dregion;
4009 
4010     if (value >= nrgs) {
4011         qemu_log_mask(LOG_GUEST_ERROR,
4012                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
4013                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
4014         return;
4015     }
4016 
4017     raw_write(env, ri, value);
4018 }
4019 
4020 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4021                           uint64_t value)
4022 {
4023     ARMCPU *cpu = env_archcpu(env);
4024 
4025     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4026     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4027 }
4028 
4029 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4030 {
4031     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4032 }
4033 
4034 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4035                           uint64_t value)
4036 {
4037     ARMCPU *cpu = env_archcpu(env);
4038 
4039     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4040     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4041 }
4042 
4043 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4044 {
4045     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4046 }
4047 
4048 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4049                            uint64_t value)
4050 {
4051     ARMCPU *cpu = env_archcpu(env);
4052 
4053     /*
4054      * Ignore writes that would select not implemented region.
4055      * This is architecturally UNPREDICTABLE.
4056      */
4057     if (value >= cpu->pmsav7_dregion) {
4058         return;
4059     }
4060 
4061     env->pmsav7.rnr[M_REG_NS] = value;
4062 }
4063 
4064 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4065                           uint64_t value)
4066 {
4067     ARMCPU *cpu = env_archcpu(env);
4068 
4069     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4070     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
4071 }
4072 
4073 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4074 {
4075     return env->pmsav8.hprbar[env->pmsav8.hprselr];
4076 }
4077 
4078 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4079                           uint64_t value)
4080 {
4081     ARMCPU *cpu = env_archcpu(env);
4082 
4083     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4084     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4085 }
4086 
4087 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4088 {
4089     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4090 }
4091 
4092 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4093                           uint64_t value)
4094 {
4095     uint32_t n;
4096     uint32_t bit;
4097     ARMCPU *cpu = env_archcpu(env);
4098 
4099     /* Ignore writes to unimplemented regions */
4100     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4101     value &= MAKE_64BIT_MASK(0, rmax);
4102 
4103     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4104 
4105     /* Register alias is only valid for first 32 indexes */
4106     for (n = 0; n < rmax; ++n) {
4107         bit = extract32(value, n, 1);
4108         env->pmsav8.hprlar[n] = deposit32(
4109                     env->pmsav8.hprlar[n], 0, 1, bit);
4110     }
4111 }
4112 
4113 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4114 {
4115     uint32_t n;
4116     uint32_t result = 0x0;
4117     ARMCPU *cpu = env_archcpu(env);
4118 
4119     /* Register alias is only valid for first 32 indexes */
4120     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4121         if (env->pmsav8.hprlar[n] & 0x1) {
4122             result |= (0x1 << n);
4123         }
4124     }
4125     return result;
4126 }
4127 
4128 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129                            uint64_t value)
4130 {
4131     ARMCPU *cpu = env_archcpu(env);
4132 
4133     /*
4134      * Ignore writes that would select not implemented region.
4135      * This is architecturally UNPREDICTABLE.
4136      */
4137     if (value >= cpu->pmsav8r_hdregion) {
4138         return;
4139     }
4140 
4141     env->pmsav8.hprselr = value;
4142 }
4143 
4144 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145                           uint64_t value)
4146 {
4147     ARMCPU *cpu = env_archcpu(env);
4148     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4149                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4150 
4151     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4152 
4153     if (ri->opc1 & 4) {
4154         if (index >= cpu->pmsav8r_hdregion) {
4155             return;
4156         }
4157         if (ri->opc2 & 0x1) {
4158             env->pmsav8.hprlar[index] = value;
4159         } else {
4160             env->pmsav8.hprbar[index] = value;
4161         }
4162     } else {
4163         if (index >= cpu->pmsav7_dregion) {
4164             return;
4165         }
4166         if (ri->opc2 & 0x1) {
4167             env->pmsav8.rlar[M_REG_NS][index] = value;
4168         } else {
4169             env->pmsav8.rbar[M_REG_NS][index] = value;
4170         }
4171     }
4172 }
4173 
4174 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4175 {
4176     ARMCPU *cpu = env_archcpu(env);
4177     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4178                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4179 
4180     if (ri->opc1 & 4) {
4181         if (index >= cpu->pmsav8r_hdregion) {
4182             return 0x0;
4183         }
4184         if (ri->opc2 & 0x1) {
4185             return env->pmsav8.hprlar[index];
4186         } else {
4187             return env->pmsav8.hprbar[index];
4188         }
4189     } else {
4190         if (index >= cpu->pmsav7_dregion) {
4191             return 0x0;
4192         }
4193         if (ri->opc2 & 0x1) {
4194             return env->pmsav8.rlar[M_REG_NS][index];
4195         } else {
4196             return env->pmsav8.rbar[M_REG_NS][index];
4197         }
4198     }
4199 }
4200 
4201 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4202     { .name = "PRBAR",
4203       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4204       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4205       .accessfn = access_tvm_trvm,
4206       .readfn = prbar_read, .writefn = prbar_write },
4207     { .name = "PRLAR",
4208       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4209       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4210       .accessfn = access_tvm_trvm,
4211       .readfn = prlar_read, .writefn = prlar_write },
4212     { .name = "PRSELR", .resetvalue = 0,
4213       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4214       .access = PL1_RW, .accessfn = access_tvm_trvm,
4215       .writefn = prselr_write,
4216       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4217     { .name = "HPRBAR", .resetvalue = 0,
4218       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4219       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4220       .readfn = hprbar_read, .writefn = hprbar_write },
4221     { .name = "HPRLAR",
4222       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4223       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4224       .readfn = hprlar_read, .writefn = hprlar_write },
4225     { .name = "HPRSELR", .resetvalue = 0,
4226       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4227       .access = PL2_RW,
4228       .writefn = hprselr_write,
4229       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4230     { .name = "HPRENR",
4231       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4232       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4233       .readfn = hprenr_read, .writefn = hprenr_write },
4234 };
4235 
4236 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4237     /*
4238      * Reset for all these registers is handled in arm_cpu_reset(),
4239      * because the PMSAv7 is also used by M-profile CPUs, which do
4240      * not register cpregs but still need the state to be reset.
4241      */
4242     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4243       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4244       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4245       .readfn = pmsav7_read, .writefn = pmsav7_write,
4246       .resetfn = arm_cp_reset_ignore },
4247     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4248       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4249       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4250       .readfn = pmsav7_read, .writefn = pmsav7_write,
4251       .resetfn = arm_cp_reset_ignore },
4252     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4253       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4254       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4255       .readfn = pmsav7_read, .writefn = pmsav7_write,
4256       .resetfn = arm_cp_reset_ignore },
4257     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4258       .access = PL1_RW,
4259       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4260       .writefn = pmsav7_rgnr_write,
4261       .resetfn = arm_cp_reset_ignore },
4262 };
4263 
4264 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4265     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4266       .access = PL1_RW, .type = ARM_CP_ALIAS,
4267       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4268       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4269     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4270       .access = PL1_RW, .type = ARM_CP_ALIAS,
4271       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4272       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4273     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4274       .access = PL1_RW,
4275       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4276       .resetvalue = 0, },
4277     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4278       .access = PL1_RW,
4279       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4280       .resetvalue = 0, },
4281     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4282       .access = PL1_RW,
4283       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4284     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4285       .access = PL1_RW,
4286       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4287     /* Protection region base and size registers */
4288     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4289       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4290       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4291     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4292       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4293       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4294     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4295       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4296       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4297     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4298       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4299       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4300     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4301       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4302       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4303     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4304       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4305       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4306     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4307       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4308       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4309     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4310       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4311       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4312 };
4313 
4314 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4315                              uint64_t value)
4316 {
4317     ARMCPU *cpu = env_archcpu(env);
4318 
4319     if (!arm_feature(env, ARM_FEATURE_V8)) {
4320         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4321             /*
4322              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4323              * using Long-descriptor translation table format
4324              */
4325             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4326         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4327             /*
4328              * In an implementation that includes the Security Extensions
4329              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4330              * Short-descriptor translation table format.
4331              */
4332             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4333         } else {
4334             value &= TTBCR_N;
4335         }
4336     }
4337 
4338     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4339         /*
4340          * With LPAE the TTBCR could result in a change of ASID
4341          * via the TTBCR.A1 bit, so do a TLB flush.
4342          */
4343         tlb_flush(CPU(cpu));
4344     }
4345     raw_write(env, ri, value);
4346 }
4347 
4348 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4349                                uint64_t value)
4350 {
4351     ARMCPU *cpu = env_archcpu(env);
4352 
4353     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4354     tlb_flush(CPU(cpu));
4355     raw_write(env, ri, value);
4356 }
4357 
4358 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4359                             uint64_t value)
4360 {
4361     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4362     if (cpreg_field_is_64bit(ri) &&
4363         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4364         ARMCPU *cpu = env_archcpu(env);
4365         tlb_flush(CPU(cpu));
4366     }
4367     raw_write(env, ri, value);
4368 }
4369 
4370 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4371                                     uint64_t value)
4372 {
4373     /*
4374      * If we are running with E2&0 regime, then an ASID is active.
4375      * Flush if that might be changing.  Note we're not checking
4376      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4377      * holds the active ASID, only checking the field that might.
4378      */
4379     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4380         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4381         uint16_t mask = ARMMMUIdxBit_E20_2 |
4382                         ARMMMUIdxBit_E20_2_PAN |
4383                         ARMMMUIdxBit_E20_0;
4384         tlb_flush_by_mmuidx(env_cpu(env), mask);
4385     }
4386     raw_write(env, ri, value);
4387 }
4388 
4389 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4390                         uint64_t value)
4391 {
4392     ARMCPU *cpu = env_archcpu(env);
4393     CPUState *cs = CPU(cpu);
4394 
4395     /*
4396      * A change in VMID to the stage2 page table (Stage2) invalidates
4397      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4398      */
4399     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4400         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4401     }
4402     raw_write(env, ri, value);
4403 }
4404 
4405 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4406     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4407       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4408       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4409                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4410     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4411       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4412       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4413                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4414     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4415       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4416       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4417                              offsetof(CPUARMState, cp15.dfar_ns) } },
4418     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4419       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4420       .access = PL1_RW, .accessfn = access_tvm_trvm,
4421       .fgt = FGT_FAR_EL1,
4422       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4423       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4424       .resetvalue = 0, },
4425 };
4426 
4427 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4428     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4429       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4430       .access = PL1_RW, .accessfn = access_tvm_trvm,
4431       .fgt = FGT_ESR_EL1,
4432       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4433       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4434     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4435       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4436       .access = PL1_RW, .accessfn = access_tvm_trvm,
4437       .fgt = FGT_TTBR0_EL1,
4438       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4439       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4440       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4441                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4442     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4443       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4444       .access = PL1_RW, .accessfn = access_tvm_trvm,
4445       .fgt = FGT_TTBR1_EL1,
4446       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4447       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4448       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4449                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4450     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4451       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4452       .access = PL1_RW, .accessfn = access_tvm_trvm,
4453       .fgt = FGT_TCR_EL1,
4454       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4455       .writefn = vmsa_tcr_el12_write,
4456       .raw_writefn = raw_write,
4457       .resetvalue = 0,
4458       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4459     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4460       .access = PL1_RW, .accessfn = access_tvm_trvm,
4461       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4462       .raw_writefn = raw_write,
4463       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4464                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4465 };
4466 
4467 /*
4468  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4469  * qemu tlbs nor adjusting cached masks.
4470  */
4471 static const ARMCPRegInfo ttbcr2_reginfo = {
4472     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4473     .access = PL1_RW, .accessfn = access_tvm_trvm,
4474     .type = ARM_CP_ALIAS,
4475     .bank_fieldoffsets = {
4476         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4477         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4478     },
4479 };
4480 
4481 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4482                                 uint64_t value)
4483 {
4484     env->cp15.c15_ticonfig = value & 0xe7;
4485     /* The OS_TYPE bit in this register changes the reported CPUID! */
4486     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4487         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4488 }
4489 
4490 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4491                                 uint64_t value)
4492 {
4493     env->cp15.c15_threadid = value & 0xffff;
4494 }
4495 
4496 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4497                            uint64_t value)
4498 {
4499     /* Wait-for-interrupt (deprecated) */
4500     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4501 }
4502 
4503 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4504                                   uint64_t value)
4505 {
4506     /*
4507      * On OMAP there are registers indicating the max/min index of dcache lines
4508      * containing a dirty line; cache flush operations have to reset these.
4509      */
4510     env->cp15.c15_i_max = 0x000;
4511     env->cp15.c15_i_min = 0xff0;
4512 }
4513 
4514 static const ARMCPRegInfo omap_cp_reginfo[] = {
4515     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4516       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4517       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4518       .resetvalue = 0, },
4519     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4520       .access = PL1_RW, .type = ARM_CP_NOP },
4521     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4522       .access = PL1_RW,
4523       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4524       .writefn = omap_ticonfig_write },
4525     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4526       .access = PL1_RW,
4527       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4528     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4529       .access = PL1_RW, .resetvalue = 0xff0,
4530       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4531     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4532       .access = PL1_RW,
4533       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4534       .writefn = omap_threadid_write },
4535     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4536       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4537       .type = ARM_CP_NO_RAW,
4538       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4539     /*
4540      * TODO: Peripheral port remap register:
4541      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4542      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4543      * when MMU is off.
4544      */
4545     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4546       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4547       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4548       .writefn = omap_cachemaint_write },
4549     { .name = "C9", .cp = 15, .crn = 9,
4550       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4551       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4552 };
4553 
4554 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4555                               uint64_t value)
4556 {
4557     env->cp15.c15_cpar = value & 0x3fff;
4558 }
4559 
4560 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4561     { .name = "XSCALE_CPAR",
4562       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4563       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4564       .writefn = xscale_cpar_write, },
4565     { .name = "XSCALE_AUXCR",
4566       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4567       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4568       .resetvalue = 0, },
4569     /*
4570      * XScale specific cache-lockdown: since we have no cache we NOP these
4571      * and hope the guest does not really rely on cache behaviour.
4572      */
4573     { .name = "XSCALE_LOCK_ICACHE_LINE",
4574       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4575       .access = PL1_W, .type = ARM_CP_NOP },
4576     { .name = "XSCALE_UNLOCK_ICACHE",
4577       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4578       .access = PL1_W, .type = ARM_CP_NOP },
4579     { .name = "XSCALE_DCACHE_LOCK",
4580       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4581       .access = PL1_RW, .type = ARM_CP_NOP },
4582     { .name = "XSCALE_UNLOCK_DCACHE",
4583       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4584       .access = PL1_W, .type = ARM_CP_NOP },
4585 };
4586 
4587 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4588     /*
4589      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4590      * implementation of this implementation-defined space.
4591      * Ideally this should eventually disappear in favour of actually
4592      * implementing the correct behaviour for all cores.
4593      */
4594     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4595       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4596       .access = PL1_RW,
4597       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4598       .resetvalue = 0 },
4599 };
4600 
4601 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4602     /* Cache status: RAZ because we have no cache so it's always clean */
4603     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4604       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4605       .resetvalue = 0 },
4606 };
4607 
4608 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4609     /* We never have a block transfer operation in progress */
4610     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4611       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4612       .resetvalue = 0 },
4613     /* The cache ops themselves: these all NOP for QEMU */
4614     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4615       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4616     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4617       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4618     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4619       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4620     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4621       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4622     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4623       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4624     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4625       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4626 };
4627 
4628 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4629     /*
4630      * The cache test-and-clean instructions always return (1 << 30)
4631      * to indicate that there are no dirty cache lines.
4632      */
4633     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4634       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4635       .resetvalue = (1 << 30) },
4636     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4637       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4638       .resetvalue = (1 << 30) },
4639 };
4640 
4641 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4642     /* Ignore ReadBuffer accesses */
4643     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4644       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4645       .access = PL1_RW, .resetvalue = 0,
4646       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4647 };
4648 
4649 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4650 {
4651     unsigned int cur_el = arm_current_el(env);
4652 
4653     if (arm_is_el2_enabled(env) && cur_el == 1) {
4654         return env->cp15.vpidr_el2;
4655     }
4656     return raw_read(env, ri);
4657 }
4658 
4659 static uint64_t mpidr_read_val(CPUARMState *env)
4660 {
4661     ARMCPU *cpu = env_archcpu(env);
4662     uint64_t mpidr = cpu->mp_affinity;
4663 
4664     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4665         mpidr |= (1U << 31);
4666         /*
4667          * Cores which are uniprocessor (non-coherent)
4668          * but still implement the MP extensions set
4669          * bit 30. (For instance, Cortex-R5).
4670          */
4671         if (cpu->mp_is_up) {
4672             mpidr |= (1u << 30);
4673         }
4674     }
4675     return mpidr;
4676 }
4677 
4678 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4679 {
4680     unsigned int cur_el = arm_current_el(env);
4681 
4682     if (arm_is_el2_enabled(env) && cur_el == 1) {
4683         return env->cp15.vmpidr_el2;
4684     }
4685     return mpidr_read_val(env);
4686 }
4687 
4688 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4689     /* NOP AMAIR0/1 */
4690     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4691       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4692       .access = PL1_RW, .accessfn = access_tvm_trvm,
4693       .fgt = FGT_AMAIR_EL1,
4694       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4695       .type = ARM_CP_CONST, .resetvalue = 0 },
4696     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4697     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4698       .access = PL1_RW, .accessfn = access_tvm_trvm,
4699       .type = ARM_CP_CONST, .resetvalue = 0 },
4700     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4701       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4702       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4703                              offsetof(CPUARMState, cp15.par_ns)} },
4704     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4705       .access = PL1_RW, .accessfn = access_tvm_trvm,
4706       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4707       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4708                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4709       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4710     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4711       .access = PL1_RW, .accessfn = access_tvm_trvm,
4712       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4713       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4714                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4715       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4716 };
4717 
4718 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4719 {
4720     return vfp_get_fpcr(env);
4721 }
4722 
4723 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4724                             uint64_t value)
4725 {
4726     vfp_set_fpcr(env, value);
4727 }
4728 
4729 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4730 {
4731     return vfp_get_fpsr(env);
4732 }
4733 
4734 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4735                             uint64_t value)
4736 {
4737     vfp_set_fpsr(env, value);
4738 }
4739 
4740 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4741                                        bool isread)
4742 {
4743     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4744         return CP_ACCESS_TRAP;
4745     }
4746     return CP_ACCESS_OK;
4747 }
4748 
4749 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4750                             uint64_t value)
4751 {
4752     env->daif = value & PSTATE_DAIF;
4753 }
4754 
4755 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4756 {
4757     return env->pstate & PSTATE_PAN;
4758 }
4759 
4760 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4761                            uint64_t value)
4762 {
4763     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4764 }
4765 
4766 static const ARMCPRegInfo pan_reginfo = {
4767     .name = "PAN", .state = ARM_CP_STATE_AA64,
4768     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4769     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4770     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4771 };
4772 
4773 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4774 {
4775     return env->pstate & PSTATE_UAO;
4776 }
4777 
4778 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4779                            uint64_t value)
4780 {
4781     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4782 }
4783 
4784 static const ARMCPRegInfo uao_reginfo = {
4785     .name = "UAO", .state = ARM_CP_STATE_AA64,
4786     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4787     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4788     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4789 };
4790 
4791 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4792 {
4793     return env->pstate & PSTATE_DIT;
4794 }
4795 
4796 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4797                            uint64_t value)
4798 {
4799     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4800 }
4801 
4802 static const ARMCPRegInfo dit_reginfo = {
4803     .name = "DIT", .state = ARM_CP_STATE_AA64,
4804     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4805     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4806     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4807 };
4808 
4809 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4810 {
4811     return env->pstate & PSTATE_SSBS;
4812 }
4813 
4814 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4815                            uint64_t value)
4816 {
4817     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4818 }
4819 
4820 static const ARMCPRegInfo ssbs_reginfo = {
4821     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4822     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4823     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4824     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4825 };
4826 
4827 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4828                                               const ARMCPRegInfo *ri,
4829                                               bool isread)
4830 {
4831     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4832     switch (arm_current_el(env)) {
4833     case 0:
4834         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4835         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4836             return CP_ACCESS_TRAP;
4837         }
4838         /* fall through */
4839     case 1:
4840         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4841         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4842             return CP_ACCESS_TRAP_EL2;
4843         }
4844         break;
4845     }
4846     return CP_ACCESS_OK;
4847 }
4848 
4849 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4850 {
4851     /* Cache invalidate/clean to Point of Unification... */
4852     switch (arm_current_el(env)) {
4853     case 0:
4854         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4855         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4856             return CP_ACCESS_TRAP;
4857         }
4858         /* fall through */
4859     case 1:
4860         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4861         if (arm_hcr_el2_eff(env) & hcrflags) {
4862             return CP_ACCESS_TRAP_EL2;
4863         }
4864         break;
4865     }
4866     return CP_ACCESS_OK;
4867 }
4868 
4869 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4870                                    bool isread)
4871 {
4872     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4873 }
4874 
4875 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4876                                   bool isread)
4877 {
4878     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4879 }
4880 
4881 /*
4882  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4883  * Page D4-1736 (DDI0487A.b)
4884  */
4885 
4886 static int vae1_tlbmask(CPUARMState *env)
4887 {
4888     uint64_t hcr = arm_hcr_el2_eff(env);
4889     uint16_t mask;
4890 
4891     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4892         mask = ARMMMUIdxBit_E20_2 |
4893                ARMMMUIdxBit_E20_2_PAN |
4894                ARMMMUIdxBit_E20_0;
4895     } else {
4896         mask = ARMMMUIdxBit_E10_1 |
4897                ARMMMUIdxBit_E10_1_PAN |
4898                ARMMMUIdxBit_E10_0;
4899     }
4900     return mask;
4901 }
4902 
4903 static int vae2_tlbmask(CPUARMState *env)
4904 {
4905     uint64_t hcr = arm_hcr_el2_eff(env);
4906     uint16_t mask;
4907 
4908     if (hcr & HCR_E2H) {
4909         mask = ARMMMUIdxBit_E20_2 |
4910                ARMMMUIdxBit_E20_2_PAN |
4911                ARMMMUIdxBit_E20_0;
4912     } else {
4913         mask = ARMMMUIdxBit_E2;
4914     }
4915     return mask;
4916 }
4917 
4918 /* Return 56 if TBI is enabled, 64 otherwise. */
4919 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4920                               uint64_t addr)
4921 {
4922     uint64_t tcr = regime_tcr(env, mmu_idx);
4923     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4924     int select = extract64(addr, 55, 1);
4925 
4926     return (tbi >> select) & 1 ? 56 : 64;
4927 }
4928 
4929 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4930 {
4931     uint64_t hcr = arm_hcr_el2_eff(env);
4932     ARMMMUIdx mmu_idx;
4933 
4934     /* Only the regime of the mmu_idx below is significant. */
4935     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4936         mmu_idx = ARMMMUIdx_E20_0;
4937     } else {
4938         mmu_idx = ARMMMUIdx_E10_0;
4939     }
4940 
4941     return tlbbits_for_regime(env, mmu_idx, addr);
4942 }
4943 
4944 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4945 {
4946     uint64_t hcr = arm_hcr_el2_eff(env);
4947     ARMMMUIdx mmu_idx;
4948 
4949     /*
4950      * Only the regime of the mmu_idx below is significant.
4951      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4952      * only has one.
4953      */
4954     if (hcr & HCR_E2H) {
4955         mmu_idx = ARMMMUIdx_E20_2;
4956     } else {
4957         mmu_idx = ARMMMUIdx_E2;
4958     }
4959 
4960     return tlbbits_for_regime(env, mmu_idx, addr);
4961 }
4962 
4963 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4964                                       uint64_t value)
4965 {
4966     CPUState *cs = env_cpu(env);
4967     int mask = vae1_tlbmask(env);
4968 
4969     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4970 }
4971 
4972 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4973                                     uint64_t value)
4974 {
4975     CPUState *cs = env_cpu(env);
4976     int mask = vae1_tlbmask(env);
4977 
4978     if (tlb_force_broadcast(env)) {
4979         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4980     } else {
4981         tlb_flush_by_mmuidx(cs, mask);
4982     }
4983 }
4984 
4985 static int e2_tlbmask(CPUARMState *env)
4986 {
4987     return (ARMMMUIdxBit_E20_0 |
4988             ARMMMUIdxBit_E20_2 |
4989             ARMMMUIdxBit_E20_2_PAN |
4990             ARMMMUIdxBit_E2);
4991 }
4992 
4993 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4994                                   uint64_t value)
4995 {
4996     CPUState *cs = env_cpu(env);
4997     int mask = alle1_tlbmask(env);
4998 
4999     tlb_flush_by_mmuidx(cs, mask);
5000 }
5001 
5002 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5003                                   uint64_t value)
5004 {
5005     CPUState *cs = env_cpu(env);
5006     int mask = e2_tlbmask(env);
5007 
5008     tlb_flush_by_mmuidx(cs, mask);
5009 }
5010 
5011 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5012                                   uint64_t value)
5013 {
5014     ARMCPU *cpu = env_archcpu(env);
5015     CPUState *cs = CPU(cpu);
5016 
5017     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
5018 }
5019 
5020 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5021                                     uint64_t value)
5022 {
5023     CPUState *cs = env_cpu(env);
5024     int mask = alle1_tlbmask(env);
5025 
5026     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5027 }
5028 
5029 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5030                                     uint64_t value)
5031 {
5032     CPUState *cs = env_cpu(env);
5033     int mask = e2_tlbmask(env);
5034 
5035     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5036 }
5037 
5038 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5039                                     uint64_t value)
5040 {
5041     CPUState *cs = env_cpu(env);
5042 
5043     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
5044 }
5045 
5046 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5047                                  uint64_t value)
5048 {
5049     /*
5050      * Invalidate by VA, EL2
5051      * Currently handles both VAE2 and VALE2, since we don't support
5052      * flush-last-level-only.
5053      */
5054     CPUState *cs = env_cpu(env);
5055     int mask = vae2_tlbmask(env);
5056     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5057     int bits = vae2_tlbbits(env, pageaddr);
5058 
5059     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5060 }
5061 
5062 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5063                                  uint64_t value)
5064 {
5065     /*
5066      * Invalidate by VA, EL3
5067      * Currently handles both VAE3 and VALE3, since we don't support
5068      * flush-last-level-only.
5069      */
5070     ARMCPU *cpu = env_archcpu(env);
5071     CPUState *cs = CPU(cpu);
5072     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5073 
5074     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
5075 }
5076 
5077 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5078                                    uint64_t value)
5079 {
5080     CPUState *cs = env_cpu(env);
5081     int mask = vae1_tlbmask(env);
5082     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5083     int bits = vae1_tlbbits(env, pageaddr);
5084 
5085     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5086 }
5087 
5088 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5089                                  uint64_t value)
5090 {
5091     /*
5092      * Invalidate by VA, EL1&0 (AArch64 version).
5093      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
5094      * since we don't support flush-for-specific-ASID-only or
5095      * flush-last-level-only.
5096      */
5097     CPUState *cs = env_cpu(env);
5098     int mask = vae1_tlbmask(env);
5099     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5100     int bits = vae1_tlbbits(env, pageaddr);
5101 
5102     if (tlb_force_broadcast(env)) {
5103         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5104     } else {
5105         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5106     }
5107 }
5108 
5109 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5110                                    uint64_t value)
5111 {
5112     CPUState *cs = env_cpu(env);
5113     int mask = vae2_tlbmask(env);
5114     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5115     int bits = vae2_tlbbits(env, pageaddr);
5116 
5117     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5118 }
5119 
5120 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5121                                    uint64_t value)
5122 {
5123     CPUState *cs = env_cpu(env);
5124     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5125     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
5126 
5127     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
5128                                                   ARMMMUIdxBit_E3, bits);
5129 }
5130 
5131 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
5132 {
5133     /*
5134      * The MSB of value is the NS field, which only applies if SEL2
5135      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5136      */
5137     return (value >= 0
5138             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5139             && arm_is_secure_below_el3(env)
5140             ? ARMMMUIdxBit_Stage2_S
5141             : ARMMMUIdxBit_Stage2);
5142 }
5143 
5144 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5145                                     uint64_t value)
5146 {
5147     CPUState *cs = env_cpu(env);
5148     int mask = ipas2e1_tlbmask(env, value);
5149     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5150 
5151     if (tlb_force_broadcast(env)) {
5152         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5153     } else {
5154         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5155     }
5156 }
5157 
5158 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5159                                       uint64_t value)
5160 {
5161     CPUState *cs = env_cpu(env);
5162     int mask = ipas2e1_tlbmask(env, value);
5163     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5164 
5165     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5166 }
5167 
5168 #ifdef TARGET_AARCH64
5169 typedef struct {
5170     uint64_t base;
5171     uint64_t length;
5172 } TLBIRange;
5173 
5174 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5175 {
5176     /*
5177      * Note that the TLBI range TG field encoding differs from both
5178      * TG0 and TG1 encodings.
5179      */
5180     switch (tg) {
5181     case 1:
5182         return Gran4K;
5183     case 2:
5184         return Gran16K;
5185     case 3:
5186         return Gran64K;
5187     default:
5188         return GranInvalid;
5189     }
5190 }
5191 
5192 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5193                                      uint64_t value)
5194 {
5195     unsigned int page_size_granule, page_shift, num, scale, exponent;
5196     /* Extract one bit to represent the va selector in use. */
5197     uint64_t select = sextract64(value, 36, 1);
5198     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5199     TLBIRange ret = { };
5200     ARMGranuleSize gran;
5201 
5202     page_size_granule = extract64(value, 46, 2);
5203     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5204 
5205     /* The granule encoded in value must match the granule in use. */
5206     if (gran != param.gran) {
5207         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5208                       page_size_granule);
5209         return ret;
5210     }
5211 
5212     page_shift = arm_granule_bits(gran);
5213     num = extract64(value, 39, 5);
5214     scale = extract64(value, 44, 2);
5215     exponent = (5 * scale) + 1;
5216 
5217     ret.length = (num + 1) << (exponent + page_shift);
5218 
5219     if (param.select) {
5220         ret.base = sextract64(value, 0, 37);
5221     } else {
5222         ret.base = extract64(value, 0, 37);
5223     }
5224     if (param.ds) {
5225         /*
5226          * With DS=1, BaseADDR is always shifted 16 so that it is able
5227          * to address all 52 va bits.  The input address is perforce
5228          * aligned on a 64k boundary regardless of translation granule.
5229          */
5230         page_shift = 16;
5231     }
5232     ret.base <<= page_shift;
5233 
5234     return ret;
5235 }
5236 
5237 static void do_rvae_write(CPUARMState *env, uint64_t value,
5238                           int idxmap, bool synced)
5239 {
5240     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5241     TLBIRange range;
5242     int bits;
5243 
5244     range = tlbi_aa64_get_range(env, one_idx, value);
5245     bits = tlbbits_for_regime(env, one_idx, range.base);
5246 
5247     if (synced) {
5248         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5249                                                   range.base,
5250                                                   range.length,
5251                                                   idxmap,
5252                                                   bits);
5253     } else {
5254         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5255                                   range.length, idxmap, bits);
5256     }
5257 }
5258 
5259 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5260                                   const ARMCPRegInfo *ri,
5261                                   uint64_t value)
5262 {
5263     /*
5264      * Invalidate by VA range, EL1&0.
5265      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5266      * since we don't support flush-for-specific-ASID-only or
5267      * flush-last-level-only.
5268      */
5269 
5270     do_rvae_write(env, value, vae1_tlbmask(env),
5271                   tlb_force_broadcast(env));
5272 }
5273 
5274 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5275                                     const ARMCPRegInfo *ri,
5276                                     uint64_t value)
5277 {
5278     /*
5279      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5280      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5281      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5282      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5283      * shareable specific flushes.
5284      */
5285 
5286     do_rvae_write(env, value, vae1_tlbmask(env), true);
5287 }
5288 
5289 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5290                                   const ARMCPRegInfo *ri,
5291                                   uint64_t value)
5292 {
5293     /*
5294      * Invalidate by VA range, EL2.
5295      * Currently handles all of RVAE2 and RVALE2,
5296      * since we don't support flush-for-specific-ASID-only or
5297      * flush-last-level-only.
5298      */
5299 
5300     do_rvae_write(env, value, vae2_tlbmask(env),
5301                   tlb_force_broadcast(env));
5302 
5303 
5304 }
5305 
5306 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5307                                     const ARMCPRegInfo *ri,
5308                                     uint64_t value)
5309 {
5310     /*
5311      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5312      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5313      * since we don't support flush-for-specific-ASID-only,
5314      * flush-last-level-only or inner/outer shareable specific flushes.
5315      */
5316 
5317     do_rvae_write(env, value, vae2_tlbmask(env), true);
5318 
5319 }
5320 
5321 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5322                                   const ARMCPRegInfo *ri,
5323                                   uint64_t value)
5324 {
5325     /*
5326      * Invalidate by VA range, EL3.
5327      * Currently handles all of RVAE3 and RVALE3,
5328      * since we don't support flush-for-specific-ASID-only or
5329      * flush-last-level-only.
5330      */
5331 
5332     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5333 }
5334 
5335 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5336                                     const ARMCPRegInfo *ri,
5337                                     uint64_t value)
5338 {
5339     /*
5340      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5341      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5342      * since we don't support flush-for-specific-ASID-only,
5343      * flush-last-level-only or inner/outer specific flushes.
5344      */
5345 
5346     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5347 }
5348 
5349 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5350                                      uint64_t value)
5351 {
5352     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5353                   tlb_force_broadcast(env));
5354 }
5355 
5356 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5357                                        const ARMCPRegInfo *ri,
5358                                        uint64_t value)
5359 {
5360     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5361 }
5362 #endif
5363 
5364 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5365                                       bool isread)
5366 {
5367     int cur_el = arm_current_el(env);
5368 
5369     if (cur_el < 2) {
5370         uint64_t hcr = arm_hcr_el2_eff(env);
5371 
5372         if (cur_el == 0) {
5373             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5374                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5375                     return CP_ACCESS_TRAP_EL2;
5376                 }
5377             } else {
5378                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5379                     return CP_ACCESS_TRAP;
5380                 }
5381                 if (hcr & HCR_TDZ) {
5382                     return CP_ACCESS_TRAP_EL2;
5383                 }
5384             }
5385         } else if (hcr & HCR_TDZ) {
5386             return CP_ACCESS_TRAP_EL2;
5387         }
5388     }
5389     return CP_ACCESS_OK;
5390 }
5391 
5392 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5393 {
5394     ARMCPU *cpu = env_archcpu(env);
5395     int dzp_bit = 1 << 4;
5396 
5397     /* DZP indicates whether DC ZVA access is allowed */
5398     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5399         dzp_bit = 0;
5400     }
5401     return cpu->dcz_blocksize | dzp_bit;
5402 }
5403 
5404 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5405                                     bool isread)
5406 {
5407     if (!(env->pstate & PSTATE_SP)) {
5408         /*
5409          * Access to SP_EL0 is undefined if it's being used as
5410          * the stack pointer.
5411          */
5412         return CP_ACCESS_TRAP_UNCATEGORIZED;
5413     }
5414     return CP_ACCESS_OK;
5415 }
5416 
5417 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5418 {
5419     return env->pstate & PSTATE_SP;
5420 }
5421 
5422 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5423 {
5424     update_spsel(env, val);
5425 }
5426 
5427 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5428                         uint64_t value)
5429 {
5430     ARMCPU *cpu = env_archcpu(env);
5431 
5432     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5433         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5434         value &= ~SCTLR_M;
5435     }
5436 
5437     /* ??? Lots of these bits are not implemented.  */
5438 
5439     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5440         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5441             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5442         } else {
5443             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5444                        SCTLR_ATA0 | SCTLR_ATA);
5445         }
5446     }
5447 
5448     if (raw_read(env, ri) == value) {
5449         /*
5450          * Skip the TLB flush if nothing actually changed; Linux likes
5451          * to do a lot of pointless SCTLR writes.
5452          */
5453         return;
5454     }
5455 
5456     raw_write(env, ri, value);
5457 
5458     /* This may enable/disable the MMU, so do a TLB flush.  */
5459     tlb_flush(CPU(cpu));
5460 
5461     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5462         /*
5463          * Normally we would always end the TB on an SCTLR write; see the
5464          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5465          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5466          * of hflags from the translator, so do it here.
5467          */
5468         arm_rebuild_hflags(env);
5469     }
5470 }
5471 
5472 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5473                            uint64_t value)
5474 {
5475     /*
5476      * Some MDCR_EL3 bits affect whether PMU counters are running:
5477      * if we are trying to change any of those then we must
5478      * bracket this update with PMU start/finish calls.
5479      */
5480     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5481 
5482     if (pmu_op) {
5483         pmu_op_start(env);
5484     }
5485     env->cp15.mdcr_el3 = value;
5486     if (pmu_op) {
5487         pmu_op_finish(env);
5488     }
5489 }
5490 
5491 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5492                        uint64_t value)
5493 {
5494     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5495     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5496 }
5497 
5498 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5499                            uint64_t value)
5500 {
5501     /*
5502      * Some MDCR_EL2 bits affect whether PMU counters are running:
5503      * if we are trying to change any of those then we must
5504      * bracket this update with PMU start/finish calls.
5505      */
5506     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5507 
5508     if (pmu_op) {
5509         pmu_op_start(env);
5510     }
5511     env->cp15.mdcr_el2 = value;
5512     if (pmu_op) {
5513         pmu_op_finish(env);
5514     }
5515 }
5516 
5517 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5518                                  bool isread)
5519 {
5520     if (arm_current_el(env) == 1) {
5521         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5522 
5523         if (hcr_nv == (HCR_NV | HCR_NV1)) {
5524             return CP_ACCESS_TRAP_EL2;
5525         }
5526     }
5527     return CP_ACCESS_OK;
5528 }
5529 
5530 #ifdef CONFIG_USER_ONLY
5531 /*
5532  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5533  * code to get around W^X restrictions, where one region is writable and the
5534  * other is executable.
5535  *
5536  * Since the executable region is never written to we cannot detect code
5537  * changes when running in user mode, and rely on the emulated JIT telling us
5538  * that the code has changed by executing this instruction.
5539  */
5540 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5541                           uint64_t value)
5542 {
5543     uint64_t icache_line_mask, start_address, end_address;
5544     const ARMCPU *cpu;
5545 
5546     cpu = env_archcpu(env);
5547 
5548     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5549     start_address = value & ~icache_line_mask;
5550     end_address = value | icache_line_mask;
5551 
5552     mmap_lock();
5553 
5554     tb_invalidate_phys_range(start_address, end_address);
5555 
5556     mmap_unlock();
5557 }
5558 #endif
5559 
5560 static const ARMCPRegInfo v8_cp_reginfo[] = {
5561     /*
5562      * Minimal set of EL0-visible registers. This will need to be expanded
5563      * significantly for system emulation of AArch64 CPUs.
5564      */
5565     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5566       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5567       .access = PL0_RW, .type = ARM_CP_NZCV },
5568     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5569       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5570       .type = ARM_CP_NO_RAW,
5571       .access = PL0_RW, .accessfn = aa64_daif_access,
5572       .fieldoffset = offsetof(CPUARMState, daif),
5573       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5574     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5575       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5576       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5577       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5578     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5579       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5580       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5581       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5582     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5583       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5584       .access = PL0_R, .type = ARM_CP_NO_RAW,
5585       .fgt = FGT_DCZID_EL0,
5586       .readfn = aa64_dczid_read },
5587     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5588       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5589       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5590 #ifndef CONFIG_USER_ONLY
5591       /* Avoid overhead of an access check that always passes in user-mode */
5592       .accessfn = aa64_zva_access,
5593       .fgt = FGT_DCZVA,
5594 #endif
5595     },
5596     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5597       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5598       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5599     /*
5600      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5601      * don't emulate caches.
5602      */
5603     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5604       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5605       .access = PL1_W, .type = ARM_CP_NOP,
5606       .fgt = FGT_ICIALLUIS,
5607       .accessfn = access_ticab },
5608     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5609       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5610       .access = PL1_W, .type = ARM_CP_NOP,
5611       .fgt = FGT_ICIALLU,
5612       .accessfn = access_tocu },
5613     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5614       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5615       .access = PL0_W,
5616       .fgt = FGT_ICIVAU,
5617       .accessfn = access_tocu,
5618 #ifdef CONFIG_USER_ONLY
5619       .type = ARM_CP_NO_RAW,
5620       .writefn = ic_ivau_write
5621 #else
5622       .type = ARM_CP_NOP
5623 #endif
5624     },
5625     /* Cache ops: all NOPs since we don't emulate caches */
5626     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5627       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5628       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5629       .fgt = FGT_DCIVAC,
5630       .type = ARM_CP_NOP },
5631     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5632       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5633       .fgt = FGT_DCISW,
5634       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5635     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5636       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5637       .access = PL0_W, .type = ARM_CP_NOP,
5638       .fgt = FGT_DCCVAC,
5639       .accessfn = aa64_cacheop_poc_access },
5640     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5641       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5642       .fgt = FGT_DCCSW,
5643       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5644     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5645       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5646       .access = PL0_W, .type = ARM_CP_NOP,
5647       .fgt = FGT_DCCVAU,
5648       .accessfn = access_tocu },
5649     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5650       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5651       .access = PL0_W, .type = ARM_CP_NOP,
5652       .fgt = FGT_DCCIVAC,
5653       .accessfn = aa64_cacheop_poc_access },
5654     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5655       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5656       .fgt = FGT_DCCISW,
5657       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5658     /* TLBI operations */
5659     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5660       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5661       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5662       .fgt = FGT_TLBIVMALLE1IS,
5663       .writefn = tlbi_aa64_vmalle1is_write },
5664     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5665       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5666       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5667       .fgt = FGT_TLBIVAE1IS,
5668       .writefn = tlbi_aa64_vae1is_write },
5669     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5670       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5671       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5672       .fgt = FGT_TLBIASIDE1IS,
5673       .writefn = tlbi_aa64_vmalle1is_write },
5674     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5675       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5676       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5677       .fgt = FGT_TLBIVAAE1IS,
5678       .writefn = tlbi_aa64_vae1is_write },
5679     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5680       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5681       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5682       .fgt = FGT_TLBIVALE1IS,
5683       .writefn = tlbi_aa64_vae1is_write },
5684     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5685       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5686       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5687       .fgt = FGT_TLBIVAALE1IS,
5688       .writefn = tlbi_aa64_vae1is_write },
5689     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5690       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5691       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5692       .fgt = FGT_TLBIVMALLE1,
5693       .writefn = tlbi_aa64_vmalle1_write },
5694     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5695       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5696       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5697       .fgt = FGT_TLBIVAE1,
5698       .writefn = tlbi_aa64_vae1_write },
5699     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5700       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5701       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5702       .fgt = FGT_TLBIASIDE1,
5703       .writefn = tlbi_aa64_vmalle1_write },
5704     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5705       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5706       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5707       .fgt = FGT_TLBIVAAE1,
5708       .writefn = tlbi_aa64_vae1_write },
5709     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5710       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5711       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5712       .fgt = FGT_TLBIVALE1,
5713       .writefn = tlbi_aa64_vae1_write },
5714     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5715       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5716       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5717       .fgt = FGT_TLBIVAALE1,
5718       .writefn = tlbi_aa64_vae1_write },
5719     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5721       .access = PL2_W, .type = ARM_CP_NO_RAW,
5722       .writefn = tlbi_aa64_ipas2e1is_write },
5723     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5724       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5725       .access = PL2_W, .type = ARM_CP_NO_RAW,
5726       .writefn = tlbi_aa64_ipas2e1is_write },
5727     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5728       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5729       .access = PL2_W, .type = ARM_CP_NO_RAW,
5730       .writefn = tlbi_aa64_alle1is_write },
5731     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5732       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5733       .access = PL2_W, .type = ARM_CP_NO_RAW,
5734       .writefn = tlbi_aa64_alle1is_write },
5735     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5736       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5737       .access = PL2_W, .type = ARM_CP_NO_RAW,
5738       .writefn = tlbi_aa64_ipas2e1_write },
5739     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5740       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5741       .access = PL2_W, .type = ARM_CP_NO_RAW,
5742       .writefn = tlbi_aa64_ipas2e1_write },
5743     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5744       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5745       .access = PL2_W, .type = ARM_CP_NO_RAW,
5746       .writefn = tlbi_aa64_alle1_write },
5747     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5748       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5749       .access = PL2_W, .type = ARM_CP_NO_RAW,
5750       .writefn = tlbi_aa64_alle1is_write },
5751 #ifndef CONFIG_USER_ONLY
5752     /* 64 bit address translation operations */
5753     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5754       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5755       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5756       .fgt = FGT_ATS1E1R,
5757       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5758     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5760       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5761       .fgt = FGT_ATS1E1W,
5762       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5763     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5764       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5765       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5766       .fgt = FGT_ATS1E0R,
5767       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5768     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5769       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5770       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5771       .fgt = FGT_ATS1E0W,
5772       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5773     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5774       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5775       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5776       .accessfn = at_e012_access, .writefn = ats_write64 },
5777     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5778       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5779       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5780       .accessfn = at_e012_access, .writefn = ats_write64 },
5781     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5782       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5783       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5784       .accessfn = at_e012_access, .writefn = ats_write64 },
5785     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5786       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5787       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5788       .accessfn = at_e012_access, .writefn = ats_write64 },
5789     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5790     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5791       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5792       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5793       .writefn = ats_write64 },
5794     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5795       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5796       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5797       .writefn = ats_write64 },
5798     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5799       .type = ARM_CP_ALIAS,
5800       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5801       .access = PL1_RW, .resetvalue = 0,
5802       .fgt = FGT_PAR_EL1,
5803       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5804       .writefn = par_write },
5805 #endif
5806     /* TLB invalidate last level of translation table walk */
5807     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5808       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5809       .writefn = tlbimva_is_write },
5810     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5811       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5812       .writefn = tlbimvaa_is_write },
5813     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5814       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5815       .writefn = tlbimva_write },
5816     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5817       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5818       .writefn = tlbimvaa_write },
5819     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5820       .type = ARM_CP_NO_RAW, .access = PL2_W,
5821       .writefn = tlbimva_hyp_write },
5822     { .name = "TLBIMVALHIS",
5823       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5824       .type = ARM_CP_NO_RAW, .access = PL2_W,
5825       .writefn = tlbimva_hyp_is_write },
5826     { .name = "TLBIIPAS2",
5827       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5828       .type = ARM_CP_NO_RAW, .access = PL2_W,
5829       .writefn = tlbiipas2_hyp_write },
5830     { .name = "TLBIIPAS2IS",
5831       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5832       .type = ARM_CP_NO_RAW, .access = PL2_W,
5833       .writefn = tlbiipas2is_hyp_write },
5834     { .name = "TLBIIPAS2L",
5835       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5836       .type = ARM_CP_NO_RAW, .access = PL2_W,
5837       .writefn = tlbiipas2_hyp_write },
5838     { .name = "TLBIIPAS2LIS",
5839       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5840       .type = ARM_CP_NO_RAW, .access = PL2_W,
5841       .writefn = tlbiipas2is_hyp_write },
5842     /* 32 bit cache operations */
5843     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5844       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5845     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5846       .type = ARM_CP_NOP, .access = PL1_W },
5847     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5848       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5849     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5850       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5851     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5852       .type = ARM_CP_NOP, .access = PL1_W },
5853     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5854       .type = ARM_CP_NOP, .access = PL1_W },
5855     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5856       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5857     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5858       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5859     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5860       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5861     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5862       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5863     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5864       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5865     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5866       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5867     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5868       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5869     /* MMU Domain access control / MPU write buffer control */
5870     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5871       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5872       .writefn = dacr_write, .raw_writefn = raw_write,
5873       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5874                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5875     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5876       .type = ARM_CP_ALIAS,
5877       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5878       .access = PL1_RW, .accessfn = access_nv1,
5879       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5880       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5881     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5882       .type = ARM_CP_ALIAS,
5883       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5884       .access = PL1_RW, .accessfn = access_nv1,
5885       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5886       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5887     /*
5888      * We rely on the access checks not allowing the guest to write to the
5889      * state field when SPSel indicates that it's being used as the stack
5890      * pointer.
5891      */
5892     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5893       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5894       .access = PL1_RW, .accessfn = sp_el0_access,
5895       .type = ARM_CP_ALIAS,
5896       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5897     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5898       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5899       .nv2_redirect_offset = 0x240,
5900       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5901       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5902     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5903       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5904       .type = ARM_CP_NO_RAW,
5905       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5906     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5907       .type = ARM_CP_ALIAS,
5908       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5909       .access = PL2_RW,
5910       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5911     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5912       .type = ARM_CP_ALIAS,
5913       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5914       .access = PL2_RW,
5915       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5916     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5917       .type = ARM_CP_ALIAS,
5918       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5919       .access = PL2_RW,
5920       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5921     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5922       .type = ARM_CP_ALIAS,
5923       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5924       .access = PL2_RW,
5925       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5926     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5927       .type = ARM_CP_IO,
5928       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5929       .resetvalue = 0,
5930       .access = PL3_RW,
5931       .writefn = mdcr_el3_write,
5932       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5933     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5934       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5935       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5936       .writefn = sdcr_write,
5937       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5938 };
5939 
5940 /* These are present only when EL1 supports AArch32 */
5941 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5942     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5943       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5944       .access = PL2_RW,
5945       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5946       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5947     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5948       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5949       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5950       .writefn = dacr_write, .raw_writefn = raw_write,
5951       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5952     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5953       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5954       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5955       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5956 };
5957 
5958 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5959 {
5960     ARMCPU *cpu = env_archcpu(env);
5961 
5962     if (arm_feature(env, ARM_FEATURE_V8)) {
5963         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5964     } else {
5965         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5966     }
5967 
5968     if (arm_feature(env, ARM_FEATURE_EL3)) {
5969         valid_mask &= ~HCR_HCD;
5970     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5971         /*
5972          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5973          * However, if we're using the SMC PSCI conduit then QEMU is
5974          * effectively acting like EL3 firmware and so the guest at
5975          * EL2 should retain the ability to prevent EL1 from being
5976          * able to make SMC calls into the ersatz firmware, so in
5977          * that case HCR.TSC should be read/write.
5978          */
5979         valid_mask &= ~HCR_TSC;
5980     }
5981 
5982     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5983         if (cpu_isar_feature(aa64_vh, cpu)) {
5984             valid_mask |= HCR_E2H;
5985         }
5986         if (cpu_isar_feature(aa64_ras, cpu)) {
5987             valid_mask |= HCR_TERR | HCR_TEA;
5988         }
5989         if (cpu_isar_feature(aa64_lor, cpu)) {
5990             valid_mask |= HCR_TLOR;
5991         }
5992         if (cpu_isar_feature(aa64_pauth, cpu)) {
5993             valid_mask |= HCR_API | HCR_APK;
5994         }
5995         if (cpu_isar_feature(aa64_mte, cpu)) {
5996             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5997         }
5998         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5999             valid_mask |= HCR_ENSCXT;
6000         }
6001         if (cpu_isar_feature(aa64_fwb, cpu)) {
6002             valid_mask |= HCR_FWB;
6003         }
6004         if (cpu_isar_feature(aa64_rme, cpu)) {
6005             valid_mask |= HCR_GPF;
6006         }
6007         if (cpu_isar_feature(aa64_nv, cpu)) {
6008             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
6009         }
6010         if (cpu_isar_feature(aa64_nv2, cpu)) {
6011             valid_mask |= HCR_NV2;
6012         }
6013     }
6014 
6015     if (cpu_isar_feature(any_evt, cpu)) {
6016         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
6017     } else if (cpu_isar_feature(any_half_evt, cpu)) {
6018         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
6019     }
6020 
6021     /* Clear RES0 bits.  */
6022     value &= valid_mask;
6023 
6024     /*
6025      * These bits change the MMU setup:
6026      * HCR_VM enables stage 2 translation
6027      * HCR_PTW forbids certain page-table setups
6028      * HCR_DC disables stage1 and enables stage2 translation
6029      * HCR_DCT enables tagging on (disabled) stage1 translation
6030      * HCR_FWB changes the interpretation of stage2 descriptor bits
6031      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
6032      */
6033     if ((env->cp15.hcr_el2 ^ value) &
6034         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
6035         tlb_flush(CPU(cpu));
6036     }
6037     env->cp15.hcr_el2 = value;
6038 
6039     /*
6040      * Updates to VI and VF require us to update the status of
6041      * virtual interrupts, which are the logical OR of these bits
6042      * and the state of the input lines from the GIC. (This requires
6043      * that we have the BQL, which is done by marking the
6044      * reginfo structs as ARM_CP_IO.)
6045      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
6046      * possible for it to be taken immediately, because VIRQ and
6047      * VFIQ are masked unless running at EL0 or EL1, and HCR
6048      * can only be written at EL2.
6049      */
6050     g_assert(bql_locked());
6051     arm_cpu_update_virq(cpu);
6052     arm_cpu_update_vfiq(cpu);
6053     arm_cpu_update_vserr(cpu);
6054 }
6055 
6056 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
6057 {
6058     do_hcr_write(env, value, 0);
6059 }
6060 
6061 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
6062                           uint64_t value)
6063 {
6064     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
6065     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
6066     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
6067 }
6068 
6069 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
6070                          uint64_t value)
6071 {
6072     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
6073     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
6074     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
6075 }
6076 
6077 /*
6078  * Return the effective value of HCR_EL2, at the given security state.
6079  * Bits that are not included here:
6080  * RW       (read from SCR_EL3.RW as needed)
6081  */
6082 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
6083 {
6084     uint64_t ret = env->cp15.hcr_el2;
6085 
6086     assert(space != ARMSS_Root);
6087 
6088     if (!arm_is_el2_enabled_secstate(env, space)) {
6089         /*
6090          * "This register has no effect if EL2 is not enabled in the
6091          * current Security state".  This is ARMv8.4-SecEL2 speak for
6092          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
6093          *
6094          * Prior to that, the language was "In an implementation that
6095          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
6096          * as if this field is 0 for all purposes other than a direct
6097          * read or write access of HCR_EL2".  With lots of enumeration
6098          * on a per-field basis.  In current QEMU, this is condition
6099          * is arm_is_secure_below_el3.
6100          *
6101          * Since the v8.4 language applies to the entire register, and
6102          * appears to be backward compatible, use that.
6103          */
6104         return 0;
6105     }
6106 
6107     /*
6108      * For a cpu that supports both aarch64 and aarch32, we can set bits
6109      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
6110      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
6111      */
6112     if (!arm_el_is_aa64(env, 2)) {
6113         uint64_t aa32_valid;
6114 
6115         /*
6116          * These bits are up-to-date as of ARMv8.6.
6117          * For HCR, it's easiest to list just the 2 bits that are invalid.
6118          * For HCR2, list those that are valid.
6119          */
6120         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
6121         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
6122                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
6123         ret &= aa32_valid;
6124     }
6125 
6126     if (ret & HCR_TGE) {
6127         /* These bits are up-to-date as of ARMv8.6.  */
6128         if (ret & HCR_E2H) {
6129             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
6130                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
6131                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
6132                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
6133                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6134                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6135         } else {
6136             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6137         }
6138         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6139                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6140                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6141                  HCR_TLOR);
6142     }
6143 
6144     return ret;
6145 }
6146 
6147 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6148 {
6149     if (arm_feature(env, ARM_FEATURE_M)) {
6150         return 0;
6151     }
6152     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6153 }
6154 
6155 /*
6156  * Corresponds to ARM pseudocode function ELIsInHost().
6157  */
6158 bool el_is_in_host(CPUARMState *env, int el)
6159 {
6160     uint64_t mask;
6161 
6162     /*
6163      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6164      * Perform the simplest bit tests first, and validate EL2 afterward.
6165      */
6166     if (el & 1) {
6167         return false; /* EL1 or EL3 */
6168     }
6169 
6170     /*
6171      * Note that hcr_write() checks isar_feature_aa64_vh(),
6172      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6173      */
6174     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6175     if ((env->cp15.hcr_el2 & mask) != mask) {
6176         return false;
6177     }
6178 
6179     /* TGE and/or E2H set: double check those bits are currently legal. */
6180     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6181 }
6182 
6183 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6184                        uint64_t value)
6185 {
6186     uint64_t valid_mask = 0;
6187 
6188     /* FEAT_MOPS adds MSCEn and MCE2 */
6189     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6190         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6191     }
6192 
6193     /* Clear RES0 bits.  */
6194     env->cp15.hcrx_el2 = value & valid_mask;
6195 }
6196 
6197 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6198                                   bool isread)
6199 {
6200     if (arm_current_el(env) == 2
6201         && arm_feature(env, ARM_FEATURE_EL3)
6202         && !(env->cp15.scr_el3 & SCR_HXEN)) {
6203         return CP_ACCESS_TRAP_EL3;
6204     }
6205     return CP_ACCESS_OK;
6206 }
6207 
6208 static const ARMCPRegInfo hcrx_el2_reginfo = {
6209     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6210     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6211     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6212     .nv2_redirect_offset = 0xa0,
6213     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6214 };
6215 
6216 /* Return the effective value of HCRX_EL2.  */
6217 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6218 {
6219     /*
6220      * The bits in this register behave as 0 for all purposes other than
6221      * direct reads of the register if SCR_EL3.HXEn is 0.
6222      * If EL2 is not enabled in the current security state, then the
6223      * bit may behave as if 0, or as if 1, depending on the bit.
6224      * For the moment, we treat the EL2-disabled case as taking
6225      * priority over the HXEn-disabled case. This is true for the only
6226      * bit for a feature which we implement where the answer is different
6227      * for the two cases (MSCEn for FEAT_MOPS).
6228      * This may need to be revisited for future bits.
6229      */
6230     if (!arm_is_el2_enabled(env)) {
6231         uint64_t hcrx = 0;
6232         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6233             /* MSCEn behaves as 1 if EL2 is not enabled */
6234             hcrx |= HCRX_MSCEN;
6235         }
6236         return hcrx;
6237     }
6238     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6239         return 0;
6240     }
6241     return env->cp15.hcrx_el2;
6242 }
6243 
6244 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6245                            uint64_t value)
6246 {
6247     /*
6248      * For A-profile AArch32 EL3, if NSACR.CP10
6249      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6250      */
6251     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6252         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6253         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6254         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6255     }
6256     env->cp15.cptr_el[2] = value;
6257 }
6258 
6259 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6260 {
6261     /*
6262      * For A-profile AArch32 EL3, if NSACR.CP10
6263      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6264      */
6265     uint64_t value = env->cp15.cptr_el[2];
6266 
6267     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6268         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6269         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6270     }
6271     return value;
6272 }
6273 
6274 static const ARMCPRegInfo el2_cp_reginfo[] = {
6275     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6276       .type = ARM_CP_IO,
6277       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6278       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6279       .nv2_redirect_offset = 0x78,
6280       .writefn = hcr_write, .raw_writefn = raw_write },
6281     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6282       .type = ARM_CP_ALIAS | ARM_CP_IO,
6283       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6284       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6285       .writefn = hcr_writelow },
6286     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6287       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6288       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6289     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6290       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6291       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6292       .access = PL2_RW,
6293       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6294     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6295       .type = ARM_CP_NV2_REDIRECT,
6296       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6297       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6298     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6299       .type = ARM_CP_NV2_REDIRECT,
6300       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6301       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6302     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6303       .type = ARM_CP_ALIAS,
6304       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6305       .access = PL2_RW,
6306       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6307     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6308       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6309       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6310       .access = PL2_RW,
6311       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6312     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6313       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6314       .access = PL2_RW, .writefn = vbar_write,
6315       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6316       .resetvalue = 0 },
6317     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6318       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6319       .access = PL3_RW, .type = ARM_CP_ALIAS,
6320       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6321     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6322       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6323       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6324       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6325       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6326     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6327       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6328       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6329       .resetvalue = 0 },
6330     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6331       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6332       .access = PL2_RW, .type = ARM_CP_ALIAS,
6333       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6334     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6335       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6336       .access = PL2_RW, .type = ARM_CP_CONST,
6337       .resetvalue = 0 },
6338     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6339     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6340       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6341       .access = PL2_RW, .type = ARM_CP_CONST,
6342       .resetvalue = 0 },
6343     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6344       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6345       .access = PL2_RW, .type = ARM_CP_CONST,
6346       .resetvalue = 0 },
6347     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6348       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6349       .access = PL2_RW, .type = ARM_CP_CONST,
6350       .resetvalue = 0 },
6351     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6352       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6353       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6354       .raw_writefn = raw_write,
6355       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6356     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6357       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6358       .type = ARM_CP_ALIAS,
6359       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6360       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6361     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6362       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6363       .access = PL2_RW,
6364       .nv2_redirect_offset = 0x40,
6365       /* no .writefn needed as this can't cause an ASID change */
6366       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6367     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6368       .cp = 15, .opc1 = 6, .crm = 2,
6369       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6370       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6371       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6372       .writefn = vttbr_write, .raw_writefn = raw_write },
6373     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6374       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6375       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6376       .nv2_redirect_offset = 0x20,
6377       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6378     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6379       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6380       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6381       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6382     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6383       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6384       .access = PL2_RW, .resetvalue = 0,
6385       .nv2_redirect_offset = 0x90,
6386       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6387     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6388       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6389       .access = PL2_RW, .resetvalue = 0,
6390       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6391       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6392     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6393       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6394       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6395     { .name = "TLBIALLNSNH",
6396       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6397       .type = ARM_CP_NO_RAW, .access = PL2_W,
6398       .writefn = tlbiall_nsnh_write },
6399     { .name = "TLBIALLNSNHIS",
6400       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6401       .type = ARM_CP_NO_RAW, .access = PL2_W,
6402       .writefn = tlbiall_nsnh_is_write },
6403     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6404       .type = ARM_CP_NO_RAW, .access = PL2_W,
6405       .writefn = tlbiall_hyp_write },
6406     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6407       .type = ARM_CP_NO_RAW, .access = PL2_W,
6408       .writefn = tlbiall_hyp_is_write },
6409     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6410       .type = ARM_CP_NO_RAW, .access = PL2_W,
6411       .writefn = tlbimva_hyp_write },
6412     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6413       .type = ARM_CP_NO_RAW, .access = PL2_W,
6414       .writefn = tlbimva_hyp_is_write },
6415     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6416       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6417       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6418       .writefn = tlbi_aa64_alle2_write },
6419     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6420       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6421       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6422       .writefn = tlbi_aa64_vae2_write },
6423     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6424       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6425       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6426       .writefn = tlbi_aa64_vae2_write },
6427     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6428       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6429       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6430       .writefn = tlbi_aa64_alle2is_write },
6431     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6432       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6433       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6434       .writefn = tlbi_aa64_vae2is_write },
6435     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6436       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6437       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6438       .writefn = tlbi_aa64_vae2is_write },
6439 #ifndef CONFIG_USER_ONLY
6440     /*
6441      * Unlike the other EL2-related AT operations, these must
6442      * UNDEF from EL3 if EL2 is not implemented, which is why we
6443      * define them here rather than with the rest of the AT ops.
6444      */
6445     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6446       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6447       .access = PL2_W, .accessfn = at_s1e2_access,
6448       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6449       .writefn = ats_write64 },
6450     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6451       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6452       .access = PL2_W, .accessfn = at_s1e2_access,
6453       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6454       .writefn = ats_write64 },
6455     /*
6456      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6457      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6458      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6459      * to behave as if SCR.NS was 1.
6460      */
6461     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6462       .access = PL2_W,
6463       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6464     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6465       .access = PL2_W,
6466       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6467     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6468       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6469       /*
6470        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6471        * reset values as IMPDEF. We choose to reset to 3 to comply with
6472        * both ARMv7 and ARMv8.
6473        */
6474       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6475       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6476       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6477     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6478       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6479       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6480       .writefn = gt_cntvoff_write,
6481       .nv2_redirect_offset = 0x60,
6482       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6483     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6484       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6485       .writefn = gt_cntvoff_write,
6486       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6487     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6488       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6489       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6490       .type = ARM_CP_IO, .access = PL2_RW,
6491       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6492     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6493       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6494       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6495       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6496     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6497       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6498       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6499       .resetfn = gt_hyp_timer_reset,
6500       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6501     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6502       .type = ARM_CP_IO,
6503       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6504       .access = PL2_RW,
6505       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6506       .resetvalue = 0,
6507       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6508 #endif
6509     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6510       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6511       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6512       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6513     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6514       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6515       .access = PL2_RW,
6516       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6517     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6518       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6519       .access = PL2_RW,
6520       .nv2_redirect_offset = 0x80,
6521       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6522 };
6523 
6524 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6525     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6526       .type = ARM_CP_ALIAS | ARM_CP_IO,
6527       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6528       .access = PL2_RW,
6529       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6530       .writefn = hcr_writehigh },
6531 };
6532 
6533 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6534                                   bool isread)
6535 {
6536     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6537         return CP_ACCESS_OK;
6538     }
6539     return CP_ACCESS_TRAP_UNCATEGORIZED;
6540 }
6541 
6542 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6543     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6544       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6545       .access = PL2_RW, .accessfn = sel2_access,
6546       .nv2_redirect_offset = 0x30,
6547       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6548     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6549       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6550       .access = PL2_RW, .accessfn = sel2_access,
6551       .nv2_redirect_offset = 0x48,
6552       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6553 };
6554 
6555 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6556                                    bool isread)
6557 {
6558     /*
6559      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6560      * At Secure EL1 it traps to EL3 or EL2.
6561      */
6562     if (arm_current_el(env) == 3) {
6563         return CP_ACCESS_OK;
6564     }
6565     if (arm_is_secure_below_el3(env)) {
6566         if (env->cp15.scr_el3 & SCR_EEL2) {
6567             return CP_ACCESS_TRAP_EL2;
6568         }
6569         return CP_ACCESS_TRAP_EL3;
6570     }
6571     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6572     if (isread) {
6573         return CP_ACCESS_OK;
6574     }
6575     return CP_ACCESS_TRAP_UNCATEGORIZED;
6576 }
6577 
6578 static const ARMCPRegInfo el3_cp_reginfo[] = {
6579     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6580       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6581       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6582       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6583     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6584       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6585       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6586       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6587       .writefn = scr_write, .raw_writefn = raw_write },
6588     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6589       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6590       .access = PL3_RW, .resetvalue = 0,
6591       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6592     { .name = "SDER",
6593       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6594       .access = PL3_RW, .resetvalue = 0,
6595       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6596     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6597       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6598       .writefn = vbar_write, .resetvalue = 0,
6599       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6600     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6601       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6602       .access = PL3_RW, .resetvalue = 0,
6603       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6604     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6605       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6606       .access = PL3_RW,
6607       /* no .writefn needed as this can't cause an ASID change */
6608       .resetvalue = 0,
6609       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6610     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6611       .type = ARM_CP_ALIAS,
6612       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6613       .access = PL3_RW,
6614       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6615     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6616       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6617       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6618     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6619       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6620       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6621     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6622       .type = ARM_CP_ALIAS,
6623       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6624       .access = PL3_RW,
6625       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6626     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6627       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6628       .access = PL3_RW, .writefn = vbar_write,
6629       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6630       .resetvalue = 0 },
6631     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6632       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6633       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6634       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6635     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6636       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6637       .access = PL3_RW, .resetvalue = 0,
6638       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6639     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6640       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6641       .access = PL3_RW, .type = ARM_CP_CONST,
6642       .resetvalue = 0 },
6643     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6644       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6645       .access = PL3_RW, .type = ARM_CP_CONST,
6646       .resetvalue = 0 },
6647     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6648       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6649       .access = PL3_RW, .type = ARM_CP_CONST,
6650       .resetvalue = 0 },
6651     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6652       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6653       .access = PL3_W, .type = ARM_CP_NO_RAW,
6654       .writefn = tlbi_aa64_alle3is_write },
6655     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6656       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6657       .access = PL3_W, .type = ARM_CP_NO_RAW,
6658       .writefn = tlbi_aa64_vae3is_write },
6659     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6660       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6661       .access = PL3_W, .type = ARM_CP_NO_RAW,
6662       .writefn = tlbi_aa64_vae3is_write },
6663     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6664       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6665       .access = PL3_W, .type = ARM_CP_NO_RAW,
6666       .writefn = tlbi_aa64_alle3_write },
6667     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6668       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6669       .access = PL3_W, .type = ARM_CP_NO_RAW,
6670       .writefn = tlbi_aa64_vae3_write },
6671     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6672       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6673       .access = PL3_W, .type = ARM_CP_NO_RAW,
6674       .writefn = tlbi_aa64_vae3_write },
6675 };
6676 
6677 #ifndef CONFIG_USER_ONLY
6678 
6679 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6680                                  bool isread)
6681 {
6682     if (arm_current_el(env) == 1) {
6683         /* This must be a FEAT_NV access */
6684         return CP_ACCESS_OK;
6685     }
6686     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6687         return CP_ACCESS_TRAP_UNCATEGORIZED;
6688     }
6689     return CP_ACCESS_OK;
6690 }
6691 
6692 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6693                                       bool isread)
6694 {
6695     if (arm_current_el(env) == 1) {
6696         /* This must be a FEAT_NV access with NVx == 101 */
6697         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6698             return CP_ACCESS_TRAP_EL2;
6699         }
6700     }
6701     return e2h_access(env, ri, isread);
6702 }
6703 
6704 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6705                                       bool isread)
6706 {
6707     if (arm_current_el(env) == 1) {
6708         /* This must be a FEAT_NV access with NVx == 101 */
6709         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6710             return CP_ACCESS_TRAP_EL2;
6711         }
6712     }
6713     return e2h_access(env, ri, isread);
6714 }
6715 
6716 /* Test if system register redirection is to occur in the current state.  */
6717 static bool redirect_for_e2h(CPUARMState *env)
6718 {
6719     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6720 }
6721 
6722 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6723 {
6724     CPReadFn *readfn;
6725 
6726     if (redirect_for_e2h(env)) {
6727         /* Switch to the saved EL2 version of the register.  */
6728         ri = ri->opaque;
6729         readfn = ri->readfn;
6730     } else {
6731         readfn = ri->orig_readfn;
6732     }
6733     if (readfn == NULL) {
6734         readfn = raw_read;
6735     }
6736     return readfn(env, ri);
6737 }
6738 
6739 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6740                           uint64_t value)
6741 {
6742     CPWriteFn *writefn;
6743 
6744     if (redirect_for_e2h(env)) {
6745         /* Switch to the saved EL2 version of the register.  */
6746         ri = ri->opaque;
6747         writefn = ri->writefn;
6748     } else {
6749         writefn = ri->orig_writefn;
6750     }
6751     if (writefn == NULL) {
6752         writefn = raw_write;
6753     }
6754     writefn(env, ri, value);
6755 }
6756 
6757 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6758 {
6759     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6760     return ri->orig_readfn(env, ri->opaque);
6761 }
6762 
6763 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6764                               uint64_t value)
6765 {
6766     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6767     return ri->orig_writefn(env, ri->opaque, value);
6768 }
6769 
6770 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6771                                          const ARMCPRegInfo *ri,
6772                                          bool isread)
6773 {
6774     if (arm_current_el(env) == 1) {
6775         /*
6776          * This must be a FEAT_NV access (will either trap or redirect
6777          * to memory). None of the registers with _EL12 aliases want to
6778          * apply their trap controls for this kind of access, so don't
6779          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6780          */
6781         return CP_ACCESS_OK;
6782     }
6783     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6784     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6785         return CP_ACCESS_TRAP_UNCATEGORIZED;
6786     }
6787     if (ri->orig_accessfn) {
6788         return ri->orig_accessfn(env, ri->opaque, isread);
6789     }
6790     return CP_ACCESS_OK;
6791 }
6792 
6793 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6794 {
6795     struct E2HAlias {
6796         uint32_t src_key, dst_key, new_key;
6797         const char *src_name, *dst_name, *new_name;
6798         bool (*feature)(const ARMISARegisters *id);
6799     };
6800 
6801 #define K(op0, op1, crn, crm, op2) \
6802     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6803 
6804     static const struct E2HAlias aliases[] = {
6805         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6806           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6807         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6808           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6809         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6810           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6811         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6812           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6813         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6814           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6815         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6816           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6817         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6818           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6819         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6820           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6821         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6822           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6823         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6824           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6825         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6826           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6827         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6828           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6829         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6830           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6831         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6832           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6833         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6834           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6835         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6836           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6837 
6838         /*
6839          * Note that redirection of ZCR is mentioned in the description
6840          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6841          * not in the summary table.
6842          */
6843         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6844           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6845         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6846           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6847 
6848         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6849           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6850 
6851         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6852           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6853           isar_feature_aa64_scxtnum },
6854 
6855         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6856         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6857     };
6858 #undef K
6859 
6860     size_t i;
6861 
6862     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6863         const struct E2HAlias *a = &aliases[i];
6864         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6865         bool ok;
6866 
6867         if (a->feature && !a->feature(&cpu->isar)) {
6868             continue;
6869         }
6870 
6871         src_reg = g_hash_table_lookup(cpu->cp_regs,
6872                                       (gpointer)(uintptr_t)a->src_key);
6873         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6874                                       (gpointer)(uintptr_t)a->dst_key);
6875         g_assert(src_reg != NULL);
6876         g_assert(dst_reg != NULL);
6877 
6878         /* Cross-compare names to detect typos in the keys.  */
6879         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6880         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6881 
6882         /* None of the core system registers use opaque; we will.  */
6883         g_assert(src_reg->opaque == NULL);
6884 
6885         /* Create alias before redirection so we dup the right data. */
6886         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6887 
6888         new_reg->name = a->new_name;
6889         new_reg->type |= ARM_CP_ALIAS;
6890         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6891         new_reg->access &= PL2_RW | PL3_RW;
6892         /* The new_reg op fields are as per new_key, not the target reg */
6893         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6894             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6895         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6896             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6897         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6898             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6899         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6900             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6901         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6902             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6903         new_reg->opaque = src_reg;
6904         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6905         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6906         new_reg->orig_accessfn = src_reg->accessfn;
6907         if (!new_reg->raw_readfn) {
6908             new_reg->raw_readfn = raw_read;
6909         }
6910         if (!new_reg->raw_writefn) {
6911             new_reg->raw_writefn = raw_write;
6912         }
6913         new_reg->readfn = el2_e2h_e12_read;
6914         new_reg->writefn = el2_e2h_e12_write;
6915         new_reg->accessfn = el2_e2h_e12_access;
6916 
6917         /*
6918          * If the _EL1 register is redirected to memory by FEAT_NV2,
6919          * then it shares the offset with the _EL12 register,
6920          * and which one is redirected depends on HCR_EL2.NV1.
6921          */
6922         if (new_reg->nv2_redirect_offset) {
6923             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6924             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6925             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6926         }
6927 
6928         ok = g_hash_table_insert(cpu->cp_regs,
6929                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6930         g_assert(ok);
6931 
6932         src_reg->opaque = dst_reg;
6933         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6934         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6935         if (!src_reg->raw_readfn) {
6936             src_reg->raw_readfn = raw_read;
6937         }
6938         if (!src_reg->raw_writefn) {
6939             src_reg->raw_writefn = raw_write;
6940         }
6941         src_reg->readfn = el2_e2h_read;
6942         src_reg->writefn = el2_e2h_write;
6943     }
6944 }
6945 #endif
6946 
6947 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6948                                      bool isread)
6949 {
6950     int cur_el = arm_current_el(env);
6951 
6952     if (cur_el < 2) {
6953         uint64_t hcr = arm_hcr_el2_eff(env);
6954 
6955         if (cur_el == 0) {
6956             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6957                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6958                     return CP_ACCESS_TRAP_EL2;
6959                 }
6960             } else {
6961                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6962                     return CP_ACCESS_TRAP;
6963                 }
6964                 if (hcr & HCR_TID2) {
6965                     return CP_ACCESS_TRAP_EL2;
6966                 }
6967             }
6968         } else if (hcr & HCR_TID2) {
6969             return CP_ACCESS_TRAP_EL2;
6970         }
6971     }
6972 
6973     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6974         return CP_ACCESS_TRAP_EL2;
6975     }
6976 
6977     return CP_ACCESS_OK;
6978 }
6979 
6980 /*
6981  * Check for traps to RAS registers, which are controlled
6982  * by HCR_EL2.TERR and SCR_EL3.TERR.
6983  */
6984 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6985                                   bool isread)
6986 {
6987     int el = arm_current_el(env);
6988 
6989     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6990         return CP_ACCESS_TRAP_EL2;
6991     }
6992     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6993         return CP_ACCESS_TRAP_EL3;
6994     }
6995     return CP_ACCESS_OK;
6996 }
6997 
6998 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6999 {
7000     int el = arm_current_el(env);
7001 
7002     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7003         return env->cp15.vdisr_el2;
7004     }
7005     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7006         return 0; /* RAZ/WI */
7007     }
7008     return env->cp15.disr_el1;
7009 }
7010 
7011 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7012 {
7013     int el = arm_current_el(env);
7014 
7015     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7016         env->cp15.vdisr_el2 = val;
7017         return;
7018     }
7019     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7020         return; /* RAZ/WI */
7021     }
7022     env->cp15.disr_el1 = val;
7023 }
7024 
7025 /*
7026  * Minimal RAS implementation with no Error Records.
7027  * Which means that all of the Error Record registers:
7028  *   ERXADDR_EL1
7029  *   ERXCTLR_EL1
7030  *   ERXFR_EL1
7031  *   ERXMISC0_EL1
7032  *   ERXMISC1_EL1
7033  *   ERXMISC2_EL1
7034  *   ERXMISC3_EL1
7035  *   ERXPFGCDN_EL1  (RASv1p1)
7036  *   ERXPFGCTL_EL1  (RASv1p1)
7037  *   ERXPFGF_EL1    (RASv1p1)
7038  *   ERXSTATUS_EL1
7039  * and
7040  *   ERRSELR_EL1
7041  * may generate UNDEFINED, which is the effect we get by not
7042  * listing them at all.
7043  *
7044  * These registers have fine-grained trap bits, but UNDEF-to-EL1
7045  * is higher priority than FGT-to-EL2 so we do not need to list them
7046  * in order to check for an FGT.
7047  */
7048 static const ARMCPRegInfo minimal_ras_reginfo[] = {
7049     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
7050       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
7051       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
7052       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
7053     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
7054       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
7055       .access = PL1_R, .accessfn = access_terr,
7056       .fgt = FGT_ERRIDR_EL1,
7057       .type = ARM_CP_CONST, .resetvalue = 0 },
7058     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
7059       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
7060       .nv2_redirect_offset = 0x500,
7061       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
7062     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
7063       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
7064       .nv2_redirect_offset = 0x508,
7065       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
7066 };
7067 
7068 /*
7069  * Return the exception level to which exceptions should be taken
7070  * via SVEAccessTrap.  This excludes the check for whether the exception
7071  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
7072  * be found by testing 0 < fp_exception_el < sve_exception_el.
7073  *
7074  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
7075  * pseudocode does *not* separate out the FP trap checks, but has them
7076  * all in one function.
7077  */
7078 int sve_exception_el(CPUARMState *env, int el)
7079 {
7080 #ifndef CONFIG_USER_ONLY
7081     if (el <= 1 && !el_is_in_host(env, el)) {
7082         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7083         case 1:
7084             if (el != 0) {
7085                 break;
7086             }
7087             /* fall through */
7088         case 0:
7089         case 2:
7090             return 1;
7091         }
7092     }
7093 
7094     if (el <= 2 && arm_is_el2_enabled(env)) {
7095         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7096         if (env->cp15.hcr_el2 & HCR_E2H) {
7097             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
7098             case 1:
7099                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7100                     break;
7101                 }
7102                 /* fall through */
7103             case 0:
7104             case 2:
7105                 return 2;
7106             }
7107         } else {
7108             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
7109                 return 2;
7110             }
7111         }
7112     }
7113 
7114     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
7115     if (arm_feature(env, ARM_FEATURE_EL3)
7116         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
7117         return 3;
7118     }
7119 #endif
7120     return 0;
7121 }
7122 
7123 /*
7124  * Return the exception level to which exceptions should be taken for SME.
7125  * C.f. the ARM pseudocode function CheckSMEAccess.
7126  */
7127 int sme_exception_el(CPUARMState *env, int el)
7128 {
7129 #ifndef CONFIG_USER_ONLY
7130     if (el <= 1 && !el_is_in_host(env, el)) {
7131         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
7132         case 1:
7133             if (el != 0) {
7134                 break;
7135             }
7136             /* fall through */
7137         case 0:
7138         case 2:
7139             return 1;
7140         }
7141     }
7142 
7143     if (el <= 2 && arm_is_el2_enabled(env)) {
7144         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7145         if (env->cp15.hcr_el2 & HCR_E2H) {
7146             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
7147             case 1:
7148                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7149                     break;
7150                 }
7151                 /* fall through */
7152             case 0:
7153             case 2:
7154                 return 2;
7155             }
7156         } else {
7157             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7158                 return 2;
7159             }
7160         }
7161     }
7162 
7163     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
7164     if (arm_feature(env, ARM_FEATURE_EL3)
7165         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7166         return 3;
7167     }
7168 #endif
7169     return 0;
7170 }
7171 
7172 /*
7173  * Given that SVE is enabled, return the vector length for EL.
7174  */
7175 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7176 {
7177     ARMCPU *cpu = env_archcpu(env);
7178     uint64_t *cr = env->vfp.zcr_el;
7179     uint32_t map = cpu->sve_vq.map;
7180     uint32_t len = ARM_MAX_VQ - 1;
7181 
7182     if (sm) {
7183         cr = env->vfp.smcr_el;
7184         map = cpu->sme_vq.map;
7185     }
7186 
7187     if (el <= 1 && !el_is_in_host(env, el)) {
7188         len = MIN(len, 0xf & (uint32_t)cr[1]);
7189     }
7190     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
7191         len = MIN(len, 0xf & (uint32_t)cr[2]);
7192     }
7193     if (arm_feature(env, ARM_FEATURE_EL3)) {
7194         len = MIN(len, 0xf & (uint32_t)cr[3]);
7195     }
7196 
7197     map &= MAKE_64BIT_MASK(0, len + 1);
7198     if (map != 0) {
7199         return 31 - clz32(map);
7200     }
7201 
7202     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7203     assert(sm);
7204     return ctz32(cpu->sme_vq.map);
7205 }
7206 
7207 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7208 {
7209     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7210 }
7211 
7212 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7213                       uint64_t value)
7214 {
7215     int cur_el = arm_current_el(env);
7216     int old_len = sve_vqm1_for_el(env, cur_el);
7217     int new_len;
7218 
7219     /* Bits other than [3:0] are RAZ/WI.  */
7220     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7221     raw_write(env, ri, value & 0xf);
7222 
7223     /*
7224      * Because we arrived here, we know both FP and SVE are enabled;
7225      * otherwise we would have trapped access to the ZCR_ELn register.
7226      */
7227     new_len = sve_vqm1_for_el(env, cur_el);
7228     if (new_len < old_len) {
7229         aarch64_sve_narrow_vq(env, new_len + 1);
7230     }
7231 }
7232 
7233 static const ARMCPRegInfo zcr_reginfo[] = {
7234     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7235       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7236       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7237       .access = PL1_RW, .type = ARM_CP_SVE,
7238       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7239       .writefn = zcr_write, .raw_writefn = raw_write },
7240     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7241       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7242       .access = PL2_RW, .type = ARM_CP_SVE,
7243       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7244       .writefn = zcr_write, .raw_writefn = raw_write },
7245     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7246       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7247       .access = PL3_RW, .type = ARM_CP_SVE,
7248       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7249       .writefn = zcr_write, .raw_writefn = raw_write },
7250 };
7251 
7252 #ifdef TARGET_AARCH64
7253 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7254                                     bool isread)
7255 {
7256     int el = arm_current_el(env);
7257 
7258     if (el == 0) {
7259         uint64_t sctlr = arm_sctlr(env, el);
7260         if (!(sctlr & SCTLR_EnTP2)) {
7261             return CP_ACCESS_TRAP;
7262         }
7263     }
7264     /* TODO: FEAT_FGT */
7265     if (el < 3
7266         && arm_feature(env, ARM_FEATURE_EL3)
7267         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7268         return CP_ACCESS_TRAP_EL3;
7269     }
7270     return CP_ACCESS_OK;
7271 }
7272 
7273 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7274                                       bool isread)
7275 {
7276     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7277     if (arm_current_el(env) == 2
7278         && arm_feature(env, ARM_FEATURE_EL3)
7279         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7280         return CP_ACCESS_TRAP_EL3;
7281     }
7282     return CP_ACCESS_OK;
7283 }
7284 
7285 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7286                                    bool isread)
7287 {
7288     if (arm_current_el(env) < 3
7289         && arm_feature(env, ARM_FEATURE_EL3)
7290         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7291         return CP_ACCESS_TRAP_EL3;
7292     }
7293     return CP_ACCESS_OK;
7294 }
7295 
7296 /* ResetSVEState */
7297 static void arm_reset_sve_state(CPUARMState *env)
7298 {
7299     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7300     /* Recall that FFR is stored as pregs[16]. */
7301     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7302     vfp_set_fpcr(env, 0x0800009f);
7303 }
7304 
7305 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7306 {
7307     uint64_t change = (env->svcr ^ new) & mask;
7308 
7309     if (change == 0) {
7310         return;
7311     }
7312     env->svcr ^= change;
7313 
7314     if (change & R_SVCR_SM_MASK) {
7315         arm_reset_sve_state(env);
7316     }
7317 
7318     /*
7319      * ResetSMEState.
7320      *
7321      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
7322      * on enable: while disabled, the storage is inaccessible and the
7323      * value does not matter.  We're not saving the storage in vmstate
7324      * when disabled either.
7325      */
7326     if (change & new & R_SVCR_ZA_MASK) {
7327         memset(env->zarray, 0, sizeof(env->zarray));
7328     }
7329 
7330     if (tcg_enabled()) {
7331         arm_rebuild_hflags(env);
7332     }
7333 }
7334 
7335 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7336                        uint64_t value)
7337 {
7338     aarch64_set_svcr(env, value, -1);
7339 }
7340 
7341 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7342                        uint64_t value)
7343 {
7344     int cur_el = arm_current_el(env);
7345     int old_len = sve_vqm1_for_el(env, cur_el);
7346     int new_len;
7347 
7348     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7349     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7350     raw_write(env, ri, value);
7351 
7352     /*
7353      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7354      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7355      * current values for simplicity.  But for QEMU internals, we must still
7356      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7357      * above aarch64_sve_narrow_vq.
7358      */
7359     new_len = sve_vqm1_for_el(env, cur_el);
7360     if (new_len < old_len) {
7361         aarch64_sve_narrow_vq(env, new_len + 1);
7362     }
7363 }
7364 
7365 static const ARMCPRegInfo sme_reginfo[] = {
7366     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7367       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7368       .access = PL0_RW, .accessfn = access_tpidr2,
7369       .fgt = FGT_NTPIDR2_EL0,
7370       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7371     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7372       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7373       .access = PL0_RW, .type = ARM_CP_SME,
7374       .fieldoffset = offsetof(CPUARMState, svcr),
7375       .writefn = svcr_write, .raw_writefn = raw_write },
7376     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7377       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7378       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7379       .access = PL1_RW, .type = ARM_CP_SME,
7380       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7381       .writefn = smcr_write, .raw_writefn = raw_write },
7382     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7383       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7384       .access = PL2_RW, .type = ARM_CP_SME,
7385       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7386       .writefn = smcr_write, .raw_writefn = raw_write },
7387     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7388       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7389       .access = PL3_RW, .type = ARM_CP_SME,
7390       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7391       .writefn = smcr_write, .raw_writefn = raw_write },
7392     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7393       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7394       .access = PL1_R, .accessfn = access_aa64_tid1,
7395       /*
7396        * IMPLEMENTOR = 0 (software)
7397        * REVISION    = 0 (implementation defined)
7398        * SMPS        = 0 (no streaming execution priority in QEMU)
7399        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7400        */
7401       .type = ARM_CP_CONST, .resetvalue = 0, },
7402     /*
7403      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7404      */
7405     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7406       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7407       .access = PL1_RW, .accessfn = access_smpri,
7408       .fgt = FGT_NSMPRI_EL1,
7409       .type = ARM_CP_CONST, .resetvalue = 0 },
7410     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7411       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7412       .nv2_redirect_offset = 0x1f8,
7413       .access = PL2_RW, .accessfn = access_smprimap,
7414       .type = ARM_CP_CONST, .resetvalue = 0 },
7415 };
7416 
7417 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7418                                   uint64_t value)
7419 {
7420     CPUState *cs = env_cpu(env);
7421 
7422     tlb_flush(cs);
7423 }
7424 
7425 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7426                         uint64_t value)
7427 {
7428     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7429     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7430         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7431         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7432 
7433     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7434 }
7435 
7436 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7437 {
7438     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7439                                      env_archcpu(env)->reset_l0gptsz);
7440 }
7441 
7442 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7443                                     uint64_t value)
7444 {
7445     CPUState *cs = env_cpu(env);
7446 
7447     tlb_flush_all_cpus_synced(cs);
7448 }
7449 
7450 static const ARMCPRegInfo rme_reginfo[] = {
7451     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7452       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7453       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7454       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7455     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7456       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7457       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7458     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7459       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7460       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7461     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7462       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7463       .access = PL3_W, .type = ARM_CP_NO_RAW,
7464       .writefn = tlbi_aa64_paall_write },
7465     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7466       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7467       .access = PL3_W, .type = ARM_CP_NO_RAW,
7468       .writefn = tlbi_aa64_paallos_write },
7469     /*
7470      * QEMU does not have a way to invalidate by physical address, thus
7471      * invalidating a range of physical addresses is accomplished by
7472      * flushing all tlb entries in the outer shareable domain,
7473      * just like PAALLOS.
7474      */
7475     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7476       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7477       .access = PL3_W, .type = ARM_CP_NO_RAW,
7478       .writefn = tlbi_aa64_paallos_write },
7479     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7480       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7481       .access = PL3_W, .type = ARM_CP_NO_RAW,
7482       .writefn = tlbi_aa64_paallos_write },
7483     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7484       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7485       .access = PL3_W, .type = ARM_CP_NOP },
7486 };
7487 
7488 static const ARMCPRegInfo rme_mte_reginfo[] = {
7489     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7490       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7491       .access = PL3_W, .type = ARM_CP_NOP },
7492 };
7493 #endif /* TARGET_AARCH64 */
7494 
7495 static void define_pmu_regs(ARMCPU *cpu)
7496 {
7497     /*
7498      * v7 performance monitor control register: same implementor
7499      * field as main ID register, and we implement four counters in
7500      * addition to the cycle count register.
7501      */
7502     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7503     ARMCPRegInfo pmcr = {
7504         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7505         .access = PL0_RW,
7506         .fgt = FGT_PMCR_EL0,
7507         .type = ARM_CP_IO | ARM_CP_ALIAS,
7508         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7509         .accessfn = pmreg_access,
7510         .readfn = pmcr_read, .raw_readfn = raw_read,
7511         .writefn = pmcr_write, .raw_writefn = raw_write,
7512     };
7513     ARMCPRegInfo pmcr64 = {
7514         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7515         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7516         .access = PL0_RW, .accessfn = pmreg_access,
7517         .fgt = FGT_PMCR_EL0,
7518         .type = ARM_CP_IO,
7519         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7520         .resetvalue = cpu->isar.reset_pmcr_el0,
7521         .readfn = pmcr_read, .raw_readfn = raw_read,
7522         .writefn = pmcr_write, .raw_writefn = raw_write,
7523     };
7524 
7525     define_one_arm_cp_reg(cpu, &pmcr);
7526     define_one_arm_cp_reg(cpu, &pmcr64);
7527     for (i = 0; i < pmcrn; i++) {
7528         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7529         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7530         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7531         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7532         ARMCPRegInfo pmev_regs[] = {
7533             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7534               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7535               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7536               .fgt = FGT_PMEVCNTRN_EL0,
7537               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7538               .accessfn = pmreg_access_xevcntr },
7539             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7540               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7541               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7542               .type = ARM_CP_IO,
7543               .fgt = FGT_PMEVCNTRN_EL0,
7544               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7545               .raw_readfn = pmevcntr_rawread,
7546               .raw_writefn = pmevcntr_rawwrite },
7547             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7548               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7549               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7550               .fgt = FGT_PMEVTYPERN_EL0,
7551               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7552               .accessfn = pmreg_access },
7553             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7554               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7555               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7556               .fgt = FGT_PMEVTYPERN_EL0,
7557               .type = ARM_CP_IO,
7558               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7559               .raw_writefn = pmevtyper_rawwrite },
7560         };
7561         define_arm_cp_regs(cpu, pmev_regs);
7562         g_free(pmevcntr_name);
7563         g_free(pmevcntr_el0_name);
7564         g_free(pmevtyper_name);
7565         g_free(pmevtyper_el0_name);
7566     }
7567     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7568         ARMCPRegInfo v81_pmu_regs[] = {
7569             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7570               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7571               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7572               .fgt = FGT_PMCEIDN_EL0,
7573               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7574             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7575               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7576               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7577               .fgt = FGT_PMCEIDN_EL0,
7578               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7579         };
7580         define_arm_cp_regs(cpu, v81_pmu_regs);
7581     }
7582     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7583         static const ARMCPRegInfo v84_pmmir = {
7584             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7585             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7586             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7587             .fgt = FGT_PMMIR_EL1,
7588             .resetvalue = 0
7589         };
7590         define_one_arm_cp_reg(cpu, &v84_pmmir);
7591     }
7592 }
7593 
7594 #ifndef CONFIG_USER_ONLY
7595 /*
7596  * We don't know until after realize whether there's a GICv3
7597  * attached, and that is what registers the gicv3 sysregs.
7598  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7599  * at runtime.
7600  */
7601 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7602 {
7603     ARMCPU *cpu = env_archcpu(env);
7604     uint64_t pfr1 = cpu->isar.id_pfr1;
7605 
7606     if (env->gicv3state) {
7607         pfr1 |= 1 << 28;
7608     }
7609     return pfr1;
7610 }
7611 
7612 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7613 {
7614     ARMCPU *cpu = env_archcpu(env);
7615     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7616 
7617     if (env->gicv3state) {
7618         pfr0 |= 1 << 24;
7619     }
7620     return pfr0;
7621 }
7622 #endif
7623 
7624 /*
7625  * Shared logic between LORID and the rest of the LOR* registers.
7626  * Secure state exclusion has already been dealt with.
7627  */
7628 static CPAccessResult access_lor_ns(CPUARMState *env,
7629                                     const ARMCPRegInfo *ri, bool isread)
7630 {
7631     int el = arm_current_el(env);
7632 
7633     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7634         return CP_ACCESS_TRAP_EL2;
7635     }
7636     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7637         return CP_ACCESS_TRAP_EL3;
7638     }
7639     return CP_ACCESS_OK;
7640 }
7641 
7642 static CPAccessResult access_lor_other(CPUARMState *env,
7643                                        const ARMCPRegInfo *ri, bool isread)
7644 {
7645     if (arm_is_secure_below_el3(env)) {
7646         /* Access denied in secure mode.  */
7647         return CP_ACCESS_TRAP;
7648     }
7649     return access_lor_ns(env, ri, isread);
7650 }
7651 
7652 /*
7653  * A trivial implementation of ARMv8.1-LOR leaves all of these
7654  * registers fixed at 0, which indicates that there are zero
7655  * supported Limited Ordering regions.
7656  */
7657 static const ARMCPRegInfo lor_reginfo[] = {
7658     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7659       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7660       .access = PL1_RW, .accessfn = access_lor_other,
7661       .fgt = FGT_LORSA_EL1,
7662       .type = ARM_CP_CONST, .resetvalue = 0 },
7663     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7664       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7665       .access = PL1_RW, .accessfn = access_lor_other,
7666       .fgt = FGT_LOREA_EL1,
7667       .type = ARM_CP_CONST, .resetvalue = 0 },
7668     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7669       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7670       .access = PL1_RW, .accessfn = access_lor_other,
7671       .fgt = FGT_LORN_EL1,
7672       .type = ARM_CP_CONST, .resetvalue = 0 },
7673     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7674       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7675       .access = PL1_RW, .accessfn = access_lor_other,
7676       .fgt = FGT_LORC_EL1,
7677       .type = ARM_CP_CONST, .resetvalue = 0 },
7678     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7679       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7680       .access = PL1_R, .accessfn = access_lor_ns,
7681       .fgt = FGT_LORID_EL1,
7682       .type = ARM_CP_CONST, .resetvalue = 0 },
7683 };
7684 
7685 #ifdef TARGET_AARCH64
7686 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7687                                    bool isread)
7688 {
7689     int el = arm_current_el(env);
7690 
7691     if (el < 2 &&
7692         arm_is_el2_enabled(env) &&
7693         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7694         return CP_ACCESS_TRAP_EL2;
7695     }
7696     if (el < 3 &&
7697         arm_feature(env, ARM_FEATURE_EL3) &&
7698         !(env->cp15.scr_el3 & SCR_APK)) {
7699         return CP_ACCESS_TRAP_EL3;
7700     }
7701     return CP_ACCESS_OK;
7702 }
7703 
7704 static const ARMCPRegInfo pauth_reginfo[] = {
7705     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7706       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7707       .access = PL1_RW, .accessfn = access_pauth,
7708       .fgt = FGT_APDAKEY,
7709       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7710     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7712       .access = PL1_RW, .accessfn = access_pauth,
7713       .fgt = FGT_APDAKEY,
7714       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7715     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7716       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7717       .access = PL1_RW, .accessfn = access_pauth,
7718       .fgt = FGT_APDBKEY,
7719       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7720     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7721       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7722       .access = PL1_RW, .accessfn = access_pauth,
7723       .fgt = FGT_APDBKEY,
7724       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7725     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7726       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7727       .access = PL1_RW, .accessfn = access_pauth,
7728       .fgt = FGT_APGAKEY,
7729       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7730     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7731       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7732       .access = PL1_RW, .accessfn = access_pauth,
7733       .fgt = FGT_APGAKEY,
7734       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7735     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7736       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7737       .access = PL1_RW, .accessfn = access_pauth,
7738       .fgt = FGT_APIAKEY,
7739       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7740     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7741       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7742       .access = PL1_RW, .accessfn = access_pauth,
7743       .fgt = FGT_APIAKEY,
7744       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7745     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7746       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7747       .access = PL1_RW, .accessfn = access_pauth,
7748       .fgt = FGT_APIBKEY,
7749       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7750     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7751       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7752       .access = PL1_RW, .accessfn = access_pauth,
7753       .fgt = FGT_APIBKEY,
7754       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7755 };
7756 
7757 static const ARMCPRegInfo tlbirange_reginfo[] = {
7758     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7759       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7760       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7761       .fgt = FGT_TLBIRVAE1IS,
7762       .writefn = tlbi_aa64_rvae1is_write },
7763     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7764       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7765       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7766       .fgt = FGT_TLBIRVAAE1IS,
7767       .writefn = tlbi_aa64_rvae1is_write },
7768    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7769       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7770       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7771       .fgt = FGT_TLBIRVALE1IS,
7772       .writefn = tlbi_aa64_rvae1is_write },
7773     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7774       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7775       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7776       .fgt = FGT_TLBIRVAALE1IS,
7777       .writefn = tlbi_aa64_rvae1is_write },
7778     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7779       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7780       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7781       .fgt = FGT_TLBIRVAE1OS,
7782       .writefn = tlbi_aa64_rvae1is_write },
7783     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7784       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7785       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7786       .fgt = FGT_TLBIRVAAE1OS,
7787       .writefn = tlbi_aa64_rvae1is_write },
7788    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7789       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7790       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7791       .fgt = FGT_TLBIRVALE1OS,
7792       .writefn = tlbi_aa64_rvae1is_write },
7793     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7794       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7795       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7796       .fgt = FGT_TLBIRVAALE1OS,
7797       .writefn = tlbi_aa64_rvae1is_write },
7798     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7799       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7800       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7801       .fgt = FGT_TLBIRVAE1,
7802       .writefn = tlbi_aa64_rvae1_write },
7803     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7804       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7805       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7806       .fgt = FGT_TLBIRVAAE1,
7807       .writefn = tlbi_aa64_rvae1_write },
7808    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7809       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7810       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7811       .fgt = FGT_TLBIRVALE1,
7812       .writefn = tlbi_aa64_rvae1_write },
7813     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7814       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7815       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7816       .fgt = FGT_TLBIRVAALE1,
7817       .writefn = tlbi_aa64_rvae1_write },
7818     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7819       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7820       .access = PL2_W, .type = ARM_CP_NO_RAW,
7821       .writefn = tlbi_aa64_ripas2e1is_write },
7822     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7823       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7824       .access = PL2_W, .type = ARM_CP_NO_RAW,
7825       .writefn = tlbi_aa64_ripas2e1is_write },
7826     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7827       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7828       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7829       .writefn = tlbi_aa64_rvae2is_write },
7830    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7831       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7832       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7833       .writefn = tlbi_aa64_rvae2is_write },
7834     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7835       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7836       .access = PL2_W, .type = ARM_CP_NO_RAW,
7837       .writefn = tlbi_aa64_ripas2e1_write },
7838     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7839       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7840       .access = PL2_W, .type = ARM_CP_NO_RAW,
7841       .writefn = tlbi_aa64_ripas2e1_write },
7842    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7843       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7844       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7845       .writefn = tlbi_aa64_rvae2is_write },
7846    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7847       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7848       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7849       .writefn = tlbi_aa64_rvae2is_write },
7850     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7851       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7852       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7853       .writefn = tlbi_aa64_rvae2_write },
7854    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7855       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7856       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7857       .writefn = tlbi_aa64_rvae2_write },
7858    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7859       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7860       .access = PL3_W, .type = ARM_CP_NO_RAW,
7861       .writefn = tlbi_aa64_rvae3is_write },
7862    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7863       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7864       .access = PL3_W, .type = ARM_CP_NO_RAW,
7865       .writefn = tlbi_aa64_rvae3is_write },
7866    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7867       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7868       .access = PL3_W, .type = ARM_CP_NO_RAW,
7869       .writefn = tlbi_aa64_rvae3is_write },
7870    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7871       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7872       .access = PL3_W, .type = ARM_CP_NO_RAW,
7873       .writefn = tlbi_aa64_rvae3is_write },
7874    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7875       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7876       .access = PL3_W, .type = ARM_CP_NO_RAW,
7877       .writefn = tlbi_aa64_rvae3_write },
7878    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7879       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7880       .access = PL3_W, .type = ARM_CP_NO_RAW,
7881       .writefn = tlbi_aa64_rvae3_write },
7882 };
7883 
7884 static const ARMCPRegInfo tlbios_reginfo[] = {
7885     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7886       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7887       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7888       .fgt = FGT_TLBIVMALLE1OS,
7889       .writefn = tlbi_aa64_vmalle1is_write },
7890     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7891       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7892       .fgt = FGT_TLBIVAE1OS,
7893       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7894       .writefn = tlbi_aa64_vae1is_write },
7895     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7896       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7897       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7898       .fgt = FGT_TLBIASIDE1OS,
7899       .writefn = tlbi_aa64_vmalle1is_write },
7900     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7901       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7902       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7903       .fgt = FGT_TLBIVAAE1OS,
7904       .writefn = tlbi_aa64_vae1is_write },
7905     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7906       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7907       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7908       .fgt = FGT_TLBIVALE1OS,
7909       .writefn = tlbi_aa64_vae1is_write },
7910     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7911       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7912       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7913       .fgt = FGT_TLBIVAALE1OS,
7914       .writefn = tlbi_aa64_vae1is_write },
7915     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7916       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7917       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7918       .writefn = tlbi_aa64_alle2is_write },
7919     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7920       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7921       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7922       .writefn = tlbi_aa64_vae2is_write },
7923    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7924       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7925       .access = PL2_W, .type = ARM_CP_NO_RAW,
7926       .writefn = tlbi_aa64_alle1is_write },
7927     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7928       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7929       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7930       .writefn = tlbi_aa64_vae2is_write },
7931     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7932       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7933       .access = PL2_W, .type = ARM_CP_NO_RAW,
7934       .writefn = tlbi_aa64_alle1is_write },
7935     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7936       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7937       .access = PL2_W, .type = ARM_CP_NOP },
7938     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7939       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7940       .access = PL2_W, .type = ARM_CP_NOP },
7941     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7942       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7943       .access = PL2_W, .type = ARM_CP_NOP },
7944     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7945       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7946       .access = PL2_W, .type = ARM_CP_NOP },
7947     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7948       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7949       .access = PL3_W, .type = ARM_CP_NO_RAW,
7950       .writefn = tlbi_aa64_alle3is_write },
7951     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7952       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7953       .access = PL3_W, .type = ARM_CP_NO_RAW,
7954       .writefn = tlbi_aa64_vae3is_write },
7955     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7956       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7957       .access = PL3_W, .type = ARM_CP_NO_RAW,
7958       .writefn = tlbi_aa64_vae3is_write },
7959 };
7960 
7961 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7962 {
7963     Error *err = NULL;
7964     uint64_t ret;
7965 
7966     /* Success sets NZCV = 0000.  */
7967     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7968 
7969     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7970         /*
7971          * ??? Failed, for unknown reasons in the crypto subsystem.
7972          * The best we can do is log the reason and return the
7973          * timed-out indication to the guest.  There is no reason
7974          * we know to expect this failure to be transitory, so the
7975          * guest may well hang retrying the operation.
7976          */
7977         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7978                       ri->name, error_get_pretty(err));
7979         error_free(err);
7980 
7981         env->ZF = 0; /* NZCF = 0100 */
7982         return 0;
7983     }
7984     return ret;
7985 }
7986 
7987 /* We do not support re-seeding, so the two registers operate the same.  */
7988 static const ARMCPRegInfo rndr_reginfo[] = {
7989     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7990       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7991       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7992       .access = PL0_R, .readfn = rndr_readfn },
7993     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7994       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7995       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7996       .access = PL0_R, .readfn = rndr_readfn },
7997 };
7998 
7999 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
8000                           uint64_t value)
8001 {
8002 #ifdef CONFIG_TCG
8003     ARMCPU *cpu = env_archcpu(env);
8004     /* CTR_EL0 System register -> DminLine, bits [19:16] */
8005     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
8006     uint64_t vaddr_in = (uint64_t) value;
8007     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
8008     void *haddr;
8009     int mem_idx = arm_env_mmu_index(env);
8010 
8011     /* This won't be crossing page boundaries */
8012     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
8013     if (haddr) {
8014 #ifndef CONFIG_USER_ONLY
8015 
8016         ram_addr_t offset;
8017         MemoryRegion *mr;
8018 
8019         /* RCU lock is already being held */
8020         mr = memory_region_from_host(haddr, &offset);
8021 
8022         if (mr) {
8023             memory_region_writeback(mr, offset, dline_size);
8024         }
8025 #endif /*CONFIG_USER_ONLY*/
8026     }
8027 #else
8028     /* Handled by hardware accelerator. */
8029     g_assert_not_reached();
8030 #endif /* CONFIG_TCG */
8031 }
8032 
8033 static const ARMCPRegInfo dcpop_reg[] = {
8034     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
8035       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
8036       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8037       .fgt = FGT_DCCVAP,
8038       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8039 };
8040 
8041 static const ARMCPRegInfo dcpodp_reg[] = {
8042     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
8043       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
8044       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8045       .fgt = FGT_DCCVADP,
8046       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8047 };
8048 
8049 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
8050                                        bool isread)
8051 {
8052     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
8053         return CP_ACCESS_TRAP_EL2;
8054     }
8055 
8056     return CP_ACCESS_OK;
8057 }
8058 
8059 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
8060                                  bool isread)
8061 {
8062     int el = arm_current_el(env);
8063     if (el < 2 && arm_is_el2_enabled(env)) {
8064         uint64_t hcr = arm_hcr_el2_eff(env);
8065         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8066             return CP_ACCESS_TRAP_EL2;
8067         }
8068     }
8069     if (el < 3 &&
8070         arm_feature(env, ARM_FEATURE_EL3) &&
8071         !(env->cp15.scr_el3 & SCR_ATA)) {
8072         return CP_ACCESS_TRAP_EL3;
8073     }
8074     return CP_ACCESS_OK;
8075 }
8076 
8077 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
8078                                       bool isread)
8079 {
8080     CPAccessResult nv1 = access_nv1(env, ri, isread);
8081 
8082     if (nv1 != CP_ACCESS_OK) {
8083         return nv1;
8084     }
8085     return access_mte(env, ri, isread);
8086 }
8087 
8088 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
8089                                       bool isread)
8090 {
8091     /*
8092      * TFSR_EL2: similar to generic access_mte(), but we need to
8093      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
8094      * if NV2 is enabled then we will redirect this to TFSR_EL1
8095      * after doing the HCR and SCR ATA traps; otherwise this will
8096      * be a trap to EL2 and the HCR/SCR traps do not apply.
8097      */
8098     int el = arm_current_el(env);
8099 
8100     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
8101         return CP_ACCESS_OK;
8102     }
8103     if (el < 2 && arm_is_el2_enabled(env)) {
8104         uint64_t hcr = arm_hcr_el2_eff(env);
8105         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8106             return CP_ACCESS_TRAP_EL2;
8107         }
8108     }
8109     if (el < 3 &&
8110         arm_feature(env, ARM_FEATURE_EL3) &&
8111         !(env->cp15.scr_el3 & SCR_ATA)) {
8112         return CP_ACCESS_TRAP_EL3;
8113     }
8114     return CP_ACCESS_OK;
8115 }
8116 
8117 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
8118 {
8119     return env->pstate & PSTATE_TCO;
8120 }
8121 
8122 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
8123 {
8124     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
8125 }
8126 
8127 static const ARMCPRegInfo mte_reginfo[] = {
8128     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
8129       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
8130       .access = PL1_RW, .accessfn = access_mte,
8131       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
8132     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
8133       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
8134       .access = PL1_RW, .accessfn = access_tfsr_el1,
8135       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
8136       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
8137     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
8138       .type = ARM_CP_NV2_REDIRECT,
8139       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
8140       .access = PL2_RW, .accessfn = access_tfsr_el2,
8141       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
8142     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
8143       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
8144       .access = PL3_RW,
8145       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
8146     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
8147       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
8148       .access = PL1_RW, .accessfn = access_mte,
8149       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
8150     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
8151       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
8152       .access = PL1_RW, .accessfn = access_mte,
8153       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
8154     { .name = "TCO", .state = ARM_CP_STATE_AA64,
8155       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8156       .type = ARM_CP_NO_RAW,
8157       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8158     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8159       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8160       .type = ARM_CP_NOP, .access = PL1_W,
8161       .fgt = FGT_DCIVAC,
8162       .accessfn = aa64_cacheop_poc_access },
8163     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8164       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8165       .fgt = FGT_DCISW,
8166       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8167     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8168       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8169       .type = ARM_CP_NOP, .access = PL1_W,
8170       .fgt = FGT_DCIVAC,
8171       .accessfn = aa64_cacheop_poc_access },
8172     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8173       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8174       .fgt = FGT_DCISW,
8175       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8176     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8177       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8178       .fgt = FGT_DCCSW,
8179       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8180     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8181       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8182       .fgt = FGT_DCCSW,
8183       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8184     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8185       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8186       .fgt = FGT_DCCISW,
8187       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8188     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8189       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8190       .fgt = FGT_DCCISW,
8191       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8192 };
8193 
8194 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8195     { .name = "TCO", .state = ARM_CP_STATE_AA64,
8196       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8197       .type = ARM_CP_CONST, .access = PL0_RW, },
8198 };
8199 
8200 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8201     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8202       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8203       .type = ARM_CP_NOP, .access = PL0_W,
8204       .fgt = FGT_DCCVAC,
8205       .accessfn = aa64_cacheop_poc_access },
8206     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8207       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8208       .type = ARM_CP_NOP, .access = PL0_W,
8209       .fgt = FGT_DCCVAC,
8210       .accessfn = aa64_cacheop_poc_access },
8211     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8212       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8213       .type = ARM_CP_NOP, .access = PL0_W,
8214       .fgt = FGT_DCCVAP,
8215       .accessfn = aa64_cacheop_poc_access },
8216     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8217       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8218       .type = ARM_CP_NOP, .access = PL0_W,
8219       .fgt = FGT_DCCVAP,
8220       .accessfn = aa64_cacheop_poc_access },
8221     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8222       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8223       .type = ARM_CP_NOP, .access = PL0_W,
8224       .fgt = FGT_DCCVADP,
8225       .accessfn = aa64_cacheop_poc_access },
8226     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8227       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8228       .type = ARM_CP_NOP, .access = PL0_W,
8229       .fgt = FGT_DCCVADP,
8230       .accessfn = aa64_cacheop_poc_access },
8231     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8232       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8233       .type = ARM_CP_NOP, .access = PL0_W,
8234       .fgt = FGT_DCCIVAC,
8235       .accessfn = aa64_cacheop_poc_access },
8236     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8237       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8238       .type = ARM_CP_NOP, .access = PL0_W,
8239       .fgt = FGT_DCCIVAC,
8240       .accessfn = aa64_cacheop_poc_access },
8241     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8242       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8243       .access = PL0_W, .type = ARM_CP_DC_GVA,
8244 #ifndef CONFIG_USER_ONLY
8245       /* Avoid overhead of an access check that always passes in user-mode */
8246       .accessfn = aa64_zva_access,
8247       .fgt = FGT_DCZVA,
8248 #endif
8249     },
8250     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8251       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8252       .access = PL0_W, .type = ARM_CP_DC_GZVA,
8253 #ifndef CONFIG_USER_ONLY
8254       /* Avoid overhead of an access check that always passes in user-mode */
8255       .accessfn = aa64_zva_access,
8256       .fgt = FGT_DCZVA,
8257 #endif
8258     },
8259 };
8260 
8261 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8262                                      bool isread)
8263 {
8264     uint64_t hcr = arm_hcr_el2_eff(env);
8265     int el = arm_current_el(env);
8266 
8267     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8268         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8269             if (hcr & HCR_TGE) {
8270                 return CP_ACCESS_TRAP_EL2;
8271             }
8272             return CP_ACCESS_TRAP;
8273         }
8274     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8275         return CP_ACCESS_TRAP_EL2;
8276     }
8277     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8278         return CP_ACCESS_TRAP_EL2;
8279     }
8280     if (el < 3
8281         && arm_feature(env, ARM_FEATURE_EL3)
8282         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8283         return CP_ACCESS_TRAP_EL3;
8284     }
8285     return CP_ACCESS_OK;
8286 }
8287 
8288 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8289                                          const ARMCPRegInfo *ri,
8290                                          bool isread)
8291 {
8292     CPAccessResult nv1 = access_nv1(env, ri, isread);
8293 
8294     if (nv1 != CP_ACCESS_OK) {
8295         return nv1;
8296     }
8297     return access_scxtnum(env, ri, isread);
8298 }
8299 
8300 static const ARMCPRegInfo scxtnum_reginfo[] = {
8301     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8302       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8303       .access = PL0_RW, .accessfn = access_scxtnum,
8304       .fgt = FGT_SCXTNUM_EL0,
8305       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8306     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8307       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8308       .access = PL1_RW, .accessfn = access_scxtnum_el1,
8309       .fgt = FGT_SCXTNUM_EL1,
8310       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8311       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8312     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8313       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8314       .access = PL2_RW, .accessfn = access_scxtnum,
8315       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8316     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8317       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8318       .access = PL3_RW,
8319       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8320 };
8321 
8322 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8323                                  bool isread)
8324 {
8325     if (arm_current_el(env) == 2 &&
8326         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8327         return CP_ACCESS_TRAP_EL3;
8328     }
8329     return CP_ACCESS_OK;
8330 }
8331 
8332 static const ARMCPRegInfo fgt_reginfo[] = {
8333     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8334       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8335       .nv2_redirect_offset = 0x1b8,
8336       .access = PL2_RW, .accessfn = access_fgt,
8337       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8338     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8339       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8340       .nv2_redirect_offset = 0x1c0,
8341       .access = PL2_RW, .accessfn = access_fgt,
8342       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8343     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8344       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8345       .nv2_redirect_offset = 0x1d0,
8346       .access = PL2_RW, .accessfn = access_fgt,
8347       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8348     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8349       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8350       .nv2_redirect_offset = 0x1d8,
8351       .access = PL2_RW, .accessfn = access_fgt,
8352       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8353     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8354       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8355       .nv2_redirect_offset = 0x1c8,
8356       .access = PL2_RW, .accessfn = access_fgt,
8357       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8358 };
8359 
8360 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8361                        uint64_t value)
8362 {
8363     /*
8364      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8365      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8366      * about the RESS bits at the top -- we choose the "generate an EL2
8367      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8368      * the ptw.c code detect the resulting invalid address).
8369      */
8370     env->cp15.vncr_el2 = value & ~0xfffULL;
8371 }
8372 
8373 static const ARMCPRegInfo nv2_reginfo[] = {
8374     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8375       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8376       .access = PL2_RW,
8377       .writefn = vncr_write,
8378       .nv2_redirect_offset = 0xb0,
8379       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8380 };
8381 
8382 #endif /* TARGET_AARCH64 */
8383 
8384 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8385                                      bool isread)
8386 {
8387     int el = arm_current_el(env);
8388 
8389     if (el == 0) {
8390         uint64_t sctlr = arm_sctlr(env, el);
8391         if (!(sctlr & SCTLR_EnRCTX)) {
8392             return CP_ACCESS_TRAP;
8393         }
8394     } else if (el == 1) {
8395         uint64_t hcr = arm_hcr_el2_eff(env);
8396         if (hcr & HCR_NV) {
8397             return CP_ACCESS_TRAP_EL2;
8398         }
8399     }
8400     return CP_ACCESS_OK;
8401 }
8402 
8403 static const ARMCPRegInfo predinv_reginfo[] = {
8404     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8405       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8406       .fgt = FGT_CFPRCTX,
8407       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8408     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8409       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8410       .fgt = FGT_DVPRCTX,
8411       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8412     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8413       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8414       .fgt = FGT_CPPRCTX,
8415       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8416     /*
8417      * Note the AArch32 opcodes have a different OPC1.
8418      */
8419     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8420       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8421       .fgt = FGT_CFPRCTX,
8422       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8423     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8424       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8425       .fgt = FGT_DVPRCTX,
8426       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8427     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8428       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8429       .fgt = FGT_CPPRCTX,
8430       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8431 };
8432 
8433 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8434 {
8435     /* Read the high 32 bits of the current CCSIDR */
8436     return extract64(ccsidr_read(env, ri), 32, 32);
8437 }
8438 
8439 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8440     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8441       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8442       .access = PL1_R,
8443       .accessfn = access_tid4,
8444       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8445 };
8446 
8447 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8448                                        bool isread)
8449 {
8450     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8451         return CP_ACCESS_TRAP_EL2;
8452     }
8453 
8454     return CP_ACCESS_OK;
8455 }
8456 
8457 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8458                                        bool isread)
8459 {
8460     if (arm_feature(env, ARM_FEATURE_V8)) {
8461         return access_aa64_tid3(env, ri, isread);
8462     }
8463 
8464     return CP_ACCESS_OK;
8465 }
8466 
8467 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8468                                      bool isread)
8469 {
8470     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8471         return CP_ACCESS_TRAP_EL2;
8472     }
8473 
8474     return CP_ACCESS_OK;
8475 }
8476 
8477 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8478                                         const ARMCPRegInfo *ri, bool isread)
8479 {
8480     /*
8481      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8482      * in v7A, not in v8A.
8483      */
8484     if (!arm_feature(env, ARM_FEATURE_V8) &&
8485         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8486         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8487         return CP_ACCESS_TRAP_EL2;
8488     }
8489     return CP_ACCESS_OK;
8490 }
8491 
8492 static const ARMCPRegInfo jazelle_regs[] = {
8493     { .name = "JIDR",
8494       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8495       .access = PL1_R, .accessfn = access_jazelle,
8496       .type = ARM_CP_CONST, .resetvalue = 0 },
8497     { .name = "JOSCR",
8498       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8499       .accessfn = access_joscr_jmcr,
8500       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8501     { .name = "JMCR",
8502       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8503       .accessfn = access_joscr_jmcr,
8504       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8505 };
8506 
8507 static const ARMCPRegInfo contextidr_el2 = {
8508     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8509     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8510     .access = PL2_RW,
8511     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8512 };
8513 
8514 static const ARMCPRegInfo vhe_reginfo[] = {
8515     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8516       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8517       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8518       .raw_writefn = raw_write,
8519       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8520 #ifndef CONFIG_USER_ONLY
8521     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8522       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8523       .fieldoffset =
8524         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8525       .type = ARM_CP_IO, .access = PL2_RW,
8526       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8527     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8528       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8529       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8530       .resetfn = gt_hv_timer_reset,
8531       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8532     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8533       .type = ARM_CP_IO,
8534       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8535       .access = PL2_RW,
8536       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8537       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8538     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8539       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8540       .type = ARM_CP_IO | ARM_CP_ALIAS,
8541       .access = PL2_RW, .accessfn = access_el1nvpct,
8542       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8543       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8544       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8545     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8546       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8547       .type = ARM_CP_IO | ARM_CP_ALIAS,
8548       .access = PL2_RW, .accessfn = access_el1nvvct,
8549       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8550       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8551       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8552     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8553       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8554       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8555       .access = PL2_RW, .accessfn = e2h_access,
8556       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8557     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8558       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8559       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8560       .access = PL2_RW, .accessfn = e2h_access,
8561       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8562     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8563       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8564       .type = ARM_CP_IO | ARM_CP_ALIAS,
8565       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8566       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8567       .access = PL2_RW, .accessfn = access_el1nvpct,
8568       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8569     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8570       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8571       .type = ARM_CP_IO | ARM_CP_ALIAS,
8572       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8573       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8574       .access = PL2_RW, .accessfn = access_el1nvvct,
8575       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8576 #endif
8577 };
8578 
8579 #ifndef CONFIG_USER_ONLY
8580 static const ARMCPRegInfo ats1e1_reginfo[] = {
8581     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8582       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8583       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8584       .fgt = FGT_ATS1E1RP,
8585       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8586     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8587       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8588       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8589       .fgt = FGT_ATS1E1WP,
8590       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8591 };
8592 
8593 static const ARMCPRegInfo ats1cp_reginfo[] = {
8594     { .name = "ATS1CPRP",
8595       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8596       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8597       .writefn = ats_write },
8598     { .name = "ATS1CPWP",
8599       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8600       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8601       .writefn = ats_write },
8602 };
8603 #endif
8604 
8605 /*
8606  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8607  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8608  * is non-zero, which is never for ARMv7, optionally in ARMv8
8609  * and mandatorily for ARMv8.2 and up.
8610  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8611  * implementation is RAZ/WI we can ignore this detail, as we
8612  * do for ACTLR.
8613  */
8614 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8615     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8616       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8617       .access = PL1_RW, .accessfn = access_tacr,
8618       .type = ARM_CP_CONST, .resetvalue = 0 },
8619     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8620       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8621       .access = PL2_RW, .type = ARM_CP_CONST,
8622       .resetvalue = 0 },
8623 };
8624 
8625 void register_cp_regs_for_features(ARMCPU *cpu)
8626 {
8627     /* Register all the coprocessor registers based on feature bits */
8628     CPUARMState *env = &cpu->env;
8629     if (arm_feature(env, ARM_FEATURE_M)) {
8630         /* M profile has no coprocessor registers */
8631         return;
8632     }
8633 
8634     define_arm_cp_regs(cpu, cp_reginfo);
8635     if (!arm_feature(env, ARM_FEATURE_V8)) {
8636         /*
8637          * Must go early as it is full of wildcards that may be
8638          * overridden by later definitions.
8639          */
8640         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8641     }
8642 
8643     if (arm_feature(env, ARM_FEATURE_V6)) {
8644         /* The ID registers all have impdef reset values */
8645         ARMCPRegInfo v6_idregs[] = {
8646             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8647               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8648               .access = PL1_R, .type = ARM_CP_CONST,
8649               .accessfn = access_aa32_tid3,
8650               .resetvalue = cpu->isar.id_pfr0 },
8651             /*
8652              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8653              * the value of the GIC field until after we define these regs.
8654              */
8655             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8656               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8657               .access = PL1_R, .type = ARM_CP_NO_RAW,
8658               .accessfn = access_aa32_tid3,
8659 #ifdef CONFIG_USER_ONLY
8660               .type = ARM_CP_CONST,
8661               .resetvalue = cpu->isar.id_pfr1,
8662 #else
8663               .type = ARM_CP_NO_RAW,
8664               .accessfn = access_aa32_tid3,
8665               .readfn = id_pfr1_read,
8666               .writefn = arm_cp_write_ignore
8667 #endif
8668             },
8669             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8670               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8671               .access = PL1_R, .type = ARM_CP_CONST,
8672               .accessfn = access_aa32_tid3,
8673               .resetvalue = cpu->isar.id_dfr0 },
8674             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8675               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8676               .access = PL1_R, .type = ARM_CP_CONST,
8677               .accessfn = access_aa32_tid3,
8678               .resetvalue = cpu->id_afr0 },
8679             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8680               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8681               .access = PL1_R, .type = ARM_CP_CONST,
8682               .accessfn = access_aa32_tid3,
8683               .resetvalue = cpu->isar.id_mmfr0 },
8684             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8685               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8686               .access = PL1_R, .type = ARM_CP_CONST,
8687               .accessfn = access_aa32_tid3,
8688               .resetvalue = cpu->isar.id_mmfr1 },
8689             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8690               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8691               .access = PL1_R, .type = ARM_CP_CONST,
8692               .accessfn = access_aa32_tid3,
8693               .resetvalue = cpu->isar.id_mmfr2 },
8694             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8695               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8696               .access = PL1_R, .type = ARM_CP_CONST,
8697               .accessfn = access_aa32_tid3,
8698               .resetvalue = cpu->isar.id_mmfr3 },
8699             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8700               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8701               .access = PL1_R, .type = ARM_CP_CONST,
8702               .accessfn = access_aa32_tid3,
8703               .resetvalue = cpu->isar.id_isar0 },
8704             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8705               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8706               .access = PL1_R, .type = ARM_CP_CONST,
8707               .accessfn = access_aa32_tid3,
8708               .resetvalue = cpu->isar.id_isar1 },
8709             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8710               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8711               .access = PL1_R, .type = ARM_CP_CONST,
8712               .accessfn = access_aa32_tid3,
8713               .resetvalue = cpu->isar.id_isar2 },
8714             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8715               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8716               .access = PL1_R, .type = ARM_CP_CONST,
8717               .accessfn = access_aa32_tid3,
8718               .resetvalue = cpu->isar.id_isar3 },
8719             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8720               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8721               .access = PL1_R, .type = ARM_CP_CONST,
8722               .accessfn = access_aa32_tid3,
8723               .resetvalue = cpu->isar.id_isar4 },
8724             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8725               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8726               .access = PL1_R, .type = ARM_CP_CONST,
8727               .accessfn = access_aa32_tid3,
8728               .resetvalue = cpu->isar.id_isar5 },
8729             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8730               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8731               .access = PL1_R, .type = ARM_CP_CONST,
8732               .accessfn = access_aa32_tid3,
8733               .resetvalue = cpu->isar.id_mmfr4 },
8734             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8735               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8736               .access = PL1_R, .type = ARM_CP_CONST,
8737               .accessfn = access_aa32_tid3,
8738               .resetvalue = cpu->isar.id_isar6 },
8739         };
8740         define_arm_cp_regs(cpu, v6_idregs);
8741         define_arm_cp_regs(cpu, v6_cp_reginfo);
8742     } else {
8743         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8744     }
8745     if (arm_feature(env, ARM_FEATURE_V6K)) {
8746         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8747     }
8748     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8749         !arm_feature(env, ARM_FEATURE_PMSA)) {
8750         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8751     }
8752     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8753         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8754     }
8755     if (arm_feature(env, ARM_FEATURE_V7)) {
8756         ARMCPRegInfo clidr = {
8757             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8758             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8759             .access = PL1_R, .type = ARM_CP_CONST,
8760             .accessfn = access_tid4,
8761             .fgt = FGT_CLIDR_EL1,
8762             .resetvalue = cpu->clidr
8763         };
8764         define_one_arm_cp_reg(cpu, &clidr);
8765         define_arm_cp_regs(cpu, v7_cp_reginfo);
8766         define_debug_regs(cpu);
8767         define_pmu_regs(cpu);
8768     } else {
8769         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8770     }
8771     if (arm_feature(env, ARM_FEATURE_V8)) {
8772         /*
8773          * v8 ID registers, which all have impdef reset values.
8774          * Note that within the ID register ranges the unused slots
8775          * must all RAZ, not UNDEF; future architecture versions may
8776          * define new registers here.
8777          * ID registers which are AArch64 views of the AArch32 ID registers
8778          * which already existed in v6 and v7 are handled elsewhere,
8779          * in v6_idregs[].
8780          */
8781         int i;
8782         ARMCPRegInfo v8_idregs[] = {
8783             /*
8784              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8785              * emulation because we don't know the right value for the
8786              * GIC field until after we define these regs.
8787              */
8788             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8790               .access = PL1_R,
8791 #ifdef CONFIG_USER_ONLY
8792               .type = ARM_CP_CONST,
8793               .resetvalue = cpu->isar.id_aa64pfr0
8794 #else
8795               .type = ARM_CP_NO_RAW,
8796               .accessfn = access_aa64_tid3,
8797               .readfn = id_aa64pfr0_read,
8798               .writefn = arm_cp_write_ignore
8799 #endif
8800             },
8801             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8802               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8803               .access = PL1_R, .type = ARM_CP_CONST,
8804               .accessfn = access_aa64_tid3,
8805               .resetvalue = cpu->isar.id_aa64pfr1},
8806             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8807               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8808               .access = PL1_R, .type = ARM_CP_CONST,
8809               .accessfn = access_aa64_tid3,
8810               .resetvalue = 0 },
8811             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8812               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8813               .access = PL1_R, .type = ARM_CP_CONST,
8814               .accessfn = access_aa64_tid3,
8815               .resetvalue = 0 },
8816             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8817               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8818               .access = PL1_R, .type = ARM_CP_CONST,
8819               .accessfn = access_aa64_tid3,
8820               .resetvalue = cpu->isar.id_aa64zfr0 },
8821             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8822               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8823               .access = PL1_R, .type = ARM_CP_CONST,
8824               .accessfn = access_aa64_tid3,
8825               .resetvalue = cpu->isar.id_aa64smfr0 },
8826             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8827               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8828               .access = PL1_R, .type = ARM_CP_CONST,
8829               .accessfn = access_aa64_tid3,
8830               .resetvalue = 0 },
8831             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8832               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8833               .access = PL1_R, .type = ARM_CP_CONST,
8834               .accessfn = access_aa64_tid3,
8835               .resetvalue = 0 },
8836             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8837               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8838               .access = PL1_R, .type = ARM_CP_CONST,
8839               .accessfn = access_aa64_tid3,
8840               .resetvalue = cpu->isar.id_aa64dfr0 },
8841             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8842               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8843               .access = PL1_R, .type = ARM_CP_CONST,
8844               .accessfn = access_aa64_tid3,
8845               .resetvalue = cpu->isar.id_aa64dfr1 },
8846             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8847               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8848               .access = PL1_R, .type = ARM_CP_CONST,
8849               .accessfn = access_aa64_tid3,
8850               .resetvalue = 0 },
8851             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8852               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8853               .access = PL1_R, .type = ARM_CP_CONST,
8854               .accessfn = access_aa64_tid3,
8855               .resetvalue = 0 },
8856             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8857               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8858               .access = PL1_R, .type = ARM_CP_CONST,
8859               .accessfn = access_aa64_tid3,
8860               .resetvalue = cpu->id_aa64afr0 },
8861             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8862               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8863               .access = PL1_R, .type = ARM_CP_CONST,
8864               .accessfn = access_aa64_tid3,
8865               .resetvalue = cpu->id_aa64afr1 },
8866             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8867               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8868               .access = PL1_R, .type = ARM_CP_CONST,
8869               .accessfn = access_aa64_tid3,
8870               .resetvalue = 0 },
8871             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8872               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8873               .access = PL1_R, .type = ARM_CP_CONST,
8874               .accessfn = access_aa64_tid3,
8875               .resetvalue = 0 },
8876             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8877               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8878               .access = PL1_R, .type = ARM_CP_CONST,
8879               .accessfn = access_aa64_tid3,
8880               .resetvalue = cpu->isar.id_aa64isar0 },
8881             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8882               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8883               .access = PL1_R, .type = ARM_CP_CONST,
8884               .accessfn = access_aa64_tid3,
8885               .resetvalue = cpu->isar.id_aa64isar1 },
8886             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8887               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8888               .access = PL1_R, .type = ARM_CP_CONST,
8889               .accessfn = access_aa64_tid3,
8890               .resetvalue = cpu->isar.id_aa64isar2 },
8891             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8892               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8893               .access = PL1_R, .type = ARM_CP_CONST,
8894               .accessfn = access_aa64_tid3,
8895               .resetvalue = 0 },
8896             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8897               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8898               .access = PL1_R, .type = ARM_CP_CONST,
8899               .accessfn = access_aa64_tid3,
8900               .resetvalue = 0 },
8901             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8902               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8903               .access = PL1_R, .type = ARM_CP_CONST,
8904               .accessfn = access_aa64_tid3,
8905               .resetvalue = 0 },
8906             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8907               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8908               .access = PL1_R, .type = ARM_CP_CONST,
8909               .accessfn = access_aa64_tid3,
8910               .resetvalue = 0 },
8911             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8912               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8913               .access = PL1_R, .type = ARM_CP_CONST,
8914               .accessfn = access_aa64_tid3,
8915               .resetvalue = 0 },
8916             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8917               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8918               .access = PL1_R, .type = ARM_CP_CONST,
8919               .accessfn = access_aa64_tid3,
8920               .resetvalue = cpu->isar.id_aa64mmfr0 },
8921             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8922               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8923               .access = PL1_R, .type = ARM_CP_CONST,
8924               .accessfn = access_aa64_tid3,
8925               .resetvalue = cpu->isar.id_aa64mmfr1 },
8926             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8927               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8928               .access = PL1_R, .type = ARM_CP_CONST,
8929               .accessfn = access_aa64_tid3,
8930               .resetvalue = cpu->isar.id_aa64mmfr2 },
8931             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8932               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8933               .access = PL1_R, .type = ARM_CP_CONST,
8934               .accessfn = access_aa64_tid3,
8935               .resetvalue = 0 },
8936             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8937               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8938               .access = PL1_R, .type = ARM_CP_CONST,
8939               .accessfn = access_aa64_tid3,
8940               .resetvalue = 0 },
8941             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8942               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8943               .access = PL1_R, .type = ARM_CP_CONST,
8944               .accessfn = access_aa64_tid3,
8945               .resetvalue = 0 },
8946             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8947               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8948               .access = PL1_R, .type = ARM_CP_CONST,
8949               .accessfn = access_aa64_tid3,
8950               .resetvalue = 0 },
8951             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8952               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8953               .access = PL1_R, .type = ARM_CP_CONST,
8954               .accessfn = access_aa64_tid3,
8955               .resetvalue = 0 },
8956             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8957               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8958               .access = PL1_R, .type = ARM_CP_CONST,
8959               .accessfn = access_aa64_tid3,
8960               .resetvalue = cpu->isar.mvfr0 },
8961             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8962               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8963               .access = PL1_R, .type = ARM_CP_CONST,
8964               .accessfn = access_aa64_tid3,
8965               .resetvalue = cpu->isar.mvfr1 },
8966             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8967               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8968               .access = PL1_R, .type = ARM_CP_CONST,
8969               .accessfn = access_aa64_tid3,
8970               .resetvalue = cpu->isar.mvfr2 },
8971             /*
8972              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8973              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8974              * as RAZ, since it is in the "reserved for future ID
8975              * registers, RAZ" part of the AArch32 encoding space.
8976              */
8977             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8978               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8979               .access = PL1_R, .type = ARM_CP_CONST,
8980               .accessfn = access_aa64_tid3,
8981               .resetvalue = 0 },
8982             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8983               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8984               .access = PL1_R, .type = ARM_CP_CONST,
8985               .accessfn = access_aa64_tid3,
8986               .resetvalue = 0 },
8987             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8988               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8989               .access = PL1_R, .type = ARM_CP_CONST,
8990               .accessfn = access_aa64_tid3,
8991               .resetvalue = 0 },
8992             /*
8993              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8994              * they're also RAZ for AArch64, and in v8 are gradually
8995              * being filled with AArch64-view-of-AArch32-ID-register
8996              * for new ID registers.
8997              */
8998             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8999               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
9000               .access = PL1_R, .type = ARM_CP_CONST,
9001               .accessfn = access_aa64_tid3,
9002               .resetvalue = 0 },
9003             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
9004               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
9005               .access = PL1_R, .type = ARM_CP_CONST,
9006               .accessfn = access_aa64_tid3,
9007               .resetvalue = cpu->isar.id_pfr2 },
9008             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
9009               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
9010               .access = PL1_R, .type = ARM_CP_CONST,
9011               .accessfn = access_aa64_tid3,
9012               .resetvalue = cpu->isar.id_dfr1 },
9013             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
9014               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
9015               .access = PL1_R, .type = ARM_CP_CONST,
9016               .accessfn = access_aa64_tid3,
9017               .resetvalue = cpu->isar.id_mmfr5 },
9018             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
9019               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
9020               .access = PL1_R, .type = ARM_CP_CONST,
9021               .accessfn = access_aa64_tid3,
9022               .resetvalue = 0 },
9023             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
9024               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
9025               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9026               .fgt = FGT_PMCEIDN_EL0,
9027               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
9028             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
9029               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
9030               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9031               .fgt = FGT_PMCEIDN_EL0,
9032               .resetvalue = cpu->pmceid0 },
9033             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
9034               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
9035               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9036               .fgt = FGT_PMCEIDN_EL0,
9037               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
9038             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
9039               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
9040               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9041               .fgt = FGT_PMCEIDN_EL0,
9042               .resetvalue = cpu->pmceid1 },
9043         };
9044 #ifdef CONFIG_USER_ONLY
9045         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
9046             { .name = "ID_AA64PFR0_EL1",
9047               .exported_bits = R_ID_AA64PFR0_FP_MASK |
9048                                R_ID_AA64PFR0_ADVSIMD_MASK |
9049                                R_ID_AA64PFR0_SVE_MASK |
9050                                R_ID_AA64PFR0_DIT_MASK,
9051               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
9052                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
9053             { .name = "ID_AA64PFR1_EL1",
9054               .exported_bits = R_ID_AA64PFR1_BT_MASK |
9055                                R_ID_AA64PFR1_SSBS_MASK |
9056                                R_ID_AA64PFR1_MTE_MASK |
9057                                R_ID_AA64PFR1_SME_MASK },
9058             { .name = "ID_AA64PFR*_EL1_RESERVED",
9059               .is_glob = true },
9060             { .name = "ID_AA64ZFR0_EL1",
9061               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
9062                                R_ID_AA64ZFR0_AES_MASK |
9063                                R_ID_AA64ZFR0_BITPERM_MASK |
9064                                R_ID_AA64ZFR0_BFLOAT16_MASK |
9065                                R_ID_AA64ZFR0_B16B16_MASK |
9066                                R_ID_AA64ZFR0_SHA3_MASK |
9067                                R_ID_AA64ZFR0_SM4_MASK |
9068                                R_ID_AA64ZFR0_I8MM_MASK |
9069                                R_ID_AA64ZFR0_F32MM_MASK |
9070                                R_ID_AA64ZFR0_F64MM_MASK },
9071             { .name = "ID_AA64SMFR0_EL1",
9072               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
9073                                R_ID_AA64SMFR0_BI32I32_MASK |
9074                                R_ID_AA64SMFR0_B16F32_MASK |
9075                                R_ID_AA64SMFR0_F16F32_MASK |
9076                                R_ID_AA64SMFR0_I8I32_MASK |
9077                                R_ID_AA64SMFR0_F16F16_MASK |
9078                                R_ID_AA64SMFR0_B16B16_MASK |
9079                                R_ID_AA64SMFR0_I16I32_MASK |
9080                                R_ID_AA64SMFR0_F64F64_MASK |
9081                                R_ID_AA64SMFR0_I16I64_MASK |
9082                                R_ID_AA64SMFR0_SMEVER_MASK |
9083                                R_ID_AA64SMFR0_FA64_MASK },
9084             { .name = "ID_AA64MMFR0_EL1",
9085               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
9086               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
9087                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
9088             { .name = "ID_AA64MMFR1_EL1",
9089               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
9090             { .name = "ID_AA64MMFR2_EL1",
9091               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
9092             { .name = "ID_AA64MMFR*_EL1_RESERVED",
9093               .is_glob = true },
9094             { .name = "ID_AA64DFR0_EL1",
9095               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
9096             { .name = "ID_AA64DFR1_EL1" },
9097             { .name = "ID_AA64DFR*_EL1_RESERVED",
9098               .is_glob = true },
9099             { .name = "ID_AA64AFR*",
9100               .is_glob = true },
9101             { .name = "ID_AA64ISAR0_EL1",
9102               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
9103                                R_ID_AA64ISAR0_SHA1_MASK |
9104                                R_ID_AA64ISAR0_SHA2_MASK |
9105                                R_ID_AA64ISAR0_CRC32_MASK |
9106                                R_ID_AA64ISAR0_ATOMIC_MASK |
9107                                R_ID_AA64ISAR0_RDM_MASK |
9108                                R_ID_AA64ISAR0_SHA3_MASK |
9109                                R_ID_AA64ISAR0_SM3_MASK |
9110                                R_ID_AA64ISAR0_SM4_MASK |
9111                                R_ID_AA64ISAR0_DP_MASK |
9112                                R_ID_AA64ISAR0_FHM_MASK |
9113                                R_ID_AA64ISAR0_TS_MASK |
9114                                R_ID_AA64ISAR0_RNDR_MASK },
9115             { .name = "ID_AA64ISAR1_EL1",
9116               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
9117                                R_ID_AA64ISAR1_APA_MASK |
9118                                R_ID_AA64ISAR1_API_MASK |
9119                                R_ID_AA64ISAR1_JSCVT_MASK |
9120                                R_ID_AA64ISAR1_FCMA_MASK |
9121                                R_ID_AA64ISAR1_LRCPC_MASK |
9122                                R_ID_AA64ISAR1_GPA_MASK |
9123                                R_ID_AA64ISAR1_GPI_MASK |
9124                                R_ID_AA64ISAR1_FRINTTS_MASK |
9125                                R_ID_AA64ISAR1_SB_MASK |
9126                                R_ID_AA64ISAR1_BF16_MASK |
9127                                R_ID_AA64ISAR1_DGH_MASK |
9128                                R_ID_AA64ISAR1_I8MM_MASK },
9129             { .name = "ID_AA64ISAR2_EL1",
9130               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
9131                                R_ID_AA64ISAR2_RPRES_MASK |
9132                                R_ID_AA64ISAR2_GPA3_MASK |
9133                                R_ID_AA64ISAR2_APA3_MASK |
9134                                R_ID_AA64ISAR2_MOPS_MASK |
9135                                R_ID_AA64ISAR2_BC_MASK |
9136                                R_ID_AA64ISAR2_RPRFM_MASK |
9137                                R_ID_AA64ISAR2_CSSC_MASK },
9138             { .name = "ID_AA64ISAR*_EL1_RESERVED",
9139               .is_glob = true },
9140         };
9141         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
9142 #endif
9143         /*
9144          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
9145          * TODO: For RMR, a write with bit 1 set should do something with
9146          * cpu_reset(). In the meantime, "the bit is strictly a request",
9147          * so we are in spec just ignoring writes.
9148          */
9149         if (!arm_feature(env, ARM_FEATURE_EL3) &&
9150             !arm_feature(env, ARM_FEATURE_EL2)) {
9151             ARMCPRegInfo el1_reset_regs[] = {
9152                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
9153                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9154                   .access = PL1_R,
9155                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9156                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9157                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9158                   .access = PL1_RW, .type = ARM_CP_CONST,
9159                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9160             };
9161             define_arm_cp_regs(cpu, el1_reset_regs);
9162         }
9163         define_arm_cp_regs(cpu, v8_idregs);
9164         define_arm_cp_regs(cpu, v8_cp_reginfo);
9165         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9166             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9167         }
9168 
9169         for (i = 4; i < 16; i++) {
9170             /*
9171              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9172              * For pre-v8 cores there are RAZ patterns for these in
9173              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9174              * v8 extends the "must RAZ" part of the ID register space
9175              * to also cover c0, 0, c{8-15}, {0-7}.
9176              * These are STATE_AA32 because in the AArch64 sysreg space
9177              * c4-c7 is where the AArch64 ID registers live (and we've
9178              * already defined those in v8_idregs[]), and c8-c15 are not
9179              * "must RAZ" for AArch64.
9180              */
9181             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9182             ARMCPRegInfo v8_aa32_raz_idregs = {
9183                 .name = name,
9184                 .state = ARM_CP_STATE_AA32,
9185                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9186                 .access = PL1_R, .type = ARM_CP_CONST,
9187                 .accessfn = access_aa64_tid3,
9188                 .resetvalue = 0 };
9189             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9190         }
9191     }
9192 
9193     /*
9194      * Register the base EL2 cpregs.
9195      * Pre v8, these registers are implemented only as part of the
9196      * Virtualization Extensions (EL2 present).  Beginning with v8,
9197      * if EL2 is missing but EL3 is enabled, mostly these become
9198      * RES0 from EL3, with some specific exceptions.
9199      */
9200     if (arm_feature(env, ARM_FEATURE_EL2)
9201         || (arm_feature(env, ARM_FEATURE_EL3)
9202             && arm_feature(env, ARM_FEATURE_V8))) {
9203         uint64_t vmpidr_def = mpidr_read_val(env);
9204         ARMCPRegInfo vpidr_regs[] = {
9205             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9206               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9207               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9208               .resetvalue = cpu->midr,
9209               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9210               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9211             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9212               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9213               .access = PL2_RW, .resetvalue = cpu->midr,
9214               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9215               .nv2_redirect_offset = 0x88,
9216               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9217             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9218               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9219               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9220               .resetvalue = vmpidr_def,
9221               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9222               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9223             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9224               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9225               .access = PL2_RW, .resetvalue = vmpidr_def,
9226               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9227               .nv2_redirect_offset = 0x50,
9228               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9229         };
9230         /*
9231          * The only field of MDCR_EL2 that has a defined architectural reset
9232          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9233          */
9234         ARMCPRegInfo mdcr_el2 = {
9235             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9236             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9237             .writefn = mdcr_el2_write,
9238             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9239             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9240         };
9241         define_one_arm_cp_reg(cpu, &mdcr_el2);
9242         define_arm_cp_regs(cpu, vpidr_regs);
9243         define_arm_cp_regs(cpu, el2_cp_reginfo);
9244         if (arm_feature(env, ARM_FEATURE_V8)) {
9245             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9246         }
9247         if (cpu_isar_feature(aa64_sel2, cpu)) {
9248             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9249         }
9250         /*
9251          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9252          * See commentary near RMR_EL1.
9253          */
9254         if (!arm_feature(env, ARM_FEATURE_EL3)) {
9255             static const ARMCPRegInfo el2_reset_regs[] = {
9256                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9257                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9258                   .access = PL2_R,
9259                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9260                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9261                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9262                   .access = PL2_R,
9263                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9264                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9265                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9266                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9267             };
9268             define_arm_cp_regs(cpu, el2_reset_regs);
9269         }
9270     }
9271 
9272     /* Register the base EL3 cpregs. */
9273     if (arm_feature(env, ARM_FEATURE_EL3)) {
9274         define_arm_cp_regs(cpu, el3_cp_reginfo);
9275         ARMCPRegInfo el3_regs[] = {
9276             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9277               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9278               .access = PL3_R,
9279               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9280             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9281               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9282               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9283             { .name = "RMR", .state = ARM_CP_STATE_AA32,
9284               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9285               .access = PL3_RW, .type = ARM_CP_CONST,
9286               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9287             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9288               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9289               .access = PL3_RW,
9290               .raw_writefn = raw_write, .writefn = sctlr_write,
9291               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9292               .resetvalue = cpu->reset_sctlr },
9293         };
9294 
9295         define_arm_cp_regs(cpu, el3_regs);
9296     }
9297     /*
9298      * The behaviour of NSACR is sufficiently various that we don't
9299      * try to describe it in a single reginfo:
9300      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
9301      *     reads as constant 0xc00 from NS EL1 and NS EL2
9302      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9303      *  if v7 without EL3, register doesn't exist
9304      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9305      */
9306     if (arm_feature(env, ARM_FEATURE_EL3)) {
9307         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9308             static const ARMCPRegInfo nsacr = {
9309                 .name = "NSACR", .type = ARM_CP_CONST,
9310                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9311                 .access = PL1_RW, .accessfn = nsacr_access,
9312                 .resetvalue = 0xc00
9313             };
9314             define_one_arm_cp_reg(cpu, &nsacr);
9315         } else {
9316             static const ARMCPRegInfo nsacr = {
9317                 .name = "NSACR",
9318                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9319                 .access = PL3_RW | PL1_R,
9320                 .resetvalue = 0,
9321                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9322             };
9323             define_one_arm_cp_reg(cpu, &nsacr);
9324         }
9325     } else {
9326         if (arm_feature(env, ARM_FEATURE_V8)) {
9327             static const ARMCPRegInfo nsacr = {
9328                 .name = "NSACR", .type = ARM_CP_CONST,
9329                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9330                 .access = PL1_R,
9331                 .resetvalue = 0xc00
9332             };
9333             define_one_arm_cp_reg(cpu, &nsacr);
9334         }
9335     }
9336 
9337     if (arm_feature(env, ARM_FEATURE_PMSA)) {
9338         if (arm_feature(env, ARM_FEATURE_V6)) {
9339             /* PMSAv6 not implemented */
9340             assert(arm_feature(env, ARM_FEATURE_V7));
9341             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9342             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9343         } else {
9344             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9345         }
9346     } else {
9347         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9348         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9349         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
9350         if (cpu_isar_feature(aa32_hpd, cpu)) {
9351             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9352         }
9353     }
9354     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9355         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9356     }
9357     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9358         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9359     }
9360     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
9361         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
9362     }
9363 #ifndef CONFIG_USER_ONLY
9364     if (cpu_isar_feature(aa64_ecv, cpu)) {
9365         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
9366     }
9367 #endif
9368     if (arm_feature(env, ARM_FEATURE_VAPA)) {
9369         ARMCPRegInfo vapa_cp_reginfo[] = {
9370             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9371               .access = PL1_RW, .resetvalue = 0,
9372               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9373                                      offsetoflow32(CPUARMState, cp15.par_ns) },
9374               .writefn = par_write},
9375 #ifndef CONFIG_USER_ONLY
9376             /* This underdecoding is safe because the reginfo is NO_RAW. */
9377             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9378               .access = PL1_W, .accessfn = ats_access,
9379               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9380 #endif
9381         };
9382 
9383         /*
9384          * When LPAE exists this 32-bit PAR register is an alias of the
9385          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9386          */
9387         if (arm_feature(env, ARM_FEATURE_LPAE)) {
9388             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9389         }
9390         define_arm_cp_regs(cpu, vapa_cp_reginfo);
9391     }
9392     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9393         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9394     }
9395     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9396         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9397     }
9398     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9399         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9400     }
9401     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9402         define_arm_cp_regs(cpu, omap_cp_reginfo);
9403     }
9404     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9405         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9406     }
9407     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9408         define_arm_cp_regs(cpu, xscale_cp_reginfo);
9409     }
9410     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9411         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9412     }
9413     if (arm_feature(env, ARM_FEATURE_LPAE)) {
9414         define_arm_cp_regs(cpu, lpae_cp_reginfo);
9415     }
9416     if (cpu_isar_feature(aa32_jazelle, cpu)) {
9417         define_arm_cp_regs(cpu, jazelle_regs);
9418     }
9419     /*
9420      * Slightly awkwardly, the OMAP and StrongARM cores need all of
9421      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9422      * be read-only (ie write causes UNDEF exception).
9423      */
9424     {
9425         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9426             /*
9427              * Pre-v8 MIDR space.
9428              * Note that the MIDR isn't a simple constant register because
9429              * of the TI925 behaviour where writes to another register can
9430              * cause the MIDR value to change.
9431              *
9432              * Unimplemented registers in the c15 0 0 0 space default to
9433              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9434              * and friends override accordingly.
9435              */
9436             { .name = "MIDR",
9437               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9438               .access = PL1_R, .resetvalue = cpu->midr,
9439               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9440               .readfn = midr_read,
9441               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9442               .type = ARM_CP_OVERRIDE },
9443             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9444             { .name = "DUMMY",
9445               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9446               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9447             { .name = "DUMMY",
9448               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9449               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9450             { .name = "DUMMY",
9451               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9452               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9453             { .name = "DUMMY",
9454               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9455               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9456             { .name = "DUMMY",
9457               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9458               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9459         };
9460         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9461             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9462               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9463               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9464               .fgt = FGT_MIDR_EL1,
9465               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9466               .readfn = midr_read },
9467             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9468             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9469               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9470               .access = PL1_R, .resetvalue = cpu->midr },
9471             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9472               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9473               .access = PL1_R,
9474               .accessfn = access_aa64_tid1,
9475               .fgt = FGT_REVIDR_EL1,
9476               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9477         };
9478         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9479             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9480             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9481             .access = PL1_R, .resetvalue = cpu->midr
9482         };
9483         ARMCPRegInfo id_cp_reginfo[] = {
9484             /* These are common to v8 and pre-v8 */
9485             { .name = "CTR",
9486               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9487               .access = PL1_R, .accessfn = ctr_el0_access,
9488               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9489             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9490               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9491               .access = PL0_R, .accessfn = ctr_el0_access,
9492               .fgt = FGT_CTR_EL0,
9493               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9494             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9495             { .name = "TCMTR",
9496               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9497               .access = PL1_R,
9498               .accessfn = access_aa32_tid1,
9499               .type = ARM_CP_CONST, .resetvalue = 0 },
9500         };
9501         /* TLBTR is specific to VMSA */
9502         ARMCPRegInfo id_tlbtr_reginfo = {
9503               .name = "TLBTR",
9504               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9505               .access = PL1_R,
9506               .accessfn = access_aa32_tid1,
9507               .type = ARM_CP_CONST, .resetvalue = 0,
9508         };
9509         /* MPUIR is specific to PMSA V6+ */
9510         ARMCPRegInfo id_mpuir_reginfo = {
9511               .name = "MPUIR",
9512               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9513               .access = PL1_R, .type = ARM_CP_CONST,
9514               .resetvalue = cpu->pmsav7_dregion << 8
9515         };
9516         /* HMPUIR is specific to PMSA V8 */
9517         ARMCPRegInfo id_hmpuir_reginfo = {
9518             .name = "HMPUIR",
9519             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9520             .access = PL2_R, .type = ARM_CP_CONST,
9521             .resetvalue = cpu->pmsav8r_hdregion
9522         };
9523         static const ARMCPRegInfo crn0_wi_reginfo = {
9524             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9525             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9526             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9527         };
9528 #ifdef CONFIG_USER_ONLY
9529         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9530             { .name = "MIDR_EL1",
9531               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9532                                R_MIDR_EL1_PARTNUM_MASK |
9533                                R_MIDR_EL1_ARCHITECTURE_MASK |
9534                                R_MIDR_EL1_VARIANT_MASK |
9535                                R_MIDR_EL1_IMPLEMENTER_MASK },
9536             { .name = "REVIDR_EL1" },
9537         };
9538         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9539 #endif
9540         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9541             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9542             size_t i;
9543             /*
9544              * Register the blanket "writes ignored" value first to cover the
9545              * whole space. Then update the specific ID registers to allow write
9546              * access, so that they ignore writes rather than causing them to
9547              * UNDEF.
9548              */
9549             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9550             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9551                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9552             }
9553             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9554                 id_cp_reginfo[i].access = PL1_RW;
9555             }
9556             id_mpuir_reginfo.access = PL1_RW;
9557             id_tlbtr_reginfo.access = PL1_RW;
9558         }
9559         if (arm_feature(env, ARM_FEATURE_V8)) {
9560             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9561             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9562                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9563             }
9564         } else {
9565             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9566         }
9567         define_arm_cp_regs(cpu, id_cp_reginfo);
9568         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9569             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9570         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9571                    arm_feature(env, ARM_FEATURE_V8)) {
9572             uint32_t i = 0;
9573             char *tmp_string;
9574 
9575             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9576             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9577             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9578 
9579             /* Register alias is only valid for first 32 indexes */
9580             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9581                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9582                 uint8_t opc1 = extract32(i, 4, 1);
9583                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9584 
9585                 tmp_string = g_strdup_printf("PRBAR%u", i);
9586                 ARMCPRegInfo tmp_prbarn_reginfo = {
9587                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9588                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9589                     .access = PL1_RW, .resetvalue = 0,
9590                     .accessfn = access_tvm_trvm,
9591                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9592                 };
9593                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9594                 g_free(tmp_string);
9595 
9596                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9597                 tmp_string = g_strdup_printf("PRLAR%u", i);
9598                 ARMCPRegInfo tmp_prlarn_reginfo = {
9599                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9600                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9601                     .access = PL1_RW, .resetvalue = 0,
9602                     .accessfn = access_tvm_trvm,
9603                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9604                 };
9605                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9606                 g_free(tmp_string);
9607             }
9608 
9609             /* Register alias is only valid for first 32 indexes */
9610             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9611                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9612                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9613                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9614 
9615                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9616                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9617                     .name = tmp_string,
9618                     .type = ARM_CP_NO_RAW,
9619                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9620                     .access = PL2_RW, .resetvalue = 0,
9621                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9622                 };
9623                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9624                 g_free(tmp_string);
9625 
9626                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9627                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9628                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9629                     .name = tmp_string,
9630                     .type = ARM_CP_NO_RAW,
9631                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9632                     .access = PL2_RW, .resetvalue = 0,
9633                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9634                 };
9635                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9636                 g_free(tmp_string);
9637             }
9638         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9639             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9640         }
9641     }
9642 
9643     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9644         ARMCPRegInfo mpidr_cp_reginfo[] = {
9645             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9646               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9647               .fgt = FGT_MPIDR_EL1,
9648               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9649         };
9650 #ifdef CONFIG_USER_ONLY
9651         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9652             { .name = "MPIDR_EL1",
9653               .fixed_bits = 0x0000000080000000 },
9654         };
9655         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9656 #endif
9657         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9658     }
9659 
9660     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9661         ARMCPRegInfo auxcr_reginfo[] = {
9662             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9663               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9664               .access = PL1_RW, .accessfn = access_tacr,
9665               .nv2_redirect_offset = 0x118,
9666               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9667             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9668               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9669               .access = PL2_RW, .type = ARM_CP_CONST,
9670               .resetvalue = 0 },
9671             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9672               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9673               .access = PL3_RW, .type = ARM_CP_CONST,
9674               .resetvalue = 0 },
9675         };
9676         define_arm_cp_regs(cpu, auxcr_reginfo);
9677         if (cpu_isar_feature(aa32_ac2, cpu)) {
9678             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9679         }
9680     }
9681 
9682     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9683         /*
9684          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9685          * There are two flavours:
9686          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9687          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9688          *      32-bit register visible to AArch32 at a different encoding
9689          *      to the "flavour 1" register and with the bits rearranged to
9690          *      be able to squash a 64-bit address into the 32-bit view.
9691          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9692          * in future if we support AArch32-only configs of some of the
9693          * AArch64 cores we might need to add a specific feature flag
9694          * to indicate cores with "flavour 2" CBAR.
9695          */
9696         if (arm_feature(env, ARM_FEATURE_V8)) {
9697             /* 32 bit view is [31:18] 0...0 [43:32]. */
9698             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9699                 | extract64(cpu->reset_cbar, 32, 12);
9700             ARMCPRegInfo cbar_reginfo[] = {
9701                 { .name = "CBAR",
9702                   .type = ARM_CP_CONST,
9703                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9704                   .access = PL1_R, .resetvalue = cbar32 },
9705                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9706                   .type = ARM_CP_CONST,
9707                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9708                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9709             };
9710             /* We don't implement a r/w 64 bit CBAR currently */
9711             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9712             define_arm_cp_regs(cpu, cbar_reginfo);
9713         } else {
9714             ARMCPRegInfo cbar = {
9715                 .name = "CBAR",
9716                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9717                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9718                 .fieldoffset = offsetof(CPUARMState,
9719                                         cp15.c15_config_base_address)
9720             };
9721             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9722                 cbar.access = PL1_R;
9723                 cbar.fieldoffset = 0;
9724                 cbar.type = ARM_CP_CONST;
9725             }
9726             define_one_arm_cp_reg(cpu, &cbar);
9727         }
9728     }
9729 
9730     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9731         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9732             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9733               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9734               .access = PL1_RW, .writefn = vbar_write,
9735               .accessfn = access_nv1,
9736               .fgt = FGT_VBAR_EL1,
9737               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
9738               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9739                                      offsetof(CPUARMState, cp15.vbar_ns) },
9740               .resetvalue = 0 },
9741         };
9742         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9743     }
9744 
9745     /* Generic registers whose values depend on the implementation */
9746     {
9747         ARMCPRegInfo sctlr = {
9748             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9749             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9750             .access = PL1_RW, .accessfn = access_tvm_trvm,
9751             .fgt = FGT_SCTLR_EL1,
9752             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
9753             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9754                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9755             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9756             .raw_writefn = raw_write,
9757         };
9758         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9759             /*
9760              * Normally we would always end the TB on an SCTLR write, but Linux
9761              * arch/arm/mach-pxa/sleep.S expects two instructions following
9762              * an MMU enable to execute from cache.  Imitate this behaviour.
9763              */
9764             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9765         }
9766         define_one_arm_cp_reg(cpu, &sctlr);
9767 
9768         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9769             arm_feature(env, ARM_FEATURE_V8)) {
9770             ARMCPRegInfo vsctlr = {
9771                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9772                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9773                 .access = PL2_RW, .resetvalue = 0x0,
9774                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9775             };
9776             define_one_arm_cp_reg(cpu, &vsctlr);
9777         }
9778     }
9779 
9780     if (cpu_isar_feature(aa64_lor, cpu)) {
9781         define_arm_cp_regs(cpu, lor_reginfo);
9782     }
9783     if (cpu_isar_feature(aa64_pan, cpu)) {
9784         define_one_arm_cp_reg(cpu, &pan_reginfo);
9785     }
9786 #ifndef CONFIG_USER_ONLY
9787     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9788         define_arm_cp_regs(cpu, ats1e1_reginfo);
9789     }
9790     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9791         define_arm_cp_regs(cpu, ats1cp_reginfo);
9792     }
9793 #endif
9794     if (cpu_isar_feature(aa64_uao, cpu)) {
9795         define_one_arm_cp_reg(cpu, &uao_reginfo);
9796     }
9797 
9798     if (cpu_isar_feature(aa64_dit, cpu)) {
9799         define_one_arm_cp_reg(cpu, &dit_reginfo);
9800     }
9801     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9802         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9803     }
9804     if (cpu_isar_feature(any_ras, cpu)) {
9805         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9806     }
9807 
9808     if (cpu_isar_feature(aa64_vh, cpu) ||
9809         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9810         define_one_arm_cp_reg(cpu, &contextidr_el2);
9811     }
9812     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9813         define_arm_cp_regs(cpu, vhe_reginfo);
9814     }
9815 
9816     if (cpu_isar_feature(aa64_sve, cpu)) {
9817         define_arm_cp_regs(cpu, zcr_reginfo);
9818     }
9819 
9820     if (cpu_isar_feature(aa64_hcx, cpu)) {
9821         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9822     }
9823 
9824 #ifdef TARGET_AARCH64
9825     if (cpu_isar_feature(aa64_sme, cpu)) {
9826         define_arm_cp_regs(cpu, sme_reginfo);
9827     }
9828     if (cpu_isar_feature(aa64_pauth, cpu)) {
9829         define_arm_cp_regs(cpu, pauth_reginfo);
9830     }
9831     if (cpu_isar_feature(aa64_rndr, cpu)) {
9832         define_arm_cp_regs(cpu, rndr_reginfo);
9833     }
9834     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9835         define_arm_cp_regs(cpu, tlbirange_reginfo);
9836     }
9837     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9838         define_arm_cp_regs(cpu, tlbios_reginfo);
9839     }
9840     /* Data Cache clean instructions up to PoP */
9841     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9842         define_one_arm_cp_reg(cpu, dcpop_reg);
9843 
9844         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9845             define_one_arm_cp_reg(cpu, dcpodp_reg);
9846         }
9847     }
9848 
9849     /*
9850      * If full MTE is enabled, add all of the system registers.
9851      * If only "instructions available at EL0" are enabled,
9852      * then define only a RAZ/WI version of PSTATE.TCO.
9853      */
9854     if (cpu_isar_feature(aa64_mte, cpu)) {
9855         ARMCPRegInfo gmid_reginfo = {
9856             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9857             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9858             .access = PL1_R, .accessfn = access_aa64_tid5,
9859             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9860         };
9861         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9862         define_arm_cp_regs(cpu, mte_reginfo);
9863         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9864     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9865         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9866         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9867     }
9868 
9869     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9870         define_arm_cp_regs(cpu, scxtnum_reginfo);
9871     }
9872 
9873     if (cpu_isar_feature(aa64_fgt, cpu)) {
9874         define_arm_cp_regs(cpu, fgt_reginfo);
9875     }
9876 
9877     if (cpu_isar_feature(aa64_rme, cpu)) {
9878         define_arm_cp_regs(cpu, rme_reginfo);
9879         if (cpu_isar_feature(aa64_mte, cpu)) {
9880             define_arm_cp_regs(cpu, rme_mte_reginfo);
9881         }
9882     }
9883 
9884     if (cpu_isar_feature(aa64_nv2, cpu)) {
9885         define_arm_cp_regs(cpu, nv2_reginfo);
9886     }
9887 #endif
9888 
9889     if (cpu_isar_feature(any_predinv, cpu)) {
9890         define_arm_cp_regs(cpu, predinv_reginfo);
9891     }
9892 
9893     if (cpu_isar_feature(any_ccidx, cpu)) {
9894         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9895     }
9896 
9897 #ifndef CONFIG_USER_ONLY
9898     /*
9899      * Register redirections and aliases must be done last,
9900      * after the registers from the other extensions have been defined.
9901      */
9902     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9903         define_arm_vh_e2h_redirects_aliases(cpu);
9904     }
9905 #endif
9906 }
9907 
9908 /*
9909  * Private utility function for define_one_arm_cp_reg_with_opaque():
9910  * add a single reginfo struct to the hash table.
9911  */
9912 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9913                                    void *opaque, CPState state,
9914                                    CPSecureState secstate,
9915                                    int crm, int opc1, int opc2,
9916                                    const char *name)
9917 {
9918     CPUARMState *env = &cpu->env;
9919     uint32_t key;
9920     ARMCPRegInfo *r2;
9921     bool is64 = r->type & ARM_CP_64BIT;
9922     bool ns = secstate & ARM_CP_SECSTATE_NS;
9923     int cp = r->cp;
9924     size_t name_len;
9925     bool make_const;
9926 
9927     switch (state) {
9928     case ARM_CP_STATE_AA32:
9929         /* We assume it is a cp15 register if the .cp field is left unset. */
9930         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9931             cp = 15;
9932         }
9933         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9934         break;
9935     case ARM_CP_STATE_AA64:
9936         /*
9937          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9938          * cp == 0 as equivalent to the value for "standard guest-visible
9939          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9940          * in their AArch64 view (the .cp value may be non-zero for the
9941          * benefit of the AArch32 view).
9942          */
9943         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9944             cp = CP_REG_ARM64_SYSREG_CP;
9945         }
9946         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9947         break;
9948     default:
9949         g_assert_not_reached();
9950     }
9951 
9952     /* Overriding of an existing definition must be explicitly requested. */
9953     if (!(r->type & ARM_CP_OVERRIDE)) {
9954         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9955         if (oldreg) {
9956             assert(oldreg->type & ARM_CP_OVERRIDE);
9957         }
9958     }
9959 
9960     /*
9961      * Eliminate registers that are not present because the EL is missing.
9962      * Doing this here makes it easier to put all registers for a given
9963      * feature into the same ARMCPRegInfo array and define them all at once.
9964      */
9965     make_const = false;
9966     if (arm_feature(env, ARM_FEATURE_EL3)) {
9967         /*
9968          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9969          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9970          */
9971         int min_el = ctz32(r->access) / 2;
9972         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9973             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9974                 return;
9975             }
9976             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9977         }
9978     } else {
9979         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9980                                  ? PL2_RW : PL1_RW);
9981         if ((r->access & max_el) == 0) {
9982             return;
9983         }
9984     }
9985 
9986     /* Combine cpreg and name into one allocation. */
9987     name_len = strlen(name) + 1;
9988     r2 = g_malloc(sizeof(*r2) + name_len);
9989     *r2 = *r;
9990     r2->name = memcpy(r2 + 1, name, name_len);
9991 
9992     /*
9993      * Update fields to match the instantiation, overwiting wildcards
9994      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9995      */
9996     r2->cp = cp;
9997     r2->crm = crm;
9998     r2->opc1 = opc1;
9999     r2->opc2 = opc2;
10000     r2->state = state;
10001     r2->secure = secstate;
10002     if (opaque) {
10003         r2->opaque = opaque;
10004     }
10005 
10006     if (make_const) {
10007         /* This should not have been a very special register to begin. */
10008         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
10009         assert(old_special == 0 || old_special == ARM_CP_NOP);
10010         /*
10011          * Set the special function to CONST, retaining the other flags.
10012          * This is important for e.g. ARM_CP_SVE so that we still
10013          * take the SVE trap if CPTR_EL3.EZ == 0.
10014          */
10015         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
10016         /*
10017          * Usually, these registers become RES0, but there are a few
10018          * special cases like VPIDR_EL2 which have a constant non-zero
10019          * value with writes ignored.
10020          */
10021         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
10022             r2->resetvalue = 0;
10023         }
10024         /*
10025          * ARM_CP_CONST has precedence, so removing the callbacks and
10026          * offsets are not strictly necessary, but it is potentially
10027          * less confusing to debug later.
10028          */
10029         r2->readfn = NULL;
10030         r2->writefn = NULL;
10031         r2->raw_readfn = NULL;
10032         r2->raw_writefn = NULL;
10033         r2->resetfn = NULL;
10034         r2->fieldoffset = 0;
10035         r2->bank_fieldoffsets[0] = 0;
10036         r2->bank_fieldoffsets[1] = 0;
10037     } else {
10038         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
10039 
10040         if (isbanked) {
10041             /*
10042              * Register is banked (using both entries in array).
10043              * Overwriting fieldoffset as the array is only used to define
10044              * banked registers but later only fieldoffset is used.
10045              */
10046             r2->fieldoffset = r->bank_fieldoffsets[ns];
10047         }
10048         if (state == ARM_CP_STATE_AA32) {
10049             if (isbanked) {
10050                 /*
10051                  * If the register is banked then we don't need to migrate or
10052                  * reset the 32-bit instance in certain cases:
10053                  *
10054                  * 1) If the register has both 32-bit and 64-bit instances
10055                  *    then we can count on the 64-bit instance taking care
10056                  *    of the non-secure bank.
10057                  * 2) If ARMv8 is enabled then we can count on a 64-bit
10058                  *    version taking care of the secure bank.  This requires
10059                  *    that separate 32 and 64-bit definitions are provided.
10060                  */
10061                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
10062                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
10063                     r2->type |= ARM_CP_ALIAS;
10064                 }
10065             } else if ((secstate != r->secure) && !ns) {
10066                 /*
10067                  * The register is not banked so we only want to allow
10068                  * migration of the non-secure instance.
10069                  */
10070                 r2->type |= ARM_CP_ALIAS;
10071             }
10072 
10073             if (HOST_BIG_ENDIAN &&
10074                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
10075                 r2->fieldoffset += sizeof(uint32_t);
10076             }
10077         }
10078     }
10079 
10080     /*
10081      * By convention, for wildcarded registers only the first
10082      * entry is used for migration; the others are marked as
10083      * ALIAS so we don't try to transfer the register
10084      * multiple times. Special registers (ie NOP/WFI) are
10085      * never migratable and not even raw-accessible.
10086      */
10087     if (r2->type & ARM_CP_SPECIAL_MASK) {
10088         r2->type |= ARM_CP_NO_RAW;
10089     }
10090     if (((r->crm == CP_ANY) && crm != 0) ||
10091         ((r->opc1 == CP_ANY) && opc1 != 0) ||
10092         ((r->opc2 == CP_ANY) && opc2 != 0)) {
10093         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
10094     }
10095 
10096     /*
10097      * Check that raw accesses are either forbidden or handled. Note that
10098      * we can't assert this earlier because the setup of fieldoffset for
10099      * banked registers has to be done first.
10100      */
10101     if (!(r2->type & ARM_CP_NO_RAW)) {
10102         assert(!raw_accessors_invalid(r2));
10103     }
10104 
10105     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
10106 }
10107 
10108 
10109 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
10110                                        const ARMCPRegInfo *r, void *opaque)
10111 {
10112     /*
10113      * Define implementations of coprocessor registers.
10114      * We store these in a hashtable because typically
10115      * there are less than 150 registers in a space which
10116      * is 16*16*16*8*8 = 262144 in size.
10117      * Wildcarding is supported for the crm, opc1 and opc2 fields.
10118      * If a register is defined twice then the second definition is
10119      * used, so this can be used to define some generic registers and
10120      * then override them with implementation specific variations.
10121      * At least one of the original and the second definition should
10122      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
10123      * against accidental use.
10124      *
10125      * The state field defines whether the register is to be
10126      * visible in the AArch32 or AArch64 execution state. If the
10127      * state is set to ARM_CP_STATE_BOTH then we synthesise a
10128      * reginfo structure for the AArch32 view, which sees the lower
10129      * 32 bits of the 64 bit register.
10130      *
10131      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
10132      * be wildcarded. AArch64 registers are always considered to be 64
10133      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
10134      * the register, if any.
10135      */
10136     int crm, opc1, opc2;
10137     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
10138     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
10139     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
10140     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
10141     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
10142     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
10143     CPState state;
10144 
10145     /* 64 bit registers have only CRm and Opc1 fields */
10146     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
10147     /* op0 only exists in the AArch64 encodings */
10148     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
10149     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
10150     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
10151     /*
10152      * This API is only for Arm's system coprocessors (14 and 15) or
10153      * (M-profile or v7A-and-earlier only) for implementation defined
10154      * coprocessors in the range 0..7.  Our decode assumes this, since
10155      * 8..13 can be used for other insns including VFP and Neon. See
10156      * valid_cp() in translate.c.  Assert here that we haven't tried
10157      * to use an invalid coprocessor number.
10158      */
10159     switch (r->state) {
10160     case ARM_CP_STATE_BOTH:
10161         /* 0 has a special meaning, but otherwise the same rules as AA32. */
10162         if (r->cp == 0) {
10163             break;
10164         }
10165         /* fall through */
10166     case ARM_CP_STATE_AA32:
10167         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10168             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10169             assert(r->cp >= 14 && r->cp <= 15);
10170         } else {
10171             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10172         }
10173         break;
10174     case ARM_CP_STATE_AA64:
10175         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10176         break;
10177     default:
10178         g_assert_not_reached();
10179     }
10180     /*
10181      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10182      * encodes a minimum access level for the register. We roll this
10183      * runtime check into our general permission check code, so check
10184      * here that the reginfo's specified permissions are strict enough
10185      * to encompass the generic architectural permission check.
10186      */
10187     if (r->state != ARM_CP_STATE_AA32) {
10188         CPAccessRights mask;
10189         switch (r->opc1) {
10190         case 0:
10191             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10192             mask = PL0U_R | PL1_RW;
10193             break;
10194         case 1: case 2:
10195             /* min_EL EL1 */
10196             mask = PL1_RW;
10197             break;
10198         case 3:
10199             /* min_EL EL0 */
10200             mask = PL0_RW;
10201             break;
10202         case 4:
10203         case 5:
10204             /* min_EL EL2 */
10205             mask = PL2_RW;
10206             break;
10207         case 6:
10208             /* min_EL EL3 */
10209             mask = PL3_RW;
10210             break;
10211         case 7:
10212             /* min_EL EL1, secure mode only (we don't check the latter) */
10213             mask = PL1_RW;
10214             break;
10215         default:
10216             /* broken reginfo with out-of-range opc1 */
10217             g_assert_not_reached();
10218         }
10219         /* assert our permissions are not too lax (stricter is fine) */
10220         assert((r->access & ~mask) == 0);
10221     }
10222 
10223     /*
10224      * Check that the register definition has enough info to handle
10225      * reads and writes if they are permitted.
10226      */
10227     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10228         if (r->access & PL3_R) {
10229             assert((r->fieldoffset ||
10230                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10231                    r->readfn);
10232         }
10233         if (r->access & PL3_W) {
10234             assert((r->fieldoffset ||
10235                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10236                    r->writefn);
10237         }
10238     }
10239 
10240     for (crm = crmmin; crm <= crmmax; crm++) {
10241         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10242             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10243                 for (state = ARM_CP_STATE_AA32;
10244                      state <= ARM_CP_STATE_AA64; state++) {
10245                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10246                         continue;
10247                     }
10248                     if (state == ARM_CP_STATE_AA32) {
10249                         /*
10250                          * Under AArch32 CP registers can be common
10251                          * (same for secure and non-secure world) or banked.
10252                          */
10253                         char *name;
10254 
10255                         switch (r->secure) {
10256                         case ARM_CP_SECSTATE_S:
10257                         case ARM_CP_SECSTATE_NS:
10258                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10259                                                    r->secure, crm, opc1, opc2,
10260                                                    r->name);
10261                             break;
10262                         case ARM_CP_SECSTATE_BOTH:
10263                             name = g_strdup_printf("%s_S", r->name);
10264                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10265                                                    ARM_CP_SECSTATE_S,
10266                                                    crm, opc1, opc2, name);
10267                             g_free(name);
10268                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10269                                                    ARM_CP_SECSTATE_NS,
10270                                                    crm, opc1, opc2, r->name);
10271                             break;
10272                         default:
10273                             g_assert_not_reached();
10274                         }
10275                     } else {
10276                         /*
10277                          * AArch64 registers get mapped to non-secure instance
10278                          * of AArch32
10279                          */
10280                         add_cpreg_to_hashtable(cpu, r, opaque, state,
10281                                                ARM_CP_SECSTATE_NS,
10282                                                crm, opc1, opc2, r->name);
10283                     }
10284                 }
10285             }
10286         }
10287     }
10288 }
10289 
10290 /* Define a whole list of registers */
10291 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10292                                         void *opaque, size_t len)
10293 {
10294     size_t i;
10295     for (i = 0; i < len; ++i) {
10296         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10297     }
10298 }
10299 
10300 /*
10301  * Modify ARMCPRegInfo for access from userspace.
10302  *
10303  * This is a data driven modification directed by
10304  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10305  * user-space cannot alter any values and dynamic values pertaining to
10306  * execution state are hidden from user space view anyway.
10307  */
10308 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10309                                  const ARMCPRegUserSpaceInfo *mods,
10310                                  size_t mods_len)
10311 {
10312     for (size_t mi = 0; mi < mods_len; ++mi) {
10313         const ARMCPRegUserSpaceInfo *m = mods + mi;
10314         GPatternSpec *pat = NULL;
10315 
10316         if (m->is_glob) {
10317             pat = g_pattern_spec_new(m->name);
10318         }
10319         for (size_t ri = 0; ri < regs_len; ++ri) {
10320             ARMCPRegInfo *r = regs + ri;
10321 
10322             if (pat && g_pattern_match_string(pat, r->name)) {
10323                 r->type = ARM_CP_CONST;
10324                 r->access = PL0U_R;
10325                 r->resetvalue = 0;
10326                 /* continue */
10327             } else if (strcmp(r->name, m->name) == 0) {
10328                 r->type = ARM_CP_CONST;
10329                 r->access = PL0U_R;
10330                 r->resetvalue &= m->exported_bits;
10331                 r->resetvalue |= m->fixed_bits;
10332                 break;
10333             }
10334         }
10335         if (pat) {
10336             g_pattern_spec_free(pat);
10337         }
10338     }
10339 }
10340 
10341 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10342 {
10343     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10344 }
10345 
10346 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10347                          uint64_t value)
10348 {
10349     /* Helper coprocessor write function for write-ignore registers */
10350 }
10351 
10352 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10353 {
10354     /* Helper coprocessor write function for read-as-zero registers */
10355     return 0;
10356 }
10357 
10358 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10359 {
10360     /* Helper coprocessor reset function for do-nothing-on-reset registers */
10361 }
10362 
10363 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10364 {
10365     /*
10366      * Return true if it is not valid for us to switch to
10367      * this CPU mode (ie all the UNPREDICTABLE cases in
10368      * the ARM ARM CPSRWriteByInstr pseudocode).
10369      */
10370 
10371     /* Changes to or from Hyp via MSR and CPS are illegal. */
10372     if (write_type == CPSRWriteByInstr &&
10373         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10374          mode == ARM_CPU_MODE_HYP)) {
10375         return 1;
10376     }
10377 
10378     switch (mode) {
10379     case ARM_CPU_MODE_USR:
10380         return 0;
10381     case ARM_CPU_MODE_SYS:
10382     case ARM_CPU_MODE_SVC:
10383     case ARM_CPU_MODE_ABT:
10384     case ARM_CPU_MODE_UND:
10385     case ARM_CPU_MODE_IRQ:
10386     case ARM_CPU_MODE_FIQ:
10387         /*
10388          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10389          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10390          */
10391         /*
10392          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10393          * and CPS are treated as illegal mode changes.
10394          */
10395         if (write_type == CPSRWriteByInstr &&
10396             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10397             (arm_hcr_el2_eff(env) & HCR_TGE)) {
10398             return 1;
10399         }
10400         return 0;
10401     case ARM_CPU_MODE_HYP:
10402         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10403     case ARM_CPU_MODE_MON:
10404         return arm_current_el(env) < 3;
10405     default:
10406         return 1;
10407     }
10408 }
10409 
10410 uint32_t cpsr_read(CPUARMState *env)
10411 {
10412     int ZF;
10413     ZF = (env->ZF == 0);
10414     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10415         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10416         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10417         | ((env->condexec_bits & 0xfc) << 8)
10418         | (env->GE << 16) | (env->daif & CPSR_AIF);
10419 }
10420 
10421 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10422                 CPSRWriteType write_type)
10423 {
10424     uint32_t changed_daif;
10425     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10426         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10427 
10428     if (mask & CPSR_NZCV) {
10429         env->ZF = (~val) & CPSR_Z;
10430         env->NF = val;
10431         env->CF = (val >> 29) & 1;
10432         env->VF = (val << 3) & 0x80000000;
10433     }
10434     if (mask & CPSR_Q) {
10435         env->QF = ((val & CPSR_Q) != 0);
10436     }
10437     if (mask & CPSR_T) {
10438         env->thumb = ((val & CPSR_T) != 0);
10439     }
10440     if (mask & CPSR_IT_0_1) {
10441         env->condexec_bits &= ~3;
10442         env->condexec_bits |= (val >> 25) & 3;
10443     }
10444     if (mask & CPSR_IT_2_7) {
10445         env->condexec_bits &= 3;
10446         env->condexec_bits |= (val >> 8) & 0xfc;
10447     }
10448     if (mask & CPSR_GE) {
10449         env->GE = (val >> 16) & 0xf;
10450     }
10451 
10452     /*
10453      * In a V7 implementation that includes the security extensions but does
10454      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10455      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10456      * bits respectively.
10457      *
10458      * In a V8 implementation, it is permitted for privileged software to
10459      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10460      */
10461     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10462         arm_feature(env, ARM_FEATURE_EL3) &&
10463         !arm_feature(env, ARM_FEATURE_EL2) &&
10464         !arm_is_secure(env)) {
10465 
10466         changed_daif = (env->daif ^ val) & mask;
10467 
10468         if (changed_daif & CPSR_A) {
10469             /*
10470              * Check to see if we are allowed to change the masking of async
10471              * abort exceptions from a non-secure state.
10472              */
10473             if (!(env->cp15.scr_el3 & SCR_AW)) {
10474                 qemu_log_mask(LOG_GUEST_ERROR,
10475                               "Ignoring attempt to switch CPSR_A flag from "
10476                               "non-secure world with SCR.AW bit clear\n");
10477                 mask &= ~CPSR_A;
10478             }
10479         }
10480 
10481         if (changed_daif & CPSR_F) {
10482             /*
10483              * Check to see if we are allowed to change the masking of FIQ
10484              * exceptions from a non-secure state.
10485              */
10486             if (!(env->cp15.scr_el3 & SCR_FW)) {
10487                 qemu_log_mask(LOG_GUEST_ERROR,
10488                               "Ignoring attempt to switch CPSR_F flag from "
10489                               "non-secure world with SCR.FW bit clear\n");
10490                 mask &= ~CPSR_F;
10491             }
10492 
10493             /*
10494              * Check whether non-maskable FIQ (NMFI) support is enabled.
10495              * If this bit is set software is not allowed to mask
10496              * FIQs, but is allowed to set CPSR_F to 0.
10497              */
10498             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10499                 (val & CPSR_F)) {
10500                 qemu_log_mask(LOG_GUEST_ERROR,
10501                               "Ignoring attempt to enable CPSR_F flag "
10502                               "(non-maskable FIQ [NMFI] support enabled)\n");
10503                 mask &= ~CPSR_F;
10504             }
10505         }
10506     }
10507 
10508     env->daif &= ~(CPSR_AIF & mask);
10509     env->daif |= val & CPSR_AIF & mask;
10510 
10511     if (write_type != CPSRWriteRaw &&
10512         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10513         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10514             /*
10515              * Note that we can only get here in USR mode if this is a
10516              * gdb stub write; for this case we follow the architectural
10517              * behaviour for guest writes in USR mode of ignoring an attempt
10518              * to switch mode. (Those are caught by translate.c for writes
10519              * triggered by guest instructions.)
10520              */
10521             mask &= ~CPSR_M;
10522         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10523             /*
10524              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10525              * v7, and has defined behaviour in v8:
10526              *  + leave CPSR.M untouched
10527              *  + allow changes to the other CPSR fields
10528              *  + set PSTATE.IL
10529              * For user changes via the GDB stub, we don't set PSTATE.IL,
10530              * as this would be unnecessarily harsh for a user error.
10531              */
10532             mask &= ~CPSR_M;
10533             if (write_type != CPSRWriteByGDBStub &&
10534                 arm_feature(env, ARM_FEATURE_V8)) {
10535                 mask |= CPSR_IL;
10536                 val |= CPSR_IL;
10537             }
10538             qemu_log_mask(LOG_GUEST_ERROR,
10539                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10540                           aarch32_mode_name(env->uncached_cpsr),
10541                           aarch32_mode_name(val));
10542         } else {
10543             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10544                           write_type == CPSRWriteExceptionReturn ?
10545                           "Exception return from AArch32" :
10546                           "AArch32 mode switch from",
10547                           aarch32_mode_name(env->uncached_cpsr),
10548                           aarch32_mode_name(val), env->regs[15]);
10549             switch_mode(env, val & CPSR_M);
10550         }
10551     }
10552     mask &= ~CACHED_CPSR_BITS;
10553     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10554     if (tcg_enabled() && rebuild_hflags) {
10555         arm_rebuild_hflags(env);
10556     }
10557 }
10558 
10559 #ifdef CONFIG_USER_ONLY
10560 
10561 static void switch_mode(CPUARMState *env, int mode)
10562 {
10563     ARMCPU *cpu = env_archcpu(env);
10564 
10565     if (mode != ARM_CPU_MODE_USR) {
10566         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10567     }
10568 }
10569 
10570 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10571                                  uint32_t cur_el, bool secure)
10572 {
10573     return 1;
10574 }
10575 
10576 void aarch64_sync_64_to_32(CPUARMState *env)
10577 {
10578     g_assert_not_reached();
10579 }
10580 
10581 #else
10582 
10583 static void switch_mode(CPUARMState *env, int mode)
10584 {
10585     int old_mode;
10586     int i;
10587 
10588     old_mode = env->uncached_cpsr & CPSR_M;
10589     if (mode == old_mode) {
10590         return;
10591     }
10592 
10593     if (old_mode == ARM_CPU_MODE_FIQ) {
10594         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10595         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10596     } else if (mode == ARM_CPU_MODE_FIQ) {
10597         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10598         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10599     }
10600 
10601     i = bank_number(old_mode);
10602     env->banked_r13[i] = env->regs[13];
10603     env->banked_spsr[i] = env->spsr;
10604 
10605     i = bank_number(mode);
10606     env->regs[13] = env->banked_r13[i];
10607     env->spsr = env->banked_spsr[i];
10608 
10609     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10610     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10611 }
10612 
10613 /*
10614  * Physical Interrupt Target EL Lookup Table
10615  *
10616  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10617  *
10618  * The below multi-dimensional table is used for looking up the target
10619  * exception level given numerous condition criteria.  Specifically, the
10620  * target EL is based on SCR and HCR routing controls as well as the
10621  * currently executing EL and secure state.
10622  *
10623  *    Dimensions:
10624  *    target_el_table[2][2][2][2][2][4]
10625  *                    |  |  |  |  |  +--- Current EL
10626  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10627  *                    |  |  |  +--------- HCR mask override
10628  *                    |  |  +------------ SCR exec state control
10629  *                    |  +--------------- SCR mask override
10630  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10631  *
10632  *    The table values are as such:
10633  *    0-3 = EL0-EL3
10634  *     -1 = Cannot occur
10635  *
10636  * The ARM ARM target EL table includes entries indicating that an "exception
10637  * is not taken".  The two cases where this is applicable are:
10638  *    1) An exception is taken from EL3 but the SCR does not have the exception
10639  *    routed to EL3.
10640  *    2) An exception is taken from EL2 but the HCR does not have the exception
10641  *    routed to EL2.
10642  * In these two cases, the below table contain a target of EL1.  This value is
10643  * returned as it is expected that the consumer of the table data will check
10644  * for "target EL >= current EL" to ensure the exception is not taken.
10645  *
10646  *            SCR     HCR
10647  *         64  EA     AMO                 From
10648  *        BIT IRQ     IMO      Non-secure         Secure
10649  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10650  */
10651 static const int8_t target_el_table[2][2][2][2][2][4] = {
10652     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10653        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10654       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10655        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10656      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10657        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10658       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10659        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10660     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10661        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10662       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10663        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10664      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10665        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10666       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10667        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10668 };
10669 
10670 /*
10671  * Determine the target EL for physical exceptions
10672  */
10673 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10674                                  uint32_t cur_el, bool secure)
10675 {
10676     CPUARMState *env = cpu_env(cs);
10677     bool rw;
10678     bool scr;
10679     bool hcr;
10680     int target_el;
10681     /* Is the highest EL AArch64? */
10682     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10683     uint64_t hcr_el2;
10684 
10685     if (arm_feature(env, ARM_FEATURE_EL3)) {
10686         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10687     } else {
10688         /*
10689          * Either EL2 is the highest EL (and so the EL2 register width
10690          * is given by is64); or there is no EL2 or EL3, in which case
10691          * the value of 'rw' does not affect the table lookup anyway.
10692          */
10693         rw = is64;
10694     }
10695 
10696     hcr_el2 = arm_hcr_el2_eff(env);
10697     switch (excp_idx) {
10698     case EXCP_IRQ:
10699         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10700         hcr = hcr_el2 & HCR_IMO;
10701         break;
10702     case EXCP_FIQ:
10703         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10704         hcr = hcr_el2 & HCR_FMO;
10705         break;
10706     default:
10707         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10708         hcr = hcr_el2 & HCR_AMO;
10709         break;
10710     };
10711 
10712     /*
10713      * For these purposes, TGE and AMO/IMO/FMO both force the
10714      * interrupt to EL2.  Fold TGE into the bit extracted above.
10715      */
10716     hcr |= (hcr_el2 & HCR_TGE) != 0;
10717 
10718     /* Perform a table-lookup for the target EL given the current state */
10719     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10720 
10721     assert(target_el > 0);
10722 
10723     return target_el;
10724 }
10725 
10726 void arm_log_exception(CPUState *cs)
10727 {
10728     int idx = cs->exception_index;
10729 
10730     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10731         const char *exc = NULL;
10732         static const char * const excnames[] = {
10733             [EXCP_UDEF] = "Undefined Instruction",
10734             [EXCP_SWI] = "SVC",
10735             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10736             [EXCP_DATA_ABORT] = "Data Abort",
10737             [EXCP_IRQ] = "IRQ",
10738             [EXCP_FIQ] = "FIQ",
10739             [EXCP_BKPT] = "Breakpoint",
10740             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10741             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10742             [EXCP_HVC] = "Hypervisor Call",
10743             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10744             [EXCP_SMC] = "Secure Monitor Call",
10745             [EXCP_VIRQ] = "Virtual IRQ",
10746             [EXCP_VFIQ] = "Virtual FIQ",
10747             [EXCP_SEMIHOST] = "Semihosting call",
10748             [EXCP_NOCP] = "v7M NOCP UsageFault",
10749             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10750             [EXCP_STKOF] = "v8M STKOF UsageFault",
10751             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10752             [EXCP_LSERR] = "v8M LSERR UsageFault",
10753             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10754             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10755             [EXCP_VSERR] = "Virtual SERR",
10756             [EXCP_GPC] = "Granule Protection Check",
10757         };
10758 
10759         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10760             exc = excnames[idx];
10761         }
10762         if (!exc) {
10763             exc = "unknown";
10764         }
10765         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10766                       idx, exc, cs->cpu_index);
10767     }
10768 }
10769 
10770 /*
10771  * Function used to synchronize QEMU's AArch64 register set with AArch32
10772  * register set.  This is necessary when switching between AArch32 and AArch64
10773  * execution state.
10774  */
10775 void aarch64_sync_32_to_64(CPUARMState *env)
10776 {
10777     int i;
10778     uint32_t mode = env->uncached_cpsr & CPSR_M;
10779 
10780     /* We can blanket copy R[0:7] to X[0:7] */
10781     for (i = 0; i < 8; i++) {
10782         env->xregs[i] = env->regs[i];
10783     }
10784 
10785     /*
10786      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10787      * Otherwise, they come from the banked user regs.
10788      */
10789     if (mode == ARM_CPU_MODE_FIQ) {
10790         for (i = 8; i < 13; i++) {
10791             env->xregs[i] = env->usr_regs[i - 8];
10792         }
10793     } else {
10794         for (i = 8; i < 13; i++) {
10795             env->xregs[i] = env->regs[i];
10796         }
10797     }
10798 
10799     /*
10800      * Registers x13-x23 are the various mode SP and FP registers. Registers
10801      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10802      * from the mode banked register.
10803      */
10804     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10805         env->xregs[13] = env->regs[13];
10806         env->xregs[14] = env->regs[14];
10807     } else {
10808         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10809         /* HYP is an exception in that it is copied from r14 */
10810         if (mode == ARM_CPU_MODE_HYP) {
10811             env->xregs[14] = env->regs[14];
10812         } else {
10813             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10814         }
10815     }
10816 
10817     if (mode == ARM_CPU_MODE_HYP) {
10818         env->xregs[15] = env->regs[13];
10819     } else {
10820         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10821     }
10822 
10823     if (mode == ARM_CPU_MODE_IRQ) {
10824         env->xregs[16] = env->regs[14];
10825         env->xregs[17] = env->regs[13];
10826     } else {
10827         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10828         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10829     }
10830 
10831     if (mode == ARM_CPU_MODE_SVC) {
10832         env->xregs[18] = env->regs[14];
10833         env->xregs[19] = env->regs[13];
10834     } else {
10835         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10836         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10837     }
10838 
10839     if (mode == ARM_CPU_MODE_ABT) {
10840         env->xregs[20] = env->regs[14];
10841         env->xregs[21] = env->regs[13];
10842     } else {
10843         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10844         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10845     }
10846 
10847     if (mode == ARM_CPU_MODE_UND) {
10848         env->xregs[22] = env->regs[14];
10849         env->xregs[23] = env->regs[13];
10850     } else {
10851         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10852         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10853     }
10854 
10855     /*
10856      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10857      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10858      * FIQ bank for r8-r14.
10859      */
10860     if (mode == ARM_CPU_MODE_FIQ) {
10861         for (i = 24; i < 31; i++) {
10862             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10863         }
10864     } else {
10865         for (i = 24; i < 29; i++) {
10866             env->xregs[i] = env->fiq_regs[i - 24];
10867         }
10868         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10869         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10870     }
10871 
10872     env->pc = env->regs[15];
10873 }
10874 
10875 /*
10876  * Function used to synchronize QEMU's AArch32 register set with AArch64
10877  * register set.  This is necessary when switching between AArch32 and AArch64
10878  * execution state.
10879  */
10880 void aarch64_sync_64_to_32(CPUARMState *env)
10881 {
10882     int i;
10883     uint32_t mode = env->uncached_cpsr & CPSR_M;
10884 
10885     /* We can blanket copy X[0:7] to R[0:7] */
10886     for (i = 0; i < 8; i++) {
10887         env->regs[i] = env->xregs[i];
10888     }
10889 
10890     /*
10891      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10892      * Otherwise, we copy x8-x12 into the banked user regs.
10893      */
10894     if (mode == ARM_CPU_MODE_FIQ) {
10895         for (i = 8; i < 13; i++) {
10896             env->usr_regs[i - 8] = env->xregs[i];
10897         }
10898     } else {
10899         for (i = 8; i < 13; i++) {
10900             env->regs[i] = env->xregs[i];
10901         }
10902     }
10903 
10904     /*
10905      * Registers r13 & r14 depend on the current mode.
10906      * If we are in a given mode, we copy the corresponding x registers to r13
10907      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10908      * for the mode.
10909      */
10910     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10911         env->regs[13] = env->xregs[13];
10912         env->regs[14] = env->xregs[14];
10913     } else {
10914         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10915 
10916         /*
10917          * HYP is an exception in that it does not have its own banked r14 but
10918          * shares the USR r14
10919          */
10920         if (mode == ARM_CPU_MODE_HYP) {
10921             env->regs[14] = env->xregs[14];
10922         } else {
10923             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10924         }
10925     }
10926 
10927     if (mode == ARM_CPU_MODE_HYP) {
10928         env->regs[13] = env->xregs[15];
10929     } else {
10930         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10931     }
10932 
10933     if (mode == ARM_CPU_MODE_IRQ) {
10934         env->regs[14] = env->xregs[16];
10935         env->regs[13] = env->xregs[17];
10936     } else {
10937         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10938         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10939     }
10940 
10941     if (mode == ARM_CPU_MODE_SVC) {
10942         env->regs[14] = env->xregs[18];
10943         env->regs[13] = env->xregs[19];
10944     } else {
10945         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10946         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10947     }
10948 
10949     if (mode == ARM_CPU_MODE_ABT) {
10950         env->regs[14] = env->xregs[20];
10951         env->regs[13] = env->xregs[21];
10952     } else {
10953         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10954         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10955     }
10956 
10957     if (mode == ARM_CPU_MODE_UND) {
10958         env->regs[14] = env->xregs[22];
10959         env->regs[13] = env->xregs[23];
10960     } else {
10961         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10962         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10963     }
10964 
10965     /*
10966      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10967      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10968      * FIQ bank for r8-r14.
10969      */
10970     if (mode == ARM_CPU_MODE_FIQ) {
10971         for (i = 24; i < 31; i++) {
10972             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10973         }
10974     } else {
10975         for (i = 24; i < 29; i++) {
10976             env->fiq_regs[i - 24] = env->xregs[i];
10977         }
10978         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10979         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10980     }
10981 
10982     env->regs[15] = env->pc;
10983 }
10984 
10985 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10986                                    uint32_t mask, uint32_t offset,
10987                                    uint32_t newpc)
10988 {
10989     int new_el;
10990 
10991     /* Change the CPU state so as to actually take the exception. */
10992     switch_mode(env, new_mode);
10993 
10994     /*
10995      * For exceptions taken to AArch32 we must clear the SS bit in both
10996      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10997      */
10998     env->pstate &= ~PSTATE_SS;
10999     env->spsr = cpsr_read(env);
11000     /* Clear IT bits.  */
11001     env->condexec_bits = 0;
11002     /* Switch to the new mode, and to the correct instruction set.  */
11003     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
11004 
11005     /* This must be after mode switching. */
11006     new_el = arm_current_el(env);
11007 
11008     /* Set new mode endianness */
11009     env->uncached_cpsr &= ~CPSR_E;
11010     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
11011         env->uncached_cpsr |= CPSR_E;
11012     }
11013     /* J and IL must always be cleared for exception entry */
11014     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
11015     env->daif |= mask;
11016 
11017     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
11018         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
11019             env->uncached_cpsr |= CPSR_SSBS;
11020         } else {
11021             env->uncached_cpsr &= ~CPSR_SSBS;
11022         }
11023     }
11024 
11025     if (new_mode == ARM_CPU_MODE_HYP) {
11026         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
11027         env->elr_el[2] = env->regs[15];
11028     } else {
11029         /* CPSR.PAN is normally preserved preserved unless...  */
11030         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
11031             switch (new_el) {
11032             case 3:
11033                 if (!arm_is_secure_below_el3(env)) {
11034                     /* ... the target is EL3, from non-secure state.  */
11035                     env->uncached_cpsr &= ~CPSR_PAN;
11036                     break;
11037                 }
11038                 /* ... the target is EL3, from secure state ... */
11039                 /* fall through */
11040             case 1:
11041                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
11042                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
11043                     env->uncached_cpsr |= CPSR_PAN;
11044                 }
11045                 break;
11046             }
11047         }
11048         /*
11049          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
11050          * and we should just guard the thumb mode on V4
11051          */
11052         if (arm_feature(env, ARM_FEATURE_V4T)) {
11053             env->thumb =
11054                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
11055         }
11056         env->regs[14] = env->regs[15] + offset;
11057     }
11058     env->regs[15] = newpc;
11059 
11060     if (tcg_enabled()) {
11061         arm_rebuild_hflags(env);
11062     }
11063 }
11064 
11065 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
11066 {
11067     /*
11068      * Handle exception entry to Hyp mode; this is sufficiently
11069      * different to entry to other AArch32 modes that we handle it
11070      * separately here.
11071      *
11072      * The vector table entry used is always the 0x14 Hyp mode entry point,
11073      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
11074      * The offset applied to the preferred return address is always zero
11075      * (see DDI0487C.a section G1.12.3).
11076      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
11077      */
11078     uint32_t addr, mask;
11079     ARMCPU *cpu = ARM_CPU(cs);
11080     CPUARMState *env = &cpu->env;
11081 
11082     switch (cs->exception_index) {
11083     case EXCP_UDEF:
11084         addr = 0x04;
11085         break;
11086     case EXCP_SWI:
11087         addr = 0x08;
11088         break;
11089     case EXCP_BKPT:
11090         /* Fall through to prefetch abort.  */
11091     case EXCP_PREFETCH_ABORT:
11092         env->cp15.ifar_s = env->exception.vaddress;
11093         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
11094                       (uint32_t)env->exception.vaddress);
11095         addr = 0x0c;
11096         break;
11097     case EXCP_DATA_ABORT:
11098         env->cp15.dfar_s = env->exception.vaddress;
11099         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
11100                       (uint32_t)env->exception.vaddress);
11101         addr = 0x10;
11102         break;
11103     case EXCP_IRQ:
11104         addr = 0x18;
11105         break;
11106     case EXCP_FIQ:
11107         addr = 0x1c;
11108         break;
11109     case EXCP_HVC:
11110         addr = 0x08;
11111         break;
11112     case EXCP_HYP_TRAP:
11113         addr = 0x14;
11114         break;
11115     default:
11116         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11117     }
11118 
11119     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
11120         if (!arm_feature(env, ARM_FEATURE_V8)) {
11121             /*
11122              * QEMU syndrome values are v8-style. v7 has the IL bit
11123              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
11124              * If this is a v7 CPU, squash the IL bit in those cases.
11125              */
11126             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
11127                 (cs->exception_index == EXCP_DATA_ABORT &&
11128                  !(env->exception.syndrome & ARM_EL_ISV)) ||
11129                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
11130                 env->exception.syndrome &= ~ARM_EL_IL;
11131             }
11132         }
11133         env->cp15.esr_el[2] = env->exception.syndrome;
11134     }
11135 
11136     if (arm_current_el(env) != 2 && addr < 0x14) {
11137         addr = 0x14;
11138     }
11139 
11140     mask = 0;
11141     if (!(env->cp15.scr_el3 & SCR_EA)) {
11142         mask |= CPSR_A;
11143     }
11144     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
11145         mask |= CPSR_I;
11146     }
11147     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
11148         mask |= CPSR_F;
11149     }
11150 
11151     addr += env->cp15.hvbar;
11152 
11153     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
11154 }
11155 
11156 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
11157 {
11158     ARMCPU *cpu = ARM_CPU(cs);
11159     CPUARMState *env = &cpu->env;
11160     uint32_t addr;
11161     uint32_t mask;
11162     int new_mode;
11163     uint32_t offset;
11164     uint32_t moe;
11165 
11166     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11167     switch (syn_get_ec(env->exception.syndrome)) {
11168     case EC_BREAKPOINT:
11169     case EC_BREAKPOINT_SAME_EL:
11170         moe = 1;
11171         break;
11172     case EC_WATCHPOINT:
11173     case EC_WATCHPOINT_SAME_EL:
11174         moe = 10;
11175         break;
11176     case EC_AA32_BKPT:
11177         moe = 3;
11178         break;
11179     case EC_VECTORCATCH:
11180         moe = 5;
11181         break;
11182     default:
11183         moe = 0;
11184         break;
11185     }
11186 
11187     if (moe) {
11188         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11189     }
11190 
11191     if (env->exception.target_el == 2) {
11192         /* Debug exceptions are reported differently on AArch32 */
11193         switch (syn_get_ec(env->exception.syndrome)) {
11194         case EC_BREAKPOINT:
11195         case EC_BREAKPOINT_SAME_EL:
11196         case EC_AA32_BKPT:
11197         case EC_VECTORCATCH:
11198             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11199                                                      0, 0, 0x22);
11200             break;
11201         case EC_WATCHPOINT:
11202             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11203                                                  EC_DATAABORT);
11204             break;
11205         case EC_WATCHPOINT_SAME_EL:
11206             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11207                                                  EC_DATAABORT_SAME_EL);
11208             break;
11209         }
11210         arm_cpu_do_interrupt_aarch32_hyp(cs);
11211         return;
11212     }
11213 
11214     switch (cs->exception_index) {
11215     case EXCP_UDEF:
11216         new_mode = ARM_CPU_MODE_UND;
11217         addr = 0x04;
11218         mask = CPSR_I;
11219         if (env->thumb) {
11220             offset = 2;
11221         } else {
11222             offset = 4;
11223         }
11224         break;
11225     case EXCP_SWI:
11226         new_mode = ARM_CPU_MODE_SVC;
11227         addr = 0x08;
11228         mask = CPSR_I;
11229         /* The PC already points to the next instruction.  */
11230         offset = 0;
11231         break;
11232     case EXCP_BKPT:
11233         /* Fall through to prefetch abort.  */
11234     case EXCP_PREFETCH_ABORT:
11235         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11236         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11237         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11238                       env->exception.fsr, (uint32_t)env->exception.vaddress);
11239         new_mode = ARM_CPU_MODE_ABT;
11240         addr = 0x0c;
11241         mask = CPSR_A | CPSR_I;
11242         offset = 4;
11243         break;
11244     case EXCP_DATA_ABORT:
11245         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11246         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11247         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11248                       env->exception.fsr,
11249                       (uint32_t)env->exception.vaddress);
11250         new_mode = ARM_CPU_MODE_ABT;
11251         addr = 0x10;
11252         mask = CPSR_A | CPSR_I;
11253         offset = 8;
11254         break;
11255     case EXCP_IRQ:
11256         new_mode = ARM_CPU_MODE_IRQ;
11257         addr = 0x18;
11258         /* Disable IRQ and imprecise data aborts.  */
11259         mask = CPSR_A | CPSR_I;
11260         offset = 4;
11261         if (env->cp15.scr_el3 & SCR_IRQ) {
11262             /* IRQ routed to monitor mode */
11263             new_mode = ARM_CPU_MODE_MON;
11264             mask |= CPSR_F;
11265         }
11266         break;
11267     case EXCP_FIQ:
11268         new_mode = ARM_CPU_MODE_FIQ;
11269         addr = 0x1c;
11270         /* Disable FIQ, IRQ and imprecise data aborts.  */
11271         mask = CPSR_A | CPSR_I | CPSR_F;
11272         if (env->cp15.scr_el3 & SCR_FIQ) {
11273             /* FIQ routed to monitor mode */
11274             new_mode = ARM_CPU_MODE_MON;
11275         }
11276         offset = 4;
11277         break;
11278     case EXCP_VIRQ:
11279         new_mode = ARM_CPU_MODE_IRQ;
11280         addr = 0x18;
11281         /* Disable IRQ and imprecise data aborts.  */
11282         mask = CPSR_A | CPSR_I;
11283         offset = 4;
11284         break;
11285     case EXCP_VFIQ:
11286         new_mode = ARM_CPU_MODE_FIQ;
11287         addr = 0x1c;
11288         /* Disable FIQ, IRQ and imprecise data aborts.  */
11289         mask = CPSR_A | CPSR_I | CPSR_F;
11290         offset = 4;
11291         break;
11292     case EXCP_VSERR:
11293         {
11294             /*
11295              * Note that this is reported as a data abort, but the DFAR
11296              * has an UNKNOWN value.  Construct the SError syndrome from
11297              * AET and ExT fields.
11298              */
11299             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11300 
11301             if (extended_addresses_enabled(env)) {
11302                 env->exception.fsr = arm_fi_to_lfsc(&fi);
11303             } else {
11304                 env->exception.fsr = arm_fi_to_sfsc(&fi);
11305             }
11306             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11307             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11308             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11309                           env->exception.fsr);
11310 
11311             new_mode = ARM_CPU_MODE_ABT;
11312             addr = 0x10;
11313             mask = CPSR_A | CPSR_I;
11314             offset = 8;
11315         }
11316         break;
11317     case EXCP_SMC:
11318         new_mode = ARM_CPU_MODE_MON;
11319         addr = 0x08;
11320         mask = CPSR_A | CPSR_I | CPSR_F;
11321         offset = 0;
11322         break;
11323     default:
11324         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11325         return; /* Never happens.  Keep compiler happy.  */
11326     }
11327 
11328     if (new_mode == ARM_CPU_MODE_MON) {
11329         addr += env->cp15.mvbar;
11330     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11331         /* High vectors. When enabled, base address cannot be remapped. */
11332         addr += 0xffff0000;
11333     } else {
11334         /*
11335          * ARM v7 architectures provide a vector base address register to remap
11336          * the interrupt vector table.
11337          * This register is only followed in non-monitor mode, and is banked.
11338          * Note: only bits 31:5 are valid.
11339          */
11340         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11341     }
11342 
11343     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11344         env->cp15.scr_el3 &= ~SCR_NS;
11345     }
11346 
11347     take_aarch32_exception(env, new_mode, mask, offset, addr);
11348 }
11349 
11350 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11351 {
11352     /*
11353      * Return the register number of the AArch64 view of the AArch32
11354      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11355      * be that of the AArch32 mode the exception came from.
11356      */
11357     int mode = env->uncached_cpsr & CPSR_M;
11358 
11359     switch (aarch32_reg) {
11360     case 0 ... 7:
11361         return aarch32_reg;
11362     case 8 ... 12:
11363         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11364     case 13:
11365         switch (mode) {
11366         case ARM_CPU_MODE_USR:
11367         case ARM_CPU_MODE_SYS:
11368             return 13;
11369         case ARM_CPU_MODE_HYP:
11370             return 15;
11371         case ARM_CPU_MODE_IRQ:
11372             return 17;
11373         case ARM_CPU_MODE_SVC:
11374             return 19;
11375         case ARM_CPU_MODE_ABT:
11376             return 21;
11377         case ARM_CPU_MODE_UND:
11378             return 23;
11379         case ARM_CPU_MODE_FIQ:
11380             return 29;
11381         default:
11382             g_assert_not_reached();
11383         }
11384     case 14:
11385         switch (mode) {
11386         case ARM_CPU_MODE_USR:
11387         case ARM_CPU_MODE_SYS:
11388         case ARM_CPU_MODE_HYP:
11389             return 14;
11390         case ARM_CPU_MODE_IRQ:
11391             return 16;
11392         case ARM_CPU_MODE_SVC:
11393             return 18;
11394         case ARM_CPU_MODE_ABT:
11395             return 20;
11396         case ARM_CPU_MODE_UND:
11397             return 22;
11398         case ARM_CPU_MODE_FIQ:
11399             return 30;
11400         default:
11401             g_assert_not_reached();
11402         }
11403     case 15:
11404         return 31;
11405     default:
11406         g_assert_not_reached();
11407     }
11408 }
11409 
11410 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11411 {
11412     uint32_t ret = cpsr_read(env);
11413 
11414     /* Move DIT to the correct location for SPSR_ELx */
11415     if (ret & CPSR_DIT) {
11416         ret &= ~CPSR_DIT;
11417         ret |= PSTATE_DIT;
11418     }
11419     /* Merge PSTATE.SS into SPSR_ELx */
11420     ret |= env->pstate & PSTATE_SS;
11421 
11422     return ret;
11423 }
11424 
11425 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11426 {
11427     /* Return true if this syndrome value is a synchronous external abort */
11428     switch (syn_get_ec(syndrome)) {
11429     case EC_INSNABORT:
11430     case EC_INSNABORT_SAME_EL:
11431     case EC_DATAABORT:
11432     case EC_DATAABORT_SAME_EL:
11433         /* Look at fault status code for all the synchronous ext abort cases */
11434         switch (syndrome & 0x3f) {
11435         case 0x10:
11436         case 0x13:
11437         case 0x14:
11438         case 0x15:
11439         case 0x16:
11440         case 0x17:
11441             return true;
11442         default:
11443             return false;
11444         }
11445     default:
11446         return false;
11447     }
11448 }
11449 
11450 /* Handle exception entry to a target EL which is using AArch64 */
11451 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11452 {
11453     ARMCPU *cpu = ARM_CPU(cs);
11454     CPUARMState *env = &cpu->env;
11455     unsigned int new_el = env->exception.target_el;
11456     target_ulong addr = env->cp15.vbar_el[new_el];
11457     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11458     unsigned int old_mode;
11459     unsigned int cur_el = arm_current_el(env);
11460     int rt;
11461 
11462     if (tcg_enabled()) {
11463         /*
11464          * Note that new_el can never be 0.  If cur_el is 0, then
11465          * el0_a64 is is_a64(), else el0_a64 is ignored.
11466          */
11467         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11468     }
11469 
11470     if (cur_el < new_el) {
11471         /*
11472          * Entry vector offset depends on whether the implemented EL
11473          * immediately lower than the target level is using AArch32 or AArch64
11474          */
11475         bool is_aa64;
11476         uint64_t hcr;
11477 
11478         switch (new_el) {
11479         case 3:
11480             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11481             break;
11482         case 2:
11483             hcr = arm_hcr_el2_eff(env);
11484             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11485                 is_aa64 = (hcr & HCR_RW) != 0;
11486                 break;
11487             }
11488             /* fall through */
11489         case 1:
11490             is_aa64 = is_a64(env);
11491             break;
11492         default:
11493             g_assert_not_reached();
11494         }
11495 
11496         if (is_aa64) {
11497             addr += 0x400;
11498         } else {
11499             addr += 0x600;
11500         }
11501     } else if (pstate_read(env) & PSTATE_SP) {
11502         addr += 0x200;
11503     }
11504 
11505     switch (cs->exception_index) {
11506     case EXCP_GPC:
11507         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11508                       env->cp15.mfar_el3);
11509         /* fall through */
11510     case EXCP_PREFETCH_ABORT:
11511     case EXCP_DATA_ABORT:
11512         /*
11513          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11514          * to be taken to the SError vector entrypoint.
11515          */
11516         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11517             syndrome_is_sync_extabt(env->exception.syndrome)) {
11518             addr += 0x180;
11519         }
11520         env->cp15.far_el[new_el] = env->exception.vaddress;
11521         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11522                       env->cp15.far_el[new_el]);
11523         /* fall through */
11524     case EXCP_BKPT:
11525     case EXCP_UDEF:
11526     case EXCP_SWI:
11527     case EXCP_HVC:
11528     case EXCP_HYP_TRAP:
11529     case EXCP_SMC:
11530         switch (syn_get_ec(env->exception.syndrome)) {
11531         case EC_ADVSIMDFPACCESSTRAP:
11532             /*
11533              * QEMU internal FP/SIMD syndromes from AArch32 include the
11534              * TA and coproc fields which are only exposed if the exception
11535              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11536              * AArch64 format syndrome.
11537              */
11538             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11539             break;
11540         case EC_CP14RTTRAP:
11541         case EC_CP15RTTRAP:
11542         case EC_CP14DTTRAP:
11543             /*
11544              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11545              * the raw register field from the insn; when taking this to
11546              * AArch64 we must convert it to the AArch64 view of the register
11547              * number. Notice that we read a 4-bit AArch32 register number and
11548              * write back a 5-bit AArch64 one.
11549              */
11550             rt = extract32(env->exception.syndrome, 5, 4);
11551             rt = aarch64_regnum(env, rt);
11552             env->exception.syndrome = deposit32(env->exception.syndrome,
11553                                                 5, 5, rt);
11554             break;
11555         case EC_CP15RRTTRAP:
11556         case EC_CP14RRTTRAP:
11557             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11558             rt = extract32(env->exception.syndrome, 5, 4);
11559             rt = aarch64_regnum(env, rt);
11560             env->exception.syndrome = deposit32(env->exception.syndrome,
11561                                                 5, 5, rt);
11562             rt = extract32(env->exception.syndrome, 10, 4);
11563             rt = aarch64_regnum(env, rt);
11564             env->exception.syndrome = deposit32(env->exception.syndrome,
11565                                                 10, 5, rt);
11566             break;
11567         }
11568         env->cp15.esr_el[new_el] = env->exception.syndrome;
11569         break;
11570     case EXCP_IRQ:
11571     case EXCP_VIRQ:
11572         addr += 0x80;
11573         break;
11574     case EXCP_FIQ:
11575     case EXCP_VFIQ:
11576         addr += 0x100;
11577         break;
11578     case EXCP_VSERR:
11579         addr += 0x180;
11580         /* Construct the SError syndrome from IDS and ISS fields. */
11581         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11582         env->cp15.esr_el[new_el] = env->exception.syndrome;
11583         break;
11584     default:
11585         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11586     }
11587 
11588     if (is_a64(env)) {
11589         old_mode = pstate_read(env);
11590         aarch64_save_sp(env, arm_current_el(env));
11591         env->elr_el[new_el] = env->pc;
11592 
11593         if (cur_el == 1 && new_el == 1) {
11594             uint64_t hcr = arm_hcr_el2_eff(env);
11595             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11596                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11597                 /*
11598                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11599                  * by setting M[3:2] to 0b10.
11600                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11601                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11602                  */
11603                 old_mode = deposit32(old_mode, 2, 2, 2);
11604             }
11605         }
11606     } else {
11607         old_mode = cpsr_read_for_spsr_elx(env);
11608         env->elr_el[new_el] = env->regs[15];
11609 
11610         aarch64_sync_32_to_64(env);
11611 
11612         env->condexec_bits = 0;
11613     }
11614     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11615 
11616     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11617     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11618                   env->elr_el[new_el]);
11619 
11620     if (cpu_isar_feature(aa64_pan, cpu)) {
11621         /* The value of PSTATE.PAN is normally preserved, except when ... */
11622         new_mode |= old_mode & PSTATE_PAN;
11623         switch (new_el) {
11624         case 2:
11625             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11626             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11627                 != (HCR_E2H | HCR_TGE)) {
11628                 break;
11629             }
11630             /* fall through */
11631         case 1:
11632             /* ... the target is EL1 ... */
11633             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11634             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11635                 new_mode |= PSTATE_PAN;
11636             }
11637             break;
11638         }
11639     }
11640     if (cpu_isar_feature(aa64_mte, cpu)) {
11641         new_mode |= PSTATE_TCO;
11642     }
11643 
11644     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11645         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11646             new_mode |= PSTATE_SSBS;
11647         } else {
11648             new_mode &= ~PSTATE_SSBS;
11649         }
11650     }
11651 
11652     pstate_write(env, PSTATE_DAIF | new_mode);
11653     env->aarch64 = true;
11654     aarch64_restore_sp(env, new_el);
11655 
11656     if (tcg_enabled()) {
11657         helper_rebuild_hflags_a64(env, new_el);
11658     }
11659 
11660     env->pc = addr;
11661 
11662     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11663                   new_el, env->pc, pstate_read(env));
11664 }
11665 
11666 /*
11667  * Do semihosting call and set the appropriate return value. All the
11668  * permission and validity checks have been done at translate time.
11669  *
11670  * We only see semihosting exceptions in TCG only as they are not
11671  * trapped to the hypervisor in KVM.
11672  */
11673 #ifdef CONFIG_TCG
11674 static void tcg_handle_semihosting(CPUState *cs)
11675 {
11676     ARMCPU *cpu = ARM_CPU(cs);
11677     CPUARMState *env = &cpu->env;
11678 
11679     if (is_a64(env)) {
11680         qemu_log_mask(CPU_LOG_INT,
11681                       "...handling as semihosting call 0x%" PRIx64 "\n",
11682                       env->xregs[0]);
11683         do_common_semihosting(cs);
11684         env->pc += 4;
11685     } else {
11686         qemu_log_mask(CPU_LOG_INT,
11687                       "...handling as semihosting call 0x%x\n",
11688                       env->regs[0]);
11689         do_common_semihosting(cs);
11690         env->regs[15] += env->thumb ? 2 : 4;
11691     }
11692 }
11693 #endif
11694 
11695 /*
11696  * Handle a CPU exception for A and R profile CPUs.
11697  * Do any appropriate logging, handle PSCI calls, and then hand off
11698  * to the AArch64-entry or AArch32-entry function depending on the
11699  * target exception level's register width.
11700  *
11701  * Note: this is used for both TCG (as the do_interrupt tcg op),
11702  *       and KVM to re-inject guest debug exceptions, and to
11703  *       inject a Synchronous-External-Abort.
11704  */
11705 void arm_cpu_do_interrupt(CPUState *cs)
11706 {
11707     ARMCPU *cpu = ARM_CPU(cs);
11708     CPUARMState *env = &cpu->env;
11709     unsigned int new_el = env->exception.target_el;
11710 
11711     assert(!arm_feature(env, ARM_FEATURE_M));
11712 
11713     arm_log_exception(cs);
11714     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11715                   new_el);
11716     if (qemu_loglevel_mask(CPU_LOG_INT)
11717         && !excp_is_internal(cs->exception_index)) {
11718         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11719                       syn_get_ec(env->exception.syndrome),
11720                       env->exception.syndrome);
11721     }
11722 
11723     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11724         arm_handle_psci_call(cpu);
11725         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11726         return;
11727     }
11728 
11729     /*
11730      * Semihosting semantics depend on the register width of the code
11731      * that caused the exception, not the target exception level, so
11732      * must be handled here.
11733      */
11734 #ifdef CONFIG_TCG
11735     if (cs->exception_index == EXCP_SEMIHOST) {
11736         tcg_handle_semihosting(cs);
11737         return;
11738     }
11739 #endif
11740 
11741     /*
11742      * Hooks may change global state so BQL should be held, also the
11743      * BQL needs to be held for any modification of
11744      * cs->interrupt_request.
11745      */
11746     g_assert(bql_locked());
11747 
11748     arm_call_pre_el_change_hook(cpu);
11749 
11750     assert(!excp_is_internal(cs->exception_index));
11751     if (arm_el_is_aa64(env, new_el)) {
11752         arm_cpu_do_interrupt_aarch64(cs);
11753     } else {
11754         arm_cpu_do_interrupt_aarch32(cs);
11755     }
11756 
11757     arm_call_el_change_hook(cpu);
11758 
11759     if (!kvm_enabled()) {
11760         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11761     }
11762 }
11763 #endif /* !CONFIG_USER_ONLY */
11764 
11765 uint64_t arm_sctlr(CPUARMState *env, int el)
11766 {
11767     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11768     if (el == 0) {
11769         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11770         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11771     }
11772     return env->cp15.sctlr_el[el];
11773 }
11774 
11775 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11776 {
11777     if (regime_has_2_ranges(mmu_idx)) {
11778         return extract64(tcr, 37, 2);
11779     } else if (regime_is_stage2(mmu_idx)) {
11780         return 0; /* VTCR_EL2 */
11781     } else {
11782         /* Replicate the single TBI bit so we always have 2 bits.  */
11783         return extract32(tcr, 20, 1) * 3;
11784     }
11785 }
11786 
11787 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11788 {
11789     if (regime_has_2_ranges(mmu_idx)) {
11790         return extract64(tcr, 51, 2);
11791     } else if (regime_is_stage2(mmu_idx)) {
11792         return 0; /* VTCR_EL2 */
11793     } else {
11794         /* Replicate the single TBID bit so we always have 2 bits.  */
11795         return extract32(tcr, 29, 1) * 3;
11796     }
11797 }
11798 
11799 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11800 {
11801     if (regime_has_2_ranges(mmu_idx)) {
11802         return extract64(tcr, 57, 2);
11803     } else {
11804         /* Replicate the single TCMA bit so we always have 2 bits.  */
11805         return extract32(tcr, 30, 1) * 3;
11806     }
11807 }
11808 
11809 static ARMGranuleSize tg0_to_gran_size(int tg)
11810 {
11811     switch (tg) {
11812     case 0:
11813         return Gran4K;
11814     case 1:
11815         return Gran64K;
11816     case 2:
11817         return Gran16K;
11818     default:
11819         return GranInvalid;
11820     }
11821 }
11822 
11823 static ARMGranuleSize tg1_to_gran_size(int tg)
11824 {
11825     switch (tg) {
11826     case 1:
11827         return Gran16K;
11828     case 2:
11829         return Gran4K;
11830     case 3:
11831         return Gran64K;
11832     default:
11833         return GranInvalid;
11834     }
11835 }
11836 
11837 static inline bool have4k(ARMCPU *cpu, bool stage2)
11838 {
11839     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11840         : cpu_isar_feature(aa64_tgran4, cpu);
11841 }
11842 
11843 static inline bool have16k(ARMCPU *cpu, bool stage2)
11844 {
11845     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11846         : cpu_isar_feature(aa64_tgran16, cpu);
11847 }
11848 
11849 static inline bool have64k(ARMCPU *cpu, bool stage2)
11850 {
11851     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11852         : cpu_isar_feature(aa64_tgran64, cpu);
11853 }
11854 
11855 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11856                                          bool stage2)
11857 {
11858     switch (gran) {
11859     case Gran4K:
11860         if (have4k(cpu, stage2)) {
11861             return gran;
11862         }
11863         break;
11864     case Gran16K:
11865         if (have16k(cpu, stage2)) {
11866             return gran;
11867         }
11868         break;
11869     case Gran64K:
11870         if (have64k(cpu, stage2)) {
11871             return gran;
11872         }
11873         break;
11874     case GranInvalid:
11875         break;
11876     }
11877     /*
11878      * If the guest selects a granule size that isn't implemented,
11879      * the architecture requires that we behave as if it selected one
11880      * that is (with an IMPDEF choice of which one to pick). We choose
11881      * to implement the smallest supported granule size.
11882      */
11883     if (have4k(cpu, stage2)) {
11884         return Gran4K;
11885     }
11886     if (have16k(cpu, stage2)) {
11887         return Gran16K;
11888     }
11889     assert(have64k(cpu, stage2));
11890     return Gran64K;
11891 }
11892 
11893 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11894                                    ARMMMUIdx mmu_idx, bool data,
11895                                    bool el1_is_aa32)
11896 {
11897     uint64_t tcr = regime_tcr(env, mmu_idx);
11898     bool epd, hpd, tsz_oob, ds, ha, hd;
11899     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11900     ARMGranuleSize gran;
11901     ARMCPU *cpu = env_archcpu(env);
11902     bool stage2 = regime_is_stage2(mmu_idx);
11903 
11904     if (!regime_has_2_ranges(mmu_idx)) {
11905         select = 0;
11906         tsz = extract32(tcr, 0, 6);
11907         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11908         if (stage2) {
11909             /* VTCR_EL2 */
11910             hpd = false;
11911         } else {
11912             hpd = extract32(tcr, 24, 1);
11913         }
11914         epd = false;
11915         sh = extract32(tcr, 12, 2);
11916         ps = extract32(tcr, 16, 3);
11917         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11918         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11919         ds = extract64(tcr, 32, 1);
11920     } else {
11921         bool e0pd;
11922 
11923         /*
11924          * Bit 55 is always between the two regions, and is canonical for
11925          * determining if address tagging is enabled.
11926          */
11927         select = extract64(va, 55, 1);
11928         if (!select) {
11929             tsz = extract32(tcr, 0, 6);
11930             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11931             epd = extract32(tcr, 7, 1);
11932             sh = extract32(tcr, 12, 2);
11933             hpd = extract64(tcr, 41, 1);
11934             e0pd = extract64(tcr, 55, 1);
11935         } else {
11936             tsz = extract32(tcr, 16, 6);
11937             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11938             epd = extract32(tcr, 23, 1);
11939             sh = extract32(tcr, 28, 2);
11940             hpd = extract64(tcr, 42, 1);
11941             e0pd = extract64(tcr, 56, 1);
11942         }
11943         ps = extract64(tcr, 32, 3);
11944         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11945         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11946         ds = extract64(tcr, 59, 1);
11947 
11948         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11949             regime_is_user(env, mmu_idx)) {
11950             epd = true;
11951         }
11952     }
11953 
11954     gran = sanitize_gran_size(cpu, gran, stage2);
11955 
11956     if (cpu_isar_feature(aa64_st, cpu)) {
11957         max_tsz = 48 - (gran == Gran64K);
11958     } else {
11959         max_tsz = 39;
11960     }
11961 
11962     /*
11963      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11964      * adjust the effective value of DS, as documented.
11965      */
11966     min_tsz = 16;
11967     if (gran == Gran64K) {
11968         if (cpu_isar_feature(aa64_lva, cpu)) {
11969             min_tsz = 12;
11970         }
11971         ds = false;
11972     } else if (ds) {
11973         if (regime_is_stage2(mmu_idx)) {
11974             if (gran == Gran16K) {
11975                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11976             } else {
11977                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11978             }
11979         } else {
11980             if (gran == Gran16K) {
11981                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11982             } else {
11983                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11984             }
11985         }
11986         if (ds) {
11987             min_tsz = 12;
11988         }
11989     }
11990 
11991     if (stage2 && el1_is_aa32) {
11992         /*
11993          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11994          * are loosened: a configured IPA of 40 bits is permitted even if
11995          * the implemented PA is less than that (and so a 40 bit IPA would
11996          * fault for an AArch64 EL1). See R_DTLMN.
11997          */
11998         min_tsz = MIN(min_tsz, 24);
11999     }
12000 
12001     if (tsz > max_tsz) {
12002         tsz = max_tsz;
12003         tsz_oob = true;
12004     } else if (tsz < min_tsz) {
12005         tsz = min_tsz;
12006         tsz_oob = true;
12007     } else {
12008         tsz_oob = false;
12009     }
12010 
12011     /* Present TBI as a composite with TBID.  */
12012     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12013     if (!data) {
12014         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12015     }
12016     tbi = (tbi >> select) & 1;
12017 
12018     return (ARMVAParameters) {
12019         .tsz = tsz,
12020         .ps = ps,
12021         .sh = sh,
12022         .select = select,
12023         .tbi = tbi,
12024         .epd = epd,
12025         .hpd = hpd,
12026         .tsz_oob = tsz_oob,
12027         .ds = ds,
12028         .ha = ha,
12029         .hd = ha && hd,
12030         .gran = gran,
12031     };
12032 }
12033 
12034 /*
12035  * Note that signed overflow is undefined in C.  The following routines are
12036  * careful to use unsigned types where modulo arithmetic is required.
12037  * Failure to do so _will_ break on newer gcc.
12038  */
12039 
12040 /* Signed saturating arithmetic.  */
12041 
12042 /* Perform 16-bit signed saturating addition.  */
12043 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12044 {
12045     uint16_t res;
12046 
12047     res = a + b;
12048     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12049         if (a & 0x8000) {
12050             res = 0x8000;
12051         } else {
12052             res = 0x7fff;
12053         }
12054     }
12055     return res;
12056 }
12057 
12058 /* Perform 8-bit signed saturating addition.  */
12059 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12060 {
12061     uint8_t res;
12062 
12063     res = a + b;
12064     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12065         if (a & 0x80) {
12066             res = 0x80;
12067         } else {
12068             res = 0x7f;
12069         }
12070     }
12071     return res;
12072 }
12073 
12074 /* Perform 16-bit signed saturating subtraction.  */
12075 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12076 {
12077     uint16_t res;
12078 
12079     res = a - b;
12080     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12081         if (a & 0x8000) {
12082             res = 0x8000;
12083         } else {
12084             res = 0x7fff;
12085         }
12086     }
12087     return res;
12088 }
12089 
12090 /* Perform 8-bit signed saturating subtraction.  */
12091 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12092 {
12093     uint8_t res;
12094 
12095     res = a - b;
12096     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12097         if (a & 0x80) {
12098             res = 0x80;
12099         } else {
12100             res = 0x7f;
12101         }
12102     }
12103     return res;
12104 }
12105 
12106 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12107 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12108 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
12109 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
12110 #define PFX q
12111 
12112 #include "op_addsub.h"
12113 
12114 /* Unsigned saturating arithmetic.  */
12115 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12116 {
12117     uint16_t res;
12118     res = a + b;
12119     if (res < a) {
12120         res = 0xffff;
12121     }
12122     return res;
12123 }
12124 
12125 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12126 {
12127     if (a > b) {
12128         return a - b;
12129     } else {
12130         return 0;
12131     }
12132 }
12133 
12134 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12135 {
12136     uint8_t res;
12137     res = a + b;
12138     if (res < a) {
12139         res = 0xff;
12140     }
12141     return res;
12142 }
12143 
12144 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12145 {
12146     if (a > b) {
12147         return a - b;
12148     } else {
12149         return 0;
12150     }
12151 }
12152 
12153 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12154 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12155 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
12156 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
12157 #define PFX uq
12158 
12159 #include "op_addsub.h"
12160 
12161 /* Signed modulo arithmetic.  */
12162 #define SARITH16(a, b, n, op) do { \
12163     int32_t sum; \
12164     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12165     RESULT(sum, n, 16); \
12166     if (sum >= 0) \
12167         ge |= 3 << (n * 2); \
12168     } while (0)
12169 
12170 #define SARITH8(a, b, n, op) do { \
12171     int32_t sum; \
12172     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12173     RESULT(sum, n, 8); \
12174     if (sum >= 0) \
12175         ge |= 1 << n; \
12176     } while (0)
12177 
12178 
12179 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12180 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12181 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
12182 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
12183 #define PFX s
12184 #define ARITH_GE
12185 
12186 #include "op_addsub.h"
12187 
12188 /* Unsigned modulo arithmetic.  */
12189 #define ADD16(a, b, n) do { \
12190     uint32_t sum; \
12191     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12192     RESULT(sum, n, 16); \
12193     if ((sum >> 16) == 1) \
12194         ge |= 3 << (n * 2); \
12195     } while (0)
12196 
12197 #define ADD8(a, b, n) do { \
12198     uint32_t sum; \
12199     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12200     RESULT(sum, n, 8); \
12201     if ((sum >> 8) == 1) \
12202         ge |= 1 << n; \
12203     } while (0)
12204 
12205 #define SUB16(a, b, n) do { \
12206     uint32_t sum; \
12207     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12208     RESULT(sum, n, 16); \
12209     if ((sum >> 16) == 0) \
12210         ge |= 3 << (n * 2); \
12211     } while (0)
12212 
12213 #define SUB8(a, b, n) do { \
12214     uint32_t sum; \
12215     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12216     RESULT(sum, n, 8); \
12217     if ((sum >> 8) == 0) \
12218         ge |= 1 << n; \
12219     } while (0)
12220 
12221 #define PFX u
12222 #define ARITH_GE
12223 
12224 #include "op_addsub.h"
12225 
12226 /* Halved signed arithmetic.  */
12227 #define ADD16(a, b, n) \
12228   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12229 #define SUB16(a, b, n) \
12230   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12231 #define ADD8(a, b, n) \
12232   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12233 #define SUB8(a, b, n) \
12234   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12235 #define PFX sh
12236 
12237 #include "op_addsub.h"
12238 
12239 /* Halved unsigned arithmetic.  */
12240 #define ADD16(a, b, n) \
12241   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12242 #define SUB16(a, b, n) \
12243   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12244 #define ADD8(a, b, n) \
12245   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12246 #define SUB8(a, b, n) \
12247   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12248 #define PFX uh
12249 
12250 #include "op_addsub.h"
12251 
12252 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12253 {
12254     if (a > b) {
12255         return a - b;
12256     } else {
12257         return b - a;
12258     }
12259 }
12260 
12261 /* Unsigned sum of absolute byte differences.  */
12262 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12263 {
12264     uint32_t sum;
12265     sum = do_usad(a, b);
12266     sum += do_usad(a >> 8, b >> 8);
12267     sum += do_usad(a >> 16, b >> 16);
12268     sum += do_usad(a >> 24, b >> 24);
12269     return sum;
12270 }
12271 
12272 /* For ARMv6 SEL instruction.  */
12273 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12274 {
12275     uint32_t mask;
12276 
12277     mask = 0;
12278     if (flags & 1) {
12279         mask |= 0xff;
12280     }
12281     if (flags & 2) {
12282         mask |= 0xff00;
12283     }
12284     if (flags & 4) {
12285         mask |= 0xff0000;
12286     }
12287     if (flags & 8) {
12288         mask |= 0xff000000;
12289     }
12290     return (a & mask) | (b & ~mask);
12291 }
12292 
12293 /*
12294  * CRC helpers.
12295  * The upper bytes of val (above the number specified by 'bytes') must have
12296  * been zeroed out by the caller.
12297  */
12298 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12299 {
12300     uint8_t buf[4];
12301 
12302     stl_le_p(buf, val);
12303 
12304     /* zlib crc32 converts the accumulator and output to one's complement.  */
12305     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12306 }
12307 
12308 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12309 {
12310     uint8_t buf[4];
12311 
12312     stl_le_p(buf, val);
12313 
12314     /* Linux crc32c converts the output to one's complement.  */
12315     return crc32c(acc, buf, bytes) ^ 0xffffffff;
12316 }
12317 
12318 /*
12319  * Return the exception level to which FP-disabled exceptions should
12320  * be taken, or 0 if FP is enabled.
12321  */
12322 int fp_exception_el(CPUARMState *env, int cur_el)
12323 {
12324 #ifndef CONFIG_USER_ONLY
12325     uint64_t hcr_el2;
12326 
12327     /*
12328      * CPACR and the CPTR registers don't exist before v6, so FP is
12329      * always accessible
12330      */
12331     if (!arm_feature(env, ARM_FEATURE_V6)) {
12332         return 0;
12333     }
12334 
12335     if (arm_feature(env, ARM_FEATURE_M)) {
12336         /* CPACR can cause a NOCP UsageFault taken to current security state */
12337         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12338             return 1;
12339         }
12340 
12341         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12342             if (!extract32(env->v7m.nsacr, 10, 1)) {
12343                 /* FP insns cause a NOCP UsageFault taken to Secure */
12344                 return 3;
12345             }
12346         }
12347 
12348         return 0;
12349     }
12350 
12351     hcr_el2 = arm_hcr_el2_eff(env);
12352 
12353     /*
12354      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12355      * 0, 2 : trap EL0 and EL1/PL1 accesses
12356      * 1    : trap only EL0 accesses
12357      * 3    : trap no accesses
12358      * This register is ignored if E2H+TGE are both set.
12359      */
12360     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12361         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12362 
12363         switch (fpen) {
12364         case 1:
12365             if (cur_el != 0) {
12366                 break;
12367             }
12368             /* fall through */
12369         case 0:
12370         case 2:
12371             /* Trap from Secure PL0 or PL1 to Secure PL1. */
12372             if (!arm_el_is_aa64(env, 3)
12373                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12374                 return 3;
12375             }
12376             if (cur_el <= 1) {
12377                 return 1;
12378             }
12379             break;
12380         }
12381     }
12382 
12383     /*
12384      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12385      * to control non-secure access to the FPU. It doesn't have any
12386      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12387      */
12388     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12389          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12390         if (!extract32(env->cp15.nsacr, 10, 1)) {
12391             /* FP insns act as UNDEF */
12392             return cur_el == 2 ? 2 : 1;
12393         }
12394     }
12395 
12396     /*
12397      * CPTR_EL2 is present in v7VE or v8, and changes format
12398      * with HCR_EL2.E2H (regardless of TGE).
12399      */
12400     if (cur_el <= 2) {
12401         if (hcr_el2 & HCR_E2H) {
12402             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12403             case 1:
12404                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12405                     break;
12406                 }
12407                 /* fall through */
12408             case 0:
12409             case 2:
12410                 return 2;
12411             }
12412         } else if (arm_is_el2_enabled(env)) {
12413             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12414                 return 2;
12415             }
12416         }
12417     }
12418 
12419     /* CPTR_EL3 : present in v8 */
12420     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12421         /* Trap all FP ops to EL3 */
12422         return 3;
12423     }
12424 #endif
12425     return 0;
12426 }
12427 
12428 /* Return the exception level we're running at if this is our mmu_idx */
12429 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12430 {
12431     if (mmu_idx & ARM_MMU_IDX_M) {
12432         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12433     }
12434 
12435     switch (mmu_idx) {
12436     case ARMMMUIdx_E10_0:
12437     case ARMMMUIdx_E20_0:
12438         return 0;
12439     case ARMMMUIdx_E10_1:
12440     case ARMMMUIdx_E10_1_PAN:
12441         return 1;
12442     case ARMMMUIdx_E2:
12443     case ARMMMUIdx_E20_2:
12444     case ARMMMUIdx_E20_2_PAN:
12445         return 2;
12446     case ARMMMUIdx_E3:
12447         return 3;
12448     default:
12449         g_assert_not_reached();
12450     }
12451 }
12452 
12453 #ifndef CONFIG_TCG
12454 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12455 {
12456     g_assert_not_reached();
12457 }
12458 #endif
12459 
12460 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12461 {
12462     ARMMMUIdx idx;
12463     uint64_t hcr;
12464 
12465     if (arm_feature(env, ARM_FEATURE_M)) {
12466         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12467     }
12468 
12469     /* See ARM pseudo-function ELIsInHost.  */
12470     switch (el) {
12471     case 0:
12472         hcr = arm_hcr_el2_eff(env);
12473         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12474             idx = ARMMMUIdx_E20_0;
12475         } else {
12476             idx = ARMMMUIdx_E10_0;
12477         }
12478         break;
12479     case 1:
12480         if (arm_pan_enabled(env)) {
12481             idx = ARMMMUIdx_E10_1_PAN;
12482         } else {
12483             idx = ARMMMUIdx_E10_1;
12484         }
12485         break;
12486     case 2:
12487         /* Note that TGE does not apply at EL2.  */
12488         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12489             if (arm_pan_enabled(env)) {
12490                 idx = ARMMMUIdx_E20_2_PAN;
12491             } else {
12492                 idx = ARMMMUIdx_E20_2;
12493             }
12494         } else {
12495             idx = ARMMMUIdx_E2;
12496         }
12497         break;
12498     case 3:
12499         return ARMMMUIdx_E3;
12500     default:
12501         g_assert_not_reached();
12502     }
12503 
12504     return idx;
12505 }
12506 
12507 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12508 {
12509     return arm_mmu_idx_el(env, arm_current_el(env));
12510 }
12511 
12512 static bool mve_no_pred(CPUARMState *env)
12513 {
12514     /*
12515      * Return true if there is definitely no predication of MVE
12516      * instructions by VPR or LTPSIZE. (Returning false even if there
12517      * isn't any predication is OK; generated code will just be
12518      * a little worse.)
12519      * If the CPU does not implement MVE then this TB flag is always 0.
12520      *
12521      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12522      * logic in gen_update_fp_context() needs to be updated to match.
12523      *
12524      * We do not include the effect of the ECI bits here -- they are
12525      * tracked in other TB flags. This simplifies the logic for
12526      * "when did we emit code that changes the MVE_NO_PRED TB flag
12527      * and thus need to end the TB?".
12528      */
12529     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12530         return false;
12531     }
12532     if (env->v7m.vpr) {
12533         return false;
12534     }
12535     if (env->v7m.ltpsize < 4) {
12536         return false;
12537     }
12538     return true;
12539 }
12540 
12541 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12542                           uint64_t *cs_base, uint32_t *pflags)
12543 {
12544     CPUARMTBFlags flags;
12545 
12546     assert_hflags_rebuild_correctly(env);
12547     flags = env->hflags;
12548 
12549     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12550         *pc = env->pc;
12551         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12552             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12553         }
12554     } else {
12555         *pc = env->regs[15];
12556 
12557         if (arm_feature(env, ARM_FEATURE_M)) {
12558             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12559                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12560                 != env->v7m.secure) {
12561                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12562             }
12563 
12564             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12565                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12566                  (env->v7m.secure &&
12567                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12568                 /*
12569                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12570                  * active FP context; we must create a new FP context before
12571                  * executing any FP insn.
12572                  */
12573                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12574             }
12575 
12576             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12577             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12578                 DP_TBFLAG_M32(flags, LSPACT, 1);
12579             }
12580 
12581             if (mve_no_pred(env)) {
12582                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12583             }
12584         } else {
12585             /*
12586              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12587              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12588              */
12589             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12590                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12591             } else {
12592                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12593                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12594             }
12595             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12596                 DP_TBFLAG_A32(flags, VFPEN, 1);
12597             }
12598         }
12599 
12600         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12601         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12602     }
12603 
12604     /*
12605      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12606      * states defined in the ARM ARM for software singlestep:
12607      *  SS_ACTIVE   PSTATE.SS   State
12608      *     0            x       Inactive (the TB flag for SS is always 0)
12609      *     1            0       Active-pending
12610      *     1            1       Active-not-pending
12611      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12612      */
12613     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12614         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12615     }
12616 
12617     *pflags = flags.flags;
12618     *cs_base = flags.flags2;
12619 }
12620 
12621 #ifdef TARGET_AARCH64
12622 /*
12623  * The manual says that when SVE is enabled and VQ is widened the
12624  * implementation is allowed to zero the previously inaccessible
12625  * portion of the registers.  The corollary to that is that when
12626  * SVE is enabled and VQ is narrowed we are also allowed to zero
12627  * the now inaccessible portion of the registers.
12628  *
12629  * The intent of this is that no predicate bit beyond VQ is ever set.
12630  * Which means that some operations on predicate registers themselves
12631  * may operate on full uint64_t or even unrolled across the maximum
12632  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12633  * may well be cheaper than conditionals to restrict the operation
12634  * to the relevant portion of a uint16_t[16].
12635  */
12636 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12637 {
12638     int i, j;
12639     uint64_t pmask;
12640 
12641     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12642     assert(vq <= env_archcpu(env)->sve_max_vq);
12643 
12644     /* Zap the high bits of the zregs.  */
12645     for (i = 0; i < 32; i++) {
12646         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12647     }
12648 
12649     /* Zap the high bits of the pregs and ffr.  */
12650     pmask = 0;
12651     if (vq & 3) {
12652         pmask = ~(-1ULL << (16 * (vq & 3)));
12653     }
12654     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12655         for (i = 0; i < 17; ++i) {
12656             env->vfp.pregs[i].p[j] &= pmask;
12657         }
12658         pmask = 0;
12659     }
12660 }
12661 
12662 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12663 {
12664     int exc_el;
12665 
12666     if (sm) {
12667         exc_el = sme_exception_el(env, el);
12668     } else {
12669         exc_el = sve_exception_el(env, el);
12670     }
12671     if (exc_el) {
12672         return 0; /* disabled */
12673     }
12674     return sve_vqm1_for_el_sm(env, el, sm);
12675 }
12676 
12677 /*
12678  * Notice a change in SVE vector size when changing EL.
12679  */
12680 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12681                            int new_el, bool el0_a64)
12682 {
12683     ARMCPU *cpu = env_archcpu(env);
12684     int old_len, new_len;
12685     bool old_a64, new_a64, sm;
12686 
12687     /* Nothing to do if no SVE.  */
12688     if (!cpu_isar_feature(aa64_sve, cpu)) {
12689         return;
12690     }
12691 
12692     /* Nothing to do if FP is disabled in either EL.  */
12693     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12694         return;
12695     }
12696 
12697     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12698     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12699 
12700     /*
12701      * Both AArch64.TakeException and AArch64.ExceptionReturn
12702      * invoke ResetSVEState when taking an exception from, or
12703      * returning to, AArch32 state when PSTATE.SM is enabled.
12704      */
12705     sm = FIELD_EX64(env->svcr, SVCR, SM);
12706     if (old_a64 != new_a64 && sm) {
12707         arm_reset_sve_state(env);
12708         return;
12709     }
12710 
12711     /*
12712      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12713      * at ELx, or not available because the EL is in AArch32 state, then
12714      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12715      * has an effective value of 0".
12716      *
12717      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12718      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12719      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12720      * we already have the correct register contents when encountering the
12721      * vq0->vq0 transition between EL0->EL1.
12722      */
12723     old_len = new_len = 0;
12724     if (old_a64) {
12725         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12726     }
12727     if (new_a64) {
12728         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12729     }
12730 
12731     /* When changing vector length, clear inaccessible state.  */
12732     if (new_len < old_len) {
12733         aarch64_sve_narrow_vq(env, new_len + 1);
12734     }
12735 }
12736 #endif
12737 
12738 #ifndef CONFIG_USER_ONLY
12739 ARMSecuritySpace arm_security_space(CPUARMState *env)
12740 {
12741     if (arm_feature(env, ARM_FEATURE_M)) {
12742         return arm_secure_to_space(env->v7m.secure);
12743     }
12744 
12745     /*
12746      * If EL3 is not supported then the secure state is implementation
12747      * defined, in which case QEMU defaults to non-secure.
12748      */
12749     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12750         return ARMSS_NonSecure;
12751     }
12752 
12753     /* Check for AArch64 EL3 or AArch32 Mon. */
12754     if (is_a64(env)) {
12755         if (extract32(env->pstate, 2, 2) == 3) {
12756             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12757                 return ARMSS_Root;
12758             } else {
12759                 return ARMSS_Secure;
12760             }
12761         }
12762     } else {
12763         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12764             return ARMSS_Secure;
12765         }
12766     }
12767 
12768     return arm_security_space_below_el3(env);
12769 }
12770 
12771 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12772 {
12773     assert(!arm_feature(env, ARM_FEATURE_M));
12774 
12775     /*
12776      * If EL3 is not supported then the secure state is implementation
12777      * defined, in which case QEMU defaults to non-secure.
12778      */
12779     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12780         return ARMSS_NonSecure;
12781     }
12782 
12783     /*
12784      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12785      * Ignoring NSE when !NS retains consistency without having to
12786      * modify other predicates.
12787      */
12788     if (!(env->cp15.scr_el3 & SCR_NS)) {
12789         return ARMSS_Secure;
12790     } else if (env->cp15.scr_el3 & SCR_NSE) {
12791         return ARMSS_Realm;
12792     } else {
12793         return ARMSS_NonSecure;
12794     }
12795 }
12796 #endif /* !CONFIG_USER_ONLY */
12797