xref: /openbmc/qemu/target/arm/helper.c (revision 1faa437d)
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         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
2025             ret |= ISR_IS;
2026             ret |= CPSR_I;
2027         }
2028     } else {
2029         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2030             ret |= CPSR_I;
2031         }
2032 
2033         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
2034             ret |= ISR_IS;
2035             ret |= CPSR_I;
2036         }
2037     }
2038 
2039     if (hcr_el2 & HCR_FMO) {
2040         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2041             ret |= CPSR_F;
2042         }
2043         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
2044             ret |= ISR_FS;
2045             ret |= CPSR_F;
2046         }
2047     } else {
2048         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2049             ret |= CPSR_F;
2050         }
2051     }
2052 
2053     if (hcr_el2 & HCR_AMO) {
2054         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2055             ret |= CPSR_A;
2056         }
2057     }
2058 
2059     return ret;
2060 }
2061 
2062 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2063                                        bool isread)
2064 {
2065     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2066         return CP_ACCESS_TRAP_EL2;
2067     }
2068 
2069     return CP_ACCESS_OK;
2070 }
2071 
2072 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2073                                        bool isread)
2074 {
2075     if (arm_feature(env, ARM_FEATURE_V8)) {
2076         return access_aa64_tid1(env, ri, isread);
2077     }
2078 
2079     return CP_ACCESS_OK;
2080 }
2081 
2082 static const ARMCPRegInfo v7_cp_reginfo[] = {
2083     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2084     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2085       .access = PL1_W, .type = ARM_CP_NOP },
2086     /*
2087      * Performance monitors are implementation defined in v7,
2088      * but with an ARM recommended set of registers, which we
2089      * follow.
2090      *
2091      * Performance registers fall into three categories:
2092      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2093      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2094      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2095      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2096      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2097      */
2098     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2099       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2100       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2101       .writefn = pmcntenset_write,
2102       .accessfn = pmreg_access,
2103       .fgt = FGT_PMCNTEN,
2104       .raw_writefn = raw_write },
2105     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2106       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2107       .access = PL0_RW, .accessfn = pmreg_access,
2108       .fgt = FGT_PMCNTEN,
2109       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2110       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2111     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2112       .access = PL0_RW,
2113       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2114       .accessfn = pmreg_access,
2115       .fgt = FGT_PMCNTEN,
2116       .writefn = pmcntenclr_write,
2117       .type = ARM_CP_ALIAS | ARM_CP_IO },
2118     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2119       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2120       .access = PL0_RW, .accessfn = pmreg_access,
2121       .fgt = FGT_PMCNTEN,
2122       .type = ARM_CP_ALIAS | ARM_CP_IO,
2123       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2124       .writefn = pmcntenclr_write },
2125     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2126       .access = PL0_RW, .type = ARM_CP_IO,
2127       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2128       .accessfn = pmreg_access,
2129       .fgt = FGT_PMOVS,
2130       .writefn = pmovsr_write,
2131       .raw_writefn = raw_write },
2132     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2133       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2134       .access = PL0_RW, .accessfn = pmreg_access,
2135       .fgt = FGT_PMOVS,
2136       .type = ARM_CP_ALIAS | ARM_CP_IO,
2137       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2138       .writefn = pmovsr_write,
2139       .raw_writefn = raw_write },
2140     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2141       .access = PL0_W, .accessfn = pmreg_access_swinc,
2142       .fgt = FGT_PMSWINC_EL0,
2143       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2144       .writefn = pmswinc_write },
2145     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2146       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2147       .access = PL0_W, .accessfn = pmreg_access_swinc,
2148       .fgt = FGT_PMSWINC_EL0,
2149       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2150       .writefn = pmswinc_write },
2151     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2152       .access = PL0_RW, .type = ARM_CP_ALIAS,
2153       .fgt = FGT_PMSELR_EL0,
2154       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2155       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2156       .raw_writefn = raw_write},
2157     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2158       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2159       .access = PL0_RW, .accessfn = pmreg_access_selr,
2160       .fgt = FGT_PMSELR_EL0,
2161       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2162       .writefn = pmselr_write, .raw_writefn = raw_write, },
2163     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2164       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2165       .fgt = FGT_PMCCNTR_EL0,
2166       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2167       .accessfn = pmreg_access_ccntr },
2168     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2169       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2170       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2171       .fgt = FGT_PMCCNTR_EL0,
2172       .type = ARM_CP_IO,
2173       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2174       .readfn = pmccntr_read, .writefn = pmccntr_write,
2175       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2176     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2177       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2178       .access = PL0_RW, .accessfn = pmreg_access,
2179       .fgt = FGT_PMCCFILTR_EL0,
2180       .type = ARM_CP_ALIAS | ARM_CP_IO,
2181       .resetvalue = 0, },
2182     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2183       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2184       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2185       .access = PL0_RW, .accessfn = pmreg_access,
2186       .fgt = FGT_PMCCFILTR_EL0,
2187       .type = ARM_CP_IO,
2188       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2189       .resetvalue = 0, },
2190     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2191       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2192       .accessfn = pmreg_access,
2193       .fgt = FGT_PMEVTYPERN_EL0,
2194       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2195     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2196       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2197       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2198       .accessfn = pmreg_access,
2199       .fgt = FGT_PMEVTYPERN_EL0,
2200       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2201     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2202       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2203       .accessfn = pmreg_access_xevcntr,
2204       .fgt = FGT_PMEVCNTRN_EL0,
2205       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2206     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2207       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2208       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2209       .accessfn = pmreg_access_xevcntr,
2210       .fgt = FGT_PMEVCNTRN_EL0,
2211       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2212     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2213       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2214       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2215       .resetvalue = 0,
2216       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2217     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2218       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2219       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2220       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2221       .resetvalue = 0,
2222       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2223     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2224       .access = PL1_RW, .accessfn = access_tpm,
2225       .fgt = FGT_PMINTEN,
2226       .type = ARM_CP_ALIAS | ARM_CP_IO,
2227       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2228       .resetvalue = 0,
2229       .writefn = pmintenset_write, .raw_writefn = raw_write },
2230     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2231       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2232       .access = PL1_RW, .accessfn = access_tpm,
2233       .fgt = FGT_PMINTEN,
2234       .type = ARM_CP_IO,
2235       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2236       .writefn = pmintenset_write, .raw_writefn = raw_write,
2237       .resetvalue = 0x0 },
2238     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2239       .access = PL1_RW, .accessfn = access_tpm,
2240       .fgt = FGT_PMINTEN,
2241       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2242       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2243       .writefn = pmintenclr_write, },
2244     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2245       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2246       .access = PL1_RW, .accessfn = access_tpm,
2247       .fgt = FGT_PMINTEN,
2248       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2249       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2250       .writefn = pmintenclr_write },
2251     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2252       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2253       .access = PL1_R,
2254       .accessfn = access_tid4,
2255       .fgt = FGT_CCSIDR_EL1,
2256       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2257     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2258       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2259       .access = PL1_RW,
2260       .accessfn = access_tid4,
2261       .fgt = FGT_CSSELR_EL1,
2262       .writefn = csselr_write, .resetvalue = 0,
2263       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2264                              offsetof(CPUARMState, cp15.csselr_ns) } },
2265     /*
2266      * Auxiliary ID register: this actually has an IMPDEF value but for now
2267      * just RAZ for all cores:
2268      */
2269     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2270       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2271       .access = PL1_R, .type = ARM_CP_CONST,
2272       .accessfn = access_aa64_tid1,
2273       .fgt = FGT_AIDR_EL1,
2274       .resetvalue = 0 },
2275     /*
2276      * Auxiliary fault status registers: these also are IMPDEF, and we
2277      * choose to RAZ/WI for all cores.
2278      */
2279     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2280       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2281       .access = PL1_RW, .accessfn = access_tvm_trvm,
2282       .fgt = FGT_AFSR0_EL1,
2283       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2284       .type = ARM_CP_CONST, .resetvalue = 0 },
2285     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2286       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2287       .access = PL1_RW, .accessfn = access_tvm_trvm,
2288       .fgt = FGT_AFSR1_EL1,
2289       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2290       .type = ARM_CP_CONST, .resetvalue = 0 },
2291     /*
2292      * MAIR can just read-as-written because we don't implement caches
2293      * and so don't need to care about memory attributes.
2294      */
2295     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2296       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2297       .access = PL1_RW, .accessfn = access_tvm_trvm,
2298       .fgt = FGT_MAIR_EL1,
2299       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2300       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2301       .resetvalue = 0 },
2302     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2303       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2304       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2305       .resetvalue = 0 },
2306     /*
2307      * For non-long-descriptor page tables these are PRRR and NMRR;
2308      * regardless they still act as reads-as-written for QEMU.
2309      */
2310      /*
2311       * MAIR0/1 are defined separately from their 64-bit counterpart which
2312       * allows them to assign the correct fieldoffset based on the endianness
2313       * handled in the field definitions.
2314       */
2315     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2316       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2317       .access = PL1_RW, .accessfn = access_tvm_trvm,
2318       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2319                              offsetof(CPUARMState, cp15.mair0_ns) },
2320       .resetfn = arm_cp_reset_ignore },
2321     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2322       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2323       .access = PL1_RW, .accessfn = access_tvm_trvm,
2324       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2325                              offsetof(CPUARMState, cp15.mair1_ns) },
2326       .resetfn = arm_cp_reset_ignore },
2327     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2328       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2329       .fgt = FGT_ISR_EL1,
2330       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2331     /* 32 bit ITLB invalidates */
2332     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2333       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2334       .writefn = tlbiall_write },
2335     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2336       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2337       .writefn = tlbimva_write },
2338     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2339       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2340       .writefn = tlbiasid_write },
2341     /* 32 bit DTLB invalidates */
2342     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2343       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2344       .writefn = tlbiall_write },
2345     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2346       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2347       .writefn = tlbimva_write },
2348     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2349       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2350       .writefn = tlbiasid_write },
2351     /* 32 bit TLB invalidates */
2352     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2353       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2354       .writefn = tlbiall_write },
2355     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2356       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2357       .writefn = tlbimva_write },
2358     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2359       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2360       .writefn = tlbiasid_write },
2361     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2362       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2363       .writefn = tlbimvaa_write },
2364 };
2365 
2366 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2367     /* 32 bit TLB invalidates, Inner Shareable */
2368     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2369       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2370       .writefn = tlbiall_is_write },
2371     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2372       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2373       .writefn = tlbimva_is_write },
2374     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2375       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2376       .writefn = tlbiasid_is_write },
2377     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2378       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2379       .writefn = tlbimvaa_is_write },
2380 };
2381 
2382 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2383     /* PMOVSSET is not implemented in v7 before v7ve */
2384     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2385       .access = PL0_RW, .accessfn = pmreg_access,
2386       .fgt = FGT_PMOVS,
2387       .type = ARM_CP_ALIAS | ARM_CP_IO,
2388       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2389       .writefn = pmovsset_write,
2390       .raw_writefn = raw_write },
2391     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2392       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2393       .access = PL0_RW, .accessfn = pmreg_access,
2394       .fgt = FGT_PMOVS,
2395       .type = ARM_CP_ALIAS | ARM_CP_IO,
2396       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2397       .writefn = pmovsset_write,
2398       .raw_writefn = raw_write },
2399 };
2400 
2401 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2402                         uint64_t value)
2403 {
2404     value &= 1;
2405     env->teecr = value;
2406 }
2407 
2408 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2409                                    bool isread)
2410 {
2411     /*
2412      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2413      * at all, so we don't need to check whether we're v8A.
2414      */
2415     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2416         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2417         return CP_ACCESS_TRAP_EL2;
2418     }
2419     return CP_ACCESS_OK;
2420 }
2421 
2422 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2423                                     bool isread)
2424 {
2425     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2426         return CP_ACCESS_TRAP;
2427     }
2428     return teecr_access(env, ri, isread);
2429 }
2430 
2431 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2432     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2433       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2434       .resetvalue = 0,
2435       .writefn = teecr_write, .accessfn = teecr_access },
2436     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2437       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2438       .accessfn = teehbr_access, .resetvalue = 0 },
2439 };
2440 
2441 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2442     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2443       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2444       .access = PL0_RW,
2445       .fgt = FGT_TPIDR_EL0,
2446       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2447     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2448       .access = PL0_RW,
2449       .fgt = FGT_TPIDR_EL0,
2450       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2451                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2452       .resetfn = arm_cp_reset_ignore },
2453     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2454       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2455       .access = PL0_R | PL1_W,
2456       .fgt = FGT_TPIDRRO_EL0,
2457       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2458       .resetvalue = 0},
2459     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2460       .access = PL0_R | PL1_W,
2461       .fgt = FGT_TPIDRRO_EL0,
2462       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2463                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2464       .resetfn = arm_cp_reset_ignore },
2465     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2466       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2467       .access = PL1_RW,
2468       .fgt = FGT_TPIDR_EL1,
2469       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2470     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2471       .access = PL1_RW,
2472       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2473                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2474       .resetvalue = 0 },
2475 };
2476 
2477 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2478 {
2479     ARMCPU *cpu = env_archcpu(env);
2480 
2481     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2482 }
2483 
2484 #ifndef CONFIG_USER_ONLY
2485 
2486 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2487                                        bool isread)
2488 {
2489     /*
2490      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2491      * Writable only at the highest implemented exception level.
2492      */
2493     int el = arm_current_el(env);
2494     uint64_t hcr;
2495     uint32_t cntkctl;
2496 
2497     switch (el) {
2498     case 0:
2499         hcr = arm_hcr_el2_eff(env);
2500         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2501             cntkctl = env->cp15.cnthctl_el2;
2502         } else {
2503             cntkctl = env->cp15.c14_cntkctl;
2504         }
2505         if (!extract32(cntkctl, 0, 2)) {
2506             return CP_ACCESS_TRAP;
2507         }
2508         break;
2509     case 1:
2510         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2511             arm_is_secure_below_el3(env)) {
2512             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2513             return CP_ACCESS_TRAP_UNCATEGORIZED;
2514         }
2515         break;
2516     case 2:
2517     case 3:
2518         break;
2519     }
2520 
2521     if (!isread && el < arm_highest_el(env)) {
2522         return CP_ACCESS_TRAP_UNCATEGORIZED;
2523     }
2524 
2525     return CP_ACCESS_OK;
2526 }
2527 
2528 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2529                                         bool isread)
2530 {
2531     unsigned int cur_el = arm_current_el(env);
2532     bool has_el2 = arm_is_el2_enabled(env);
2533     uint64_t hcr = arm_hcr_el2_eff(env);
2534 
2535     switch (cur_el) {
2536     case 0:
2537         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2538         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2539             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2540                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2541         }
2542 
2543         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2544         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2545             return CP_ACCESS_TRAP;
2546         }
2547         /* fall through */
2548     case 1:
2549         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2550         if (has_el2 && timeridx == GTIMER_PHYS &&
2551             (hcr & HCR_E2H
2552              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2553              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2554             return CP_ACCESS_TRAP_EL2;
2555         }
2556         if (has_el2 && timeridx == GTIMER_VIRT) {
2557             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2558                 return CP_ACCESS_TRAP_EL2;
2559             }
2560         }
2561         break;
2562     }
2563     return CP_ACCESS_OK;
2564 }
2565 
2566 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2567                                       bool isread)
2568 {
2569     unsigned int cur_el = arm_current_el(env);
2570     bool has_el2 = arm_is_el2_enabled(env);
2571     uint64_t hcr = arm_hcr_el2_eff(env);
2572 
2573     switch (cur_el) {
2574     case 0:
2575         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2576             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2577             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2578                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2579         }
2580 
2581         /*
2582          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2583          * EL0 if EL0[PV]TEN is zero.
2584          */
2585         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2586             return CP_ACCESS_TRAP;
2587         }
2588         /* fall through */
2589 
2590     case 1:
2591         if (has_el2 && timeridx == GTIMER_PHYS) {
2592             if (hcr & HCR_E2H) {
2593                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2594                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2595                     return CP_ACCESS_TRAP_EL2;
2596                 }
2597             } else {
2598                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2599                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2600                     return CP_ACCESS_TRAP_EL2;
2601                 }
2602             }
2603         }
2604         if (has_el2 && timeridx == GTIMER_VIRT) {
2605             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2606                 return CP_ACCESS_TRAP_EL2;
2607             }
2608         }
2609         break;
2610     }
2611     return CP_ACCESS_OK;
2612 }
2613 
2614 static CPAccessResult gt_pct_access(CPUARMState *env,
2615                                     const ARMCPRegInfo *ri,
2616                                     bool isread)
2617 {
2618     return gt_counter_access(env, GTIMER_PHYS, isread);
2619 }
2620 
2621 static CPAccessResult gt_vct_access(CPUARMState *env,
2622                                     const ARMCPRegInfo *ri,
2623                                     bool isread)
2624 {
2625     return gt_counter_access(env, GTIMER_VIRT, isread);
2626 }
2627 
2628 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2629                                        bool isread)
2630 {
2631     return gt_timer_access(env, GTIMER_PHYS, isread);
2632 }
2633 
2634 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2635                                        bool isread)
2636 {
2637     return gt_timer_access(env, GTIMER_VIRT, isread);
2638 }
2639 
2640 static CPAccessResult gt_stimer_access(CPUARMState *env,
2641                                        const ARMCPRegInfo *ri,
2642                                        bool isread)
2643 {
2644     /*
2645      * The AArch64 register view of the secure physical timer is
2646      * always accessible from EL3, and configurably accessible from
2647      * Secure EL1.
2648      */
2649     switch (arm_current_el(env)) {
2650     case 1:
2651         if (!arm_is_secure(env)) {
2652             return CP_ACCESS_TRAP;
2653         }
2654         if (!(env->cp15.scr_el3 & SCR_ST)) {
2655             return CP_ACCESS_TRAP_EL3;
2656         }
2657         return CP_ACCESS_OK;
2658     case 0:
2659     case 2:
2660         return CP_ACCESS_TRAP;
2661     case 3:
2662         return CP_ACCESS_OK;
2663     default:
2664         g_assert_not_reached();
2665     }
2666 }
2667 
2668 uint64_t gt_get_countervalue(CPUARMState *env)
2669 {
2670     ARMCPU *cpu = env_archcpu(env);
2671 
2672     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2673 }
2674 
2675 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2676 {
2677     CPUARMState *env = &cpu->env;
2678     uint64_t cnthctl = env->cp15.cnthctl_el2;
2679     ARMSecuritySpace ss = arm_security_space(env);
2680     /* ISTATUS && !IMASK */
2681     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2682 
2683     /*
2684      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2685      * It is RES0 in Secure and NonSecure state.
2686      */
2687     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2688         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2689          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2690         irqstate = 0;
2691     }
2692 
2693     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2694     trace_arm_gt_update_irq(timeridx, irqstate);
2695 }
2696 
2697 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2698 {
2699     /*
2700      * Changing security state between Root and Secure/NonSecure, which may
2701      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2702      * mask bits. Update the IRQ state accordingly.
2703      */
2704     gt_update_irq(cpu, GTIMER_VIRT);
2705     gt_update_irq(cpu, GTIMER_PHYS);
2706 }
2707 
2708 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2709 {
2710     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2711         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2712         arm_is_el2_enabled(env) &&
2713         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2714         return env->cp15.cntpoff_el2;
2715     }
2716     return 0;
2717 }
2718 
2719 static uint64_t gt_phys_cnt_offset(CPUARMState *env)
2720 {
2721     if (arm_current_el(env) >= 2) {
2722         return 0;
2723     }
2724     return gt_phys_raw_cnt_offset(env);
2725 }
2726 
2727 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2728 {
2729     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2730 
2731     if (gt->ctl & 1) {
2732         /*
2733          * Timer enabled: calculate and set current ISTATUS, irq, and
2734          * reset timer to when ISTATUS next has to change
2735          */
2736         uint64_t offset = timeridx == GTIMER_VIRT ?
2737             cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
2738         uint64_t count = gt_get_countervalue(&cpu->env);
2739         /* Note that this must be unsigned 64 bit arithmetic: */
2740         int istatus = count - offset >= gt->cval;
2741         uint64_t nexttick;
2742 
2743         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2744 
2745         if (istatus) {
2746             /*
2747              * Next transition is when (count - offset) rolls back over to 0.
2748              * If offset > count then this is when count == offset;
2749              * if offset <= count then this is when count == offset + 2^64
2750              * For the latter case we set nexttick to an "as far in future
2751              * as possible" value and let the code below handle it.
2752              */
2753             if (offset > count) {
2754                 nexttick = offset;
2755             } else {
2756                 nexttick = UINT64_MAX;
2757             }
2758         } else {
2759             /*
2760              * Next transition is when (count - offset) == cval, i.e.
2761              * when count == (cval + offset).
2762              * If that would overflow, then again we set up the next interrupt
2763              * for "as far in the future as possible" for the code below.
2764              */
2765             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2766                 nexttick = UINT64_MAX;
2767             }
2768         }
2769         /*
2770          * Note that the desired next expiry time might be beyond the
2771          * signed-64-bit range of a QEMUTimer -- in this case we just
2772          * set the timer for as far in the future as possible. When the
2773          * timer expires we will reset the timer for any remaining period.
2774          */
2775         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2776             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2777         } else {
2778             timer_mod(cpu->gt_timer[timeridx], nexttick);
2779         }
2780         trace_arm_gt_recalc(timeridx, nexttick);
2781     } else {
2782         /* Timer disabled: ISTATUS and timer output always clear */
2783         gt->ctl &= ~4;
2784         timer_del(cpu->gt_timer[timeridx]);
2785         trace_arm_gt_recalc_disabled(timeridx);
2786     }
2787     gt_update_irq(cpu, timeridx);
2788 }
2789 
2790 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2791                            int timeridx)
2792 {
2793     ARMCPU *cpu = env_archcpu(env);
2794 
2795     timer_del(cpu->gt_timer[timeridx]);
2796 }
2797 
2798 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2799 {
2800     return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
2801 }
2802 
2803 uint64_t gt_virt_cnt_offset(CPUARMState *env)
2804 {
2805     uint64_t hcr;
2806 
2807     switch (arm_current_el(env)) {
2808     case 2:
2809         hcr = arm_hcr_el2_eff(env);
2810         if (hcr & HCR_E2H) {
2811             return 0;
2812         }
2813         break;
2814     case 0:
2815         hcr = arm_hcr_el2_eff(env);
2816         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2817             return 0;
2818         }
2819         break;
2820     }
2821 
2822     return env->cp15.cntvoff_el2;
2823 }
2824 
2825 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2826 {
2827     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2828 }
2829 
2830 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2831                           int timeridx,
2832                           uint64_t value)
2833 {
2834     trace_arm_gt_cval_write(timeridx, value);
2835     env->cp15.c14_timer[timeridx].cval = value;
2836     gt_recalc_timer(env_archcpu(env), timeridx);
2837 }
2838 
2839 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2840                              int timeridx)
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     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2855                       (gt_get_countervalue(env) - offset));
2856 }
2857 
2858 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2859                           int timeridx,
2860                           uint64_t value)
2861 {
2862     uint64_t offset = 0;
2863 
2864     switch (timeridx) {
2865     case GTIMER_VIRT:
2866     case GTIMER_HYPVIRT:
2867         offset = gt_virt_cnt_offset(env);
2868         break;
2869     case GTIMER_PHYS:
2870         offset = gt_phys_cnt_offset(env);
2871         break;
2872     }
2873 
2874     trace_arm_gt_tval_write(timeridx, value);
2875     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2876                                          sextract64(value, 0, 32);
2877     gt_recalc_timer(env_archcpu(env), timeridx);
2878 }
2879 
2880 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2881                          int timeridx,
2882                          uint64_t value)
2883 {
2884     ARMCPU *cpu = env_archcpu(env);
2885     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2886 
2887     trace_arm_gt_ctl_write(timeridx, value);
2888     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2889     if ((oldval ^ value) & 1) {
2890         /* Enable toggled */
2891         gt_recalc_timer(cpu, timeridx);
2892     } else if ((oldval ^ value) & 2) {
2893         /*
2894          * IMASK toggled: don't need to recalculate,
2895          * just set the interrupt line based on ISTATUS
2896          */
2897         trace_arm_gt_imask_toggle(timeridx);
2898         gt_update_irq(cpu, timeridx);
2899     }
2900 }
2901 
2902 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2903 {
2904     gt_timer_reset(env, ri, GTIMER_PHYS);
2905 }
2906 
2907 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2908                                uint64_t value)
2909 {
2910     gt_cval_write(env, ri, GTIMER_PHYS, value);
2911 }
2912 
2913 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2914 {
2915     return gt_tval_read(env, ri, GTIMER_PHYS);
2916 }
2917 
2918 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2919                                uint64_t value)
2920 {
2921     gt_tval_write(env, ri, GTIMER_PHYS, value);
2922 }
2923 
2924 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2925                               uint64_t value)
2926 {
2927     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2928 }
2929 
2930 static int gt_phys_redir_timeridx(CPUARMState *env)
2931 {
2932     switch (arm_mmu_idx(env)) {
2933     case ARMMMUIdx_E20_0:
2934     case ARMMMUIdx_E20_2:
2935     case ARMMMUIdx_E20_2_PAN:
2936         return GTIMER_HYP;
2937     default:
2938         return GTIMER_PHYS;
2939     }
2940 }
2941 
2942 static int gt_virt_redir_timeridx(CPUARMState *env)
2943 {
2944     switch (arm_mmu_idx(env)) {
2945     case ARMMMUIdx_E20_0:
2946     case ARMMMUIdx_E20_2:
2947     case ARMMMUIdx_E20_2_PAN:
2948         return GTIMER_HYPVIRT;
2949     default:
2950         return GTIMER_VIRT;
2951     }
2952 }
2953 
2954 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2955                                         const ARMCPRegInfo *ri)
2956 {
2957     int timeridx = gt_phys_redir_timeridx(env);
2958     return env->cp15.c14_timer[timeridx].cval;
2959 }
2960 
2961 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2962                                      uint64_t value)
2963 {
2964     int timeridx = gt_phys_redir_timeridx(env);
2965     gt_cval_write(env, ri, timeridx, value);
2966 }
2967 
2968 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2969                                         const ARMCPRegInfo *ri)
2970 {
2971     int timeridx = gt_phys_redir_timeridx(env);
2972     return gt_tval_read(env, ri, timeridx);
2973 }
2974 
2975 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2976                                      uint64_t value)
2977 {
2978     int timeridx = gt_phys_redir_timeridx(env);
2979     gt_tval_write(env, ri, timeridx, value);
2980 }
2981 
2982 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2983                                        const ARMCPRegInfo *ri)
2984 {
2985     int timeridx = gt_phys_redir_timeridx(env);
2986     return env->cp15.c14_timer[timeridx].ctl;
2987 }
2988 
2989 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2990                                     uint64_t value)
2991 {
2992     int timeridx = gt_phys_redir_timeridx(env);
2993     gt_ctl_write(env, ri, timeridx, value);
2994 }
2995 
2996 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2997 {
2998     gt_timer_reset(env, ri, GTIMER_VIRT);
2999 }
3000 
3001 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3002                                uint64_t value)
3003 {
3004     gt_cval_write(env, ri, GTIMER_VIRT, value);
3005 }
3006 
3007 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3008 {
3009     return gt_tval_read(env, ri, GTIMER_VIRT);
3010 }
3011 
3012 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3013                                uint64_t value)
3014 {
3015     gt_tval_write(env, ri, GTIMER_VIRT, value);
3016 }
3017 
3018 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3019                               uint64_t value)
3020 {
3021     gt_ctl_write(env, ri, GTIMER_VIRT, value);
3022 }
3023 
3024 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3025                              uint64_t value)
3026 {
3027     ARMCPU *cpu = env_archcpu(env);
3028     uint32_t oldval = env->cp15.cnthctl_el2;
3029     uint32_t valid_mask =
3030         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
3031         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
3032         R_CNTHCTL_EVNTEN_MASK |
3033         R_CNTHCTL_EVNTDIR_MASK |
3034         R_CNTHCTL_EVNTI_MASK |
3035         R_CNTHCTL_EL0VTEN_MASK |
3036         R_CNTHCTL_EL0PTEN_MASK |
3037         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
3038         R_CNTHCTL_EL1PTEN_MASK;
3039 
3040     if (cpu_isar_feature(aa64_rme, cpu)) {
3041         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
3042     }
3043     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
3044         valid_mask |=
3045             R_CNTHCTL_EL1TVT_MASK |
3046             R_CNTHCTL_EL1TVCT_MASK |
3047             R_CNTHCTL_EL1NVPCT_MASK |
3048             R_CNTHCTL_EL1NVVCT_MASK |
3049             R_CNTHCTL_EVNTIS_MASK;
3050     }
3051     if (cpu_isar_feature(aa64_ecv, cpu)) {
3052         valid_mask |= R_CNTHCTL_ECV_MASK;
3053     }
3054 
3055     /* Clear RES0 bits */
3056     value &= valid_mask;
3057 
3058     raw_write(env, ri, value);
3059 
3060     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
3061         gt_update_irq(cpu, GTIMER_VIRT);
3062     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
3063         gt_update_irq(cpu, GTIMER_PHYS);
3064     }
3065 }
3066 
3067 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3068                               uint64_t value)
3069 {
3070     ARMCPU *cpu = env_archcpu(env);
3071 
3072     trace_arm_gt_cntvoff_write(value);
3073     raw_write(env, ri, value);
3074     gt_recalc_timer(cpu, GTIMER_VIRT);
3075 }
3076 
3077 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
3078                                         const ARMCPRegInfo *ri)
3079 {
3080     int timeridx = gt_virt_redir_timeridx(env);
3081     return env->cp15.c14_timer[timeridx].cval;
3082 }
3083 
3084 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3085                                      uint64_t value)
3086 {
3087     int timeridx = gt_virt_redir_timeridx(env);
3088     gt_cval_write(env, ri, timeridx, value);
3089 }
3090 
3091 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3092                                         const ARMCPRegInfo *ri)
3093 {
3094     int timeridx = gt_virt_redir_timeridx(env);
3095     return gt_tval_read(env, ri, timeridx);
3096 }
3097 
3098 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3099                                      uint64_t value)
3100 {
3101     int timeridx = gt_virt_redir_timeridx(env);
3102     gt_tval_write(env, ri, timeridx, value);
3103 }
3104 
3105 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3106                                        const ARMCPRegInfo *ri)
3107 {
3108     int timeridx = gt_virt_redir_timeridx(env);
3109     return env->cp15.c14_timer[timeridx].ctl;
3110 }
3111 
3112 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3113                                     uint64_t value)
3114 {
3115     int timeridx = gt_virt_redir_timeridx(env);
3116     gt_ctl_write(env, ri, timeridx, value);
3117 }
3118 
3119 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3120 {
3121     gt_timer_reset(env, ri, GTIMER_HYP);
3122 }
3123 
3124 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3125                               uint64_t value)
3126 {
3127     gt_cval_write(env, ri, GTIMER_HYP, value);
3128 }
3129 
3130 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3131 {
3132     return gt_tval_read(env, ri, GTIMER_HYP);
3133 }
3134 
3135 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3136                               uint64_t value)
3137 {
3138     gt_tval_write(env, ri, GTIMER_HYP, value);
3139 }
3140 
3141 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3142                               uint64_t value)
3143 {
3144     gt_ctl_write(env, ri, GTIMER_HYP, value);
3145 }
3146 
3147 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3148 {
3149     gt_timer_reset(env, ri, GTIMER_SEC);
3150 }
3151 
3152 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3153                               uint64_t value)
3154 {
3155     gt_cval_write(env, ri, GTIMER_SEC, value);
3156 }
3157 
3158 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3159 {
3160     return gt_tval_read(env, ri, GTIMER_SEC);
3161 }
3162 
3163 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3164                               uint64_t value)
3165 {
3166     gt_tval_write(env, ri, GTIMER_SEC, value);
3167 }
3168 
3169 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3170                               uint64_t value)
3171 {
3172     gt_ctl_write(env, ri, GTIMER_SEC, value);
3173 }
3174 
3175 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3176 {
3177     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3178 }
3179 
3180 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3181                              uint64_t value)
3182 {
3183     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3184 }
3185 
3186 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3187 {
3188     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3189 }
3190 
3191 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3192                              uint64_t value)
3193 {
3194     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3195 }
3196 
3197 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3198                             uint64_t value)
3199 {
3200     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3201 }
3202 
3203 void arm_gt_ptimer_cb(void *opaque)
3204 {
3205     ARMCPU *cpu = opaque;
3206 
3207     gt_recalc_timer(cpu, GTIMER_PHYS);
3208 }
3209 
3210 void arm_gt_vtimer_cb(void *opaque)
3211 {
3212     ARMCPU *cpu = opaque;
3213 
3214     gt_recalc_timer(cpu, GTIMER_VIRT);
3215 }
3216 
3217 void arm_gt_htimer_cb(void *opaque)
3218 {
3219     ARMCPU *cpu = opaque;
3220 
3221     gt_recalc_timer(cpu, GTIMER_HYP);
3222 }
3223 
3224 void arm_gt_stimer_cb(void *opaque)
3225 {
3226     ARMCPU *cpu = opaque;
3227 
3228     gt_recalc_timer(cpu, GTIMER_SEC);
3229 }
3230 
3231 void arm_gt_hvtimer_cb(void *opaque)
3232 {
3233     ARMCPU *cpu = opaque;
3234 
3235     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3236 }
3237 
3238 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3239     /*
3240      * Note that CNTFRQ is purely reads-as-written for the benefit
3241      * of software; writing it doesn't actually change the timer frequency.
3242      * Our reset value matches the fixed frequency we implement the timer at.
3243      */
3244     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3245       .type = ARM_CP_ALIAS,
3246       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3247       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3248     },
3249     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3250       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3251       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3252       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3253       .resetfn = arm_gt_cntfrq_reset,
3254     },
3255     /* overall control: mostly access permissions */
3256     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3257       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3258       .access = PL1_RW,
3259       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3260       .resetvalue = 0,
3261     },
3262     /* per-timer control */
3263     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3264       .secure = ARM_CP_SECSTATE_NS,
3265       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3266       .accessfn = gt_ptimer_access,
3267       .fieldoffset = offsetoflow32(CPUARMState,
3268                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3269       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3270       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3271     },
3272     { .name = "CNTP_CTL_S",
3273       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3274       .secure = ARM_CP_SECSTATE_S,
3275       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3276       .accessfn = gt_ptimer_access,
3277       .fieldoffset = offsetoflow32(CPUARMState,
3278                                    cp15.c14_timer[GTIMER_SEC].ctl),
3279       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3280     },
3281     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3282       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3283       .type = ARM_CP_IO, .access = PL0_RW,
3284       .accessfn = gt_ptimer_access,
3285       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3286       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3287       .resetvalue = 0,
3288       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3289       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3290     },
3291     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3292       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3293       .accessfn = gt_vtimer_access,
3294       .fieldoffset = offsetoflow32(CPUARMState,
3295                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3296       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3297       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3298     },
3299     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3300       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3301       .type = ARM_CP_IO, .access = PL0_RW,
3302       .accessfn = gt_vtimer_access,
3303       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3304       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3305       .resetvalue = 0,
3306       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3307       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3308     },
3309     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3310     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3311       .secure = ARM_CP_SECSTATE_NS,
3312       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3313       .accessfn = gt_ptimer_access,
3314       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3315     },
3316     { .name = "CNTP_TVAL_S",
3317       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3318       .secure = ARM_CP_SECSTATE_S,
3319       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3320       .accessfn = gt_ptimer_access,
3321       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3322     },
3323     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3324       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3325       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3326       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3327       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3328     },
3329     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3330       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3331       .accessfn = gt_vtimer_access,
3332       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3333     },
3334     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3335       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3336       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3337       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3338       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3339     },
3340     /* The counter itself */
3341     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3342       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3343       .accessfn = gt_pct_access,
3344       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3345     },
3346     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3347       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3348       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3349       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3350     },
3351     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3352       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3353       .accessfn = gt_vct_access,
3354       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3355     },
3356     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3357       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3358       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3359       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3360     },
3361     /* Comparison value, indicating when the timer goes off */
3362     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3363       .secure = ARM_CP_SECSTATE_NS,
3364       .access = PL0_RW,
3365       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3366       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3367       .accessfn = gt_ptimer_access,
3368       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3369       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3370     },
3371     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3372       .secure = ARM_CP_SECSTATE_S,
3373       .access = PL0_RW,
3374       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3375       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3376       .accessfn = gt_ptimer_access,
3377       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3378     },
3379     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3380       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3381       .access = PL0_RW,
3382       .type = ARM_CP_IO,
3383       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3384       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3385       .resetvalue = 0, .accessfn = gt_ptimer_access,
3386       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3387       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3388     },
3389     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3390       .access = PL0_RW,
3391       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3392       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3393       .accessfn = gt_vtimer_access,
3394       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3395       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3396     },
3397     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3398       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3399       .access = PL0_RW,
3400       .type = ARM_CP_IO,
3401       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3402       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3403       .resetvalue = 0, .accessfn = gt_vtimer_access,
3404       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3405       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3406     },
3407     /*
3408      * Secure timer -- this is actually restricted to only EL3
3409      * and configurably Secure-EL1 via the accessfn.
3410      */
3411     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3412       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3413       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3414       .accessfn = gt_stimer_access,
3415       .readfn = gt_sec_tval_read,
3416       .writefn = gt_sec_tval_write,
3417       .resetfn = gt_sec_timer_reset,
3418     },
3419     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3420       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3421       .type = ARM_CP_IO, .access = PL1_RW,
3422       .accessfn = gt_stimer_access,
3423       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3424       .resetvalue = 0,
3425       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3426     },
3427     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3428       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3429       .type = ARM_CP_IO, .access = PL1_RW,
3430       .accessfn = gt_stimer_access,
3431       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3432       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3433     },
3434 };
3435 
3436 /*
3437  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3438  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3439  * so our implementations here are identical to the normal registers.
3440  */
3441 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3442     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3443       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3444       .accessfn = gt_vct_access,
3445       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3446     },
3447     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3448       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3449       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3450       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3451     },
3452     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3453       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3454       .accessfn = gt_pct_access,
3455       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3456     },
3457     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3458       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3459       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3460       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3461     },
3462 };
3463 
3464 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3465                                         const ARMCPRegInfo *ri,
3466                                         bool isread)
3467 {
3468     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3469         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3470         return CP_ACCESS_TRAP_EL3;
3471     }
3472     return CP_ACCESS_OK;
3473 }
3474 
3475 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3476                               uint64_t value)
3477 {
3478     ARMCPU *cpu = env_archcpu(env);
3479 
3480     trace_arm_gt_cntpoff_write(value);
3481     raw_write(env, ri, value);
3482     gt_recalc_timer(cpu, GTIMER_PHYS);
3483 }
3484 
3485 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3486     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3487     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3488     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3489     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3490     .nv2_redirect_offset = 0x1a8,
3491     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3492 };
3493 #else
3494 
3495 /*
3496  * In user-mode most of the generic timer registers are inaccessible
3497  * however modern kernels (4.12+) allow access to cntvct_el0
3498  */
3499 
3500 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3501 {
3502     ARMCPU *cpu = env_archcpu(env);
3503 
3504     /*
3505      * Currently we have no support for QEMUTimer in linux-user so we
3506      * can't call gt_get_countervalue(env), instead we directly
3507      * call the lower level functions.
3508      */
3509     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3510 }
3511 
3512 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3513     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3514       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3515       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3516       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3517       .resetfn = arm_gt_cntfrq_reset,
3518     },
3519     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3520       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3521       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3522       .readfn = gt_virt_cnt_read,
3523     },
3524 };
3525 
3526 /*
3527  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3528  * is exposed to userspace by Linux.
3529  */
3530 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3531     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3532       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3533       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3534       .readfn = gt_virt_cnt_read,
3535     },
3536 };
3537 
3538 #endif
3539 
3540 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3541 {
3542     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3543         raw_write(env, ri, value);
3544     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3545         raw_write(env, ri, value & 0xfffff6ff);
3546     } else {
3547         raw_write(env, ri, value & 0xfffff1ff);
3548     }
3549 }
3550 
3551 #ifndef CONFIG_USER_ONLY
3552 /* get_phys_addr() isn't present for user-mode-only targets */
3553 
3554 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3555                                  bool isread)
3556 {
3557     if (ri->opc2 & 4) {
3558         /*
3559          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3560          * Secure EL1 (which can only happen if EL3 is AArch64).
3561          * They are simply UNDEF if executed from NS EL1.
3562          * They function normally from EL2 or EL3.
3563          */
3564         if (arm_current_el(env) == 1) {
3565             if (arm_is_secure_below_el3(env)) {
3566                 if (env->cp15.scr_el3 & SCR_EEL2) {
3567                     return CP_ACCESS_TRAP_EL2;
3568                 }
3569                 return CP_ACCESS_TRAP_EL3;
3570             }
3571             return CP_ACCESS_TRAP_UNCATEGORIZED;
3572         }
3573     }
3574     return CP_ACCESS_OK;
3575 }
3576 
3577 #ifdef CONFIG_TCG
3578 static int par_el1_shareability(GetPhysAddrResult *res)
3579 {
3580     /*
3581      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3582      * memory -- see pseudocode PAREncodeShareability().
3583      */
3584     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3585         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3586         return 2;
3587     }
3588     return res->cacheattrs.shareability;
3589 }
3590 
3591 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3592                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3593                              ARMSecuritySpace ss)
3594 {
3595     bool ret;
3596     uint64_t par64;
3597     bool format64 = false;
3598     ARMMMUFaultInfo fi = {};
3599     GetPhysAddrResult res = {};
3600 
3601     /*
3602      * I_MXTJT: Granule protection checks are not performed on the final address
3603      * of a successful translation.
3604      */
3605     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3606                                          &res, &fi);
3607 
3608     /*
3609      * ATS operations only do S1 or S1+S2 translations, so we never
3610      * have to deal with the ARMCacheAttrs format for S2 only.
3611      */
3612     assert(!res.cacheattrs.is_s2_format);
3613 
3614     if (ret) {
3615         /*
3616          * Some kinds of translation fault must cause exceptions rather
3617          * than being reported in the PAR.
3618          */
3619         int current_el = arm_current_el(env);
3620         int target_el;
3621         uint32_t syn, fsr, fsc;
3622         bool take_exc = false;
3623 
3624         if (fi.s1ptw && current_el == 1
3625             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3626             /*
3627              * Synchronous stage 2 fault on an access made as part of the
3628              * translation table walk for AT S1E0* or AT S1E1* insn
3629              * executed from NS EL1. If this is a synchronous external abort
3630              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3631              * to EL3. Otherwise the fault is taken as an exception to EL2,
3632              * and HPFAR_EL2 holds the faulting IPA.
3633              */
3634             if (fi.type == ARMFault_SyncExternalOnWalk &&
3635                 (env->cp15.scr_el3 & SCR_EA)) {
3636                 target_el = 3;
3637             } else {
3638                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3639                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3640                     env->cp15.hpfar_el2 |= HPFAR_NS;
3641                 }
3642                 target_el = 2;
3643             }
3644             take_exc = true;
3645         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3646             /*
3647              * Synchronous external aborts during a translation table walk
3648              * are taken as Data Abort exceptions.
3649              */
3650             if (fi.stage2) {
3651                 if (current_el == 3) {
3652                     target_el = 3;
3653                 } else {
3654                     target_el = 2;
3655                 }
3656             } else {
3657                 target_el = exception_target_el(env);
3658             }
3659             take_exc = true;
3660         }
3661 
3662         if (take_exc) {
3663             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3664             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3665                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3666                 fsr = arm_fi_to_lfsc(&fi);
3667                 fsc = extract32(fsr, 0, 6);
3668             } else {
3669                 fsr = arm_fi_to_sfsc(&fi);
3670                 fsc = 0x3f;
3671             }
3672             /*
3673              * Report exception with ESR indicating a fault due to a
3674              * translation table walk for a cache maintenance instruction.
3675              */
3676             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3677                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3678             env->exception.vaddress = value;
3679             env->exception.fsr = fsr;
3680             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3681         }
3682     }
3683 
3684     if (is_a64(env)) {
3685         format64 = true;
3686     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3687         /*
3688          * ATS1Cxx:
3689          * * TTBCR.EAE determines whether the result is returned using the
3690          *   32-bit or the 64-bit PAR format
3691          * * Instructions executed in Hyp mode always use the 64bit format
3692          *
3693          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3694          * * The Non-secure TTBCR.EAE bit is set to 1
3695          * * The implementation includes EL2, and the value of HCR.VM is 1
3696          *
3697          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3698          *
3699          * ATS1Hx always uses the 64bit format.
3700          */
3701         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3702 
3703         if (arm_feature(env, ARM_FEATURE_EL2) && !arm_aa32_secure_pl1_0(env)) {
3704             if (mmu_idx == ARMMMUIdx_E10_0 ||
3705                 mmu_idx == ARMMMUIdx_E10_1 ||
3706                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3707                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3708             } else {
3709                 format64 |= arm_current_el(env) == 2;
3710             }
3711         }
3712     }
3713 
3714     if (format64) {
3715         /* Create a 64-bit PAR */
3716         par64 = (1 << 11); /* LPAE bit always set */
3717         if (!ret) {
3718             par64 |= res.f.phys_addr & ~0xfffULL;
3719             if (!res.f.attrs.secure) {
3720                 par64 |= (1 << 9); /* NS */
3721             }
3722             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3723             par64 |= par_el1_shareability(&res) << 7; /* SH */
3724         } else {
3725             uint32_t fsr = arm_fi_to_lfsc(&fi);
3726 
3727             par64 |= 1; /* F */
3728             par64 |= (fsr & 0x3f) << 1; /* FS */
3729             if (fi.stage2) {
3730                 par64 |= (1 << 9); /* S */
3731             }
3732             if (fi.s1ptw) {
3733                 par64 |= (1 << 8); /* PTW */
3734             }
3735         }
3736     } else {
3737         /*
3738          * fsr is a DFSR/IFSR value for the short descriptor
3739          * translation table format (with WnR always clear).
3740          * Convert it to a 32-bit PAR.
3741          */
3742         if (!ret) {
3743             /* We do not set any attribute bits in the PAR */
3744             if (res.f.lg_page_size == 24
3745                 && arm_feature(env, ARM_FEATURE_V7)) {
3746                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3747             } else {
3748                 par64 = res.f.phys_addr & 0xfffff000;
3749             }
3750             if (!res.f.attrs.secure) {
3751                 par64 |= (1 << 9); /* NS */
3752             }
3753         } else {
3754             uint32_t fsr = arm_fi_to_sfsc(&fi);
3755 
3756             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3757                     ((fsr & 0xf) << 1) | 1;
3758         }
3759     }
3760     return par64;
3761 }
3762 #endif /* CONFIG_TCG */
3763 
3764 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3765 {
3766 #ifdef CONFIG_TCG
3767     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3768     uint64_t par64;
3769     ARMMMUIdx mmu_idx;
3770     int el = arm_current_el(env);
3771     ARMSecuritySpace ss = arm_security_space(env);
3772 
3773     switch (ri->opc2 & 6) {
3774     case 0:
3775         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3776         switch (el) {
3777         case 2:
3778             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3779             /* fall through */
3780         case 1:
3781         case 3:
3782             if (ri->crm == 9 && arm_pan_enabled(env)) {
3783                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3784             } else {
3785                 mmu_idx = ARMMMUIdx_Stage1_E1;
3786             }
3787             break;
3788         default:
3789             g_assert_not_reached();
3790         }
3791         break;
3792     case 2:
3793         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3794         switch (el) {
3795         case 3:
3796             mmu_idx = ARMMMUIdx_E10_0;
3797             break;
3798         case 2:
3799             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3800             mmu_idx = ARMMMUIdx_Stage1_E0;
3801             break;
3802         case 1:
3803             mmu_idx = ARMMMUIdx_Stage1_E0;
3804             break;
3805         default:
3806             g_assert_not_reached();
3807         }
3808         break;
3809     case 4:
3810         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3811         mmu_idx = ARMMMUIdx_E10_1;
3812         ss = ARMSS_NonSecure;
3813         break;
3814     case 6:
3815         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3816         mmu_idx = ARMMMUIdx_E10_0;
3817         ss = ARMSS_NonSecure;
3818         break;
3819     default:
3820         g_assert_not_reached();
3821     }
3822 
3823     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3824 
3825     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3826 #else
3827     /* Handled by hardware accelerator. */
3828     g_assert_not_reached();
3829 #endif /* CONFIG_TCG */
3830 }
3831 
3832 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3833                         uint64_t value)
3834 {
3835 #ifdef CONFIG_TCG
3836     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3837     uint64_t par64;
3838 
3839     /* There is no SecureEL2 for AArch32. */
3840     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3841                          ARMSS_NonSecure);
3842 
3843     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3844 #else
3845     /* Handled by hardware accelerator. */
3846     g_assert_not_reached();
3847 #endif /* CONFIG_TCG */
3848 }
3849 
3850 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3851                                      bool isread)
3852 {
3853     /*
3854      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3855      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3856      * only happen when executing at EL3 because that combination also causes an
3857      * illegal exception return. We don't need to check FEAT_RME either, because
3858      * scr_write() ensures that the NSE bit is not set otherwise.
3859      */
3860     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3861         return CP_ACCESS_TRAP;
3862     }
3863     return CP_ACCESS_OK;
3864 }
3865 
3866 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3867                                      bool isread)
3868 {
3869     if (arm_current_el(env) == 3 &&
3870         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3871         return CP_ACCESS_TRAP;
3872     }
3873     return at_e012_access(env, ri, isread);
3874 }
3875 
3876 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3877                                       bool isread)
3878 {
3879     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3880         return CP_ACCESS_TRAP_EL2;
3881     }
3882     return at_e012_access(env, ri, isread);
3883 }
3884 
3885 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3886                         uint64_t value)
3887 {
3888 #ifdef CONFIG_TCG
3889     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3890     ARMMMUIdx mmu_idx;
3891     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3892     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3893     bool for_el3 = false;
3894     ARMSecuritySpace ss;
3895 
3896     switch (ri->opc2 & 6) {
3897     case 0:
3898         switch (ri->opc1) {
3899         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3900             if (ri->crm == 9 && arm_pan_enabled(env)) {
3901                 mmu_idx = regime_e20 ?
3902                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3903             } else {
3904                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3905             }
3906             break;
3907         case 4: /* AT S1E2R, AT S1E2W */
3908             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3909             break;
3910         case 6: /* AT S1E3R, AT S1E3W */
3911             mmu_idx = ARMMMUIdx_E3;
3912             for_el3 = true;
3913             break;
3914         default:
3915             g_assert_not_reached();
3916         }
3917         break;
3918     case 2: /* AT S1E0R, AT S1E0W */
3919         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3920         break;
3921     case 4: /* AT S12E1R, AT S12E1W */
3922         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3923         break;
3924     case 6: /* AT S12E0R, AT S12E0W */
3925         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3926         break;
3927     default:
3928         g_assert_not_reached();
3929     }
3930 
3931     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3932     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3933 #else
3934     /* Handled by hardware accelerator. */
3935     g_assert_not_reached();
3936 #endif /* CONFIG_TCG */
3937 }
3938 #endif
3939 
3940 /* Return basic MPU access permission bits.  */
3941 static uint32_t simple_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 >> i) & mask;
3950         mask <<= 2;
3951     }
3952     return ret;
3953 }
3954 
3955 /* Pad basic MPU access permission bits to extended format.  */
3956 static uint32_t extended_mpu_ap_bits(uint32_t val)
3957 {
3958     uint32_t ret;
3959     uint32_t mask;
3960     int i;
3961     ret = 0;
3962     mask = 3;
3963     for (i = 0; i < 16; i += 2) {
3964         ret |= (val & mask) << i;
3965         mask <<= 2;
3966     }
3967     return ret;
3968 }
3969 
3970 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3971                                  uint64_t value)
3972 {
3973     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3974 }
3975 
3976 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3977 {
3978     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3979 }
3980 
3981 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3982                                  uint64_t value)
3983 {
3984     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3985 }
3986 
3987 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3988 {
3989     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3990 }
3991 
3992 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3993 {
3994     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3995 
3996     if (!u32p) {
3997         return 0;
3998     }
3999 
4000     u32p += env->pmsav7.rnr[M_REG_NS];
4001     return *u32p;
4002 }
4003 
4004 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
4005                          uint64_t value)
4006 {
4007     ARMCPU *cpu = env_archcpu(env);
4008     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
4009 
4010     if (!u32p) {
4011         return;
4012     }
4013 
4014     u32p += env->pmsav7.rnr[M_REG_NS];
4015     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4016     *u32p = value;
4017 }
4018 
4019 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4020                               uint64_t value)
4021 {
4022     ARMCPU *cpu = env_archcpu(env);
4023     uint32_t nrgs = cpu->pmsav7_dregion;
4024 
4025     if (value >= nrgs) {
4026         qemu_log_mask(LOG_GUEST_ERROR,
4027                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
4028                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
4029         return;
4030     }
4031 
4032     raw_write(env, ri, value);
4033 }
4034 
4035 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4036                           uint64_t value)
4037 {
4038     ARMCPU *cpu = env_archcpu(env);
4039 
4040     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4041     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4042 }
4043 
4044 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4045 {
4046     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4047 }
4048 
4049 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4050                           uint64_t value)
4051 {
4052     ARMCPU *cpu = env_archcpu(env);
4053 
4054     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4055     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4056 }
4057 
4058 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4059 {
4060     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4061 }
4062 
4063 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4064                            uint64_t value)
4065 {
4066     ARMCPU *cpu = env_archcpu(env);
4067 
4068     /*
4069      * Ignore writes that would select not implemented region.
4070      * This is architecturally UNPREDICTABLE.
4071      */
4072     if (value >= cpu->pmsav7_dregion) {
4073         return;
4074     }
4075 
4076     env->pmsav7.rnr[M_REG_NS] = value;
4077 }
4078 
4079 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4080                           uint64_t value)
4081 {
4082     ARMCPU *cpu = env_archcpu(env);
4083 
4084     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4085     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
4086 }
4087 
4088 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4089 {
4090     return env->pmsav8.hprbar[env->pmsav8.hprselr];
4091 }
4092 
4093 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4094                           uint64_t value)
4095 {
4096     ARMCPU *cpu = env_archcpu(env);
4097 
4098     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4099     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4100 }
4101 
4102 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4103 {
4104     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4105 }
4106 
4107 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4108                           uint64_t value)
4109 {
4110     uint32_t n;
4111     uint32_t bit;
4112     ARMCPU *cpu = env_archcpu(env);
4113 
4114     /* Ignore writes to unimplemented regions */
4115     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4116     value &= MAKE_64BIT_MASK(0, rmax);
4117 
4118     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4119 
4120     /* Register alias is only valid for first 32 indexes */
4121     for (n = 0; n < rmax; ++n) {
4122         bit = extract32(value, n, 1);
4123         env->pmsav8.hprlar[n] = deposit32(
4124                     env->pmsav8.hprlar[n], 0, 1, bit);
4125     }
4126 }
4127 
4128 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4129 {
4130     uint32_t n;
4131     uint32_t result = 0x0;
4132     ARMCPU *cpu = env_archcpu(env);
4133 
4134     /* Register alias is only valid for first 32 indexes */
4135     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4136         if (env->pmsav8.hprlar[n] & 0x1) {
4137             result |= (0x1 << n);
4138         }
4139     }
4140     return result;
4141 }
4142 
4143 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144                            uint64_t value)
4145 {
4146     ARMCPU *cpu = env_archcpu(env);
4147 
4148     /*
4149      * Ignore writes that would select not implemented region.
4150      * This is architecturally UNPREDICTABLE.
4151      */
4152     if (value >= cpu->pmsav8r_hdregion) {
4153         return;
4154     }
4155 
4156     env->pmsav8.hprselr = value;
4157 }
4158 
4159 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4160                           uint64_t value)
4161 {
4162     ARMCPU *cpu = env_archcpu(env);
4163     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4164                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4165 
4166     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4167 
4168     if (ri->opc1 & 4) {
4169         if (index >= cpu->pmsav8r_hdregion) {
4170             return;
4171         }
4172         if (ri->opc2 & 0x1) {
4173             env->pmsav8.hprlar[index] = value;
4174         } else {
4175             env->pmsav8.hprbar[index] = value;
4176         }
4177     } else {
4178         if (index >= cpu->pmsav7_dregion) {
4179             return;
4180         }
4181         if (ri->opc2 & 0x1) {
4182             env->pmsav8.rlar[M_REG_NS][index] = value;
4183         } else {
4184             env->pmsav8.rbar[M_REG_NS][index] = value;
4185         }
4186     }
4187 }
4188 
4189 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4190 {
4191     ARMCPU *cpu = env_archcpu(env);
4192     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4193                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4194 
4195     if (ri->opc1 & 4) {
4196         if (index >= cpu->pmsav8r_hdregion) {
4197             return 0x0;
4198         }
4199         if (ri->opc2 & 0x1) {
4200             return env->pmsav8.hprlar[index];
4201         } else {
4202             return env->pmsav8.hprbar[index];
4203         }
4204     } else {
4205         if (index >= cpu->pmsav7_dregion) {
4206             return 0x0;
4207         }
4208         if (ri->opc2 & 0x1) {
4209             return env->pmsav8.rlar[M_REG_NS][index];
4210         } else {
4211             return env->pmsav8.rbar[M_REG_NS][index];
4212         }
4213     }
4214 }
4215 
4216 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4217     { .name = "PRBAR",
4218       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4219       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4220       .accessfn = access_tvm_trvm,
4221       .readfn = prbar_read, .writefn = prbar_write },
4222     { .name = "PRLAR",
4223       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4224       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4225       .accessfn = access_tvm_trvm,
4226       .readfn = prlar_read, .writefn = prlar_write },
4227     { .name = "PRSELR", .resetvalue = 0,
4228       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4229       .access = PL1_RW, .accessfn = access_tvm_trvm,
4230       .writefn = prselr_write,
4231       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4232     { .name = "HPRBAR", .resetvalue = 0,
4233       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4234       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4235       .readfn = hprbar_read, .writefn = hprbar_write },
4236     { .name = "HPRLAR",
4237       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4238       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4239       .readfn = hprlar_read, .writefn = hprlar_write },
4240     { .name = "HPRSELR", .resetvalue = 0,
4241       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4242       .access = PL2_RW,
4243       .writefn = hprselr_write,
4244       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4245     { .name = "HPRENR",
4246       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4247       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4248       .readfn = hprenr_read, .writefn = hprenr_write },
4249 };
4250 
4251 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4252     /*
4253      * Reset for all these registers is handled in arm_cpu_reset(),
4254      * because the PMSAv7 is also used by M-profile CPUs, which do
4255      * not register cpregs but still need the state to be reset.
4256      */
4257     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4258       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4259       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4260       .readfn = pmsav7_read, .writefn = pmsav7_write,
4261       .resetfn = arm_cp_reset_ignore },
4262     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4263       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4264       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4265       .readfn = pmsav7_read, .writefn = pmsav7_write,
4266       .resetfn = arm_cp_reset_ignore },
4267     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4268       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4269       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4270       .readfn = pmsav7_read, .writefn = pmsav7_write,
4271       .resetfn = arm_cp_reset_ignore },
4272     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4273       .access = PL1_RW,
4274       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4275       .writefn = pmsav7_rgnr_write,
4276       .resetfn = arm_cp_reset_ignore },
4277 };
4278 
4279 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4280     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4281       .access = PL1_RW, .type = ARM_CP_ALIAS,
4282       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4283       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4284     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4285       .access = PL1_RW, .type = ARM_CP_ALIAS,
4286       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4287       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4288     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4289       .access = PL1_RW,
4290       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4291       .resetvalue = 0, },
4292     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4293       .access = PL1_RW,
4294       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4295       .resetvalue = 0, },
4296     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4297       .access = PL1_RW,
4298       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4299     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4300       .access = PL1_RW,
4301       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4302     /* Protection region base and size registers */
4303     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4304       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4305       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4306     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4307       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4308       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4309     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4310       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4311       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4312     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4313       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4314       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4315     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4316       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4317       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4318     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4319       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4320       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4321     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4322       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4323       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4324     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4325       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4326       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4327 };
4328 
4329 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4330                              uint64_t value)
4331 {
4332     ARMCPU *cpu = env_archcpu(env);
4333 
4334     if (!arm_feature(env, ARM_FEATURE_V8)) {
4335         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4336             /*
4337              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4338              * using Long-descriptor translation table format
4339              */
4340             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4341         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4342             /*
4343              * In an implementation that includes the Security Extensions
4344              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4345              * Short-descriptor translation table format.
4346              */
4347             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4348         } else {
4349             value &= TTBCR_N;
4350         }
4351     }
4352 
4353     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4354         /*
4355          * With LPAE the TTBCR could result in a change of ASID
4356          * via the TTBCR.A1 bit, so do a TLB flush.
4357          */
4358         tlb_flush(CPU(cpu));
4359     }
4360     raw_write(env, ri, value);
4361 }
4362 
4363 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4364                                uint64_t value)
4365 {
4366     ARMCPU *cpu = env_archcpu(env);
4367 
4368     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4369     tlb_flush(CPU(cpu));
4370     raw_write(env, ri, value);
4371 }
4372 
4373 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4374                             uint64_t value)
4375 {
4376     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4377     if (cpreg_field_is_64bit(ri) &&
4378         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4379         ARMCPU *cpu = env_archcpu(env);
4380         tlb_flush(CPU(cpu));
4381     }
4382     raw_write(env, ri, value);
4383 }
4384 
4385 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4386                                     uint64_t value)
4387 {
4388     /*
4389      * If we are running with E2&0 regime, then an ASID is active.
4390      * Flush if that might be changing.  Note we're not checking
4391      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4392      * holds the active ASID, only checking the field that might.
4393      */
4394     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4395         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4396         uint16_t mask = ARMMMUIdxBit_E20_2 |
4397                         ARMMMUIdxBit_E20_2_PAN |
4398                         ARMMMUIdxBit_E20_0;
4399         tlb_flush_by_mmuidx(env_cpu(env), mask);
4400     }
4401     raw_write(env, ri, value);
4402 }
4403 
4404 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405                         uint64_t value)
4406 {
4407     ARMCPU *cpu = env_archcpu(env);
4408     CPUState *cs = CPU(cpu);
4409 
4410     /*
4411      * A change in VMID to the stage2 page table (Stage2) invalidates
4412      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4413      */
4414     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4415         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4416     }
4417     raw_write(env, ri, value);
4418 }
4419 
4420 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4421     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4422       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4423       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4424                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4425     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4426       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4427       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4428                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4429     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4430       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4431       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4432                              offsetof(CPUARMState, cp15.dfar_ns) } },
4433     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4434       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4435       .access = PL1_RW, .accessfn = access_tvm_trvm,
4436       .fgt = FGT_FAR_EL1,
4437       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4438       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4439       .resetvalue = 0, },
4440 };
4441 
4442 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4443     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4444       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4445       .access = PL1_RW, .accessfn = access_tvm_trvm,
4446       .fgt = FGT_ESR_EL1,
4447       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4448       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4449     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4450       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4451       .access = PL1_RW, .accessfn = access_tvm_trvm,
4452       .fgt = FGT_TTBR0_EL1,
4453       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4454       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4455       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4456                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4457     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4458       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4459       .access = PL1_RW, .accessfn = access_tvm_trvm,
4460       .fgt = FGT_TTBR1_EL1,
4461       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4462       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4463       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4464                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4465     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4466       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4467       .access = PL1_RW, .accessfn = access_tvm_trvm,
4468       .fgt = FGT_TCR_EL1,
4469       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4470       .writefn = vmsa_tcr_el12_write,
4471       .raw_writefn = raw_write,
4472       .resetvalue = 0,
4473       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4474     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4475       .access = PL1_RW, .accessfn = access_tvm_trvm,
4476       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4477       .raw_writefn = raw_write,
4478       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4479                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4480 };
4481 
4482 /*
4483  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4484  * qemu tlbs nor adjusting cached masks.
4485  */
4486 static const ARMCPRegInfo ttbcr2_reginfo = {
4487     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4488     .access = PL1_RW, .accessfn = access_tvm_trvm,
4489     .type = ARM_CP_ALIAS,
4490     .bank_fieldoffsets = {
4491         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4492         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4493     },
4494 };
4495 
4496 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4497                                 uint64_t value)
4498 {
4499     env->cp15.c15_ticonfig = value & 0xe7;
4500     /* The OS_TYPE bit in this register changes the reported CPUID! */
4501     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4502         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4503 }
4504 
4505 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4506                                 uint64_t value)
4507 {
4508     env->cp15.c15_threadid = value & 0xffff;
4509 }
4510 
4511 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4512                            uint64_t value)
4513 {
4514     /* Wait-for-interrupt (deprecated) */
4515     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4516 }
4517 
4518 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4519                                   uint64_t value)
4520 {
4521     /*
4522      * On OMAP there are registers indicating the max/min index of dcache lines
4523      * containing a dirty line; cache flush operations have to reset these.
4524      */
4525     env->cp15.c15_i_max = 0x000;
4526     env->cp15.c15_i_min = 0xff0;
4527 }
4528 
4529 static const ARMCPRegInfo omap_cp_reginfo[] = {
4530     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4531       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4532       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4533       .resetvalue = 0, },
4534     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4535       .access = PL1_RW, .type = ARM_CP_NOP },
4536     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4537       .access = PL1_RW,
4538       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4539       .writefn = omap_ticonfig_write },
4540     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4541       .access = PL1_RW,
4542       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4543     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4544       .access = PL1_RW, .resetvalue = 0xff0,
4545       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4546     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4547       .access = PL1_RW,
4548       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4549       .writefn = omap_threadid_write },
4550     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4551       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4552       .type = ARM_CP_NO_RAW,
4553       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4554     /*
4555      * TODO: Peripheral port remap register:
4556      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4557      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4558      * when MMU is off.
4559      */
4560     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4561       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4562       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4563       .writefn = omap_cachemaint_write },
4564     { .name = "C9", .cp = 15, .crn = 9,
4565       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4566       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4567 };
4568 
4569 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4570                               uint64_t value)
4571 {
4572     env->cp15.c15_cpar = value & 0x3fff;
4573 }
4574 
4575 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4576     { .name = "XSCALE_CPAR",
4577       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4578       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4579       .writefn = xscale_cpar_write, },
4580     { .name = "XSCALE_AUXCR",
4581       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4582       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4583       .resetvalue = 0, },
4584     /*
4585      * XScale specific cache-lockdown: since we have no cache we NOP these
4586      * and hope the guest does not really rely on cache behaviour.
4587      */
4588     { .name = "XSCALE_LOCK_ICACHE_LINE",
4589       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4590       .access = PL1_W, .type = ARM_CP_NOP },
4591     { .name = "XSCALE_UNLOCK_ICACHE",
4592       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4593       .access = PL1_W, .type = ARM_CP_NOP },
4594     { .name = "XSCALE_DCACHE_LOCK",
4595       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4596       .access = PL1_RW, .type = ARM_CP_NOP },
4597     { .name = "XSCALE_UNLOCK_DCACHE",
4598       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4599       .access = PL1_W, .type = ARM_CP_NOP },
4600 };
4601 
4602 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4603     /*
4604      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4605      * implementation of this implementation-defined space.
4606      * Ideally this should eventually disappear in favour of actually
4607      * implementing the correct behaviour for all cores.
4608      */
4609     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4610       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4611       .access = PL1_RW,
4612       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4613       .resetvalue = 0 },
4614 };
4615 
4616 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4617     /* Cache status: RAZ because we have no cache so it's always clean */
4618     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4619       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4620       .resetvalue = 0 },
4621 };
4622 
4623 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4624     /* We never have a block transfer operation in progress */
4625     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4626       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4627       .resetvalue = 0 },
4628     /* The cache ops themselves: these all NOP for QEMU */
4629     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4630       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4631     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4632       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4633     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4634       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4635     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4636       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4637     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4638       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4639     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4640       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4641 };
4642 
4643 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4644     /*
4645      * The cache test-and-clean instructions always return (1 << 30)
4646      * to indicate that there are no dirty cache lines.
4647      */
4648     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4649       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4650       .resetvalue = (1 << 30) },
4651     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4652       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4653       .resetvalue = (1 << 30) },
4654 };
4655 
4656 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4657     /* Ignore ReadBuffer accesses */
4658     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4659       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4660       .access = PL1_RW, .resetvalue = 0,
4661       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4662 };
4663 
4664 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4665 {
4666     unsigned int cur_el = arm_current_el(env);
4667 
4668     if (arm_is_el2_enabled(env) && cur_el == 1) {
4669         return env->cp15.vpidr_el2;
4670     }
4671     return raw_read(env, ri);
4672 }
4673 
4674 static uint64_t mpidr_read_val(CPUARMState *env)
4675 {
4676     ARMCPU *cpu = env_archcpu(env);
4677     uint64_t mpidr = cpu->mp_affinity;
4678 
4679     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4680         mpidr |= (1U << 31);
4681         /*
4682          * Cores which are uniprocessor (non-coherent)
4683          * but still implement the MP extensions set
4684          * bit 30. (For instance, Cortex-R5).
4685          */
4686         if (cpu->mp_is_up) {
4687             mpidr |= (1u << 30);
4688         }
4689     }
4690     return mpidr;
4691 }
4692 
4693 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4694 {
4695     unsigned int cur_el = arm_current_el(env);
4696 
4697     if (arm_is_el2_enabled(env) && cur_el == 1) {
4698         return env->cp15.vmpidr_el2;
4699     }
4700     return mpidr_read_val(env);
4701 }
4702 
4703 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4704     /* NOP AMAIR0/1 */
4705     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4706       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4707       .access = PL1_RW, .accessfn = access_tvm_trvm,
4708       .fgt = FGT_AMAIR_EL1,
4709       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4710       .type = ARM_CP_CONST, .resetvalue = 0 },
4711     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4712     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4713       .access = PL1_RW, .accessfn = access_tvm_trvm,
4714       .type = ARM_CP_CONST, .resetvalue = 0 },
4715     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4716       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4717       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4718                              offsetof(CPUARMState, cp15.par_ns)} },
4719     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4720       .access = PL1_RW, .accessfn = access_tvm_trvm,
4721       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4722       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4723                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4724       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4725     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4726       .access = PL1_RW, .accessfn = access_tvm_trvm,
4727       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4728       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4729                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4730       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4731 };
4732 
4733 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4734 {
4735     return vfp_get_fpcr(env);
4736 }
4737 
4738 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4739                             uint64_t value)
4740 {
4741     vfp_set_fpcr(env, value);
4742 }
4743 
4744 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4745 {
4746     return vfp_get_fpsr(env);
4747 }
4748 
4749 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4750                             uint64_t value)
4751 {
4752     vfp_set_fpsr(env, value);
4753 }
4754 
4755 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4756                                        bool isread)
4757 {
4758     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4759         return CP_ACCESS_TRAP;
4760     }
4761     return CP_ACCESS_OK;
4762 }
4763 
4764 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4765                             uint64_t value)
4766 {
4767     env->daif = value & PSTATE_DAIF;
4768 }
4769 
4770 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4771 {
4772     return env->pstate & PSTATE_PAN;
4773 }
4774 
4775 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4776                            uint64_t value)
4777 {
4778     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4779 }
4780 
4781 static const ARMCPRegInfo pan_reginfo = {
4782     .name = "PAN", .state = ARM_CP_STATE_AA64,
4783     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4784     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4785     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4786 };
4787 
4788 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4789 {
4790     return env->pstate & PSTATE_UAO;
4791 }
4792 
4793 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4794                            uint64_t value)
4795 {
4796     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4797 }
4798 
4799 static const ARMCPRegInfo uao_reginfo = {
4800     .name = "UAO", .state = ARM_CP_STATE_AA64,
4801     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4802     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4803     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4804 };
4805 
4806 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4807 {
4808     return env->pstate & PSTATE_DIT;
4809 }
4810 
4811 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4812                            uint64_t value)
4813 {
4814     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4815 }
4816 
4817 static const ARMCPRegInfo dit_reginfo = {
4818     .name = "DIT", .state = ARM_CP_STATE_AA64,
4819     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4820     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4821     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4822 };
4823 
4824 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4825 {
4826     return env->pstate & PSTATE_SSBS;
4827 }
4828 
4829 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4830                            uint64_t value)
4831 {
4832     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4833 }
4834 
4835 static const ARMCPRegInfo ssbs_reginfo = {
4836     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4837     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4838     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4839     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4840 };
4841 
4842 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4843                                               const ARMCPRegInfo *ri,
4844                                               bool isread)
4845 {
4846     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4847     switch (arm_current_el(env)) {
4848     case 0:
4849         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4850         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4851             return CP_ACCESS_TRAP;
4852         }
4853         /* fall through */
4854     case 1:
4855         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4856         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4857             return CP_ACCESS_TRAP_EL2;
4858         }
4859         break;
4860     }
4861     return CP_ACCESS_OK;
4862 }
4863 
4864 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4865 {
4866     /* Cache invalidate/clean to Point of Unification... */
4867     switch (arm_current_el(env)) {
4868     case 0:
4869         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4870         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4871             return CP_ACCESS_TRAP;
4872         }
4873         /* fall through */
4874     case 1:
4875         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4876         if (arm_hcr_el2_eff(env) & hcrflags) {
4877             return CP_ACCESS_TRAP_EL2;
4878         }
4879         break;
4880     }
4881     return CP_ACCESS_OK;
4882 }
4883 
4884 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4885                                    bool isread)
4886 {
4887     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4888 }
4889 
4890 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4891                                   bool isread)
4892 {
4893     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4894 }
4895 
4896 /*
4897  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4898  * Page D4-1736 (DDI0487A.b)
4899  */
4900 
4901 static int vae1_tlbmask(CPUARMState *env)
4902 {
4903     uint64_t hcr = arm_hcr_el2_eff(env);
4904     uint16_t mask;
4905 
4906     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4907         mask = ARMMMUIdxBit_E20_2 |
4908                ARMMMUIdxBit_E20_2_PAN |
4909                ARMMMUIdxBit_E20_0;
4910     } else {
4911         mask = ARMMMUIdxBit_E10_1 |
4912                ARMMMUIdxBit_E10_1_PAN |
4913                ARMMMUIdxBit_E10_0;
4914     }
4915     return mask;
4916 }
4917 
4918 static int vae2_tlbmask(CPUARMState *env)
4919 {
4920     uint64_t hcr = arm_hcr_el2_eff(env);
4921     uint16_t mask;
4922 
4923     if (hcr & HCR_E2H) {
4924         mask = ARMMMUIdxBit_E20_2 |
4925                ARMMMUIdxBit_E20_2_PAN |
4926                ARMMMUIdxBit_E20_0;
4927     } else {
4928         mask = ARMMMUIdxBit_E2;
4929     }
4930     return mask;
4931 }
4932 
4933 /* Return 56 if TBI is enabled, 64 otherwise. */
4934 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4935                               uint64_t addr)
4936 {
4937     uint64_t tcr = regime_tcr(env, mmu_idx);
4938     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4939     int select = extract64(addr, 55, 1);
4940 
4941     return (tbi >> select) & 1 ? 56 : 64;
4942 }
4943 
4944 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4945 {
4946     uint64_t hcr = arm_hcr_el2_eff(env);
4947     ARMMMUIdx mmu_idx;
4948 
4949     /* Only the regime of the mmu_idx below is significant. */
4950     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4951         mmu_idx = ARMMMUIdx_E20_0;
4952     } else {
4953         mmu_idx = ARMMMUIdx_E10_0;
4954     }
4955 
4956     return tlbbits_for_regime(env, mmu_idx, addr);
4957 }
4958 
4959 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4960 {
4961     uint64_t hcr = arm_hcr_el2_eff(env);
4962     ARMMMUIdx mmu_idx;
4963 
4964     /*
4965      * Only the regime of the mmu_idx below is significant.
4966      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4967      * only has one.
4968      */
4969     if (hcr & HCR_E2H) {
4970         mmu_idx = ARMMMUIdx_E20_2;
4971     } else {
4972         mmu_idx = ARMMMUIdx_E2;
4973     }
4974 
4975     return tlbbits_for_regime(env, mmu_idx, addr);
4976 }
4977 
4978 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4979                                       uint64_t value)
4980 {
4981     CPUState *cs = env_cpu(env);
4982     int mask = vae1_tlbmask(env);
4983 
4984     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4985 }
4986 
4987 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4988                                     uint64_t value)
4989 {
4990     CPUState *cs = env_cpu(env);
4991     int mask = vae1_tlbmask(env);
4992 
4993     if (tlb_force_broadcast(env)) {
4994         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4995     } else {
4996         tlb_flush_by_mmuidx(cs, mask);
4997     }
4998 }
4999 
5000 static int e2_tlbmask(CPUARMState *env)
5001 {
5002     return (ARMMMUIdxBit_E20_0 |
5003             ARMMMUIdxBit_E20_2 |
5004             ARMMMUIdxBit_E20_2_PAN |
5005             ARMMMUIdxBit_E2);
5006 }
5007 
5008 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5009                                   uint64_t value)
5010 {
5011     CPUState *cs = env_cpu(env);
5012     int mask = alle1_tlbmask(env);
5013 
5014     tlb_flush_by_mmuidx(cs, mask);
5015 }
5016 
5017 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5018                                   uint64_t value)
5019 {
5020     CPUState *cs = env_cpu(env);
5021     int mask = e2_tlbmask(env);
5022 
5023     tlb_flush_by_mmuidx(cs, mask);
5024 }
5025 
5026 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5027                                   uint64_t value)
5028 {
5029     ARMCPU *cpu = env_archcpu(env);
5030     CPUState *cs = CPU(cpu);
5031 
5032     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
5033 }
5034 
5035 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5036                                     uint64_t value)
5037 {
5038     CPUState *cs = env_cpu(env);
5039     int mask = alle1_tlbmask(env);
5040 
5041     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5042 }
5043 
5044 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5045                                     uint64_t value)
5046 {
5047     CPUState *cs = env_cpu(env);
5048     int mask = e2_tlbmask(env);
5049 
5050     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5051 }
5052 
5053 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5054                                     uint64_t value)
5055 {
5056     CPUState *cs = env_cpu(env);
5057 
5058     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
5059 }
5060 
5061 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5062                                  uint64_t value)
5063 {
5064     /*
5065      * Invalidate by VA, EL2
5066      * Currently handles both VAE2 and VALE2, since we don't support
5067      * flush-last-level-only.
5068      */
5069     CPUState *cs = env_cpu(env);
5070     int mask = vae2_tlbmask(env);
5071     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5072     int bits = vae2_tlbbits(env, pageaddr);
5073 
5074     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5075 }
5076 
5077 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5078                                  uint64_t value)
5079 {
5080     /*
5081      * Invalidate by VA, EL3
5082      * Currently handles both VAE3 and VALE3, since we don't support
5083      * flush-last-level-only.
5084      */
5085     ARMCPU *cpu = env_archcpu(env);
5086     CPUState *cs = CPU(cpu);
5087     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5088 
5089     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
5090 }
5091 
5092 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5093                                    uint64_t value)
5094 {
5095     CPUState *cs = env_cpu(env);
5096     int mask = vae1_tlbmask(env);
5097     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5098     int bits = vae1_tlbbits(env, pageaddr);
5099 
5100     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5101 }
5102 
5103 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5104                                  uint64_t value)
5105 {
5106     /*
5107      * Invalidate by VA, EL1&0 (AArch64 version).
5108      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
5109      * since we don't support flush-for-specific-ASID-only or
5110      * flush-last-level-only.
5111      */
5112     CPUState *cs = env_cpu(env);
5113     int mask = vae1_tlbmask(env);
5114     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5115     int bits = vae1_tlbbits(env, pageaddr);
5116 
5117     if (tlb_force_broadcast(env)) {
5118         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5119     } else {
5120         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5121     }
5122 }
5123 
5124 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5125                                    uint64_t value)
5126 {
5127     CPUState *cs = env_cpu(env);
5128     int mask = vae2_tlbmask(env);
5129     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5130     int bits = vae2_tlbbits(env, pageaddr);
5131 
5132     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5133 }
5134 
5135 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5136                                    uint64_t value)
5137 {
5138     CPUState *cs = env_cpu(env);
5139     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5140     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
5141 
5142     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
5143                                                   ARMMMUIdxBit_E3, bits);
5144 }
5145 
5146 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
5147 {
5148     /*
5149      * The MSB of value is the NS field, which only applies if SEL2
5150      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5151      */
5152     return (value >= 0
5153             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5154             && arm_is_secure_below_el3(env)
5155             ? ARMMMUIdxBit_Stage2_S
5156             : ARMMMUIdxBit_Stage2);
5157 }
5158 
5159 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5160                                     uint64_t value)
5161 {
5162     CPUState *cs = env_cpu(env);
5163     int mask = ipas2e1_tlbmask(env, value);
5164     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5165 
5166     if (tlb_force_broadcast(env)) {
5167         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5168     } else {
5169         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5170     }
5171 }
5172 
5173 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5174                                       uint64_t value)
5175 {
5176     CPUState *cs = env_cpu(env);
5177     int mask = ipas2e1_tlbmask(env, value);
5178     uint64_t pageaddr = sextract64(value << 12, 0, 56);
5179 
5180     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5181 }
5182 
5183 #ifdef TARGET_AARCH64
5184 typedef struct {
5185     uint64_t base;
5186     uint64_t length;
5187 } TLBIRange;
5188 
5189 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5190 {
5191     /*
5192      * Note that the TLBI range TG field encoding differs from both
5193      * TG0 and TG1 encodings.
5194      */
5195     switch (tg) {
5196     case 1:
5197         return Gran4K;
5198     case 2:
5199         return Gran16K;
5200     case 3:
5201         return Gran64K;
5202     default:
5203         return GranInvalid;
5204     }
5205 }
5206 
5207 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5208                                      uint64_t value)
5209 {
5210     unsigned int page_size_granule, page_shift, num, scale, exponent;
5211     /* Extract one bit to represent the va selector in use. */
5212     uint64_t select = sextract64(value, 36, 1);
5213     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5214     TLBIRange ret = { };
5215     ARMGranuleSize gran;
5216 
5217     page_size_granule = extract64(value, 46, 2);
5218     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5219 
5220     /* The granule encoded in value must match the granule in use. */
5221     if (gran != param.gran) {
5222         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5223                       page_size_granule);
5224         return ret;
5225     }
5226 
5227     page_shift = arm_granule_bits(gran);
5228     num = extract64(value, 39, 5);
5229     scale = extract64(value, 44, 2);
5230     exponent = (5 * scale) + 1;
5231 
5232     ret.length = (num + 1) << (exponent + page_shift);
5233 
5234     if (param.select) {
5235         ret.base = sextract64(value, 0, 37);
5236     } else {
5237         ret.base = extract64(value, 0, 37);
5238     }
5239     if (param.ds) {
5240         /*
5241          * With DS=1, BaseADDR is always shifted 16 so that it is able
5242          * to address all 52 va bits.  The input address is perforce
5243          * aligned on a 64k boundary regardless of translation granule.
5244          */
5245         page_shift = 16;
5246     }
5247     ret.base <<= page_shift;
5248 
5249     return ret;
5250 }
5251 
5252 static void do_rvae_write(CPUARMState *env, uint64_t value,
5253                           int idxmap, bool synced)
5254 {
5255     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5256     TLBIRange range;
5257     int bits;
5258 
5259     range = tlbi_aa64_get_range(env, one_idx, value);
5260     bits = tlbbits_for_regime(env, one_idx, range.base);
5261 
5262     if (synced) {
5263         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5264                                                   range.base,
5265                                                   range.length,
5266                                                   idxmap,
5267                                                   bits);
5268     } else {
5269         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5270                                   range.length, idxmap, bits);
5271     }
5272 }
5273 
5274 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5275                                   const ARMCPRegInfo *ri,
5276                                   uint64_t value)
5277 {
5278     /*
5279      * Invalidate by VA range, EL1&0.
5280      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5281      * since we don't support flush-for-specific-ASID-only or
5282      * flush-last-level-only.
5283      */
5284 
5285     do_rvae_write(env, value, vae1_tlbmask(env),
5286                   tlb_force_broadcast(env));
5287 }
5288 
5289 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5290                                     const ARMCPRegInfo *ri,
5291                                     uint64_t value)
5292 {
5293     /*
5294      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5295      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5296      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5297      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5298      * shareable specific flushes.
5299      */
5300 
5301     do_rvae_write(env, value, vae1_tlbmask(env), true);
5302 }
5303 
5304 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5305                                   const ARMCPRegInfo *ri,
5306                                   uint64_t value)
5307 {
5308     /*
5309      * Invalidate by VA range, EL2.
5310      * Currently handles all of RVAE2 and RVALE2,
5311      * since we don't support flush-for-specific-ASID-only or
5312      * flush-last-level-only.
5313      */
5314 
5315     do_rvae_write(env, value, vae2_tlbmask(env),
5316                   tlb_force_broadcast(env));
5317 
5318 
5319 }
5320 
5321 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5322                                     const ARMCPRegInfo *ri,
5323                                     uint64_t value)
5324 {
5325     /*
5326      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5327      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5328      * since we don't support flush-for-specific-ASID-only,
5329      * flush-last-level-only or inner/outer shareable specific flushes.
5330      */
5331 
5332     do_rvae_write(env, value, vae2_tlbmask(env), true);
5333 
5334 }
5335 
5336 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5337                                   const ARMCPRegInfo *ri,
5338                                   uint64_t value)
5339 {
5340     /*
5341      * Invalidate by VA range, EL3.
5342      * Currently handles all of RVAE3 and RVALE3,
5343      * since we don't support flush-for-specific-ASID-only or
5344      * flush-last-level-only.
5345      */
5346 
5347     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5348 }
5349 
5350 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5351                                     const ARMCPRegInfo *ri,
5352                                     uint64_t value)
5353 {
5354     /*
5355      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5356      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5357      * since we don't support flush-for-specific-ASID-only,
5358      * flush-last-level-only or inner/outer specific flushes.
5359      */
5360 
5361     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5362 }
5363 
5364 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5365                                      uint64_t value)
5366 {
5367     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5368                   tlb_force_broadcast(env));
5369 }
5370 
5371 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5372                                        const ARMCPRegInfo *ri,
5373                                        uint64_t value)
5374 {
5375     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5376 }
5377 #endif
5378 
5379 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5380                                       bool isread)
5381 {
5382     int cur_el = arm_current_el(env);
5383 
5384     if (cur_el < 2) {
5385         uint64_t hcr = arm_hcr_el2_eff(env);
5386 
5387         if (cur_el == 0) {
5388             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5389                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5390                     return CP_ACCESS_TRAP_EL2;
5391                 }
5392             } else {
5393                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5394                     return CP_ACCESS_TRAP;
5395                 }
5396                 if (hcr & HCR_TDZ) {
5397                     return CP_ACCESS_TRAP_EL2;
5398                 }
5399             }
5400         } else if (hcr & HCR_TDZ) {
5401             return CP_ACCESS_TRAP_EL2;
5402         }
5403     }
5404     return CP_ACCESS_OK;
5405 }
5406 
5407 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5408 {
5409     ARMCPU *cpu = env_archcpu(env);
5410     int dzp_bit = 1 << 4;
5411 
5412     /* DZP indicates whether DC ZVA access is allowed */
5413     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5414         dzp_bit = 0;
5415     }
5416     return cpu->dcz_blocksize | dzp_bit;
5417 }
5418 
5419 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5420                                     bool isread)
5421 {
5422     if (!(env->pstate & PSTATE_SP)) {
5423         /*
5424          * Access to SP_EL0 is undefined if it's being used as
5425          * the stack pointer.
5426          */
5427         return CP_ACCESS_TRAP_UNCATEGORIZED;
5428     }
5429     return CP_ACCESS_OK;
5430 }
5431 
5432 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5433 {
5434     return env->pstate & PSTATE_SP;
5435 }
5436 
5437 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5438 {
5439     update_spsel(env, val);
5440 }
5441 
5442 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5443                         uint64_t value)
5444 {
5445     ARMCPU *cpu = env_archcpu(env);
5446 
5447     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5448         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5449         value &= ~SCTLR_M;
5450     }
5451 
5452     /* ??? Lots of these bits are not implemented.  */
5453 
5454     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5455         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5456             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5457         } else {
5458             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5459                        SCTLR_ATA0 | SCTLR_ATA);
5460         }
5461     }
5462 
5463     if (raw_read(env, ri) == value) {
5464         /*
5465          * Skip the TLB flush if nothing actually changed; Linux likes
5466          * to do a lot of pointless SCTLR writes.
5467          */
5468         return;
5469     }
5470 
5471     raw_write(env, ri, value);
5472 
5473     /* This may enable/disable the MMU, so do a TLB flush.  */
5474     tlb_flush(CPU(cpu));
5475 
5476     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5477         /*
5478          * Normally we would always end the TB on an SCTLR write; see the
5479          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5480          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5481          * of hflags from the translator, so do it here.
5482          */
5483         arm_rebuild_hflags(env);
5484     }
5485 }
5486 
5487 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5488                            uint64_t value)
5489 {
5490     /*
5491      * Some MDCR_EL3 bits affect whether PMU counters are running:
5492      * if we are trying to change any of those then we must
5493      * bracket this update with PMU start/finish calls.
5494      */
5495     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5496 
5497     if (pmu_op) {
5498         pmu_op_start(env);
5499     }
5500     env->cp15.mdcr_el3 = value;
5501     if (pmu_op) {
5502         pmu_op_finish(env);
5503     }
5504 }
5505 
5506 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5507                        uint64_t value)
5508 {
5509     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5510     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5511 }
5512 
5513 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5514                            uint64_t value)
5515 {
5516     /*
5517      * Some MDCR_EL2 bits affect whether PMU counters are running:
5518      * if we are trying to change any of those then we must
5519      * bracket this update with PMU start/finish calls.
5520      */
5521     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5522 
5523     if (pmu_op) {
5524         pmu_op_start(env);
5525     }
5526     env->cp15.mdcr_el2 = value;
5527     if (pmu_op) {
5528         pmu_op_finish(env);
5529     }
5530 }
5531 
5532 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5533                                  bool isread)
5534 {
5535     if (arm_current_el(env) == 1) {
5536         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5537 
5538         if (hcr_nv == (HCR_NV | HCR_NV1)) {
5539             return CP_ACCESS_TRAP_EL2;
5540         }
5541     }
5542     return CP_ACCESS_OK;
5543 }
5544 
5545 #ifdef CONFIG_USER_ONLY
5546 /*
5547  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5548  * code to get around W^X restrictions, where one region is writable and the
5549  * other is executable.
5550  *
5551  * Since the executable region is never written to we cannot detect code
5552  * changes when running in user mode, and rely on the emulated JIT telling us
5553  * that the code has changed by executing this instruction.
5554  */
5555 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5556                           uint64_t value)
5557 {
5558     uint64_t icache_line_mask, start_address, end_address;
5559     const ARMCPU *cpu;
5560 
5561     cpu = env_archcpu(env);
5562 
5563     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5564     start_address = value & ~icache_line_mask;
5565     end_address = value | icache_line_mask;
5566 
5567     mmap_lock();
5568 
5569     tb_invalidate_phys_range(start_address, end_address);
5570 
5571     mmap_unlock();
5572 }
5573 #endif
5574 
5575 static const ARMCPRegInfo v8_cp_reginfo[] = {
5576     /*
5577      * Minimal set of EL0-visible registers. This will need to be expanded
5578      * significantly for system emulation of AArch64 CPUs.
5579      */
5580     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5581       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5582       .access = PL0_RW, .type = ARM_CP_NZCV },
5583     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5584       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5585       .type = ARM_CP_NO_RAW,
5586       .access = PL0_RW, .accessfn = aa64_daif_access,
5587       .fieldoffset = offsetof(CPUARMState, daif),
5588       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5589     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5590       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5591       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5592       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5593     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5594       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5595       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5596       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5597     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5598       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5599       .access = PL0_R, .type = ARM_CP_NO_RAW,
5600       .fgt = FGT_DCZID_EL0,
5601       .readfn = aa64_dczid_read },
5602     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5603       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5604       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5605 #ifndef CONFIG_USER_ONLY
5606       /* Avoid overhead of an access check that always passes in user-mode */
5607       .accessfn = aa64_zva_access,
5608       .fgt = FGT_DCZVA,
5609 #endif
5610     },
5611     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5612       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5613       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5614     /*
5615      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5616      * don't emulate caches.
5617      */
5618     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5619       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5620       .access = PL1_W, .type = ARM_CP_NOP,
5621       .fgt = FGT_ICIALLUIS,
5622       .accessfn = access_ticab },
5623     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5624       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5625       .access = PL1_W, .type = ARM_CP_NOP,
5626       .fgt = FGT_ICIALLU,
5627       .accessfn = access_tocu },
5628     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5629       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5630       .access = PL0_W,
5631       .fgt = FGT_ICIVAU,
5632       .accessfn = access_tocu,
5633 #ifdef CONFIG_USER_ONLY
5634       .type = ARM_CP_NO_RAW,
5635       .writefn = ic_ivau_write
5636 #else
5637       .type = ARM_CP_NOP
5638 #endif
5639     },
5640     /* Cache ops: all NOPs since we don't emulate caches */
5641     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5642       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5643       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5644       .fgt = FGT_DCIVAC,
5645       .type = ARM_CP_NOP },
5646     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5647       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5648       .fgt = FGT_DCISW,
5649       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5650     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5651       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5652       .access = PL0_W, .type = ARM_CP_NOP,
5653       .fgt = FGT_DCCVAC,
5654       .accessfn = aa64_cacheop_poc_access },
5655     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5656       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5657       .fgt = FGT_DCCSW,
5658       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5659     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5660       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5661       .access = PL0_W, .type = ARM_CP_NOP,
5662       .fgt = FGT_DCCVAU,
5663       .accessfn = access_tocu },
5664     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5665       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5666       .access = PL0_W, .type = ARM_CP_NOP,
5667       .fgt = FGT_DCCIVAC,
5668       .accessfn = aa64_cacheop_poc_access },
5669     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5670       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5671       .fgt = FGT_DCCISW,
5672       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5673     /* TLBI operations */
5674     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5675       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5676       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5677       .fgt = FGT_TLBIVMALLE1IS,
5678       .writefn = tlbi_aa64_vmalle1is_write },
5679     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5680       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5681       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5682       .fgt = FGT_TLBIVAE1IS,
5683       .writefn = tlbi_aa64_vae1is_write },
5684     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5685       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5686       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5687       .fgt = FGT_TLBIASIDE1IS,
5688       .writefn = tlbi_aa64_vmalle1is_write },
5689     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5690       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5691       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5692       .fgt = FGT_TLBIVAAE1IS,
5693       .writefn = tlbi_aa64_vae1is_write },
5694     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5695       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5696       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5697       .fgt = FGT_TLBIVALE1IS,
5698       .writefn = tlbi_aa64_vae1is_write },
5699     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5700       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5701       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5702       .fgt = FGT_TLBIVAALE1IS,
5703       .writefn = tlbi_aa64_vae1is_write },
5704     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5705       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5706       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5707       .fgt = FGT_TLBIVMALLE1,
5708       .writefn = tlbi_aa64_vmalle1_write },
5709     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5710       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5711       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5712       .fgt = FGT_TLBIVAE1,
5713       .writefn = tlbi_aa64_vae1_write },
5714     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5715       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5716       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5717       .fgt = FGT_TLBIASIDE1,
5718       .writefn = tlbi_aa64_vmalle1_write },
5719     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5721       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5722       .fgt = FGT_TLBIVAAE1,
5723       .writefn = tlbi_aa64_vae1_write },
5724     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5725       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5726       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5727       .fgt = FGT_TLBIVALE1,
5728       .writefn = tlbi_aa64_vae1_write },
5729     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5730       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5731       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5732       .fgt = FGT_TLBIVAALE1,
5733       .writefn = tlbi_aa64_vae1_write },
5734     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5735       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5736       .access = PL2_W, .type = ARM_CP_NO_RAW,
5737       .writefn = tlbi_aa64_ipas2e1is_write },
5738     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5739       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5740       .access = PL2_W, .type = ARM_CP_NO_RAW,
5741       .writefn = tlbi_aa64_ipas2e1is_write },
5742     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5743       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5744       .access = PL2_W, .type = ARM_CP_NO_RAW,
5745       .writefn = tlbi_aa64_alle1is_write },
5746     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5747       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5748       .access = PL2_W, .type = ARM_CP_NO_RAW,
5749       .writefn = tlbi_aa64_alle1is_write },
5750     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5751       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5752       .access = PL2_W, .type = ARM_CP_NO_RAW,
5753       .writefn = tlbi_aa64_ipas2e1_write },
5754     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5755       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5756       .access = PL2_W, .type = ARM_CP_NO_RAW,
5757       .writefn = tlbi_aa64_ipas2e1_write },
5758     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5760       .access = PL2_W, .type = ARM_CP_NO_RAW,
5761       .writefn = tlbi_aa64_alle1_write },
5762     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5763       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5764       .access = PL2_W, .type = ARM_CP_NO_RAW,
5765       .writefn = tlbi_aa64_alle1is_write },
5766 #ifndef CONFIG_USER_ONLY
5767     /* 64 bit address translation operations */
5768     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5769       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5770       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5771       .fgt = FGT_ATS1E1R,
5772       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5773     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5774       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5775       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5776       .fgt = FGT_ATS1E1W,
5777       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5778     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5779       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5780       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5781       .fgt = FGT_ATS1E0R,
5782       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5783     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5784       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5785       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5786       .fgt = FGT_ATS1E0W,
5787       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5788     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5789       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5790       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5791       .accessfn = at_e012_access, .writefn = ats_write64 },
5792     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5793       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5794       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5795       .accessfn = at_e012_access, .writefn = ats_write64 },
5796     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5797       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5798       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5799       .accessfn = at_e012_access, .writefn = ats_write64 },
5800     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5801       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5802       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5803       .accessfn = at_e012_access, .writefn = ats_write64 },
5804     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5805     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5806       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5807       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5808       .writefn = ats_write64 },
5809     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5810       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5811       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5812       .writefn = ats_write64 },
5813     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5814       .type = ARM_CP_ALIAS,
5815       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5816       .access = PL1_RW, .resetvalue = 0,
5817       .fgt = FGT_PAR_EL1,
5818       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5819       .writefn = par_write },
5820 #endif
5821     /* TLB invalidate last level of translation table walk */
5822     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5823       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5824       .writefn = tlbimva_is_write },
5825     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5826       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5827       .writefn = tlbimvaa_is_write },
5828     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5829       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5830       .writefn = tlbimva_write },
5831     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5832       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5833       .writefn = tlbimvaa_write },
5834     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5835       .type = ARM_CP_NO_RAW, .access = PL2_W,
5836       .writefn = tlbimva_hyp_write },
5837     { .name = "TLBIMVALHIS",
5838       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5839       .type = ARM_CP_NO_RAW, .access = PL2_W,
5840       .writefn = tlbimva_hyp_is_write },
5841     { .name = "TLBIIPAS2",
5842       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5843       .type = ARM_CP_NO_RAW, .access = PL2_W,
5844       .writefn = tlbiipas2_hyp_write },
5845     { .name = "TLBIIPAS2IS",
5846       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5847       .type = ARM_CP_NO_RAW, .access = PL2_W,
5848       .writefn = tlbiipas2is_hyp_write },
5849     { .name = "TLBIIPAS2L",
5850       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5851       .type = ARM_CP_NO_RAW, .access = PL2_W,
5852       .writefn = tlbiipas2_hyp_write },
5853     { .name = "TLBIIPAS2LIS",
5854       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5855       .type = ARM_CP_NO_RAW, .access = PL2_W,
5856       .writefn = tlbiipas2is_hyp_write },
5857     /* 32 bit cache operations */
5858     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5859       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5860     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5861       .type = ARM_CP_NOP, .access = PL1_W },
5862     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5863       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5864     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5865       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5866     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5867       .type = ARM_CP_NOP, .access = PL1_W },
5868     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5869       .type = ARM_CP_NOP, .access = PL1_W },
5870     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5871       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5872     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5873       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5874     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5875       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5876     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5877       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5878     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5879       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5880     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5881       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5882     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5883       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5884     /* MMU Domain access control / MPU write buffer control */
5885     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5886       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5887       .writefn = dacr_write, .raw_writefn = raw_write,
5888       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5889                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5890     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5891       .type = ARM_CP_ALIAS,
5892       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5893       .access = PL1_RW, .accessfn = access_nv1,
5894       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5895       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5896     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5897       .type = ARM_CP_ALIAS,
5898       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5899       .access = PL1_RW, .accessfn = access_nv1,
5900       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5901       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5902     /*
5903      * We rely on the access checks not allowing the guest to write to the
5904      * state field when SPSel indicates that it's being used as the stack
5905      * pointer.
5906      */
5907     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5908       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5909       .access = PL1_RW, .accessfn = sp_el0_access,
5910       .type = ARM_CP_ALIAS,
5911       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5912     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5913       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5914       .nv2_redirect_offset = 0x240,
5915       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5916       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5917     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5918       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5919       .type = ARM_CP_NO_RAW,
5920       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5921     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5922       .type = ARM_CP_ALIAS,
5923       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5924       .access = PL2_RW,
5925       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5926     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5927       .type = ARM_CP_ALIAS,
5928       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5929       .access = PL2_RW,
5930       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5931     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5932       .type = ARM_CP_ALIAS,
5933       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5934       .access = PL2_RW,
5935       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5936     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5937       .type = ARM_CP_ALIAS,
5938       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5939       .access = PL2_RW,
5940       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5941     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5942       .type = ARM_CP_IO,
5943       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5944       .resetvalue = 0,
5945       .access = PL3_RW,
5946       .writefn = mdcr_el3_write,
5947       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5948     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5949       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5950       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5951       .writefn = sdcr_write,
5952       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5953 };
5954 
5955 /* These are present only when EL1 supports AArch32 */
5956 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5957     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5958       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5959       .access = PL2_RW,
5960       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5961       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5962     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5963       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5964       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5965       .writefn = dacr_write, .raw_writefn = raw_write,
5966       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5967     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5968       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5969       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5970       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5971 };
5972 
5973 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5974 {
5975     ARMCPU *cpu = env_archcpu(env);
5976 
5977     if (arm_feature(env, ARM_FEATURE_V8)) {
5978         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5979     } else {
5980         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5981     }
5982 
5983     if (arm_feature(env, ARM_FEATURE_EL3)) {
5984         valid_mask &= ~HCR_HCD;
5985     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5986         /*
5987          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5988          * However, if we're using the SMC PSCI conduit then QEMU is
5989          * effectively acting like EL3 firmware and so the guest at
5990          * EL2 should retain the ability to prevent EL1 from being
5991          * able to make SMC calls into the ersatz firmware, so in
5992          * that case HCR.TSC should be read/write.
5993          */
5994         valid_mask &= ~HCR_TSC;
5995     }
5996 
5997     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5998         if (cpu_isar_feature(aa64_vh, cpu)) {
5999             valid_mask |= HCR_E2H;
6000         }
6001         if (cpu_isar_feature(aa64_ras, cpu)) {
6002             valid_mask |= HCR_TERR | HCR_TEA;
6003         }
6004         if (cpu_isar_feature(aa64_lor, cpu)) {
6005             valid_mask |= HCR_TLOR;
6006         }
6007         if (cpu_isar_feature(aa64_pauth, cpu)) {
6008             valid_mask |= HCR_API | HCR_APK;
6009         }
6010         if (cpu_isar_feature(aa64_mte, cpu)) {
6011             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
6012         }
6013         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
6014             valid_mask |= HCR_ENSCXT;
6015         }
6016         if (cpu_isar_feature(aa64_fwb, cpu)) {
6017             valid_mask |= HCR_FWB;
6018         }
6019         if (cpu_isar_feature(aa64_rme, cpu)) {
6020             valid_mask |= HCR_GPF;
6021         }
6022         if (cpu_isar_feature(aa64_nv, cpu)) {
6023             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
6024         }
6025         if (cpu_isar_feature(aa64_nv2, cpu)) {
6026             valid_mask |= HCR_NV2;
6027         }
6028     }
6029 
6030     if (cpu_isar_feature(any_evt, cpu)) {
6031         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
6032     } else if (cpu_isar_feature(any_half_evt, cpu)) {
6033         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
6034     }
6035 
6036     /* Clear RES0 bits.  */
6037     value &= valid_mask;
6038 
6039     /*
6040      * These bits change the MMU setup:
6041      * HCR_VM enables stage 2 translation
6042      * HCR_PTW forbids certain page-table setups
6043      * HCR_DC disables stage1 and enables stage2 translation
6044      * HCR_DCT enables tagging on (disabled) stage1 translation
6045      * HCR_FWB changes the interpretation of stage2 descriptor bits
6046      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
6047      */
6048     if ((env->cp15.hcr_el2 ^ value) &
6049         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
6050         tlb_flush(CPU(cpu));
6051     }
6052     env->cp15.hcr_el2 = value;
6053 
6054     /*
6055      * Updates to VI and VF require us to update the status of
6056      * virtual interrupts, which are the logical OR of these bits
6057      * and the state of the input lines from the GIC. (This requires
6058      * that we have the BQL, which is done by marking the
6059      * reginfo structs as ARM_CP_IO.)
6060      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
6061      * VFNMI, it is never possible for it to be taken immediately
6062      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
6063      * at EL0 or EL1, and HCR can only be written at EL2.
6064      */
6065     g_assert(bql_locked());
6066     arm_cpu_update_virq(cpu);
6067     arm_cpu_update_vfiq(cpu);
6068     arm_cpu_update_vserr(cpu);
6069     if (cpu_isar_feature(aa64_nmi, cpu)) {
6070         arm_cpu_update_vinmi(cpu);
6071         arm_cpu_update_vfnmi(cpu);
6072     }
6073 }
6074 
6075 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
6076 {
6077     do_hcr_write(env, value, 0);
6078 }
6079 
6080 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
6081                           uint64_t value)
6082 {
6083     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
6084     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
6085     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
6086 }
6087 
6088 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
6089                          uint64_t value)
6090 {
6091     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
6092     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
6093     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
6094 }
6095 
6096 /*
6097  * Return the effective value of HCR_EL2, at the given security state.
6098  * Bits that are not included here:
6099  * RW       (read from SCR_EL3.RW as needed)
6100  */
6101 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
6102 {
6103     uint64_t ret = env->cp15.hcr_el2;
6104 
6105     assert(space != ARMSS_Root);
6106 
6107     if (!arm_is_el2_enabled_secstate(env, space)) {
6108         /*
6109          * "This register has no effect if EL2 is not enabled in the
6110          * current Security state".  This is ARMv8.4-SecEL2 speak for
6111          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
6112          *
6113          * Prior to that, the language was "In an implementation that
6114          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
6115          * as if this field is 0 for all purposes other than a direct
6116          * read or write access of HCR_EL2".  With lots of enumeration
6117          * on a per-field basis.  In current QEMU, this is condition
6118          * is arm_is_secure_below_el3.
6119          *
6120          * Since the v8.4 language applies to the entire register, and
6121          * appears to be backward compatible, use that.
6122          */
6123         return 0;
6124     }
6125 
6126     /*
6127      * For a cpu that supports both aarch64 and aarch32, we can set bits
6128      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
6129      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
6130      */
6131     if (!arm_el_is_aa64(env, 2)) {
6132         uint64_t aa32_valid;
6133 
6134         /*
6135          * These bits are up-to-date as of ARMv8.6.
6136          * For HCR, it's easiest to list just the 2 bits that are invalid.
6137          * For HCR2, list those that are valid.
6138          */
6139         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
6140         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
6141                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
6142         ret &= aa32_valid;
6143     }
6144 
6145     if (ret & HCR_TGE) {
6146         /* These bits are up-to-date as of ARMv8.6.  */
6147         if (ret & HCR_E2H) {
6148             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
6149                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
6150                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
6151                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
6152                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6153                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6154         } else {
6155             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6156         }
6157         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6158                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6159                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6160                  HCR_TLOR);
6161     }
6162 
6163     return ret;
6164 }
6165 
6166 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6167 {
6168     if (arm_feature(env, ARM_FEATURE_M)) {
6169         return 0;
6170     }
6171     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6172 }
6173 
6174 /*
6175  * Corresponds to ARM pseudocode function ELIsInHost().
6176  */
6177 bool el_is_in_host(CPUARMState *env, int el)
6178 {
6179     uint64_t mask;
6180 
6181     /*
6182      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6183      * Perform the simplest bit tests first, and validate EL2 afterward.
6184      */
6185     if (el & 1) {
6186         return false; /* EL1 or EL3 */
6187     }
6188 
6189     /*
6190      * Note that hcr_write() checks isar_feature_aa64_vh(),
6191      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6192      */
6193     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6194     if ((env->cp15.hcr_el2 & mask) != mask) {
6195         return false;
6196     }
6197 
6198     /* TGE and/or E2H set: double check those bits are currently legal. */
6199     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6200 }
6201 
6202 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6203                        uint64_t value)
6204 {
6205     ARMCPU *cpu = env_archcpu(env);
6206     uint64_t valid_mask = 0;
6207 
6208     /* FEAT_MOPS adds MSCEn and MCE2 */
6209     if (cpu_isar_feature(aa64_mops, cpu)) {
6210         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6211     }
6212 
6213     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
6214     if (cpu_isar_feature(aa64_nmi, cpu)) {
6215         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
6216     }
6217 
6218     /* Clear RES0 bits.  */
6219     env->cp15.hcrx_el2 = value & valid_mask;
6220 
6221     /*
6222      * Updates to VINMI and VFNMI require us to update the status of
6223      * virtual NMI, which are the logical OR of these bits
6224      * and the state of the input lines from the GIC. (This requires
6225      * that we have the BQL, which is done by marking the
6226      * reginfo structs as ARM_CP_IO.)
6227      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
6228      * possible for it to be taken immediately, because VINMI and
6229      * VFNMI are masked unless running at EL0 or EL1, and HCRX
6230      * can only be written at EL2.
6231      */
6232     if (cpu_isar_feature(aa64_nmi, cpu)) {
6233         g_assert(bql_locked());
6234         arm_cpu_update_vinmi(cpu);
6235         arm_cpu_update_vfnmi(cpu);
6236     }
6237 }
6238 
6239 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6240                                   bool isread)
6241 {
6242     if (arm_current_el(env) == 2
6243         && arm_feature(env, ARM_FEATURE_EL3)
6244         && !(env->cp15.scr_el3 & SCR_HXEN)) {
6245         return CP_ACCESS_TRAP_EL3;
6246     }
6247     return CP_ACCESS_OK;
6248 }
6249 
6250 static const ARMCPRegInfo hcrx_el2_reginfo = {
6251     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6252     .type = ARM_CP_IO,
6253     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6254     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6255     .nv2_redirect_offset = 0xa0,
6256     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6257 };
6258 
6259 /* Return the effective value of HCRX_EL2.  */
6260 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6261 {
6262     /*
6263      * The bits in this register behave as 0 for all purposes other than
6264      * direct reads of the register if SCR_EL3.HXEn is 0.
6265      * If EL2 is not enabled in the current security state, then the
6266      * bit may behave as if 0, or as if 1, depending on the bit.
6267      * For the moment, we treat the EL2-disabled case as taking
6268      * priority over the HXEn-disabled case. This is true for the only
6269      * bit for a feature which we implement where the answer is different
6270      * for the two cases (MSCEn for FEAT_MOPS).
6271      * This may need to be revisited for future bits.
6272      */
6273     if (!arm_is_el2_enabled(env)) {
6274         uint64_t hcrx = 0;
6275         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6276             /* MSCEn behaves as 1 if EL2 is not enabled */
6277             hcrx |= HCRX_MSCEN;
6278         }
6279         return hcrx;
6280     }
6281     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6282         return 0;
6283     }
6284     return env->cp15.hcrx_el2;
6285 }
6286 
6287 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6288                            uint64_t value)
6289 {
6290     /*
6291      * For A-profile AArch32 EL3, if NSACR.CP10
6292      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6293      */
6294     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6295         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6296         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6297         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6298     }
6299     env->cp15.cptr_el[2] = value;
6300 }
6301 
6302 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6303 {
6304     /*
6305      * For A-profile AArch32 EL3, if NSACR.CP10
6306      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6307      */
6308     uint64_t value = env->cp15.cptr_el[2];
6309 
6310     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6311         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6312         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6313     }
6314     return value;
6315 }
6316 
6317 static const ARMCPRegInfo el2_cp_reginfo[] = {
6318     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6319       .type = ARM_CP_IO,
6320       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6321       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6322       .nv2_redirect_offset = 0x78,
6323       .writefn = hcr_write, .raw_writefn = raw_write },
6324     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6325       .type = ARM_CP_ALIAS | ARM_CP_IO,
6326       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6327       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6328       .writefn = hcr_writelow },
6329     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6330       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6331       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6332     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6333       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6334       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6335       .access = PL2_RW,
6336       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6337     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6338       .type = ARM_CP_NV2_REDIRECT,
6339       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6340       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6341     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6342       .type = ARM_CP_NV2_REDIRECT,
6343       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6344       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6345     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6346       .type = ARM_CP_ALIAS,
6347       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6348       .access = PL2_RW,
6349       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6350     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6351       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6352       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6353       .access = PL2_RW,
6354       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6355     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6356       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6357       .access = PL2_RW, .writefn = vbar_write,
6358       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6359       .resetvalue = 0 },
6360     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6361       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6362       .access = PL3_RW, .type = ARM_CP_ALIAS,
6363       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6364     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6365       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6366       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6367       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6368       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6369     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6370       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6371       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6372       .resetvalue = 0 },
6373     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6374       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6375       .access = PL2_RW, .type = ARM_CP_ALIAS,
6376       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6377     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6378       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6379       .access = PL2_RW, .type = ARM_CP_CONST,
6380       .resetvalue = 0 },
6381     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6382     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6383       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6384       .access = PL2_RW, .type = ARM_CP_CONST,
6385       .resetvalue = 0 },
6386     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6387       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6388       .access = PL2_RW, .type = ARM_CP_CONST,
6389       .resetvalue = 0 },
6390     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6391       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6392       .access = PL2_RW, .type = ARM_CP_CONST,
6393       .resetvalue = 0 },
6394     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6395       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6396       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6397       .raw_writefn = raw_write,
6398       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6399     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6400       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6401       .type = ARM_CP_ALIAS,
6402       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6403       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6404     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6405       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6406       .access = PL2_RW,
6407       .nv2_redirect_offset = 0x40,
6408       /* no .writefn needed as this can't cause an ASID change */
6409       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6410     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6411       .cp = 15, .opc1 = 6, .crm = 2,
6412       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6413       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6414       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6415       .writefn = vttbr_write, .raw_writefn = raw_write },
6416     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6417       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6418       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6419       .nv2_redirect_offset = 0x20,
6420       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6421     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6422       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6423       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6424       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6425     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6426       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6427       .access = PL2_RW, .resetvalue = 0,
6428       .nv2_redirect_offset = 0x90,
6429       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6430     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6431       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6432       .access = PL2_RW, .resetvalue = 0,
6433       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6434       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6435     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6436       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6437       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6438     { .name = "TLBIALLNSNH",
6439       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6440       .type = ARM_CP_NO_RAW, .access = PL2_W,
6441       .writefn = tlbiall_nsnh_write },
6442     { .name = "TLBIALLNSNHIS",
6443       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6444       .type = ARM_CP_NO_RAW, .access = PL2_W,
6445       .writefn = tlbiall_nsnh_is_write },
6446     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6447       .type = ARM_CP_NO_RAW, .access = PL2_W,
6448       .writefn = tlbiall_hyp_write },
6449     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6450       .type = ARM_CP_NO_RAW, .access = PL2_W,
6451       .writefn = tlbiall_hyp_is_write },
6452     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6453       .type = ARM_CP_NO_RAW, .access = PL2_W,
6454       .writefn = tlbimva_hyp_write },
6455     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6456       .type = ARM_CP_NO_RAW, .access = PL2_W,
6457       .writefn = tlbimva_hyp_is_write },
6458     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6459       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6460       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6461       .writefn = tlbi_aa64_alle2_write },
6462     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6463       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6464       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6465       .writefn = tlbi_aa64_vae2_write },
6466     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6467       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6468       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6469       .writefn = tlbi_aa64_vae2_write },
6470     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6471       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6472       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6473       .writefn = tlbi_aa64_alle2is_write },
6474     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6475       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6476       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6477       .writefn = tlbi_aa64_vae2is_write },
6478     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6479       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6480       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6481       .writefn = tlbi_aa64_vae2is_write },
6482 #ifndef CONFIG_USER_ONLY
6483     /*
6484      * Unlike the other EL2-related AT operations, these must
6485      * UNDEF from EL3 if EL2 is not implemented, which is why we
6486      * define them here rather than with the rest of the AT ops.
6487      */
6488     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6489       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6490       .access = PL2_W, .accessfn = at_s1e2_access,
6491       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6492       .writefn = ats_write64 },
6493     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6494       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6495       .access = PL2_W, .accessfn = at_s1e2_access,
6496       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6497       .writefn = ats_write64 },
6498     /*
6499      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6500      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6501      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6502      * to behave as if SCR.NS was 1.
6503      */
6504     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6505       .access = PL2_W,
6506       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6507     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6508       .access = PL2_W,
6509       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6510     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6511       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6512       /*
6513        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6514        * reset values as IMPDEF. We choose to reset to 3 to comply with
6515        * both ARMv7 and ARMv8.
6516        */
6517       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6518       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6519       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6520     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6521       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6522       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6523       .writefn = gt_cntvoff_write,
6524       .nv2_redirect_offset = 0x60,
6525       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6526     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6527       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6528       .writefn = gt_cntvoff_write,
6529       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6530     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6531       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6532       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6533       .type = ARM_CP_IO, .access = PL2_RW,
6534       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6535     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6536       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6537       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6538       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6539     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6540       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6541       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6542       .resetfn = gt_hyp_timer_reset,
6543       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6544     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6545       .type = ARM_CP_IO,
6546       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6547       .access = PL2_RW,
6548       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6549       .resetvalue = 0,
6550       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6551 #endif
6552     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6553       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6554       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6555       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6556     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6557       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6558       .access = PL2_RW,
6559       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6560     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6561       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6562       .access = PL2_RW,
6563       .nv2_redirect_offset = 0x80,
6564       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6565 };
6566 
6567 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6568     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6569       .type = ARM_CP_ALIAS | ARM_CP_IO,
6570       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6571       .access = PL2_RW,
6572       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6573       .writefn = hcr_writehigh },
6574 };
6575 
6576 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6577                                   bool isread)
6578 {
6579     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6580         return CP_ACCESS_OK;
6581     }
6582     return CP_ACCESS_TRAP_UNCATEGORIZED;
6583 }
6584 
6585 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6586     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6587       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6588       .access = PL2_RW, .accessfn = sel2_access,
6589       .nv2_redirect_offset = 0x30,
6590       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6591     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6592       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6593       .access = PL2_RW, .accessfn = sel2_access,
6594       .nv2_redirect_offset = 0x48,
6595       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6596 };
6597 
6598 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6599                                    bool isread)
6600 {
6601     /*
6602      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6603      * At Secure EL1 it traps to EL3 or EL2.
6604      */
6605     if (arm_current_el(env) == 3) {
6606         return CP_ACCESS_OK;
6607     }
6608     if (arm_is_secure_below_el3(env)) {
6609         if (env->cp15.scr_el3 & SCR_EEL2) {
6610             return CP_ACCESS_TRAP_EL2;
6611         }
6612         return CP_ACCESS_TRAP_EL3;
6613     }
6614     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6615     if (isread) {
6616         return CP_ACCESS_OK;
6617     }
6618     return CP_ACCESS_TRAP_UNCATEGORIZED;
6619 }
6620 
6621 static const ARMCPRegInfo el3_cp_reginfo[] = {
6622     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6623       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6624       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6625       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6626     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6627       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6628       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6629       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6630       .writefn = scr_write, .raw_writefn = raw_write },
6631     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6632       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6633       .access = PL3_RW, .resetvalue = 0,
6634       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6635     { .name = "SDER",
6636       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6637       .access = PL3_RW, .resetvalue = 0,
6638       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6639     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6640       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6641       .writefn = vbar_write, .resetvalue = 0,
6642       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6643     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6644       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6645       .access = PL3_RW, .resetvalue = 0,
6646       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6647     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6648       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6649       .access = PL3_RW,
6650       /* no .writefn needed as this can't cause an ASID change */
6651       .resetvalue = 0,
6652       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6653     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6654       .type = ARM_CP_ALIAS,
6655       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6656       .access = PL3_RW,
6657       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6658     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6659       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6660       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6661     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6662       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6663       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6664     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6665       .type = ARM_CP_ALIAS,
6666       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6667       .access = PL3_RW,
6668       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6669     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6670       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6671       .access = PL3_RW, .writefn = vbar_write,
6672       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6673       .resetvalue = 0 },
6674     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6675       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6676       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6677       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6678     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6679       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6680       .access = PL3_RW, .resetvalue = 0,
6681       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6682     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6683       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6684       .access = PL3_RW, .type = ARM_CP_CONST,
6685       .resetvalue = 0 },
6686     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6687       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6688       .access = PL3_RW, .type = ARM_CP_CONST,
6689       .resetvalue = 0 },
6690     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6691       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6692       .access = PL3_RW, .type = ARM_CP_CONST,
6693       .resetvalue = 0 },
6694     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6695       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6696       .access = PL3_W, .type = ARM_CP_NO_RAW,
6697       .writefn = tlbi_aa64_alle3is_write },
6698     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6699       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6700       .access = PL3_W, .type = ARM_CP_NO_RAW,
6701       .writefn = tlbi_aa64_vae3is_write },
6702     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6703       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6704       .access = PL3_W, .type = ARM_CP_NO_RAW,
6705       .writefn = tlbi_aa64_vae3is_write },
6706     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6707       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6708       .access = PL3_W, .type = ARM_CP_NO_RAW,
6709       .writefn = tlbi_aa64_alle3_write },
6710     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6711       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6712       .access = PL3_W, .type = ARM_CP_NO_RAW,
6713       .writefn = tlbi_aa64_vae3_write },
6714     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6715       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6716       .access = PL3_W, .type = ARM_CP_NO_RAW,
6717       .writefn = tlbi_aa64_vae3_write },
6718 };
6719 
6720 #ifndef CONFIG_USER_ONLY
6721 
6722 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6723                                  bool isread)
6724 {
6725     if (arm_current_el(env) == 1) {
6726         /* This must be a FEAT_NV access */
6727         return CP_ACCESS_OK;
6728     }
6729     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6730         return CP_ACCESS_TRAP_UNCATEGORIZED;
6731     }
6732     return CP_ACCESS_OK;
6733 }
6734 
6735 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6736                                       bool isread)
6737 {
6738     if (arm_current_el(env) == 1) {
6739         /* This must be a FEAT_NV access with NVx == 101 */
6740         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6741             return CP_ACCESS_TRAP_EL2;
6742         }
6743     }
6744     return e2h_access(env, ri, isread);
6745 }
6746 
6747 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6748                                       bool isread)
6749 {
6750     if (arm_current_el(env) == 1) {
6751         /* This must be a FEAT_NV access with NVx == 101 */
6752         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6753             return CP_ACCESS_TRAP_EL2;
6754         }
6755     }
6756     return e2h_access(env, ri, isread);
6757 }
6758 
6759 /* Test if system register redirection is to occur in the current state.  */
6760 static bool redirect_for_e2h(CPUARMState *env)
6761 {
6762     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6763 }
6764 
6765 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6766 {
6767     CPReadFn *readfn;
6768 
6769     if (redirect_for_e2h(env)) {
6770         /* Switch to the saved EL2 version of the register.  */
6771         ri = ri->opaque;
6772         readfn = ri->readfn;
6773     } else {
6774         readfn = ri->orig_readfn;
6775     }
6776     if (readfn == NULL) {
6777         readfn = raw_read;
6778     }
6779     return readfn(env, ri);
6780 }
6781 
6782 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6783                           uint64_t value)
6784 {
6785     CPWriteFn *writefn;
6786 
6787     if (redirect_for_e2h(env)) {
6788         /* Switch to the saved EL2 version of the register.  */
6789         ri = ri->opaque;
6790         writefn = ri->writefn;
6791     } else {
6792         writefn = ri->orig_writefn;
6793     }
6794     if (writefn == NULL) {
6795         writefn = raw_write;
6796     }
6797     writefn(env, ri, value);
6798 }
6799 
6800 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6801 {
6802     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6803     return ri->orig_readfn(env, ri->opaque);
6804 }
6805 
6806 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6807                               uint64_t value)
6808 {
6809     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6810     return ri->orig_writefn(env, ri->opaque, value);
6811 }
6812 
6813 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6814                                          const ARMCPRegInfo *ri,
6815                                          bool isread)
6816 {
6817     if (arm_current_el(env) == 1) {
6818         /*
6819          * This must be a FEAT_NV access (will either trap or redirect
6820          * to memory). None of the registers with _EL12 aliases want to
6821          * apply their trap controls for this kind of access, so don't
6822          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6823          */
6824         return CP_ACCESS_OK;
6825     }
6826     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6827     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6828         return CP_ACCESS_TRAP_UNCATEGORIZED;
6829     }
6830     if (ri->orig_accessfn) {
6831         return ri->orig_accessfn(env, ri->opaque, isread);
6832     }
6833     return CP_ACCESS_OK;
6834 }
6835 
6836 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6837 {
6838     struct E2HAlias {
6839         uint32_t src_key, dst_key, new_key;
6840         const char *src_name, *dst_name, *new_name;
6841         bool (*feature)(const ARMISARegisters *id);
6842     };
6843 
6844 #define K(op0, op1, crn, crm, op2) \
6845     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6846 
6847     static const struct E2HAlias aliases[] = {
6848         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6849           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6850         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6851           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6852         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6853           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6854         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6855           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6856         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6857           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6858         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6859           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6860         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6861           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6862         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6863           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6864         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6865           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6866         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6867           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6868         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6869           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6870         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6871           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6872         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6873           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6874         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6875           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6876         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6877           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6878         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6879           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6880 
6881         /*
6882          * Note that redirection of ZCR is mentioned in the description
6883          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6884          * not in the summary table.
6885          */
6886         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6887           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6888         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6889           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6890 
6891         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6892           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6893 
6894         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6895           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6896           isar_feature_aa64_scxtnum },
6897 
6898         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6899         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6900     };
6901 #undef K
6902 
6903     size_t i;
6904 
6905     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6906         const struct E2HAlias *a = &aliases[i];
6907         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6908         bool ok;
6909 
6910         if (a->feature && !a->feature(&cpu->isar)) {
6911             continue;
6912         }
6913 
6914         src_reg = g_hash_table_lookup(cpu->cp_regs,
6915                                       (gpointer)(uintptr_t)a->src_key);
6916         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6917                                       (gpointer)(uintptr_t)a->dst_key);
6918         g_assert(src_reg != NULL);
6919         g_assert(dst_reg != NULL);
6920 
6921         /* Cross-compare names to detect typos in the keys.  */
6922         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6923         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6924 
6925         /* None of the core system registers use opaque; we will.  */
6926         g_assert(src_reg->opaque == NULL);
6927 
6928         /* Create alias before redirection so we dup the right data. */
6929         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6930 
6931         new_reg->name = a->new_name;
6932         new_reg->type |= ARM_CP_ALIAS;
6933         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6934         new_reg->access &= PL2_RW | PL3_RW;
6935         /* The new_reg op fields are as per new_key, not the target reg */
6936         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6937             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6938         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6939             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6940         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6941             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6942         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6943             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6944         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6945             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6946         new_reg->opaque = src_reg;
6947         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6948         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6949         new_reg->orig_accessfn = src_reg->accessfn;
6950         if (!new_reg->raw_readfn) {
6951             new_reg->raw_readfn = raw_read;
6952         }
6953         if (!new_reg->raw_writefn) {
6954             new_reg->raw_writefn = raw_write;
6955         }
6956         new_reg->readfn = el2_e2h_e12_read;
6957         new_reg->writefn = el2_e2h_e12_write;
6958         new_reg->accessfn = el2_e2h_e12_access;
6959 
6960         /*
6961          * If the _EL1 register is redirected to memory by FEAT_NV2,
6962          * then it shares the offset with the _EL12 register,
6963          * and which one is redirected depends on HCR_EL2.NV1.
6964          */
6965         if (new_reg->nv2_redirect_offset) {
6966             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6967             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6968             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6969         }
6970 
6971         ok = g_hash_table_insert(cpu->cp_regs,
6972                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6973         g_assert(ok);
6974 
6975         src_reg->opaque = dst_reg;
6976         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6977         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6978         if (!src_reg->raw_readfn) {
6979             src_reg->raw_readfn = raw_read;
6980         }
6981         if (!src_reg->raw_writefn) {
6982             src_reg->raw_writefn = raw_write;
6983         }
6984         src_reg->readfn = el2_e2h_read;
6985         src_reg->writefn = el2_e2h_write;
6986     }
6987 }
6988 #endif
6989 
6990 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6991                                      bool isread)
6992 {
6993     int cur_el = arm_current_el(env);
6994 
6995     if (cur_el < 2) {
6996         uint64_t hcr = arm_hcr_el2_eff(env);
6997 
6998         if (cur_el == 0) {
6999             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
7000                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
7001                     return CP_ACCESS_TRAP_EL2;
7002                 }
7003             } else {
7004                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7005                     return CP_ACCESS_TRAP;
7006                 }
7007                 if (hcr & HCR_TID2) {
7008                     return CP_ACCESS_TRAP_EL2;
7009                 }
7010             }
7011         } else if (hcr & HCR_TID2) {
7012             return CP_ACCESS_TRAP_EL2;
7013         }
7014     }
7015 
7016     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
7017         return CP_ACCESS_TRAP_EL2;
7018     }
7019 
7020     return CP_ACCESS_OK;
7021 }
7022 
7023 /*
7024  * Check for traps to RAS registers, which are controlled
7025  * by HCR_EL2.TERR and SCR_EL3.TERR.
7026  */
7027 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
7028                                   bool isread)
7029 {
7030     int el = arm_current_el(env);
7031 
7032     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
7033         return CP_ACCESS_TRAP_EL2;
7034     }
7035     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
7036         return CP_ACCESS_TRAP_EL3;
7037     }
7038     return CP_ACCESS_OK;
7039 }
7040 
7041 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
7042 {
7043     int el = arm_current_el(env);
7044 
7045     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7046         return env->cp15.vdisr_el2;
7047     }
7048     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7049         return 0; /* RAZ/WI */
7050     }
7051     return env->cp15.disr_el1;
7052 }
7053 
7054 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7055 {
7056     int el = arm_current_el(env);
7057 
7058     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7059         env->cp15.vdisr_el2 = val;
7060         return;
7061     }
7062     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7063         return; /* RAZ/WI */
7064     }
7065     env->cp15.disr_el1 = val;
7066 }
7067 
7068 /*
7069  * Minimal RAS implementation with no Error Records.
7070  * Which means that all of the Error Record registers:
7071  *   ERXADDR_EL1
7072  *   ERXCTLR_EL1
7073  *   ERXFR_EL1
7074  *   ERXMISC0_EL1
7075  *   ERXMISC1_EL1
7076  *   ERXMISC2_EL1
7077  *   ERXMISC3_EL1
7078  *   ERXPFGCDN_EL1  (RASv1p1)
7079  *   ERXPFGCTL_EL1  (RASv1p1)
7080  *   ERXPFGF_EL1    (RASv1p1)
7081  *   ERXSTATUS_EL1
7082  * and
7083  *   ERRSELR_EL1
7084  * may generate UNDEFINED, which is the effect we get by not
7085  * listing them at all.
7086  *
7087  * These registers have fine-grained trap bits, but UNDEF-to-EL1
7088  * is higher priority than FGT-to-EL2 so we do not need to list them
7089  * in order to check for an FGT.
7090  */
7091 static const ARMCPRegInfo minimal_ras_reginfo[] = {
7092     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
7093       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
7094       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
7095       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
7096     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
7097       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
7098       .access = PL1_R, .accessfn = access_terr,
7099       .fgt = FGT_ERRIDR_EL1,
7100       .type = ARM_CP_CONST, .resetvalue = 0 },
7101     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
7102       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
7103       .nv2_redirect_offset = 0x500,
7104       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
7105     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
7106       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
7107       .nv2_redirect_offset = 0x508,
7108       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
7109 };
7110 
7111 /*
7112  * Return the exception level to which exceptions should be taken
7113  * via SVEAccessTrap.  This excludes the check for whether the exception
7114  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
7115  * be found by testing 0 < fp_exception_el < sve_exception_el.
7116  *
7117  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
7118  * pseudocode does *not* separate out the FP trap checks, but has them
7119  * all in one function.
7120  */
7121 int sve_exception_el(CPUARMState *env, int el)
7122 {
7123 #ifndef CONFIG_USER_ONLY
7124     if (el <= 1 && !el_is_in_host(env, el)) {
7125         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7126         case 1:
7127             if (el != 0) {
7128                 break;
7129             }
7130             /* fall through */
7131         case 0:
7132         case 2:
7133             return 1;
7134         }
7135     }
7136 
7137     if (el <= 2 && arm_is_el2_enabled(env)) {
7138         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7139         if (env->cp15.hcr_el2 & HCR_E2H) {
7140             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
7141             case 1:
7142                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7143                     break;
7144                 }
7145                 /* fall through */
7146             case 0:
7147             case 2:
7148                 return 2;
7149             }
7150         } else {
7151             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
7152                 return 2;
7153             }
7154         }
7155     }
7156 
7157     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
7158     if (arm_feature(env, ARM_FEATURE_EL3)
7159         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
7160         return 3;
7161     }
7162 #endif
7163     return 0;
7164 }
7165 
7166 /*
7167  * Return the exception level to which exceptions should be taken for SME.
7168  * C.f. the ARM pseudocode function CheckSMEAccess.
7169  */
7170 int sme_exception_el(CPUARMState *env, int el)
7171 {
7172 #ifndef CONFIG_USER_ONLY
7173     if (el <= 1 && !el_is_in_host(env, el)) {
7174         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
7175         case 1:
7176             if (el != 0) {
7177                 break;
7178             }
7179             /* fall through */
7180         case 0:
7181         case 2:
7182             return 1;
7183         }
7184     }
7185 
7186     if (el <= 2 && arm_is_el2_enabled(env)) {
7187         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7188         if (env->cp15.hcr_el2 & HCR_E2H) {
7189             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
7190             case 1:
7191                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7192                     break;
7193                 }
7194                 /* fall through */
7195             case 0:
7196             case 2:
7197                 return 2;
7198             }
7199         } else {
7200             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7201                 return 2;
7202             }
7203         }
7204     }
7205 
7206     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
7207     if (arm_feature(env, ARM_FEATURE_EL3)
7208         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7209         return 3;
7210     }
7211 #endif
7212     return 0;
7213 }
7214 
7215 /*
7216  * Given that SVE is enabled, return the vector length for EL.
7217  */
7218 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7219 {
7220     ARMCPU *cpu = env_archcpu(env);
7221     uint64_t *cr = env->vfp.zcr_el;
7222     uint32_t map = cpu->sve_vq.map;
7223     uint32_t len = ARM_MAX_VQ - 1;
7224 
7225     if (sm) {
7226         cr = env->vfp.smcr_el;
7227         map = cpu->sme_vq.map;
7228     }
7229 
7230     if (el <= 1 && !el_is_in_host(env, el)) {
7231         len = MIN(len, 0xf & (uint32_t)cr[1]);
7232     }
7233     if (el <= 2 && arm_is_el2_enabled(env)) {
7234         len = MIN(len, 0xf & (uint32_t)cr[2]);
7235     }
7236     if (arm_feature(env, ARM_FEATURE_EL3)) {
7237         len = MIN(len, 0xf & (uint32_t)cr[3]);
7238     }
7239 
7240     map &= MAKE_64BIT_MASK(0, len + 1);
7241     if (map != 0) {
7242         return 31 - clz32(map);
7243     }
7244 
7245     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7246     assert(sm);
7247     return ctz32(cpu->sme_vq.map);
7248 }
7249 
7250 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7251 {
7252     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7253 }
7254 
7255 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7256                       uint64_t value)
7257 {
7258     int cur_el = arm_current_el(env);
7259     int old_len = sve_vqm1_for_el(env, cur_el);
7260     int new_len;
7261 
7262     /* Bits other than [3:0] are RAZ/WI.  */
7263     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7264     raw_write(env, ri, value & 0xf);
7265 
7266     /*
7267      * Because we arrived here, we know both FP and SVE are enabled;
7268      * otherwise we would have trapped access to the ZCR_ELn register.
7269      */
7270     new_len = sve_vqm1_for_el(env, cur_el);
7271     if (new_len < old_len) {
7272         aarch64_sve_narrow_vq(env, new_len + 1);
7273     }
7274 }
7275 
7276 static const ARMCPRegInfo zcr_reginfo[] = {
7277     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7278       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7279       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7280       .access = PL1_RW, .type = ARM_CP_SVE,
7281       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7282       .writefn = zcr_write, .raw_writefn = raw_write },
7283     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7284       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7285       .access = PL2_RW, .type = ARM_CP_SVE,
7286       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7287       .writefn = zcr_write, .raw_writefn = raw_write },
7288     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7290       .access = PL3_RW, .type = ARM_CP_SVE,
7291       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7292       .writefn = zcr_write, .raw_writefn = raw_write },
7293 };
7294 
7295 #ifdef TARGET_AARCH64
7296 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7297                                     bool isread)
7298 {
7299     int el = arm_current_el(env);
7300 
7301     if (el == 0) {
7302         uint64_t sctlr = arm_sctlr(env, el);
7303         if (!(sctlr & SCTLR_EnTP2)) {
7304             return CP_ACCESS_TRAP;
7305         }
7306     }
7307     /* TODO: FEAT_FGT */
7308     if (el < 3
7309         && arm_feature(env, ARM_FEATURE_EL3)
7310         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7311         return CP_ACCESS_TRAP_EL3;
7312     }
7313     return CP_ACCESS_OK;
7314 }
7315 
7316 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7317                                       bool isread)
7318 {
7319     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7320     if (arm_current_el(env) == 2
7321         && arm_feature(env, ARM_FEATURE_EL3)
7322         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7323         return CP_ACCESS_TRAP_EL3;
7324     }
7325     return CP_ACCESS_OK;
7326 }
7327 
7328 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7329                                    bool isread)
7330 {
7331     if (arm_current_el(env) < 3
7332         && arm_feature(env, ARM_FEATURE_EL3)
7333         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7334         return CP_ACCESS_TRAP_EL3;
7335     }
7336     return CP_ACCESS_OK;
7337 }
7338 
7339 /* ResetSVEState */
7340 static void arm_reset_sve_state(CPUARMState *env)
7341 {
7342     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7343     /* Recall that FFR is stored as pregs[16]. */
7344     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7345     vfp_set_fpcr(env, 0x0800009f);
7346 }
7347 
7348 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7349 {
7350     uint64_t change = (env->svcr ^ new) & mask;
7351 
7352     if (change == 0) {
7353         return;
7354     }
7355     env->svcr ^= change;
7356 
7357     if (change & R_SVCR_SM_MASK) {
7358         arm_reset_sve_state(env);
7359     }
7360 
7361     /*
7362      * ResetSMEState.
7363      *
7364      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
7365      * on enable: while disabled, the storage is inaccessible and the
7366      * value does not matter.  We're not saving the storage in vmstate
7367      * when disabled either.
7368      */
7369     if (change & new & R_SVCR_ZA_MASK) {
7370         memset(env->zarray, 0, sizeof(env->zarray));
7371     }
7372 
7373     if (tcg_enabled()) {
7374         arm_rebuild_hflags(env);
7375     }
7376 }
7377 
7378 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7379                        uint64_t value)
7380 {
7381     aarch64_set_svcr(env, value, -1);
7382 }
7383 
7384 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7385                        uint64_t value)
7386 {
7387     int cur_el = arm_current_el(env);
7388     int old_len = sve_vqm1_for_el(env, cur_el);
7389     int new_len;
7390 
7391     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7392     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7393     raw_write(env, ri, value);
7394 
7395     /*
7396      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7397      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7398      * current values for simplicity.  But for QEMU internals, we must still
7399      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7400      * above aarch64_sve_narrow_vq.
7401      */
7402     new_len = sve_vqm1_for_el(env, cur_el);
7403     if (new_len < old_len) {
7404         aarch64_sve_narrow_vq(env, new_len + 1);
7405     }
7406 }
7407 
7408 static const ARMCPRegInfo sme_reginfo[] = {
7409     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7410       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7411       .access = PL0_RW, .accessfn = access_tpidr2,
7412       .fgt = FGT_NTPIDR2_EL0,
7413       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7414     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7415       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7416       .access = PL0_RW, .type = ARM_CP_SME,
7417       .fieldoffset = offsetof(CPUARMState, svcr),
7418       .writefn = svcr_write, .raw_writefn = raw_write },
7419     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7420       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7421       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7422       .access = PL1_RW, .type = ARM_CP_SME,
7423       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7424       .writefn = smcr_write, .raw_writefn = raw_write },
7425     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7426       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7427       .access = PL2_RW, .type = ARM_CP_SME,
7428       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7429       .writefn = smcr_write, .raw_writefn = raw_write },
7430     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7431       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7432       .access = PL3_RW, .type = ARM_CP_SME,
7433       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7434       .writefn = smcr_write, .raw_writefn = raw_write },
7435     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7436       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7437       .access = PL1_R, .accessfn = access_aa64_tid1,
7438       /*
7439        * IMPLEMENTOR = 0 (software)
7440        * REVISION    = 0 (implementation defined)
7441        * SMPS        = 0 (no streaming execution priority in QEMU)
7442        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7443        */
7444       .type = ARM_CP_CONST, .resetvalue = 0, },
7445     /*
7446      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7447      */
7448     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7449       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7450       .access = PL1_RW, .accessfn = access_smpri,
7451       .fgt = FGT_NSMPRI_EL1,
7452       .type = ARM_CP_CONST, .resetvalue = 0 },
7453     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7454       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7455       .nv2_redirect_offset = 0x1f8,
7456       .access = PL2_RW, .accessfn = access_smprimap,
7457       .type = ARM_CP_CONST, .resetvalue = 0 },
7458 };
7459 
7460 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7461                                   uint64_t value)
7462 {
7463     CPUState *cs = env_cpu(env);
7464 
7465     tlb_flush(cs);
7466 }
7467 
7468 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7469                         uint64_t value)
7470 {
7471     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7472     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7473         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7474         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7475 
7476     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7477 }
7478 
7479 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7480 {
7481     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7482                                      env_archcpu(env)->reset_l0gptsz);
7483 }
7484 
7485 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7486                                     uint64_t value)
7487 {
7488     CPUState *cs = env_cpu(env);
7489 
7490     tlb_flush_all_cpus_synced(cs);
7491 }
7492 
7493 static const ARMCPRegInfo rme_reginfo[] = {
7494     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7495       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7496       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7497       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7498     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7499       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7500       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7501     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7502       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7503       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7504     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7505       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7506       .access = PL3_W, .type = ARM_CP_NO_RAW,
7507       .writefn = tlbi_aa64_paall_write },
7508     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7509       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7510       .access = PL3_W, .type = ARM_CP_NO_RAW,
7511       .writefn = tlbi_aa64_paallos_write },
7512     /*
7513      * QEMU does not have a way to invalidate by physical address, thus
7514      * invalidating a range of physical addresses is accomplished by
7515      * flushing all tlb entries in the outer shareable domain,
7516      * just like PAALLOS.
7517      */
7518     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7519       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7520       .access = PL3_W, .type = ARM_CP_NO_RAW,
7521       .writefn = tlbi_aa64_paallos_write },
7522     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7523       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7524       .access = PL3_W, .type = ARM_CP_NO_RAW,
7525       .writefn = tlbi_aa64_paallos_write },
7526     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7527       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7528       .access = PL3_W, .type = ARM_CP_NOP },
7529 };
7530 
7531 static const ARMCPRegInfo rme_mte_reginfo[] = {
7532     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7533       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7534       .access = PL3_W, .type = ARM_CP_NOP },
7535 };
7536 
7537 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
7538                               uint64_t value)
7539 {
7540     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
7541 }
7542 
7543 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
7544 {
7545     return env->pstate & PSTATE_ALLINT;
7546 }
7547 
7548 static CPAccessResult aa64_allint_access(CPUARMState *env,
7549                                          const ARMCPRegInfo *ri, bool isread)
7550 {
7551     if (!isread && arm_current_el(env) == 1 &&
7552         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
7553         return CP_ACCESS_TRAP_EL2;
7554     }
7555     return CP_ACCESS_OK;
7556 }
7557 
7558 static const ARMCPRegInfo nmi_reginfo[] = {
7559     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
7560       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
7561       .type = ARM_CP_NO_RAW,
7562       .access = PL1_RW, .accessfn = aa64_allint_access,
7563       .fieldoffset = offsetof(CPUARMState, pstate),
7564       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
7565       .resetfn = arm_cp_reset_ignore },
7566 };
7567 #endif /* TARGET_AARCH64 */
7568 
7569 static void define_pmu_regs(ARMCPU *cpu)
7570 {
7571     /*
7572      * v7 performance monitor control register: same implementor
7573      * field as main ID register, and we implement four counters in
7574      * addition to the cycle count register.
7575      */
7576     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7577     ARMCPRegInfo pmcr = {
7578         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7579         .access = PL0_RW,
7580         .fgt = FGT_PMCR_EL0,
7581         .type = ARM_CP_IO | ARM_CP_ALIAS,
7582         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7583         .accessfn = pmreg_access,
7584         .readfn = pmcr_read, .raw_readfn = raw_read,
7585         .writefn = pmcr_write, .raw_writefn = raw_write,
7586     };
7587     ARMCPRegInfo pmcr64 = {
7588         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7589         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7590         .access = PL0_RW, .accessfn = pmreg_access,
7591         .fgt = FGT_PMCR_EL0,
7592         .type = ARM_CP_IO,
7593         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7594         .resetvalue = cpu->isar.reset_pmcr_el0,
7595         .readfn = pmcr_read, .raw_readfn = raw_read,
7596         .writefn = pmcr_write, .raw_writefn = raw_write,
7597     };
7598 
7599     define_one_arm_cp_reg(cpu, &pmcr);
7600     define_one_arm_cp_reg(cpu, &pmcr64);
7601     for (i = 0; i < pmcrn; i++) {
7602         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7603         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7604         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7605         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7606         ARMCPRegInfo pmev_regs[] = {
7607             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7608               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7609               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7610               .fgt = FGT_PMEVCNTRN_EL0,
7611               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7612               .accessfn = pmreg_access_xevcntr },
7613             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7614               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7615               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7616               .type = ARM_CP_IO,
7617               .fgt = FGT_PMEVCNTRN_EL0,
7618               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7619               .raw_readfn = pmevcntr_rawread,
7620               .raw_writefn = pmevcntr_rawwrite },
7621             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7622               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7623               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7624               .fgt = FGT_PMEVTYPERN_EL0,
7625               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7626               .accessfn = pmreg_access },
7627             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7628               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7629               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7630               .fgt = FGT_PMEVTYPERN_EL0,
7631               .type = ARM_CP_IO,
7632               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7633               .raw_writefn = pmevtyper_rawwrite },
7634         };
7635         define_arm_cp_regs(cpu, pmev_regs);
7636         g_free(pmevcntr_name);
7637         g_free(pmevcntr_el0_name);
7638         g_free(pmevtyper_name);
7639         g_free(pmevtyper_el0_name);
7640     }
7641     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7642         ARMCPRegInfo v81_pmu_regs[] = {
7643             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7644               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7645               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7646               .fgt = FGT_PMCEIDN_EL0,
7647               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7648             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7649               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7650               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7651               .fgt = FGT_PMCEIDN_EL0,
7652               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7653         };
7654         define_arm_cp_regs(cpu, v81_pmu_regs);
7655     }
7656     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7657         static const ARMCPRegInfo v84_pmmir = {
7658             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7659             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7660             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7661             .fgt = FGT_PMMIR_EL1,
7662             .resetvalue = 0
7663         };
7664         define_one_arm_cp_reg(cpu, &v84_pmmir);
7665     }
7666 }
7667 
7668 #ifndef CONFIG_USER_ONLY
7669 /*
7670  * We don't know until after realize whether there's a GICv3
7671  * attached, and that is what registers the gicv3 sysregs.
7672  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7673  * at runtime.
7674  */
7675 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7676 {
7677     ARMCPU *cpu = env_archcpu(env);
7678     uint64_t pfr1 = cpu->isar.id_pfr1;
7679 
7680     if (env->gicv3state) {
7681         pfr1 |= 1 << 28;
7682     }
7683     return pfr1;
7684 }
7685 
7686 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7687 {
7688     ARMCPU *cpu = env_archcpu(env);
7689     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7690 
7691     if (env->gicv3state) {
7692         pfr0 |= 1 << 24;
7693     }
7694     return pfr0;
7695 }
7696 #endif
7697 
7698 /*
7699  * Shared logic between LORID and the rest of the LOR* registers.
7700  * Secure state exclusion has already been dealt with.
7701  */
7702 static CPAccessResult access_lor_ns(CPUARMState *env,
7703                                     const ARMCPRegInfo *ri, bool isread)
7704 {
7705     int el = arm_current_el(env);
7706 
7707     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7708         return CP_ACCESS_TRAP_EL2;
7709     }
7710     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7711         return CP_ACCESS_TRAP_EL3;
7712     }
7713     return CP_ACCESS_OK;
7714 }
7715 
7716 static CPAccessResult access_lor_other(CPUARMState *env,
7717                                        const ARMCPRegInfo *ri, bool isread)
7718 {
7719     if (arm_is_secure_below_el3(env)) {
7720         /* Access denied in secure mode.  */
7721         return CP_ACCESS_TRAP;
7722     }
7723     return access_lor_ns(env, ri, isread);
7724 }
7725 
7726 /*
7727  * A trivial implementation of ARMv8.1-LOR leaves all of these
7728  * registers fixed at 0, which indicates that there are zero
7729  * supported Limited Ordering regions.
7730  */
7731 static const ARMCPRegInfo lor_reginfo[] = {
7732     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7733       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7734       .access = PL1_RW, .accessfn = access_lor_other,
7735       .fgt = FGT_LORSA_EL1,
7736       .type = ARM_CP_CONST, .resetvalue = 0 },
7737     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7738       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7739       .access = PL1_RW, .accessfn = access_lor_other,
7740       .fgt = FGT_LOREA_EL1,
7741       .type = ARM_CP_CONST, .resetvalue = 0 },
7742     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7743       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7744       .access = PL1_RW, .accessfn = access_lor_other,
7745       .fgt = FGT_LORN_EL1,
7746       .type = ARM_CP_CONST, .resetvalue = 0 },
7747     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7748       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7749       .access = PL1_RW, .accessfn = access_lor_other,
7750       .fgt = FGT_LORC_EL1,
7751       .type = ARM_CP_CONST, .resetvalue = 0 },
7752     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7753       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7754       .access = PL1_R, .accessfn = access_lor_ns,
7755       .fgt = FGT_LORID_EL1,
7756       .type = ARM_CP_CONST, .resetvalue = 0 },
7757 };
7758 
7759 #ifdef TARGET_AARCH64
7760 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7761                                    bool isread)
7762 {
7763     int el = arm_current_el(env);
7764 
7765     if (el < 2 &&
7766         arm_is_el2_enabled(env) &&
7767         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7768         return CP_ACCESS_TRAP_EL2;
7769     }
7770     if (el < 3 &&
7771         arm_feature(env, ARM_FEATURE_EL3) &&
7772         !(env->cp15.scr_el3 & SCR_APK)) {
7773         return CP_ACCESS_TRAP_EL3;
7774     }
7775     return CP_ACCESS_OK;
7776 }
7777 
7778 static const ARMCPRegInfo pauth_reginfo[] = {
7779     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7780       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7781       .access = PL1_RW, .accessfn = access_pauth,
7782       .fgt = FGT_APDAKEY,
7783       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7784     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7785       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7786       .access = PL1_RW, .accessfn = access_pauth,
7787       .fgt = FGT_APDAKEY,
7788       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7789     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7790       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7791       .access = PL1_RW, .accessfn = access_pauth,
7792       .fgt = FGT_APDBKEY,
7793       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7794     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7795       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7796       .access = PL1_RW, .accessfn = access_pauth,
7797       .fgt = FGT_APDBKEY,
7798       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7799     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7800       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7801       .access = PL1_RW, .accessfn = access_pauth,
7802       .fgt = FGT_APGAKEY,
7803       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7804     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7805       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7806       .access = PL1_RW, .accessfn = access_pauth,
7807       .fgt = FGT_APGAKEY,
7808       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7809     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7810       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7811       .access = PL1_RW, .accessfn = access_pauth,
7812       .fgt = FGT_APIAKEY,
7813       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7814     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7815       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7816       .access = PL1_RW, .accessfn = access_pauth,
7817       .fgt = FGT_APIAKEY,
7818       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7819     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7820       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7821       .access = PL1_RW, .accessfn = access_pauth,
7822       .fgt = FGT_APIBKEY,
7823       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7824     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7825       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7826       .access = PL1_RW, .accessfn = access_pauth,
7827       .fgt = FGT_APIBKEY,
7828       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7829 };
7830 
7831 static const ARMCPRegInfo tlbirange_reginfo[] = {
7832     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7833       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7834       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7835       .fgt = FGT_TLBIRVAE1IS,
7836       .writefn = tlbi_aa64_rvae1is_write },
7837     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7838       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7839       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7840       .fgt = FGT_TLBIRVAAE1IS,
7841       .writefn = tlbi_aa64_rvae1is_write },
7842    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7843       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7844       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7845       .fgt = FGT_TLBIRVALE1IS,
7846       .writefn = tlbi_aa64_rvae1is_write },
7847     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7848       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7849       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7850       .fgt = FGT_TLBIRVAALE1IS,
7851       .writefn = tlbi_aa64_rvae1is_write },
7852     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7853       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7854       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7855       .fgt = FGT_TLBIRVAE1OS,
7856       .writefn = tlbi_aa64_rvae1is_write },
7857     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7858       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7859       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7860       .fgt = FGT_TLBIRVAAE1OS,
7861       .writefn = tlbi_aa64_rvae1is_write },
7862    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7863       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7864       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7865       .fgt = FGT_TLBIRVALE1OS,
7866       .writefn = tlbi_aa64_rvae1is_write },
7867     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7868       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7869       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7870       .fgt = FGT_TLBIRVAALE1OS,
7871       .writefn = tlbi_aa64_rvae1is_write },
7872     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7873       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7874       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7875       .fgt = FGT_TLBIRVAE1,
7876       .writefn = tlbi_aa64_rvae1_write },
7877     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7878       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7879       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7880       .fgt = FGT_TLBIRVAAE1,
7881       .writefn = tlbi_aa64_rvae1_write },
7882    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7883       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7884       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7885       .fgt = FGT_TLBIRVALE1,
7886       .writefn = tlbi_aa64_rvae1_write },
7887     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7888       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7889       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7890       .fgt = FGT_TLBIRVAALE1,
7891       .writefn = tlbi_aa64_rvae1_write },
7892     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7893       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7894       .access = PL2_W, .type = ARM_CP_NO_RAW,
7895       .writefn = tlbi_aa64_ripas2e1is_write },
7896     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7897       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7898       .access = PL2_W, .type = ARM_CP_NO_RAW,
7899       .writefn = tlbi_aa64_ripas2e1is_write },
7900     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7901       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7902       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7903       .writefn = tlbi_aa64_rvae2is_write },
7904    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7905       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7906       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7907       .writefn = tlbi_aa64_rvae2is_write },
7908     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7909       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7910       .access = PL2_W, .type = ARM_CP_NO_RAW,
7911       .writefn = tlbi_aa64_ripas2e1_write },
7912     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7913       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7914       .access = PL2_W, .type = ARM_CP_NO_RAW,
7915       .writefn = tlbi_aa64_ripas2e1_write },
7916    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7917       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7918       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7919       .writefn = tlbi_aa64_rvae2is_write },
7920    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7921       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7922       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7923       .writefn = tlbi_aa64_rvae2is_write },
7924     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7925       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7926       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7927       .writefn = tlbi_aa64_rvae2_write },
7928    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7929       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7930       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7931       .writefn = tlbi_aa64_rvae2_write },
7932    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7933       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7934       .access = PL3_W, .type = ARM_CP_NO_RAW,
7935       .writefn = tlbi_aa64_rvae3is_write },
7936    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7937       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7938       .access = PL3_W, .type = ARM_CP_NO_RAW,
7939       .writefn = tlbi_aa64_rvae3is_write },
7940    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7941       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7942       .access = PL3_W, .type = ARM_CP_NO_RAW,
7943       .writefn = tlbi_aa64_rvae3is_write },
7944    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7945       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7946       .access = PL3_W, .type = ARM_CP_NO_RAW,
7947       .writefn = tlbi_aa64_rvae3is_write },
7948    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7949       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7950       .access = PL3_W, .type = ARM_CP_NO_RAW,
7951       .writefn = tlbi_aa64_rvae3_write },
7952    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7953       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7954       .access = PL3_W, .type = ARM_CP_NO_RAW,
7955       .writefn = tlbi_aa64_rvae3_write },
7956 };
7957 
7958 static const ARMCPRegInfo tlbios_reginfo[] = {
7959     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7960       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7961       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7962       .fgt = FGT_TLBIVMALLE1OS,
7963       .writefn = tlbi_aa64_vmalle1is_write },
7964     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7965       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7966       .fgt = FGT_TLBIVAE1OS,
7967       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7968       .writefn = tlbi_aa64_vae1is_write },
7969     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7970       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7971       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7972       .fgt = FGT_TLBIASIDE1OS,
7973       .writefn = tlbi_aa64_vmalle1is_write },
7974     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7975       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7976       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7977       .fgt = FGT_TLBIVAAE1OS,
7978       .writefn = tlbi_aa64_vae1is_write },
7979     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7980       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7981       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7982       .fgt = FGT_TLBIVALE1OS,
7983       .writefn = tlbi_aa64_vae1is_write },
7984     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7985       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7986       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7987       .fgt = FGT_TLBIVAALE1OS,
7988       .writefn = tlbi_aa64_vae1is_write },
7989     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7990       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7991       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7992       .writefn = tlbi_aa64_alle2is_write },
7993     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7994       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7995       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7996       .writefn = tlbi_aa64_vae2is_write },
7997    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7998       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7999       .access = PL2_W, .type = ARM_CP_NO_RAW,
8000       .writefn = tlbi_aa64_alle1is_write },
8001     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
8002       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
8003       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8004       .writefn = tlbi_aa64_vae2is_write },
8005     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
8006       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
8007       .access = PL2_W, .type = ARM_CP_NO_RAW,
8008       .writefn = tlbi_aa64_alle1is_write },
8009     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
8010       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
8011       .access = PL2_W, .type = ARM_CP_NOP },
8012     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
8013       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
8014       .access = PL2_W, .type = ARM_CP_NOP },
8015     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8016       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
8017       .access = PL2_W, .type = ARM_CP_NOP },
8018     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8019       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
8020       .access = PL2_W, .type = ARM_CP_NOP },
8021     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
8022       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
8023       .access = PL3_W, .type = ARM_CP_NO_RAW,
8024       .writefn = tlbi_aa64_alle3is_write },
8025     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
8026       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
8027       .access = PL3_W, .type = ARM_CP_NO_RAW,
8028       .writefn = tlbi_aa64_vae3is_write },
8029     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
8030       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
8031       .access = PL3_W, .type = ARM_CP_NO_RAW,
8032       .writefn = tlbi_aa64_vae3is_write },
8033 };
8034 
8035 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
8036 {
8037     Error *err = NULL;
8038     uint64_t ret;
8039 
8040     /* Success sets NZCV = 0000.  */
8041     env->NF = env->CF = env->VF = 0, env->ZF = 1;
8042 
8043     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
8044         /*
8045          * ??? Failed, for unknown reasons in the crypto subsystem.
8046          * The best we can do is log the reason and return the
8047          * timed-out indication to the guest.  There is no reason
8048          * we know to expect this failure to be transitory, so the
8049          * guest may well hang retrying the operation.
8050          */
8051         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
8052                       ri->name, error_get_pretty(err));
8053         error_free(err);
8054 
8055         env->ZF = 0; /* NZCF = 0100 */
8056         return 0;
8057     }
8058     return ret;
8059 }
8060 
8061 /* We do not support re-seeding, so the two registers operate the same.  */
8062 static const ARMCPRegInfo rndr_reginfo[] = {
8063     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
8064       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8065       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
8066       .access = PL0_R, .readfn = rndr_readfn },
8067     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
8068       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8069       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
8070       .access = PL0_R, .readfn = rndr_readfn },
8071 };
8072 
8073 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
8074                           uint64_t value)
8075 {
8076 #ifdef CONFIG_TCG
8077     ARMCPU *cpu = env_archcpu(env);
8078     /* CTR_EL0 System register -> DminLine, bits [19:16] */
8079     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
8080     uint64_t vaddr_in = (uint64_t) value;
8081     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
8082     void *haddr;
8083     int mem_idx = arm_env_mmu_index(env);
8084 
8085     /* This won't be crossing page boundaries */
8086     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
8087     if (haddr) {
8088 #ifndef CONFIG_USER_ONLY
8089 
8090         ram_addr_t offset;
8091         MemoryRegion *mr;
8092 
8093         /* RCU lock is already being held */
8094         mr = memory_region_from_host(haddr, &offset);
8095 
8096         if (mr) {
8097             memory_region_writeback(mr, offset, dline_size);
8098         }
8099 #endif /*CONFIG_USER_ONLY*/
8100     }
8101 #else
8102     /* Handled by hardware accelerator. */
8103     g_assert_not_reached();
8104 #endif /* CONFIG_TCG */
8105 }
8106 
8107 static const ARMCPRegInfo dcpop_reg[] = {
8108     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
8109       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
8110       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8111       .fgt = FGT_DCCVAP,
8112       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8113 };
8114 
8115 static const ARMCPRegInfo dcpodp_reg[] = {
8116     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
8117       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
8118       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8119       .fgt = FGT_DCCVADP,
8120       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8121 };
8122 
8123 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
8124                                        bool isread)
8125 {
8126     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
8127         return CP_ACCESS_TRAP_EL2;
8128     }
8129 
8130     return CP_ACCESS_OK;
8131 }
8132 
8133 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
8134                                  bool isread)
8135 {
8136     int el = arm_current_el(env);
8137     if (el < 2 && arm_is_el2_enabled(env)) {
8138         uint64_t hcr = arm_hcr_el2_eff(env);
8139         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8140             return CP_ACCESS_TRAP_EL2;
8141         }
8142     }
8143     if (el < 3 &&
8144         arm_feature(env, ARM_FEATURE_EL3) &&
8145         !(env->cp15.scr_el3 & SCR_ATA)) {
8146         return CP_ACCESS_TRAP_EL3;
8147     }
8148     return CP_ACCESS_OK;
8149 }
8150 
8151 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
8152                                       bool isread)
8153 {
8154     CPAccessResult nv1 = access_nv1(env, ri, isread);
8155 
8156     if (nv1 != CP_ACCESS_OK) {
8157         return nv1;
8158     }
8159     return access_mte(env, ri, isread);
8160 }
8161 
8162 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
8163                                       bool isread)
8164 {
8165     /*
8166      * TFSR_EL2: similar to generic access_mte(), but we need to
8167      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
8168      * if NV2 is enabled then we will redirect this to TFSR_EL1
8169      * after doing the HCR and SCR ATA traps; otherwise this will
8170      * be a trap to EL2 and the HCR/SCR traps do not apply.
8171      */
8172     int el = arm_current_el(env);
8173 
8174     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
8175         return CP_ACCESS_OK;
8176     }
8177     if (el < 2 && arm_is_el2_enabled(env)) {
8178         uint64_t hcr = arm_hcr_el2_eff(env);
8179         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8180             return CP_ACCESS_TRAP_EL2;
8181         }
8182     }
8183     if (el < 3 &&
8184         arm_feature(env, ARM_FEATURE_EL3) &&
8185         !(env->cp15.scr_el3 & SCR_ATA)) {
8186         return CP_ACCESS_TRAP_EL3;
8187     }
8188     return CP_ACCESS_OK;
8189 }
8190 
8191 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
8192 {
8193     return env->pstate & PSTATE_TCO;
8194 }
8195 
8196 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
8197 {
8198     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
8199 }
8200 
8201 static const ARMCPRegInfo mte_reginfo[] = {
8202     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
8203       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
8204       .access = PL1_RW, .accessfn = access_mte,
8205       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
8206     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
8207       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
8208       .access = PL1_RW, .accessfn = access_tfsr_el1,
8209       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
8210       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
8211     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
8212       .type = ARM_CP_NV2_REDIRECT,
8213       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
8214       .access = PL2_RW, .accessfn = access_tfsr_el2,
8215       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
8216     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
8217       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
8218       .access = PL3_RW,
8219       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
8220     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
8221       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
8222       .access = PL1_RW, .accessfn = access_mte,
8223       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
8224     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
8225       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
8226       .access = PL1_RW, .accessfn = access_mte,
8227       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
8228     { .name = "TCO", .state = ARM_CP_STATE_AA64,
8229       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8230       .type = ARM_CP_NO_RAW,
8231       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8232     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8233       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8234       .type = ARM_CP_NOP, .access = PL1_W,
8235       .fgt = FGT_DCIVAC,
8236       .accessfn = aa64_cacheop_poc_access },
8237     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8238       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8239       .fgt = FGT_DCISW,
8240       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8241     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8242       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8243       .type = ARM_CP_NOP, .access = PL1_W,
8244       .fgt = FGT_DCIVAC,
8245       .accessfn = aa64_cacheop_poc_access },
8246     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8247       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8248       .fgt = FGT_DCISW,
8249       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8250     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8251       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8252       .fgt = FGT_DCCSW,
8253       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8254     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8255       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8256       .fgt = FGT_DCCSW,
8257       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8258     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8259       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8260       .fgt = FGT_DCCISW,
8261       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8262     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8263       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8264       .fgt = FGT_DCCISW,
8265       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8266 };
8267 
8268 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8269     { .name = "TCO", .state = ARM_CP_STATE_AA64,
8270       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8271       .type = ARM_CP_CONST, .access = PL0_RW, },
8272 };
8273 
8274 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8275     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8276       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8277       .type = ARM_CP_NOP, .access = PL0_W,
8278       .fgt = FGT_DCCVAC,
8279       .accessfn = aa64_cacheop_poc_access },
8280     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8281       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8282       .type = ARM_CP_NOP, .access = PL0_W,
8283       .fgt = FGT_DCCVAC,
8284       .accessfn = aa64_cacheop_poc_access },
8285     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8286       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8287       .type = ARM_CP_NOP, .access = PL0_W,
8288       .fgt = FGT_DCCVAP,
8289       .accessfn = aa64_cacheop_poc_access },
8290     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8291       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8292       .type = ARM_CP_NOP, .access = PL0_W,
8293       .fgt = FGT_DCCVAP,
8294       .accessfn = aa64_cacheop_poc_access },
8295     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8296       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8297       .type = ARM_CP_NOP, .access = PL0_W,
8298       .fgt = FGT_DCCVADP,
8299       .accessfn = aa64_cacheop_poc_access },
8300     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8301       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8302       .type = ARM_CP_NOP, .access = PL0_W,
8303       .fgt = FGT_DCCVADP,
8304       .accessfn = aa64_cacheop_poc_access },
8305     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8306       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8307       .type = ARM_CP_NOP, .access = PL0_W,
8308       .fgt = FGT_DCCIVAC,
8309       .accessfn = aa64_cacheop_poc_access },
8310     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8311       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8312       .type = ARM_CP_NOP, .access = PL0_W,
8313       .fgt = FGT_DCCIVAC,
8314       .accessfn = aa64_cacheop_poc_access },
8315     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8316       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8317       .access = PL0_W, .type = ARM_CP_DC_GVA,
8318 #ifndef CONFIG_USER_ONLY
8319       /* Avoid overhead of an access check that always passes in user-mode */
8320       .accessfn = aa64_zva_access,
8321       .fgt = FGT_DCZVA,
8322 #endif
8323     },
8324     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8325       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8326       .access = PL0_W, .type = ARM_CP_DC_GZVA,
8327 #ifndef CONFIG_USER_ONLY
8328       /* Avoid overhead of an access check that always passes in user-mode */
8329       .accessfn = aa64_zva_access,
8330       .fgt = FGT_DCZVA,
8331 #endif
8332     },
8333 };
8334 
8335 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8336                                      bool isread)
8337 {
8338     uint64_t hcr = arm_hcr_el2_eff(env);
8339     int el = arm_current_el(env);
8340 
8341     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8342         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8343             if (hcr & HCR_TGE) {
8344                 return CP_ACCESS_TRAP_EL2;
8345             }
8346             return CP_ACCESS_TRAP;
8347         }
8348     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8349         return CP_ACCESS_TRAP_EL2;
8350     }
8351     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8352         return CP_ACCESS_TRAP_EL2;
8353     }
8354     if (el < 3
8355         && arm_feature(env, ARM_FEATURE_EL3)
8356         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8357         return CP_ACCESS_TRAP_EL3;
8358     }
8359     return CP_ACCESS_OK;
8360 }
8361 
8362 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8363                                          const ARMCPRegInfo *ri,
8364                                          bool isread)
8365 {
8366     CPAccessResult nv1 = access_nv1(env, ri, isread);
8367 
8368     if (nv1 != CP_ACCESS_OK) {
8369         return nv1;
8370     }
8371     return access_scxtnum(env, ri, isread);
8372 }
8373 
8374 static const ARMCPRegInfo scxtnum_reginfo[] = {
8375     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8376       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8377       .access = PL0_RW, .accessfn = access_scxtnum,
8378       .fgt = FGT_SCXTNUM_EL0,
8379       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8380     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8381       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8382       .access = PL1_RW, .accessfn = access_scxtnum_el1,
8383       .fgt = FGT_SCXTNUM_EL1,
8384       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8385       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8386     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8387       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8388       .access = PL2_RW, .accessfn = access_scxtnum,
8389       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8390     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8391       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8392       .access = PL3_RW,
8393       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8394 };
8395 
8396 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8397                                  bool isread)
8398 {
8399     if (arm_current_el(env) == 2 &&
8400         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8401         return CP_ACCESS_TRAP_EL3;
8402     }
8403     return CP_ACCESS_OK;
8404 }
8405 
8406 static const ARMCPRegInfo fgt_reginfo[] = {
8407     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8408       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8409       .nv2_redirect_offset = 0x1b8,
8410       .access = PL2_RW, .accessfn = access_fgt,
8411       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8412     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8413       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8414       .nv2_redirect_offset = 0x1c0,
8415       .access = PL2_RW, .accessfn = access_fgt,
8416       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8417     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8418       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8419       .nv2_redirect_offset = 0x1d0,
8420       .access = PL2_RW, .accessfn = access_fgt,
8421       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8422     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8423       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8424       .nv2_redirect_offset = 0x1d8,
8425       .access = PL2_RW, .accessfn = access_fgt,
8426       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8427     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8428       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8429       .nv2_redirect_offset = 0x1c8,
8430       .access = PL2_RW, .accessfn = access_fgt,
8431       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8432 };
8433 
8434 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8435                        uint64_t value)
8436 {
8437     /*
8438      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8439      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8440      * about the RESS bits at the top -- we choose the "generate an EL2
8441      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8442      * the ptw.c code detect the resulting invalid address).
8443      */
8444     env->cp15.vncr_el2 = value & ~0xfffULL;
8445 }
8446 
8447 static const ARMCPRegInfo nv2_reginfo[] = {
8448     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8449       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8450       .access = PL2_RW,
8451       .writefn = vncr_write,
8452       .nv2_redirect_offset = 0xb0,
8453       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8454 };
8455 
8456 #endif /* TARGET_AARCH64 */
8457 
8458 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8459                                      bool isread)
8460 {
8461     int el = arm_current_el(env);
8462 
8463     if (el == 0) {
8464         uint64_t sctlr = arm_sctlr(env, el);
8465         if (!(sctlr & SCTLR_EnRCTX)) {
8466             return CP_ACCESS_TRAP;
8467         }
8468     } else if (el == 1) {
8469         uint64_t hcr = arm_hcr_el2_eff(env);
8470         if (hcr & HCR_NV) {
8471             return CP_ACCESS_TRAP_EL2;
8472         }
8473     }
8474     return CP_ACCESS_OK;
8475 }
8476 
8477 static const ARMCPRegInfo predinv_reginfo[] = {
8478     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8479       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8480       .fgt = FGT_CFPRCTX,
8481       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8482     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8483       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8484       .fgt = FGT_DVPRCTX,
8485       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8486     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8487       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8488       .fgt = FGT_CPPRCTX,
8489       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8490     /*
8491      * Note the AArch32 opcodes have a different OPC1.
8492      */
8493     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8494       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8495       .fgt = FGT_CFPRCTX,
8496       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8497     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8498       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8499       .fgt = FGT_DVPRCTX,
8500       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8501     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8502       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8503       .fgt = FGT_CPPRCTX,
8504       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8505 };
8506 
8507 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8508 {
8509     /* Read the high 32 bits of the current CCSIDR */
8510     return extract64(ccsidr_read(env, ri), 32, 32);
8511 }
8512 
8513 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8514     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8515       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8516       .access = PL1_R,
8517       .accessfn = access_tid4,
8518       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8519 };
8520 
8521 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8522                                        bool isread)
8523 {
8524     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8525         return CP_ACCESS_TRAP_EL2;
8526     }
8527 
8528     return CP_ACCESS_OK;
8529 }
8530 
8531 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8532                                        bool isread)
8533 {
8534     if (arm_feature(env, ARM_FEATURE_V8)) {
8535         return access_aa64_tid3(env, ri, isread);
8536     }
8537 
8538     return CP_ACCESS_OK;
8539 }
8540 
8541 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8542                                      bool isread)
8543 {
8544     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8545         return CP_ACCESS_TRAP_EL2;
8546     }
8547 
8548     return CP_ACCESS_OK;
8549 }
8550 
8551 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8552                                         const ARMCPRegInfo *ri, bool isread)
8553 {
8554     /*
8555      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8556      * in v7A, not in v8A.
8557      */
8558     if (!arm_feature(env, ARM_FEATURE_V8) &&
8559         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8560         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8561         return CP_ACCESS_TRAP_EL2;
8562     }
8563     return CP_ACCESS_OK;
8564 }
8565 
8566 static const ARMCPRegInfo jazelle_regs[] = {
8567     { .name = "JIDR",
8568       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8569       .access = PL1_R, .accessfn = access_jazelle,
8570       .type = ARM_CP_CONST, .resetvalue = 0 },
8571     { .name = "JOSCR",
8572       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8573       .accessfn = access_joscr_jmcr,
8574       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8575     { .name = "JMCR",
8576       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8577       .accessfn = access_joscr_jmcr,
8578       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8579 };
8580 
8581 static const ARMCPRegInfo contextidr_el2 = {
8582     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8583     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8584     .access = PL2_RW,
8585     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8586 };
8587 
8588 static const ARMCPRegInfo vhe_reginfo[] = {
8589     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8590       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8591       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8592       .raw_writefn = raw_write,
8593       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8594 #ifndef CONFIG_USER_ONLY
8595     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8596       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8597       .fieldoffset =
8598         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8599       .type = ARM_CP_IO, .access = PL2_RW,
8600       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8601     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8602       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8603       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8604       .resetfn = gt_hv_timer_reset,
8605       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8606     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8607       .type = ARM_CP_IO,
8608       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8609       .access = PL2_RW,
8610       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8611       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8612     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8613       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8614       .type = ARM_CP_IO | ARM_CP_ALIAS,
8615       .access = PL2_RW, .accessfn = access_el1nvpct,
8616       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8617       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8618       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8619     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8620       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8621       .type = ARM_CP_IO | ARM_CP_ALIAS,
8622       .access = PL2_RW, .accessfn = access_el1nvvct,
8623       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8624       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8625       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8626     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8627       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8628       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8629       .access = PL2_RW, .accessfn = e2h_access,
8630       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8631     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8632       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8633       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8634       .access = PL2_RW, .accessfn = e2h_access,
8635       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8636     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8637       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8638       .type = ARM_CP_IO | ARM_CP_ALIAS,
8639       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8640       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8641       .access = PL2_RW, .accessfn = access_el1nvpct,
8642       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8643     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8644       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8645       .type = ARM_CP_IO | ARM_CP_ALIAS,
8646       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8647       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8648       .access = PL2_RW, .accessfn = access_el1nvvct,
8649       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8650 #endif
8651 };
8652 
8653 #ifndef CONFIG_USER_ONLY
8654 static const ARMCPRegInfo ats1e1_reginfo[] = {
8655     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8656       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8657       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8658       .fgt = FGT_ATS1E1RP,
8659       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8660     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8661       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8662       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8663       .fgt = FGT_ATS1E1WP,
8664       .accessfn = at_s1e01_access, .writefn = ats_write64 },
8665 };
8666 
8667 static const ARMCPRegInfo ats1cp_reginfo[] = {
8668     { .name = "ATS1CPRP",
8669       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8670       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8671       .writefn = ats_write },
8672     { .name = "ATS1CPWP",
8673       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8674       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8675       .writefn = ats_write },
8676 };
8677 #endif
8678 
8679 /*
8680  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8681  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8682  * is non-zero, which is never for ARMv7, optionally in ARMv8
8683  * and mandatorily for ARMv8.2 and up.
8684  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8685  * implementation is RAZ/WI we can ignore this detail, as we
8686  * do for ACTLR.
8687  */
8688 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8689     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8690       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8691       .access = PL1_RW, .accessfn = access_tacr,
8692       .type = ARM_CP_CONST, .resetvalue = 0 },
8693     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8694       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8695       .access = PL2_RW, .type = ARM_CP_CONST,
8696       .resetvalue = 0 },
8697 };
8698 
8699 void register_cp_regs_for_features(ARMCPU *cpu)
8700 {
8701     /* Register all the coprocessor registers based on feature bits */
8702     CPUARMState *env = &cpu->env;
8703     if (arm_feature(env, ARM_FEATURE_M)) {
8704         /* M profile has no coprocessor registers */
8705         return;
8706     }
8707 
8708     define_arm_cp_regs(cpu, cp_reginfo);
8709     if (!arm_feature(env, ARM_FEATURE_V8)) {
8710         /*
8711          * Must go early as it is full of wildcards that may be
8712          * overridden by later definitions.
8713          */
8714         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8715     }
8716 
8717     if (arm_feature(env, ARM_FEATURE_V6)) {
8718         /* The ID registers all have impdef reset values */
8719         ARMCPRegInfo v6_idregs[] = {
8720             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8721               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8722               .access = PL1_R, .type = ARM_CP_CONST,
8723               .accessfn = access_aa32_tid3,
8724               .resetvalue = cpu->isar.id_pfr0 },
8725             /*
8726              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8727              * the value of the GIC field until after we define these regs.
8728              */
8729             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8730               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8731               .access = PL1_R, .type = ARM_CP_NO_RAW,
8732               .accessfn = access_aa32_tid3,
8733 #ifdef CONFIG_USER_ONLY
8734               .type = ARM_CP_CONST,
8735               .resetvalue = cpu->isar.id_pfr1,
8736 #else
8737               .type = ARM_CP_NO_RAW,
8738               .accessfn = access_aa32_tid3,
8739               .readfn = id_pfr1_read,
8740               .writefn = arm_cp_write_ignore
8741 #endif
8742             },
8743             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8744               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8745               .access = PL1_R, .type = ARM_CP_CONST,
8746               .accessfn = access_aa32_tid3,
8747               .resetvalue = cpu->isar.id_dfr0 },
8748             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8749               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8750               .access = PL1_R, .type = ARM_CP_CONST,
8751               .accessfn = access_aa32_tid3,
8752               .resetvalue = cpu->id_afr0 },
8753             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8754               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8755               .access = PL1_R, .type = ARM_CP_CONST,
8756               .accessfn = access_aa32_tid3,
8757               .resetvalue = cpu->isar.id_mmfr0 },
8758             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8759               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8760               .access = PL1_R, .type = ARM_CP_CONST,
8761               .accessfn = access_aa32_tid3,
8762               .resetvalue = cpu->isar.id_mmfr1 },
8763             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8764               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8765               .access = PL1_R, .type = ARM_CP_CONST,
8766               .accessfn = access_aa32_tid3,
8767               .resetvalue = cpu->isar.id_mmfr2 },
8768             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8769               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8770               .access = PL1_R, .type = ARM_CP_CONST,
8771               .accessfn = access_aa32_tid3,
8772               .resetvalue = cpu->isar.id_mmfr3 },
8773             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8774               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8775               .access = PL1_R, .type = ARM_CP_CONST,
8776               .accessfn = access_aa32_tid3,
8777               .resetvalue = cpu->isar.id_isar0 },
8778             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8779               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8780               .access = PL1_R, .type = ARM_CP_CONST,
8781               .accessfn = access_aa32_tid3,
8782               .resetvalue = cpu->isar.id_isar1 },
8783             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8785               .access = PL1_R, .type = ARM_CP_CONST,
8786               .accessfn = access_aa32_tid3,
8787               .resetvalue = cpu->isar.id_isar2 },
8788             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8790               .access = PL1_R, .type = ARM_CP_CONST,
8791               .accessfn = access_aa32_tid3,
8792               .resetvalue = cpu->isar.id_isar3 },
8793             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8794               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8795               .access = PL1_R, .type = ARM_CP_CONST,
8796               .accessfn = access_aa32_tid3,
8797               .resetvalue = cpu->isar.id_isar4 },
8798             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8799               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8800               .access = PL1_R, .type = ARM_CP_CONST,
8801               .accessfn = access_aa32_tid3,
8802               .resetvalue = cpu->isar.id_isar5 },
8803             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8804               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8805               .access = PL1_R, .type = ARM_CP_CONST,
8806               .accessfn = access_aa32_tid3,
8807               .resetvalue = cpu->isar.id_mmfr4 },
8808             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8809               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8810               .access = PL1_R, .type = ARM_CP_CONST,
8811               .accessfn = access_aa32_tid3,
8812               .resetvalue = cpu->isar.id_isar6 },
8813         };
8814         define_arm_cp_regs(cpu, v6_idregs);
8815         define_arm_cp_regs(cpu, v6_cp_reginfo);
8816     } else {
8817         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8818     }
8819     if (arm_feature(env, ARM_FEATURE_V6K)) {
8820         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8821     }
8822     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8823         !arm_feature(env, ARM_FEATURE_PMSA)) {
8824         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8825     }
8826     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8827         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8828     }
8829     if (arm_feature(env, ARM_FEATURE_V7)) {
8830         ARMCPRegInfo clidr = {
8831             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8832             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8833             .access = PL1_R, .type = ARM_CP_CONST,
8834             .accessfn = access_tid4,
8835             .fgt = FGT_CLIDR_EL1,
8836             .resetvalue = cpu->clidr
8837         };
8838         define_one_arm_cp_reg(cpu, &clidr);
8839         define_arm_cp_regs(cpu, v7_cp_reginfo);
8840         define_debug_regs(cpu);
8841         define_pmu_regs(cpu);
8842     } else {
8843         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8844     }
8845     if (arm_feature(env, ARM_FEATURE_V8)) {
8846         /*
8847          * v8 ID registers, which all have impdef reset values.
8848          * Note that within the ID register ranges the unused slots
8849          * must all RAZ, not UNDEF; future architecture versions may
8850          * define new registers here.
8851          * ID registers which are AArch64 views of the AArch32 ID registers
8852          * which already existed in v6 and v7 are handled elsewhere,
8853          * in v6_idregs[].
8854          */
8855         int i;
8856         ARMCPRegInfo v8_idregs[] = {
8857             /*
8858              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8859              * emulation because we don't know the right value for the
8860              * GIC field until after we define these regs.
8861              */
8862             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8863               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8864               .access = PL1_R,
8865 #ifdef CONFIG_USER_ONLY
8866               .type = ARM_CP_CONST,
8867               .resetvalue = cpu->isar.id_aa64pfr0
8868 #else
8869               .type = ARM_CP_NO_RAW,
8870               .accessfn = access_aa64_tid3,
8871               .readfn = id_aa64pfr0_read,
8872               .writefn = arm_cp_write_ignore
8873 #endif
8874             },
8875             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8876               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8877               .access = PL1_R, .type = ARM_CP_CONST,
8878               .accessfn = access_aa64_tid3,
8879               .resetvalue = cpu->isar.id_aa64pfr1},
8880             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8881               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8882               .access = PL1_R, .type = ARM_CP_CONST,
8883               .accessfn = access_aa64_tid3,
8884               .resetvalue = 0 },
8885             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8886               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8887               .access = PL1_R, .type = ARM_CP_CONST,
8888               .accessfn = access_aa64_tid3,
8889               .resetvalue = 0 },
8890             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8891               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8892               .access = PL1_R, .type = ARM_CP_CONST,
8893               .accessfn = access_aa64_tid3,
8894               .resetvalue = cpu->isar.id_aa64zfr0 },
8895             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8896               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8897               .access = PL1_R, .type = ARM_CP_CONST,
8898               .accessfn = access_aa64_tid3,
8899               .resetvalue = cpu->isar.id_aa64smfr0 },
8900             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8901               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8902               .access = PL1_R, .type = ARM_CP_CONST,
8903               .accessfn = access_aa64_tid3,
8904               .resetvalue = 0 },
8905             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8906               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8907               .access = PL1_R, .type = ARM_CP_CONST,
8908               .accessfn = access_aa64_tid3,
8909               .resetvalue = 0 },
8910             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8911               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8912               .access = PL1_R, .type = ARM_CP_CONST,
8913               .accessfn = access_aa64_tid3,
8914               .resetvalue = cpu->isar.id_aa64dfr0 },
8915             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8916               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8917               .access = PL1_R, .type = ARM_CP_CONST,
8918               .accessfn = access_aa64_tid3,
8919               .resetvalue = cpu->isar.id_aa64dfr1 },
8920             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8921               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8922               .access = PL1_R, .type = ARM_CP_CONST,
8923               .accessfn = access_aa64_tid3,
8924               .resetvalue = 0 },
8925             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8926               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8927               .access = PL1_R, .type = ARM_CP_CONST,
8928               .accessfn = access_aa64_tid3,
8929               .resetvalue = 0 },
8930             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8931               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8932               .access = PL1_R, .type = ARM_CP_CONST,
8933               .accessfn = access_aa64_tid3,
8934               .resetvalue = cpu->id_aa64afr0 },
8935             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8936               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8937               .access = PL1_R, .type = ARM_CP_CONST,
8938               .accessfn = access_aa64_tid3,
8939               .resetvalue = cpu->id_aa64afr1 },
8940             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8941               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8942               .access = PL1_R, .type = ARM_CP_CONST,
8943               .accessfn = access_aa64_tid3,
8944               .resetvalue = 0 },
8945             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8946               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8947               .access = PL1_R, .type = ARM_CP_CONST,
8948               .accessfn = access_aa64_tid3,
8949               .resetvalue = 0 },
8950             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8951               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8952               .access = PL1_R, .type = ARM_CP_CONST,
8953               .accessfn = access_aa64_tid3,
8954               .resetvalue = cpu->isar.id_aa64isar0 },
8955             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8956               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8957               .access = PL1_R, .type = ARM_CP_CONST,
8958               .accessfn = access_aa64_tid3,
8959               .resetvalue = cpu->isar.id_aa64isar1 },
8960             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8961               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8962               .access = PL1_R, .type = ARM_CP_CONST,
8963               .accessfn = access_aa64_tid3,
8964               .resetvalue = cpu->isar.id_aa64isar2 },
8965             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8966               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8967               .access = PL1_R, .type = ARM_CP_CONST,
8968               .accessfn = access_aa64_tid3,
8969               .resetvalue = 0 },
8970             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8971               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8972               .access = PL1_R, .type = ARM_CP_CONST,
8973               .accessfn = access_aa64_tid3,
8974               .resetvalue = 0 },
8975             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8976               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8977               .access = PL1_R, .type = ARM_CP_CONST,
8978               .accessfn = access_aa64_tid3,
8979               .resetvalue = 0 },
8980             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8981               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8982               .access = PL1_R, .type = ARM_CP_CONST,
8983               .accessfn = access_aa64_tid3,
8984               .resetvalue = 0 },
8985             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8986               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8987               .access = PL1_R, .type = ARM_CP_CONST,
8988               .accessfn = access_aa64_tid3,
8989               .resetvalue = 0 },
8990             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8991               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8992               .access = PL1_R, .type = ARM_CP_CONST,
8993               .accessfn = access_aa64_tid3,
8994               .resetvalue = cpu->isar.id_aa64mmfr0 },
8995             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8996               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8997               .access = PL1_R, .type = ARM_CP_CONST,
8998               .accessfn = access_aa64_tid3,
8999               .resetvalue = cpu->isar.id_aa64mmfr1 },
9000             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
9001               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
9002               .access = PL1_R, .type = ARM_CP_CONST,
9003               .accessfn = access_aa64_tid3,
9004               .resetvalue = cpu->isar.id_aa64mmfr2 },
9005             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
9006               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
9007               .access = PL1_R, .type = ARM_CP_CONST,
9008               .accessfn = access_aa64_tid3,
9009               .resetvalue = cpu->isar.id_aa64mmfr3 },
9010             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9011               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
9012               .access = PL1_R, .type = ARM_CP_CONST,
9013               .accessfn = access_aa64_tid3,
9014               .resetvalue = 0 },
9015             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9016               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
9017               .access = PL1_R, .type = ARM_CP_CONST,
9018               .accessfn = access_aa64_tid3,
9019               .resetvalue = 0 },
9020             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9021               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
9022               .access = PL1_R, .type = ARM_CP_CONST,
9023               .accessfn = access_aa64_tid3,
9024               .resetvalue = 0 },
9025             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9026               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
9027               .access = PL1_R, .type = ARM_CP_CONST,
9028               .accessfn = access_aa64_tid3,
9029               .resetvalue = 0 },
9030             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
9031               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9032               .access = PL1_R, .type = ARM_CP_CONST,
9033               .accessfn = access_aa64_tid3,
9034               .resetvalue = cpu->isar.mvfr0 },
9035             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
9036               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9037               .access = PL1_R, .type = ARM_CP_CONST,
9038               .accessfn = access_aa64_tid3,
9039               .resetvalue = cpu->isar.mvfr1 },
9040             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
9041               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9042               .access = PL1_R, .type = ARM_CP_CONST,
9043               .accessfn = access_aa64_tid3,
9044               .resetvalue = cpu->isar.mvfr2 },
9045             /*
9046              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
9047              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
9048              * as RAZ, since it is in the "reserved for future ID
9049              * registers, RAZ" part of the AArch32 encoding space.
9050              */
9051             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
9052               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9053               .access = PL1_R, .type = ARM_CP_CONST,
9054               .accessfn = access_aa64_tid3,
9055               .resetvalue = 0 },
9056             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
9057               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9058               .access = PL1_R, .type = ARM_CP_CONST,
9059               .accessfn = access_aa64_tid3,
9060               .resetvalue = 0 },
9061             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
9062               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9063               .access = PL1_R, .type = ARM_CP_CONST,
9064               .accessfn = access_aa64_tid3,
9065               .resetvalue = 0 },
9066             /*
9067              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
9068              * they're also RAZ for AArch64, and in v8 are gradually
9069              * being filled with AArch64-view-of-AArch32-ID-register
9070              * for new ID registers.
9071              */
9072             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
9073               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
9074               .access = PL1_R, .type = ARM_CP_CONST,
9075               .accessfn = access_aa64_tid3,
9076               .resetvalue = 0 },
9077             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
9078               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
9079               .access = PL1_R, .type = ARM_CP_CONST,
9080               .accessfn = access_aa64_tid3,
9081               .resetvalue = cpu->isar.id_pfr2 },
9082             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
9083               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
9084               .access = PL1_R, .type = ARM_CP_CONST,
9085               .accessfn = access_aa64_tid3,
9086               .resetvalue = cpu->isar.id_dfr1 },
9087             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
9088               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
9089               .access = PL1_R, .type = ARM_CP_CONST,
9090               .accessfn = access_aa64_tid3,
9091               .resetvalue = cpu->isar.id_mmfr5 },
9092             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
9093               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
9094               .access = PL1_R, .type = ARM_CP_CONST,
9095               .accessfn = access_aa64_tid3,
9096               .resetvalue = 0 },
9097             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
9098               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
9099               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9100               .fgt = FGT_PMCEIDN_EL0,
9101               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
9102             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
9103               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
9104               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9105               .fgt = FGT_PMCEIDN_EL0,
9106               .resetvalue = cpu->pmceid0 },
9107             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
9108               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
9109               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9110               .fgt = FGT_PMCEIDN_EL0,
9111               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
9112             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
9113               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
9114               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9115               .fgt = FGT_PMCEIDN_EL0,
9116               .resetvalue = cpu->pmceid1 },
9117         };
9118 #ifdef CONFIG_USER_ONLY
9119         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
9120             { .name = "ID_AA64PFR0_EL1",
9121               .exported_bits = R_ID_AA64PFR0_FP_MASK |
9122                                R_ID_AA64PFR0_ADVSIMD_MASK |
9123                                R_ID_AA64PFR0_SVE_MASK |
9124                                R_ID_AA64PFR0_DIT_MASK,
9125               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
9126                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
9127             { .name = "ID_AA64PFR1_EL1",
9128               .exported_bits = R_ID_AA64PFR1_BT_MASK |
9129                                R_ID_AA64PFR1_SSBS_MASK |
9130                                R_ID_AA64PFR1_MTE_MASK |
9131                                R_ID_AA64PFR1_SME_MASK },
9132             { .name = "ID_AA64PFR*_EL1_RESERVED",
9133               .is_glob = true },
9134             { .name = "ID_AA64ZFR0_EL1",
9135               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
9136                                R_ID_AA64ZFR0_AES_MASK |
9137                                R_ID_AA64ZFR0_BITPERM_MASK |
9138                                R_ID_AA64ZFR0_BFLOAT16_MASK |
9139                                R_ID_AA64ZFR0_B16B16_MASK |
9140                                R_ID_AA64ZFR0_SHA3_MASK |
9141                                R_ID_AA64ZFR0_SM4_MASK |
9142                                R_ID_AA64ZFR0_I8MM_MASK |
9143                                R_ID_AA64ZFR0_F32MM_MASK |
9144                                R_ID_AA64ZFR0_F64MM_MASK },
9145             { .name = "ID_AA64SMFR0_EL1",
9146               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
9147                                R_ID_AA64SMFR0_BI32I32_MASK |
9148                                R_ID_AA64SMFR0_B16F32_MASK |
9149                                R_ID_AA64SMFR0_F16F32_MASK |
9150                                R_ID_AA64SMFR0_I8I32_MASK |
9151                                R_ID_AA64SMFR0_F16F16_MASK |
9152                                R_ID_AA64SMFR0_B16B16_MASK |
9153                                R_ID_AA64SMFR0_I16I32_MASK |
9154                                R_ID_AA64SMFR0_F64F64_MASK |
9155                                R_ID_AA64SMFR0_I16I64_MASK |
9156                                R_ID_AA64SMFR0_SMEVER_MASK |
9157                                R_ID_AA64SMFR0_FA64_MASK },
9158             { .name = "ID_AA64MMFR0_EL1",
9159               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
9160               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
9161                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
9162             { .name = "ID_AA64MMFR1_EL1",
9163               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
9164             { .name = "ID_AA64MMFR2_EL1",
9165               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
9166             { .name = "ID_AA64MMFR3_EL1",
9167               .exported_bits = 0 },
9168             { .name = "ID_AA64MMFR*_EL1_RESERVED",
9169               .is_glob = true },
9170             { .name = "ID_AA64DFR0_EL1",
9171               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
9172             { .name = "ID_AA64DFR1_EL1" },
9173             { .name = "ID_AA64DFR*_EL1_RESERVED",
9174               .is_glob = true },
9175             { .name = "ID_AA64AFR*",
9176               .is_glob = true },
9177             { .name = "ID_AA64ISAR0_EL1",
9178               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
9179                                R_ID_AA64ISAR0_SHA1_MASK |
9180                                R_ID_AA64ISAR0_SHA2_MASK |
9181                                R_ID_AA64ISAR0_CRC32_MASK |
9182                                R_ID_AA64ISAR0_ATOMIC_MASK |
9183                                R_ID_AA64ISAR0_RDM_MASK |
9184                                R_ID_AA64ISAR0_SHA3_MASK |
9185                                R_ID_AA64ISAR0_SM3_MASK |
9186                                R_ID_AA64ISAR0_SM4_MASK |
9187                                R_ID_AA64ISAR0_DP_MASK |
9188                                R_ID_AA64ISAR0_FHM_MASK |
9189                                R_ID_AA64ISAR0_TS_MASK |
9190                                R_ID_AA64ISAR0_RNDR_MASK },
9191             { .name = "ID_AA64ISAR1_EL1",
9192               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
9193                                R_ID_AA64ISAR1_APA_MASK |
9194                                R_ID_AA64ISAR1_API_MASK |
9195                                R_ID_AA64ISAR1_JSCVT_MASK |
9196                                R_ID_AA64ISAR1_FCMA_MASK |
9197                                R_ID_AA64ISAR1_LRCPC_MASK |
9198                                R_ID_AA64ISAR1_GPA_MASK |
9199                                R_ID_AA64ISAR1_GPI_MASK |
9200                                R_ID_AA64ISAR1_FRINTTS_MASK |
9201                                R_ID_AA64ISAR1_SB_MASK |
9202                                R_ID_AA64ISAR1_BF16_MASK |
9203                                R_ID_AA64ISAR1_DGH_MASK |
9204                                R_ID_AA64ISAR1_I8MM_MASK },
9205             { .name = "ID_AA64ISAR2_EL1",
9206               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
9207                                R_ID_AA64ISAR2_RPRES_MASK |
9208                                R_ID_AA64ISAR2_GPA3_MASK |
9209                                R_ID_AA64ISAR2_APA3_MASK |
9210                                R_ID_AA64ISAR2_MOPS_MASK |
9211                                R_ID_AA64ISAR2_BC_MASK |
9212                                R_ID_AA64ISAR2_RPRFM_MASK |
9213                                R_ID_AA64ISAR2_CSSC_MASK },
9214             { .name = "ID_AA64ISAR*_EL1_RESERVED",
9215               .is_glob = true },
9216         };
9217         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
9218 #endif
9219         /*
9220          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
9221          * TODO: For RMR, a write with bit 1 set should do something with
9222          * cpu_reset(). In the meantime, "the bit is strictly a request",
9223          * so we are in spec just ignoring writes.
9224          */
9225         if (!arm_feature(env, ARM_FEATURE_EL3) &&
9226             !arm_feature(env, ARM_FEATURE_EL2)) {
9227             ARMCPRegInfo el1_reset_regs[] = {
9228                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
9229                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9230                   .access = PL1_R,
9231                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9232                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9233                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9234                   .access = PL1_RW, .type = ARM_CP_CONST,
9235                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9236             };
9237             define_arm_cp_regs(cpu, el1_reset_regs);
9238         }
9239         define_arm_cp_regs(cpu, v8_idregs);
9240         define_arm_cp_regs(cpu, v8_cp_reginfo);
9241         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9242             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9243         }
9244 
9245         for (i = 4; i < 16; i++) {
9246             /*
9247              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9248              * For pre-v8 cores there are RAZ patterns for these in
9249              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9250              * v8 extends the "must RAZ" part of the ID register space
9251              * to also cover c0, 0, c{8-15}, {0-7}.
9252              * These are STATE_AA32 because in the AArch64 sysreg space
9253              * c4-c7 is where the AArch64 ID registers live (and we've
9254              * already defined those in v8_idregs[]), and c8-c15 are not
9255              * "must RAZ" for AArch64.
9256              */
9257             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9258             ARMCPRegInfo v8_aa32_raz_idregs = {
9259                 .name = name,
9260                 .state = ARM_CP_STATE_AA32,
9261                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9262                 .access = PL1_R, .type = ARM_CP_CONST,
9263                 .accessfn = access_aa64_tid3,
9264                 .resetvalue = 0 };
9265             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9266         }
9267     }
9268 
9269     /*
9270      * Register the base EL2 cpregs.
9271      * Pre v8, these registers are implemented only as part of the
9272      * Virtualization Extensions (EL2 present).  Beginning with v8,
9273      * if EL2 is missing but EL3 is enabled, mostly these become
9274      * RES0 from EL3, with some specific exceptions.
9275      */
9276     if (arm_feature(env, ARM_FEATURE_EL2)
9277         || (arm_feature(env, ARM_FEATURE_EL3)
9278             && arm_feature(env, ARM_FEATURE_V8))) {
9279         uint64_t vmpidr_def = mpidr_read_val(env);
9280         ARMCPRegInfo vpidr_regs[] = {
9281             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9282               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9283               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9284               .resetvalue = cpu->midr,
9285               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9286               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9287             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9288               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9289               .access = PL2_RW, .resetvalue = cpu->midr,
9290               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9291               .nv2_redirect_offset = 0x88,
9292               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9293             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9294               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9295               .access = PL2_RW, .accessfn = access_el3_aa32ns,
9296               .resetvalue = vmpidr_def,
9297               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9298               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9299             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9300               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9301               .access = PL2_RW, .resetvalue = vmpidr_def,
9302               .type = ARM_CP_EL3_NO_EL2_C_NZ,
9303               .nv2_redirect_offset = 0x50,
9304               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9305         };
9306         /*
9307          * The only field of MDCR_EL2 that has a defined architectural reset
9308          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9309          */
9310         ARMCPRegInfo mdcr_el2 = {
9311             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9312             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9313             .writefn = mdcr_el2_write,
9314             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9315             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9316         };
9317         define_one_arm_cp_reg(cpu, &mdcr_el2);
9318         define_arm_cp_regs(cpu, vpidr_regs);
9319         define_arm_cp_regs(cpu, el2_cp_reginfo);
9320         if (arm_feature(env, ARM_FEATURE_V8)) {
9321             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9322         }
9323         if (cpu_isar_feature(aa64_sel2, cpu)) {
9324             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9325         }
9326         /*
9327          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9328          * See commentary near RMR_EL1.
9329          */
9330         if (!arm_feature(env, ARM_FEATURE_EL3)) {
9331             static const ARMCPRegInfo el2_reset_regs[] = {
9332                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9333                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9334                   .access = PL2_R,
9335                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9336                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9337                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9338                   .access = PL2_R,
9339                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9340                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9341                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9342                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9343             };
9344             define_arm_cp_regs(cpu, el2_reset_regs);
9345         }
9346     }
9347 
9348     /* Register the base EL3 cpregs. */
9349     if (arm_feature(env, ARM_FEATURE_EL3)) {
9350         define_arm_cp_regs(cpu, el3_cp_reginfo);
9351         ARMCPRegInfo el3_regs[] = {
9352             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9353               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9354               .access = PL3_R,
9355               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9356             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9357               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9358               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9359             { .name = "RMR", .state = ARM_CP_STATE_AA32,
9360               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9361               .access = PL3_RW, .type = ARM_CP_CONST,
9362               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9363             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9364               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9365               .access = PL3_RW,
9366               .raw_writefn = raw_write, .writefn = sctlr_write,
9367               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9368               .resetvalue = cpu->reset_sctlr },
9369         };
9370 
9371         define_arm_cp_regs(cpu, el3_regs);
9372     }
9373     /*
9374      * The behaviour of NSACR is sufficiently various that we don't
9375      * try to describe it in a single reginfo:
9376      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
9377      *     reads as constant 0xc00 from NS EL1 and NS EL2
9378      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9379      *  if v7 without EL3, register doesn't exist
9380      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9381      */
9382     if (arm_feature(env, ARM_FEATURE_EL3)) {
9383         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9384             static const ARMCPRegInfo nsacr = {
9385                 .name = "NSACR", .type = ARM_CP_CONST,
9386                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9387                 .access = PL1_RW, .accessfn = nsacr_access,
9388                 .resetvalue = 0xc00
9389             };
9390             define_one_arm_cp_reg(cpu, &nsacr);
9391         } else {
9392             static const ARMCPRegInfo nsacr = {
9393                 .name = "NSACR",
9394                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9395                 .access = PL3_RW | PL1_R,
9396                 .resetvalue = 0,
9397                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9398             };
9399             define_one_arm_cp_reg(cpu, &nsacr);
9400         }
9401     } else {
9402         if (arm_feature(env, ARM_FEATURE_V8)) {
9403             static const ARMCPRegInfo nsacr = {
9404                 .name = "NSACR", .type = ARM_CP_CONST,
9405                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9406                 .access = PL1_R,
9407                 .resetvalue = 0xc00
9408             };
9409             define_one_arm_cp_reg(cpu, &nsacr);
9410         }
9411     }
9412 
9413     if (arm_feature(env, ARM_FEATURE_PMSA)) {
9414         if (arm_feature(env, ARM_FEATURE_V6)) {
9415             /* PMSAv6 not implemented */
9416             assert(arm_feature(env, ARM_FEATURE_V7));
9417             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9418             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9419         } else {
9420             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9421         }
9422     } else {
9423         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9424         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9425         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
9426         if (cpu_isar_feature(aa32_hpd, cpu)) {
9427             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9428         }
9429     }
9430     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9431         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9432     }
9433     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9434         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9435     }
9436     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
9437         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
9438     }
9439 #ifndef CONFIG_USER_ONLY
9440     if (cpu_isar_feature(aa64_ecv, cpu)) {
9441         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
9442     }
9443 #endif
9444     if (arm_feature(env, ARM_FEATURE_VAPA)) {
9445         ARMCPRegInfo vapa_cp_reginfo[] = {
9446             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9447               .access = PL1_RW, .resetvalue = 0,
9448               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9449                                      offsetoflow32(CPUARMState, cp15.par_ns) },
9450               .writefn = par_write},
9451 #ifndef CONFIG_USER_ONLY
9452             /* This underdecoding is safe because the reginfo is NO_RAW. */
9453             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9454               .access = PL1_W, .accessfn = ats_access,
9455               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9456 #endif
9457         };
9458 
9459         /*
9460          * When LPAE exists this 32-bit PAR register is an alias of the
9461          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9462          */
9463         if (arm_feature(env, ARM_FEATURE_LPAE)) {
9464             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9465         }
9466         define_arm_cp_regs(cpu, vapa_cp_reginfo);
9467     }
9468     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9469         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9470     }
9471     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9472         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9473     }
9474     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9475         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9476     }
9477     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9478         define_arm_cp_regs(cpu, omap_cp_reginfo);
9479     }
9480     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9481         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9482     }
9483     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9484         define_arm_cp_regs(cpu, xscale_cp_reginfo);
9485     }
9486     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9487         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9488     }
9489     if (arm_feature(env, ARM_FEATURE_LPAE)) {
9490         define_arm_cp_regs(cpu, lpae_cp_reginfo);
9491     }
9492     if (cpu_isar_feature(aa32_jazelle, cpu)) {
9493         define_arm_cp_regs(cpu, jazelle_regs);
9494     }
9495     /*
9496      * Slightly awkwardly, the OMAP and StrongARM cores need all of
9497      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9498      * be read-only (ie write causes UNDEF exception).
9499      */
9500     {
9501         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9502             /*
9503              * Pre-v8 MIDR space.
9504              * Note that the MIDR isn't a simple constant register because
9505              * of the TI925 behaviour where writes to another register can
9506              * cause the MIDR value to change.
9507              *
9508              * Unimplemented registers in the c15 0 0 0 space default to
9509              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9510              * and friends override accordingly.
9511              */
9512             { .name = "MIDR",
9513               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9514               .access = PL1_R, .resetvalue = cpu->midr,
9515               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9516               .readfn = midr_read,
9517               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9518               .type = ARM_CP_OVERRIDE },
9519             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9520             { .name = "DUMMY",
9521               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9522               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9523             { .name = "DUMMY",
9524               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9525               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9526             { .name = "DUMMY",
9527               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9528               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9529             { .name = "DUMMY",
9530               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9531               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9532             { .name = "DUMMY",
9533               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9534               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9535         };
9536         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9537             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9538               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9539               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9540               .fgt = FGT_MIDR_EL1,
9541               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9542               .readfn = midr_read },
9543             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9544             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9545               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9546               .access = PL1_R, .resetvalue = cpu->midr },
9547             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9548               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9549               .access = PL1_R,
9550               .accessfn = access_aa64_tid1,
9551               .fgt = FGT_REVIDR_EL1,
9552               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9553         };
9554         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9555             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9556             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9557             .access = PL1_R, .resetvalue = cpu->midr
9558         };
9559         ARMCPRegInfo id_cp_reginfo[] = {
9560             /* These are common to v8 and pre-v8 */
9561             { .name = "CTR",
9562               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9563               .access = PL1_R, .accessfn = ctr_el0_access,
9564               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9565             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9566               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9567               .access = PL0_R, .accessfn = ctr_el0_access,
9568               .fgt = FGT_CTR_EL0,
9569               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9570             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9571             { .name = "TCMTR",
9572               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9573               .access = PL1_R,
9574               .accessfn = access_aa32_tid1,
9575               .type = ARM_CP_CONST, .resetvalue = 0 },
9576         };
9577         /* TLBTR is specific to VMSA */
9578         ARMCPRegInfo id_tlbtr_reginfo = {
9579               .name = "TLBTR",
9580               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9581               .access = PL1_R,
9582               .accessfn = access_aa32_tid1,
9583               .type = ARM_CP_CONST, .resetvalue = 0,
9584         };
9585         /* MPUIR is specific to PMSA V6+ */
9586         ARMCPRegInfo id_mpuir_reginfo = {
9587               .name = "MPUIR",
9588               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9589               .access = PL1_R, .type = ARM_CP_CONST,
9590               .resetvalue = cpu->pmsav7_dregion << 8
9591         };
9592         /* HMPUIR is specific to PMSA V8 */
9593         ARMCPRegInfo id_hmpuir_reginfo = {
9594             .name = "HMPUIR",
9595             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9596             .access = PL2_R, .type = ARM_CP_CONST,
9597             .resetvalue = cpu->pmsav8r_hdregion
9598         };
9599         static const ARMCPRegInfo crn0_wi_reginfo = {
9600             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9601             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9602             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9603         };
9604 #ifdef CONFIG_USER_ONLY
9605         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9606             { .name = "MIDR_EL1",
9607               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9608                                R_MIDR_EL1_PARTNUM_MASK |
9609                                R_MIDR_EL1_ARCHITECTURE_MASK |
9610                                R_MIDR_EL1_VARIANT_MASK |
9611                                R_MIDR_EL1_IMPLEMENTER_MASK },
9612             { .name = "REVIDR_EL1" },
9613         };
9614         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9615 #endif
9616         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9617             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9618             size_t i;
9619             /*
9620              * Register the blanket "writes ignored" value first to cover the
9621              * whole space. Then update the specific ID registers to allow write
9622              * access, so that they ignore writes rather than causing them to
9623              * UNDEF.
9624              */
9625             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9626             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9627                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9628             }
9629             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9630                 id_cp_reginfo[i].access = PL1_RW;
9631             }
9632             id_mpuir_reginfo.access = PL1_RW;
9633             id_tlbtr_reginfo.access = PL1_RW;
9634         }
9635         if (arm_feature(env, ARM_FEATURE_V8)) {
9636             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9637             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9638                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9639             }
9640         } else {
9641             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9642         }
9643         define_arm_cp_regs(cpu, id_cp_reginfo);
9644         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9645             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9646         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9647                    arm_feature(env, ARM_FEATURE_V8)) {
9648             uint32_t i = 0;
9649             char *tmp_string;
9650 
9651             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9652             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9653             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9654 
9655             /* Register alias is only valid for first 32 indexes */
9656             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9657                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9658                 uint8_t opc1 = extract32(i, 4, 1);
9659                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9660 
9661                 tmp_string = g_strdup_printf("PRBAR%u", i);
9662                 ARMCPRegInfo tmp_prbarn_reginfo = {
9663                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9664                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9665                     .access = PL1_RW, .resetvalue = 0,
9666                     .accessfn = access_tvm_trvm,
9667                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9668                 };
9669                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9670                 g_free(tmp_string);
9671 
9672                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9673                 tmp_string = g_strdup_printf("PRLAR%u", i);
9674                 ARMCPRegInfo tmp_prlarn_reginfo = {
9675                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9676                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9677                     .access = PL1_RW, .resetvalue = 0,
9678                     .accessfn = access_tvm_trvm,
9679                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9680                 };
9681                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9682                 g_free(tmp_string);
9683             }
9684 
9685             /* Register alias is only valid for first 32 indexes */
9686             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9687                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9688                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9689                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9690 
9691                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9692                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9693                     .name = tmp_string,
9694                     .type = ARM_CP_NO_RAW,
9695                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9696                     .access = PL2_RW, .resetvalue = 0,
9697                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9698                 };
9699                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9700                 g_free(tmp_string);
9701 
9702                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9703                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9704                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9705                     .name = tmp_string,
9706                     .type = ARM_CP_NO_RAW,
9707                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9708                     .access = PL2_RW, .resetvalue = 0,
9709                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9710                 };
9711                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9712                 g_free(tmp_string);
9713             }
9714         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9715             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9716         }
9717     }
9718 
9719     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9720         ARMCPRegInfo mpidr_cp_reginfo[] = {
9721             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9722               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9723               .fgt = FGT_MPIDR_EL1,
9724               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9725         };
9726 #ifdef CONFIG_USER_ONLY
9727         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9728             { .name = "MPIDR_EL1",
9729               .fixed_bits = 0x0000000080000000 },
9730         };
9731         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9732 #endif
9733         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9734     }
9735 
9736     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9737         ARMCPRegInfo auxcr_reginfo[] = {
9738             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9739               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9740               .access = PL1_RW, .accessfn = access_tacr,
9741               .nv2_redirect_offset = 0x118,
9742               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9743             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9744               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9745               .access = PL2_RW, .type = ARM_CP_CONST,
9746               .resetvalue = 0 },
9747             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9748               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9749               .access = PL3_RW, .type = ARM_CP_CONST,
9750               .resetvalue = 0 },
9751         };
9752         define_arm_cp_regs(cpu, auxcr_reginfo);
9753         if (cpu_isar_feature(aa32_ac2, cpu)) {
9754             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9755         }
9756     }
9757 
9758     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9759         /*
9760          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9761          * There are two flavours:
9762          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9763          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9764          *      32-bit register visible to AArch32 at a different encoding
9765          *      to the "flavour 1" register and with the bits rearranged to
9766          *      be able to squash a 64-bit address into the 32-bit view.
9767          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9768          * in future if we support AArch32-only configs of some of the
9769          * AArch64 cores we might need to add a specific feature flag
9770          * to indicate cores with "flavour 2" CBAR.
9771          */
9772         if (arm_feature(env, ARM_FEATURE_V8)) {
9773             /* 32 bit view is [31:18] 0...0 [43:32]. */
9774             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9775                 | extract64(cpu->reset_cbar, 32, 12);
9776             ARMCPRegInfo cbar_reginfo[] = {
9777                 { .name = "CBAR",
9778                   .type = ARM_CP_CONST,
9779                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9780                   .access = PL1_R, .resetvalue = cbar32 },
9781                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9782                   .type = ARM_CP_CONST,
9783                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9784                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9785             };
9786             /* We don't implement a r/w 64 bit CBAR currently */
9787             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9788             define_arm_cp_regs(cpu, cbar_reginfo);
9789         } else {
9790             ARMCPRegInfo cbar = {
9791                 .name = "CBAR",
9792                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9793                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9794                 .fieldoffset = offsetof(CPUARMState,
9795                                         cp15.c15_config_base_address)
9796             };
9797             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9798                 cbar.access = PL1_R;
9799                 cbar.fieldoffset = 0;
9800                 cbar.type = ARM_CP_CONST;
9801             }
9802             define_one_arm_cp_reg(cpu, &cbar);
9803         }
9804     }
9805 
9806     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9807         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9808             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9809               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9810               .access = PL1_RW, .writefn = vbar_write,
9811               .accessfn = access_nv1,
9812               .fgt = FGT_VBAR_EL1,
9813               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
9814               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9815                                      offsetof(CPUARMState, cp15.vbar_ns) },
9816               .resetvalue = 0 },
9817         };
9818         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9819     }
9820 
9821     /* Generic registers whose values depend on the implementation */
9822     {
9823         ARMCPRegInfo sctlr = {
9824             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9825             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9826             .access = PL1_RW, .accessfn = access_tvm_trvm,
9827             .fgt = FGT_SCTLR_EL1,
9828             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
9829             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9830                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9831             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9832             .raw_writefn = raw_write,
9833         };
9834         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9835             /*
9836              * Normally we would always end the TB on an SCTLR write, but Linux
9837              * arch/arm/mach-pxa/sleep.S expects two instructions following
9838              * an MMU enable to execute from cache.  Imitate this behaviour.
9839              */
9840             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9841         }
9842         define_one_arm_cp_reg(cpu, &sctlr);
9843 
9844         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9845             arm_feature(env, ARM_FEATURE_V8)) {
9846             ARMCPRegInfo vsctlr = {
9847                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9848                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9849                 .access = PL2_RW, .resetvalue = 0x0,
9850                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9851             };
9852             define_one_arm_cp_reg(cpu, &vsctlr);
9853         }
9854     }
9855 
9856     if (cpu_isar_feature(aa64_lor, cpu)) {
9857         define_arm_cp_regs(cpu, lor_reginfo);
9858     }
9859     if (cpu_isar_feature(aa64_pan, cpu)) {
9860         define_one_arm_cp_reg(cpu, &pan_reginfo);
9861     }
9862 #ifndef CONFIG_USER_ONLY
9863     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9864         define_arm_cp_regs(cpu, ats1e1_reginfo);
9865     }
9866     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9867         define_arm_cp_regs(cpu, ats1cp_reginfo);
9868     }
9869 #endif
9870     if (cpu_isar_feature(aa64_uao, cpu)) {
9871         define_one_arm_cp_reg(cpu, &uao_reginfo);
9872     }
9873 
9874     if (cpu_isar_feature(aa64_dit, cpu)) {
9875         define_one_arm_cp_reg(cpu, &dit_reginfo);
9876     }
9877     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9878         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9879     }
9880     if (cpu_isar_feature(any_ras, cpu)) {
9881         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9882     }
9883 
9884     if (cpu_isar_feature(aa64_vh, cpu) ||
9885         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9886         define_one_arm_cp_reg(cpu, &contextidr_el2);
9887     }
9888     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9889         define_arm_cp_regs(cpu, vhe_reginfo);
9890     }
9891 
9892     if (cpu_isar_feature(aa64_sve, cpu)) {
9893         define_arm_cp_regs(cpu, zcr_reginfo);
9894     }
9895 
9896     if (cpu_isar_feature(aa64_hcx, cpu)) {
9897         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9898     }
9899 
9900 #ifdef TARGET_AARCH64
9901     if (cpu_isar_feature(aa64_sme, cpu)) {
9902         define_arm_cp_regs(cpu, sme_reginfo);
9903     }
9904     if (cpu_isar_feature(aa64_pauth, cpu)) {
9905         define_arm_cp_regs(cpu, pauth_reginfo);
9906     }
9907     if (cpu_isar_feature(aa64_rndr, cpu)) {
9908         define_arm_cp_regs(cpu, rndr_reginfo);
9909     }
9910     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9911         define_arm_cp_regs(cpu, tlbirange_reginfo);
9912     }
9913     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9914         define_arm_cp_regs(cpu, tlbios_reginfo);
9915     }
9916     /* Data Cache clean instructions up to PoP */
9917     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9918         define_one_arm_cp_reg(cpu, dcpop_reg);
9919 
9920         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9921             define_one_arm_cp_reg(cpu, dcpodp_reg);
9922         }
9923     }
9924 
9925     /*
9926      * If full MTE is enabled, add all of the system registers.
9927      * If only "instructions available at EL0" are enabled,
9928      * then define only a RAZ/WI version of PSTATE.TCO.
9929      */
9930     if (cpu_isar_feature(aa64_mte, cpu)) {
9931         ARMCPRegInfo gmid_reginfo = {
9932             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9933             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9934             .access = PL1_R, .accessfn = access_aa64_tid5,
9935             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9936         };
9937         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9938         define_arm_cp_regs(cpu, mte_reginfo);
9939         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9940     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9941         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9942         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9943     }
9944 
9945     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9946         define_arm_cp_regs(cpu, scxtnum_reginfo);
9947     }
9948 
9949     if (cpu_isar_feature(aa64_fgt, cpu)) {
9950         define_arm_cp_regs(cpu, fgt_reginfo);
9951     }
9952 
9953     if (cpu_isar_feature(aa64_rme, cpu)) {
9954         define_arm_cp_regs(cpu, rme_reginfo);
9955         if (cpu_isar_feature(aa64_mte, cpu)) {
9956             define_arm_cp_regs(cpu, rme_mte_reginfo);
9957         }
9958     }
9959 
9960     if (cpu_isar_feature(aa64_nv2, cpu)) {
9961         define_arm_cp_regs(cpu, nv2_reginfo);
9962     }
9963 
9964     if (cpu_isar_feature(aa64_nmi, cpu)) {
9965         define_arm_cp_regs(cpu, nmi_reginfo);
9966     }
9967 #endif
9968 
9969     if (cpu_isar_feature(any_predinv, cpu)) {
9970         define_arm_cp_regs(cpu, predinv_reginfo);
9971     }
9972 
9973     if (cpu_isar_feature(any_ccidx, cpu)) {
9974         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9975     }
9976 
9977 #ifndef CONFIG_USER_ONLY
9978     /*
9979      * Register redirections and aliases must be done last,
9980      * after the registers from the other extensions have been defined.
9981      */
9982     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9983         define_arm_vh_e2h_redirects_aliases(cpu);
9984     }
9985 #endif
9986 }
9987 
9988 /*
9989  * Private utility function for define_one_arm_cp_reg_with_opaque():
9990  * add a single reginfo struct to the hash table.
9991  */
9992 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9993                                    void *opaque, CPState state,
9994                                    CPSecureState secstate,
9995                                    int crm, int opc1, int opc2,
9996                                    const char *name)
9997 {
9998     CPUARMState *env = &cpu->env;
9999     uint32_t key;
10000     ARMCPRegInfo *r2;
10001     bool is64 = r->type & ARM_CP_64BIT;
10002     bool ns = secstate & ARM_CP_SECSTATE_NS;
10003     int cp = r->cp;
10004     size_t name_len;
10005     bool make_const;
10006 
10007     switch (state) {
10008     case ARM_CP_STATE_AA32:
10009         /* We assume it is a cp15 register if the .cp field is left unset. */
10010         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
10011             cp = 15;
10012         }
10013         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
10014         break;
10015     case ARM_CP_STATE_AA64:
10016         /*
10017          * To allow abbreviation of ARMCPRegInfo definitions, we treat
10018          * cp == 0 as equivalent to the value for "standard guest-visible
10019          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
10020          * in their AArch64 view (the .cp value may be non-zero for the
10021          * benefit of the AArch32 view).
10022          */
10023         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
10024             cp = CP_REG_ARM64_SYSREG_CP;
10025         }
10026         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
10027         break;
10028     default:
10029         g_assert_not_reached();
10030     }
10031 
10032     /* Overriding of an existing definition must be explicitly requested. */
10033     if (!(r->type & ARM_CP_OVERRIDE)) {
10034         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
10035         if (oldreg) {
10036             assert(oldreg->type & ARM_CP_OVERRIDE);
10037         }
10038     }
10039 
10040     /*
10041      * Eliminate registers that are not present because the EL is missing.
10042      * Doing this here makes it easier to put all registers for a given
10043      * feature into the same ARMCPRegInfo array and define them all at once.
10044      */
10045     make_const = false;
10046     if (arm_feature(env, ARM_FEATURE_EL3)) {
10047         /*
10048          * An EL2 register without EL2 but with EL3 is (usually) RES0.
10049          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
10050          */
10051         int min_el = ctz32(r->access) / 2;
10052         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
10053             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
10054                 return;
10055             }
10056             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
10057         }
10058     } else {
10059         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
10060                                  ? PL2_RW : PL1_RW);
10061         if ((r->access & max_el) == 0) {
10062             return;
10063         }
10064     }
10065 
10066     /* Combine cpreg and name into one allocation. */
10067     name_len = strlen(name) + 1;
10068     r2 = g_malloc(sizeof(*r2) + name_len);
10069     *r2 = *r;
10070     r2->name = memcpy(r2 + 1, name, name_len);
10071 
10072     /*
10073      * Update fields to match the instantiation, overwiting wildcards
10074      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
10075      */
10076     r2->cp = cp;
10077     r2->crm = crm;
10078     r2->opc1 = opc1;
10079     r2->opc2 = opc2;
10080     r2->state = state;
10081     r2->secure = secstate;
10082     if (opaque) {
10083         r2->opaque = opaque;
10084     }
10085 
10086     if (make_const) {
10087         /* This should not have been a very special register to begin. */
10088         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
10089         assert(old_special == 0 || old_special == ARM_CP_NOP);
10090         /*
10091          * Set the special function to CONST, retaining the other flags.
10092          * This is important for e.g. ARM_CP_SVE so that we still
10093          * take the SVE trap if CPTR_EL3.EZ == 0.
10094          */
10095         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
10096         /*
10097          * Usually, these registers become RES0, but there are a few
10098          * special cases like VPIDR_EL2 which have a constant non-zero
10099          * value with writes ignored.
10100          */
10101         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
10102             r2->resetvalue = 0;
10103         }
10104         /*
10105          * ARM_CP_CONST has precedence, so removing the callbacks and
10106          * offsets are not strictly necessary, but it is potentially
10107          * less confusing to debug later.
10108          */
10109         r2->readfn = NULL;
10110         r2->writefn = NULL;
10111         r2->raw_readfn = NULL;
10112         r2->raw_writefn = NULL;
10113         r2->resetfn = NULL;
10114         r2->fieldoffset = 0;
10115         r2->bank_fieldoffsets[0] = 0;
10116         r2->bank_fieldoffsets[1] = 0;
10117     } else {
10118         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
10119 
10120         if (isbanked) {
10121             /*
10122              * Register is banked (using both entries in array).
10123              * Overwriting fieldoffset as the array is only used to define
10124              * banked registers but later only fieldoffset is used.
10125              */
10126             r2->fieldoffset = r->bank_fieldoffsets[ns];
10127         }
10128         if (state == ARM_CP_STATE_AA32) {
10129             if (isbanked) {
10130                 /*
10131                  * If the register is banked then we don't need to migrate or
10132                  * reset the 32-bit instance in certain cases:
10133                  *
10134                  * 1) If the register has both 32-bit and 64-bit instances
10135                  *    then we can count on the 64-bit instance taking care
10136                  *    of the non-secure bank.
10137                  * 2) If ARMv8 is enabled then we can count on a 64-bit
10138                  *    version taking care of the secure bank.  This requires
10139                  *    that separate 32 and 64-bit definitions are provided.
10140                  */
10141                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
10142                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
10143                     r2->type |= ARM_CP_ALIAS;
10144                 }
10145             } else if ((secstate != r->secure) && !ns) {
10146                 /*
10147                  * The register is not banked so we only want to allow
10148                  * migration of the non-secure instance.
10149                  */
10150                 r2->type |= ARM_CP_ALIAS;
10151             }
10152 
10153             if (HOST_BIG_ENDIAN &&
10154                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
10155                 r2->fieldoffset += sizeof(uint32_t);
10156             }
10157         }
10158     }
10159 
10160     /*
10161      * By convention, for wildcarded registers only the first
10162      * entry is used for migration; the others are marked as
10163      * ALIAS so we don't try to transfer the register
10164      * multiple times. Special registers (ie NOP/WFI) are
10165      * never migratable and not even raw-accessible.
10166      */
10167     if (r2->type & ARM_CP_SPECIAL_MASK) {
10168         r2->type |= ARM_CP_NO_RAW;
10169     }
10170     if (((r->crm == CP_ANY) && crm != 0) ||
10171         ((r->opc1 == CP_ANY) && opc1 != 0) ||
10172         ((r->opc2 == CP_ANY) && opc2 != 0)) {
10173         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
10174     }
10175 
10176     /*
10177      * Check that raw accesses are either forbidden or handled. Note that
10178      * we can't assert this earlier because the setup of fieldoffset for
10179      * banked registers has to be done first.
10180      */
10181     if (!(r2->type & ARM_CP_NO_RAW)) {
10182         assert(!raw_accessors_invalid(r2));
10183     }
10184 
10185     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
10186 }
10187 
10188 
10189 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
10190                                        const ARMCPRegInfo *r, void *opaque)
10191 {
10192     /*
10193      * Define implementations of coprocessor registers.
10194      * We store these in a hashtable because typically
10195      * there are less than 150 registers in a space which
10196      * is 16*16*16*8*8 = 262144 in size.
10197      * Wildcarding is supported for the crm, opc1 and opc2 fields.
10198      * If a register is defined twice then the second definition is
10199      * used, so this can be used to define some generic registers and
10200      * then override them with implementation specific variations.
10201      * At least one of the original and the second definition should
10202      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
10203      * against accidental use.
10204      *
10205      * The state field defines whether the register is to be
10206      * visible in the AArch32 or AArch64 execution state. If the
10207      * state is set to ARM_CP_STATE_BOTH then we synthesise a
10208      * reginfo structure for the AArch32 view, which sees the lower
10209      * 32 bits of the 64 bit register.
10210      *
10211      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
10212      * be wildcarded. AArch64 registers are always considered to be 64
10213      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
10214      * the register, if any.
10215      */
10216     int crm, opc1, opc2;
10217     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
10218     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
10219     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
10220     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
10221     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
10222     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
10223     CPState state;
10224 
10225     /* 64 bit registers have only CRm and Opc1 fields */
10226     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
10227     /* op0 only exists in the AArch64 encodings */
10228     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
10229     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
10230     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
10231     /*
10232      * This API is only for Arm's system coprocessors (14 and 15) or
10233      * (M-profile or v7A-and-earlier only) for implementation defined
10234      * coprocessors in the range 0..7.  Our decode assumes this, since
10235      * 8..13 can be used for other insns including VFP and Neon. See
10236      * valid_cp() in translate.c.  Assert here that we haven't tried
10237      * to use an invalid coprocessor number.
10238      */
10239     switch (r->state) {
10240     case ARM_CP_STATE_BOTH:
10241         /* 0 has a special meaning, but otherwise the same rules as AA32. */
10242         if (r->cp == 0) {
10243             break;
10244         }
10245         /* fall through */
10246     case ARM_CP_STATE_AA32:
10247         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10248             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10249             assert(r->cp >= 14 && r->cp <= 15);
10250         } else {
10251             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10252         }
10253         break;
10254     case ARM_CP_STATE_AA64:
10255         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10256         break;
10257     default:
10258         g_assert_not_reached();
10259     }
10260     /*
10261      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10262      * encodes a minimum access level for the register. We roll this
10263      * runtime check into our general permission check code, so check
10264      * here that the reginfo's specified permissions are strict enough
10265      * to encompass the generic architectural permission check.
10266      */
10267     if (r->state != ARM_CP_STATE_AA32) {
10268         CPAccessRights mask;
10269         switch (r->opc1) {
10270         case 0:
10271             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10272             mask = PL0U_R | PL1_RW;
10273             break;
10274         case 1: case 2:
10275             /* min_EL EL1 */
10276             mask = PL1_RW;
10277             break;
10278         case 3:
10279             /* min_EL EL0 */
10280             mask = PL0_RW;
10281             break;
10282         case 4:
10283         case 5:
10284             /* min_EL EL2 */
10285             mask = PL2_RW;
10286             break;
10287         case 6:
10288             /* min_EL EL3 */
10289             mask = PL3_RW;
10290             break;
10291         case 7:
10292             /* min_EL EL1, secure mode only (we don't check the latter) */
10293             mask = PL1_RW;
10294             break;
10295         default:
10296             /* broken reginfo with out-of-range opc1 */
10297             g_assert_not_reached();
10298         }
10299         /* assert our permissions are not too lax (stricter is fine) */
10300         assert((r->access & ~mask) == 0);
10301     }
10302 
10303     /*
10304      * Check that the register definition has enough info to handle
10305      * reads and writes if they are permitted.
10306      */
10307     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10308         if (r->access & PL3_R) {
10309             assert((r->fieldoffset ||
10310                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10311                    r->readfn);
10312         }
10313         if (r->access & PL3_W) {
10314             assert((r->fieldoffset ||
10315                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10316                    r->writefn);
10317         }
10318     }
10319 
10320     for (crm = crmmin; crm <= crmmax; crm++) {
10321         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10322             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10323                 for (state = ARM_CP_STATE_AA32;
10324                      state <= ARM_CP_STATE_AA64; state++) {
10325                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10326                         continue;
10327                     }
10328                     if (state == ARM_CP_STATE_AA32) {
10329                         /*
10330                          * Under AArch32 CP registers can be common
10331                          * (same for secure and non-secure world) or banked.
10332                          */
10333                         char *name;
10334 
10335                         switch (r->secure) {
10336                         case ARM_CP_SECSTATE_S:
10337                         case ARM_CP_SECSTATE_NS:
10338                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10339                                                    r->secure, crm, opc1, opc2,
10340                                                    r->name);
10341                             break;
10342                         case ARM_CP_SECSTATE_BOTH:
10343                             name = g_strdup_printf("%s_S", r->name);
10344                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10345                                                    ARM_CP_SECSTATE_S,
10346                                                    crm, opc1, opc2, name);
10347                             g_free(name);
10348                             add_cpreg_to_hashtable(cpu, r, opaque, state,
10349                                                    ARM_CP_SECSTATE_NS,
10350                                                    crm, opc1, opc2, r->name);
10351                             break;
10352                         default:
10353                             g_assert_not_reached();
10354                         }
10355                     } else {
10356                         /*
10357                          * AArch64 registers get mapped to non-secure instance
10358                          * of AArch32
10359                          */
10360                         add_cpreg_to_hashtable(cpu, r, opaque, state,
10361                                                ARM_CP_SECSTATE_NS,
10362                                                crm, opc1, opc2, r->name);
10363                     }
10364                 }
10365             }
10366         }
10367     }
10368 }
10369 
10370 /* Define a whole list of registers */
10371 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10372                                         void *opaque, size_t len)
10373 {
10374     size_t i;
10375     for (i = 0; i < len; ++i) {
10376         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10377     }
10378 }
10379 
10380 /*
10381  * Modify ARMCPRegInfo for access from userspace.
10382  *
10383  * This is a data driven modification directed by
10384  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10385  * user-space cannot alter any values and dynamic values pertaining to
10386  * execution state are hidden from user space view anyway.
10387  */
10388 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10389                                  const ARMCPRegUserSpaceInfo *mods,
10390                                  size_t mods_len)
10391 {
10392     for (size_t mi = 0; mi < mods_len; ++mi) {
10393         const ARMCPRegUserSpaceInfo *m = mods + mi;
10394         GPatternSpec *pat = NULL;
10395 
10396         if (m->is_glob) {
10397             pat = g_pattern_spec_new(m->name);
10398         }
10399         for (size_t ri = 0; ri < regs_len; ++ri) {
10400             ARMCPRegInfo *r = regs + ri;
10401 
10402             if (pat && g_pattern_match_string(pat, r->name)) {
10403                 r->type = ARM_CP_CONST;
10404                 r->access = PL0U_R;
10405                 r->resetvalue = 0;
10406                 /* continue */
10407             } else if (strcmp(r->name, m->name) == 0) {
10408                 r->type = ARM_CP_CONST;
10409                 r->access = PL0U_R;
10410                 r->resetvalue &= m->exported_bits;
10411                 r->resetvalue |= m->fixed_bits;
10412                 break;
10413             }
10414         }
10415         if (pat) {
10416             g_pattern_spec_free(pat);
10417         }
10418     }
10419 }
10420 
10421 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10422 {
10423     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10424 }
10425 
10426 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10427                          uint64_t value)
10428 {
10429     /* Helper coprocessor write function for write-ignore registers */
10430 }
10431 
10432 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10433 {
10434     /* Helper coprocessor write function for read-as-zero registers */
10435     return 0;
10436 }
10437 
10438 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10439 {
10440     /* Helper coprocessor reset function for do-nothing-on-reset registers */
10441 }
10442 
10443 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10444 {
10445     /*
10446      * Return true if it is not valid for us to switch to
10447      * this CPU mode (ie all the UNPREDICTABLE cases in
10448      * the ARM ARM CPSRWriteByInstr pseudocode).
10449      */
10450 
10451     /* Changes to or from Hyp via MSR and CPS are illegal. */
10452     if (write_type == CPSRWriteByInstr &&
10453         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10454          mode == ARM_CPU_MODE_HYP)) {
10455         return 1;
10456     }
10457 
10458     switch (mode) {
10459     case ARM_CPU_MODE_USR:
10460         return 0;
10461     case ARM_CPU_MODE_SYS:
10462     case ARM_CPU_MODE_SVC:
10463     case ARM_CPU_MODE_ABT:
10464     case ARM_CPU_MODE_UND:
10465     case ARM_CPU_MODE_IRQ:
10466     case ARM_CPU_MODE_FIQ:
10467         /*
10468          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10469          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10470          */
10471         /*
10472          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10473          * and CPS are treated as illegal mode changes.
10474          */
10475         if (write_type == CPSRWriteByInstr &&
10476             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10477             (arm_hcr_el2_eff(env) & HCR_TGE)) {
10478             return 1;
10479         }
10480         return 0;
10481     case ARM_CPU_MODE_HYP:
10482         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10483     case ARM_CPU_MODE_MON:
10484         return arm_current_el(env) < 3;
10485     default:
10486         return 1;
10487     }
10488 }
10489 
10490 uint32_t cpsr_read(CPUARMState *env)
10491 {
10492     int ZF;
10493     ZF = (env->ZF == 0);
10494     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10495         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10496         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10497         | ((env->condexec_bits & 0xfc) << 8)
10498         | (env->GE << 16) | (env->daif & CPSR_AIF);
10499 }
10500 
10501 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10502                 CPSRWriteType write_type)
10503 {
10504     uint32_t changed_daif;
10505     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10506         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10507 
10508     if (mask & CPSR_NZCV) {
10509         env->ZF = (~val) & CPSR_Z;
10510         env->NF = val;
10511         env->CF = (val >> 29) & 1;
10512         env->VF = (val << 3) & 0x80000000;
10513     }
10514     if (mask & CPSR_Q) {
10515         env->QF = ((val & CPSR_Q) != 0);
10516     }
10517     if (mask & CPSR_T) {
10518         env->thumb = ((val & CPSR_T) != 0);
10519     }
10520     if (mask & CPSR_IT_0_1) {
10521         env->condexec_bits &= ~3;
10522         env->condexec_bits |= (val >> 25) & 3;
10523     }
10524     if (mask & CPSR_IT_2_7) {
10525         env->condexec_bits &= 3;
10526         env->condexec_bits |= (val >> 8) & 0xfc;
10527     }
10528     if (mask & CPSR_GE) {
10529         env->GE = (val >> 16) & 0xf;
10530     }
10531 
10532     /*
10533      * In a V7 implementation that includes the security extensions but does
10534      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10535      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10536      * bits respectively.
10537      *
10538      * In a V8 implementation, it is permitted for privileged software to
10539      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10540      */
10541     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10542         arm_feature(env, ARM_FEATURE_EL3) &&
10543         !arm_feature(env, ARM_FEATURE_EL2) &&
10544         !arm_is_secure(env)) {
10545 
10546         changed_daif = (env->daif ^ val) & mask;
10547 
10548         if (changed_daif & CPSR_A) {
10549             /*
10550              * Check to see if we are allowed to change the masking of async
10551              * abort exceptions from a non-secure state.
10552              */
10553             if (!(env->cp15.scr_el3 & SCR_AW)) {
10554                 qemu_log_mask(LOG_GUEST_ERROR,
10555                               "Ignoring attempt to switch CPSR_A flag from "
10556                               "non-secure world with SCR.AW bit clear\n");
10557                 mask &= ~CPSR_A;
10558             }
10559         }
10560 
10561         if (changed_daif & CPSR_F) {
10562             /*
10563              * Check to see if we are allowed to change the masking of FIQ
10564              * exceptions from a non-secure state.
10565              */
10566             if (!(env->cp15.scr_el3 & SCR_FW)) {
10567                 qemu_log_mask(LOG_GUEST_ERROR,
10568                               "Ignoring attempt to switch CPSR_F flag from "
10569                               "non-secure world with SCR.FW bit clear\n");
10570                 mask &= ~CPSR_F;
10571             }
10572 
10573             /*
10574              * Check whether non-maskable FIQ (NMFI) support is enabled.
10575              * If this bit is set software is not allowed to mask
10576              * FIQs, but is allowed to set CPSR_F to 0.
10577              */
10578             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10579                 (val & CPSR_F)) {
10580                 qemu_log_mask(LOG_GUEST_ERROR,
10581                               "Ignoring attempt to enable CPSR_F flag "
10582                               "(non-maskable FIQ [NMFI] support enabled)\n");
10583                 mask &= ~CPSR_F;
10584             }
10585         }
10586     }
10587 
10588     env->daif &= ~(CPSR_AIF & mask);
10589     env->daif |= val & CPSR_AIF & mask;
10590 
10591     if (write_type != CPSRWriteRaw &&
10592         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10593         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10594             /*
10595              * Note that we can only get here in USR mode if this is a
10596              * gdb stub write; for this case we follow the architectural
10597              * behaviour for guest writes in USR mode of ignoring an attempt
10598              * to switch mode. (Those are caught by translate.c for writes
10599              * triggered by guest instructions.)
10600              */
10601             mask &= ~CPSR_M;
10602         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10603             /*
10604              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10605              * v7, and has defined behaviour in v8:
10606              *  + leave CPSR.M untouched
10607              *  + allow changes to the other CPSR fields
10608              *  + set PSTATE.IL
10609              * For user changes via the GDB stub, we don't set PSTATE.IL,
10610              * as this would be unnecessarily harsh for a user error.
10611              */
10612             mask &= ~CPSR_M;
10613             if (write_type != CPSRWriteByGDBStub &&
10614                 arm_feature(env, ARM_FEATURE_V8)) {
10615                 mask |= CPSR_IL;
10616                 val |= CPSR_IL;
10617             }
10618             qemu_log_mask(LOG_GUEST_ERROR,
10619                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10620                           aarch32_mode_name(env->uncached_cpsr),
10621                           aarch32_mode_name(val));
10622         } else {
10623             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10624                           write_type == CPSRWriteExceptionReturn ?
10625                           "Exception return from AArch32" :
10626                           "AArch32 mode switch from",
10627                           aarch32_mode_name(env->uncached_cpsr),
10628                           aarch32_mode_name(val), env->regs[15]);
10629             switch_mode(env, val & CPSR_M);
10630         }
10631     }
10632     mask &= ~CACHED_CPSR_BITS;
10633     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10634     if (tcg_enabled() && rebuild_hflags) {
10635         arm_rebuild_hflags(env);
10636     }
10637 }
10638 
10639 #ifdef CONFIG_USER_ONLY
10640 
10641 static void switch_mode(CPUARMState *env, int mode)
10642 {
10643     ARMCPU *cpu = env_archcpu(env);
10644 
10645     if (mode != ARM_CPU_MODE_USR) {
10646         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10647     }
10648 }
10649 
10650 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10651                                  uint32_t cur_el, bool secure)
10652 {
10653     return 1;
10654 }
10655 
10656 void aarch64_sync_64_to_32(CPUARMState *env)
10657 {
10658     g_assert_not_reached();
10659 }
10660 
10661 #else
10662 
10663 static void switch_mode(CPUARMState *env, int mode)
10664 {
10665     int old_mode;
10666     int i;
10667 
10668     old_mode = env->uncached_cpsr & CPSR_M;
10669     if (mode == old_mode) {
10670         return;
10671     }
10672 
10673     if (old_mode == ARM_CPU_MODE_FIQ) {
10674         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10675         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10676     } else if (mode == ARM_CPU_MODE_FIQ) {
10677         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10678         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10679     }
10680 
10681     i = bank_number(old_mode);
10682     env->banked_r13[i] = env->regs[13];
10683     env->banked_spsr[i] = env->spsr;
10684 
10685     i = bank_number(mode);
10686     env->regs[13] = env->banked_r13[i];
10687     env->spsr = env->banked_spsr[i];
10688 
10689     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10690     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10691 }
10692 
10693 /*
10694  * Physical Interrupt Target EL Lookup Table
10695  *
10696  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10697  *
10698  * The below multi-dimensional table is used for looking up the target
10699  * exception level given numerous condition criteria.  Specifically, the
10700  * target EL is based on SCR and HCR routing controls as well as the
10701  * currently executing EL and secure state.
10702  *
10703  *    Dimensions:
10704  *    target_el_table[2][2][2][2][2][4]
10705  *                    |  |  |  |  |  +--- Current EL
10706  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10707  *                    |  |  |  +--------- HCR mask override
10708  *                    |  |  +------------ SCR exec state control
10709  *                    |  +--------------- SCR mask override
10710  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10711  *
10712  *    The table values are as such:
10713  *    0-3 = EL0-EL3
10714  *     -1 = Cannot occur
10715  *
10716  * The ARM ARM target EL table includes entries indicating that an "exception
10717  * is not taken".  The two cases where this is applicable are:
10718  *    1) An exception is taken from EL3 but the SCR does not have the exception
10719  *    routed to EL3.
10720  *    2) An exception is taken from EL2 but the HCR does not have the exception
10721  *    routed to EL2.
10722  * In these two cases, the below table contain a target of EL1.  This value is
10723  * returned as it is expected that the consumer of the table data will check
10724  * for "target EL >= current EL" to ensure the exception is not taken.
10725  *
10726  *            SCR     HCR
10727  *         64  EA     AMO                 From
10728  *        BIT IRQ     IMO      Non-secure         Secure
10729  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10730  */
10731 static const int8_t target_el_table[2][2][2][2][2][4] = {
10732     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10733        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10734       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10735        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10736      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10737        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10738       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10739        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10740     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10741        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10742       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10743        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10744      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10745        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10746       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10747        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10748 };
10749 
10750 /*
10751  * Determine the target EL for physical exceptions
10752  */
10753 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10754                                  uint32_t cur_el, bool secure)
10755 {
10756     CPUARMState *env = cpu_env(cs);
10757     bool rw;
10758     bool scr;
10759     bool hcr;
10760     int target_el;
10761     /* Is the highest EL AArch64? */
10762     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10763     uint64_t hcr_el2;
10764 
10765     if (arm_feature(env, ARM_FEATURE_EL3)) {
10766         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10767     } else {
10768         /*
10769          * Either EL2 is the highest EL (and so the EL2 register width
10770          * is given by is64); or there is no EL2 or EL3, in which case
10771          * the value of 'rw' does not affect the table lookup anyway.
10772          */
10773         rw = is64;
10774     }
10775 
10776     hcr_el2 = arm_hcr_el2_eff(env);
10777     switch (excp_idx) {
10778     case EXCP_IRQ:
10779     case EXCP_NMI:
10780         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10781         hcr = hcr_el2 & HCR_IMO;
10782         break;
10783     case EXCP_FIQ:
10784         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10785         hcr = hcr_el2 & HCR_FMO;
10786         break;
10787     default:
10788         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10789         hcr = hcr_el2 & HCR_AMO;
10790         break;
10791     };
10792 
10793     /*
10794      * For these purposes, TGE and AMO/IMO/FMO both force the
10795      * interrupt to EL2.  Fold TGE into the bit extracted above.
10796      */
10797     hcr |= (hcr_el2 & HCR_TGE) != 0;
10798 
10799     /* Perform a table-lookup for the target EL given the current state */
10800     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10801 
10802     assert(target_el > 0);
10803 
10804     return target_el;
10805 }
10806 
10807 void arm_log_exception(CPUState *cs)
10808 {
10809     int idx = cs->exception_index;
10810 
10811     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10812         const char *exc = NULL;
10813         static const char * const excnames[] = {
10814             [EXCP_UDEF] = "Undefined Instruction",
10815             [EXCP_SWI] = "SVC",
10816             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10817             [EXCP_DATA_ABORT] = "Data Abort",
10818             [EXCP_IRQ] = "IRQ",
10819             [EXCP_FIQ] = "FIQ",
10820             [EXCP_BKPT] = "Breakpoint",
10821             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10822             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10823             [EXCP_HVC] = "Hypervisor Call",
10824             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10825             [EXCP_SMC] = "Secure Monitor Call",
10826             [EXCP_VIRQ] = "Virtual IRQ",
10827             [EXCP_VFIQ] = "Virtual FIQ",
10828             [EXCP_SEMIHOST] = "Semihosting call",
10829             [EXCP_NOCP] = "v7M NOCP UsageFault",
10830             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10831             [EXCP_STKOF] = "v8M STKOF UsageFault",
10832             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10833             [EXCP_LSERR] = "v8M LSERR UsageFault",
10834             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10835             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10836             [EXCP_VSERR] = "Virtual SERR",
10837             [EXCP_GPC] = "Granule Protection Check",
10838             [EXCP_NMI] = "NMI",
10839             [EXCP_VINMI] = "Virtual IRQ NMI",
10840             [EXCP_VFNMI] = "Virtual FIQ NMI",
10841         };
10842 
10843         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10844             exc = excnames[idx];
10845         }
10846         if (!exc) {
10847             exc = "unknown";
10848         }
10849         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10850                       idx, exc, cs->cpu_index);
10851     }
10852 }
10853 
10854 /*
10855  * Function used to synchronize QEMU's AArch64 register set with AArch32
10856  * register set.  This is necessary when switching between AArch32 and AArch64
10857  * execution state.
10858  */
10859 void aarch64_sync_32_to_64(CPUARMState *env)
10860 {
10861     int i;
10862     uint32_t mode = env->uncached_cpsr & CPSR_M;
10863 
10864     /* We can blanket copy R[0:7] to X[0:7] */
10865     for (i = 0; i < 8; i++) {
10866         env->xregs[i] = env->regs[i];
10867     }
10868 
10869     /*
10870      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10871      * Otherwise, they come from the banked user regs.
10872      */
10873     if (mode == ARM_CPU_MODE_FIQ) {
10874         for (i = 8; i < 13; i++) {
10875             env->xregs[i] = env->usr_regs[i - 8];
10876         }
10877     } else {
10878         for (i = 8; i < 13; i++) {
10879             env->xregs[i] = env->regs[i];
10880         }
10881     }
10882 
10883     /*
10884      * Registers x13-x23 are the various mode SP and FP registers. Registers
10885      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10886      * from the mode banked register.
10887      */
10888     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10889         env->xregs[13] = env->regs[13];
10890         env->xregs[14] = env->regs[14];
10891     } else {
10892         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10893         /* HYP is an exception in that it is copied from r14 */
10894         if (mode == ARM_CPU_MODE_HYP) {
10895             env->xregs[14] = env->regs[14];
10896         } else {
10897             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10898         }
10899     }
10900 
10901     if (mode == ARM_CPU_MODE_HYP) {
10902         env->xregs[15] = env->regs[13];
10903     } else {
10904         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10905     }
10906 
10907     if (mode == ARM_CPU_MODE_IRQ) {
10908         env->xregs[16] = env->regs[14];
10909         env->xregs[17] = env->regs[13];
10910     } else {
10911         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10912         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10913     }
10914 
10915     if (mode == ARM_CPU_MODE_SVC) {
10916         env->xregs[18] = env->regs[14];
10917         env->xregs[19] = env->regs[13];
10918     } else {
10919         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10920         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10921     }
10922 
10923     if (mode == ARM_CPU_MODE_ABT) {
10924         env->xregs[20] = env->regs[14];
10925         env->xregs[21] = env->regs[13];
10926     } else {
10927         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10928         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10929     }
10930 
10931     if (mode == ARM_CPU_MODE_UND) {
10932         env->xregs[22] = env->regs[14];
10933         env->xregs[23] = env->regs[13];
10934     } else {
10935         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10936         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10937     }
10938 
10939     /*
10940      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10941      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10942      * FIQ bank for r8-r14.
10943      */
10944     if (mode == ARM_CPU_MODE_FIQ) {
10945         for (i = 24; i < 31; i++) {
10946             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10947         }
10948     } else {
10949         for (i = 24; i < 29; i++) {
10950             env->xregs[i] = env->fiq_regs[i - 24];
10951         }
10952         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10953         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10954     }
10955 
10956     env->pc = env->regs[15];
10957 }
10958 
10959 /*
10960  * Function used to synchronize QEMU's AArch32 register set with AArch64
10961  * register set.  This is necessary when switching between AArch32 and AArch64
10962  * execution state.
10963  */
10964 void aarch64_sync_64_to_32(CPUARMState *env)
10965 {
10966     int i;
10967     uint32_t mode = env->uncached_cpsr & CPSR_M;
10968 
10969     /* We can blanket copy X[0:7] to R[0:7] */
10970     for (i = 0; i < 8; i++) {
10971         env->regs[i] = env->xregs[i];
10972     }
10973 
10974     /*
10975      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10976      * Otherwise, we copy x8-x12 into the banked user regs.
10977      */
10978     if (mode == ARM_CPU_MODE_FIQ) {
10979         for (i = 8; i < 13; i++) {
10980             env->usr_regs[i - 8] = env->xregs[i];
10981         }
10982     } else {
10983         for (i = 8; i < 13; i++) {
10984             env->regs[i] = env->xregs[i];
10985         }
10986     }
10987 
10988     /*
10989      * Registers r13 & r14 depend on the current mode.
10990      * If we are in a given mode, we copy the corresponding x registers to r13
10991      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10992      * for the mode.
10993      */
10994     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10995         env->regs[13] = env->xregs[13];
10996         env->regs[14] = env->xregs[14];
10997     } else {
10998         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10999 
11000         /*
11001          * HYP is an exception in that it does not have its own banked r14 but
11002          * shares the USR r14
11003          */
11004         if (mode == ARM_CPU_MODE_HYP) {
11005             env->regs[14] = env->xregs[14];
11006         } else {
11007             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
11008         }
11009     }
11010 
11011     if (mode == ARM_CPU_MODE_HYP) {
11012         env->regs[13] = env->xregs[15];
11013     } else {
11014         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
11015     }
11016 
11017     if (mode == ARM_CPU_MODE_IRQ) {
11018         env->regs[14] = env->xregs[16];
11019         env->regs[13] = env->xregs[17];
11020     } else {
11021         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
11022         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
11023     }
11024 
11025     if (mode == ARM_CPU_MODE_SVC) {
11026         env->regs[14] = env->xregs[18];
11027         env->regs[13] = env->xregs[19];
11028     } else {
11029         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
11030         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
11031     }
11032 
11033     if (mode == ARM_CPU_MODE_ABT) {
11034         env->regs[14] = env->xregs[20];
11035         env->regs[13] = env->xregs[21];
11036     } else {
11037         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
11038         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
11039     }
11040 
11041     if (mode == ARM_CPU_MODE_UND) {
11042         env->regs[14] = env->xregs[22];
11043         env->regs[13] = env->xregs[23];
11044     } else {
11045         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
11046         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
11047     }
11048 
11049     /*
11050      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
11051      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
11052      * FIQ bank for r8-r14.
11053      */
11054     if (mode == ARM_CPU_MODE_FIQ) {
11055         for (i = 24; i < 31; i++) {
11056             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
11057         }
11058     } else {
11059         for (i = 24; i < 29; i++) {
11060             env->fiq_regs[i - 24] = env->xregs[i];
11061         }
11062         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
11063         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
11064     }
11065 
11066     env->regs[15] = env->pc;
11067 }
11068 
11069 static void take_aarch32_exception(CPUARMState *env, int new_mode,
11070                                    uint32_t mask, uint32_t offset,
11071                                    uint32_t newpc)
11072 {
11073     int new_el;
11074 
11075     /* Change the CPU state so as to actually take the exception. */
11076     switch_mode(env, new_mode);
11077 
11078     /*
11079      * For exceptions taken to AArch32 we must clear the SS bit in both
11080      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
11081      */
11082     env->pstate &= ~PSTATE_SS;
11083     env->spsr = cpsr_read(env);
11084     /* Clear IT bits.  */
11085     env->condexec_bits = 0;
11086     /* Switch to the new mode, and to the correct instruction set.  */
11087     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
11088 
11089     /* This must be after mode switching. */
11090     new_el = arm_current_el(env);
11091 
11092     /* Set new mode endianness */
11093     env->uncached_cpsr &= ~CPSR_E;
11094     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
11095         env->uncached_cpsr |= CPSR_E;
11096     }
11097     /* J and IL must always be cleared for exception entry */
11098     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
11099     env->daif |= mask;
11100 
11101     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
11102         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
11103             env->uncached_cpsr |= CPSR_SSBS;
11104         } else {
11105             env->uncached_cpsr &= ~CPSR_SSBS;
11106         }
11107     }
11108 
11109     if (new_mode == ARM_CPU_MODE_HYP) {
11110         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
11111         env->elr_el[2] = env->regs[15];
11112     } else {
11113         /* CPSR.PAN is normally preserved preserved unless...  */
11114         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
11115             switch (new_el) {
11116             case 3:
11117                 if (!arm_is_secure_below_el3(env)) {
11118                     /* ... the target is EL3, from non-secure state.  */
11119                     env->uncached_cpsr &= ~CPSR_PAN;
11120                     break;
11121                 }
11122                 /* ... the target is EL3, from secure state ... */
11123                 /* fall through */
11124             case 1:
11125                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
11126                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
11127                     env->uncached_cpsr |= CPSR_PAN;
11128                 }
11129                 break;
11130             }
11131         }
11132         /*
11133          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
11134          * and we should just guard the thumb mode on V4
11135          */
11136         if (arm_feature(env, ARM_FEATURE_V4T)) {
11137             env->thumb =
11138                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
11139         }
11140         env->regs[14] = env->regs[15] + offset;
11141     }
11142     env->regs[15] = newpc;
11143 
11144     if (tcg_enabled()) {
11145         arm_rebuild_hflags(env);
11146     }
11147 }
11148 
11149 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
11150 {
11151     /*
11152      * Handle exception entry to Hyp mode; this is sufficiently
11153      * different to entry to other AArch32 modes that we handle it
11154      * separately here.
11155      *
11156      * The vector table entry used is always the 0x14 Hyp mode entry point,
11157      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
11158      * The offset applied to the preferred return address is always zero
11159      * (see DDI0487C.a section G1.12.3).
11160      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
11161      */
11162     uint32_t addr, mask;
11163     ARMCPU *cpu = ARM_CPU(cs);
11164     CPUARMState *env = &cpu->env;
11165 
11166     switch (cs->exception_index) {
11167     case EXCP_UDEF:
11168         addr = 0x04;
11169         break;
11170     case EXCP_SWI:
11171         addr = 0x08;
11172         break;
11173     case EXCP_BKPT:
11174         /* Fall through to prefetch abort.  */
11175     case EXCP_PREFETCH_ABORT:
11176         env->cp15.ifar_s = env->exception.vaddress;
11177         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
11178                       (uint32_t)env->exception.vaddress);
11179         addr = 0x0c;
11180         break;
11181     case EXCP_DATA_ABORT:
11182         env->cp15.dfar_s = env->exception.vaddress;
11183         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
11184                       (uint32_t)env->exception.vaddress);
11185         addr = 0x10;
11186         break;
11187     case EXCP_IRQ:
11188         addr = 0x18;
11189         break;
11190     case EXCP_FIQ:
11191         addr = 0x1c;
11192         break;
11193     case EXCP_HVC:
11194         addr = 0x08;
11195         break;
11196     case EXCP_HYP_TRAP:
11197         addr = 0x14;
11198         break;
11199     default:
11200         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11201     }
11202 
11203     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
11204         if (!arm_feature(env, ARM_FEATURE_V8)) {
11205             /*
11206              * QEMU syndrome values are v8-style. v7 has the IL bit
11207              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
11208              * If this is a v7 CPU, squash the IL bit in those cases.
11209              */
11210             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
11211                 (cs->exception_index == EXCP_DATA_ABORT &&
11212                  !(env->exception.syndrome & ARM_EL_ISV)) ||
11213                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
11214                 env->exception.syndrome &= ~ARM_EL_IL;
11215             }
11216         }
11217         env->cp15.esr_el[2] = env->exception.syndrome;
11218     }
11219 
11220     if (arm_current_el(env) != 2 && addr < 0x14) {
11221         addr = 0x14;
11222     }
11223 
11224     mask = 0;
11225     if (!(env->cp15.scr_el3 & SCR_EA)) {
11226         mask |= CPSR_A;
11227     }
11228     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
11229         mask |= CPSR_I;
11230     }
11231     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
11232         mask |= CPSR_F;
11233     }
11234 
11235     addr += env->cp15.hvbar;
11236 
11237     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
11238 }
11239 
11240 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
11241 {
11242     ARMCPU *cpu = ARM_CPU(cs);
11243     CPUARMState *env = &cpu->env;
11244     uint32_t addr;
11245     uint32_t mask;
11246     int new_mode;
11247     uint32_t offset;
11248     uint32_t moe;
11249 
11250     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11251     switch (syn_get_ec(env->exception.syndrome)) {
11252     case EC_BREAKPOINT:
11253     case EC_BREAKPOINT_SAME_EL:
11254         moe = 1;
11255         break;
11256     case EC_WATCHPOINT:
11257     case EC_WATCHPOINT_SAME_EL:
11258         moe = 10;
11259         break;
11260     case EC_AA32_BKPT:
11261         moe = 3;
11262         break;
11263     case EC_VECTORCATCH:
11264         moe = 5;
11265         break;
11266     default:
11267         moe = 0;
11268         break;
11269     }
11270 
11271     if (moe) {
11272         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11273     }
11274 
11275     if (env->exception.target_el == 2) {
11276         /* Debug exceptions are reported differently on AArch32 */
11277         switch (syn_get_ec(env->exception.syndrome)) {
11278         case EC_BREAKPOINT:
11279         case EC_BREAKPOINT_SAME_EL:
11280         case EC_AA32_BKPT:
11281         case EC_VECTORCATCH:
11282             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11283                                                      0, 0, 0x22);
11284             break;
11285         case EC_WATCHPOINT:
11286             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11287                                                  EC_DATAABORT);
11288             break;
11289         case EC_WATCHPOINT_SAME_EL:
11290             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11291                                                  EC_DATAABORT_SAME_EL);
11292             break;
11293         }
11294         arm_cpu_do_interrupt_aarch32_hyp(cs);
11295         return;
11296     }
11297 
11298     switch (cs->exception_index) {
11299     case EXCP_UDEF:
11300         new_mode = ARM_CPU_MODE_UND;
11301         addr = 0x04;
11302         mask = CPSR_I;
11303         if (env->thumb) {
11304             offset = 2;
11305         } else {
11306             offset = 4;
11307         }
11308         break;
11309     case EXCP_SWI:
11310         new_mode = ARM_CPU_MODE_SVC;
11311         addr = 0x08;
11312         mask = CPSR_I;
11313         /* The PC already points to the next instruction.  */
11314         offset = 0;
11315         break;
11316     case EXCP_BKPT:
11317         /* Fall through to prefetch abort.  */
11318     case EXCP_PREFETCH_ABORT:
11319         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11320         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11321         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11322                       env->exception.fsr, (uint32_t)env->exception.vaddress);
11323         new_mode = ARM_CPU_MODE_ABT;
11324         addr = 0x0c;
11325         mask = CPSR_A | CPSR_I;
11326         offset = 4;
11327         break;
11328     case EXCP_DATA_ABORT:
11329         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11330         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11331         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11332                       env->exception.fsr,
11333                       (uint32_t)env->exception.vaddress);
11334         new_mode = ARM_CPU_MODE_ABT;
11335         addr = 0x10;
11336         mask = CPSR_A | CPSR_I;
11337         offset = 8;
11338         break;
11339     case EXCP_IRQ:
11340         new_mode = ARM_CPU_MODE_IRQ;
11341         addr = 0x18;
11342         /* Disable IRQ and imprecise data aborts.  */
11343         mask = CPSR_A | CPSR_I;
11344         offset = 4;
11345         if (env->cp15.scr_el3 & SCR_IRQ) {
11346             /* IRQ routed to monitor mode */
11347             new_mode = ARM_CPU_MODE_MON;
11348             mask |= CPSR_F;
11349         }
11350         break;
11351     case EXCP_FIQ:
11352         new_mode = ARM_CPU_MODE_FIQ;
11353         addr = 0x1c;
11354         /* Disable FIQ, IRQ and imprecise data aborts.  */
11355         mask = CPSR_A | CPSR_I | CPSR_F;
11356         if (env->cp15.scr_el3 & SCR_FIQ) {
11357             /* FIQ routed to monitor mode */
11358             new_mode = ARM_CPU_MODE_MON;
11359         }
11360         offset = 4;
11361         break;
11362     case EXCP_VIRQ:
11363         new_mode = ARM_CPU_MODE_IRQ;
11364         addr = 0x18;
11365         /* Disable IRQ and imprecise data aborts.  */
11366         mask = CPSR_A | CPSR_I;
11367         offset = 4;
11368         break;
11369     case EXCP_VFIQ:
11370         new_mode = ARM_CPU_MODE_FIQ;
11371         addr = 0x1c;
11372         /* Disable FIQ, IRQ and imprecise data aborts.  */
11373         mask = CPSR_A | CPSR_I | CPSR_F;
11374         offset = 4;
11375         break;
11376     case EXCP_VSERR:
11377         {
11378             /*
11379              * Note that this is reported as a data abort, but the DFAR
11380              * has an UNKNOWN value.  Construct the SError syndrome from
11381              * AET and ExT fields.
11382              */
11383             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11384 
11385             if (extended_addresses_enabled(env)) {
11386                 env->exception.fsr = arm_fi_to_lfsc(&fi);
11387             } else {
11388                 env->exception.fsr = arm_fi_to_sfsc(&fi);
11389             }
11390             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11391             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11392             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11393                           env->exception.fsr);
11394 
11395             new_mode = ARM_CPU_MODE_ABT;
11396             addr = 0x10;
11397             mask = CPSR_A | CPSR_I;
11398             offset = 8;
11399         }
11400         break;
11401     case EXCP_SMC:
11402         new_mode = ARM_CPU_MODE_MON;
11403         addr = 0x08;
11404         mask = CPSR_A | CPSR_I | CPSR_F;
11405         offset = 0;
11406         break;
11407     default:
11408         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11409         return; /* Never happens.  Keep compiler happy.  */
11410     }
11411 
11412     if (new_mode == ARM_CPU_MODE_MON) {
11413         addr += env->cp15.mvbar;
11414     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11415         /* High vectors. When enabled, base address cannot be remapped. */
11416         addr += 0xffff0000;
11417     } else {
11418         /*
11419          * ARM v7 architectures provide a vector base address register to remap
11420          * the interrupt vector table.
11421          * This register is only followed in non-monitor mode, and is banked.
11422          * Note: only bits 31:5 are valid.
11423          */
11424         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11425     }
11426 
11427     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11428         env->cp15.scr_el3 &= ~SCR_NS;
11429     }
11430 
11431     take_aarch32_exception(env, new_mode, mask, offset, addr);
11432 }
11433 
11434 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11435 {
11436     /*
11437      * Return the register number of the AArch64 view of the AArch32
11438      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11439      * be that of the AArch32 mode the exception came from.
11440      */
11441     int mode = env->uncached_cpsr & CPSR_M;
11442 
11443     switch (aarch32_reg) {
11444     case 0 ... 7:
11445         return aarch32_reg;
11446     case 8 ... 12:
11447         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11448     case 13:
11449         switch (mode) {
11450         case ARM_CPU_MODE_USR:
11451         case ARM_CPU_MODE_SYS:
11452             return 13;
11453         case ARM_CPU_MODE_HYP:
11454             return 15;
11455         case ARM_CPU_MODE_IRQ:
11456             return 17;
11457         case ARM_CPU_MODE_SVC:
11458             return 19;
11459         case ARM_CPU_MODE_ABT:
11460             return 21;
11461         case ARM_CPU_MODE_UND:
11462             return 23;
11463         case ARM_CPU_MODE_FIQ:
11464             return 29;
11465         default:
11466             g_assert_not_reached();
11467         }
11468     case 14:
11469         switch (mode) {
11470         case ARM_CPU_MODE_USR:
11471         case ARM_CPU_MODE_SYS:
11472         case ARM_CPU_MODE_HYP:
11473             return 14;
11474         case ARM_CPU_MODE_IRQ:
11475             return 16;
11476         case ARM_CPU_MODE_SVC:
11477             return 18;
11478         case ARM_CPU_MODE_ABT:
11479             return 20;
11480         case ARM_CPU_MODE_UND:
11481             return 22;
11482         case ARM_CPU_MODE_FIQ:
11483             return 30;
11484         default:
11485             g_assert_not_reached();
11486         }
11487     case 15:
11488         return 31;
11489     default:
11490         g_assert_not_reached();
11491     }
11492 }
11493 
11494 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11495 {
11496     uint32_t ret = cpsr_read(env);
11497 
11498     /* Move DIT to the correct location for SPSR_ELx */
11499     if (ret & CPSR_DIT) {
11500         ret &= ~CPSR_DIT;
11501         ret |= PSTATE_DIT;
11502     }
11503     /* Merge PSTATE.SS into SPSR_ELx */
11504     ret |= env->pstate & PSTATE_SS;
11505 
11506     return ret;
11507 }
11508 
11509 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11510 {
11511     /* Return true if this syndrome value is a synchronous external abort */
11512     switch (syn_get_ec(syndrome)) {
11513     case EC_INSNABORT:
11514     case EC_INSNABORT_SAME_EL:
11515     case EC_DATAABORT:
11516     case EC_DATAABORT_SAME_EL:
11517         /* Look at fault status code for all the synchronous ext abort cases */
11518         switch (syndrome & 0x3f) {
11519         case 0x10:
11520         case 0x13:
11521         case 0x14:
11522         case 0x15:
11523         case 0x16:
11524         case 0x17:
11525             return true;
11526         default:
11527             return false;
11528         }
11529     default:
11530         return false;
11531     }
11532 }
11533 
11534 /* Handle exception entry to a target EL which is using AArch64 */
11535 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11536 {
11537     ARMCPU *cpu = ARM_CPU(cs);
11538     CPUARMState *env = &cpu->env;
11539     unsigned int new_el = env->exception.target_el;
11540     target_ulong addr = env->cp15.vbar_el[new_el];
11541     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11542     unsigned int old_mode;
11543     unsigned int cur_el = arm_current_el(env);
11544     int rt;
11545 
11546     if (tcg_enabled()) {
11547         /*
11548          * Note that new_el can never be 0.  If cur_el is 0, then
11549          * el0_a64 is is_a64(), else el0_a64 is ignored.
11550          */
11551         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11552     }
11553 
11554     if (cur_el < new_el) {
11555         /*
11556          * Entry vector offset depends on whether the implemented EL
11557          * immediately lower than the target level is using AArch32 or AArch64
11558          */
11559         bool is_aa64;
11560         uint64_t hcr;
11561 
11562         switch (new_el) {
11563         case 3:
11564             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11565             break;
11566         case 2:
11567             hcr = arm_hcr_el2_eff(env);
11568             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11569                 is_aa64 = (hcr & HCR_RW) != 0;
11570                 break;
11571             }
11572             /* fall through */
11573         case 1:
11574             is_aa64 = is_a64(env);
11575             break;
11576         default:
11577             g_assert_not_reached();
11578         }
11579 
11580         if (is_aa64) {
11581             addr += 0x400;
11582         } else {
11583             addr += 0x600;
11584         }
11585     } else if (pstate_read(env) & PSTATE_SP) {
11586         addr += 0x200;
11587     }
11588 
11589     switch (cs->exception_index) {
11590     case EXCP_GPC:
11591         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11592                       env->cp15.mfar_el3);
11593         /* fall through */
11594     case EXCP_PREFETCH_ABORT:
11595     case EXCP_DATA_ABORT:
11596         /*
11597          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11598          * to be taken to the SError vector entrypoint.
11599          */
11600         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11601             syndrome_is_sync_extabt(env->exception.syndrome)) {
11602             addr += 0x180;
11603         }
11604         env->cp15.far_el[new_el] = env->exception.vaddress;
11605         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11606                       env->cp15.far_el[new_el]);
11607         /* fall through */
11608     case EXCP_BKPT:
11609     case EXCP_UDEF:
11610     case EXCP_SWI:
11611     case EXCP_HVC:
11612     case EXCP_HYP_TRAP:
11613     case EXCP_SMC:
11614         switch (syn_get_ec(env->exception.syndrome)) {
11615         case EC_ADVSIMDFPACCESSTRAP:
11616             /*
11617              * QEMU internal FP/SIMD syndromes from AArch32 include the
11618              * TA and coproc fields which are only exposed if the exception
11619              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11620              * AArch64 format syndrome.
11621              */
11622             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11623             break;
11624         case EC_CP14RTTRAP:
11625         case EC_CP15RTTRAP:
11626         case EC_CP14DTTRAP:
11627             /*
11628              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11629              * the raw register field from the insn; when taking this to
11630              * AArch64 we must convert it to the AArch64 view of the register
11631              * number. Notice that we read a 4-bit AArch32 register number and
11632              * write back a 5-bit AArch64 one.
11633              */
11634             rt = extract32(env->exception.syndrome, 5, 4);
11635             rt = aarch64_regnum(env, rt);
11636             env->exception.syndrome = deposit32(env->exception.syndrome,
11637                                                 5, 5, rt);
11638             break;
11639         case EC_CP15RRTTRAP:
11640         case EC_CP14RRTTRAP:
11641             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11642             rt = extract32(env->exception.syndrome, 5, 4);
11643             rt = aarch64_regnum(env, rt);
11644             env->exception.syndrome = deposit32(env->exception.syndrome,
11645                                                 5, 5, rt);
11646             rt = extract32(env->exception.syndrome, 10, 4);
11647             rt = aarch64_regnum(env, rt);
11648             env->exception.syndrome = deposit32(env->exception.syndrome,
11649                                                 10, 5, rt);
11650             break;
11651         }
11652         env->cp15.esr_el[new_el] = env->exception.syndrome;
11653         break;
11654     case EXCP_IRQ:
11655     case EXCP_VIRQ:
11656     case EXCP_NMI:
11657     case EXCP_VINMI:
11658         addr += 0x80;
11659         break;
11660     case EXCP_FIQ:
11661     case EXCP_VFIQ:
11662     case EXCP_VFNMI:
11663         addr += 0x100;
11664         break;
11665     case EXCP_VSERR:
11666         addr += 0x180;
11667         /* Construct the SError syndrome from IDS and ISS fields. */
11668         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11669         env->cp15.esr_el[new_el] = env->exception.syndrome;
11670         break;
11671     default:
11672         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11673     }
11674 
11675     if (is_a64(env)) {
11676         old_mode = pstate_read(env);
11677         aarch64_save_sp(env, arm_current_el(env));
11678         env->elr_el[new_el] = env->pc;
11679 
11680         if (cur_el == 1 && new_el == 1) {
11681             uint64_t hcr = arm_hcr_el2_eff(env);
11682             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11683                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11684                 /*
11685                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11686                  * by setting M[3:2] to 0b10.
11687                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11688                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11689                  */
11690                 old_mode = deposit32(old_mode, 2, 2, 2);
11691             }
11692         }
11693     } else {
11694         old_mode = cpsr_read_for_spsr_elx(env);
11695         env->elr_el[new_el] = env->regs[15];
11696 
11697         aarch64_sync_32_to_64(env);
11698 
11699         env->condexec_bits = 0;
11700     }
11701     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11702 
11703     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11704     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11705                   env->elr_el[new_el]);
11706 
11707     if (cpu_isar_feature(aa64_pan, cpu)) {
11708         /* The value of PSTATE.PAN is normally preserved, except when ... */
11709         new_mode |= old_mode & PSTATE_PAN;
11710         switch (new_el) {
11711         case 2:
11712             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11713             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11714                 != (HCR_E2H | HCR_TGE)) {
11715                 break;
11716             }
11717             /* fall through */
11718         case 1:
11719             /* ... the target is EL1 ... */
11720             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11721             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11722                 new_mode |= PSTATE_PAN;
11723             }
11724             break;
11725         }
11726     }
11727     if (cpu_isar_feature(aa64_mte, cpu)) {
11728         new_mode |= PSTATE_TCO;
11729     }
11730 
11731     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11732         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11733             new_mode |= PSTATE_SSBS;
11734         } else {
11735             new_mode &= ~PSTATE_SSBS;
11736         }
11737     }
11738 
11739     if (cpu_isar_feature(aa64_nmi, cpu)) {
11740         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
11741             new_mode |= PSTATE_ALLINT;
11742         } else {
11743             new_mode &= ~PSTATE_ALLINT;
11744         }
11745     }
11746 
11747     pstate_write(env, PSTATE_DAIF | new_mode);
11748     env->aarch64 = true;
11749     aarch64_restore_sp(env, new_el);
11750 
11751     if (tcg_enabled()) {
11752         helper_rebuild_hflags_a64(env, new_el);
11753     }
11754 
11755     env->pc = addr;
11756 
11757     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11758                   new_el, env->pc, pstate_read(env));
11759 }
11760 
11761 /*
11762  * Do semihosting call and set the appropriate return value. All the
11763  * permission and validity checks have been done at translate time.
11764  *
11765  * We only see semihosting exceptions in TCG only as they are not
11766  * trapped to the hypervisor in KVM.
11767  */
11768 #ifdef CONFIG_TCG
11769 static void tcg_handle_semihosting(CPUState *cs)
11770 {
11771     ARMCPU *cpu = ARM_CPU(cs);
11772     CPUARMState *env = &cpu->env;
11773 
11774     if (is_a64(env)) {
11775         qemu_log_mask(CPU_LOG_INT,
11776                       "...handling as semihosting call 0x%" PRIx64 "\n",
11777                       env->xregs[0]);
11778         do_common_semihosting(cs);
11779         env->pc += 4;
11780     } else {
11781         qemu_log_mask(CPU_LOG_INT,
11782                       "...handling as semihosting call 0x%x\n",
11783                       env->regs[0]);
11784         do_common_semihosting(cs);
11785         env->regs[15] += env->thumb ? 2 : 4;
11786     }
11787 }
11788 #endif
11789 
11790 /*
11791  * Handle a CPU exception for A and R profile CPUs.
11792  * Do any appropriate logging, handle PSCI calls, and then hand off
11793  * to the AArch64-entry or AArch32-entry function depending on the
11794  * target exception level's register width.
11795  *
11796  * Note: this is used for both TCG (as the do_interrupt tcg op),
11797  *       and KVM to re-inject guest debug exceptions, and to
11798  *       inject a Synchronous-External-Abort.
11799  */
11800 void arm_cpu_do_interrupt(CPUState *cs)
11801 {
11802     ARMCPU *cpu = ARM_CPU(cs);
11803     CPUARMState *env = &cpu->env;
11804     unsigned int new_el = env->exception.target_el;
11805 
11806     assert(!arm_feature(env, ARM_FEATURE_M));
11807 
11808     arm_log_exception(cs);
11809     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11810                   new_el);
11811     if (qemu_loglevel_mask(CPU_LOG_INT)
11812         && !excp_is_internal(cs->exception_index)) {
11813         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11814                       syn_get_ec(env->exception.syndrome),
11815                       env->exception.syndrome);
11816     }
11817 
11818     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11819         arm_handle_psci_call(cpu);
11820         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11821         return;
11822     }
11823 
11824     /*
11825      * Semihosting semantics depend on the register width of the code
11826      * that caused the exception, not the target exception level, so
11827      * must be handled here.
11828      */
11829 #ifdef CONFIG_TCG
11830     if (cs->exception_index == EXCP_SEMIHOST) {
11831         tcg_handle_semihosting(cs);
11832         return;
11833     }
11834 #endif
11835 
11836     /*
11837      * Hooks may change global state so BQL should be held, also the
11838      * BQL needs to be held for any modification of
11839      * cs->interrupt_request.
11840      */
11841     g_assert(bql_locked());
11842 
11843     arm_call_pre_el_change_hook(cpu);
11844 
11845     assert(!excp_is_internal(cs->exception_index));
11846     if (arm_el_is_aa64(env, new_el)) {
11847         arm_cpu_do_interrupt_aarch64(cs);
11848     } else {
11849         arm_cpu_do_interrupt_aarch32(cs);
11850     }
11851 
11852     arm_call_el_change_hook(cpu);
11853 
11854     if (!kvm_enabled()) {
11855         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11856     }
11857 }
11858 #endif /* !CONFIG_USER_ONLY */
11859 
11860 uint64_t arm_sctlr(CPUARMState *env, int el)
11861 {
11862     if (arm_aa32_secure_pl1_0(env)) {
11863         /* In Secure PL1&0 SCTLR_S is always controlling */
11864         el = 3;
11865     } else if (el == 0) {
11866         /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11867         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11868         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11869     }
11870     return env->cp15.sctlr_el[el];
11871 }
11872 
11873 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11874 {
11875     if (regime_has_2_ranges(mmu_idx)) {
11876         return extract64(tcr, 37, 2);
11877     } else if (regime_is_stage2(mmu_idx)) {
11878         return 0; /* VTCR_EL2 */
11879     } else {
11880         /* Replicate the single TBI bit so we always have 2 bits.  */
11881         return extract32(tcr, 20, 1) * 3;
11882     }
11883 }
11884 
11885 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11886 {
11887     if (regime_has_2_ranges(mmu_idx)) {
11888         return extract64(tcr, 51, 2);
11889     } else if (regime_is_stage2(mmu_idx)) {
11890         return 0; /* VTCR_EL2 */
11891     } else {
11892         /* Replicate the single TBID bit so we always have 2 bits.  */
11893         return extract32(tcr, 29, 1) * 3;
11894     }
11895 }
11896 
11897 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11898 {
11899     if (regime_has_2_ranges(mmu_idx)) {
11900         return extract64(tcr, 57, 2);
11901     } else {
11902         /* Replicate the single TCMA bit so we always have 2 bits.  */
11903         return extract32(tcr, 30, 1) * 3;
11904     }
11905 }
11906 
11907 static ARMGranuleSize tg0_to_gran_size(int tg)
11908 {
11909     switch (tg) {
11910     case 0:
11911         return Gran4K;
11912     case 1:
11913         return Gran64K;
11914     case 2:
11915         return Gran16K;
11916     default:
11917         return GranInvalid;
11918     }
11919 }
11920 
11921 static ARMGranuleSize tg1_to_gran_size(int tg)
11922 {
11923     switch (tg) {
11924     case 1:
11925         return Gran16K;
11926     case 2:
11927         return Gran4K;
11928     case 3:
11929         return Gran64K;
11930     default:
11931         return GranInvalid;
11932     }
11933 }
11934 
11935 static inline bool have4k(ARMCPU *cpu, bool stage2)
11936 {
11937     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11938         : cpu_isar_feature(aa64_tgran4, cpu);
11939 }
11940 
11941 static inline bool have16k(ARMCPU *cpu, bool stage2)
11942 {
11943     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11944         : cpu_isar_feature(aa64_tgran16, cpu);
11945 }
11946 
11947 static inline bool have64k(ARMCPU *cpu, bool stage2)
11948 {
11949     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11950         : cpu_isar_feature(aa64_tgran64, cpu);
11951 }
11952 
11953 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11954                                          bool stage2)
11955 {
11956     switch (gran) {
11957     case Gran4K:
11958         if (have4k(cpu, stage2)) {
11959             return gran;
11960         }
11961         break;
11962     case Gran16K:
11963         if (have16k(cpu, stage2)) {
11964             return gran;
11965         }
11966         break;
11967     case Gran64K:
11968         if (have64k(cpu, stage2)) {
11969             return gran;
11970         }
11971         break;
11972     case GranInvalid:
11973         break;
11974     }
11975     /*
11976      * If the guest selects a granule size that isn't implemented,
11977      * the architecture requires that we behave as if it selected one
11978      * that is (with an IMPDEF choice of which one to pick). We choose
11979      * to implement the smallest supported granule size.
11980      */
11981     if (have4k(cpu, stage2)) {
11982         return Gran4K;
11983     }
11984     if (have16k(cpu, stage2)) {
11985         return Gran16K;
11986     }
11987     assert(have64k(cpu, stage2));
11988     return Gran64K;
11989 }
11990 
11991 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11992                                    ARMMMUIdx mmu_idx, bool data,
11993                                    bool el1_is_aa32)
11994 {
11995     uint64_t tcr = regime_tcr(env, mmu_idx);
11996     bool epd, hpd, tsz_oob, ds, ha, hd;
11997     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11998     ARMGranuleSize gran;
11999     ARMCPU *cpu = env_archcpu(env);
12000     bool stage2 = regime_is_stage2(mmu_idx);
12001 
12002     if (!regime_has_2_ranges(mmu_idx)) {
12003         select = 0;
12004         tsz = extract32(tcr, 0, 6);
12005         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12006         if (stage2) {
12007             /* VTCR_EL2 */
12008             hpd = false;
12009         } else {
12010             hpd = extract32(tcr, 24, 1);
12011         }
12012         epd = false;
12013         sh = extract32(tcr, 12, 2);
12014         ps = extract32(tcr, 16, 3);
12015         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
12016         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12017         ds = extract64(tcr, 32, 1);
12018     } else {
12019         bool e0pd;
12020 
12021         /*
12022          * Bit 55 is always between the two regions, and is canonical for
12023          * determining if address tagging is enabled.
12024          */
12025         select = extract64(va, 55, 1);
12026         if (!select) {
12027             tsz = extract32(tcr, 0, 6);
12028             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12029             epd = extract32(tcr, 7, 1);
12030             sh = extract32(tcr, 12, 2);
12031             hpd = extract64(tcr, 41, 1);
12032             e0pd = extract64(tcr, 55, 1);
12033         } else {
12034             tsz = extract32(tcr, 16, 6);
12035             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
12036             epd = extract32(tcr, 23, 1);
12037             sh = extract32(tcr, 28, 2);
12038             hpd = extract64(tcr, 42, 1);
12039             e0pd = extract64(tcr, 56, 1);
12040         }
12041         ps = extract64(tcr, 32, 3);
12042         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
12043         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12044         ds = extract64(tcr, 59, 1);
12045 
12046         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
12047             regime_is_user(env, mmu_idx)) {
12048             epd = true;
12049         }
12050     }
12051 
12052     gran = sanitize_gran_size(cpu, gran, stage2);
12053 
12054     if (cpu_isar_feature(aa64_st, cpu)) {
12055         max_tsz = 48 - (gran == Gran64K);
12056     } else {
12057         max_tsz = 39;
12058     }
12059 
12060     /*
12061      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
12062      * adjust the effective value of DS, as documented.
12063      */
12064     min_tsz = 16;
12065     if (gran == Gran64K) {
12066         if (cpu_isar_feature(aa64_lva, cpu)) {
12067             min_tsz = 12;
12068         }
12069         ds = false;
12070     } else if (ds) {
12071         if (regime_is_stage2(mmu_idx)) {
12072             if (gran == Gran16K) {
12073                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
12074             } else {
12075                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
12076             }
12077         } else {
12078             if (gran == Gran16K) {
12079                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
12080             } else {
12081                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
12082             }
12083         }
12084         if (ds) {
12085             min_tsz = 12;
12086         }
12087     }
12088 
12089     if (stage2 && el1_is_aa32) {
12090         /*
12091          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
12092          * are loosened: a configured IPA of 40 bits is permitted even if
12093          * the implemented PA is less than that (and so a 40 bit IPA would
12094          * fault for an AArch64 EL1). See R_DTLMN.
12095          */
12096         min_tsz = MIN(min_tsz, 24);
12097     }
12098 
12099     if (tsz > max_tsz) {
12100         tsz = max_tsz;
12101         tsz_oob = true;
12102     } else if (tsz < min_tsz) {
12103         tsz = min_tsz;
12104         tsz_oob = true;
12105     } else {
12106         tsz_oob = false;
12107     }
12108 
12109     /* Present TBI as a composite with TBID.  */
12110     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12111     if (!data) {
12112         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12113     }
12114     tbi = (tbi >> select) & 1;
12115 
12116     return (ARMVAParameters) {
12117         .tsz = tsz,
12118         .ps = ps,
12119         .sh = sh,
12120         .select = select,
12121         .tbi = tbi,
12122         .epd = epd,
12123         .hpd = hpd,
12124         .tsz_oob = tsz_oob,
12125         .ds = ds,
12126         .ha = ha,
12127         .hd = ha && hd,
12128         .gran = gran,
12129     };
12130 }
12131 
12132 /*
12133  * Note that signed overflow is undefined in C.  The following routines are
12134  * careful to use unsigned types where modulo arithmetic is required.
12135  * Failure to do so _will_ break on newer gcc.
12136  */
12137 
12138 /* Signed saturating arithmetic.  */
12139 
12140 /* Perform 16-bit signed saturating addition.  */
12141 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12142 {
12143     uint16_t res;
12144 
12145     res = a + b;
12146     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12147         if (a & 0x8000) {
12148             res = 0x8000;
12149         } else {
12150             res = 0x7fff;
12151         }
12152     }
12153     return res;
12154 }
12155 
12156 /* Perform 8-bit signed saturating addition.  */
12157 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12158 {
12159     uint8_t res;
12160 
12161     res = a + b;
12162     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12163         if (a & 0x80) {
12164             res = 0x80;
12165         } else {
12166             res = 0x7f;
12167         }
12168     }
12169     return res;
12170 }
12171 
12172 /* Perform 16-bit signed saturating subtraction.  */
12173 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12174 {
12175     uint16_t res;
12176 
12177     res = a - b;
12178     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12179         if (a & 0x8000) {
12180             res = 0x8000;
12181         } else {
12182             res = 0x7fff;
12183         }
12184     }
12185     return res;
12186 }
12187 
12188 /* Perform 8-bit signed saturating subtraction.  */
12189 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12190 {
12191     uint8_t res;
12192 
12193     res = a - b;
12194     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12195         if (a & 0x80) {
12196             res = 0x80;
12197         } else {
12198             res = 0x7f;
12199         }
12200     }
12201     return res;
12202 }
12203 
12204 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12205 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12206 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
12207 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
12208 #define PFX q
12209 
12210 #include "op_addsub.h"
12211 
12212 /* Unsigned saturating arithmetic.  */
12213 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12214 {
12215     uint16_t res;
12216     res = a + b;
12217     if (res < a) {
12218         res = 0xffff;
12219     }
12220     return res;
12221 }
12222 
12223 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12224 {
12225     if (a > b) {
12226         return a - b;
12227     } else {
12228         return 0;
12229     }
12230 }
12231 
12232 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12233 {
12234     uint8_t res;
12235     res = a + b;
12236     if (res < a) {
12237         res = 0xff;
12238     }
12239     return res;
12240 }
12241 
12242 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12243 {
12244     if (a > b) {
12245         return a - b;
12246     } else {
12247         return 0;
12248     }
12249 }
12250 
12251 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12252 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12253 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
12254 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
12255 #define PFX uq
12256 
12257 #include "op_addsub.h"
12258 
12259 /* Signed modulo arithmetic.  */
12260 #define SARITH16(a, b, n, op) do { \
12261     int32_t sum; \
12262     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12263     RESULT(sum, n, 16); \
12264     if (sum >= 0) \
12265         ge |= 3 << (n * 2); \
12266     } while (0)
12267 
12268 #define SARITH8(a, b, n, op) do { \
12269     int32_t sum; \
12270     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12271     RESULT(sum, n, 8); \
12272     if (sum >= 0) \
12273         ge |= 1 << n; \
12274     } while (0)
12275 
12276 
12277 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12278 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12279 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
12280 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
12281 #define PFX s
12282 #define ARITH_GE
12283 
12284 #include "op_addsub.h"
12285 
12286 /* Unsigned modulo arithmetic.  */
12287 #define ADD16(a, b, n) do { \
12288     uint32_t sum; \
12289     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12290     RESULT(sum, n, 16); \
12291     if ((sum >> 16) == 1) \
12292         ge |= 3 << (n * 2); \
12293     } while (0)
12294 
12295 #define ADD8(a, b, n) do { \
12296     uint32_t sum; \
12297     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12298     RESULT(sum, n, 8); \
12299     if ((sum >> 8) == 1) \
12300         ge |= 1 << n; \
12301     } while (0)
12302 
12303 #define SUB16(a, b, n) do { \
12304     uint32_t sum; \
12305     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12306     RESULT(sum, n, 16); \
12307     if ((sum >> 16) == 0) \
12308         ge |= 3 << (n * 2); \
12309     } while (0)
12310 
12311 #define SUB8(a, b, n) do { \
12312     uint32_t sum; \
12313     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12314     RESULT(sum, n, 8); \
12315     if ((sum >> 8) == 0) \
12316         ge |= 1 << n; \
12317     } while (0)
12318 
12319 #define PFX u
12320 #define ARITH_GE
12321 
12322 #include "op_addsub.h"
12323 
12324 /* Halved signed arithmetic.  */
12325 #define ADD16(a, b, n) \
12326   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12327 #define SUB16(a, b, n) \
12328   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12329 #define ADD8(a, b, n) \
12330   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12331 #define SUB8(a, b, n) \
12332   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12333 #define PFX sh
12334 
12335 #include "op_addsub.h"
12336 
12337 /* Halved unsigned arithmetic.  */
12338 #define ADD16(a, b, n) \
12339   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12340 #define SUB16(a, b, n) \
12341   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12342 #define ADD8(a, b, n) \
12343   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12344 #define SUB8(a, b, n) \
12345   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12346 #define PFX uh
12347 
12348 #include "op_addsub.h"
12349 
12350 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12351 {
12352     if (a > b) {
12353         return a - b;
12354     } else {
12355         return b - a;
12356     }
12357 }
12358 
12359 /* Unsigned sum of absolute byte differences.  */
12360 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12361 {
12362     uint32_t sum;
12363     sum = do_usad(a, b);
12364     sum += do_usad(a >> 8, b >> 8);
12365     sum += do_usad(a >> 16, b >> 16);
12366     sum += do_usad(a >> 24, b >> 24);
12367     return sum;
12368 }
12369 
12370 /* For ARMv6 SEL instruction.  */
12371 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12372 {
12373     uint32_t mask;
12374 
12375     mask = 0;
12376     if (flags & 1) {
12377         mask |= 0xff;
12378     }
12379     if (flags & 2) {
12380         mask |= 0xff00;
12381     }
12382     if (flags & 4) {
12383         mask |= 0xff0000;
12384     }
12385     if (flags & 8) {
12386         mask |= 0xff000000;
12387     }
12388     return (a & mask) | (b & ~mask);
12389 }
12390 
12391 /*
12392  * CRC helpers.
12393  * The upper bytes of val (above the number specified by 'bytes') must have
12394  * been zeroed out by the caller.
12395  */
12396 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12397 {
12398     uint8_t buf[4];
12399 
12400     stl_le_p(buf, val);
12401 
12402     /* zlib crc32 converts the accumulator and output to one's complement.  */
12403     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12404 }
12405 
12406 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12407 {
12408     uint8_t buf[4];
12409 
12410     stl_le_p(buf, val);
12411 
12412     /* Linux crc32c converts the output to one's complement.  */
12413     return crc32c(acc, buf, bytes) ^ 0xffffffff;
12414 }
12415 
12416 /*
12417  * Return the exception level to which FP-disabled exceptions should
12418  * be taken, or 0 if FP is enabled.
12419  */
12420 int fp_exception_el(CPUARMState *env, int cur_el)
12421 {
12422 #ifndef CONFIG_USER_ONLY
12423     uint64_t hcr_el2;
12424 
12425     /*
12426      * CPACR and the CPTR registers don't exist before v6, so FP is
12427      * always accessible
12428      */
12429     if (!arm_feature(env, ARM_FEATURE_V6)) {
12430         return 0;
12431     }
12432 
12433     if (arm_feature(env, ARM_FEATURE_M)) {
12434         /* CPACR can cause a NOCP UsageFault taken to current security state */
12435         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12436             return 1;
12437         }
12438 
12439         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12440             if (!extract32(env->v7m.nsacr, 10, 1)) {
12441                 /* FP insns cause a NOCP UsageFault taken to Secure */
12442                 return 3;
12443             }
12444         }
12445 
12446         return 0;
12447     }
12448 
12449     hcr_el2 = arm_hcr_el2_eff(env);
12450 
12451     /*
12452      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12453      * 0, 2 : trap EL0 and EL1/PL1 accesses
12454      * 1    : trap only EL0 accesses
12455      * 3    : trap no accesses
12456      * This register is ignored if E2H+TGE are both set.
12457      */
12458     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12459         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12460 
12461         switch (fpen) {
12462         case 1:
12463             if (cur_el != 0) {
12464                 break;
12465             }
12466             /* fall through */
12467         case 0:
12468         case 2:
12469             /* Trap from Secure PL0 or PL1 to Secure PL1. */
12470             if (!arm_el_is_aa64(env, 3)
12471                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12472                 return 3;
12473             }
12474             if (cur_el <= 1) {
12475                 return 1;
12476             }
12477             break;
12478         }
12479     }
12480 
12481     /*
12482      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12483      * to control non-secure access to the FPU. It doesn't have any
12484      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12485      */
12486     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12487          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12488         if (!extract32(env->cp15.nsacr, 10, 1)) {
12489             /* FP insns act as UNDEF */
12490             return cur_el == 2 ? 2 : 1;
12491         }
12492     }
12493 
12494     /*
12495      * CPTR_EL2 is present in v7VE or v8, and changes format
12496      * with HCR_EL2.E2H (regardless of TGE).
12497      */
12498     if (cur_el <= 2) {
12499         if (hcr_el2 & HCR_E2H) {
12500             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12501             case 1:
12502                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12503                     break;
12504                 }
12505                 /* fall through */
12506             case 0:
12507             case 2:
12508                 return 2;
12509             }
12510         } else if (arm_is_el2_enabled(env)) {
12511             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12512                 return 2;
12513             }
12514         }
12515     }
12516 
12517     /* CPTR_EL3 : present in v8 */
12518     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12519         /* Trap all FP ops to EL3 */
12520         return 3;
12521     }
12522 #endif
12523     return 0;
12524 }
12525 
12526 /*
12527  * Return the exception level we're running at if this is our mmu_idx.
12528  * s_pl1_0 should be true if this is the AArch32 Secure PL1&0 translation
12529  * regime.
12530  */
12531 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx, bool s_pl1_0)
12532 {
12533     if (mmu_idx & ARM_MMU_IDX_M) {
12534         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12535     }
12536 
12537     switch (mmu_idx) {
12538     case ARMMMUIdx_E10_0:
12539     case ARMMMUIdx_E20_0:
12540         return 0;
12541     case ARMMMUIdx_E10_1:
12542     case ARMMMUIdx_E10_1_PAN:
12543         return s_pl1_0 ? 3 : 1;
12544     case ARMMMUIdx_E2:
12545     case ARMMMUIdx_E20_2:
12546     case ARMMMUIdx_E20_2_PAN:
12547         return 2;
12548     case ARMMMUIdx_E3:
12549         return 3;
12550     default:
12551         g_assert_not_reached();
12552     }
12553 }
12554 
12555 #ifndef CONFIG_TCG
12556 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12557 {
12558     g_assert_not_reached();
12559 }
12560 #endif
12561 
12562 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12563 {
12564     ARMMMUIdx idx;
12565     uint64_t hcr;
12566 
12567     if (arm_feature(env, ARM_FEATURE_M)) {
12568         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12569     }
12570 
12571     /* See ARM pseudo-function ELIsInHost.  */
12572     switch (el) {
12573     case 0:
12574         hcr = arm_hcr_el2_eff(env);
12575         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12576             idx = ARMMMUIdx_E20_0;
12577         } else {
12578             idx = ARMMMUIdx_E10_0;
12579         }
12580         break;
12581     case 3:
12582         /*
12583          * AArch64 EL3 has its own translation regime; AArch32 EL3
12584          * uses the Secure PL1&0 translation regime.
12585          */
12586         if (arm_el_is_aa64(env, 3)) {
12587             return ARMMMUIdx_E3;
12588         }
12589         /* fall through */
12590     case 1:
12591         if (arm_pan_enabled(env)) {
12592             idx = ARMMMUIdx_E10_1_PAN;
12593         } else {
12594             idx = ARMMMUIdx_E10_1;
12595         }
12596         break;
12597     case 2:
12598         /* Note that TGE does not apply at EL2.  */
12599         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12600             if (arm_pan_enabled(env)) {
12601                 idx = ARMMMUIdx_E20_2_PAN;
12602             } else {
12603                 idx = ARMMMUIdx_E20_2;
12604             }
12605         } else {
12606             idx = ARMMMUIdx_E2;
12607         }
12608         break;
12609     default:
12610         g_assert_not_reached();
12611     }
12612 
12613     return idx;
12614 }
12615 
12616 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12617 {
12618     return arm_mmu_idx_el(env, arm_current_el(env));
12619 }
12620 
12621 static bool mve_no_pred(CPUARMState *env)
12622 {
12623     /*
12624      * Return true if there is definitely no predication of MVE
12625      * instructions by VPR or LTPSIZE. (Returning false even if there
12626      * isn't any predication is OK; generated code will just be
12627      * a little worse.)
12628      * If the CPU does not implement MVE then this TB flag is always 0.
12629      *
12630      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12631      * logic in gen_update_fp_context() needs to be updated to match.
12632      *
12633      * We do not include the effect of the ECI bits here -- they are
12634      * tracked in other TB flags. This simplifies the logic for
12635      * "when did we emit code that changes the MVE_NO_PRED TB flag
12636      * and thus need to end the TB?".
12637      */
12638     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12639         return false;
12640     }
12641     if (env->v7m.vpr) {
12642         return false;
12643     }
12644     if (env->v7m.ltpsize < 4) {
12645         return false;
12646     }
12647     return true;
12648 }
12649 
12650 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12651                           uint64_t *cs_base, uint32_t *pflags)
12652 {
12653     CPUARMTBFlags flags;
12654 
12655     assert_hflags_rebuild_correctly(env);
12656     flags = env->hflags;
12657 
12658     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12659         *pc = env->pc;
12660         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12661             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12662         }
12663     } else {
12664         *pc = env->regs[15];
12665 
12666         if (arm_feature(env, ARM_FEATURE_M)) {
12667             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12668                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12669                 != env->v7m.secure) {
12670                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12671             }
12672 
12673             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12674                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12675                  (env->v7m.secure &&
12676                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12677                 /*
12678                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12679                  * active FP context; we must create a new FP context before
12680                  * executing any FP insn.
12681                  */
12682                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12683             }
12684 
12685             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12686             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12687                 DP_TBFLAG_M32(flags, LSPACT, 1);
12688             }
12689 
12690             if (mve_no_pred(env)) {
12691                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12692             }
12693         } else {
12694             /*
12695              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12696              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12697              */
12698             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12699                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12700             } else {
12701                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12702                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12703             }
12704             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12705                 DP_TBFLAG_A32(flags, VFPEN, 1);
12706             }
12707         }
12708 
12709         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12710         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12711     }
12712 
12713     /*
12714      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12715      * states defined in the ARM ARM for software singlestep:
12716      *  SS_ACTIVE   PSTATE.SS   State
12717      *     0            x       Inactive (the TB flag for SS is always 0)
12718      *     1            0       Active-pending
12719      *     1            1       Active-not-pending
12720      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12721      */
12722     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12723         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12724     }
12725 
12726     *pflags = flags.flags;
12727     *cs_base = flags.flags2;
12728 }
12729 
12730 #ifdef TARGET_AARCH64
12731 /*
12732  * The manual says that when SVE is enabled and VQ is widened the
12733  * implementation is allowed to zero the previously inaccessible
12734  * portion of the registers.  The corollary to that is that when
12735  * SVE is enabled and VQ is narrowed we are also allowed to zero
12736  * the now inaccessible portion of the registers.
12737  *
12738  * The intent of this is that no predicate bit beyond VQ is ever set.
12739  * Which means that some operations on predicate registers themselves
12740  * may operate on full uint64_t or even unrolled across the maximum
12741  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12742  * may well be cheaper than conditionals to restrict the operation
12743  * to the relevant portion of a uint16_t[16].
12744  */
12745 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12746 {
12747     int i, j;
12748     uint64_t pmask;
12749 
12750     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12751     assert(vq <= env_archcpu(env)->sve_max_vq);
12752 
12753     /* Zap the high bits of the zregs.  */
12754     for (i = 0; i < 32; i++) {
12755         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12756     }
12757 
12758     /* Zap the high bits of the pregs and ffr.  */
12759     pmask = 0;
12760     if (vq & 3) {
12761         pmask = ~(-1ULL << (16 * (vq & 3)));
12762     }
12763     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12764         for (i = 0; i < 17; ++i) {
12765             env->vfp.pregs[i].p[j] &= pmask;
12766         }
12767         pmask = 0;
12768     }
12769 }
12770 
12771 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12772 {
12773     int exc_el;
12774 
12775     if (sm) {
12776         exc_el = sme_exception_el(env, el);
12777     } else {
12778         exc_el = sve_exception_el(env, el);
12779     }
12780     if (exc_el) {
12781         return 0; /* disabled */
12782     }
12783     return sve_vqm1_for_el_sm(env, el, sm);
12784 }
12785 
12786 /*
12787  * Notice a change in SVE vector size when changing EL.
12788  */
12789 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12790                            int new_el, bool el0_a64)
12791 {
12792     ARMCPU *cpu = env_archcpu(env);
12793     int old_len, new_len;
12794     bool old_a64, new_a64, sm;
12795 
12796     /* Nothing to do if no SVE.  */
12797     if (!cpu_isar_feature(aa64_sve, cpu)) {
12798         return;
12799     }
12800 
12801     /* Nothing to do if FP is disabled in either EL.  */
12802     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12803         return;
12804     }
12805 
12806     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12807     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12808 
12809     /*
12810      * Both AArch64.TakeException and AArch64.ExceptionReturn
12811      * invoke ResetSVEState when taking an exception from, or
12812      * returning to, AArch32 state when PSTATE.SM is enabled.
12813      */
12814     sm = FIELD_EX64(env->svcr, SVCR, SM);
12815     if (old_a64 != new_a64 && sm) {
12816         arm_reset_sve_state(env);
12817         return;
12818     }
12819 
12820     /*
12821      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12822      * at ELx, or not available because the EL is in AArch32 state, then
12823      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12824      * has an effective value of 0".
12825      *
12826      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12827      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12828      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12829      * we already have the correct register contents when encountering the
12830      * vq0->vq0 transition between EL0->EL1.
12831      */
12832     old_len = new_len = 0;
12833     if (old_a64) {
12834         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12835     }
12836     if (new_a64) {
12837         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12838     }
12839 
12840     /* When changing vector length, clear inaccessible state.  */
12841     if (new_len < old_len) {
12842         aarch64_sve_narrow_vq(env, new_len + 1);
12843     }
12844 }
12845 #endif
12846 
12847 #ifndef CONFIG_USER_ONLY
12848 ARMSecuritySpace arm_security_space(CPUARMState *env)
12849 {
12850     if (arm_feature(env, ARM_FEATURE_M)) {
12851         return arm_secure_to_space(env->v7m.secure);
12852     }
12853 
12854     /*
12855      * If EL3 is not supported then the secure state is implementation
12856      * defined, in which case QEMU defaults to non-secure.
12857      */
12858     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12859         return ARMSS_NonSecure;
12860     }
12861 
12862     /* Check for AArch64 EL3 or AArch32 Mon. */
12863     if (is_a64(env)) {
12864         if (extract32(env->pstate, 2, 2) == 3) {
12865             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12866                 return ARMSS_Root;
12867             } else {
12868                 return ARMSS_Secure;
12869             }
12870         }
12871     } else {
12872         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12873             return ARMSS_Secure;
12874         }
12875     }
12876 
12877     return arm_security_space_below_el3(env);
12878 }
12879 
12880 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12881 {
12882     assert(!arm_feature(env, ARM_FEATURE_M));
12883 
12884     /*
12885      * If EL3 is not supported then the secure state is implementation
12886      * defined, in which case QEMU defaults to non-secure.
12887      */
12888     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12889         return ARMSS_NonSecure;
12890     }
12891 
12892     /*
12893      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12894      * Ignoring NSE when !NS retains consistency without having to
12895      * modify other predicates.
12896      */
12897     if (!(env->cp15.scr_el3 & SCR_NS)) {
12898         return ARMSS_Secure;
12899     } else if (env->cp15.scr_el3 & SCR_NSE) {
12900         return ARMSS_Realm;
12901     } else {
12902         return ARMSS_NonSecure;
12903     }
12904 }
12905 #endif /* !CONFIG_USER_ONLY */
12906