xref: /openbmc/qemu/target/arm/helper.c (revision a95570e3)
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/units.h"
11 #include "qemu/log.h"
12 #include "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/irq.h"
25 #include "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
33 #ifdef CONFIG_TCG
34 #include "arm_ldst.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
37 #endif
38 #include "cpregs.h"
39 
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41 
42 static void switch_mode(CPUARMState *env, int mode);
43 
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
45 {
46     assert(ri->fieldoffset);
47     if (cpreg_field_is_64bit(ri)) {
48         return CPREG_FIELD64(env, ri);
49     } else {
50         return CPREG_FIELD32(env, ri);
51     }
52 }
53 
54 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
55 {
56     assert(ri->fieldoffset);
57     if (cpreg_field_is_64bit(ri)) {
58         CPREG_FIELD64(env, ri) = value;
59     } else {
60         CPREG_FIELD32(env, ri) = value;
61     }
62 }
63 
64 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66     return (char *)env + ri->fieldoffset;
67 }
68 
69 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
70 {
71     /* Raw read of a coprocessor register (as needed for migration, etc). */
72     if (ri->type & ARM_CP_CONST) {
73         return ri->resetvalue;
74     } else if (ri->raw_readfn) {
75         return ri->raw_readfn(env, ri);
76     } else if (ri->readfn) {
77         return ri->readfn(env, ri);
78     } else {
79         return raw_read(env, ri);
80     }
81 }
82 
83 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
84                              uint64_t v)
85 {
86     /* Raw write of a coprocessor register (as needed for migration, etc).
87      * Note that constant registers are treated as write-ignored; the
88      * caller should check for success by whether a readback gives the
89      * value written.
90      */
91     if (ri->type & ARM_CP_CONST) {
92         return;
93     } else if (ri->raw_writefn) {
94         ri->raw_writefn(env, ri, v);
95     } else if (ri->writefn) {
96         ri->writefn(env, ri, v);
97     } else {
98         raw_write(env, ri, v);
99     }
100 }
101 
102 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
103 {
104    /* Return true if the regdef would cause an assertion if you called
105     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
106     * program bug for it not to have the NO_RAW flag).
107     * NB that returning false here doesn't necessarily mean that calling
108     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
109     * read/write access functions which are safe for raw use" from "has
110     * read/write access functions which have side effects but has forgotten
111     * to provide raw access functions".
112     * The tests here line up with the conditions in read/write_raw_cp_reg()
113     * and assertions in raw_read()/raw_write().
114     */
115     if ((ri->type & ARM_CP_CONST) ||
116         ri->fieldoffset ||
117         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
118         return false;
119     }
120     return true;
121 }
122 
123 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
124 {
125     /* Write the coprocessor state from cpu->env to the (index,value) list. */
126     int i;
127     bool ok = true;
128 
129     for (i = 0; i < cpu->cpreg_array_len; i++) {
130         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
131         const ARMCPRegInfo *ri;
132         uint64_t newval;
133 
134         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
135         if (!ri) {
136             ok = false;
137             continue;
138         }
139         if (ri->type & ARM_CP_NO_RAW) {
140             continue;
141         }
142 
143         newval = read_raw_cp_reg(&cpu->env, ri);
144         if (kvm_sync) {
145             /*
146              * Only sync if the previous list->cpustate sync succeeded.
147              * Rather than tracking the success/failure state for every
148              * item in the list, we just recheck "does the raw write we must
149              * have made in write_list_to_cpustate() read back OK" here.
150              */
151             uint64_t oldval = cpu->cpreg_values[i];
152 
153             if (oldval == newval) {
154                 continue;
155             }
156 
157             write_raw_cp_reg(&cpu->env, ri, oldval);
158             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
159                 continue;
160             }
161 
162             write_raw_cp_reg(&cpu->env, ri, newval);
163         }
164         cpu->cpreg_values[i] = newval;
165     }
166     return ok;
167 }
168 
169 bool write_list_to_cpustate(ARMCPU *cpu)
170 {
171     int i;
172     bool ok = true;
173 
174     for (i = 0; i < cpu->cpreg_array_len; i++) {
175         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176         uint64_t v = cpu->cpreg_values[i];
177         const ARMCPRegInfo *ri;
178 
179         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
180         if (!ri) {
181             ok = false;
182             continue;
183         }
184         if (ri->type & ARM_CP_NO_RAW) {
185             continue;
186         }
187         /* Write value and confirm it reads back as written
188          * (to catch read-only registers and partially read-only
189          * registers where the incoming migration value doesn't match)
190          */
191         write_raw_cp_reg(&cpu->env, ri, v);
192         if (read_raw_cp_reg(&cpu->env, ri) != v) {
193             ok = false;
194         }
195     }
196     return ok;
197 }
198 
199 static void add_cpreg_to_list(gpointer key, gpointer opaque)
200 {
201     ARMCPU *cpu = opaque;
202     uint32_t regidx = (uintptr_t)key;
203     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
204 
205     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
206         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207         /* The value array need not be initialized at this point */
208         cpu->cpreg_array_len++;
209     }
210 }
211 
212 static void count_cpreg(gpointer key, gpointer opaque)
213 {
214     ARMCPU *cpu = opaque;
215     const ARMCPRegInfo *ri;
216 
217     ri = g_hash_table_lookup(cpu->cp_regs, key);
218 
219     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
220         cpu->cpreg_array_len++;
221     }
222 }
223 
224 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
225 {
226     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
228 
229     if (aidx > bidx) {
230         return 1;
231     }
232     if (aidx < bidx) {
233         return -1;
234     }
235     return 0;
236 }
237 
238 void init_cpreg_list(ARMCPU *cpu)
239 {
240     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
241      * Note that we require cpreg_tuples[] to be sorted by key ID.
242      */
243     GList *keys;
244     int arraylen;
245 
246     keys = g_hash_table_get_keys(cpu->cp_regs);
247     keys = g_list_sort(keys, cpreg_key_compare);
248 
249     cpu->cpreg_array_len = 0;
250 
251     g_list_foreach(keys, count_cpreg, cpu);
252 
253     arraylen = cpu->cpreg_array_len;
254     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259     cpu->cpreg_array_len = 0;
260 
261     g_list_foreach(keys, add_cpreg_to_list, cpu);
262 
263     assert(cpu->cpreg_array_len == arraylen);
264 
265     g_list_free(keys);
266 }
267 
268 /*
269  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
270  */
271 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
272                                         const ARMCPRegInfo *ri,
273                                         bool isread)
274 {
275     if (!is_a64(env) && arm_current_el(env) == 3 &&
276         arm_is_secure_below_el3(env)) {
277         return CP_ACCESS_TRAP_UNCATEGORIZED;
278     }
279     return CP_ACCESS_OK;
280 }
281 
282 /* Some secure-only AArch32 registers trap to EL3 if used from
283  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
284  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
285  * We assume that the .access field is set to PL1_RW.
286  */
287 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
288                                             const ARMCPRegInfo *ri,
289                                             bool isread)
290 {
291     if (arm_current_el(env) == 3) {
292         return CP_ACCESS_OK;
293     }
294     if (arm_is_secure_below_el3(env)) {
295         if (env->cp15.scr_el3 & SCR_EEL2) {
296             return CP_ACCESS_TRAP_EL2;
297         }
298         return CP_ACCESS_TRAP_EL3;
299     }
300     /* This will be EL1 NS and EL2 NS, which just UNDEF */
301     return CP_ACCESS_TRAP_UNCATEGORIZED;
302 }
303 
304 /* Check for traps to performance monitor registers, which are controlled
305  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306  */
307 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308                                  bool isread)
309 {
310     int el = arm_current_el(env);
311     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
312 
313     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
314         return CP_ACCESS_TRAP_EL2;
315     }
316     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317         return CP_ACCESS_TRAP_EL3;
318     }
319     return CP_ACCESS_OK;
320 }
321 
322 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
323 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324                                       bool isread)
325 {
326     if (arm_current_el(env) == 1) {
327         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328         if (arm_hcr_el2_eff(env) & trap) {
329             return CP_ACCESS_TRAP_EL2;
330         }
331     }
332     return CP_ACCESS_OK;
333 }
334 
335 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
336 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337                                  bool isread)
338 {
339     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340         return CP_ACCESS_TRAP_EL2;
341     }
342     return CP_ACCESS_OK;
343 }
344 
345 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
346 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347                                   bool isread)
348 {
349     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350         return CP_ACCESS_TRAP_EL2;
351     }
352     return CP_ACCESS_OK;
353 }
354 
355 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
356 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357                                   bool isread)
358 {
359     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360         return CP_ACCESS_TRAP_EL2;
361     }
362     return CP_ACCESS_OK;
363 }
364 
365 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
366 {
367     ARMCPU *cpu = env_archcpu(env);
368 
369     raw_write(env, ri, value);
370     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
371 }
372 
373 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
374 {
375     ARMCPU *cpu = env_archcpu(env);
376 
377     if (raw_read(env, ri) != value) {
378         /* Unlike real hardware the qemu TLB uses virtual addresses,
379          * not modified virtual addresses, so this causes a TLB flush.
380          */
381         tlb_flush(CPU(cpu));
382         raw_write(env, ri, value);
383     }
384 }
385 
386 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
387                              uint64_t value)
388 {
389     ARMCPU *cpu = env_archcpu(env);
390 
391     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
392         && !extended_addresses_enabled(env)) {
393         /* For VMSA (when not using the LPAE long descriptor page table
394          * format) this register includes the ASID, so do a TLB flush.
395          * For PMSA it is purely a process ID and no action is needed.
396          */
397         tlb_flush(CPU(cpu));
398     }
399     raw_write(env, ri, value);
400 }
401 
402 /* IS variants of TLB operations must affect all cores */
403 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
404                              uint64_t value)
405 {
406     CPUState *cs = env_cpu(env);
407 
408     tlb_flush_all_cpus_synced(cs);
409 }
410 
411 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                              uint64_t value)
413 {
414     CPUState *cs = env_cpu(env);
415 
416     tlb_flush_all_cpus_synced(cs);
417 }
418 
419 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420                              uint64_t value)
421 {
422     CPUState *cs = env_cpu(env);
423 
424     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
425 }
426 
427 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
428                              uint64_t value)
429 {
430     CPUState *cs = env_cpu(env);
431 
432     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
433 }
434 
435 /*
436  * Non-IS variants of TLB operations are upgraded to
437  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
438  * force broadcast of these operations.
439  */
440 static bool tlb_force_broadcast(CPUARMState *env)
441 {
442     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
443 }
444 
445 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
446                           uint64_t value)
447 {
448     /* Invalidate all (TLBIALL) */
449     CPUState *cs = env_cpu(env);
450 
451     if (tlb_force_broadcast(env)) {
452         tlb_flush_all_cpus_synced(cs);
453     } else {
454         tlb_flush(cs);
455     }
456 }
457 
458 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
459                           uint64_t value)
460 {
461     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
462     CPUState *cs = env_cpu(env);
463 
464     value &= TARGET_PAGE_MASK;
465     if (tlb_force_broadcast(env)) {
466         tlb_flush_page_all_cpus_synced(cs, value);
467     } else {
468         tlb_flush_page(cs, value);
469     }
470 }
471 
472 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
473                            uint64_t value)
474 {
475     /* Invalidate by ASID (TLBIASID) */
476     CPUState *cs = env_cpu(env);
477 
478     if (tlb_force_broadcast(env)) {
479         tlb_flush_all_cpus_synced(cs);
480     } else {
481         tlb_flush(cs);
482     }
483 }
484 
485 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                            uint64_t value)
487 {
488     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
489     CPUState *cs = env_cpu(env);
490 
491     value &= TARGET_PAGE_MASK;
492     if (tlb_force_broadcast(env)) {
493         tlb_flush_page_all_cpus_synced(cs, value);
494     } else {
495         tlb_flush_page(cs, value);
496     }
497 }
498 
499 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                                uint64_t value)
501 {
502     CPUState *cs = env_cpu(env);
503 
504     tlb_flush_by_mmuidx(cs,
505                         ARMMMUIdxBit_E10_1 |
506                         ARMMMUIdxBit_E10_1_PAN |
507                         ARMMMUIdxBit_E10_0);
508 }
509 
510 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
511                                   uint64_t value)
512 {
513     CPUState *cs = env_cpu(env);
514 
515     tlb_flush_by_mmuidx_all_cpus_synced(cs,
516                                         ARMMMUIdxBit_E10_1 |
517                                         ARMMMUIdxBit_E10_1_PAN |
518                                         ARMMMUIdxBit_E10_0);
519 }
520 
521 
522 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
523                               uint64_t value)
524 {
525     CPUState *cs = env_cpu(env);
526 
527     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
528 }
529 
530 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
531                                  uint64_t value)
532 {
533     CPUState *cs = env_cpu(env);
534 
535     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
536 }
537 
538 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                               uint64_t value)
540 {
541     CPUState *cs = env_cpu(env);
542     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
543 
544     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
545 }
546 
547 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548                                  uint64_t value)
549 {
550     CPUState *cs = env_cpu(env);
551     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
552 
553     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
554                                              ARMMMUIdxBit_E2);
555 }
556 
557 static const ARMCPRegInfo cp_reginfo[] = {
558     /* Define the secure and non-secure FCSE identifier CP registers
559      * separately because there is no secure bank in V8 (no _EL3).  This allows
560      * the secure register to be properly reset and migrated. There is also no
561      * v8 EL1 version of the register so the non-secure instance stands alone.
562      */
563     { .name = "FCSEIDR",
564       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
565       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
566       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
567       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
568     { .name = "FCSEIDR_S",
569       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
570       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
571       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
572       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
573     /* Define the secure and non-secure context identifier CP registers
574      * separately because there is no secure bank in V8 (no _EL3).  This allows
575      * the secure register to be properly reset and migrated.  In the
576      * non-secure case, the 32-bit register will have reset and migration
577      * disabled during registration as it is handled by the 64-bit instance.
578      */
579     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
580       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
581       .access = PL1_RW, .accessfn = access_tvm_trvm,
582       .secure = ARM_CP_SECSTATE_NS,
583       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
584       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
585     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
586       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
587       .access = PL1_RW, .accessfn = access_tvm_trvm,
588       .secure = ARM_CP_SECSTATE_S,
589       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
590       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
591 };
592 
593 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
594     /* NB: Some of these registers exist in v8 but with more precise
595      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
596      */
597     /* MMU Domain access control / MPU write buffer control */
598     { .name = "DACR",
599       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
600       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
601       .writefn = dacr_write, .raw_writefn = raw_write,
602       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
603                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
604     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
605      * For v6 and v5, these mappings are overly broad.
606      */
607     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
608       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
610       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
612       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
614       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
615     /* Cache maintenance ops; some of this space may be overridden later. */
616     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
617       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
618       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
619 };
620 
621 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
622     /* Not all pre-v6 cores implemented this WFI, so this is slightly
623      * over-broad.
624      */
625     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
626       .access = PL1_W, .type = ARM_CP_WFI },
627 };
628 
629 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631      * is UNPREDICTABLE; we choose to NOP as most implementations do).
632      */
633     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634       .access = PL1_W, .type = ARM_CP_WFI },
635     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637      * OMAPCP will override this space.
638      */
639     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
641       .resetvalue = 0 },
642     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
644       .resetvalue = 0 },
645     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
647       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
648       .resetvalue = 0 },
649     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650      * implementing it as RAZ means the "debug architecture version" bits
651      * will read as a reserved value, which should cause Linux to not try
652      * to use the debug hardware.
653      */
654     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
656     /* MMU TLB control. Note that the wildcarding means we cover not just
657      * the unified TLB ops but also the dside/iside/inner-shareable variants.
658      */
659     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
661       .type = ARM_CP_NO_RAW },
662     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
664       .type = ARM_CP_NO_RAW },
665     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
667       .type = ARM_CP_NO_RAW },
668     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
670       .type = ARM_CP_NO_RAW },
671     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
675 };
676 
677 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
678                         uint64_t value)
679 {
680     uint32_t mask = 0;
681 
682     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
683     if (!arm_feature(env, ARM_FEATURE_V8)) {
684         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
685          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
686          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
687          */
688         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
689             /* VFP coprocessor: cp10 & cp11 [23:20] */
690             mask |= R_CPACR_ASEDIS_MASK |
691                     R_CPACR_D32DIS_MASK |
692                     R_CPACR_CP11_MASK |
693                     R_CPACR_CP10_MASK;
694 
695             if (!arm_feature(env, ARM_FEATURE_NEON)) {
696                 /* ASEDIS [31] bit is RAO/WI */
697                 value |= R_CPACR_ASEDIS_MASK;
698             }
699 
700             /* VFPv3 and upwards with NEON implement 32 double precision
701              * registers (D0-D31).
702              */
703             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
704                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
705                 value |= R_CPACR_D32DIS_MASK;
706             }
707         }
708         value &= mask;
709     }
710 
711     /*
712      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
713      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
714      */
715     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
716         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
717         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
718         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
719     }
720 
721     env->cp15.cpacr_el1 = value;
722 }
723 
724 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
725 {
726     /*
727      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
728      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
729      */
730     uint64_t value = env->cp15.cpacr_el1;
731 
732     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
733         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
734         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
735     }
736     return value;
737 }
738 
739 
740 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
741 {
742     /* Call cpacr_write() so that we reset with the correct RAO bits set
743      * for our CPU features.
744      */
745     cpacr_write(env, ri, 0);
746 }
747 
748 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749                                    bool isread)
750 {
751     if (arm_feature(env, ARM_FEATURE_V8)) {
752         /* Check if CPACR accesses are to be trapped to EL2 */
753         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
754             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
755             return CP_ACCESS_TRAP_EL2;
756         /* Check if CPACR accesses are to be trapped to EL3 */
757         } else if (arm_current_el(env) < 3 &&
758                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
759             return CP_ACCESS_TRAP_EL3;
760         }
761     }
762 
763     return CP_ACCESS_OK;
764 }
765 
766 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
767                                   bool isread)
768 {
769     /* Check if CPTR accesses are set to trap to EL3 */
770     if (arm_current_el(env) == 2 &&
771         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
772         return CP_ACCESS_TRAP_EL3;
773     }
774 
775     return CP_ACCESS_OK;
776 }
777 
778 static const ARMCPRegInfo v6_cp_reginfo[] = {
779     /* prefetch by MVA in v6, NOP in v7 */
780     { .name = "MVA_prefetch",
781       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
782       .access = PL1_W, .type = ARM_CP_NOP },
783     /* We need to break the TB after ISB to execute self-modifying code
784      * correctly and also to take any pending interrupts immediately.
785      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
786      */
787     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
788       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
789     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
790       .access = PL0_W, .type = ARM_CP_NOP },
791     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
792       .access = PL0_W, .type = ARM_CP_NOP },
793     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
794       .access = PL1_RW, .accessfn = access_tvm_trvm,
795       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
796                              offsetof(CPUARMState, cp15.ifar_ns) },
797       .resetvalue = 0, },
798     /* Watchpoint Fault Address Register : should actually only be present
799      * for 1136, 1176, 11MPCore.
800      */
801     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
802       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
803     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
804       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
805       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
806       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
807 };
808 
809 typedef struct pm_event {
810     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
811     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
812     bool (*supported)(CPUARMState *);
813     /*
814      * Retrieve the current count of the underlying event. The programmed
815      * counters hold a difference from the return value from this function
816      */
817     uint64_t (*get_count)(CPUARMState *);
818     /*
819      * Return how many nanoseconds it will take (at a minimum) for count events
820      * to occur. A negative value indicates the counter will never overflow, or
821      * that the counter has otherwise arranged for the overflow bit to be set
822      * and the PMU interrupt to be raised on overflow.
823      */
824     int64_t (*ns_per_count)(uint64_t);
825 } pm_event;
826 
827 static bool event_always_supported(CPUARMState *env)
828 {
829     return true;
830 }
831 
832 static uint64_t swinc_get_count(CPUARMState *env)
833 {
834     /*
835      * SW_INCR events are written directly to the pmevcntr's by writes to
836      * PMSWINC, so there is no underlying count maintained by the PMU itself
837      */
838     return 0;
839 }
840 
841 static int64_t swinc_ns_per(uint64_t ignored)
842 {
843     return -1;
844 }
845 
846 /*
847  * Return the underlying cycle count for the PMU cycle counters. If we're in
848  * usermode, simply return 0.
849  */
850 static uint64_t cycles_get_count(CPUARMState *env)
851 {
852 #ifndef CONFIG_USER_ONLY
853     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
854                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
855 #else
856     return cpu_get_host_ticks();
857 #endif
858 }
859 
860 #ifndef CONFIG_USER_ONLY
861 static int64_t cycles_ns_per(uint64_t cycles)
862 {
863     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
864 }
865 
866 static bool instructions_supported(CPUARMState *env)
867 {
868     return icount_enabled() == 1; /* Precise instruction counting */
869 }
870 
871 static uint64_t instructions_get_count(CPUARMState *env)
872 {
873     return (uint64_t)icount_get_raw();
874 }
875 
876 static int64_t instructions_ns_per(uint64_t icount)
877 {
878     return icount_to_ns((int64_t)icount);
879 }
880 #endif
881 
882 static bool pmuv3p1_events_supported(CPUARMState *env)
883 {
884     /* For events which are supported in any v8.1 PMU */
885     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
886 }
887 
888 static bool pmuv3p4_events_supported(CPUARMState *env)
889 {
890     /* For events which are supported in any v8.1 PMU */
891     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
892 }
893 
894 static uint64_t zero_event_get_count(CPUARMState *env)
895 {
896     /* For events which on QEMU never fire, so their count is always zero */
897     return 0;
898 }
899 
900 static int64_t zero_event_ns_per(uint64_t cycles)
901 {
902     /* An event which never fires can never overflow */
903     return -1;
904 }
905 
906 static const pm_event pm_events[] = {
907     { .number = 0x000, /* SW_INCR */
908       .supported = event_always_supported,
909       .get_count = swinc_get_count,
910       .ns_per_count = swinc_ns_per,
911     },
912 #ifndef CONFIG_USER_ONLY
913     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
914       .supported = instructions_supported,
915       .get_count = instructions_get_count,
916       .ns_per_count = instructions_ns_per,
917     },
918     { .number = 0x011, /* CPU_CYCLES, Cycle */
919       .supported = event_always_supported,
920       .get_count = cycles_get_count,
921       .ns_per_count = cycles_ns_per,
922     },
923 #endif
924     { .number = 0x023, /* STALL_FRONTEND */
925       .supported = pmuv3p1_events_supported,
926       .get_count = zero_event_get_count,
927       .ns_per_count = zero_event_ns_per,
928     },
929     { .number = 0x024, /* STALL_BACKEND */
930       .supported = pmuv3p1_events_supported,
931       .get_count = zero_event_get_count,
932       .ns_per_count = zero_event_ns_per,
933     },
934     { .number = 0x03c, /* STALL */
935       .supported = pmuv3p4_events_supported,
936       .get_count = zero_event_get_count,
937       .ns_per_count = zero_event_ns_per,
938     },
939 };
940 
941 /*
942  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
943  * events (i.e. the statistical profiling extension), this implementation
944  * should first be updated to something sparse instead of the current
945  * supported_event_map[] array.
946  */
947 #define MAX_EVENT_ID 0x3c
948 #define UNSUPPORTED_EVENT UINT16_MAX
949 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
950 
951 /*
952  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
953  * of ARM event numbers to indices in our pm_events array.
954  *
955  * Note: Events in the 0x40XX range are not currently supported.
956  */
957 void pmu_init(ARMCPU *cpu)
958 {
959     unsigned int i;
960 
961     /*
962      * Empty supported_event_map and cpu->pmceid[01] before adding supported
963      * events to them
964      */
965     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
966         supported_event_map[i] = UNSUPPORTED_EVENT;
967     }
968     cpu->pmceid0 = 0;
969     cpu->pmceid1 = 0;
970 
971     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
972         const pm_event *cnt = &pm_events[i];
973         assert(cnt->number <= MAX_EVENT_ID);
974         /* We do not currently support events in the 0x40xx range */
975         assert(cnt->number <= 0x3f);
976 
977         if (cnt->supported(&cpu->env)) {
978             supported_event_map[cnt->number] = i;
979             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
980             if (cnt->number & 0x20) {
981                 cpu->pmceid1 |= event_mask;
982             } else {
983                 cpu->pmceid0 |= event_mask;
984             }
985         }
986     }
987 }
988 
989 /*
990  * Check at runtime whether a PMU event is supported for the current machine
991  */
992 static bool event_supported(uint16_t number)
993 {
994     if (number > MAX_EVENT_ID) {
995         return false;
996     }
997     return supported_event_map[number] != UNSUPPORTED_EVENT;
998 }
999 
1000 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1001                                    bool isread)
1002 {
1003     /* Performance monitor registers user accessibility is controlled
1004      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1005      * trapping to EL2 or EL3 for other accesses.
1006      */
1007     int el = arm_current_el(env);
1008     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1009 
1010     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1011         return CP_ACCESS_TRAP;
1012     }
1013     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1014         return CP_ACCESS_TRAP_EL2;
1015     }
1016     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1017         return CP_ACCESS_TRAP_EL3;
1018     }
1019 
1020     return CP_ACCESS_OK;
1021 }
1022 
1023 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1024                                            const ARMCPRegInfo *ri,
1025                                            bool isread)
1026 {
1027     /* ER: event counter read trap control */
1028     if (arm_feature(env, ARM_FEATURE_V8)
1029         && arm_current_el(env) == 0
1030         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1031         && isread) {
1032         return CP_ACCESS_OK;
1033     }
1034 
1035     return pmreg_access(env, ri, isread);
1036 }
1037 
1038 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1039                                          const ARMCPRegInfo *ri,
1040                                          bool isread)
1041 {
1042     /* SW: software increment write trap control */
1043     if (arm_feature(env, ARM_FEATURE_V8)
1044         && arm_current_el(env) == 0
1045         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1046         && !isread) {
1047         return CP_ACCESS_OK;
1048     }
1049 
1050     return pmreg_access(env, ri, isread);
1051 }
1052 
1053 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1054                                         const ARMCPRegInfo *ri,
1055                                         bool isread)
1056 {
1057     /* ER: event counter read trap control */
1058     if (arm_feature(env, ARM_FEATURE_V8)
1059         && arm_current_el(env) == 0
1060         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1061         return CP_ACCESS_OK;
1062     }
1063 
1064     return pmreg_access(env, ri, isread);
1065 }
1066 
1067 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1068                                          const ARMCPRegInfo *ri,
1069                                          bool isread)
1070 {
1071     /* CR: cycle counter read trap control */
1072     if (arm_feature(env, ARM_FEATURE_V8)
1073         && arm_current_el(env) == 0
1074         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1075         && isread) {
1076         return CP_ACCESS_OK;
1077     }
1078 
1079     return pmreg_access(env, ri, isread);
1080 }
1081 
1082 /*
1083  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1084  * We use these to decide whether we need to wrap a write to MDCR_EL2
1085  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1086  */
1087 #define MDCR_EL2_PMU_ENABLE_BITS \
1088     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1089 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1090 
1091 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1092  * the current EL, security state, and register configuration.
1093  */
1094 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1095 {
1096     uint64_t filter;
1097     bool e, p, u, nsk, nsu, nsh, m;
1098     bool enabled, prohibited = false, filtered;
1099     bool secure = arm_is_secure(env);
1100     int el = arm_current_el(env);
1101     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1102     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1103 
1104     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1105         return false;
1106     }
1107 
1108     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1109             (counter < hpmn || counter == 31)) {
1110         e = env->cp15.c9_pmcr & PMCRE;
1111     } else {
1112         e = mdcr_el2 & MDCR_HPME;
1113     }
1114     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1115 
1116     /* Is event counting prohibited? */
1117     if (el == 2 && (counter < hpmn || counter == 31)) {
1118         prohibited = mdcr_el2 & MDCR_HPMD;
1119     }
1120     if (secure) {
1121         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1122     }
1123 
1124     if (counter == 31) {
1125         /*
1126          * The cycle counter defaults to running. PMCR.DP says "disable
1127          * the cycle counter when event counting is prohibited".
1128          * Some MDCR bits disable the cycle counter specifically.
1129          */
1130         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1131         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1132             if (secure) {
1133                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1134             }
1135             if (el == 2) {
1136                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1137             }
1138         }
1139     }
1140 
1141     if (counter == 31) {
1142         filter = env->cp15.pmccfiltr_el0;
1143     } else {
1144         filter = env->cp15.c14_pmevtyper[counter];
1145     }
1146 
1147     p   = filter & PMXEVTYPER_P;
1148     u   = filter & PMXEVTYPER_U;
1149     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1150     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1151     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1152     m   = arm_el_is_aa64(env, 1) &&
1153               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1154 
1155     if (el == 0) {
1156         filtered = secure ? u : u != nsu;
1157     } else if (el == 1) {
1158         filtered = secure ? p : p != nsk;
1159     } else if (el == 2) {
1160         filtered = !nsh;
1161     } else { /* EL3 */
1162         filtered = m != p;
1163     }
1164 
1165     if (counter != 31) {
1166         /*
1167          * If not checking PMCCNTR, ensure the counter is setup to an event we
1168          * support
1169          */
1170         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1171         if (!event_supported(event)) {
1172             return false;
1173         }
1174     }
1175 
1176     return enabled && !prohibited && !filtered;
1177 }
1178 
1179 static void pmu_update_irq(CPUARMState *env)
1180 {
1181     ARMCPU *cpu = env_archcpu(env);
1182     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1183             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1184 }
1185 
1186 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1187 {
1188     /*
1189      * Return true if the clock divider is enabled and the cycle counter
1190      * is supposed to tick only once every 64 clock cycles. This is
1191      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1192      * (64-bit) cycle counter PMCR.D has no effect.
1193      */
1194     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1195 }
1196 
1197 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1198 {
1199     /* Return true if the specified event counter is configured to be 64 bit */
1200 
1201     /* This isn't intended to be used with the cycle counter */
1202     assert(counter < 31);
1203 
1204     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1205         return false;
1206     }
1207 
1208     if (arm_feature(env, ARM_FEATURE_EL2)) {
1209         /*
1210          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1211          * current security state, so we don't use arm_mdcr_el2_eff() here.
1212          */
1213         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1214         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1215 
1216         if (hpmn != 0 && counter >= hpmn) {
1217             return hlp;
1218         }
1219     }
1220     return env->cp15.c9_pmcr & PMCRLP;
1221 }
1222 
1223 /*
1224  * Ensure c15_ccnt is the guest-visible count so that operations such as
1225  * enabling/disabling the counter or filtering, modifying the count itself,
1226  * etc. can be done logically. This is essentially a no-op if the counter is
1227  * not enabled at the time of the call.
1228  */
1229 static void pmccntr_op_start(CPUARMState *env)
1230 {
1231     uint64_t cycles = cycles_get_count(env);
1232 
1233     if (pmu_counter_enabled(env, 31)) {
1234         uint64_t eff_cycles = cycles;
1235         if (pmccntr_clockdiv_enabled(env)) {
1236             eff_cycles /= 64;
1237         }
1238 
1239         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1240 
1241         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1242                                  1ull << 63 : 1ull << 31;
1243         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1244             env->cp15.c9_pmovsr |= (1ULL << 31);
1245             pmu_update_irq(env);
1246         }
1247 
1248         env->cp15.c15_ccnt = new_pmccntr;
1249     }
1250     env->cp15.c15_ccnt_delta = cycles;
1251 }
1252 
1253 /*
1254  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1255  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1256  * pmccntr_op_start.
1257  */
1258 static void pmccntr_op_finish(CPUARMState *env)
1259 {
1260     if (pmu_counter_enabled(env, 31)) {
1261 #ifndef CONFIG_USER_ONLY
1262         /* Calculate when the counter will next overflow */
1263         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1264         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1265             remaining_cycles = (uint32_t)remaining_cycles;
1266         }
1267         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1268 
1269         if (overflow_in > 0) {
1270             int64_t overflow_at;
1271 
1272             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1273                                  overflow_in, &overflow_at)) {
1274                 ARMCPU *cpu = env_archcpu(env);
1275                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1276             }
1277         }
1278 #endif
1279 
1280         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1281         if (pmccntr_clockdiv_enabled(env)) {
1282             prev_cycles /= 64;
1283         }
1284         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1285     }
1286 }
1287 
1288 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1289 {
1290 
1291     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1292     uint64_t count = 0;
1293     if (event_supported(event)) {
1294         uint16_t event_idx = supported_event_map[event];
1295         count = pm_events[event_idx].get_count(env);
1296     }
1297 
1298     if (pmu_counter_enabled(env, counter)) {
1299         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1300         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1301             1ULL << 63 : 1ULL << 31;
1302 
1303         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1304             env->cp15.c9_pmovsr |= (1 << counter);
1305             pmu_update_irq(env);
1306         }
1307         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1308     }
1309     env->cp15.c14_pmevcntr_delta[counter] = count;
1310 }
1311 
1312 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1313 {
1314     if (pmu_counter_enabled(env, counter)) {
1315 #ifndef CONFIG_USER_ONLY
1316         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1317         uint16_t event_idx = supported_event_map[event];
1318         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1319         int64_t overflow_in;
1320 
1321         if (!pmevcntr_is_64_bit(env, counter)) {
1322             delta = (uint32_t)delta;
1323         }
1324         overflow_in = pm_events[event_idx].ns_per_count(delta);
1325 
1326         if (overflow_in > 0) {
1327             int64_t overflow_at;
1328 
1329             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1330                                  overflow_in, &overflow_at)) {
1331                 ARMCPU *cpu = env_archcpu(env);
1332                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1333             }
1334         }
1335 #endif
1336 
1337         env->cp15.c14_pmevcntr_delta[counter] -=
1338             env->cp15.c14_pmevcntr[counter];
1339     }
1340 }
1341 
1342 void pmu_op_start(CPUARMState *env)
1343 {
1344     unsigned int i;
1345     pmccntr_op_start(env);
1346     for (i = 0; i < pmu_num_counters(env); i++) {
1347         pmevcntr_op_start(env, i);
1348     }
1349 }
1350 
1351 void pmu_op_finish(CPUARMState *env)
1352 {
1353     unsigned int i;
1354     pmccntr_op_finish(env);
1355     for (i = 0; i < pmu_num_counters(env); i++) {
1356         pmevcntr_op_finish(env, i);
1357     }
1358 }
1359 
1360 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1361 {
1362     pmu_op_start(&cpu->env);
1363 }
1364 
1365 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1366 {
1367     pmu_op_finish(&cpu->env);
1368 }
1369 
1370 void arm_pmu_timer_cb(void *opaque)
1371 {
1372     ARMCPU *cpu = opaque;
1373 
1374     /*
1375      * Update all the counter values based on the current underlying counts,
1376      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378      * counter may expire.
1379      */
1380     pmu_op_start(&cpu->env);
1381     pmu_op_finish(&cpu->env);
1382 }
1383 
1384 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385                        uint64_t value)
1386 {
1387     pmu_op_start(env);
1388 
1389     if (value & PMCRC) {
1390         /* The counter has been reset */
1391         env->cp15.c15_ccnt = 0;
1392     }
1393 
1394     if (value & PMCRP) {
1395         unsigned int i;
1396         for (i = 0; i < pmu_num_counters(env); i++) {
1397             env->cp15.c14_pmevcntr[i] = 0;
1398         }
1399     }
1400 
1401     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1403 
1404     pmu_op_finish(env);
1405 }
1406 
1407 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408                           uint64_t value)
1409 {
1410     unsigned int i;
1411     uint64_t overflow_mask, new_pmswinc;
1412 
1413     for (i = 0; i < pmu_num_counters(env); i++) {
1414         /* Increment a counter's count iff: */
1415         if ((value & (1 << i)) && /* counter's bit is set */
1416                 /* counter is enabled and not filtered */
1417                 pmu_counter_enabled(env, i) &&
1418                 /* counter is SW_INCR */
1419                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1420             pmevcntr_op_start(env, i);
1421 
1422             /*
1423              * Detect if this write causes an overflow since we can't predict
1424              * PMSWINC overflows like we can for other events
1425              */
1426             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1427 
1428             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1429                 1ULL << 63 : 1ULL << 31;
1430 
1431             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1432                 env->cp15.c9_pmovsr |= (1 << i);
1433                 pmu_update_irq(env);
1434             }
1435 
1436             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1437 
1438             pmevcntr_op_finish(env, i);
1439         }
1440     }
1441 }
1442 
1443 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1444 {
1445     uint64_t ret;
1446     pmccntr_op_start(env);
1447     ret = env->cp15.c15_ccnt;
1448     pmccntr_op_finish(env);
1449     return ret;
1450 }
1451 
1452 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                          uint64_t value)
1454 {
1455     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1456      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1457      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1458      * accessed.
1459      */
1460     env->cp15.c9_pmselr = value & 0x1f;
1461 }
1462 
1463 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1464                         uint64_t value)
1465 {
1466     pmccntr_op_start(env);
1467     env->cp15.c15_ccnt = value;
1468     pmccntr_op_finish(env);
1469 }
1470 
1471 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1472                             uint64_t value)
1473 {
1474     uint64_t cur_val = pmccntr_read(env, NULL);
1475 
1476     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1477 }
1478 
1479 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1480                             uint64_t value)
1481 {
1482     pmccntr_op_start(env);
1483     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1484     pmccntr_op_finish(env);
1485 }
1486 
1487 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1488                             uint64_t value)
1489 {
1490     pmccntr_op_start(env);
1491     /* M is not accessible from AArch32 */
1492     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1493         (value & PMCCFILTR);
1494     pmccntr_op_finish(env);
1495 }
1496 
1497 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1498 {
1499     /* M is not visible in AArch32 */
1500     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1501 }
1502 
1503 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                             uint64_t value)
1505 {
1506     pmu_op_start(env);
1507     value &= pmu_counter_mask(env);
1508     env->cp15.c9_pmcnten |= value;
1509     pmu_op_finish(env);
1510 }
1511 
1512 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513                              uint64_t value)
1514 {
1515     pmu_op_start(env);
1516     value &= pmu_counter_mask(env);
1517     env->cp15.c9_pmcnten &= ~value;
1518     pmu_op_finish(env);
1519 }
1520 
1521 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1522                          uint64_t value)
1523 {
1524     value &= pmu_counter_mask(env);
1525     env->cp15.c9_pmovsr &= ~value;
1526     pmu_update_irq(env);
1527 }
1528 
1529 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1530                          uint64_t value)
1531 {
1532     value &= pmu_counter_mask(env);
1533     env->cp15.c9_pmovsr |= value;
1534     pmu_update_irq(env);
1535 }
1536 
1537 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1538                              uint64_t value, const uint8_t counter)
1539 {
1540     if (counter == 31) {
1541         pmccfiltr_write(env, ri, value);
1542     } else if (counter < pmu_num_counters(env)) {
1543         pmevcntr_op_start(env, counter);
1544 
1545         /*
1546          * If this counter's event type is changing, store the current
1547          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1548          * pmevcntr_op_finish has the correct baseline when it converts back to
1549          * a delta.
1550          */
1551         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1552             PMXEVTYPER_EVTCOUNT;
1553         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1554         if (old_event != new_event) {
1555             uint64_t count = 0;
1556             if (event_supported(new_event)) {
1557                 uint16_t event_idx = supported_event_map[new_event];
1558                 count = pm_events[event_idx].get_count(env);
1559             }
1560             env->cp15.c14_pmevcntr_delta[counter] = count;
1561         }
1562 
1563         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1564         pmevcntr_op_finish(env, counter);
1565     }
1566     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1567      * PMSELR value is equal to or greater than the number of implemented
1568      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1569      */
1570 }
1571 
1572 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1573                                const uint8_t counter)
1574 {
1575     if (counter == 31) {
1576         return env->cp15.pmccfiltr_el0;
1577     } else if (counter < pmu_num_counters(env)) {
1578         return env->cp15.c14_pmevtyper[counter];
1579     } else {
1580       /*
1581        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1582        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1583        */
1584         return 0;
1585     }
1586 }
1587 
1588 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1589                               uint64_t value)
1590 {
1591     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1592     pmevtyper_write(env, ri, value, counter);
1593 }
1594 
1595 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1596                                uint64_t value)
1597 {
1598     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1599     env->cp15.c14_pmevtyper[counter] = value;
1600 
1601     /*
1602      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1603      * pmu_op_finish calls when loading saved state for a migration. Because
1604      * we're potentially updating the type of event here, the value written to
1605      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1606      * different counter type. Therefore, we need to set this value to the
1607      * current count for the counter type we're writing so that pmu_op_finish
1608      * has the correct count for its calculation.
1609      */
1610     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1611     if (event_supported(event)) {
1612         uint16_t event_idx = supported_event_map[event];
1613         env->cp15.c14_pmevcntr_delta[counter] =
1614             pm_events[event_idx].get_count(env);
1615     }
1616 }
1617 
1618 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1619 {
1620     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1621     return pmevtyper_read(env, ri, counter);
1622 }
1623 
1624 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1625                              uint64_t value)
1626 {
1627     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1628 }
1629 
1630 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1631 {
1632     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1633 }
1634 
1635 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                              uint64_t value, uint8_t counter)
1637 {
1638     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1639         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1640         value &= MAKE_64BIT_MASK(0, 32);
1641     }
1642     if (counter < pmu_num_counters(env)) {
1643         pmevcntr_op_start(env, counter);
1644         env->cp15.c14_pmevcntr[counter] = value;
1645         pmevcntr_op_finish(env, counter);
1646     }
1647     /*
1648      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1649      * are CONSTRAINED UNPREDICTABLE.
1650      */
1651 }
1652 
1653 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1654                               uint8_t counter)
1655 {
1656     if (counter < pmu_num_counters(env)) {
1657         uint64_t ret;
1658         pmevcntr_op_start(env, counter);
1659         ret = env->cp15.c14_pmevcntr[counter];
1660         pmevcntr_op_finish(env, counter);
1661         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1662             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1663             ret &= MAKE_64BIT_MASK(0, 32);
1664         }
1665         return ret;
1666     } else {
1667       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1668        * are CONSTRAINED UNPREDICTABLE. */
1669         return 0;
1670     }
1671 }
1672 
1673 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1674                              uint64_t value)
1675 {
1676     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1677     pmevcntr_write(env, ri, value, counter);
1678 }
1679 
1680 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1681 {
1682     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683     return pmevcntr_read(env, ri, counter);
1684 }
1685 
1686 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1687                              uint64_t value)
1688 {
1689     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1690     assert(counter < pmu_num_counters(env));
1691     env->cp15.c14_pmevcntr[counter] = value;
1692     pmevcntr_write(env, ri, value, counter);
1693 }
1694 
1695 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1696 {
1697     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1698     assert(counter < pmu_num_counters(env));
1699     return env->cp15.c14_pmevcntr[counter];
1700 }
1701 
1702 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1703                              uint64_t value)
1704 {
1705     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1706 }
1707 
1708 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1709 {
1710     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1711 }
1712 
1713 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1714                             uint64_t value)
1715 {
1716     if (arm_feature(env, ARM_FEATURE_V8)) {
1717         env->cp15.c9_pmuserenr = value & 0xf;
1718     } else {
1719         env->cp15.c9_pmuserenr = value & 1;
1720     }
1721 }
1722 
1723 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724                              uint64_t value)
1725 {
1726     /* We have no event counters so only the C bit can be changed */
1727     value &= pmu_counter_mask(env);
1728     env->cp15.c9_pminten |= value;
1729     pmu_update_irq(env);
1730 }
1731 
1732 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733                              uint64_t value)
1734 {
1735     value &= pmu_counter_mask(env);
1736     env->cp15.c9_pminten &= ~value;
1737     pmu_update_irq(env);
1738 }
1739 
1740 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741                        uint64_t value)
1742 {
1743     /* Note that even though the AArch64 view of this register has bits
1744      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1745      * architectural requirements for bits which are RES0 only in some
1746      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1747      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1748      */
1749     raw_write(env, ri, value & ~0x1FULL);
1750 }
1751 
1752 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1753 {
1754     /* Begin with base v8.0 state.  */
1755     uint32_t valid_mask = 0x3fff;
1756     ARMCPU *cpu = env_archcpu(env);
1757 
1758     /*
1759      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1760      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1761      * Instead, choose the format based on the mode of EL3.
1762      */
1763     if (arm_el_is_aa64(env, 3)) {
1764         value |= SCR_FW | SCR_AW;      /* RES1 */
1765         valid_mask &= ~SCR_NET;        /* RES0 */
1766 
1767         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1768             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1769             value |= SCR_RW;           /* RAO/WI */
1770         }
1771         if (cpu_isar_feature(aa64_ras, cpu)) {
1772             valid_mask |= SCR_TERR;
1773         }
1774         if (cpu_isar_feature(aa64_lor, cpu)) {
1775             valid_mask |= SCR_TLOR;
1776         }
1777         if (cpu_isar_feature(aa64_pauth, cpu)) {
1778             valid_mask |= SCR_API | SCR_APK;
1779         }
1780         if (cpu_isar_feature(aa64_sel2, cpu)) {
1781             valid_mask |= SCR_EEL2;
1782         }
1783         if (cpu_isar_feature(aa64_mte, cpu)) {
1784             valid_mask |= SCR_ATA;
1785         }
1786         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1787             valid_mask |= SCR_ENSCXT;
1788         }
1789         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1790             valid_mask |= SCR_EASE | SCR_NMEA;
1791         }
1792     } else {
1793         valid_mask &= ~(SCR_RW | SCR_ST);
1794         if (cpu_isar_feature(aa32_ras, cpu)) {
1795             valid_mask |= SCR_TERR;
1796         }
1797     }
1798 
1799     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1800         valid_mask &= ~SCR_HCE;
1801 
1802         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1803          * supported if EL2 exists. The bit is UNK/SBZP when
1804          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1805          * when EL2 is unavailable.
1806          * On ARMv8, this bit is always available.
1807          */
1808         if (arm_feature(env, ARM_FEATURE_V7) &&
1809             !arm_feature(env, ARM_FEATURE_V8)) {
1810             valid_mask &= ~SCR_SMD;
1811         }
1812     }
1813 
1814     /* Clear all-context RES0 bits.  */
1815     value &= valid_mask;
1816     raw_write(env, ri, value);
1817 }
1818 
1819 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1820 {
1821     /*
1822      * scr_write will set the RES1 bits on an AArch64-only CPU.
1823      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1824      */
1825     scr_write(env, ri, 0);
1826 }
1827 
1828 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1829                                        const ARMCPRegInfo *ri,
1830                                        bool isread)
1831 {
1832     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1833         return CP_ACCESS_TRAP_EL2;
1834     }
1835 
1836     return CP_ACCESS_OK;
1837 }
1838 
1839 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1840 {
1841     ARMCPU *cpu = env_archcpu(env);
1842 
1843     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1844      * bank
1845      */
1846     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1847                                         ri->secure & ARM_CP_SECSTATE_S);
1848 
1849     return cpu->ccsidr[index];
1850 }
1851 
1852 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1853                          uint64_t value)
1854 {
1855     raw_write(env, ri, value & 0xf);
1856 }
1857 
1858 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1859 {
1860     CPUState *cs = env_cpu(env);
1861     bool el1 = arm_current_el(env) == 1;
1862     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1863     uint64_t ret = 0;
1864 
1865     if (hcr_el2 & HCR_IMO) {
1866         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1867             ret |= CPSR_I;
1868         }
1869     } else {
1870         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1871             ret |= CPSR_I;
1872         }
1873     }
1874 
1875     if (hcr_el2 & HCR_FMO) {
1876         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1877             ret |= CPSR_F;
1878         }
1879     } else {
1880         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1881             ret |= CPSR_F;
1882         }
1883     }
1884 
1885     if (hcr_el2 & HCR_AMO) {
1886         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1887             ret |= CPSR_A;
1888         }
1889     }
1890 
1891     return ret;
1892 }
1893 
1894 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1895                                        bool isread)
1896 {
1897     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1898         return CP_ACCESS_TRAP_EL2;
1899     }
1900 
1901     return CP_ACCESS_OK;
1902 }
1903 
1904 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1905                                        bool isread)
1906 {
1907     if (arm_feature(env, ARM_FEATURE_V8)) {
1908         return access_aa64_tid1(env, ri, isread);
1909     }
1910 
1911     return CP_ACCESS_OK;
1912 }
1913 
1914 static const ARMCPRegInfo v7_cp_reginfo[] = {
1915     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1916     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1917       .access = PL1_W, .type = ARM_CP_NOP },
1918     /* Performance monitors are implementation defined in v7,
1919      * but with an ARM recommended set of registers, which we
1920      * follow.
1921      *
1922      * Performance registers fall into three categories:
1923      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1924      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1925      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1926      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1927      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1928      */
1929     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1930       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1931       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1932       .writefn = pmcntenset_write,
1933       .accessfn = pmreg_access,
1934       .raw_writefn = raw_write },
1935     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1936       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1937       .access = PL0_RW, .accessfn = pmreg_access,
1938       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1939       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1940     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1941       .access = PL0_RW,
1942       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1943       .accessfn = pmreg_access,
1944       .writefn = pmcntenclr_write,
1945       .type = ARM_CP_ALIAS | ARM_CP_IO },
1946     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1947       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1948       .access = PL0_RW, .accessfn = pmreg_access,
1949       .type = ARM_CP_ALIAS | ARM_CP_IO,
1950       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1951       .writefn = pmcntenclr_write },
1952     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1953       .access = PL0_RW, .type = ARM_CP_IO,
1954       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1955       .accessfn = pmreg_access,
1956       .writefn = pmovsr_write,
1957       .raw_writefn = raw_write },
1958     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1959       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1960       .access = PL0_RW, .accessfn = pmreg_access,
1961       .type = ARM_CP_ALIAS | ARM_CP_IO,
1962       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1963       .writefn = pmovsr_write,
1964       .raw_writefn = raw_write },
1965     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1966       .access = PL0_W, .accessfn = pmreg_access_swinc,
1967       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1968       .writefn = pmswinc_write },
1969     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1970       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1971       .access = PL0_W, .accessfn = pmreg_access_swinc,
1972       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1973       .writefn = pmswinc_write },
1974     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1975       .access = PL0_RW, .type = ARM_CP_ALIAS,
1976       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1977       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1978       .raw_writefn = raw_write},
1979     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1980       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1981       .access = PL0_RW, .accessfn = pmreg_access_selr,
1982       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1983       .writefn = pmselr_write, .raw_writefn = raw_write, },
1984     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1985       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1986       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1987       .accessfn = pmreg_access_ccntr },
1988     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1989       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1990       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1991       .type = ARM_CP_IO,
1992       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1993       .readfn = pmccntr_read, .writefn = pmccntr_write,
1994       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1995     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1996       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1997       .access = PL0_RW, .accessfn = pmreg_access,
1998       .type = ARM_CP_ALIAS | ARM_CP_IO,
1999       .resetvalue = 0, },
2000     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2001       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2002       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2003       .access = PL0_RW, .accessfn = pmreg_access,
2004       .type = ARM_CP_IO,
2005       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2006       .resetvalue = 0, },
2007     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2008       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2009       .accessfn = pmreg_access,
2010       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2011     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2012       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2013       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2014       .accessfn = pmreg_access,
2015       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2016     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2017       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2018       .accessfn = pmreg_access_xevcntr,
2019       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2020     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2021       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2022       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2023       .accessfn = pmreg_access_xevcntr,
2024       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2025     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2026       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2027       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2028       .resetvalue = 0,
2029       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2030     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2031       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2032       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2033       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2034       .resetvalue = 0,
2035       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2036     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2037       .access = PL1_RW, .accessfn = access_tpm,
2038       .type = ARM_CP_ALIAS | ARM_CP_IO,
2039       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2040       .resetvalue = 0,
2041       .writefn = pmintenset_write, .raw_writefn = raw_write },
2042     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2043       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2044       .access = PL1_RW, .accessfn = access_tpm,
2045       .type = ARM_CP_IO,
2046       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2047       .writefn = pmintenset_write, .raw_writefn = raw_write,
2048       .resetvalue = 0x0 },
2049     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2050       .access = PL1_RW, .accessfn = access_tpm,
2051       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2052       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2053       .writefn = pmintenclr_write, },
2054     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2055       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2056       .access = PL1_RW, .accessfn = access_tpm,
2057       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2058       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2059       .writefn = pmintenclr_write },
2060     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2061       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2062       .access = PL1_R,
2063       .accessfn = access_aa64_tid2,
2064       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2065     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2066       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2067       .access = PL1_RW,
2068       .accessfn = access_aa64_tid2,
2069       .writefn = csselr_write, .resetvalue = 0,
2070       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2071                              offsetof(CPUARMState, cp15.csselr_ns) } },
2072     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2073      * just RAZ for all cores:
2074      */
2075     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2076       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2077       .access = PL1_R, .type = ARM_CP_CONST,
2078       .accessfn = access_aa64_tid1,
2079       .resetvalue = 0 },
2080     /* Auxiliary fault status registers: these also are IMPDEF, and we
2081      * choose to RAZ/WI for all cores.
2082      */
2083     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2084       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2085       .access = PL1_RW, .accessfn = access_tvm_trvm,
2086       .type = ARM_CP_CONST, .resetvalue = 0 },
2087     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2088       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2089       .access = PL1_RW, .accessfn = access_tvm_trvm,
2090       .type = ARM_CP_CONST, .resetvalue = 0 },
2091     /* MAIR can just read-as-written because we don't implement caches
2092      * and so don't need to care about memory attributes.
2093      */
2094     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2095       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2096       .access = PL1_RW, .accessfn = access_tvm_trvm,
2097       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2098       .resetvalue = 0 },
2099     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2100       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2101       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2102       .resetvalue = 0 },
2103     /* For non-long-descriptor page tables these are PRRR and NMRR;
2104      * regardless they still act as reads-as-written for QEMU.
2105      */
2106      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2107       * allows them to assign the correct fieldoffset based on the endianness
2108       * handled in the field definitions.
2109       */
2110     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2111       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2112       .access = PL1_RW, .accessfn = access_tvm_trvm,
2113       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2114                              offsetof(CPUARMState, cp15.mair0_ns) },
2115       .resetfn = arm_cp_reset_ignore },
2116     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2117       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2118       .access = PL1_RW, .accessfn = access_tvm_trvm,
2119       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2120                              offsetof(CPUARMState, cp15.mair1_ns) },
2121       .resetfn = arm_cp_reset_ignore },
2122     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2123       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2124       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2125     /* 32 bit ITLB invalidates */
2126     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2127       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2128       .writefn = tlbiall_write },
2129     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2130       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131       .writefn = tlbimva_write },
2132     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2133       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134       .writefn = tlbiasid_write },
2135     /* 32 bit DTLB invalidates */
2136     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2137       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2138       .writefn = tlbiall_write },
2139     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2140       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2141       .writefn = tlbimva_write },
2142     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2143       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2144       .writefn = tlbiasid_write },
2145     /* 32 bit TLB invalidates */
2146     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2147       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2148       .writefn = tlbiall_write },
2149     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2150       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2151       .writefn = tlbimva_write },
2152     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2153       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2154       .writefn = tlbiasid_write },
2155     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2156       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2157       .writefn = tlbimvaa_write },
2158 };
2159 
2160 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2161     /* 32 bit TLB invalidates, Inner Shareable */
2162     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2163       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2164       .writefn = tlbiall_is_write },
2165     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2166       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2167       .writefn = tlbimva_is_write },
2168     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2169       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2170       .writefn = tlbiasid_is_write },
2171     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2172       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2173       .writefn = tlbimvaa_is_write },
2174 };
2175 
2176 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2177     /* PMOVSSET is not implemented in v7 before v7ve */
2178     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2179       .access = PL0_RW, .accessfn = pmreg_access,
2180       .type = ARM_CP_ALIAS | ARM_CP_IO,
2181       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2182       .writefn = pmovsset_write,
2183       .raw_writefn = raw_write },
2184     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2185       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2186       .access = PL0_RW, .accessfn = pmreg_access,
2187       .type = ARM_CP_ALIAS | ARM_CP_IO,
2188       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2189       .writefn = pmovsset_write,
2190       .raw_writefn = raw_write },
2191 };
2192 
2193 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2194                         uint64_t value)
2195 {
2196     value &= 1;
2197     env->teecr = value;
2198 }
2199 
2200 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2201                                    bool isread)
2202 {
2203     /*
2204      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2205      * at all, so we don't need to check whether we're v8A.
2206      */
2207     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2208         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2209         return CP_ACCESS_TRAP_EL2;
2210     }
2211     return CP_ACCESS_OK;
2212 }
2213 
2214 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2215                                     bool isread)
2216 {
2217     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2218         return CP_ACCESS_TRAP;
2219     }
2220     return teecr_access(env, ri, isread);
2221 }
2222 
2223 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2224     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2225       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2226       .resetvalue = 0,
2227       .writefn = teecr_write, .accessfn = teecr_access },
2228     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2229       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2230       .accessfn = teehbr_access, .resetvalue = 0 },
2231 };
2232 
2233 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2234     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2235       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2236       .access = PL0_RW,
2237       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2238     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2239       .access = PL0_RW,
2240       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2241                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2242       .resetfn = arm_cp_reset_ignore },
2243     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2244       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2245       .access = PL0_R|PL1_W,
2246       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2247       .resetvalue = 0},
2248     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2249       .access = PL0_R|PL1_W,
2250       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2251                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2252       .resetfn = arm_cp_reset_ignore },
2253     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2254       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2255       .access = PL1_RW,
2256       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2257     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2258       .access = PL1_RW,
2259       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2260                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2261       .resetvalue = 0 },
2262 };
2263 
2264 #ifndef CONFIG_USER_ONLY
2265 
2266 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2267                                        bool isread)
2268 {
2269     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2270      * Writable only at the highest implemented exception level.
2271      */
2272     int el = arm_current_el(env);
2273     uint64_t hcr;
2274     uint32_t cntkctl;
2275 
2276     switch (el) {
2277     case 0:
2278         hcr = arm_hcr_el2_eff(env);
2279         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2280             cntkctl = env->cp15.cnthctl_el2;
2281         } else {
2282             cntkctl = env->cp15.c14_cntkctl;
2283         }
2284         if (!extract32(cntkctl, 0, 2)) {
2285             return CP_ACCESS_TRAP;
2286         }
2287         break;
2288     case 1:
2289         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2290             arm_is_secure_below_el3(env)) {
2291             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2292             return CP_ACCESS_TRAP_UNCATEGORIZED;
2293         }
2294         break;
2295     case 2:
2296     case 3:
2297         break;
2298     }
2299 
2300     if (!isread && el < arm_highest_el(env)) {
2301         return CP_ACCESS_TRAP_UNCATEGORIZED;
2302     }
2303 
2304     return CP_ACCESS_OK;
2305 }
2306 
2307 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2308                                         bool isread)
2309 {
2310     unsigned int cur_el = arm_current_el(env);
2311     bool has_el2 = arm_is_el2_enabled(env);
2312     uint64_t hcr = arm_hcr_el2_eff(env);
2313 
2314     switch (cur_el) {
2315     case 0:
2316         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2317         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2318             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2319                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2320         }
2321 
2322         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2323         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2324             return CP_ACCESS_TRAP;
2325         }
2326 
2327         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2328         if (hcr & HCR_E2H) {
2329             if (timeridx == GTIMER_PHYS &&
2330                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2331                 return CP_ACCESS_TRAP_EL2;
2332             }
2333         } else {
2334             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2335             if (has_el2 && timeridx == GTIMER_PHYS &&
2336                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2337                 return CP_ACCESS_TRAP_EL2;
2338             }
2339         }
2340         break;
2341 
2342     case 1:
2343         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2344         if (has_el2 && timeridx == GTIMER_PHYS &&
2345             (hcr & HCR_E2H
2346              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2347              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2348             return CP_ACCESS_TRAP_EL2;
2349         }
2350         break;
2351     }
2352     return CP_ACCESS_OK;
2353 }
2354 
2355 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2356                                       bool isread)
2357 {
2358     unsigned int cur_el = arm_current_el(env);
2359     bool has_el2 = arm_is_el2_enabled(env);
2360     uint64_t hcr = arm_hcr_el2_eff(env);
2361 
2362     switch (cur_el) {
2363     case 0:
2364         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2365             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2366             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2367                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2368         }
2369 
2370         /*
2371          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2372          * EL0 if EL0[PV]TEN is zero.
2373          */
2374         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2375             return CP_ACCESS_TRAP;
2376         }
2377         /* fall through */
2378 
2379     case 1:
2380         if (has_el2 && timeridx == GTIMER_PHYS) {
2381             if (hcr & HCR_E2H) {
2382                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2383                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2384                     return CP_ACCESS_TRAP_EL2;
2385                 }
2386             } else {
2387                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2388                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2389                     return CP_ACCESS_TRAP_EL2;
2390                 }
2391             }
2392         }
2393         break;
2394     }
2395     return CP_ACCESS_OK;
2396 }
2397 
2398 static CPAccessResult gt_pct_access(CPUARMState *env,
2399                                     const ARMCPRegInfo *ri,
2400                                     bool isread)
2401 {
2402     return gt_counter_access(env, GTIMER_PHYS, isread);
2403 }
2404 
2405 static CPAccessResult gt_vct_access(CPUARMState *env,
2406                                     const ARMCPRegInfo *ri,
2407                                     bool isread)
2408 {
2409     return gt_counter_access(env, GTIMER_VIRT, isread);
2410 }
2411 
2412 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2413                                        bool isread)
2414 {
2415     return gt_timer_access(env, GTIMER_PHYS, isread);
2416 }
2417 
2418 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2419                                        bool isread)
2420 {
2421     return gt_timer_access(env, GTIMER_VIRT, isread);
2422 }
2423 
2424 static CPAccessResult gt_stimer_access(CPUARMState *env,
2425                                        const ARMCPRegInfo *ri,
2426                                        bool isread)
2427 {
2428     /* The AArch64 register view of the secure physical timer is
2429      * always accessible from EL3, and configurably accessible from
2430      * Secure EL1.
2431      */
2432     switch (arm_current_el(env)) {
2433     case 1:
2434         if (!arm_is_secure(env)) {
2435             return CP_ACCESS_TRAP;
2436         }
2437         if (!(env->cp15.scr_el3 & SCR_ST)) {
2438             return CP_ACCESS_TRAP_EL3;
2439         }
2440         return CP_ACCESS_OK;
2441     case 0:
2442     case 2:
2443         return CP_ACCESS_TRAP;
2444     case 3:
2445         return CP_ACCESS_OK;
2446     default:
2447         g_assert_not_reached();
2448     }
2449 }
2450 
2451 static uint64_t gt_get_countervalue(CPUARMState *env)
2452 {
2453     ARMCPU *cpu = env_archcpu(env);
2454 
2455     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2456 }
2457 
2458 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2459 {
2460     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2461 
2462     if (gt->ctl & 1) {
2463         /* Timer enabled: calculate and set current ISTATUS, irq, and
2464          * reset timer to when ISTATUS next has to change
2465          */
2466         uint64_t offset = timeridx == GTIMER_VIRT ?
2467                                       cpu->env.cp15.cntvoff_el2 : 0;
2468         uint64_t count = gt_get_countervalue(&cpu->env);
2469         /* Note that this must be unsigned 64 bit arithmetic: */
2470         int istatus = count - offset >= gt->cval;
2471         uint64_t nexttick;
2472         int irqstate;
2473 
2474         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2475 
2476         irqstate = (istatus && !(gt->ctl & 2));
2477         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2478 
2479         if (istatus) {
2480             /* Next transition is when count rolls back over to zero */
2481             nexttick = UINT64_MAX;
2482         } else {
2483             /* Next transition is when we hit cval */
2484             nexttick = gt->cval + offset;
2485         }
2486         /* Note that the desired next expiry time might be beyond the
2487          * signed-64-bit range of a QEMUTimer -- in this case we just
2488          * set the timer for as far in the future as possible. When the
2489          * timer expires we will reset the timer for any remaining period.
2490          */
2491         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2492             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2493         } else {
2494             timer_mod(cpu->gt_timer[timeridx], nexttick);
2495         }
2496         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2497     } else {
2498         /* Timer disabled: ISTATUS and timer output always clear */
2499         gt->ctl &= ~4;
2500         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2501         timer_del(cpu->gt_timer[timeridx]);
2502         trace_arm_gt_recalc_disabled(timeridx);
2503     }
2504 }
2505 
2506 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2507                            int timeridx)
2508 {
2509     ARMCPU *cpu = env_archcpu(env);
2510 
2511     timer_del(cpu->gt_timer[timeridx]);
2512 }
2513 
2514 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2515 {
2516     return gt_get_countervalue(env);
2517 }
2518 
2519 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2520 {
2521     uint64_t hcr;
2522 
2523     switch (arm_current_el(env)) {
2524     case 2:
2525         hcr = arm_hcr_el2_eff(env);
2526         if (hcr & HCR_E2H) {
2527             return 0;
2528         }
2529         break;
2530     case 0:
2531         hcr = arm_hcr_el2_eff(env);
2532         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2533             return 0;
2534         }
2535         break;
2536     }
2537 
2538     return env->cp15.cntvoff_el2;
2539 }
2540 
2541 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2542 {
2543     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2544 }
2545 
2546 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2547                           int timeridx,
2548                           uint64_t value)
2549 {
2550     trace_arm_gt_cval_write(timeridx, value);
2551     env->cp15.c14_timer[timeridx].cval = value;
2552     gt_recalc_timer(env_archcpu(env), timeridx);
2553 }
2554 
2555 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2556                              int timeridx)
2557 {
2558     uint64_t offset = 0;
2559 
2560     switch (timeridx) {
2561     case GTIMER_VIRT:
2562     case GTIMER_HYPVIRT:
2563         offset = gt_virt_cnt_offset(env);
2564         break;
2565     }
2566 
2567     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2568                       (gt_get_countervalue(env) - offset));
2569 }
2570 
2571 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2572                           int timeridx,
2573                           uint64_t value)
2574 {
2575     uint64_t offset = 0;
2576 
2577     switch (timeridx) {
2578     case GTIMER_VIRT:
2579     case GTIMER_HYPVIRT:
2580         offset = gt_virt_cnt_offset(env);
2581         break;
2582     }
2583 
2584     trace_arm_gt_tval_write(timeridx, value);
2585     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2586                                          sextract64(value, 0, 32);
2587     gt_recalc_timer(env_archcpu(env), timeridx);
2588 }
2589 
2590 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2591                          int timeridx,
2592                          uint64_t value)
2593 {
2594     ARMCPU *cpu = env_archcpu(env);
2595     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2596 
2597     trace_arm_gt_ctl_write(timeridx, value);
2598     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2599     if ((oldval ^ value) & 1) {
2600         /* Enable toggled */
2601         gt_recalc_timer(cpu, timeridx);
2602     } else if ((oldval ^ value) & 2) {
2603         /* IMASK toggled: don't need to recalculate,
2604          * just set the interrupt line based on ISTATUS
2605          */
2606         int irqstate = (oldval & 4) && !(value & 2);
2607 
2608         trace_arm_gt_imask_toggle(timeridx, irqstate);
2609         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2610     }
2611 }
2612 
2613 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2614 {
2615     gt_timer_reset(env, ri, GTIMER_PHYS);
2616 }
2617 
2618 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2619                                uint64_t value)
2620 {
2621     gt_cval_write(env, ri, GTIMER_PHYS, value);
2622 }
2623 
2624 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2625 {
2626     return gt_tval_read(env, ri, GTIMER_PHYS);
2627 }
2628 
2629 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2630                                uint64_t value)
2631 {
2632     gt_tval_write(env, ri, GTIMER_PHYS, value);
2633 }
2634 
2635 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2636                               uint64_t value)
2637 {
2638     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2639 }
2640 
2641 static int gt_phys_redir_timeridx(CPUARMState *env)
2642 {
2643     switch (arm_mmu_idx(env)) {
2644     case ARMMMUIdx_E20_0:
2645     case ARMMMUIdx_E20_2:
2646     case ARMMMUIdx_E20_2_PAN:
2647     case ARMMMUIdx_SE20_0:
2648     case ARMMMUIdx_SE20_2:
2649     case ARMMMUIdx_SE20_2_PAN:
2650         return GTIMER_HYP;
2651     default:
2652         return GTIMER_PHYS;
2653     }
2654 }
2655 
2656 static int gt_virt_redir_timeridx(CPUARMState *env)
2657 {
2658     switch (arm_mmu_idx(env)) {
2659     case ARMMMUIdx_E20_0:
2660     case ARMMMUIdx_E20_2:
2661     case ARMMMUIdx_E20_2_PAN:
2662     case ARMMMUIdx_SE20_0:
2663     case ARMMMUIdx_SE20_2:
2664     case ARMMMUIdx_SE20_2_PAN:
2665         return GTIMER_HYPVIRT;
2666     default:
2667         return GTIMER_VIRT;
2668     }
2669 }
2670 
2671 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2672                                         const ARMCPRegInfo *ri)
2673 {
2674     int timeridx = gt_phys_redir_timeridx(env);
2675     return env->cp15.c14_timer[timeridx].cval;
2676 }
2677 
2678 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2679                                      uint64_t value)
2680 {
2681     int timeridx = gt_phys_redir_timeridx(env);
2682     gt_cval_write(env, ri, timeridx, value);
2683 }
2684 
2685 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2686                                         const ARMCPRegInfo *ri)
2687 {
2688     int timeridx = gt_phys_redir_timeridx(env);
2689     return gt_tval_read(env, ri, timeridx);
2690 }
2691 
2692 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2693                                      uint64_t value)
2694 {
2695     int timeridx = gt_phys_redir_timeridx(env);
2696     gt_tval_write(env, ri, timeridx, value);
2697 }
2698 
2699 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2700                                        const ARMCPRegInfo *ri)
2701 {
2702     int timeridx = gt_phys_redir_timeridx(env);
2703     return env->cp15.c14_timer[timeridx].ctl;
2704 }
2705 
2706 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2707                                     uint64_t value)
2708 {
2709     int timeridx = gt_phys_redir_timeridx(env);
2710     gt_ctl_write(env, ri, timeridx, value);
2711 }
2712 
2713 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2714 {
2715     gt_timer_reset(env, ri, GTIMER_VIRT);
2716 }
2717 
2718 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719                                uint64_t value)
2720 {
2721     gt_cval_write(env, ri, GTIMER_VIRT, value);
2722 }
2723 
2724 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2725 {
2726     return gt_tval_read(env, ri, GTIMER_VIRT);
2727 }
2728 
2729 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730                                uint64_t value)
2731 {
2732     gt_tval_write(env, ri, GTIMER_VIRT, value);
2733 }
2734 
2735 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2736                               uint64_t value)
2737 {
2738     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2739 }
2740 
2741 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742                               uint64_t value)
2743 {
2744     ARMCPU *cpu = env_archcpu(env);
2745 
2746     trace_arm_gt_cntvoff_write(value);
2747     raw_write(env, ri, value);
2748     gt_recalc_timer(cpu, GTIMER_VIRT);
2749 }
2750 
2751 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2752                                         const ARMCPRegInfo *ri)
2753 {
2754     int timeridx = gt_virt_redir_timeridx(env);
2755     return env->cp15.c14_timer[timeridx].cval;
2756 }
2757 
2758 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2759                                      uint64_t value)
2760 {
2761     int timeridx = gt_virt_redir_timeridx(env);
2762     gt_cval_write(env, ri, timeridx, value);
2763 }
2764 
2765 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2766                                         const ARMCPRegInfo *ri)
2767 {
2768     int timeridx = gt_virt_redir_timeridx(env);
2769     return gt_tval_read(env, ri, timeridx);
2770 }
2771 
2772 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2773                                      uint64_t value)
2774 {
2775     int timeridx = gt_virt_redir_timeridx(env);
2776     gt_tval_write(env, ri, timeridx, value);
2777 }
2778 
2779 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2780                                        const ARMCPRegInfo *ri)
2781 {
2782     int timeridx = gt_virt_redir_timeridx(env);
2783     return env->cp15.c14_timer[timeridx].ctl;
2784 }
2785 
2786 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787                                     uint64_t value)
2788 {
2789     int timeridx = gt_virt_redir_timeridx(env);
2790     gt_ctl_write(env, ri, timeridx, value);
2791 }
2792 
2793 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2794 {
2795     gt_timer_reset(env, ri, GTIMER_HYP);
2796 }
2797 
2798 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799                               uint64_t value)
2800 {
2801     gt_cval_write(env, ri, GTIMER_HYP, value);
2802 }
2803 
2804 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2805 {
2806     return gt_tval_read(env, ri, GTIMER_HYP);
2807 }
2808 
2809 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810                               uint64_t value)
2811 {
2812     gt_tval_write(env, ri, GTIMER_HYP, value);
2813 }
2814 
2815 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2816                               uint64_t value)
2817 {
2818     gt_ctl_write(env, ri, GTIMER_HYP, value);
2819 }
2820 
2821 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2822 {
2823     gt_timer_reset(env, ri, GTIMER_SEC);
2824 }
2825 
2826 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827                               uint64_t value)
2828 {
2829     gt_cval_write(env, ri, GTIMER_SEC, value);
2830 }
2831 
2832 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2833 {
2834     return gt_tval_read(env, ri, GTIMER_SEC);
2835 }
2836 
2837 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                               uint64_t value)
2839 {
2840     gt_tval_write(env, ri, GTIMER_SEC, value);
2841 }
2842 
2843 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844                               uint64_t value)
2845 {
2846     gt_ctl_write(env, ri, GTIMER_SEC, value);
2847 }
2848 
2849 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2850 {
2851     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2852 }
2853 
2854 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855                              uint64_t value)
2856 {
2857     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2858 }
2859 
2860 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2861 {
2862     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2863 }
2864 
2865 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2866                              uint64_t value)
2867 {
2868     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2869 }
2870 
2871 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2872                             uint64_t value)
2873 {
2874     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2875 }
2876 
2877 void arm_gt_ptimer_cb(void *opaque)
2878 {
2879     ARMCPU *cpu = opaque;
2880 
2881     gt_recalc_timer(cpu, GTIMER_PHYS);
2882 }
2883 
2884 void arm_gt_vtimer_cb(void *opaque)
2885 {
2886     ARMCPU *cpu = opaque;
2887 
2888     gt_recalc_timer(cpu, GTIMER_VIRT);
2889 }
2890 
2891 void arm_gt_htimer_cb(void *opaque)
2892 {
2893     ARMCPU *cpu = opaque;
2894 
2895     gt_recalc_timer(cpu, GTIMER_HYP);
2896 }
2897 
2898 void arm_gt_stimer_cb(void *opaque)
2899 {
2900     ARMCPU *cpu = opaque;
2901 
2902     gt_recalc_timer(cpu, GTIMER_SEC);
2903 }
2904 
2905 void arm_gt_hvtimer_cb(void *opaque)
2906 {
2907     ARMCPU *cpu = opaque;
2908 
2909     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2910 }
2911 
2912 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2913 {
2914     ARMCPU *cpu = env_archcpu(env);
2915 
2916     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2917 }
2918 
2919 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2920     /* Note that CNTFRQ is purely reads-as-written for the benefit
2921      * of software; writing it doesn't actually change the timer frequency.
2922      * Our reset value matches the fixed frequency we implement the timer at.
2923      */
2924     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2925       .type = ARM_CP_ALIAS,
2926       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2927       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2928     },
2929     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2930       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2931       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2932       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2933       .resetfn = arm_gt_cntfrq_reset,
2934     },
2935     /* overall control: mostly access permissions */
2936     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2937       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2938       .access = PL1_RW,
2939       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2940       .resetvalue = 0,
2941     },
2942     /* per-timer control */
2943     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2944       .secure = ARM_CP_SECSTATE_NS,
2945       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2946       .accessfn = gt_ptimer_access,
2947       .fieldoffset = offsetoflow32(CPUARMState,
2948                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2949       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2950       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2951     },
2952     { .name = "CNTP_CTL_S",
2953       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2954       .secure = ARM_CP_SECSTATE_S,
2955       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2956       .accessfn = gt_ptimer_access,
2957       .fieldoffset = offsetoflow32(CPUARMState,
2958                                    cp15.c14_timer[GTIMER_SEC].ctl),
2959       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2960     },
2961     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2962       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2963       .type = ARM_CP_IO, .access = PL0_RW,
2964       .accessfn = gt_ptimer_access,
2965       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2966       .resetvalue = 0,
2967       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2968       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2969     },
2970     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2971       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2972       .accessfn = gt_vtimer_access,
2973       .fieldoffset = offsetoflow32(CPUARMState,
2974                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2975       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2976       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2977     },
2978     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2979       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2980       .type = ARM_CP_IO, .access = PL0_RW,
2981       .accessfn = gt_vtimer_access,
2982       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2983       .resetvalue = 0,
2984       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2985       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2986     },
2987     /* TimerValue views: a 32 bit downcounting view of the underlying state */
2988     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2989       .secure = ARM_CP_SECSTATE_NS,
2990       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2991       .accessfn = gt_ptimer_access,
2992       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2993     },
2994     { .name = "CNTP_TVAL_S",
2995       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2996       .secure = ARM_CP_SECSTATE_S,
2997       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2998       .accessfn = gt_ptimer_access,
2999       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3000     },
3001     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3002       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3003       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3004       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3005       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3006     },
3007     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3008       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3009       .accessfn = gt_vtimer_access,
3010       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3011     },
3012     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3013       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3014       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3015       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3016       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3017     },
3018     /* The counter itself */
3019     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3020       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3021       .accessfn = gt_pct_access,
3022       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3023     },
3024     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3025       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3026       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3027       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3028     },
3029     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3030       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3031       .accessfn = gt_vct_access,
3032       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3033     },
3034     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3035       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3036       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3037       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3038     },
3039     /* Comparison value, indicating when the timer goes off */
3040     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3041       .secure = ARM_CP_SECSTATE_NS,
3042       .access = PL0_RW,
3043       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3044       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3045       .accessfn = gt_ptimer_access,
3046       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3047       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3048     },
3049     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3050       .secure = ARM_CP_SECSTATE_S,
3051       .access = PL0_RW,
3052       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3053       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3054       .accessfn = gt_ptimer_access,
3055       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3056     },
3057     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3058       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3059       .access = PL0_RW,
3060       .type = ARM_CP_IO,
3061       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3062       .resetvalue = 0, .accessfn = gt_ptimer_access,
3063       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3064       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3065     },
3066     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3067       .access = PL0_RW,
3068       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3069       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3070       .accessfn = gt_vtimer_access,
3071       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3072       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3073     },
3074     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3075       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3076       .access = PL0_RW,
3077       .type = ARM_CP_IO,
3078       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3079       .resetvalue = 0, .accessfn = gt_vtimer_access,
3080       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3081       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3082     },
3083     /* Secure timer -- this is actually restricted to only EL3
3084      * and configurably Secure-EL1 via the accessfn.
3085      */
3086     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3087       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3088       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3089       .accessfn = gt_stimer_access,
3090       .readfn = gt_sec_tval_read,
3091       .writefn = gt_sec_tval_write,
3092       .resetfn = gt_sec_timer_reset,
3093     },
3094     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3095       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3096       .type = ARM_CP_IO, .access = PL1_RW,
3097       .accessfn = gt_stimer_access,
3098       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3099       .resetvalue = 0,
3100       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3101     },
3102     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3103       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3104       .type = ARM_CP_IO, .access = PL1_RW,
3105       .accessfn = gt_stimer_access,
3106       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3107       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3108     },
3109 };
3110 
3111 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3112                                  bool isread)
3113 {
3114     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3115         return CP_ACCESS_TRAP;
3116     }
3117     return CP_ACCESS_OK;
3118 }
3119 
3120 #else
3121 
3122 /* In user-mode most of the generic timer registers are inaccessible
3123  * however modern kernels (4.12+) allow access to cntvct_el0
3124  */
3125 
3126 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3127 {
3128     ARMCPU *cpu = env_archcpu(env);
3129 
3130     /* Currently we have no support for QEMUTimer in linux-user so we
3131      * can't call gt_get_countervalue(env), instead we directly
3132      * call the lower level functions.
3133      */
3134     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3135 }
3136 
3137 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3138     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3139       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3140       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3141       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3142       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3143     },
3144     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3145       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3146       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3147       .readfn = gt_virt_cnt_read,
3148     },
3149 };
3150 
3151 #endif
3152 
3153 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3154 {
3155     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3156         raw_write(env, ri, value);
3157     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3158         raw_write(env, ri, value & 0xfffff6ff);
3159     } else {
3160         raw_write(env, ri, value & 0xfffff1ff);
3161     }
3162 }
3163 
3164 #ifndef CONFIG_USER_ONLY
3165 /* get_phys_addr() isn't present for user-mode-only targets */
3166 
3167 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3168                                  bool isread)
3169 {
3170     if (ri->opc2 & 4) {
3171         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3172          * Secure EL1 (which can only happen if EL3 is AArch64).
3173          * They are simply UNDEF if executed from NS EL1.
3174          * They function normally from EL2 or EL3.
3175          */
3176         if (arm_current_el(env) == 1) {
3177             if (arm_is_secure_below_el3(env)) {
3178                 if (env->cp15.scr_el3 & SCR_EEL2) {
3179                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3180                 }
3181                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3182             }
3183             return CP_ACCESS_TRAP_UNCATEGORIZED;
3184         }
3185     }
3186     return CP_ACCESS_OK;
3187 }
3188 
3189 #ifdef CONFIG_TCG
3190 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3191                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3192 {
3193     bool ret;
3194     uint64_t par64;
3195     bool format64 = false;
3196     ARMMMUFaultInfo fi = {};
3197     GetPhysAddrResult res = {};
3198 
3199     ret = get_phys_addr(env, value, access_type, mmu_idx, &res, &fi);
3200 
3201     /*
3202      * ATS operations only do S1 or S1+S2 translations, so we never
3203      * have to deal with the ARMCacheAttrs format for S2 only.
3204      */
3205     assert(!res.cacheattrs.is_s2_format);
3206 
3207     if (ret) {
3208         /*
3209          * Some kinds of translation fault must cause exceptions rather
3210          * than being reported in the PAR.
3211          */
3212         int current_el = arm_current_el(env);
3213         int target_el;
3214         uint32_t syn, fsr, fsc;
3215         bool take_exc = false;
3216 
3217         if (fi.s1ptw && current_el == 1
3218             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3219             /*
3220              * Synchronous stage 2 fault on an access made as part of the
3221              * translation table walk for AT S1E0* or AT S1E1* insn
3222              * executed from NS EL1. If this is a synchronous external abort
3223              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3224              * to EL3. Otherwise the fault is taken as an exception to EL2,
3225              * and HPFAR_EL2 holds the faulting IPA.
3226              */
3227             if (fi.type == ARMFault_SyncExternalOnWalk &&
3228                 (env->cp15.scr_el3 & SCR_EA)) {
3229                 target_el = 3;
3230             } else {
3231                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3232                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3233                     env->cp15.hpfar_el2 |= HPFAR_NS;
3234                 }
3235                 target_el = 2;
3236             }
3237             take_exc = true;
3238         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3239             /*
3240              * Synchronous external aborts during a translation table walk
3241              * are taken as Data Abort exceptions.
3242              */
3243             if (fi.stage2) {
3244                 if (current_el == 3) {
3245                     target_el = 3;
3246                 } else {
3247                     target_el = 2;
3248                 }
3249             } else {
3250                 target_el = exception_target_el(env);
3251             }
3252             take_exc = true;
3253         }
3254 
3255         if (take_exc) {
3256             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3257             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3258                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3259                 fsr = arm_fi_to_lfsc(&fi);
3260                 fsc = extract32(fsr, 0, 6);
3261             } else {
3262                 fsr = arm_fi_to_sfsc(&fi);
3263                 fsc = 0x3f;
3264             }
3265             /*
3266              * Report exception with ESR indicating a fault due to a
3267              * translation table walk for a cache maintenance instruction.
3268              */
3269             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3270                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3271             env->exception.vaddress = value;
3272             env->exception.fsr = fsr;
3273             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3274         }
3275     }
3276 
3277     if (is_a64(env)) {
3278         format64 = true;
3279     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3280         /*
3281          * ATS1Cxx:
3282          * * TTBCR.EAE determines whether the result is returned using the
3283          *   32-bit or the 64-bit PAR format
3284          * * Instructions executed in Hyp mode always use the 64bit format
3285          *
3286          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3287          * * The Non-secure TTBCR.EAE bit is set to 1
3288          * * The implementation includes EL2, and the value of HCR.VM is 1
3289          *
3290          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3291          *
3292          * ATS1Hx always uses the 64bit format.
3293          */
3294         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3295 
3296         if (arm_feature(env, ARM_FEATURE_EL2)) {
3297             if (mmu_idx == ARMMMUIdx_E10_0 ||
3298                 mmu_idx == ARMMMUIdx_E10_1 ||
3299                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3300                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3301             } else {
3302                 format64 |= arm_current_el(env) == 2;
3303             }
3304         }
3305     }
3306 
3307     if (format64) {
3308         /* Create a 64-bit PAR */
3309         par64 = (1 << 11); /* LPAE bit always set */
3310         if (!ret) {
3311             par64 |= res.phys & ~0xfffULL;
3312             if (!res.attrs.secure) {
3313                 par64 |= (1 << 9); /* NS */
3314             }
3315             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3316             par64 |= res.cacheattrs.shareability << 7; /* SH */
3317         } else {
3318             uint32_t fsr = arm_fi_to_lfsc(&fi);
3319 
3320             par64 |= 1; /* F */
3321             par64 |= (fsr & 0x3f) << 1; /* FS */
3322             if (fi.stage2) {
3323                 par64 |= (1 << 9); /* S */
3324             }
3325             if (fi.s1ptw) {
3326                 par64 |= (1 << 8); /* PTW */
3327             }
3328         }
3329     } else {
3330         /* fsr is a DFSR/IFSR value for the short descriptor
3331          * translation table format (with WnR always clear).
3332          * Convert it to a 32-bit PAR.
3333          */
3334         if (!ret) {
3335             /* We do not set any attribute bits in the PAR */
3336             if (res.page_size == (1 << 24)
3337                 && arm_feature(env, ARM_FEATURE_V7)) {
3338                 par64 = (res.phys & 0xff000000) | (1 << 1);
3339             } else {
3340                 par64 = res.phys & 0xfffff000;
3341             }
3342             if (!res.attrs.secure) {
3343                 par64 |= (1 << 9); /* NS */
3344             }
3345         } else {
3346             uint32_t fsr = arm_fi_to_sfsc(&fi);
3347 
3348             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3349                     ((fsr & 0xf) << 1) | 1;
3350         }
3351     }
3352     return par64;
3353 }
3354 #endif /* CONFIG_TCG */
3355 
3356 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3357 {
3358 #ifdef CONFIG_TCG
3359     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3360     uint64_t par64;
3361     ARMMMUIdx mmu_idx;
3362     int el = arm_current_el(env);
3363     bool secure = arm_is_secure_below_el3(env);
3364 
3365     switch (ri->opc2 & 6) {
3366     case 0:
3367         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3368         switch (el) {
3369         case 3:
3370             mmu_idx = ARMMMUIdx_SE3;
3371             break;
3372         case 2:
3373             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3374             /* fall through */
3375         case 1:
3376             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3377                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3378                            : ARMMMUIdx_Stage1_E1_PAN);
3379             } else {
3380                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3381             }
3382             break;
3383         default:
3384             g_assert_not_reached();
3385         }
3386         break;
3387     case 2:
3388         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3389         switch (el) {
3390         case 3:
3391             mmu_idx = ARMMMUIdx_SE10_0;
3392             break;
3393         case 2:
3394             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3395             mmu_idx = ARMMMUIdx_Stage1_E0;
3396             break;
3397         case 1:
3398             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3399             break;
3400         default:
3401             g_assert_not_reached();
3402         }
3403         break;
3404     case 4:
3405         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3406         mmu_idx = ARMMMUIdx_E10_1;
3407         break;
3408     case 6:
3409         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3410         mmu_idx = ARMMMUIdx_E10_0;
3411         break;
3412     default:
3413         g_assert_not_reached();
3414     }
3415 
3416     par64 = do_ats_write(env, value, access_type, mmu_idx);
3417 
3418     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3419 #else
3420     /* Handled by hardware accelerator. */
3421     g_assert_not_reached();
3422 #endif /* CONFIG_TCG */
3423 }
3424 
3425 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3426                         uint64_t value)
3427 {
3428 #ifdef CONFIG_TCG
3429     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3430     uint64_t par64;
3431 
3432     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3433 
3434     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3435 #else
3436     /* Handled by hardware accelerator. */
3437     g_assert_not_reached();
3438 #endif /* CONFIG_TCG */
3439 }
3440 
3441 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3442                                      bool isread)
3443 {
3444     if (arm_current_el(env) == 3 &&
3445         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3446         return CP_ACCESS_TRAP;
3447     }
3448     return CP_ACCESS_OK;
3449 }
3450 
3451 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3452                         uint64_t value)
3453 {
3454 #ifdef CONFIG_TCG
3455     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3456     ARMMMUIdx mmu_idx;
3457     int secure = arm_is_secure_below_el3(env);
3458 
3459     switch (ri->opc2 & 6) {
3460     case 0:
3461         switch (ri->opc1) {
3462         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3463             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3464                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3465                            : ARMMMUIdx_Stage1_E1_PAN);
3466             } else {
3467                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3468             }
3469             break;
3470         case 4: /* AT S1E2R, AT S1E2W */
3471             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3472             break;
3473         case 6: /* AT S1E3R, AT S1E3W */
3474             mmu_idx = ARMMMUIdx_SE3;
3475             break;
3476         default:
3477             g_assert_not_reached();
3478         }
3479         break;
3480     case 2: /* AT S1E0R, AT S1E0W */
3481         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3482         break;
3483     case 4: /* AT S12E1R, AT S12E1W */
3484         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3485         break;
3486     case 6: /* AT S12E0R, AT S12E0W */
3487         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3488         break;
3489     default:
3490         g_assert_not_reached();
3491     }
3492 
3493     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3494 #else
3495     /* Handled by hardware accelerator. */
3496     g_assert_not_reached();
3497 #endif /* CONFIG_TCG */
3498 }
3499 #endif
3500 
3501 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3502     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3503       .access = PL1_RW, .resetvalue = 0,
3504       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3505                              offsetoflow32(CPUARMState, cp15.par_ns) },
3506       .writefn = par_write },
3507 #ifndef CONFIG_USER_ONLY
3508     /* This underdecoding is safe because the reginfo is NO_RAW. */
3509     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3510       .access = PL1_W, .accessfn = ats_access,
3511       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3512 #endif
3513 };
3514 
3515 /* Return basic MPU access permission bits.  */
3516 static uint32_t simple_mpu_ap_bits(uint32_t val)
3517 {
3518     uint32_t ret;
3519     uint32_t mask;
3520     int i;
3521     ret = 0;
3522     mask = 3;
3523     for (i = 0; i < 16; i += 2) {
3524         ret |= (val >> i) & mask;
3525         mask <<= 2;
3526     }
3527     return ret;
3528 }
3529 
3530 /* Pad basic MPU access permission bits to extended format.  */
3531 static uint32_t extended_mpu_ap_bits(uint32_t val)
3532 {
3533     uint32_t ret;
3534     uint32_t mask;
3535     int i;
3536     ret = 0;
3537     mask = 3;
3538     for (i = 0; i < 16; i += 2) {
3539         ret |= (val & mask) << i;
3540         mask <<= 2;
3541     }
3542     return ret;
3543 }
3544 
3545 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3546                                  uint64_t value)
3547 {
3548     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3549 }
3550 
3551 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3552 {
3553     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3554 }
3555 
3556 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3557                                  uint64_t value)
3558 {
3559     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3560 }
3561 
3562 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3563 {
3564     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3565 }
3566 
3567 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3568 {
3569     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3570 
3571     if (!u32p) {
3572         return 0;
3573     }
3574 
3575     u32p += env->pmsav7.rnr[M_REG_NS];
3576     return *u32p;
3577 }
3578 
3579 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3580                          uint64_t value)
3581 {
3582     ARMCPU *cpu = env_archcpu(env);
3583     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3584 
3585     if (!u32p) {
3586         return;
3587     }
3588 
3589     u32p += env->pmsav7.rnr[M_REG_NS];
3590     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3591     *u32p = value;
3592 }
3593 
3594 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3595                               uint64_t value)
3596 {
3597     ARMCPU *cpu = env_archcpu(env);
3598     uint32_t nrgs = cpu->pmsav7_dregion;
3599 
3600     if (value >= nrgs) {
3601         qemu_log_mask(LOG_GUEST_ERROR,
3602                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3603                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3604         return;
3605     }
3606 
3607     raw_write(env, ri, value);
3608 }
3609 
3610 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3611     /* Reset for all these registers is handled in arm_cpu_reset(),
3612      * because the PMSAv7 is also used by M-profile CPUs, which do
3613      * not register cpregs but still need the state to be reset.
3614      */
3615     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3616       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3617       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3618       .readfn = pmsav7_read, .writefn = pmsav7_write,
3619       .resetfn = arm_cp_reset_ignore },
3620     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3621       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3622       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3623       .readfn = pmsav7_read, .writefn = pmsav7_write,
3624       .resetfn = arm_cp_reset_ignore },
3625     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3626       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3627       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3628       .readfn = pmsav7_read, .writefn = pmsav7_write,
3629       .resetfn = arm_cp_reset_ignore },
3630     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3631       .access = PL1_RW,
3632       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3633       .writefn = pmsav7_rgnr_write,
3634       .resetfn = arm_cp_reset_ignore },
3635 };
3636 
3637 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3638     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3639       .access = PL1_RW, .type = ARM_CP_ALIAS,
3640       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3641       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3642     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3643       .access = PL1_RW, .type = ARM_CP_ALIAS,
3644       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3645       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3646     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3647       .access = PL1_RW,
3648       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3649       .resetvalue = 0, },
3650     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3651       .access = PL1_RW,
3652       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3653       .resetvalue = 0, },
3654     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3655       .access = PL1_RW,
3656       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3657     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3658       .access = PL1_RW,
3659       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3660     /* Protection region base and size registers */
3661     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3662       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3663       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3664     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3665       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3666       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3667     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3668       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3669       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3670     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3671       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3672       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3673     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3674       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3675       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3676     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3677       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3678       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3679     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3680       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3681       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3682     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3683       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3684       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3685 };
3686 
3687 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3688                              uint64_t value)
3689 {
3690     ARMCPU *cpu = env_archcpu(env);
3691 
3692     if (!arm_feature(env, ARM_FEATURE_V8)) {
3693         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3694             /*
3695              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3696              * using Long-descriptor translation table format
3697              */
3698             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3699         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3700             /*
3701              * In an implementation that includes the Security Extensions
3702              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3703              * Short-descriptor translation table format.
3704              */
3705             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3706         } else {
3707             value &= TTBCR_N;
3708         }
3709     }
3710 
3711     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3712         /* With LPAE the TTBCR could result in a change of ASID
3713          * via the TTBCR.A1 bit, so do a TLB flush.
3714          */
3715         tlb_flush(CPU(cpu));
3716     }
3717     raw_write(env, ri, value);
3718 }
3719 
3720 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3721                                uint64_t value)
3722 {
3723     ARMCPU *cpu = env_archcpu(env);
3724 
3725     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3726     tlb_flush(CPU(cpu));
3727     raw_write(env, ri, value);
3728 }
3729 
3730 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3731                             uint64_t value)
3732 {
3733     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3734     if (cpreg_field_is_64bit(ri) &&
3735         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3736         ARMCPU *cpu = env_archcpu(env);
3737         tlb_flush(CPU(cpu));
3738     }
3739     raw_write(env, ri, value);
3740 }
3741 
3742 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3743                                     uint64_t value)
3744 {
3745     /*
3746      * If we are running with E2&0 regime, then an ASID is active.
3747      * Flush if that might be changing.  Note we're not checking
3748      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3749      * holds the active ASID, only checking the field that might.
3750      */
3751     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3752         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3753         uint16_t mask = ARMMMUIdxBit_E20_2 |
3754                         ARMMMUIdxBit_E20_2_PAN |
3755                         ARMMMUIdxBit_E20_0;
3756 
3757         if (arm_is_secure_below_el3(env)) {
3758             mask >>= ARM_MMU_IDX_A_NS;
3759         }
3760 
3761         tlb_flush_by_mmuidx(env_cpu(env), mask);
3762     }
3763     raw_write(env, ri, value);
3764 }
3765 
3766 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3767                         uint64_t value)
3768 {
3769     ARMCPU *cpu = env_archcpu(env);
3770     CPUState *cs = CPU(cpu);
3771 
3772     /*
3773      * A change in VMID to the stage2 page table (Stage2) invalidates
3774      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3775      */
3776     if (raw_read(env, ri) != value) {
3777         uint16_t mask = ARMMMUIdxBit_E10_1 |
3778                         ARMMMUIdxBit_E10_1_PAN |
3779                         ARMMMUIdxBit_E10_0;
3780 
3781         if (arm_is_secure_below_el3(env)) {
3782             mask >>= ARM_MMU_IDX_A_NS;
3783         }
3784 
3785         tlb_flush_by_mmuidx(cs, mask);
3786         raw_write(env, ri, value);
3787     }
3788 }
3789 
3790 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3791     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3792       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3793       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3794                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3795     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3796       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3797       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3798                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3799     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3800       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3801       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3802                              offsetof(CPUARMState, cp15.dfar_ns) } },
3803     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3804       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3805       .access = PL1_RW, .accessfn = access_tvm_trvm,
3806       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3807       .resetvalue = 0, },
3808 };
3809 
3810 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3811     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3812       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3813       .access = PL1_RW, .accessfn = access_tvm_trvm,
3814       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3815     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3816       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3817       .access = PL1_RW, .accessfn = access_tvm_trvm,
3818       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3819       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3820                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3821     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3822       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3823       .access = PL1_RW, .accessfn = access_tvm_trvm,
3824       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3825       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3826                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3827     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3828       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3829       .access = PL1_RW, .accessfn = access_tvm_trvm,
3830       .writefn = vmsa_tcr_el12_write,
3831       .raw_writefn = raw_write,
3832       .resetvalue = 0,
3833       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3834     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3835       .access = PL1_RW, .accessfn = access_tvm_trvm,
3836       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3837       .raw_writefn = raw_write,
3838       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3839                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3840 };
3841 
3842 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3843  * qemu tlbs nor adjusting cached masks.
3844  */
3845 static const ARMCPRegInfo ttbcr2_reginfo = {
3846     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3847     .access = PL1_RW, .accessfn = access_tvm_trvm,
3848     .type = ARM_CP_ALIAS,
3849     .bank_fieldoffsets = {
3850         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3851         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3852     },
3853 };
3854 
3855 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3856                                 uint64_t value)
3857 {
3858     env->cp15.c15_ticonfig = value & 0xe7;
3859     /* The OS_TYPE bit in this register changes the reported CPUID! */
3860     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3861         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3862 }
3863 
3864 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3865                                 uint64_t value)
3866 {
3867     env->cp15.c15_threadid = value & 0xffff;
3868 }
3869 
3870 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3871                            uint64_t value)
3872 {
3873     /* Wait-for-interrupt (deprecated) */
3874     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3875 }
3876 
3877 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3878                                   uint64_t value)
3879 {
3880     /* On OMAP there are registers indicating the max/min index of dcache lines
3881      * containing a dirty line; cache flush operations have to reset these.
3882      */
3883     env->cp15.c15_i_max = 0x000;
3884     env->cp15.c15_i_min = 0xff0;
3885 }
3886 
3887 static const ARMCPRegInfo omap_cp_reginfo[] = {
3888     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3889       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3890       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3891       .resetvalue = 0, },
3892     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3893       .access = PL1_RW, .type = ARM_CP_NOP },
3894     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3895       .access = PL1_RW,
3896       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3897       .writefn = omap_ticonfig_write },
3898     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3899       .access = PL1_RW,
3900       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3901     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3902       .access = PL1_RW, .resetvalue = 0xff0,
3903       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3904     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3905       .access = PL1_RW,
3906       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3907       .writefn = omap_threadid_write },
3908     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3909       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3910       .type = ARM_CP_NO_RAW,
3911       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3912     /* TODO: Peripheral port remap register:
3913      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3914      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3915      * when MMU is off.
3916      */
3917     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3918       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3919       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3920       .writefn = omap_cachemaint_write },
3921     { .name = "C9", .cp = 15, .crn = 9,
3922       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3923       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3924 };
3925 
3926 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3927                               uint64_t value)
3928 {
3929     env->cp15.c15_cpar = value & 0x3fff;
3930 }
3931 
3932 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3933     { .name = "XSCALE_CPAR",
3934       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3935       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3936       .writefn = xscale_cpar_write, },
3937     { .name = "XSCALE_AUXCR",
3938       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3939       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3940       .resetvalue = 0, },
3941     /* XScale specific cache-lockdown: since we have no cache we NOP these
3942      * and hope the guest does not really rely on cache behaviour.
3943      */
3944     { .name = "XSCALE_LOCK_ICACHE_LINE",
3945       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3946       .access = PL1_W, .type = ARM_CP_NOP },
3947     { .name = "XSCALE_UNLOCK_ICACHE",
3948       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3949       .access = PL1_W, .type = ARM_CP_NOP },
3950     { .name = "XSCALE_DCACHE_LOCK",
3951       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3952       .access = PL1_RW, .type = ARM_CP_NOP },
3953     { .name = "XSCALE_UNLOCK_DCACHE",
3954       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3955       .access = PL1_W, .type = ARM_CP_NOP },
3956 };
3957 
3958 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3959     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3960      * implementation of this implementation-defined space.
3961      * Ideally this should eventually disappear in favour of actually
3962      * implementing the correct behaviour for all cores.
3963      */
3964     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3965       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3966       .access = PL1_RW,
3967       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3968       .resetvalue = 0 },
3969 };
3970 
3971 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3972     /* Cache status: RAZ because we have no cache so it's always clean */
3973     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3974       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3975       .resetvalue = 0 },
3976 };
3977 
3978 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3979     /* We never have a block transfer operation in progress */
3980     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3981       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3982       .resetvalue = 0 },
3983     /* The cache ops themselves: these all NOP for QEMU */
3984     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3985       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3986     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3987       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3988     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3989       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3990     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3991       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3992     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3993       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3994     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3995       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3996 };
3997 
3998 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3999     /* The cache test-and-clean instructions always return (1 << 30)
4000      * to indicate that there are no dirty cache lines.
4001      */
4002     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4003       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4004       .resetvalue = (1 << 30) },
4005     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4006       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4007       .resetvalue = (1 << 30) },
4008 };
4009 
4010 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4011     /* Ignore ReadBuffer accesses */
4012     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4013       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4014       .access = PL1_RW, .resetvalue = 0,
4015       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4016 };
4017 
4018 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4019 {
4020     unsigned int cur_el = arm_current_el(env);
4021 
4022     if (arm_is_el2_enabled(env) && cur_el == 1) {
4023         return env->cp15.vpidr_el2;
4024     }
4025     return raw_read(env, ri);
4026 }
4027 
4028 static uint64_t mpidr_read_val(CPUARMState *env)
4029 {
4030     ARMCPU *cpu = env_archcpu(env);
4031     uint64_t mpidr = cpu->mp_affinity;
4032 
4033     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4034         mpidr |= (1U << 31);
4035         /* Cores which are uniprocessor (non-coherent)
4036          * but still implement the MP extensions set
4037          * bit 30. (For instance, Cortex-R5).
4038          */
4039         if (cpu->mp_is_up) {
4040             mpidr |= (1u << 30);
4041         }
4042     }
4043     return mpidr;
4044 }
4045 
4046 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4047 {
4048     unsigned int cur_el = arm_current_el(env);
4049 
4050     if (arm_is_el2_enabled(env) && cur_el == 1) {
4051         return env->cp15.vmpidr_el2;
4052     }
4053     return mpidr_read_val(env);
4054 }
4055 
4056 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4057     /* NOP AMAIR0/1 */
4058     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4059       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4060       .access = PL1_RW, .accessfn = access_tvm_trvm,
4061       .type = ARM_CP_CONST, .resetvalue = 0 },
4062     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4063     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4064       .access = PL1_RW, .accessfn = access_tvm_trvm,
4065       .type = ARM_CP_CONST, .resetvalue = 0 },
4066     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4067       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4068       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4069                              offsetof(CPUARMState, cp15.par_ns)} },
4070     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4071       .access = PL1_RW, .accessfn = access_tvm_trvm,
4072       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4073       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4074                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4075       .writefn = vmsa_ttbr_write, },
4076     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4077       .access = PL1_RW, .accessfn = access_tvm_trvm,
4078       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4079       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4080                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4081       .writefn = vmsa_ttbr_write, },
4082 };
4083 
4084 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4085 {
4086     return vfp_get_fpcr(env);
4087 }
4088 
4089 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4090                             uint64_t value)
4091 {
4092     vfp_set_fpcr(env, value);
4093 }
4094 
4095 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4096 {
4097     return vfp_get_fpsr(env);
4098 }
4099 
4100 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4101                             uint64_t value)
4102 {
4103     vfp_set_fpsr(env, value);
4104 }
4105 
4106 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4107                                        bool isread)
4108 {
4109     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4110         return CP_ACCESS_TRAP;
4111     }
4112     return CP_ACCESS_OK;
4113 }
4114 
4115 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4116                             uint64_t value)
4117 {
4118     env->daif = value & PSTATE_DAIF;
4119 }
4120 
4121 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4122 {
4123     return env->pstate & PSTATE_PAN;
4124 }
4125 
4126 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4127                            uint64_t value)
4128 {
4129     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4130 }
4131 
4132 static const ARMCPRegInfo pan_reginfo = {
4133     .name = "PAN", .state = ARM_CP_STATE_AA64,
4134     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4135     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4136     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4137 };
4138 
4139 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4140 {
4141     return env->pstate & PSTATE_UAO;
4142 }
4143 
4144 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145                            uint64_t value)
4146 {
4147     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4148 }
4149 
4150 static const ARMCPRegInfo uao_reginfo = {
4151     .name = "UAO", .state = ARM_CP_STATE_AA64,
4152     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4153     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4154     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4155 };
4156 
4157 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4158 {
4159     return env->pstate & PSTATE_DIT;
4160 }
4161 
4162 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4163                            uint64_t value)
4164 {
4165     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4166 }
4167 
4168 static const ARMCPRegInfo dit_reginfo = {
4169     .name = "DIT", .state = ARM_CP_STATE_AA64,
4170     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4171     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4172     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4173 };
4174 
4175 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4176 {
4177     return env->pstate & PSTATE_SSBS;
4178 }
4179 
4180 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181                            uint64_t value)
4182 {
4183     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4184 }
4185 
4186 static const ARMCPRegInfo ssbs_reginfo = {
4187     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4188     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4189     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4190     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4191 };
4192 
4193 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4194                                               const ARMCPRegInfo *ri,
4195                                               bool isread)
4196 {
4197     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4198     switch (arm_current_el(env)) {
4199     case 0:
4200         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4201         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4202             return CP_ACCESS_TRAP;
4203         }
4204         /* fall through */
4205     case 1:
4206         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4207         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4208             return CP_ACCESS_TRAP_EL2;
4209         }
4210         break;
4211     }
4212     return CP_ACCESS_OK;
4213 }
4214 
4215 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4216                                               const ARMCPRegInfo *ri,
4217                                               bool isread)
4218 {
4219     /* Cache invalidate/clean to Point of Unification... */
4220     switch (arm_current_el(env)) {
4221     case 0:
4222         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4223         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4224             return CP_ACCESS_TRAP;
4225         }
4226         /* fall through */
4227     case 1:
4228         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4229         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4230             return CP_ACCESS_TRAP_EL2;
4231         }
4232         break;
4233     }
4234     return CP_ACCESS_OK;
4235 }
4236 
4237 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4238  * Page D4-1736 (DDI0487A.b)
4239  */
4240 
4241 static int vae1_tlbmask(CPUARMState *env)
4242 {
4243     uint64_t hcr = arm_hcr_el2_eff(env);
4244     uint16_t mask;
4245 
4246     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4247         mask = ARMMMUIdxBit_E20_2 |
4248                ARMMMUIdxBit_E20_2_PAN |
4249                ARMMMUIdxBit_E20_0;
4250     } else {
4251         mask = ARMMMUIdxBit_E10_1 |
4252                ARMMMUIdxBit_E10_1_PAN |
4253                ARMMMUIdxBit_E10_0;
4254     }
4255 
4256     if (arm_is_secure_below_el3(env)) {
4257         mask >>= ARM_MMU_IDX_A_NS;
4258     }
4259 
4260     return mask;
4261 }
4262 
4263 /* Return 56 if TBI is enabled, 64 otherwise. */
4264 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4265                               uint64_t addr)
4266 {
4267     uint64_t tcr = regime_tcr(env, mmu_idx);
4268     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4269     int select = extract64(addr, 55, 1);
4270 
4271     return (tbi >> select) & 1 ? 56 : 64;
4272 }
4273 
4274 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4275 {
4276     uint64_t hcr = arm_hcr_el2_eff(env);
4277     ARMMMUIdx mmu_idx;
4278 
4279     /* Only the regime of the mmu_idx below is significant. */
4280     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4281         mmu_idx = ARMMMUIdx_E20_0;
4282     } else {
4283         mmu_idx = ARMMMUIdx_E10_0;
4284     }
4285 
4286     if (arm_is_secure_below_el3(env)) {
4287         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4288     }
4289 
4290     return tlbbits_for_regime(env, mmu_idx, addr);
4291 }
4292 
4293 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4294                                       uint64_t value)
4295 {
4296     CPUState *cs = env_cpu(env);
4297     int mask = vae1_tlbmask(env);
4298 
4299     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4300 }
4301 
4302 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4303                                     uint64_t value)
4304 {
4305     CPUState *cs = env_cpu(env);
4306     int mask = vae1_tlbmask(env);
4307 
4308     if (tlb_force_broadcast(env)) {
4309         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4310     } else {
4311         tlb_flush_by_mmuidx(cs, mask);
4312     }
4313 }
4314 
4315 static int alle1_tlbmask(CPUARMState *env)
4316 {
4317     /*
4318      * Note that the 'ALL' scope must invalidate both stage 1 and
4319      * stage 2 translations, whereas most other scopes only invalidate
4320      * stage 1 translations.
4321      */
4322     if (arm_is_secure_below_el3(env)) {
4323         return ARMMMUIdxBit_SE10_1 |
4324                ARMMMUIdxBit_SE10_1_PAN |
4325                ARMMMUIdxBit_SE10_0;
4326     } else {
4327         return ARMMMUIdxBit_E10_1 |
4328                ARMMMUIdxBit_E10_1_PAN |
4329                ARMMMUIdxBit_E10_0;
4330     }
4331 }
4332 
4333 static int e2_tlbmask(CPUARMState *env)
4334 {
4335     if (arm_is_secure_below_el3(env)) {
4336         return ARMMMUIdxBit_SE20_0 |
4337                ARMMMUIdxBit_SE20_2 |
4338                ARMMMUIdxBit_SE20_2_PAN |
4339                ARMMMUIdxBit_SE2;
4340     } else {
4341         return ARMMMUIdxBit_E20_0 |
4342                ARMMMUIdxBit_E20_2 |
4343                ARMMMUIdxBit_E20_2_PAN |
4344                ARMMMUIdxBit_E2;
4345     }
4346 }
4347 
4348 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4349                                   uint64_t value)
4350 {
4351     CPUState *cs = env_cpu(env);
4352     int mask = alle1_tlbmask(env);
4353 
4354     tlb_flush_by_mmuidx(cs, mask);
4355 }
4356 
4357 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4358                                   uint64_t value)
4359 {
4360     CPUState *cs = env_cpu(env);
4361     int mask = e2_tlbmask(env);
4362 
4363     tlb_flush_by_mmuidx(cs, mask);
4364 }
4365 
4366 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367                                   uint64_t value)
4368 {
4369     ARMCPU *cpu = env_archcpu(env);
4370     CPUState *cs = CPU(cpu);
4371 
4372     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4373 }
4374 
4375 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4376                                     uint64_t value)
4377 {
4378     CPUState *cs = env_cpu(env);
4379     int mask = alle1_tlbmask(env);
4380 
4381     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4382 }
4383 
4384 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4385                                     uint64_t value)
4386 {
4387     CPUState *cs = env_cpu(env);
4388     int mask = e2_tlbmask(env);
4389 
4390     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4391 }
4392 
4393 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4394                                     uint64_t value)
4395 {
4396     CPUState *cs = env_cpu(env);
4397 
4398     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4399 }
4400 
4401 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4402                                  uint64_t value)
4403 {
4404     /* Invalidate by VA, EL2
4405      * Currently handles both VAE2 and VALE2, since we don't support
4406      * flush-last-level-only.
4407      */
4408     CPUState *cs = env_cpu(env);
4409     int mask = e2_tlbmask(env);
4410     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4411 
4412     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4413 }
4414 
4415 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4416                                  uint64_t value)
4417 {
4418     /* Invalidate by VA, EL3
4419      * Currently handles both VAE3 and VALE3, since we don't support
4420      * flush-last-level-only.
4421      */
4422     ARMCPU *cpu = env_archcpu(env);
4423     CPUState *cs = CPU(cpu);
4424     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4425 
4426     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4427 }
4428 
4429 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4430                                    uint64_t value)
4431 {
4432     CPUState *cs = env_cpu(env);
4433     int mask = vae1_tlbmask(env);
4434     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4435     int bits = vae1_tlbbits(env, pageaddr);
4436 
4437     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4438 }
4439 
4440 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4441                                  uint64_t value)
4442 {
4443     /* Invalidate by VA, EL1&0 (AArch64 version).
4444      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4445      * since we don't support flush-for-specific-ASID-only or
4446      * flush-last-level-only.
4447      */
4448     CPUState *cs = env_cpu(env);
4449     int mask = vae1_tlbmask(env);
4450     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4451     int bits = vae1_tlbbits(env, pageaddr);
4452 
4453     if (tlb_force_broadcast(env)) {
4454         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4455     } else {
4456         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4457     }
4458 }
4459 
4460 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4461                                    uint64_t value)
4462 {
4463     CPUState *cs = env_cpu(env);
4464     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4465     bool secure = arm_is_secure_below_el3(env);
4466     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4467     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4468                                   pageaddr);
4469 
4470     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4471 }
4472 
4473 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4474                                    uint64_t value)
4475 {
4476     CPUState *cs = env_cpu(env);
4477     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4478     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4479 
4480     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4481                                                   ARMMMUIdxBit_SE3, bits);
4482 }
4483 
4484 #ifdef TARGET_AARCH64
4485 typedef struct {
4486     uint64_t base;
4487     uint64_t length;
4488 } TLBIRange;
4489 
4490 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4491                                      uint64_t value)
4492 {
4493     unsigned int page_size_granule, page_shift, num, scale, exponent;
4494     /* Extract one bit to represent the va selector in use. */
4495     uint64_t select = sextract64(value, 36, 1);
4496     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4497     TLBIRange ret = { };
4498 
4499     page_size_granule = extract64(value, 46, 2);
4500 
4501     /* The granule encoded in value must match the granule in use. */
4502     if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4503         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4504                       page_size_granule);
4505         return ret;
4506     }
4507 
4508     page_shift = (page_size_granule - 1) * 2 + 12;
4509     num = extract64(value, 39, 5);
4510     scale = extract64(value, 44, 2);
4511     exponent = (5 * scale) + 1;
4512 
4513     ret.length = (num + 1) << (exponent + page_shift);
4514 
4515     if (param.select) {
4516         ret.base = sextract64(value, 0, 37);
4517     } else {
4518         ret.base = extract64(value, 0, 37);
4519     }
4520     if (param.ds) {
4521         /*
4522          * With DS=1, BaseADDR is always shifted 16 so that it is able
4523          * to address all 52 va bits.  The input address is perforce
4524          * aligned on a 64k boundary regardless of translation granule.
4525          */
4526         page_shift = 16;
4527     }
4528     ret.base <<= page_shift;
4529 
4530     return ret;
4531 }
4532 
4533 static void do_rvae_write(CPUARMState *env, uint64_t value,
4534                           int idxmap, bool synced)
4535 {
4536     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4537     TLBIRange range;
4538     int bits;
4539 
4540     range = tlbi_aa64_get_range(env, one_idx, value);
4541     bits = tlbbits_for_regime(env, one_idx, range.base);
4542 
4543     if (synced) {
4544         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4545                                                   range.base,
4546                                                   range.length,
4547                                                   idxmap,
4548                                                   bits);
4549     } else {
4550         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4551                                   range.length, idxmap, bits);
4552     }
4553 }
4554 
4555 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4556                                   const ARMCPRegInfo *ri,
4557                                   uint64_t value)
4558 {
4559     /*
4560      * Invalidate by VA range, EL1&0.
4561      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4562      * since we don't support flush-for-specific-ASID-only or
4563      * flush-last-level-only.
4564      */
4565 
4566     do_rvae_write(env, value, vae1_tlbmask(env),
4567                   tlb_force_broadcast(env));
4568 }
4569 
4570 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4571                                     const ARMCPRegInfo *ri,
4572                                     uint64_t value)
4573 {
4574     /*
4575      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4576      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4577      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4578      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4579      * shareable specific flushes.
4580      */
4581 
4582     do_rvae_write(env, value, vae1_tlbmask(env), true);
4583 }
4584 
4585 static int vae2_tlbmask(CPUARMState *env)
4586 {
4587     return (arm_is_secure_below_el3(env)
4588             ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4589 }
4590 
4591 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4592                                   const ARMCPRegInfo *ri,
4593                                   uint64_t value)
4594 {
4595     /*
4596      * Invalidate by VA range, EL2.
4597      * Currently handles all of RVAE2 and RVALE2,
4598      * since we don't support flush-for-specific-ASID-only or
4599      * flush-last-level-only.
4600      */
4601 
4602     do_rvae_write(env, value, vae2_tlbmask(env),
4603                   tlb_force_broadcast(env));
4604 
4605 
4606 }
4607 
4608 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4609                                     const ARMCPRegInfo *ri,
4610                                     uint64_t value)
4611 {
4612     /*
4613      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4614      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4615      * since we don't support flush-for-specific-ASID-only,
4616      * flush-last-level-only or inner/outer shareable specific flushes.
4617      */
4618 
4619     do_rvae_write(env, value, vae2_tlbmask(env), true);
4620 
4621 }
4622 
4623 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4624                                   const ARMCPRegInfo *ri,
4625                                   uint64_t value)
4626 {
4627     /*
4628      * Invalidate by VA range, EL3.
4629      * Currently handles all of RVAE3 and RVALE3,
4630      * since we don't support flush-for-specific-ASID-only or
4631      * flush-last-level-only.
4632      */
4633 
4634     do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4635                   tlb_force_broadcast(env));
4636 }
4637 
4638 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4639                                     const ARMCPRegInfo *ri,
4640                                     uint64_t value)
4641 {
4642     /*
4643      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4644      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4645      * since we don't support flush-for-specific-ASID-only,
4646      * flush-last-level-only or inner/outer specific flushes.
4647      */
4648 
4649     do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4650 }
4651 #endif
4652 
4653 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4654                                       bool isread)
4655 {
4656     int cur_el = arm_current_el(env);
4657 
4658     if (cur_el < 2) {
4659         uint64_t hcr = arm_hcr_el2_eff(env);
4660 
4661         if (cur_el == 0) {
4662             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4663                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4664                     return CP_ACCESS_TRAP_EL2;
4665                 }
4666             } else {
4667                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4668                     return CP_ACCESS_TRAP;
4669                 }
4670                 if (hcr & HCR_TDZ) {
4671                     return CP_ACCESS_TRAP_EL2;
4672                 }
4673             }
4674         } else if (hcr & HCR_TDZ) {
4675             return CP_ACCESS_TRAP_EL2;
4676         }
4677     }
4678     return CP_ACCESS_OK;
4679 }
4680 
4681 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4682 {
4683     ARMCPU *cpu = env_archcpu(env);
4684     int dzp_bit = 1 << 4;
4685 
4686     /* DZP indicates whether DC ZVA access is allowed */
4687     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4688         dzp_bit = 0;
4689     }
4690     return cpu->dcz_blocksize | dzp_bit;
4691 }
4692 
4693 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4694                                     bool isread)
4695 {
4696     if (!(env->pstate & PSTATE_SP)) {
4697         /* Access to SP_EL0 is undefined if it's being used as
4698          * the stack pointer.
4699          */
4700         return CP_ACCESS_TRAP_UNCATEGORIZED;
4701     }
4702     return CP_ACCESS_OK;
4703 }
4704 
4705 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4706 {
4707     return env->pstate & PSTATE_SP;
4708 }
4709 
4710 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4711 {
4712     update_spsel(env, val);
4713 }
4714 
4715 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4716                         uint64_t value)
4717 {
4718     ARMCPU *cpu = env_archcpu(env);
4719 
4720     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4721         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4722         value &= ~SCTLR_M;
4723     }
4724 
4725     /* ??? Lots of these bits are not implemented.  */
4726 
4727     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4728         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4729             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4730         } else {
4731             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4732                        SCTLR_ATA0 | SCTLR_ATA);
4733         }
4734     }
4735 
4736     if (raw_read(env, ri) == value) {
4737         /* Skip the TLB flush if nothing actually changed; Linux likes
4738          * to do a lot of pointless SCTLR writes.
4739          */
4740         return;
4741     }
4742 
4743     raw_write(env, ri, value);
4744 
4745     /* This may enable/disable the MMU, so do a TLB flush.  */
4746     tlb_flush(CPU(cpu));
4747 
4748     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4749         /*
4750          * Normally we would always end the TB on an SCTLR write; see the
4751          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4752          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4753          * of hflags from the translator, so do it here.
4754          */
4755         arm_rebuild_hflags(env);
4756     }
4757 }
4758 
4759 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4760                            uint64_t value)
4761 {
4762     /*
4763      * Some MDCR_EL3 bits affect whether PMU counters are running:
4764      * if we are trying to change any of those then we must
4765      * bracket this update with PMU start/finish calls.
4766      */
4767     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4768 
4769     if (pmu_op) {
4770         pmu_op_start(env);
4771     }
4772     env->cp15.mdcr_el3 = value;
4773     if (pmu_op) {
4774         pmu_op_finish(env);
4775     }
4776 }
4777 
4778 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4779                        uint64_t value)
4780 {
4781     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4782     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4783 }
4784 
4785 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4786                            uint64_t value)
4787 {
4788     /*
4789      * Some MDCR_EL2 bits affect whether PMU counters are running:
4790      * if we are trying to change any of those then we must
4791      * bracket this update with PMU start/finish calls.
4792      */
4793     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4794 
4795     if (pmu_op) {
4796         pmu_op_start(env);
4797     }
4798     env->cp15.mdcr_el2 = value;
4799     if (pmu_op) {
4800         pmu_op_finish(env);
4801     }
4802 }
4803 
4804 static const ARMCPRegInfo v8_cp_reginfo[] = {
4805     /* Minimal set of EL0-visible registers. This will need to be expanded
4806      * significantly for system emulation of AArch64 CPUs.
4807      */
4808     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4809       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4810       .access = PL0_RW, .type = ARM_CP_NZCV },
4811     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4812       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4813       .type = ARM_CP_NO_RAW,
4814       .access = PL0_RW, .accessfn = aa64_daif_access,
4815       .fieldoffset = offsetof(CPUARMState, daif),
4816       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4817     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4818       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4819       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4820       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4821     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4822       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4823       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4824       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4825     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4826       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4827       .access = PL0_R, .type = ARM_CP_NO_RAW,
4828       .readfn = aa64_dczid_read },
4829     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4830       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4831       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4832 #ifndef CONFIG_USER_ONLY
4833       /* Avoid overhead of an access check that always passes in user-mode */
4834       .accessfn = aa64_zva_access,
4835 #endif
4836     },
4837     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4838       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4839       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4840     /* Cache ops: all NOPs since we don't emulate caches */
4841     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4842       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4843       .access = PL1_W, .type = ARM_CP_NOP,
4844       .accessfn = aa64_cacheop_pou_access },
4845     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4846       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4847       .access = PL1_W, .type = ARM_CP_NOP,
4848       .accessfn = aa64_cacheop_pou_access },
4849     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4850       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4851       .access = PL0_W, .type = ARM_CP_NOP,
4852       .accessfn = aa64_cacheop_pou_access },
4853     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4854       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4855       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4856       .type = ARM_CP_NOP },
4857     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4858       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4859       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4860     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4861       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4862       .access = PL0_W, .type = ARM_CP_NOP,
4863       .accessfn = aa64_cacheop_poc_access },
4864     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4865       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4866       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4867     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4868       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4869       .access = PL0_W, .type = ARM_CP_NOP,
4870       .accessfn = aa64_cacheop_pou_access },
4871     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4872       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4873       .access = PL0_W, .type = ARM_CP_NOP,
4874       .accessfn = aa64_cacheop_poc_access },
4875     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4876       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4877       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4878     /* TLBI operations */
4879     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4880       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4881       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4882       .writefn = tlbi_aa64_vmalle1is_write },
4883     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4884       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4885       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4886       .writefn = tlbi_aa64_vae1is_write },
4887     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4888       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4889       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4890       .writefn = tlbi_aa64_vmalle1is_write },
4891     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4892       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4893       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4894       .writefn = tlbi_aa64_vae1is_write },
4895     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4896       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4897       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4898       .writefn = tlbi_aa64_vae1is_write },
4899     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4900       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4901       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4902       .writefn = tlbi_aa64_vae1is_write },
4903     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4904       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4905       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4906       .writefn = tlbi_aa64_vmalle1_write },
4907     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4908       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4909       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4910       .writefn = tlbi_aa64_vae1_write },
4911     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4912       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4913       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4914       .writefn = tlbi_aa64_vmalle1_write },
4915     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4916       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4917       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4918       .writefn = tlbi_aa64_vae1_write },
4919     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4920       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4921       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4922       .writefn = tlbi_aa64_vae1_write },
4923     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4924       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4925       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4926       .writefn = tlbi_aa64_vae1_write },
4927     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4928       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4929       .access = PL2_W, .type = ARM_CP_NOP },
4930     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4931       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4932       .access = PL2_W, .type = ARM_CP_NOP },
4933     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4934       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4935       .access = PL2_W, .type = ARM_CP_NO_RAW,
4936       .writefn = tlbi_aa64_alle1is_write },
4937     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4938       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4939       .access = PL2_W, .type = ARM_CP_NO_RAW,
4940       .writefn = tlbi_aa64_alle1is_write },
4941     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4942       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4943       .access = PL2_W, .type = ARM_CP_NOP },
4944     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4945       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4946       .access = PL2_W, .type = ARM_CP_NOP },
4947     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4948       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4949       .access = PL2_W, .type = ARM_CP_NO_RAW,
4950       .writefn = tlbi_aa64_alle1_write },
4951     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4952       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4953       .access = PL2_W, .type = ARM_CP_NO_RAW,
4954       .writefn = tlbi_aa64_alle1is_write },
4955 #ifndef CONFIG_USER_ONLY
4956     /* 64 bit address translation operations */
4957     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4958       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4959       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4960       .writefn = ats_write64 },
4961     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4962       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4963       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4964       .writefn = ats_write64 },
4965     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4966       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4967       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4968       .writefn = ats_write64 },
4969     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4970       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4971       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4972       .writefn = ats_write64 },
4973     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4974       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4975       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4976       .writefn = ats_write64 },
4977     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4978       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4979       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4980       .writefn = ats_write64 },
4981     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4982       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4983       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4984       .writefn = ats_write64 },
4985     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4986       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4987       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4988       .writefn = ats_write64 },
4989     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4990     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4991       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4992       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4993       .writefn = ats_write64 },
4994     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4995       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4996       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4997       .writefn = ats_write64 },
4998     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4999       .type = ARM_CP_ALIAS,
5000       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5001       .access = PL1_RW, .resetvalue = 0,
5002       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5003       .writefn = par_write },
5004 #endif
5005     /* TLB invalidate last level of translation table walk */
5006     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5007       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5008       .writefn = tlbimva_is_write },
5009     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5010       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5011       .writefn = tlbimvaa_is_write },
5012     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5013       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5014       .writefn = tlbimva_write },
5015     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5016       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5017       .writefn = tlbimvaa_write },
5018     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5019       .type = ARM_CP_NO_RAW, .access = PL2_W,
5020       .writefn = tlbimva_hyp_write },
5021     { .name = "TLBIMVALHIS",
5022       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5023       .type = ARM_CP_NO_RAW, .access = PL2_W,
5024       .writefn = tlbimva_hyp_is_write },
5025     { .name = "TLBIIPAS2",
5026       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5027       .type = ARM_CP_NOP, .access = PL2_W },
5028     { .name = "TLBIIPAS2IS",
5029       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5030       .type = ARM_CP_NOP, .access = PL2_W },
5031     { .name = "TLBIIPAS2L",
5032       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5033       .type = ARM_CP_NOP, .access = PL2_W },
5034     { .name = "TLBIIPAS2LIS",
5035       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5036       .type = ARM_CP_NOP, .access = PL2_W },
5037     /* 32 bit cache operations */
5038     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5039       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5040     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5041       .type = ARM_CP_NOP, .access = PL1_W },
5042     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5043       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5044     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5045       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5046     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5047       .type = ARM_CP_NOP, .access = PL1_W },
5048     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5049       .type = ARM_CP_NOP, .access = PL1_W },
5050     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5051       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5052     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5053       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5054     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5055       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5056     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5057       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5058     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5059       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5060     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5061       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5062     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5063       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5064     /* MMU Domain access control / MPU write buffer control */
5065     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5066       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5067       .writefn = dacr_write, .raw_writefn = raw_write,
5068       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5069                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5070     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5071       .type = ARM_CP_ALIAS,
5072       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5073       .access = PL1_RW,
5074       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5075     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5076       .type = ARM_CP_ALIAS,
5077       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5078       .access = PL1_RW,
5079       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5080     /* We rely on the access checks not allowing the guest to write to the
5081      * state field when SPSel indicates that it's being used as the stack
5082      * pointer.
5083      */
5084     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5085       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5086       .access = PL1_RW, .accessfn = sp_el0_access,
5087       .type = ARM_CP_ALIAS,
5088       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5089     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5090       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5091       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5092       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5093     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5094       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5095       .type = ARM_CP_NO_RAW,
5096       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5097     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5098       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5099       .access = PL2_RW,
5100       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5101       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5102     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5103       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5104       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5105       .writefn = dacr_write, .raw_writefn = raw_write,
5106       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5107     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5108       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5109       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5110       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5111     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5112       .type = ARM_CP_ALIAS,
5113       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5114       .access = PL2_RW,
5115       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5116     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5117       .type = ARM_CP_ALIAS,
5118       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5119       .access = PL2_RW,
5120       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5121     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5122       .type = ARM_CP_ALIAS,
5123       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5124       .access = PL2_RW,
5125       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5126     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5127       .type = ARM_CP_ALIAS,
5128       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5129       .access = PL2_RW,
5130       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5131     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5132       .type = ARM_CP_IO,
5133       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5134       .resetvalue = 0,
5135       .access = PL3_RW,
5136       .writefn = mdcr_el3_write,
5137       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5138     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5139       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5140       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5141       .writefn = sdcr_write,
5142       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5143 };
5144 
5145 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5146 {
5147     ARMCPU *cpu = env_archcpu(env);
5148 
5149     if (arm_feature(env, ARM_FEATURE_V8)) {
5150         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5151     } else {
5152         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5153     }
5154 
5155     if (arm_feature(env, ARM_FEATURE_EL3)) {
5156         valid_mask &= ~HCR_HCD;
5157     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5158         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5159          * However, if we're using the SMC PSCI conduit then QEMU is
5160          * effectively acting like EL3 firmware and so the guest at
5161          * EL2 should retain the ability to prevent EL1 from being
5162          * able to make SMC calls into the ersatz firmware, so in
5163          * that case HCR.TSC should be read/write.
5164          */
5165         valid_mask &= ~HCR_TSC;
5166     }
5167 
5168     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5169         if (cpu_isar_feature(aa64_vh, cpu)) {
5170             valid_mask |= HCR_E2H;
5171         }
5172         if (cpu_isar_feature(aa64_ras, cpu)) {
5173             valid_mask |= HCR_TERR | HCR_TEA;
5174         }
5175         if (cpu_isar_feature(aa64_lor, cpu)) {
5176             valid_mask |= HCR_TLOR;
5177         }
5178         if (cpu_isar_feature(aa64_pauth, cpu)) {
5179             valid_mask |= HCR_API | HCR_APK;
5180         }
5181         if (cpu_isar_feature(aa64_mte, cpu)) {
5182             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5183         }
5184         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5185             valid_mask |= HCR_ENSCXT;
5186         }
5187         if (cpu_isar_feature(aa64_fwb, cpu)) {
5188             valid_mask |= HCR_FWB;
5189         }
5190     }
5191 
5192     /* Clear RES0 bits.  */
5193     value &= valid_mask;
5194 
5195     /*
5196      * These bits change the MMU setup:
5197      * HCR_VM enables stage 2 translation
5198      * HCR_PTW forbids certain page-table setups
5199      * HCR_DC disables stage1 and enables stage2 translation
5200      * HCR_DCT enables tagging on (disabled) stage1 translation
5201      * HCR_FWB changes the interpretation of stage2 descriptor bits
5202      */
5203     if ((env->cp15.hcr_el2 ^ value) &
5204         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5205         tlb_flush(CPU(cpu));
5206     }
5207     env->cp15.hcr_el2 = value;
5208 
5209     /*
5210      * Updates to VI and VF require us to update the status of
5211      * virtual interrupts, which are the logical OR of these bits
5212      * and the state of the input lines from the GIC. (This requires
5213      * that we have the iothread lock, which is done by marking the
5214      * reginfo structs as ARM_CP_IO.)
5215      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5216      * possible for it to be taken immediately, because VIRQ and
5217      * VFIQ are masked unless running at EL0 or EL1, and HCR
5218      * can only be written at EL2.
5219      */
5220     g_assert(qemu_mutex_iothread_locked());
5221     arm_cpu_update_virq(cpu);
5222     arm_cpu_update_vfiq(cpu);
5223     arm_cpu_update_vserr(cpu);
5224 }
5225 
5226 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5227 {
5228     do_hcr_write(env, value, 0);
5229 }
5230 
5231 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5232                           uint64_t value)
5233 {
5234     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5235     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5236     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5237 }
5238 
5239 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5240                          uint64_t value)
5241 {
5242     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5243     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5244     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5245 }
5246 
5247 /*
5248  * Return the effective value of HCR_EL2.
5249  * Bits that are not included here:
5250  * RW       (read from SCR_EL3.RW as needed)
5251  */
5252 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5253 {
5254     uint64_t ret = env->cp15.hcr_el2;
5255 
5256     if (!arm_is_el2_enabled(env)) {
5257         /*
5258          * "This register has no effect if EL2 is not enabled in the
5259          * current Security state".  This is ARMv8.4-SecEL2 speak for
5260          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5261          *
5262          * Prior to that, the language was "In an implementation that
5263          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5264          * as if this field is 0 for all purposes other than a direct
5265          * read or write access of HCR_EL2".  With lots of enumeration
5266          * on a per-field basis.  In current QEMU, this is condition
5267          * is arm_is_secure_below_el3.
5268          *
5269          * Since the v8.4 language applies to the entire register, and
5270          * appears to be backward compatible, use that.
5271          */
5272         return 0;
5273     }
5274 
5275     /*
5276      * For a cpu that supports both aarch64 and aarch32, we can set bits
5277      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5278      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5279      */
5280     if (!arm_el_is_aa64(env, 2)) {
5281         uint64_t aa32_valid;
5282 
5283         /*
5284          * These bits are up-to-date as of ARMv8.6.
5285          * For HCR, it's easiest to list just the 2 bits that are invalid.
5286          * For HCR2, list those that are valid.
5287          */
5288         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5289         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5290                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5291         ret &= aa32_valid;
5292     }
5293 
5294     if (ret & HCR_TGE) {
5295         /* These bits are up-to-date as of ARMv8.6.  */
5296         if (ret & HCR_E2H) {
5297             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5298                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5299                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5300                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5301                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5302                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5303         } else {
5304             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5305         }
5306         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5307                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5308                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5309                  HCR_TLOR);
5310     }
5311 
5312     return ret;
5313 }
5314 
5315 /*
5316  * Corresponds to ARM pseudocode function ELIsInHost().
5317  */
5318 bool el_is_in_host(CPUARMState *env, int el)
5319 {
5320     uint64_t mask;
5321 
5322     /*
5323      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5324      * Perform the simplest bit tests first, and validate EL2 afterward.
5325      */
5326     if (el & 1) {
5327         return false; /* EL1 or EL3 */
5328     }
5329 
5330     /*
5331      * Note that hcr_write() checks isar_feature_aa64_vh(),
5332      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5333      */
5334     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5335     if ((env->cp15.hcr_el2 & mask) != mask) {
5336         return false;
5337     }
5338 
5339     /* TGE and/or E2H set: double check those bits are currently legal. */
5340     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5341 }
5342 
5343 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5344                        uint64_t value)
5345 {
5346     uint64_t valid_mask = 0;
5347 
5348     /* No features adding bits to HCRX are implemented. */
5349 
5350     /* Clear RES0 bits.  */
5351     env->cp15.hcrx_el2 = value & valid_mask;
5352 }
5353 
5354 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5355                                   bool isread)
5356 {
5357     if (arm_current_el(env) < 3
5358         && arm_feature(env, ARM_FEATURE_EL3)
5359         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5360         return CP_ACCESS_TRAP_EL3;
5361     }
5362     return CP_ACCESS_OK;
5363 }
5364 
5365 static const ARMCPRegInfo hcrx_el2_reginfo = {
5366     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5367     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5368     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5369     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5370 };
5371 
5372 /* Return the effective value of HCRX_EL2.  */
5373 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5374 {
5375     /*
5376      * The bits in this register behave as 0 for all purposes other than
5377      * direct reads of the register if:
5378      *   - EL2 is not enabled in the current security state,
5379      *   - SCR_EL3.HXEn is 0.
5380      */
5381     if (!arm_is_el2_enabled(env)
5382         || (arm_feature(env, ARM_FEATURE_EL3)
5383             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5384         return 0;
5385     }
5386     return env->cp15.hcrx_el2;
5387 }
5388 
5389 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5390                            uint64_t value)
5391 {
5392     /*
5393      * For A-profile AArch32 EL3, if NSACR.CP10
5394      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5395      */
5396     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5397         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5398         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5399         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5400     }
5401     env->cp15.cptr_el[2] = value;
5402 }
5403 
5404 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5405 {
5406     /*
5407      * For A-profile AArch32 EL3, if NSACR.CP10
5408      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5409      */
5410     uint64_t value = env->cp15.cptr_el[2];
5411 
5412     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5413         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5414         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5415     }
5416     return value;
5417 }
5418 
5419 static const ARMCPRegInfo el2_cp_reginfo[] = {
5420     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5421       .type = ARM_CP_IO,
5422       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5423       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5424       .writefn = hcr_write },
5425     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5426       .type = ARM_CP_ALIAS | ARM_CP_IO,
5427       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5428       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5429       .writefn = hcr_writelow },
5430     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5431       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5432       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5433     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5434       .type = ARM_CP_ALIAS,
5435       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5436       .access = PL2_RW,
5437       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5438     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5439       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5440       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5441     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5442       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5443       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5444     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5445       .type = ARM_CP_ALIAS,
5446       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5447       .access = PL2_RW,
5448       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5449     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5450       .type = ARM_CP_ALIAS,
5451       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5452       .access = PL2_RW,
5453       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5454     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5455       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5456       .access = PL2_RW, .writefn = vbar_write,
5457       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5458       .resetvalue = 0 },
5459     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5460       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5461       .access = PL3_RW, .type = ARM_CP_ALIAS,
5462       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5463     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5464       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5465       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5466       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5467       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5468     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5469       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5470       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5471       .resetvalue = 0 },
5472     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5473       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5474       .access = PL2_RW, .type = ARM_CP_ALIAS,
5475       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5476     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5477       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5478       .access = PL2_RW, .type = ARM_CP_CONST,
5479       .resetvalue = 0 },
5480     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5481     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5482       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5483       .access = PL2_RW, .type = ARM_CP_CONST,
5484       .resetvalue = 0 },
5485     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5486       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5487       .access = PL2_RW, .type = ARM_CP_CONST,
5488       .resetvalue = 0 },
5489     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5490       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5491       .access = PL2_RW, .type = ARM_CP_CONST,
5492       .resetvalue = 0 },
5493     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5494       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5495       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5496       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5497     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5498       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5499       .type = ARM_CP_ALIAS,
5500       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5501       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5502     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5503       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5504       .access = PL2_RW,
5505       /* no .writefn needed as this can't cause an ASID change */
5506       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5507     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5508       .cp = 15, .opc1 = 6, .crm = 2,
5509       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5510       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5511       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5512       .writefn = vttbr_write },
5513     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5514       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5515       .access = PL2_RW, .writefn = vttbr_write,
5516       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5517     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5518       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5519       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5520       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5521     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5522       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5523       .access = PL2_RW, .resetvalue = 0,
5524       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5525     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5526       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5527       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5528       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5529     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5530       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5531       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5532     { .name = "TLBIALLNSNH",
5533       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5534       .type = ARM_CP_NO_RAW, .access = PL2_W,
5535       .writefn = tlbiall_nsnh_write },
5536     { .name = "TLBIALLNSNHIS",
5537       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5538       .type = ARM_CP_NO_RAW, .access = PL2_W,
5539       .writefn = tlbiall_nsnh_is_write },
5540     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5541       .type = ARM_CP_NO_RAW, .access = PL2_W,
5542       .writefn = tlbiall_hyp_write },
5543     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5544       .type = ARM_CP_NO_RAW, .access = PL2_W,
5545       .writefn = tlbiall_hyp_is_write },
5546     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5547       .type = ARM_CP_NO_RAW, .access = PL2_W,
5548       .writefn = tlbimva_hyp_write },
5549     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5550       .type = ARM_CP_NO_RAW, .access = PL2_W,
5551       .writefn = tlbimva_hyp_is_write },
5552     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5553       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5554       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5555       .writefn = tlbi_aa64_alle2_write },
5556     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5557       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5558       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5559       .writefn = tlbi_aa64_vae2_write },
5560     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5561       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5562       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5563       .writefn = tlbi_aa64_vae2_write },
5564     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5565       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5566       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5567       .writefn = tlbi_aa64_alle2is_write },
5568     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5569       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5570       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5571       .writefn = tlbi_aa64_vae2is_write },
5572     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5573       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5574       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5575       .writefn = tlbi_aa64_vae2is_write },
5576 #ifndef CONFIG_USER_ONLY
5577     /* Unlike the other EL2-related AT operations, these must
5578      * UNDEF from EL3 if EL2 is not implemented, which is why we
5579      * define them here rather than with the rest of the AT ops.
5580      */
5581     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5582       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5583       .access = PL2_W, .accessfn = at_s1e2_access,
5584       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5585       .writefn = ats_write64 },
5586     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5587       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5588       .access = PL2_W, .accessfn = at_s1e2_access,
5589       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5590       .writefn = ats_write64 },
5591     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5592      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5593      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5594      * to behave as if SCR.NS was 1.
5595      */
5596     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5597       .access = PL2_W,
5598       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5599     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5600       .access = PL2_W,
5601       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5602     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5603       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5604       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5605        * reset values as IMPDEF. We choose to reset to 3 to comply with
5606        * both ARMv7 and ARMv8.
5607        */
5608       .access = PL2_RW, .resetvalue = 3,
5609       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5610     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5611       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5612       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5613       .writefn = gt_cntvoff_write,
5614       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5615     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5616       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5617       .writefn = gt_cntvoff_write,
5618       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5619     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5620       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5621       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5622       .type = ARM_CP_IO, .access = PL2_RW,
5623       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5624     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5625       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5626       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5627       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5628     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5629       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5630       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5631       .resetfn = gt_hyp_timer_reset,
5632       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5633     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5634       .type = ARM_CP_IO,
5635       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5636       .access = PL2_RW,
5637       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5638       .resetvalue = 0,
5639       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5640 #endif
5641     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5642       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5643       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5644       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5645     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5646       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5647       .access = PL2_RW,
5648       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5649     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5650       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5651       .access = PL2_RW,
5652       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5653 };
5654 
5655 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5656     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5657       .type = ARM_CP_ALIAS | ARM_CP_IO,
5658       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5659       .access = PL2_RW,
5660       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5661       .writefn = hcr_writehigh },
5662 };
5663 
5664 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5665                                   bool isread)
5666 {
5667     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5668         return CP_ACCESS_OK;
5669     }
5670     return CP_ACCESS_TRAP_UNCATEGORIZED;
5671 }
5672 
5673 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5674     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5675       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5676       .access = PL2_RW, .accessfn = sel2_access,
5677       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5678     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5679       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5680       .access = PL2_RW, .accessfn = sel2_access,
5681       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5682 };
5683 
5684 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5685                                    bool isread)
5686 {
5687     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5688      * At Secure EL1 it traps to EL3 or EL2.
5689      */
5690     if (arm_current_el(env) == 3) {
5691         return CP_ACCESS_OK;
5692     }
5693     if (arm_is_secure_below_el3(env)) {
5694         if (env->cp15.scr_el3 & SCR_EEL2) {
5695             return CP_ACCESS_TRAP_EL2;
5696         }
5697         return CP_ACCESS_TRAP_EL3;
5698     }
5699     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5700     if (isread) {
5701         return CP_ACCESS_OK;
5702     }
5703     return CP_ACCESS_TRAP_UNCATEGORIZED;
5704 }
5705 
5706 static const ARMCPRegInfo el3_cp_reginfo[] = {
5707     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5708       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5709       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5710       .resetfn = scr_reset, .writefn = scr_write },
5711     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5712       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5713       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5714       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5715       .writefn = scr_write },
5716     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5717       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5718       .access = PL3_RW, .resetvalue = 0,
5719       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5720     { .name = "SDER",
5721       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5722       .access = PL3_RW, .resetvalue = 0,
5723       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5724     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5725       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5726       .writefn = vbar_write, .resetvalue = 0,
5727       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5728     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5729       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5730       .access = PL3_RW, .resetvalue = 0,
5731       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5732     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5733       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5734       .access = PL3_RW,
5735       /* no .writefn needed as this can't cause an ASID change */
5736       .resetvalue = 0,
5737       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5738     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5739       .type = ARM_CP_ALIAS,
5740       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5741       .access = PL3_RW,
5742       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5743     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5744       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5745       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5746     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5747       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5748       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5749     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5750       .type = ARM_CP_ALIAS,
5751       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5752       .access = PL3_RW,
5753       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5754     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5755       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5756       .access = PL3_RW, .writefn = vbar_write,
5757       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5758       .resetvalue = 0 },
5759     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5760       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5761       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5762       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5763     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5764       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5765       .access = PL3_RW, .resetvalue = 0,
5766       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5767     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5768       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5769       .access = PL3_RW, .type = ARM_CP_CONST,
5770       .resetvalue = 0 },
5771     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5772       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5773       .access = PL3_RW, .type = ARM_CP_CONST,
5774       .resetvalue = 0 },
5775     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5776       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5777       .access = PL3_RW, .type = ARM_CP_CONST,
5778       .resetvalue = 0 },
5779     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5780       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5781       .access = PL3_W, .type = ARM_CP_NO_RAW,
5782       .writefn = tlbi_aa64_alle3is_write },
5783     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5784       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5785       .access = PL3_W, .type = ARM_CP_NO_RAW,
5786       .writefn = tlbi_aa64_vae3is_write },
5787     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5788       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5789       .access = PL3_W, .type = ARM_CP_NO_RAW,
5790       .writefn = tlbi_aa64_vae3is_write },
5791     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5792       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5793       .access = PL3_W, .type = ARM_CP_NO_RAW,
5794       .writefn = tlbi_aa64_alle3_write },
5795     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5796       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5797       .access = PL3_W, .type = ARM_CP_NO_RAW,
5798       .writefn = tlbi_aa64_vae3_write },
5799     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5800       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5801       .access = PL3_W, .type = ARM_CP_NO_RAW,
5802       .writefn = tlbi_aa64_vae3_write },
5803 };
5804 
5805 #ifndef CONFIG_USER_ONLY
5806 /* Test if system register redirection is to occur in the current state.  */
5807 static bool redirect_for_e2h(CPUARMState *env)
5808 {
5809     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5810 }
5811 
5812 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5813 {
5814     CPReadFn *readfn;
5815 
5816     if (redirect_for_e2h(env)) {
5817         /* Switch to the saved EL2 version of the register.  */
5818         ri = ri->opaque;
5819         readfn = ri->readfn;
5820     } else {
5821         readfn = ri->orig_readfn;
5822     }
5823     if (readfn == NULL) {
5824         readfn = raw_read;
5825     }
5826     return readfn(env, ri);
5827 }
5828 
5829 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5830                           uint64_t value)
5831 {
5832     CPWriteFn *writefn;
5833 
5834     if (redirect_for_e2h(env)) {
5835         /* Switch to the saved EL2 version of the register.  */
5836         ri = ri->opaque;
5837         writefn = ri->writefn;
5838     } else {
5839         writefn = ri->orig_writefn;
5840     }
5841     if (writefn == NULL) {
5842         writefn = raw_write;
5843     }
5844     writefn(env, ri, value);
5845 }
5846 
5847 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5848 {
5849     struct E2HAlias {
5850         uint32_t src_key, dst_key, new_key;
5851         const char *src_name, *dst_name, *new_name;
5852         bool (*feature)(const ARMISARegisters *id);
5853     };
5854 
5855 #define K(op0, op1, crn, crm, op2) \
5856     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5857 
5858     static const struct E2HAlias aliases[] = {
5859         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5860           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5861         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5862           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5863         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5864           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5865         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5866           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5867         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5868           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5869         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5870           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5871         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5872           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5873         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5874           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5875         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5876           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5877         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5878           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5879         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5880           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5881         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5882           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5883         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5884           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5885         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5886           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5887         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5888           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5889         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5890           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5891 
5892         /*
5893          * Note that redirection of ZCR is mentioned in the description
5894          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5895          * not in the summary table.
5896          */
5897         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5898           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5899         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5900           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5901 
5902         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5903           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5904 
5905         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5906           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5907           isar_feature_aa64_scxtnum },
5908 
5909         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5910         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5911     };
5912 #undef K
5913 
5914     size_t i;
5915 
5916     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5917         const struct E2HAlias *a = &aliases[i];
5918         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5919         bool ok;
5920 
5921         if (a->feature && !a->feature(&cpu->isar)) {
5922             continue;
5923         }
5924 
5925         src_reg = g_hash_table_lookup(cpu->cp_regs,
5926                                       (gpointer)(uintptr_t)a->src_key);
5927         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5928                                       (gpointer)(uintptr_t)a->dst_key);
5929         g_assert(src_reg != NULL);
5930         g_assert(dst_reg != NULL);
5931 
5932         /* Cross-compare names to detect typos in the keys.  */
5933         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5934         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5935 
5936         /* None of the core system registers use opaque; we will.  */
5937         g_assert(src_reg->opaque == NULL);
5938 
5939         /* Create alias before redirection so we dup the right data. */
5940         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5941 
5942         new_reg->name = a->new_name;
5943         new_reg->type |= ARM_CP_ALIAS;
5944         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5945         new_reg->access &= PL2_RW | PL3_RW;
5946 
5947         ok = g_hash_table_insert(cpu->cp_regs,
5948                                  (gpointer)(uintptr_t)a->new_key, new_reg);
5949         g_assert(ok);
5950 
5951         src_reg->opaque = dst_reg;
5952         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5953         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5954         if (!src_reg->raw_readfn) {
5955             src_reg->raw_readfn = raw_read;
5956         }
5957         if (!src_reg->raw_writefn) {
5958             src_reg->raw_writefn = raw_write;
5959         }
5960         src_reg->readfn = el2_e2h_read;
5961         src_reg->writefn = el2_e2h_write;
5962     }
5963 }
5964 #endif
5965 
5966 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5967                                      bool isread)
5968 {
5969     int cur_el = arm_current_el(env);
5970 
5971     if (cur_el < 2) {
5972         uint64_t hcr = arm_hcr_el2_eff(env);
5973 
5974         if (cur_el == 0) {
5975             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5976                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5977                     return CP_ACCESS_TRAP_EL2;
5978                 }
5979             } else {
5980                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5981                     return CP_ACCESS_TRAP;
5982                 }
5983                 if (hcr & HCR_TID2) {
5984                     return CP_ACCESS_TRAP_EL2;
5985                 }
5986             }
5987         } else if (hcr & HCR_TID2) {
5988             return CP_ACCESS_TRAP_EL2;
5989         }
5990     }
5991 
5992     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5993         return CP_ACCESS_TRAP_EL2;
5994     }
5995 
5996     return CP_ACCESS_OK;
5997 }
5998 
5999 /*
6000  * Check for traps to RAS registers, which are controlled
6001  * by HCR_EL2.TERR and SCR_EL3.TERR.
6002  */
6003 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6004                                   bool isread)
6005 {
6006     int el = arm_current_el(env);
6007 
6008     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6009         return CP_ACCESS_TRAP_EL2;
6010     }
6011     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6012         return CP_ACCESS_TRAP_EL3;
6013     }
6014     return CP_ACCESS_OK;
6015 }
6016 
6017 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6018 {
6019     int el = arm_current_el(env);
6020 
6021     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6022         return env->cp15.vdisr_el2;
6023     }
6024     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6025         return 0; /* RAZ/WI */
6026     }
6027     return env->cp15.disr_el1;
6028 }
6029 
6030 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6031 {
6032     int el = arm_current_el(env);
6033 
6034     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6035         env->cp15.vdisr_el2 = val;
6036         return;
6037     }
6038     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6039         return; /* RAZ/WI */
6040     }
6041     env->cp15.disr_el1 = val;
6042 }
6043 
6044 /*
6045  * Minimal RAS implementation with no Error Records.
6046  * Which means that all of the Error Record registers:
6047  *   ERXADDR_EL1
6048  *   ERXCTLR_EL1
6049  *   ERXFR_EL1
6050  *   ERXMISC0_EL1
6051  *   ERXMISC1_EL1
6052  *   ERXMISC2_EL1
6053  *   ERXMISC3_EL1
6054  *   ERXPFGCDN_EL1  (RASv1p1)
6055  *   ERXPFGCTL_EL1  (RASv1p1)
6056  *   ERXPFGF_EL1    (RASv1p1)
6057  *   ERXSTATUS_EL1
6058  * and
6059  *   ERRSELR_EL1
6060  * may generate UNDEFINED, which is the effect we get by not
6061  * listing them at all.
6062  */
6063 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6064     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6065       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6066       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6067       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6068     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6069       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6070       .access = PL1_R, .accessfn = access_terr,
6071       .type = ARM_CP_CONST, .resetvalue = 0 },
6072     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6073       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6074       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6075     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6076       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6077       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6078 };
6079 
6080 /*
6081  * Return the exception level to which exceptions should be taken
6082  * via SVEAccessTrap.  This excludes the check for whether the exception
6083  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6084  * be found by testing 0 < fp_exception_el < sve_exception_el.
6085  *
6086  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6087  * pseudocode does *not* separate out the FP trap checks, but has them
6088  * all in one function.
6089  */
6090 int sve_exception_el(CPUARMState *env, int el)
6091 {
6092 #ifndef CONFIG_USER_ONLY
6093     if (el <= 1 && !el_is_in_host(env, el)) {
6094         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6095         case 1:
6096             if (el != 0) {
6097                 break;
6098             }
6099             /* fall through */
6100         case 0:
6101         case 2:
6102             return 1;
6103         }
6104     }
6105 
6106     if (el <= 2 && arm_is_el2_enabled(env)) {
6107         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6108         if (env->cp15.hcr_el2 & HCR_E2H) {
6109             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6110             case 1:
6111                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6112                     break;
6113                 }
6114                 /* fall through */
6115             case 0:
6116             case 2:
6117                 return 2;
6118             }
6119         } else {
6120             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6121                 return 2;
6122             }
6123         }
6124     }
6125 
6126     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6127     if (arm_feature(env, ARM_FEATURE_EL3)
6128         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6129         return 3;
6130     }
6131 #endif
6132     return 0;
6133 }
6134 
6135 /*
6136  * Return the exception level to which exceptions should be taken for SME.
6137  * C.f. the ARM pseudocode function CheckSMEAccess.
6138  */
6139 int sme_exception_el(CPUARMState *env, int el)
6140 {
6141 #ifndef CONFIG_USER_ONLY
6142     if (el <= 1 && !el_is_in_host(env, el)) {
6143         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6144         case 1:
6145             if (el != 0) {
6146                 break;
6147             }
6148             /* fall through */
6149         case 0:
6150         case 2:
6151             return 1;
6152         }
6153     }
6154 
6155     if (el <= 2 && arm_is_el2_enabled(env)) {
6156         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6157         if (env->cp15.hcr_el2 & HCR_E2H) {
6158             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6159             case 1:
6160                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6161                     break;
6162                 }
6163                 /* fall through */
6164             case 0:
6165             case 2:
6166                 return 2;
6167             }
6168         } else {
6169             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6170                 return 2;
6171             }
6172         }
6173     }
6174 
6175     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6176     if (arm_feature(env, ARM_FEATURE_EL3)
6177         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6178         return 3;
6179     }
6180 #endif
6181     return 0;
6182 }
6183 
6184 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6185 static bool sme_fa64(CPUARMState *env, int el)
6186 {
6187     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6188         return false;
6189     }
6190 
6191     if (el <= 1 && !el_is_in_host(env, el)) {
6192         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6193             return false;
6194         }
6195     }
6196     if (el <= 2 && arm_is_el2_enabled(env)) {
6197         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6198             return false;
6199         }
6200     }
6201     if (arm_feature(env, ARM_FEATURE_EL3)) {
6202         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6203             return false;
6204         }
6205     }
6206 
6207     return true;
6208 }
6209 
6210 /*
6211  * Given that SVE is enabled, return the vector length for EL.
6212  */
6213 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6214 {
6215     ARMCPU *cpu = env_archcpu(env);
6216     uint64_t *cr = env->vfp.zcr_el;
6217     uint32_t map = cpu->sve_vq.map;
6218     uint32_t len = ARM_MAX_VQ - 1;
6219 
6220     if (sm) {
6221         cr = env->vfp.smcr_el;
6222         map = cpu->sme_vq.map;
6223     }
6224 
6225     if (el <= 1 && !el_is_in_host(env, el)) {
6226         len = MIN(len, 0xf & (uint32_t)cr[1]);
6227     }
6228     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6229         len = MIN(len, 0xf & (uint32_t)cr[2]);
6230     }
6231     if (arm_feature(env, ARM_FEATURE_EL3)) {
6232         len = MIN(len, 0xf & (uint32_t)cr[3]);
6233     }
6234 
6235     map &= MAKE_64BIT_MASK(0, len + 1);
6236     if (map != 0) {
6237         return 31 - clz32(map);
6238     }
6239 
6240     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6241     assert(sm);
6242     return ctz32(cpu->sme_vq.map);
6243 }
6244 
6245 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6246 {
6247     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6248 }
6249 
6250 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6251                       uint64_t value)
6252 {
6253     int cur_el = arm_current_el(env);
6254     int old_len = sve_vqm1_for_el(env, cur_el);
6255     int new_len;
6256 
6257     /* Bits other than [3:0] are RAZ/WI.  */
6258     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6259     raw_write(env, ri, value & 0xf);
6260 
6261     /*
6262      * Because we arrived here, we know both FP and SVE are enabled;
6263      * otherwise we would have trapped access to the ZCR_ELn register.
6264      */
6265     new_len = sve_vqm1_for_el(env, cur_el);
6266     if (new_len < old_len) {
6267         aarch64_sve_narrow_vq(env, new_len + 1);
6268     }
6269 }
6270 
6271 static const ARMCPRegInfo zcr_reginfo[] = {
6272     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6273       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6274       .access = PL1_RW, .type = ARM_CP_SVE,
6275       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6276       .writefn = zcr_write, .raw_writefn = raw_write },
6277     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6278       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6279       .access = PL2_RW, .type = ARM_CP_SVE,
6280       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6281       .writefn = zcr_write, .raw_writefn = raw_write },
6282     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6283       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6284       .access = PL3_RW, .type = ARM_CP_SVE,
6285       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6286       .writefn = zcr_write, .raw_writefn = raw_write },
6287 };
6288 
6289 #ifdef TARGET_AARCH64
6290 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6291                                     bool isread)
6292 {
6293     int el = arm_current_el(env);
6294 
6295     if (el == 0) {
6296         uint64_t sctlr = arm_sctlr(env, el);
6297         if (!(sctlr & SCTLR_EnTP2)) {
6298             return CP_ACCESS_TRAP;
6299         }
6300     }
6301     /* TODO: FEAT_FGT */
6302     if (el < 3
6303         && arm_feature(env, ARM_FEATURE_EL3)
6304         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6305         return CP_ACCESS_TRAP_EL3;
6306     }
6307     return CP_ACCESS_OK;
6308 }
6309 
6310 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6311                                  bool isread)
6312 {
6313     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6314     if (arm_current_el(env) < 3
6315         && arm_feature(env, ARM_FEATURE_EL3)
6316         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6317         return CP_ACCESS_TRAP_EL3;
6318     }
6319     return CP_ACCESS_OK;
6320 }
6321 
6322 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6323                        uint64_t value)
6324 {
6325     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6326     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6327     arm_rebuild_hflags(env);
6328 }
6329 
6330 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6331                        uint64_t value)
6332 {
6333     int cur_el = arm_current_el(env);
6334     int old_len = sve_vqm1_for_el(env, cur_el);
6335     int new_len;
6336 
6337     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6338     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6339     raw_write(env, ri, value);
6340 
6341     /*
6342      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6343      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6344      * current values for simplicity.  But for QEMU internals, we must still
6345      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6346      * above aarch64_sve_narrow_vq.
6347      */
6348     new_len = sve_vqm1_for_el(env, cur_el);
6349     if (new_len < old_len) {
6350         aarch64_sve_narrow_vq(env, new_len + 1);
6351     }
6352 }
6353 
6354 static const ARMCPRegInfo sme_reginfo[] = {
6355     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6356       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6357       .access = PL0_RW, .accessfn = access_tpidr2,
6358       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6359     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6360       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6361       .access = PL0_RW, .type = ARM_CP_SME,
6362       .fieldoffset = offsetof(CPUARMState, svcr),
6363       .writefn = svcr_write, .raw_writefn = raw_write },
6364     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6365       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6366       .access = PL1_RW, .type = ARM_CP_SME,
6367       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6368       .writefn = smcr_write, .raw_writefn = raw_write },
6369     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6370       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6371       .access = PL2_RW, .type = ARM_CP_SME,
6372       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6373       .writefn = smcr_write, .raw_writefn = raw_write },
6374     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6375       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6376       .access = PL3_RW, .type = ARM_CP_SME,
6377       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6378       .writefn = smcr_write, .raw_writefn = raw_write },
6379     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6380       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6381       .access = PL1_R, .accessfn = access_aa64_tid1,
6382       /*
6383        * IMPLEMENTOR = 0 (software)
6384        * REVISION    = 0 (implementation defined)
6385        * SMPS        = 0 (no streaming execution priority in QEMU)
6386        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6387        */
6388       .type = ARM_CP_CONST, .resetvalue = 0, },
6389     /*
6390      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6391      */
6392     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6393       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6394       .access = PL1_RW, .accessfn = access_esm,
6395       .type = ARM_CP_CONST, .resetvalue = 0 },
6396     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6397       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6398       .access = PL2_RW, .accessfn = access_esm,
6399       .type = ARM_CP_CONST, .resetvalue = 0 },
6400 };
6401 #endif /* TARGET_AARCH64 */
6402 
6403 static void define_pmu_regs(ARMCPU *cpu)
6404 {
6405     /*
6406      * v7 performance monitor control register: same implementor
6407      * field as main ID register, and we implement four counters in
6408      * addition to the cycle count register.
6409      */
6410     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6411     ARMCPRegInfo pmcr = {
6412         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6413         .access = PL0_RW,
6414         .type = ARM_CP_IO | ARM_CP_ALIAS,
6415         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6416         .accessfn = pmreg_access, .writefn = pmcr_write,
6417         .raw_writefn = raw_write,
6418     };
6419     ARMCPRegInfo pmcr64 = {
6420         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6421         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6422         .access = PL0_RW, .accessfn = pmreg_access,
6423         .type = ARM_CP_IO,
6424         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6425         .resetvalue = cpu->isar.reset_pmcr_el0,
6426         .writefn = pmcr_write, .raw_writefn = raw_write,
6427     };
6428 
6429     define_one_arm_cp_reg(cpu, &pmcr);
6430     define_one_arm_cp_reg(cpu, &pmcr64);
6431     for (i = 0; i < pmcrn; i++) {
6432         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6433         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6434         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6435         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6436         ARMCPRegInfo pmev_regs[] = {
6437             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6438               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6439               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6440               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6441               .accessfn = pmreg_access_xevcntr },
6442             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6443               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6444               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6445               .type = ARM_CP_IO,
6446               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6447               .raw_readfn = pmevcntr_rawread,
6448               .raw_writefn = pmevcntr_rawwrite },
6449             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6450               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6451               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6452               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6453               .accessfn = pmreg_access },
6454             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6455               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6456               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6457               .type = ARM_CP_IO,
6458               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6459               .raw_writefn = pmevtyper_rawwrite },
6460         };
6461         define_arm_cp_regs(cpu, pmev_regs);
6462         g_free(pmevcntr_name);
6463         g_free(pmevcntr_el0_name);
6464         g_free(pmevtyper_name);
6465         g_free(pmevtyper_el0_name);
6466     }
6467     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6468         ARMCPRegInfo v81_pmu_regs[] = {
6469             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6470               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6471               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6472               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6473             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6474               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6475               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6476               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6477         };
6478         define_arm_cp_regs(cpu, v81_pmu_regs);
6479     }
6480     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6481         static const ARMCPRegInfo v84_pmmir = {
6482             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6483             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6484             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6485             .resetvalue = 0
6486         };
6487         define_one_arm_cp_reg(cpu, &v84_pmmir);
6488     }
6489 }
6490 
6491 /* We don't know until after realize whether there's a GICv3
6492  * attached, and that is what registers the gicv3 sysregs.
6493  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6494  * at runtime.
6495  */
6496 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6497 {
6498     ARMCPU *cpu = env_archcpu(env);
6499     uint64_t pfr1 = cpu->isar.id_pfr1;
6500 
6501     if (env->gicv3state) {
6502         pfr1 |= 1 << 28;
6503     }
6504     return pfr1;
6505 }
6506 
6507 #ifndef CONFIG_USER_ONLY
6508 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6509 {
6510     ARMCPU *cpu = env_archcpu(env);
6511     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6512 
6513     if (env->gicv3state) {
6514         pfr0 |= 1 << 24;
6515     }
6516     return pfr0;
6517 }
6518 #endif
6519 
6520 /* Shared logic between LORID and the rest of the LOR* registers.
6521  * Secure state exclusion has already been dealt with.
6522  */
6523 static CPAccessResult access_lor_ns(CPUARMState *env,
6524                                     const ARMCPRegInfo *ri, bool isread)
6525 {
6526     int el = arm_current_el(env);
6527 
6528     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6529         return CP_ACCESS_TRAP_EL2;
6530     }
6531     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6532         return CP_ACCESS_TRAP_EL3;
6533     }
6534     return CP_ACCESS_OK;
6535 }
6536 
6537 static CPAccessResult access_lor_other(CPUARMState *env,
6538                                        const ARMCPRegInfo *ri, bool isread)
6539 {
6540     if (arm_is_secure_below_el3(env)) {
6541         /* Access denied in secure mode.  */
6542         return CP_ACCESS_TRAP;
6543     }
6544     return access_lor_ns(env, ri, isread);
6545 }
6546 
6547 /*
6548  * A trivial implementation of ARMv8.1-LOR leaves all of these
6549  * registers fixed at 0, which indicates that there are zero
6550  * supported Limited Ordering regions.
6551  */
6552 static const ARMCPRegInfo lor_reginfo[] = {
6553     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6554       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6555       .access = PL1_RW, .accessfn = access_lor_other,
6556       .type = ARM_CP_CONST, .resetvalue = 0 },
6557     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6558       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6559       .access = PL1_RW, .accessfn = access_lor_other,
6560       .type = ARM_CP_CONST, .resetvalue = 0 },
6561     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6562       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6563       .access = PL1_RW, .accessfn = access_lor_other,
6564       .type = ARM_CP_CONST, .resetvalue = 0 },
6565     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6566       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6567       .access = PL1_RW, .accessfn = access_lor_other,
6568       .type = ARM_CP_CONST, .resetvalue = 0 },
6569     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6570       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6571       .access = PL1_R, .accessfn = access_lor_ns,
6572       .type = ARM_CP_CONST, .resetvalue = 0 },
6573 };
6574 
6575 #ifdef TARGET_AARCH64
6576 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6577                                    bool isread)
6578 {
6579     int el = arm_current_el(env);
6580 
6581     if (el < 2 &&
6582         arm_is_el2_enabled(env) &&
6583         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6584         return CP_ACCESS_TRAP_EL2;
6585     }
6586     if (el < 3 &&
6587         arm_feature(env, ARM_FEATURE_EL3) &&
6588         !(env->cp15.scr_el3 & SCR_APK)) {
6589         return CP_ACCESS_TRAP_EL3;
6590     }
6591     return CP_ACCESS_OK;
6592 }
6593 
6594 static const ARMCPRegInfo pauth_reginfo[] = {
6595     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6596       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6597       .access = PL1_RW, .accessfn = access_pauth,
6598       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6599     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6600       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6601       .access = PL1_RW, .accessfn = access_pauth,
6602       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6603     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6604       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6605       .access = PL1_RW, .accessfn = access_pauth,
6606       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6607     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6608       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6609       .access = PL1_RW, .accessfn = access_pauth,
6610       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6611     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6612       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6613       .access = PL1_RW, .accessfn = access_pauth,
6614       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6615     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6616       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6617       .access = PL1_RW, .accessfn = access_pauth,
6618       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6619     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6620       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6621       .access = PL1_RW, .accessfn = access_pauth,
6622       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6623     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6624       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6625       .access = PL1_RW, .accessfn = access_pauth,
6626       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6627     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6628       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6629       .access = PL1_RW, .accessfn = access_pauth,
6630       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6631     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6632       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6633       .access = PL1_RW, .accessfn = access_pauth,
6634       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6635 };
6636 
6637 static const ARMCPRegInfo tlbirange_reginfo[] = {
6638     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6639       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6640       .access = PL1_W, .type = ARM_CP_NO_RAW,
6641       .writefn = tlbi_aa64_rvae1is_write },
6642     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6643       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6644       .access = PL1_W, .type = ARM_CP_NO_RAW,
6645       .writefn = tlbi_aa64_rvae1is_write },
6646    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6647       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6648       .access = PL1_W, .type = ARM_CP_NO_RAW,
6649       .writefn = tlbi_aa64_rvae1is_write },
6650     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6651       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6652       .access = PL1_W, .type = ARM_CP_NO_RAW,
6653       .writefn = tlbi_aa64_rvae1is_write },
6654     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6655       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6656       .access = PL1_W, .type = ARM_CP_NO_RAW,
6657       .writefn = tlbi_aa64_rvae1is_write },
6658     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6659       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6660       .access = PL1_W, .type = ARM_CP_NO_RAW,
6661       .writefn = tlbi_aa64_rvae1is_write },
6662    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6663       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6664       .access = PL1_W, .type = ARM_CP_NO_RAW,
6665       .writefn = tlbi_aa64_rvae1is_write },
6666     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6667       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6668       .access = PL1_W, .type = ARM_CP_NO_RAW,
6669       .writefn = tlbi_aa64_rvae1is_write },
6670     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6671       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6672       .access = PL1_W, .type = ARM_CP_NO_RAW,
6673       .writefn = tlbi_aa64_rvae1_write },
6674     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6675       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6676       .access = PL1_W, .type = ARM_CP_NO_RAW,
6677       .writefn = tlbi_aa64_rvae1_write },
6678    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6679       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6680       .access = PL1_W, .type = ARM_CP_NO_RAW,
6681       .writefn = tlbi_aa64_rvae1_write },
6682     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6683       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6684       .access = PL1_W, .type = ARM_CP_NO_RAW,
6685       .writefn = tlbi_aa64_rvae1_write },
6686     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6687       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6688       .access = PL2_W, .type = ARM_CP_NOP },
6689     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6690       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6691       .access = PL2_W, .type = ARM_CP_NOP },
6692     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6693       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6694       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6695       .writefn = tlbi_aa64_rvae2is_write },
6696    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6697       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6698       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6699       .writefn = tlbi_aa64_rvae2is_write },
6700     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6701       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6702       .access = PL2_W, .type = ARM_CP_NOP },
6703    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6704       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6705       .access = PL2_W, .type = ARM_CP_NOP },
6706    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6707       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6708       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6709       .writefn = tlbi_aa64_rvae2is_write },
6710    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6711       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6712       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6713       .writefn = tlbi_aa64_rvae2is_write },
6714     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6715       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6716       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6717       .writefn = tlbi_aa64_rvae2_write },
6718    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6719       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6720       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6721       .writefn = tlbi_aa64_rvae2_write },
6722    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6723       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6724       .access = PL3_W, .type = ARM_CP_NO_RAW,
6725       .writefn = tlbi_aa64_rvae3is_write },
6726    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6727       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6728       .access = PL3_W, .type = ARM_CP_NO_RAW,
6729       .writefn = tlbi_aa64_rvae3is_write },
6730    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6731       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6732       .access = PL3_W, .type = ARM_CP_NO_RAW,
6733       .writefn = tlbi_aa64_rvae3is_write },
6734    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6735       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6736       .access = PL3_W, .type = ARM_CP_NO_RAW,
6737       .writefn = tlbi_aa64_rvae3is_write },
6738    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6739       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6740       .access = PL3_W, .type = ARM_CP_NO_RAW,
6741       .writefn = tlbi_aa64_rvae3_write },
6742    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6743       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6744       .access = PL3_W, .type = ARM_CP_NO_RAW,
6745       .writefn = tlbi_aa64_rvae3_write },
6746 };
6747 
6748 static const ARMCPRegInfo tlbios_reginfo[] = {
6749     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6750       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6751       .access = PL1_W, .type = ARM_CP_NO_RAW,
6752       .writefn = tlbi_aa64_vmalle1is_write },
6753     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6754       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6755       .access = PL1_W, .type = ARM_CP_NO_RAW,
6756       .writefn = tlbi_aa64_vae1is_write },
6757     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6758       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6759       .access = PL1_W, .type = ARM_CP_NO_RAW,
6760       .writefn = tlbi_aa64_vmalle1is_write },
6761     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6762       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6763       .access = PL1_W, .type = ARM_CP_NO_RAW,
6764       .writefn = tlbi_aa64_vae1is_write },
6765     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6766       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6767       .access = PL1_W, .type = ARM_CP_NO_RAW,
6768       .writefn = tlbi_aa64_vae1is_write },
6769     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6770       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6771       .access = PL1_W, .type = ARM_CP_NO_RAW,
6772       .writefn = tlbi_aa64_vae1is_write },
6773     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6774       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6775       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6776       .writefn = tlbi_aa64_alle2is_write },
6777     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6778       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6779       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6780       .writefn = tlbi_aa64_vae2is_write },
6781    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6782       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6783       .access = PL2_W, .type = ARM_CP_NO_RAW,
6784       .writefn = tlbi_aa64_alle1is_write },
6785     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6786       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6787       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6788       .writefn = tlbi_aa64_vae2is_write },
6789     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6790       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6791       .access = PL2_W, .type = ARM_CP_NO_RAW,
6792       .writefn = tlbi_aa64_alle1is_write },
6793     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6794       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6795       .access = PL2_W, .type = ARM_CP_NOP },
6796     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6797       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6798       .access = PL2_W, .type = ARM_CP_NOP },
6799     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6800       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6801       .access = PL2_W, .type = ARM_CP_NOP },
6802     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6803       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6804       .access = PL2_W, .type = ARM_CP_NOP },
6805     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6806       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6807       .access = PL3_W, .type = ARM_CP_NO_RAW,
6808       .writefn = tlbi_aa64_alle3is_write },
6809     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6810       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6811       .access = PL3_W, .type = ARM_CP_NO_RAW,
6812       .writefn = tlbi_aa64_vae3is_write },
6813     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6814       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6815       .access = PL3_W, .type = ARM_CP_NO_RAW,
6816       .writefn = tlbi_aa64_vae3is_write },
6817 };
6818 
6819 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6820 {
6821     Error *err = NULL;
6822     uint64_t ret;
6823 
6824     /* Success sets NZCV = 0000.  */
6825     env->NF = env->CF = env->VF = 0, env->ZF = 1;
6826 
6827     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6828         /*
6829          * ??? Failed, for unknown reasons in the crypto subsystem.
6830          * The best we can do is log the reason and return the
6831          * timed-out indication to the guest.  There is no reason
6832          * we know to expect this failure to be transitory, so the
6833          * guest may well hang retrying the operation.
6834          */
6835         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6836                       ri->name, error_get_pretty(err));
6837         error_free(err);
6838 
6839         env->ZF = 0; /* NZCF = 0100 */
6840         return 0;
6841     }
6842     return ret;
6843 }
6844 
6845 /* We do not support re-seeding, so the two registers operate the same.  */
6846 static const ARMCPRegInfo rndr_reginfo[] = {
6847     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6848       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6849       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6850       .access = PL0_R, .readfn = rndr_readfn },
6851     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6852       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6853       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6854       .access = PL0_R, .readfn = rndr_readfn },
6855 };
6856 
6857 #ifndef CONFIG_USER_ONLY
6858 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6859                           uint64_t value)
6860 {
6861     ARMCPU *cpu = env_archcpu(env);
6862     /* CTR_EL0 System register -> DminLine, bits [19:16] */
6863     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6864     uint64_t vaddr_in = (uint64_t) value;
6865     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6866     void *haddr;
6867     int mem_idx = cpu_mmu_index(env, false);
6868 
6869     /* This won't be crossing page boundaries */
6870     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6871     if (haddr) {
6872 
6873         ram_addr_t offset;
6874         MemoryRegion *mr;
6875 
6876         /* RCU lock is already being held */
6877         mr = memory_region_from_host(haddr, &offset);
6878 
6879         if (mr) {
6880             memory_region_writeback(mr, offset, dline_size);
6881         }
6882     }
6883 }
6884 
6885 static const ARMCPRegInfo dcpop_reg[] = {
6886     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6887       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6888       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6889       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6890 };
6891 
6892 static const ARMCPRegInfo dcpodp_reg[] = {
6893     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6894       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6895       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6896       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6897 };
6898 #endif /*CONFIG_USER_ONLY*/
6899 
6900 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6901                                        bool isread)
6902 {
6903     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6904         return CP_ACCESS_TRAP_EL2;
6905     }
6906 
6907     return CP_ACCESS_OK;
6908 }
6909 
6910 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6911                                  bool isread)
6912 {
6913     int el = arm_current_el(env);
6914 
6915     if (el < 2 && arm_is_el2_enabled(env)) {
6916         uint64_t hcr = arm_hcr_el2_eff(env);
6917         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6918             return CP_ACCESS_TRAP_EL2;
6919         }
6920     }
6921     if (el < 3 &&
6922         arm_feature(env, ARM_FEATURE_EL3) &&
6923         !(env->cp15.scr_el3 & SCR_ATA)) {
6924         return CP_ACCESS_TRAP_EL3;
6925     }
6926     return CP_ACCESS_OK;
6927 }
6928 
6929 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6930 {
6931     return env->pstate & PSTATE_TCO;
6932 }
6933 
6934 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6935 {
6936     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6937 }
6938 
6939 static const ARMCPRegInfo mte_reginfo[] = {
6940     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6941       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6942       .access = PL1_RW, .accessfn = access_mte,
6943       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6944     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6945       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6946       .access = PL1_RW, .accessfn = access_mte,
6947       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6948     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6949       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6950       .access = PL2_RW, .accessfn = access_mte,
6951       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6952     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6953       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6954       .access = PL3_RW,
6955       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6956     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6957       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6958       .access = PL1_RW, .accessfn = access_mte,
6959       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6960     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6961       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6962       .access = PL1_RW, .accessfn = access_mte,
6963       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6964     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6965       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6966       .access = PL1_R, .accessfn = access_aa64_tid5,
6967       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6968     { .name = "TCO", .state = ARM_CP_STATE_AA64,
6969       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6970       .type = ARM_CP_NO_RAW,
6971       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6972     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6973       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6974       .type = ARM_CP_NOP, .access = PL1_W,
6975       .accessfn = aa64_cacheop_poc_access },
6976     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6977       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6978       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6979     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6980       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6981       .type = ARM_CP_NOP, .access = PL1_W,
6982       .accessfn = aa64_cacheop_poc_access },
6983     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6984       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6985       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6986     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6987       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6988       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6989     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6990       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6991       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6992     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6993       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6994       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6995     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6996       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6997       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6998 };
6999 
7000 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7001     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7002       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7003       .type = ARM_CP_CONST, .access = PL0_RW, },
7004 };
7005 
7006 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7007     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7008       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7009       .type = ARM_CP_NOP, .access = PL0_W,
7010       .accessfn = aa64_cacheop_poc_access },
7011     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7012       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7013       .type = ARM_CP_NOP, .access = PL0_W,
7014       .accessfn = aa64_cacheop_poc_access },
7015     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7016       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7017       .type = ARM_CP_NOP, .access = PL0_W,
7018       .accessfn = aa64_cacheop_poc_access },
7019     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7020       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7021       .type = ARM_CP_NOP, .access = PL0_W,
7022       .accessfn = aa64_cacheop_poc_access },
7023     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7024       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7025       .type = ARM_CP_NOP, .access = PL0_W,
7026       .accessfn = aa64_cacheop_poc_access },
7027     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7028       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7029       .type = ARM_CP_NOP, .access = PL0_W,
7030       .accessfn = aa64_cacheop_poc_access },
7031     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7032       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7033       .type = ARM_CP_NOP, .access = PL0_W,
7034       .accessfn = aa64_cacheop_poc_access },
7035     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7036       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7037       .type = ARM_CP_NOP, .access = PL0_W,
7038       .accessfn = aa64_cacheop_poc_access },
7039     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7040       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7041       .access = PL0_W, .type = ARM_CP_DC_GVA,
7042 #ifndef CONFIG_USER_ONLY
7043       /* Avoid overhead of an access check that always passes in user-mode */
7044       .accessfn = aa64_zva_access,
7045 #endif
7046     },
7047     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7048       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7049       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7050 #ifndef CONFIG_USER_ONLY
7051       /* Avoid overhead of an access check that always passes in user-mode */
7052       .accessfn = aa64_zva_access,
7053 #endif
7054     },
7055 };
7056 
7057 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7058                                      bool isread)
7059 {
7060     uint64_t hcr = arm_hcr_el2_eff(env);
7061     int el = arm_current_el(env);
7062 
7063     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7064         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7065             if (hcr & HCR_TGE) {
7066                 return CP_ACCESS_TRAP_EL2;
7067             }
7068             return CP_ACCESS_TRAP;
7069         }
7070     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7071         return CP_ACCESS_TRAP_EL2;
7072     }
7073     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7074         return CP_ACCESS_TRAP_EL2;
7075     }
7076     if (el < 3
7077         && arm_feature(env, ARM_FEATURE_EL3)
7078         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7079         return CP_ACCESS_TRAP_EL3;
7080     }
7081     return CP_ACCESS_OK;
7082 }
7083 
7084 static const ARMCPRegInfo scxtnum_reginfo[] = {
7085     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7086       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7087       .access = PL0_RW, .accessfn = access_scxtnum,
7088       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7089     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7090       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7091       .access = PL1_RW, .accessfn = access_scxtnum,
7092       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7093     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7094       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7095       .access = PL2_RW, .accessfn = access_scxtnum,
7096       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7097     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7098       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7099       .access = PL3_RW,
7100       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7101 };
7102 #endif /* TARGET_AARCH64 */
7103 
7104 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7105                                      bool isread)
7106 {
7107     int el = arm_current_el(env);
7108 
7109     if (el == 0) {
7110         uint64_t sctlr = arm_sctlr(env, el);
7111         if (!(sctlr & SCTLR_EnRCTX)) {
7112             return CP_ACCESS_TRAP;
7113         }
7114     } else if (el == 1) {
7115         uint64_t hcr = arm_hcr_el2_eff(env);
7116         if (hcr & HCR_NV) {
7117             return CP_ACCESS_TRAP_EL2;
7118         }
7119     }
7120     return CP_ACCESS_OK;
7121 }
7122 
7123 static const ARMCPRegInfo predinv_reginfo[] = {
7124     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7125       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7126       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7127     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7129       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7130     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7131       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7132       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7133     /*
7134      * Note the AArch32 opcodes have a different OPC1.
7135      */
7136     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7137       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7138       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7139     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7140       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7141       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7142     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7143       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7144       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7145 };
7146 
7147 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7148 {
7149     /* Read the high 32 bits of the current CCSIDR */
7150     return extract64(ccsidr_read(env, ri), 32, 32);
7151 }
7152 
7153 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7154     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7155       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7156       .access = PL1_R,
7157       .accessfn = access_aa64_tid2,
7158       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7159 };
7160 
7161 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7162                                        bool isread)
7163 {
7164     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7165         return CP_ACCESS_TRAP_EL2;
7166     }
7167 
7168     return CP_ACCESS_OK;
7169 }
7170 
7171 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7172                                        bool isread)
7173 {
7174     if (arm_feature(env, ARM_FEATURE_V8)) {
7175         return access_aa64_tid3(env, ri, isread);
7176     }
7177 
7178     return CP_ACCESS_OK;
7179 }
7180 
7181 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7182                                      bool isread)
7183 {
7184     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7185         return CP_ACCESS_TRAP_EL2;
7186     }
7187 
7188     return CP_ACCESS_OK;
7189 }
7190 
7191 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7192                                         const ARMCPRegInfo *ri, bool isread)
7193 {
7194     /*
7195      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7196      * in v7A, not in v8A.
7197      */
7198     if (!arm_feature(env, ARM_FEATURE_V8) &&
7199         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7200         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7201         return CP_ACCESS_TRAP_EL2;
7202     }
7203     return CP_ACCESS_OK;
7204 }
7205 
7206 static const ARMCPRegInfo jazelle_regs[] = {
7207     { .name = "JIDR",
7208       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7209       .access = PL1_R, .accessfn = access_jazelle,
7210       .type = ARM_CP_CONST, .resetvalue = 0 },
7211     { .name = "JOSCR",
7212       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7213       .accessfn = access_joscr_jmcr,
7214       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7215     { .name = "JMCR",
7216       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7217       .accessfn = access_joscr_jmcr,
7218       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7219 };
7220 
7221 static const ARMCPRegInfo contextidr_el2 = {
7222     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7223     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7224     .access = PL2_RW,
7225     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7226 };
7227 
7228 static const ARMCPRegInfo vhe_reginfo[] = {
7229     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7230       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7231       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7232       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7233 #ifndef CONFIG_USER_ONLY
7234     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7235       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7236       .fieldoffset =
7237         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7238       .type = ARM_CP_IO, .access = PL2_RW,
7239       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7240     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7241       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7242       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7243       .resetfn = gt_hv_timer_reset,
7244       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7245     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7246       .type = ARM_CP_IO,
7247       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7248       .access = PL2_RW,
7249       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7250       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7251     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7252       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7253       .type = ARM_CP_IO | ARM_CP_ALIAS,
7254       .access = PL2_RW, .accessfn = e2h_access,
7255       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7256       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7257     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7258       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7259       .type = ARM_CP_IO | ARM_CP_ALIAS,
7260       .access = PL2_RW, .accessfn = e2h_access,
7261       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7262       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7263     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7264       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7265       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7266       .access = PL2_RW, .accessfn = e2h_access,
7267       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7268     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7269       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7270       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7271       .access = PL2_RW, .accessfn = e2h_access,
7272       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7273     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7274       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7275       .type = ARM_CP_IO | ARM_CP_ALIAS,
7276       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7277       .access = PL2_RW, .accessfn = e2h_access,
7278       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7279     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7280       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7281       .type = ARM_CP_IO | ARM_CP_ALIAS,
7282       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7283       .access = PL2_RW, .accessfn = e2h_access,
7284       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7285 #endif
7286 };
7287 
7288 #ifndef CONFIG_USER_ONLY
7289 static const ARMCPRegInfo ats1e1_reginfo[] = {
7290     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7291       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7292       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7293       .writefn = ats_write64 },
7294     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7295       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7296       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7297       .writefn = ats_write64 },
7298 };
7299 
7300 static const ARMCPRegInfo ats1cp_reginfo[] = {
7301     { .name = "ATS1CPRP",
7302       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7303       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7304       .writefn = ats_write },
7305     { .name = "ATS1CPWP",
7306       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7307       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7308       .writefn = ats_write },
7309 };
7310 #endif
7311 
7312 /*
7313  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7314  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7315  * is non-zero, which is never for ARMv7, optionally in ARMv8
7316  * and mandatorily for ARMv8.2 and up.
7317  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7318  * implementation is RAZ/WI we can ignore this detail, as we
7319  * do for ACTLR.
7320  */
7321 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7322     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7323       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7324       .access = PL1_RW, .accessfn = access_tacr,
7325       .type = ARM_CP_CONST, .resetvalue = 0 },
7326     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7327       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7328       .access = PL2_RW, .type = ARM_CP_CONST,
7329       .resetvalue = 0 },
7330 };
7331 
7332 void register_cp_regs_for_features(ARMCPU *cpu)
7333 {
7334     /* Register all the coprocessor registers based on feature bits */
7335     CPUARMState *env = &cpu->env;
7336     if (arm_feature(env, ARM_FEATURE_M)) {
7337         /* M profile has no coprocessor registers */
7338         return;
7339     }
7340 
7341     define_arm_cp_regs(cpu, cp_reginfo);
7342     if (!arm_feature(env, ARM_FEATURE_V8)) {
7343         /* Must go early as it is full of wildcards that may be
7344          * overridden by later definitions.
7345          */
7346         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7347     }
7348 
7349     if (arm_feature(env, ARM_FEATURE_V6)) {
7350         /* The ID registers all have impdef reset values */
7351         ARMCPRegInfo v6_idregs[] = {
7352             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7353               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7354               .access = PL1_R, .type = ARM_CP_CONST,
7355               .accessfn = access_aa32_tid3,
7356               .resetvalue = cpu->isar.id_pfr0 },
7357             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7358              * the value of the GIC field until after we define these regs.
7359              */
7360             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7361               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7362               .access = PL1_R, .type = ARM_CP_NO_RAW,
7363               .accessfn = access_aa32_tid3,
7364               .readfn = id_pfr1_read,
7365               .writefn = arm_cp_write_ignore },
7366             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7367               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7368               .access = PL1_R, .type = ARM_CP_CONST,
7369               .accessfn = access_aa32_tid3,
7370               .resetvalue = cpu->isar.id_dfr0 },
7371             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7372               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7373               .access = PL1_R, .type = ARM_CP_CONST,
7374               .accessfn = access_aa32_tid3,
7375               .resetvalue = cpu->id_afr0 },
7376             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7377               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7378               .access = PL1_R, .type = ARM_CP_CONST,
7379               .accessfn = access_aa32_tid3,
7380               .resetvalue = cpu->isar.id_mmfr0 },
7381             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7382               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7383               .access = PL1_R, .type = ARM_CP_CONST,
7384               .accessfn = access_aa32_tid3,
7385               .resetvalue = cpu->isar.id_mmfr1 },
7386             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7387               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7388               .access = PL1_R, .type = ARM_CP_CONST,
7389               .accessfn = access_aa32_tid3,
7390               .resetvalue = cpu->isar.id_mmfr2 },
7391             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7392               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7393               .access = PL1_R, .type = ARM_CP_CONST,
7394               .accessfn = access_aa32_tid3,
7395               .resetvalue = cpu->isar.id_mmfr3 },
7396             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7397               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7398               .access = PL1_R, .type = ARM_CP_CONST,
7399               .accessfn = access_aa32_tid3,
7400               .resetvalue = cpu->isar.id_isar0 },
7401             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7402               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7403               .access = PL1_R, .type = ARM_CP_CONST,
7404               .accessfn = access_aa32_tid3,
7405               .resetvalue = cpu->isar.id_isar1 },
7406             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7407               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7408               .access = PL1_R, .type = ARM_CP_CONST,
7409               .accessfn = access_aa32_tid3,
7410               .resetvalue = cpu->isar.id_isar2 },
7411             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7412               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7413               .access = PL1_R, .type = ARM_CP_CONST,
7414               .accessfn = access_aa32_tid3,
7415               .resetvalue = cpu->isar.id_isar3 },
7416             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7417               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7418               .access = PL1_R, .type = ARM_CP_CONST,
7419               .accessfn = access_aa32_tid3,
7420               .resetvalue = cpu->isar.id_isar4 },
7421             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7422               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7423               .access = PL1_R, .type = ARM_CP_CONST,
7424               .accessfn = access_aa32_tid3,
7425               .resetvalue = cpu->isar.id_isar5 },
7426             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7427               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7428               .access = PL1_R, .type = ARM_CP_CONST,
7429               .accessfn = access_aa32_tid3,
7430               .resetvalue = cpu->isar.id_mmfr4 },
7431             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7432               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7433               .access = PL1_R, .type = ARM_CP_CONST,
7434               .accessfn = access_aa32_tid3,
7435               .resetvalue = cpu->isar.id_isar6 },
7436         };
7437         define_arm_cp_regs(cpu, v6_idregs);
7438         define_arm_cp_regs(cpu, v6_cp_reginfo);
7439     } else {
7440         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7441     }
7442     if (arm_feature(env, ARM_FEATURE_V6K)) {
7443         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7444     }
7445     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7446         !arm_feature(env, ARM_FEATURE_PMSA)) {
7447         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7448     }
7449     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7450         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7451     }
7452     if (arm_feature(env, ARM_FEATURE_V7)) {
7453         ARMCPRegInfo clidr = {
7454             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7455             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7456             .access = PL1_R, .type = ARM_CP_CONST,
7457             .accessfn = access_aa64_tid2,
7458             .resetvalue = cpu->clidr
7459         };
7460         define_one_arm_cp_reg(cpu, &clidr);
7461         define_arm_cp_regs(cpu, v7_cp_reginfo);
7462         define_debug_regs(cpu);
7463         define_pmu_regs(cpu);
7464     } else {
7465         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7466     }
7467     if (arm_feature(env, ARM_FEATURE_V8)) {
7468         /*
7469          * v8 ID registers, which all have impdef reset values.
7470          * Note that within the ID register ranges the unused slots
7471          * must all RAZ, not UNDEF; future architecture versions may
7472          * define new registers here.
7473          * ID registers which are AArch64 views of the AArch32 ID registers
7474          * which already existed in v6 and v7 are handled elsewhere,
7475          * in v6_idregs[].
7476          */
7477         int i;
7478         ARMCPRegInfo v8_idregs[] = {
7479             /*
7480              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7481              * emulation because we don't know the right value for the
7482              * GIC field until after we define these regs.
7483              */
7484             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7485               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7486               .access = PL1_R,
7487 #ifdef CONFIG_USER_ONLY
7488               .type = ARM_CP_CONST,
7489               .resetvalue = cpu->isar.id_aa64pfr0
7490 #else
7491               .type = ARM_CP_NO_RAW,
7492               .accessfn = access_aa64_tid3,
7493               .readfn = id_aa64pfr0_read,
7494               .writefn = arm_cp_write_ignore
7495 #endif
7496             },
7497             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7498               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7499               .access = PL1_R, .type = ARM_CP_CONST,
7500               .accessfn = access_aa64_tid3,
7501               .resetvalue = cpu->isar.id_aa64pfr1},
7502             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7503               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7504               .access = PL1_R, .type = ARM_CP_CONST,
7505               .accessfn = access_aa64_tid3,
7506               .resetvalue = 0 },
7507             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7508               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7509               .access = PL1_R, .type = ARM_CP_CONST,
7510               .accessfn = access_aa64_tid3,
7511               .resetvalue = 0 },
7512             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7513               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7514               .access = PL1_R, .type = ARM_CP_CONST,
7515               .accessfn = access_aa64_tid3,
7516               .resetvalue = cpu->isar.id_aa64zfr0 },
7517             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7518               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7519               .access = PL1_R, .type = ARM_CP_CONST,
7520               .accessfn = access_aa64_tid3,
7521               .resetvalue = cpu->isar.id_aa64smfr0 },
7522             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7523               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7524               .access = PL1_R, .type = ARM_CP_CONST,
7525               .accessfn = access_aa64_tid3,
7526               .resetvalue = 0 },
7527             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7528               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7529               .access = PL1_R, .type = ARM_CP_CONST,
7530               .accessfn = access_aa64_tid3,
7531               .resetvalue = 0 },
7532             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7533               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7534               .access = PL1_R, .type = ARM_CP_CONST,
7535               .accessfn = access_aa64_tid3,
7536               .resetvalue = cpu->isar.id_aa64dfr0 },
7537             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7538               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7539               .access = PL1_R, .type = ARM_CP_CONST,
7540               .accessfn = access_aa64_tid3,
7541               .resetvalue = cpu->isar.id_aa64dfr1 },
7542             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7543               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7544               .access = PL1_R, .type = ARM_CP_CONST,
7545               .accessfn = access_aa64_tid3,
7546               .resetvalue = 0 },
7547             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7548               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7549               .access = PL1_R, .type = ARM_CP_CONST,
7550               .accessfn = access_aa64_tid3,
7551               .resetvalue = 0 },
7552             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7553               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7554               .access = PL1_R, .type = ARM_CP_CONST,
7555               .accessfn = access_aa64_tid3,
7556               .resetvalue = cpu->id_aa64afr0 },
7557             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7558               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7559               .access = PL1_R, .type = ARM_CP_CONST,
7560               .accessfn = access_aa64_tid3,
7561               .resetvalue = cpu->id_aa64afr1 },
7562             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7563               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7564               .access = PL1_R, .type = ARM_CP_CONST,
7565               .accessfn = access_aa64_tid3,
7566               .resetvalue = 0 },
7567             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7568               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7569               .access = PL1_R, .type = ARM_CP_CONST,
7570               .accessfn = access_aa64_tid3,
7571               .resetvalue = 0 },
7572             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7573               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7574               .access = PL1_R, .type = ARM_CP_CONST,
7575               .accessfn = access_aa64_tid3,
7576               .resetvalue = cpu->isar.id_aa64isar0 },
7577             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7578               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7579               .access = PL1_R, .type = ARM_CP_CONST,
7580               .accessfn = access_aa64_tid3,
7581               .resetvalue = cpu->isar.id_aa64isar1 },
7582             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7583               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7584               .access = PL1_R, .type = ARM_CP_CONST,
7585               .accessfn = access_aa64_tid3,
7586               .resetvalue = 0 },
7587             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7588               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7589               .access = PL1_R, .type = ARM_CP_CONST,
7590               .accessfn = access_aa64_tid3,
7591               .resetvalue = 0 },
7592             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7593               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7594               .access = PL1_R, .type = ARM_CP_CONST,
7595               .accessfn = access_aa64_tid3,
7596               .resetvalue = 0 },
7597             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7598               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7599               .access = PL1_R, .type = ARM_CP_CONST,
7600               .accessfn = access_aa64_tid3,
7601               .resetvalue = 0 },
7602             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7603               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7604               .access = PL1_R, .type = ARM_CP_CONST,
7605               .accessfn = access_aa64_tid3,
7606               .resetvalue = 0 },
7607             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7608               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7609               .access = PL1_R, .type = ARM_CP_CONST,
7610               .accessfn = access_aa64_tid3,
7611               .resetvalue = 0 },
7612             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7613               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7614               .access = PL1_R, .type = ARM_CP_CONST,
7615               .accessfn = access_aa64_tid3,
7616               .resetvalue = cpu->isar.id_aa64mmfr0 },
7617             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7618               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7619               .access = PL1_R, .type = ARM_CP_CONST,
7620               .accessfn = access_aa64_tid3,
7621               .resetvalue = cpu->isar.id_aa64mmfr1 },
7622             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7623               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7624               .access = PL1_R, .type = ARM_CP_CONST,
7625               .accessfn = access_aa64_tid3,
7626               .resetvalue = cpu->isar.id_aa64mmfr2 },
7627             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7628               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7629               .access = PL1_R, .type = ARM_CP_CONST,
7630               .accessfn = access_aa64_tid3,
7631               .resetvalue = 0 },
7632             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7633               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7634               .access = PL1_R, .type = ARM_CP_CONST,
7635               .accessfn = access_aa64_tid3,
7636               .resetvalue = 0 },
7637             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7638               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7639               .access = PL1_R, .type = ARM_CP_CONST,
7640               .accessfn = access_aa64_tid3,
7641               .resetvalue = 0 },
7642             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7643               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7644               .access = PL1_R, .type = ARM_CP_CONST,
7645               .accessfn = access_aa64_tid3,
7646               .resetvalue = 0 },
7647             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7648               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7649               .access = PL1_R, .type = ARM_CP_CONST,
7650               .accessfn = access_aa64_tid3,
7651               .resetvalue = 0 },
7652             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7653               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7654               .access = PL1_R, .type = ARM_CP_CONST,
7655               .accessfn = access_aa64_tid3,
7656               .resetvalue = cpu->isar.mvfr0 },
7657             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7658               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7659               .access = PL1_R, .type = ARM_CP_CONST,
7660               .accessfn = access_aa64_tid3,
7661               .resetvalue = cpu->isar.mvfr1 },
7662             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7663               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7664               .access = PL1_R, .type = ARM_CP_CONST,
7665               .accessfn = access_aa64_tid3,
7666               .resetvalue = cpu->isar.mvfr2 },
7667             /*
7668              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7669              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7670              * as RAZ, since it is in the "reserved for future ID
7671              * registers, RAZ" part of the AArch32 encoding space.
7672              */
7673             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7674               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7675               .access = PL1_R, .type = ARM_CP_CONST,
7676               .accessfn = access_aa64_tid3,
7677               .resetvalue = 0 },
7678             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7679               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7680               .access = PL1_R, .type = ARM_CP_CONST,
7681               .accessfn = access_aa64_tid3,
7682               .resetvalue = 0 },
7683             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7684               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7685               .access = PL1_R, .type = ARM_CP_CONST,
7686               .accessfn = access_aa64_tid3,
7687               .resetvalue = 0 },
7688             /*
7689              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7690              * they're also RAZ for AArch64, and in v8 are gradually
7691              * being filled with AArch64-view-of-AArch32-ID-register
7692              * for new ID registers.
7693              */
7694             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7695               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7696               .access = PL1_R, .type = ARM_CP_CONST,
7697               .accessfn = access_aa64_tid3,
7698               .resetvalue = 0 },
7699             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7700               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7701               .access = PL1_R, .type = ARM_CP_CONST,
7702               .accessfn = access_aa64_tid3,
7703               .resetvalue = cpu->isar.id_pfr2 },
7704             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7705               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7706               .access = PL1_R, .type = ARM_CP_CONST,
7707               .accessfn = access_aa64_tid3,
7708               .resetvalue = cpu->isar.id_dfr1 },
7709             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7710               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7711               .access = PL1_R, .type = ARM_CP_CONST,
7712               .accessfn = access_aa64_tid3,
7713               .resetvalue = cpu->isar.id_mmfr5 },
7714             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7715               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7716               .access = PL1_R, .type = ARM_CP_CONST,
7717               .accessfn = access_aa64_tid3,
7718               .resetvalue = 0 },
7719             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7720               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7721               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7722               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7723             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7724               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7725               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7726               .resetvalue = cpu->pmceid0 },
7727             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7728               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7729               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7730               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7731             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7732               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7733               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7734               .resetvalue = cpu->pmceid1 },
7735         };
7736 #ifdef CONFIG_USER_ONLY
7737         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7738             { .name = "ID_AA64PFR0_EL1",
7739               .exported_bits = 0x000f000f00ff0000,
7740               .fixed_bits    = 0x0000000000000011 },
7741             { .name = "ID_AA64PFR1_EL1",
7742               .exported_bits = 0x00000000000000f0 },
7743             { .name = "ID_AA64PFR*_EL1_RESERVED",
7744               .is_glob = true                     },
7745             { .name = "ID_AA64ZFR0_EL1"           },
7746             { .name = "ID_AA64MMFR0_EL1",
7747               .fixed_bits    = 0x00000000ff000000 },
7748             { .name = "ID_AA64MMFR1_EL1"          },
7749             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7750               .is_glob = true                     },
7751             { .name = "ID_AA64DFR0_EL1",
7752               .fixed_bits    = 0x0000000000000006 },
7753             { .name = "ID_AA64DFR1_EL1"           },
7754             { .name = "ID_AA64DFR*_EL1_RESERVED",
7755               .is_glob = true                     },
7756             { .name = "ID_AA64AFR*",
7757               .is_glob = true                     },
7758             { .name = "ID_AA64ISAR0_EL1",
7759               .exported_bits = 0x00fffffff0fffff0 },
7760             { .name = "ID_AA64ISAR1_EL1",
7761               .exported_bits = 0x000000f0ffffffff },
7762             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7763               .is_glob = true                     },
7764         };
7765         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7766 #endif
7767         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7768         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7769             !arm_feature(env, ARM_FEATURE_EL2)) {
7770             ARMCPRegInfo rvbar = {
7771                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7772                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7773                 .access = PL1_R,
7774                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7775             };
7776             define_one_arm_cp_reg(cpu, &rvbar);
7777         }
7778         define_arm_cp_regs(cpu, v8_idregs);
7779         define_arm_cp_regs(cpu, v8_cp_reginfo);
7780 
7781         for (i = 4; i < 16; i++) {
7782             /*
7783              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7784              * For pre-v8 cores there are RAZ patterns for these in
7785              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7786              * v8 extends the "must RAZ" part of the ID register space
7787              * to also cover c0, 0, c{8-15}, {0-7}.
7788              * These are STATE_AA32 because in the AArch64 sysreg space
7789              * c4-c7 is where the AArch64 ID registers live (and we've
7790              * already defined those in v8_idregs[]), and c8-c15 are not
7791              * "must RAZ" for AArch64.
7792              */
7793             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7794             ARMCPRegInfo v8_aa32_raz_idregs = {
7795                 .name = name,
7796                 .state = ARM_CP_STATE_AA32,
7797                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7798                 .access = PL1_R, .type = ARM_CP_CONST,
7799                 .accessfn = access_aa64_tid3,
7800                 .resetvalue = 0 };
7801             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7802         }
7803     }
7804 
7805     /*
7806      * Register the base EL2 cpregs.
7807      * Pre v8, these registers are implemented only as part of the
7808      * Virtualization Extensions (EL2 present).  Beginning with v8,
7809      * if EL2 is missing but EL3 is enabled, mostly these become
7810      * RES0 from EL3, with some specific exceptions.
7811      */
7812     if (arm_feature(env, ARM_FEATURE_EL2)
7813         || (arm_feature(env, ARM_FEATURE_EL3)
7814             && arm_feature(env, ARM_FEATURE_V8))) {
7815         uint64_t vmpidr_def = mpidr_read_val(env);
7816         ARMCPRegInfo vpidr_regs[] = {
7817             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7818               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7819               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7820               .resetvalue = cpu->midr,
7821               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7822               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7823             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7824               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7825               .access = PL2_RW, .resetvalue = cpu->midr,
7826               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7827               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7828             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7829               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7830               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7831               .resetvalue = vmpidr_def,
7832               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7833               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7834             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7835               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7836               .access = PL2_RW, .resetvalue = vmpidr_def,
7837               .type = ARM_CP_EL3_NO_EL2_C_NZ,
7838               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7839         };
7840         /*
7841          * The only field of MDCR_EL2 that has a defined architectural reset
7842          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7843          */
7844         ARMCPRegInfo mdcr_el2 = {
7845             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
7846             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7847             .writefn = mdcr_el2_write,
7848             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7849             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7850         };
7851         define_one_arm_cp_reg(cpu, &mdcr_el2);
7852         define_arm_cp_regs(cpu, vpidr_regs);
7853         define_arm_cp_regs(cpu, el2_cp_reginfo);
7854         if (arm_feature(env, ARM_FEATURE_V8)) {
7855             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7856         }
7857         if (cpu_isar_feature(aa64_sel2, cpu)) {
7858             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7859         }
7860         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7861         if (!arm_feature(env, ARM_FEATURE_EL3)) {
7862             ARMCPRegInfo rvbar = {
7863                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7864                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7865                 .access = PL2_R,
7866                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7867             };
7868             define_one_arm_cp_reg(cpu, &rvbar);
7869         }
7870     }
7871 
7872     /* Register the base EL3 cpregs. */
7873     if (arm_feature(env, ARM_FEATURE_EL3)) {
7874         define_arm_cp_regs(cpu, el3_cp_reginfo);
7875         ARMCPRegInfo el3_regs[] = {
7876             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7877               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7878               .access = PL3_R,
7879               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7880             },
7881             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7882               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7883               .access = PL3_RW,
7884               .raw_writefn = raw_write, .writefn = sctlr_write,
7885               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7886               .resetvalue = cpu->reset_sctlr },
7887         };
7888 
7889         define_arm_cp_regs(cpu, el3_regs);
7890     }
7891     /* The behaviour of NSACR is sufficiently various that we don't
7892      * try to describe it in a single reginfo:
7893      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
7894      *     reads as constant 0xc00 from NS EL1 and NS EL2
7895      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7896      *  if v7 without EL3, register doesn't exist
7897      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7898      */
7899     if (arm_feature(env, ARM_FEATURE_EL3)) {
7900         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7901             static const ARMCPRegInfo nsacr = {
7902                 .name = "NSACR", .type = ARM_CP_CONST,
7903                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7904                 .access = PL1_RW, .accessfn = nsacr_access,
7905                 .resetvalue = 0xc00
7906             };
7907             define_one_arm_cp_reg(cpu, &nsacr);
7908         } else {
7909             static const ARMCPRegInfo nsacr = {
7910                 .name = "NSACR",
7911                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7912                 .access = PL3_RW | PL1_R,
7913                 .resetvalue = 0,
7914                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7915             };
7916             define_one_arm_cp_reg(cpu, &nsacr);
7917         }
7918     } else {
7919         if (arm_feature(env, ARM_FEATURE_V8)) {
7920             static const ARMCPRegInfo nsacr = {
7921                 .name = "NSACR", .type = ARM_CP_CONST,
7922                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7923                 .access = PL1_R,
7924                 .resetvalue = 0xc00
7925             };
7926             define_one_arm_cp_reg(cpu, &nsacr);
7927         }
7928     }
7929 
7930     if (arm_feature(env, ARM_FEATURE_PMSA)) {
7931         if (arm_feature(env, ARM_FEATURE_V6)) {
7932             /* PMSAv6 not implemented */
7933             assert(arm_feature(env, ARM_FEATURE_V7));
7934             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7935             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7936         } else {
7937             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7938         }
7939     } else {
7940         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7941         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7942         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
7943         if (cpu_isar_feature(aa32_hpd, cpu)) {
7944             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7945         }
7946     }
7947     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7948         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7949     }
7950     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7951         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7952     }
7953     if (arm_feature(env, ARM_FEATURE_VAPA)) {
7954         define_arm_cp_regs(cpu, vapa_cp_reginfo);
7955     }
7956     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7957         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7958     }
7959     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7960         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7961     }
7962     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7963         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7964     }
7965     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7966         define_arm_cp_regs(cpu, omap_cp_reginfo);
7967     }
7968     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7969         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7970     }
7971     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7972         define_arm_cp_regs(cpu, xscale_cp_reginfo);
7973     }
7974     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7975         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7976     }
7977     if (arm_feature(env, ARM_FEATURE_LPAE)) {
7978         define_arm_cp_regs(cpu, lpae_cp_reginfo);
7979     }
7980     if (cpu_isar_feature(aa32_jazelle, cpu)) {
7981         define_arm_cp_regs(cpu, jazelle_regs);
7982     }
7983     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7984      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7985      * be read-only (ie write causes UNDEF exception).
7986      */
7987     {
7988         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7989             /* Pre-v8 MIDR space.
7990              * Note that the MIDR isn't a simple constant register because
7991              * of the TI925 behaviour where writes to another register can
7992              * cause the MIDR value to change.
7993              *
7994              * Unimplemented registers in the c15 0 0 0 space default to
7995              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7996              * and friends override accordingly.
7997              */
7998             { .name = "MIDR",
7999               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8000               .access = PL1_R, .resetvalue = cpu->midr,
8001               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8002               .readfn = midr_read,
8003               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8004               .type = ARM_CP_OVERRIDE },
8005             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8006             { .name = "DUMMY",
8007               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8008               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8009             { .name = "DUMMY",
8010               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8011               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8012             { .name = "DUMMY",
8013               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8014               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8015             { .name = "DUMMY",
8016               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8017               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8018             { .name = "DUMMY",
8019               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8020               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8021         };
8022         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8023             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8024               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8025               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8026               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8027               .readfn = midr_read },
8028             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8029             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8030               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8031               .access = PL1_R, .resetvalue = cpu->midr },
8032             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8033               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8034               .access = PL1_R, .resetvalue = cpu->midr },
8035             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8036               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8037               .access = PL1_R,
8038               .accessfn = access_aa64_tid1,
8039               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8040         };
8041         ARMCPRegInfo id_cp_reginfo[] = {
8042             /* These are common to v8 and pre-v8 */
8043             { .name = "CTR",
8044               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8045               .access = PL1_R, .accessfn = ctr_el0_access,
8046               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8047             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8048               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8049               .access = PL0_R, .accessfn = ctr_el0_access,
8050               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8051             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8052             { .name = "TCMTR",
8053               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8054               .access = PL1_R,
8055               .accessfn = access_aa32_tid1,
8056               .type = ARM_CP_CONST, .resetvalue = 0 },
8057         };
8058         /* TLBTR is specific to VMSA */
8059         ARMCPRegInfo id_tlbtr_reginfo = {
8060               .name = "TLBTR",
8061               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8062               .access = PL1_R,
8063               .accessfn = access_aa32_tid1,
8064               .type = ARM_CP_CONST, .resetvalue = 0,
8065         };
8066         /* MPUIR is specific to PMSA V6+ */
8067         ARMCPRegInfo id_mpuir_reginfo = {
8068               .name = "MPUIR",
8069               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8070               .access = PL1_R, .type = ARM_CP_CONST,
8071               .resetvalue = cpu->pmsav7_dregion << 8
8072         };
8073         static const ARMCPRegInfo crn0_wi_reginfo = {
8074             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8075             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8076             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8077         };
8078 #ifdef CONFIG_USER_ONLY
8079         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8080             { .name = "MIDR_EL1",
8081               .exported_bits = 0x00000000ffffffff },
8082             { .name = "REVIDR_EL1"                },
8083         };
8084         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8085 #endif
8086         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8087             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8088             size_t i;
8089             /* Register the blanket "writes ignored" value first to cover the
8090              * whole space. Then update the specific ID registers to allow write
8091              * access, so that they ignore writes rather than causing them to
8092              * UNDEF.
8093              */
8094             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8095             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8096                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8097             }
8098             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8099                 id_cp_reginfo[i].access = PL1_RW;
8100             }
8101             id_mpuir_reginfo.access = PL1_RW;
8102             id_tlbtr_reginfo.access = PL1_RW;
8103         }
8104         if (arm_feature(env, ARM_FEATURE_V8)) {
8105             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8106         } else {
8107             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8108         }
8109         define_arm_cp_regs(cpu, id_cp_reginfo);
8110         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8111             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8112         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8113             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8114         }
8115     }
8116 
8117     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8118         ARMCPRegInfo mpidr_cp_reginfo[] = {
8119             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8120               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8121               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8122         };
8123 #ifdef CONFIG_USER_ONLY
8124         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8125             { .name = "MPIDR_EL1",
8126               .fixed_bits = 0x0000000080000000 },
8127         };
8128         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8129 #endif
8130         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8131     }
8132 
8133     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8134         ARMCPRegInfo auxcr_reginfo[] = {
8135             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8136               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8137               .access = PL1_RW, .accessfn = access_tacr,
8138               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8139             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8140               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8141               .access = PL2_RW, .type = ARM_CP_CONST,
8142               .resetvalue = 0 },
8143             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8144               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8145               .access = PL3_RW, .type = ARM_CP_CONST,
8146               .resetvalue = 0 },
8147         };
8148         define_arm_cp_regs(cpu, auxcr_reginfo);
8149         if (cpu_isar_feature(aa32_ac2, cpu)) {
8150             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8151         }
8152     }
8153 
8154     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8155         /*
8156          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8157          * There are two flavours:
8158          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8159          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8160          *      32-bit register visible to AArch32 at a different encoding
8161          *      to the "flavour 1" register and with the bits rearranged to
8162          *      be able to squash a 64-bit address into the 32-bit view.
8163          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8164          * in future if we support AArch32-only configs of some of the
8165          * AArch64 cores we might need to add a specific feature flag
8166          * to indicate cores with "flavour 2" CBAR.
8167          */
8168         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8169             /* 32 bit view is [31:18] 0...0 [43:32]. */
8170             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8171                 | extract64(cpu->reset_cbar, 32, 12);
8172             ARMCPRegInfo cbar_reginfo[] = {
8173                 { .name = "CBAR",
8174                   .type = ARM_CP_CONST,
8175                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8176                   .access = PL1_R, .resetvalue = cbar32 },
8177                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8178                   .type = ARM_CP_CONST,
8179                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8180                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8181             };
8182             /* We don't implement a r/w 64 bit CBAR currently */
8183             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8184             define_arm_cp_regs(cpu, cbar_reginfo);
8185         } else {
8186             ARMCPRegInfo cbar = {
8187                 .name = "CBAR",
8188                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8189                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8190                 .fieldoffset = offsetof(CPUARMState,
8191                                         cp15.c15_config_base_address)
8192             };
8193             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8194                 cbar.access = PL1_R;
8195                 cbar.fieldoffset = 0;
8196                 cbar.type = ARM_CP_CONST;
8197             }
8198             define_one_arm_cp_reg(cpu, &cbar);
8199         }
8200     }
8201 
8202     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8203         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8204             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8205               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8206               .access = PL1_RW, .writefn = vbar_write,
8207               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8208                                      offsetof(CPUARMState, cp15.vbar_ns) },
8209               .resetvalue = 0 },
8210         };
8211         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8212     }
8213 
8214     /* Generic registers whose values depend on the implementation */
8215     {
8216         ARMCPRegInfo sctlr = {
8217             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8218             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8219             .access = PL1_RW, .accessfn = access_tvm_trvm,
8220             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8221                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8222             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8223             .raw_writefn = raw_write,
8224         };
8225         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8226             /* Normally we would always end the TB on an SCTLR write, but Linux
8227              * arch/arm/mach-pxa/sleep.S expects two instructions following
8228              * an MMU enable to execute from cache.  Imitate this behaviour.
8229              */
8230             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8231         }
8232         define_one_arm_cp_reg(cpu, &sctlr);
8233     }
8234 
8235     if (cpu_isar_feature(aa64_lor, cpu)) {
8236         define_arm_cp_regs(cpu, lor_reginfo);
8237     }
8238     if (cpu_isar_feature(aa64_pan, cpu)) {
8239         define_one_arm_cp_reg(cpu, &pan_reginfo);
8240     }
8241 #ifndef CONFIG_USER_ONLY
8242     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8243         define_arm_cp_regs(cpu, ats1e1_reginfo);
8244     }
8245     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8246         define_arm_cp_regs(cpu, ats1cp_reginfo);
8247     }
8248 #endif
8249     if (cpu_isar_feature(aa64_uao, cpu)) {
8250         define_one_arm_cp_reg(cpu, &uao_reginfo);
8251     }
8252 
8253     if (cpu_isar_feature(aa64_dit, cpu)) {
8254         define_one_arm_cp_reg(cpu, &dit_reginfo);
8255     }
8256     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8257         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8258     }
8259     if (cpu_isar_feature(any_ras, cpu)) {
8260         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8261     }
8262 
8263     if (cpu_isar_feature(aa64_vh, cpu) ||
8264         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8265         define_one_arm_cp_reg(cpu, &contextidr_el2);
8266     }
8267     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8268         define_arm_cp_regs(cpu, vhe_reginfo);
8269     }
8270 
8271     if (cpu_isar_feature(aa64_sve, cpu)) {
8272         define_arm_cp_regs(cpu, zcr_reginfo);
8273     }
8274 
8275     if (cpu_isar_feature(aa64_hcx, cpu)) {
8276         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8277     }
8278 
8279 #ifdef TARGET_AARCH64
8280     if (cpu_isar_feature(aa64_sme, cpu)) {
8281         define_arm_cp_regs(cpu, sme_reginfo);
8282     }
8283     if (cpu_isar_feature(aa64_pauth, cpu)) {
8284         define_arm_cp_regs(cpu, pauth_reginfo);
8285     }
8286     if (cpu_isar_feature(aa64_rndr, cpu)) {
8287         define_arm_cp_regs(cpu, rndr_reginfo);
8288     }
8289     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8290         define_arm_cp_regs(cpu, tlbirange_reginfo);
8291     }
8292     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8293         define_arm_cp_regs(cpu, tlbios_reginfo);
8294     }
8295 #ifndef CONFIG_USER_ONLY
8296     /* Data Cache clean instructions up to PoP */
8297     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8298         define_one_arm_cp_reg(cpu, dcpop_reg);
8299 
8300         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8301             define_one_arm_cp_reg(cpu, dcpodp_reg);
8302         }
8303     }
8304 #endif /*CONFIG_USER_ONLY*/
8305 
8306     /*
8307      * If full MTE is enabled, add all of the system registers.
8308      * If only "instructions available at EL0" are enabled,
8309      * then define only a RAZ/WI version of PSTATE.TCO.
8310      */
8311     if (cpu_isar_feature(aa64_mte, cpu)) {
8312         define_arm_cp_regs(cpu, mte_reginfo);
8313         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8314     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8315         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8316         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8317     }
8318 
8319     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8320         define_arm_cp_regs(cpu, scxtnum_reginfo);
8321     }
8322 #endif
8323 
8324     if (cpu_isar_feature(any_predinv, cpu)) {
8325         define_arm_cp_regs(cpu, predinv_reginfo);
8326     }
8327 
8328     if (cpu_isar_feature(any_ccidx, cpu)) {
8329         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8330     }
8331 
8332 #ifndef CONFIG_USER_ONLY
8333     /*
8334      * Register redirections and aliases must be done last,
8335      * after the registers from the other extensions have been defined.
8336      */
8337     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8338         define_arm_vh_e2h_redirects_aliases(cpu);
8339     }
8340 #endif
8341 }
8342 
8343 /* Sort alphabetically by type name, except for "any". */
8344 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8345 {
8346     ObjectClass *class_a = (ObjectClass *)a;
8347     ObjectClass *class_b = (ObjectClass *)b;
8348     const char *name_a, *name_b;
8349 
8350     name_a = object_class_get_name(class_a);
8351     name_b = object_class_get_name(class_b);
8352     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8353         return 1;
8354     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8355         return -1;
8356     } else {
8357         return strcmp(name_a, name_b);
8358     }
8359 }
8360 
8361 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8362 {
8363     ObjectClass *oc = data;
8364     CPUClass *cc = CPU_CLASS(oc);
8365     const char *typename;
8366     char *name;
8367 
8368     typename = object_class_get_name(oc);
8369     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8370     if (cc->deprecation_note) {
8371         qemu_printf("  %s (deprecated)\n", name);
8372     } else {
8373         qemu_printf("  %s\n", name);
8374     }
8375     g_free(name);
8376 }
8377 
8378 void arm_cpu_list(void)
8379 {
8380     GSList *list;
8381 
8382     list = object_class_get_list(TYPE_ARM_CPU, false);
8383     list = g_slist_sort(list, arm_cpu_list_compare);
8384     qemu_printf("Available CPUs:\n");
8385     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8386     g_slist_free(list);
8387 }
8388 
8389 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8390 {
8391     ObjectClass *oc = data;
8392     CpuDefinitionInfoList **cpu_list = user_data;
8393     CpuDefinitionInfo *info;
8394     const char *typename;
8395 
8396     typename = object_class_get_name(oc);
8397     info = g_malloc0(sizeof(*info));
8398     info->name = g_strndup(typename,
8399                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8400     info->q_typename = g_strdup(typename);
8401 
8402     QAPI_LIST_PREPEND(*cpu_list, info);
8403 }
8404 
8405 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8406 {
8407     CpuDefinitionInfoList *cpu_list = NULL;
8408     GSList *list;
8409 
8410     list = object_class_get_list(TYPE_ARM_CPU, false);
8411     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8412     g_slist_free(list);
8413 
8414     return cpu_list;
8415 }
8416 
8417 /*
8418  * Private utility function for define_one_arm_cp_reg_with_opaque():
8419  * add a single reginfo struct to the hash table.
8420  */
8421 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8422                                    void *opaque, CPState state,
8423                                    CPSecureState secstate,
8424                                    int crm, int opc1, int opc2,
8425                                    const char *name)
8426 {
8427     CPUARMState *env = &cpu->env;
8428     uint32_t key;
8429     ARMCPRegInfo *r2;
8430     bool is64 = r->type & ARM_CP_64BIT;
8431     bool ns = secstate & ARM_CP_SECSTATE_NS;
8432     int cp = r->cp;
8433     size_t name_len;
8434     bool make_const;
8435 
8436     switch (state) {
8437     case ARM_CP_STATE_AA32:
8438         /* We assume it is a cp15 register if the .cp field is left unset. */
8439         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8440             cp = 15;
8441         }
8442         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8443         break;
8444     case ARM_CP_STATE_AA64:
8445         /*
8446          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8447          * cp == 0 as equivalent to the value for "standard guest-visible
8448          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8449          * in their AArch64 view (the .cp value may be non-zero for the
8450          * benefit of the AArch32 view).
8451          */
8452         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8453             cp = CP_REG_ARM64_SYSREG_CP;
8454         }
8455         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8456         break;
8457     default:
8458         g_assert_not_reached();
8459     }
8460 
8461     /* Overriding of an existing definition must be explicitly requested. */
8462     if (!(r->type & ARM_CP_OVERRIDE)) {
8463         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8464         if (oldreg) {
8465             assert(oldreg->type & ARM_CP_OVERRIDE);
8466         }
8467     }
8468 
8469     /*
8470      * Eliminate registers that are not present because the EL is missing.
8471      * Doing this here makes it easier to put all registers for a given
8472      * feature into the same ARMCPRegInfo array and define them all at once.
8473      */
8474     make_const = false;
8475     if (arm_feature(env, ARM_FEATURE_EL3)) {
8476         /*
8477          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8478          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8479          */
8480         int min_el = ctz32(r->access) / 2;
8481         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8482             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8483                 return;
8484             }
8485             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8486         }
8487     } else {
8488         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8489                                  ? PL2_RW : PL1_RW);
8490         if ((r->access & max_el) == 0) {
8491             return;
8492         }
8493     }
8494 
8495     /* Combine cpreg and name into one allocation. */
8496     name_len = strlen(name) + 1;
8497     r2 = g_malloc(sizeof(*r2) + name_len);
8498     *r2 = *r;
8499     r2->name = memcpy(r2 + 1, name, name_len);
8500 
8501     /*
8502      * Update fields to match the instantiation, overwiting wildcards
8503      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8504      */
8505     r2->cp = cp;
8506     r2->crm = crm;
8507     r2->opc1 = opc1;
8508     r2->opc2 = opc2;
8509     r2->state = state;
8510     r2->secure = secstate;
8511     if (opaque) {
8512         r2->opaque = opaque;
8513     }
8514 
8515     if (make_const) {
8516         /* This should not have been a very special register to begin. */
8517         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8518         assert(old_special == 0 || old_special == ARM_CP_NOP);
8519         /*
8520          * Set the special function to CONST, retaining the other flags.
8521          * This is important for e.g. ARM_CP_SVE so that we still
8522          * take the SVE trap if CPTR_EL3.EZ == 0.
8523          */
8524         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8525         /*
8526          * Usually, these registers become RES0, but there are a few
8527          * special cases like VPIDR_EL2 which have a constant non-zero
8528          * value with writes ignored.
8529          */
8530         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8531             r2->resetvalue = 0;
8532         }
8533         /*
8534          * ARM_CP_CONST has precedence, so removing the callbacks and
8535          * offsets are not strictly necessary, but it is potentially
8536          * less confusing to debug later.
8537          */
8538         r2->readfn = NULL;
8539         r2->writefn = NULL;
8540         r2->raw_readfn = NULL;
8541         r2->raw_writefn = NULL;
8542         r2->resetfn = NULL;
8543         r2->fieldoffset = 0;
8544         r2->bank_fieldoffsets[0] = 0;
8545         r2->bank_fieldoffsets[1] = 0;
8546     } else {
8547         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8548 
8549         if (isbanked) {
8550             /*
8551              * Register is banked (using both entries in array).
8552              * Overwriting fieldoffset as the array is only used to define
8553              * banked registers but later only fieldoffset is used.
8554              */
8555             r2->fieldoffset = r->bank_fieldoffsets[ns];
8556         }
8557         if (state == ARM_CP_STATE_AA32) {
8558             if (isbanked) {
8559                 /*
8560                  * If the register is banked then we don't need to migrate or
8561                  * reset the 32-bit instance in certain cases:
8562                  *
8563                  * 1) If the register has both 32-bit and 64-bit instances
8564                  *    then we can count on the 64-bit instance taking care
8565                  *    of the non-secure bank.
8566                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8567                  *    version taking care of the secure bank.  This requires
8568                  *    that separate 32 and 64-bit definitions are provided.
8569                  */
8570                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8571                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8572                     r2->type |= ARM_CP_ALIAS;
8573                 }
8574             } else if ((secstate != r->secure) && !ns) {
8575                 /*
8576                  * The register is not banked so we only want to allow
8577                  * migration of the non-secure instance.
8578                  */
8579                 r2->type |= ARM_CP_ALIAS;
8580             }
8581 
8582             if (HOST_BIG_ENDIAN &&
8583                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8584                 r2->fieldoffset += sizeof(uint32_t);
8585             }
8586         }
8587     }
8588 
8589     /*
8590      * By convention, for wildcarded registers only the first
8591      * entry is used for migration; the others are marked as
8592      * ALIAS so we don't try to transfer the register
8593      * multiple times. Special registers (ie NOP/WFI) are
8594      * never migratable and not even raw-accessible.
8595      */
8596     if (r2->type & ARM_CP_SPECIAL_MASK) {
8597         r2->type |= ARM_CP_NO_RAW;
8598     }
8599     if (((r->crm == CP_ANY) && crm != 0) ||
8600         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8601         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8602         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8603     }
8604 
8605     /*
8606      * Check that raw accesses are either forbidden or handled. Note that
8607      * we can't assert this earlier because the setup of fieldoffset for
8608      * banked registers has to be done first.
8609      */
8610     if (!(r2->type & ARM_CP_NO_RAW)) {
8611         assert(!raw_accessors_invalid(r2));
8612     }
8613 
8614     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8615 }
8616 
8617 
8618 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8619                                        const ARMCPRegInfo *r, void *opaque)
8620 {
8621     /* Define implementations of coprocessor registers.
8622      * We store these in a hashtable because typically
8623      * there are less than 150 registers in a space which
8624      * is 16*16*16*8*8 = 262144 in size.
8625      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8626      * If a register is defined twice then the second definition is
8627      * used, so this can be used to define some generic registers and
8628      * then override them with implementation specific variations.
8629      * At least one of the original and the second definition should
8630      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8631      * against accidental use.
8632      *
8633      * The state field defines whether the register is to be
8634      * visible in the AArch32 or AArch64 execution state. If the
8635      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8636      * reginfo structure for the AArch32 view, which sees the lower
8637      * 32 bits of the 64 bit register.
8638      *
8639      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8640      * be wildcarded. AArch64 registers are always considered to be 64
8641      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8642      * the register, if any.
8643      */
8644     int crm, opc1, opc2;
8645     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8646     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8647     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8648     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8649     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8650     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8651     CPState state;
8652 
8653     /* 64 bit registers have only CRm and Opc1 fields */
8654     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8655     /* op0 only exists in the AArch64 encodings */
8656     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8657     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8658     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8659     /*
8660      * This API is only for Arm's system coprocessors (14 and 15) or
8661      * (M-profile or v7A-and-earlier only) for implementation defined
8662      * coprocessors in the range 0..7.  Our decode assumes this, since
8663      * 8..13 can be used for other insns including VFP and Neon. See
8664      * valid_cp() in translate.c.  Assert here that we haven't tried
8665      * to use an invalid coprocessor number.
8666      */
8667     switch (r->state) {
8668     case ARM_CP_STATE_BOTH:
8669         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8670         if (r->cp == 0) {
8671             break;
8672         }
8673         /* fall through */
8674     case ARM_CP_STATE_AA32:
8675         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8676             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8677             assert(r->cp >= 14 && r->cp <= 15);
8678         } else {
8679             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8680         }
8681         break;
8682     case ARM_CP_STATE_AA64:
8683         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8684         break;
8685     default:
8686         g_assert_not_reached();
8687     }
8688     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8689      * encodes a minimum access level for the register. We roll this
8690      * runtime check into our general permission check code, so check
8691      * here that the reginfo's specified permissions are strict enough
8692      * to encompass the generic architectural permission check.
8693      */
8694     if (r->state != ARM_CP_STATE_AA32) {
8695         CPAccessRights mask;
8696         switch (r->opc1) {
8697         case 0:
8698             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8699             mask = PL0U_R | PL1_RW;
8700             break;
8701         case 1: case 2:
8702             /* min_EL EL1 */
8703             mask = PL1_RW;
8704             break;
8705         case 3:
8706             /* min_EL EL0 */
8707             mask = PL0_RW;
8708             break;
8709         case 4:
8710         case 5:
8711             /* min_EL EL2 */
8712             mask = PL2_RW;
8713             break;
8714         case 6:
8715             /* min_EL EL3 */
8716             mask = PL3_RW;
8717             break;
8718         case 7:
8719             /* min_EL EL1, secure mode only (we don't check the latter) */
8720             mask = PL1_RW;
8721             break;
8722         default:
8723             /* broken reginfo with out-of-range opc1 */
8724             g_assert_not_reached();
8725         }
8726         /* assert our permissions are not too lax (stricter is fine) */
8727         assert((r->access & ~mask) == 0);
8728     }
8729 
8730     /* Check that the register definition has enough info to handle
8731      * reads and writes if they are permitted.
8732      */
8733     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8734         if (r->access & PL3_R) {
8735             assert((r->fieldoffset ||
8736                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8737                    r->readfn);
8738         }
8739         if (r->access & PL3_W) {
8740             assert((r->fieldoffset ||
8741                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8742                    r->writefn);
8743         }
8744     }
8745 
8746     for (crm = crmmin; crm <= crmmax; crm++) {
8747         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8748             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8749                 for (state = ARM_CP_STATE_AA32;
8750                      state <= ARM_CP_STATE_AA64; state++) {
8751                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8752                         continue;
8753                     }
8754                     if (state == ARM_CP_STATE_AA32) {
8755                         /* Under AArch32 CP registers can be common
8756                          * (same for secure and non-secure world) or banked.
8757                          */
8758                         char *name;
8759 
8760                         switch (r->secure) {
8761                         case ARM_CP_SECSTATE_S:
8762                         case ARM_CP_SECSTATE_NS:
8763                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8764                                                    r->secure, crm, opc1, opc2,
8765                                                    r->name);
8766                             break;
8767                         case ARM_CP_SECSTATE_BOTH:
8768                             name = g_strdup_printf("%s_S", r->name);
8769                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8770                                                    ARM_CP_SECSTATE_S,
8771                                                    crm, opc1, opc2, name);
8772                             g_free(name);
8773                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8774                                                    ARM_CP_SECSTATE_NS,
8775                                                    crm, opc1, opc2, r->name);
8776                             break;
8777                         default:
8778                             g_assert_not_reached();
8779                         }
8780                     } else {
8781                         /* AArch64 registers get mapped to non-secure instance
8782                          * of AArch32 */
8783                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8784                                                ARM_CP_SECSTATE_NS,
8785                                                crm, opc1, opc2, r->name);
8786                     }
8787                 }
8788             }
8789         }
8790     }
8791 }
8792 
8793 /* Define a whole list of registers */
8794 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8795                                         void *opaque, size_t len)
8796 {
8797     size_t i;
8798     for (i = 0; i < len; ++i) {
8799         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8800     }
8801 }
8802 
8803 /*
8804  * Modify ARMCPRegInfo for access from userspace.
8805  *
8806  * This is a data driven modification directed by
8807  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8808  * user-space cannot alter any values and dynamic values pertaining to
8809  * execution state are hidden from user space view anyway.
8810  */
8811 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8812                                  const ARMCPRegUserSpaceInfo *mods,
8813                                  size_t mods_len)
8814 {
8815     for (size_t mi = 0; mi < mods_len; ++mi) {
8816         const ARMCPRegUserSpaceInfo *m = mods + mi;
8817         GPatternSpec *pat = NULL;
8818 
8819         if (m->is_glob) {
8820             pat = g_pattern_spec_new(m->name);
8821         }
8822         for (size_t ri = 0; ri < regs_len; ++ri) {
8823             ARMCPRegInfo *r = regs + ri;
8824 
8825             if (pat && g_pattern_match_string(pat, r->name)) {
8826                 r->type = ARM_CP_CONST;
8827                 r->access = PL0U_R;
8828                 r->resetvalue = 0;
8829                 /* continue */
8830             } else if (strcmp(r->name, m->name) == 0) {
8831                 r->type = ARM_CP_CONST;
8832                 r->access = PL0U_R;
8833                 r->resetvalue &= m->exported_bits;
8834                 r->resetvalue |= m->fixed_bits;
8835                 break;
8836             }
8837         }
8838         if (pat) {
8839             g_pattern_spec_free(pat);
8840         }
8841     }
8842 }
8843 
8844 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8845 {
8846     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8847 }
8848 
8849 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8850                          uint64_t value)
8851 {
8852     /* Helper coprocessor write function for write-ignore registers */
8853 }
8854 
8855 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8856 {
8857     /* Helper coprocessor write function for read-as-zero registers */
8858     return 0;
8859 }
8860 
8861 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8862 {
8863     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8864 }
8865 
8866 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8867 {
8868     /* Return true if it is not valid for us to switch to
8869      * this CPU mode (ie all the UNPREDICTABLE cases in
8870      * the ARM ARM CPSRWriteByInstr pseudocode).
8871      */
8872 
8873     /* Changes to or from Hyp via MSR and CPS are illegal. */
8874     if (write_type == CPSRWriteByInstr &&
8875         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8876          mode == ARM_CPU_MODE_HYP)) {
8877         return 1;
8878     }
8879 
8880     switch (mode) {
8881     case ARM_CPU_MODE_USR:
8882         return 0;
8883     case ARM_CPU_MODE_SYS:
8884     case ARM_CPU_MODE_SVC:
8885     case ARM_CPU_MODE_ABT:
8886     case ARM_CPU_MODE_UND:
8887     case ARM_CPU_MODE_IRQ:
8888     case ARM_CPU_MODE_FIQ:
8889         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8890          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8891          */
8892         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8893          * and CPS are treated as illegal mode changes.
8894          */
8895         if (write_type == CPSRWriteByInstr &&
8896             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8897             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8898             return 1;
8899         }
8900         return 0;
8901     case ARM_CPU_MODE_HYP:
8902         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8903     case ARM_CPU_MODE_MON:
8904         return arm_current_el(env) < 3;
8905     default:
8906         return 1;
8907     }
8908 }
8909 
8910 uint32_t cpsr_read(CPUARMState *env)
8911 {
8912     int ZF;
8913     ZF = (env->ZF == 0);
8914     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8915         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8916         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8917         | ((env->condexec_bits & 0xfc) << 8)
8918         | (env->GE << 16) | (env->daif & CPSR_AIF);
8919 }
8920 
8921 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8922                 CPSRWriteType write_type)
8923 {
8924     uint32_t changed_daif;
8925     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8926         (mask & (CPSR_M | CPSR_E | CPSR_IL));
8927 
8928     if (mask & CPSR_NZCV) {
8929         env->ZF = (~val) & CPSR_Z;
8930         env->NF = val;
8931         env->CF = (val >> 29) & 1;
8932         env->VF = (val << 3) & 0x80000000;
8933     }
8934     if (mask & CPSR_Q)
8935         env->QF = ((val & CPSR_Q) != 0);
8936     if (mask & CPSR_T)
8937         env->thumb = ((val & CPSR_T) != 0);
8938     if (mask & CPSR_IT_0_1) {
8939         env->condexec_bits &= ~3;
8940         env->condexec_bits |= (val >> 25) & 3;
8941     }
8942     if (mask & CPSR_IT_2_7) {
8943         env->condexec_bits &= 3;
8944         env->condexec_bits |= (val >> 8) & 0xfc;
8945     }
8946     if (mask & CPSR_GE) {
8947         env->GE = (val >> 16) & 0xf;
8948     }
8949 
8950     /* In a V7 implementation that includes the security extensions but does
8951      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8952      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8953      * bits respectively.
8954      *
8955      * In a V8 implementation, it is permitted for privileged software to
8956      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8957      */
8958     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8959         arm_feature(env, ARM_FEATURE_EL3) &&
8960         !arm_feature(env, ARM_FEATURE_EL2) &&
8961         !arm_is_secure(env)) {
8962 
8963         changed_daif = (env->daif ^ val) & mask;
8964 
8965         if (changed_daif & CPSR_A) {
8966             /* Check to see if we are allowed to change the masking of async
8967              * abort exceptions from a non-secure state.
8968              */
8969             if (!(env->cp15.scr_el3 & SCR_AW)) {
8970                 qemu_log_mask(LOG_GUEST_ERROR,
8971                               "Ignoring attempt to switch CPSR_A flag from "
8972                               "non-secure world with SCR.AW bit clear\n");
8973                 mask &= ~CPSR_A;
8974             }
8975         }
8976 
8977         if (changed_daif & CPSR_F) {
8978             /* Check to see if we are allowed to change the masking of FIQ
8979              * exceptions from a non-secure state.
8980              */
8981             if (!(env->cp15.scr_el3 & SCR_FW)) {
8982                 qemu_log_mask(LOG_GUEST_ERROR,
8983                               "Ignoring attempt to switch CPSR_F flag from "
8984                               "non-secure world with SCR.FW bit clear\n");
8985                 mask &= ~CPSR_F;
8986             }
8987 
8988             /* Check whether non-maskable FIQ (NMFI) support is enabled.
8989              * If this bit is set software is not allowed to mask
8990              * FIQs, but is allowed to set CPSR_F to 0.
8991              */
8992             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8993                 (val & CPSR_F)) {
8994                 qemu_log_mask(LOG_GUEST_ERROR,
8995                               "Ignoring attempt to enable CPSR_F flag "
8996                               "(non-maskable FIQ [NMFI] support enabled)\n");
8997                 mask &= ~CPSR_F;
8998             }
8999         }
9000     }
9001 
9002     env->daif &= ~(CPSR_AIF & mask);
9003     env->daif |= val & CPSR_AIF & mask;
9004 
9005     if (write_type != CPSRWriteRaw &&
9006         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9007         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9008             /* Note that we can only get here in USR mode if this is a
9009              * gdb stub write; for this case we follow the architectural
9010              * behaviour for guest writes in USR mode of ignoring an attempt
9011              * to switch mode. (Those are caught by translate.c for writes
9012              * triggered by guest instructions.)
9013              */
9014             mask &= ~CPSR_M;
9015         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9016             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9017              * v7, and has defined behaviour in v8:
9018              *  + leave CPSR.M untouched
9019              *  + allow changes to the other CPSR fields
9020              *  + set PSTATE.IL
9021              * For user changes via the GDB stub, we don't set PSTATE.IL,
9022              * as this would be unnecessarily harsh for a user error.
9023              */
9024             mask &= ~CPSR_M;
9025             if (write_type != CPSRWriteByGDBStub &&
9026                 arm_feature(env, ARM_FEATURE_V8)) {
9027                 mask |= CPSR_IL;
9028                 val |= CPSR_IL;
9029             }
9030             qemu_log_mask(LOG_GUEST_ERROR,
9031                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9032                           aarch32_mode_name(env->uncached_cpsr),
9033                           aarch32_mode_name(val));
9034         } else {
9035             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9036                           write_type == CPSRWriteExceptionReturn ?
9037                           "Exception return from AArch32" :
9038                           "AArch32 mode switch from",
9039                           aarch32_mode_name(env->uncached_cpsr),
9040                           aarch32_mode_name(val), env->regs[15]);
9041             switch_mode(env, val & CPSR_M);
9042         }
9043     }
9044     mask &= ~CACHED_CPSR_BITS;
9045     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9046     if (rebuild_hflags) {
9047         arm_rebuild_hflags(env);
9048     }
9049 }
9050 
9051 /* Sign/zero extend */
9052 uint32_t HELPER(sxtb16)(uint32_t x)
9053 {
9054     uint32_t res;
9055     res = (uint16_t)(int8_t)x;
9056     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9057     return res;
9058 }
9059 
9060 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9061 {
9062     /*
9063      * Take a division-by-zero exception if necessary; otherwise return
9064      * to get the usual non-trapping division behaviour (result of 0)
9065      */
9066     if (arm_feature(env, ARM_FEATURE_M)
9067         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9068         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9069     }
9070 }
9071 
9072 uint32_t HELPER(uxtb16)(uint32_t x)
9073 {
9074     uint32_t res;
9075     res = (uint16_t)(uint8_t)x;
9076     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9077     return res;
9078 }
9079 
9080 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9081 {
9082     if (den == 0) {
9083         handle_possible_div0_trap(env, GETPC());
9084         return 0;
9085     }
9086     if (num == INT_MIN && den == -1) {
9087         return INT_MIN;
9088     }
9089     return num / den;
9090 }
9091 
9092 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9093 {
9094     if (den == 0) {
9095         handle_possible_div0_trap(env, GETPC());
9096         return 0;
9097     }
9098     return num / den;
9099 }
9100 
9101 uint32_t HELPER(rbit)(uint32_t x)
9102 {
9103     return revbit32(x);
9104 }
9105 
9106 #ifdef CONFIG_USER_ONLY
9107 
9108 static void switch_mode(CPUARMState *env, int mode)
9109 {
9110     ARMCPU *cpu = env_archcpu(env);
9111 
9112     if (mode != ARM_CPU_MODE_USR) {
9113         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9114     }
9115 }
9116 
9117 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9118                                  uint32_t cur_el, bool secure)
9119 {
9120     return 1;
9121 }
9122 
9123 void aarch64_sync_64_to_32(CPUARMState *env)
9124 {
9125     g_assert_not_reached();
9126 }
9127 
9128 #else
9129 
9130 static void switch_mode(CPUARMState *env, int mode)
9131 {
9132     int old_mode;
9133     int i;
9134 
9135     old_mode = env->uncached_cpsr & CPSR_M;
9136     if (mode == old_mode)
9137         return;
9138 
9139     if (old_mode == ARM_CPU_MODE_FIQ) {
9140         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9141         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9142     } else if (mode == ARM_CPU_MODE_FIQ) {
9143         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9144         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9145     }
9146 
9147     i = bank_number(old_mode);
9148     env->banked_r13[i] = env->regs[13];
9149     env->banked_spsr[i] = env->spsr;
9150 
9151     i = bank_number(mode);
9152     env->regs[13] = env->banked_r13[i];
9153     env->spsr = env->banked_spsr[i];
9154 
9155     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9156     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9157 }
9158 
9159 /* Physical Interrupt Target EL Lookup Table
9160  *
9161  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9162  *
9163  * The below multi-dimensional table is used for looking up the target
9164  * exception level given numerous condition criteria.  Specifically, the
9165  * target EL is based on SCR and HCR routing controls as well as the
9166  * currently executing EL and secure state.
9167  *
9168  *    Dimensions:
9169  *    target_el_table[2][2][2][2][2][4]
9170  *                    |  |  |  |  |  +--- Current EL
9171  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9172  *                    |  |  |  +--------- HCR mask override
9173  *                    |  |  +------------ SCR exec state control
9174  *                    |  +--------------- SCR mask override
9175  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9176  *
9177  *    The table values are as such:
9178  *    0-3 = EL0-EL3
9179  *     -1 = Cannot occur
9180  *
9181  * The ARM ARM target EL table includes entries indicating that an "exception
9182  * is not taken".  The two cases where this is applicable are:
9183  *    1) An exception is taken from EL3 but the SCR does not have the exception
9184  *    routed to EL3.
9185  *    2) An exception is taken from EL2 but the HCR does not have the exception
9186  *    routed to EL2.
9187  * In these two cases, the below table contain a target of EL1.  This value is
9188  * returned as it is expected that the consumer of the table data will check
9189  * for "target EL >= current EL" to ensure the exception is not taken.
9190  *
9191  *            SCR     HCR
9192  *         64  EA     AMO                 From
9193  *        BIT IRQ     IMO      Non-secure         Secure
9194  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9195  */
9196 static const int8_t target_el_table[2][2][2][2][2][4] = {
9197     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9198        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9199       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9200        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9201      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9202        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9203       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9204        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9205     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9206        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9207       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9208        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9209      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9210        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9211       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9212        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9213 };
9214 
9215 /*
9216  * Determine the target EL for physical exceptions
9217  */
9218 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9219                                  uint32_t cur_el, bool secure)
9220 {
9221     CPUARMState *env = cs->env_ptr;
9222     bool rw;
9223     bool scr;
9224     bool hcr;
9225     int target_el;
9226     /* Is the highest EL AArch64? */
9227     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9228     uint64_t hcr_el2;
9229 
9230     if (arm_feature(env, ARM_FEATURE_EL3)) {
9231         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9232     } else {
9233         /* Either EL2 is the highest EL (and so the EL2 register width
9234          * is given by is64); or there is no EL2 or EL3, in which case
9235          * the value of 'rw' does not affect the table lookup anyway.
9236          */
9237         rw = is64;
9238     }
9239 
9240     hcr_el2 = arm_hcr_el2_eff(env);
9241     switch (excp_idx) {
9242     case EXCP_IRQ:
9243         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9244         hcr = hcr_el2 & HCR_IMO;
9245         break;
9246     case EXCP_FIQ:
9247         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9248         hcr = hcr_el2 & HCR_FMO;
9249         break;
9250     default:
9251         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9252         hcr = hcr_el2 & HCR_AMO;
9253         break;
9254     };
9255 
9256     /*
9257      * For these purposes, TGE and AMO/IMO/FMO both force the
9258      * interrupt to EL2.  Fold TGE into the bit extracted above.
9259      */
9260     hcr |= (hcr_el2 & HCR_TGE) != 0;
9261 
9262     /* Perform a table-lookup for the target EL given the current state */
9263     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9264 
9265     assert(target_el > 0);
9266 
9267     return target_el;
9268 }
9269 
9270 void arm_log_exception(CPUState *cs)
9271 {
9272     int idx = cs->exception_index;
9273 
9274     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9275         const char *exc = NULL;
9276         static const char * const excnames[] = {
9277             [EXCP_UDEF] = "Undefined Instruction",
9278             [EXCP_SWI] = "SVC",
9279             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9280             [EXCP_DATA_ABORT] = "Data Abort",
9281             [EXCP_IRQ] = "IRQ",
9282             [EXCP_FIQ] = "FIQ",
9283             [EXCP_BKPT] = "Breakpoint",
9284             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9285             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9286             [EXCP_HVC] = "Hypervisor Call",
9287             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9288             [EXCP_SMC] = "Secure Monitor Call",
9289             [EXCP_VIRQ] = "Virtual IRQ",
9290             [EXCP_VFIQ] = "Virtual FIQ",
9291             [EXCP_SEMIHOST] = "Semihosting call",
9292             [EXCP_NOCP] = "v7M NOCP UsageFault",
9293             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9294             [EXCP_STKOF] = "v8M STKOF UsageFault",
9295             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9296             [EXCP_LSERR] = "v8M LSERR UsageFault",
9297             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9298             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9299             [EXCP_VSERR] = "Virtual SERR",
9300         };
9301 
9302         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9303             exc = excnames[idx];
9304         }
9305         if (!exc) {
9306             exc = "unknown";
9307         }
9308         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9309                       idx, exc, cs->cpu_index);
9310     }
9311 }
9312 
9313 /*
9314  * Function used to synchronize QEMU's AArch64 register set with AArch32
9315  * register set.  This is necessary when switching between AArch32 and AArch64
9316  * execution state.
9317  */
9318 void aarch64_sync_32_to_64(CPUARMState *env)
9319 {
9320     int i;
9321     uint32_t mode = env->uncached_cpsr & CPSR_M;
9322 
9323     /* We can blanket copy R[0:7] to X[0:7] */
9324     for (i = 0; i < 8; i++) {
9325         env->xregs[i] = env->regs[i];
9326     }
9327 
9328     /*
9329      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9330      * Otherwise, they come from the banked user regs.
9331      */
9332     if (mode == ARM_CPU_MODE_FIQ) {
9333         for (i = 8; i < 13; i++) {
9334             env->xregs[i] = env->usr_regs[i - 8];
9335         }
9336     } else {
9337         for (i = 8; i < 13; i++) {
9338             env->xregs[i] = env->regs[i];
9339         }
9340     }
9341 
9342     /*
9343      * Registers x13-x23 are the various mode SP and FP registers. Registers
9344      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9345      * from the mode banked register.
9346      */
9347     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9348         env->xregs[13] = env->regs[13];
9349         env->xregs[14] = env->regs[14];
9350     } else {
9351         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9352         /* HYP is an exception in that it is copied from r14 */
9353         if (mode == ARM_CPU_MODE_HYP) {
9354             env->xregs[14] = env->regs[14];
9355         } else {
9356             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9357         }
9358     }
9359 
9360     if (mode == ARM_CPU_MODE_HYP) {
9361         env->xregs[15] = env->regs[13];
9362     } else {
9363         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9364     }
9365 
9366     if (mode == ARM_CPU_MODE_IRQ) {
9367         env->xregs[16] = env->regs[14];
9368         env->xregs[17] = env->regs[13];
9369     } else {
9370         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9371         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9372     }
9373 
9374     if (mode == ARM_CPU_MODE_SVC) {
9375         env->xregs[18] = env->regs[14];
9376         env->xregs[19] = env->regs[13];
9377     } else {
9378         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9379         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9380     }
9381 
9382     if (mode == ARM_CPU_MODE_ABT) {
9383         env->xregs[20] = env->regs[14];
9384         env->xregs[21] = env->regs[13];
9385     } else {
9386         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9387         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9388     }
9389 
9390     if (mode == ARM_CPU_MODE_UND) {
9391         env->xregs[22] = env->regs[14];
9392         env->xregs[23] = env->regs[13];
9393     } else {
9394         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9395         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9396     }
9397 
9398     /*
9399      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9400      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9401      * FIQ bank for r8-r14.
9402      */
9403     if (mode == ARM_CPU_MODE_FIQ) {
9404         for (i = 24; i < 31; i++) {
9405             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9406         }
9407     } else {
9408         for (i = 24; i < 29; i++) {
9409             env->xregs[i] = env->fiq_regs[i - 24];
9410         }
9411         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9412         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9413     }
9414 
9415     env->pc = env->regs[15];
9416 }
9417 
9418 /*
9419  * Function used to synchronize QEMU's AArch32 register set with AArch64
9420  * register set.  This is necessary when switching between AArch32 and AArch64
9421  * execution state.
9422  */
9423 void aarch64_sync_64_to_32(CPUARMState *env)
9424 {
9425     int i;
9426     uint32_t mode = env->uncached_cpsr & CPSR_M;
9427 
9428     /* We can blanket copy X[0:7] to R[0:7] */
9429     for (i = 0; i < 8; i++) {
9430         env->regs[i] = env->xregs[i];
9431     }
9432 
9433     /*
9434      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9435      * Otherwise, we copy x8-x12 into the banked user regs.
9436      */
9437     if (mode == ARM_CPU_MODE_FIQ) {
9438         for (i = 8; i < 13; i++) {
9439             env->usr_regs[i - 8] = env->xregs[i];
9440         }
9441     } else {
9442         for (i = 8; i < 13; i++) {
9443             env->regs[i] = env->xregs[i];
9444         }
9445     }
9446 
9447     /*
9448      * Registers r13 & r14 depend on the current mode.
9449      * If we are in a given mode, we copy the corresponding x registers to r13
9450      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9451      * for the mode.
9452      */
9453     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9454         env->regs[13] = env->xregs[13];
9455         env->regs[14] = env->xregs[14];
9456     } else {
9457         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9458 
9459         /*
9460          * HYP is an exception in that it does not have its own banked r14 but
9461          * shares the USR r14
9462          */
9463         if (mode == ARM_CPU_MODE_HYP) {
9464             env->regs[14] = env->xregs[14];
9465         } else {
9466             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9467         }
9468     }
9469 
9470     if (mode == ARM_CPU_MODE_HYP) {
9471         env->regs[13] = env->xregs[15];
9472     } else {
9473         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9474     }
9475 
9476     if (mode == ARM_CPU_MODE_IRQ) {
9477         env->regs[14] = env->xregs[16];
9478         env->regs[13] = env->xregs[17];
9479     } else {
9480         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9481         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9482     }
9483 
9484     if (mode == ARM_CPU_MODE_SVC) {
9485         env->regs[14] = env->xregs[18];
9486         env->regs[13] = env->xregs[19];
9487     } else {
9488         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9489         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9490     }
9491 
9492     if (mode == ARM_CPU_MODE_ABT) {
9493         env->regs[14] = env->xregs[20];
9494         env->regs[13] = env->xregs[21];
9495     } else {
9496         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9497         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9498     }
9499 
9500     if (mode == ARM_CPU_MODE_UND) {
9501         env->regs[14] = env->xregs[22];
9502         env->regs[13] = env->xregs[23];
9503     } else {
9504         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9505         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9506     }
9507 
9508     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9509      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9510      * FIQ bank for r8-r14.
9511      */
9512     if (mode == ARM_CPU_MODE_FIQ) {
9513         for (i = 24; i < 31; i++) {
9514             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9515         }
9516     } else {
9517         for (i = 24; i < 29; i++) {
9518             env->fiq_regs[i - 24] = env->xregs[i];
9519         }
9520         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9521         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9522     }
9523 
9524     env->regs[15] = env->pc;
9525 }
9526 
9527 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9528                                    uint32_t mask, uint32_t offset,
9529                                    uint32_t newpc)
9530 {
9531     int new_el;
9532 
9533     /* Change the CPU state so as to actually take the exception. */
9534     switch_mode(env, new_mode);
9535 
9536     /*
9537      * For exceptions taken to AArch32 we must clear the SS bit in both
9538      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9539      */
9540     env->pstate &= ~PSTATE_SS;
9541     env->spsr = cpsr_read(env);
9542     /* Clear IT bits.  */
9543     env->condexec_bits = 0;
9544     /* Switch to the new mode, and to the correct instruction set.  */
9545     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9546 
9547     /* This must be after mode switching. */
9548     new_el = arm_current_el(env);
9549 
9550     /* Set new mode endianness */
9551     env->uncached_cpsr &= ~CPSR_E;
9552     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9553         env->uncached_cpsr |= CPSR_E;
9554     }
9555     /* J and IL must always be cleared for exception entry */
9556     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9557     env->daif |= mask;
9558 
9559     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9560         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9561             env->uncached_cpsr |= CPSR_SSBS;
9562         } else {
9563             env->uncached_cpsr &= ~CPSR_SSBS;
9564         }
9565     }
9566 
9567     if (new_mode == ARM_CPU_MODE_HYP) {
9568         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9569         env->elr_el[2] = env->regs[15];
9570     } else {
9571         /* CPSR.PAN is normally preserved preserved unless...  */
9572         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9573             switch (new_el) {
9574             case 3:
9575                 if (!arm_is_secure_below_el3(env)) {
9576                     /* ... the target is EL3, from non-secure state.  */
9577                     env->uncached_cpsr &= ~CPSR_PAN;
9578                     break;
9579                 }
9580                 /* ... the target is EL3, from secure state ... */
9581                 /* fall through */
9582             case 1:
9583                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9584                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9585                     env->uncached_cpsr |= CPSR_PAN;
9586                 }
9587                 break;
9588             }
9589         }
9590         /*
9591          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9592          * and we should just guard the thumb mode on V4
9593          */
9594         if (arm_feature(env, ARM_FEATURE_V4T)) {
9595             env->thumb =
9596                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9597         }
9598         env->regs[14] = env->regs[15] + offset;
9599     }
9600     env->regs[15] = newpc;
9601     arm_rebuild_hflags(env);
9602 }
9603 
9604 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9605 {
9606     /*
9607      * Handle exception entry to Hyp mode; this is sufficiently
9608      * different to entry to other AArch32 modes that we handle it
9609      * separately here.
9610      *
9611      * The vector table entry used is always the 0x14 Hyp mode entry point,
9612      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9613      * The offset applied to the preferred return address is always zero
9614      * (see DDI0487C.a section G1.12.3).
9615      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9616      */
9617     uint32_t addr, mask;
9618     ARMCPU *cpu = ARM_CPU(cs);
9619     CPUARMState *env = &cpu->env;
9620 
9621     switch (cs->exception_index) {
9622     case EXCP_UDEF:
9623         addr = 0x04;
9624         break;
9625     case EXCP_SWI:
9626         addr = 0x08;
9627         break;
9628     case EXCP_BKPT:
9629         /* Fall through to prefetch abort.  */
9630     case EXCP_PREFETCH_ABORT:
9631         env->cp15.ifar_s = env->exception.vaddress;
9632         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9633                       (uint32_t)env->exception.vaddress);
9634         addr = 0x0c;
9635         break;
9636     case EXCP_DATA_ABORT:
9637         env->cp15.dfar_s = env->exception.vaddress;
9638         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9639                       (uint32_t)env->exception.vaddress);
9640         addr = 0x10;
9641         break;
9642     case EXCP_IRQ:
9643         addr = 0x18;
9644         break;
9645     case EXCP_FIQ:
9646         addr = 0x1c;
9647         break;
9648     case EXCP_HVC:
9649         addr = 0x08;
9650         break;
9651     case EXCP_HYP_TRAP:
9652         addr = 0x14;
9653         break;
9654     default:
9655         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9656     }
9657 
9658     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9659         if (!arm_feature(env, ARM_FEATURE_V8)) {
9660             /*
9661              * QEMU syndrome values are v8-style. v7 has the IL bit
9662              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9663              * If this is a v7 CPU, squash the IL bit in those cases.
9664              */
9665             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9666                 (cs->exception_index == EXCP_DATA_ABORT &&
9667                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9668                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9669                 env->exception.syndrome &= ~ARM_EL_IL;
9670             }
9671         }
9672         env->cp15.esr_el[2] = env->exception.syndrome;
9673     }
9674 
9675     if (arm_current_el(env) != 2 && addr < 0x14) {
9676         addr = 0x14;
9677     }
9678 
9679     mask = 0;
9680     if (!(env->cp15.scr_el3 & SCR_EA)) {
9681         mask |= CPSR_A;
9682     }
9683     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9684         mask |= CPSR_I;
9685     }
9686     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9687         mask |= CPSR_F;
9688     }
9689 
9690     addr += env->cp15.hvbar;
9691 
9692     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9693 }
9694 
9695 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9696 {
9697     ARMCPU *cpu = ARM_CPU(cs);
9698     CPUARMState *env = &cpu->env;
9699     uint32_t addr;
9700     uint32_t mask;
9701     int new_mode;
9702     uint32_t offset;
9703     uint32_t moe;
9704 
9705     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9706     switch (syn_get_ec(env->exception.syndrome)) {
9707     case EC_BREAKPOINT:
9708     case EC_BREAKPOINT_SAME_EL:
9709         moe = 1;
9710         break;
9711     case EC_WATCHPOINT:
9712     case EC_WATCHPOINT_SAME_EL:
9713         moe = 10;
9714         break;
9715     case EC_AA32_BKPT:
9716         moe = 3;
9717         break;
9718     case EC_VECTORCATCH:
9719         moe = 5;
9720         break;
9721     default:
9722         moe = 0;
9723         break;
9724     }
9725 
9726     if (moe) {
9727         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9728     }
9729 
9730     if (env->exception.target_el == 2) {
9731         arm_cpu_do_interrupt_aarch32_hyp(cs);
9732         return;
9733     }
9734 
9735     switch (cs->exception_index) {
9736     case EXCP_UDEF:
9737         new_mode = ARM_CPU_MODE_UND;
9738         addr = 0x04;
9739         mask = CPSR_I;
9740         if (env->thumb)
9741             offset = 2;
9742         else
9743             offset = 4;
9744         break;
9745     case EXCP_SWI:
9746         new_mode = ARM_CPU_MODE_SVC;
9747         addr = 0x08;
9748         mask = CPSR_I;
9749         /* The PC already points to the next instruction.  */
9750         offset = 0;
9751         break;
9752     case EXCP_BKPT:
9753         /* Fall through to prefetch abort.  */
9754     case EXCP_PREFETCH_ABORT:
9755         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9756         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9757         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9758                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9759         new_mode = ARM_CPU_MODE_ABT;
9760         addr = 0x0c;
9761         mask = CPSR_A | CPSR_I;
9762         offset = 4;
9763         break;
9764     case EXCP_DATA_ABORT:
9765         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9766         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9767         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9768                       env->exception.fsr,
9769                       (uint32_t)env->exception.vaddress);
9770         new_mode = ARM_CPU_MODE_ABT;
9771         addr = 0x10;
9772         mask = CPSR_A | CPSR_I;
9773         offset = 8;
9774         break;
9775     case EXCP_IRQ:
9776         new_mode = ARM_CPU_MODE_IRQ;
9777         addr = 0x18;
9778         /* Disable IRQ and imprecise data aborts.  */
9779         mask = CPSR_A | CPSR_I;
9780         offset = 4;
9781         if (env->cp15.scr_el3 & SCR_IRQ) {
9782             /* IRQ routed to monitor mode */
9783             new_mode = ARM_CPU_MODE_MON;
9784             mask |= CPSR_F;
9785         }
9786         break;
9787     case EXCP_FIQ:
9788         new_mode = ARM_CPU_MODE_FIQ;
9789         addr = 0x1c;
9790         /* Disable FIQ, IRQ and imprecise data aborts.  */
9791         mask = CPSR_A | CPSR_I | CPSR_F;
9792         if (env->cp15.scr_el3 & SCR_FIQ) {
9793             /* FIQ routed to monitor mode */
9794             new_mode = ARM_CPU_MODE_MON;
9795         }
9796         offset = 4;
9797         break;
9798     case EXCP_VIRQ:
9799         new_mode = ARM_CPU_MODE_IRQ;
9800         addr = 0x18;
9801         /* Disable IRQ and imprecise data aborts.  */
9802         mask = CPSR_A | CPSR_I;
9803         offset = 4;
9804         break;
9805     case EXCP_VFIQ:
9806         new_mode = ARM_CPU_MODE_FIQ;
9807         addr = 0x1c;
9808         /* Disable FIQ, IRQ and imprecise data aborts.  */
9809         mask = CPSR_A | CPSR_I | CPSR_F;
9810         offset = 4;
9811         break;
9812     case EXCP_VSERR:
9813         {
9814             /*
9815              * Note that this is reported as a data abort, but the DFAR
9816              * has an UNKNOWN value.  Construct the SError syndrome from
9817              * AET and ExT fields.
9818              */
9819             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9820 
9821             if (extended_addresses_enabled(env)) {
9822                 env->exception.fsr = arm_fi_to_lfsc(&fi);
9823             } else {
9824                 env->exception.fsr = arm_fi_to_sfsc(&fi);
9825             }
9826             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9827             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9828             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9829                           env->exception.fsr);
9830 
9831             new_mode = ARM_CPU_MODE_ABT;
9832             addr = 0x10;
9833             mask = CPSR_A | CPSR_I;
9834             offset = 8;
9835         }
9836         break;
9837     case EXCP_SMC:
9838         new_mode = ARM_CPU_MODE_MON;
9839         addr = 0x08;
9840         mask = CPSR_A | CPSR_I | CPSR_F;
9841         offset = 0;
9842         break;
9843     default:
9844         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9845         return; /* Never happens.  Keep compiler happy.  */
9846     }
9847 
9848     if (new_mode == ARM_CPU_MODE_MON) {
9849         addr += env->cp15.mvbar;
9850     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9851         /* High vectors. When enabled, base address cannot be remapped. */
9852         addr += 0xffff0000;
9853     } else {
9854         /* ARM v7 architectures provide a vector base address register to remap
9855          * the interrupt vector table.
9856          * This register is only followed in non-monitor mode, and is banked.
9857          * Note: only bits 31:5 are valid.
9858          */
9859         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9860     }
9861 
9862     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9863         env->cp15.scr_el3 &= ~SCR_NS;
9864     }
9865 
9866     take_aarch32_exception(env, new_mode, mask, offset, addr);
9867 }
9868 
9869 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9870 {
9871     /*
9872      * Return the register number of the AArch64 view of the AArch32
9873      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9874      * be that of the AArch32 mode the exception came from.
9875      */
9876     int mode = env->uncached_cpsr & CPSR_M;
9877 
9878     switch (aarch32_reg) {
9879     case 0 ... 7:
9880         return aarch32_reg;
9881     case 8 ... 12:
9882         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9883     case 13:
9884         switch (mode) {
9885         case ARM_CPU_MODE_USR:
9886         case ARM_CPU_MODE_SYS:
9887             return 13;
9888         case ARM_CPU_MODE_HYP:
9889             return 15;
9890         case ARM_CPU_MODE_IRQ:
9891             return 17;
9892         case ARM_CPU_MODE_SVC:
9893             return 19;
9894         case ARM_CPU_MODE_ABT:
9895             return 21;
9896         case ARM_CPU_MODE_UND:
9897             return 23;
9898         case ARM_CPU_MODE_FIQ:
9899             return 29;
9900         default:
9901             g_assert_not_reached();
9902         }
9903     case 14:
9904         switch (mode) {
9905         case ARM_CPU_MODE_USR:
9906         case ARM_CPU_MODE_SYS:
9907         case ARM_CPU_MODE_HYP:
9908             return 14;
9909         case ARM_CPU_MODE_IRQ:
9910             return 16;
9911         case ARM_CPU_MODE_SVC:
9912             return 18;
9913         case ARM_CPU_MODE_ABT:
9914             return 20;
9915         case ARM_CPU_MODE_UND:
9916             return 22;
9917         case ARM_CPU_MODE_FIQ:
9918             return 30;
9919         default:
9920             g_assert_not_reached();
9921         }
9922     case 15:
9923         return 31;
9924     default:
9925         g_assert_not_reached();
9926     }
9927 }
9928 
9929 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9930 {
9931     uint32_t ret = cpsr_read(env);
9932 
9933     /* Move DIT to the correct location for SPSR_ELx */
9934     if (ret & CPSR_DIT) {
9935         ret &= ~CPSR_DIT;
9936         ret |= PSTATE_DIT;
9937     }
9938     /* Merge PSTATE.SS into SPSR_ELx */
9939     ret |= env->pstate & PSTATE_SS;
9940 
9941     return ret;
9942 }
9943 
9944 static bool syndrome_is_sync_extabt(uint32_t syndrome)
9945 {
9946     /* Return true if this syndrome value is a synchronous external abort */
9947     switch (syn_get_ec(syndrome)) {
9948     case EC_INSNABORT:
9949     case EC_INSNABORT_SAME_EL:
9950     case EC_DATAABORT:
9951     case EC_DATAABORT_SAME_EL:
9952         /* Look at fault status code for all the synchronous ext abort cases */
9953         switch (syndrome & 0x3f) {
9954         case 0x10:
9955         case 0x13:
9956         case 0x14:
9957         case 0x15:
9958         case 0x16:
9959         case 0x17:
9960             return true;
9961         default:
9962             return false;
9963         }
9964     default:
9965         return false;
9966     }
9967 }
9968 
9969 /* Handle exception entry to a target EL which is using AArch64 */
9970 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9971 {
9972     ARMCPU *cpu = ARM_CPU(cs);
9973     CPUARMState *env = &cpu->env;
9974     unsigned int new_el = env->exception.target_el;
9975     target_ulong addr = env->cp15.vbar_el[new_el];
9976     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9977     unsigned int old_mode;
9978     unsigned int cur_el = arm_current_el(env);
9979     int rt;
9980 
9981     /*
9982      * Note that new_el can never be 0.  If cur_el is 0, then
9983      * el0_a64 is is_a64(), else el0_a64 is ignored.
9984      */
9985     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9986 
9987     if (cur_el < new_el) {
9988         /* Entry vector offset depends on whether the implemented EL
9989          * immediately lower than the target level is using AArch32 or AArch64
9990          */
9991         bool is_aa64;
9992         uint64_t hcr;
9993 
9994         switch (new_el) {
9995         case 3:
9996             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9997             break;
9998         case 2:
9999             hcr = arm_hcr_el2_eff(env);
10000             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10001                 is_aa64 = (hcr & HCR_RW) != 0;
10002                 break;
10003             }
10004             /* fall through */
10005         case 1:
10006             is_aa64 = is_a64(env);
10007             break;
10008         default:
10009             g_assert_not_reached();
10010         }
10011 
10012         if (is_aa64) {
10013             addr += 0x400;
10014         } else {
10015             addr += 0x600;
10016         }
10017     } else if (pstate_read(env) & PSTATE_SP) {
10018         addr += 0x200;
10019     }
10020 
10021     switch (cs->exception_index) {
10022     case EXCP_PREFETCH_ABORT:
10023     case EXCP_DATA_ABORT:
10024         /*
10025          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10026          * to be taken to the SError vector entrypoint.
10027          */
10028         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10029             syndrome_is_sync_extabt(env->exception.syndrome)) {
10030             addr += 0x180;
10031         }
10032         env->cp15.far_el[new_el] = env->exception.vaddress;
10033         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10034                       env->cp15.far_el[new_el]);
10035         /* fall through */
10036     case EXCP_BKPT:
10037     case EXCP_UDEF:
10038     case EXCP_SWI:
10039     case EXCP_HVC:
10040     case EXCP_HYP_TRAP:
10041     case EXCP_SMC:
10042         switch (syn_get_ec(env->exception.syndrome)) {
10043         case EC_ADVSIMDFPACCESSTRAP:
10044             /*
10045              * QEMU internal FP/SIMD syndromes from AArch32 include the
10046              * TA and coproc fields which are only exposed if the exception
10047              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10048              * AArch64 format syndrome.
10049              */
10050             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10051             break;
10052         case EC_CP14RTTRAP:
10053         case EC_CP15RTTRAP:
10054         case EC_CP14DTTRAP:
10055             /*
10056              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10057              * the raw register field from the insn; when taking this to
10058              * AArch64 we must convert it to the AArch64 view of the register
10059              * number. Notice that we read a 4-bit AArch32 register number and
10060              * write back a 5-bit AArch64 one.
10061              */
10062             rt = extract32(env->exception.syndrome, 5, 4);
10063             rt = aarch64_regnum(env, rt);
10064             env->exception.syndrome = deposit32(env->exception.syndrome,
10065                                                 5, 5, rt);
10066             break;
10067         case EC_CP15RRTTRAP:
10068         case EC_CP14RRTTRAP:
10069             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10070             rt = extract32(env->exception.syndrome, 5, 4);
10071             rt = aarch64_regnum(env, rt);
10072             env->exception.syndrome = deposit32(env->exception.syndrome,
10073                                                 5, 5, rt);
10074             rt = extract32(env->exception.syndrome, 10, 4);
10075             rt = aarch64_regnum(env, rt);
10076             env->exception.syndrome = deposit32(env->exception.syndrome,
10077                                                 10, 5, rt);
10078             break;
10079         }
10080         env->cp15.esr_el[new_el] = env->exception.syndrome;
10081         break;
10082     case EXCP_IRQ:
10083     case EXCP_VIRQ:
10084         addr += 0x80;
10085         break;
10086     case EXCP_FIQ:
10087     case EXCP_VFIQ:
10088         addr += 0x100;
10089         break;
10090     case EXCP_VSERR:
10091         addr += 0x180;
10092         /* Construct the SError syndrome from IDS and ISS fields. */
10093         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10094         env->cp15.esr_el[new_el] = env->exception.syndrome;
10095         break;
10096     default:
10097         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10098     }
10099 
10100     if (is_a64(env)) {
10101         old_mode = pstate_read(env);
10102         aarch64_save_sp(env, arm_current_el(env));
10103         env->elr_el[new_el] = env->pc;
10104     } else {
10105         old_mode = cpsr_read_for_spsr_elx(env);
10106         env->elr_el[new_el] = env->regs[15];
10107 
10108         aarch64_sync_32_to_64(env);
10109 
10110         env->condexec_bits = 0;
10111     }
10112     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10113 
10114     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10115                   env->elr_el[new_el]);
10116 
10117     if (cpu_isar_feature(aa64_pan, cpu)) {
10118         /* The value of PSTATE.PAN is normally preserved, except when ... */
10119         new_mode |= old_mode & PSTATE_PAN;
10120         switch (new_el) {
10121         case 2:
10122             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10123             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10124                 != (HCR_E2H | HCR_TGE)) {
10125                 break;
10126             }
10127             /* fall through */
10128         case 1:
10129             /* ... the target is EL1 ... */
10130             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10131             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10132                 new_mode |= PSTATE_PAN;
10133             }
10134             break;
10135         }
10136     }
10137     if (cpu_isar_feature(aa64_mte, cpu)) {
10138         new_mode |= PSTATE_TCO;
10139     }
10140 
10141     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10142         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10143             new_mode |= PSTATE_SSBS;
10144         } else {
10145             new_mode &= ~PSTATE_SSBS;
10146         }
10147     }
10148 
10149     pstate_write(env, PSTATE_DAIF | new_mode);
10150     env->aarch64 = true;
10151     aarch64_restore_sp(env, new_el);
10152     helper_rebuild_hflags_a64(env, new_el);
10153 
10154     env->pc = addr;
10155 
10156     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10157                   new_el, env->pc, pstate_read(env));
10158 }
10159 
10160 /*
10161  * Do semihosting call and set the appropriate return value. All the
10162  * permission and validity checks have been done at translate time.
10163  *
10164  * We only see semihosting exceptions in TCG only as they are not
10165  * trapped to the hypervisor in KVM.
10166  */
10167 #ifdef CONFIG_TCG
10168 static void handle_semihosting(CPUState *cs)
10169 {
10170     ARMCPU *cpu = ARM_CPU(cs);
10171     CPUARMState *env = &cpu->env;
10172 
10173     if (is_a64(env)) {
10174         qemu_log_mask(CPU_LOG_INT,
10175                       "...handling as semihosting call 0x%" PRIx64 "\n",
10176                       env->xregs[0]);
10177         do_common_semihosting(cs);
10178         env->pc += 4;
10179     } else {
10180         qemu_log_mask(CPU_LOG_INT,
10181                       "...handling as semihosting call 0x%x\n",
10182                       env->regs[0]);
10183         do_common_semihosting(cs);
10184         env->regs[15] += env->thumb ? 2 : 4;
10185     }
10186 }
10187 #endif
10188 
10189 /* Handle a CPU exception for A and R profile CPUs.
10190  * Do any appropriate logging, handle PSCI calls, and then hand off
10191  * to the AArch64-entry or AArch32-entry function depending on the
10192  * target exception level's register width.
10193  *
10194  * Note: this is used for both TCG (as the do_interrupt tcg op),
10195  *       and KVM to re-inject guest debug exceptions, and to
10196  *       inject a Synchronous-External-Abort.
10197  */
10198 void arm_cpu_do_interrupt(CPUState *cs)
10199 {
10200     ARMCPU *cpu = ARM_CPU(cs);
10201     CPUARMState *env = &cpu->env;
10202     unsigned int new_el = env->exception.target_el;
10203 
10204     assert(!arm_feature(env, ARM_FEATURE_M));
10205 
10206     arm_log_exception(cs);
10207     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10208                   new_el);
10209     if (qemu_loglevel_mask(CPU_LOG_INT)
10210         && !excp_is_internal(cs->exception_index)) {
10211         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10212                       syn_get_ec(env->exception.syndrome),
10213                       env->exception.syndrome);
10214     }
10215 
10216     if (arm_is_psci_call(cpu, cs->exception_index)) {
10217         arm_handle_psci_call(cpu);
10218         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10219         return;
10220     }
10221 
10222     /*
10223      * Semihosting semantics depend on the register width of the code
10224      * that caused the exception, not the target exception level, so
10225      * must be handled here.
10226      */
10227 #ifdef CONFIG_TCG
10228     if (cs->exception_index == EXCP_SEMIHOST) {
10229         handle_semihosting(cs);
10230         return;
10231     }
10232 #endif
10233 
10234     /* Hooks may change global state so BQL should be held, also the
10235      * BQL needs to be held for any modification of
10236      * cs->interrupt_request.
10237      */
10238     g_assert(qemu_mutex_iothread_locked());
10239 
10240     arm_call_pre_el_change_hook(cpu);
10241 
10242     assert(!excp_is_internal(cs->exception_index));
10243     if (arm_el_is_aa64(env, new_el)) {
10244         arm_cpu_do_interrupt_aarch64(cs);
10245     } else {
10246         arm_cpu_do_interrupt_aarch32(cs);
10247     }
10248 
10249     arm_call_el_change_hook(cpu);
10250 
10251     if (!kvm_enabled()) {
10252         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10253     }
10254 }
10255 #endif /* !CONFIG_USER_ONLY */
10256 
10257 uint64_t arm_sctlr(CPUARMState *env, int el)
10258 {
10259     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10260     if (el == 0) {
10261         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10262         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10263              ? 2 : 1;
10264     }
10265     return env->cp15.sctlr_el[el];
10266 }
10267 
10268 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10269 {
10270     if (regime_has_2_ranges(mmu_idx)) {
10271         return extract64(tcr, 37, 2);
10272     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10273         return 0; /* VTCR_EL2 */
10274     } else {
10275         /* Replicate the single TBI bit so we always have 2 bits.  */
10276         return extract32(tcr, 20, 1) * 3;
10277     }
10278 }
10279 
10280 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10281 {
10282     if (regime_has_2_ranges(mmu_idx)) {
10283         return extract64(tcr, 51, 2);
10284     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10285         return 0; /* VTCR_EL2 */
10286     } else {
10287         /* Replicate the single TBID bit so we always have 2 bits.  */
10288         return extract32(tcr, 29, 1) * 3;
10289     }
10290 }
10291 
10292 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10293 {
10294     if (regime_has_2_ranges(mmu_idx)) {
10295         return extract64(tcr, 57, 2);
10296     } else {
10297         /* Replicate the single TCMA bit so we always have 2 bits.  */
10298         return extract32(tcr, 30, 1) * 3;
10299     }
10300 }
10301 
10302 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10303                                    ARMMMUIdx mmu_idx, bool data)
10304 {
10305     uint64_t tcr = regime_tcr(env, mmu_idx);
10306     bool epd, hpd, using16k, using64k, tsz_oob, ds;
10307     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10308     ARMCPU *cpu = env_archcpu(env);
10309 
10310     if (!regime_has_2_ranges(mmu_idx)) {
10311         select = 0;
10312         tsz = extract32(tcr, 0, 6);
10313         using64k = extract32(tcr, 14, 1);
10314         using16k = extract32(tcr, 15, 1);
10315         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10316             /* VTCR_EL2 */
10317             hpd = false;
10318         } else {
10319             hpd = extract32(tcr, 24, 1);
10320         }
10321         epd = false;
10322         sh = extract32(tcr, 12, 2);
10323         ps = extract32(tcr, 16, 3);
10324         ds = extract64(tcr, 32, 1);
10325     } else {
10326         /*
10327          * Bit 55 is always between the two regions, and is canonical for
10328          * determining if address tagging is enabled.
10329          */
10330         select = extract64(va, 55, 1);
10331         if (!select) {
10332             tsz = extract32(tcr, 0, 6);
10333             epd = extract32(tcr, 7, 1);
10334             sh = extract32(tcr, 12, 2);
10335             using64k = extract32(tcr, 14, 1);
10336             using16k = extract32(tcr, 15, 1);
10337             hpd = extract64(tcr, 41, 1);
10338         } else {
10339             int tg = extract32(tcr, 30, 2);
10340             using16k = tg == 1;
10341             using64k = tg == 3;
10342             tsz = extract32(tcr, 16, 6);
10343             epd = extract32(tcr, 23, 1);
10344             sh = extract32(tcr, 28, 2);
10345             hpd = extract64(tcr, 42, 1);
10346         }
10347         ps = extract64(tcr, 32, 3);
10348         ds = extract64(tcr, 59, 1);
10349     }
10350 
10351     if (cpu_isar_feature(aa64_st, cpu)) {
10352         max_tsz = 48 - using64k;
10353     } else {
10354         max_tsz = 39;
10355     }
10356 
10357     /*
10358      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10359      * adjust the effective value of DS, as documented.
10360      */
10361     min_tsz = 16;
10362     if (using64k) {
10363         if (cpu_isar_feature(aa64_lva, cpu)) {
10364             min_tsz = 12;
10365         }
10366         ds = false;
10367     } else if (ds) {
10368         switch (mmu_idx) {
10369         case ARMMMUIdx_Stage2:
10370         case ARMMMUIdx_Stage2_S:
10371             if (using16k) {
10372                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10373             } else {
10374                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10375             }
10376             break;
10377         default:
10378             if (using16k) {
10379                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10380             } else {
10381                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10382             }
10383             break;
10384         }
10385         if (ds) {
10386             min_tsz = 12;
10387         }
10388     }
10389 
10390     if (tsz > max_tsz) {
10391         tsz = max_tsz;
10392         tsz_oob = true;
10393     } else if (tsz < min_tsz) {
10394         tsz = min_tsz;
10395         tsz_oob = true;
10396     } else {
10397         tsz_oob = false;
10398     }
10399 
10400     /* Present TBI as a composite with TBID.  */
10401     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10402     if (!data) {
10403         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10404     }
10405     tbi = (tbi >> select) & 1;
10406 
10407     return (ARMVAParameters) {
10408         .tsz = tsz,
10409         .ps = ps,
10410         .sh = sh,
10411         .select = select,
10412         .tbi = tbi,
10413         .epd = epd,
10414         .hpd = hpd,
10415         .using16k = using16k,
10416         .using64k = using64k,
10417         .tsz_oob = tsz_oob,
10418         .ds = ds,
10419     };
10420 }
10421 
10422 /* Note that signed overflow is undefined in C.  The following routines are
10423    careful to use unsigned types where modulo arithmetic is required.
10424    Failure to do so _will_ break on newer gcc.  */
10425 
10426 /* Signed saturating arithmetic.  */
10427 
10428 /* Perform 16-bit signed saturating addition.  */
10429 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10430 {
10431     uint16_t res;
10432 
10433     res = a + b;
10434     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10435         if (a & 0x8000)
10436             res = 0x8000;
10437         else
10438             res = 0x7fff;
10439     }
10440     return res;
10441 }
10442 
10443 /* Perform 8-bit signed saturating addition.  */
10444 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10445 {
10446     uint8_t res;
10447 
10448     res = a + b;
10449     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10450         if (a & 0x80)
10451             res = 0x80;
10452         else
10453             res = 0x7f;
10454     }
10455     return res;
10456 }
10457 
10458 /* Perform 16-bit signed saturating subtraction.  */
10459 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10460 {
10461     uint16_t res;
10462 
10463     res = a - b;
10464     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10465         if (a & 0x8000)
10466             res = 0x8000;
10467         else
10468             res = 0x7fff;
10469     }
10470     return res;
10471 }
10472 
10473 /* Perform 8-bit signed saturating subtraction.  */
10474 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10475 {
10476     uint8_t res;
10477 
10478     res = a - b;
10479     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10480         if (a & 0x80)
10481             res = 0x80;
10482         else
10483             res = 0x7f;
10484     }
10485     return res;
10486 }
10487 
10488 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10489 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10490 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10491 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10492 #define PFX q
10493 
10494 #include "op_addsub.h"
10495 
10496 /* Unsigned saturating arithmetic.  */
10497 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10498 {
10499     uint16_t res;
10500     res = a + b;
10501     if (res < a)
10502         res = 0xffff;
10503     return res;
10504 }
10505 
10506 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10507 {
10508     if (a > b)
10509         return a - b;
10510     else
10511         return 0;
10512 }
10513 
10514 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10515 {
10516     uint8_t res;
10517     res = a + b;
10518     if (res < a)
10519         res = 0xff;
10520     return res;
10521 }
10522 
10523 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10524 {
10525     if (a > b)
10526         return a - b;
10527     else
10528         return 0;
10529 }
10530 
10531 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10532 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10533 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10534 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10535 #define PFX uq
10536 
10537 #include "op_addsub.h"
10538 
10539 /* Signed modulo arithmetic.  */
10540 #define SARITH16(a, b, n, op) do { \
10541     int32_t sum; \
10542     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10543     RESULT(sum, n, 16); \
10544     if (sum >= 0) \
10545         ge |= 3 << (n * 2); \
10546     } while(0)
10547 
10548 #define SARITH8(a, b, n, op) do { \
10549     int32_t sum; \
10550     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10551     RESULT(sum, n, 8); \
10552     if (sum >= 0) \
10553         ge |= 1 << n; \
10554     } while(0)
10555 
10556 
10557 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10558 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10559 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10560 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10561 #define PFX s
10562 #define ARITH_GE
10563 
10564 #include "op_addsub.h"
10565 
10566 /* Unsigned modulo arithmetic.  */
10567 #define ADD16(a, b, n) do { \
10568     uint32_t sum; \
10569     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10570     RESULT(sum, n, 16); \
10571     if ((sum >> 16) == 1) \
10572         ge |= 3 << (n * 2); \
10573     } while(0)
10574 
10575 #define ADD8(a, b, n) do { \
10576     uint32_t sum; \
10577     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10578     RESULT(sum, n, 8); \
10579     if ((sum >> 8) == 1) \
10580         ge |= 1 << n; \
10581     } while(0)
10582 
10583 #define SUB16(a, b, n) do { \
10584     uint32_t sum; \
10585     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10586     RESULT(sum, n, 16); \
10587     if ((sum >> 16) == 0) \
10588         ge |= 3 << (n * 2); \
10589     } while(0)
10590 
10591 #define SUB8(a, b, n) do { \
10592     uint32_t sum; \
10593     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10594     RESULT(sum, n, 8); \
10595     if ((sum >> 8) == 0) \
10596         ge |= 1 << n; \
10597     } while(0)
10598 
10599 #define PFX u
10600 #define ARITH_GE
10601 
10602 #include "op_addsub.h"
10603 
10604 /* Halved signed arithmetic.  */
10605 #define ADD16(a, b, n) \
10606   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10607 #define SUB16(a, b, n) \
10608   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10609 #define ADD8(a, b, n) \
10610   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10611 #define SUB8(a, b, n) \
10612   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10613 #define PFX sh
10614 
10615 #include "op_addsub.h"
10616 
10617 /* Halved unsigned arithmetic.  */
10618 #define ADD16(a, b, n) \
10619   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10620 #define SUB16(a, b, n) \
10621   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10622 #define ADD8(a, b, n) \
10623   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10624 #define SUB8(a, b, n) \
10625   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10626 #define PFX uh
10627 
10628 #include "op_addsub.h"
10629 
10630 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10631 {
10632     if (a > b)
10633         return a - b;
10634     else
10635         return b - a;
10636 }
10637 
10638 /* Unsigned sum of absolute byte differences.  */
10639 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10640 {
10641     uint32_t sum;
10642     sum = do_usad(a, b);
10643     sum += do_usad(a >> 8, b >> 8);
10644     sum += do_usad(a >> 16, b >> 16);
10645     sum += do_usad(a >> 24, b >> 24);
10646     return sum;
10647 }
10648 
10649 /* For ARMv6 SEL instruction.  */
10650 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10651 {
10652     uint32_t mask;
10653 
10654     mask = 0;
10655     if (flags & 1)
10656         mask |= 0xff;
10657     if (flags & 2)
10658         mask |= 0xff00;
10659     if (flags & 4)
10660         mask |= 0xff0000;
10661     if (flags & 8)
10662         mask |= 0xff000000;
10663     return (a & mask) | (b & ~mask);
10664 }
10665 
10666 /* CRC helpers.
10667  * The upper bytes of val (above the number specified by 'bytes') must have
10668  * been zeroed out by the caller.
10669  */
10670 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10671 {
10672     uint8_t buf[4];
10673 
10674     stl_le_p(buf, val);
10675 
10676     /* zlib crc32 converts the accumulator and output to one's complement.  */
10677     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10678 }
10679 
10680 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10681 {
10682     uint8_t buf[4];
10683 
10684     stl_le_p(buf, val);
10685 
10686     /* Linux crc32c converts the output to one's complement.  */
10687     return crc32c(acc, buf, bytes) ^ 0xffffffff;
10688 }
10689 
10690 /* Return the exception level to which FP-disabled exceptions should
10691  * be taken, or 0 if FP is enabled.
10692  */
10693 int fp_exception_el(CPUARMState *env, int cur_el)
10694 {
10695 #ifndef CONFIG_USER_ONLY
10696     uint64_t hcr_el2;
10697 
10698     /* CPACR and the CPTR registers don't exist before v6, so FP is
10699      * always accessible
10700      */
10701     if (!arm_feature(env, ARM_FEATURE_V6)) {
10702         return 0;
10703     }
10704 
10705     if (arm_feature(env, ARM_FEATURE_M)) {
10706         /* CPACR can cause a NOCP UsageFault taken to current security state */
10707         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10708             return 1;
10709         }
10710 
10711         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10712             if (!extract32(env->v7m.nsacr, 10, 1)) {
10713                 /* FP insns cause a NOCP UsageFault taken to Secure */
10714                 return 3;
10715             }
10716         }
10717 
10718         return 0;
10719     }
10720 
10721     hcr_el2 = arm_hcr_el2_eff(env);
10722 
10723     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10724      * 0, 2 : trap EL0 and EL1/PL1 accesses
10725      * 1    : trap only EL0 accesses
10726      * 3    : trap no accesses
10727      * This register is ignored if E2H+TGE are both set.
10728      */
10729     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10730         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10731 
10732         switch (fpen) {
10733         case 1:
10734             if (cur_el != 0) {
10735                 break;
10736             }
10737             /* fall through */
10738         case 0:
10739         case 2:
10740             /* Trap from Secure PL0 or PL1 to Secure PL1. */
10741             if (!arm_el_is_aa64(env, 3)
10742                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10743                 return 3;
10744             }
10745             if (cur_el <= 1) {
10746                 return 1;
10747             }
10748             break;
10749         }
10750     }
10751 
10752     /*
10753      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10754      * to control non-secure access to the FPU. It doesn't have any
10755      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10756      */
10757     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10758          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10759         if (!extract32(env->cp15.nsacr, 10, 1)) {
10760             /* FP insns act as UNDEF */
10761             return cur_el == 2 ? 2 : 1;
10762         }
10763     }
10764 
10765     /*
10766      * CPTR_EL2 is present in v7VE or v8, and changes format
10767      * with HCR_EL2.E2H (regardless of TGE).
10768      */
10769     if (cur_el <= 2) {
10770         if (hcr_el2 & HCR_E2H) {
10771             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10772             case 1:
10773                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10774                     break;
10775                 }
10776                 /* fall through */
10777             case 0:
10778             case 2:
10779                 return 2;
10780             }
10781         } else if (arm_is_el2_enabled(env)) {
10782             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10783                 return 2;
10784             }
10785         }
10786     }
10787 
10788     /* CPTR_EL3 : present in v8 */
10789     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10790         /* Trap all FP ops to EL3 */
10791         return 3;
10792     }
10793 #endif
10794     return 0;
10795 }
10796 
10797 /* Return the exception level we're running at if this is our mmu_idx */
10798 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10799 {
10800     if (mmu_idx & ARM_MMU_IDX_M) {
10801         return mmu_idx & ARM_MMU_IDX_M_PRIV;
10802     }
10803 
10804     switch (mmu_idx) {
10805     case ARMMMUIdx_E10_0:
10806     case ARMMMUIdx_E20_0:
10807     case ARMMMUIdx_SE10_0:
10808     case ARMMMUIdx_SE20_0:
10809         return 0;
10810     case ARMMMUIdx_E10_1:
10811     case ARMMMUIdx_E10_1_PAN:
10812     case ARMMMUIdx_SE10_1:
10813     case ARMMMUIdx_SE10_1_PAN:
10814         return 1;
10815     case ARMMMUIdx_E2:
10816     case ARMMMUIdx_E20_2:
10817     case ARMMMUIdx_E20_2_PAN:
10818     case ARMMMUIdx_SE2:
10819     case ARMMMUIdx_SE20_2:
10820     case ARMMMUIdx_SE20_2_PAN:
10821         return 2;
10822     case ARMMMUIdx_SE3:
10823         return 3;
10824     default:
10825         g_assert_not_reached();
10826     }
10827 }
10828 
10829 #ifndef CONFIG_TCG
10830 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10831 {
10832     g_assert_not_reached();
10833 }
10834 #endif
10835 
10836 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10837 {
10838     ARMMMUIdx idx;
10839     uint64_t hcr;
10840 
10841     if (arm_feature(env, ARM_FEATURE_M)) {
10842         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10843     }
10844 
10845     /* See ARM pseudo-function ELIsInHost.  */
10846     switch (el) {
10847     case 0:
10848         hcr = arm_hcr_el2_eff(env);
10849         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10850             idx = ARMMMUIdx_E20_0;
10851         } else {
10852             idx = ARMMMUIdx_E10_0;
10853         }
10854         break;
10855     case 1:
10856         if (env->pstate & PSTATE_PAN) {
10857             idx = ARMMMUIdx_E10_1_PAN;
10858         } else {
10859             idx = ARMMMUIdx_E10_1;
10860         }
10861         break;
10862     case 2:
10863         /* Note that TGE does not apply at EL2.  */
10864         if (arm_hcr_el2_eff(env) & HCR_E2H) {
10865             if (env->pstate & PSTATE_PAN) {
10866                 idx = ARMMMUIdx_E20_2_PAN;
10867             } else {
10868                 idx = ARMMMUIdx_E20_2;
10869             }
10870         } else {
10871             idx = ARMMMUIdx_E2;
10872         }
10873         break;
10874     case 3:
10875         return ARMMMUIdx_SE3;
10876     default:
10877         g_assert_not_reached();
10878     }
10879 
10880     if (arm_is_secure_below_el3(env)) {
10881         idx &= ~ARM_MMU_IDX_A_NS;
10882     }
10883 
10884     return idx;
10885 }
10886 
10887 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10888 {
10889     return arm_mmu_idx_el(env, arm_current_el(env));
10890 }
10891 
10892 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10893                                            ARMMMUIdx mmu_idx,
10894                                            CPUARMTBFlags flags)
10895 {
10896     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10897     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
10898 
10899     if (arm_singlestep_active(env)) {
10900         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
10901     }
10902     return flags;
10903 }
10904 
10905 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10906                                               ARMMMUIdx mmu_idx,
10907                                               CPUARMTBFlags flags)
10908 {
10909     bool sctlr_b = arm_sctlr_b(env);
10910 
10911     if (sctlr_b) {
10912         DP_TBFLAG_A32(flags, SCTLR__B, 1);
10913     }
10914     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
10915         DP_TBFLAG_ANY(flags, BE_DATA, 1);
10916     }
10917     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
10918 
10919     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
10920 }
10921 
10922 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
10923                                         ARMMMUIdx mmu_idx)
10924 {
10925     CPUARMTBFlags flags = {};
10926     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
10927 
10928     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
10929     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
10930         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10931     }
10932 
10933     if (arm_v7m_is_handler_mode(env)) {
10934         DP_TBFLAG_M32(flags, HANDLER, 1);
10935     }
10936 
10937     /*
10938      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
10939      * is suppressing them because the requested execution priority
10940      * is less than 0.
10941      */
10942     if (arm_feature(env, ARM_FEATURE_V8) &&
10943         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
10944           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
10945         DP_TBFLAG_M32(flags, STACKCHECK, 1);
10946     }
10947 
10948     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10949 }
10950 
10951 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
10952                                         ARMMMUIdx mmu_idx)
10953 {
10954     CPUARMTBFlags flags = {};
10955     int el = arm_current_el(env);
10956 
10957     if (arm_sctlr(env, el) & SCTLR_A) {
10958         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10959     }
10960 
10961     if (arm_el_is_aa64(env, 1)) {
10962         DP_TBFLAG_A32(flags, VFPEN, 1);
10963     }
10964 
10965     if (el < 2 && env->cp15.hstr_el2 &&
10966         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10967         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
10968     }
10969 
10970     if (env->uncached_cpsr & CPSR_IL) {
10971         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
10972     }
10973 
10974     /*
10975      * The SME exception we are testing for is raised via
10976      * AArch64.CheckFPAdvSIMDEnabled(), as called from
10977      * AArch32.CheckAdvSIMDOrFPEnabled().
10978      */
10979     if (el == 0
10980         && FIELD_EX64(env->svcr, SVCR, SM)
10981         && (!arm_is_el2_enabled(env)
10982             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
10983         && arm_el_is_aa64(env, 1)
10984         && !sme_fa64(env, el)) {
10985         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
10986     }
10987 
10988     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10989 }
10990 
10991 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
10992                                         ARMMMUIdx mmu_idx)
10993 {
10994     CPUARMTBFlags flags = {};
10995     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
10996     uint64_t tcr = regime_tcr(env, mmu_idx);
10997     uint64_t sctlr;
10998     int tbii, tbid;
10999 
11000     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11001 
11002     /* Get control bits for tagged addresses.  */
11003     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11004     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11005 
11006     DP_TBFLAG_A64(flags, TBII, tbii);
11007     DP_TBFLAG_A64(flags, TBID, tbid);
11008 
11009     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11010         int sve_el = sve_exception_el(env, el);
11011 
11012         /*
11013          * If either FP or SVE are disabled, translator does not need len.
11014          * If SVE EL > FP EL, FP exception has precedence, and translator
11015          * does not need SVE EL.  Save potential re-translations by forcing
11016          * the unneeded data to zero.
11017          */
11018         if (fp_el != 0) {
11019             if (sve_el > fp_el) {
11020                 sve_el = 0;
11021             }
11022         } else if (sve_el == 0) {
11023             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11024         }
11025         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11026     }
11027     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11028         int sme_el = sme_exception_el(env, el);
11029         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11030 
11031         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11032         if (sme_el == 0) {
11033             /* Similarly, do not compute SVL if SME is disabled. */
11034             int svl = sve_vqm1_for_el_sm(env, el, true);
11035             DP_TBFLAG_A64(flags, SVL, svl);
11036             if (sm) {
11037                 /* If SVE is disabled, we will not have set VL above. */
11038                 DP_TBFLAG_A64(flags, VL, svl);
11039             }
11040         }
11041         if (sm) {
11042             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11043             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11044         }
11045         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11046     }
11047 
11048     sctlr = regime_sctlr(env, stage1);
11049 
11050     if (sctlr & SCTLR_A) {
11051         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11052     }
11053 
11054     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11055         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11056     }
11057 
11058     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11059         /*
11060          * In order to save space in flags, we record only whether
11061          * pauth is "inactive", meaning all insns are implemented as
11062          * a nop, or "active" when some action must be performed.
11063          * The decision of which action to take is left to a helper.
11064          */
11065         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11066             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11067         }
11068     }
11069 
11070     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11071         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11072         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11073             DP_TBFLAG_A64(flags, BT, 1);
11074         }
11075     }
11076 
11077     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11078     if (!(env->pstate & PSTATE_UAO)) {
11079         switch (mmu_idx) {
11080         case ARMMMUIdx_E10_1:
11081         case ARMMMUIdx_E10_1_PAN:
11082         case ARMMMUIdx_SE10_1:
11083         case ARMMMUIdx_SE10_1_PAN:
11084             /* TODO: ARMv8.3-NV */
11085             DP_TBFLAG_A64(flags, UNPRIV, 1);
11086             break;
11087         case ARMMMUIdx_E20_2:
11088         case ARMMMUIdx_E20_2_PAN:
11089         case ARMMMUIdx_SE20_2:
11090         case ARMMMUIdx_SE20_2_PAN:
11091             /*
11092              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11093              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11094              */
11095             if (env->cp15.hcr_el2 & HCR_TGE) {
11096                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11097             }
11098             break;
11099         default:
11100             break;
11101         }
11102     }
11103 
11104     if (env->pstate & PSTATE_IL) {
11105         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11106     }
11107 
11108     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11109         /*
11110          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11111          * if all accesses must be Unchecked:
11112          * 1) If no TBI, then there are no tags in the address to check,
11113          * 2) If Tag Check Override, then all accesses are Unchecked,
11114          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11115          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11116          */
11117         if (allocation_tag_access_enabled(env, el, sctlr)) {
11118             DP_TBFLAG_A64(flags, ATA, 1);
11119             if (tbid
11120                 && !(env->pstate & PSTATE_TCO)
11121                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11122                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11123             }
11124         }
11125         /* And again for unprivileged accesses, if required.  */
11126         if (EX_TBFLAG_A64(flags, UNPRIV)
11127             && tbid
11128             && !(env->pstate & PSTATE_TCO)
11129             && (sctlr & SCTLR_TCF0)
11130             && allocation_tag_access_enabled(env, 0, sctlr)) {
11131             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11132         }
11133         /* Cache TCMA as well as TBI. */
11134         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11135     }
11136 
11137     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11138 }
11139 
11140 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11141 {
11142     int el = arm_current_el(env);
11143     int fp_el = fp_exception_el(env, el);
11144     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11145 
11146     if (is_a64(env)) {
11147         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11148     } else if (arm_feature(env, ARM_FEATURE_M)) {
11149         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11150     } else {
11151         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11152     }
11153 }
11154 
11155 void arm_rebuild_hflags(CPUARMState *env)
11156 {
11157     env->hflags = rebuild_hflags_internal(env);
11158 }
11159 
11160 /*
11161  * If we have triggered a EL state change we can't rely on the
11162  * translator having passed it to us, we need to recompute.
11163  */
11164 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11165 {
11166     int el = arm_current_el(env);
11167     int fp_el = fp_exception_el(env, el);
11168     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11169 
11170     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11171 }
11172 
11173 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11174 {
11175     int fp_el = fp_exception_el(env, el);
11176     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11177 
11178     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11179 }
11180 
11181 /*
11182  * If we have triggered a EL state change we can't rely on the
11183  * translator having passed it to us, we need to recompute.
11184  */
11185 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11186 {
11187     int el = arm_current_el(env);
11188     int fp_el = fp_exception_el(env, el);
11189     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11190     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11191 }
11192 
11193 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11194 {
11195     int fp_el = fp_exception_el(env, el);
11196     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11197 
11198     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11199 }
11200 
11201 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11202 {
11203     int fp_el = fp_exception_el(env, el);
11204     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11205 
11206     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11207 }
11208 
11209 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11210 {
11211 #ifdef CONFIG_DEBUG_TCG
11212     CPUARMTBFlags c = env->hflags;
11213     CPUARMTBFlags r = rebuild_hflags_internal(env);
11214 
11215     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11216         fprintf(stderr, "TCG hflags mismatch "
11217                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11218                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11219                 c.flags, c.flags2, r.flags, r.flags2);
11220         abort();
11221     }
11222 #endif
11223 }
11224 
11225 static bool mve_no_pred(CPUARMState *env)
11226 {
11227     /*
11228      * Return true if there is definitely no predication of MVE
11229      * instructions by VPR or LTPSIZE. (Returning false even if there
11230      * isn't any predication is OK; generated code will just be
11231      * a little worse.)
11232      * If the CPU does not implement MVE then this TB flag is always 0.
11233      *
11234      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11235      * logic in gen_update_fp_context() needs to be updated to match.
11236      *
11237      * We do not include the effect of the ECI bits here -- they are
11238      * tracked in other TB flags. This simplifies the logic for
11239      * "when did we emit code that changes the MVE_NO_PRED TB flag
11240      * and thus need to end the TB?".
11241      */
11242     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11243         return false;
11244     }
11245     if (env->v7m.vpr) {
11246         return false;
11247     }
11248     if (env->v7m.ltpsize < 4) {
11249         return false;
11250     }
11251     return true;
11252 }
11253 
11254 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11255                           target_ulong *cs_base, uint32_t *pflags)
11256 {
11257     CPUARMTBFlags flags;
11258 
11259     assert_hflags_rebuild_correctly(env);
11260     flags = env->hflags;
11261 
11262     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11263         *pc = env->pc;
11264         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11265             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11266         }
11267     } else {
11268         *pc = env->regs[15];
11269 
11270         if (arm_feature(env, ARM_FEATURE_M)) {
11271             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11272                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11273                 != env->v7m.secure) {
11274                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11275             }
11276 
11277             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11278                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11279                  (env->v7m.secure &&
11280                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11281                 /*
11282                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11283                  * active FP context; we must create a new FP context before
11284                  * executing any FP insn.
11285                  */
11286                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11287             }
11288 
11289             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11290             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11291                 DP_TBFLAG_M32(flags, LSPACT, 1);
11292             }
11293 
11294             if (mve_no_pred(env)) {
11295                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11296             }
11297         } else {
11298             /*
11299              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11300              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11301              */
11302             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11303                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11304             } else {
11305                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11306                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11307             }
11308             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11309                 DP_TBFLAG_A32(flags, VFPEN, 1);
11310             }
11311         }
11312 
11313         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11314         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11315     }
11316 
11317     /*
11318      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11319      * states defined in the ARM ARM for software singlestep:
11320      *  SS_ACTIVE   PSTATE.SS   State
11321      *     0            x       Inactive (the TB flag for SS is always 0)
11322      *     1            0       Active-pending
11323      *     1            1       Active-not-pending
11324      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11325      */
11326     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11327         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11328     }
11329 
11330     *pflags = flags.flags;
11331     *cs_base = flags.flags2;
11332 }
11333 
11334 #ifdef TARGET_AARCH64
11335 /*
11336  * The manual says that when SVE is enabled and VQ is widened the
11337  * implementation is allowed to zero the previously inaccessible
11338  * portion of the registers.  The corollary to that is that when
11339  * SVE is enabled and VQ is narrowed we are also allowed to zero
11340  * the now inaccessible portion of the registers.
11341  *
11342  * The intent of this is that no predicate bit beyond VQ is ever set.
11343  * Which means that some operations on predicate registers themselves
11344  * may operate on full uint64_t or even unrolled across the maximum
11345  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11346  * may well be cheaper than conditionals to restrict the operation
11347  * to the relevant portion of a uint16_t[16].
11348  */
11349 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11350 {
11351     int i, j;
11352     uint64_t pmask;
11353 
11354     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11355     assert(vq <= env_archcpu(env)->sve_max_vq);
11356 
11357     /* Zap the high bits of the zregs.  */
11358     for (i = 0; i < 32; i++) {
11359         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11360     }
11361 
11362     /* Zap the high bits of the pregs and ffr.  */
11363     pmask = 0;
11364     if (vq & 3) {
11365         pmask = ~(-1ULL << (16 * (vq & 3)));
11366     }
11367     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11368         for (i = 0; i < 17; ++i) {
11369             env->vfp.pregs[i].p[j] &= pmask;
11370         }
11371         pmask = 0;
11372     }
11373 }
11374 
11375 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11376 {
11377     int exc_el;
11378 
11379     if (sm) {
11380         exc_el = sme_exception_el(env, el);
11381     } else {
11382         exc_el = sve_exception_el(env, el);
11383     }
11384     if (exc_el) {
11385         return 0; /* disabled */
11386     }
11387     return sve_vqm1_for_el_sm(env, el, sm);
11388 }
11389 
11390 /*
11391  * Notice a change in SVE vector size when changing EL.
11392  */
11393 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11394                            int new_el, bool el0_a64)
11395 {
11396     ARMCPU *cpu = env_archcpu(env);
11397     int old_len, new_len;
11398     bool old_a64, new_a64, sm;
11399 
11400     /* Nothing to do if no SVE.  */
11401     if (!cpu_isar_feature(aa64_sve, cpu)) {
11402         return;
11403     }
11404 
11405     /* Nothing to do if FP is disabled in either EL.  */
11406     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11407         return;
11408     }
11409 
11410     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11411     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11412 
11413     /*
11414      * Both AArch64.TakeException and AArch64.ExceptionReturn
11415      * invoke ResetSVEState when taking an exception from, or
11416      * returning to, AArch32 state when PSTATE.SM is enabled.
11417      */
11418     sm = FIELD_EX64(env->svcr, SVCR, SM);
11419     if (old_a64 != new_a64 && sm) {
11420         arm_reset_sve_state(env);
11421         return;
11422     }
11423 
11424     /*
11425      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11426      * at ELx, or not available because the EL is in AArch32 state, then
11427      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11428      * has an effective value of 0".
11429      *
11430      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11431      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11432      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11433      * we already have the correct register contents when encountering the
11434      * vq0->vq0 transition between EL0->EL1.
11435      */
11436     old_len = new_len = 0;
11437     if (old_a64) {
11438         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11439     }
11440     if (new_a64) {
11441         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11442     }
11443 
11444     /* When changing vector length, clear inaccessible state.  */
11445     if (new_len < old_len) {
11446         aarch64_sve_narrow_vq(env, new_len + 1);
11447     }
11448 }
11449 #endif
11450