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