xref: /openbmc/qemu/target/arm/helper.c (revision 7f2a01e7)
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 "exec/helper-proto.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/timer.h"
17 #include "qemu/bitops.h"
18 #include "qemu/crc32c.h"
19 #include "qemu/qemu-print.h"
20 #include "exec/exec-all.h"
21 #include <zlib.h> /* For crc32 */
22 #include "hw/irq.h"
23 #include "sysemu/cpu-timers.h"
24 #include "sysemu/kvm.h"
25 #include "qapi/qapi-commands-machine-target.h"
26 #include "qapi/error.h"
27 #include "qemu/guest-random.h"
28 #ifdef CONFIG_TCG
29 #include "semihosting/common-semi.h"
30 #endif
31 #include "cpregs.h"
32 
33 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
34 
35 static void switch_mode(CPUARMState *env, int mode);
36 
37 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
38 {
39     assert(ri->fieldoffset);
40     if (cpreg_field_is_64bit(ri)) {
41         return CPREG_FIELD64(env, ri);
42     } else {
43         return CPREG_FIELD32(env, ri);
44     }
45 }
46 
47 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
48 {
49     assert(ri->fieldoffset);
50     if (cpreg_field_is_64bit(ri)) {
51         CPREG_FIELD64(env, ri) = value;
52     } else {
53         CPREG_FIELD32(env, ri) = value;
54     }
55 }
56 
57 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
58 {
59     return (char *)env + ri->fieldoffset;
60 }
61 
62 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
63 {
64     /* Raw read of a coprocessor register (as needed for migration, etc). */
65     if (ri->type & ARM_CP_CONST) {
66         return ri->resetvalue;
67     } else if (ri->raw_readfn) {
68         return ri->raw_readfn(env, ri);
69     } else if (ri->readfn) {
70         return ri->readfn(env, ri);
71     } else {
72         return raw_read(env, ri);
73     }
74 }
75 
76 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
77                              uint64_t v)
78 {
79     /*
80      * Raw write of a coprocessor register (as needed for migration, etc).
81      * Note that constant registers are treated as write-ignored; the
82      * caller should check for success by whether a readback gives the
83      * value written.
84      */
85     if (ri->type & ARM_CP_CONST) {
86         return;
87     } else if (ri->raw_writefn) {
88         ri->raw_writefn(env, ri, v);
89     } else if (ri->writefn) {
90         ri->writefn(env, ri, v);
91     } else {
92         raw_write(env, ri, v);
93     }
94 }
95 
96 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
97 {
98    /*
99     * Return true if the regdef would cause an assertion if you called
100     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
101     * program bug for it not to have the NO_RAW flag).
102     * NB that returning false here doesn't necessarily mean that calling
103     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
104     * read/write access functions which are safe for raw use" from "has
105     * read/write access functions which have side effects but has forgotten
106     * to provide raw access functions".
107     * The tests here line up with the conditions in read/write_raw_cp_reg()
108     * and assertions in raw_read()/raw_write().
109     */
110     if ((ri->type & ARM_CP_CONST) ||
111         ri->fieldoffset ||
112         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
113         return false;
114     }
115     return true;
116 }
117 
118 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
119 {
120     /* Write the coprocessor state from cpu->env to the (index,value) list. */
121     int i;
122     bool ok = true;
123 
124     for (i = 0; i < cpu->cpreg_array_len; i++) {
125         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
126         const ARMCPRegInfo *ri;
127         uint64_t newval;
128 
129         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
130         if (!ri) {
131             ok = false;
132             continue;
133         }
134         if (ri->type & ARM_CP_NO_RAW) {
135             continue;
136         }
137 
138         newval = read_raw_cp_reg(&cpu->env, ri);
139         if (kvm_sync) {
140             /*
141              * Only sync if the previous list->cpustate sync succeeded.
142              * Rather than tracking the success/failure state for every
143              * item in the list, we just recheck "does the raw write we must
144              * have made in write_list_to_cpustate() read back OK" here.
145              */
146             uint64_t oldval = cpu->cpreg_values[i];
147 
148             if (oldval == newval) {
149                 continue;
150             }
151 
152             write_raw_cp_reg(&cpu->env, ri, oldval);
153             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
154                 continue;
155             }
156 
157             write_raw_cp_reg(&cpu->env, ri, newval);
158         }
159         cpu->cpreg_values[i] = newval;
160     }
161     return ok;
162 }
163 
164 bool write_list_to_cpustate(ARMCPU *cpu)
165 {
166     int i;
167     bool ok = true;
168 
169     for (i = 0; i < cpu->cpreg_array_len; i++) {
170         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171         uint64_t v = cpu->cpreg_values[i];
172         const ARMCPRegInfo *ri;
173 
174         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
175         if (!ri) {
176             ok = false;
177             continue;
178         }
179         if (ri->type & ARM_CP_NO_RAW) {
180             continue;
181         }
182         /*
183          * Write value and confirm it reads back as written
184          * (to catch read-only registers and partially read-only
185          * registers where the incoming migration value doesn't match)
186          */
187         write_raw_cp_reg(&cpu->env, ri, v);
188         if (read_raw_cp_reg(&cpu->env, ri) != v) {
189             ok = false;
190         }
191     }
192     return ok;
193 }
194 
195 static void add_cpreg_to_list(gpointer key, gpointer opaque)
196 {
197     ARMCPU *cpu = opaque;
198     uint32_t regidx = (uintptr_t)key;
199     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
200 
201     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
202         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
203         /* The value array need not be initialized at this point */
204         cpu->cpreg_array_len++;
205     }
206 }
207 
208 static void count_cpreg(gpointer key, gpointer opaque)
209 {
210     ARMCPU *cpu = opaque;
211     const ARMCPRegInfo *ri;
212 
213     ri = g_hash_table_lookup(cpu->cp_regs, key);
214 
215     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
216         cpu->cpreg_array_len++;
217     }
218 }
219 
220 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
221 {
222     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
223     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
224 
225     if (aidx > bidx) {
226         return 1;
227     }
228     if (aidx < bidx) {
229         return -1;
230     }
231     return 0;
232 }
233 
234 void init_cpreg_list(ARMCPU *cpu)
235 {
236     /*
237      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
238      * Note that we require cpreg_tuples[] to be sorted by key ID.
239      */
240     GList *keys;
241     int arraylen;
242 
243     keys = g_hash_table_get_keys(cpu->cp_regs);
244     keys = g_list_sort(keys, cpreg_key_compare);
245 
246     cpu->cpreg_array_len = 0;
247 
248     g_list_foreach(keys, count_cpreg, cpu);
249 
250     arraylen = cpu->cpreg_array_len;
251     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
252     cpu->cpreg_values = g_new(uint64_t, arraylen);
253     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
254     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
256     cpu->cpreg_array_len = 0;
257 
258     g_list_foreach(keys, add_cpreg_to_list, cpu);
259 
260     assert(cpu->cpreg_array_len == arraylen);
261 
262     g_list_free(keys);
263 }
264 
265 /*
266  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
267  */
268 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
269                                         const ARMCPRegInfo *ri,
270                                         bool isread)
271 {
272     if (!is_a64(env) && arm_current_el(env) == 3 &&
273         arm_is_secure_below_el3(env)) {
274         return CP_ACCESS_TRAP_UNCATEGORIZED;
275     }
276     return CP_ACCESS_OK;
277 }
278 
279 /*
280  * Some secure-only AArch32 registers trap to EL3 if used from
281  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
282  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
283  * We assume that the .access field is set to PL1_RW.
284  */
285 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
286                                             const ARMCPRegInfo *ri,
287                                             bool isread)
288 {
289     if (arm_current_el(env) == 3) {
290         return CP_ACCESS_OK;
291     }
292     if (arm_is_secure_below_el3(env)) {
293         if (env->cp15.scr_el3 & SCR_EEL2) {
294             return CP_ACCESS_TRAP_EL2;
295         }
296         return CP_ACCESS_TRAP_EL3;
297     }
298     /* This will be EL1 NS and EL2 NS, which just UNDEF */
299     return CP_ACCESS_TRAP_UNCATEGORIZED;
300 }
301 
302 /*
303  * Check for traps to performance monitor registers, which are controlled
304  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
305  */
306 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
307                                  bool isread)
308 {
309     int el = arm_current_el(env);
310     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
311 
312     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
313         return CP_ACCESS_TRAP_EL2;
314     }
315     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
316         return CP_ACCESS_TRAP_EL3;
317     }
318     return CP_ACCESS_OK;
319 }
320 
321 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
322 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
323                                       bool isread)
324 {
325     if (arm_current_el(env) == 1) {
326         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
327         if (arm_hcr_el2_eff(env) & trap) {
328             return CP_ACCESS_TRAP_EL2;
329         }
330     }
331     return CP_ACCESS_OK;
332 }
333 
334 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
335 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
336                                  bool isread)
337 {
338     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
339         return CP_ACCESS_TRAP_EL2;
340     }
341     return CP_ACCESS_OK;
342 }
343 
344 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
345 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
346                                   bool isread)
347 {
348     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
349         return CP_ACCESS_TRAP_EL2;
350     }
351     return CP_ACCESS_OK;
352 }
353 
354 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
355 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
356                                   bool isread)
357 {
358     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
359         return CP_ACCESS_TRAP_EL2;
360     }
361     return CP_ACCESS_OK;
362 }
363 
364 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
365 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
366                                     bool isread)
367 {
368     if (arm_current_el(env) == 1 &&
369         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
370         return CP_ACCESS_TRAP_EL2;
371     }
372     return CP_ACCESS_OK;
373 }
374 
375 #ifdef TARGET_AARCH64
376 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
377 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
378                                     bool isread)
379 {
380     if (arm_current_el(env) == 1 &&
381         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
382         return CP_ACCESS_TRAP_EL2;
383     }
384     return CP_ACCESS_OK;
385 }
386 #endif
387 
388 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
389 {
390     ARMCPU *cpu = env_archcpu(env);
391 
392     raw_write(env, ri, value);
393     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
394 }
395 
396 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
397 {
398     ARMCPU *cpu = env_archcpu(env);
399 
400     if (raw_read(env, ri) != value) {
401         /*
402          * Unlike real hardware the qemu TLB uses virtual addresses,
403          * not modified virtual addresses, so this causes a TLB flush.
404          */
405         tlb_flush(CPU(cpu));
406         raw_write(env, ri, value);
407     }
408 }
409 
410 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
411                              uint64_t value)
412 {
413     ARMCPU *cpu = env_archcpu(env);
414 
415     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
416         && !extended_addresses_enabled(env)) {
417         /*
418          * For VMSA (when not using the LPAE long descriptor page table
419          * format) this register includes the ASID, so do a TLB flush.
420          * For PMSA it is purely a process ID and no action is needed.
421          */
422         tlb_flush(CPU(cpu));
423     }
424     raw_write(env, ri, value);
425 }
426 
427 static int alle1_tlbmask(CPUARMState *env)
428 {
429     /*
430      * Note that the 'ALL' scope must invalidate both stage 1 and
431      * stage 2 translations, whereas most other scopes only invalidate
432      * stage 1 translations.
433      */
434     return (ARMMMUIdxBit_E10_1 |
435             ARMMMUIdxBit_E10_1_PAN |
436             ARMMMUIdxBit_E10_0 |
437             ARMMMUIdxBit_Stage2 |
438             ARMMMUIdxBit_Stage2_S);
439 }
440 
441 
442 /* IS variants of TLB operations must affect all cores */
443 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
444                              uint64_t value)
445 {
446     CPUState *cs = env_cpu(env);
447 
448     tlb_flush_all_cpus_synced(cs);
449 }
450 
451 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
452                              uint64_t value)
453 {
454     CPUState *cs = env_cpu(env);
455 
456     tlb_flush_all_cpus_synced(cs);
457 }
458 
459 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
460                              uint64_t value)
461 {
462     CPUState *cs = env_cpu(env);
463 
464     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
465 }
466 
467 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
468                              uint64_t value)
469 {
470     CPUState *cs = env_cpu(env);
471 
472     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
473 }
474 
475 /*
476  * Non-IS variants of TLB operations are upgraded to
477  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
478  * force broadcast of these operations.
479  */
480 static bool tlb_force_broadcast(CPUARMState *env)
481 {
482     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
483 }
484 
485 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                           uint64_t value)
487 {
488     /* Invalidate all (TLBIALL) */
489     CPUState *cs = env_cpu(env);
490 
491     if (tlb_force_broadcast(env)) {
492         tlb_flush_all_cpus_synced(cs);
493     } else {
494         tlb_flush(cs);
495     }
496 }
497 
498 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
499                           uint64_t value)
500 {
501     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
502     CPUState *cs = env_cpu(env);
503 
504     value &= TARGET_PAGE_MASK;
505     if (tlb_force_broadcast(env)) {
506         tlb_flush_page_all_cpus_synced(cs, value);
507     } else {
508         tlb_flush_page(cs, value);
509     }
510 }
511 
512 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
513                            uint64_t value)
514 {
515     /* Invalidate by ASID (TLBIASID) */
516     CPUState *cs = env_cpu(env);
517 
518     if (tlb_force_broadcast(env)) {
519         tlb_flush_all_cpus_synced(cs);
520     } else {
521         tlb_flush(cs);
522     }
523 }
524 
525 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
526                            uint64_t value)
527 {
528     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
529     CPUState *cs = env_cpu(env);
530 
531     value &= TARGET_PAGE_MASK;
532     if (tlb_force_broadcast(env)) {
533         tlb_flush_page_all_cpus_synced(cs, value);
534     } else {
535         tlb_flush_page(cs, value);
536     }
537 }
538 
539 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                                uint64_t value)
541 {
542     CPUState *cs = env_cpu(env);
543 
544     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
545 }
546 
547 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548                                   uint64_t value)
549 {
550     CPUState *cs = env_cpu(env);
551 
552     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
553 }
554 
555 
556 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
557                               uint64_t value)
558 {
559     CPUState *cs = env_cpu(env);
560 
561     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
562 }
563 
564 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565                                  uint64_t value)
566 {
567     CPUState *cs = env_cpu(env);
568 
569     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
570 }
571 
572 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
573                               uint64_t value)
574 {
575     CPUState *cs = env_cpu(env);
576     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
577 
578     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
579 }
580 
581 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582                                  uint64_t value)
583 {
584     CPUState *cs = env_cpu(env);
585     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
586 
587     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
588                                              ARMMMUIdxBit_E2);
589 }
590 
591 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
592                                 uint64_t value)
593 {
594     CPUState *cs = env_cpu(env);
595     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
596 
597     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
598 }
599 
600 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
601                                 uint64_t value)
602 {
603     CPUState *cs = env_cpu(env);
604     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
605 
606     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
607 }
608 
609 static const ARMCPRegInfo cp_reginfo[] = {
610     /*
611      * Define the secure and non-secure FCSE identifier CP registers
612      * separately because there is no secure bank in V8 (no _EL3).  This allows
613      * the secure register to be properly reset and migrated. There is also no
614      * v8 EL1 version of the register so the non-secure instance stands alone.
615      */
616     { .name = "FCSEIDR",
617       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
618       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
619       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
620       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
621     { .name = "FCSEIDR_S",
622       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
623       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
624       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
625       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
626     /*
627      * Define the secure and non-secure context identifier CP registers
628      * separately because there is no secure bank in V8 (no _EL3).  This allows
629      * the secure register to be properly reset and migrated.  In the
630      * non-secure case, the 32-bit register will have reset and migration
631      * disabled during registration as it is handled by the 64-bit instance.
632      */
633     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
634       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
635       .access = PL1_RW, .accessfn = access_tvm_trvm,
636       .secure = ARM_CP_SECSTATE_NS,
637       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
638       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
639     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
640       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
641       .access = PL1_RW, .accessfn = access_tvm_trvm,
642       .secure = ARM_CP_SECSTATE_S,
643       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
644       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
645 };
646 
647 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
648     /*
649      * NB: Some of these registers exist in v8 but with more precise
650      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
651      */
652     /* MMU Domain access control / MPU write buffer control */
653     { .name = "DACR",
654       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
655       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
656       .writefn = dacr_write, .raw_writefn = raw_write,
657       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
658                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
659     /*
660      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
661      * For v6 and v5, these mappings are overly broad.
662      */
663     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
664       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
665     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
666       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
668       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
670       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671     /* Cache maintenance ops; some of this space may be overridden later. */
672     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
673       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
674       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
675 };
676 
677 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
678     /*
679      * Not all pre-v6 cores implemented this WFI, so this is slightly
680      * over-broad.
681      */
682     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
683       .access = PL1_W, .type = ARM_CP_WFI },
684 };
685 
686 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
687     /*
688      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
689      * is UNPREDICTABLE; we choose to NOP as most implementations do).
690      */
691     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
692       .access = PL1_W, .type = ARM_CP_WFI },
693     /*
694      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
695      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
696      * OMAPCP will override this space.
697      */
698     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
699       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
700       .resetvalue = 0 },
701     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
702       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
703       .resetvalue = 0 },
704     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
705     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
706       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
707       .resetvalue = 0 },
708     /*
709      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
710      * implementing it as RAZ means the "debug architecture version" bits
711      * will read as a reserved value, which should cause Linux to not try
712      * to use the debug hardware.
713      */
714     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
715       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
716     /*
717      * MMU TLB control. Note that the wildcarding means we cover not just
718      * the unified TLB ops but also the dside/iside/inner-shareable variants.
719      */
720     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
721       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
722       .type = ARM_CP_NO_RAW },
723     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
724       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
725       .type = ARM_CP_NO_RAW },
726     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
727       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
728       .type = ARM_CP_NO_RAW },
729     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
730       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
731       .type = ARM_CP_NO_RAW },
732     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
733       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
734     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
735       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
736 };
737 
738 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
739                         uint64_t value)
740 {
741     uint32_t mask = 0;
742 
743     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
744     if (!arm_feature(env, ARM_FEATURE_V8)) {
745         /*
746          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
747          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
748          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
749          */
750         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
751             /* VFP coprocessor: cp10 & cp11 [23:20] */
752             mask |= R_CPACR_ASEDIS_MASK |
753                     R_CPACR_D32DIS_MASK |
754                     R_CPACR_CP11_MASK |
755                     R_CPACR_CP10_MASK;
756 
757             if (!arm_feature(env, ARM_FEATURE_NEON)) {
758                 /* ASEDIS [31] bit is RAO/WI */
759                 value |= R_CPACR_ASEDIS_MASK;
760             }
761 
762             /*
763              * VFPv3 and upwards with NEON implement 32 double precision
764              * registers (D0-D31).
765              */
766             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
767                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
768                 value |= R_CPACR_D32DIS_MASK;
769             }
770         }
771         value &= mask;
772     }
773 
774     /*
775      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
776      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
777      */
778     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
779         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
780         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
781         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
782     }
783 
784     env->cp15.cpacr_el1 = value;
785 }
786 
787 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
788 {
789     /*
790      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
791      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
792      */
793     uint64_t value = env->cp15.cpacr_el1;
794 
795     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
796         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
797         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
798     }
799     return value;
800 }
801 
802 
803 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
804 {
805     /*
806      * Call cpacr_write() so that we reset with the correct RAO bits set
807      * for our CPU features.
808      */
809     cpacr_write(env, ri, 0);
810 }
811 
812 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
813                                    bool isread)
814 {
815     if (arm_feature(env, ARM_FEATURE_V8)) {
816         /* Check if CPACR accesses are to be trapped to EL2 */
817         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
818             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
819             return CP_ACCESS_TRAP_EL2;
820         /* Check if CPACR accesses are to be trapped to EL3 */
821         } else if (arm_current_el(env) < 3 &&
822                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
823             return CP_ACCESS_TRAP_EL3;
824         }
825     }
826 
827     return CP_ACCESS_OK;
828 }
829 
830 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
831                                   bool isread)
832 {
833     /* Check if CPTR accesses are set to trap to EL3 */
834     if (arm_current_el(env) == 2 &&
835         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
836         return CP_ACCESS_TRAP_EL3;
837     }
838 
839     return CP_ACCESS_OK;
840 }
841 
842 static const ARMCPRegInfo v6_cp_reginfo[] = {
843     /* prefetch by MVA in v6, NOP in v7 */
844     { .name = "MVA_prefetch",
845       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
846       .access = PL1_W, .type = ARM_CP_NOP },
847     /*
848      * We need to break the TB after ISB to execute self-modifying code
849      * correctly and also to take any pending interrupts immediately.
850      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
851      */
852     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
853       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
854     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
855       .access = PL0_W, .type = ARM_CP_NOP },
856     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
857       .access = PL0_W, .type = ARM_CP_NOP },
858     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
859       .access = PL1_RW, .accessfn = access_tvm_trvm,
860       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
861                              offsetof(CPUARMState, cp15.ifar_ns) },
862       .resetvalue = 0, },
863     /*
864      * Watchpoint Fault Address Register : should actually only be present
865      * for 1136, 1176, 11MPCore.
866      */
867     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
868       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
869     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
870       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
871       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
872       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
873 };
874 
875 typedef struct pm_event {
876     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
877     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
878     bool (*supported)(CPUARMState *);
879     /*
880      * Retrieve the current count of the underlying event. The programmed
881      * counters hold a difference from the return value from this function
882      */
883     uint64_t (*get_count)(CPUARMState *);
884     /*
885      * Return how many nanoseconds it will take (at a minimum) for count events
886      * to occur. A negative value indicates the counter will never overflow, or
887      * that the counter has otherwise arranged for the overflow bit to be set
888      * and the PMU interrupt to be raised on overflow.
889      */
890     int64_t (*ns_per_count)(uint64_t);
891 } pm_event;
892 
893 static bool event_always_supported(CPUARMState *env)
894 {
895     return true;
896 }
897 
898 static uint64_t swinc_get_count(CPUARMState *env)
899 {
900     /*
901      * SW_INCR events are written directly to the pmevcntr's by writes to
902      * PMSWINC, so there is no underlying count maintained by the PMU itself
903      */
904     return 0;
905 }
906 
907 static int64_t swinc_ns_per(uint64_t ignored)
908 {
909     return -1;
910 }
911 
912 /*
913  * Return the underlying cycle count for the PMU cycle counters. If we're in
914  * usermode, simply return 0.
915  */
916 static uint64_t cycles_get_count(CPUARMState *env)
917 {
918 #ifndef CONFIG_USER_ONLY
919     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
920                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
921 #else
922     return cpu_get_host_ticks();
923 #endif
924 }
925 
926 #ifndef CONFIG_USER_ONLY
927 static int64_t cycles_ns_per(uint64_t cycles)
928 {
929     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
930 }
931 
932 static bool instructions_supported(CPUARMState *env)
933 {
934     return icount_enabled() == 1; /* Precise instruction counting */
935 }
936 
937 static uint64_t instructions_get_count(CPUARMState *env)
938 {
939     return (uint64_t)icount_get_raw();
940 }
941 
942 static int64_t instructions_ns_per(uint64_t icount)
943 {
944     return icount_to_ns((int64_t)icount);
945 }
946 #endif
947 
948 static bool pmuv3p1_events_supported(CPUARMState *env)
949 {
950     /* For events which are supported in any v8.1 PMU */
951     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
952 }
953 
954 static bool pmuv3p4_events_supported(CPUARMState *env)
955 {
956     /* For events which are supported in any v8.1 PMU */
957     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
958 }
959 
960 static uint64_t zero_event_get_count(CPUARMState *env)
961 {
962     /* For events which on QEMU never fire, so their count is always zero */
963     return 0;
964 }
965 
966 static int64_t zero_event_ns_per(uint64_t cycles)
967 {
968     /* An event which never fires can never overflow */
969     return -1;
970 }
971 
972 static const pm_event pm_events[] = {
973     { .number = 0x000, /* SW_INCR */
974       .supported = event_always_supported,
975       .get_count = swinc_get_count,
976       .ns_per_count = swinc_ns_per,
977     },
978 #ifndef CONFIG_USER_ONLY
979     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
980       .supported = instructions_supported,
981       .get_count = instructions_get_count,
982       .ns_per_count = instructions_ns_per,
983     },
984     { .number = 0x011, /* CPU_CYCLES, Cycle */
985       .supported = event_always_supported,
986       .get_count = cycles_get_count,
987       .ns_per_count = cycles_ns_per,
988     },
989 #endif
990     { .number = 0x023, /* STALL_FRONTEND */
991       .supported = pmuv3p1_events_supported,
992       .get_count = zero_event_get_count,
993       .ns_per_count = zero_event_ns_per,
994     },
995     { .number = 0x024, /* STALL_BACKEND */
996       .supported = pmuv3p1_events_supported,
997       .get_count = zero_event_get_count,
998       .ns_per_count = zero_event_ns_per,
999     },
1000     { .number = 0x03c, /* STALL */
1001       .supported = pmuv3p4_events_supported,
1002       .get_count = zero_event_get_count,
1003       .ns_per_count = zero_event_ns_per,
1004     },
1005 };
1006 
1007 /*
1008  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1009  * events (i.e. the statistical profiling extension), this implementation
1010  * should first be updated to something sparse instead of the current
1011  * supported_event_map[] array.
1012  */
1013 #define MAX_EVENT_ID 0x3c
1014 #define UNSUPPORTED_EVENT UINT16_MAX
1015 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1016 
1017 /*
1018  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1019  * of ARM event numbers to indices in our pm_events array.
1020  *
1021  * Note: Events in the 0x40XX range are not currently supported.
1022  */
1023 void pmu_init(ARMCPU *cpu)
1024 {
1025     unsigned int i;
1026 
1027     /*
1028      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1029      * events to them
1030      */
1031     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1032         supported_event_map[i] = UNSUPPORTED_EVENT;
1033     }
1034     cpu->pmceid0 = 0;
1035     cpu->pmceid1 = 0;
1036 
1037     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1038         const pm_event *cnt = &pm_events[i];
1039         assert(cnt->number <= MAX_EVENT_ID);
1040         /* We do not currently support events in the 0x40xx range */
1041         assert(cnt->number <= 0x3f);
1042 
1043         if (cnt->supported(&cpu->env)) {
1044             supported_event_map[cnt->number] = i;
1045             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1046             if (cnt->number & 0x20) {
1047                 cpu->pmceid1 |= event_mask;
1048             } else {
1049                 cpu->pmceid0 |= event_mask;
1050             }
1051         }
1052     }
1053 }
1054 
1055 /*
1056  * Check at runtime whether a PMU event is supported for the current machine
1057  */
1058 static bool event_supported(uint16_t number)
1059 {
1060     if (number > MAX_EVENT_ID) {
1061         return false;
1062     }
1063     return supported_event_map[number] != UNSUPPORTED_EVENT;
1064 }
1065 
1066 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1067                                    bool isread)
1068 {
1069     /*
1070      * Performance monitor registers user accessibility is controlled
1071      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1072      * trapping to EL2 or EL3 for other accesses.
1073      */
1074     int el = arm_current_el(env);
1075     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1076 
1077     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1078         return CP_ACCESS_TRAP;
1079     }
1080     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1081         return CP_ACCESS_TRAP_EL2;
1082     }
1083     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1084         return CP_ACCESS_TRAP_EL3;
1085     }
1086 
1087     return CP_ACCESS_OK;
1088 }
1089 
1090 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1091                                            const ARMCPRegInfo *ri,
1092                                            bool isread)
1093 {
1094     /* ER: event counter read trap control */
1095     if (arm_feature(env, ARM_FEATURE_V8)
1096         && arm_current_el(env) == 0
1097         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1098         && isread) {
1099         return CP_ACCESS_OK;
1100     }
1101 
1102     return pmreg_access(env, ri, isread);
1103 }
1104 
1105 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1106                                          const ARMCPRegInfo *ri,
1107                                          bool isread)
1108 {
1109     /* SW: software increment write trap control */
1110     if (arm_feature(env, ARM_FEATURE_V8)
1111         && arm_current_el(env) == 0
1112         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1113         && !isread) {
1114         return CP_ACCESS_OK;
1115     }
1116 
1117     return pmreg_access(env, ri, isread);
1118 }
1119 
1120 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1121                                         const ARMCPRegInfo *ri,
1122                                         bool isread)
1123 {
1124     /* ER: event counter read trap control */
1125     if (arm_feature(env, ARM_FEATURE_V8)
1126         && arm_current_el(env) == 0
1127         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1128         return CP_ACCESS_OK;
1129     }
1130 
1131     return pmreg_access(env, ri, isread);
1132 }
1133 
1134 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1135                                          const ARMCPRegInfo *ri,
1136                                          bool isread)
1137 {
1138     /* CR: cycle counter read trap control */
1139     if (arm_feature(env, ARM_FEATURE_V8)
1140         && arm_current_el(env) == 0
1141         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1142         && isread) {
1143         return CP_ACCESS_OK;
1144     }
1145 
1146     return pmreg_access(env, ri, isread);
1147 }
1148 
1149 /*
1150  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1151  * We use these to decide whether we need to wrap a write to MDCR_EL2
1152  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1153  */
1154 #define MDCR_EL2_PMU_ENABLE_BITS \
1155     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1156 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1157 
1158 /*
1159  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1160  * the current EL, security state, and register configuration.
1161  */
1162 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1163 {
1164     uint64_t filter;
1165     bool e, p, u, nsk, nsu, nsh, m;
1166     bool enabled, prohibited = false, filtered;
1167     bool secure = arm_is_secure(env);
1168     int el = arm_current_el(env);
1169     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1170     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1171 
1172     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1173         return false;
1174     }
1175 
1176     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1177             (counter < hpmn || counter == 31)) {
1178         e = env->cp15.c9_pmcr & PMCRE;
1179     } else {
1180         e = mdcr_el2 & MDCR_HPME;
1181     }
1182     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1183 
1184     /* Is event counting prohibited? */
1185     if (el == 2 && (counter < hpmn || counter == 31)) {
1186         prohibited = mdcr_el2 & MDCR_HPMD;
1187     }
1188     if (secure) {
1189         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1190     }
1191 
1192     if (counter == 31) {
1193         /*
1194          * The cycle counter defaults to running. PMCR.DP says "disable
1195          * the cycle counter when event counting is prohibited".
1196          * Some MDCR bits disable the cycle counter specifically.
1197          */
1198         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1199         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1200             if (secure) {
1201                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1202             }
1203             if (el == 2) {
1204                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1205             }
1206         }
1207     }
1208 
1209     if (counter == 31) {
1210         filter = env->cp15.pmccfiltr_el0;
1211     } else {
1212         filter = env->cp15.c14_pmevtyper[counter];
1213     }
1214 
1215     p   = filter & PMXEVTYPER_P;
1216     u   = filter & PMXEVTYPER_U;
1217     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1218     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1219     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1220     m   = arm_el_is_aa64(env, 1) &&
1221               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1222 
1223     if (el == 0) {
1224         filtered = secure ? u : u != nsu;
1225     } else if (el == 1) {
1226         filtered = secure ? p : p != nsk;
1227     } else if (el == 2) {
1228         filtered = !nsh;
1229     } else { /* EL3 */
1230         filtered = m != p;
1231     }
1232 
1233     if (counter != 31) {
1234         /*
1235          * If not checking PMCCNTR, ensure the counter is setup to an event we
1236          * support
1237          */
1238         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1239         if (!event_supported(event)) {
1240             return false;
1241         }
1242     }
1243 
1244     return enabled && !prohibited && !filtered;
1245 }
1246 
1247 static void pmu_update_irq(CPUARMState *env)
1248 {
1249     ARMCPU *cpu = env_archcpu(env);
1250     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1251             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1252 }
1253 
1254 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1255 {
1256     /*
1257      * Return true if the clock divider is enabled and the cycle counter
1258      * is supposed to tick only once every 64 clock cycles. This is
1259      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1260      * (64-bit) cycle counter PMCR.D has no effect.
1261      */
1262     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1263 }
1264 
1265 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1266 {
1267     /* Return true if the specified event counter is configured to be 64 bit */
1268 
1269     /* This isn't intended to be used with the cycle counter */
1270     assert(counter < 31);
1271 
1272     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1273         return false;
1274     }
1275 
1276     if (arm_feature(env, ARM_FEATURE_EL2)) {
1277         /*
1278          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1279          * current security state, so we don't use arm_mdcr_el2_eff() here.
1280          */
1281         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1282         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1283 
1284         if (hpmn != 0 && counter >= hpmn) {
1285             return hlp;
1286         }
1287     }
1288     return env->cp15.c9_pmcr & PMCRLP;
1289 }
1290 
1291 /*
1292  * Ensure c15_ccnt is the guest-visible count so that operations such as
1293  * enabling/disabling the counter or filtering, modifying the count itself,
1294  * etc. can be done logically. This is essentially a no-op if the counter is
1295  * not enabled at the time of the call.
1296  */
1297 static void pmccntr_op_start(CPUARMState *env)
1298 {
1299     uint64_t cycles = cycles_get_count(env);
1300 
1301     if (pmu_counter_enabled(env, 31)) {
1302         uint64_t eff_cycles = cycles;
1303         if (pmccntr_clockdiv_enabled(env)) {
1304             eff_cycles /= 64;
1305         }
1306 
1307         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1308 
1309         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1310                                  1ull << 63 : 1ull << 31;
1311         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1312             env->cp15.c9_pmovsr |= (1ULL << 31);
1313             pmu_update_irq(env);
1314         }
1315 
1316         env->cp15.c15_ccnt = new_pmccntr;
1317     }
1318     env->cp15.c15_ccnt_delta = cycles;
1319 }
1320 
1321 /*
1322  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1323  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1324  * pmccntr_op_start.
1325  */
1326 static void pmccntr_op_finish(CPUARMState *env)
1327 {
1328     if (pmu_counter_enabled(env, 31)) {
1329 #ifndef CONFIG_USER_ONLY
1330         /* Calculate when the counter will next overflow */
1331         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1332         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1333             remaining_cycles = (uint32_t)remaining_cycles;
1334         }
1335         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1336 
1337         if (overflow_in > 0) {
1338             int64_t overflow_at;
1339 
1340             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1341                                  overflow_in, &overflow_at)) {
1342                 ARMCPU *cpu = env_archcpu(env);
1343                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1344             }
1345         }
1346 #endif
1347 
1348         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1349         if (pmccntr_clockdiv_enabled(env)) {
1350             prev_cycles /= 64;
1351         }
1352         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1353     }
1354 }
1355 
1356 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1357 {
1358 
1359     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1360     uint64_t count = 0;
1361     if (event_supported(event)) {
1362         uint16_t event_idx = supported_event_map[event];
1363         count = pm_events[event_idx].get_count(env);
1364     }
1365 
1366     if (pmu_counter_enabled(env, counter)) {
1367         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1368         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1369             1ULL << 63 : 1ULL << 31;
1370 
1371         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1372             env->cp15.c9_pmovsr |= (1 << counter);
1373             pmu_update_irq(env);
1374         }
1375         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1376     }
1377     env->cp15.c14_pmevcntr_delta[counter] = count;
1378 }
1379 
1380 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1381 {
1382     if (pmu_counter_enabled(env, counter)) {
1383 #ifndef CONFIG_USER_ONLY
1384         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1385         uint16_t event_idx = supported_event_map[event];
1386         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1387         int64_t overflow_in;
1388 
1389         if (!pmevcntr_is_64_bit(env, counter)) {
1390             delta = (uint32_t)delta;
1391         }
1392         overflow_in = pm_events[event_idx].ns_per_count(delta);
1393 
1394         if (overflow_in > 0) {
1395             int64_t overflow_at;
1396 
1397             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1398                                  overflow_in, &overflow_at)) {
1399                 ARMCPU *cpu = env_archcpu(env);
1400                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1401             }
1402         }
1403 #endif
1404 
1405         env->cp15.c14_pmevcntr_delta[counter] -=
1406             env->cp15.c14_pmevcntr[counter];
1407     }
1408 }
1409 
1410 void pmu_op_start(CPUARMState *env)
1411 {
1412     unsigned int i;
1413     pmccntr_op_start(env);
1414     for (i = 0; i < pmu_num_counters(env); i++) {
1415         pmevcntr_op_start(env, i);
1416     }
1417 }
1418 
1419 void pmu_op_finish(CPUARMState *env)
1420 {
1421     unsigned int i;
1422     pmccntr_op_finish(env);
1423     for (i = 0; i < pmu_num_counters(env); i++) {
1424         pmevcntr_op_finish(env, i);
1425     }
1426 }
1427 
1428 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1429 {
1430     pmu_op_start(&cpu->env);
1431 }
1432 
1433 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1434 {
1435     pmu_op_finish(&cpu->env);
1436 }
1437 
1438 void arm_pmu_timer_cb(void *opaque)
1439 {
1440     ARMCPU *cpu = opaque;
1441 
1442     /*
1443      * Update all the counter values based on the current underlying counts,
1444      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1445      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1446      * counter may expire.
1447      */
1448     pmu_op_start(&cpu->env);
1449     pmu_op_finish(&cpu->env);
1450 }
1451 
1452 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                        uint64_t value)
1454 {
1455     pmu_op_start(env);
1456 
1457     if (value & PMCRC) {
1458         /* The counter has been reset */
1459         env->cp15.c15_ccnt = 0;
1460     }
1461 
1462     if (value & PMCRP) {
1463         unsigned int i;
1464         for (i = 0; i < pmu_num_counters(env); i++) {
1465             env->cp15.c14_pmevcntr[i] = 0;
1466         }
1467     }
1468 
1469     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1470     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1471 
1472     pmu_op_finish(env);
1473 }
1474 
1475 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476                           uint64_t value)
1477 {
1478     unsigned int i;
1479     uint64_t overflow_mask, new_pmswinc;
1480 
1481     for (i = 0; i < pmu_num_counters(env); i++) {
1482         /* Increment a counter's count iff: */
1483         if ((value & (1 << i)) && /* counter's bit is set */
1484                 /* counter is enabled and not filtered */
1485                 pmu_counter_enabled(env, i) &&
1486                 /* counter is SW_INCR */
1487                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1488             pmevcntr_op_start(env, i);
1489 
1490             /*
1491              * Detect if this write causes an overflow since we can't predict
1492              * PMSWINC overflows like we can for other events
1493              */
1494             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1495 
1496             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1497                 1ULL << 63 : 1ULL << 31;
1498 
1499             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1500                 env->cp15.c9_pmovsr |= (1 << i);
1501                 pmu_update_irq(env);
1502             }
1503 
1504             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1505 
1506             pmevcntr_op_finish(env, i);
1507         }
1508     }
1509 }
1510 
1511 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1512 {
1513     uint64_t ret;
1514     pmccntr_op_start(env);
1515     ret = env->cp15.c15_ccnt;
1516     pmccntr_op_finish(env);
1517     return ret;
1518 }
1519 
1520 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521                          uint64_t value)
1522 {
1523     /*
1524      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1525      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1526      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1527      * accessed.
1528      */
1529     env->cp15.c9_pmselr = value & 0x1f;
1530 }
1531 
1532 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1533                         uint64_t value)
1534 {
1535     pmccntr_op_start(env);
1536     env->cp15.c15_ccnt = value;
1537     pmccntr_op_finish(env);
1538 }
1539 
1540 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1541                             uint64_t value)
1542 {
1543     uint64_t cur_val = pmccntr_read(env, NULL);
1544 
1545     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1546 }
1547 
1548 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549                             uint64_t value)
1550 {
1551     pmccntr_op_start(env);
1552     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1553     pmccntr_op_finish(env);
1554 }
1555 
1556 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1557                             uint64_t value)
1558 {
1559     pmccntr_op_start(env);
1560     /* M is not accessible from AArch32 */
1561     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1562         (value & PMCCFILTR);
1563     pmccntr_op_finish(env);
1564 }
1565 
1566 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1567 {
1568     /* M is not visible in AArch32 */
1569     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1570 }
1571 
1572 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1573                             uint64_t value)
1574 {
1575     pmu_op_start(env);
1576     value &= pmu_counter_mask(env);
1577     env->cp15.c9_pmcnten |= value;
1578     pmu_op_finish(env);
1579 }
1580 
1581 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1582                              uint64_t value)
1583 {
1584     pmu_op_start(env);
1585     value &= pmu_counter_mask(env);
1586     env->cp15.c9_pmcnten &= ~value;
1587     pmu_op_finish(env);
1588 }
1589 
1590 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1591                          uint64_t value)
1592 {
1593     value &= pmu_counter_mask(env);
1594     env->cp15.c9_pmovsr &= ~value;
1595     pmu_update_irq(env);
1596 }
1597 
1598 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1599                          uint64_t value)
1600 {
1601     value &= pmu_counter_mask(env);
1602     env->cp15.c9_pmovsr |= value;
1603     pmu_update_irq(env);
1604 }
1605 
1606 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1607                              uint64_t value, const uint8_t counter)
1608 {
1609     if (counter == 31) {
1610         pmccfiltr_write(env, ri, value);
1611     } else if (counter < pmu_num_counters(env)) {
1612         pmevcntr_op_start(env, counter);
1613 
1614         /*
1615          * If this counter's event type is changing, store the current
1616          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1617          * pmevcntr_op_finish has the correct baseline when it converts back to
1618          * a delta.
1619          */
1620         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1621             PMXEVTYPER_EVTCOUNT;
1622         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1623         if (old_event != new_event) {
1624             uint64_t count = 0;
1625             if (event_supported(new_event)) {
1626                 uint16_t event_idx = supported_event_map[new_event];
1627                 count = pm_events[event_idx].get_count(env);
1628             }
1629             env->cp15.c14_pmevcntr_delta[counter] = count;
1630         }
1631 
1632         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1633         pmevcntr_op_finish(env, counter);
1634     }
1635     /*
1636      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1637      * PMSELR value is equal to or greater than the number of implemented
1638      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1639      */
1640 }
1641 
1642 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1643                                const uint8_t counter)
1644 {
1645     if (counter == 31) {
1646         return env->cp15.pmccfiltr_el0;
1647     } else if (counter < pmu_num_counters(env)) {
1648         return env->cp15.c14_pmevtyper[counter];
1649     } else {
1650       /*
1651        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1652        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1653        */
1654         return 0;
1655     }
1656 }
1657 
1658 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1659                               uint64_t value)
1660 {
1661     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1662     pmevtyper_write(env, ri, value, counter);
1663 }
1664 
1665 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1666                                uint64_t value)
1667 {
1668     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1669     env->cp15.c14_pmevtyper[counter] = value;
1670 
1671     /*
1672      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1673      * pmu_op_finish calls when loading saved state for a migration. Because
1674      * we're potentially updating the type of event here, the value written to
1675      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1676      * different counter type. Therefore, we need to set this value to the
1677      * current count for the counter type we're writing so that pmu_op_finish
1678      * has the correct count for its calculation.
1679      */
1680     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1681     if (event_supported(event)) {
1682         uint16_t event_idx = supported_event_map[event];
1683         env->cp15.c14_pmevcntr_delta[counter] =
1684             pm_events[event_idx].get_count(env);
1685     }
1686 }
1687 
1688 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1689 {
1690     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691     return pmevtyper_read(env, ri, counter);
1692 }
1693 
1694 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1695                              uint64_t value)
1696 {
1697     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1698 }
1699 
1700 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1701 {
1702     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1703 }
1704 
1705 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1706                              uint64_t value, uint8_t counter)
1707 {
1708     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1709         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1710         value &= MAKE_64BIT_MASK(0, 32);
1711     }
1712     if (counter < pmu_num_counters(env)) {
1713         pmevcntr_op_start(env, counter);
1714         env->cp15.c14_pmevcntr[counter] = value;
1715         pmevcntr_op_finish(env, counter);
1716     }
1717     /*
1718      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1719      * are CONSTRAINED UNPREDICTABLE.
1720      */
1721 }
1722 
1723 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1724                               uint8_t counter)
1725 {
1726     if (counter < pmu_num_counters(env)) {
1727         uint64_t ret;
1728         pmevcntr_op_start(env, counter);
1729         ret = env->cp15.c14_pmevcntr[counter];
1730         pmevcntr_op_finish(env, counter);
1731         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1732             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1733             ret &= MAKE_64BIT_MASK(0, 32);
1734         }
1735         return ret;
1736     } else {
1737       /*
1738        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1739        * are CONSTRAINED UNPREDICTABLE.
1740        */
1741         return 0;
1742     }
1743 }
1744 
1745 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1746                              uint64_t value)
1747 {
1748     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749     pmevcntr_write(env, ri, value, counter);
1750 }
1751 
1752 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1753 {
1754     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1755     return pmevcntr_read(env, ri, counter);
1756 }
1757 
1758 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1759                              uint64_t value)
1760 {
1761     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1762     assert(counter < pmu_num_counters(env));
1763     env->cp15.c14_pmevcntr[counter] = value;
1764     pmevcntr_write(env, ri, value, counter);
1765 }
1766 
1767 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1768 {
1769     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1770     assert(counter < pmu_num_counters(env));
1771     return env->cp15.c14_pmevcntr[counter];
1772 }
1773 
1774 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1775                              uint64_t value)
1776 {
1777     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1778 }
1779 
1780 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1781 {
1782     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1783 }
1784 
1785 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1786                             uint64_t value)
1787 {
1788     if (arm_feature(env, ARM_FEATURE_V8)) {
1789         env->cp15.c9_pmuserenr = value & 0xf;
1790     } else {
1791         env->cp15.c9_pmuserenr = value & 1;
1792     }
1793 }
1794 
1795 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1796                              uint64_t value)
1797 {
1798     /* We have no event counters so only the C bit can be changed */
1799     value &= pmu_counter_mask(env);
1800     env->cp15.c9_pminten |= value;
1801     pmu_update_irq(env);
1802 }
1803 
1804 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1805                              uint64_t value)
1806 {
1807     value &= pmu_counter_mask(env);
1808     env->cp15.c9_pminten &= ~value;
1809     pmu_update_irq(env);
1810 }
1811 
1812 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1813                        uint64_t value)
1814 {
1815     /*
1816      * Note that even though the AArch64 view of this register has bits
1817      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1818      * architectural requirements for bits which are RES0 only in some
1819      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1820      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1821      */
1822     raw_write(env, ri, value & ~0x1FULL);
1823 }
1824 
1825 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1826 {
1827     /* Begin with base v8.0 state.  */
1828     uint64_t valid_mask = 0x3fff;
1829     ARMCPU *cpu = env_archcpu(env);
1830     uint64_t changed;
1831 
1832     /*
1833      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1834      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1835      * Instead, choose the format based on the mode of EL3.
1836      */
1837     if (arm_el_is_aa64(env, 3)) {
1838         value |= SCR_FW | SCR_AW;      /* RES1 */
1839         valid_mask &= ~SCR_NET;        /* RES0 */
1840 
1841         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1842             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1843             value |= SCR_RW;           /* RAO/WI */
1844         }
1845         if (cpu_isar_feature(aa64_ras, cpu)) {
1846             valid_mask |= SCR_TERR;
1847         }
1848         if (cpu_isar_feature(aa64_lor, cpu)) {
1849             valid_mask |= SCR_TLOR;
1850         }
1851         if (cpu_isar_feature(aa64_pauth, cpu)) {
1852             valid_mask |= SCR_API | SCR_APK;
1853         }
1854         if (cpu_isar_feature(aa64_sel2, cpu)) {
1855             valid_mask |= SCR_EEL2;
1856         }
1857         if (cpu_isar_feature(aa64_mte, cpu)) {
1858             valid_mask |= SCR_ATA;
1859         }
1860         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1861             valid_mask |= SCR_ENSCXT;
1862         }
1863         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1864             valid_mask |= SCR_EASE | SCR_NMEA;
1865         }
1866         if (cpu_isar_feature(aa64_sme, cpu)) {
1867             valid_mask |= SCR_ENTP2;
1868         }
1869         if (cpu_isar_feature(aa64_hcx, cpu)) {
1870             valid_mask |= SCR_HXEN;
1871         }
1872     } else {
1873         valid_mask &= ~(SCR_RW | SCR_ST);
1874         if (cpu_isar_feature(aa32_ras, cpu)) {
1875             valid_mask |= SCR_TERR;
1876         }
1877     }
1878 
1879     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1880         valid_mask &= ~SCR_HCE;
1881 
1882         /*
1883          * On ARMv7, SMD (or SCD as it is called in v7) is only
1884          * supported if EL2 exists. The bit is UNK/SBZP when
1885          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1886          * when EL2 is unavailable.
1887          * On ARMv8, this bit is always available.
1888          */
1889         if (arm_feature(env, ARM_FEATURE_V7) &&
1890             !arm_feature(env, ARM_FEATURE_V8)) {
1891             valid_mask &= ~SCR_SMD;
1892         }
1893     }
1894 
1895     /* Clear all-context RES0 bits.  */
1896     value &= valid_mask;
1897     changed = env->cp15.scr_el3 ^ value;
1898     env->cp15.scr_el3 = value;
1899 
1900     /*
1901      * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1902      * we must invalidate all TLBs below EL3.
1903      */
1904     if (changed & SCR_NS) {
1905         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1906                                            ARMMMUIdxBit_E20_0 |
1907                                            ARMMMUIdxBit_E10_1 |
1908                                            ARMMMUIdxBit_E20_2 |
1909                                            ARMMMUIdxBit_E10_1_PAN |
1910                                            ARMMMUIdxBit_E20_2_PAN |
1911                                            ARMMMUIdxBit_E2));
1912     }
1913 }
1914 
1915 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1916 {
1917     /*
1918      * scr_write will set the RES1 bits on an AArch64-only CPU.
1919      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1920      */
1921     scr_write(env, ri, 0);
1922 }
1923 
1924 static CPAccessResult access_tid4(CPUARMState *env,
1925                                   const ARMCPRegInfo *ri,
1926                                   bool isread)
1927 {
1928     if (arm_current_el(env) == 1 &&
1929         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1930         return CP_ACCESS_TRAP_EL2;
1931     }
1932 
1933     return CP_ACCESS_OK;
1934 }
1935 
1936 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1937 {
1938     ARMCPU *cpu = env_archcpu(env);
1939 
1940     /*
1941      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1942      * bank
1943      */
1944     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1945                                         ri->secure & ARM_CP_SECSTATE_S);
1946 
1947     return cpu->ccsidr[index];
1948 }
1949 
1950 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1951                          uint64_t value)
1952 {
1953     raw_write(env, ri, value & 0xf);
1954 }
1955 
1956 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1957 {
1958     CPUState *cs = env_cpu(env);
1959     bool el1 = arm_current_el(env) == 1;
1960     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1961     uint64_t ret = 0;
1962 
1963     if (hcr_el2 & HCR_IMO) {
1964         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1965             ret |= CPSR_I;
1966         }
1967     } else {
1968         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1969             ret |= CPSR_I;
1970         }
1971     }
1972 
1973     if (hcr_el2 & HCR_FMO) {
1974         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1975             ret |= CPSR_F;
1976         }
1977     } else {
1978         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1979             ret |= CPSR_F;
1980         }
1981     }
1982 
1983     if (hcr_el2 & HCR_AMO) {
1984         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1985             ret |= CPSR_A;
1986         }
1987     }
1988 
1989     return ret;
1990 }
1991 
1992 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1993                                        bool isread)
1994 {
1995     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1996         return CP_ACCESS_TRAP_EL2;
1997     }
1998 
1999     return CP_ACCESS_OK;
2000 }
2001 
2002 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2003                                        bool isread)
2004 {
2005     if (arm_feature(env, ARM_FEATURE_V8)) {
2006         return access_aa64_tid1(env, ri, isread);
2007     }
2008 
2009     return CP_ACCESS_OK;
2010 }
2011 
2012 static const ARMCPRegInfo v7_cp_reginfo[] = {
2013     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2014     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2015       .access = PL1_W, .type = ARM_CP_NOP },
2016     /*
2017      * Performance monitors are implementation defined in v7,
2018      * but with an ARM recommended set of registers, which we
2019      * follow.
2020      *
2021      * Performance registers fall into three categories:
2022      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2023      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2024      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2025      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2026      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2027      */
2028     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2029       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2030       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2031       .writefn = pmcntenset_write,
2032       .accessfn = pmreg_access,
2033       .raw_writefn = raw_write },
2034     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2035       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2036       .access = PL0_RW, .accessfn = pmreg_access,
2037       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2038       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2039     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2040       .access = PL0_RW,
2041       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2042       .accessfn = pmreg_access,
2043       .writefn = pmcntenclr_write,
2044       .type = ARM_CP_ALIAS | ARM_CP_IO },
2045     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2046       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2047       .access = PL0_RW, .accessfn = pmreg_access,
2048       .type = ARM_CP_ALIAS | ARM_CP_IO,
2049       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2050       .writefn = pmcntenclr_write },
2051     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2052       .access = PL0_RW, .type = ARM_CP_IO,
2053       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2054       .accessfn = pmreg_access,
2055       .writefn = pmovsr_write,
2056       .raw_writefn = raw_write },
2057     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2058       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2059       .access = PL0_RW, .accessfn = pmreg_access,
2060       .type = ARM_CP_ALIAS | ARM_CP_IO,
2061       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2062       .writefn = pmovsr_write,
2063       .raw_writefn = raw_write },
2064     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2065       .access = PL0_W, .accessfn = pmreg_access_swinc,
2066       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2067       .writefn = pmswinc_write },
2068     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2069       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2070       .access = PL0_W, .accessfn = pmreg_access_swinc,
2071       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2072       .writefn = pmswinc_write },
2073     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2074       .access = PL0_RW, .type = ARM_CP_ALIAS,
2075       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2076       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2077       .raw_writefn = raw_write},
2078     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2079       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2080       .access = PL0_RW, .accessfn = pmreg_access_selr,
2081       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2082       .writefn = pmselr_write, .raw_writefn = raw_write, },
2083     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2084       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2085       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2086       .accessfn = pmreg_access_ccntr },
2087     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2088       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2089       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2090       .type = ARM_CP_IO,
2091       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2092       .readfn = pmccntr_read, .writefn = pmccntr_write,
2093       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2094     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2095       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2096       .access = PL0_RW, .accessfn = pmreg_access,
2097       .type = ARM_CP_ALIAS | ARM_CP_IO,
2098       .resetvalue = 0, },
2099     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2100       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2101       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2102       .access = PL0_RW, .accessfn = pmreg_access,
2103       .type = ARM_CP_IO,
2104       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2105       .resetvalue = 0, },
2106     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2107       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2108       .accessfn = pmreg_access,
2109       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2110     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2111       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2112       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2113       .accessfn = pmreg_access,
2114       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2115     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2116       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2117       .accessfn = pmreg_access_xevcntr,
2118       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2119     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2120       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2121       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2122       .accessfn = pmreg_access_xevcntr,
2123       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2124     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2125       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2126       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2127       .resetvalue = 0,
2128       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2129     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2130       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2131       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2132       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2133       .resetvalue = 0,
2134       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2135     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2136       .access = PL1_RW, .accessfn = access_tpm,
2137       .type = ARM_CP_ALIAS | ARM_CP_IO,
2138       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2139       .resetvalue = 0,
2140       .writefn = pmintenset_write, .raw_writefn = raw_write },
2141     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2142       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2143       .access = PL1_RW, .accessfn = access_tpm,
2144       .type = ARM_CP_IO,
2145       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2146       .writefn = pmintenset_write, .raw_writefn = raw_write,
2147       .resetvalue = 0x0 },
2148     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2149       .access = PL1_RW, .accessfn = access_tpm,
2150       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2151       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2152       .writefn = pmintenclr_write, },
2153     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2154       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2155       .access = PL1_RW, .accessfn = access_tpm,
2156       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2157       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2158       .writefn = pmintenclr_write },
2159     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2160       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2161       .access = PL1_R,
2162       .accessfn = access_tid4,
2163       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2164     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2165       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2166       .access = PL1_RW,
2167       .accessfn = access_tid4,
2168       .writefn = csselr_write, .resetvalue = 0,
2169       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2170                              offsetof(CPUARMState, cp15.csselr_ns) } },
2171     /*
2172      * Auxiliary ID register: this actually has an IMPDEF value but for now
2173      * just RAZ for all cores:
2174      */
2175     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2176       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2177       .access = PL1_R, .type = ARM_CP_CONST,
2178       .accessfn = access_aa64_tid1,
2179       .resetvalue = 0 },
2180     /*
2181      * Auxiliary fault status registers: these also are IMPDEF, and we
2182      * choose to RAZ/WI for all cores.
2183      */
2184     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2185       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2186       .access = PL1_RW, .accessfn = access_tvm_trvm,
2187       .type = ARM_CP_CONST, .resetvalue = 0 },
2188     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2189       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2190       .access = PL1_RW, .accessfn = access_tvm_trvm,
2191       .type = ARM_CP_CONST, .resetvalue = 0 },
2192     /*
2193      * MAIR can just read-as-written because we don't implement caches
2194      * and so don't need to care about memory attributes.
2195      */
2196     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2197       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2198       .access = PL1_RW, .accessfn = access_tvm_trvm,
2199       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2200       .resetvalue = 0 },
2201     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2202       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2203       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2204       .resetvalue = 0 },
2205     /*
2206      * For non-long-descriptor page tables these are PRRR and NMRR;
2207      * regardless they still act as reads-as-written for QEMU.
2208      */
2209      /*
2210       * MAIR0/1 are defined separately from their 64-bit counterpart which
2211       * allows them to assign the correct fieldoffset based on the endianness
2212       * handled in the field definitions.
2213       */
2214     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2215       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2216       .access = PL1_RW, .accessfn = access_tvm_trvm,
2217       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2218                              offsetof(CPUARMState, cp15.mair0_ns) },
2219       .resetfn = arm_cp_reset_ignore },
2220     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2221       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2222       .access = PL1_RW, .accessfn = access_tvm_trvm,
2223       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2224                              offsetof(CPUARMState, cp15.mair1_ns) },
2225       .resetfn = arm_cp_reset_ignore },
2226     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2227       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2228       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2229     /* 32 bit ITLB invalidates */
2230     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2231       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2232       .writefn = tlbiall_write },
2233     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2234       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2235       .writefn = tlbimva_write },
2236     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2237       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2238       .writefn = tlbiasid_write },
2239     /* 32 bit DTLB invalidates */
2240     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2241       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2242       .writefn = tlbiall_write },
2243     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2244       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2245       .writefn = tlbimva_write },
2246     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2247       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2248       .writefn = tlbiasid_write },
2249     /* 32 bit TLB invalidates */
2250     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2251       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2252       .writefn = tlbiall_write },
2253     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2254       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2255       .writefn = tlbimva_write },
2256     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2257       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2258       .writefn = tlbiasid_write },
2259     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2260       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2261       .writefn = tlbimvaa_write },
2262 };
2263 
2264 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2265     /* 32 bit TLB invalidates, Inner Shareable */
2266     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2267       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2268       .writefn = tlbiall_is_write },
2269     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2270       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2271       .writefn = tlbimva_is_write },
2272     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2273       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2274       .writefn = tlbiasid_is_write },
2275     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2276       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2277       .writefn = tlbimvaa_is_write },
2278 };
2279 
2280 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2281     /* PMOVSSET is not implemented in v7 before v7ve */
2282     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2283       .access = PL0_RW, .accessfn = pmreg_access,
2284       .type = ARM_CP_ALIAS | ARM_CP_IO,
2285       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2286       .writefn = pmovsset_write,
2287       .raw_writefn = raw_write },
2288     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2289       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2290       .access = PL0_RW, .accessfn = pmreg_access,
2291       .type = ARM_CP_ALIAS | ARM_CP_IO,
2292       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2293       .writefn = pmovsset_write,
2294       .raw_writefn = raw_write },
2295 };
2296 
2297 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2298                         uint64_t value)
2299 {
2300     value &= 1;
2301     env->teecr = value;
2302 }
2303 
2304 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2305                                    bool isread)
2306 {
2307     /*
2308      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2309      * at all, so we don't need to check whether we're v8A.
2310      */
2311     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2312         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2313         return CP_ACCESS_TRAP_EL2;
2314     }
2315     return CP_ACCESS_OK;
2316 }
2317 
2318 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2319                                     bool isread)
2320 {
2321     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2322         return CP_ACCESS_TRAP;
2323     }
2324     return teecr_access(env, ri, isread);
2325 }
2326 
2327 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2328     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2329       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2330       .resetvalue = 0,
2331       .writefn = teecr_write, .accessfn = teecr_access },
2332     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2333       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2334       .accessfn = teehbr_access, .resetvalue = 0 },
2335 };
2336 
2337 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2338     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2339       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2340       .access = PL0_RW,
2341       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2342     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2343       .access = PL0_RW,
2344       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2345                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2346       .resetfn = arm_cp_reset_ignore },
2347     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2348       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2349       .access = PL0_R | PL1_W,
2350       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2351       .resetvalue = 0},
2352     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2353       .access = PL0_R | PL1_W,
2354       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2355                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2356       .resetfn = arm_cp_reset_ignore },
2357     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2358       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2359       .access = PL1_RW,
2360       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2361     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2362       .access = PL1_RW,
2363       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2364                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2365       .resetvalue = 0 },
2366 };
2367 
2368 #ifndef CONFIG_USER_ONLY
2369 
2370 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2371                                        bool isread)
2372 {
2373     /*
2374      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2375      * Writable only at the highest implemented exception level.
2376      */
2377     int el = arm_current_el(env);
2378     uint64_t hcr;
2379     uint32_t cntkctl;
2380 
2381     switch (el) {
2382     case 0:
2383         hcr = arm_hcr_el2_eff(env);
2384         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2385             cntkctl = env->cp15.cnthctl_el2;
2386         } else {
2387             cntkctl = env->cp15.c14_cntkctl;
2388         }
2389         if (!extract32(cntkctl, 0, 2)) {
2390             return CP_ACCESS_TRAP;
2391         }
2392         break;
2393     case 1:
2394         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2395             arm_is_secure_below_el3(env)) {
2396             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2397             return CP_ACCESS_TRAP_UNCATEGORIZED;
2398         }
2399         break;
2400     case 2:
2401     case 3:
2402         break;
2403     }
2404 
2405     if (!isread && el < arm_highest_el(env)) {
2406         return CP_ACCESS_TRAP_UNCATEGORIZED;
2407     }
2408 
2409     return CP_ACCESS_OK;
2410 }
2411 
2412 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2413                                         bool isread)
2414 {
2415     unsigned int cur_el = arm_current_el(env);
2416     bool has_el2 = arm_is_el2_enabled(env);
2417     uint64_t hcr = arm_hcr_el2_eff(env);
2418 
2419     switch (cur_el) {
2420     case 0:
2421         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2422         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2423             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2424                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2425         }
2426 
2427         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2428         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2429             return CP_ACCESS_TRAP;
2430         }
2431 
2432         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2433         if (hcr & HCR_E2H) {
2434             if (timeridx == GTIMER_PHYS &&
2435                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2436                 return CP_ACCESS_TRAP_EL2;
2437             }
2438         } else {
2439             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2440             if (has_el2 && timeridx == GTIMER_PHYS &&
2441                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2442                 return CP_ACCESS_TRAP_EL2;
2443             }
2444         }
2445         break;
2446 
2447     case 1:
2448         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2449         if (has_el2 && timeridx == GTIMER_PHYS &&
2450             (hcr & HCR_E2H
2451              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2452              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2453             return CP_ACCESS_TRAP_EL2;
2454         }
2455         break;
2456     }
2457     return CP_ACCESS_OK;
2458 }
2459 
2460 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2461                                       bool isread)
2462 {
2463     unsigned int cur_el = arm_current_el(env);
2464     bool has_el2 = arm_is_el2_enabled(env);
2465     uint64_t hcr = arm_hcr_el2_eff(env);
2466 
2467     switch (cur_el) {
2468     case 0:
2469         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2470             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2471             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2472                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473         }
2474 
2475         /*
2476          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2477          * EL0 if EL0[PV]TEN is zero.
2478          */
2479         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2480             return CP_ACCESS_TRAP;
2481         }
2482         /* fall through */
2483 
2484     case 1:
2485         if (has_el2 && timeridx == GTIMER_PHYS) {
2486             if (hcr & HCR_E2H) {
2487                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2488                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2489                     return CP_ACCESS_TRAP_EL2;
2490                 }
2491             } else {
2492                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2493                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2494                     return CP_ACCESS_TRAP_EL2;
2495                 }
2496             }
2497         }
2498         break;
2499     }
2500     return CP_ACCESS_OK;
2501 }
2502 
2503 static CPAccessResult gt_pct_access(CPUARMState *env,
2504                                     const ARMCPRegInfo *ri,
2505                                     bool isread)
2506 {
2507     return gt_counter_access(env, GTIMER_PHYS, isread);
2508 }
2509 
2510 static CPAccessResult gt_vct_access(CPUARMState *env,
2511                                     const ARMCPRegInfo *ri,
2512                                     bool isread)
2513 {
2514     return gt_counter_access(env, GTIMER_VIRT, isread);
2515 }
2516 
2517 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2518                                        bool isread)
2519 {
2520     return gt_timer_access(env, GTIMER_PHYS, isread);
2521 }
2522 
2523 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2524                                        bool isread)
2525 {
2526     return gt_timer_access(env, GTIMER_VIRT, isread);
2527 }
2528 
2529 static CPAccessResult gt_stimer_access(CPUARMState *env,
2530                                        const ARMCPRegInfo *ri,
2531                                        bool isread)
2532 {
2533     /*
2534      * The AArch64 register view of the secure physical timer is
2535      * always accessible from EL3, and configurably accessible from
2536      * Secure EL1.
2537      */
2538     switch (arm_current_el(env)) {
2539     case 1:
2540         if (!arm_is_secure(env)) {
2541             return CP_ACCESS_TRAP;
2542         }
2543         if (!(env->cp15.scr_el3 & SCR_ST)) {
2544             return CP_ACCESS_TRAP_EL3;
2545         }
2546         return CP_ACCESS_OK;
2547     case 0:
2548     case 2:
2549         return CP_ACCESS_TRAP;
2550     case 3:
2551         return CP_ACCESS_OK;
2552     default:
2553         g_assert_not_reached();
2554     }
2555 }
2556 
2557 static uint64_t gt_get_countervalue(CPUARMState *env)
2558 {
2559     ARMCPU *cpu = env_archcpu(env);
2560 
2561     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2562 }
2563 
2564 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2565 {
2566     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2567 
2568     if (gt->ctl & 1) {
2569         /*
2570          * Timer enabled: calculate and set current ISTATUS, irq, and
2571          * reset timer to when ISTATUS next has to change
2572          */
2573         uint64_t offset = timeridx == GTIMER_VIRT ?
2574                                       cpu->env.cp15.cntvoff_el2 : 0;
2575         uint64_t count = gt_get_countervalue(&cpu->env);
2576         /* Note that this must be unsigned 64 bit arithmetic: */
2577         int istatus = count - offset >= gt->cval;
2578         uint64_t nexttick;
2579         int irqstate;
2580 
2581         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2582 
2583         irqstate = (istatus && !(gt->ctl & 2));
2584         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2585 
2586         if (istatus) {
2587             /* Next transition is when count rolls back over to zero */
2588             nexttick = UINT64_MAX;
2589         } else {
2590             /* Next transition is when we hit cval */
2591             nexttick = gt->cval + offset;
2592         }
2593         /*
2594          * Note that the desired next expiry time might be beyond the
2595          * signed-64-bit range of a QEMUTimer -- in this case we just
2596          * set the timer for as far in the future as possible. When the
2597          * timer expires we will reset the timer for any remaining period.
2598          */
2599         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2600             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2601         } else {
2602             timer_mod(cpu->gt_timer[timeridx], nexttick);
2603         }
2604         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2605     } else {
2606         /* Timer disabled: ISTATUS and timer output always clear */
2607         gt->ctl &= ~4;
2608         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2609         timer_del(cpu->gt_timer[timeridx]);
2610         trace_arm_gt_recalc_disabled(timeridx);
2611     }
2612 }
2613 
2614 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2615                            int timeridx)
2616 {
2617     ARMCPU *cpu = env_archcpu(env);
2618 
2619     timer_del(cpu->gt_timer[timeridx]);
2620 }
2621 
2622 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2623 {
2624     return gt_get_countervalue(env);
2625 }
2626 
2627 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2628 {
2629     uint64_t hcr;
2630 
2631     switch (arm_current_el(env)) {
2632     case 2:
2633         hcr = arm_hcr_el2_eff(env);
2634         if (hcr & HCR_E2H) {
2635             return 0;
2636         }
2637         break;
2638     case 0:
2639         hcr = arm_hcr_el2_eff(env);
2640         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2641             return 0;
2642         }
2643         break;
2644     }
2645 
2646     return env->cp15.cntvoff_el2;
2647 }
2648 
2649 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650 {
2651     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2652 }
2653 
2654 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655                           int timeridx,
2656                           uint64_t value)
2657 {
2658     trace_arm_gt_cval_write(timeridx, value);
2659     env->cp15.c14_timer[timeridx].cval = value;
2660     gt_recalc_timer(env_archcpu(env), timeridx);
2661 }
2662 
2663 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2664                              int timeridx)
2665 {
2666     uint64_t offset = 0;
2667 
2668     switch (timeridx) {
2669     case GTIMER_VIRT:
2670     case GTIMER_HYPVIRT:
2671         offset = gt_virt_cnt_offset(env);
2672         break;
2673     }
2674 
2675     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2676                       (gt_get_countervalue(env) - offset));
2677 }
2678 
2679 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2680                           int timeridx,
2681                           uint64_t value)
2682 {
2683     uint64_t offset = 0;
2684 
2685     switch (timeridx) {
2686     case GTIMER_VIRT:
2687     case GTIMER_HYPVIRT:
2688         offset = gt_virt_cnt_offset(env);
2689         break;
2690     }
2691 
2692     trace_arm_gt_tval_write(timeridx, value);
2693     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2694                                          sextract64(value, 0, 32);
2695     gt_recalc_timer(env_archcpu(env), timeridx);
2696 }
2697 
2698 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2699                          int timeridx,
2700                          uint64_t value)
2701 {
2702     ARMCPU *cpu = env_archcpu(env);
2703     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2704 
2705     trace_arm_gt_ctl_write(timeridx, value);
2706     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2707     if ((oldval ^ value) & 1) {
2708         /* Enable toggled */
2709         gt_recalc_timer(cpu, timeridx);
2710     } else if ((oldval ^ value) & 2) {
2711         /*
2712          * IMASK toggled: don't need to recalculate,
2713          * just set the interrupt line based on ISTATUS
2714          */
2715         int irqstate = (oldval & 4) && !(value & 2);
2716 
2717         trace_arm_gt_imask_toggle(timeridx, irqstate);
2718         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2719     }
2720 }
2721 
2722 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2723 {
2724     gt_timer_reset(env, ri, GTIMER_PHYS);
2725 }
2726 
2727 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2728                                uint64_t value)
2729 {
2730     gt_cval_write(env, ri, GTIMER_PHYS, value);
2731 }
2732 
2733 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2734 {
2735     return gt_tval_read(env, ri, GTIMER_PHYS);
2736 }
2737 
2738 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739                                uint64_t value)
2740 {
2741     gt_tval_write(env, ri, GTIMER_PHYS, value);
2742 }
2743 
2744 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745                               uint64_t value)
2746 {
2747     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2748 }
2749 
2750 static int gt_phys_redir_timeridx(CPUARMState *env)
2751 {
2752     switch (arm_mmu_idx(env)) {
2753     case ARMMMUIdx_E20_0:
2754     case ARMMMUIdx_E20_2:
2755     case ARMMMUIdx_E20_2_PAN:
2756         return GTIMER_HYP;
2757     default:
2758         return GTIMER_PHYS;
2759     }
2760 }
2761 
2762 static int gt_virt_redir_timeridx(CPUARMState *env)
2763 {
2764     switch (arm_mmu_idx(env)) {
2765     case ARMMMUIdx_E20_0:
2766     case ARMMMUIdx_E20_2:
2767     case ARMMMUIdx_E20_2_PAN:
2768         return GTIMER_HYPVIRT;
2769     default:
2770         return GTIMER_VIRT;
2771     }
2772 }
2773 
2774 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2775                                         const ARMCPRegInfo *ri)
2776 {
2777     int timeridx = gt_phys_redir_timeridx(env);
2778     return env->cp15.c14_timer[timeridx].cval;
2779 }
2780 
2781 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782                                      uint64_t value)
2783 {
2784     int timeridx = gt_phys_redir_timeridx(env);
2785     gt_cval_write(env, ri, timeridx, value);
2786 }
2787 
2788 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2789                                         const ARMCPRegInfo *ri)
2790 {
2791     int timeridx = gt_phys_redir_timeridx(env);
2792     return gt_tval_read(env, ri, timeridx);
2793 }
2794 
2795 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796                                      uint64_t value)
2797 {
2798     int timeridx = gt_phys_redir_timeridx(env);
2799     gt_tval_write(env, ri, timeridx, value);
2800 }
2801 
2802 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2803                                        const ARMCPRegInfo *ri)
2804 {
2805     int timeridx = gt_phys_redir_timeridx(env);
2806     return env->cp15.c14_timer[timeridx].ctl;
2807 }
2808 
2809 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810                                     uint64_t value)
2811 {
2812     int timeridx = gt_phys_redir_timeridx(env);
2813     gt_ctl_write(env, ri, timeridx, value);
2814 }
2815 
2816 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2817 {
2818     gt_timer_reset(env, ri, GTIMER_VIRT);
2819 }
2820 
2821 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822                                uint64_t value)
2823 {
2824     gt_cval_write(env, ri, GTIMER_VIRT, value);
2825 }
2826 
2827 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     return gt_tval_read(env, ri, GTIMER_VIRT);
2830 }
2831 
2832 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833                                uint64_t value)
2834 {
2835     gt_tval_write(env, ri, GTIMER_VIRT, value);
2836 }
2837 
2838 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839                               uint64_t value)
2840 {
2841     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2842 }
2843 
2844 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2845                               uint64_t value)
2846 {
2847     ARMCPU *cpu = env_archcpu(env);
2848 
2849     trace_arm_gt_cntvoff_write(value);
2850     raw_write(env, ri, value);
2851     gt_recalc_timer(cpu, GTIMER_VIRT);
2852 }
2853 
2854 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2855                                         const ARMCPRegInfo *ri)
2856 {
2857     int timeridx = gt_virt_redir_timeridx(env);
2858     return env->cp15.c14_timer[timeridx].cval;
2859 }
2860 
2861 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2862                                      uint64_t value)
2863 {
2864     int timeridx = gt_virt_redir_timeridx(env);
2865     gt_cval_write(env, ri, timeridx, value);
2866 }
2867 
2868 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2869                                         const ARMCPRegInfo *ri)
2870 {
2871     int timeridx = gt_virt_redir_timeridx(env);
2872     return gt_tval_read(env, ri, timeridx);
2873 }
2874 
2875 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2876                                      uint64_t value)
2877 {
2878     int timeridx = gt_virt_redir_timeridx(env);
2879     gt_tval_write(env, ri, timeridx, value);
2880 }
2881 
2882 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2883                                        const ARMCPRegInfo *ri)
2884 {
2885     int timeridx = gt_virt_redir_timeridx(env);
2886     return env->cp15.c14_timer[timeridx].ctl;
2887 }
2888 
2889 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2890                                     uint64_t value)
2891 {
2892     int timeridx = gt_virt_redir_timeridx(env);
2893     gt_ctl_write(env, ri, timeridx, value);
2894 }
2895 
2896 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2897 {
2898     gt_timer_reset(env, ri, GTIMER_HYP);
2899 }
2900 
2901 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902                               uint64_t value)
2903 {
2904     gt_cval_write(env, ri, GTIMER_HYP, value);
2905 }
2906 
2907 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2908 {
2909     return gt_tval_read(env, ri, GTIMER_HYP);
2910 }
2911 
2912 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2913                               uint64_t value)
2914 {
2915     gt_tval_write(env, ri, GTIMER_HYP, value);
2916 }
2917 
2918 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2919                               uint64_t value)
2920 {
2921     gt_ctl_write(env, ri, GTIMER_HYP, value);
2922 }
2923 
2924 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2925 {
2926     gt_timer_reset(env, ri, GTIMER_SEC);
2927 }
2928 
2929 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2930                               uint64_t value)
2931 {
2932     gt_cval_write(env, ri, GTIMER_SEC, value);
2933 }
2934 
2935 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2936 {
2937     return gt_tval_read(env, ri, GTIMER_SEC);
2938 }
2939 
2940 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941                               uint64_t value)
2942 {
2943     gt_tval_write(env, ri, GTIMER_SEC, value);
2944 }
2945 
2946 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2947                               uint64_t value)
2948 {
2949     gt_ctl_write(env, ri, GTIMER_SEC, value);
2950 }
2951 
2952 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2953 {
2954     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2955 }
2956 
2957 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2958                              uint64_t value)
2959 {
2960     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2961 }
2962 
2963 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2964 {
2965     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2966 }
2967 
2968 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2969                              uint64_t value)
2970 {
2971     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2972 }
2973 
2974 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2975                             uint64_t value)
2976 {
2977     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2978 }
2979 
2980 void arm_gt_ptimer_cb(void *opaque)
2981 {
2982     ARMCPU *cpu = opaque;
2983 
2984     gt_recalc_timer(cpu, GTIMER_PHYS);
2985 }
2986 
2987 void arm_gt_vtimer_cb(void *opaque)
2988 {
2989     ARMCPU *cpu = opaque;
2990 
2991     gt_recalc_timer(cpu, GTIMER_VIRT);
2992 }
2993 
2994 void arm_gt_htimer_cb(void *opaque)
2995 {
2996     ARMCPU *cpu = opaque;
2997 
2998     gt_recalc_timer(cpu, GTIMER_HYP);
2999 }
3000 
3001 void arm_gt_stimer_cb(void *opaque)
3002 {
3003     ARMCPU *cpu = opaque;
3004 
3005     gt_recalc_timer(cpu, GTIMER_SEC);
3006 }
3007 
3008 void arm_gt_hvtimer_cb(void *opaque)
3009 {
3010     ARMCPU *cpu = opaque;
3011 
3012     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3013 }
3014 
3015 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3016 {
3017     ARMCPU *cpu = env_archcpu(env);
3018 
3019     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3020 }
3021 
3022 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3023     /*
3024      * Note that CNTFRQ is purely reads-as-written for the benefit
3025      * of software; writing it doesn't actually change the timer frequency.
3026      * Our reset value matches the fixed frequency we implement the timer at.
3027      */
3028     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3029       .type = ARM_CP_ALIAS,
3030       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3031       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3032     },
3033     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3034       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3035       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3036       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3037       .resetfn = arm_gt_cntfrq_reset,
3038     },
3039     /* overall control: mostly access permissions */
3040     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3041       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3042       .access = PL1_RW,
3043       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3044       .resetvalue = 0,
3045     },
3046     /* per-timer control */
3047     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3048       .secure = ARM_CP_SECSTATE_NS,
3049       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3050       .accessfn = gt_ptimer_access,
3051       .fieldoffset = offsetoflow32(CPUARMState,
3052                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3053       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3054       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3055     },
3056     { .name = "CNTP_CTL_S",
3057       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3058       .secure = ARM_CP_SECSTATE_S,
3059       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3060       .accessfn = gt_ptimer_access,
3061       .fieldoffset = offsetoflow32(CPUARMState,
3062                                    cp15.c14_timer[GTIMER_SEC].ctl),
3063       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3064     },
3065     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3066       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3067       .type = ARM_CP_IO, .access = PL0_RW,
3068       .accessfn = gt_ptimer_access,
3069       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3070       .resetvalue = 0,
3071       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3072       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3073     },
3074     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3075       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3076       .accessfn = gt_vtimer_access,
3077       .fieldoffset = offsetoflow32(CPUARMState,
3078                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3079       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3080       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3081     },
3082     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3083       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3084       .type = ARM_CP_IO, .access = PL0_RW,
3085       .accessfn = gt_vtimer_access,
3086       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3087       .resetvalue = 0,
3088       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3089       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3090     },
3091     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3092     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3093       .secure = ARM_CP_SECSTATE_NS,
3094       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3095       .accessfn = gt_ptimer_access,
3096       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3097     },
3098     { .name = "CNTP_TVAL_S",
3099       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3100       .secure = ARM_CP_SECSTATE_S,
3101       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3102       .accessfn = gt_ptimer_access,
3103       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3104     },
3105     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3106       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3107       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3108       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3109       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3110     },
3111     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3112       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3113       .accessfn = gt_vtimer_access,
3114       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3115     },
3116     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3117       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3118       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3119       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3120       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3121     },
3122     /* The counter itself */
3123     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3124       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3125       .accessfn = gt_pct_access,
3126       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3127     },
3128     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3129       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3130       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3131       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3132     },
3133     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3134       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3135       .accessfn = gt_vct_access,
3136       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3137     },
3138     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3139       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3140       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3141       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3142     },
3143     /* Comparison value, indicating when the timer goes off */
3144     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3145       .secure = ARM_CP_SECSTATE_NS,
3146       .access = PL0_RW,
3147       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3148       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3149       .accessfn = gt_ptimer_access,
3150       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3151       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3152     },
3153     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3154       .secure = ARM_CP_SECSTATE_S,
3155       .access = PL0_RW,
3156       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3157       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3158       .accessfn = gt_ptimer_access,
3159       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3160     },
3161     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3162       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3163       .access = PL0_RW,
3164       .type = ARM_CP_IO,
3165       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3166       .resetvalue = 0, .accessfn = gt_ptimer_access,
3167       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3168       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3169     },
3170     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3171       .access = PL0_RW,
3172       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3173       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3174       .accessfn = gt_vtimer_access,
3175       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3176       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3177     },
3178     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3179       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3180       .access = PL0_RW,
3181       .type = ARM_CP_IO,
3182       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3183       .resetvalue = 0, .accessfn = gt_vtimer_access,
3184       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3185       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3186     },
3187     /*
3188      * Secure timer -- this is actually restricted to only EL3
3189      * and configurably Secure-EL1 via the accessfn.
3190      */
3191     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3192       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3193       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3194       .accessfn = gt_stimer_access,
3195       .readfn = gt_sec_tval_read,
3196       .writefn = gt_sec_tval_write,
3197       .resetfn = gt_sec_timer_reset,
3198     },
3199     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3200       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3201       .type = ARM_CP_IO, .access = PL1_RW,
3202       .accessfn = gt_stimer_access,
3203       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3204       .resetvalue = 0,
3205       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3206     },
3207     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3208       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3209       .type = ARM_CP_IO, .access = PL1_RW,
3210       .accessfn = gt_stimer_access,
3211       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3212       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3213     },
3214 };
3215 
3216 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3217                                  bool isread)
3218 {
3219     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3220         return CP_ACCESS_TRAP;
3221     }
3222     return CP_ACCESS_OK;
3223 }
3224 
3225 #else
3226 
3227 /*
3228  * In user-mode most of the generic timer registers are inaccessible
3229  * however modern kernels (4.12+) allow access to cntvct_el0
3230  */
3231 
3232 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3233 {
3234     ARMCPU *cpu = env_archcpu(env);
3235 
3236     /*
3237      * Currently we have no support for QEMUTimer in linux-user so we
3238      * can't call gt_get_countervalue(env), instead we directly
3239      * call the lower level functions.
3240      */
3241     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3242 }
3243 
3244 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3245     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3246       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3247       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3248       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3249       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3250     },
3251     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3252       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3253       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3254       .readfn = gt_virt_cnt_read,
3255     },
3256 };
3257 
3258 #endif
3259 
3260 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3261 {
3262     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3263         raw_write(env, ri, value);
3264     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3265         raw_write(env, ri, value & 0xfffff6ff);
3266     } else {
3267         raw_write(env, ri, value & 0xfffff1ff);
3268     }
3269 }
3270 
3271 #ifndef CONFIG_USER_ONLY
3272 /* get_phys_addr() isn't present for user-mode-only targets */
3273 
3274 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3275                                  bool isread)
3276 {
3277     if (ri->opc2 & 4) {
3278         /*
3279          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3280          * Secure EL1 (which can only happen if EL3 is AArch64).
3281          * They are simply UNDEF if executed from NS EL1.
3282          * They function normally from EL2 or EL3.
3283          */
3284         if (arm_current_el(env) == 1) {
3285             if (arm_is_secure_below_el3(env)) {
3286                 if (env->cp15.scr_el3 & SCR_EEL2) {
3287                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3288                 }
3289                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3290             }
3291             return CP_ACCESS_TRAP_UNCATEGORIZED;
3292         }
3293     }
3294     return CP_ACCESS_OK;
3295 }
3296 
3297 #ifdef CONFIG_TCG
3298 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3299                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3300                              bool is_secure)
3301 {
3302     bool ret;
3303     uint64_t par64;
3304     bool format64 = false;
3305     ARMMMUFaultInfo fi = {};
3306     GetPhysAddrResult res = {};
3307 
3308     ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3309                                     is_secure, &res, &fi);
3310 
3311     /*
3312      * ATS operations only do S1 or S1+S2 translations, so we never
3313      * have to deal with the ARMCacheAttrs format for S2 only.
3314      */
3315     assert(!res.cacheattrs.is_s2_format);
3316 
3317     if (ret) {
3318         /*
3319          * Some kinds of translation fault must cause exceptions rather
3320          * than being reported in the PAR.
3321          */
3322         int current_el = arm_current_el(env);
3323         int target_el;
3324         uint32_t syn, fsr, fsc;
3325         bool take_exc = false;
3326 
3327         if (fi.s1ptw && current_el == 1
3328             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3329             /*
3330              * Synchronous stage 2 fault on an access made as part of the
3331              * translation table walk for AT S1E0* or AT S1E1* insn
3332              * executed from NS EL1. If this is a synchronous external abort
3333              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3334              * to EL3. Otherwise the fault is taken as an exception to EL2,
3335              * and HPFAR_EL2 holds the faulting IPA.
3336              */
3337             if (fi.type == ARMFault_SyncExternalOnWalk &&
3338                 (env->cp15.scr_el3 & SCR_EA)) {
3339                 target_el = 3;
3340             } else {
3341                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3342                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3343                     env->cp15.hpfar_el2 |= HPFAR_NS;
3344                 }
3345                 target_el = 2;
3346             }
3347             take_exc = true;
3348         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3349             /*
3350              * Synchronous external aborts during a translation table walk
3351              * are taken as Data Abort exceptions.
3352              */
3353             if (fi.stage2) {
3354                 if (current_el == 3) {
3355                     target_el = 3;
3356                 } else {
3357                     target_el = 2;
3358                 }
3359             } else {
3360                 target_el = exception_target_el(env);
3361             }
3362             take_exc = true;
3363         }
3364 
3365         if (take_exc) {
3366             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3367             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3368                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3369                 fsr = arm_fi_to_lfsc(&fi);
3370                 fsc = extract32(fsr, 0, 6);
3371             } else {
3372                 fsr = arm_fi_to_sfsc(&fi);
3373                 fsc = 0x3f;
3374             }
3375             /*
3376              * Report exception with ESR indicating a fault due to a
3377              * translation table walk for a cache maintenance instruction.
3378              */
3379             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3380                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3381             env->exception.vaddress = value;
3382             env->exception.fsr = fsr;
3383             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3384         }
3385     }
3386 
3387     if (is_a64(env)) {
3388         format64 = true;
3389     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3390         /*
3391          * ATS1Cxx:
3392          * * TTBCR.EAE determines whether the result is returned using the
3393          *   32-bit or the 64-bit PAR format
3394          * * Instructions executed in Hyp mode always use the 64bit format
3395          *
3396          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3397          * * The Non-secure TTBCR.EAE bit is set to 1
3398          * * The implementation includes EL2, and the value of HCR.VM is 1
3399          *
3400          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3401          *
3402          * ATS1Hx always uses the 64bit format.
3403          */
3404         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3405 
3406         if (arm_feature(env, ARM_FEATURE_EL2)) {
3407             if (mmu_idx == ARMMMUIdx_E10_0 ||
3408                 mmu_idx == ARMMMUIdx_E10_1 ||
3409                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3410                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3411             } else {
3412                 format64 |= arm_current_el(env) == 2;
3413             }
3414         }
3415     }
3416 
3417     if (format64) {
3418         /* Create a 64-bit PAR */
3419         par64 = (1 << 11); /* LPAE bit always set */
3420         if (!ret) {
3421             par64 |= res.f.phys_addr & ~0xfffULL;
3422             if (!res.f.attrs.secure) {
3423                 par64 |= (1 << 9); /* NS */
3424             }
3425             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3426             par64 |= res.cacheattrs.shareability << 7; /* SH */
3427         } else {
3428             uint32_t fsr = arm_fi_to_lfsc(&fi);
3429 
3430             par64 |= 1; /* F */
3431             par64 |= (fsr & 0x3f) << 1; /* FS */
3432             if (fi.stage2) {
3433                 par64 |= (1 << 9); /* S */
3434             }
3435             if (fi.s1ptw) {
3436                 par64 |= (1 << 8); /* PTW */
3437             }
3438         }
3439     } else {
3440         /*
3441          * fsr is a DFSR/IFSR value for the short descriptor
3442          * translation table format (with WnR always clear).
3443          * Convert it to a 32-bit PAR.
3444          */
3445         if (!ret) {
3446             /* We do not set any attribute bits in the PAR */
3447             if (res.f.lg_page_size == 24
3448                 && arm_feature(env, ARM_FEATURE_V7)) {
3449                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3450             } else {
3451                 par64 = res.f.phys_addr & 0xfffff000;
3452             }
3453             if (!res.f.attrs.secure) {
3454                 par64 |= (1 << 9); /* NS */
3455             }
3456         } else {
3457             uint32_t fsr = arm_fi_to_sfsc(&fi);
3458 
3459             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3460                     ((fsr & 0xf) << 1) | 1;
3461         }
3462     }
3463     return par64;
3464 }
3465 #endif /* CONFIG_TCG */
3466 
3467 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3468 {
3469 #ifdef CONFIG_TCG
3470     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3471     uint64_t par64;
3472     ARMMMUIdx mmu_idx;
3473     int el = arm_current_el(env);
3474     bool secure = arm_is_secure_below_el3(env);
3475 
3476     switch (ri->opc2 & 6) {
3477     case 0:
3478         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3479         switch (el) {
3480         case 3:
3481             mmu_idx = ARMMMUIdx_E3;
3482             secure = true;
3483             break;
3484         case 2:
3485             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3486             /* fall through */
3487         case 1:
3488             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3489                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3490             } else {
3491                 mmu_idx = ARMMMUIdx_Stage1_E1;
3492             }
3493             break;
3494         default:
3495             g_assert_not_reached();
3496         }
3497         break;
3498     case 2:
3499         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3500         switch (el) {
3501         case 3:
3502             mmu_idx = ARMMMUIdx_E10_0;
3503             secure = true;
3504             break;
3505         case 2:
3506             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3507             mmu_idx = ARMMMUIdx_Stage1_E0;
3508             break;
3509         case 1:
3510             mmu_idx = ARMMMUIdx_Stage1_E0;
3511             break;
3512         default:
3513             g_assert_not_reached();
3514         }
3515         break;
3516     case 4:
3517         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3518         mmu_idx = ARMMMUIdx_E10_1;
3519         secure = false;
3520         break;
3521     case 6:
3522         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3523         mmu_idx = ARMMMUIdx_E10_0;
3524         secure = false;
3525         break;
3526     default:
3527         g_assert_not_reached();
3528     }
3529 
3530     par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
3531 
3532     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3533 #else
3534     /* Handled by hardware accelerator. */
3535     g_assert_not_reached();
3536 #endif /* CONFIG_TCG */
3537 }
3538 
3539 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3540                         uint64_t value)
3541 {
3542 #ifdef CONFIG_TCG
3543     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3544     uint64_t par64;
3545 
3546     /* There is no SecureEL2 for AArch32. */
3547     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
3548 
3549     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3550 #else
3551     /* Handled by hardware accelerator. */
3552     g_assert_not_reached();
3553 #endif /* CONFIG_TCG */
3554 }
3555 
3556 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3557                                      bool isread)
3558 {
3559     if (arm_current_el(env) == 3 &&
3560         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3561         return CP_ACCESS_TRAP;
3562     }
3563     return CP_ACCESS_OK;
3564 }
3565 
3566 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3567                         uint64_t value)
3568 {
3569 #ifdef CONFIG_TCG
3570     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3571     ARMMMUIdx mmu_idx;
3572     int secure = arm_is_secure_below_el3(env);
3573     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3574     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3575 
3576     switch (ri->opc2 & 6) {
3577     case 0:
3578         switch (ri->opc1) {
3579         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3580             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3581                 mmu_idx = regime_e20 ?
3582                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3583             } else {
3584                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3585             }
3586             break;
3587         case 4: /* AT S1E2R, AT S1E2W */
3588             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3589             break;
3590         case 6: /* AT S1E3R, AT S1E3W */
3591             mmu_idx = ARMMMUIdx_E3;
3592             secure = true;
3593             break;
3594         default:
3595             g_assert_not_reached();
3596         }
3597         break;
3598     case 2: /* AT S1E0R, AT S1E0W */
3599         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3600         break;
3601     case 4: /* AT S12E1R, AT S12E1W */
3602         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3603         break;
3604     case 6: /* AT S12E0R, AT S12E0W */
3605         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3606         break;
3607     default:
3608         g_assert_not_reached();
3609     }
3610 
3611     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3612                                        mmu_idx, secure);
3613 #else
3614     /* Handled by hardware accelerator. */
3615     g_assert_not_reached();
3616 #endif /* CONFIG_TCG */
3617 }
3618 #endif
3619 
3620 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3621     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3622       .access = PL1_RW, .resetvalue = 0,
3623       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3624                              offsetoflow32(CPUARMState, cp15.par_ns) },
3625       .writefn = par_write },
3626 #ifndef CONFIG_USER_ONLY
3627     /* This underdecoding is safe because the reginfo is NO_RAW. */
3628     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3629       .access = PL1_W, .accessfn = ats_access,
3630       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3631 #endif
3632 };
3633 
3634 /* Return basic MPU access permission bits.  */
3635 static uint32_t simple_mpu_ap_bits(uint32_t val)
3636 {
3637     uint32_t ret;
3638     uint32_t mask;
3639     int i;
3640     ret = 0;
3641     mask = 3;
3642     for (i = 0; i < 16; i += 2) {
3643         ret |= (val >> i) & mask;
3644         mask <<= 2;
3645     }
3646     return ret;
3647 }
3648 
3649 /* Pad basic MPU access permission bits to extended format.  */
3650 static uint32_t extended_mpu_ap_bits(uint32_t val)
3651 {
3652     uint32_t ret;
3653     uint32_t mask;
3654     int i;
3655     ret = 0;
3656     mask = 3;
3657     for (i = 0; i < 16; i += 2) {
3658         ret |= (val & mask) << i;
3659         mask <<= 2;
3660     }
3661     return ret;
3662 }
3663 
3664 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3665                                  uint64_t value)
3666 {
3667     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3668 }
3669 
3670 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3671 {
3672     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3673 }
3674 
3675 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676                                  uint64_t value)
3677 {
3678     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3679 }
3680 
3681 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3682 {
3683     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3684 }
3685 
3686 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3687 {
3688     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3689 
3690     if (!u32p) {
3691         return 0;
3692     }
3693 
3694     u32p += env->pmsav7.rnr[M_REG_NS];
3695     return *u32p;
3696 }
3697 
3698 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3699                          uint64_t value)
3700 {
3701     ARMCPU *cpu = env_archcpu(env);
3702     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3703 
3704     if (!u32p) {
3705         return;
3706     }
3707 
3708     u32p += env->pmsav7.rnr[M_REG_NS];
3709     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3710     *u32p = value;
3711 }
3712 
3713 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3714                               uint64_t value)
3715 {
3716     ARMCPU *cpu = env_archcpu(env);
3717     uint32_t nrgs = cpu->pmsav7_dregion;
3718 
3719     if (value >= nrgs) {
3720         qemu_log_mask(LOG_GUEST_ERROR,
3721                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3722                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3723         return;
3724     }
3725 
3726     raw_write(env, ri, value);
3727 }
3728 
3729 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3730                           uint64_t value)
3731 {
3732     ARMCPU *cpu = env_archcpu(env);
3733 
3734     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3735     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3736 }
3737 
3738 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3739 {
3740     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3741 }
3742 
3743 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3744                           uint64_t value)
3745 {
3746     ARMCPU *cpu = env_archcpu(env);
3747 
3748     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3749     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3750 }
3751 
3752 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3753 {
3754     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3755 }
3756 
3757 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3758                            uint64_t value)
3759 {
3760     ARMCPU *cpu = env_archcpu(env);
3761 
3762     /*
3763      * Ignore writes that would select not implemented region.
3764      * This is architecturally UNPREDICTABLE.
3765      */
3766     if (value >= cpu->pmsav7_dregion) {
3767         return;
3768     }
3769 
3770     env->pmsav7.rnr[M_REG_NS] = value;
3771 }
3772 
3773 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3774                           uint64_t value)
3775 {
3776     ARMCPU *cpu = env_archcpu(env);
3777 
3778     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3779     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3780 }
3781 
3782 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3783 {
3784     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3785 }
3786 
3787 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3788                           uint64_t value)
3789 {
3790     ARMCPU *cpu = env_archcpu(env);
3791 
3792     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3793     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3794 }
3795 
3796 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3797 {
3798     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3799 }
3800 
3801 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3802                           uint64_t value)
3803 {
3804     uint32_t n;
3805     uint32_t bit;
3806     ARMCPU *cpu = env_archcpu(env);
3807 
3808     /* Ignore writes to unimplemented regions */
3809     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3810     value &= MAKE_64BIT_MASK(0, rmax);
3811 
3812     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3813 
3814     /* Register alias is only valid for first 32 indexes */
3815     for (n = 0; n < rmax; ++n) {
3816         bit = extract32(value, n, 1);
3817         env->pmsav8.hprlar[n] = deposit32(
3818                     env->pmsav8.hprlar[n], 0, 1, bit);
3819     }
3820 }
3821 
3822 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3823 {
3824     uint32_t n;
3825     uint32_t result = 0x0;
3826     ARMCPU *cpu = env_archcpu(env);
3827 
3828     /* Register alias is only valid for first 32 indexes */
3829     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3830         if (env->pmsav8.hprlar[n] & 0x1) {
3831             result |= (0x1 << n);
3832         }
3833     }
3834     return result;
3835 }
3836 
3837 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3838                            uint64_t value)
3839 {
3840     ARMCPU *cpu = env_archcpu(env);
3841 
3842     /*
3843      * Ignore writes that would select not implemented region.
3844      * This is architecturally UNPREDICTABLE.
3845      */
3846     if (value >= cpu->pmsav8r_hdregion) {
3847         return;
3848     }
3849 
3850     env->pmsav8.hprselr = value;
3851 }
3852 
3853 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3854                           uint64_t value)
3855 {
3856     ARMCPU *cpu = env_archcpu(env);
3857     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3858                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3859 
3860     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3861 
3862     if (ri->opc1 & 4) {
3863         if (index >= cpu->pmsav8r_hdregion) {
3864             return;
3865         }
3866         if (ri->opc2 & 0x1) {
3867             env->pmsav8.hprlar[index] = value;
3868         } else {
3869             env->pmsav8.hprbar[index] = value;
3870         }
3871     } else {
3872         if (index >= cpu->pmsav7_dregion) {
3873             return;
3874         }
3875         if (ri->opc2 & 0x1) {
3876             env->pmsav8.rlar[M_REG_NS][index] = value;
3877         } else {
3878             env->pmsav8.rbar[M_REG_NS][index] = value;
3879         }
3880     }
3881 }
3882 
3883 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3884 {
3885     ARMCPU *cpu = env_archcpu(env);
3886     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3887                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3888 
3889     if (ri->opc1 & 4) {
3890         if (index >= cpu->pmsav8r_hdregion) {
3891             return 0x0;
3892         }
3893         if (ri->opc2 & 0x1) {
3894             return env->pmsav8.hprlar[index];
3895         } else {
3896             return env->pmsav8.hprbar[index];
3897         }
3898     } else {
3899         if (index >= cpu->pmsav7_dregion) {
3900             return 0x0;
3901         }
3902         if (ri->opc2 & 0x1) {
3903             return env->pmsav8.rlar[M_REG_NS][index];
3904         } else {
3905             return env->pmsav8.rbar[M_REG_NS][index];
3906         }
3907     }
3908 }
3909 
3910 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3911     { .name = "PRBAR",
3912       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3913       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3914       .accessfn = access_tvm_trvm,
3915       .readfn = prbar_read, .writefn = prbar_write },
3916     { .name = "PRLAR",
3917       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3918       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3919       .accessfn = access_tvm_trvm,
3920       .readfn = prlar_read, .writefn = prlar_write },
3921     { .name = "PRSELR", .resetvalue = 0,
3922       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3923       .access = PL1_RW, .accessfn = access_tvm_trvm,
3924       .writefn = prselr_write,
3925       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3926     { .name = "HPRBAR", .resetvalue = 0,
3927       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3928       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3929       .readfn = hprbar_read, .writefn = hprbar_write },
3930     { .name = "HPRLAR",
3931       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3932       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3933       .readfn = hprlar_read, .writefn = hprlar_write },
3934     { .name = "HPRSELR", .resetvalue = 0,
3935       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3936       .access = PL2_RW,
3937       .writefn = hprselr_write,
3938       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3939     { .name = "HPRENR",
3940       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3941       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3942       .readfn = hprenr_read, .writefn = hprenr_write },
3943 };
3944 
3945 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3946     /*
3947      * Reset for all these registers is handled in arm_cpu_reset(),
3948      * because the PMSAv7 is also used by M-profile CPUs, which do
3949      * not register cpregs but still need the state to be reset.
3950      */
3951     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3952       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3953       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3954       .readfn = pmsav7_read, .writefn = pmsav7_write,
3955       .resetfn = arm_cp_reset_ignore },
3956     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3957       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3958       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3959       .readfn = pmsav7_read, .writefn = pmsav7_write,
3960       .resetfn = arm_cp_reset_ignore },
3961     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3962       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3963       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3964       .readfn = pmsav7_read, .writefn = pmsav7_write,
3965       .resetfn = arm_cp_reset_ignore },
3966     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3967       .access = PL1_RW,
3968       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3969       .writefn = pmsav7_rgnr_write,
3970       .resetfn = arm_cp_reset_ignore },
3971 };
3972 
3973 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3974     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3975       .access = PL1_RW, .type = ARM_CP_ALIAS,
3976       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3977       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3978     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3979       .access = PL1_RW, .type = ARM_CP_ALIAS,
3980       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3981       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3982     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3983       .access = PL1_RW,
3984       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3985       .resetvalue = 0, },
3986     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3987       .access = PL1_RW,
3988       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3989       .resetvalue = 0, },
3990     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3991       .access = PL1_RW,
3992       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3993     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3994       .access = PL1_RW,
3995       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3996     /* Protection region base and size registers */
3997     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3998       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3999       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4000     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4001       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4002       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4003     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4004       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4005       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4006     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4007       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4008       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4009     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4010       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4011       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4012     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4013       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4014       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4015     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4016       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4017       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4018     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4019       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4020       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4021 };
4022 
4023 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4024                              uint64_t value)
4025 {
4026     ARMCPU *cpu = env_archcpu(env);
4027 
4028     if (!arm_feature(env, ARM_FEATURE_V8)) {
4029         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4030             /*
4031              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4032              * using Long-descriptor translation table format
4033              */
4034             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4035         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4036             /*
4037              * In an implementation that includes the Security Extensions
4038              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4039              * Short-descriptor translation table format.
4040              */
4041             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4042         } else {
4043             value &= TTBCR_N;
4044         }
4045     }
4046 
4047     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4048         /*
4049          * With LPAE the TTBCR could result in a change of ASID
4050          * via the TTBCR.A1 bit, so do a TLB flush.
4051          */
4052         tlb_flush(CPU(cpu));
4053     }
4054     raw_write(env, ri, value);
4055 }
4056 
4057 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4058                                uint64_t value)
4059 {
4060     ARMCPU *cpu = env_archcpu(env);
4061 
4062     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4063     tlb_flush(CPU(cpu));
4064     raw_write(env, ri, value);
4065 }
4066 
4067 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4068                             uint64_t value)
4069 {
4070     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4071     if (cpreg_field_is_64bit(ri) &&
4072         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4073         ARMCPU *cpu = env_archcpu(env);
4074         tlb_flush(CPU(cpu));
4075     }
4076     raw_write(env, ri, value);
4077 }
4078 
4079 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4080                                     uint64_t value)
4081 {
4082     /*
4083      * If we are running with E2&0 regime, then an ASID is active.
4084      * Flush if that might be changing.  Note we're not checking
4085      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4086      * holds the active ASID, only checking the field that might.
4087      */
4088     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4089         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4090         uint16_t mask = ARMMMUIdxBit_E20_2 |
4091                         ARMMMUIdxBit_E20_2_PAN |
4092                         ARMMMUIdxBit_E20_0;
4093         tlb_flush_by_mmuidx(env_cpu(env), mask);
4094     }
4095     raw_write(env, ri, value);
4096 }
4097 
4098 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4099                         uint64_t value)
4100 {
4101     ARMCPU *cpu = env_archcpu(env);
4102     CPUState *cs = CPU(cpu);
4103 
4104     /*
4105      * A change in VMID to the stage2 page table (Stage2) invalidates
4106      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4107      */
4108     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4109         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4110     }
4111     raw_write(env, ri, value);
4112 }
4113 
4114 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4115     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4116       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4117       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4118                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4119     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4120       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4121       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4122                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4123     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4124       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4125       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4126                              offsetof(CPUARMState, cp15.dfar_ns) } },
4127     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4128       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4129       .access = PL1_RW, .accessfn = access_tvm_trvm,
4130       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4131       .resetvalue = 0, },
4132 };
4133 
4134 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4135     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4136       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4137       .access = PL1_RW, .accessfn = access_tvm_trvm,
4138       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4139     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4140       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4141       .access = PL1_RW, .accessfn = access_tvm_trvm,
4142       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4143       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4144                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4145     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4146       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4147       .access = PL1_RW, .accessfn = access_tvm_trvm,
4148       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4149       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4150                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4151     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4152       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4153       .access = PL1_RW, .accessfn = access_tvm_trvm,
4154       .writefn = vmsa_tcr_el12_write,
4155       .raw_writefn = raw_write,
4156       .resetvalue = 0,
4157       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4158     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4159       .access = PL1_RW, .accessfn = access_tvm_trvm,
4160       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4161       .raw_writefn = raw_write,
4162       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4163                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4164 };
4165 
4166 /*
4167  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4168  * qemu tlbs nor adjusting cached masks.
4169  */
4170 static const ARMCPRegInfo ttbcr2_reginfo = {
4171     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4172     .access = PL1_RW, .accessfn = access_tvm_trvm,
4173     .type = ARM_CP_ALIAS,
4174     .bank_fieldoffsets = {
4175         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4176         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4177     },
4178 };
4179 
4180 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181                                 uint64_t value)
4182 {
4183     env->cp15.c15_ticonfig = value & 0xe7;
4184     /* The OS_TYPE bit in this register changes the reported CPUID! */
4185     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4186         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4187 }
4188 
4189 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4190                                 uint64_t value)
4191 {
4192     env->cp15.c15_threadid = value & 0xffff;
4193 }
4194 
4195 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4196                            uint64_t value)
4197 {
4198     /* Wait-for-interrupt (deprecated) */
4199     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4200 }
4201 
4202 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4203                                   uint64_t value)
4204 {
4205     /*
4206      * On OMAP there are registers indicating the max/min index of dcache lines
4207      * containing a dirty line; cache flush operations have to reset these.
4208      */
4209     env->cp15.c15_i_max = 0x000;
4210     env->cp15.c15_i_min = 0xff0;
4211 }
4212 
4213 static const ARMCPRegInfo omap_cp_reginfo[] = {
4214     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4215       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4216       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4217       .resetvalue = 0, },
4218     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4219       .access = PL1_RW, .type = ARM_CP_NOP },
4220     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4221       .access = PL1_RW,
4222       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4223       .writefn = omap_ticonfig_write },
4224     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4225       .access = PL1_RW,
4226       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4227     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4228       .access = PL1_RW, .resetvalue = 0xff0,
4229       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4230     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4231       .access = PL1_RW,
4232       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4233       .writefn = omap_threadid_write },
4234     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4235       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4236       .type = ARM_CP_NO_RAW,
4237       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4238     /*
4239      * TODO: Peripheral port remap register:
4240      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4241      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4242      * when MMU is off.
4243      */
4244     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4245       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4246       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4247       .writefn = omap_cachemaint_write },
4248     { .name = "C9", .cp = 15, .crn = 9,
4249       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4250       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4251 };
4252 
4253 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4254                               uint64_t value)
4255 {
4256     env->cp15.c15_cpar = value & 0x3fff;
4257 }
4258 
4259 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4260     { .name = "XSCALE_CPAR",
4261       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4262       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4263       .writefn = xscale_cpar_write, },
4264     { .name = "XSCALE_AUXCR",
4265       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4266       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4267       .resetvalue = 0, },
4268     /*
4269      * XScale specific cache-lockdown: since we have no cache we NOP these
4270      * and hope the guest does not really rely on cache behaviour.
4271      */
4272     { .name = "XSCALE_LOCK_ICACHE_LINE",
4273       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4274       .access = PL1_W, .type = ARM_CP_NOP },
4275     { .name = "XSCALE_UNLOCK_ICACHE",
4276       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4277       .access = PL1_W, .type = ARM_CP_NOP },
4278     { .name = "XSCALE_DCACHE_LOCK",
4279       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4280       .access = PL1_RW, .type = ARM_CP_NOP },
4281     { .name = "XSCALE_UNLOCK_DCACHE",
4282       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4283       .access = PL1_W, .type = ARM_CP_NOP },
4284 };
4285 
4286 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4287     /*
4288      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4289      * implementation of this implementation-defined space.
4290      * Ideally this should eventually disappear in favour of actually
4291      * implementing the correct behaviour for all cores.
4292      */
4293     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4294       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4295       .access = PL1_RW,
4296       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4297       .resetvalue = 0 },
4298 };
4299 
4300 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4301     /* Cache status: RAZ because we have no cache so it's always clean */
4302     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4303       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4304       .resetvalue = 0 },
4305 };
4306 
4307 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4308     /* We never have a block transfer operation in progress */
4309     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4310       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4311       .resetvalue = 0 },
4312     /* The cache ops themselves: these all NOP for QEMU */
4313     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4314       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4315     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4316       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4317     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4318       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4319     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4320       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4321     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4322       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4323     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4324       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4325 };
4326 
4327 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4328     /*
4329      * The cache test-and-clean instructions always return (1 << 30)
4330      * to indicate that there are no dirty cache lines.
4331      */
4332     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4333       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4334       .resetvalue = (1 << 30) },
4335     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4336       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4337       .resetvalue = (1 << 30) },
4338 };
4339 
4340 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4341     /* Ignore ReadBuffer accesses */
4342     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4343       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4344       .access = PL1_RW, .resetvalue = 0,
4345       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4346 };
4347 
4348 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4349 {
4350     unsigned int cur_el = arm_current_el(env);
4351 
4352     if (arm_is_el2_enabled(env) && cur_el == 1) {
4353         return env->cp15.vpidr_el2;
4354     }
4355     return raw_read(env, ri);
4356 }
4357 
4358 static uint64_t mpidr_read_val(CPUARMState *env)
4359 {
4360     ARMCPU *cpu = env_archcpu(env);
4361     uint64_t mpidr = cpu->mp_affinity;
4362 
4363     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4364         mpidr |= (1U << 31);
4365         /*
4366          * Cores which are uniprocessor (non-coherent)
4367          * but still implement the MP extensions set
4368          * bit 30. (For instance, Cortex-R5).
4369          */
4370         if (cpu->mp_is_up) {
4371             mpidr |= (1u << 30);
4372         }
4373     }
4374     return mpidr;
4375 }
4376 
4377 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4378 {
4379     unsigned int cur_el = arm_current_el(env);
4380 
4381     if (arm_is_el2_enabled(env) && cur_el == 1) {
4382         return env->cp15.vmpidr_el2;
4383     }
4384     return mpidr_read_val(env);
4385 }
4386 
4387 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4388     /* NOP AMAIR0/1 */
4389     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4390       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4391       .access = PL1_RW, .accessfn = access_tvm_trvm,
4392       .type = ARM_CP_CONST, .resetvalue = 0 },
4393     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4394     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4395       .access = PL1_RW, .accessfn = access_tvm_trvm,
4396       .type = ARM_CP_CONST, .resetvalue = 0 },
4397     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4398       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4399       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4400                              offsetof(CPUARMState, cp15.par_ns)} },
4401     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4402       .access = PL1_RW, .accessfn = access_tvm_trvm,
4403       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4404       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4405                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4406       .writefn = vmsa_ttbr_write, },
4407     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4408       .access = PL1_RW, .accessfn = access_tvm_trvm,
4409       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4410       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4411                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4412       .writefn = vmsa_ttbr_write, },
4413 };
4414 
4415 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4416 {
4417     return vfp_get_fpcr(env);
4418 }
4419 
4420 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421                             uint64_t value)
4422 {
4423     vfp_set_fpcr(env, value);
4424 }
4425 
4426 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4427 {
4428     return vfp_get_fpsr(env);
4429 }
4430 
4431 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4432                             uint64_t value)
4433 {
4434     vfp_set_fpsr(env, value);
4435 }
4436 
4437 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4438                                        bool isread)
4439 {
4440     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4441         return CP_ACCESS_TRAP;
4442     }
4443     return CP_ACCESS_OK;
4444 }
4445 
4446 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4447                             uint64_t value)
4448 {
4449     env->daif = value & PSTATE_DAIF;
4450 }
4451 
4452 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4453 {
4454     return env->pstate & PSTATE_PAN;
4455 }
4456 
4457 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4458                            uint64_t value)
4459 {
4460     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4461 }
4462 
4463 static const ARMCPRegInfo pan_reginfo = {
4464     .name = "PAN", .state = ARM_CP_STATE_AA64,
4465     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4466     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4467     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4468 };
4469 
4470 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4471 {
4472     return env->pstate & PSTATE_UAO;
4473 }
4474 
4475 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4476                            uint64_t value)
4477 {
4478     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4479 }
4480 
4481 static const ARMCPRegInfo uao_reginfo = {
4482     .name = "UAO", .state = ARM_CP_STATE_AA64,
4483     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4484     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4485     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4486 };
4487 
4488 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4489 {
4490     return env->pstate & PSTATE_DIT;
4491 }
4492 
4493 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4494                            uint64_t value)
4495 {
4496     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4497 }
4498 
4499 static const ARMCPRegInfo dit_reginfo = {
4500     .name = "DIT", .state = ARM_CP_STATE_AA64,
4501     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4502     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4503     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4504 };
4505 
4506 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4507 {
4508     return env->pstate & PSTATE_SSBS;
4509 }
4510 
4511 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4512                            uint64_t value)
4513 {
4514     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4515 }
4516 
4517 static const ARMCPRegInfo ssbs_reginfo = {
4518     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4519     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4520     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4521     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4522 };
4523 
4524 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4525                                               const ARMCPRegInfo *ri,
4526                                               bool isread)
4527 {
4528     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4529     switch (arm_current_el(env)) {
4530     case 0:
4531         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4532         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4533             return CP_ACCESS_TRAP;
4534         }
4535         /* fall through */
4536     case 1:
4537         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4538         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4539             return CP_ACCESS_TRAP_EL2;
4540         }
4541         break;
4542     }
4543     return CP_ACCESS_OK;
4544 }
4545 
4546 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4547 {
4548     /* Cache invalidate/clean to Point of Unification... */
4549     switch (arm_current_el(env)) {
4550     case 0:
4551         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4552         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4553             return CP_ACCESS_TRAP;
4554         }
4555         /* fall through */
4556     case 1:
4557         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4558         if (arm_hcr_el2_eff(env) & hcrflags) {
4559             return CP_ACCESS_TRAP_EL2;
4560         }
4561         break;
4562     }
4563     return CP_ACCESS_OK;
4564 }
4565 
4566 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4567                                    bool isread)
4568 {
4569     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4570 }
4571 
4572 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4573                                   bool isread)
4574 {
4575     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4576 }
4577 
4578 /*
4579  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4580  * Page D4-1736 (DDI0487A.b)
4581  */
4582 
4583 static int vae1_tlbmask(CPUARMState *env)
4584 {
4585     uint64_t hcr = arm_hcr_el2_eff(env);
4586     uint16_t mask;
4587 
4588     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4589         mask = ARMMMUIdxBit_E20_2 |
4590                ARMMMUIdxBit_E20_2_PAN |
4591                ARMMMUIdxBit_E20_0;
4592     } else {
4593         mask = ARMMMUIdxBit_E10_1 |
4594                ARMMMUIdxBit_E10_1_PAN |
4595                ARMMMUIdxBit_E10_0;
4596     }
4597     return mask;
4598 }
4599 
4600 /* Return 56 if TBI is enabled, 64 otherwise. */
4601 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4602                               uint64_t addr)
4603 {
4604     uint64_t tcr = regime_tcr(env, mmu_idx);
4605     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4606     int select = extract64(addr, 55, 1);
4607 
4608     return (tbi >> select) & 1 ? 56 : 64;
4609 }
4610 
4611 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4612 {
4613     uint64_t hcr = arm_hcr_el2_eff(env);
4614     ARMMMUIdx mmu_idx;
4615 
4616     /* Only the regime of the mmu_idx below is significant. */
4617     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4618         mmu_idx = ARMMMUIdx_E20_0;
4619     } else {
4620         mmu_idx = ARMMMUIdx_E10_0;
4621     }
4622 
4623     return tlbbits_for_regime(env, mmu_idx, addr);
4624 }
4625 
4626 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4627                                       uint64_t value)
4628 {
4629     CPUState *cs = env_cpu(env);
4630     int mask = vae1_tlbmask(env);
4631 
4632     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4633 }
4634 
4635 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4636                                     uint64_t value)
4637 {
4638     CPUState *cs = env_cpu(env);
4639     int mask = vae1_tlbmask(env);
4640 
4641     if (tlb_force_broadcast(env)) {
4642         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4643     } else {
4644         tlb_flush_by_mmuidx(cs, mask);
4645     }
4646 }
4647 
4648 static int e2_tlbmask(CPUARMState *env)
4649 {
4650     return (ARMMMUIdxBit_E20_0 |
4651             ARMMMUIdxBit_E20_2 |
4652             ARMMMUIdxBit_E20_2_PAN |
4653             ARMMMUIdxBit_E2);
4654 }
4655 
4656 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4657                                   uint64_t value)
4658 {
4659     CPUState *cs = env_cpu(env);
4660     int mask = alle1_tlbmask(env);
4661 
4662     tlb_flush_by_mmuidx(cs, mask);
4663 }
4664 
4665 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4666                                   uint64_t value)
4667 {
4668     CPUState *cs = env_cpu(env);
4669     int mask = e2_tlbmask(env);
4670 
4671     tlb_flush_by_mmuidx(cs, mask);
4672 }
4673 
4674 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4675                                   uint64_t value)
4676 {
4677     ARMCPU *cpu = env_archcpu(env);
4678     CPUState *cs = CPU(cpu);
4679 
4680     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4681 }
4682 
4683 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4684                                     uint64_t value)
4685 {
4686     CPUState *cs = env_cpu(env);
4687     int mask = alle1_tlbmask(env);
4688 
4689     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4690 }
4691 
4692 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4693                                     uint64_t value)
4694 {
4695     CPUState *cs = env_cpu(env);
4696     int mask = e2_tlbmask(env);
4697 
4698     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4699 }
4700 
4701 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4702                                     uint64_t value)
4703 {
4704     CPUState *cs = env_cpu(env);
4705 
4706     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4707 }
4708 
4709 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4710                                  uint64_t value)
4711 {
4712     /*
4713      * Invalidate by VA, EL2
4714      * Currently handles both VAE2 and VALE2, since we don't support
4715      * flush-last-level-only.
4716      */
4717     CPUState *cs = env_cpu(env);
4718     int mask = e2_tlbmask(env);
4719     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4720 
4721     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4722 }
4723 
4724 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4725                                  uint64_t value)
4726 {
4727     /*
4728      * Invalidate by VA, EL3
4729      * Currently handles both VAE3 and VALE3, since we don't support
4730      * flush-last-level-only.
4731      */
4732     ARMCPU *cpu = env_archcpu(env);
4733     CPUState *cs = CPU(cpu);
4734     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4735 
4736     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4737 }
4738 
4739 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4740                                    uint64_t value)
4741 {
4742     CPUState *cs = env_cpu(env);
4743     int mask = vae1_tlbmask(env);
4744     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4745     int bits = vae1_tlbbits(env, pageaddr);
4746 
4747     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4748 }
4749 
4750 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4751                                  uint64_t value)
4752 {
4753     /*
4754      * Invalidate by VA, EL1&0 (AArch64 version).
4755      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4756      * since we don't support flush-for-specific-ASID-only or
4757      * flush-last-level-only.
4758      */
4759     CPUState *cs = env_cpu(env);
4760     int mask = vae1_tlbmask(env);
4761     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4762     int bits = vae1_tlbbits(env, pageaddr);
4763 
4764     if (tlb_force_broadcast(env)) {
4765         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4766     } else {
4767         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4768     }
4769 }
4770 
4771 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4772                                    uint64_t value)
4773 {
4774     CPUState *cs = env_cpu(env);
4775     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4776     int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
4777 
4778     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4779                                                   ARMMMUIdxBit_E2, bits);
4780 }
4781 
4782 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4783                                    uint64_t value)
4784 {
4785     CPUState *cs = env_cpu(env);
4786     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4787     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4788 
4789     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4790                                                   ARMMMUIdxBit_E3, bits);
4791 }
4792 
4793 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4794 {
4795     /*
4796      * The MSB of value is the NS field, which only applies if SEL2
4797      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4798      */
4799     return (value >= 0
4800             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4801             && arm_is_secure_below_el3(env)
4802             ? ARMMMUIdxBit_Stage2_S
4803             : ARMMMUIdxBit_Stage2);
4804 }
4805 
4806 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4807                                     uint64_t value)
4808 {
4809     CPUState *cs = env_cpu(env);
4810     int mask = ipas2e1_tlbmask(env, value);
4811     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4812 
4813     if (tlb_force_broadcast(env)) {
4814         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4815     } else {
4816         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4817     }
4818 }
4819 
4820 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821                                       uint64_t value)
4822 {
4823     CPUState *cs = env_cpu(env);
4824     int mask = ipas2e1_tlbmask(env, value);
4825     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4826 
4827     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4828 }
4829 
4830 #ifdef TARGET_AARCH64
4831 typedef struct {
4832     uint64_t base;
4833     uint64_t length;
4834 } TLBIRange;
4835 
4836 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4837 {
4838     /*
4839      * Note that the TLBI range TG field encoding differs from both
4840      * TG0 and TG1 encodings.
4841      */
4842     switch (tg) {
4843     case 1:
4844         return Gran4K;
4845     case 2:
4846         return Gran16K;
4847     case 3:
4848         return Gran64K;
4849     default:
4850         return GranInvalid;
4851     }
4852 }
4853 
4854 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4855                                      uint64_t value)
4856 {
4857     unsigned int page_size_granule, page_shift, num, scale, exponent;
4858     /* Extract one bit to represent the va selector in use. */
4859     uint64_t select = sextract64(value, 36, 1);
4860     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4861     TLBIRange ret = { };
4862     ARMGranuleSize gran;
4863 
4864     page_size_granule = extract64(value, 46, 2);
4865     gran = tlbi_range_tg_to_gran_size(page_size_granule);
4866 
4867     /* The granule encoded in value must match the granule in use. */
4868     if (gran != param.gran) {
4869         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4870                       page_size_granule);
4871         return ret;
4872     }
4873 
4874     page_shift = arm_granule_bits(gran);
4875     num = extract64(value, 39, 5);
4876     scale = extract64(value, 44, 2);
4877     exponent = (5 * scale) + 1;
4878 
4879     ret.length = (num + 1) << (exponent + page_shift);
4880 
4881     if (param.select) {
4882         ret.base = sextract64(value, 0, 37);
4883     } else {
4884         ret.base = extract64(value, 0, 37);
4885     }
4886     if (param.ds) {
4887         /*
4888          * With DS=1, BaseADDR is always shifted 16 so that it is able
4889          * to address all 52 va bits.  The input address is perforce
4890          * aligned on a 64k boundary regardless of translation granule.
4891          */
4892         page_shift = 16;
4893     }
4894     ret.base <<= page_shift;
4895 
4896     return ret;
4897 }
4898 
4899 static void do_rvae_write(CPUARMState *env, uint64_t value,
4900                           int idxmap, bool synced)
4901 {
4902     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4903     TLBIRange range;
4904     int bits;
4905 
4906     range = tlbi_aa64_get_range(env, one_idx, value);
4907     bits = tlbbits_for_regime(env, one_idx, range.base);
4908 
4909     if (synced) {
4910         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4911                                                   range.base,
4912                                                   range.length,
4913                                                   idxmap,
4914                                                   bits);
4915     } else {
4916         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4917                                   range.length, idxmap, bits);
4918     }
4919 }
4920 
4921 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4922                                   const ARMCPRegInfo *ri,
4923                                   uint64_t value)
4924 {
4925     /*
4926      * Invalidate by VA range, EL1&0.
4927      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4928      * since we don't support flush-for-specific-ASID-only or
4929      * flush-last-level-only.
4930      */
4931 
4932     do_rvae_write(env, value, vae1_tlbmask(env),
4933                   tlb_force_broadcast(env));
4934 }
4935 
4936 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4937                                     const ARMCPRegInfo *ri,
4938                                     uint64_t value)
4939 {
4940     /*
4941      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4942      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4943      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4944      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4945      * shareable specific flushes.
4946      */
4947 
4948     do_rvae_write(env, value, vae1_tlbmask(env), true);
4949 }
4950 
4951 static int vae2_tlbmask(CPUARMState *env)
4952 {
4953     return ARMMMUIdxBit_E2;
4954 }
4955 
4956 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4957                                   const ARMCPRegInfo *ri,
4958                                   uint64_t value)
4959 {
4960     /*
4961      * Invalidate by VA range, EL2.
4962      * Currently handles all of RVAE2 and RVALE2,
4963      * since we don't support flush-for-specific-ASID-only or
4964      * flush-last-level-only.
4965      */
4966 
4967     do_rvae_write(env, value, vae2_tlbmask(env),
4968                   tlb_force_broadcast(env));
4969 
4970 
4971 }
4972 
4973 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4974                                     const ARMCPRegInfo *ri,
4975                                     uint64_t value)
4976 {
4977     /*
4978      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4979      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4980      * since we don't support flush-for-specific-ASID-only,
4981      * flush-last-level-only or inner/outer shareable specific flushes.
4982      */
4983 
4984     do_rvae_write(env, value, vae2_tlbmask(env), true);
4985 
4986 }
4987 
4988 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4989                                   const ARMCPRegInfo *ri,
4990                                   uint64_t value)
4991 {
4992     /*
4993      * Invalidate by VA range, EL3.
4994      * Currently handles all of RVAE3 and RVALE3,
4995      * since we don't support flush-for-specific-ASID-only or
4996      * flush-last-level-only.
4997      */
4998 
4999     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5000 }
5001 
5002 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5003                                     const ARMCPRegInfo *ri,
5004                                     uint64_t value)
5005 {
5006     /*
5007      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5008      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5009      * since we don't support flush-for-specific-ASID-only,
5010      * flush-last-level-only or inner/outer specific flushes.
5011      */
5012 
5013     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5014 }
5015 
5016 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5017                                      uint64_t value)
5018 {
5019     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5020                   tlb_force_broadcast(env));
5021 }
5022 
5023 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5024                                        const ARMCPRegInfo *ri,
5025                                        uint64_t value)
5026 {
5027     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5028 }
5029 #endif
5030 
5031 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5032                                       bool isread)
5033 {
5034     int cur_el = arm_current_el(env);
5035 
5036     if (cur_el < 2) {
5037         uint64_t hcr = arm_hcr_el2_eff(env);
5038 
5039         if (cur_el == 0) {
5040             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5041                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5042                     return CP_ACCESS_TRAP_EL2;
5043                 }
5044             } else {
5045                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5046                     return CP_ACCESS_TRAP;
5047                 }
5048                 if (hcr & HCR_TDZ) {
5049                     return CP_ACCESS_TRAP_EL2;
5050                 }
5051             }
5052         } else if (hcr & HCR_TDZ) {
5053             return CP_ACCESS_TRAP_EL2;
5054         }
5055     }
5056     return CP_ACCESS_OK;
5057 }
5058 
5059 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5060 {
5061     ARMCPU *cpu = env_archcpu(env);
5062     int dzp_bit = 1 << 4;
5063 
5064     /* DZP indicates whether DC ZVA access is allowed */
5065     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5066         dzp_bit = 0;
5067     }
5068     return cpu->dcz_blocksize | dzp_bit;
5069 }
5070 
5071 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5072                                     bool isread)
5073 {
5074     if (!(env->pstate & PSTATE_SP)) {
5075         /*
5076          * Access to SP_EL0 is undefined if it's being used as
5077          * the stack pointer.
5078          */
5079         return CP_ACCESS_TRAP_UNCATEGORIZED;
5080     }
5081     return CP_ACCESS_OK;
5082 }
5083 
5084 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5085 {
5086     return env->pstate & PSTATE_SP;
5087 }
5088 
5089 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5090 {
5091     update_spsel(env, val);
5092 }
5093 
5094 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5095                         uint64_t value)
5096 {
5097     ARMCPU *cpu = env_archcpu(env);
5098 
5099     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5100         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5101         value &= ~SCTLR_M;
5102     }
5103 
5104     /* ??? Lots of these bits are not implemented.  */
5105 
5106     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5107         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5108             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5109         } else {
5110             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5111                        SCTLR_ATA0 | SCTLR_ATA);
5112         }
5113     }
5114 
5115     if (raw_read(env, ri) == value) {
5116         /*
5117          * Skip the TLB flush if nothing actually changed; Linux likes
5118          * to do a lot of pointless SCTLR writes.
5119          */
5120         return;
5121     }
5122 
5123     raw_write(env, ri, value);
5124 
5125     /* This may enable/disable the MMU, so do a TLB flush.  */
5126     tlb_flush(CPU(cpu));
5127 
5128     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
5129         /*
5130          * Normally we would always end the TB on an SCTLR write; see the
5131          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5132          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5133          * of hflags from the translator, so do it here.
5134          */
5135         arm_rebuild_hflags(env);
5136     }
5137 }
5138 
5139 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5140                            uint64_t value)
5141 {
5142     /*
5143      * Some MDCR_EL3 bits affect whether PMU counters are running:
5144      * if we are trying to change any of those then we must
5145      * bracket this update with PMU start/finish calls.
5146      */
5147     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5148 
5149     if (pmu_op) {
5150         pmu_op_start(env);
5151     }
5152     env->cp15.mdcr_el3 = value;
5153     if (pmu_op) {
5154         pmu_op_finish(env);
5155     }
5156 }
5157 
5158 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5159                        uint64_t value)
5160 {
5161     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5162     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5163 }
5164 
5165 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5166                            uint64_t value)
5167 {
5168     /*
5169      * Some MDCR_EL2 bits affect whether PMU counters are running:
5170      * if we are trying to change any of those then we must
5171      * bracket this update with PMU start/finish calls.
5172      */
5173     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5174 
5175     if (pmu_op) {
5176         pmu_op_start(env);
5177     }
5178     env->cp15.mdcr_el2 = value;
5179     if (pmu_op) {
5180         pmu_op_finish(env);
5181     }
5182 }
5183 
5184 static const ARMCPRegInfo v8_cp_reginfo[] = {
5185     /*
5186      * Minimal set of EL0-visible registers. This will need to be expanded
5187      * significantly for system emulation of AArch64 CPUs.
5188      */
5189     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5190       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5191       .access = PL0_RW, .type = ARM_CP_NZCV },
5192     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5193       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5194       .type = ARM_CP_NO_RAW,
5195       .access = PL0_RW, .accessfn = aa64_daif_access,
5196       .fieldoffset = offsetof(CPUARMState, daif),
5197       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5198     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5199       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5200       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5201       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5202     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5203       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5204       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5205       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5206     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5207       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5208       .access = PL0_R, .type = ARM_CP_NO_RAW,
5209       .readfn = aa64_dczid_read },
5210     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5211       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5212       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5213 #ifndef CONFIG_USER_ONLY
5214       /* Avoid overhead of an access check that always passes in user-mode */
5215       .accessfn = aa64_zva_access,
5216 #endif
5217     },
5218     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5219       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5220       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5221     /* Cache ops: all NOPs since we don't emulate caches */
5222     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5223       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5224       .access = PL1_W, .type = ARM_CP_NOP,
5225       .accessfn = access_ticab },
5226     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5227       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5228       .access = PL1_W, .type = ARM_CP_NOP,
5229       .accessfn = access_tocu },
5230     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5231       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5232       .access = PL0_W, .type = ARM_CP_NOP,
5233       .accessfn = access_tocu },
5234     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5235       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5236       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5237       .type = ARM_CP_NOP },
5238     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5239       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5240       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5241     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5242       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5243       .access = PL0_W, .type = ARM_CP_NOP,
5244       .accessfn = aa64_cacheop_poc_access },
5245     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5246       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5247       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5248     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5249       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5250       .access = PL0_W, .type = ARM_CP_NOP,
5251       .accessfn = access_tocu },
5252     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5253       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5254       .access = PL0_W, .type = ARM_CP_NOP,
5255       .accessfn = aa64_cacheop_poc_access },
5256     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5257       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5258       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5259     /* TLBI operations */
5260     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5261       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5262       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5263       .writefn = tlbi_aa64_vmalle1is_write },
5264     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5265       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5266       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5267       .writefn = tlbi_aa64_vae1is_write },
5268     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5269       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5270       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5271       .writefn = tlbi_aa64_vmalle1is_write },
5272     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5273       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5274       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5275       .writefn = tlbi_aa64_vae1is_write },
5276     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5277       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5278       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5279       .writefn = tlbi_aa64_vae1is_write },
5280     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5281       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5282       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5283       .writefn = tlbi_aa64_vae1is_write },
5284     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5285       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5286       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5287       .writefn = tlbi_aa64_vmalle1_write },
5288     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5289       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5290       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5291       .writefn = tlbi_aa64_vae1_write },
5292     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5293       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5294       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5295       .writefn = tlbi_aa64_vmalle1_write },
5296     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5297       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5298       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5299       .writefn = tlbi_aa64_vae1_write },
5300     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5301       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5302       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5303       .writefn = tlbi_aa64_vae1_write },
5304     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5305       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5306       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5307       .writefn = tlbi_aa64_vae1_write },
5308     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5309       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5310       .access = PL2_W, .type = ARM_CP_NO_RAW,
5311       .writefn = tlbi_aa64_ipas2e1is_write },
5312     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5313       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5314       .access = PL2_W, .type = ARM_CP_NO_RAW,
5315       .writefn = tlbi_aa64_ipas2e1is_write },
5316     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5317       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5318       .access = PL2_W, .type = ARM_CP_NO_RAW,
5319       .writefn = tlbi_aa64_alle1is_write },
5320     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5321       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5322       .access = PL2_W, .type = ARM_CP_NO_RAW,
5323       .writefn = tlbi_aa64_alle1is_write },
5324     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5325       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5326       .access = PL2_W, .type = ARM_CP_NO_RAW,
5327       .writefn = tlbi_aa64_ipas2e1_write },
5328     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5329       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5330       .access = PL2_W, .type = ARM_CP_NO_RAW,
5331       .writefn = tlbi_aa64_ipas2e1_write },
5332     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5333       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5334       .access = PL2_W, .type = ARM_CP_NO_RAW,
5335       .writefn = tlbi_aa64_alle1_write },
5336     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5337       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5338       .access = PL2_W, .type = ARM_CP_NO_RAW,
5339       .writefn = tlbi_aa64_alle1is_write },
5340 #ifndef CONFIG_USER_ONLY
5341     /* 64 bit address translation operations */
5342     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5343       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5344       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5345       .writefn = ats_write64 },
5346     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5347       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5348       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5349       .writefn = ats_write64 },
5350     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5351       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5352       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5353       .writefn = ats_write64 },
5354     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5355       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5356       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5357       .writefn = ats_write64 },
5358     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5359       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5360       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5361       .writefn = ats_write64 },
5362     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5363       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5364       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5365       .writefn = ats_write64 },
5366     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5367       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5368       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5369       .writefn = ats_write64 },
5370     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5371       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5372       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5373       .writefn = ats_write64 },
5374     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5375     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5376       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5377       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5378       .writefn = ats_write64 },
5379     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5380       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5381       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5382       .writefn = ats_write64 },
5383     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5384       .type = ARM_CP_ALIAS,
5385       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5386       .access = PL1_RW, .resetvalue = 0,
5387       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5388       .writefn = par_write },
5389 #endif
5390     /* TLB invalidate last level of translation table walk */
5391     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5392       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5393       .writefn = tlbimva_is_write },
5394     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5395       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5396       .writefn = tlbimvaa_is_write },
5397     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5398       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5399       .writefn = tlbimva_write },
5400     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5401       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5402       .writefn = tlbimvaa_write },
5403     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5404       .type = ARM_CP_NO_RAW, .access = PL2_W,
5405       .writefn = tlbimva_hyp_write },
5406     { .name = "TLBIMVALHIS",
5407       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5408       .type = ARM_CP_NO_RAW, .access = PL2_W,
5409       .writefn = tlbimva_hyp_is_write },
5410     { .name = "TLBIIPAS2",
5411       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5412       .type = ARM_CP_NO_RAW, .access = PL2_W,
5413       .writefn = tlbiipas2_hyp_write },
5414     { .name = "TLBIIPAS2IS",
5415       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5416       .type = ARM_CP_NO_RAW, .access = PL2_W,
5417       .writefn = tlbiipas2is_hyp_write },
5418     { .name = "TLBIIPAS2L",
5419       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5420       .type = ARM_CP_NO_RAW, .access = PL2_W,
5421       .writefn = tlbiipas2_hyp_write },
5422     { .name = "TLBIIPAS2LIS",
5423       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5424       .type = ARM_CP_NO_RAW, .access = PL2_W,
5425       .writefn = tlbiipas2is_hyp_write },
5426     /* 32 bit cache operations */
5427     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5428       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5429     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5430       .type = ARM_CP_NOP, .access = PL1_W },
5431     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5432       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5433     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5434       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5435     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5436       .type = ARM_CP_NOP, .access = PL1_W },
5437     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5438       .type = ARM_CP_NOP, .access = PL1_W },
5439     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5440       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5441     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5442       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5443     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5444       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5445     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5446       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5447     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5448       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5449     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5450       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5451     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5452       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5453     /* MMU Domain access control / MPU write buffer control */
5454     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5455       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5456       .writefn = dacr_write, .raw_writefn = raw_write,
5457       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5458                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5459     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5460       .type = ARM_CP_ALIAS,
5461       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5462       .access = PL1_RW,
5463       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5464     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5465       .type = ARM_CP_ALIAS,
5466       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5467       .access = PL1_RW,
5468       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5469     /*
5470      * We rely on the access checks not allowing the guest to write to the
5471      * state field when SPSel indicates that it's being used as the stack
5472      * pointer.
5473      */
5474     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5475       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5476       .access = PL1_RW, .accessfn = sp_el0_access,
5477       .type = ARM_CP_ALIAS,
5478       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5479     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5481       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5482       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5483     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5484       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5485       .type = ARM_CP_NO_RAW,
5486       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5487     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5488       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5489       .access = PL2_RW,
5490       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5491       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5492     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5494       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5495       .writefn = dacr_write, .raw_writefn = raw_write,
5496       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5497     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5498       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5499       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5500       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5501     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5502       .type = ARM_CP_ALIAS,
5503       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5504       .access = PL2_RW,
5505       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5506     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5507       .type = ARM_CP_ALIAS,
5508       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5509       .access = PL2_RW,
5510       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5511     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5512       .type = ARM_CP_ALIAS,
5513       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5514       .access = PL2_RW,
5515       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5516     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5517       .type = ARM_CP_ALIAS,
5518       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5519       .access = PL2_RW,
5520       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5521     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5522       .type = ARM_CP_IO,
5523       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5524       .resetvalue = 0,
5525       .access = PL3_RW,
5526       .writefn = mdcr_el3_write,
5527       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5528     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5529       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5530       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5531       .writefn = sdcr_write,
5532       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5533 };
5534 
5535 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5536 {
5537     ARMCPU *cpu = env_archcpu(env);
5538 
5539     if (arm_feature(env, ARM_FEATURE_V8)) {
5540         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5541     } else {
5542         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5543     }
5544 
5545     if (arm_feature(env, ARM_FEATURE_EL3)) {
5546         valid_mask &= ~HCR_HCD;
5547     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5548         /*
5549          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5550          * However, if we're using the SMC PSCI conduit then QEMU is
5551          * effectively acting like EL3 firmware and so the guest at
5552          * EL2 should retain the ability to prevent EL1 from being
5553          * able to make SMC calls into the ersatz firmware, so in
5554          * that case HCR.TSC should be read/write.
5555          */
5556         valid_mask &= ~HCR_TSC;
5557     }
5558 
5559     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5560         if (cpu_isar_feature(aa64_vh, cpu)) {
5561             valid_mask |= HCR_E2H;
5562         }
5563         if (cpu_isar_feature(aa64_ras, cpu)) {
5564             valid_mask |= HCR_TERR | HCR_TEA;
5565         }
5566         if (cpu_isar_feature(aa64_lor, cpu)) {
5567             valid_mask |= HCR_TLOR;
5568         }
5569         if (cpu_isar_feature(aa64_pauth, cpu)) {
5570             valid_mask |= HCR_API | HCR_APK;
5571         }
5572         if (cpu_isar_feature(aa64_mte, cpu)) {
5573             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5574         }
5575         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5576             valid_mask |= HCR_ENSCXT;
5577         }
5578         if (cpu_isar_feature(aa64_fwb, cpu)) {
5579             valid_mask |= HCR_FWB;
5580         }
5581     }
5582 
5583     if (cpu_isar_feature(any_evt, cpu)) {
5584         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5585     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5586         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5587     }
5588 
5589     /* Clear RES0 bits.  */
5590     value &= valid_mask;
5591 
5592     /*
5593      * These bits change the MMU setup:
5594      * HCR_VM enables stage 2 translation
5595      * HCR_PTW forbids certain page-table setups
5596      * HCR_DC disables stage1 and enables stage2 translation
5597      * HCR_DCT enables tagging on (disabled) stage1 translation
5598      * HCR_FWB changes the interpretation of stage2 descriptor bits
5599      */
5600     if ((env->cp15.hcr_el2 ^ value) &
5601         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5602         tlb_flush(CPU(cpu));
5603     }
5604     env->cp15.hcr_el2 = value;
5605 
5606     /*
5607      * Updates to VI and VF require us to update the status of
5608      * virtual interrupts, which are the logical OR of these bits
5609      * and the state of the input lines from the GIC. (This requires
5610      * that we have the iothread lock, which is done by marking the
5611      * reginfo structs as ARM_CP_IO.)
5612      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5613      * possible for it to be taken immediately, because VIRQ and
5614      * VFIQ are masked unless running at EL0 or EL1, and HCR
5615      * can only be written at EL2.
5616      */
5617     g_assert(qemu_mutex_iothread_locked());
5618     arm_cpu_update_virq(cpu);
5619     arm_cpu_update_vfiq(cpu);
5620     arm_cpu_update_vserr(cpu);
5621 }
5622 
5623 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5624 {
5625     do_hcr_write(env, value, 0);
5626 }
5627 
5628 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5629                           uint64_t value)
5630 {
5631     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5632     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5633     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5634 }
5635 
5636 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5637                          uint64_t value)
5638 {
5639     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5640     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5641     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5642 }
5643 
5644 /*
5645  * Return the effective value of HCR_EL2, at the given security state.
5646  * Bits that are not included here:
5647  * RW       (read from SCR_EL3.RW as needed)
5648  */
5649 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
5650 {
5651     uint64_t ret = env->cp15.hcr_el2;
5652 
5653     if (!arm_is_el2_enabled_secstate(env, secure)) {
5654         /*
5655          * "This register has no effect if EL2 is not enabled in the
5656          * current Security state".  This is ARMv8.4-SecEL2 speak for
5657          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5658          *
5659          * Prior to that, the language was "In an implementation that
5660          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5661          * as if this field is 0 for all purposes other than a direct
5662          * read or write access of HCR_EL2".  With lots of enumeration
5663          * on a per-field basis.  In current QEMU, this is condition
5664          * is arm_is_secure_below_el3.
5665          *
5666          * Since the v8.4 language applies to the entire register, and
5667          * appears to be backward compatible, use that.
5668          */
5669         return 0;
5670     }
5671 
5672     /*
5673      * For a cpu that supports both aarch64 and aarch32, we can set bits
5674      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5675      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5676      */
5677     if (!arm_el_is_aa64(env, 2)) {
5678         uint64_t aa32_valid;
5679 
5680         /*
5681          * These bits are up-to-date as of ARMv8.6.
5682          * For HCR, it's easiest to list just the 2 bits that are invalid.
5683          * For HCR2, list those that are valid.
5684          */
5685         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5686         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5687                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5688         ret &= aa32_valid;
5689     }
5690 
5691     if (ret & HCR_TGE) {
5692         /* These bits are up-to-date as of ARMv8.6.  */
5693         if (ret & HCR_E2H) {
5694             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5695                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5696                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5697                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5698                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5699                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5700         } else {
5701             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5702         }
5703         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5704                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5705                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5706                  HCR_TLOR);
5707     }
5708 
5709     return ret;
5710 }
5711 
5712 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5713 {
5714     return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5715 }
5716 
5717 /*
5718  * Corresponds to ARM pseudocode function ELIsInHost().
5719  */
5720 bool el_is_in_host(CPUARMState *env, int el)
5721 {
5722     uint64_t mask;
5723 
5724     /*
5725      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5726      * Perform the simplest bit tests first, and validate EL2 afterward.
5727      */
5728     if (el & 1) {
5729         return false; /* EL1 or EL3 */
5730     }
5731 
5732     /*
5733      * Note that hcr_write() checks isar_feature_aa64_vh(),
5734      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5735      */
5736     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5737     if ((env->cp15.hcr_el2 & mask) != mask) {
5738         return false;
5739     }
5740 
5741     /* TGE and/or E2H set: double check those bits are currently legal. */
5742     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5743 }
5744 
5745 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5746                        uint64_t value)
5747 {
5748     uint64_t valid_mask = 0;
5749 
5750     /* No features adding bits to HCRX are implemented. */
5751 
5752     /* Clear RES0 bits.  */
5753     env->cp15.hcrx_el2 = value & valid_mask;
5754 }
5755 
5756 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5757                                   bool isread)
5758 {
5759     if (arm_current_el(env) < 3
5760         && arm_feature(env, ARM_FEATURE_EL3)
5761         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5762         return CP_ACCESS_TRAP_EL3;
5763     }
5764     return CP_ACCESS_OK;
5765 }
5766 
5767 static const ARMCPRegInfo hcrx_el2_reginfo = {
5768     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5769     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5770     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5771     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5772 };
5773 
5774 /* Return the effective value of HCRX_EL2.  */
5775 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5776 {
5777     /*
5778      * The bits in this register behave as 0 for all purposes other than
5779      * direct reads of the register if:
5780      *   - EL2 is not enabled in the current security state,
5781      *   - SCR_EL3.HXEn is 0.
5782      */
5783     if (!arm_is_el2_enabled(env)
5784         || (arm_feature(env, ARM_FEATURE_EL3)
5785             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5786         return 0;
5787     }
5788     return env->cp15.hcrx_el2;
5789 }
5790 
5791 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5792                            uint64_t value)
5793 {
5794     /*
5795      * For A-profile AArch32 EL3, if NSACR.CP10
5796      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5797      */
5798     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5799         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5800         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5801         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5802     }
5803     env->cp15.cptr_el[2] = value;
5804 }
5805 
5806 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5807 {
5808     /*
5809      * For A-profile AArch32 EL3, if NSACR.CP10
5810      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5811      */
5812     uint64_t value = env->cp15.cptr_el[2];
5813 
5814     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5815         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5816         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5817     }
5818     return value;
5819 }
5820 
5821 static const ARMCPRegInfo el2_cp_reginfo[] = {
5822     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5823       .type = ARM_CP_IO,
5824       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5825       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5826       .writefn = hcr_write },
5827     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5828       .type = ARM_CP_ALIAS | ARM_CP_IO,
5829       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5830       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5831       .writefn = hcr_writelow },
5832     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5833       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5834       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5835     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5836       .type = ARM_CP_ALIAS,
5837       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5838       .access = PL2_RW,
5839       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5840     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5841       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5842       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5843     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5844       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5845       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5846     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5847       .type = ARM_CP_ALIAS,
5848       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5849       .access = PL2_RW,
5850       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5851     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5852       .type = ARM_CP_ALIAS,
5853       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5854       .access = PL2_RW,
5855       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5856     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5857       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5858       .access = PL2_RW, .writefn = vbar_write,
5859       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5860       .resetvalue = 0 },
5861     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5862       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5863       .access = PL3_RW, .type = ARM_CP_ALIAS,
5864       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5865     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5866       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5867       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5868       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5869       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5870     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5871       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5872       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5873       .resetvalue = 0 },
5874     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5875       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5876       .access = PL2_RW, .type = ARM_CP_ALIAS,
5877       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5878     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5879       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5880       .access = PL2_RW, .type = ARM_CP_CONST,
5881       .resetvalue = 0 },
5882     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5883     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5884       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5885       .access = PL2_RW, .type = ARM_CP_CONST,
5886       .resetvalue = 0 },
5887     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5888       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5889       .access = PL2_RW, .type = ARM_CP_CONST,
5890       .resetvalue = 0 },
5891     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5892       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5893       .access = PL2_RW, .type = ARM_CP_CONST,
5894       .resetvalue = 0 },
5895     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5896       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5897       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5898       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5899     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5900       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5901       .type = ARM_CP_ALIAS,
5902       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5903       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5904     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5905       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5906       .access = PL2_RW,
5907       /* no .writefn needed as this can't cause an ASID change */
5908       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5909     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5910       .cp = 15, .opc1 = 6, .crm = 2,
5911       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5912       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5913       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5914       .writefn = vttbr_write },
5915     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5916       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5917       .access = PL2_RW, .writefn = vttbr_write,
5918       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5919     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5920       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5921       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5922       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5923     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5924       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5925       .access = PL2_RW, .resetvalue = 0,
5926       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5927     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5928       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5929       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5930       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5931     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5932       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5933       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5934     { .name = "TLBIALLNSNH",
5935       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5936       .type = ARM_CP_NO_RAW, .access = PL2_W,
5937       .writefn = tlbiall_nsnh_write },
5938     { .name = "TLBIALLNSNHIS",
5939       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5940       .type = ARM_CP_NO_RAW, .access = PL2_W,
5941       .writefn = tlbiall_nsnh_is_write },
5942     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5943       .type = ARM_CP_NO_RAW, .access = PL2_W,
5944       .writefn = tlbiall_hyp_write },
5945     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5946       .type = ARM_CP_NO_RAW, .access = PL2_W,
5947       .writefn = tlbiall_hyp_is_write },
5948     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5949       .type = ARM_CP_NO_RAW, .access = PL2_W,
5950       .writefn = tlbimva_hyp_write },
5951     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5952       .type = ARM_CP_NO_RAW, .access = PL2_W,
5953       .writefn = tlbimva_hyp_is_write },
5954     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5955       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5956       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5957       .writefn = tlbi_aa64_alle2_write },
5958     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5959       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5960       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5961       .writefn = tlbi_aa64_vae2_write },
5962     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5963       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5964       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5965       .writefn = tlbi_aa64_vae2_write },
5966     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5967       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5968       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5969       .writefn = tlbi_aa64_alle2is_write },
5970     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5971       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5972       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5973       .writefn = tlbi_aa64_vae2is_write },
5974     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5975       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5976       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5977       .writefn = tlbi_aa64_vae2is_write },
5978 #ifndef CONFIG_USER_ONLY
5979     /*
5980      * Unlike the other EL2-related AT operations, these must
5981      * UNDEF from EL3 if EL2 is not implemented, which is why we
5982      * define them here rather than with the rest of the AT ops.
5983      */
5984     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5985       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5986       .access = PL2_W, .accessfn = at_s1e2_access,
5987       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5988       .writefn = ats_write64 },
5989     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5990       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5991       .access = PL2_W, .accessfn = at_s1e2_access,
5992       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5993       .writefn = ats_write64 },
5994     /*
5995      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5996      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5997      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5998      * to behave as if SCR.NS was 1.
5999      */
6000     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6001       .access = PL2_W,
6002       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6003     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6004       .access = PL2_W,
6005       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6006     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6007       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6008       /*
6009        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6010        * reset values as IMPDEF. We choose to reset to 3 to comply with
6011        * both ARMv7 and ARMv8.
6012        */
6013       .access = PL2_RW, .resetvalue = 3,
6014       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6015     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6016       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6017       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6018       .writefn = gt_cntvoff_write,
6019       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6020     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6021       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6022       .writefn = gt_cntvoff_write,
6023       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6024     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6025       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6026       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6027       .type = ARM_CP_IO, .access = PL2_RW,
6028       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6029     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6030       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6031       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6032       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6033     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6034       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6035       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6036       .resetfn = gt_hyp_timer_reset,
6037       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6038     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6039       .type = ARM_CP_IO,
6040       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6041       .access = PL2_RW,
6042       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6043       .resetvalue = 0,
6044       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6045 #endif
6046     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6047       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6048       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6049       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6050     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6051       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6052       .access = PL2_RW,
6053       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6054     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6055       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6056       .access = PL2_RW,
6057       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6058 };
6059 
6060 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6061     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6062       .type = ARM_CP_ALIAS | ARM_CP_IO,
6063       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6064       .access = PL2_RW,
6065       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6066       .writefn = hcr_writehigh },
6067 };
6068 
6069 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6070                                   bool isread)
6071 {
6072     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6073         return CP_ACCESS_OK;
6074     }
6075     return CP_ACCESS_TRAP_UNCATEGORIZED;
6076 }
6077 
6078 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6079     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6080       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6081       .access = PL2_RW, .accessfn = sel2_access,
6082       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6083     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6084       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6085       .access = PL2_RW, .accessfn = sel2_access,
6086       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6087 };
6088 
6089 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6090                                    bool isread)
6091 {
6092     /*
6093      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6094      * At Secure EL1 it traps to EL3 or EL2.
6095      */
6096     if (arm_current_el(env) == 3) {
6097         return CP_ACCESS_OK;
6098     }
6099     if (arm_is_secure_below_el3(env)) {
6100         if (env->cp15.scr_el3 & SCR_EEL2) {
6101             return CP_ACCESS_TRAP_EL2;
6102         }
6103         return CP_ACCESS_TRAP_EL3;
6104     }
6105     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6106     if (isread) {
6107         return CP_ACCESS_OK;
6108     }
6109     return CP_ACCESS_TRAP_UNCATEGORIZED;
6110 }
6111 
6112 static const ARMCPRegInfo el3_cp_reginfo[] = {
6113     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6114       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6115       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6116       .resetfn = scr_reset, .writefn = scr_write },
6117     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6118       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6119       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6120       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6121       .writefn = scr_write },
6122     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6123       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6124       .access = PL3_RW, .resetvalue = 0,
6125       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6126     { .name = "SDER",
6127       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6128       .access = PL3_RW, .resetvalue = 0,
6129       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6130     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6131       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6132       .writefn = vbar_write, .resetvalue = 0,
6133       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6134     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6135       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6136       .access = PL3_RW, .resetvalue = 0,
6137       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6138     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6139       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6140       .access = PL3_RW,
6141       /* no .writefn needed as this can't cause an ASID change */
6142       .resetvalue = 0,
6143       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6144     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6145       .type = ARM_CP_ALIAS,
6146       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6147       .access = PL3_RW,
6148       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6149     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6150       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6151       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6152     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6153       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6154       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6155     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6156       .type = ARM_CP_ALIAS,
6157       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6158       .access = PL3_RW,
6159       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6160     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6161       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6162       .access = PL3_RW, .writefn = vbar_write,
6163       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6164       .resetvalue = 0 },
6165     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6166       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6167       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6168       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6169     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6170       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6171       .access = PL3_RW, .resetvalue = 0,
6172       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6173     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6174       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6175       .access = PL3_RW, .type = ARM_CP_CONST,
6176       .resetvalue = 0 },
6177     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6178       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6179       .access = PL3_RW, .type = ARM_CP_CONST,
6180       .resetvalue = 0 },
6181     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6182       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6183       .access = PL3_RW, .type = ARM_CP_CONST,
6184       .resetvalue = 0 },
6185     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6186       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6187       .access = PL3_W, .type = ARM_CP_NO_RAW,
6188       .writefn = tlbi_aa64_alle3is_write },
6189     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6190       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6191       .access = PL3_W, .type = ARM_CP_NO_RAW,
6192       .writefn = tlbi_aa64_vae3is_write },
6193     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6194       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6195       .access = PL3_W, .type = ARM_CP_NO_RAW,
6196       .writefn = tlbi_aa64_vae3is_write },
6197     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6198       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6199       .access = PL3_W, .type = ARM_CP_NO_RAW,
6200       .writefn = tlbi_aa64_alle3_write },
6201     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6202       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6203       .access = PL3_W, .type = ARM_CP_NO_RAW,
6204       .writefn = tlbi_aa64_vae3_write },
6205     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6206       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6207       .access = PL3_W, .type = ARM_CP_NO_RAW,
6208       .writefn = tlbi_aa64_vae3_write },
6209 };
6210 
6211 #ifndef CONFIG_USER_ONLY
6212 /* Test if system register redirection is to occur in the current state.  */
6213 static bool redirect_for_e2h(CPUARMState *env)
6214 {
6215     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6216 }
6217 
6218 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6219 {
6220     CPReadFn *readfn;
6221 
6222     if (redirect_for_e2h(env)) {
6223         /* Switch to the saved EL2 version of the register.  */
6224         ri = ri->opaque;
6225         readfn = ri->readfn;
6226     } else {
6227         readfn = ri->orig_readfn;
6228     }
6229     if (readfn == NULL) {
6230         readfn = raw_read;
6231     }
6232     return readfn(env, ri);
6233 }
6234 
6235 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6236                           uint64_t value)
6237 {
6238     CPWriteFn *writefn;
6239 
6240     if (redirect_for_e2h(env)) {
6241         /* Switch to the saved EL2 version of the register.  */
6242         ri = ri->opaque;
6243         writefn = ri->writefn;
6244     } else {
6245         writefn = ri->orig_writefn;
6246     }
6247     if (writefn == NULL) {
6248         writefn = raw_write;
6249     }
6250     writefn(env, ri, value);
6251 }
6252 
6253 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6254 {
6255     struct E2HAlias {
6256         uint32_t src_key, dst_key, new_key;
6257         const char *src_name, *dst_name, *new_name;
6258         bool (*feature)(const ARMISARegisters *id);
6259     };
6260 
6261 #define K(op0, op1, crn, crm, op2) \
6262     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6263 
6264     static const struct E2HAlias aliases[] = {
6265         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6266           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6267         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6268           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6269         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6270           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6271         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6272           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6273         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6274           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6275         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6276           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6277         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6278           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6279         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6280           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6281         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6282           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6283         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6284           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6285         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6286           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6287         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6288           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6289         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6290           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6291         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6292           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6293         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6294           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6295         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6296           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6297 
6298         /*
6299          * Note that redirection of ZCR is mentioned in the description
6300          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6301          * not in the summary table.
6302          */
6303         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6304           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6305         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6306           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6307 
6308         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6309           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6310 
6311         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6312           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6313           isar_feature_aa64_scxtnum },
6314 
6315         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6316         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6317     };
6318 #undef K
6319 
6320     size_t i;
6321 
6322     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6323         const struct E2HAlias *a = &aliases[i];
6324         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6325         bool ok;
6326 
6327         if (a->feature && !a->feature(&cpu->isar)) {
6328             continue;
6329         }
6330 
6331         src_reg = g_hash_table_lookup(cpu->cp_regs,
6332                                       (gpointer)(uintptr_t)a->src_key);
6333         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6334                                       (gpointer)(uintptr_t)a->dst_key);
6335         g_assert(src_reg != NULL);
6336         g_assert(dst_reg != NULL);
6337 
6338         /* Cross-compare names to detect typos in the keys.  */
6339         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6340         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6341 
6342         /* None of the core system registers use opaque; we will.  */
6343         g_assert(src_reg->opaque == NULL);
6344 
6345         /* Create alias before redirection so we dup the right data. */
6346         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6347 
6348         new_reg->name = a->new_name;
6349         new_reg->type |= ARM_CP_ALIAS;
6350         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6351         new_reg->access &= PL2_RW | PL3_RW;
6352 
6353         ok = g_hash_table_insert(cpu->cp_regs,
6354                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6355         g_assert(ok);
6356 
6357         src_reg->opaque = dst_reg;
6358         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6359         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6360         if (!src_reg->raw_readfn) {
6361             src_reg->raw_readfn = raw_read;
6362         }
6363         if (!src_reg->raw_writefn) {
6364             src_reg->raw_writefn = raw_write;
6365         }
6366         src_reg->readfn = el2_e2h_read;
6367         src_reg->writefn = el2_e2h_write;
6368     }
6369 }
6370 #endif
6371 
6372 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6373                                      bool isread)
6374 {
6375     int cur_el = arm_current_el(env);
6376 
6377     if (cur_el < 2) {
6378         uint64_t hcr = arm_hcr_el2_eff(env);
6379 
6380         if (cur_el == 0) {
6381             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6382                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6383                     return CP_ACCESS_TRAP_EL2;
6384                 }
6385             } else {
6386                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6387                     return CP_ACCESS_TRAP;
6388                 }
6389                 if (hcr & HCR_TID2) {
6390                     return CP_ACCESS_TRAP_EL2;
6391                 }
6392             }
6393         } else if (hcr & HCR_TID2) {
6394             return CP_ACCESS_TRAP_EL2;
6395         }
6396     }
6397 
6398     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6399         return CP_ACCESS_TRAP_EL2;
6400     }
6401 
6402     return CP_ACCESS_OK;
6403 }
6404 
6405 /*
6406  * Check for traps to RAS registers, which are controlled
6407  * by HCR_EL2.TERR and SCR_EL3.TERR.
6408  */
6409 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6410                                   bool isread)
6411 {
6412     int el = arm_current_el(env);
6413 
6414     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6415         return CP_ACCESS_TRAP_EL2;
6416     }
6417     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6418         return CP_ACCESS_TRAP_EL3;
6419     }
6420     return CP_ACCESS_OK;
6421 }
6422 
6423 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6424 {
6425     int el = arm_current_el(env);
6426 
6427     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6428         return env->cp15.vdisr_el2;
6429     }
6430     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6431         return 0; /* RAZ/WI */
6432     }
6433     return env->cp15.disr_el1;
6434 }
6435 
6436 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6437 {
6438     int el = arm_current_el(env);
6439 
6440     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6441         env->cp15.vdisr_el2 = val;
6442         return;
6443     }
6444     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6445         return; /* RAZ/WI */
6446     }
6447     env->cp15.disr_el1 = val;
6448 }
6449 
6450 /*
6451  * Minimal RAS implementation with no Error Records.
6452  * Which means that all of the Error Record registers:
6453  *   ERXADDR_EL1
6454  *   ERXCTLR_EL1
6455  *   ERXFR_EL1
6456  *   ERXMISC0_EL1
6457  *   ERXMISC1_EL1
6458  *   ERXMISC2_EL1
6459  *   ERXMISC3_EL1
6460  *   ERXPFGCDN_EL1  (RASv1p1)
6461  *   ERXPFGCTL_EL1  (RASv1p1)
6462  *   ERXPFGF_EL1    (RASv1p1)
6463  *   ERXSTATUS_EL1
6464  * and
6465  *   ERRSELR_EL1
6466  * may generate UNDEFINED, which is the effect we get by not
6467  * listing them at all.
6468  */
6469 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6470     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6471       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6472       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6473       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6474     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6475       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6476       .access = PL1_R, .accessfn = access_terr,
6477       .type = ARM_CP_CONST, .resetvalue = 0 },
6478     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6479       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6480       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6481     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6482       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6483       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6484 };
6485 
6486 /*
6487  * Return the exception level to which exceptions should be taken
6488  * via SVEAccessTrap.  This excludes the check for whether the exception
6489  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6490  * be found by testing 0 < fp_exception_el < sve_exception_el.
6491  *
6492  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6493  * pseudocode does *not* separate out the FP trap checks, but has them
6494  * all in one function.
6495  */
6496 int sve_exception_el(CPUARMState *env, int el)
6497 {
6498 #ifndef CONFIG_USER_ONLY
6499     if (el <= 1 && !el_is_in_host(env, el)) {
6500         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6501         case 1:
6502             if (el != 0) {
6503                 break;
6504             }
6505             /* fall through */
6506         case 0:
6507         case 2:
6508             return 1;
6509         }
6510     }
6511 
6512     if (el <= 2 && arm_is_el2_enabled(env)) {
6513         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6514         if (env->cp15.hcr_el2 & HCR_E2H) {
6515             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6516             case 1:
6517                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6518                     break;
6519                 }
6520                 /* fall through */
6521             case 0:
6522             case 2:
6523                 return 2;
6524             }
6525         } else {
6526             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6527                 return 2;
6528             }
6529         }
6530     }
6531 
6532     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6533     if (arm_feature(env, ARM_FEATURE_EL3)
6534         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6535         return 3;
6536     }
6537 #endif
6538     return 0;
6539 }
6540 
6541 /*
6542  * Return the exception level to which exceptions should be taken for SME.
6543  * C.f. the ARM pseudocode function CheckSMEAccess.
6544  */
6545 int sme_exception_el(CPUARMState *env, int el)
6546 {
6547 #ifndef CONFIG_USER_ONLY
6548     if (el <= 1 && !el_is_in_host(env, el)) {
6549         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6550         case 1:
6551             if (el != 0) {
6552                 break;
6553             }
6554             /* fall through */
6555         case 0:
6556         case 2:
6557             return 1;
6558         }
6559     }
6560 
6561     if (el <= 2 && arm_is_el2_enabled(env)) {
6562         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6563         if (env->cp15.hcr_el2 & HCR_E2H) {
6564             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6565             case 1:
6566                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6567                     break;
6568                 }
6569                 /* fall through */
6570             case 0:
6571             case 2:
6572                 return 2;
6573             }
6574         } else {
6575             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6576                 return 2;
6577             }
6578         }
6579     }
6580 
6581     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6582     if (arm_feature(env, ARM_FEATURE_EL3)
6583         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6584         return 3;
6585     }
6586 #endif
6587     return 0;
6588 }
6589 
6590 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6591 static bool sme_fa64(CPUARMState *env, int el)
6592 {
6593     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6594         return false;
6595     }
6596 
6597     if (el <= 1 && !el_is_in_host(env, el)) {
6598         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6599             return false;
6600         }
6601     }
6602     if (el <= 2 && arm_is_el2_enabled(env)) {
6603         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6604             return false;
6605         }
6606     }
6607     if (arm_feature(env, ARM_FEATURE_EL3)) {
6608         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6609             return false;
6610         }
6611     }
6612 
6613     return true;
6614 }
6615 
6616 /*
6617  * Given that SVE is enabled, return the vector length for EL.
6618  */
6619 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6620 {
6621     ARMCPU *cpu = env_archcpu(env);
6622     uint64_t *cr = env->vfp.zcr_el;
6623     uint32_t map = cpu->sve_vq.map;
6624     uint32_t len = ARM_MAX_VQ - 1;
6625 
6626     if (sm) {
6627         cr = env->vfp.smcr_el;
6628         map = cpu->sme_vq.map;
6629     }
6630 
6631     if (el <= 1 && !el_is_in_host(env, el)) {
6632         len = MIN(len, 0xf & (uint32_t)cr[1]);
6633     }
6634     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6635         len = MIN(len, 0xf & (uint32_t)cr[2]);
6636     }
6637     if (arm_feature(env, ARM_FEATURE_EL3)) {
6638         len = MIN(len, 0xf & (uint32_t)cr[3]);
6639     }
6640 
6641     map &= MAKE_64BIT_MASK(0, len + 1);
6642     if (map != 0) {
6643         return 31 - clz32(map);
6644     }
6645 
6646     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6647     assert(sm);
6648     return ctz32(cpu->sme_vq.map);
6649 }
6650 
6651 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6652 {
6653     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6654 }
6655 
6656 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6657                       uint64_t value)
6658 {
6659     int cur_el = arm_current_el(env);
6660     int old_len = sve_vqm1_for_el(env, cur_el);
6661     int new_len;
6662 
6663     /* Bits other than [3:0] are RAZ/WI.  */
6664     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6665     raw_write(env, ri, value & 0xf);
6666 
6667     /*
6668      * Because we arrived here, we know both FP and SVE are enabled;
6669      * otherwise we would have trapped access to the ZCR_ELn register.
6670      */
6671     new_len = sve_vqm1_for_el(env, cur_el);
6672     if (new_len < old_len) {
6673         aarch64_sve_narrow_vq(env, new_len + 1);
6674     }
6675 }
6676 
6677 static const ARMCPRegInfo zcr_reginfo[] = {
6678     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6679       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6680       .access = PL1_RW, .type = ARM_CP_SVE,
6681       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6682       .writefn = zcr_write, .raw_writefn = raw_write },
6683     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6684       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6685       .access = PL2_RW, .type = ARM_CP_SVE,
6686       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6687       .writefn = zcr_write, .raw_writefn = raw_write },
6688     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6689       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6690       .access = PL3_RW, .type = ARM_CP_SVE,
6691       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6692       .writefn = zcr_write, .raw_writefn = raw_write },
6693 };
6694 
6695 #ifdef TARGET_AARCH64
6696 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6697                                     bool isread)
6698 {
6699     int el = arm_current_el(env);
6700 
6701     if (el == 0) {
6702         uint64_t sctlr = arm_sctlr(env, el);
6703         if (!(sctlr & SCTLR_EnTP2)) {
6704             return CP_ACCESS_TRAP;
6705         }
6706     }
6707     /* TODO: FEAT_FGT */
6708     if (el < 3
6709         && arm_feature(env, ARM_FEATURE_EL3)
6710         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6711         return CP_ACCESS_TRAP_EL3;
6712     }
6713     return CP_ACCESS_OK;
6714 }
6715 
6716 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6717                                  bool isread)
6718 {
6719     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6720     if (arm_current_el(env) < 3
6721         && arm_feature(env, ARM_FEATURE_EL3)
6722         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6723         return CP_ACCESS_TRAP_EL3;
6724     }
6725     return CP_ACCESS_OK;
6726 }
6727 
6728 /* ResetSVEState */
6729 static void arm_reset_sve_state(CPUARMState *env)
6730 {
6731     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6732     /* Recall that FFR is stored as pregs[16]. */
6733     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6734     vfp_set_fpcr(env, 0x0800009f);
6735 }
6736 
6737 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6738 {
6739     uint64_t change = (env->svcr ^ new) & mask;
6740 
6741     env->svcr ^= change;
6742 
6743     if (change & R_SVCR_SM_MASK) {
6744         arm_reset_sve_state(env);
6745     }
6746 }
6747 
6748 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6749                        uint64_t value)
6750 {
6751     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6752     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6753     aarch64_set_svcr(env, value, -1);
6754     arm_rebuild_hflags(env);
6755 }
6756 
6757 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6758                        uint64_t value)
6759 {
6760     int cur_el = arm_current_el(env);
6761     int old_len = sve_vqm1_for_el(env, cur_el);
6762     int new_len;
6763 
6764     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6765     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6766     raw_write(env, ri, value);
6767 
6768     /*
6769      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6770      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6771      * current values for simplicity.  But for QEMU internals, we must still
6772      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6773      * above aarch64_sve_narrow_vq.
6774      */
6775     new_len = sve_vqm1_for_el(env, cur_el);
6776     if (new_len < old_len) {
6777         aarch64_sve_narrow_vq(env, new_len + 1);
6778     }
6779 }
6780 
6781 static const ARMCPRegInfo sme_reginfo[] = {
6782     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6783       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6784       .access = PL0_RW, .accessfn = access_tpidr2,
6785       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6786     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6787       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6788       .access = PL0_RW, .type = ARM_CP_SME,
6789       .fieldoffset = offsetof(CPUARMState, svcr),
6790       .writefn = svcr_write, .raw_writefn = raw_write },
6791     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6792       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6793       .access = PL1_RW, .type = ARM_CP_SME,
6794       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6795       .writefn = smcr_write, .raw_writefn = raw_write },
6796     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6797       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6798       .access = PL2_RW, .type = ARM_CP_SME,
6799       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6800       .writefn = smcr_write, .raw_writefn = raw_write },
6801     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6802       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6803       .access = PL3_RW, .type = ARM_CP_SME,
6804       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6805       .writefn = smcr_write, .raw_writefn = raw_write },
6806     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6807       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6808       .access = PL1_R, .accessfn = access_aa64_tid1,
6809       /*
6810        * IMPLEMENTOR = 0 (software)
6811        * REVISION    = 0 (implementation defined)
6812        * SMPS        = 0 (no streaming execution priority in QEMU)
6813        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6814        */
6815       .type = ARM_CP_CONST, .resetvalue = 0, },
6816     /*
6817      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6818      */
6819     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6820       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6821       .access = PL1_RW, .accessfn = access_esm,
6822       .type = ARM_CP_CONST, .resetvalue = 0 },
6823     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6824       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6825       .access = PL2_RW, .accessfn = access_esm,
6826       .type = ARM_CP_CONST, .resetvalue = 0 },
6827 };
6828 #endif /* TARGET_AARCH64 */
6829 
6830 static void define_pmu_regs(ARMCPU *cpu)
6831 {
6832     /*
6833      * v7 performance monitor control register: same implementor
6834      * field as main ID register, and we implement four counters in
6835      * addition to the cycle count register.
6836      */
6837     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6838     ARMCPRegInfo pmcr = {
6839         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6840         .access = PL0_RW,
6841         .type = ARM_CP_IO | ARM_CP_ALIAS,
6842         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6843         .accessfn = pmreg_access, .writefn = pmcr_write,
6844         .raw_writefn = raw_write,
6845     };
6846     ARMCPRegInfo pmcr64 = {
6847         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6848         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6849         .access = PL0_RW, .accessfn = pmreg_access,
6850         .type = ARM_CP_IO,
6851         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6852         .resetvalue = cpu->isar.reset_pmcr_el0,
6853         .writefn = pmcr_write, .raw_writefn = raw_write,
6854     };
6855 
6856     define_one_arm_cp_reg(cpu, &pmcr);
6857     define_one_arm_cp_reg(cpu, &pmcr64);
6858     for (i = 0; i < pmcrn; i++) {
6859         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6860         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6861         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6862         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6863         ARMCPRegInfo pmev_regs[] = {
6864             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6865               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6866               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6867               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6868               .accessfn = pmreg_access_xevcntr },
6869             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6870               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6871               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6872               .type = ARM_CP_IO,
6873               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6874               .raw_readfn = pmevcntr_rawread,
6875               .raw_writefn = pmevcntr_rawwrite },
6876             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6877               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6878               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6879               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6880               .accessfn = pmreg_access },
6881             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6882               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6883               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6884               .type = ARM_CP_IO,
6885               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6886               .raw_writefn = pmevtyper_rawwrite },
6887         };
6888         define_arm_cp_regs(cpu, pmev_regs);
6889         g_free(pmevcntr_name);
6890         g_free(pmevcntr_el0_name);
6891         g_free(pmevtyper_name);
6892         g_free(pmevtyper_el0_name);
6893     }
6894     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6895         ARMCPRegInfo v81_pmu_regs[] = {
6896             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6897               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6898               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6899               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6900             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6901               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6902               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6903               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6904         };
6905         define_arm_cp_regs(cpu, v81_pmu_regs);
6906     }
6907     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6908         static const ARMCPRegInfo v84_pmmir = {
6909             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6910             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6911             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6912             .resetvalue = 0
6913         };
6914         define_one_arm_cp_reg(cpu, &v84_pmmir);
6915     }
6916 }
6917 
6918 /*
6919  * We don't know until after realize whether there's a GICv3
6920  * attached, and that is what registers the gicv3 sysregs.
6921  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6922  * at runtime.
6923  */
6924 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6925 {
6926     ARMCPU *cpu = env_archcpu(env);
6927     uint64_t pfr1 = cpu->isar.id_pfr1;
6928 
6929     if (env->gicv3state) {
6930         pfr1 |= 1 << 28;
6931     }
6932     return pfr1;
6933 }
6934 
6935 #ifndef CONFIG_USER_ONLY
6936 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6937 {
6938     ARMCPU *cpu = env_archcpu(env);
6939     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6940 
6941     if (env->gicv3state) {
6942         pfr0 |= 1 << 24;
6943     }
6944     return pfr0;
6945 }
6946 #endif
6947 
6948 /*
6949  * Shared logic between LORID and the rest of the LOR* registers.
6950  * Secure state exclusion has already been dealt with.
6951  */
6952 static CPAccessResult access_lor_ns(CPUARMState *env,
6953                                     const ARMCPRegInfo *ri, bool isread)
6954 {
6955     int el = arm_current_el(env);
6956 
6957     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6958         return CP_ACCESS_TRAP_EL2;
6959     }
6960     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6961         return CP_ACCESS_TRAP_EL3;
6962     }
6963     return CP_ACCESS_OK;
6964 }
6965 
6966 static CPAccessResult access_lor_other(CPUARMState *env,
6967                                        const ARMCPRegInfo *ri, bool isread)
6968 {
6969     if (arm_is_secure_below_el3(env)) {
6970         /* Access denied in secure mode.  */
6971         return CP_ACCESS_TRAP;
6972     }
6973     return access_lor_ns(env, ri, isread);
6974 }
6975 
6976 /*
6977  * A trivial implementation of ARMv8.1-LOR leaves all of these
6978  * registers fixed at 0, which indicates that there are zero
6979  * supported Limited Ordering regions.
6980  */
6981 static const ARMCPRegInfo lor_reginfo[] = {
6982     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6983       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6984       .access = PL1_RW, .accessfn = access_lor_other,
6985       .type = ARM_CP_CONST, .resetvalue = 0 },
6986     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6987       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6988       .access = PL1_RW, .accessfn = access_lor_other,
6989       .type = ARM_CP_CONST, .resetvalue = 0 },
6990     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6991       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6992       .access = PL1_RW, .accessfn = access_lor_other,
6993       .type = ARM_CP_CONST, .resetvalue = 0 },
6994     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6995       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6996       .access = PL1_RW, .accessfn = access_lor_other,
6997       .type = ARM_CP_CONST, .resetvalue = 0 },
6998     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6999       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7000       .access = PL1_R, .accessfn = access_lor_ns,
7001       .type = ARM_CP_CONST, .resetvalue = 0 },
7002 };
7003 
7004 #ifdef TARGET_AARCH64
7005 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7006                                    bool isread)
7007 {
7008     int el = arm_current_el(env);
7009 
7010     if (el < 2 &&
7011         arm_is_el2_enabled(env) &&
7012         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7013         return CP_ACCESS_TRAP_EL2;
7014     }
7015     if (el < 3 &&
7016         arm_feature(env, ARM_FEATURE_EL3) &&
7017         !(env->cp15.scr_el3 & SCR_APK)) {
7018         return CP_ACCESS_TRAP_EL3;
7019     }
7020     return CP_ACCESS_OK;
7021 }
7022 
7023 static const ARMCPRegInfo pauth_reginfo[] = {
7024     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7025       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7026       .access = PL1_RW, .accessfn = access_pauth,
7027       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7028     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7029       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7030       .access = PL1_RW, .accessfn = access_pauth,
7031       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7032     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7033       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7034       .access = PL1_RW, .accessfn = access_pauth,
7035       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7036     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7038       .access = PL1_RW, .accessfn = access_pauth,
7039       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7040     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7042       .access = PL1_RW, .accessfn = access_pauth,
7043       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7044     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7046       .access = PL1_RW, .accessfn = access_pauth,
7047       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7048     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7050       .access = PL1_RW, .accessfn = access_pauth,
7051       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7052     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7053       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7054       .access = PL1_RW, .accessfn = access_pauth,
7055       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7056     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7058       .access = PL1_RW, .accessfn = access_pauth,
7059       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7060     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7062       .access = PL1_RW, .accessfn = access_pauth,
7063       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7064 };
7065 
7066 static const ARMCPRegInfo tlbirange_reginfo[] = {
7067     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7068       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7069       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7070       .writefn = tlbi_aa64_rvae1is_write },
7071     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7072       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7073       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7074       .writefn = tlbi_aa64_rvae1is_write },
7075    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7076       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7077       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7078       .writefn = tlbi_aa64_rvae1is_write },
7079     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7081       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7082       .writefn = tlbi_aa64_rvae1is_write },
7083     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7084       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7085       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7086       .writefn = tlbi_aa64_rvae1is_write },
7087     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7088       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7089       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7090       .writefn = tlbi_aa64_rvae1is_write },
7091    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7092       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7093       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7094       .writefn = tlbi_aa64_rvae1is_write },
7095     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7096       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7097       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7098       .writefn = tlbi_aa64_rvae1is_write },
7099     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7100       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7101       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7102       .writefn = tlbi_aa64_rvae1_write },
7103     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7105       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7106       .writefn = tlbi_aa64_rvae1_write },
7107    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7108       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7109       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7110       .writefn = tlbi_aa64_rvae1_write },
7111     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7113       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7114       .writefn = tlbi_aa64_rvae1_write },
7115     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7116       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7117       .access = PL2_W, .type = ARM_CP_NO_RAW,
7118       .writefn = tlbi_aa64_ripas2e1is_write },
7119     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7121       .access = PL2_W, .type = ARM_CP_NO_RAW,
7122       .writefn = tlbi_aa64_ripas2e1is_write },
7123     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7125       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7126       .writefn = tlbi_aa64_rvae2is_write },
7127    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7129       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7130       .writefn = tlbi_aa64_rvae2is_write },
7131     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7133       .access = PL2_W, .type = ARM_CP_NO_RAW,
7134       .writefn = tlbi_aa64_ripas2e1_write },
7135     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7136       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7137       .access = PL2_W, .type = ARM_CP_NO_RAW,
7138       .writefn = tlbi_aa64_ripas2e1_write },
7139    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7141       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7142       .writefn = tlbi_aa64_rvae2is_write },
7143    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7144       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7145       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7146       .writefn = tlbi_aa64_rvae2is_write },
7147     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7148       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7149       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7150       .writefn = tlbi_aa64_rvae2_write },
7151    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7152       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7153       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7154       .writefn = tlbi_aa64_rvae2_write },
7155    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7156       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7157       .access = PL3_W, .type = ARM_CP_NO_RAW,
7158       .writefn = tlbi_aa64_rvae3is_write },
7159    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7160       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7161       .access = PL3_W, .type = ARM_CP_NO_RAW,
7162       .writefn = tlbi_aa64_rvae3is_write },
7163    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7164       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7165       .access = PL3_W, .type = ARM_CP_NO_RAW,
7166       .writefn = tlbi_aa64_rvae3is_write },
7167    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7168       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7169       .access = PL3_W, .type = ARM_CP_NO_RAW,
7170       .writefn = tlbi_aa64_rvae3is_write },
7171    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7172       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7173       .access = PL3_W, .type = ARM_CP_NO_RAW,
7174       .writefn = tlbi_aa64_rvae3_write },
7175    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7177       .access = PL3_W, .type = ARM_CP_NO_RAW,
7178       .writefn = tlbi_aa64_rvae3_write },
7179 };
7180 
7181 static const ARMCPRegInfo tlbios_reginfo[] = {
7182     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7183       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7184       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7185       .writefn = tlbi_aa64_vmalle1is_write },
7186     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7187       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7188       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7189       .writefn = tlbi_aa64_vae1is_write },
7190     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7191       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7192       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7193       .writefn = tlbi_aa64_vmalle1is_write },
7194     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7195       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7196       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7197       .writefn = tlbi_aa64_vae1is_write },
7198     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7199       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7200       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7201       .writefn = tlbi_aa64_vae1is_write },
7202     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7203       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7204       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7205       .writefn = tlbi_aa64_vae1is_write },
7206     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7207       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7208       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7209       .writefn = tlbi_aa64_alle2is_write },
7210     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7211       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7212       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7213       .writefn = tlbi_aa64_vae2is_write },
7214    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7215       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7216       .access = PL2_W, .type = ARM_CP_NO_RAW,
7217       .writefn = tlbi_aa64_alle1is_write },
7218     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7219       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7220       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7221       .writefn = tlbi_aa64_vae2is_write },
7222     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7223       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7224       .access = PL2_W, .type = ARM_CP_NO_RAW,
7225       .writefn = tlbi_aa64_alle1is_write },
7226     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7227       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7228       .access = PL2_W, .type = ARM_CP_NOP },
7229     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7230       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7231       .access = PL2_W, .type = ARM_CP_NOP },
7232     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7233       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7234       .access = PL2_W, .type = ARM_CP_NOP },
7235     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7236       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7237       .access = PL2_W, .type = ARM_CP_NOP },
7238     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7239       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7240       .access = PL3_W, .type = ARM_CP_NO_RAW,
7241       .writefn = tlbi_aa64_alle3is_write },
7242     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7243       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7244       .access = PL3_W, .type = ARM_CP_NO_RAW,
7245       .writefn = tlbi_aa64_vae3is_write },
7246     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7247       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7248       .access = PL3_W, .type = ARM_CP_NO_RAW,
7249       .writefn = tlbi_aa64_vae3is_write },
7250 };
7251 
7252 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7253 {
7254     Error *err = NULL;
7255     uint64_t ret;
7256 
7257     /* Success sets NZCV = 0000.  */
7258     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7259 
7260     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7261         /*
7262          * ??? Failed, for unknown reasons in the crypto subsystem.
7263          * The best we can do is log the reason and return the
7264          * timed-out indication to the guest.  There is no reason
7265          * we know to expect this failure to be transitory, so the
7266          * guest may well hang retrying the operation.
7267          */
7268         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7269                       ri->name, error_get_pretty(err));
7270         error_free(err);
7271 
7272         env->ZF = 0; /* NZCF = 0100 */
7273         return 0;
7274     }
7275     return ret;
7276 }
7277 
7278 /* We do not support re-seeding, so the two registers operate the same.  */
7279 static const ARMCPRegInfo rndr_reginfo[] = {
7280     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7281       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7282       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7283       .access = PL0_R, .readfn = rndr_readfn },
7284     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7285       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7286       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7287       .access = PL0_R, .readfn = rndr_readfn },
7288 };
7289 
7290 #ifndef CONFIG_USER_ONLY
7291 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7292                           uint64_t value)
7293 {
7294     ARMCPU *cpu = env_archcpu(env);
7295     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7296     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7297     uint64_t vaddr_in = (uint64_t) value;
7298     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7299     void *haddr;
7300     int mem_idx = cpu_mmu_index(env, false);
7301 
7302     /* This won't be crossing page boundaries */
7303     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7304     if (haddr) {
7305 
7306         ram_addr_t offset;
7307         MemoryRegion *mr;
7308 
7309         /* RCU lock is already being held */
7310         mr = memory_region_from_host(haddr, &offset);
7311 
7312         if (mr) {
7313             memory_region_writeback(mr, offset, dline_size);
7314         }
7315     }
7316 }
7317 
7318 static const ARMCPRegInfo dcpop_reg[] = {
7319     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7320       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7321       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7322       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7323 };
7324 
7325 static const ARMCPRegInfo dcpodp_reg[] = {
7326     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7327       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7328       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7329       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7330 };
7331 #endif /*CONFIG_USER_ONLY*/
7332 
7333 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7334                                        bool isread)
7335 {
7336     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7337         return CP_ACCESS_TRAP_EL2;
7338     }
7339 
7340     return CP_ACCESS_OK;
7341 }
7342 
7343 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7344                                  bool isread)
7345 {
7346     int el = arm_current_el(env);
7347 
7348     if (el < 2 && arm_is_el2_enabled(env)) {
7349         uint64_t hcr = arm_hcr_el2_eff(env);
7350         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7351             return CP_ACCESS_TRAP_EL2;
7352         }
7353     }
7354     if (el < 3 &&
7355         arm_feature(env, ARM_FEATURE_EL3) &&
7356         !(env->cp15.scr_el3 & SCR_ATA)) {
7357         return CP_ACCESS_TRAP_EL3;
7358     }
7359     return CP_ACCESS_OK;
7360 }
7361 
7362 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7363 {
7364     return env->pstate & PSTATE_TCO;
7365 }
7366 
7367 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7368 {
7369     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7370 }
7371 
7372 static const ARMCPRegInfo mte_reginfo[] = {
7373     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7374       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7375       .access = PL1_RW, .accessfn = access_mte,
7376       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7377     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7378       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7379       .access = PL1_RW, .accessfn = access_mte,
7380       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7381     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7382       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7383       .access = PL2_RW, .accessfn = access_mte,
7384       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7385     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7386       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7387       .access = PL3_RW,
7388       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7389     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7390       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7391       .access = PL1_RW, .accessfn = access_mte,
7392       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7393     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7394       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7395       .access = PL1_RW, .accessfn = access_mte,
7396       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7397     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7398       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7399       .access = PL1_R, .accessfn = access_aa64_tid5,
7400       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7401     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7402       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7403       .type = ARM_CP_NO_RAW,
7404       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7405     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7406       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7407       .type = ARM_CP_NOP, .access = PL1_W,
7408       .accessfn = aa64_cacheop_poc_access },
7409     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7410       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7411       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7412     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7413       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7414       .type = ARM_CP_NOP, .access = PL1_W,
7415       .accessfn = aa64_cacheop_poc_access },
7416     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7417       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7418       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7419     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7420       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7421       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7422     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7423       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7424       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7425     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7426       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7427       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7428     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7429       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7430       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7431 };
7432 
7433 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7434     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7435       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7436       .type = ARM_CP_CONST, .access = PL0_RW, },
7437 };
7438 
7439 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7440     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7441       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7442       .type = ARM_CP_NOP, .access = PL0_W,
7443       .accessfn = aa64_cacheop_poc_access },
7444     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7445       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7446       .type = ARM_CP_NOP, .access = PL0_W,
7447       .accessfn = aa64_cacheop_poc_access },
7448     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7449       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7450       .type = ARM_CP_NOP, .access = PL0_W,
7451       .accessfn = aa64_cacheop_poc_access },
7452     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7453       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7454       .type = ARM_CP_NOP, .access = PL0_W,
7455       .accessfn = aa64_cacheop_poc_access },
7456     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7457       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7458       .type = ARM_CP_NOP, .access = PL0_W,
7459       .accessfn = aa64_cacheop_poc_access },
7460     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7461       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7462       .type = ARM_CP_NOP, .access = PL0_W,
7463       .accessfn = aa64_cacheop_poc_access },
7464     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7465       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7466       .type = ARM_CP_NOP, .access = PL0_W,
7467       .accessfn = aa64_cacheop_poc_access },
7468     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7469       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7470       .type = ARM_CP_NOP, .access = PL0_W,
7471       .accessfn = aa64_cacheop_poc_access },
7472     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7473       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7474       .access = PL0_W, .type = ARM_CP_DC_GVA,
7475 #ifndef CONFIG_USER_ONLY
7476       /* Avoid overhead of an access check that always passes in user-mode */
7477       .accessfn = aa64_zva_access,
7478 #endif
7479     },
7480     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7481       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7482       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7483 #ifndef CONFIG_USER_ONLY
7484       /* Avoid overhead of an access check that always passes in user-mode */
7485       .accessfn = aa64_zva_access,
7486 #endif
7487     },
7488 };
7489 
7490 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7491                                      bool isread)
7492 {
7493     uint64_t hcr = arm_hcr_el2_eff(env);
7494     int el = arm_current_el(env);
7495 
7496     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7497         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7498             if (hcr & HCR_TGE) {
7499                 return CP_ACCESS_TRAP_EL2;
7500             }
7501             return CP_ACCESS_TRAP;
7502         }
7503     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7504         return CP_ACCESS_TRAP_EL2;
7505     }
7506     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7507         return CP_ACCESS_TRAP_EL2;
7508     }
7509     if (el < 3
7510         && arm_feature(env, ARM_FEATURE_EL3)
7511         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7512         return CP_ACCESS_TRAP_EL3;
7513     }
7514     return CP_ACCESS_OK;
7515 }
7516 
7517 static const ARMCPRegInfo scxtnum_reginfo[] = {
7518     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7519       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7520       .access = PL0_RW, .accessfn = access_scxtnum,
7521       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7522     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7523       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7524       .access = PL1_RW, .accessfn = access_scxtnum,
7525       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7526     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7527       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7528       .access = PL2_RW, .accessfn = access_scxtnum,
7529       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7530     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7531       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7532       .access = PL3_RW,
7533       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7534 };
7535 #endif /* TARGET_AARCH64 */
7536 
7537 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7538                                      bool isread)
7539 {
7540     int el = arm_current_el(env);
7541 
7542     if (el == 0) {
7543         uint64_t sctlr = arm_sctlr(env, el);
7544         if (!(sctlr & SCTLR_EnRCTX)) {
7545             return CP_ACCESS_TRAP;
7546         }
7547     } else if (el == 1) {
7548         uint64_t hcr = arm_hcr_el2_eff(env);
7549         if (hcr & HCR_NV) {
7550             return CP_ACCESS_TRAP_EL2;
7551         }
7552     }
7553     return CP_ACCESS_OK;
7554 }
7555 
7556 static const ARMCPRegInfo predinv_reginfo[] = {
7557     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7558       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7559       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7560     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7561       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7562       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7563     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7564       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7565       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7566     /*
7567      * Note the AArch32 opcodes have a different OPC1.
7568      */
7569     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7570       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7571       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7572     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7573       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7574       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7575     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7576       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7577       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7578 };
7579 
7580 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7581 {
7582     /* Read the high 32 bits of the current CCSIDR */
7583     return extract64(ccsidr_read(env, ri), 32, 32);
7584 }
7585 
7586 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7587     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7588       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7589       .access = PL1_R,
7590       .accessfn = access_tid4,
7591       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7592 };
7593 
7594 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7595                                        bool isread)
7596 {
7597     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7598         return CP_ACCESS_TRAP_EL2;
7599     }
7600 
7601     return CP_ACCESS_OK;
7602 }
7603 
7604 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7605                                        bool isread)
7606 {
7607     if (arm_feature(env, ARM_FEATURE_V8)) {
7608         return access_aa64_tid3(env, ri, isread);
7609     }
7610 
7611     return CP_ACCESS_OK;
7612 }
7613 
7614 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7615                                      bool isread)
7616 {
7617     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7618         return CP_ACCESS_TRAP_EL2;
7619     }
7620 
7621     return CP_ACCESS_OK;
7622 }
7623 
7624 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7625                                         const ARMCPRegInfo *ri, bool isread)
7626 {
7627     /*
7628      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7629      * in v7A, not in v8A.
7630      */
7631     if (!arm_feature(env, ARM_FEATURE_V8) &&
7632         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7633         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7634         return CP_ACCESS_TRAP_EL2;
7635     }
7636     return CP_ACCESS_OK;
7637 }
7638 
7639 static const ARMCPRegInfo jazelle_regs[] = {
7640     { .name = "JIDR",
7641       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7642       .access = PL1_R, .accessfn = access_jazelle,
7643       .type = ARM_CP_CONST, .resetvalue = 0 },
7644     { .name = "JOSCR",
7645       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7646       .accessfn = access_joscr_jmcr,
7647       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7648     { .name = "JMCR",
7649       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7650       .accessfn = access_joscr_jmcr,
7651       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7652 };
7653 
7654 static const ARMCPRegInfo contextidr_el2 = {
7655     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7656     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7657     .access = PL2_RW,
7658     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7659 };
7660 
7661 static const ARMCPRegInfo vhe_reginfo[] = {
7662     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7663       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7664       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7665       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7666 #ifndef CONFIG_USER_ONLY
7667     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7668       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7669       .fieldoffset =
7670         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7671       .type = ARM_CP_IO, .access = PL2_RW,
7672       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7673     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7674       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7675       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7676       .resetfn = gt_hv_timer_reset,
7677       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7678     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7679       .type = ARM_CP_IO,
7680       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7681       .access = PL2_RW,
7682       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7683       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7684     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7685       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7686       .type = ARM_CP_IO | ARM_CP_ALIAS,
7687       .access = PL2_RW, .accessfn = e2h_access,
7688       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7689       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7690     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7691       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7692       .type = ARM_CP_IO | ARM_CP_ALIAS,
7693       .access = PL2_RW, .accessfn = e2h_access,
7694       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7695       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7696     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7697       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7698       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7699       .access = PL2_RW, .accessfn = e2h_access,
7700       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7701     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7702       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7703       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7704       .access = PL2_RW, .accessfn = e2h_access,
7705       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7706     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7707       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7708       .type = ARM_CP_IO | ARM_CP_ALIAS,
7709       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7710       .access = PL2_RW, .accessfn = e2h_access,
7711       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7712     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7713       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7714       .type = ARM_CP_IO | ARM_CP_ALIAS,
7715       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7716       .access = PL2_RW, .accessfn = e2h_access,
7717       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7718 #endif
7719 };
7720 
7721 #ifndef CONFIG_USER_ONLY
7722 static const ARMCPRegInfo ats1e1_reginfo[] = {
7723     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7724       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7725       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7726       .writefn = ats_write64 },
7727     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7728       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7729       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7730       .writefn = ats_write64 },
7731 };
7732 
7733 static const ARMCPRegInfo ats1cp_reginfo[] = {
7734     { .name = "ATS1CPRP",
7735       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7736       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7737       .writefn = ats_write },
7738     { .name = "ATS1CPWP",
7739       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7740       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7741       .writefn = ats_write },
7742 };
7743 #endif
7744 
7745 /*
7746  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7747  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7748  * is non-zero, which is never for ARMv7, optionally in ARMv8
7749  * and mandatorily for ARMv8.2 and up.
7750  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7751  * implementation is RAZ/WI we can ignore this detail, as we
7752  * do for ACTLR.
7753  */
7754 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7755     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7756       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7757       .access = PL1_RW, .accessfn = access_tacr,
7758       .type = ARM_CP_CONST, .resetvalue = 0 },
7759     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7760       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7761       .access = PL2_RW, .type = ARM_CP_CONST,
7762       .resetvalue = 0 },
7763 };
7764 
7765 void register_cp_regs_for_features(ARMCPU *cpu)
7766 {
7767     /* Register all the coprocessor registers based on feature bits */
7768     CPUARMState *env = &cpu->env;
7769     if (arm_feature(env, ARM_FEATURE_M)) {
7770         /* M profile has no coprocessor registers */
7771         return;
7772     }
7773 
7774     define_arm_cp_regs(cpu, cp_reginfo);
7775     if (!arm_feature(env, ARM_FEATURE_V8)) {
7776         /*
7777          * Must go early as it is full of wildcards that may be
7778          * overridden by later definitions.
7779          */
7780         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7781     }
7782 
7783     if (arm_feature(env, ARM_FEATURE_V6)) {
7784         /* The ID registers all have impdef reset values */
7785         ARMCPRegInfo v6_idregs[] = {
7786             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7787               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7788               .access = PL1_R, .type = ARM_CP_CONST,
7789               .accessfn = access_aa32_tid3,
7790               .resetvalue = cpu->isar.id_pfr0 },
7791             /*
7792              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7793              * the value of the GIC field until after we define these regs.
7794              */
7795             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7796               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7797               .access = PL1_R, .type = ARM_CP_NO_RAW,
7798               .accessfn = access_aa32_tid3,
7799               .readfn = id_pfr1_read,
7800               .writefn = arm_cp_write_ignore },
7801             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7802               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7803               .access = PL1_R, .type = ARM_CP_CONST,
7804               .accessfn = access_aa32_tid3,
7805               .resetvalue = cpu->isar.id_dfr0 },
7806             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7807               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7808               .access = PL1_R, .type = ARM_CP_CONST,
7809               .accessfn = access_aa32_tid3,
7810               .resetvalue = cpu->id_afr0 },
7811             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7812               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7813               .access = PL1_R, .type = ARM_CP_CONST,
7814               .accessfn = access_aa32_tid3,
7815               .resetvalue = cpu->isar.id_mmfr0 },
7816             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7817               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7818               .access = PL1_R, .type = ARM_CP_CONST,
7819               .accessfn = access_aa32_tid3,
7820               .resetvalue = cpu->isar.id_mmfr1 },
7821             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7822               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7823               .access = PL1_R, .type = ARM_CP_CONST,
7824               .accessfn = access_aa32_tid3,
7825               .resetvalue = cpu->isar.id_mmfr2 },
7826             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7827               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7828               .access = PL1_R, .type = ARM_CP_CONST,
7829               .accessfn = access_aa32_tid3,
7830               .resetvalue = cpu->isar.id_mmfr3 },
7831             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7832               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7833               .access = PL1_R, .type = ARM_CP_CONST,
7834               .accessfn = access_aa32_tid3,
7835               .resetvalue = cpu->isar.id_isar0 },
7836             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7837               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7838               .access = PL1_R, .type = ARM_CP_CONST,
7839               .accessfn = access_aa32_tid3,
7840               .resetvalue = cpu->isar.id_isar1 },
7841             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7842               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7843               .access = PL1_R, .type = ARM_CP_CONST,
7844               .accessfn = access_aa32_tid3,
7845               .resetvalue = cpu->isar.id_isar2 },
7846             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7847               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7848               .access = PL1_R, .type = ARM_CP_CONST,
7849               .accessfn = access_aa32_tid3,
7850               .resetvalue = cpu->isar.id_isar3 },
7851             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7852               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7853               .access = PL1_R, .type = ARM_CP_CONST,
7854               .accessfn = access_aa32_tid3,
7855               .resetvalue = cpu->isar.id_isar4 },
7856             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7857               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7858               .access = PL1_R, .type = ARM_CP_CONST,
7859               .accessfn = access_aa32_tid3,
7860               .resetvalue = cpu->isar.id_isar5 },
7861             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7862               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7863               .access = PL1_R, .type = ARM_CP_CONST,
7864               .accessfn = access_aa32_tid3,
7865               .resetvalue = cpu->isar.id_mmfr4 },
7866             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7867               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7868               .access = PL1_R, .type = ARM_CP_CONST,
7869               .accessfn = access_aa32_tid3,
7870               .resetvalue = cpu->isar.id_isar6 },
7871         };
7872         define_arm_cp_regs(cpu, v6_idregs);
7873         define_arm_cp_regs(cpu, v6_cp_reginfo);
7874     } else {
7875         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7876     }
7877     if (arm_feature(env, ARM_FEATURE_V6K)) {
7878         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7879     }
7880     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7881         !arm_feature(env, ARM_FEATURE_PMSA)) {
7882         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7883     }
7884     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7885         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7886     }
7887     if (arm_feature(env, ARM_FEATURE_V7)) {
7888         ARMCPRegInfo clidr = {
7889             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7890             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7891             .access = PL1_R, .type = ARM_CP_CONST,
7892             .accessfn = access_tid4,
7893             .resetvalue = cpu->clidr
7894         };
7895         define_one_arm_cp_reg(cpu, &clidr);
7896         define_arm_cp_regs(cpu, v7_cp_reginfo);
7897         define_debug_regs(cpu);
7898         define_pmu_regs(cpu);
7899     } else {
7900         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7901     }
7902     if (arm_feature(env, ARM_FEATURE_V8)) {
7903         /*
7904          * v8 ID registers, which all have impdef reset values.
7905          * Note that within the ID register ranges the unused slots
7906          * must all RAZ, not UNDEF; future architecture versions may
7907          * define new registers here.
7908          * ID registers which are AArch64 views of the AArch32 ID registers
7909          * which already existed in v6 and v7 are handled elsewhere,
7910          * in v6_idregs[].
7911          */
7912         int i;
7913         ARMCPRegInfo v8_idregs[] = {
7914             /*
7915              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7916              * emulation because we don't know the right value for the
7917              * GIC field until after we define these regs.
7918              */
7919             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7920               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7921               .access = PL1_R,
7922 #ifdef CONFIG_USER_ONLY
7923               .type = ARM_CP_CONST,
7924               .resetvalue = cpu->isar.id_aa64pfr0
7925 #else
7926               .type = ARM_CP_NO_RAW,
7927               .accessfn = access_aa64_tid3,
7928               .readfn = id_aa64pfr0_read,
7929               .writefn = arm_cp_write_ignore
7930 #endif
7931             },
7932             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7933               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7934               .access = PL1_R, .type = ARM_CP_CONST,
7935               .accessfn = access_aa64_tid3,
7936               .resetvalue = cpu->isar.id_aa64pfr1},
7937             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7938               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7939               .access = PL1_R, .type = ARM_CP_CONST,
7940               .accessfn = access_aa64_tid3,
7941               .resetvalue = 0 },
7942             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7943               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7944               .access = PL1_R, .type = ARM_CP_CONST,
7945               .accessfn = access_aa64_tid3,
7946               .resetvalue = 0 },
7947             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7948               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7949               .access = PL1_R, .type = ARM_CP_CONST,
7950               .accessfn = access_aa64_tid3,
7951               .resetvalue = cpu->isar.id_aa64zfr0 },
7952             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7953               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7954               .access = PL1_R, .type = ARM_CP_CONST,
7955               .accessfn = access_aa64_tid3,
7956               .resetvalue = cpu->isar.id_aa64smfr0 },
7957             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7958               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7959               .access = PL1_R, .type = ARM_CP_CONST,
7960               .accessfn = access_aa64_tid3,
7961               .resetvalue = 0 },
7962             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7963               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7964               .access = PL1_R, .type = ARM_CP_CONST,
7965               .accessfn = access_aa64_tid3,
7966               .resetvalue = 0 },
7967             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7968               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7969               .access = PL1_R, .type = ARM_CP_CONST,
7970               .accessfn = access_aa64_tid3,
7971               .resetvalue = cpu->isar.id_aa64dfr0 },
7972             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7973               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7974               .access = PL1_R, .type = ARM_CP_CONST,
7975               .accessfn = access_aa64_tid3,
7976               .resetvalue = cpu->isar.id_aa64dfr1 },
7977             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7978               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7979               .access = PL1_R, .type = ARM_CP_CONST,
7980               .accessfn = access_aa64_tid3,
7981               .resetvalue = 0 },
7982             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7983               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7984               .access = PL1_R, .type = ARM_CP_CONST,
7985               .accessfn = access_aa64_tid3,
7986               .resetvalue = 0 },
7987             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7988               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7989               .access = PL1_R, .type = ARM_CP_CONST,
7990               .accessfn = access_aa64_tid3,
7991               .resetvalue = cpu->id_aa64afr0 },
7992             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7993               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7994               .access = PL1_R, .type = ARM_CP_CONST,
7995               .accessfn = access_aa64_tid3,
7996               .resetvalue = cpu->id_aa64afr1 },
7997             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7998               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7999               .access = PL1_R, .type = ARM_CP_CONST,
8000               .accessfn = access_aa64_tid3,
8001               .resetvalue = 0 },
8002             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8003               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8004               .access = PL1_R, .type = ARM_CP_CONST,
8005               .accessfn = access_aa64_tid3,
8006               .resetvalue = 0 },
8007             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8008               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8009               .access = PL1_R, .type = ARM_CP_CONST,
8010               .accessfn = access_aa64_tid3,
8011               .resetvalue = cpu->isar.id_aa64isar0 },
8012             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8013               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8014               .access = PL1_R, .type = ARM_CP_CONST,
8015               .accessfn = access_aa64_tid3,
8016               .resetvalue = cpu->isar.id_aa64isar1 },
8017             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8018               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8019               .access = PL1_R, .type = ARM_CP_CONST,
8020               .accessfn = access_aa64_tid3,
8021               .resetvalue = 0 },
8022             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8023               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8024               .access = PL1_R, .type = ARM_CP_CONST,
8025               .accessfn = access_aa64_tid3,
8026               .resetvalue = 0 },
8027             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8028               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8029               .access = PL1_R, .type = ARM_CP_CONST,
8030               .accessfn = access_aa64_tid3,
8031               .resetvalue = 0 },
8032             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8033               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8034               .access = PL1_R, .type = ARM_CP_CONST,
8035               .accessfn = access_aa64_tid3,
8036               .resetvalue = 0 },
8037             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8038               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8039               .access = PL1_R, .type = ARM_CP_CONST,
8040               .accessfn = access_aa64_tid3,
8041               .resetvalue = 0 },
8042             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8043               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8044               .access = PL1_R, .type = ARM_CP_CONST,
8045               .accessfn = access_aa64_tid3,
8046               .resetvalue = 0 },
8047             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8048               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8049               .access = PL1_R, .type = ARM_CP_CONST,
8050               .accessfn = access_aa64_tid3,
8051               .resetvalue = cpu->isar.id_aa64mmfr0 },
8052             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8053               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8054               .access = PL1_R, .type = ARM_CP_CONST,
8055               .accessfn = access_aa64_tid3,
8056               .resetvalue = cpu->isar.id_aa64mmfr1 },
8057             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8058               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8059               .access = PL1_R, .type = ARM_CP_CONST,
8060               .accessfn = access_aa64_tid3,
8061               .resetvalue = cpu->isar.id_aa64mmfr2 },
8062             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8063               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8064               .access = PL1_R, .type = ARM_CP_CONST,
8065               .accessfn = access_aa64_tid3,
8066               .resetvalue = 0 },
8067             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8068               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8069               .access = PL1_R, .type = ARM_CP_CONST,
8070               .accessfn = access_aa64_tid3,
8071               .resetvalue = 0 },
8072             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8073               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8074               .access = PL1_R, .type = ARM_CP_CONST,
8075               .accessfn = access_aa64_tid3,
8076               .resetvalue = 0 },
8077             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8078               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8079               .access = PL1_R, .type = ARM_CP_CONST,
8080               .accessfn = access_aa64_tid3,
8081               .resetvalue = 0 },
8082             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8083               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8084               .access = PL1_R, .type = ARM_CP_CONST,
8085               .accessfn = access_aa64_tid3,
8086               .resetvalue = 0 },
8087             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8088               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8089               .access = PL1_R, .type = ARM_CP_CONST,
8090               .accessfn = access_aa64_tid3,
8091               .resetvalue = cpu->isar.mvfr0 },
8092             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8093               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8094               .access = PL1_R, .type = ARM_CP_CONST,
8095               .accessfn = access_aa64_tid3,
8096               .resetvalue = cpu->isar.mvfr1 },
8097             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8098               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8099               .access = PL1_R, .type = ARM_CP_CONST,
8100               .accessfn = access_aa64_tid3,
8101               .resetvalue = cpu->isar.mvfr2 },
8102             /*
8103              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8104              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8105              * as RAZ, since it is in the "reserved for future ID
8106              * registers, RAZ" part of the AArch32 encoding space.
8107              */
8108             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8109               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8110               .access = PL1_R, .type = ARM_CP_CONST,
8111               .accessfn = access_aa64_tid3,
8112               .resetvalue = 0 },
8113             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8114               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8115               .access = PL1_R, .type = ARM_CP_CONST,
8116               .accessfn = access_aa64_tid3,
8117               .resetvalue = 0 },
8118             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8119               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8120               .access = PL1_R, .type = ARM_CP_CONST,
8121               .accessfn = access_aa64_tid3,
8122               .resetvalue = 0 },
8123             /*
8124              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8125              * they're also RAZ for AArch64, and in v8 are gradually
8126              * being filled with AArch64-view-of-AArch32-ID-register
8127              * for new ID registers.
8128              */
8129             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8130               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8131               .access = PL1_R, .type = ARM_CP_CONST,
8132               .accessfn = access_aa64_tid3,
8133               .resetvalue = 0 },
8134             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8135               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8136               .access = PL1_R, .type = ARM_CP_CONST,
8137               .accessfn = access_aa64_tid3,
8138               .resetvalue = cpu->isar.id_pfr2 },
8139             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8140               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8141               .access = PL1_R, .type = ARM_CP_CONST,
8142               .accessfn = access_aa64_tid3,
8143               .resetvalue = cpu->isar.id_dfr1 },
8144             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8145               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8146               .access = PL1_R, .type = ARM_CP_CONST,
8147               .accessfn = access_aa64_tid3,
8148               .resetvalue = cpu->isar.id_mmfr5 },
8149             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8150               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8151               .access = PL1_R, .type = ARM_CP_CONST,
8152               .accessfn = access_aa64_tid3,
8153               .resetvalue = 0 },
8154             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8155               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8156               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8157               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8158             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8159               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8160               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8161               .resetvalue = cpu->pmceid0 },
8162             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8163               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8164               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8165               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8166             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8167               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8168               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8169               .resetvalue = cpu->pmceid1 },
8170         };
8171 #ifdef CONFIG_USER_ONLY
8172         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8173             { .name = "ID_AA64PFR0_EL1",
8174               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8175                                R_ID_AA64PFR0_ADVSIMD_MASK |
8176                                R_ID_AA64PFR0_SVE_MASK |
8177                                R_ID_AA64PFR0_DIT_MASK,
8178               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8179                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8180             { .name = "ID_AA64PFR1_EL1",
8181               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8182                                R_ID_AA64PFR1_SSBS_MASK |
8183                                R_ID_AA64PFR1_MTE_MASK |
8184                                R_ID_AA64PFR1_SME_MASK },
8185             { .name = "ID_AA64PFR*_EL1_RESERVED",
8186               .is_glob = true },
8187             { .name = "ID_AA64ZFR0_EL1",
8188               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8189                                R_ID_AA64ZFR0_AES_MASK |
8190                                R_ID_AA64ZFR0_BITPERM_MASK |
8191                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8192                                R_ID_AA64ZFR0_SHA3_MASK |
8193                                R_ID_AA64ZFR0_SM4_MASK |
8194                                R_ID_AA64ZFR0_I8MM_MASK |
8195                                R_ID_AA64ZFR0_F32MM_MASK |
8196                                R_ID_AA64ZFR0_F64MM_MASK },
8197             { .name = "ID_AA64SMFR0_EL1",
8198               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8199                                R_ID_AA64SMFR0_B16F32_MASK |
8200                                R_ID_AA64SMFR0_F16F32_MASK |
8201                                R_ID_AA64SMFR0_I8I32_MASK |
8202                                R_ID_AA64SMFR0_F64F64_MASK |
8203                                R_ID_AA64SMFR0_I16I64_MASK |
8204                                R_ID_AA64SMFR0_FA64_MASK },
8205             { .name = "ID_AA64MMFR0_EL1",
8206               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8207               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8208                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8209             { .name = "ID_AA64MMFR1_EL1",
8210               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8211             { .name = "ID_AA64MMFR2_EL1",
8212               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8213             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8214               .is_glob = true },
8215             { .name = "ID_AA64DFR0_EL1",
8216               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8217             { .name = "ID_AA64DFR1_EL1" },
8218             { .name = "ID_AA64DFR*_EL1_RESERVED",
8219               .is_glob = true },
8220             { .name = "ID_AA64AFR*",
8221               .is_glob = true },
8222             { .name = "ID_AA64ISAR0_EL1",
8223               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8224                                R_ID_AA64ISAR0_SHA1_MASK |
8225                                R_ID_AA64ISAR0_SHA2_MASK |
8226                                R_ID_AA64ISAR0_CRC32_MASK |
8227                                R_ID_AA64ISAR0_ATOMIC_MASK |
8228                                R_ID_AA64ISAR0_RDM_MASK |
8229                                R_ID_AA64ISAR0_SHA3_MASK |
8230                                R_ID_AA64ISAR0_SM3_MASK |
8231                                R_ID_AA64ISAR0_SM4_MASK |
8232                                R_ID_AA64ISAR0_DP_MASK |
8233                                R_ID_AA64ISAR0_FHM_MASK |
8234                                R_ID_AA64ISAR0_TS_MASK |
8235                                R_ID_AA64ISAR0_RNDR_MASK },
8236             { .name = "ID_AA64ISAR1_EL1",
8237               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8238                                R_ID_AA64ISAR1_APA_MASK |
8239                                R_ID_AA64ISAR1_API_MASK |
8240                                R_ID_AA64ISAR1_JSCVT_MASK |
8241                                R_ID_AA64ISAR1_FCMA_MASK |
8242                                R_ID_AA64ISAR1_LRCPC_MASK |
8243                                R_ID_AA64ISAR1_GPA_MASK |
8244                                R_ID_AA64ISAR1_GPI_MASK |
8245                                R_ID_AA64ISAR1_FRINTTS_MASK |
8246                                R_ID_AA64ISAR1_SB_MASK |
8247                                R_ID_AA64ISAR1_BF16_MASK |
8248                                R_ID_AA64ISAR1_DGH_MASK |
8249                                R_ID_AA64ISAR1_I8MM_MASK },
8250             { .name = "ID_AA64ISAR2_EL1",
8251               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8252                                R_ID_AA64ISAR2_RPRES_MASK |
8253                                R_ID_AA64ISAR2_GPA3_MASK |
8254                                R_ID_AA64ISAR2_APA3_MASK },
8255             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8256               .is_glob = true },
8257         };
8258         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8259 #endif
8260         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8261         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8262             !arm_feature(env, ARM_FEATURE_EL2)) {
8263             ARMCPRegInfo rvbar = {
8264                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8265                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8266                 .access = PL1_R,
8267                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8268             };
8269             define_one_arm_cp_reg(cpu, &rvbar);
8270         }
8271         define_arm_cp_regs(cpu, v8_idregs);
8272         define_arm_cp_regs(cpu, v8_cp_reginfo);
8273 
8274         for (i = 4; i < 16; i++) {
8275             /*
8276              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8277              * For pre-v8 cores there are RAZ patterns for these in
8278              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8279              * v8 extends the "must RAZ" part of the ID register space
8280              * to also cover c0, 0, c{8-15}, {0-7}.
8281              * These are STATE_AA32 because in the AArch64 sysreg space
8282              * c4-c7 is where the AArch64 ID registers live (and we've
8283              * already defined those in v8_idregs[]), and c8-c15 are not
8284              * "must RAZ" for AArch64.
8285              */
8286             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8287             ARMCPRegInfo v8_aa32_raz_idregs = {
8288                 .name = name,
8289                 .state = ARM_CP_STATE_AA32,
8290                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8291                 .access = PL1_R, .type = ARM_CP_CONST,
8292                 .accessfn = access_aa64_tid3,
8293                 .resetvalue = 0 };
8294             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8295         }
8296     }
8297 
8298     /*
8299      * Register the base EL2 cpregs.
8300      * Pre v8, these registers are implemented only as part of the
8301      * Virtualization Extensions (EL2 present).  Beginning with v8,
8302      * if EL2 is missing but EL3 is enabled, mostly these become
8303      * RES0 from EL3, with some specific exceptions.
8304      */
8305     if (arm_feature(env, ARM_FEATURE_EL2)
8306         || (arm_feature(env, ARM_FEATURE_EL3)
8307             && arm_feature(env, ARM_FEATURE_V8))) {
8308         uint64_t vmpidr_def = mpidr_read_val(env);
8309         ARMCPRegInfo vpidr_regs[] = {
8310             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8311               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8312               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8313               .resetvalue = cpu->midr,
8314               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8315               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8316             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8317               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8318               .access = PL2_RW, .resetvalue = cpu->midr,
8319               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8320               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8321             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8322               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8323               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8324               .resetvalue = vmpidr_def,
8325               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8326               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8327             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8328               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8329               .access = PL2_RW, .resetvalue = vmpidr_def,
8330               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8331               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8332         };
8333         /*
8334          * The only field of MDCR_EL2 that has a defined architectural reset
8335          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8336          */
8337         ARMCPRegInfo mdcr_el2 = {
8338             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8339             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8340             .writefn = mdcr_el2_write,
8341             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8342             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8343         };
8344         define_one_arm_cp_reg(cpu, &mdcr_el2);
8345         define_arm_cp_regs(cpu, vpidr_regs);
8346         define_arm_cp_regs(cpu, el2_cp_reginfo);
8347         if (arm_feature(env, ARM_FEATURE_V8)) {
8348             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8349         }
8350         if (cpu_isar_feature(aa64_sel2, cpu)) {
8351             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8352         }
8353         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8354         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8355             ARMCPRegInfo rvbar[] = {
8356                 {
8357                     .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8358                     .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8359                     .access = PL2_R,
8360                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8361                 },
8362                 {   .name = "RVBAR", .type = ARM_CP_ALIAS,
8363                     .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8364                     .access = PL2_R,
8365                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8366                 },
8367             };
8368             define_arm_cp_regs(cpu, rvbar);
8369         }
8370     }
8371 
8372     /* Register the base EL3 cpregs. */
8373     if (arm_feature(env, ARM_FEATURE_EL3)) {
8374         define_arm_cp_regs(cpu, el3_cp_reginfo);
8375         ARMCPRegInfo el3_regs[] = {
8376             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8377               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8378               .access = PL3_R,
8379               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8380             },
8381             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8382               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8383               .access = PL3_RW,
8384               .raw_writefn = raw_write, .writefn = sctlr_write,
8385               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8386               .resetvalue = cpu->reset_sctlr },
8387         };
8388 
8389         define_arm_cp_regs(cpu, el3_regs);
8390     }
8391     /*
8392      * The behaviour of NSACR is sufficiently various that we don't
8393      * try to describe it in a single reginfo:
8394      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8395      *     reads as constant 0xc00 from NS EL1 and NS EL2
8396      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8397      *  if v7 without EL3, register doesn't exist
8398      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8399      */
8400     if (arm_feature(env, ARM_FEATURE_EL3)) {
8401         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8402             static const ARMCPRegInfo nsacr = {
8403                 .name = "NSACR", .type = ARM_CP_CONST,
8404                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8405                 .access = PL1_RW, .accessfn = nsacr_access,
8406                 .resetvalue = 0xc00
8407             };
8408             define_one_arm_cp_reg(cpu, &nsacr);
8409         } else {
8410             static const ARMCPRegInfo nsacr = {
8411                 .name = "NSACR",
8412                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8413                 .access = PL3_RW | PL1_R,
8414                 .resetvalue = 0,
8415                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8416             };
8417             define_one_arm_cp_reg(cpu, &nsacr);
8418         }
8419     } else {
8420         if (arm_feature(env, ARM_FEATURE_V8)) {
8421             static const ARMCPRegInfo nsacr = {
8422                 .name = "NSACR", .type = ARM_CP_CONST,
8423                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8424                 .access = PL1_R,
8425                 .resetvalue = 0xc00
8426             };
8427             define_one_arm_cp_reg(cpu, &nsacr);
8428         }
8429     }
8430 
8431     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8432         if (arm_feature(env, ARM_FEATURE_V6)) {
8433             /* PMSAv6 not implemented */
8434             assert(arm_feature(env, ARM_FEATURE_V7));
8435             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8436             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8437         } else {
8438             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8439         }
8440     } else {
8441         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8442         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8443         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8444         if (cpu_isar_feature(aa32_hpd, cpu)) {
8445             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8446         }
8447     }
8448     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8449         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8450     }
8451     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8452         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8453     }
8454     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8455         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8456     }
8457     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8458         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8459     }
8460     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8461         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8462     }
8463     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8464         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8465     }
8466     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8467         define_arm_cp_regs(cpu, omap_cp_reginfo);
8468     }
8469     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8470         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8471     }
8472     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8473         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8474     }
8475     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8476         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8477     }
8478     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8479         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8480     }
8481     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8482         define_arm_cp_regs(cpu, jazelle_regs);
8483     }
8484     /*
8485      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8486      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8487      * be read-only (ie write causes UNDEF exception).
8488      */
8489     {
8490         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8491             /*
8492              * Pre-v8 MIDR space.
8493              * Note that the MIDR isn't a simple constant register because
8494              * of the TI925 behaviour where writes to another register can
8495              * cause the MIDR value to change.
8496              *
8497              * Unimplemented registers in the c15 0 0 0 space default to
8498              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8499              * and friends override accordingly.
8500              */
8501             { .name = "MIDR",
8502               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8503               .access = PL1_R, .resetvalue = cpu->midr,
8504               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8505               .readfn = midr_read,
8506               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8507               .type = ARM_CP_OVERRIDE },
8508             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8509             { .name = "DUMMY",
8510               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8511               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8512             { .name = "DUMMY",
8513               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8514               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8515             { .name = "DUMMY",
8516               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8517               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8518             { .name = "DUMMY",
8519               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8520               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8521             { .name = "DUMMY",
8522               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8523               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8524         };
8525         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8526             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8527               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8528               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8529               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8530               .readfn = midr_read },
8531             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8532             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8533               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8534               .access = PL1_R, .resetvalue = cpu->midr },
8535             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8536               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8537               .access = PL1_R,
8538               .accessfn = access_aa64_tid1,
8539               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8540         };
8541         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8542             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8543             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8544             .access = PL1_R, .resetvalue = cpu->midr
8545         };
8546         ARMCPRegInfo id_cp_reginfo[] = {
8547             /* These are common to v8 and pre-v8 */
8548             { .name = "CTR",
8549               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8550               .access = PL1_R, .accessfn = ctr_el0_access,
8551               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8552             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8553               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8554               .access = PL0_R, .accessfn = ctr_el0_access,
8555               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8556             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8557             { .name = "TCMTR",
8558               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8559               .access = PL1_R,
8560               .accessfn = access_aa32_tid1,
8561               .type = ARM_CP_CONST, .resetvalue = 0 },
8562         };
8563         /* TLBTR is specific to VMSA */
8564         ARMCPRegInfo id_tlbtr_reginfo = {
8565               .name = "TLBTR",
8566               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8567               .access = PL1_R,
8568               .accessfn = access_aa32_tid1,
8569               .type = ARM_CP_CONST, .resetvalue = 0,
8570         };
8571         /* MPUIR is specific to PMSA V6+ */
8572         ARMCPRegInfo id_mpuir_reginfo = {
8573               .name = "MPUIR",
8574               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8575               .access = PL1_R, .type = ARM_CP_CONST,
8576               .resetvalue = cpu->pmsav7_dregion << 8
8577         };
8578         /* HMPUIR is specific to PMSA V8 */
8579         ARMCPRegInfo id_hmpuir_reginfo = {
8580             .name = "HMPUIR",
8581             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8582             .access = PL2_R, .type = ARM_CP_CONST,
8583             .resetvalue = cpu->pmsav8r_hdregion
8584         };
8585         static const ARMCPRegInfo crn0_wi_reginfo = {
8586             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8587             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8588             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8589         };
8590 #ifdef CONFIG_USER_ONLY
8591         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8592             { .name = "MIDR_EL1",
8593               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8594                                R_MIDR_EL1_PARTNUM_MASK |
8595                                R_MIDR_EL1_ARCHITECTURE_MASK |
8596                                R_MIDR_EL1_VARIANT_MASK |
8597                                R_MIDR_EL1_IMPLEMENTER_MASK },
8598             { .name = "REVIDR_EL1" },
8599         };
8600         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8601 #endif
8602         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8603             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8604             size_t i;
8605             /*
8606              * Register the blanket "writes ignored" value first to cover the
8607              * whole space. Then update the specific ID registers to allow write
8608              * access, so that they ignore writes rather than causing them to
8609              * UNDEF.
8610              */
8611             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8612             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8613                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8614             }
8615             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8616                 id_cp_reginfo[i].access = PL1_RW;
8617             }
8618             id_mpuir_reginfo.access = PL1_RW;
8619             id_tlbtr_reginfo.access = PL1_RW;
8620         }
8621         if (arm_feature(env, ARM_FEATURE_V8)) {
8622             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8623             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8624                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8625             }
8626         } else {
8627             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8628         }
8629         define_arm_cp_regs(cpu, id_cp_reginfo);
8630         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8631             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8632         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8633                    arm_feature(env, ARM_FEATURE_V8)) {
8634             uint32_t i = 0;
8635             char *tmp_string;
8636 
8637             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8638             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8639             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8640 
8641             /* Register alias is only valid for first 32 indexes */
8642             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8643                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8644                 uint8_t opc1 = extract32(i, 4, 1);
8645                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8646 
8647                 tmp_string = g_strdup_printf("PRBAR%u", i);
8648                 ARMCPRegInfo tmp_prbarn_reginfo = {
8649                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8650                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8651                     .access = PL1_RW, .resetvalue = 0,
8652                     .accessfn = access_tvm_trvm,
8653                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8654                 };
8655                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8656                 g_free(tmp_string);
8657 
8658                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8659                 tmp_string = g_strdup_printf("PRLAR%u", i);
8660                 ARMCPRegInfo tmp_prlarn_reginfo = {
8661                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8662                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8663                     .access = PL1_RW, .resetvalue = 0,
8664                     .accessfn = access_tvm_trvm,
8665                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8666                 };
8667                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8668                 g_free(tmp_string);
8669             }
8670 
8671             /* Register alias is only valid for first 32 indexes */
8672             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8673                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8674                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8675                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8676 
8677                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8678                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8679                     .name = tmp_string,
8680                     .type = ARM_CP_NO_RAW,
8681                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8682                     .access = PL2_RW, .resetvalue = 0,
8683                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8684                 };
8685                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8686                 g_free(tmp_string);
8687 
8688                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8689                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8690                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8691                     .name = tmp_string,
8692                     .type = ARM_CP_NO_RAW,
8693                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8694                     .access = PL2_RW, .resetvalue = 0,
8695                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8696                 };
8697                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8698                 g_free(tmp_string);
8699             }
8700         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8701             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8702         }
8703     }
8704 
8705     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8706         ARMCPRegInfo mpidr_cp_reginfo[] = {
8707             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8708               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8709               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8710         };
8711 #ifdef CONFIG_USER_ONLY
8712         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8713             { .name = "MPIDR_EL1",
8714               .fixed_bits = 0x0000000080000000 },
8715         };
8716         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8717 #endif
8718         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8719     }
8720 
8721     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8722         ARMCPRegInfo auxcr_reginfo[] = {
8723             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8724               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8725               .access = PL1_RW, .accessfn = access_tacr,
8726               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8727             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8728               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8729               .access = PL2_RW, .type = ARM_CP_CONST,
8730               .resetvalue = 0 },
8731             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8732               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8733               .access = PL3_RW, .type = ARM_CP_CONST,
8734               .resetvalue = 0 },
8735         };
8736         define_arm_cp_regs(cpu, auxcr_reginfo);
8737         if (cpu_isar_feature(aa32_ac2, cpu)) {
8738             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8739         }
8740     }
8741 
8742     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8743         /*
8744          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8745          * There are two flavours:
8746          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8747          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8748          *      32-bit register visible to AArch32 at a different encoding
8749          *      to the "flavour 1" register and with the bits rearranged to
8750          *      be able to squash a 64-bit address into the 32-bit view.
8751          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8752          * in future if we support AArch32-only configs of some of the
8753          * AArch64 cores we might need to add a specific feature flag
8754          * to indicate cores with "flavour 2" CBAR.
8755          */
8756         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8757             /* 32 bit view is [31:18] 0...0 [43:32]. */
8758             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8759                 | extract64(cpu->reset_cbar, 32, 12);
8760             ARMCPRegInfo cbar_reginfo[] = {
8761                 { .name = "CBAR",
8762                   .type = ARM_CP_CONST,
8763                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8764                   .access = PL1_R, .resetvalue = cbar32 },
8765                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8766                   .type = ARM_CP_CONST,
8767                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8768                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8769             };
8770             /* We don't implement a r/w 64 bit CBAR currently */
8771             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8772             define_arm_cp_regs(cpu, cbar_reginfo);
8773         } else {
8774             ARMCPRegInfo cbar = {
8775                 .name = "CBAR",
8776                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8777                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8778                 .fieldoffset = offsetof(CPUARMState,
8779                                         cp15.c15_config_base_address)
8780             };
8781             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8782                 cbar.access = PL1_R;
8783                 cbar.fieldoffset = 0;
8784                 cbar.type = ARM_CP_CONST;
8785             }
8786             define_one_arm_cp_reg(cpu, &cbar);
8787         }
8788     }
8789 
8790     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8791         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8792             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8793               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8794               .access = PL1_RW, .writefn = vbar_write,
8795               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8796                                      offsetof(CPUARMState, cp15.vbar_ns) },
8797               .resetvalue = 0 },
8798         };
8799         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8800     }
8801 
8802     /* Generic registers whose values depend on the implementation */
8803     {
8804         ARMCPRegInfo sctlr = {
8805             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8806             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8807             .access = PL1_RW, .accessfn = access_tvm_trvm,
8808             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8809                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8810             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8811             .raw_writefn = raw_write,
8812         };
8813         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8814             /*
8815              * Normally we would always end the TB on an SCTLR write, but Linux
8816              * arch/arm/mach-pxa/sleep.S expects two instructions following
8817              * an MMU enable to execute from cache.  Imitate this behaviour.
8818              */
8819             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8820         }
8821         define_one_arm_cp_reg(cpu, &sctlr);
8822 
8823         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8824             arm_feature(env, ARM_FEATURE_V8)) {
8825             ARMCPRegInfo vsctlr = {
8826                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8827                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8828                 .access = PL2_RW, .resetvalue = 0x0,
8829                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8830             };
8831             define_one_arm_cp_reg(cpu, &vsctlr);
8832         }
8833     }
8834 
8835     if (cpu_isar_feature(aa64_lor, cpu)) {
8836         define_arm_cp_regs(cpu, lor_reginfo);
8837     }
8838     if (cpu_isar_feature(aa64_pan, cpu)) {
8839         define_one_arm_cp_reg(cpu, &pan_reginfo);
8840     }
8841 #ifndef CONFIG_USER_ONLY
8842     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8843         define_arm_cp_regs(cpu, ats1e1_reginfo);
8844     }
8845     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8846         define_arm_cp_regs(cpu, ats1cp_reginfo);
8847     }
8848 #endif
8849     if (cpu_isar_feature(aa64_uao, cpu)) {
8850         define_one_arm_cp_reg(cpu, &uao_reginfo);
8851     }
8852 
8853     if (cpu_isar_feature(aa64_dit, cpu)) {
8854         define_one_arm_cp_reg(cpu, &dit_reginfo);
8855     }
8856     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8857         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8858     }
8859     if (cpu_isar_feature(any_ras, cpu)) {
8860         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8861     }
8862 
8863     if (cpu_isar_feature(aa64_vh, cpu) ||
8864         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8865         define_one_arm_cp_reg(cpu, &contextidr_el2);
8866     }
8867     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8868         define_arm_cp_regs(cpu, vhe_reginfo);
8869     }
8870 
8871     if (cpu_isar_feature(aa64_sve, cpu)) {
8872         define_arm_cp_regs(cpu, zcr_reginfo);
8873     }
8874 
8875     if (cpu_isar_feature(aa64_hcx, cpu)) {
8876         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8877     }
8878 
8879 #ifdef TARGET_AARCH64
8880     if (cpu_isar_feature(aa64_sme, cpu)) {
8881         define_arm_cp_regs(cpu, sme_reginfo);
8882     }
8883     if (cpu_isar_feature(aa64_pauth, cpu)) {
8884         define_arm_cp_regs(cpu, pauth_reginfo);
8885     }
8886     if (cpu_isar_feature(aa64_rndr, cpu)) {
8887         define_arm_cp_regs(cpu, rndr_reginfo);
8888     }
8889     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8890         define_arm_cp_regs(cpu, tlbirange_reginfo);
8891     }
8892     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8893         define_arm_cp_regs(cpu, tlbios_reginfo);
8894     }
8895 #ifndef CONFIG_USER_ONLY
8896     /* Data Cache clean instructions up to PoP */
8897     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8898         define_one_arm_cp_reg(cpu, dcpop_reg);
8899 
8900         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8901             define_one_arm_cp_reg(cpu, dcpodp_reg);
8902         }
8903     }
8904 #endif /*CONFIG_USER_ONLY*/
8905 
8906     /*
8907      * If full MTE is enabled, add all of the system registers.
8908      * If only "instructions available at EL0" are enabled,
8909      * then define only a RAZ/WI version of PSTATE.TCO.
8910      */
8911     if (cpu_isar_feature(aa64_mte, cpu)) {
8912         define_arm_cp_regs(cpu, mte_reginfo);
8913         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8914     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8915         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8916         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8917     }
8918 
8919     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8920         define_arm_cp_regs(cpu, scxtnum_reginfo);
8921     }
8922 #endif
8923 
8924     if (cpu_isar_feature(any_predinv, cpu)) {
8925         define_arm_cp_regs(cpu, predinv_reginfo);
8926     }
8927 
8928     if (cpu_isar_feature(any_ccidx, cpu)) {
8929         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8930     }
8931 
8932 #ifndef CONFIG_USER_ONLY
8933     /*
8934      * Register redirections and aliases must be done last,
8935      * after the registers from the other extensions have been defined.
8936      */
8937     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8938         define_arm_vh_e2h_redirects_aliases(cpu);
8939     }
8940 #endif
8941 }
8942 
8943 /* Sort alphabetically by type name, except for "any". */
8944 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8945 {
8946     ObjectClass *class_a = (ObjectClass *)a;
8947     ObjectClass *class_b = (ObjectClass *)b;
8948     const char *name_a, *name_b;
8949 
8950     name_a = object_class_get_name(class_a);
8951     name_b = object_class_get_name(class_b);
8952     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8953         return 1;
8954     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8955         return -1;
8956     } else {
8957         return strcmp(name_a, name_b);
8958     }
8959 }
8960 
8961 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8962 {
8963     ObjectClass *oc = data;
8964     CPUClass *cc = CPU_CLASS(oc);
8965     const char *typename;
8966     char *name;
8967 
8968     typename = object_class_get_name(oc);
8969     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8970     if (cc->deprecation_note) {
8971         qemu_printf("  %s (deprecated)\n", name);
8972     } else {
8973         qemu_printf("  %s\n", name);
8974     }
8975     g_free(name);
8976 }
8977 
8978 void arm_cpu_list(void)
8979 {
8980     GSList *list;
8981 
8982     list = object_class_get_list(TYPE_ARM_CPU, false);
8983     list = g_slist_sort(list, arm_cpu_list_compare);
8984     qemu_printf("Available CPUs:\n");
8985     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8986     g_slist_free(list);
8987 }
8988 
8989 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8990 {
8991     ObjectClass *oc = data;
8992     CpuDefinitionInfoList **cpu_list = user_data;
8993     CpuDefinitionInfo *info;
8994     const char *typename;
8995 
8996     typename = object_class_get_name(oc);
8997     info = g_malloc0(sizeof(*info));
8998     info->name = g_strndup(typename,
8999                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
9000     info->q_typename = g_strdup(typename);
9001 
9002     QAPI_LIST_PREPEND(*cpu_list, info);
9003 }
9004 
9005 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
9006 {
9007     CpuDefinitionInfoList *cpu_list = NULL;
9008     GSList *list;
9009 
9010     list = object_class_get_list(TYPE_ARM_CPU, false);
9011     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9012     g_slist_free(list);
9013 
9014     return cpu_list;
9015 }
9016 
9017 /*
9018  * Private utility function for define_one_arm_cp_reg_with_opaque():
9019  * add a single reginfo struct to the hash table.
9020  */
9021 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9022                                    void *opaque, CPState state,
9023                                    CPSecureState secstate,
9024                                    int crm, int opc1, int opc2,
9025                                    const char *name)
9026 {
9027     CPUARMState *env = &cpu->env;
9028     uint32_t key;
9029     ARMCPRegInfo *r2;
9030     bool is64 = r->type & ARM_CP_64BIT;
9031     bool ns = secstate & ARM_CP_SECSTATE_NS;
9032     int cp = r->cp;
9033     size_t name_len;
9034     bool make_const;
9035 
9036     switch (state) {
9037     case ARM_CP_STATE_AA32:
9038         /* We assume it is a cp15 register if the .cp field is left unset. */
9039         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9040             cp = 15;
9041         }
9042         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9043         break;
9044     case ARM_CP_STATE_AA64:
9045         /*
9046          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9047          * cp == 0 as equivalent to the value for "standard guest-visible
9048          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9049          * in their AArch64 view (the .cp value may be non-zero for the
9050          * benefit of the AArch32 view).
9051          */
9052         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9053             cp = CP_REG_ARM64_SYSREG_CP;
9054         }
9055         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9056         break;
9057     default:
9058         g_assert_not_reached();
9059     }
9060 
9061     /* Overriding of an existing definition must be explicitly requested. */
9062     if (!(r->type & ARM_CP_OVERRIDE)) {
9063         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9064         if (oldreg) {
9065             assert(oldreg->type & ARM_CP_OVERRIDE);
9066         }
9067     }
9068 
9069     /*
9070      * Eliminate registers that are not present because the EL is missing.
9071      * Doing this here makes it easier to put all registers for a given
9072      * feature into the same ARMCPRegInfo array and define them all at once.
9073      */
9074     make_const = false;
9075     if (arm_feature(env, ARM_FEATURE_EL3)) {
9076         /*
9077          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9078          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9079          */
9080         int min_el = ctz32(r->access) / 2;
9081         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9082             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9083                 return;
9084             }
9085             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9086         }
9087     } else {
9088         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9089                                  ? PL2_RW : PL1_RW);
9090         if ((r->access & max_el) == 0) {
9091             return;
9092         }
9093     }
9094 
9095     /* Combine cpreg and name into one allocation. */
9096     name_len = strlen(name) + 1;
9097     r2 = g_malloc(sizeof(*r2) + name_len);
9098     *r2 = *r;
9099     r2->name = memcpy(r2 + 1, name, name_len);
9100 
9101     /*
9102      * Update fields to match the instantiation, overwiting wildcards
9103      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9104      */
9105     r2->cp = cp;
9106     r2->crm = crm;
9107     r2->opc1 = opc1;
9108     r2->opc2 = opc2;
9109     r2->state = state;
9110     r2->secure = secstate;
9111     if (opaque) {
9112         r2->opaque = opaque;
9113     }
9114 
9115     if (make_const) {
9116         /* This should not have been a very special register to begin. */
9117         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9118         assert(old_special == 0 || old_special == ARM_CP_NOP);
9119         /*
9120          * Set the special function to CONST, retaining the other flags.
9121          * This is important for e.g. ARM_CP_SVE so that we still
9122          * take the SVE trap if CPTR_EL3.EZ == 0.
9123          */
9124         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9125         /*
9126          * Usually, these registers become RES0, but there are a few
9127          * special cases like VPIDR_EL2 which have a constant non-zero
9128          * value with writes ignored.
9129          */
9130         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9131             r2->resetvalue = 0;
9132         }
9133         /*
9134          * ARM_CP_CONST has precedence, so removing the callbacks and
9135          * offsets are not strictly necessary, but it is potentially
9136          * less confusing to debug later.
9137          */
9138         r2->readfn = NULL;
9139         r2->writefn = NULL;
9140         r2->raw_readfn = NULL;
9141         r2->raw_writefn = NULL;
9142         r2->resetfn = NULL;
9143         r2->fieldoffset = 0;
9144         r2->bank_fieldoffsets[0] = 0;
9145         r2->bank_fieldoffsets[1] = 0;
9146     } else {
9147         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9148 
9149         if (isbanked) {
9150             /*
9151              * Register is banked (using both entries in array).
9152              * Overwriting fieldoffset as the array is only used to define
9153              * banked registers but later only fieldoffset is used.
9154              */
9155             r2->fieldoffset = r->bank_fieldoffsets[ns];
9156         }
9157         if (state == ARM_CP_STATE_AA32) {
9158             if (isbanked) {
9159                 /*
9160                  * If the register is banked then we don't need to migrate or
9161                  * reset the 32-bit instance in certain cases:
9162                  *
9163                  * 1) If the register has both 32-bit and 64-bit instances
9164                  *    then we can count on the 64-bit instance taking care
9165                  *    of the non-secure bank.
9166                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9167                  *    version taking care of the secure bank.  This requires
9168                  *    that separate 32 and 64-bit definitions are provided.
9169                  */
9170                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9171                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9172                     r2->type |= ARM_CP_ALIAS;
9173                 }
9174             } else if ((secstate != r->secure) && !ns) {
9175                 /*
9176                  * The register is not banked so we only want to allow
9177                  * migration of the non-secure instance.
9178                  */
9179                 r2->type |= ARM_CP_ALIAS;
9180             }
9181 
9182             if (HOST_BIG_ENDIAN &&
9183                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9184                 r2->fieldoffset += sizeof(uint32_t);
9185             }
9186         }
9187     }
9188 
9189     /*
9190      * By convention, for wildcarded registers only the first
9191      * entry is used for migration; the others are marked as
9192      * ALIAS so we don't try to transfer the register
9193      * multiple times. Special registers (ie NOP/WFI) are
9194      * never migratable and not even raw-accessible.
9195      */
9196     if (r2->type & ARM_CP_SPECIAL_MASK) {
9197         r2->type |= ARM_CP_NO_RAW;
9198     }
9199     if (((r->crm == CP_ANY) && crm != 0) ||
9200         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9201         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9202         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9203     }
9204 
9205     /*
9206      * Check that raw accesses are either forbidden or handled. Note that
9207      * we can't assert this earlier because the setup of fieldoffset for
9208      * banked registers has to be done first.
9209      */
9210     if (!(r2->type & ARM_CP_NO_RAW)) {
9211         assert(!raw_accessors_invalid(r2));
9212     }
9213 
9214     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9215 }
9216 
9217 
9218 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9219                                        const ARMCPRegInfo *r, void *opaque)
9220 {
9221     /*
9222      * Define implementations of coprocessor registers.
9223      * We store these in a hashtable because typically
9224      * there are less than 150 registers in a space which
9225      * is 16*16*16*8*8 = 262144 in size.
9226      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9227      * If a register is defined twice then the second definition is
9228      * used, so this can be used to define some generic registers and
9229      * then override them with implementation specific variations.
9230      * At least one of the original and the second definition should
9231      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9232      * against accidental use.
9233      *
9234      * The state field defines whether the register is to be
9235      * visible in the AArch32 or AArch64 execution state. If the
9236      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9237      * reginfo structure for the AArch32 view, which sees the lower
9238      * 32 bits of the 64 bit register.
9239      *
9240      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9241      * be wildcarded. AArch64 registers are always considered to be 64
9242      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9243      * the register, if any.
9244      */
9245     int crm, opc1, opc2;
9246     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9247     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9248     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9249     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9250     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9251     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9252     CPState state;
9253 
9254     /* 64 bit registers have only CRm and Opc1 fields */
9255     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9256     /* op0 only exists in the AArch64 encodings */
9257     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9258     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9259     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9260     /*
9261      * This API is only for Arm's system coprocessors (14 and 15) or
9262      * (M-profile or v7A-and-earlier only) for implementation defined
9263      * coprocessors in the range 0..7.  Our decode assumes this, since
9264      * 8..13 can be used for other insns including VFP and Neon. See
9265      * valid_cp() in translate.c.  Assert here that we haven't tried
9266      * to use an invalid coprocessor number.
9267      */
9268     switch (r->state) {
9269     case ARM_CP_STATE_BOTH:
9270         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9271         if (r->cp == 0) {
9272             break;
9273         }
9274         /* fall through */
9275     case ARM_CP_STATE_AA32:
9276         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9277             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9278             assert(r->cp >= 14 && r->cp <= 15);
9279         } else {
9280             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9281         }
9282         break;
9283     case ARM_CP_STATE_AA64:
9284         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9285         break;
9286     default:
9287         g_assert_not_reached();
9288     }
9289     /*
9290      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9291      * encodes a minimum access level for the register. We roll this
9292      * runtime check into our general permission check code, so check
9293      * here that the reginfo's specified permissions are strict enough
9294      * to encompass the generic architectural permission check.
9295      */
9296     if (r->state != ARM_CP_STATE_AA32) {
9297         CPAccessRights mask;
9298         switch (r->opc1) {
9299         case 0:
9300             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9301             mask = PL0U_R | PL1_RW;
9302             break;
9303         case 1: case 2:
9304             /* min_EL EL1 */
9305             mask = PL1_RW;
9306             break;
9307         case 3:
9308             /* min_EL EL0 */
9309             mask = PL0_RW;
9310             break;
9311         case 4:
9312         case 5:
9313             /* min_EL EL2 */
9314             mask = PL2_RW;
9315             break;
9316         case 6:
9317             /* min_EL EL3 */
9318             mask = PL3_RW;
9319             break;
9320         case 7:
9321             /* min_EL EL1, secure mode only (we don't check the latter) */
9322             mask = PL1_RW;
9323             break;
9324         default:
9325             /* broken reginfo with out-of-range opc1 */
9326             g_assert_not_reached();
9327         }
9328         /* assert our permissions are not too lax (stricter is fine) */
9329         assert((r->access & ~mask) == 0);
9330     }
9331 
9332     /*
9333      * Check that the register definition has enough info to handle
9334      * reads and writes if they are permitted.
9335      */
9336     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9337         if (r->access & PL3_R) {
9338             assert((r->fieldoffset ||
9339                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9340                    r->readfn);
9341         }
9342         if (r->access & PL3_W) {
9343             assert((r->fieldoffset ||
9344                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9345                    r->writefn);
9346         }
9347     }
9348 
9349     for (crm = crmmin; crm <= crmmax; crm++) {
9350         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9351             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9352                 for (state = ARM_CP_STATE_AA32;
9353                      state <= ARM_CP_STATE_AA64; state++) {
9354                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9355                         continue;
9356                     }
9357                     if (state == ARM_CP_STATE_AA32) {
9358                         /*
9359                          * Under AArch32 CP registers can be common
9360                          * (same for secure and non-secure world) or banked.
9361                          */
9362                         char *name;
9363 
9364                         switch (r->secure) {
9365                         case ARM_CP_SECSTATE_S:
9366                         case ARM_CP_SECSTATE_NS:
9367                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9368                                                    r->secure, crm, opc1, opc2,
9369                                                    r->name);
9370                             break;
9371                         case ARM_CP_SECSTATE_BOTH:
9372                             name = g_strdup_printf("%s_S", r->name);
9373                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9374                                                    ARM_CP_SECSTATE_S,
9375                                                    crm, opc1, opc2, name);
9376                             g_free(name);
9377                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9378                                                    ARM_CP_SECSTATE_NS,
9379                                                    crm, opc1, opc2, r->name);
9380                             break;
9381                         default:
9382                             g_assert_not_reached();
9383                         }
9384                     } else {
9385                         /*
9386                          * AArch64 registers get mapped to non-secure instance
9387                          * of AArch32
9388                          */
9389                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9390                                                ARM_CP_SECSTATE_NS,
9391                                                crm, opc1, opc2, r->name);
9392                     }
9393                 }
9394             }
9395         }
9396     }
9397 }
9398 
9399 /* Define a whole list of registers */
9400 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9401                                         void *opaque, size_t len)
9402 {
9403     size_t i;
9404     for (i = 0; i < len; ++i) {
9405         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9406     }
9407 }
9408 
9409 /*
9410  * Modify ARMCPRegInfo for access from userspace.
9411  *
9412  * This is a data driven modification directed by
9413  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9414  * user-space cannot alter any values and dynamic values pertaining to
9415  * execution state are hidden from user space view anyway.
9416  */
9417 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9418                                  const ARMCPRegUserSpaceInfo *mods,
9419                                  size_t mods_len)
9420 {
9421     for (size_t mi = 0; mi < mods_len; ++mi) {
9422         const ARMCPRegUserSpaceInfo *m = mods + mi;
9423         GPatternSpec *pat = NULL;
9424 
9425         if (m->is_glob) {
9426             pat = g_pattern_spec_new(m->name);
9427         }
9428         for (size_t ri = 0; ri < regs_len; ++ri) {
9429             ARMCPRegInfo *r = regs + ri;
9430 
9431             if (pat && g_pattern_match_string(pat, r->name)) {
9432                 r->type = ARM_CP_CONST;
9433                 r->access = PL0U_R;
9434                 r->resetvalue = 0;
9435                 /* continue */
9436             } else if (strcmp(r->name, m->name) == 0) {
9437                 r->type = ARM_CP_CONST;
9438                 r->access = PL0U_R;
9439                 r->resetvalue &= m->exported_bits;
9440                 r->resetvalue |= m->fixed_bits;
9441                 break;
9442             }
9443         }
9444         if (pat) {
9445             g_pattern_spec_free(pat);
9446         }
9447     }
9448 }
9449 
9450 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9451 {
9452     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9453 }
9454 
9455 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9456                          uint64_t value)
9457 {
9458     /* Helper coprocessor write function for write-ignore registers */
9459 }
9460 
9461 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9462 {
9463     /* Helper coprocessor write function for read-as-zero registers */
9464     return 0;
9465 }
9466 
9467 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9468 {
9469     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9470 }
9471 
9472 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9473 {
9474     /*
9475      * Return true if it is not valid for us to switch to
9476      * this CPU mode (ie all the UNPREDICTABLE cases in
9477      * the ARM ARM CPSRWriteByInstr pseudocode).
9478      */
9479 
9480     /* Changes to or from Hyp via MSR and CPS are illegal. */
9481     if (write_type == CPSRWriteByInstr &&
9482         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9483          mode == ARM_CPU_MODE_HYP)) {
9484         return 1;
9485     }
9486 
9487     switch (mode) {
9488     case ARM_CPU_MODE_USR:
9489         return 0;
9490     case ARM_CPU_MODE_SYS:
9491     case ARM_CPU_MODE_SVC:
9492     case ARM_CPU_MODE_ABT:
9493     case ARM_CPU_MODE_UND:
9494     case ARM_CPU_MODE_IRQ:
9495     case ARM_CPU_MODE_FIQ:
9496         /*
9497          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9498          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9499          */
9500         /*
9501          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9502          * and CPS are treated as illegal mode changes.
9503          */
9504         if (write_type == CPSRWriteByInstr &&
9505             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9506             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9507             return 1;
9508         }
9509         return 0;
9510     case ARM_CPU_MODE_HYP:
9511         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9512     case ARM_CPU_MODE_MON:
9513         return arm_current_el(env) < 3;
9514     default:
9515         return 1;
9516     }
9517 }
9518 
9519 uint32_t cpsr_read(CPUARMState *env)
9520 {
9521     int ZF;
9522     ZF = (env->ZF == 0);
9523     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9524         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9525         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9526         | ((env->condexec_bits & 0xfc) << 8)
9527         | (env->GE << 16) | (env->daif & CPSR_AIF);
9528 }
9529 
9530 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9531                 CPSRWriteType write_type)
9532 {
9533     uint32_t changed_daif;
9534     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9535         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9536 
9537     if (mask & CPSR_NZCV) {
9538         env->ZF = (~val) & CPSR_Z;
9539         env->NF = val;
9540         env->CF = (val >> 29) & 1;
9541         env->VF = (val << 3) & 0x80000000;
9542     }
9543     if (mask & CPSR_Q) {
9544         env->QF = ((val & CPSR_Q) != 0);
9545     }
9546     if (mask & CPSR_T) {
9547         env->thumb = ((val & CPSR_T) != 0);
9548     }
9549     if (mask & CPSR_IT_0_1) {
9550         env->condexec_bits &= ~3;
9551         env->condexec_bits |= (val >> 25) & 3;
9552     }
9553     if (mask & CPSR_IT_2_7) {
9554         env->condexec_bits &= 3;
9555         env->condexec_bits |= (val >> 8) & 0xfc;
9556     }
9557     if (mask & CPSR_GE) {
9558         env->GE = (val >> 16) & 0xf;
9559     }
9560 
9561     /*
9562      * In a V7 implementation that includes the security extensions but does
9563      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9564      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9565      * bits respectively.
9566      *
9567      * In a V8 implementation, it is permitted for privileged software to
9568      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9569      */
9570     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9571         arm_feature(env, ARM_FEATURE_EL3) &&
9572         !arm_feature(env, ARM_FEATURE_EL2) &&
9573         !arm_is_secure(env)) {
9574 
9575         changed_daif = (env->daif ^ val) & mask;
9576 
9577         if (changed_daif & CPSR_A) {
9578             /*
9579              * Check to see if we are allowed to change the masking of async
9580              * abort exceptions from a non-secure state.
9581              */
9582             if (!(env->cp15.scr_el3 & SCR_AW)) {
9583                 qemu_log_mask(LOG_GUEST_ERROR,
9584                               "Ignoring attempt to switch CPSR_A flag from "
9585                               "non-secure world with SCR.AW bit clear\n");
9586                 mask &= ~CPSR_A;
9587             }
9588         }
9589 
9590         if (changed_daif & CPSR_F) {
9591             /*
9592              * Check to see if we are allowed to change the masking of FIQ
9593              * exceptions from a non-secure state.
9594              */
9595             if (!(env->cp15.scr_el3 & SCR_FW)) {
9596                 qemu_log_mask(LOG_GUEST_ERROR,
9597                               "Ignoring attempt to switch CPSR_F flag from "
9598                               "non-secure world with SCR.FW bit clear\n");
9599                 mask &= ~CPSR_F;
9600             }
9601 
9602             /*
9603              * Check whether non-maskable FIQ (NMFI) support is enabled.
9604              * If this bit is set software is not allowed to mask
9605              * FIQs, but is allowed to set CPSR_F to 0.
9606              */
9607             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9608                 (val & CPSR_F)) {
9609                 qemu_log_mask(LOG_GUEST_ERROR,
9610                               "Ignoring attempt to enable CPSR_F flag "
9611                               "(non-maskable FIQ [NMFI] support enabled)\n");
9612                 mask &= ~CPSR_F;
9613             }
9614         }
9615     }
9616 
9617     env->daif &= ~(CPSR_AIF & mask);
9618     env->daif |= val & CPSR_AIF & mask;
9619 
9620     if (write_type != CPSRWriteRaw &&
9621         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9622         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9623             /*
9624              * Note that we can only get here in USR mode if this is a
9625              * gdb stub write; for this case we follow the architectural
9626              * behaviour for guest writes in USR mode of ignoring an attempt
9627              * to switch mode. (Those are caught by translate.c for writes
9628              * triggered by guest instructions.)
9629              */
9630             mask &= ~CPSR_M;
9631         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9632             /*
9633              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9634              * v7, and has defined behaviour in v8:
9635              *  + leave CPSR.M untouched
9636              *  + allow changes to the other CPSR fields
9637              *  + set PSTATE.IL
9638              * For user changes via the GDB stub, we don't set PSTATE.IL,
9639              * as this would be unnecessarily harsh for a user error.
9640              */
9641             mask &= ~CPSR_M;
9642             if (write_type != CPSRWriteByGDBStub &&
9643                 arm_feature(env, ARM_FEATURE_V8)) {
9644                 mask |= CPSR_IL;
9645                 val |= CPSR_IL;
9646             }
9647             qemu_log_mask(LOG_GUEST_ERROR,
9648                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9649                           aarch32_mode_name(env->uncached_cpsr),
9650                           aarch32_mode_name(val));
9651         } else {
9652             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9653                           write_type == CPSRWriteExceptionReturn ?
9654                           "Exception return from AArch32" :
9655                           "AArch32 mode switch from",
9656                           aarch32_mode_name(env->uncached_cpsr),
9657                           aarch32_mode_name(val), env->regs[15]);
9658             switch_mode(env, val & CPSR_M);
9659         }
9660     }
9661     mask &= ~CACHED_CPSR_BITS;
9662     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9663     if (rebuild_hflags) {
9664         arm_rebuild_hflags(env);
9665     }
9666 }
9667 
9668 /* Sign/zero extend */
9669 uint32_t HELPER(sxtb16)(uint32_t x)
9670 {
9671     uint32_t res;
9672     res = (uint16_t)(int8_t)x;
9673     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9674     return res;
9675 }
9676 
9677 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9678 {
9679     /*
9680      * Take a division-by-zero exception if necessary; otherwise return
9681      * to get the usual non-trapping division behaviour (result of 0)
9682      */
9683     if (arm_feature(env, ARM_FEATURE_M)
9684         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9685         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9686     }
9687 }
9688 
9689 uint32_t HELPER(uxtb16)(uint32_t x)
9690 {
9691     uint32_t res;
9692     res = (uint16_t)(uint8_t)x;
9693     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9694     return res;
9695 }
9696 
9697 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9698 {
9699     if (den == 0) {
9700         handle_possible_div0_trap(env, GETPC());
9701         return 0;
9702     }
9703     if (num == INT_MIN && den == -1) {
9704         return INT_MIN;
9705     }
9706     return num / den;
9707 }
9708 
9709 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9710 {
9711     if (den == 0) {
9712         handle_possible_div0_trap(env, GETPC());
9713         return 0;
9714     }
9715     return num / den;
9716 }
9717 
9718 uint32_t HELPER(rbit)(uint32_t x)
9719 {
9720     return revbit32(x);
9721 }
9722 
9723 #ifdef CONFIG_USER_ONLY
9724 
9725 static void switch_mode(CPUARMState *env, int mode)
9726 {
9727     ARMCPU *cpu = env_archcpu(env);
9728 
9729     if (mode != ARM_CPU_MODE_USR) {
9730         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9731     }
9732 }
9733 
9734 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9735                                  uint32_t cur_el, bool secure)
9736 {
9737     return 1;
9738 }
9739 
9740 void aarch64_sync_64_to_32(CPUARMState *env)
9741 {
9742     g_assert_not_reached();
9743 }
9744 
9745 #else
9746 
9747 static void switch_mode(CPUARMState *env, int mode)
9748 {
9749     int old_mode;
9750     int i;
9751 
9752     old_mode = env->uncached_cpsr & CPSR_M;
9753     if (mode == old_mode) {
9754         return;
9755     }
9756 
9757     if (old_mode == ARM_CPU_MODE_FIQ) {
9758         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9759         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9760     } else if (mode == ARM_CPU_MODE_FIQ) {
9761         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9762         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9763     }
9764 
9765     i = bank_number(old_mode);
9766     env->banked_r13[i] = env->regs[13];
9767     env->banked_spsr[i] = env->spsr;
9768 
9769     i = bank_number(mode);
9770     env->regs[13] = env->banked_r13[i];
9771     env->spsr = env->banked_spsr[i];
9772 
9773     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9774     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9775 }
9776 
9777 /*
9778  * Physical Interrupt Target EL Lookup Table
9779  *
9780  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9781  *
9782  * The below multi-dimensional table is used for looking up the target
9783  * exception level given numerous condition criteria.  Specifically, the
9784  * target EL is based on SCR and HCR routing controls as well as the
9785  * currently executing EL and secure state.
9786  *
9787  *    Dimensions:
9788  *    target_el_table[2][2][2][2][2][4]
9789  *                    |  |  |  |  |  +--- Current EL
9790  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9791  *                    |  |  |  +--------- HCR mask override
9792  *                    |  |  +------------ SCR exec state control
9793  *                    |  +--------------- SCR mask override
9794  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9795  *
9796  *    The table values are as such:
9797  *    0-3 = EL0-EL3
9798  *     -1 = Cannot occur
9799  *
9800  * The ARM ARM target EL table includes entries indicating that an "exception
9801  * is not taken".  The two cases where this is applicable are:
9802  *    1) An exception is taken from EL3 but the SCR does not have the exception
9803  *    routed to EL3.
9804  *    2) An exception is taken from EL2 but the HCR does not have the exception
9805  *    routed to EL2.
9806  * In these two cases, the below table contain a target of EL1.  This value is
9807  * returned as it is expected that the consumer of the table data will check
9808  * for "target EL >= current EL" to ensure the exception is not taken.
9809  *
9810  *            SCR     HCR
9811  *         64  EA     AMO                 From
9812  *        BIT IRQ     IMO      Non-secure         Secure
9813  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9814  */
9815 static const int8_t target_el_table[2][2][2][2][2][4] = {
9816     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9817        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9818       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9819        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9820      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9821        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9822       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9823        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9824     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9825        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9826       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9827        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9828      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9829        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9830       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9831        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9832 };
9833 
9834 /*
9835  * Determine the target EL for physical exceptions
9836  */
9837 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9838                                  uint32_t cur_el, bool secure)
9839 {
9840     CPUARMState *env = cs->env_ptr;
9841     bool rw;
9842     bool scr;
9843     bool hcr;
9844     int target_el;
9845     /* Is the highest EL AArch64? */
9846     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9847     uint64_t hcr_el2;
9848 
9849     if (arm_feature(env, ARM_FEATURE_EL3)) {
9850         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9851     } else {
9852         /*
9853          * Either EL2 is the highest EL (and so the EL2 register width
9854          * is given by is64); or there is no EL2 or EL3, in which case
9855          * the value of 'rw' does not affect the table lookup anyway.
9856          */
9857         rw = is64;
9858     }
9859 
9860     hcr_el2 = arm_hcr_el2_eff(env);
9861     switch (excp_idx) {
9862     case EXCP_IRQ:
9863         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9864         hcr = hcr_el2 & HCR_IMO;
9865         break;
9866     case EXCP_FIQ:
9867         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9868         hcr = hcr_el2 & HCR_FMO;
9869         break;
9870     default:
9871         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9872         hcr = hcr_el2 & HCR_AMO;
9873         break;
9874     };
9875 
9876     /*
9877      * For these purposes, TGE and AMO/IMO/FMO both force the
9878      * interrupt to EL2.  Fold TGE into the bit extracted above.
9879      */
9880     hcr |= (hcr_el2 & HCR_TGE) != 0;
9881 
9882     /* Perform a table-lookup for the target EL given the current state */
9883     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9884 
9885     assert(target_el > 0);
9886 
9887     return target_el;
9888 }
9889 
9890 void arm_log_exception(CPUState *cs)
9891 {
9892     int idx = cs->exception_index;
9893 
9894     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9895         const char *exc = NULL;
9896         static const char * const excnames[] = {
9897             [EXCP_UDEF] = "Undefined Instruction",
9898             [EXCP_SWI] = "SVC",
9899             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9900             [EXCP_DATA_ABORT] = "Data Abort",
9901             [EXCP_IRQ] = "IRQ",
9902             [EXCP_FIQ] = "FIQ",
9903             [EXCP_BKPT] = "Breakpoint",
9904             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9905             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9906             [EXCP_HVC] = "Hypervisor Call",
9907             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9908             [EXCP_SMC] = "Secure Monitor Call",
9909             [EXCP_VIRQ] = "Virtual IRQ",
9910             [EXCP_VFIQ] = "Virtual FIQ",
9911             [EXCP_SEMIHOST] = "Semihosting call",
9912             [EXCP_NOCP] = "v7M NOCP UsageFault",
9913             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9914             [EXCP_STKOF] = "v8M STKOF UsageFault",
9915             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9916             [EXCP_LSERR] = "v8M LSERR UsageFault",
9917             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9918             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9919             [EXCP_VSERR] = "Virtual SERR",
9920         };
9921 
9922         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9923             exc = excnames[idx];
9924         }
9925         if (!exc) {
9926             exc = "unknown";
9927         }
9928         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9929                       idx, exc, cs->cpu_index);
9930     }
9931 }
9932 
9933 /*
9934  * Function used to synchronize QEMU's AArch64 register set with AArch32
9935  * register set.  This is necessary when switching between AArch32 and AArch64
9936  * execution state.
9937  */
9938 void aarch64_sync_32_to_64(CPUARMState *env)
9939 {
9940     int i;
9941     uint32_t mode = env->uncached_cpsr & CPSR_M;
9942 
9943     /* We can blanket copy R[0:7] to X[0:7] */
9944     for (i = 0; i < 8; i++) {
9945         env->xregs[i] = env->regs[i];
9946     }
9947 
9948     /*
9949      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9950      * Otherwise, they come from the banked user regs.
9951      */
9952     if (mode == ARM_CPU_MODE_FIQ) {
9953         for (i = 8; i < 13; i++) {
9954             env->xregs[i] = env->usr_regs[i - 8];
9955         }
9956     } else {
9957         for (i = 8; i < 13; i++) {
9958             env->xregs[i] = env->regs[i];
9959         }
9960     }
9961 
9962     /*
9963      * Registers x13-x23 are the various mode SP and FP registers. Registers
9964      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9965      * from the mode banked register.
9966      */
9967     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9968         env->xregs[13] = env->regs[13];
9969         env->xregs[14] = env->regs[14];
9970     } else {
9971         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9972         /* HYP is an exception in that it is copied from r14 */
9973         if (mode == ARM_CPU_MODE_HYP) {
9974             env->xregs[14] = env->regs[14];
9975         } else {
9976             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9977         }
9978     }
9979 
9980     if (mode == ARM_CPU_MODE_HYP) {
9981         env->xregs[15] = env->regs[13];
9982     } else {
9983         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9984     }
9985 
9986     if (mode == ARM_CPU_MODE_IRQ) {
9987         env->xregs[16] = env->regs[14];
9988         env->xregs[17] = env->regs[13];
9989     } else {
9990         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9991         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9992     }
9993 
9994     if (mode == ARM_CPU_MODE_SVC) {
9995         env->xregs[18] = env->regs[14];
9996         env->xregs[19] = env->regs[13];
9997     } else {
9998         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9999         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10000     }
10001 
10002     if (mode == ARM_CPU_MODE_ABT) {
10003         env->xregs[20] = env->regs[14];
10004         env->xregs[21] = env->regs[13];
10005     } else {
10006         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10007         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10008     }
10009 
10010     if (mode == ARM_CPU_MODE_UND) {
10011         env->xregs[22] = env->regs[14];
10012         env->xregs[23] = env->regs[13];
10013     } else {
10014         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10015         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10016     }
10017 
10018     /*
10019      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10020      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10021      * FIQ bank for r8-r14.
10022      */
10023     if (mode == ARM_CPU_MODE_FIQ) {
10024         for (i = 24; i < 31; i++) {
10025             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10026         }
10027     } else {
10028         for (i = 24; i < 29; i++) {
10029             env->xregs[i] = env->fiq_regs[i - 24];
10030         }
10031         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10032         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10033     }
10034 
10035     env->pc = env->regs[15];
10036 }
10037 
10038 /*
10039  * Function used to synchronize QEMU's AArch32 register set with AArch64
10040  * register set.  This is necessary when switching between AArch32 and AArch64
10041  * execution state.
10042  */
10043 void aarch64_sync_64_to_32(CPUARMState *env)
10044 {
10045     int i;
10046     uint32_t mode = env->uncached_cpsr & CPSR_M;
10047 
10048     /* We can blanket copy X[0:7] to R[0:7] */
10049     for (i = 0; i < 8; i++) {
10050         env->regs[i] = env->xregs[i];
10051     }
10052 
10053     /*
10054      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10055      * Otherwise, we copy x8-x12 into the banked user regs.
10056      */
10057     if (mode == ARM_CPU_MODE_FIQ) {
10058         for (i = 8; i < 13; i++) {
10059             env->usr_regs[i - 8] = env->xregs[i];
10060         }
10061     } else {
10062         for (i = 8; i < 13; i++) {
10063             env->regs[i] = env->xregs[i];
10064         }
10065     }
10066 
10067     /*
10068      * Registers r13 & r14 depend on the current mode.
10069      * If we are in a given mode, we copy the corresponding x registers to r13
10070      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10071      * for the mode.
10072      */
10073     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10074         env->regs[13] = env->xregs[13];
10075         env->regs[14] = env->xregs[14];
10076     } else {
10077         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10078 
10079         /*
10080          * HYP is an exception in that it does not have its own banked r14 but
10081          * shares the USR r14
10082          */
10083         if (mode == ARM_CPU_MODE_HYP) {
10084             env->regs[14] = env->xregs[14];
10085         } else {
10086             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10087         }
10088     }
10089 
10090     if (mode == ARM_CPU_MODE_HYP) {
10091         env->regs[13] = env->xregs[15];
10092     } else {
10093         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10094     }
10095 
10096     if (mode == ARM_CPU_MODE_IRQ) {
10097         env->regs[14] = env->xregs[16];
10098         env->regs[13] = env->xregs[17];
10099     } else {
10100         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10101         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10102     }
10103 
10104     if (mode == ARM_CPU_MODE_SVC) {
10105         env->regs[14] = env->xregs[18];
10106         env->regs[13] = env->xregs[19];
10107     } else {
10108         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10109         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10110     }
10111 
10112     if (mode == ARM_CPU_MODE_ABT) {
10113         env->regs[14] = env->xregs[20];
10114         env->regs[13] = env->xregs[21];
10115     } else {
10116         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10117         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10118     }
10119 
10120     if (mode == ARM_CPU_MODE_UND) {
10121         env->regs[14] = env->xregs[22];
10122         env->regs[13] = env->xregs[23];
10123     } else {
10124         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10125         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10126     }
10127 
10128     /*
10129      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10130      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10131      * FIQ bank for r8-r14.
10132      */
10133     if (mode == ARM_CPU_MODE_FIQ) {
10134         for (i = 24; i < 31; i++) {
10135             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10136         }
10137     } else {
10138         for (i = 24; i < 29; i++) {
10139             env->fiq_regs[i - 24] = env->xregs[i];
10140         }
10141         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10142         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10143     }
10144 
10145     env->regs[15] = env->pc;
10146 }
10147 
10148 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10149                                    uint32_t mask, uint32_t offset,
10150                                    uint32_t newpc)
10151 {
10152     int new_el;
10153 
10154     /* Change the CPU state so as to actually take the exception. */
10155     switch_mode(env, new_mode);
10156 
10157     /*
10158      * For exceptions taken to AArch32 we must clear the SS bit in both
10159      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10160      */
10161     env->pstate &= ~PSTATE_SS;
10162     env->spsr = cpsr_read(env);
10163     /* Clear IT bits.  */
10164     env->condexec_bits = 0;
10165     /* Switch to the new mode, and to the correct instruction set.  */
10166     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10167 
10168     /* This must be after mode switching. */
10169     new_el = arm_current_el(env);
10170 
10171     /* Set new mode endianness */
10172     env->uncached_cpsr &= ~CPSR_E;
10173     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10174         env->uncached_cpsr |= CPSR_E;
10175     }
10176     /* J and IL must always be cleared for exception entry */
10177     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10178     env->daif |= mask;
10179 
10180     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10181         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10182             env->uncached_cpsr |= CPSR_SSBS;
10183         } else {
10184             env->uncached_cpsr &= ~CPSR_SSBS;
10185         }
10186     }
10187 
10188     if (new_mode == ARM_CPU_MODE_HYP) {
10189         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10190         env->elr_el[2] = env->regs[15];
10191     } else {
10192         /* CPSR.PAN is normally preserved preserved unless...  */
10193         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10194             switch (new_el) {
10195             case 3:
10196                 if (!arm_is_secure_below_el3(env)) {
10197                     /* ... the target is EL3, from non-secure state.  */
10198                     env->uncached_cpsr &= ~CPSR_PAN;
10199                     break;
10200                 }
10201                 /* ... the target is EL3, from secure state ... */
10202                 /* fall through */
10203             case 1:
10204                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10205                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10206                     env->uncached_cpsr |= CPSR_PAN;
10207                 }
10208                 break;
10209             }
10210         }
10211         /*
10212          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10213          * and we should just guard the thumb mode on V4
10214          */
10215         if (arm_feature(env, ARM_FEATURE_V4T)) {
10216             env->thumb =
10217                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10218         }
10219         env->regs[14] = env->regs[15] + offset;
10220     }
10221     env->regs[15] = newpc;
10222     arm_rebuild_hflags(env);
10223 }
10224 
10225 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10226 {
10227     /*
10228      * Handle exception entry to Hyp mode; this is sufficiently
10229      * different to entry to other AArch32 modes that we handle it
10230      * separately here.
10231      *
10232      * The vector table entry used is always the 0x14 Hyp mode entry point,
10233      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10234      * The offset applied to the preferred return address is always zero
10235      * (see DDI0487C.a section G1.12.3).
10236      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10237      */
10238     uint32_t addr, mask;
10239     ARMCPU *cpu = ARM_CPU(cs);
10240     CPUARMState *env = &cpu->env;
10241 
10242     switch (cs->exception_index) {
10243     case EXCP_UDEF:
10244         addr = 0x04;
10245         break;
10246     case EXCP_SWI:
10247         addr = 0x08;
10248         break;
10249     case EXCP_BKPT:
10250         /* Fall through to prefetch abort.  */
10251     case EXCP_PREFETCH_ABORT:
10252         env->cp15.ifar_s = env->exception.vaddress;
10253         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10254                       (uint32_t)env->exception.vaddress);
10255         addr = 0x0c;
10256         break;
10257     case EXCP_DATA_ABORT:
10258         env->cp15.dfar_s = env->exception.vaddress;
10259         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10260                       (uint32_t)env->exception.vaddress);
10261         addr = 0x10;
10262         break;
10263     case EXCP_IRQ:
10264         addr = 0x18;
10265         break;
10266     case EXCP_FIQ:
10267         addr = 0x1c;
10268         break;
10269     case EXCP_HVC:
10270         addr = 0x08;
10271         break;
10272     case EXCP_HYP_TRAP:
10273         addr = 0x14;
10274         break;
10275     default:
10276         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10277     }
10278 
10279     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10280         if (!arm_feature(env, ARM_FEATURE_V8)) {
10281             /*
10282              * QEMU syndrome values are v8-style. v7 has the IL bit
10283              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10284              * If this is a v7 CPU, squash the IL bit in those cases.
10285              */
10286             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10287                 (cs->exception_index == EXCP_DATA_ABORT &&
10288                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10289                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10290                 env->exception.syndrome &= ~ARM_EL_IL;
10291             }
10292         }
10293         env->cp15.esr_el[2] = env->exception.syndrome;
10294     }
10295 
10296     if (arm_current_el(env) != 2 && addr < 0x14) {
10297         addr = 0x14;
10298     }
10299 
10300     mask = 0;
10301     if (!(env->cp15.scr_el3 & SCR_EA)) {
10302         mask |= CPSR_A;
10303     }
10304     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10305         mask |= CPSR_I;
10306     }
10307     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10308         mask |= CPSR_F;
10309     }
10310 
10311     addr += env->cp15.hvbar;
10312 
10313     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10314 }
10315 
10316 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10317 {
10318     ARMCPU *cpu = ARM_CPU(cs);
10319     CPUARMState *env = &cpu->env;
10320     uint32_t addr;
10321     uint32_t mask;
10322     int new_mode;
10323     uint32_t offset;
10324     uint32_t moe;
10325 
10326     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10327     switch (syn_get_ec(env->exception.syndrome)) {
10328     case EC_BREAKPOINT:
10329     case EC_BREAKPOINT_SAME_EL:
10330         moe = 1;
10331         break;
10332     case EC_WATCHPOINT:
10333     case EC_WATCHPOINT_SAME_EL:
10334         moe = 10;
10335         break;
10336     case EC_AA32_BKPT:
10337         moe = 3;
10338         break;
10339     case EC_VECTORCATCH:
10340         moe = 5;
10341         break;
10342     default:
10343         moe = 0;
10344         break;
10345     }
10346 
10347     if (moe) {
10348         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10349     }
10350 
10351     if (env->exception.target_el == 2) {
10352         arm_cpu_do_interrupt_aarch32_hyp(cs);
10353         return;
10354     }
10355 
10356     switch (cs->exception_index) {
10357     case EXCP_UDEF:
10358         new_mode = ARM_CPU_MODE_UND;
10359         addr = 0x04;
10360         mask = CPSR_I;
10361         if (env->thumb) {
10362             offset = 2;
10363         } else {
10364             offset = 4;
10365         }
10366         break;
10367     case EXCP_SWI:
10368         new_mode = ARM_CPU_MODE_SVC;
10369         addr = 0x08;
10370         mask = CPSR_I;
10371         /* The PC already points to the next instruction.  */
10372         offset = 0;
10373         break;
10374     case EXCP_BKPT:
10375         /* Fall through to prefetch abort.  */
10376     case EXCP_PREFETCH_ABORT:
10377         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10378         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10379         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10380                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10381         new_mode = ARM_CPU_MODE_ABT;
10382         addr = 0x0c;
10383         mask = CPSR_A | CPSR_I;
10384         offset = 4;
10385         break;
10386     case EXCP_DATA_ABORT:
10387         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10388         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10389         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10390                       env->exception.fsr,
10391                       (uint32_t)env->exception.vaddress);
10392         new_mode = ARM_CPU_MODE_ABT;
10393         addr = 0x10;
10394         mask = CPSR_A | CPSR_I;
10395         offset = 8;
10396         break;
10397     case EXCP_IRQ:
10398         new_mode = ARM_CPU_MODE_IRQ;
10399         addr = 0x18;
10400         /* Disable IRQ and imprecise data aborts.  */
10401         mask = CPSR_A | CPSR_I;
10402         offset = 4;
10403         if (env->cp15.scr_el3 & SCR_IRQ) {
10404             /* IRQ routed to monitor mode */
10405             new_mode = ARM_CPU_MODE_MON;
10406             mask |= CPSR_F;
10407         }
10408         break;
10409     case EXCP_FIQ:
10410         new_mode = ARM_CPU_MODE_FIQ;
10411         addr = 0x1c;
10412         /* Disable FIQ, IRQ and imprecise data aborts.  */
10413         mask = CPSR_A | CPSR_I | CPSR_F;
10414         if (env->cp15.scr_el3 & SCR_FIQ) {
10415             /* FIQ routed to monitor mode */
10416             new_mode = ARM_CPU_MODE_MON;
10417         }
10418         offset = 4;
10419         break;
10420     case EXCP_VIRQ:
10421         new_mode = ARM_CPU_MODE_IRQ;
10422         addr = 0x18;
10423         /* Disable IRQ and imprecise data aborts.  */
10424         mask = CPSR_A | CPSR_I;
10425         offset = 4;
10426         break;
10427     case EXCP_VFIQ:
10428         new_mode = ARM_CPU_MODE_FIQ;
10429         addr = 0x1c;
10430         /* Disable FIQ, IRQ and imprecise data aborts.  */
10431         mask = CPSR_A | CPSR_I | CPSR_F;
10432         offset = 4;
10433         break;
10434     case EXCP_VSERR:
10435         {
10436             /*
10437              * Note that this is reported as a data abort, but the DFAR
10438              * has an UNKNOWN value.  Construct the SError syndrome from
10439              * AET and ExT fields.
10440              */
10441             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10442 
10443             if (extended_addresses_enabled(env)) {
10444                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10445             } else {
10446                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10447             }
10448             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10449             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10450             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10451                           env->exception.fsr);
10452 
10453             new_mode = ARM_CPU_MODE_ABT;
10454             addr = 0x10;
10455             mask = CPSR_A | CPSR_I;
10456             offset = 8;
10457         }
10458         break;
10459     case EXCP_SMC:
10460         new_mode = ARM_CPU_MODE_MON;
10461         addr = 0x08;
10462         mask = CPSR_A | CPSR_I | CPSR_F;
10463         offset = 0;
10464         break;
10465     default:
10466         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10467         return; /* Never happens.  Keep compiler happy.  */
10468     }
10469 
10470     if (new_mode == ARM_CPU_MODE_MON) {
10471         addr += env->cp15.mvbar;
10472     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10473         /* High vectors. When enabled, base address cannot be remapped. */
10474         addr += 0xffff0000;
10475     } else {
10476         /*
10477          * ARM v7 architectures provide a vector base address register to remap
10478          * the interrupt vector table.
10479          * This register is only followed in non-monitor mode, and is banked.
10480          * Note: only bits 31:5 are valid.
10481          */
10482         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10483     }
10484 
10485     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10486         env->cp15.scr_el3 &= ~SCR_NS;
10487     }
10488 
10489     take_aarch32_exception(env, new_mode, mask, offset, addr);
10490 }
10491 
10492 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10493 {
10494     /*
10495      * Return the register number of the AArch64 view of the AArch32
10496      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10497      * be that of the AArch32 mode the exception came from.
10498      */
10499     int mode = env->uncached_cpsr & CPSR_M;
10500 
10501     switch (aarch32_reg) {
10502     case 0 ... 7:
10503         return aarch32_reg;
10504     case 8 ... 12:
10505         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10506     case 13:
10507         switch (mode) {
10508         case ARM_CPU_MODE_USR:
10509         case ARM_CPU_MODE_SYS:
10510             return 13;
10511         case ARM_CPU_MODE_HYP:
10512             return 15;
10513         case ARM_CPU_MODE_IRQ:
10514             return 17;
10515         case ARM_CPU_MODE_SVC:
10516             return 19;
10517         case ARM_CPU_MODE_ABT:
10518             return 21;
10519         case ARM_CPU_MODE_UND:
10520             return 23;
10521         case ARM_CPU_MODE_FIQ:
10522             return 29;
10523         default:
10524             g_assert_not_reached();
10525         }
10526     case 14:
10527         switch (mode) {
10528         case ARM_CPU_MODE_USR:
10529         case ARM_CPU_MODE_SYS:
10530         case ARM_CPU_MODE_HYP:
10531             return 14;
10532         case ARM_CPU_MODE_IRQ:
10533             return 16;
10534         case ARM_CPU_MODE_SVC:
10535             return 18;
10536         case ARM_CPU_MODE_ABT:
10537             return 20;
10538         case ARM_CPU_MODE_UND:
10539             return 22;
10540         case ARM_CPU_MODE_FIQ:
10541             return 30;
10542         default:
10543             g_assert_not_reached();
10544         }
10545     case 15:
10546         return 31;
10547     default:
10548         g_assert_not_reached();
10549     }
10550 }
10551 
10552 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10553 {
10554     uint32_t ret = cpsr_read(env);
10555 
10556     /* Move DIT to the correct location for SPSR_ELx */
10557     if (ret & CPSR_DIT) {
10558         ret &= ~CPSR_DIT;
10559         ret |= PSTATE_DIT;
10560     }
10561     /* Merge PSTATE.SS into SPSR_ELx */
10562     ret |= env->pstate & PSTATE_SS;
10563 
10564     return ret;
10565 }
10566 
10567 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10568 {
10569     /* Return true if this syndrome value is a synchronous external abort */
10570     switch (syn_get_ec(syndrome)) {
10571     case EC_INSNABORT:
10572     case EC_INSNABORT_SAME_EL:
10573     case EC_DATAABORT:
10574     case EC_DATAABORT_SAME_EL:
10575         /* Look at fault status code for all the synchronous ext abort cases */
10576         switch (syndrome & 0x3f) {
10577         case 0x10:
10578         case 0x13:
10579         case 0x14:
10580         case 0x15:
10581         case 0x16:
10582         case 0x17:
10583             return true;
10584         default:
10585             return false;
10586         }
10587     default:
10588         return false;
10589     }
10590 }
10591 
10592 /* Handle exception entry to a target EL which is using AArch64 */
10593 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10594 {
10595     ARMCPU *cpu = ARM_CPU(cs);
10596     CPUARMState *env = &cpu->env;
10597     unsigned int new_el = env->exception.target_el;
10598     target_ulong addr = env->cp15.vbar_el[new_el];
10599     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10600     unsigned int old_mode;
10601     unsigned int cur_el = arm_current_el(env);
10602     int rt;
10603 
10604     /*
10605      * Note that new_el can never be 0.  If cur_el is 0, then
10606      * el0_a64 is is_a64(), else el0_a64 is ignored.
10607      */
10608     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10609 
10610     if (cur_el < new_el) {
10611         /*
10612          * Entry vector offset depends on whether the implemented EL
10613          * immediately lower than the target level is using AArch32 or AArch64
10614          */
10615         bool is_aa64;
10616         uint64_t hcr;
10617 
10618         switch (new_el) {
10619         case 3:
10620             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10621             break;
10622         case 2:
10623             hcr = arm_hcr_el2_eff(env);
10624             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10625                 is_aa64 = (hcr & HCR_RW) != 0;
10626                 break;
10627             }
10628             /* fall through */
10629         case 1:
10630             is_aa64 = is_a64(env);
10631             break;
10632         default:
10633             g_assert_not_reached();
10634         }
10635 
10636         if (is_aa64) {
10637             addr += 0x400;
10638         } else {
10639             addr += 0x600;
10640         }
10641     } else if (pstate_read(env) & PSTATE_SP) {
10642         addr += 0x200;
10643     }
10644 
10645     switch (cs->exception_index) {
10646     case EXCP_PREFETCH_ABORT:
10647     case EXCP_DATA_ABORT:
10648         /*
10649          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10650          * to be taken to the SError vector entrypoint.
10651          */
10652         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10653             syndrome_is_sync_extabt(env->exception.syndrome)) {
10654             addr += 0x180;
10655         }
10656         env->cp15.far_el[new_el] = env->exception.vaddress;
10657         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10658                       env->cp15.far_el[new_el]);
10659         /* fall through */
10660     case EXCP_BKPT:
10661     case EXCP_UDEF:
10662     case EXCP_SWI:
10663     case EXCP_HVC:
10664     case EXCP_HYP_TRAP:
10665     case EXCP_SMC:
10666         switch (syn_get_ec(env->exception.syndrome)) {
10667         case EC_ADVSIMDFPACCESSTRAP:
10668             /*
10669              * QEMU internal FP/SIMD syndromes from AArch32 include the
10670              * TA and coproc fields which are only exposed if the exception
10671              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10672              * AArch64 format syndrome.
10673              */
10674             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10675             break;
10676         case EC_CP14RTTRAP:
10677         case EC_CP15RTTRAP:
10678         case EC_CP14DTTRAP:
10679             /*
10680              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10681              * the raw register field from the insn; when taking this to
10682              * AArch64 we must convert it to the AArch64 view of the register
10683              * number. Notice that we read a 4-bit AArch32 register number and
10684              * write back a 5-bit AArch64 one.
10685              */
10686             rt = extract32(env->exception.syndrome, 5, 4);
10687             rt = aarch64_regnum(env, rt);
10688             env->exception.syndrome = deposit32(env->exception.syndrome,
10689                                                 5, 5, rt);
10690             break;
10691         case EC_CP15RRTTRAP:
10692         case EC_CP14RRTTRAP:
10693             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10694             rt = extract32(env->exception.syndrome, 5, 4);
10695             rt = aarch64_regnum(env, rt);
10696             env->exception.syndrome = deposit32(env->exception.syndrome,
10697                                                 5, 5, rt);
10698             rt = extract32(env->exception.syndrome, 10, 4);
10699             rt = aarch64_regnum(env, rt);
10700             env->exception.syndrome = deposit32(env->exception.syndrome,
10701                                                 10, 5, rt);
10702             break;
10703         }
10704         env->cp15.esr_el[new_el] = env->exception.syndrome;
10705         break;
10706     case EXCP_IRQ:
10707     case EXCP_VIRQ:
10708         addr += 0x80;
10709         break;
10710     case EXCP_FIQ:
10711     case EXCP_VFIQ:
10712         addr += 0x100;
10713         break;
10714     case EXCP_VSERR:
10715         addr += 0x180;
10716         /* Construct the SError syndrome from IDS and ISS fields. */
10717         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10718         env->cp15.esr_el[new_el] = env->exception.syndrome;
10719         break;
10720     default:
10721         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10722     }
10723 
10724     if (is_a64(env)) {
10725         old_mode = pstate_read(env);
10726         aarch64_save_sp(env, arm_current_el(env));
10727         env->elr_el[new_el] = env->pc;
10728     } else {
10729         old_mode = cpsr_read_for_spsr_elx(env);
10730         env->elr_el[new_el] = env->regs[15];
10731 
10732         aarch64_sync_32_to_64(env);
10733 
10734         env->condexec_bits = 0;
10735     }
10736     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10737 
10738     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10739                   env->elr_el[new_el]);
10740 
10741     if (cpu_isar_feature(aa64_pan, cpu)) {
10742         /* The value of PSTATE.PAN is normally preserved, except when ... */
10743         new_mode |= old_mode & PSTATE_PAN;
10744         switch (new_el) {
10745         case 2:
10746             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10747             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10748                 != (HCR_E2H | HCR_TGE)) {
10749                 break;
10750             }
10751             /* fall through */
10752         case 1:
10753             /* ... the target is EL1 ... */
10754             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10755             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10756                 new_mode |= PSTATE_PAN;
10757             }
10758             break;
10759         }
10760     }
10761     if (cpu_isar_feature(aa64_mte, cpu)) {
10762         new_mode |= PSTATE_TCO;
10763     }
10764 
10765     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10766         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10767             new_mode |= PSTATE_SSBS;
10768         } else {
10769             new_mode &= ~PSTATE_SSBS;
10770         }
10771     }
10772 
10773     pstate_write(env, PSTATE_DAIF | new_mode);
10774     env->aarch64 = true;
10775     aarch64_restore_sp(env, new_el);
10776     helper_rebuild_hflags_a64(env, new_el);
10777 
10778     env->pc = addr;
10779 
10780     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10781                   new_el, env->pc, pstate_read(env));
10782 }
10783 
10784 /*
10785  * Do semihosting call and set the appropriate return value. All the
10786  * permission and validity checks have been done at translate time.
10787  *
10788  * We only see semihosting exceptions in TCG only as they are not
10789  * trapped to the hypervisor in KVM.
10790  */
10791 #ifdef CONFIG_TCG
10792 static void handle_semihosting(CPUState *cs)
10793 {
10794     ARMCPU *cpu = ARM_CPU(cs);
10795     CPUARMState *env = &cpu->env;
10796 
10797     if (is_a64(env)) {
10798         qemu_log_mask(CPU_LOG_INT,
10799                       "...handling as semihosting call 0x%" PRIx64 "\n",
10800                       env->xregs[0]);
10801         do_common_semihosting(cs);
10802         env->pc += 4;
10803     } else {
10804         qemu_log_mask(CPU_LOG_INT,
10805                       "...handling as semihosting call 0x%x\n",
10806                       env->regs[0]);
10807         do_common_semihosting(cs);
10808         env->regs[15] += env->thumb ? 2 : 4;
10809     }
10810 }
10811 #endif
10812 
10813 /*
10814  * Handle a CPU exception for A and R profile CPUs.
10815  * Do any appropriate logging, handle PSCI calls, and then hand off
10816  * to the AArch64-entry or AArch32-entry function depending on the
10817  * target exception level's register width.
10818  *
10819  * Note: this is used for both TCG (as the do_interrupt tcg op),
10820  *       and KVM to re-inject guest debug exceptions, and to
10821  *       inject a Synchronous-External-Abort.
10822  */
10823 void arm_cpu_do_interrupt(CPUState *cs)
10824 {
10825     ARMCPU *cpu = ARM_CPU(cs);
10826     CPUARMState *env = &cpu->env;
10827     unsigned int new_el = env->exception.target_el;
10828 
10829     assert(!arm_feature(env, ARM_FEATURE_M));
10830 
10831     arm_log_exception(cs);
10832     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10833                   new_el);
10834     if (qemu_loglevel_mask(CPU_LOG_INT)
10835         && !excp_is_internal(cs->exception_index)) {
10836         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10837                       syn_get_ec(env->exception.syndrome),
10838                       env->exception.syndrome);
10839     }
10840 
10841     if (arm_is_psci_call(cpu, cs->exception_index)) {
10842         arm_handle_psci_call(cpu);
10843         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10844         return;
10845     }
10846 
10847     /*
10848      * Semihosting semantics depend on the register width of the code
10849      * that caused the exception, not the target exception level, so
10850      * must be handled here.
10851      */
10852 #ifdef CONFIG_TCG
10853     if (cs->exception_index == EXCP_SEMIHOST) {
10854         handle_semihosting(cs);
10855         return;
10856     }
10857 #endif
10858 
10859     /*
10860      * Hooks may change global state so BQL should be held, also the
10861      * BQL needs to be held for any modification of
10862      * cs->interrupt_request.
10863      */
10864     g_assert(qemu_mutex_iothread_locked());
10865 
10866     arm_call_pre_el_change_hook(cpu);
10867 
10868     assert(!excp_is_internal(cs->exception_index));
10869     if (arm_el_is_aa64(env, new_el)) {
10870         arm_cpu_do_interrupt_aarch64(cs);
10871     } else {
10872         arm_cpu_do_interrupt_aarch32(cs);
10873     }
10874 
10875     arm_call_el_change_hook(cpu);
10876 
10877     if (!kvm_enabled()) {
10878         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10879     }
10880 }
10881 #endif /* !CONFIG_USER_ONLY */
10882 
10883 uint64_t arm_sctlr(CPUARMState *env, int el)
10884 {
10885     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10886     if (el == 0) {
10887         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10888         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10889     }
10890     return env->cp15.sctlr_el[el];
10891 }
10892 
10893 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10894 {
10895     if (regime_has_2_ranges(mmu_idx)) {
10896         return extract64(tcr, 37, 2);
10897     } else if (regime_is_stage2(mmu_idx)) {
10898         return 0; /* VTCR_EL2 */
10899     } else {
10900         /* Replicate the single TBI bit so we always have 2 bits.  */
10901         return extract32(tcr, 20, 1) * 3;
10902     }
10903 }
10904 
10905 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10906 {
10907     if (regime_has_2_ranges(mmu_idx)) {
10908         return extract64(tcr, 51, 2);
10909     } else if (regime_is_stage2(mmu_idx)) {
10910         return 0; /* VTCR_EL2 */
10911     } else {
10912         /* Replicate the single TBID bit so we always have 2 bits.  */
10913         return extract32(tcr, 29, 1) * 3;
10914     }
10915 }
10916 
10917 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10918 {
10919     if (regime_has_2_ranges(mmu_idx)) {
10920         return extract64(tcr, 57, 2);
10921     } else {
10922         /* Replicate the single TCMA bit so we always have 2 bits.  */
10923         return extract32(tcr, 30, 1) * 3;
10924     }
10925 }
10926 
10927 static ARMGranuleSize tg0_to_gran_size(int tg)
10928 {
10929     switch (tg) {
10930     case 0:
10931         return Gran4K;
10932     case 1:
10933         return Gran64K;
10934     case 2:
10935         return Gran16K;
10936     default:
10937         return GranInvalid;
10938     }
10939 }
10940 
10941 static ARMGranuleSize tg1_to_gran_size(int tg)
10942 {
10943     switch (tg) {
10944     case 1:
10945         return Gran16K;
10946     case 2:
10947         return Gran4K;
10948     case 3:
10949         return Gran64K;
10950     default:
10951         return GranInvalid;
10952     }
10953 }
10954 
10955 static inline bool have4k(ARMCPU *cpu, bool stage2)
10956 {
10957     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10958         : cpu_isar_feature(aa64_tgran4, cpu);
10959 }
10960 
10961 static inline bool have16k(ARMCPU *cpu, bool stage2)
10962 {
10963     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10964         : cpu_isar_feature(aa64_tgran16, cpu);
10965 }
10966 
10967 static inline bool have64k(ARMCPU *cpu, bool stage2)
10968 {
10969     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10970         : cpu_isar_feature(aa64_tgran64, cpu);
10971 }
10972 
10973 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10974                                          bool stage2)
10975 {
10976     switch (gran) {
10977     case Gran4K:
10978         if (have4k(cpu, stage2)) {
10979             return gran;
10980         }
10981         break;
10982     case Gran16K:
10983         if (have16k(cpu, stage2)) {
10984             return gran;
10985         }
10986         break;
10987     case Gran64K:
10988         if (have64k(cpu, stage2)) {
10989             return gran;
10990         }
10991         break;
10992     case GranInvalid:
10993         break;
10994     }
10995     /*
10996      * If the guest selects a granule size that isn't implemented,
10997      * the architecture requires that we behave as if it selected one
10998      * that is (with an IMPDEF choice of which one to pick). We choose
10999      * to implement the smallest supported granule size.
11000      */
11001     if (have4k(cpu, stage2)) {
11002         return Gran4K;
11003     }
11004     if (have16k(cpu, stage2)) {
11005         return Gran16K;
11006     }
11007     assert(have64k(cpu, stage2));
11008     return Gran64K;
11009 }
11010 
11011 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11012                                    ARMMMUIdx mmu_idx, bool data)
11013 {
11014     uint64_t tcr = regime_tcr(env, mmu_idx);
11015     bool epd, hpd, tsz_oob, ds, ha, hd;
11016     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11017     ARMGranuleSize gran;
11018     ARMCPU *cpu = env_archcpu(env);
11019     bool stage2 = regime_is_stage2(mmu_idx);
11020 
11021     if (!regime_has_2_ranges(mmu_idx)) {
11022         select = 0;
11023         tsz = extract32(tcr, 0, 6);
11024         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11025         if (stage2) {
11026             /* VTCR_EL2 */
11027             hpd = false;
11028         } else {
11029             hpd = extract32(tcr, 24, 1);
11030         }
11031         epd = false;
11032         sh = extract32(tcr, 12, 2);
11033         ps = extract32(tcr, 16, 3);
11034         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11035         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11036         ds = extract64(tcr, 32, 1);
11037     } else {
11038         bool e0pd;
11039 
11040         /*
11041          * Bit 55 is always between the two regions, and is canonical for
11042          * determining if address tagging is enabled.
11043          */
11044         select = extract64(va, 55, 1);
11045         if (!select) {
11046             tsz = extract32(tcr, 0, 6);
11047             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11048             epd = extract32(tcr, 7, 1);
11049             sh = extract32(tcr, 12, 2);
11050             hpd = extract64(tcr, 41, 1);
11051             e0pd = extract64(tcr, 55, 1);
11052         } else {
11053             tsz = extract32(tcr, 16, 6);
11054             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11055             epd = extract32(tcr, 23, 1);
11056             sh = extract32(tcr, 28, 2);
11057             hpd = extract64(tcr, 42, 1);
11058             e0pd = extract64(tcr, 56, 1);
11059         }
11060         ps = extract64(tcr, 32, 3);
11061         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11062         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11063         ds = extract64(tcr, 59, 1);
11064 
11065         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11066             regime_is_user(env, mmu_idx)) {
11067             epd = true;
11068         }
11069     }
11070 
11071     gran = sanitize_gran_size(cpu, gran, stage2);
11072 
11073     if (cpu_isar_feature(aa64_st, cpu)) {
11074         max_tsz = 48 - (gran == Gran64K);
11075     } else {
11076         max_tsz = 39;
11077     }
11078 
11079     /*
11080      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11081      * adjust the effective value of DS, as documented.
11082      */
11083     min_tsz = 16;
11084     if (gran == Gran64K) {
11085         if (cpu_isar_feature(aa64_lva, cpu)) {
11086             min_tsz = 12;
11087         }
11088         ds = false;
11089     } else if (ds) {
11090         if (regime_is_stage2(mmu_idx)) {
11091             if (gran == Gran16K) {
11092                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11093             } else {
11094                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11095             }
11096         } else {
11097             if (gran == Gran16K) {
11098                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11099             } else {
11100                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11101             }
11102         }
11103         if (ds) {
11104             min_tsz = 12;
11105         }
11106     }
11107 
11108     if (tsz > max_tsz) {
11109         tsz = max_tsz;
11110         tsz_oob = true;
11111     } else if (tsz < min_tsz) {
11112         tsz = min_tsz;
11113         tsz_oob = true;
11114     } else {
11115         tsz_oob = false;
11116     }
11117 
11118     /* Present TBI as a composite with TBID.  */
11119     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11120     if (!data) {
11121         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11122     }
11123     tbi = (tbi >> select) & 1;
11124 
11125     return (ARMVAParameters) {
11126         .tsz = tsz,
11127         .ps = ps,
11128         .sh = sh,
11129         .select = select,
11130         .tbi = tbi,
11131         .epd = epd,
11132         .hpd = hpd,
11133         .tsz_oob = tsz_oob,
11134         .ds = ds,
11135         .ha = ha,
11136         .hd = ha && hd,
11137         .gran = gran,
11138     };
11139 }
11140 
11141 /*
11142  * Note that signed overflow is undefined in C.  The following routines are
11143  * careful to use unsigned types where modulo arithmetic is required.
11144  * Failure to do so _will_ break on newer gcc.
11145  */
11146 
11147 /* Signed saturating arithmetic.  */
11148 
11149 /* Perform 16-bit signed saturating addition.  */
11150 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11151 {
11152     uint16_t res;
11153 
11154     res = a + b;
11155     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11156         if (a & 0x8000) {
11157             res = 0x8000;
11158         } else {
11159             res = 0x7fff;
11160         }
11161     }
11162     return res;
11163 }
11164 
11165 /* Perform 8-bit signed saturating addition.  */
11166 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11167 {
11168     uint8_t res;
11169 
11170     res = a + b;
11171     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11172         if (a & 0x80) {
11173             res = 0x80;
11174         } else {
11175             res = 0x7f;
11176         }
11177     }
11178     return res;
11179 }
11180 
11181 /* Perform 16-bit signed saturating subtraction.  */
11182 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11183 {
11184     uint16_t res;
11185 
11186     res = a - b;
11187     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11188         if (a & 0x8000) {
11189             res = 0x8000;
11190         } else {
11191             res = 0x7fff;
11192         }
11193     }
11194     return res;
11195 }
11196 
11197 /* Perform 8-bit signed saturating subtraction.  */
11198 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11199 {
11200     uint8_t res;
11201 
11202     res = a - b;
11203     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11204         if (a & 0x80) {
11205             res = 0x80;
11206         } else {
11207             res = 0x7f;
11208         }
11209     }
11210     return res;
11211 }
11212 
11213 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11214 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11215 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11216 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11217 #define PFX q
11218 
11219 #include "op_addsub.h"
11220 
11221 /* Unsigned saturating arithmetic.  */
11222 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11223 {
11224     uint16_t res;
11225     res = a + b;
11226     if (res < a) {
11227         res = 0xffff;
11228     }
11229     return res;
11230 }
11231 
11232 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11233 {
11234     if (a > b) {
11235         return a - b;
11236     } else {
11237         return 0;
11238     }
11239 }
11240 
11241 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11242 {
11243     uint8_t res;
11244     res = a + b;
11245     if (res < a) {
11246         res = 0xff;
11247     }
11248     return res;
11249 }
11250 
11251 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11252 {
11253     if (a > b) {
11254         return a - b;
11255     } else {
11256         return 0;
11257     }
11258 }
11259 
11260 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11261 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11262 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11263 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11264 #define PFX uq
11265 
11266 #include "op_addsub.h"
11267 
11268 /* Signed modulo arithmetic.  */
11269 #define SARITH16(a, b, n, op) do { \
11270     int32_t sum; \
11271     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11272     RESULT(sum, n, 16); \
11273     if (sum >= 0) \
11274         ge |= 3 << (n * 2); \
11275     } while (0)
11276 
11277 #define SARITH8(a, b, n, op) do { \
11278     int32_t sum; \
11279     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11280     RESULT(sum, n, 8); \
11281     if (sum >= 0) \
11282         ge |= 1 << n; \
11283     } while (0)
11284 
11285 
11286 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11287 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11288 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11289 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11290 #define PFX s
11291 #define ARITH_GE
11292 
11293 #include "op_addsub.h"
11294 
11295 /* Unsigned modulo arithmetic.  */
11296 #define ADD16(a, b, n) do { \
11297     uint32_t sum; \
11298     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11299     RESULT(sum, n, 16); \
11300     if ((sum >> 16) == 1) \
11301         ge |= 3 << (n * 2); \
11302     } while (0)
11303 
11304 #define ADD8(a, b, n) do { \
11305     uint32_t sum; \
11306     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11307     RESULT(sum, n, 8); \
11308     if ((sum >> 8) == 1) \
11309         ge |= 1 << n; \
11310     } while (0)
11311 
11312 #define SUB16(a, b, n) do { \
11313     uint32_t sum; \
11314     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11315     RESULT(sum, n, 16); \
11316     if ((sum >> 16) == 0) \
11317         ge |= 3 << (n * 2); \
11318     } while (0)
11319 
11320 #define SUB8(a, b, n) do { \
11321     uint32_t sum; \
11322     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11323     RESULT(sum, n, 8); \
11324     if ((sum >> 8) == 0) \
11325         ge |= 1 << n; \
11326     } while (0)
11327 
11328 #define PFX u
11329 #define ARITH_GE
11330 
11331 #include "op_addsub.h"
11332 
11333 /* Halved signed arithmetic.  */
11334 #define ADD16(a, b, n) \
11335   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11336 #define SUB16(a, b, n) \
11337   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11338 #define ADD8(a, b, n) \
11339   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11340 #define SUB8(a, b, n) \
11341   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11342 #define PFX sh
11343 
11344 #include "op_addsub.h"
11345 
11346 /* Halved unsigned arithmetic.  */
11347 #define ADD16(a, b, n) \
11348   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11349 #define SUB16(a, b, n) \
11350   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11351 #define ADD8(a, b, n) \
11352   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11353 #define SUB8(a, b, n) \
11354   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11355 #define PFX uh
11356 
11357 #include "op_addsub.h"
11358 
11359 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11360 {
11361     if (a > b) {
11362         return a - b;
11363     } else {
11364         return b - a;
11365     }
11366 }
11367 
11368 /* Unsigned sum of absolute byte differences.  */
11369 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11370 {
11371     uint32_t sum;
11372     sum = do_usad(a, b);
11373     sum += do_usad(a >> 8, b >> 8);
11374     sum += do_usad(a >> 16, b >> 16);
11375     sum += do_usad(a >> 24, b >> 24);
11376     return sum;
11377 }
11378 
11379 /* For ARMv6 SEL instruction.  */
11380 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11381 {
11382     uint32_t mask;
11383 
11384     mask = 0;
11385     if (flags & 1) {
11386         mask |= 0xff;
11387     }
11388     if (flags & 2) {
11389         mask |= 0xff00;
11390     }
11391     if (flags & 4) {
11392         mask |= 0xff0000;
11393     }
11394     if (flags & 8) {
11395         mask |= 0xff000000;
11396     }
11397     return (a & mask) | (b & ~mask);
11398 }
11399 
11400 /*
11401  * CRC helpers.
11402  * The upper bytes of val (above the number specified by 'bytes') must have
11403  * been zeroed out by the caller.
11404  */
11405 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11406 {
11407     uint8_t buf[4];
11408 
11409     stl_le_p(buf, val);
11410 
11411     /* zlib crc32 converts the accumulator and output to one's complement.  */
11412     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11413 }
11414 
11415 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11416 {
11417     uint8_t buf[4];
11418 
11419     stl_le_p(buf, val);
11420 
11421     /* Linux crc32c converts the output to one's complement.  */
11422     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11423 }
11424 
11425 /*
11426  * Return the exception level to which FP-disabled exceptions should
11427  * be taken, or 0 if FP is enabled.
11428  */
11429 int fp_exception_el(CPUARMState *env, int cur_el)
11430 {
11431 #ifndef CONFIG_USER_ONLY
11432     uint64_t hcr_el2;
11433 
11434     /*
11435      * CPACR and the CPTR registers don't exist before v6, so FP is
11436      * always accessible
11437      */
11438     if (!arm_feature(env, ARM_FEATURE_V6)) {
11439         return 0;
11440     }
11441 
11442     if (arm_feature(env, ARM_FEATURE_M)) {
11443         /* CPACR can cause a NOCP UsageFault taken to current security state */
11444         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11445             return 1;
11446         }
11447 
11448         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11449             if (!extract32(env->v7m.nsacr, 10, 1)) {
11450                 /* FP insns cause a NOCP UsageFault taken to Secure */
11451                 return 3;
11452             }
11453         }
11454 
11455         return 0;
11456     }
11457 
11458     hcr_el2 = arm_hcr_el2_eff(env);
11459 
11460     /*
11461      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11462      * 0, 2 : trap EL0 and EL1/PL1 accesses
11463      * 1    : trap only EL0 accesses
11464      * 3    : trap no accesses
11465      * This register is ignored if E2H+TGE are both set.
11466      */
11467     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11468         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11469 
11470         switch (fpen) {
11471         case 1:
11472             if (cur_el != 0) {
11473                 break;
11474             }
11475             /* fall through */
11476         case 0:
11477         case 2:
11478             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11479             if (!arm_el_is_aa64(env, 3)
11480                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11481                 return 3;
11482             }
11483             if (cur_el <= 1) {
11484                 return 1;
11485             }
11486             break;
11487         }
11488     }
11489 
11490     /*
11491      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11492      * to control non-secure access to the FPU. It doesn't have any
11493      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11494      */
11495     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11496          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11497         if (!extract32(env->cp15.nsacr, 10, 1)) {
11498             /* FP insns act as UNDEF */
11499             return cur_el == 2 ? 2 : 1;
11500         }
11501     }
11502 
11503     /*
11504      * CPTR_EL2 is present in v7VE or v8, and changes format
11505      * with HCR_EL2.E2H (regardless of TGE).
11506      */
11507     if (cur_el <= 2) {
11508         if (hcr_el2 & HCR_E2H) {
11509             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11510             case 1:
11511                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11512                     break;
11513                 }
11514                 /* fall through */
11515             case 0:
11516             case 2:
11517                 return 2;
11518             }
11519         } else if (arm_is_el2_enabled(env)) {
11520             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11521                 return 2;
11522             }
11523         }
11524     }
11525 
11526     /* CPTR_EL3 : present in v8 */
11527     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11528         /* Trap all FP ops to EL3 */
11529         return 3;
11530     }
11531 #endif
11532     return 0;
11533 }
11534 
11535 /* Return the exception level we're running at if this is our mmu_idx */
11536 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11537 {
11538     if (mmu_idx & ARM_MMU_IDX_M) {
11539         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11540     }
11541 
11542     switch (mmu_idx) {
11543     case ARMMMUIdx_E10_0:
11544     case ARMMMUIdx_E20_0:
11545         return 0;
11546     case ARMMMUIdx_E10_1:
11547     case ARMMMUIdx_E10_1_PAN:
11548         return 1;
11549     case ARMMMUIdx_E2:
11550     case ARMMMUIdx_E20_2:
11551     case ARMMMUIdx_E20_2_PAN:
11552         return 2;
11553     case ARMMMUIdx_E3:
11554         return 3;
11555     default:
11556         g_assert_not_reached();
11557     }
11558 }
11559 
11560 #ifndef CONFIG_TCG
11561 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11562 {
11563     g_assert_not_reached();
11564 }
11565 #endif
11566 
11567 static bool arm_pan_enabled(CPUARMState *env)
11568 {
11569     if (is_a64(env)) {
11570         return env->pstate & PSTATE_PAN;
11571     } else {
11572         return env->uncached_cpsr & CPSR_PAN;
11573     }
11574 }
11575 
11576 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11577 {
11578     ARMMMUIdx idx;
11579     uint64_t hcr;
11580 
11581     if (arm_feature(env, ARM_FEATURE_M)) {
11582         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11583     }
11584 
11585     /* See ARM pseudo-function ELIsInHost.  */
11586     switch (el) {
11587     case 0:
11588         hcr = arm_hcr_el2_eff(env);
11589         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11590             idx = ARMMMUIdx_E20_0;
11591         } else {
11592             idx = ARMMMUIdx_E10_0;
11593         }
11594         break;
11595     case 1:
11596         if (arm_pan_enabled(env)) {
11597             idx = ARMMMUIdx_E10_1_PAN;
11598         } else {
11599             idx = ARMMMUIdx_E10_1;
11600         }
11601         break;
11602     case 2:
11603         /* Note that TGE does not apply at EL2.  */
11604         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11605             if (arm_pan_enabled(env)) {
11606                 idx = ARMMMUIdx_E20_2_PAN;
11607             } else {
11608                 idx = ARMMMUIdx_E20_2;
11609             }
11610         } else {
11611             idx = ARMMMUIdx_E2;
11612         }
11613         break;
11614     case 3:
11615         return ARMMMUIdx_E3;
11616     default:
11617         g_assert_not_reached();
11618     }
11619 
11620     return idx;
11621 }
11622 
11623 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11624 {
11625     return arm_mmu_idx_el(env, arm_current_el(env));
11626 }
11627 
11628 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11629                                            ARMMMUIdx mmu_idx,
11630                                            CPUARMTBFlags flags)
11631 {
11632     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11633     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11634 
11635     if (arm_singlestep_active(env)) {
11636         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11637     }
11638     return flags;
11639 }
11640 
11641 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11642                                               ARMMMUIdx mmu_idx,
11643                                               CPUARMTBFlags flags)
11644 {
11645     bool sctlr_b = arm_sctlr_b(env);
11646 
11647     if (sctlr_b) {
11648         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11649     }
11650     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11651         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11652     }
11653     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11654 
11655     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11656 }
11657 
11658 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11659                                         ARMMMUIdx mmu_idx)
11660 {
11661     CPUARMTBFlags flags = {};
11662     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11663 
11664     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11665     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11666         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11667     }
11668 
11669     if (arm_v7m_is_handler_mode(env)) {
11670         DP_TBFLAG_M32(flags, HANDLER, 1);
11671     }
11672 
11673     /*
11674      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11675      * is suppressing them because the requested execution priority
11676      * is less than 0.
11677      */
11678     if (arm_feature(env, ARM_FEATURE_V8) &&
11679         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11680           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11681         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11682     }
11683 
11684     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11685         DP_TBFLAG_M32(flags, SECURE, 1);
11686     }
11687 
11688     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11689 }
11690 
11691 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11692                                         ARMMMUIdx mmu_idx)
11693 {
11694     CPUARMTBFlags flags = {};
11695     int el = arm_current_el(env);
11696 
11697     if (arm_sctlr(env, el) & SCTLR_A) {
11698         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11699     }
11700 
11701     if (arm_el_is_aa64(env, 1)) {
11702         DP_TBFLAG_A32(flags, VFPEN, 1);
11703     }
11704 
11705     if (el < 2 && env->cp15.hstr_el2 &&
11706         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11707         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11708     }
11709 
11710     if (env->uncached_cpsr & CPSR_IL) {
11711         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11712     }
11713 
11714     /*
11715      * The SME exception we are testing for is raised via
11716      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11717      * AArch32.CheckAdvSIMDOrFPEnabled().
11718      */
11719     if (el == 0
11720         && FIELD_EX64(env->svcr, SVCR, SM)
11721         && (!arm_is_el2_enabled(env)
11722             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11723         && arm_el_is_aa64(env, 1)
11724         && !sme_fa64(env, el)) {
11725         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11726     }
11727 
11728     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11729 }
11730 
11731 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11732                                         ARMMMUIdx mmu_idx)
11733 {
11734     CPUARMTBFlags flags = {};
11735     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11736     uint64_t tcr = regime_tcr(env, mmu_idx);
11737     uint64_t sctlr;
11738     int tbii, tbid;
11739 
11740     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11741 
11742     /* Get control bits for tagged addresses.  */
11743     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11744     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11745 
11746     DP_TBFLAG_A64(flags, TBII, tbii);
11747     DP_TBFLAG_A64(flags, TBID, tbid);
11748 
11749     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11750         int sve_el = sve_exception_el(env, el);
11751 
11752         /*
11753          * If either FP or SVE are disabled, translator does not need len.
11754          * If SVE EL > FP EL, FP exception has precedence, and translator
11755          * does not need SVE EL.  Save potential re-translations by forcing
11756          * the unneeded data to zero.
11757          */
11758         if (fp_el != 0) {
11759             if (sve_el > fp_el) {
11760                 sve_el = 0;
11761             }
11762         } else if (sve_el == 0) {
11763             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11764         }
11765         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11766     }
11767     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11768         int sme_el = sme_exception_el(env, el);
11769         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11770 
11771         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11772         if (sme_el == 0) {
11773             /* Similarly, do not compute SVL if SME is disabled. */
11774             int svl = sve_vqm1_for_el_sm(env, el, true);
11775             DP_TBFLAG_A64(flags, SVL, svl);
11776             if (sm) {
11777                 /* If SVE is disabled, we will not have set VL above. */
11778                 DP_TBFLAG_A64(flags, VL, svl);
11779             }
11780         }
11781         if (sm) {
11782             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11783             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11784         }
11785         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11786     }
11787 
11788     sctlr = regime_sctlr(env, stage1);
11789 
11790     if (sctlr & SCTLR_A) {
11791         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11792     }
11793 
11794     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11795         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11796     }
11797 
11798     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11799         /*
11800          * In order to save space in flags, we record only whether
11801          * pauth is "inactive", meaning all insns are implemented as
11802          * a nop, or "active" when some action must be performed.
11803          * The decision of which action to take is left to a helper.
11804          */
11805         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11806             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11807         }
11808     }
11809 
11810     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11811         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11812         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11813             DP_TBFLAG_A64(flags, BT, 1);
11814         }
11815     }
11816 
11817     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11818     if (!(env->pstate & PSTATE_UAO)) {
11819         switch (mmu_idx) {
11820         case ARMMMUIdx_E10_1:
11821         case ARMMMUIdx_E10_1_PAN:
11822             /* TODO: ARMv8.3-NV */
11823             DP_TBFLAG_A64(flags, UNPRIV, 1);
11824             break;
11825         case ARMMMUIdx_E20_2:
11826         case ARMMMUIdx_E20_2_PAN:
11827             /*
11828              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11829              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11830              */
11831             if (env->cp15.hcr_el2 & HCR_TGE) {
11832                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11833             }
11834             break;
11835         default:
11836             break;
11837         }
11838     }
11839 
11840     if (env->pstate & PSTATE_IL) {
11841         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11842     }
11843 
11844     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11845         /*
11846          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11847          * if all accesses must be Unchecked:
11848          * 1) If no TBI, then there are no tags in the address to check,
11849          * 2) If Tag Check Override, then all accesses are Unchecked,
11850          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11851          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11852          */
11853         if (allocation_tag_access_enabled(env, el, sctlr)) {
11854             DP_TBFLAG_A64(flags, ATA, 1);
11855             if (tbid
11856                 && !(env->pstate & PSTATE_TCO)
11857                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11858                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11859             }
11860         }
11861         /* And again for unprivileged accesses, if required.  */
11862         if (EX_TBFLAG_A64(flags, UNPRIV)
11863             && tbid
11864             && !(env->pstate & PSTATE_TCO)
11865             && (sctlr & SCTLR_TCF0)
11866             && allocation_tag_access_enabled(env, 0, sctlr)) {
11867             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11868         }
11869         /* Cache TCMA as well as TBI. */
11870         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11871     }
11872 
11873     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11874 }
11875 
11876 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11877 {
11878     int el = arm_current_el(env);
11879     int fp_el = fp_exception_el(env, el);
11880     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11881 
11882     if (is_a64(env)) {
11883         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11884     } else if (arm_feature(env, ARM_FEATURE_M)) {
11885         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11886     } else {
11887         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11888     }
11889 }
11890 
11891 void arm_rebuild_hflags(CPUARMState *env)
11892 {
11893     env->hflags = rebuild_hflags_internal(env);
11894 }
11895 
11896 /*
11897  * If we have triggered a EL state change we can't rely on the
11898  * translator having passed it to us, we need to recompute.
11899  */
11900 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11901 {
11902     int el = arm_current_el(env);
11903     int fp_el = fp_exception_el(env, el);
11904     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11905 
11906     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11907 }
11908 
11909 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11910 {
11911     int fp_el = fp_exception_el(env, el);
11912     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11913 
11914     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11915 }
11916 
11917 /*
11918  * If we have triggered a EL state change we can't rely on the
11919  * translator having passed it to us, we need to recompute.
11920  */
11921 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11922 {
11923     int el = arm_current_el(env);
11924     int fp_el = fp_exception_el(env, el);
11925     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11926     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11927 }
11928 
11929 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11930 {
11931     int fp_el = fp_exception_el(env, el);
11932     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11933 
11934     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11935 }
11936 
11937 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11938 {
11939     int fp_el = fp_exception_el(env, el);
11940     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11941 
11942     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11943 }
11944 
11945 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11946 {
11947 #ifdef CONFIG_DEBUG_TCG
11948     CPUARMTBFlags c = env->hflags;
11949     CPUARMTBFlags r = rebuild_hflags_internal(env);
11950 
11951     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11952         fprintf(stderr, "TCG hflags mismatch "
11953                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11954                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11955                 c.flags, c.flags2, r.flags, r.flags2);
11956         abort();
11957     }
11958 #endif
11959 }
11960 
11961 static bool mve_no_pred(CPUARMState *env)
11962 {
11963     /*
11964      * Return true if there is definitely no predication of MVE
11965      * instructions by VPR or LTPSIZE. (Returning false even if there
11966      * isn't any predication is OK; generated code will just be
11967      * a little worse.)
11968      * If the CPU does not implement MVE then this TB flag is always 0.
11969      *
11970      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11971      * logic in gen_update_fp_context() needs to be updated to match.
11972      *
11973      * We do not include the effect of the ECI bits here -- they are
11974      * tracked in other TB flags. This simplifies the logic for
11975      * "when did we emit code that changes the MVE_NO_PRED TB flag
11976      * and thus need to end the TB?".
11977      */
11978     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11979         return false;
11980     }
11981     if (env->v7m.vpr) {
11982         return false;
11983     }
11984     if (env->v7m.ltpsize < 4) {
11985         return false;
11986     }
11987     return true;
11988 }
11989 
11990 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11991                           target_ulong *cs_base, uint32_t *pflags)
11992 {
11993     CPUARMTBFlags flags;
11994 
11995     assert_hflags_rebuild_correctly(env);
11996     flags = env->hflags;
11997 
11998     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11999         *pc = env->pc;
12000         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12001             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12002         }
12003     } else {
12004         *pc = env->regs[15];
12005 
12006         if (arm_feature(env, ARM_FEATURE_M)) {
12007             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12008                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12009                 != env->v7m.secure) {
12010                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12011             }
12012 
12013             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12014                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12015                  (env->v7m.secure &&
12016                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12017                 /*
12018                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12019                  * active FP context; we must create a new FP context before
12020                  * executing any FP insn.
12021                  */
12022                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12023             }
12024 
12025             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12026             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12027                 DP_TBFLAG_M32(flags, LSPACT, 1);
12028             }
12029 
12030             if (mve_no_pred(env)) {
12031                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12032             }
12033         } else {
12034             /*
12035              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12036              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12037              */
12038             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12039                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12040             } else {
12041                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12042                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12043             }
12044             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12045                 DP_TBFLAG_A32(flags, VFPEN, 1);
12046             }
12047         }
12048 
12049         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12050         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12051     }
12052 
12053     /*
12054      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12055      * states defined in the ARM ARM for software singlestep:
12056      *  SS_ACTIVE   PSTATE.SS   State
12057      *     0            x       Inactive (the TB flag for SS is always 0)
12058      *     1            0       Active-pending
12059      *     1            1       Active-not-pending
12060      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12061      */
12062     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12063         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12064     }
12065 
12066     *pflags = flags.flags;
12067     *cs_base = flags.flags2;
12068 }
12069 
12070 #ifdef TARGET_AARCH64
12071 /*
12072  * The manual says that when SVE is enabled and VQ is widened the
12073  * implementation is allowed to zero the previously inaccessible
12074  * portion of the registers.  The corollary to that is that when
12075  * SVE is enabled and VQ is narrowed we are also allowed to zero
12076  * the now inaccessible portion of the registers.
12077  *
12078  * The intent of this is that no predicate bit beyond VQ is ever set.
12079  * Which means that some operations on predicate registers themselves
12080  * may operate on full uint64_t or even unrolled across the maximum
12081  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12082  * may well be cheaper than conditionals to restrict the operation
12083  * to the relevant portion of a uint16_t[16].
12084  */
12085 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12086 {
12087     int i, j;
12088     uint64_t pmask;
12089 
12090     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12091     assert(vq <= env_archcpu(env)->sve_max_vq);
12092 
12093     /* Zap the high bits of the zregs.  */
12094     for (i = 0; i < 32; i++) {
12095         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12096     }
12097 
12098     /* Zap the high bits of the pregs and ffr.  */
12099     pmask = 0;
12100     if (vq & 3) {
12101         pmask = ~(-1ULL << (16 * (vq & 3)));
12102     }
12103     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12104         for (i = 0; i < 17; ++i) {
12105             env->vfp.pregs[i].p[j] &= pmask;
12106         }
12107         pmask = 0;
12108     }
12109 }
12110 
12111 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12112 {
12113     int exc_el;
12114 
12115     if (sm) {
12116         exc_el = sme_exception_el(env, el);
12117     } else {
12118         exc_el = sve_exception_el(env, el);
12119     }
12120     if (exc_el) {
12121         return 0; /* disabled */
12122     }
12123     return sve_vqm1_for_el_sm(env, el, sm);
12124 }
12125 
12126 /*
12127  * Notice a change in SVE vector size when changing EL.
12128  */
12129 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12130                            int new_el, bool el0_a64)
12131 {
12132     ARMCPU *cpu = env_archcpu(env);
12133     int old_len, new_len;
12134     bool old_a64, new_a64, sm;
12135 
12136     /* Nothing to do if no SVE.  */
12137     if (!cpu_isar_feature(aa64_sve, cpu)) {
12138         return;
12139     }
12140 
12141     /* Nothing to do if FP is disabled in either EL.  */
12142     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12143         return;
12144     }
12145 
12146     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12147     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12148 
12149     /*
12150      * Both AArch64.TakeException and AArch64.ExceptionReturn
12151      * invoke ResetSVEState when taking an exception from, or
12152      * returning to, AArch32 state when PSTATE.SM is enabled.
12153      */
12154     sm = FIELD_EX64(env->svcr, SVCR, SM);
12155     if (old_a64 != new_a64 && sm) {
12156         arm_reset_sve_state(env);
12157         return;
12158     }
12159 
12160     /*
12161      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12162      * at ELx, or not available because the EL is in AArch32 state, then
12163      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12164      * has an effective value of 0".
12165      *
12166      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12167      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12168      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12169      * we already have the correct register contents when encountering the
12170      * vq0->vq0 transition between EL0->EL1.
12171      */
12172     old_len = new_len = 0;
12173     if (old_a64) {
12174         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12175     }
12176     if (new_a64) {
12177         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12178     }
12179 
12180     /* When changing vector length, clear inaccessible state.  */
12181     if (new_len < old_len) {
12182         aarch64_sve_narrow_vq(env, new_len + 1);
12183     }
12184 }
12185 #endif
12186