xref: /openbmc/qemu/target/arm/helper.c (revision f4318557)
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     if (change == 0) {
6742         return;
6743     }
6744     env->svcr ^= change;
6745 
6746     if (change & R_SVCR_SM_MASK) {
6747         arm_reset_sve_state(env);
6748     }
6749 
6750     /*
6751      * ResetSMEState.
6752      *
6753      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6754      * on enable: while disabled, the storage is inaccessible and the
6755      * value does not matter.  We're not saving the storage in vmstate
6756      * when disabled either.
6757      */
6758     if (change & new & R_SVCR_ZA_MASK) {
6759         memset(env->zarray, 0, sizeof(env->zarray));
6760     }
6761 
6762     arm_rebuild_hflags(env);
6763 }
6764 
6765 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6766                        uint64_t value)
6767 {
6768     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6769     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6770     aarch64_set_svcr(env, value, -1);
6771 }
6772 
6773 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6774                        uint64_t value)
6775 {
6776     int cur_el = arm_current_el(env);
6777     int old_len = sve_vqm1_for_el(env, cur_el);
6778     int new_len;
6779 
6780     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6781     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6782     raw_write(env, ri, value);
6783 
6784     /*
6785      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6786      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6787      * current values for simplicity.  But for QEMU internals, we must still
6788      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6789      * above aarch64_sve_narrow_vq.
6790      */
6791     new_len = sve_vqm1_for_el(env, cur_el);
6792     if (new_len < old_len) {
6793         aarch64_sve_narrow_vq(env, new_len + 1);
6794     }
6795 }
6796 
6797 static const ARMCPRegInfo sme_reginfo[] = {
6798     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6799       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6800       .access = PL0_RW, .accessfn = access_tpidr2,
6801       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6802     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6804       .access = PL0_RW, .type = ARM_CP_SME,
6805       .fieldoffset = offsetof(CPUARMState, svcr),
6806       .writefn = svcr_write, .raw_writefn = raw_write },
6807     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6808       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6809       .access = PL1_RW, .type = ARM_CP_SME,
6810       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6811       .writefn = smcr_write, .raw_writefn = raw_write },
6812     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6813       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6814       .access = PL2_RW, .type = ARM_CP_SME,
6815       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6816       .writefn = smcr_write, .raw_writefn = raw_write },
6817     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6818       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6819       .access = PL3_RW, .type = ARM_CP_SME,
6820       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6821       .writefn = smcr_write, .raw_writefn = raw_write },
6822     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6823       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6824       .access = PL1_R, .accessfn = access_aa64_tid1,
6825       /*
6826        * IMPLEMENTOR = 0 (software)
6827        * REVISION    = 0 (implementation defined)
6828        * SMPS        = 0 (no streaming execution priority in QEMU)
6829        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6830        */
6831       .type = ARM_CP_CONST, .resetvalue = 0, },
6832     /*
6833      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6834      */
6835     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6836       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6837       .access = PL1_RW, .accessfn = access_esm,
6838       .type = ARM_CP_CONST, .resetvalue = 0 },
6839     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6840       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6841       .access = PL2_RW, .accessfn = access_esm,
6842       .type = ARM_CP_CONST, .resetvalue = 0 },
6843 };
6844 #endif /* TARGET_AARCH64 */
6845 
6846 static void define_pmu_regs(ARMCPU *cpu)
6847 {
6848     /*
6849      * v7 performance monitor control register: same implementor
6850      * field as main ID register, and we implement four counters in
6851      * addition to the cycle count register.
6852      */
6853     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6854     ARMCPRegInfo pmcr = {
6855         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6856         .access = PL0_RW,
6857         .type = ARM_CP_IO | ARM_CP_ALIAS,
6858         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6859         .accessfn = pmreg_access, .writefn = pmcr_write,
6860         .raw_writefn = raw_write,
6861     };
6862     ARMCPRegInfo pmcr64 = {
6863         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6864         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6865         .access = PL0_RW, .accessfn = pmreg_access,
6866         .type = ARM_CP_IO,
6867         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6868         .resetvalue = cpu->isar.reset_pmcr_el0,
6869         .writefn = pmcr_write, .raw_writefn = raw_write,
6870     };
6871 
6872     define_one_arm_cp_reg(cpu, &pmcr);
6873     define_one_arm_cp_reg(cpu, &pmcr64);
6874     for (i = 0; i < pmcrn; i++) {
6875         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6876         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6877         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6878         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6879         ARMCPRegInfo pmev_regs[] = {
6880             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6881               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6882               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6883               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6884               .accessfn = pmreg_access_xevcntr },
6885             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6886               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6887               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6888               .type = ARM_CP_IO,
6889               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6890               .raw_readfn = pmevcntr_rawread,
6891               .raw_writefn = pmevcntr_rawwrite },
6892             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6893               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6894               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6895               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6896               .accessfn = pmreg_access },
6897             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6898               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6899               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6900               .type = ARM_CP_IO,
6901               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6902               .raw_writefn = pmevtyper_rawwrite },
6903         };
6904         define_arm_cp_regs(cpu, pmev_regs);
6905         g_free(pmevcntr_name);
6906         g_free(pmevcntr_el0_name);
6907         g_free(pmevtyper_name);
6908         g_free(pmevtyper_el0_name);
6909     }
6910     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6911         ARMCPRegInfo v81_pmu_regs[] = {
6912             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6913               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6914               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6915               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6916             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6917               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6918               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6919               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6920         };
6921         define_arm_cp_regs(cpu, v81_pmu_regs);
6922     }
6923     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6924         static const ARMCPRegInfo v84_pmmir = {
6925             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6926             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6927             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6928             .resetvalue = 0
6929         };
6930         define_one_arm_cp_reg(cpu, &v84_pmmir);
6931     }
6932 }
6933 
6934 /*
6935  * We don't know until after realize whether there's a GICv3
6936  * attached, and that is what registers the gicv3 sysregs.
6937  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6938  * at runtime.
6939  */
6940 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6941 {
6942     ARMCPU *cpu = env_archcpu(env);
6943     uint64_t pfr1 = cpu->isar.id_pfr1;
6944 
6945     if (env->gicv3state) {
6946         pfr1 |= 1 << 28;
6947     }
6948     return pfr1;
6949 }
6950 
6951 #ifndef CONFIG_USER_ONLY
6952 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6953 {
6954     ARMCPU *cpu = env_archcpu(env);
6955     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6956 
6957     if (env->gicv3state) {
6958         pfr0 |= 1 << 24;
6959     }
6960     return pfr0;
6961 }
6962 #endif
6963 
6964 /*
6965  * Shared logic between LORID and the rest of the LOR* registers.
6966  * Secure state exclusion has already been dealt with.
6967  */
6968 static CPAccessResult access_lor_ns(CPUARMState *env,
6969                                     const ARMCPRegInfo *ri, bool isread)
6970 {
6971     int el = arm_current_el(env);
6972 
6973     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6974         return CP_ACCESS_TRAP_EL2;
6975     }
6976     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6977         return CP_ACCESS_TRAP_EL3;
6978     }
6979     return CP_ACCESS_OK;
6980 }
6981 
6982 static CPAccessResult access_lor_other(CPUARMState *env,
6983                                        const ARMCPRegInfo *ri, bool isread)
6984 {
6985     if (arm_is_secure_below_el3(env)) {
6986         /* Access denied in secure mode.  */
6987         return CP_ACCESS_TRAP;
6988     }
6989     return access_lor_ns(env, ri, isread);
6990 }
6991 
6992 /*
6993  * A trivial implementation of ARMv8.1-LOR leaves all of these
6994  * registers fixed at 0, which indicates that there are zero
6995  * supported Limited Ordering regions.
6996  */
6997 static const ARMCPRegInfo lor_reginfo[] = {
6998     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6999       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7000       .access = PL1_RW, .accessfn = access_lor_other,
7001       .type = ARM_CP_CONST, .resetvalue = 0 },
7002     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7003       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7004       .access = PL1_RW, .accessfn = access_lor_other,
7005       .type = ARM_CP_CONST, .resetvalue = 0 },
7006     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7007       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7008       .access = PL1_RW, .accessfn = access_lor_other,
7009       .type = ARM_CP_CONST, .resetvalue = 0 },
7010     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7011       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7012       .access = PL1_RW, .accessfn = access_lor_other,
7013       .type = ARM_CP_CONST, .resetvalue = 0 },
7014     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7015       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7016       .access = PL1_R, .accessfn = access_lor_ns,
7017       .type = ARM_CP_CONST, .resetvalue = 0 },
7018 };
7019 
7020 #ifdef TARGET_AARCH64
7021 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7022                                    bool isread)
7023 {
7024     int el = arm_current_el(env);
7025 
7026     if (el < 2 &&
7027         arm_is_el2_enabled(env) &&
7028         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7029         return CP_ACCESS_TRAP_EL2;
7030     }
7031     if (el < 3 &&
7032         arm_feature(env, ARM_FEATURE_EL3) &&
7033         !(env->cp15.scr_el3 & SCR_APK)) {
7034         return CP_ACCESS_TRAP_EL3;
7035     }
7036     return CP_ACCESS_OK;
7037 }
7038 
7039 static const ARMCPRegInfo pauth_reginfo[] = {
7040     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7042       .access = PL1_RW, .accessfn = access_pauth,
7043       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7044     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7046       .access = PL1_RW, .accessfn = access_pauth,
7047       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7048     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7050       .access = PL1_RW, .accessfn = access_pauth,
7051       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7052     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7053       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7054       .access = PL1_RW, .accessfn = access_pauth,
7055       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7056     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7058       .access = PL1_RW, .accessfn = access_pauth,
7059       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7060     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7062       .access = PL1_RW, .accessfn = access_pauth,
7063       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7064     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7065       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7066       .access = PL1_RW, .accessfn = access_pauth,
7067       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7068     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7069       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7070       .access = PL1_RW, .accessfn = access_pauth,
7071       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7072     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7073       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7074       .access = PL1_RW, .accessfn = access_pauth,
7075       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7076     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7078       .access = PL1_RW, .accessfn = access_pauth,
7079       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7080 };
7081 
7082 static const ARMCPRegInfo tlbirange_reginfo[] = {
7083     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7084       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7085       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7086       .writefn = tlbi_aa64_rvae1is_write },
7087     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7088       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7089       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7090       .writefn = tlbi_aa64_rvae1is_write },
7091    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7092       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7093       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7094       .writefn = tlbi_aa64_rvae1is_write },
7095     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7096       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7097       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7098       .writefn = tlbi_aa64_rvae1is_write },
7099     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7100       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7101       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7102       .writefn = tlbi_aa64_rvae1is_write },
7103     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7105       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7106       .writefn = tlbi_aa64_rvae1is_write },
7107    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7108       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7109       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7110       .writefn = tlbi_aa64_rvae1is_write },
7111     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7112       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7113       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7114       .writefn = tlbi_aa64_rvae1is_write },
7115     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7116       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7117       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7118       .writefn = tlbi_aa64_rvae1_write },
7119     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7121       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7122       .writefn = tlbi_aa64_rvae1_write },
7123    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7125       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7126       .writefn = tlbi_aa64_rvae1_write },
7127     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7129       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7130       .writefn = tlbi_aa64_rvae1_write },
7131     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7133       .access = PL2_W, .type = ARM_CP_NO_RAW,
7134       .writefn = tlbi_aa64_ripas2e1is_write },
7135     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7136       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7137       .access = PL2_W, .type = ARM_CP_NO_RAW,
7138       .writefn = tlbi_aa64_ripas2e1is_write },
7139     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7140       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .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_RVALE2IS", .state = ARM_CP_STATE_AA64,
7144       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .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_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7148       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7149       .access = PL2_W, .type = ARM_CP_NO_RAW,
7150       .writefn = tlbi_aa64_ripas2e1_write },
7151     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7152       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7153       .access = PL2_W, .type = ARM_CP_NO_RAW,
7154       .writefn = tlbi_aa64_ripas2e1_write },
7155    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7156       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7157       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7158       .writefn = tlbi_aa64_rvae2is_write },
7159    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7160       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7161       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7162       .writefn = tlbi_aa64_rvae2is_write },
7163     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7164       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7165       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7166       .writefn = tlbi_aa64_rvae2_write },
7167    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7168       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7169       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7170       .writefn = tlbi_aa64_rvae2_write },
7171    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7172       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7173       .access = PL3_W, .type = ARM_CP_NO_RAW,
7174       .writefn = tlbi_aa64_rvae3is_write },
7175    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7177       .access = PL3_W, .type = ARM_CP_NO_RAW,
7178       .writefn = tlbi_aa64_rvae3is_write },
7179    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7180       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7181       .access = PL3_W, .type = ARM_CP_NO_RAW,
7182       .writefn = tlbi_aa64_rvae3is_write },
7183    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7184       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7185       .access = PL3_W, .type = ARM_CP_NO_RAW,
7186       .writefn = tlbi_aa64_rvae3is_write },
7187    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7188       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7189       .access = PL3_W, .type = ARM_CP_NO_RAW,
7190       .writefn = tlbi_aa64_rvae3_write },
7191    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7192       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7193       .access = PL3_W, .type = ARM_CP_NO_RAW,
7194       .writefn = tlbi_aa64_rvae3_write },
7195 };
7196 
7197 static const ARMCPRegInfo tlbios_reginfo[] = {
7198     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7199       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7200       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7201       .writefn = tlbi_aa64_vmalle1is_write },
7202     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7203       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7204       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7205       .writefn = tlbi_aa64_vae1is_write },
7206     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7207       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7208       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7209       .writefn = tlbi_aa64_vmalle1is_write },
7210     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7211       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7212       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7213       .writefn = tlbi_aa64_vae1is_write },
7214     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7215       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7216       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7217       .writefn = tlbi_aa64_vae1is_write },
7218     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7219       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7220       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7221       .writefn = tlbi_aa64_vae1is_write },
7222     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7223       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7224       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7225       .writefn = tlbi_aa64_alle2is_write },
7226     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7227       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7228       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7229       .writefn = tlbi_aa64_vae2is_write },
7230    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7231       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7232       .access = PL2_W, .type = ARM_CP_NO_RAW,
7233       .writefn = tlbi_aa64_alle1is_write },
7234     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7235       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7236       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7237       .writefn = tlbi_aa64_vae2is_write },
7238     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7239       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7240       .access = PL2_W, .type = ARM_CP_NO_RAW,
7241       .writefn = tlbi_aa64_alle1is_write },
7242     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7243       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7244       .access = PL2_W, .type = ARM_CP_NOP },
7245     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7246       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7247       .access = PL2_W, .type = ARM_CP_NOP },
7248     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7249       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7250       .access = PL2_W, .type = ARM_CP_NOP },
7251     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7252       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7253       .access = PL2_W, .type = ARM_CP_NOP },
7254     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7255       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7256       .access = PL3_W, .type = ARM_CP_NO_RAW,
7257       .writefn = tlbi_aa64_alle3is_write },
7258     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7259       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7260       .access = PL3_W, .type = ARM_CP_NO_RAW,
7261       .writefn = tlbi_aa64_vae3is_write },
7262     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7263       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7264       .access = PL3_W, .type = ARM_CP_NO_RAW,
7265       .writefn = tlbi_aa64_vae3is_write },
7266 };
7267 
7268 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7269 {
7270     Error *err = NULL;
7271     uint64_t ret;
7272 
7273     /* Success sets NZCV = 0000.  */
7274     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7275 
7276     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7277         /*
7278          * ??? Failed, for unknown reasons in the crypto subsystem.
7279          * The best we can do is log the reason and return the
7280          * timed-out indication to the guest.  There is no reason
7281          * we know to expect this failure to be transitory, so the
7282          * guest may well hang retrying the operation.
7283          */
7284         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7285                       ri->name, error_get_pretty(err));
7286         error_free(err);
7287 
7288         env->ZF = 0; /* NZCF = 0100 */
7289         return 0;
7290     }
7291     return ret;
7292 }
7293 
7294 /* We do not support re-seeding, so the two registers operate the same.  */
7295 static const ARMCPRegInfo rndr_reginfo[] = {
7296     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7297       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7298       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7299       .access = PL0_R, .readfn = rndr_readfn },
7300     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7301       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7302       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7303       .access = PL0_R, .readfn = rndr_readfn },
7304 };
7305 
7306 #ifndef CONFIG_USER_ONLY
7307 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7308                           uint64_t value)
7309 {
7310     ARMCPU *cpu = env_archcpu(env);
7311     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7312     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7313     uint64_t vaddr_in = (uint64_t) value;
7314     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7315     void *haddr;
7316     int mem_idx = cpu_mmu_index(env, false);
7317 
7318     /* This won't be crossing page boundaries */
7319     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7320     if (haddr) {
7321 
7322         ram_addr_t offset;
7323         MemoryRegion *mr;
7324 
7325         /* RCU lock is already being held */
7326         mr = memory_region_from_host(haddr, &offset);
7327 
7328         if (mr) {
7329             memory_region_writeback(mr, offset, dline_size);
7330         }
7331     }
7332 }
7333 
7334 static const ARMCPRegInfo dcpop_reg[] = {
7335     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7336       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7337       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7338       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7339 };
7340 
7341 static const ARMCPRegInfo dcpodp_reg[] = {
7342     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7343       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7344       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7345       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7346 };
7347 #endif /*CONFIG_USER_ONLY*/
7348 
7349 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7350                                        bool isread)
7351 {
7352     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7353         return CP_ACCESS_TRAP_EL2;
7354     }
7355 
7356     return CP_ACCESS_OK;
7357 }
7358 
7359 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7360                                  bool isread)
7361 {
7362     int el = arm_current_el(env);
7363 
7364     if (el < 2 && arm_is_el2_enabled(env)) {
7365         uint64_t hcr = arm_hcr_el2_eff(env);
7366         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7367             return CP_ACCESS_TRAP_EL2;
7368         }
7369     }
7370     if (el < 3 &&
7371         arm_feature(env, ARM_FEATURE_EL3) &&
7372         !(env->cp15.scr_el3 & SCR_ATA)) {
7373         return CP_ACCESS_TRAP_EL3;
7374     }
7375     return CP_ACCESS_OK;
7376 }
7377 
7378 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7379 {
7380     return env->pstate & PSTATE_TCO;
7381 }
7382 
7383 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7384 {
7385     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7386 }
7387 
7388 static const ARMCPRegInfo mte_reginfo[] = {
7389     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7390       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7391       .access = PL1_RW, .accessfn = access_mte,
7392       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7393     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7394       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7395       .access = PL1_RW, .accessfn = access_mte,
7396       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7397     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7398       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7399       .access = PL2_RW, .accessfn = access_mte,
7400       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7401     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7402       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7403       .access = PL3_RW,
7404       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7405     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7406       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7407       .access = PL1_RW, .accessfn = access_mte,
7408       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7409     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7410       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7411       .access = PL1_RW, .accessfn = access_mte,
7412       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7413     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7414       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7415       .access = PL1_R, .accessfn = access_aa64_tid5,
7416       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7417     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7418       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7419       .type = ARM_CP_NO_RAW,
7420       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7421     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7422       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7423       .type = ARM_CP_NOP, .access = PL1_W,
7424       .accessfn = aa64_cacheop_poc_access },
7425     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7426       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7427       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7428     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7429       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7430       .type = ARM_CP_NOP, .access = PL1_W,
7431       .accessfn = aa64_cacheop_poc_access },
7432     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7433       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7434       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7435     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7436       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7437       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7438     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7439       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7440       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7441     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7443       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7444     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7445       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7446       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7447 };
7448 
7449 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7450     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7451       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7452       .type = ARM_CP_CONST, .access = PL0_RW, },
7453 };
7454 
7455 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7456     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7457       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7458       .type = ARM_CP_NOP, .access = PL0_W,
7459       .accessfn = aa64_cacheop_poc_access },
7460     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7461       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7462       .type = ARM_CP_NOP, .access = PL0_W,
7463       .accessfn = aa64_cacheop_poc_access },
7464     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7465       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7466       .type = ARM_CP_NOP, .access = PL0_W,
7467       .accessfn = aa64_cacheop_poc_access },
7468     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7469       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7470       .type = ARM_CP_NOP, .access = PL0_W,
7471       .accessfn = aa64_cacheop_poc_access },
7472     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7473       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7474       .type = ARM_CP_NOP, .access = PL0_W,
7475       .accessfn = aa64_cacheop_poc_access },
7476     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7477       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7478       .type = ARM_CP_NOP, .access = PL0_W,
7479       .accessfn = aa64_cacheop_poc_access },
7480     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7481       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7482       .type = ARM_CP_NOP, .access = PL0_W,
7483       .accessfn = aa64_cacheop_poc_access },
7484     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7485       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7486       .type = ARM_CP_NOP, .access = PL0_W,
7487       .accessfn = aa64_cacheop_poc_access },
7488     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7489       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7490       .access = PL0_W, .type = ARM_CP_DC_GVA,
7491 #ifndef CONFIG_USER_ONLY
7492       /* Avoid overhead of an access check that always passes in user-mode */
7493       .accessfn = aa64_zva_access,
7494 #endif
7495     },
7496     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7497       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7498       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7499 #ifndef CONFIG_USER_ONLY
7500       /* Avoid overhead of an access check that always passes in user-mode */
7501       .accessfn = aa64_zva_access,
7502 #endif
7503     },
7504 };
7505 
7506 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7507                                      bool isread)
7508 {
7509     uint64_t hcr = arm_hcr_el2_eff(env);
7510     int el = arm_current_el(env);
7511 
7512     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7513         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7514             if (hcr & HCR_TGE) {
7515                 return CP_ACCESS_TRAP_EL2;
7516             }
7517             return CP_ACCESS_TRAP;
7518         }
7519     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7520         return CP_ACCESS_TRAP_EL2;
7521     }
7522     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7523         return CP_ACCESS_TRAP_EL2;
7524     }
7525     if (el < 3
7526         && arm_feature(env, ARM_FEATURE_EL3)
7527         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7528         return CP_ACCESS_TRAP_EL3;
7529     }
7530     return CP_ACCESS_OK;
7531 }
7532 
7533 static const ARMCPRegInfo scxtnum_reginfo[] = {
7534     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7535       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7536       .access = PL0_RW, .accessfn = access_scxtnum,
7537       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7538     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7539       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7540       .access = PL1_RW, .accessfn = access_scxtnum,
7541       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7542     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7543       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7544       .access = PL2_RW, .accessfn = access_scxtnum,
7545       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7546     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7547       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7548       .access = PL3_RW,
7549       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7550 };
7551 #endif /* TARGET_AARCH64 */
7552 
7553 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7554                                      bool isread)
7555 {
7556     int el = arm_current_el(env);
7557 
7558     if (el == 0) {
7559         uint64_t sctlr = arm_sctlr(env, el);
7560         if (!(sctlr & SCTLR_EnRCTX)) {
7561             return CP_ACCESS_TRAP;
7562         }
7563     } else if (el == 1) {
7564         uint64_t hcr = arm_hcr_el2_eff(env);
7565         if (hcr & HCR_NV) {
7566             return CP_ACCESS_TRAP_EL2;
7567         }
7568     }
7569     return CP_ACCESS_OK;
7570 }
7571 
7572 static const ARMCPRegInfo predinv_reginfo[] = {
7573     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7574       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7575       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7576     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7577       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7578       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7579     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7580       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7581       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7582     /*
7583      * Note the AArch32 opcodes have a different OPC1.
7584      */
7585     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7586       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7587       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7588     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7589       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7590       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7591     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7592       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7593       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7594 };
7595 
7596 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7597 {
7598     /* Read the high 32 bits of the current CCSIDR */
7599     return extract64(ccsidr_read(env, ri), 32, 32);
7600 }
7601 
7602 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7603     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7604       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7605       .access = PL1_R,
7606       .accessfn = access_tid4,
7607       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7608 };
7609 
7610 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7611                                        bool isread)
7612 {
7613     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7614         return CP_ACCESS_TRAP_EL2;
7615     }
7616 
7617     return CP_ACCESS_OK;
7618 }
7619 
7620 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7621                                        bool isread)
7622 {
7623     if (arm_feature(env, ARM_FEATURE_V8)) {
7624         return access_aa64_tid3(env, ri, isread);
7625     }
7626 
7627     return CP_ACCESS_OK;
7628 }
7629 
7630 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7631                                      bool isread)
7632 {
7633     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7634         return CP_ACCESS_TRAP_EL2;
7635     }
7636 
7637     return CP_ACCESS_OK;
7638 }
7639 
7640 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7641                                         const ARMCPRegInfo *ri, bool isread)
7642 {
7643     /*
7644      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7645      * in v7A, not in v8A.
7646      */
7647     if (!arm_feature(env, ARM_FEATURE_V8) &&
7648         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7649         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7650         return CP_ACCESS_TRAP_EL2;
7651     }
7652     return CP_ACCESS_OK;
7653 }
7654 
7655 static const ARMCPRegInfo jazelle_regs[] = {
7656     { .name = "JIDR",
7657       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7658       .access = PL1_R, .accessfn = access_jazelle,
7659       .type = ARM_CP_CONST, .resetvalue = 0 },
7660     { .name = "JOSCR",
7661       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7662       .accessfn = access_joscr_jmcr,
7663       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7664     { .name = "JMCR",
7665       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7666       .accessfn = access_joscr_jmcr,
7667       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7668 };
7669 
7670 static const ARMCPRegInfo contextidr_el2 = {
7671     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7672     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7673     .access = PL2_RW,
7674     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7675 };
7676 
7677 static const ARMCPRegInfo vhe_reginfo[] = {
7678     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7679       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7680       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7681       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7682 #ifndef CONFIG_USER_ONLY
7683     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7684       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7685       .fieldoffset =
7686         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7687       .type = ARM_CP_IO, .access = PL2_RW,
7688       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7689     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7690       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7691       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7692       .resetfn = gt_hv_timer_reset,
7693       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7694     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7695       .type = ARM_CP_IO,
7696       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7697       .access = PL2_RW,
7698       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7699       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7700     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7701       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7702       .type = ARM_CP_IO | ARM_CP_ALIAS,
7703       .access = PL2_RW, .accessfn = e2h_access,
7704       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7705       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7706     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7707       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7708       .type = ARM_CP_IO | ARM_CP_ALIAS,
7709       .access = PL2_RW, .accessfn = e2h_access,
7710       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7711       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7712     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7713       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7714       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7715       .access = PL2_RW, .accessfn = e2h_access,
7716       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7717     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7718       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7719       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7720       .access = PL2_RW, .accessfn = e2h_access,
7721       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7722     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7723       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7724       .type = ARM_CP_IO | ARM_CP_ALIAS,
7725       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7726       .access = PL2_RW, .accessfn = e2h_access,
7727       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7728     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7729       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7730       .type = ARM_CP_IO | ARM_CP_ALIAS,
7731       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7732       .access = PL2_RW, .accessfn = e2h_access,
7733       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7734 #endif
7735 };
7736 
7737 #ifndef CONFIG_USER_ONLY
7738 static const ARMCPRegInfo ats1e1_reginfo[] = {
7739     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7740       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7741       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7742       .writefn = ats_write64 },
7743     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7744       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7745       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7746       .writefn = ats_write64 },
7747 };
7748 
7749 static const ARMCPRegInfo ats1cp_reginfo[] = {
7750     { .name = "ATS1CPRP",
7751       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7752       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7753       .writefn = ats_write },
7754     { .name = "ATS1CPWP",
7755       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7756       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7757       .writefn = ats_write },
7758 };
7759 #endif
7760 
7761 /*
7762  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7763  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7764  * is non-zero, which is never for ARMv7, optionally in ARMv8
7765  * and mandatorily for ARMv8.2 and up.
7766  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7767  * implementation is RAZ/WI we can ignore this detail, as we
7768  * do for ACTLR.
7769  */
7770 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7771     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7772       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7773       .access = PL1_RW, .accessfn = access_tacr,
7774       .type = ARM_CP_CONST, .resetvalue = 0 },
7775     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7776       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7777       .access = PL2_RW, .type = ARM_CP_CONST,
7778       .resetvalue = 0 },
7779 };
7780 
7781 void register_cp_regs_for_features(ARMCPU *cpu)
7782 {
7783     /* Register all the coprocessor registers based on feature bits */
7784     CPUARMState *env = &cpu->env;
7785     if (arm_feature(env, ARM_FEATURE_M)) {
7786         /* M profile has no coprocessor registers */
7787         return;
7788     }
7789 
7790     define_arm_cp_regs(cpu, cp_reginfo);
7791     if (!arm_feature(env, ARM_FEATURE_V8)) {
7792         /*
7793          * Must go early as it is full of wildcards that may be
7794          * overridden by later definitions.
7795          */
7796         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7797     }
7798 
7799     if (arm_feature(env, ARM_FEATURE_V6)) {
7800         /* The ID registers all have impdef reset values */
7801         ARMCPRegInfo v6_idregs[] = {
7802             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7803               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7804               .access = PL1_R, .type = ARM_CP_CONST,
7805               .accessfn = access_aa32_tid3,
7806               .resetvalue = cpu->isar.id_pfr0 },
7807             /*
7808              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7809              * the value of the GIC field until after we define these regs.
7810              */
7811             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7812               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7813               .access = PL1_R, .type = ARM_CP_NO_RAW,
7814               .accessfn = access_aa32_tid3,
7815               .readfn = id_pfr1_read,
7816               .writefn = arm_cp_write_ignore },
7817             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7818               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7819               .access = PL1_R, .type = ARM_CP_CONST,
7820               .accessfn = access_aa32_tid3,
7821               .resetvalue = cpu->isar.id_dfr0 },
7822             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7823               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7824               .access = PL1_R, .type = ARM_CP_CONST,
7825               .accessfn = access_aa32_tid3,
7826               .resetvalue = cpu->id_afr0 },
7827             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7828               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7829               .access = PL1_R, .type = ARM_CP_CONST,
7830               .accessfn = access_aa32_tid3,
7831               .resetvalue = cpu->isar.id_mmfr0 },
7832             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7833               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7834               .access = PL1_R, .type = ARM_CP_CONST,
7835               .accessfn = access_aa32_tid3,
7836               .resetvalue = cpu->isar.id_mmfr1 },
7837             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7838               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7839               .access = PL1_R, .type = ARM_CP_CONST,
7840               .accessfn = access_aa32_tid3,
7841               .resetvalue = cpu->isar.id_mmfr2 },
7842             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7843               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7844               .access = PL1_R, .type = ARM_CP_CONST,
7845               .accessfn = access_aa32_tid3,
7846               .resetvalue = cpu->isar.id_mmfr3 },
7847             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7848               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7849               .access = PL1_R, .type = ARM_CP_CONST,
7850               .accessfn = access_aa32_tid3,
7851               .resetvalue = cpu->isar.id_isar0 },
7852             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7853               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7854               .access = PL1_R, .type = ARM_CP_CONST,
7855               .accessfn = access_aa32_tid3,
7856               .resetvalue = cpu->isar.id_isar1 },
7857             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7858               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7859               .access = PL1_R, .type = ARM_CP_CONST,
7860               .accessfn = access_aa32_tid3,
7861               .resetvalue = cpu->isar.id_isar2 },
7862             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7863               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7864               .access = PL1_R, .type = ARM_CP_CONST,
7865               .accessfn = access_aa32_tid3,
7866               .resetvalue = cpu->isar.id_isar3 },
7867             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7868               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7869               .access = PL1_R, .type = ARM_CP_CONST,
7870               .accessfn = access_aa32_tid3,
7871               .resetvalue = cpu->isar.id_isar4 },
7872             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7873               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7874               .access = PL1_R, .type = ARM_CP_CONST,
7875               .accessfn = access_aa32_tid3,
7876               .resetvalue = cpu->isar.id_isar5 },
7877             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7878               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7879               .access = PL1_R, .type = ARM_CP_CONST,
7880               .accessfn = access_aa32_tid3,
7881               .resetvalue = cpu->isar.id_mmfr4 },
7882             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7883               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7884               .access = PL1_R, .type = ARM_CP_CONST,
7885               .accessfn = access_aa32_tid3,
7886               .resetvalue = cpu->isar.id_isar6 },
7887         };
7888         define_arm_cp_regs(cpu, v6_idregs);
7889         define_arm_cp_regs(cpu, v6_cp_reginfo);
7890     } else {
7891         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7892     }
7893     if (arm_feature(env, ARM_FEATURE_V6K)) {
7894         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7895     }
7896     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7897         !arm_feature(env, ARM_FEATURE_PMSA)) {
7898         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7899     }
7900     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7901         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7902     }
7903     if (arm_feature(env, ARM_FEATURE_V7)) {
7904         ARMCPRegInfo clidr = {
7905             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7906             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7907             .access = PL1_R, .type = ARM_CP_CONST,
7908             .accessfn = access_tid4,
7909             .resetvalue = cpu->clidr
7910         };
7911         define_one_arm_cp_reg(cpu, &clidr);
7912         define_arm_cp_regs(cpu, v7_cp_reginfo);
7913         define_debug_regs(cpu);
7914         define_pmu_regs(cpu);
7915     } else {
7916         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7917     }
7918     if (arm_feature(env, ARM_FEATURE_V8)) {
7919         /*
7920          * v8 ID registers, which all have impdef reset values.
7921          * Note that within the ID register ranges the unused slots
7922          * must all RAZ, not UNDEF; future architecture versions may
7923          * define new registers here.
7924          * ID registers which are AArch64 views of the AArch32 ID registers
7925          * which already existed in v6 and v7 are handled elsewhere,
7926          * in v6_idregs[].
7927          */
7928         int i;
7929         ARMCPRegInfo v8_idregs[] = {
7930             /*
7931              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7932              * emulation because we don't know the right value for the
7933              * GIC field until after we define these regs.
7934              */
7935             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7936               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7937               .access = PL1_R,
7938 #ifdef CONFIG_USER_ONLY
7939               .type = ARM_CP_CONST,
7940               .resetvalue = cpu->isar.id_aa64pfr0
7941 #else
7942               .type = ARM_CP_NO_RAW,
7943               .accessfn = access_aa64_tid3,
7944               .readfn = id_aa64pfr0_read,
7945               .writefn = arm_cp_write_ignore
7946 #endif
7947             },
7948             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7949               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7950               .access = PL1_R, .type = ARM_CP_CONST,
7951               .accessfn = access_aa64_tid3,
7952               .resetvalue = cpu->isar.id_aa64pfr1},
7953             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7954               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7955               .access = PL1_R, .type = ARM_CP_CONST,
7956               .accessfn = access_aa64_tid3,
7957               .resetvalue = 0 },
7958             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7959               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7960               .access = PL1_R, .type = ARM_CP_CONST,
7961               .accessfn = access_aa64_tid3,
7962               .resetvalue = 0 },
7963             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7964               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7965               .access = PL1_R, .type = ARM_CP_CONST,
7966               .accessfn = access_aa64_tid3,
7967               .resetvalue = cpu->isar.id_aa64zfr0 },
7968             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7969               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7970               .access = PL1_R, .type = ARM_CP_CONST,
7971               .accessfn = access_aa64_tid3,
7972               .resetvalue = cpu->isar.id_aa64smfr0 },
7973             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7974               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7975               .access = PL1_R, .type = ARM_CP_CONST,
7976               .accessfn = access_aa64_tid3,
7977               .resetvalue = 0 },
7978             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7979               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7980               .access = PL1_R, .type = ARM_CP_CONST,
7981               .accessfn = access_aa64_tid3,
7982               .resetvalue = 0 },
7983             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7984               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7985               .access = PL1_R, .type = ARM_CP_CONST,
7986               .accessfn = access_aa64_tid3,
7987               .resetvalue = cpu->isar.id_aa64dfr0 },
7988             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7989               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7990               .access = PL1_R, .type = ARM_CP_CONST,
7991               .accessfn = access_aa64_tid3,
7992               .resetvalue = cpu->isar.id_aa64dfr1 },
7993             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7994               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7995               .access = PL1_R, .type = ARM_CP_CONST,
7996               .accessfn = access_aa64_tid3,
7997               .resetvalue = 0 },
7998             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7999               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8000               .access = PL1_R, .type = ARM_CP_CONST,
8001               .accessfn = access_aa64_tid3,
8002               .resetvalue = 0 },
8003             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8004               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8005               .access = PL1_R, .type = ARM_CP_CONST,
8006               .accessfn = access_aa64_tid3,
8007               .resetvalue = cpu->id_aa64afr0 },
8008             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8009               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8010               .access = PL1_R, .type = ARM_CP_CONST,
8011               .accessfn = access_aa64_tid3,
8012               .resetvalue = cpu->id_aa64afr1 },
8013             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8014               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8015               .access = PL1_R, .type = ARM_CP_CONST,
8016               .accessfn = access_aa64_tid3,
8017               .resetvalue = 0 },
8018             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8019               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8020               .access = PL1_R, .type = ARM_CP_CONST,
8021               .accessfn = access_aa64_tid3,
8022               .resetvalue = 0 },
8023             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8024               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8025               .access = PL1_R, .type = ARM_CP_CONST,
8026               .accessfn = access_aa64_tid3,
8027               .resetvalue = cpu->isar.id_aa64isar0 },
8028             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8029               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8030               .access = PL1_R, .type = ARM_CP_CONST,
8031               .accessfn = access_aa64_tid3,
8032               .resetvalue = cpu->isar.id_aa64isar1 },
8033             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8034               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8035               .access = PL1_R, .type = ARM_CP_CONST,
8036               .accessfn = access_aa64_tid3,
8037               .resetvalue = 0 },
8038             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8039               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8040               .access = PL1_R, .type = ARM_CP_CONST,
8041               .accessfn = access_aa64_tid3,
8042               .resetvalue = 0 },
8043             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8044               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8045               .access = PL1_R, .type = ARM_CP_CONST,
8046               .accessfn = access_aa64_tid3,
8047               .resetvalue = 0 },
8048             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8049               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8050               .access = PL1_R, .type = ARM_CP_CONST,
8051               .accessfn = access_aa64_tid3,
8052               .resetvalue = 0 },
8053             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8054               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8055               .access = PL1_R, .type = ARM_CP_CONST,
8056               .accessfn = access_aa64_tid3,
8057               .resetvalue = 0 },
8058             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8059               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8060               .access = PL1_R, .type = ARM_CP_CONST,
8061               .accessfn = access_aa64_tid3,
8062               .resetvalue = 0 },
8063             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8064               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8065               .access = PL1_R, .type = ARM_CP_CONST,
8066               .accessfn = access_aa64_tid3,
8067               .resetvalue = cpu->isar.id_aa64mmfr0 },
8068             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8069               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8070               .access = PL1_R, .type = ARM_CP_CONST,
8071               .accessfn = access_aa64_tid3,
8072               .resetvalue = cpu->isar.id_aa64mmfr1 },
8073             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8074               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8075               .access = PL1_R, .type = ARM_CP_CONST,
8076               .accessfn = access_aa64_tid3,
8077               .resetvalue = cpu->isar.id_aa64mmfr2 },
8078             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8079               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8080               .access = PL1_R, .type = ARM_CP_CONST,
8081               .accessfn = access_aa64_tid3,
8082               .resetvalue = 0 },
8083             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8084               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8085               .access = PL1_R, .type = ARM_CP_CONST,
8086               .accessfn = access_aa64_tid3,
8087               .resetvalue = 0 },
8088             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8089               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8090               .access = PL1_R, .type = ARM_CP_CONST,
8091               .accessfn = access_aa64_tid3,
8092               .resetvalue = 0 },
8093             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8094               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8095               .access = PL1_R, .type = ARM_CP_CONST,
8096               .accessfn = access_aa64_tid3,
8097               .resetvalue = 0 },
8098             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8099               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8100               .access = PL1_R, .type = ARM_CP_CONST,
8101               .accessfn = access_aa64_tid3,
8102               .resetvalue = 0 },
8103             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8104               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8105               .access = PL1_R, .type = ARM_CP_CONST,
8106               .accessfn = access_aa64_tid3,
8107               .resetvalue = cpu->isar.mvfr0 },
8108             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8109               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8110               .access = PL1_R, .type = ARM_CP_CONST,
8111               .accessfn = access_aa64_tid3,
8112               .resetvalue = cpu->isar.mvfr1 },
8113             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8114               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8115               .access = PL1_R, .type = ARM_CP_CONST,
8116               .accessfn = access_aa64_tid3,
8117               .resetvalue = cpu->isar.mvfr2 },
8118             /*
8119              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8120              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8121              * as RAZ, since it is in the "reserved for future ID
8122              * registers, RAZ" part of the AArch32 encoding space.
8123              */
8124             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8125               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8126               .access = PL1_R, .type = ARM_CP_CONST,
8127               .accessfn = access_aa64_tid3,
8128               .resetvalue = 0 },
8129             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8130               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8131               .access = PL1_R, .type = ARM_CP_CONST,
8132               .accessfn = access_aa64_tid3,
8133               .resetvalue = 0 },
8134             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8135               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8136               .access = PL1_R, .type = ARM_CP_CONST,
8137               .accessfn = access_aa64_tid3,
8138               .resetvalue = 0 },
8139             /*
8140              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8141              * they're also RAZ for AArch64, and in v8 are gradually
8142              * being filled with AArch64-view-of-AArch32-ID-register
8143              * for new ID registers.
8144              */
8145             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8146               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8147               .access = PL1_R, .type = ARM_CP_CONST,
8148               .accessfn = access_aa64_tid3,
8149               .resetvalue = 0 },
8150             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8151               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8152               .access = PL1_R, .type = ARM_CP_CONST,
8153               .accessfn = access_aa64_tid3,
8154               .resetvalue = cpu->isar.id_pfr2 },
8155             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8156               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8157               .access = PL1_R, .type = ARM_CP_CONST,
8158               .accessfn = access_aa64_tid3,
8159               .resetvalue = cpu->isar.id_dfr1 },
8160             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8161               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8162               .access = PL1_R, .type = ARM_CP_CONST,
8163               .accessfn = access_aa64_tid3,
8164               .resetvalue = cpu->isar.id_mmfr5 },
8165             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8166               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8167               .access = PL1_R, .type = ARM_CP_CONST,
8168               .accessfn = access_aa64_tid3,
8169               .resetvalue = 0 },
8170             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8171               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8172               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8173               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8174             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8175               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8176               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8177               .resetvalue = cpu->pmceid0 },
8178             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8179               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8180               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8181               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8182             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8183               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8184               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8185               .resetvalue = cpu->pmceid1 },
8186         };
8187 #ifdef CONFIG_USER_ONLY
8188         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8189             { .name = "ID_AA64PFR0_EL1",
8190               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8191                                R_ID_AA64PFR0_ADVSIMD_MASK |
8192                                R_ID_AA64PFR0_SVE_MASK |
8193                                R_ID_AA64PFR0_DIT_MASK,
8194               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8195                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8196             { .name = "ID_AA64PFR1_EL1",
8197               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8198                                R_ID_AA64PFR1_SSBS_MASK |
8199                                R_ID_AA64PFR1_MTE_MASK |
8200                                R_ID_AA64PFR1_SME_MASK },
8201             { .name = "ID_AA64PFR*_EL1_RESERVED",
8202               .is_glob = true },
8203             { .name = "ID_AA64ZFR0_EL1",
8204               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8205                                R_ID_AA64ZFR0_AES_MASK |
8206                                R_ID_AA64ZFR0_BITPERM_MASK |
8207                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8208                                R_ID_AA64ZFR0_SHA3_MASK |
8209                                R_ID_AA64ZFR0_SM4_MASK |
8210                                R_ID_AA64ZFR0_I8MM_MASK |
8211                                R_ID_AA64ZFR0_F32MM_MASK |
8212                                R_ID_AA64ZFR0_F64MM_MASK },
8213             { .name = "ID_AA64SMFR0_EL1",
8214               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8215                                R_ID_AA64SMFR0_B16F32_MASK |
8216                                R_ID_AA64SMFR0_F16F32_MASK |
8217                                R_ID_AA64SMFR0_I8I32_MASK |
8218                                R_ID_AA64SMFR0_F64F64_MASK |
8219                                R_ID_AA64SMFR0_I16I64_MASK |
8220                                R_ID_AA64SMFR0_FA64_MASK },
8221             { .name = "ID_AA64MMFR0_EL1",
8222               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8223               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8224                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8225             { .name = "ID_AA64MMFR1_EL1",
8226               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8227             { .name = "ID_AA64MMFR2_EL1",
8228               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8229             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8230               .is_glob = true },
8231             { .name = "ID_AA64DFR0_EL1",
8232               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8233             { .name = "ID_AA64DFR1_EL1" },
8234             { .name = "ID_AA64DFR*_EL1_RESERVED",
8235               .is_glob = true },
8236             { .name = "ID_AA64AFR*",
8237               .is_glob = true },
8238             { .name = "ID_AA64ISAR0_EL1",
8239               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8240                                R_ID_AA64ISAR0_SHA1_MASK |
8241                                R_ID_AA64ISAR0_SHA2_MASK |
8242                                R_ID_AA64ISAR0_CRC32_MASK |
8243                                R_ID_AA64ISAR0_ATOMIC_MASK |
8244                                R_ID_AA64ISAR0_RDM_MASK |
8245                                R_ID_AA64ISAR0_SHA3_MASK |
8246                                R_ID_AA64ISAR0_SM3_MASK |
8247                                R_ID_AA64ISAR0_SM4_MASK |
8248                                R_ID_AA64ISAR0_DP_MASK |
8249                                R_ID_AA64ISAR0_FHM_MASK |
8250                                R_ID_AA64ISAR0_TS_MASK |
8251                                R_ID_AA64ISAR0_RNDR_MASK },
8252             { .name = "ID_AA64ISAR1_EL1",
8253               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8254                                R_ID_AA64ISAR1_APA_MASK |
8255                                R_ID_AA64ISAR1_API_MASK |
8256                                R_ID_AA64ISAR1_JSCVT_MASK |
8257                                R_ID_AA64ISAR1_FCMA_MASK |
8258                                R_ID_AA64ISAR1_LRCPC_MASK |
8259                                R_ID_AA64ISAR1_GPA_MASK |
8260                                R_ID_AA64ISAR1_GPI_MASK |
8261                                R_ID_AA64ISAR1_FRINTTS_MASK |
8262                                R_ID_AA64ISAR1_SB_MASK |
8263                                R_ID_AA64ISAR1_BF16_MASK |
8264                                R_ID_AA64ISAR1_DGH_MASK |
8265                                R_ID_AA64ISAR1_I8MM_MASK },
8266             { .name = "ID_AA64ISAR2_EL1",
8267               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8268                                R_ID_AA64ISAR2_RPRES_MASK |
8269                                R_ID_AA64ISAR2_GPA3_MASK |
8270                                R_ID_AA64ISAR2_APA3_MASK },
8271             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8272               .is_glob = true },
8273         };
8274         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8275 #endif
8276         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8277         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8278             !arm_feature(env, ARM_FEATURE_EL2)) {
8279             ARMCPRegInfo rvbar = {
8280                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8281                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8282                 .access = PL1_R,
8283                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8284             };
8285             define_one_arm_cp_reg(cpu, &rvbar);
8286         }
8287         define_arm_cp_regs(cpu, v8_idregs);
8288         define_arm_cp_regs(cpu, v8_cp_reginfo);
8289 
8290         for (i = 4; i < 16; i++) {
8291             /*
8292              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8293              * For pre-v8 cores there are RAZ patterns for these in
8294              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8295              * v8 extends the "must RAZ" part of the ID register space
8296              * to also cover c0, 0, c{8-15}, {0-7}.
8297              * These are STATE_AA32 because in the AArch64 sysreg space
8298              * c4-c7 is where the AArch64 ID registers live (and we've
8299              * already defined those in v8_idregs[]), and c8-c15 are not
8300              * "must RAZ" for AArch64.
8301              */
8302             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8303             ARMCPRegInfo v8_aa32_raz_idregs = {
8304                 .name = name,
8305                 .state = ARM_CP_STATE_AA32,
8306                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8307                 .access = PL1_R, .type = ARM_CP_CONST,
8308                 .accessfn = access_aa64_tid3,
8309                 .resetvalue = 0 };
8310             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8311         }
8312     }
8313 
8314     /*
8315      * Register the base EL2 cpregs.
8316      * Pre v8, these registers are implemented only as part of the
8317      * Virtualization Extensions (EL2 present).  Beginning with v8,
8318      * if EL2 is missing but EL3 is enabled, mostly these become
8319      * RES0 from EL3, with some specific exceptions.
8320      */
8321     if (arm_feature(env, ARM_FEATURE_EL2)
8322         || (arm_feature(env, ARM_FEATURE_EL3)
8323             && arm_feature(env, ARM_FEATURE_V8))) {
8324         uint64_t vmpidr_def = mpidr_read_val(env);
8325         ARMCPRegInfo vpidr_regs[] = {
8326             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8327               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8328               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8329               .resetvalue = cpu->midr,
8330               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8331               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8332             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8333               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8334               .access = PL2_RW, .resetvalue = cpu->midr,
8335               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8336               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8337             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8338               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8339               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8340               .resetvalue = vmpidr_def,
8341               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8342               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8343             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8344               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8345               .access = PL2_RW, .resetvalue = vmpidr_def,
8346               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8347               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8348         };
8349         /*
8350          * The only field of MDCR_EL2 that has a defined architectural reset
8351          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8352          */
8353         ARMCPRegInfo mdcr_el2 = {
8354             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8355             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8356             .writefn = mdcr_el2_write,
8357             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8358             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8359         };
8360         define_one_arm_cp_reg(cpu, &mdcr_el2);
8361         define_arm_cp_regs(cpu, vpidr_regs);
8362         define_arm_cp_regs(cpu, el2_cp_reginfo);
8363         if (arm_feature(env, ARM_FEATURE_V8)) {
8364             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8365         }
8366         if (cpu_isar_feature(aa64_sel2, cpu)) {
8367             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8368         }
8369         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8370         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8371             ARMCPRegInfo rvbar[] = {
8372                 {
8373                     .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8374                     .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8375                     .access = PL2_R,
8376                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8377                 },
8378                 {   .name = "RVBAR", .type = ARM_CP_ALIAS,
8379                     .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8380                     .access = PL2_R,
8381                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8382                 },
8383             };
8384             define_arm_cp_regs(cpu, rvbar);
8385         }
8386     }
8387 
8388     /* Register the base EL3 cpregs. */
8389     if (arm_feature(env, ARM_FEATURE_EL3)) {
8390         define_arm_cp_regs(cpu, el3_cp_reginfo);
8391         ARMCPRegInfo el3_regs[] = {
8392             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8393               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8394               .access = PL3_R,
8395               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8396             },
8397             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8398               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8399               .access = PL3_RW,
8400               .raw_writefn = raw_write, .writefn = sctlr_write,
8401               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8402               .resetvalue = cpu->reset_sctlr },
8403         };
8404 
8405         define_arm_cp_regs(cpu, el3_regs);
8406     }
8407     /*
8408      * The behaviour of NSACR is sufficiently various that we don't
8409      * try to describe it in a single reginfo:
8410      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8411      *     reads as constant 0xc00 from NS EL1 and NS EL2
8412      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8413      *  if v7 without EL3, register doesn't exist
8414      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8415      */
8416     if (arm_feature(env, ARM_FEATURE_EL3)) {
8417         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8418             static const ARMCPRegInfo nsacr = {
8419                 .name = "NSACR", .type = ARM_CP_CONST,
8420                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8421                 .access = PL1_RW, .accessfn = nsacr_access,
8422                 .resetvalue = 0xc00
8423             };
8424             define_one_arm_cp_reg(cpu, &nsacr);
8425         } else {
8426             static const ARMCPRegInfo nsacr = {
8427                 .name = "NSACR",
8428                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8429                 .access = PL3_RW | PL1_R,
8430                 .resetvalue = 0,
8431                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8432             };
8433             define_one_arm_cp_reg(cpu, &nsacr);
8434         }
8435     } else {
8436         if (arm_feature(env, ARM_FEATURE_V8)) {
8437             static const ARMCPRegInfo nsacr = {
8438                 .name = "NSACR", .type = ARM_CP_CONST,
8439                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8440                 .access = PL1_R,
8441                 .resetvalue = 0xc00
8442             };
8443             define_one_arm_cp_reg(cpu, &nsacr);
8444         }
8445     }
8446 
8447     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8448         if (arm_feature(env, ARM_FEATURE_V6)) {
8449             /* PMSAv6 not implemented */
8450             assert(arm_feature(env, ARM_FEATURE_V7));
8451             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8452             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8453         } else {
8454             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8455         }
8456     } else {
8457         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8458         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8459         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8460         if (cpu_isar_feature(aa32_hpd, cpu)) {
8461             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8462         }
8463     }
8464     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8465         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8466     }
8467     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8468         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8469     }
8470     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8471         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8472     }
8473     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8474         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8475     }
8476     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8477         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8478     }
8479     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8480         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8481     }
8482     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8483         define_arm_cp_regs(cpu, omap_cp_reginfo);
8484     }
8485     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8486         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8487     }
8488     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8489         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8490     }
8491     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8492         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8493     }
8494     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8495         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8496     }
8497     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8498         define_arm_cp_regs(cpu, jazelle_regs);
8499     }
8500     /*
8501      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8502      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8503      * be read-only (ie write causes UNDEF exception).
8504      */
8505     {
8506         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8507             /*
8508              * Pre-v8 MIDR space.
8509              * Note that the MIDR isn't a simple constant register because
8510              * of the TI925 behaviour where writes to another register can
8511              * cause the MIDR value to change.
8512              *
8513              * Unimplemented registers in the c15 0 0 0 space default to
8514              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8515              * and friends override accordingly.
8516              */
8517             { .name = "MIDR",
8518               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8519               .access = PL1_R, .resetvalue = cpu->midr,
8520               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8521               .readfn = midr_read,
8522               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8523               .type = ARM_CP_OVERRIDE },
8524             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8525             { .name = "DUMMY",
8526               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8527               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8528             { .name = "DUMMY",
8529               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8530               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8531             { .name = "DUMMY",
8532               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8533               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8534             { .name = "DUMMY",
8535               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8536               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8537             { .name = "DUMMY",
8538               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8539               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8540         };
8541         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8542             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8543               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8544               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8545               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8546               .readfn = midr_read },
8547             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8548             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8549               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8550               .access = PL1_R, .resetvalue = cpu->midr },
8551             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8552               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8553               .access = PL1_R,
8554               .accessfn = access_aa64_tid1,
8555               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8556         };
8557         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8558             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8559             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8560             .access = PL1_R, .resetvalue = cpu->midr
8561         };
8562         ARMCPRegInfo id_cp_reginfo[] = {
8563             /* These are common to v8 and pre-v8 */
8564             { .name = "CTR",
8565               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8566               .access = PL1_R, .accessfn = ctr_el0_access,
8567               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8568             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8569               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8570               .access = PL0_R, .accessfn = ctr_el0_access,
8571               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8572             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8573             { .name = "TCMTR",
8574               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8575               .access = PL1_R,
8576               .accessfn = access_aa32_tid1,
8577               .type = ARM_CP_CONST, .resetvalue = 0 },
8578         };
8579         /* TLBTR is specific to VMSA */
8580         ARMCPRegInfo id_tlbtr_reginfo = {
8581               .name = "TLBTR",
8582               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8583               .access = PL1_R,
8584               .accessfn = access_aa32_tid1,
8585               .type = ARM_CP_CONST, .resetvalue = 0,
8586         };
8587         /* MPUIR is specific to PMSA V6+ */
8588         ARMCPRegInfo id_mpuir_reginfo = {
8589               .name = "MPUIR",
8590               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8591               .access = PL1_R, .type = ARM_CP_CONST,
8592               .resetvalue = cpu->pmsav7_dregion << 8
8593         };
8594         /* HMPUIR is specific to PMSA V8 */
8595         ARMCPRegInfo id_hmpuir_reginfo = {
8596             .name = "HMPUIR",
8597             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8598             .access = PL2_R, .type = ARM_CP_CONST,
8599             .resetvalue = cpu->pmsav8r_hdregion
8600         };
8601         static const ARMCPRegInfo crn0_wi_reginfo = {
8602             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8603             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8604             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8605         };
8606 #ifdef CONFIG_USER_ONLY
8607         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8608             { .name = "MIDR_EL1",
8609               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8610                                R_MIDR_EL1_PARTNUM_MASK |
8611                                R_MIDR_EL1_ARCHITECTURE_MASK |
8612                                R_MIDR_EL1_VARIANT_MASK |
8613                                R_MIDR_EL1_IMPLEMENTER_MASK },
8614             { .name = "REVIDR_EL1" },
8615         };
8616         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8617 #endif
8618         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8619             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8620             size_t i;
8621             /*
8622              * Register the blanket "writes ignored" value first to cover the
8623              * whole space. Then update the specific ID registers to allow write
8624              * access, so that they ignore writes rather than causing them to
8625              * UNDEF.
8626              */
8627             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8628             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8629                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8630             }
8631             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8632                 id_cp_reginfo[i].access = PL1_RW;
8633             }
8634             id_mpuir_reginfo.access = PL1_RW;
8635             id_tlbtr_reginfo.access = PL1_RW;
8636         }
8637         if (arm_feature(env, ARM_FEATURE_V8)) {
8638             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8639             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8640                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8641             }
8642         } else {
8643             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8644         }
8645         define_arm_cp_regs(cpu, id_cp_reginfo);
8646         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8647             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8648         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8649                    arm_feature(env, ARM_FEATURE_V8)) {
8650             uint32_t i = 0;
8651             char *tmp_string;
8652 
8653             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8654             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8655             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8656 
8657             /* Register alias is only valid for first 32 indexes */
8658             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8659                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8660                 uint8_t opc1 = extract32(i, 4, 1);
8661                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8662 
8663                 tmp_string = g_strdup_printf("PRBAR%u", i);
8664                 ARMCPRegInfo tmp_prbarn_reginfo = {
8665                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8666                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8667                     .access = PL1_RW, .resetvalue = 0,
8668                     .accessfn = access_tvm_trvm,
8669                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8670                 };
8671                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8672                 g_free(tmp_string);
8673 
8674                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8675                 tmp_string = g_strdup_printf("PRLAR%u", i);
8676                 ARMCPRegInfo tmp_prlarn_reginfo = {
8677                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8678                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8679                     .access = PL1_RW, .resetvalue = 0,
8680                     .accessfn = access_tvm_trvm,
8681                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8682                 };
8683                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8684                 g_free(tmp_string);
8685             }
8686 
8687             /* Register alias is only valid for first 32 indexes */
8688             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8689                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8690                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8691                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8692 
8693                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8694                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8695                     .name = tmp_string,
8696                     .type = ARM_CP_NO_RAW,
8697                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8698                     .access = PL2_RW, .resetvalue = 0,
8699                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8700                 };
8701                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8702                 g_free(tmp_string);
8703 
8704                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8705                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8706                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8707                     .name = tmp_string,
8708                     .type = ARM_CP_NO_RAW,
8709                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8710                     .access = PL2_RW, .resetvalue = 0,
8711                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8712                 };
8713                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8714                 g_free(tmp_string);
8715             }
8716         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8717             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8718         }
8719     }
8720 
8721     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8722         ARMCPRegInfo mpidr_cp_reginfo[] = {
8723             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8724               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8725               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8726         };
8727 #ifdef CONFIG_USER_ONLY
8728         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8729             { .name = "MPIDR_EL1",
8730               .fixed_bits = 0x0000000080000000 },
8731         };
8732         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8733 #endif
8734         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8735     }
8736 
8737     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8738         ARMCPRegInfo auxcr_reginfo[] = {
8739             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8740               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8741               .access = PL1_RW, .accessfn = access_tacr,
8742               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8743             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8744               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8745               .access = PL2_RW, .type = ARM_CP_CONST,
8746               .resetvalue = 0 },
8747             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8748               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8749               .access = PL3_RW, .type = ARM_CP_CONST,
8750               .resetvalue = 0 },
8751         };
8752         define_arm_cp_regs(cpu, auxcr_reginfo);
8753         if (cpu_isar_feature(aa32_ac2, cpu)) {
8754             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8755         }
8756     }
8757 
8758     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8759         /*
8760          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8761          * There are two flavours:
8762          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8763          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8764          *      32-bit register visible to AArch32 at a different encoding
8765          *      to the "flavour 1" register and with the bits rearranged to
8766          *      be able to squash a 64-bit address into the 32-bit view.
8767          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8768          * in future if we support AArch32-only configs of some of the
8769          * AArch64 cores we might need to add a specific feature flag
8770          * to indicate cores with "flavour 2" CBAR.
8771          */
8772         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8773             /* 32 bit view is [31:18] 0...0 [43:32]. */
8774             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8775                 | extract64(cpu->reset_cbar, 32, 12);
8776             ARMCPRegInfo cbar_reginfo[] = {
8777                 { .name = "CBAR",
8778                   .type = ARM_CP_CONST,
8779                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8780                   .access = PL1_R, .resetvalue = cbar32 },
8781                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8782                   .type = ARM_CP_CONST,
8783                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8784                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8785             };
8786             /* We don't implement a r/w 64 bit CBAR currently */
8787             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8788             define_arm_cp_regs(cpu, cbar_reginfo);
8789         } else {
8790             ARMCPRegInfo cbar = {
8791                 .name = "CBAR",
8792                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8793                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8794                 .fieldoffset = offsetof(CPUARMState,
8795                                         cp15.c15_config_base_address)
8796             };
8797             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8798                 cbar.access = PL1_R;
8799                 cbar.fieldoffset = 0;
8800                 cbar.type = ARM_CP_CONST;
8801             }
8802             define_one_arm_cp_reg(cpu, &cbar);
8803         }
8804     }
8805 
8806     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8807         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8808             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8809               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8810               .access = PL1_RW, .writefn = vbar_write,
8811               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8812                                      offsetof(CPUARMState, cp15.vbar_ns) },
8813               .resetvalue = 0 },
8814         };
8815         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8816     }
8817 
8818     /* Generic registers whose values depend on the implementation */
8819     {
8820         ARMCPRegInfo sctlr = {
8821             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8822             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8823             .access = PL1_RW, .accessfn = access_tvm_trvm,
8824             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8825                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8826             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8827             .raw_writefn = raw_write,
8828         };
8829         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8830             /*
8831              * Normally we would always end the TB on an SCTLR write, but Linux
8832              * arch/arm/mach-pxa/sleep.S expects two instructions following
8833              * an MMU enable to execute from cache.  Imitate this behaviour.
8834              */
8835             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8836         }
8837         define_one_arm_cp_reg(cpu, &sctlr);
8838 
8839         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8840             arm_feature(env, ARM_FEATURE_V8)) {
8841             ARMCPRegInfo vsctlr = {
8842                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8843                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8844                 .access = PL2_RW, .resetvalue = 0x0,
8845                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8846             };
8847             define_one_arm_cp_reg(cpu, &vsctlr);
8848         }
8849     }
8850 
8851     if (cpu_isar_feature(aa64_lor, cpu)) {
8852         define_arm_cp_regs(cpu, lor_reginfo);
8853     }
8854     if (cpu_isar_feature(aa64_pan, cpu)) {
8855         define_one_arm_cp_reg(cpu, &pan_reginfo);
8856     }
8857 #ifndef CONFIG_USER_ONLY
8858     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8859         define_arm_cp_regs(cpu, ats1e1_reginfo);
8860     }
8861     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8862         define_arm_cp_regs(cpu, ats1cp_reginfo);
8863     }
8864 #endif
8865     if (cpu_isar_feature(aa64_uao, cpu)) {
8866         define_one_arm_cp_reg(cpu, &uao_reginfo);
8867     }
8868 
8869     if (cpu_isar_feature(aa64_dit, cpu)) {
8870         define_one_arm_cp_reg(cpu, &dit_reginfo);
8871     }
8872     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8873         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8874     }
8875     if (cpu_isar_feature(any_ras, cpu)) {
8876         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8877     }
8878 
8879     if (cpu_isar_feature(aa64_vh, cpu) ||
8880         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8881         define_one_arm_cp_reg(cpu, &contextidr_el2);
8882     }
8883     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8884         define_arm_cp_regs(cpu, vhe_reginfo);
8885     }
8886 
8887     if (cpu_isar_feature(aa64_sve, cpu)) {
8888         define_arm_cp_regs(cpu, zcr_reginfo);
8889     }
8890 
8891     if (cpu_isar_feature(aa64_hcx, cpu)) {
8892         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8893     }
8894 
8895 #ifdef TARGET_AARCH64
8896     if (cpu_isar_feature(aa64_sme, cpu)) {
8897         define_arm_cp_regs(cpu, sme_reginfo);
8898     }
8899     if (cpu_isar_feature(aa64_pauth, cpu)) {
8900         define_arm_cp_regs(cpu, pauth_reginfo);
8901     }
8902     if (cpu_isar_feature(aa64_rndr, cpu)) {
8903         define_arm_cp_regs(cpu, rndr_reginfo);
8904     }
8905     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8906         define_arm_cp_regs(cpu, tlbirange_reginfo);
8907     }
8908     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8909         define_arm_cp_regs(cpu, tlbios_reginfo);
8910     }
8911 #ifndef CONFIG_USER_ONLY
8912     /* Data Cache clean instructions up to PoP */
8913     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8914         define_one_arm_cp_reg(cpu, dcpop_reg);
8915 
8916         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8917             define_one_arm_cp_reg(cpu, dcpodp_reg);
8918         }
8919     }
8920 #endif /*CONFIG_USER_ONLY*/
8921 
8922     /*
8923      * If full MTE is enabled, add all of the system registers.
8924      * If only "instructions available at EL0" are enabled,
8925      * then define only a RAZ/WI version of PSTATE.TCO.
8926      */
8927     if (cpu_isar_feature(aa64_mte, cpu)) {
8928         define_arm_cp_regs(cpu, mte_reginfo);
8929         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8930     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8931         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8932         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8933     }
8934 
8935     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8936         define_arm_cp_regs(cpu, scxtnum_reginfo);
8937     }
8938 #endif
8939 
8940     if (cpu_isar_feature(any_predinv, cpu)) {
8941         define_arm_cp_regs(cpu, predinv_reginfo);
8942     }
8943 
8944     if (cpu_isar_feature(any_ccidx, cpu)) {
8945         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8946     }
8947 
8948 #ifndef CONFIG_USER_ONLY
8949     /*
8950      * Register redirections and aliases must be done last,
8951      * after the registers from the other extensions have been defined.
8952      */
8953     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8954         define_arm_vh_e2h_redirects_aliases(cpu);
8955     }
8956 #endif
8957 }
8958 
8959 /* Sort alphabetically by type name, except for "any". */
8960 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8961 {
8962     ObjectClass *class_a = (ObjectClass *)a;
8963     ObjectClass *class_b = (ObjectClass *)b;
8964     const char *name_a, *name_b;
8965 
8966     name_a = object_class_get_name(class_a);
8967     name_b = object_class_get_name(class_b);
8968     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8969         return 1;
8970     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8971         return -1;
8972     } else {
8973         return strcmp(name_a, name_b);
8974     }
8975 }
8976 
8977 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8978 {
8979     ObjectClass *oc = data;
8980     CPUClass *cc = CPU_CLASS(oc);
8981     const char *typename;
8982     char *name;
8983 
8984     typename = object_class_get_name(oc);
8985     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8986     if (cc->deprecation_note) {
8987         qemu_printf("  %s (deprecated)\n", name);
8988     } else {
8989         qemu_printf("  %s\n", name);
8990     }
8991     g_free(name);
8992 }
8993 
8994 void arm_cpu_list(void)
8995 {
8996     GSList *list;
8997 
8998     list = object_class_get_list(TYPE_ARM_CPU, false);
8999     list = g_slist_sort(list, arm_cpu_list_compare);
9000     qemu_printf("Available CPUs:\n");
9001     g_slist_foreach(list, arm_cpu_list_entry, NULL);
9002     g_slist_free(list);
9003 }
9004 
9005 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
9006 {
9007     ObjectClass *oc = data;
9008     CpuDefinitionInfoList **cpu_list = user_data;
9009     CpuDefinitionInfo *info;
9010     const char *typename;
9011 
9012     typename = object_class_get_name(oc);
9013     info = g_malloc0(sizeof(*info));
9014     info->name = g_strndup(typename,
9015                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
9016     info->q_typename = g_strdup(typename);
9017 
9018     QAPI_LIST_PREPEND(*cpu_list, info);
9019 }
9020 
9021 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
9022 {
9023     CpuDefinitionInfoList *cpu_list = NULL;
9024     GSList *list;
9025 
9026     list = object_class_get_list(TYPE_ARM_CPU, false);
9027     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9028     g_slist_free(list);
9029 
9030     return cpu_list;
9031 }
9032 
9033 /*
9034  * Private utility function for define_one_arm_cp_reg_with_opaque():
9035  * add a single reginfo struct to the hash table.
9036  */
9037 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9038                                    void *opaque, CPState state,
9039                                    CPSecureState secstate,
9040                                    int crm, int opc1, int opc2,
9041                                    const char *name)
9042 {
9043     CPUARMState *env = &cpu->env;
9044     uint32_t key;
9045     ARMCPRegInfo *r2;
9046     bool is64 = r->type & ARM_CP_64BIT;
9047     bool ns = secstate & ARM_CP_SECSTATE_NS;
9048     int cp = r->cp;
9049     size_t name_len;
9050     bool make_const;
9051 
9052     switch (state) {
9053     case ARM_CP_STATE_AA32:
9054         /* We assume it is a cp15 register if the .cp field is left unset. */
9055         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9056             cp = 15;
9057         }
9058         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9059         break;
9060     case ARM_CP_STATE_AA64:
9061         /*
9062          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9063          * cp == 0 as equivalent to the value for "standard guest-visible
9064          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9065          * in their AArch64 view (the .cp value may be non-zero for the
9066          * benefit of the AArch32 view).
9067          */
9068         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9069             cp = CP_REG_ARM64_SYSREG_CP;
9070         }
9071         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9072         break;
9073     default:
9074         g_assert_not_reached();
9075     }
9076 
9077     /* Overriding of an existing definition must be explicitly requested. */
9078     if (!(r->type & ARM_CP_OVERRIDE)) {
9079         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9080         if (oldreg) {
9081             assert(oldreg->type & ARM_CP_OVERRIDE);
9082         }
9083     }
9084 
9085     /*
9086      * Eliminate registers that are not present because the EL is missing.
9087      * Doing this here makes it easier to put all registers for a given
9088      * feature into the same ARMCPRegInfo array and define them all at once.
9089      */
9090     make_const = false;
9091     if (arm_feature(env, ARM_FEATURE_EL3)) {
9092         /*
9093          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9094          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9095          */
9096         int min_el = ctz32(r->access) / 2;
9097         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9098             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9099                 return;
9100             }
9101             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9102         }
9103     } else {
9104         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9105                                  ? PL2_RW : PL1_RW);
9106         if ((r->access & max_el) == 0) {
9107             return;
9108         }
9109     }
9110 
9111     /* Combine cpreg and name into one allocation. */
9112     name_len = strlen(name) + 1;
9113     r2 = g_malloc(sizeof(*r2) + name_len);
9114     *r2 = *r;
9115     r2->name = memcpy(r2 + 1, name, name_len);
9116 
9117     /*
9118      * Update fields to match the instantiation, overwiting wildcards
9119      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9120      */
9121     r2->cp = cp;
9122     r2->crm = crm;
9123     r2->opc1 = opc1;
9124     r2->opc2 = opc2;
9125     r2->state = state;
9126     r2->secure = secstate;
9127     if (opaque) {
9128         r2->opaque = opaque;
9129     }
9130 
9131     if (make_const) {
9132         /* This should not have been a very special register to begin. */
9133         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9134         assert(old_special == 0 || old_special == ARM_CP_NOP);
9135         /*
9136          * Set the special function to CONST, retaining the other flags.
9137          * This is important for e.g. ARM_CP_SVE so that we still
9138          * take the SVE trap if CPTR_EL3.EZ == 0.
9139          */
9140         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9141         /*
9142          * Usually, these registers become RES0, but there are a few
9143          * special cases like VPIDR_EL2 which have a constant non-zero
9144          * value with writes ignored.
9145          */
9146         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9147             r2->resetvalue = 0;
9148         }
9149         /*
9150          * ARM_CP_CONST has precedence, so removing the callbacks and
9151          * offsets are not strictly necessary, but it is potentially
9152          * less confusing to debug later.
9153          */
9154         r2->readfn = NULL;
9155         r2->writefn = NULL;
9156         r2->raw_readfn = NULL;
9157         r2->raw_writefn = NULL;
9158         r2->resetfn = NULL;
9159         r2->fieldoffset = 0;
9160         r2->bank_fieldoffsets[0] = 0;
9161         r2->bank_fieldoffsets[1] = 0;
9162     } else {
9163         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9164 
9165         if (isbanked) {
9166             /*
9167              * Register is banked (using both entries in array).
9168              * Overwriting fieldoffset as the array is only used to define
9169              * banked registers but later only fieldoffset is used.
9170              */
9171             r2->fieldoffset = r->bank_fieldoffsets[ns];
9172         }
9173         if (state == ARM_CP_STATE_AA32) {
9174             if (isbanked) {
9175                 /*
9176                  * If the register is banked then we don't need to migrate or
9177                  * reset the 32-bit instance in certain cases:
9178                  *
9179                  * 1) If the register has both 32-bit and 64-bit instances
9180                  *    then we can count on the 64-bit instance taking care
9181                  *    of the non-secure bank.
9182                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9183                  *    version taking care of the secure bank.  This requires
9184                  *    that separate 32 and 64-bit definitions are provided.
9185                  */
9186                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9187                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9188                     r2->type |= ARM_CP_ALIAS;
9189                 }
9190             } else if ((secstate != r->secure) && !ns) {
9191                 /*
9192                  * The register is not banked so we only want to allow
9193                  * migration of the non-secure instance.
9194                  */
9195                 r2->type |= ARM_CP_ALIAS;
9196             }
9197 
9198             if (HOST_BIG_ENDIAN &&
9199                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9200                 r2->fieldoffset += sizeof(uint32_t);
9201             }
9202         }
9203     }
9204 
9205     /*
9206      * By convention, for wildcarded registers only the first
9207      * entry is used for migration; the others are marked as
9208      * ALIAS so we don't try to transfer the register
9209      * multiple times. Special registers (ie NOP/WFI) are
9210      * never migratable and not even raw-accessible.
9211      */
9212     if (r2->type & ARM_CP_SPECIAL_MASK) {
9213         r2->type |= ARM_CP_NO_RAW;
9214     }
9215     if (((r->crm == CP_ANY) && crm != 0) ||
9216         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9217         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9218         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9219     }
9220 
9221     /*
9222      * Check that raw accesses are either forbidden or handled. Note that
9223      * we can't assert this earlier because the setup of fieldoffset for
9224      * banked registers has to be done first.
9225      */
9226     if (!(r2->type & ARM_CP_NO_RAW)) {
9227         assert(!raw_accessors_invalid(r2));
9228     }
9229 
9230     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9231 }
9232 
9233 
9234 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9235                                        const ARMCPRegInfo *r, void *opaque)
9236 {
9237     /*
9238      * Define implementations of coprocessor registers.
9239      * We store these in a hashtable because typically
9240      * there are less than 150 registers in a space which
9241      * is 16*16*16*8*8 = 262144 in size.
9242      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9243      * If a register is defined twice then the second definition is
9244      * used, so this can be used to define some generic registers and
9245      * then override them with implementation specific variations.
9246      * At least one of the original and the second definition should
9247      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9248      * against accidental use.
9249      *
9250      * The state field defines whether the register is to be
9251      * visible in the AArch32 or AArch64 execution state. If the
9252      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9253      * reginfo structure for the AArch32 view, which sees the lower
9254      * 32 bits of the 64 bit register.
9255      *
9256      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9257      * be wildcarded. AArch64 registers are always considered to be 64
9258      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9259      * the register, if any.
9260      */
9261     int crm, opc1, opc2;
9262     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9263     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9264     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9265     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9266     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9267     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9268     CPState state;
9269 
9270     /* 64 bit registers have only CRm and Opc1 fields */
9271     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9272     /* op0 only exists in the AArch64 encodings */
9273     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9274     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9275     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9276     /*
9277      * This API is only for Arm's system coprocessors (14 and 15) or
9278      * (M-profile or v7A-and-earlier only) for implementation defined
9279      * coprocessors in the range 0..7.  Our decode assumes this, since
9280      * 8..13 can be used for other insns including VFP and Neon. See
9281      * valid_cp() in translate.c.  Assert here that we haven't tried
9282      * to use an invalid coprocessor number.
9283      */
9284     switch (r->state) {
9285     case ARM_CP_STATE_BOTH:
9286         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9287         if (r->cp == 0) {
9288             break;
9289         }
9290         /* fall through */
9291     case ARM_CP_STATE_AA32:
9292         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9293             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9294             assert(r->cp >= 14 && r->cp <= 15);
9295         } else {
9296             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9297         }
9298         break;
9299     case ARM_CP_STATE_AA64:
9300         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9301         break;
9302     default:
9303         g_assert_not_reached();
9304     }
9305     /*
9306      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9307      * encodes a minimum access level for the register. We roll this
9308      * runtime check into our general permission check code, so check
9309      * here that the reginfo's specified permissions are strict enough
9310      * to encompass the generic architectural permission check.
9311      */
9312     if (r->state != ARM_CP_STATE_AA32) {
9313         CPAccessRights mask;
9314         switch (r->opc1) {
9315         case 0:
9316             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9317             mask = PL0U_R | PL1_RW;
9318             break;
9319         case 1: case 2:
9320             /* min_EL EL1 */
9321             mask = PL1_RW;
9322             break;
9323         case 3:
9324             /* min_EL EL0 */
9325             mask = PL0_RW;
9326             break;
9327         case 4:
9328         case 5:
9329             /* min_EL EL2 */
9330             mask = PL2_RW;
9331             break;
9332         case 6:
9333             /* min_EL EL3 */
9334             mask = PL3_RW;
9335             break;
9336         case 7:
9337             /* min_EL EL1, secure mode only (we don't check the latter) */
9338             mask = PL1_RW;
9339             break;
9340         default:
9341             /* broken reginfo with out-of-range opc1 */
9342             g_assert_not_reached();
9343         }
9344         /* assert our permissions are not too lax (stricter is fine) */
9345         assert((r->access & ~mask) == 0);
9346     }
9347 
9348     /*
9349      * Check that the register definition has enough info to handle
9350      * reads and writes if they are permitted.
9351      */
9352     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9353         if (r->access & PL3_R) {
9354             assert((r->fieldoffset ||
9355                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9356                    r->readfn);
9357         }
9358         if (r->access & PL3_W) {
9359             assert((r->fieldoffset ||
9360                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9361                    r->writefn);
9362         }
9363     }
9364 
9365     for (crm = crmmin; crm <= crmmax; crm++) {
9366         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9367             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9368                 for (state = ARM_CP_STATE_AA32;
9369                      state <= ARM_CP_STATE_AA64; state++) {
9370                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9371                         continue;
9372                     }
9373                     if (state == ARM_CP_STATE_AA32) {
9374                         /*
9375                          * Under AArch32 CP registers can be common
9376                          * (same for secure and non-secure world) or banked.
9377                          */
9378                         char *name;
9379 
9380                         switch (r->secure) {
9381                         case ARM_CP_SECSTATE_S:
9382                         case ARM_CP_SECSTATE_NS:
9383                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9384                                                    r->secure, crm, opc1, opc2,
9385                                                    r->name);
9386                             break;
9387                         case ARM_CP_SECSTATE_BOTH:
9388                             name = g_strdup_printf("%s_S", r->name);
9389                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9390                                                    ARM_CP_SECSTATE_S,
9391                                                    crm, opc1, opc2, name);
9392                             g_free(name);
9393                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9394                                                    ARM_CP_SECSTATE_NS,
9395                                                    crm, opc1, opc2, r->name);
9396                             break;
9397                         default:
9398                             g_assert_not_reached();
9399                         }
9400                     } else {
9401                         /*
9402                          * AArch64 registers get mapped to non-secure instance
9403                          * of AArch32
9404                          */
9405                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9406                                                ARM_CP_SECSTATE_NS,
9407                                                crm, opc1, opc2, r->name);
9408                     }
9409                 }
9410             }
9411         }
9412     }
9413 }
9414 
9415 /* Define a whole list of registers */
9416 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9417                                         void *opaque, size_t len)
9418 {
9419     size_t i;
9420     for (i = 0; i < len; ++i) {
9421         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9422     }
9423 }
9424 
9425 /*
9426  * Modify ARMCPRegInfo for access from userspace.
9427  *
9428  * This is a data driven modification directed by
9429  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9430  * user-space cannot alter any values and dynamic values pertaining to
9431  * execution state are hidden from user space view anyway.
9432  */
9433 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9434                                  const ARMCPRegUserSpaceInfo *mods,
9435                                  size_t mods_len)
9436 {
9437     for (size_t mi = 0; mi < mods_len; ++mi) {
9438         const ARMCPRegUserSpaceInfo *m = mods + mi;
9439         GPatternSpec *pat = NULL;
9440 
9441         if (m->is_glob) {
9442             pat = g_pattern_spec_new(m->name);
9443         }
9444         for (size_t ri = 0; ri < regs_len; ++ri) {
9445             ARMCPRegInfo *r = regs + ri;
9446 
9447             if (pat && g_pattern_match_string(pat, r->name)) {
9448                 r->type = ARM_CP_CONST;
9449                 r->access = PL0U_R;
9450                 r->resetvalue = 0;
9451                 /* continue */
9452             } else if (strcmp(r->name, m->name) == 0) {
9453                 r->type = ARM_CP_CONST;
9454                 r->access = PL0U_R;
9455                 r->resetvalue &= m->exported_bits;
9456                 r->resetvalue |= m->fixed_bits;
9457                 break;
9458             }
9459         }
9460         if (pat) {
9461             g_pattern_spec_free(pat);
9462         }
9463     }
9464 }
9465 
9466 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9467 {
9468     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9469 }
9470 
9471 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9472                          uint64_t value)
9473 {
9474     /* Helper coprocessor write function for write-ignore registers */
9475 }
9476 
9477 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9478 {
9479     /* Helper coprocessor write function for read-as-zero registers */
9480     return 0;
9481 }
9482 
9483 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9484 {
9485     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9486 }
9487 
9488 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9489 {
9490     /*
9491      * Return true if it is not valid for us to switch to
9492      * this CPU mode (ie all the UNPREDICTABLE cases in
9493      * the ARM ARM CPSRWriteByInstr pseudocode).
9494      */
9495 
9496     /* Changes to or from Hyp via MSR and CPS are illegal. */
9497     if (write_type == CPSRWriteByInstr &&
9498         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9499          mode == ARM_CPU_MODE_HYP)) {
9500         return 1;
9501     }
9502 
9503     switch (mode) {
9504     case ARM_CPU_MODE_USR:
9505         return 0;
9506     case ARM_CPU_MODE_SYS:
9507     case ARM_CPU_MODE_SVC:
9508     case ARM_CPU_MODE_ABT:
9509     case ARM_CPU_MODE_UND:
9510     case ARM_CPU_MODE_IRQ:
9511     case ARM_CPU_MODE_FIQ:
9512         /*
9513          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9514          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9515          */
9516         /*
9517          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9518          * and CPS are treated as illegal mode changes.
9519          */
9520         if (write_type == CPSRWriteByInstr &&
9521             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9522             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9523             return 1;
9524         }
9525         return 0;
9526     case ARM_CPU_MODE_HYP:
9527         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9528     case ARM_CPU_MODE_MON:
9529         return arm_current_el(env) < 3;
9530     default:
9531         return 1;
9532     }
9533 }
9534 
9535 uint32_t cpsr_read(CPUARMState *env)
9536 {
9537     int ZF;
9538     ZF = (env->ZF == 0);
9539     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9540         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9541         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9542         | ((env->condexec_bits & 0xfc) << 8)
9543         | (env->GE << 16) | (env->daif & CPSR_AIF);
9544 }
9545 
9546 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9547                 CPSRWriteType write_type)
9548 {
9549     uint32_t changed_daif;
9550     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9551         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9552 
9553     if (mask & CPSR_NZCV) {
9554         env->ZF = (~val) & CPSR_Z;
9555         env->NF = val;
9556         env->CF = (val >> 29) & 1;
9557         env->VF = (val << 3) & 0x80000000;
9558     }
9559     if (mask & CPSR_Q) {
9560         env->QF = ((val & CPSR_Q) != 0);
9561     }
9562     if (mask & CPSR_T) {
9563         env->thumb = ((val & CPSR_T) != 0);
9564     }
9565     if (mask & CPSR_IT_0_1) {
9566         env->condexec_bits &= ~3;
9567         env->condexec_bits |= (val >> 25) & 3;
9568     }
9569     if (mask & CPSR_IT_2_7) {
9570         env->condexec_bits &= 3;
9571         env->condexec_bits |= (val >> 8) & 0xfc;
9572     }
9573     if (mask & CPSR_GE) {
9574         env->GE = (val >> 16) & 0xf;
9575     }
9576 
9577     /*
9578      * In a V7 implementation that includes the security extensions but does
9579      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9580      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9581      * bits respectively.
9582      *
9583      * In a V8 implementation, it is permitted for privileged software to
9584      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9585      */
9586     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9587         arm_feature(env, ARM_FEATURE_EL3) &&
9588         !arm_feature(env, ARM_FEATURE_EL2) &&
9589         !arm_is_secure(env)) {
9590 
9591         changed_daif = (env->daif ^ val) & mask;
9592 
9593         if (changed_daif & CPSR_A) {
9594             /*
9595              * Check to see if we are allowed to change the masking of async
9596              * abort exceptions from a non-secure state.
9597              */
9598             if (!(env->cp15.scr_el3 & SCR_AW)) {
9599                 qemu_log_mask(LOG_GUEST_ERROR,
9600                               "Ignoring attempt to switch CPSR_A flag from "
9601                               "non-secure world with SCR.AW bit clear\n");
9602                 mask &= ~CPSR_A;
9603             }
9604         }
9605 
9606         if (changed_daif & CPSR_F) {
9607             /*
9608              * Check to see if we are allowed to change the masking of FIQ
9609              * exceptions from a non-secure state.
9610              */
9611             if (!(env->cp15.scr_el3 & SCR_FW)) {
9612                 qemu_log_mask(LOG_GUEST_ERROR,
9613                               "Ignoring attempt to switch CPSR_F flag from "
9614                               "non-secure world with SCR.FW bit clear\n");
9615                 mask &= ~CPSR_F;
9616             }
9617 
9618             /*
9619              * Check whether non-maskable FIQ (NMFI) support is enabled.
9620              * If this bit is set software is not allowed to mask
9621              * FIQs, but is allowed to set CPSR_F to 0.
9622              */
9623             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9624                 (val & CPSR_F)) {
9625                 qemu_log_mask(LOG_GUEST_ERROR,
9626                               "Ignoring attempt to enable CPSR_F flag "
9627                               "(non-maskable FIQ [NMFI] support enabled)\n");
9628                 mask &= ~CPSR_F;
9629             }
9630         }
9631     }
9632 
9633     env->daif &= ~(CPSR_AIF & mask);
9634     env->daif |= val & CPSR_AIF & mask;
9635 
9636     if (write_type != CPSRWriteRaw &&
9637         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9638         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9639             /*
9640              * Note that we can only get here in USR mode if this is a
9641              * gdb stub write; for this case we follow the architectural
9642              * behaviour for guest writes in USR mode of ignoring an attempt
9643              * to switch mode. (Those are caught by translate.c for writes
9644              * triggered by guest instructions.)
9645              */
9646             mask &= ~CPSR_M;
9647         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9648             /*
9649              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9650              * v7, and has defined behaviour in v8:
9651              *  + leave CPSR.M untouched
9652              *  + allow changes to the other CPSR fields
9653              *  + set PSTATE.IL
9654              * For user changes via the GDB stub, we don't set PSTATE.IL,
9655              * as this would be unnecessarily harsh for a user error.
9656              */
9657             mask &= ~CPSR_M;
9658             if (write_type != CPSRWriteByGDBStub &&
9659                 arm_feature(env, ARM_FEATURE_V8)) {
9660                 mask |= CPSR_IL;
9661                 val |= CPSR_IL;
9662             }
9663             qemu_log_mask(LOG_GUEST_ERROR,
9664                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9665                           aarch32_mode_name(env->uncached_cpsr),
9666                           aarch32_mode_name(val));
9667         } else {
9668             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9669                           write_type == CPSRWriteExceptionReturn ?
9670                           "Exception return from AArch32" :
9671                           "AArch32 mode switch from",
9672                           aarch32_mode_name(env->uncached_cpsr),
9673                           aarch32_mode_name(val), env->regs[15]);
9674             switch_mode(env, val & CPSR_M);
9675         }
9676     }
9677     mask &= ~CACHED_CPSR_BITS;
9678     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9679     if (rebuild_hflags) {
9680         arm_rebuild_hflags(env);
9681     }
9682 }
9683 
9684 /* Sign/zero extend */
9685 uint32_t HELPER(sxtb16)(uint32_t x)
9686 {
9687     uint32_t res;
9688     res = (uint16_t)(int8_t)x;
9689     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9690     return res;
9691 }
9692 
9693 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9694 {
9695     /*
9696      * Take a division-by-zero exception if necessary; otherwise return
9697      * to get the usual non-trapping division behaviour (result of 0)
9698      */
9699     if (arm_feature(env, ARM_FEATURE_M)
9700         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9701         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9702     }
9703 }
9704 
9705 uint32_t HELPER(uxtb16)(uint32_t x)
9706 {
9707     uint32_t res;
9708     res = (uint16_t)(uint8_t)x;
9709     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9710     return res;
9711 }
9712 
9713 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9714 {
9715     if (den == 0) {
9716         handle_possible_div0_trap(env, GETPC());
9717         return 0;
9718     }
9719     if (num == INT_MIN && den == -1) {
9720         return INT_MIN;
9721     }
9722     return num / den;
9723 }
9724 
9725 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9726 {
9727     if (den == 0) {
9728         handle_possible_div0_trap(env, GETPC());
9729         return 0;
9730     }
9731     return num / den;
9732 }
9733 
9734 uint32_t HELPER(rbit)(uint32_t x)
9735 {
9736     return revbit32(x);
9737 }
9738 
9739 #ifdef CONFIG_USER_ONLY
9740 
9741 static void switch_mode(CPUARMState *env, int mode)
9742 {
9743     ARMCPU *cpu = env_archcpu(env);
9744 
9745     if (mode != ARM_CPU_MODE_USR) {
9746         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9747     }
9748 }
9749 
9750 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9751                                  uint32_t cur_el, bool secure)
9752 {
9753     return 1;
9754 }
9755 
9756 void aarch64_sync_64_to_32(CPUARMState *env)
9757 {
9758     g_assert_not_reached();
9759 }
9760 
9761 #else
9762 
9763 static void switch_mode(CPUARMState *env, int mode)
9764 {
9765     int old_mode;
9766     int i;
9767 
9768     old_mode = env->uncached_cpsr & CPSR_M;
9769     if (mode == old_mode) {
9770         return;
9771     }
9772 
9773     if (old_mode == ARM_CPU_MODE_FIQ) {
9774         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9775         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9776     } else if (mode == ARM_CPU_MODE_FIQ) {
9777         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9778         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9779     }
9780 
9781     i = bank_number(old_mode);
9782     env->banked_r13[i] = env->regs[13];
9783     env->banked_spsr[i] = env->spsr;
9784 
9785     i = bank_number(mode);
9786     env->regs[13] = env->banked_r13[i];
9787     env->spsr = env->banked_spsr[i];
9788 
9789     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9790     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9791 }
9792 
9793 /*
9794  * Physical Interrupt Target EL Lookup Table
9795  *
9796  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9797  *
9798  * The below multi-dimensional table is used for looking up the target
9799  * exception level given numerous condition criteria.  Specifically, the
9800  * target EL is based on SCR and HCR routing controls as well as the
9801  * currently executing EL and secure state.
9802  *
9803  *    Dimensions:
9804  *    target_el_table[2][2][2][2][2][4]
9805  *                    |  |  |  |  |  +--- Current EL
9806  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9807  *                    |  |  |  +--------- HCR mask override
9808  *                    |  |  +------------ SCR exec state control
9809  *                    |  +--------------- SCR mask override
9810  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9811  *
9812  *    The table values are as such:
9813  *    0-3 = EL0-EL3
9814  *     -1 = Cannot occur
9815  *
9816  * The ARM ARM target EL table includes entries indicating that an "exception
9817  * is not taken".  The two cases where this is applicable are:
9818  *    1) An exception is taken from EL3 but the SCR does not have the exception
9819  *    routed to EL3.
9820  *    2) An exception is taken from EL2 but the HCR does not have the exception
9821  *    routed to EL2.
9822  * In these two cases, the below table contain a target of EL1.  This value is
9823  * returned as it is expected that the consumer of the table data will check
9824  * for "target EL >= current EL" to ensure the exception is not taken.
9825  *
9826  *            SCR     HCR
9827  *         64  EA     AMO                 From
9828  *        BIT IRQ     IMO      Non-secure         Secure
9829  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9830  */
9831 static const int8_t target_el_table[2][2][2][2][2][4] = {
9832     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9833        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9834       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9835        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9836      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9837        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9838       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9839        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9840     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9841        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9842       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9843        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9844      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9845        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9846       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9847        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9848 };
9849 
9850 /*
9851  * Determine the target EL for physical exceptions
9852  */
9853 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9854                                  uint32_t cur_el, bool secure)
9855 {
9856     CPUARMState *env = cs->env_ptr;
9857     bool rw;
9858     bool scr;
9859     bool hcr;
9860     int target_el;
9861     /* Is the highest EL AArch64? */
9862     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9863     uint64_t hcr_el2;
9864 
9865     if (arm_feature(env, ARM_FEATURE_EL3)) {
9866         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9867     } else {
9868         /*
9869          * Either EL2 is the highest EL (and so the EL2 register width
9870          * is given by is64); or there is no EL2 or EL3, in which case
9871          * the value of 'rw' does not affect the table lookup anyway.
9872          */
9873         rw = is64;
9874     }
9875 
9876     hcr_el2 = arm_hcr_el2_eff(env);
9877     switch (excp_idx) {
9878     case EXCP_IRQ:
9879         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9880         hcr = hcr_el2 & HCR_IMO;
9881         break;
9882     case EXCP_FIQ:
9883         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9884         hcr = hcr_el2 & HCR_FMO;
9885         break;
9886     default:
9887         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9888         hcr = hcr_el2 & HCR_AMO;
9889         break;
9890     };
9891 
9892     /*
9893      * For these purposes, TGE and AMO/IMO/FMO both force the
9894      * interrupt to EL2.  Fold TGE into the bit extracted above.
9895      */
9896     hcr |= (hcr_el2 & HCR_TGE) != 0;
9897 
9898     /* Perform a table-lookup for the target EL given the current state */
9899     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9900 
9901     assert(target_el > 0);
9902 
9903     return target_el;
9904 }
9905 
9906 void arm_log_exception(CPUState *cs)
9907 {
9908     int idx = cs->exception_index;
9909 
9910     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9911         const char *exc = NULL;
9912         static const char * const excnames[] = {
9913             [EXCP_UDEF] = "Undefined Instruction",
9914             [EXCP_SWI] = "SVC",
9915             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9916             [EXCP_DATA_ABORT] = "Data Abort",
9917             [EXCP_IRQ] = "IRQ",
9918             [EXCP_FIQ] = "FIQ",
9919             [EXCP_BKPT] = "Breakpoint",
9920             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9921             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9922             [EXCP_HVC] = "Hypervisor Call",
9923             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9924             [EXCP_SMC] = "Secure Monitor Call",
9925             [EXCP_VIRQ] = "Virtual IRQ",
9926             [EXCP_VFIQ] = "Virtual FIQ",
9927             [EXCP_SEMIHOST] = "Semihosting call",
9928             [EXCP_NOCP] = "v7M NOCP UsageFault",
9929             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9930             [EXCP_STKOF] = "v8M STKOF UsageFault",
9931             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9932             [EXCP_LSERR] = "v8M LSERR UsageFault",
9933             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9934             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9935             [EXCP_VSERR] = "Virtual SERR",
9936         };
9937 
9938         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9939             exc = excnames[idx];
9940         }
9941         if (!exc) {
9942             exc = "unknown";
9943         }
9944         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9945                       idx, exc, cs->cpu_index);
9946     }
9947 }
9948 
9949 /*
9950  * Function used to synchronize QEMU's AArch64 register set with AArch32
9951  * register set.  This is necessary when switching between AArch32 and AArch64
9952  * execution state.
9953  */
9954 void aarch64_sync_32_to_64(CPUARMState *env)
9955 {
9956     int i;
9957     uint32_t mode = env->uncached_cpsr & CPSR_M;
9958 
9959     /* We can blanket copy R[0:7] to X[0:7] */
9960     for (i = 0; i < 8; i++) {
9961         env->xregs[i] = env->regs[i];
9962     }
9963 
9964     /*
9965      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9966      * Otherwise, they come from the banked user regs.
9967      */
9968     if (mode == ARM_CPU_MODE_FIQ) {
9969         for (i = 8; i < 13; i++) {
9970             env->xregs[i] = env->usr_regs[i - 8];
9971         }
9972     } else {
9973         for (i = 8; i < 13; i++) {
9974             env->xregs[i] = env->regs[i];
9975         }
9976     }
9977 
9978     /*
9979      * Registers x13-x23 are the various mode SP and FP registers. Registers
9980      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9981      * from the mode banked register.
9982      */
9983     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9984         env->xregs[13] = env->regs[13];
9985         env->xregs[14] = env->regs[14];
9986     } else {
9987         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9988         /* HYP is an exception in that it is copied from r14 */
9989         if (mode == ARM_CPU_MODE_HYP) {
9990             env->xregs[14] = env->regs[14];
9991         } else {
9992             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9993         }
9994     }
9995 
9996     if (mode == ARM_CPU_MODE_HYP) {
9997         env->xregs[15] = env->regs[13];
9998     } else {
9999         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10000     }
10001 
10002     if (mode == ARM_CPU_MODE_IRQ) {
10003         env->xregs[16] = env->regs[14];
10004         env->xregs[17] = env->regs[13];
10005     } else {
10006         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10007         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10008     }
10009 
10010     if (mode == ARM_CPU_MODE_SVC) {
10011         env->xregs[18] = env->regs[14];
10012         env->xregs[19] = env->regs[13];
10013     } else {
10014         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10015         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10016     }
10017 
10018     if (mode == ARM_CPU_MODE_ABT) {
10019         env->xregs[20] = env->regs[14];
10020         env->xregs[21] = env->regs[13];
10021     } else {
10022         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10023         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10024     }
10025 
10026     if (mode == ARM_CPU_MODE_UND) {
10027         env->xregs[22] = env->regs[14];
10028         env->xregs[23] = env->regs[13];
10029     } else {
10030         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10031         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10032     }
10033 
10034     /*
10035      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10036      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10037      * FIQ bank for r8-r14.
10038      */
10039     if (mode == ARM_CPU_MODE_FIQ) {
10040         for (i = 24; i < 31; i++) {
10041             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10042         }
10043     } else {
10044         for (i = 24; i < 29; i++) {
10045             env->xregs[i] = env->fiq_regs[i - 24];
10046         }
10047         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10048         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10049     }
10050 
10051     env->pc = env->regs[15];
10052 }
10053 
10054 /*
10055  * Function used to synchronize QEMU's AArch32 register set with AArch64
10056  * register set.  This is necessary when switching between AArch32 and AArch64
10057  * execution state.
10058  */
10059 void aarch64_sync_64_to_32(CPUARMState *env)
10060 {
10061     int i;
10062     uint32_t mode = env->uncached_cpsr & CPSR_M;
10063 
10064     /* We can blanket copy X[0:7] to R[0:7] */
10065     for (i = 0; i < 8; i++) {
10066         env->regs[i] = env->xregs[i];
10067     }
10068 
10069     /*
10070      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10071      * Otherwise, we copy x8-x12 into the banked user regs.
10072      */
10073     if (mode == ARM_CPU_MODE_FIQ) {
10074         for (i = 8; i < 13; i++) {
10075             env->usr_regs[i - 8] = env->xregs[i];
10076         }
10077     } else {
10078         for (i = 8; i < 13; i++) {
10079             env->regs[i] = env->xregs[i];
10080         }
10081     }
10082 
10083     /*
10084      * Registers r13 & r14 depend on the current mode.
10085      * If we are in a given mode, we copy the corresponding x registers to r13
10086      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10087      * for the mode.
10088      */
10089     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10090         env->regs[13] = env->xregs[13];
10091         env->regs[14] = env->xregs[14];
10092     } else {
10093         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10094 
10095         /*
10096          * HYP is an exception in that it does not have its own banked r14 but
10097          * shares the USR r14
10098          */
10099         if (mode == ARM_CPU_MODE_HYP) {
10100             env->regs[14] = env->xregs[14];
10101         } else {
10102             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10103         }
10104     }
10105 
10106     if (mode == ARM_CPU_MODE_HYP) {
10107         env->regs[13] = env->xregs[15];
10108     } else {
10109         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10110     }
10111 
10112     if (mode == ARM_CPU_MODE_IRQ) {
10113         env->regs[14] = env->xregs[16];
10114         env->regs[13] = env->xregs[17];
10115     } else {
10116         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10117         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10118     }
10119 
10120     if (mode == ARM_CPU_MODE_SVC) {
10121         env->regs[14] = env->xregs[18];
10122         env->regs[13] = env->xregs[19];
10123     } else {
10124         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10125         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10126     }
10127 
10128     if (mode == ARM_CPU_MODE_ABT) {
10129         env->regs[14] = env->xregs[20];
10130         env->regs[13] = env->xregs[21];
10131     } else {
10132         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10133         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10134     }
10135 
10136     if (mode == ARM_CPU_MODE_UND) {
10137         env->regs[14] = env->xregs[22];
10138         env->regs[13] = env->xregs[23];
10139     } else {
10140         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10141         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10142     }
10143 
10144     /*
10145      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10146      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10147      * FIQ bank for r8-r14.
10148      */
10149     if (mode == ARM_CPU_MODE_FIQ) {
10150         for (i = 24; i < 31; i++) {
10151             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10152         }
10153     } else {
10154         for (i = 24; i < 29; i++) {
10155             env->fiq_regs[i - 24] = env->xregs[i];
10156         }
10157         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10158         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10159     }
10160 
10161     env->regs[15] = env->pc;
10162 }
10163 
10164 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10165                                    uint32_t mask, uint32_t offset,
10166                                    uint32_t newpc)
10167 {
10168     int new_el;
10169 
10170     /* Change the CPU state so as to actually take the exception. */
10171     switch_mode(env, new_mode);
10172 
10173     /*
10174      * For exceptions taken to AArch32 we must clear the SS bit in both
10175      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10176      */
10177     env->pstate &= ~PSTATE_SS;
10178     env->spsr = cpsr_read(env);
10179     /* Clear IT bits.  */
10180     env->condexec_bits = 0;
10181     /* Switch to the new mode, and to the correct instruction set.  */
10182     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10183 
10184     /* This must be after mode switching. */
10185     new_el = arm_current_el(env);
10186 
10187     /* Set new mode endianness */
10188     env->uncached_cpsr &= ~CPSR_E;
10189     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10190         env->uncached_cpsr |= CPSR_E;
10191     }
10192     /* J and IL must always be cleared for exception entry */
10193     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10194     env->daif |= mask;
10195 
10196     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10197         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10198             env->uncached_cpsr |= CPSR_SSBS;
10199         } else {
10200             env->uncached_cpsr &= ~CPSR_SSBS;
10201         }
10202     }
10203 
10204     if (new_mode == ARM_CPU_MODE_HYP) {
10205         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10206         env->elr_el[2] = env->regs[15];
10207     } else {
10208         /* CPSR.PAN is normally preserved preserved unless...  */
10209         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10210             switch (new_el) {
10211             case 3:
10212                 if (!arm_is_secure_below_el3(env)) {
10213                     /* ... the target is EL3, from non-secure state.  */
10214                     env->uncached_cpsr &= ~CPSR_PAN;
10215                     break;
10216                 }
10217                 /* ... the target is EL3, from secure state ... */
10218                 /* fall through */
10219             case 1:
10220                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10221                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10222                     env->uncached_cpsr |= CPSR_PAN;
10223                 }
10224                 break;
10225             }
10226         }
10227         /*
10228          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10229          * and we should just guard the thumb mode on V4
10230          */
10231         if (arm_feature(env, ARM_FEATURE_V4T)) {
10232             env->thumb =
10233                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10234         }
10235         env->regs[14] = env->regs[15] + offset;
10236     }
10237     env->regs[15] = newpc;
10238     arm_rebuild_hflags(env);
10239 }
10240 
10241 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10242 {
10243     /*
10244      * Handle exception entry to Hyp mode; this is sufficiently
10245      * different to entry to other AArch32 modes that we handle it
10246      * separately here.
10247      *
10248      * The vector table entry used is always the 0x14 Hyp mode entry point,
10249      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10250      * The offset applied to the preferred return address is always zero
10251      * (see DDI0487C.a section G1.12.3).
10252      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10253      */
10254     uint32_t addr, mask;
10255     ARMCPU *cpu = ARM_CPU(cs);
10256     CPUARMState *env = &cpu->env;
10257 
10258     switch (cs->exception_index) {
10259     case EXCP_UDEF:
10260         addr = 0x04;
10261         break;
10262     case EXCP_SWI:
10263         addr = 0x08;
10264         break;
10265     case EXCP_BKPT:
10266         /* Fall through to prefetch abort.  */
10267     case EXCP_PREFETCH_ABORT:
10268         env->cp15.ifar_s = env->exception.vaddress;
10269         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10270                       (uint32_t)env->exception.vaddress);
10271         addr = 0x0c;
10272         break;
10273     case EXCP_DATA_ABORT:
10274         env->cp15.dfar_s = env->exception.vaddress;
10275         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10276                       (uint32_t)env->exception.vaddress);
10277         addr = 0x10;
10278         break;
10279     case EXCP_IRQ:
10280         addr = 0x18;
10281         break;
10282     case EXCP_FIQ:
10283         addr = 0x1c;
10284         break;
10285     case EXCP_HVC:
10286         addr = 0x08;
10287         break;
10288     case EXCP_HYP_TRAP:
10289         addr = 0x14;
10290         break;
10291     default:
10292         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10293     }
10294 
10295     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10296         if (!arm_feature(env, ARM_FEATURE_V8)) {
10297             /*
10298              * QEMU syndrome values are v8-style. v7 has the IL bit
10299              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10300              * If this is a v7 CPU, squash the IL bit in those cases.
10301              */
10302             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10303                 (cs->exception_index == EXCP_DATA_ABORT &&
10304                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10305                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10306                 env->exception.syndrome &= ~ARM_EL_IL;
10307             }
10308         }
10309         env->cp15.esr_el[2] = env->exception.syndrome;
10310     }
10311 
10312     if (arm_current_el(env) != 2 && addr < 0x14) {
10313         addr = 0x14;
10314     }
10315 
10316     mask = 0;
10317     if (!(env->cp15.scr_el3 & SCR_EA)) {
10318         mask |= CPSR_A;
10319     }
10320     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10321         mask |= CPSR_I;
10322     }
10323     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10324         mask |= CPSR_F;
10325     }
10326 
10327     addr += env->cp15.hvbar;
10328 
10329     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10330 }
10331 
10332 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10333 {
10334     ARMCPU *cpu = ARM_CPU(cs);
10335     CPUARMState *env = &cpu->env;
10336     uint32_t addr;
10337     uint32_t mask;
10338     int new_mode;
10339     uint32_t offset;
10340     uint32_t moe;
10341 
10342     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10343     switch (syn_get_ec(env->exception.syndrome)) {
10344     case EC_BREAKPOINT:
10345     case EC_BREAKPOINT_SAME_EL:
10346         moe = 1;
10347         break;
10348     case EC_WATCHPOINT:
10349     case EC_WATCHPOINT_SAME_EL:
10350         moe = 10;
10351         break;
10352     case EC_AA32_BKPT:
10353         moe = 3;
10354         break;
10355     case EC_VECTORCATCH:
10356         moe = 5;
10357         break;
10358     default:
10359         moe = 0;
10360         break;
10361     }
10362 
10363     if (moe) {
10364         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10365     }
10366 
10367     if (env->exception.target_el == 2) {
10368         arm_cpu_do_interrupt_aarch32_hyp(cs);
10369         return;
10370     }
10371 
10372     switch (cs->exception_index) {
10373     case EXCP_UDEF:
10374         new_mode = ARM_CPU_MODE_UND;
10375         addr = 0x04;
10376         mask = CPSR_I;
10377         if (env->thumb) {
10378             offset = 2;
10379         } else {
10380             offset = 4;
10381         }
10382         break;
10383     case EXCP_SWI:
10384         new_mode = ARM_CPU_MODE_SVC;
10385         addr = 0x08;
10386         mask = CPSR_I;
10387         /* The PC already points to the next instruction.  */
10388         offset = 0;
10389         break;
10390     case EXCP_BKPT:
10391         /* Fall through to prefetch abort.  */
10392     case EXCP_PREFETCH_ABORT:
10393         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10394         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10395         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10396                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10397         new_mode = ARM_CPU_MODE_ABT;
10398         addr = 0x0c;
10399         mask = CPSR_A | CPSR_I;
10400         offset = 4;
10401         break;
10402     case EXCP_DATA_ABORT:
10403         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10404         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10405         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10406                       env->exception.fsr,
10407                       (uint32_t)env->exception.vaddress);
10408         new_mode = ARM_CPU_MODE_ABT;
10409         addr = 0x10;
10410         mask = CPSR_A | CPSR_I;
10411         offset = 8;
10412         break;
10413     case EXCP_IRQ:
10414         new_mode = ARM_CPU_MODE_IRQ;
10415         addr = 0x18;
10416         /* Disable IRQ and imprecise data aborts.  */
10417         mask = CPSR_A | CPSR_I;
10418         offset = 4;
10419         if (env->cp15.scr_el3 & SCR_IRQ) {
10420             /* IRQ routed to monitor mode */
10421             new_mode = ARM_CPU_MODE_MON;
10422             mask |= CPSR_F;
10423         }
10424         break;
10425     case EXCP_FIQ:
10426         new_mode = ARM_CPU_MODE_FIQ;
10427         addr = 0x1c;
10428         /* Disable FIQ, IRQ and imprecise data aborts.  */
10429         mask = CPSR_A | CPSR_I | CPSR_F;
10430         if (env->cp15.scr_el3 & SCR_FIQ) {
10431             /* FIQ routed to monitor mode */
10432             new_mode = ARM_CPU_MODE_MON;
10433         }
10434         offset = 4;
10435         break;
10436     case EXCP_VIRQ:
10437         new_mode = ARM_CPU_MODE_IRQ;
10438         addr = 0x18;
10439         /* Disable IRQ and imprecise data aborts.  */
10440         mask = CPSR_A | CPSR_I;
10441         offset = 4;
10442         break;
10443     case EXCP_VFIQ:
10444         new_mode = ARM_CPU_MODE_FIQ;
10445         addr = 0x1c;
10446         /* Disable FIQ, IRQ and imprecise data aborts.  */
10447         mask = CPSR_A | CPSR_I | CPSR_F;
10448         offset = 4;
10449         break;
10450     case EXCP_VSERR:
10451         {
10452             /*
10453              * Note that this is reported as a data abort, but the DFAR
10454              * has an UNKNOWN value.  Construct the SError syndrome from
10455              * AET and ExT fields.
10456              */
10457             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10458 
10459             if (extended_addresses_enabled(env)) {
10460                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10461             } else {
10462                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10463             }
10464             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10465             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10466             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10467                           env->exception.fsr);
10468 
10469             new_mode = ARM_CPU_MODE_ABT;
10470             addr = 0x10;
10471             mask = CPSR_A | CPSR_I;
10472             offset = 8;
10473         }
10474         break;
10475     case EXCP_SMC:
10476         new_mode = ARM_CPU_MODE_MON;
10477         addr = 0x08;
10478         mask = CPSR_A | CPSR_I | CPSR_F;
10479         offset = 0;
10480         break;
10481     default:
10482         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10483         return; /* Never happens.  Keep compiler happy.  */
10484     }
10485 
10486     if (new_mode == ARM_CPU_MODE_MON) {
10487         addr += env->cp15.mvbar;
10488     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10489         /* High vectors. When enabled, base address cannot be remapped. */
10490         addr += 0xffff0000;
10491     } else {
10492         /*
10493          * ARM v7 architectures provide a vector base address register to remap
10494          * the interrupt vector table.
10495          * This register is only followed in non-monitor mode, and is banked.
10496          * Note: only bits 31:5 are valid.
10497          */
10498         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10499     }
10500 
10501     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10502         env->cp15.scr_el3 &= ~SCR_NS;
10503     }
10504 
10505     take_aarch32_exception(env, new_mode, mask, offset, addr);
10506 }
10507 
10508 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10509 {
10510     /*
10511      * Return the register number of the AArch64 view of the AArch32
10512      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10513      * be that of the AArch32 mode the exception came from.
10514      */
10515     int mode = env->uncached_cpsr & CPSR_M;
10516 
10517     switch (aarch32_reg) {
10518     case 0 ... 7:
10519         return aarch32_reg;
10520     case 8 ... 12:
10521         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10522     case 13:
10523         switch (mode) {
10524         case ARM_CPU_MODE_USR:
10525         case ARM_CPU_MODE_SYS:
10526             return 13;
10527         case ARM_CPU_MODE_HYP:
10528             return 15;
10529         case ARM_CPU_MODE_IRQ:
10530             return 17;
10531         case ARM_CPU_MODE_SVC:
10532             return 19;
10533         case ARM_CPU_MODE_ABT:
10534             return 21;
10535         case ARM_CPU_MODE_UND:
10536             return 23;
10537         case ARM_CPU_MODE_FIQ:
10538             return 29;
10539         default:
10540             g_assert_not_reached();
10541         }
10542     case 14:
10543         switch (mode) {
10544         case ARM_CPU_MODE_USR:
10545         case ARM_CPU_MODE_SYS:
10546         case ARM_CPU_MODE_HYP:
10547             return 14;
10548         case ARM_CPU_MODE_IRQ:
10549             return 16;
10550         case ARM_CPU_MODE_SVC:
10551             return 18;
10552         case ARM_CPU_MODE_ABT:
10553             return 20;
10554         case ARM_CPU_MODE_UND:
10555             return 22;
10556         case ARM_CPU_MODE_FIQ:
10557             return 30;
10558         default:
10559             g_assert_not_reached();
10560         }
10561     case 15:
10562         return 31;
10563     default:
10564         g_assert_not_reached();
10565     }
10566 }
10567 
10568 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10569 {
10570     uint32_t ret = cpsr_read(env);
10571 
10572     /* Move DIT to the correct location for SPSR_ELx */
10573     if (ret & CPSR_DIT) {
10574         ret &= ~CPSR_DIT;
10575         ret |= PSTATE_DIT;
10576     }
10577     /* Merge PSTATE.SS into SPSR_ELx */
10578     ret |= env->pstate & PSTATE_SS;
10579 
10580     return ret;
10581 }
10582 
10583 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10584 {
10585     /* Return true if this syndrome value is a synchronous external abort */
10586     switch (syn_get_ec(syndrome)) {
10587     case EC_INSNABORT:
10588     case EC_INSNABORT_SAME_EL:
10589     case EC_DATAABORT:
10590     case EC_DATAABORT_SAME_EL:
10591         /* Look at fault status code for all the synchronous ext abort cases */
10592         switch (syndrome & 0x3f) {
10593         case 0x10:
10594         case 0x13:
10595         case 0x14:
10596         case 0x15:
10597         case 0x16:
10598         case 0x17:
10599             return true;
10600         default:
10601             return false;
10602         }
10603     default:
10604         return false;
10605     }
10606 }
10607 
10608 /* Handle exception entry to a target EL which is using AArch64 */
10609 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10610 {
10611     ARMCPU *cpu = ARM_CPU(cs);
10612     CPUARMState *env = &cpu->env;
10613     unsigned int new_el = env->exception.target_el;
10614     target_ulong addr = env->cp15.vbar_el[new_el];
10615     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10616     unsigned int old_mode;
10617     unsigned int cur_el = arm_current_el(env);
10618     int rt;
10619 
10620     /*
10621      * Note that new_el can never be 0.  If cur_el is 0, then
10622      * el0_a64 is is_a64(), else el0_a64 is ignored.
10623      */
10624     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10625 
10626     if (cur_el < new_el) {
10627         /*
10628          * Entry vector offset depends on whether the implemented EL
10629          * immediately lower than the target level is using AArch32 or AArch64
10630          */
10631         bool is_aa64;
10632         uint64_t hcr;
10633 
10634         switch (new_el) {
10635         case 3:
10636             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10637             break;
10638         case 2:
10639             hcr = arm_hcr_el2_eff(env);
10640             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10641                 is_aa64 = (hcr & HCR_RW) != 0;
10642                 break;
10643             }
10644             /* fall through */
10645         case 1:
10646             is_aa64 = is_a64(env);
10647             break;
10648         default:
10649             g_assert_not_reached();
10650         }
10651 
10652         if (is_aa64) {
10653             addr += 0x400;
10654         } else {
10655             addr += 0x600;
10656         }
10657     } else if (pstate_read(env) & PSTATE_SP) {
10658         addr += 0x200;
10659     }
10660 
10661     switch (cs->exception_index) {
10662     case EXCP_PREFETCH_ABORT:
10663     case EXCP_DATA_ABORT:
10664         /*
10665          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10666          * to be taken to the SError vector entrypoint.
10667          */
10668         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10669             syndrome_is_sync_extabt(env->exception.syndrome)) {
10670             addr += 0x180;
10671         }
10672         env->cp15.far_el[new_el] = env->exception.vaddress;
10673         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10674                       env->cp15.far_el[new_el]);
10675         /* fall through */
10676     case EXCP_BKPT:
10677     case EXCP_UDEF:
10678     case EXCP_SWI:
10679     case EXCP_HVC:
10680     case EXCP_HYP_TRAP:
10681     case EXCP_SMC:
10682         switch (syn_get_ec(env->exception.syndrome)) {
10683         case EC_ADVSIMDFPACCESSTRAP:
10684             /*
10685              * QEMU internal FP/SIMD syndromes from AArch32 include the
10686              * TA and coproc fields which are only exposed if the exception
10687              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10688              * AArch64 format syndrome.
10689              */
10690             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10691             break;
10692         case EC_CP14RTTRAP:
10693         case EC_CP15RTTRAP:
10694         case EC_CP14DTTRAP:
10695             /*
10696              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10697              * the raw register field from the insn; when taking this to
10698              * AArch64 we must convert it to the AArch64 view of the register
10699              * number. Notice that we read a 4-bit AArch32 register number and
10700              * write back a 5-bit AArch64 one.
10701              */
10702             rt = extract32(env->exception.syndrome, 5, 4);
10703             rt = aarch64_regnum(env, rt);
10704             env->exception.syndrome = deposit32(env->exception.syndrome,
10705                                                 5, 5, rt);
10706             break;
10707         case EC_CP15RRTTRAP:
10708         case EC_CP14RRTTRAP:
10709             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10710             rt = extract32(env->exception.syndrome, 5, 4);
10711             rt = aarch64_regnum(env, rt);
10712             env->exception.syndrome = deposit32(env->exception.syndrome,
10713                                                 5, 5, rt);
10714             rt = extract32(env->exception.syndrome, 10, 4);
10715             rt = aarch64_regnum(env, rt);
10716             env->exception.syndrome = deposit32(env->exception.syndrome,
10717                                                 10, 5, rt);
10718             break;
10719         }
10720         env->cp15.esr_el[new_el] = env->exception.syndrome;
10721         break;
10722     case EXCP_IRQ:
10723     case EXCP_VIRQ:
10724         addr += 0x80;
10725         break;
10726     case EXCP_FIQ:
10727     case EXCP_VFIQ:
10728         addr += 0x100;
10729         break;
10730     case EXCP_VSERR:
10731         addr += 0x180;
10732         /* Construct the SError syndrome from IDS and ISS fields. */
10733         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10734         env->cp15.esr_el[new_el] = env->exception.syndrome;
10735         break;
10736     default:
10737         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10738     }
10739 
10740     if (is_a64(env)) {
10741         old_mode = pstate_read(env);
10742         aarch64_save_sp(env, arm_current_el(env));
10743         env->elr_el[new_el] = env->pc;
10744     } else {
10745         old_mode = cpsr_read_for_spsr_elx(env);
10746         env->elr_el[new_el] = env->regs[15];
10747 
10748         aarch64_sync_32_to_64(env);
10749 
10750         env->condexec_bits = 0;
10751     }
10752     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10753 
10754     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10755                   env->elr_el[new_el]);
10756 
10757     if (cpu_isar_feature(aa64_pan, cpu)) {
10758         /* The value of PSTATE.PAN is normally preserved, except when ... */
10759         new_mode |= old_mode & PSTATE_PAN;
10760         switch (new_el) {
10761         case 2:
10762             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10763             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10764                 != (HCR_E2H | HCR_TGE)) {
10765                 break;
10766             }
10767             /* fall through */
10768         case 1:
10769             /* ... the target is EL1 ... */
10770             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10771             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10772                 new_mode |= PSTATE_PAN;
10773             }
10774             break;
10775         }
10776     }
10777     if (cpu_isar_feature(aa64_mte, cpu)) {
10778         new_mode |= PSTATE_TCO;
10779     }
10780 
10781     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10782         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10783             new_mode |= PSTATE_SSBS;
10784         } else {
10785             new_mode &= ~PSTATE_SSBS;
10786         }
10787     }
10788 
10789     pstate_write(env, PSTATE_DAIF | new_mode);
10790     env->aarch64 = true;
10791     aarch64_restore_sp(env, new_el);
10792     helper_rebuild_hflags_a64(env, new_el);
10793 
10794     env->pc = addr;
10795 
10796     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10797                   new_el, env->pc, pstate_read(env));
10798 }
10799 
10800 /*
10801  * Do semihosting call and set the appropriate return value. All the
10802  * permission and validity checks have been done at translate time.
10803  *
10804  * We only see semihosting exceptions in TCG only as they are not
10805  * trapped to the hypervisor in KVM.
10806  */
10807 #ifdef CONFIG_TCG
10808 static void handle_semihosting(CPUState *cs)
10809 {
10810     ARMCPU *cpu = ARM_CPU(cs);
10811     CPUARMState *env = &cpu->env;
10812 
10813     if (is_a64(env)) {
10814         qemu_log_mask(CPU_LOG_INT,
10815                       "...handling as semihosting call 0x%" PRIx64 "\n",
10816                       env->xregs[0]);
10817         do_common_semihosting(cs);
10818         env->pc += 4;
10819     } else {
10820         qemu_log_mask(CPU_LOG_INT,
10821                       "...handling as semihosting call 0x%x\n",
10822                       env->regs[0]);
10823         do_common_semihosting(cs);
10824         env->regs[15] += env->thumb ? 2 : 4;
10825     }
10826 }
10827 #endif
10828 
10829 /*
10830  * Handle a CPU exception for A and R profile CPUs.
10831  * Do any appropriate logging, handle PSCI calls, and then hand off
10832  * to the AArch64-entry or AArch32-entry function depending on the
10833  * target exception level's register width.
10834  *
10835  * Note: this is used for both TCG (as the do_interrupt tcg op),
10836  *       and KVM to re-inject guest debug exceptions, and to
10837  *       inject a Synchronous-External-Abort.
10838  */
10839 void arm_cpu_do_interrupt(CPUState *cs)
10840 {
10841     ARMCPU *cpu = ARM_CPU(cs);
10842     CPUARMState *env = &cpu->env;
10843     unsigned int new_el = env->exception.target_el;
10844 
10845     assert(!arm_feature(env, ARM_FEATURE_M));
10846 
10847     arm_log_exception(cs);
10848     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10849                   new_el);
10850     if (qemu_loglevel_mask(CPU_LOG_INT)
10851         && !excp_is_internal(cs->exception_index)) {
10852         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10853                       syn_get_ec(env->exception.syndrome),
10854                       env->exception.syndrome);
10855     }
10856 
10857     if (arm_is_psci_call(cpu, cs->exception_index)) {
10858         arm_handle_psci_call(cpu);
10859         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10860         return;
10861     }
10862 
10863     /*
10864      * Semihosting semantics depend on the register width of the code
10865      * that caused the exception, not the target exception level, so
10866      * must be handled here.
10867      */
10868 #ifdef CONFIG_TCG
10869     if (cs->exception_index == EXCP_SEMIHOST) {
10870         handle_semihosting(cs);
10871         return;
10872     }
10873 #endif
10874 
10875     /*
10876      * Hooks may change global state so BQL should be held, also the
10877      * BQL needs to be held for any modification of
10878      * cs->interrupt_request.
10879      */
10880     g_assert(qemu_mutex_iothread_locked());
10881 
10882     arm_call_pre_el_change_hook(cpu);
10883 
10884     assert(!excp_is_internal(cs->exception_index));
10885     if (arm_el_is_aa64(env, new_el)) {
10886         arm_cpu_do_interrupt_aarch64(cs);
10887     } else {
10888         arm_cpu_do_interrupt_aarch32(cs);
10889     }
10890 
10891     arm_call_el_change_hook(cpu);
10892 
10893     if (!kvm_enabled()) {
10894         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10895     }
10896 }
10897 #endif /* !CONFIG_USER_ONLY */
10898 
10899 uint64_t arm_sctlr(CPUARMState *env, int el)
10900 {
10901     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10902     if (el == 0) {
10903         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10904         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10905     }
10906     return env->cp15.sctlr_el[el];
10907 }
10908 
10909 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10910 {
10911     if (regime_has_2_ranges(mmu_idx)) {
10912         return extract64(tcr, 37, 2);
10913     } else if (regime_is_stage2(mmu_idx)) {
10914         return 0; /* VTCR_EL2 */
10915     } else {
10916         /* Replicate the single TBI bit so we always have 2 bits.  */
10917         return extract32(tcr, 20, 1) * 3;
10918     }
10919 }
10920 
10921 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10922 {
10923     if (regime_has_2_ranges(mmu_idx)) {
10924         return extract64(tcr, 51, 2);
10925     } else if (regime_is_stage2(mmu_idx)) {
10926         return 0; /* VTCR_EL2 */
10927     } else {
10928         /* Replicate the single TBID bit so we always have 2 bits.  */
10929         return extract32(tcr, 29, 1) * 3;
10930     }
10931 }
10932 
10933 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10934 {
10935     if (regime_has_2_ranges(mmu_idx)) {
10936         return extract64(tcr, 57, 2);
10937     } else {
10938         /* Replicate the single TCMA bit so we always have 2 bits.  */
10939         return extract32(tcr, 30, 1) * 3;
10940     }
10941 }
10942 
10943 static ARMGranuleSize tg0_to_gran_size(int tg)
10944 {
10945     switch (tg) {
10946     case 0:
10947         return Gran4K;
10948     case 1:
10949         return Gran64K;
10950     case 2:
10951         return Gran16K;
10952     default:
10953         return GranInvalid;
10954     }
10955 }
10956 
10957 static ARMGranuleSize tg1_to_gran_size(int tg)
10958 {
10959     switch (tg) {
10960     case 1:
10961         return Gran16K;
10962     case 2:
10963         return Gran4K;
10964     case 3:
10965         return Gran64K;
10966     default:
10967         return GranInvalid;
10968     }
10969 }
10970 
10971 static inline bool have4k(ARMCPU *cpu, bool stage2)
10972 {
10973     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10974         : cpu_isar_feature(aa64_tgran4, cpu);
10975 }
10976 
10977 static inline bool have16k(ARMCPU *cpu, bool stage2)
10978 {
10979     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10980         : cpu_isar_feature(aa64_tgran16, cpu);
10981 }
10982 
10983 static inline bool have64k(ARMCPU *cpu, bool stage2)
10984 {
10985     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10986         : cpu_isar_feature(aa64_tgran64, cpu);
10987 }
10988 
10989 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10990                                          bool stage2)
10991 {
10992     switch (gran) {
10993     case Gran4K:
10994         if (have4k(cpu, stage2)) {
10995             return gran;
10996         }
10997         break;
10998     case Gran16K:
10999         if (have16k(cpu, stage2)) {
11000             return gran;
11001         }
11002         break;
11003     case Gran64K:
11004         if (have64k(cpu, stage2)) {
11005             return gran;
11006         }
11007         break;
11008     case GranInvalid:
11009         break;
11010     }
11011     /*
11012      * If the guest selects a granule size that isn't implemented,
11013      * the architecture requires that we behave as if it selected one
11014      * that is (with an IMPDEF choice of which one to pick). We choose
11015      * to implement the smallest supported granule size.
11016      */
11017     if (have4k(cpu, stage2)) {
11018         return Gran4K;
11019     }
11020     if (have16k(cpu, stage2)) {
11021         return Gran16K;
11022     }
11023     assert(have64k(cpu, stage2));
11024     return Gran64K;
11025 }
11026 
11027 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11028                                    ARMMMUIdx mmu_idx, bool data)
11029 {
11030     uint64_t tcr = regime_tcr(env, mmu_idx);
11031     bool epd, hpd, tsz_oob, ds, ha, hd;
11032     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11033     ARMGranuleSize gran;
11034     ARMCPU *cpu = env_archcpu(env);
11035     bool stage2 = regime_is_stage2(mmu_idx);
11036 
11037     if (!regime_has_2_ranges(mmu_idx)) {
11038         select = 0;
11039         tsz = extract32(tcr, 0, 6);
11040         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11041         if (stage2) {
11042             /* VTCR_EL2 */
11043             hpd = false;
11044         } else {
11045             hpd = extract32(tcr, 24, 1);
11046         }
11047         epd = false;
11048         sh = extract32(tcr, 12, 2);
11049         ps = extract32(tcr, 16, 3);
11050         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11051         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11052         ds = extract64(tcr, 32, 1);
11053     } else {
11054         bool e0pd;
11055 
11056         /*
11057          * Bit 55 is always between the two regions, and is canonical for
11058          * determining if address tagging is enabled.
11059          */
11060         select = extract64(va, 55, 1);
11061         if (!select) {
11062             tsz = extract32(tcr, 0, 6);
11063             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11064             epd = extract32(tcr, 7, 1);
11065             sh = extract32(tcr, 12, 2);
11066             hpd = extract64(tcr, 41, 1);
11067             e0pd = extract64(tcr, 55, 1);
11068         } else {
11069             tsz = extract32(tcr, 16, 6);
11070             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11071             epd = extract32(tcr, 23, 1);
11072             sh = extract32(tcr, 28, 2);
11073             hpd = extract64(tcr, 42, 1);
11074             e0pd = extract64(tcr, 56, 1);
11075         }
11076         ps = extract64(tcr, 32, 3);
11077         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11078         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11079         ds = extract64(tcr, 59, 1);
11080 
11081         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11082             regime_is_user(env, mmu_idx)) {
11083             epd = true;
11084         }
11085     }
11086 
11087     gran = sanitize_gran_size(cpu, gran, stage2);
11088 
11089     if (cpu_isar_feature(aa64_st, cpu)) {
11090         max_tsz = 48 - (gran == Gran64K);
11091     } else {
11092         max_tsz = 39;
11093     }
11094 
11095     /*
11096      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11097      * adjust the effective value of DS, as documented.
11098      */
11099     min_tsz = 16;
11100     if (gran == Gran64K) {
11101         if (cpu_isar_feature(aa64_lva, cpu)) {
11102             min_tsz = 12;
11103         }
11104         ds = false;
11105     } else if (ds) {
11106         if (regime_is_stage2(mmu_idx)) {
11107             if (gran == Gran16K) {
11108                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11109             } else {
11110                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11111             }
11112         } else {
11113             if (gran == Gran16K) {
11114                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11115             } else {
11116                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11117             }
11118         }
11119         if (ds) {
11120             min_tsz = 12;
11121         }
11122     }
11123 
11124     if (tsz > max_tsz) {
11125         tsz = max_tsz;
11126         tsz_oob = true;
11127     } else if (tsz < min_tsz) {
11128         tsz = min_tsz;
11129         tsz_oob = true;
11130     } else {
11131         tsz_oob = false;
11132     }
11133 
11134     /* Present TBI as a composite with TBID.  */
11135     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11136     if (!data) {
11137         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11138     }
11139     tbi = (tbi >> select) & 1;
11140 
11141     return (ARMVAParameters) {
11142         .tsz = tsz,
11143         .ps = ps,
11144         .sh = sh,
11145         .select = select,
11146         .tbi = tbi,
11147         .epd = epd,
11148         .hpd = hpd,
11149         .tsz_oob = tsz_oob,
11150         .ds = ds,
11151         .ha = ha,
11152         .hd = ha && hd,
11153         .gran = gran,
11154     };
11155 }
11156 
11157 /*
11158  * Note that signed overflow is undefined in C.  The following routines are
11159  * careful to use unsigned types where modulo arithmetic is required.
11160  * Failure to do so _will_ break on newer gcc.
11161  */
11162 
11163 /* Signed saturating arithmetic.  */
11164 
11165 /* Perform 16-bit signed saturating addition.  */
11166 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11167 {
11168     uint16_t res;
11169 
11170     res = a + b;
11171     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11172         if (a & 0x8000) {
11173             res = 0x8000;
11174         } else {
11175             res = 0x7fff;
11176         }
11177     }
11178     return res;
11179 }
11180 
11181 /* Perform 8-bit signed saturating addition.  */
11182 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11183 {
11184     uint8_t res;
11185 
11186     res = a + b;
11187     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11188         if (a & 0x80) {
11189             res = 0x80;
11190         } else {
11191             res = 0x7f;
11192         }
11193     }
11194     return res;
11195 }
11196 
11197 /* Perform 16-bit signed saturating subtraction.  */
11198 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11199 {
11200     uint16_t res;
11201 
11202     res = a - b;
11203     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11204         if (a & 0x8000) {
11205             res = 0x8000;
11206         } else {
11207             res = 0x7fff;
11208         }
11209     }
11210     return res;
11211 }
11212 
11213 /* Perform 8-bit signed saturating subtraction.  */
11214 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11215 {
11216     uint8_t res;
11217 
11218     res = a - b;
11219     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11220         if (a & 0x80) {
11221             res = 0x80;
11222         } else {
11223             res = 0x7f;
11224         }
11225     }
11226     return res;
11227 }
11228 
11229 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11230 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11231 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11232 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11233 #define PFX q
11234 
11235 #include "op_addsub.h"
11236 
11237 /* Unsigned saturating arithmetic.  */
11238 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11239 {
11240     uint16_t res;
11241     res = a + b;
11242     if (res < a) {
11243         res = 0xffff;
11244     }
11245     return res;
11246 }
11247 
11248 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11249 {
11250     if (a > b) {
11251         return a - b;
11252     } else {
11253         return 0;
11254     }
11255 }
11256 
11257 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11258 {
11259     uint8_t res;
11260     res = a + b;
11261     if (res < a) {
11262         res = 0xff;
11263     }
11264     return res;
11265 }
11266 
11267 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11268 {
11269     if (a > b) {
11270         return a - b;
11271     } else {
11272         return 0;
11273     }
11274 }
11275 
11276 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11277 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11278 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11279 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11280 #define PFX uq
11281 
11282 #include "op_addsub.h"
11283 
11284 /* Signed modulo arithmetic.  */
11285 #define SARITH16(a, b, n, op) do { \
11286     int32_t sum; \
11287     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11288     RESULT(sum, n, 16); \
11289     if (sum >= 0) \
11290         ge |= 3 << (n * 2); \
11291     } while (0)
11292 
11293 #define SARITH8(a, b, n, op) do { \
11294     int32_t sum; \
11295     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11296     RESULT(sum, n, 8); \
11297     if (sum >= 0) \
11298         ge |= 1 << n; \
11299     } while (0)
11300 
11301 
11302 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11303 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11304 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11305 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11306 #define PFX s
11307 #define ARITH_GE
11308 
11309 #include "op_addsub.h"
11310 
11311 /* Unsigned modulo arithmetic.  */
11312 #define ADD16(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) == 1) \
11317         ge |= 3 << (n * 2); \
11318     } while (0)
11319 
11320 #define ADD8(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) == 1) \
11325         ge |= 1 << n; \
11326     } while (0)
11327 
11328 #define SUB16(a, b, n) do { \
11329     uint32_t sum; \
11330     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11331     RESULT(sum, n, 16); \
11332     if ((sum >> 16) == 0) \
11333         ge |= 3 << (n * 2); \
11334     } while (0)
11335 
11336 #define SUB8(a, b, n) do { \
11337     uint32_t sum; \
11338     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11339     RESULT(sum, n, 8); \
11340     if ((sum >> 8) == 0) \
11341         ge |= 1 << n; \
11342     } while (0)
11343 
11344 #define PFX u
11345 #define ARITH_GE
11346 
11347 #include "op_addsub.h"
11348 
11349 /* Halved signed arithmetic.  */
11350 #define ADD16(a, b, n) \
11351   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11352 #define SUB16(a, b, n) \
11353   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11354 #define ADD8(a, b, n) \
11355   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11356 #define SUB8(a, b, n) \
11357   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11358 #define PFX sh
11359 
11360 #include "op_addsub.h"
11361 
11362 /* Halved unsigned arithmetic.  */
11363 #define ADD16(a, b, n) \
11364   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11365 #define SUB16(a, b, n) \
11366   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11367 #define ADD8(a, b, n) \
11368   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11369 #define SUB8(a, b, n) \
11370   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11371 #define PFX uh
11372 
11373 #include "op_addsub.h"
11374 
11375 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11376 {
11377     if (a > b) {
11378         return a - b;
11379     } else {
11380         return b - a;
11381     }
11382 }
11383 
11384 /* Unsigned sum of absolute byte differences.  */
11385 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11386 {
11387     uint32_t sum;
11388     sum = do_usad(a, b);
11389     sum += do_usad(a >> 8, b >> 8);
11390     sum += do_usad(a >> 16, b >> 16);
11391     sum += do_usad(a >> 24, b >> 24);
11392     return sum;
11393 }
11394 
11395 /* For ARMv6 SEL instruction.  */
11396 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11397 {
11398     uint32_t mask;
11399 
11400     mask = 0;
11401     if (flags & 1) {
11402         mask |= 0xff;
11403     }
11404     if (flags & 2) {
11405         mask |= 0xff00;
11406     }
11407     if (flags & 4) {
11408         mask |= 0xff0000;
11409     }
11410     if (flags & 8) {
11411         mask |= 0xff000000;
11412     }
11413     return (a & mask) | (b & ~mask);
11414 }
11415 
11416 /*
11417  * CRC helpers.
11418  * The upper bytes of val (above the number specified by 'bytes') must have
11419  * been zeroed out by the caller.
11420  */
11421 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11422 {
11423     uint8_t buf[4];
11424 
11425     stl_le_p(buf, val);
11426 
11427     /* zlib crc32 converts the accumulator and output to one's complement.  */
11428     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11429 }
11430 
11431 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11432 {
11433     uint8_t buf[4];
11434 
11435     stl_le_p(buf, val);
11436 
11437     /* Linux crc32c converts the output to one's complement.  */
11438     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11439 }
11440 
11441 /*
11442  * Return the exception level to which FP-disabled exceptions should
11443  * be taken, or 0 if FP is enabled.
11444  */
11445 int fp_exception_el(CPUARMState *env, int cur_el)
11446 {
11447 #ifndef CONFIG_USER_ONLY
11448     uint64_t hcr_el2;
11449 
11450     /*
11451      * CPACR and the CPTR registers don't exist before v6, so FP is
11452      * always accessible
11453      */
11454     if (!arm_feature(env, ARM_FEATURE_V6)) {
11455         return 0;
11456     }
11457 
11458     if (arm_feature(env, ARM_FEATURE_M)) {
11459         /* CPACR can cause a NOCP UsageFault taken to current security state */
11460         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11461             return 1;
11462         }
11463 
11464         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11465             if (!extract32(env->v7m.nsacr, 10, 1)) {
11466                 /* FP insns cause a NOCP UsageFault taken to Secure */
11467                 return 3;
11468             }
11469         }
11470 
11471         return 0;
11472     }
11473 
11474     hcr_el2 = arm_hcr_el2_eff(env);
11475 
11476     /*
11477      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11478      * 0, 2 : trap EL0 and EL1/PL1 accesses
11479      * 1    : trap only EL0 accesses
11480      * 3    : trap no accesses
11481      * This register is ignored if E2H+TGE are both set.
11482      */
11483     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11484         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11485 
11486         switch (fpen) {
11487         case 1:
11488             if (cur_el != 0) {
11489                 break;
11490             }
11491             /* fall through */
11492         case 0:
11493         case 2:
11494             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11495             if (!arm_el_is_aa64(env, 3)
11496                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11497                 return 3;
11498             }
11499             if (cur_el <= 1) {
11500                 return 1;
11501             }
11502             break;
11503         }
11504     }
11505 
11506     /*
11507      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11508      * to control non-secure access to the FPU. It doesn't have any
11509      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11510      */
11511     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11512          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11513         if (!extract32(env->cp15.nsacr, 10, 1)) {
11514             /* FP insns act as UNDEF */
11515             return cur_el == 2 ? 2 : 1;
11516         }
11517     }
11518 
11519     /*
11520      * CPTR_EL2 is present in v7VE or v8, and changes format
11521      * with HCR_EL2.E2H (regardless of TGE).
11522      */
11523     if (cur_el <= 2) {
11524         if (hcr_el2 & HCR_E2H) {
11525             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11526             case 1:
11527                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11528                     break;
11529                 }
11530                 /* fall through */
11531             case 0:
11532             case 2:
11533                 return 2;
11534             }
11535         } else if (arm_is_el2_enabled(env)) {
11536             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11537                 return 2;
11538             }
11539         }
11540     }
11541 
11542     /* CPTR_EL3 : present in v8 */
11543     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11544         /* Trap all FP ops to EL3 */
11545         return 3;
11546     }
11547 #endif
11548     return 0;
11549 }
11550 
11551 /* Return the exception level we're running at if this is our mmu_idx */
11552 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11553 {
11554     if (mmu_idx & ARM_MMU_IDX_M) {
11555         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11556     }
11557 
11558     switch (mmu_idx) {
11559     case ARMMMUIdx_E10_0:
11560     case ARMMMUIdx_E20_0:
11561         return 0;
11562     case ARMMMUIdx_E10_1:
11563     case ARMMMUIdx_E10_1_PAN:
11564         return 1;
11565     case ARMMMUIdx_E2:
11566     case ARMMMUIdx_E20_2:
11567     case ARMMMUIdx_E20_2_PAN:
11568         return 2;
11569     case ARMMMUIdx_E3:
11570         return 3;
11571     default:
11572         g_assert_not_reached();
11573     }
11574 }
11575 
11576 #ifndef CONFIG_TCG
11577 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11578 {
11579     g_assert_not_reached();
11580 }
11581 #endif
11582 
11583 static bool arm_pan_enabled(CPUARMState *env)
11584 {
11585     if (is_a64(env)) {
11586         return env->pstate & PSTATE_PAN;
11587     } else {
11588         return env->uncached_cpsr & CPSR_PAN;
11589     }
11590 }
11591 
11592 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11593 {
11594     ARMMMUIdx idx;
11595     uint64_t hcr;
11596 
11597     if (arm_feature(env, ARM_FEATURE_M)) {
11598         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11599     }
11600 
11601     /* See ARM pseudo-function ELIsInHost.  */
11602     switch (el) {
11603     case 0:
11604         hcr = arm_hcr_el2_eff(env);
11605         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11606             idx = ARMMMUIdx_E20_0;
11607         } else {
11608             idx = ARMMMUIdx_E10_0;
11609         }
11610         break;
11611     case 1:
11612         if (arm_pan_enabled(env)) {
11613             idx = ARMMMUIdx_E10_1_PAN;
11614         } else {
11615             idx = ARMMMUIdx_E10_1;
11616         }
11617         break;
11618     case 2:
11619         /* Note that TGE does not apply at EL2.  */
11620         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11621             if (arm_pan_enabled(env)) {
11622                 idx = ARMMMUIdx_E20_2_PAN;
11623             } else {
11624                 idx = ARMMMUIdx_E20_2;
11625             }
11626         } else {
11627             idx = ARMMMUIdx_E2;
11628         }
11629         break;
11630     case 3:
11631         return ARMMMUIdx_E3;
11632     default:
11633         g_assert_not_reached();
11634     }
11635 
11636     return idx;
11637 }
11638 
11639 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11640 {
11641     return arm_mmu_idx_el(env, arm_current_el(env));
11642 }
11643 
11644 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11645                                            ARMMMUIdx mmu_idx,
11646                                            CPUARMTBFlags flags)
11647 {
11648     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11649     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11650 
11651     if (arm_singlestep_active(env)) {
11652         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11653     }
11654     return flags;
11655 }
11656 
11657 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11658                                               ARMMMUIdx mmu_idx,
11659                                               CPUARMTBFlags flags)
11660 {
11661     bool sctlr_b = arm_sctlr_b(env);
11662 
11663     if (sctlr_b) {
11664         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11665     }
11666     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11667         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11668     }
11669     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11670 
11671     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11672 }
11673 
11674 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11675                                         ARMMMUIdx mmu_idx)
11676 {
11677     CPUARMTBFlags flags = {};
11678     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11679 
11680     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11681     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11682         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11683     }
11684 
11685     if (arm_v7m_is_handler_mode(env)) {
11686         DP_TBFLAG_M32(flags, HANDLER, 1);
11687     }
11688 
11689     /*
11690      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11691      * is suppressing them because the requested execution priority
11692      * is less than 0.
11693      */
11694     if (arm_feature(env, ARM_FEATURE_V8) &&
11695         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11696           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11697         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11698     }
11699 
11700     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11701         DP_TBFLAG_M32(flags, SECURE, 1);
11702     }
11703 
11704     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11705 }
11706 
11707 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11708                                         ARMMMUIdx mmu_idx)
11709 {
11710     CPUARMTBFlags flags = {};
11711     int el = arm_current_el(env);
11712 
11713     if (arm_sctlr(env, el) & SCTLR_A) {
11714         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11715     }
11716 
11717     if (arm_el_is_aa64(env, 1)) {
11718         DP_TBFLAG_A32(flags, VFPEN, 1);
11719     }
11720 
11721     if (el < 2 && env->cp15.hstr_el2 &&
11722         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11723         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11724     }
11725 
11726     if (env->uncached_cpsr & CPSR_IL) {
11727         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11728     }
11729 
11730     /*
11731      * The SME exception we are testing for is raised via
11732      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11733      * AArch32.CheckAdvSIMDOrFPEnabled().
11734      */
11735     if (el == 0
11736         && FIELD_EX64(env->svcr, SVCR, SM)
11737         && (!arm_is_el2_enabled(env)
11738             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11739         && arm_el_is_aa64(env, 1)
11740         && !sme_fa64(env, el)) {
11741         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11742     }
11743 
11744     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11745 }
11746 
11747 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11748                                         ARMMMUIdx mmu_idx)
11749 {
11750     CPUARMTBFlags flags = {};
11751     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11752     uint64_t tcr = regime_tcr(env, mmu_idx);
11753     uint64_t sctlr;
11754     int tbii, tbid;
11755 
11756     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11757 
11758     /* Get control bits for tagged addresses.  */
11759     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11760     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11761 
11762     DP_TBFLAG_A64(flags, TBII, tbii);
11763     DP_TBFLAG_A64(flags, TBID, tbid);
11764 
11765     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11766         int sve_el = sve_exception_el(env, el);
11767 
11768         /*
11769          * If either FP or SVE are disabled, translator does not need len.
11770          * If SVE EL > FP EL, FP exception has precedence, and translator
11771          * does not need SVE EL.  Save potential re-translations by forcing
11772          * the unneeded data to zero.
11773          */
11774         if (fp_el != 0) {
11775             if (sve_el > fp_el) {
11776                 sve_el = 0;
11777             }
11778         } else if (sve_el == 0) {
11779             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11780         }
11781         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11782     }
11783     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11784         int sme_el = sme_exception_el(env, el);
11785         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11786 
11787         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11788         if (sme_el == 0) {
11789             /* Similarly, do not compute SVL if SME is disabled. */
11790             int svl = sve_vqm1_for_el_sm(env, el, true);
11791             DP_TBFLAG_A64(flags, SVL, svl);
11792             if (sm) {
11793                 /* If SVE is disabled, we will not have set VL above. */
11794                 DP_TBFLAG_A64(flags, VL, svl);
11795             }
11796         }
11797         if (sm) {
11798             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11799             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11800         }
11801         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11802     }
11803 
11804     sctlr = regime_sctlr(env, stage1);
11805 
11806     if (sctlr & SCTLR_A) {
11807         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11808     }
11809 
11810     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11811         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11812     }
11813 
11814     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11815         /*
11816          * In order to save space in flags, we record only whether
11817          * pauth is "inactive", meaning all insns are implemented as
11818          * a nop, or "active" when some action must be performed.
11819          * The decision of which action to take is left to a helper.
11820          */
11821         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11822             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11823         }
11824     }
11825 
11826     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11827         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11828         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11829             DP_TBFLAG_A64(flags, BT, 1);
11830         }
11831     }
11832 
11833     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11834     if (!(env->pstate & PSTATE_UAO)) {
11835         switch (mmu_idx) {
11836         case ARMMMUIdx_E10_1:
11837         case ARMMMUIdx_E10_1_PAN:
11838             /* TODO: ARMv8.3-NV */
11839             DP_TBFLAG_A64(flags, UNPRIV, 1);
11840             break;
11841         case ARMMMUIdx_E20_2:
11842         case ARMMMUIdx_E20_2_PAN:
11843             /*
11844              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11845              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11846              */
11847             if (env->cp15.hcr_el2 & HCR_TGE) {
11848                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11849             }
11850             break;
11851         default:
11852             break;
11853         }
11854     }
11855 
11856     if (env->pstate & PSTATE_IL) {
11857         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11858     }
11859 
11860     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11861         /*
11862          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11863          * if all accesses must be Unchecked:
11864          * 1) If no TBI, then there are no tags in the address to check,
11865          * 2) If Tag Check Override, then all accesses are Unchecked,
11866          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11867          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11868          */
11869         if (allocation_tag_access_enabled(env, el, sctlr)) {
11870             DP_TBFLAG_A64(flags, ATA, 1);
11871             if (tbid
11872                 && !(env->pstate & PSTATE_TCO)
11873                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11874                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11875             }
11876         }
11877         /* And again for unprivileged accesses, if required.  */
11878         if (EX_TBFLAG_A64(flags, UNPRIV)
11879             && tbid
11880             && !(env->pstate & PSTATE_TCO)
11881             && (sctlr & SCTLR_TCF0)
11882             && allocation_tag_access_enabled(env, 0, sctlr)) {
11883             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11884         }
11885         /* Cache TCMA as well as TBI. */
11886         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11887     }
11888 
11889     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11890 }
11891 
11892 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11893 {
11894     int el = arm_current_el(env);
11895     int fp_el = fp_exception_el(env, el);
11896     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11897 
11898     if (is_a64(env)) {
11899         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11900     } else if (arm_feature(env, ARM_FEATURE_M)) {
11901         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11902     } else {
11903         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11904     }
11905 }
11906 
11907 void arm_rebuild_hflags(CPUARMState *env)
11908 {
11909     env->hflags = rebuild_hflags_internal(env);
11910 }
11911 
11912 /*
11913  * If we have triggered a EL state change we can't rely on the
11914  * translator having passed it to us, we need to recompute.
11915  */
11916 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11917 {
11918     int el = arm_current_el(env);
11919     int fp_el = fp_exception_el(env, el);
11920     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11921 
11922     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11923 }
11924 
11925 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11926 {
11927     int fp_el = fp_exception_el(env, el);
11928     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11929 
11930     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11931 }
11932 
11933 /*
11934  * If we have triggered a EL state change we can't rely on the
11935  * translator having passed it to us, we need to recompute.
11936  */
11937 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11938 {
11939     int el = arm_current_el(env);
11940     int fp_el = fp_exception_el(env, el);
11941     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11942     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11943 }
11944 
11945 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11946 {
11947     int fp_el = fp_exception_el(env, el);
11948     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11949 
11950     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11951 }
11952 
11953 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11954 {
11955     int fp_el = fp_exception_el(env, el);
11956     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11957 
11958     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11959 }
11960 
11961 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11962 {
11963 #ifdef CONFIG_DEBUG_TCG
11964     CPUARMTBFlags c = env->hflags;
11965     CPUARMTBFlags r = rebuild_hflags_internal(env);
11966 
11967     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11968         fprintf(stderr, "TCG hflags mismatch "
11969                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11970                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11971                 c.flags, c.flags2, r.flags, r.flags2);
11972         abort();
11973     }
11974 #endif
11975 }
11976 
11977 static bool mve_no_pred(CPUARMState *env)
11978 {
11979     /*
11980      * Return true if there is definitely no predication of MVE
11981      * instructions by VPR or LTPSIZE. (Returning false even if there
11982      * isn't any predication is OK; generated code will just be
11983      * a little worse.)
11984      * If the CPU does not implement MVE then this TB flag is always 0.
11985      *
11986      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11987      * logic in gen_update_fp_context() needs to be updated to match.
11988      *
11989      * We do not include the effect of the ECI bits here -- they are
11990      * tracked in other TB flags. This simplifies the logic for
11991      * "when did we emit code that changes the MVE_NO_PRED TB flag
11992      * and thus need to end the TB?".
11993      */
11994     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11995         return false;
11996     }
11997     if (env->v7m.vpr) {
11998         return false;
11999     }
12000     if (env->v7m.ltpsize < 4) {
12001         return false;
12002     }
12003     return true;
12004 }
12005 
12006 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12007                           target_ulong *cs_base, uint32_t *pflags)
12008 {
12009     CPUARMTBFlags flags;
12010 
12011     assert_hflags_rebuild_correctly(env);
12012     flags = env->hflags;
12013 
12014     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12015         *pc = env->pc;
12016         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12017             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12018         }
12019     } else {
12020         *pc = env->regs[15];
12021 
12022         if (arm_feature(env, ARM_FEATURE_M)) {
12023             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12024                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12025                 != env->v7m.secure) {
12026                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12027             }
12028 
12029             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12030                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12031                  (env->v7m.secure &&
12032                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12033                 /*
12034                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12035                  * active FP context; we must create a new FP context before
12036                  * executing any FP insn.
12037                  */
12038                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12039             }
12040 
12041             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12042             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12043                 DP_TBFLAG_M32(flags, LSPACT, 1);
12044             }
12045 
12046             if (mve_no_pred(env)) {
12047                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12048             }
12049         } else {
12050             /*
12051              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12052              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12053              */
12054             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12055                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12056             } else {
12057                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12058                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12059             }
12060             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12061                 DP_TBFLAG_A32(flags, VFPEN, 1);
12062             }
12063         }
12064 
12065         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12066         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12067     }
12068 
12069     /*
12070      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12071      * states defined in the ARM ARM for software singlestep:
12072      *  SS_ACTIVE   PSTATE.SS   State
12073      *     0            x       Inactive (the TB flag for SS is always 0)
12074      *     1            0       Active-pending
12075      *     1            1       Active-not-pending
12076      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12077      */
12078     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12079         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12080     }
12081 
12082     *pflags = flags.flags;
12083     *cs_base = flags.flags2;
12084 }
12085 
12086 #ifdef TARGET_AARCH64
12087 /*
12088  * The manual says that when SVE is enabled and VQ is widened the
12089  * implementation is allowed to zero the previously inaccessible
12090  * portion of the registers.  The corollary to that is that when
12091  * SVE is enabled and VQ is narrowed we are also allowed to zero
12092  * the now inaccessible portion of the registers.
12093  *
12094  * The intent of this is that no predicate bit beyond VQ is ever set.
12095  * Which means that some operations on predicate registers themselves
12096  * may operate on full uint64_t or even unrolled across the maximum
12097  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12098  * may well be cheaper than conditionals to restrict the operation
12099  * to the relevant portion of a uint16_t[16].
12100  */
12101 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12102 {
12103     int i, j;
12104     uint64_t pmask;
12105 
12106     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12107     assert(vq <= env_archcpu(env)->sve_max_vq);
12108 
12109     /* Zap the high bits of the zregs.  */
12110     for (i = 0; i < 32; i++) {
12111         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12112     }
12113 
12114     /* Zap the high bits of the pregs and ffr.  */
12115     pmask = 0;
12116     if (vq & 3) {
12117         pmask = ~(-1ULL << (16 * (vq & 3)));
12118     }
12119     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12120         for (i = 0; i < 17; ++i) {
12121             env->vfp.pregs[i].p[j] &= pmask;
12122         }
12123         pmask = 0;
12124     }
12125 }
12126 
12127 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12128 {
12129     int exc_el;
12130 
12131     if (sm) {
12132         exc_el = sme_exception_el(env, el);
12133     } else {
12134         exc_el = sve_exception_el(env, el);
12135     }
12136     if (exc_el) {
12137         return 0; /* disabled */
12138     }
12139     return sve_vqm1_for_el_sm(env, el, sm);
12140 }
12141 
12142 /*
12143  * Notice a change in SVE vector size when changing EL.
12144  */
12145 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12146                            int new_el, bool el0_a64)
12147 {
12148     ARMCPU *cpu = env_archcpu(env);
12149     int old_len, new_len;
12150     bool old_a64, new_a64, sm;
12151 
12152     /* Nothing to do if no SVE.  */
12153     if (!cpu_isar_feature(aa64_sve, cpu)) {
12154         return;
12155     }
12156 
12157     /* Nothing to do if FP is disabled in either EL.  */
12158     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12159         return;
12160     }
12161 
12162     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12163     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12164 
12165     /*
12166      * Both AArch64.TakeException and AArch64.ExceptionReturn
12167      * invoke ResetSVEState when taking an exception from, or
12168      * returning to, AArch32 state when PSTATE.SM is enabled.
12169      */
12170     sm = FIELD_EX64(env->svcr, SVCR, SM);
12171     if (old_a64 != new_a64 && sm) {
12172         arm_reset_sve_state(env);
12173         return;
12174     }
12175 
12176     /*
12177      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12178      * at ELx, or not available because the EL is in AArch32 state, then
12179      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12180      * has an effective value of 0".
12181      *
12182      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12183      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12184      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12185      * we already have the correct register contents when encountering the
12186      * vq0->vq0 transition between EL0->EL1.
12187      */
12188     old_len = new_len = 0;
12189     if (old_a64) {
12190         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12191     }
12192     if (new_a64) {
12193         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12194     }
12195 
12196     /* When changing vector length, clear inaccessible state.  */
12197     if (new_len < old_len) {
12198         aarch64_sve_narrow_vq(env, new_len + 1);
12199     }
12200 }
12201 #endif
12202